iser_verbs.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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_CQ_LEN (ISER_QP_MAX_RECV_DTOS * ISCSI_ISER_MAX_CONN)
  41. #define ISER_MAX_TX_CQ_LEN (ISER_QP_MAX_REQ_DTOS * ISCSI_ISER_MAX_CONN)
  42. static void iser_cq_tasklet_fn(unsigned long data);
  43. static void iser_cq_callback(struct ib_cq *cq, void *cq_context);
  44. static void iser_cq_event_callback(struct ib_event *cause, void *context)
  45. {
  46. iser_err("got cq event %d \n", cause->event);
  47. }
  48. static void iser_qp_event_callback(struct ib_event *cause, void *context)
  49. {
  50. iser_err("got qp event %d\n",cause->event);
  51. }
  52. static void iser_event_handler(struct ib_event_handler *handler,
  53. struct ib_event *event)
  54. {
  55. iser_err("async event %d on device %s port %d\n", event->event,
  56. event->device->name, event->element.port_num);
  57. }
  58. /**
  59. * iser_create_device_ib_res - creates Protection Domain (PD), Completion
  60. * Queue (CQ), DMA Memory Region (DMA MR) with the device associated with
  61. * the adapator.
  62. *
  63. * returns 0 on success, -1 on failure
  64. */
  65. static int iser_create_device_ib_res(struct iser_device *device)
  66. {
  67. struct iser_cq_desc *cq_desc;
  68. struct ib_device_attr *dev_attr = &device->dev_attr;
  69. int ret, i;
  70. ret = ib_query_device(device->ib_device, dev_attr);
  71. if (ret) {
  72. pr_warn("Query device failed for %s\n", device->ib_device->name);
  73. return ret;
  74. }
  75. /* Assign function handles - based on FMR support */
  76. if (device->ib_device->alloc_fmr && device->ib_device->dealloc_fmr &&
  77. device->ib_device->map_phys_fmr && device->ib_device->unmap_fmr) {
  78. iser_info("FMR supported, using FMR for registration\n");
  79. device->iser_alloc_rdma_reg_res = iser_create_fmr_pool;
  80. device->iser_free_rdma_reg_res = iser_free_fmr_pool;
  81. device->iser_reg_rdma_mem = iser_reg_rdma_mem_fmr;
  82. device->iser_unreg_rdma_mem = iser_unreg_mem_fmr;
  83. } else
  84. if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
  85. iser_info("FastReg supported, using FastReg for registration\n");
  86. device->iser_alloc_rdma_reg_res = iser_create_fastreg_pool;
  87. device->iser_free_rdma_reg_res = iser_free_fastreg_pool;
  88. device->iser_reg_rdma_mem = iser_reg_rdma_mem_fastreg;
  89. device->iser_unreg_rdma_mem = iser_unreg_mem_fastreg;
  90. } else {
  91. iser_err("IB device does not support FMRs nor FastRegs, can't register memory\n");
  92. return -1;
  93. }
  94. device->cqs_used = min(ISER_MAX_CQ, device->ib_device->num_comp_vectors);
  95. iser_info("using %d CQs, device %s supports %d vectors\n",
  96. device->cqs_used, device->ib_device->name,
  97. device->ib_device->num_comp_vectors);
  98. device->cq_desc = kmalloc(sizeof(struct iser_cq_desc) * device->cqs_used,
  99. GFP_KERNEL);
  100. if (device->cq_desc == NULL)
  101. goto cq_desc_err;
  102. cq_desc = device->cq_desc;
  103. device->pd = ib_alloc_pd(device->ib_device);
  104. if (IS_ERR(device->pd))
  105. goto pd_err;
  106. for (i = 0; i < device->cqs_used; i++) {
  107. cq_desc[i].device = device;
  108. cq_desc[i].cq_index = i;
  109. device->rx_cq[i] = ib_create_cq(device->ib_device,
  110. iser_cq_callback,
  111. iser_cq_event_callback,
  112. (void *)&cq_desc[i],
  113. ISER_MAX_RX_CQ_LEN, i);
  114. if (IS_ERR(device->rx_cq[i])) {
  115. device->rx_cq[i] = NULL;
  116. goto cq_err;
  117. }
  118. device->tx_cq[i] = ib_create_cq(device->ib_device,
  119. NULL, iser_cq_event_callback,
  120. (void *)&cq_desc[i],
  121. ISER_MAX_TX_CQ_LEN, i);
  122. if (IS_ERR(device->tx_cq[i])) {
  123. device->tx_cq[i] = NULL;
  124. goto cq_err;
  125. }
  126. if (ib_req_notify_cq(device->rx_cq[i], IB_CQ_NEXT_COMP))
  127. goto cq_err;
  128. tasklet_init(&device->cq_tasklet[i],
  129. iser_cq_tasklet_fn,
  130. (unsigned long)&cq_desc[i]);
  131. }
  132. device->mr = ib_get_dma_mr(device->pd, IB_ACCESS_LOCAL_WRITE |
  133. IB_ACCESS_REMOTE_WRITE |
  134. IB_ACCESS_REMOTE_READ);
  135. if (IS_ERR(device->mr))
  136. goto dma_mr_err;
  137. INIT_IB_EVENT_HANDLER(&device->event_handler, device->ib_device,
  138. iser_event_handler);
  139. if (ib_register_event_handler(&device->event_handler))
  140. goto handler_err;
  141. return 0;
  142. handler_err:
  143. ib_dereg_mr(device->mr);
  144. dma_mr_err:
  145. for (i = 0; i < device->cqs_used; i++)
  146. tasklet_kill(&device->cq_tasklet[i]);
  147. cq_err:
  148. for (i = 0; i < device->cqs_used; i++) {
  149. if (device->tx_cq[i])
  150. ib_destroy_cq(device->tx_cq[i]);
  151. if (device->rx_cq[i])
  152. ib_destroy_cq(device->rx_cq[i]);
  153. }
  154. ib_dealloc_pd(device->pd);
  155. pd_err:
  156. kfree(device->cq_desc);
  157. cq_desc_err:
  158. iser_err("failed to allocate an IB resource\n");
  159. return -1;
  160. }
  161. /**
  162. * iser_free_device_ib_res - destroy/dealloc/dereg the DMA MR,
  163. * CQ and PD created with the device associated with the adapator.
  164. */
  165. static void iser_free_device_ib_res(struct iser_device *device)
  166. {
  167. int i;
  168. BUG_ON(device->mr == NULL);
  169. for (i = 0; i < device->cqs_used; i++) {
  170. tasklet_kill(&device->cq_tasklet[i]);
  171. (void)ib_destroy_cq(device->tx_cq[i]);
  172. (void)ib_destroy_cq(device->rx_cq[i]);
  173. device->tx_cq[i] = NULL;
  174. device->rx_cq[i] = NULL;
  175. }
  176. (void)ib_unregister_event_handler(&device->event_handler);
  177. (void)ib_dereg_mr(device->mr);
  178. (void)ib_dealloc_pd(device->pd);
  179. kfree(device->cq_desc);
  180. device->mr = NULL;
  181. device->pd = NULL;
  182. }
  183. /**
  184. * iser_create_fmr_pool - Creates FMR pool and page_vector
  185. *
  186. * returns 0 on success, or errno code on failure
  187. */
  188. int iser_create_fmr_pool(struct iser_conn *ib_conn, unsigned cmds_max)
  189. {
  190. struct iser_device *device = ib_conn->device;
  191. struct ib_fmr_pool_param params;
  192. int ret = -ENOMEM;
  193. ib_conn->fmr.page_vec = kmalloc(sizeof(*ib_conn->fmr.page_vec) +
  194. (sizeof(u64)*(ISCSI_ISER_SG_TABLESIZE + 1)),
  195. GFP_KERNEL);
  196. if (!ib_conn->fmr.page_vec)
  197. return ret;
  198. ib_conn->fmr.page_vec->pages = (u64 *)(ib_conn->fmr.page_vec + 1);
  199. params.page_shift = SHIFT_4K;
  200. /* when the first/last SG element are not start/end *
  201. * page aligned, the map whould be of N+1 pages */
  202. params.max_pages_per_fmr = ISCSI_ISER_SG_TABLESIZE + 1;
  203. /* make the pool size twice the max number of SCSI commands *
  204. * the ML is expected to queue, watermark for unmap at 50% */
  205. params.pool_size = cmds_max * 2;
  206. params.dirty_watermark = cmds_max;
  207. params.cache = 0;
  208. params.flush_function = NULL;
  209. params.access = (IB_ACCESS_LOCAL_WRITE |
  210. IB_ACCESS_REMOTE_WRITE |
  211. IB_ACCESS_REMOTE_READ);
  212. ib_conn->fmr.pool = ib_create_fmr_pool(device->pd, &params);
  213. if (!IS_ERR(ib_conn->fmr.pool))
  214. return 0;
  215. /* no FMR => no need for page_vec */
  216. kfree(ib_conn->fmr.page_vec);
  217. ib_conn->fmr.page_vec = NULL;
  218. ret = PTR_ERR(ib_conn->fmr.pool);
  219. ib_conn->fmr.pool = NULL;
  220. if (ret != -ENOSYS) {
  221. iser_err("FMR allocation failed, err %d\n", ret);
  222. return ret;
  223. } else {
  224. iser_warn("FMRs are not supported, using unaligned mode\n");
  225. return 0;
  226. }
  227. }
  228. /**
  229. * iser_free_fmr_pool - releases the FMR pool and page vec
  230. */
  231. void iser_free_fmr_pool(struct iser_conn *ib_conn)
  232. {
  233. iser_info("freeing conn %p fmr pool %p\n",
  234. ib_conn, ib_conn->fmr.pool);
  235. if (ib_conn->fmr.pool != NULL)
  236. ib_destroy_fmr_pool(ib_conn->fmr.pool);
  237. ib_conn->fmr.pool = NULL;
  238. kfree(ib_conn->fmr.page_vec);
  239. ib_conn->fmr.page_vec = NULL;
  240. }
  241. static int
  242. iser_create_fastreg_desc(struct ib_device *ib_device, struct ib_pd *pd,
  243. bool pi_enable, struct fast_reg_descriptor *desc)
  244. {
  245. int ret;
  246. desc->data_frpl = ib_alloc_fast_reg_page_list(ib_device,
  247. ISCSI_ISER_SG_TABLESIZE + 1);
  248. if (IS_ERR(desc->data_frpl)) {
  249. ret = PTR_ERR(desc->data_frpl);
  250. iser_err("Failed to allocate ib_fast_reg_page_list err=%d\n",
  251. ret);
  252. return PTR_ERR(desc->data_frpl);
  253. }
  254. desc->data_mr = ib_alloc_fast_reg_mr(pd, ISCSI_ISER_SG_TABLESIZE + 1);
  255. if (IS_ERR(desc->data_mr)) {
  256. ret = PTR_ERR(desc->data_mr);
  257. iser_err("Failed to allocate ib_fast_reg_mr err=%d\n", ret);
  258. goto fast_reg_mr_failure;
  259. }
  260. desc->reg_indicators |= ISER_DATA_KEY_VALID;
  261. if (pi_enable) {
  262. struct ib_mr_init_attr mr_init_attr = {0};
  263. struct iser_pi_context *pi_ctx = NULL;
  264. desc->pi_ctx = kzalloc(sizeof(*desc->pi_ctx), GFP_KERNEL);
  265. if (!desc->pi_ctx) {
  266. iser_err("Failed to allocate pi context\n");
  267. ret = -ENOMEM;
  268. goto pi_ctx_alloc_failure;
  269. }
  270. pi_ctx = desc->pi_ctx;
  271. pi_ctx->prot_frpl = ib_alloc_fast_reg_page_list(ib_device,
  272. ISCSI_ISER_SG_TABLESIZE);
  273. if (IS_ERR(pi_ctx->prot_frpl)) {
  274. ret = PTR_ERR(pi_ctx->prot_frpl);
  275. iser_err("Failed to allocate prot frpl ret=%d\n",
  276. ret);
  277. goto prot_frpl_failure;
  278. }
  279. pi_ctx->prot_mr = ib_alloc_fast_reg_mr(pd,
  280. ISCSI_ISER_SG_TABLESIZE + 1);
  281. if (IS_ERR(pi_ctx->prot_mr)) {
  282. ret = PTR_ERR(pi_ctx->prot_mr);
  283. iser_err("Failed to allocate prot frmr ret=%d\n",
  284. ret);
  285. goto prot_mr_failure;
  286. }
  287. desc->reg_indicators |= ISER_PROT_KEY_VALID;
  288. mr_init_attr.max_reg_descriptors = 2;
  289. mr_init_attr.flags |= IB_MR_SIGNATURE_EN;
  290. pi_ctx->sig_mr = ib_create_mr(pd, &mr_init_attr);
  291. if (IS_ERR(pi_ctx->sig_mr)) {
  292. ret = PTR_ERR(pi_ctx->sig_mr);
  293. iser_err("Failed to allocate signature enabled mr err=%d\n",
  294. ret);
  295. goto sig_mr_failure;
  296. }
  297. desc->reg_indicators |= ISER_SIG_KEY_VALID;
  298. }
  299. desc->reg_indicators &= ~ISER_FASTREG_PROTECTED;
  300. iser_dbg("Create fr_desc %p page_list %p\n",
  301. desc, desc->data_frpl->page_list);
  302. return 0;
  303. sig_mr_failure:
  304. ib_dereg_mr(desc->pi_ctx->prot_mr);
  305. prot_mr_failure:
  306. ib_free_fast_reg_page_list(desc->pi_ctx->prot_frpl);
  307. prot_frpl_failure:
  308. kfree(desc->pi_ctx);
  309. pi_ctx_alloc_failure:
  310. ib_dereg_mr(desc->data_mr);
  311. fast_reg_mr_failure:
  312. ib_free_fast_reg_page_list(desc->data_frpl);
  313. return ret;
  314. }
  315. /**
  316. * iser_create_fastreg_pool - Creates pool of fast_reg descriptors
  317. * for fast registration work requests.
  318. * returns 0 on success, or errno code on failure
  319. */
  320. int iser_create_fastreg_pool(struct iser_conn *ib_conn, unsigned cmds_max)
  321. {
  322. struct iser_device *device = ib_conn->device;
  323. struct fast_reg_descriptor *desc;
  324. int i, ret;
  325. INIT_LIST_HEAD(&ib_conn->fastreg.pool);
  326. ib_conn->fastreg.pool_size = 0;
  327. for (i = 0; i < cmds_max; i++) {
  328. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  329. if (!desc) {
  330. iser_err("Failed to allocate a new fast_reg descriptor\n");
  331. ret = -ENOMEM;
  332. goto err;
  333. }
  334. ret = iser_create_fastreg_desc(device->ib_device, device->pd,
  335. ib_conn->pi_support, desc);
  336. if (ret) {
  337. iser_err("Failed to create fastreg descriptor err=%d\n",
  338. ret);
  339. kfree(desc);
  340. goto err;
  341. }
  342. list_add_tail(&desc->list, &ib_conn->fastreg.pool);
  343. ib_conn->fastreg.pool_size++;
  344. }
  345. return 0;
  346. err:
  347. iser_free_fastreg_pool(ib_conn);
  348. return ret;
  349. }
  350. /**
  351. * iser_free_fastreg_pool - releases the pool of fast_reg descriptors
  352. */
  353. void iser_free_fastreg_pool(struct iser_conn *ib_conn)
  354. {
  355. struct fast_reg_descriptor *desc, *tmp;
  356. int i = 0;
  357. if (list_empty(&ib_conn->fastreg.pool))
  358. return;
  359. iser_info("freeing conn %p fr pool\n", ib_conn);
  360. list_for_each_entry_safe(desc, tmp, &ib_conn->fastreg.pool, list) {
  361. list_del(&desc->list);
  362. ib_free_fast_reg_page_list(desc->data_frpl);
  363. ib_dereg_mr(desc->data_mr);
  364. if (desc->pi_ctx) {
  365. ib_free_fast_reg_page_list(desc->pi_ctx->prot_frpl);
  366. ib_dereg_mr(desc->pi_ctx->prot_mr);
  367. ib_destroy_mr(desc->pi_ctx->sig_mr);
  368. kfree(desc->pi_ctx);
  369. }
  370. kfree(desc);
  371. ++i;
  372. }
  373. if (i < ib_conn->fastreg.pool_size)
  374. iser_warn("pool still has %d regions registered\n",
  375. ib_conn->fastreg.pool_size - i);
  376. }
  377. /**
  378. * iser_create_ib_conn_res - Queue-Pair (QP)
  379. *
  380. * returns 0 on success, -1 on failure
  381. */
  382. static int iser_create_ib_conn_res(struct iser_conn *ib_conn)
  383. {
  384. struct iser_device *device;
  385. struct ib_qp_init_attr init_attr;
  386. int ret = -ENOMEM;
  387. int index, min_index = 0;
  388. BUG_ON(ib_conn->device == NULL);
  389. device = ib_conn->device;
  390. memset(&init_attr, 0, sizeof init_attr);
  391. mutex_lock(&ig.connlist_mutex);
  392. /* select the CQ with the minimal number of usages */
  393. for (index = 0; index < device->cqs_used; index++)
  394. if (device->cq_active_qps[index] <
  395. device->cq_active_qps[min_index])
  396. min_index = index;
  397. device->cq_active_qps[min_index]++;
  398. mutex_unlock(&ig.connlist_mutex);
  399. iser_info("cq index %d used for ib_conn %p\n", min_index, ib_conn);
  400. init_attr.event_handler = iser_qp_event_callback;
  401. init_attr.qp_context = (void *)ib_conn;
  402. init_attr.send_cq = device->tx_cq[min_index];
  403. init_attr.recv_cq = device->rx_cq[min_index];
  404. init_attr.cap.max_recv_wr = ISER_QP_MAX_RECV_DTOS;
  405. init_attr.cap.max_send_sge = 2;
  406. init_attr.cap.max_recv_sge = 1;
  407. init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  408. init_attr.qp_type = IB_QPT_RC;
  409. if (ib_conn->pi_support) {
  410. init_attr.cap.max_send_wr = ISER_QP_SIG_MAX_REQ_DTOS;
  411. init_attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
  412. } else {
  413. init_attr.cap.max_send_wr = ISER_QP_MAX_REQ_DTOS;
  414. }
  415. ret = rdma_create_qp(ib_conn->cma_id, device->pd, &init_attr);
  416. if (ret)
  417. goto out_err;
  418. ib_conn->qp = ib_conn->cma_id->qp;
  419. iser_info("setting conn %p cma_id %p qp %p\n",
  420. ib_conn, ib_conn->cma_id,
  421. ib_conn->cma_id->qp);
  422. return ret;
  423. out_err:
  424. iser_err("unable to alloc mem or create resource, err %d\n", ret);
  425. return ret;
  426. }
  427. /**
  428. * releases the QP object
  429. */
  430. static void iser_free_ib_conn_res(struct iser_conn *ib_conn)
  431. {
  432. int cq_index;
  433. BUG_ON(ib_conn == NULL);
  434. iser_info("freeing conn %p cma_id %p qp %p\n",
  435. ib_conn, ib_conn->cma_id,
  436. ib_conn->qp);
  437. /* qp is created only once both addr & route are resolved */
  438. if (ib_conn->qp != NULL) {
  439. cq_index = ((struct iser_cq_desc *)ib_conn->qp->recv_cq->cq_context)->cq_index;
  440. ib_conn->device->cq_active_qps[cq_index]--;
  441. rdma_destroy_qp(ib_conn->cma_id);
  442. }
  443. ib_conn->qp = NULL;
  444. }
  445. /**
  446. * based on the resolved device node GUID see if there already allocated
  447. * device for this device. If there's no such, create one.
  448. */
  449. static
  450. struct iser_device *iser_device_find_by_ib_device(struct rdma_cm_id *cma_id)
  451. {
  452. struct iser_device *device;
  453. mutex_lock(&ig.device_list_mutex);
  454. list_for_each_entry(device, &ig.device_list, ig_list)
  455. /* find if there's a match using the node GUID */
  456. if (device->ib_device->node_guid == cma_id->device->node_guid)
  457. goto inc_refcnt;
  458. device = kzalloc(sizeof *device, GFP_KERNEL);
  459. if (device == NULL)
  460. goto out;
  461. /* assign this device to the device */
  462. device->ib_device = cma_id->device;
  463. /* init the device and link it into ig device list */
  464. if (iser_create_device_ib_res(device)) {
  465. kfree(device);
  466. device = NULL;
  467. goto out;
  468. }
  469. list_add(&device->ig_list, &ig.device_list);
  470. inc_refcnt:
  471. device->refcount++;
  472. out:
  473. mutex_unlock(&ig.device_list_mutex);
  474. return device;
  475. }
  476. /* if there's no demand for this device, release it */
  477. static void iser_device_try_release(struct iser_device *device)
  478. {
  479. mutex_lock(&ig.device_list_mutex);
  480. device->refcount--;
  481. iser_info("device %p refcount %d\n", device, device->refcount);
  482. if (!device->refcount) {
  483. iser_free_device_ib_res(device);
  484. list_del(&device->ig_list);
  485. kfree(device);
  486. }
  487. mutex_unlock(&ig.device_list_mutex);
  488. }
  489. /**
  490. * Called with state mutex held
  491. **/
  492. static int iser_conn_state_comp_exch(struct iser_conn *ib_conn,
  493. enum iser_ib_conn_state comp,
  494. enum iser_ib_conn_state exch)
  495. {
  496. int ret;
  497. if ((ret = (ib_conn->state == comp)))
  498. ib_conn->state = exch;
  499. return ret;
  500. }
  501. void iser_release_work(struct work_struct *work)
  502. {
  503. struct iser_conn *ib_conn;
  504. int rc;
  505. ib_conn = container_of(work, struct iser_conn, release_work);
  506. /* wait for .conn_stop callback */
  507. rc = wait_for_completion_timeout(&ib_conn->stop_completion, 30 * HZ);
  508. WARN_ON(rc == 0);
  509. /* wait for the qp`s post send and post receive buffers to empty */
  510. rc = wait_for_completion_timeout(&ib_conn->flush_completion, 30 * HZ);
  511. WARN_ON(rc == 0);
  512. ib_conn->state = ISER_CONN_DOWN;
  513. mutex_lock(&ib_conn->state_mutex);
  514. ib_conn->state = ISER_CONN_DOWN;
  515. mutex_unlock(&ib_conn->state_mutex);
  516. iser_conn_release(ib_conn);
  517. }
  518. /**
  519. * Frees all conn objects and deallocs conn descriptor
  520. */
  521. void iser_conn_release(struct iser_conn *ib_conn)
  522. {
  523. struct iser_device *device = ib_conn->device;
  524. mutex_lock(&ig.connlist_mutex);
  525. list_del(&ib_conn->conn_list);
  526. mutex_unlock(&ig.connlist_mutex);
  527. mutex_lock(&ib_conn->state_mutex);
  528. BUG_ON(ib_conn->state != ISER_CONN_DOWN);
  529. iser_free_rx_descriptors(ib_conn);
  530. iser_free_ib_conn_res(ib_conn);
  531. ib_conn->device = NULL;
  532. /* on EVENT_ADDR_ERROR there's no device yet for this conn */
  533. if (device != NULL)
  534. iser_device_try_release(device);
  535. mutex_unlock(&ib_conn->state_mutex);
  536. /* if cma handler context, the caller actually destroy the id */
  537. if (ib_conn->cma_id != NULL) {
  538. rdma_destroy_id(ib_conn->cma_id);
  539. ib_conn->cma_id = NULL;
  540. }
  541. kfree(ib_conn);
  542. }
  543. /**
  544. * triggers start of the disconnect procedures and wait for them to be done
  545. */
  546. void iser_conn_terminate(struct iser_conn *ib_conn)
  547. {
  548. int err = 0;
  549. /* change the ib conn state only if the conn is UP, however always call
  550. * rdma_disconnect since this is the only way to cause the CMA to change
  551. * the QP state to ERROR
  552. */
  553. iser_conn_state_comp_exch(ib_conn, ISER_CONN_UP, ISER_CONN_TERMINATING);
  554. err = rdma_disconnect(ib_conn->cma_id);
  555. if (err)
  556. iser_err("Failed to disconnect, conn: 0x%p err %d\n",
  557. ib_conn,err);
  558. }
  559. /**
  560. * Called with state mutex held
  561. **/
  562. static void iser_connect_error(struct rdma_cm_id *cma_id)
  563. {
  564. struct iser_conn *ib_conn;
  565. ib_conn = (struct iser_conn *)cma_id->context;
  566. ib_conn->state = ISER_CONN_DOWN;
  567. }
  568. /**
  569. * Called with state mutex held
  570. **/
  571. static void iser_addr_handler(struct rdma_cm_id *cma_id)
  572. {
  573. struct iser_device *device;
  574. struct iser_conn *ib_conn;
  575. int ret;
  576. ib_conn = (struct iser_conn *)cma_id->context;
  577. if (ib_conn->state != ISER_CONN_PENDING)
  578. /* bailout */
  579. return;
  580. device = iser_device_find_by_ib_device(cma_id);
  581. if (!device) {
  582. iser_err("device lookup/creation failed\n");
  583. iser_connect_error(cma_id);
  584. return;
  585. }
  586. ib_conn->device = device;
  587. /* connection T10-PI support */
  588. if (iser_pi_enable) {
  589. if (!(device->dev_attr.device_cap_flags &
  590. IB_DEVICE_SIGNATURE_HANDOVER)) {
  591. iser_warn("T10-PI requested but not supported on %s, "
  592. "continue without T10-PI\n",
  593. ib_conn->device->ib_device->name);
  594. ib_conn->pi_support = false;
  595. } else {
  596. ib_conn->pi_support = true;
  597. }
  598. }
  599. ret = rdma_resolve_route(cma_id, 1000);
  600. if (ret) {
  601. iser_err("resolve route failed: %d\n", ret);
  602. iser_connect_error(cma_id);
  603. return;
  604. }
  605. }
  606. /**
  607. * Called with state mutex held
  608. **/
  609. static void iser_route_handler(struct rdma_cm_id *cma_id)
  610. {
  611. struct rdma_conn_param conn_param;
  612. int ret;
  613. struct iser_cm_hdr req_hdr;
  614. struct iser_conn *ib_conn = (struct iser_conn *)cma_id->context;
  615. struct iser_device *device = ib_conn->device;
  616. if (ib_conn->state != ISER_CONN_PENDING)
  617. /* bailout */
  618. return;
  619. ret = iser_create_ib_conn_res((struct iser_conn *)cma_id->context);
  620. if (ret)
  621. goto failure;
  622. memset(&conn_param, 0, sizeof conn_param);
  623. conn_param.responder_resources = device->dev_attr.max_qp_rd_atom;
  624. conn_param.initiator_depth = 1;
  625. conn_param.retry_count = 7;
  626. conn_param.rnr_retry_count = 6;
  627. memset(&req_hdr, 0, sizeof(req_hdr));
  628. req_hdr.flags = (ISER_ZBVA_NOT_SUPPORTED |
  629. ISER_SEND_W_INV_NOT_SUPPORTED);
  630. conn_param.private_data = (void *)&req_hdr;
  631. conn_param.private_data_len = sizeof(struct iser_cm_hdr);
  632. ret = rdma_connect(cma_id, &conn_param);
  633. if (ret) {
  634. iser_err("failure connecting: %d\n", ret);
  635. goto failure;
  636. }
  637. return;
  638. failure:
  639. iser_connect_error(cma_id);
  640. }
  641. static void iser_connected_handler(struct rdma_cm_id *cma_id)
  642. {
  643. struct iser_conn *ib_conn;
  644. struct ib_qp_attr attr;
  645. struct ib_qp_init_attr init_attr;
  646. ib_conn = (struct iser_conn *)cma_id->context;
  647. if (ib_conn->state != ISER_CONN_PENDING)
  648. /* bailout */
  649. return;
  650. (void)ib_query_qp(cma_id->qp, &attr, ~0, &init_attr);
  651. iser_info("remote qpn:%x my qpn:%x\n", attr.dest_qp_num, cma_id->qp->qp_num);
  652. ib_conn->state = ISER_CONN_UP;
  653. complete(&ib_conn->up_completion);
  654. }
  655. static void iser_disconnected_handler(struct rdma_cm_id *cma_id)
  656. {
  657. struct iser_conn *ib_conn;
  658. ib_conn = (struct iser_conn *)cma_id->context;
  659. /* getting here when the state is UP means that the conn is being *
  660. * terminated asynchronously from the iSCSI layer's perspective. */
  661. if (iser_conn_state_comp_exch(ib_conn, ISER_CONN_UP,
  662. ISER_CONN_TERMINATING)){
  663. if (ib_conn->iscsi_conn)
  664. iscsi_conn_failure(ib_conn->iscsi_conn, ISCSI_ERR_CONN_FAILED);
  665. else
  666. iser_err("iscsi_iser connection isn't bound\n");
  667. }
  668. /* Complete the termination process if no posts are pending. This code
  669. * block also exists in iser_handle_comp_error(), but it is needed here
  670. * for cases of no flushes at all, e.g. discovery over rdma.
  671. */
  672. if (ib_conn->post_recv_buf_count == 0 &&
  673. (atomic_read(&ib_conn->post_send_buf_count) == 0)) {
  674. complete(&ib_conn->flush_completion);
  675. }
  676. }
  677. static int iser_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
  678. {
  679. struct iser_conn *ib_conn;
  680. ib_conn = (struct iser_conn *)cma_id->context;
  681. iser_info("event %d status %d conn %p id %p\n",
  682. event->event, event->status, cma_id->context, cma_id);
  683. mutex_lock(&ib_conn->state_mutex);
  684. switch (event->event) {
  685. case RDMA_CM_EVENT_ADDR_RESOLVED:
  686. iser_addr_handler(cma_id);
  687. break;
  688. case RDMA_CM_EVENT_ROUTE_RESOLVED:
  689. iser_route_handler(cma_id);
  690. break;
  691. case RDMA_CM_EVENT_ESTABLISHED:
  692. iser_connected_handler(cma_id);
  693. break;
  694. case RDMA_CM_EVENT_ADDR_ERROR:
  695. case RDMA_CM_EVENT_ROUTE_ERROR:
  696. case RDMA_CM_EVENT_CONNECT_ERROR:
  697. case RDMA_CM_EVENT_UNREACHABLE:
  698. case RDMA_CM_EVENT_REJECTED:
  699. iser_connect_error(cma_id);
  700. break;
  701. case RDMA_CM_EVENT_DISCONNECTED:
  702. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  703. case RDMA_CM_EVENT_ADDR_CHANGE:
  704. case RDMA_CM_EVENT_TIMEWAIT_EXIT:
  705. iser_disconnected_handler(cma_id);
  706. break;
  707. default:
  708. iser_err("Unexpected RDMA CM event (%d)\n", event->event);
  709. break;
  710. }
  711. mutex_unlock(&ib_conn->state_mutex);
  712. return 0;
  713. }
  714. void iser_conn_init(struct iser_conn *ib_conn)
  715. {
  716. ib_conn->state = ISER_CONN_INIT;
  717. ib_conn->post_recv_buf_count = 0;
  718. atomic_set(&ib_conn->post_send_buf_count, 0);
  719. init_completion(&ib_conn->stop_completion);
  720. init_completion(&ib_conn->flush_completion);
  721. init_completion(&ib_conn->up_completion);
  722. INIT_LIST_HEAD(&ib_conn->conn_list);
  723. spin_lock_init(&ib_conn->lock);
  724. mutex_init(&ib_conn->state_mutex);
  725. }
  726. /**
  727. * starts the process of connecting to the target
  728. * sleeps until the connection is established or rejected
  729. */
  730. int iser_connect(struct iser_conn *ib_conn,
  731. struct sockaddr *src_addr,
  732. struct sockaddr *dst_addr,
  733. int non_blocking)
  734. {
  735. int err = 0;
  736. mutex_lock(&ib_conn->state_mutex);
  737. sprintf(ib_conn->name, "%pISp", dst_addr);
  738. iser_info("connecting to: %s\n", ib_conn->name);
  739. /* the device is known only --after-- address resolution */
  740. ib_conn->device = NULL;
  741. ib_conn->state = ISER_CONN_PENDING;
  742. ib_conn->cma_id = rdma_create_id(iser_cma_handler,
  743. (void *)ib_conn,
  744. RDMA_PS_TCP, IB_QPT_RC);
  745. if (IS_ERR(ib_conn->cma_id)) {
  746. err = PTR_ERR(ib_conn->cma_id);
  747. iser_err("rdma_create_id failed: %d\n", err);
  748. goto id_failure;
  749. }
  750. err = rdma_resolve_addr(ib_conn->cma_id, src_addr, dst_addr, 1000);
  751. if (err) {
  752. iser_err("rdma_resolve_addr failed: %d\n", err);
  753. goto addr_failure;
  754. }
  755. if (!non_blocking) {
  756. wait_for_completion_interruptible(&ib_conn->up_completion);
  757. if (ib_conn->state != ISER_CONN_UP) {
  758. err = -EIO;
  759. goto connect_failure;
  760. }
  761. }
  762. mutex_unlock(&ib_conn->state_mutex);
  763. mutex_lock(&ig.connlist_mutex);
  764. list_add(&ib_conn->conn_list, &ig.connlist);
  765. mutex_unlock(&ig.connlist_mutex);
  766. return 0;
  767. id_failure:
  768. ib_conn->cma_id = NULL;
  769. addr_failure:
  770. ib_conn->state = ISER_CONN_DOWN;
  771. connect_failure:
  772. mutex_unlock(&ib_conn->state_mutex);
  773. iser_conn_release(ib_conn);
  774. return err;
  775. }
  776. /**
  777. * iser_reg_page_vec - Register physical memory
  778. *
  779. * returns: 0 on success, errno code on failure
  780. */
  781. int iser_reg_page_vec(struct iser_conn *ib_conn,
  782. struct iser_page_vec *page_vec,
  783. struct iser_mem_reg *mem_reg)
  784. {
  785. struct ib_pool_fmr *mem;
  786. u64 io_addr;
  787. u64 *page_list;
  788. int status;
  789. page_list = page_vec->pages;
  790. io_addr = page_list[0];
  791. mem = ib_fmr_pool_map_phys(ib_conn->fmr.pool,
  792. page_list,
  793. page_vec->length,
  794. io_addr);
  795. if (IS_ERR(mem)) {
  796. status = (int)PTR_ERR(mem);
  797. iser_err("ib_fmr_pool_map_phys failed: %d\n", status);
  798. return status;
  799. }
  800. mem_reg->lkey = mem->fmr->lkey;
  801. mem_reg->rkey = mem->fmr->rkey;
  802. mem_reg->len = page_vec->length * SIZE_4K;
  803. mem_reg->va = io_addr;
  804. mem_reg->is_mr = 1;
  805. mem_reg->mem_h = (void *)mem;
  806. mem_reg->va += page_vec->offset;
  807. mem_reg->len = page_vec->data_size;
  808. iser_dbg("PHYSICAL Mem.register, [PHYS p_array: 0x%p, sz: %d, "
  809. "entry[0]: (0x%08lx,%ld)] -> "
  810. "[lkey: 0x%08X mem_h: 0x%p va: 0x%08lX sz: %ld]\n",
  811. page_vec, page_vec->length,
  812. (unsigned long)page_vec->pages[0],
  813. (unsigned long)page_vec->data_size,
  814. (unsigned int)mem_reg->lkey, mem_reg->mem_h,
  815. (unsigned long)mem_reg->va, (unsigned long)mem_reg->len);
  816. return 0;
  817. }
  818. /**
  819. * Unregister (previosuly registered using FMR) memory.
  820. * If memory is non-FMR does nothing.
  821. */
  822. void iser_unreg_mem_fmr(struct iscsi_iser_task *iser_task,
  823. enum iser_data_dir cmd_dir)
  824. {
  825. struct iser_mem_reg *reg = &iser_task->rdma_regd[cmd_dir].reg;
  826. int ret;
  827. if (!reg->is_mr)
  828. return;
  829. iser_dbg("PHYSICAL Mem.Unregister mem_h %p\n",reg->mem_h);
  830. ret = ib_fmr_pool_unmap((struct ib_pool_fmr *)reg->mem_h);
  831. if (ret)
  832. iser_err("ib_fmr_pool_unmap failed %d\n", ret);
  833. reg->mem_h = NULL;
  834. }
  835. void iser_unreg_mem_fastreg(struct iscsi_iser_task *iser_task,
  836. enum iser_data_dir cmd_dir)
  837. {
  838. struct iser_mem_reg *reg = &iser_task->rdma_regd[cmd_dir].reg;
  839. struct iser_conn *ib_conn = iser_task->ib_conn;
  840. struct fast_reg_descriptor *desc = reg->mem_h;
  841. if (!reg->is_mr)
  842. return;
  843. reg->mem_h = NULL;
  844. reg->is_mr = 0;
  845. spin_lock_bh(&ib_conn->lock);
  846. list_add_tail(&desc->list, &ib_conn->fastreg.pool);
  847. spin_unlock_bh(&ib_conn->lock);
  848. }
  849. int iser_post_recvl(struct iser_conn *ib_conn)
  850. {
  851. struct ib_recv_wr rx_wr, *rx_wr_failed;
  852. struct ib_sge sge;
  853. int ib_ret;
  854. sge.addr = ib_conn->login_resp_dma;
  855. sge.length = ISER_RX_LOGIN_SIZE;
  856. sge.lkey = ib_conn->device->mr->lkey;
  857. rx_wr.wr_id = (unsigned long)ib_conn->login_resp_buf;
  858. rx_wr.sg_list = &sge;
  859. rx_wr.num_sge = 1;
  860. rx_wr.next = NULL;
  861. ib_conn->post_recv_buf_count++;
  862. ib_ret = ib_post_recv(ib_conn->qp, &rx_wr, &rx_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 *ib_conn, int count)
  870. {
  871. struct ib_recv_wr *rx_wr, *rx_wr_failed;
  872. int i, ib_ret;
  873. unsigned int my_rx_head = ib_conn->rx_desc_head;
  874. struct iser_rx_desc *rx_desc;
  875. for (rx_wr = ib_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
  876. rx_desc = &ib_conn->rx_descs[my_rx_head];
  877. rx_wr->wr_id = (unsigned long)rx_desc;
  878. rx_wr->sg_list = &rx_desc->rx_sg;
  879. rx_wr->num_sge = 1;
  880. rx_wr->next = rx_wr + 1;
  881. my_rx_head = (my_rx_head + 1) & ib_conn->qp_max_recv_dtos_mask;
  882. }
  883. rx_wr--;
  884. rx_wr->next = NULL; /* mark end of work requests list */
  885. ib_conn->post_recv_buf_count += count;
  886. ib_ret = ib_post_recv(ib_conn->qp, ib_conn->rx_wr, &rx_wr_failed);
  887. if (ib_ret) {
  888. iser_err("ib_post_recv failed ret=%d\n", ib_ret);
  889. ib_conn->post_recv_buf_count -= count;
  890. } else
  891. ib_conn->rx_desc_head = my_rx_head;
  892. return ib_ret;
  893. }
  894. /**
  895. * iser_start_send - Initiate a Send DTO operation
  896. *
  897. * returns 0 on success, -1 on failure
  898. */
  899. int iser_post_send(struct iser_conn *ib_conn, struct iser_tx_desc *tx_desc)
  900. {
  901. int ib_ret;
  902. struct ib_send_wr send_wr, *send_wr_failed;
  903. ib_dma_sync_single_for_device(ib_conn->device->ib_device,
  904. tx_desc->dma_addr, ISER_HEADERS_LEN, DMA_TO_DEVICE);
  905. send_wr.next = NULL;
  906. send_wr.wr_id = (unsigned long)tx_desc;
  907. send_wr.sg_list = tx_desc->tx_sg;
  908. send_wr.num_sge = tx_desc->num_sge;
  909. send_wr.opcode = IB_WR_SEND;
  910. send_wr.send_flags = IB_SEND_SIGNALED;
  911. atomic_inc(&ib_conn->post_send_buf_count);
  912. ib_ret = ib_post_send(ib_conn->qp, &send_wr, &send_wr_failed);
  913. if (ib_ret) {
  914. iser_err("ib_post_send failed, ret:%d\n", ib_ret);
  915. atomic_dec(&ib_conn->post_send_buf_count);
  916. }
  917. return ib_ret;
  918. }
  919. static void iser_handle_comp_error(struct iser_tx_desc *desc,
  920. struct iser_conn *ib_conn)
  921. {
  922. if (desc && desc->type == ISCSI_TX_DATAOUT)
  923. kmem_cache_free(ig.desc_cache, desc);
  924. if (ib_conn->post_recv_buf_count == 0 &&
  925. atomic_read(&ib_conn->post_send_buf_count) == 0) {
  926. /**
  927. * getting here when the state is UP means that the conn is
  928. * being terminated asynchronously from the iSCSI layer's
  929. * perspective. It is safe to peek at the connection state
  930. * since iscsi_conn_failure is allowed to be called twice.
  931. **/
  932. if (ib_conn->state == ISER_CONN_UP)
  933. iscsi_conn_failure(ib_conn->iscsi_conn,
  934. ISCSI_ERR_CONN_FAILED);
  935. /* no more non completed posts to the QP, complete the
  936. * termination process w.o worrying on disconnect event */
  937. complete(&ib_conn->flush_completion);
  938. }
  939. }
  940. static int iser_drain_tx_cq(struct iser_device *device, int cq_index)
  941. {
  942. struct ib_cq *cq = device->tx_cq[cq_index];
  943. struct ib_wc wc;
  944. struct iser_tx_desc *tx_desc;
  945. struct iser_conn *ib_conn;
  946. int completed_tx = 0;
  947. while (ib_poll_cq(cq, 1, &wc) == 1) {
  948. tx_desc = (struct iser_tx_desc *) (unsigned long) wc.wr_id;
  949. ib_conn = wc.qp->qp_context;
  950. if (wc.status == IB_WC_SUCCESS) {
  951. if (wc.opcode == IB_WC_SEND)
  952. iser_snd_completion(tx_desc, ib_conn);
  953. else
  954. iser_err("expected opcode %d got %d\n",
  955. IB_WC_SEND, wc.opcode);
  956. } else {
  957. iser_err("tx id %llx status %d vend_err %x\n",
  958. wc.wr_id, wc.status, wc.vendor_err);
  959. if (wc.wr_id != ISER_FASTREG_LI_WRID) {
  960. atomic_dec(&ib_conn->post_send_buf_count);
  961. iser_handle_comp_error(tx_desc, ib_conn);
  962. }
  963. }
  964. completed_tx++;
  965. }
  966. return completed_tx;
  967. }
  968. static void iser_cq_tasklet_fn(unsigned long data)
  969. {
  970. struct iser_cq_desc *cq_desc = (struct iser_cq_desc *)data;
  971. struct iser_device *device = cq_desc->device;
  972. int cq_index = cq_desc->cq_index;
  973. struct ib_cq *cq = device->rx_cq[cq_index];
  974. struct ib_wc wc;
  975. struct iser_rx_desc *desc;
  976. unsigned long xfer_len;
  977. struct iser_conn *ib_conn;
  978. int completed_tx, completed_rx = 0;
  979. /* First do tx drain, so in a case where we have rx flushes and a successful
  980. * tx completion we will still go through completion error handling.
  981. */
  982. completed_tx = iser_drain_tx_cq(device, cq_index);
  983. while (ib_poll_cq(cq, 1, &wc) == 1) {
  984. desc = (struct iser_rx_desc *) (unsigned long) wc.wr_id;
  985. BUG_ON(desc == NULL);
  986. ib_conn = wc.qp->qp_context;
  987. if (wc.status == IB_WC_SUCCESS) {
  988. if (wc.opcode == IB_WC_RECV) {
  989. xfer_len = (unsigned long)wc.byte_len;
  990. iser_rcv_completion(desc, xfer_len, ib_conn);
  991. } else
  992. iser_err("expected opcode %d got %d\n",
  993. IB_WC_RECV, wc.opcode);
  994. } else {
  995. if (wc.status != IB_WC_WR_FLUSH_ERR)
  996. iser_err("rx id %llx status %d vend_err %x\n",
  997. wc.wr_id, wc.status, wc.vendor_err);
  998. ib_conn->post_recv_buf_count--;
  999. iser_handle_comp_error(NULL, ib_conn);
  1000. }
  1001. completed_rx++;
  1002. if (!(completed_rx & 63))
  1003. completed_tx += iser_drain_tx_cq(device, cq_index);
  1004. }
  1005. /* #warning "it is assumed here that arming CQ only once its empty" *
  1006. * " would not cause interrupts to be missed" */
  1007. ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
  1008. iser_dbg("got %d rx %d tx completions\n", completed_rx, completed_tx);
  1009. }
  1010. static void iser_cq_callback(struct ib_cq *cq, void *cq_context)
  1011. {
  1012. struct iser_cq_desc *cq_desc = (struct iser_cq_desc *)cq_context;
  1013. struct iser_device *device = cq_desc->device;
  1014. int cq_index = cq_desc->cq_index;
  1015. tasklet_schedule(&device->cq_tasklet[cq_index]);
  1016. }
  1017. u8 iser_check_task_pi_status(struct iscsi_iser_task *iser_task,
  1018. enum iser_data_dir cmd_dir, sector_t *sector)
  1019. {
  1020. struct iser_mem_reg *reg = &iser_task->rdma_regd[cmd_dir].reg;
  1021. struct fast_reg_descriptor *desc = reg->mem_h;
  1022. unsigned long sector_size = iser_task->sc->device->sector_size;
  1023. struct ib_mr_status mr_status;
  1024. int ret;
  1025. if (desc && desc->reg_indicators & ISER_FASTREG_PROTECTED) {
  1026. desc->reg_indicators &= ~ISER_FASTREG_PROTECTED;
  1027. ret = ib_check_mr_status(desc->pi_ctx->sig_mr,
  1028. IB_MR_CHECK_SIG_STATUS, &mr_status);
  1029. if (ret) {
  1030. pr_err("ib_check_mr_status failed, ret %d\n", ret);
  1031. goto err;
  1032. }
  1033. if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
  1034. sector_t sector_off = mr_status.sig_err.sig_err_offset;
  1035. do_div(sector_off, sector_size + 8);
  1036. *sector = scsi_get_lba(iser_task->sc) + sector_off;
  1037. pr_err("PI error found type %d at sector %llx "
  1038. "expected %x vs actual %x\n",
  1039. mr_status.sig_err.err_type,
  1040. (unsigned long long)*sector,
  1041. mr_status.sig_err.expected,
  1042. mr_status.sig_err.actual);
  1043. switch (mr_status.sig_err.err_type) {
  1044. case IB_SIG_BAD_GUARD:
  1045. return 0x1;
  1046. case IB_SIG_BAD_REFTAG:
  1047. return 0x3;
  1048. case IB_SIG_BAD_APPTAG:
  1049. return 0x2;
  1050. }
  1051. }
  1052. }
  1053. return 0;
  1054. err:
  1055. /* Not alot we can do here, return ambiguous guard error */
  1056. return 0x1;
  1057. }