iser_verbs.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. /*
  2. * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved.
  3. * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.
  4. * Copyright (c) 2013-2014 Mellanox Technologies. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/slab.h>
  37. #include <linux/delay.h>
  38. #include "iscsi_iser.h"
  39. #define ISCSI_ISER_MAX_CONN 8
  40. #define ISER_MAX_RX_LEN (ISER_QP_MAX_RECV_DTOS * ISCSI_ISER_MAX_CONN)
  41. #define ISER_MAX_TX_LEN (ISER_QP_MAX_REQ_DTOS * ISCSI_ISER_MAX_CONN)
  42. #define ISER_MAX_CQ_LEN (ISER_MAX_RX_LEN + ISER_MAX_TX_LEN + \
  43. ISCSI_ISER_MAX_CONN)
  44. static void iser_qp_event_callback(struct ib_event *cause, void *context)
  45. {
  46. iser_err("qp event %s (%d)\n",
  47. ib_event_msg(cause->event), cause->event);
  48. }
  49. static void iser_event_handler(struct ib_event_handler *handler,
  50. struct ib_event *event)
  51. {
  52. iser_err("async event %s (%d) on device %s port %d\n",
  53. ib_event_msg(event->event), event->event,
  54. event->device->name, event->element.port_num);
  55. }
  56. /**
  57. * iser_create_device_ib_res - creates Protection Domain (PD), Completion
  58. * Queue (CQ), DMA Memory Region (DMA MR) with the device associated with
  59. * the adapator.
  60. *
  61. * returns 0 on success, -1 on failure
  62. */
  63. static int iser_create_device_ib_res(struct iser_device *device)
  64. {
  65. struct ib_device *ib_dev = device->ib_device;
  66. int ret, i, max_cqe;
  67. ret = iser_assign_reg_ops(device);
  68. if (ret)
  69. return ret;
  70. device->comps_used = min_t(int, num_online_cpus(),
  71. ib_dev->num_comp_vectors);
  72. device->comps = kcalloc(device->comps_used, sizeof(*device->comps),
  73. GFP_KERNEL);
  74. if (!device->comps)
  75. goto comps_err;
  76. max_cqe = min(ISER_MAX_CQ_LEN, ib_dev->attrs.max_cqe);
  77. iser_info("using %d CQs, device %s supports %d vectors max_cqe %d\n",
  78. device->comps_used, ib_dev->name,
  79. ib_dev->num_comp_vectors, max_cqe);
  80. device->pd = ib_alloc_pd(ib_dev,
  81. iser_always_reg ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
  82. if (IS_ERR(device->pd))
  83. goto pd_err;
  84. for (i = 0; i < device->comps_used; i++) {
  85. struct iser_comp *comp = &device->comps[i];
  86. comp->cq = ib_alloc_cq(ib_dev, comp, max_cqe, i,
  87. IB_POLL_SOFTIRQ);
  88. if (IS_ERR(comp->cq)) {
  89. comp->cq = NULL;
  90. goto cq_err;
  91. }
  92. }
  93. INIT_IB_EVENT_HANDLER(&device->event_handler, ib_dev,
  94. iser_event_handler);
  95. if (ib_register_event_handler(&device->event_handler))
  96. goto cq_err;
  97. return 0;
  98. cq_err:
  99. for (i = 0; i < device->comps_used; i++) {
  100. struct iser_comp *comp = &device->comps[i];
  101. if (comp->cq)
  102. ib_free_cq(comp->cq);
  103. }
  104. ib_dealloc_pd(device->pd);
  105. pd_err:
  106. kfree(device->comps);
  107. comps_err:
  108. iser_err("failed to allocate an IB resource\n");
  109. return -1;
  110. }
  111. /**
  112. * iser_free_device_ib_res - destroy/dealloc/dereg the DMA MR,
  113. * CQ and PD created with the device associated with the adapator.
  114. */
  115. static void iser_free_device_ib_res(struct iser_device *device)
  116. {
  117. int i;
  118. for (i = 0; i < device->comps_used; i++) {
  119. struct iser_comp *comp = &device->comps[i];
  120. ib_free_cq(comp->cq);
  121. comp->cq = NULL;
  122. }
  123. (void)ib_unregister_event_handler(&device->event_handler);
  124. ib_dealloc_pd(device->pd);
  125. kfree(device->comps);
  126. device->comps = NULL;
  127. device->pd = NULL;
  128. }
  129. /**
  130. * iser_alloc_fmr_pool - Creates FMR pool and page_vector
  131. *
  132. * returns 0 on success, or errno code on failure
  133. */
  134. int iser_alloc_fmr_pool(struct ib_conn *ib_conn,
  135. unsigned cmds_max,
  136. unsigned int size)
  137. {
  138. struct iser_device *device = ib_conn->device;
  139. struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
  140. struct iser_page_vec *page_vec;
  141. struct iser_fr_desc *desc;
  142. struct ib_fmr_pool *fmr_pool;
  143. struct ib_fmr_pool_param params;
  144. int ret;
  145. INIT_LIST_HEAD(&fr_pool->list);
  146. spin_lock_init(&fr_pool->lock);
  147. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  148. if (!desc)
  149. return -ENOMEM;
  150. page_vec = kmalloc(sizeof(*page_vec) + (sizeof(u64) * size),
  151. GFP_KERNEL);
  152. if (!page_vec) {
  153. ret = -ENOMEM;
  154. goto err_frpl;
  155. }
  156. page_vec->pages = (u64 *)(page_vec + 1);
  157. params.page_shift = SHIFT_4K;
  158. params.max_pages_per_fmr = size;
  159. /* make the pool size twice the max number of SCSI commands *
  160. * the ML is expected to queue, watermark for unmap at 50% */
  161. params.pool_size = cmds_max * 2;
  162. params.dirty_watermark = cmds_max;
  163. params.cache = 0;
  164. params.flush_function = NULL;
  165. params.access = (IB_ACCESS_LOCAL_WRITE |
  166. IB_ACCESS_REMOTE_WRITE |
  167. IB_ACCESS_REMOTE_READ);
  168. fmr_pool = ib_create_fmr_pool(device->pd, &params);
  169. if (IS_ERR(fmr_pool)) {
  170. ret = PTR_ERR(fmr_pool);
  171. iser_err("FMR allocation failed, err %d\n", ret);
  172. goto err_fmr;
  173. }
  174. desc->rsc.page_vec = page_vec;
  175. desc->rsc.fmr_pool = fmr_pool;
  176. list_add(&desc->list, &fr_pool->list);
  177. return 0;
  178. err_fmr:
  179. kfree(page_vec);
  180. err_frpl:
  181. kfree(desc);
  182. return ret;
  183. }
  184. /**
  185. * iser_free_fmr_pool - releases the FMR pool and page vec
  186. */
  187. void iser_free_fmr_pool(struct ib_conn *ib_conn)
  188. {
  189. struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
  190. struct iser_fr_desc *desc;
  191. desc = list_first_entry(&fr_pool->list,
  192. struct iser_fr_desc, list);
  193. list_del(&desc->list);
  194. iser_info("freeing conn %p fmr pool %p\n",
  195. ib_conn, desc->rsc.fmr_pool);
  196. ib_destroy_fmr_pool(desc->rsc.fmr_pool);
  197. kfree(desc->rsc.page_vec);
  198. kfree(desc);
  199. }
  200. static int
  201. iser_alloc_reg_res(struct iser_device *device,
  202. struct ib_pd *pd,
  203. struct iser_reg_resources *res,
  204. unsigned int size)
  205. {
  206. struct ib_device *ib_dev = device->ib_device;
  207. enum ib_mr_type mr_type;
  208. int ret;
  209. if (ib_dev->attrs.device_cap_flags & IB_DEVICE_SG_GAPS_REG)
  210. mr_type = IB_MR_TYPE_SG_GAPS;
  211. else
  212. mr_type = IB_MR_TYPE_MEM_REG;
  213. res->mr = ib_alloc_mr(pd, mr_type, size);
  214. if (IS_ERR(res->mr)) {
  215. ret = PTR_ERR(res->mr);
  216. iser_err("Failed to allocate ib_fast_reg_mr err=%d\n", ret);
  217. return ret;
  218. }
  219. res->mr_valid = 0;
  220. return 0;
  221. }
  222. static void
  223. iser_free_reg_res(struct iser_reg_resources *rsc)
  224. {
  225. ib_dereg_mr(rsc->mr);
  226. }
  227. static int
  228. iser_alloc_pi_ctx(struct iser_device *device,
  229. struct ib_pd *pd,
  230. struct iser_fr_desc *desc,
  231. unsigned int size)
  232. {
  233. struct iser_pi_context *pi_ctx = NULL;
  234. int ret;
  235. desc->pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
  236. if (!desc->pi_ctx)
  237. return -ENOMEM;
  238. pi_ctx = desc->pi_ctx;
  239. ret = iser_alloc_reg_res(device, pd, &pi_ctx->rsc, size);
  240. if (ret) {
  241. iser_err("failed to allocate reg_resources\n");
  242. goto alloc_reg_res_err;
  243. }
  244. pi_ctx->sig_mr = ib_alloc_mr(pd, IB_MR_TYPE_SIGNATURE, 2);
  245. if (IS_ERR(pi_ctx->sig_mr)) {
  246. ret = PTR_ERR(pi_ctx->sig_mr);
  247. goto sig_mr_failure;
  248. }
  249. pi_ctx->sig_mr_valid = 0;
  250. desc->pi_ctx->sig_protected = 0;
  251. return 0;
  252. sig_mr_failure:
  253. iser_free_reg_res(&pi_ctx->rsc);
  254. alloc_reg_res_err:
  255. kfree(desc->pi_ctx);
  256. return ret;
  257. }
  258. static void
  259. iser_free_pi_ctx(struct iser_pi_context *pi_ctx)
  260. {
  261. iser_free_reg_res(&pi_ctx->rsc);
  262. ib_dereg_mr(pi_ctx->sig_mr);
  263. kfree(pi_ctx);
  264. }
  265. static struct iser_fr_desc *
  266. iser_create_fastreg_desc(struct iser_device *device,
  267. struct ib_pd *pd,
  268. bool pi_enable,
  269. unsigned int size)
  270. {
  271. struct iser_fr_desc *desc;
  272. int ret;
  273. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  274. if (!desc)
  275. return ERR_PTR(-ENOMEM);
  276. ret = iser_alloc_reg_res(device, pd, &desc->rsc, size);
  277. if (ret)
  278. goto reg_res_alloc_failure;
  279. if (pi_enable) {
  280. ret = iser_alloc_pi_ctx(device, pd, desc, size);
  281. if (ret)
  282. goto pi_ctx_alloc_failure;
  283. }
  284. return desc;
  285. pi_ctx_alloc_failure:
  286. iser_free_reg_res(&desc->rsc);
  287. reg_res_alloc_failure:
  288. kfree(desc);
  289. return ERR_PTR(ret);
  290. }
  291. /**
  292. * iser_alloc_fastreg_pool - Creates pool of fast_reg descriptors
  293. * for fast registration work requests.
  294. * returns 0 on success, or errno code on failure
  295. */
  296. int iser_alloc_fastreg_pool(struct ib_conn *ib_conn,
  297. unsigned cmds_max,
  298. unsigned int size)
  299. {
  300. struct iser_device *device = ib_conn->device;
  301. struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
  302. struct iser_fr_desc *desc;
  303. int i, ret;
  304. INIT_LIST_HEAD(&fr_pool->list);
  305. spin_lock_init(&fr_pool->lock);
  306. fr_pool->size = 0;
  307. for (i = 0; i < cmds_max; i++) {
  308. desc = iser_create_fastreg_desc(device, device->pd,
  309. ib_conn->pi_support, size);
  310. if (IS_ERR(desc)) {
  311. ret = PTR_ERR(desc);
  312. goto err;
  313. }
  314. list_add_tail(&desc->list, &fr_pool->list);
  315. fr_pool->size++;
  316. }
  317. return 0;
  318. err:
  319. iser_free_fastreg_pool(ib_conn);
  320. return ret;
  321. }
  322. /**
  323. * iser_free_fastreg_pool - releases the pool of fast_reg descriptors
  324. */
  325. void iser_free_fastreg_pool(struct ib_conn *ib_conn)
  326. {
  327. struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
  328. struct iser_fr_desc *desc, *tmp;
  329. int i = 0;
  330. if (list_empty(&fr_pool->list))
  331. return;
  332. iser_info("freeing conn %p fr pool\n", ib_conn);
  333. list_for_each_entry_safe(desc, tmp, &fr_pool->list, list) {
  334. list_del(&desc->list);
  335. iser_free_reg_res(&desc->rsc);
  336. if (desc->pi_ctx)
  337. iser_free_pi_ctx(desc->pi_ctx);
  338. kfree(desc);
  339. ++i;
  340. }
  341. if (i < fr_pool->size)
  342. iser_warn("pool still has %d regions registered\n",
  343. fr_pool->size - i);
  344. }
  345. /**
  346. * iser_create_ib_conn_res - Queue-Pair (QP)
  347. *
  348. * returns 0 on success, -1 on failure
  349. */
  350. static int iser_create_ib_conn_res(struct ib_conn *ib_conn)
  351. {
  352. struct iser_conn *iser_conn = to_iser_conn(ib_conn);
  353. struct iser_device *device;
  354. struct ib_device *ib_dev;
  355. struct ib_qp_init_attr init_attr;
  356. int ret = -ENOMEM;
  357. int index, min_index = 0;
  358. BUG_ON(ib_conn->device == NULL);
  359. device = ib_conn->device;
  360. ib_dev = device->ib_device;
  361. memset(&init_attr, 0, sizeof init_attr);
  362. mutex_lock(&ig.connlist_mutex);
  363. /* select the CQ with the minimal number of usages */
  364. for (index = 0; index < device->comps_used; index++) {
  365. if (device->comps[index].active_qps <
  366. device->comps[min_index].active_qps)
  367. min_index = index;
  368. }
  369. ib_conn->comp = &device->comps[min_index];
  370. ib_conn->comp->active_qps++;
  371. mutex_unlock(&ig.connlist_mutex);
  372. iser_info("cq index %d used for ib_conn %p\n", min_index, ib_conn);
  373. init_attr.event_handler = iser_qp_event_callback;
  374. init_attr.qp_context = (void *)ib_conn;
  375. init_attr.send_cq = ib_conn->comp->cq;
  376. init_attr.recv_cq = ib_conn->comp->cq;
  377. init_attr.cap.max_recv_wr = ISER_QP_MAX_RECV_DTOS;
  378. init_attr.cap.max_send_sge = 2;
  379. init_attr.cap.max_recv_sge = 1;
  380. init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  381. init_attr.qp_type = IB_QPT_RC;
  382. if (ib_conn->pi_support) {
  383. init_attr.cap.max_send_wr = ISER_QP_SIG_MAX_REQ_DTOS + 1;
  384. init_attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
  385. iser_conn->max_cmds =
  386. ISER_GET_MAX_XMIT_CMDS(ISER_QP_SIG_MAX_REQ_DTOS);
  387. } else {
  388. if (ib_dev->attrs.max_qp_wr > ISER_QP_MAX_REQ_DTOS) {
  389. init_attr.cap.max_send_wr = ISER_QP_MAX_REQ_DTOS + 1;
  390. iser_conn->max_cmds =
  391. ISER_GET_MAX_XMIT_CMDS(ISER_QP_MAX_REQ_DTOS);
  392. } else {
  393. init_attr.cap.max_send_wr = ib_dev->attrs.max_qp_wr;
  394. iser_conn->max_cmds =
  395. ISER_GET_MAX_XMIT_CMDS(ib_dev->attrs.max_qp_wr);
  396. iser_dbg("device %s supports max_send_wr %d\n",
  397. device->ib_device->name, ib_dev->attrs.max_qp_wr);
  398. }
  399. }
  400. ret = rdma_create_qp(ib_conn->cma_id, device->pd, &init_attr);
  401. if (ret)
  402. goto out_err;
  403. ib_conn->qp = ib_conn->cma_id->qp;
  404. iser_info("setting conn %p cma_id %p qp %p\n",
  405. ib_conn, ib_conn->cma_id,
  406. ib_conn->cma_id->qp);
  407. return ret;
  408. out_err:
  409. mutex_lock(&ig.connlist_mutex);
  410. ib_conn->comp->active_qps--;
  411. mutex_unlock(&ig.connlist_mutex);
  412. iser_err("unable to alloc mem or create resource, err %d\n", ret);
  413. return ret;
  414. }
  415. /**
  416. * based on the resolved device node GUID see if there already allocated
  417. * device for this device. If there's no such, create one.
  418. */
  419. static
  420. struct iser_device *iser_device_find_by_ib_device(struct rdma_cm_id *cma_id)
  421. {
  422. struct iser_device *device;
  423. mutex_lock(&ig.device_list_mutex);
  424. list_for_each_entry(device, &ig.device_list, ig_list)
  425. /* find if there's a match using the node GUID */
  426. if (device->ib_device->node_guid == cma_id->device->node_guid)
  427. goto inc_refcnt;
  428. device = kzalloc(sizeof *device, GFP_KERNEL);
  429. if (device == NULL)
  430. goto out;
  431. /* assign this device to the device */
  432. device->ib_device = cma_id->device;
  433. /* init the device and link it into ig device list */
  434. if (iser_create_device_ib_res(device)) {
  435. kfree(device);
  436. device = NULL;
  437. goto out;
  438. }
  439. list_add(&device->ig_list, &ig.device_list);
  440. inc_refcnt:
  441. device->refcount++;
  442. out:
  443. mutex_unlock(&ig.device_list_mutex);
  444. return device;
  445. }
  446. /* if there's no demand for this device, release it */
  447. static void iser_device_try_release(struct iser_device *device)
  448. {
  449. mutex_lock(&ig.device_list_mutex);
  450. device->refcount--;
  451. iser_info("device %p refcount %d\n", device, device->refcount);
  452. if (!device->refcount) {
  453. iser_free_device_ib_res(device);
  454. list_del(&device->ig_list);
  455. kfree(device);
  456. }
  457. mutex_unlock(&ig.device_list_mutex);
  458. }
  459. /**
  460. * Called with state mutex held
  461. **/
  462. static int iser_conn_state_comp_exch(struct iser_conn *iser_conn,
  463. enum iser_conn_state comp,
  464. enum iser_conn_state exch)
  465. {
  466. int ret;
  467. ret = (iser_conn->state == comp);
  468. if (ret)
  469. iser_conn->state = exch;
  470. return ret;
  471. }
  472. void iser_release_work(struct work_struct *work)
  473. {
  474. struct iser_conn *iser_conn;
  475. iser_conn = container_of(work, struct iser_conn, release_work);
  476. /* Wait for conn_stop to complete */
  477. wait_for_completion(&iser_conn->stop_completion);
  478. /* Wait for IB resouces cleanup to complete */
  479. wait_for_completion(&iser_conn->ib_completion);
  480. mutex_lock(&iser_conn->state_mutex);
  481. iser_conn->state = ISER_CONN_DOWN;
  482. mutex_unlock(&iser_conn->state_mutex);
  483. iser_conn_release(iser_conn);
  484. }
  485. /**
  486. * iser_free_ib_conn_res - release IB related resources
  487. * @iser_conn: iser connection struct
  488. * @destroy: indicator if we need to try to release the
  489. * iser device and memory regoins pool (only iscsi
  490. * shutdown and DEVICE_REMOVAL will use this).
  491. *
  492. * This routine is called with the iser state mutex held
  493. * so the cm_id removal is out of here. It is Safe to
  494. * be invoked multiple times.
  495. */
  496. static void iser_free_ib_conn_res(struct iser_conn *iser_conn,
  497. bool destroy)
  498. {
  499. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  500. struct iser_device *device = ib_conn->device;
  501. iser_info("freeing conn %p cma_id %p qp %p\n",
  502. iser_conn, ib_conn->cma_id, ib_conn->qp);
  503. if (ib_conn->qp != NULL) {
  504. ib_conn->comp->active_qps--;
  505. rdma_destroy_qp(ib_conn->cma_id);
  506. ib_conn->qp = NULL;
  507. }
  508. if (destroy) {
  509. if (iser_conn->rx_descs)
  510. iser_free_rx_descriptors(iser_conn);
  511. if (device != NULL) {
  512. iser_device_try_release(device);
  513. ib_conn->device = NULL;
  514. }
  515. }
  516. }
  517. /**
  518. * Frees all conn objects and deallocs conn descriptor
  519. */
  520. void iser_conn_release(struct iser_conn *iser_conn)
  521. {
  522. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  523. mutex_lock(&ig.connlist_mutex);
  524. list_del(&iser_conn->conn_list);
  525. mutex_unlock(&ig.connlist_mutex);
  526. mutex_lock(&iser_conn->state_mutex);
  527. /* In case we endup here without ep_disconnect being invoked. */
  528. if (iser_conn->state != ISER_CONN_DOWN) {
  529. iser_warn("iser conn %p state %d, expected state down.\n",
  530. iser_conn, iser_conn->state);
  531. iscsi_destroy_endpoint(iser_conn->ep);
  532. iser_conn->state = ISER_CONN_DOWN;
  533. }
  534. /*
  535. * In case we never got to bind stage, we still need to
  536. * release IB resources (which is safe to call more than once).
  537. */
  538. iser_free_ib_conn_res(iser_conn, true);
  539. mutex_unlock(&iser_conn->state_mutex);
  540. if (ib_conn->cma_id != NULL) {
  541. rdma_destroy_id(ib_conn->cma_id);
  542. ib_conn->cma_id = NULL;
  543. }
  544. kfree(iser_conn);
  545. }
  546. /**
  547. * triggers start of the disconnect procedures and wait for them to be done
  548. * Called with state mutex held
  549. */
  550. int iser_conn_terminate(struct iser_conn *iser_conn)
  551. {
  552. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  553. int err = 0;
  554. /* terminate the iser conn only if the conn state is UP */
  555. if (!iser_conn_state_comp_exch(iser_conn, ISER_CONN_UP,
  556. ISER_CONN_TERMINATING))
  557. return 0;
  558. iser_info("iser_conn %p state %d\n", iser_conn, iser_conn->state);
  559. /* suspend queuing of new iscsi commands */
  560. if (iser_conn->iscsi_conn)
  561. iscsi_suspend_queue(iser_conn->iscsi_conn);
  562. /*
  563. * In case we didn't already clean up the cma_id (peer initiated
  564. * a disconnection), we need to Cause the CMA to change the QP
  565. * state to ERROR.
  566. */
  567. if (ib_conn->cma_id) {
  568. err = rdma_disconnect(ib_conn->cma_id);
  569. if (err)
  570. iser_err("Failed to disconnect, conn: 0x%p err %d\n",
  571. iser_conn, err);
  572. /* block until all flush errors are consumed */
  573. ib_drain_sq(ib_conn->qp);
  574. }
  575. return 1;
  576. }
  577. /**
  578. * Called with state mutex held
  579. **/
  580. static void iser_connect_error(struct rdma_cm_id *cma_id)
  581. {
  582. struct iser_conn *iser_conn;
  583. iser_conn = (struct iser_conn *)cma_id->context;
  584. iser_conn->state = ISER_CONN_TERMINATING;
  585. }
  586. static void
  587. iser_calc_scsi_params(struct iser_conn *iser_conn,
  588. unsigned int max_sectors)
  589. {
  590. struct iser_device *device = iser_conn->ib_conn.device;
  591. unsigned short sg_tablesize, sup_sg_tablesize;
  592. sg_tablesize = DIV_ROUND_UP(max_sectors * 512, SIZE_4K);
  593. sup_sg_tablesize = min_t(unsigned, ISCSI_ISER_MAX_SG_TABLESIZE,
  594. device->ib_device->attrs.max_fast_reg_page_list_len);
  595. if (sg_tablesize > sup_sg_tablesize) {
  596. sg_tablesize = sup_sg_tablesize;
  597. iser_conn->scsi_max_sectors = sg_tablesize * SIZE_4K / 512;
  598. } else {
  599. iser_conn->scsi_max_sectors = max_sectors;
  600. }
  601. iser_conn->scsi_sg_tablesize = sg_tablesize;
  602. iser_dbg("iser_conn %p, sg_tablesize %u, max_sectors %u\n",
  603. iser_conn, iser_conn->scsi_sg_tablesize,
  604. iser_conn->scsi_max_sectors);
  605. }
  606. /**
  607. * Called with state mutex held
  608. **/
  609. static void iser_addr_handler(struct rdma_cm_id *cma_id)
  610. {
  611. struct iser_device *device;
  612. struct iser_conn *iser_conn;
  613. struct ib_conn *ib_conn;
  614. int ret;
  615. iser_conn = (struct iser_conn *)cma_id->context;
  616. if (iser_conn->state != ISER_CONN_PENDING)
  617. /* bailout */
  618. return;
  619. ib_conn = &iser_conn->ib_conn;
  620. device = iser_device_find_by_ib_device(cma_id);
  621. if (!device) {
  622. iser_err("device lookup/creation failed\n");
  623. iser_connect_error(cma_id);
  624. return;
  625. }
  626. ib_conn->device = device;
  627. /* connection T10-PI support */
  628. if (iser_pi_enable) {
  629. if (!(device->ib_device->attrs.device_cap_flags &
  630. IB_DEVICE_SIGNATURE_HANDOVER)) {
  631. iser_warn("T10-PI requested but not supported on %s, "
  632. "continue without T10-PI\n",
  633. ib_conn->device->ib_device->name);
  634. ib_conn->pi_support = false;
  635. } else {
  636. ib_conn->pi_support = true;
  637. }
  638. }
  639. iser_calc_scsi_params(iser_conn, iser_max_sectors);
  640. ret = rdma_resolve_route(cma_id, 1000);
  641. if (ret) {
  642. iser_err("resolve route failed: %d\n", ret);
  643. iser_connect_error(cma_id);
  644. return;
  645. }
  646. }
  647. /**
  648. * Called with state mutex held
  649. **/
  650. static void iser_route_handler(struct rdma_cm_id *cma_id)
  651. {
  652. struct rdma_conn_param conn_param;
  653. int ret;
  654. struct iser_cm_hdr req_hdr;
  655. struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
  656. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  657. struct iser_device *device = ib_conn->device;
  658. if (iser_conn->state != ISER_CONN_PENDING)
  659. /* bailout */
  660. return;
  661. ret = iser_create_ib_conn_res(ib_conn);
  662. if (ret)
  663. goto failure;
  664. memset(&conn_param, 0, sizeof conn_param);
  665. conn_param.responder_resources = device->ib_device->attrs.max_qp_rd_atom;
  666. conn_param.initiator_depth = 1;
  667. conn_param.retry_count = 7;
  668. conn_param.rnr_retry_count = 6;
  669. memset(&req_hdr, 0, sizeof(req_hdr));
  670. req_hdr.flags = ISER_ZBVA_NOT_SUP;
  671. if (!device->remote_inv_sup)
  672. req_hdr.flags |= ISER_SEND_W_INV_NOT_SUP;
  673. conn_param.private_data = (void *)&req_hdr;
  674. conn_param.private_data_len = sizeof(struct iser_cm_hdr);
  675. ret = rdma_connect(cma_id, &conn_param);
  676. if (ret) {
  677. iser_err("failure connecting: %d\n", ret);
  678. goto failure;
  679. }
  680. return;
  681. failure:
  682. iser_connect_error(cma_id);
  683. }
  684. static void iser_connected_handler(struct rdma_cm_id *cma_id,
  685. const void *private_data)
  686. {
  687. struct iser_conn *iser_conn;
  688. struct ib_qp_attr attr;
  689. struct ib_qp_init_attr init_attr;
  690. iser_conn = (struct iser_conn *)cma_id->context;
  691. if (iser_conn->state != ISER_CONN_PENDING)
  692. /* bailout */
  693. return;
  694. (void)ib_query_qp(cma_id->qp, &attr, ~0, &init_attr);
  695. iser_info("remote qpn:%x my qpn:%x\n", attr.dest_qp_num, cma_id->qp->qp_num);
  696. if (private_data) {
  697. u8 flags = *(u8 *)private_data;
  698. iser_conn->snd_w_inv = !(flags & ISER_SEND_W_INV_NOT_SUP);
  699. }
  700. iser_info("conn %p: negotiated %s invalidation\n",
  701. iser_conn, iser_conn->snd_w_inv ? "remote" : "local");
  702. iser_conn->state = ISER_CONN_UP;
  703. complete(&iser_conn->up_completion);
  704. }
  705. static void iser_disconnected_handler(struct rdma_cm_id *cma_id)
  706. {
  707. struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
  708. if (iser_conn_terminate(iser_conn)) {
  709. if (iser_conn->iscsi_conn)
  710. iscsi_conn_failure(iser_conn->iscsi_conn,
  711. ISCSI_ERR_CONN_FAILED);
  712. else
  713. iser_err("iscsi_iser connection isn't bound\n");
  714. }
  715. }
  716. static void iser_cleanup_handler(struct rdma_cm_id *cma_id,
  717. bool destroy)
  718. {
  719. struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
  720. /*
  721. * We are not guaranteed that we visited disconnected_handler
  722. * by now, call it here to be safe that we handle CM drep
  723. * and flush errors.
  724. */
  725. iser_disconnected_handler(cma_id);
  726. iser_free_ib_conn_res(iser_conn, destroy);
  727. complete(&iser_conn->ib_completion);
  728. };
  729. static int iser_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
  730. {
  731. struct iser_conn *iser_conn;
  732. int ret = 0;
  733. iser_conn = (struct iser_conn *)cma_id->context;
  734. iser_info("%s (%d): status %d conn %p id %p\n",
  735. rdma_event_msg(event->event), event->event,
  736. event->status, cma_id->context, cma_id);
  737. mutex_lock(&iser_conn->state_mutex);
  738. switch (event->event) {
  739. case RDMA_CM_EVENT_ADDR_RESOLVED:
  740. iser_addr_handler(cma_id);
  741. break;
  742. case RDMA_CM_EVENT_ROUTE_RESOLVED:
  743. iser_route_handler(cma_id);
  744. break;
  745. case RDMA_CM_EVENT_ESTABLISHED:
  746. iser_connected_handler(cma_id, event->param.conn.private_data);
  747. break;
  748. case RDMA_CM_EVENT_REJECTED:
  749. iser_info("Connection rejected: %s\n",
  750. rdma_reject_msg(cma_id, event->status));
  751. /* FALLTHROUGH */
  752. case RDMA_CM_EVENT_ADDR_ERROR:
  753. case RDMA_CM_EVENT_ROUTE_ERROR:
  754. case RDMA_CM_EVENT_CONNECT_ERROR:
  755. case RDMA_CM_EVENT_UNREACHABLE:
  756. iser_connect_error(cma_id);
  757. break;
  758. case RDMA_CM_EVENT_DISCONNECTED:
  759. case RDMA_CM_EVENT_ADDR_CHANGE:
  760. case RDMA_CM_EVENT_TIMEWAIT_EXIT:
  761. iser_cleanup_handler(cma_id, false);
  762. break;
  763. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  764. /*
  765. * we *must* destroy the device as we cannot rely
  766. * on iscsid to be around to initiate error handling.
  767. * also if we are not in state DOWN implicitly destroy
  768. * the cma_id.
  769. */
  770. iser_cleanup_handler(cma_id, true);
  771. if (iser_conn->state != ISER_CONN_DOWN) {
  772. iser_conn->ib_conn.cma_id = NULL;
  773. ret = 1;
  774. }
  775. break;
  776. default:
  777. iser_err("Unexpected RDMA CM event: %s (%d)\n",
  778. rdma_event_msg(event->event), event->event);
  779. break;
  780. }
  781. mutex_unlock(&iser_conn->state_mutex);
  782. return ret;
  783. }
  784. void iser_conn_init(struct iser_conn *iser_conn)
  785. {
  786. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  787. iser_conn->state = ISER_CONN_INIT;
  788. init_completion(&iser_conn->stop_completion);
  789. init_completion(&iser_conn->ib_completion);
  790. init_completion(&iser_conn->up_completion);
  791. INIT_LIST_HEAD(&iser_conn->conn_list);
  792. mutex_init(&iser_conn->state_mutex);
  793. ib_conn->post_recv_buf_count = 0;
  794. ib_conn->reg_cqe.done = iser_reg_comp;
  795. }
  796. /**
  797. * starts the process of connecting to the target
  798. * sleeps until the connection is established or rejected
  799. */
  800. int iser_connect(struct iser_conn *iser_conn,
  801. struct sockaddr *src_addr,
  802. struct sockaddr *dst_addr,
  803. int non_blocking)
  804. {
  805. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  806. int err = 0;
  807. mutex_lock(&iser_conn->state_mutex);
  808. sprintf(iser_conn->name, "%pISp", dst_addr);
  809. iser_info("connecting to: %s\n", iser_conn->name);
  810. /* the device is known only --after-- address resolution */
  811. ib_conn->device = NULL;
  812. iser_conn->state = ISER_CONN_PENDING;
  813. ib_conn->cma_id = rdma_create_id(&init_net, iser_cma_handler,
  814. (void *)iser_conn,
  815. RDMA_PS_TCP, IB_QPT_RC);
  816. if (IS_ERR(ib_conn->cma_id)) {
  817. err = PTR_ERR(ib_conn->cma_id);
  818. iser_err("rdma_create_id failed: %d\n", err);
  819. goto id_failure;
  820. }
  821. err = rdma_resolve_addr(ib_conn->cma_id, src_addr, dst_addr, 1000);
  822. if (err) {
  823. iser_err("rdma_resolve_addr failed: %d\n", err);
  824. goto addr_failure;
  825. }
  826. if (!non_blocking) {
  827. wait_for_completion_interruptible(&iser_conn->up_completion);
  828. if (iser_conn->state != ISER_CONN_UP) {
  829. err = -EIO;
  830. goto connect_failure;
  831. }
  832. }
  833. mutex_unlock(&iser_conn->state_mutex);
  834. mutex_lock(&ig.connlist_mutex);
  835. list_add(&iser_conn->conn_list, &ig.connlist);
  836. mutex_unlock(&ig.connlist_mutex);
  837. return 0;
  838. id_failure:
  839. ib_conn->cma_id = NULL;
  840. addr_failure:
  841. iser_conn->state = ISER_CONN_DOWN;
  842. connect_failure:
  843. mutex_unlock(&iser_conn->state_mutex);
  844. iser_conn_release(iser_conn);
  845. return err;
  846. }
  847. int iser_post_recvl(struct iser_conn *iser_conn)
  848. {
  849. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  850. struct iser_login_desc *desc = &iser_conn->login_desc;
  851. struct ib_recv_wr wr, *wr_failed;
  852. int ib_ret;
  853. desc->sge.addr = desc->rsp_dma;
  854. desc->sge.length = ISER_RX_LOGIN_SIZE;
  855. desc->sge.lkey = ib_conn->device->pd->local_dma_lkey;
  856. desc->cqe.done = iser_login_rsp;
  857. wr.wr_cqe = &desc->cqe;
  858. wr.sg_list = &desc->sge;
  859. wr.num_sge = 1;
  860. wr.next = NULL;
  861. ib_conn->post_recv_buf_count++;
  862. ib_ret = ib_post_recv(ib_conn->qp, &wr, &wr_failed);
  863. if (ib_ret) {
  864. iser_err("ib_post_recv failed ret=%d\n", ib_ret);
  865. ib_conn->post_recv_buf_count--;
  866. }
  867. return ib_ret;
  868. }
  869. int iser_post_recvm(struct iser_conn *iser_conn, int count)
  870. {
  871. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  872. unsigned int my_rx_head = iser_conn->rx_desc_head;
  873. struct iser_rx_desc *rx_desc;
  874. struct ib_recv_wr *wr, *wr_failed;
  875. int i, ib_ret;
  876. for (wr = ib_conn->rx_wr, i = 0; i < count; i++, wr++) {
  877. rx_desc = &iser_conn->rx_descs[my_rx_head];
  878. rx_desc->cqe.done = iser_task_rsp;
  879. wr->wr_cqe = &rx_desc->cqe;
  880. wr->sg_list = &rx_desc->rx_sg;
  881. wr->num_sge = 1;
  882. wr->next = wr + 1;
  883. my_rx_head = (my_rx_head + 1) & iser_conn->qp_max_recv_dtos_mask;
  884. }
  885. wr--;
  886. wr->next = NULL; /* mark end of work requests list */
  887. ib_conn->post_recv_buf_count += count;
  888. ib_ret = ib_post_recv(ib_conn->qp, ib_conn->rx_wr, &wr_failed);
  889. if (ib_ret) {
  890. iser_err("ib_post_recv failed ret=%d\n", ib_ret);
  891. ib_conn->post_recv_buf_count -= count;
  892. } else
  893. iser_conn->rx_desc_head = my_rx_head;
  894. return ib_ret;
  895. }
  896. /**
  897. * iser_start_send - Initiate a Send DTO operation
  898. *
  899. * returns 0 on success, -1 on failure
  900. */
  901. int iser_post_send(struct ib_conn *ib_conn, struct iser_tx_desc *tx_desc,
  902. bool signal)
  903. {
  904. struct ib_send_wr *bad_wr, *wr = iser_tx_next_wr(tx_desc);
  905. int ib_ret;
  906. ib_dma_sync_single_for_device(ib_conn->device->ib_device,
  907. tx_desc->dma_addr, ISER_HEADERS_LEN,
  908. DMA_TO_DEVICE);
  909. wr->next = NULL;
  910. wr->wr_cqe = &tx_desc->cqe;
  911. wr->sg_list = tx_desc->tx_sg;
  912. wr->num_sge = tx_desc->num_sge;
  913. wr->opcode = IB_WR_SEND;
  914. wr->send_flags = signal ? IB_SEND_SIGNALED : 0;
  915. ib_ret = ib_post_send(ib_conn->qp, &tx_desc->wrs[0].send, &bad_wr);
  916. if (ib_ret)
  917. iser_err("ib_post_send failed, ret:%d opcode:%d\n",
  918. ib_ret, bad_wr->opcode);
  919. return ib_ret;
  920. }
  921. u8 iser_check_task_pi_status(struct iscsi_iser_task *iser_task,
  922. enum iser_data_dir cmd_dir, sector_t *sector)
  923. {
  924. struct iser_mem_reg *reg = &iser_task->rdma_reg[cmd_dir];
  925. struct iser_fr_desc *desc = reg->mem_h;
  926. unsigned long sector_size = iser_task->sc->device->sector_size;
  927. struct ib_mr_status mr_status;
  928. int ret;
  929. if (desc && desc->pi_ctx->sig_protected) {
  930. desc->pi_ctx->sig_protected = 0;
  931. ret = ib_check_mr_status(desc->pi_ctx->sig_mr,
  932. IB_MR_CHECK_SIG_STATUS, &mr_status);
  933. if (ret) {
  934. pr_err("ib_check_mr_status failed, ret %d\n", ret);
  935. goto err;
  936. }
  937. if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
  938. sector_t sector_off = mr_status.sig_err.sig_err_offset;
  939. sector_div(sector_off, sector_size + 8);
  940. *sector = scsi_get_lba(iser_task->sc) + sector_off;
  941. pr_err("PI error found type %d at sector %llx "
  942. "expected %x vs actual %x\n",
  943. mr_status.sig_err.err_type,
  944. (unsigned long long)*sector,
  945. mr_status.sig_err.expected,
  946. mr_status.sig_err.actual);
  947. switch (mr_status.sig_err.err_type) {
  948. case IB_SIG_BAD_GUARD:
  949. return 0x1;
  950. case IB_SIG_BAD_REFTAG:
  951. return 0x3;
  952. case IB_SIG_BAD_APPTAG:
  953. return 0x2;
  954. }
  955. }
  956. }
  957. return 0;
  958. err:
  959. /* Not alot we can do here, return ambiguous guard error */
  960. return 0x1;
  961. }
  962. void iser_err_comp(struct ib_wc *wc, const char *type)
  963. {
  964. if (wc->status != IB_WC_WR_FLUSH_ERR) {
  965. struct iser_conn *iser_conn = to_iser_conn(wc->qp->qp_context);
  966. iser_err("%s failure: %s (%d) vend_err %x\n", type,
  967. ib_wc_status_msg(wc->status), wc->status,
  968. wc->vendor_err);
  969. if (iser_conn->iscsi_conn)
  970. iscsi_conn_failure(iser_conn->iscsi_conn,
  971. ISCSI_ERR_CONN_FAILED);
  972. } else {
  973. iser_dbg("%s failure: %s (%d)\n", type,
  974. ib_wc_status_msg(wc->status), wc->status);
  975. }
  976. }