iser_verbs.c 33 KB

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