iser_verbs.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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. INIT_LIST_HEAD(&fr_pool->all_list);
  306. spin_lock_init(&fr_pool->lock);
  307. fr_pool->size = 0;
  308. for (i = 0; i < cmds_max; i++) {
  309. desc = iser_create_fastreg_desc(device, device->pd,
  310. ib_conn->pi_support, size);
  311. if (IS_ERR(desc)) {
  312. ret = PTR_ERR(desc);
  313. goto err;
  314. }
  315. list_add_tail(&desc->list, &fr_pool->list);
  316. list_add_tail(&desc->all_list, &fr_pool->all_list);
  317. fr_pool->size++;
  318. }
  319. return 0;
  320. err:
  321. iser_free_fastreg_pool(ib_conn);
  322. return ret;
  323. }
  324. /**
  325. * iser_free_fastreg_pool - releases the pool of fast_reg descriptors
  326. */
  327. void iser_free_fastreg_pool(struct ib_conn *ib_conn)
  328. {
  329. struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
  330. struct iser_fr_desc *desc, *tmp;
  331. int i = 0;
  332. if (list_empty(&fr_pool->all_list))
  333. return;
  334. iser_info("freeing conn %p fr pool\n", ib_conn);
  335. list_for_each_entry_safe(desc, tmp, &fr_pool->all_list, all_list) {
  336. list_del(&desc->all_list);
  337. iser_free_reg_res(&desc->rsc);
  338. if (desc->pi_ctx)
  339. iser_free_pi_ctx(desc->pi_ctx);
  340. kfree(desc);
  341. ++i;
  342. }
  343. if (i < fr_pool->size)
  344. iser_warn("pool still has %d regions registered\n",
  345. fr_pool->size - i);
  346. }
  347. /**
  348. * iser_create_ib_conn_res - Queue-Pair (QP)
  349. *
  350. * returns 0 on success, -1 on failure
  351. */
  352. static int iser_create_ib_conn_res(struct ib_conn *ib_conn)
  353. {
  354. struct iser_conn *iser_conn = to_iser_conn(ib_conn);
  355. struct iser_device *device;
  356. struct ib_device *ib_dev;
  357. struct ib_qp_init_attr init_attr;
  358. int ret = -ENOMEM;
  359. int index, min_index = 0;
  360. BUG_ON(ib_conn->device == NULL);
  361. device = ib_conn->device;
  362. ib_dev = device->ib_device;
  363. memset(&init_attr, 0, sizeof init_attr);
  364. mutex_lock(&ig.connlist_mutex);
  365. /* select the CQ with the minimal number of usages */
  366. for (index = 0; index < device->comps_used; index++) {
  367. if (device->comps[index].active_qps <
  368. device->comps[min_index].active_qps)
  369. min_index = index;
  370. }
  371. ib_conn->comp = &device->comps[min_index];
  372. ib_conn->comp->active_qps++;
  373. mutex_unlock(&ig.connlist_mutex);
  374. iser_info("cq index %d used for ib_conn %p\n", min_index, ib_conn);
  375. init_attr.event_handler = iser_qp_event_callback;
  376. init_attr.qp_context = (void *)ib_conn;
  377. init_attr.send_cq = ib_conn->comp->cq;
  378. init_attr.recv_cq = ib_conn->comp->cq;
  379. init_attr.cap.max_recv_wr = ISER_QP_MAX_RECV_DTOS;
  380. init_attr.cap.max_send_sge = 2;
  381. init_attr.cap.max_recv_sge = 1;
  382. init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  383. init_attr.qp_type = IB_QPT_RC;
  384. if (ib_conn->pi_support) {
  385. init_attr.cap.max_send_wr = ISER_QP_SIG_MAX_REQ_DTOS + 1;
  386. init_attr.create_flags |= IB_QP_CREATE_SIGNATURE_EN;
  387. iser_conn->max_cmds =
  388. ISER_GET_MAX_XMIT_CMDS(ISER_QP_SIG_MAX_REQ_DTOS);
  389. } else {
  390. if (ib_dev->attrs.max_qp_wr > ISER_QP_MAX_REQ_DTOS) {
  391. init_attr.cap.max_send_wr = ISER_QP_MAX_REQ_DTOS + 1;
  392. iser_conn->max_cmds =
  393. ISER_GET_MAX_XMIT_CMDS(ISER_QP_MAX_REQ_DTOS);
  394. } else {
  395. init_attr.cap.max_send_wr = ib_dev->attrs.max_qp_wr;
  396. iser_conn->max_cmds =
  397. ISER_GET_MAX_XMIT_CMDS(ib_dev->attrs.max_qp_wr);
  398. iser_dbg("device %s supports max_send_wr %d\n",
  399. device->ib_device->name, ib_dev->attrs.max_qp_wr);
  400. }
  401. }
  402. ret = rdma_create_qp(ib_conn->cma_id, device->pd, &init_attr);
  403. if (ret)
  404. goto out_err;
  405. ib_conn->qp = ib_conn->cma_id->qp;
  406. iser_info("setting conn %p cma_id %p qp %p\n",
  407. ib_conn, ib_conn->cma_id,
  408. ib_conn->cma_id->qp);
  409. return ret;
  410. out_err:
  411. mutex_lock(&ig.connlist_mutex);
  412. ib_conn->comp->active_qps--;
  413. mutex_unlock(&ig.connlist_mutex);
  414. iser_err("unable to alloc mem or create resource, err %d\n", ret);
  415. return ret;
  416. }
  417. /**
  418. * based on the resolved device node GUID see if there already allocated
  419. * device for this device. If there's no such, create one.
  420. */
  421. static
  422. struct iser_device *iser_device_find_by_ib_device(struct rdma_cm_id *cma_id)
  423. {
  424. struct iser_device *device;
  425. mutex_lock(&ig.device_list_mutex);
  426. list_for_each_entry(device, &ig.device_list, ig_list)
  427. /* find if there's a match using the node GUID */
  428. if (device->ib_device->node_guid == cma_id->device->node_guid)
  429. goto inc_refcnt;
  430. device = kzalloc(sizeof *device, GFP_KERNEL);
  431. if (device == NULL)
  432. goto out;
  433. /* assign this device to the device */
  434. device->ib_device = cma_id->device;
  435. /* init the device and link it into ig device list */
  436. if (iser_create_device_ib_res(device)) {
  437. kfree(device);
  438. device = NULL;
  439. goto out;
  440. }
  441. list_add(&device->ig_list, &ig.device_list);
  442. inc_refcnt:
  443. device->refcount++;
  444. out:
  445. mutex_unlock(&ig.device_list_mutex);
  446. return device;
  447. }
  448. /* if there's no demand for this device, release it */
  449. static void iser_device_try_release(struct iser_device *device)
  450. {
  451. mutex_lock(&ig.device_list_mutex);
  452. device->refcount--;
  453. iser_info("device %p refcount %d\n", device, device->refcount);
  454. if (!device->refcount) {
  455. iser_free_device_ib_res(device);
  456. list_del(&device->ig_list);
  457. kfree(device);
  458. }
  459. mutex_unlock(&ig.device_list_mutex);
  460. }
  461. /**
  462. * Called with state mutex held
  463. **/
  464. static int iser_conn_state_comp_exch(struct iser_conn *iser_conn,
  465. enum iser_conn_state comp,
  466. enum iser_conn_state exch)
  467. {
  468. int ret;
  469. ret = (iser_conn->state == comp);
  470. if (ret)
  471. iser_conn->state = exch;
  472. return ret;
  473. }
  474. void iser_release_work(struct work_struct *work)
  475. {
  476. struct iser_conn *iser_conn;
  477. iser_conn = container_of(work, struct iser_conn, release_work);
  478. /* Wait for conn_stop to complete */
  479. wait_for_completion(&iser_conn->stop_completion);
  480. /* Wait for IB resouces cleanup to complete */
  481. wait_for_completion(&iser_conn->ib_completion);
  482. mutex_lock(&iser_conn->state_mutex);
  483. iser_conn->state = ISER_CONN_DOWN;
  484. mutex_unlock(&iser_conn->state_mutex);
  485. iser_conn_release(iser_conn);
  486. }
  487. /**
  488. * iser_free_ib_conn_res - release IB related resources
  489. * @iser_conn: iser connection struct
  490. * @destroy: indicator if we need to try to release the
  491. * iser device and memory regoins pool (only iscsi
  492. * shutdown and DEVICE_REMOVAL will use this).
  493. *
  494. * This routine is called with the iser state mutex held
  495. * so the cm_id removal is out of here. It is Safe to
  496. * be invoked multiple times.
  497. */
  498. static void iser_free_ib_conn_res(struct iser_conn *iser_conn,
  499. bool destroy)
  500. {
  501. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  502. struct iser_device *device = ib_conn->device;
  503. iser_info("freeing conn %p cma_id %p qp %p\n",
  504. iser_conn, ib_conn->cma_id, ib_conn->qp);
  505. if (ib_conn->qp != NULL) {
  506. mutex_lock(&ig.connlist_mutex);
  507. ib_conn->comp->active_qps--;
  508. mutex_unlock(&ig.connlist_mutex);
  509. rdma_destroy_qp(ib_conn->cma_id);
  510. ib_conn->qp = NULL;
  511. }
  512. if (destroy) {
  513. if (iser_conn->rx_descs)
  514. iser_free_rx_descriptors(iser_conn);
  515. if (device != NULL) {
  516. iser_device_try_release(device);
  517. ib_conn->device = NULL;
  518. }
  519. }
  520. }
  521. /**
  522. * Frees all conn objects and deallocs conn descriptor
  523. */
  524. void iser_conn_release(struct iser_conn *iser_conn)
  525. {
  526. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  527. mutex_lock(&ig.connlist_mutex);
  528. list_del(&iser_conn->conn_list);
  529. mutex_unlock(&ig.connlist_mutex);
  530. mutex_lock(&iser_conn->state_mutex);
  531. /* In case we endup here without ep_disconnect being invoked. */
  532. if (iser_conn->state != ISER_CONN_DOWN) {
  533. iser_warn("iser conn %p state %d, expected state down.\n",
  534. iser_conn, iser_conn->state);
  535. iscsi_destroy_endpoint(iser_conn->ep);
  536. iser_conn->state = ISER_CONN_DOWN;
  537. }
  538. /*
  539. * In case we never got to bind stage, we still need to
  540. * release IB resources (which is safe to call more than once).
  541. */
  542. iser_free_ib_conn_res(iser_conn, true);
  543. mutex_unlock(&iser_conn->state_mutex);
  544. if (ib_conn->cma_id != NULL) {
  545. rdma_destroy_id(ib_conn->cma_id);
  546. ib_conn->cma_id = NULL;
  547. }
  548. kfree(iser_conn);
  549. }
  550. /**
  551. * triggers start of the disconnect procedures and wait for them to be done
  552. * Called with state mutex held
  553. */
  554. int iser_conn_terminate(struct iser_conn *iser_conn)
  555. {
  556. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  557. int err = 0;
  558. /* terminate the iser conn only if the conn state is UP */
  559. if (!iser_conn_state_comp_exch(iser_conn, ISER_CONN_UP,
  560. ISER_CONN_TERMINATING))
  561. return 0;
  562. iser_info("iser_conn %p state %d\n", iser_conn, iser_conn->state);
  563. /* suspend queuing of new iscsi commands */
  564. if (iser_conn->iscsi_conn)
  565. iscsi_suspend_queue(iser_conn->iscsi_conn);
  566. /*
  567. * In case we didn't already clean up the cma_id (peer initiated
  568. * a disconnection), we need to Cause the CMA to change the QP
  569. * state to ERROR.
  570. */
  571. if (ib_conn->cma_id) {
  572. err = rdma_disconnect(ib_conn->cma_id);
  573. if (err)
  574. iser_err("Failed to disconnect, conn: 0x%p err %d\n",
  575. iser_conn, err);
  576. /* block until all flush errors are consumed */
  577. ib_drain_sq(ib_conn->qp);
  578. }
  579. return 1;
  580. }
  581. /**
  582. * Called with state mutex held
  583. **/
  584. static void iser_connect_error(struct rdma_cm_id *cma_id)
  585. {
  586. struct iser_conn *iser_conn;
  587. iser_conn = (struct iser_conn *)cma_id->context;
  588. iser_conn->state = ISER_CONN_TERMINATING;
  589. }
  590. static void
  591. iser_calc_scsi_params(struct iser_conn *iser_conn,
  592. unsigned int max_sectors)
  593. {
  594. struct iser_device *device = iser_conn->ib_conn.device;
  595. unsigned short sg_tablesize, sup_sg_tablesize;
  596. sg_tablesize = DIV_ROUND_UP(max_sectors * 512, SIZE_4K);
  597. sup_sg_tablesize = min_t(unsigned, ISCSI_ISER_MAX_SG_TABLESIZE,
  598. device->ib_device->attrs.max_fast_reg_page_list_len);
  599. iser_conn->scsi_sg_tablesize = min(sg_tablesize, sup_sg_tablesize);
  600. }
  601. /**
  602. * Called with state mutex held
  603. **/
  604. static void iser_addr_handler(struct rdma_cm_id *cma_id)
  605. {
  606. struct iser_device *device;
  607. struct iser_conn *iser_conn;
  608. struct ib_conn *ib_conn;
  609. int ret;
  610. iser_conn = (struct iser_conn *)cma_id->context;
  611. if (iser_conn->state != ISER_CONN_PENDING)
  612. /* bailout */
  613. return;
  614. ib_conn = &iser_conn->ib_conn;
  615. device = iser_device_find_by_ib_device(cma_id);
  616. if (!device) {
  617. iser_err("device lookup/creation failed\n");
  618. iser_connect_error(cma_id);
  619. return;
  620. }
  621. ib_conn->device = device;
  622. /* connection T10-PI support */
  623. if (iser_pi_enable) {
  624. if (!(device->ib_device->attrs.device_cap_flags &
  625. IB_DEVICE_SIGNATURE_HANDOVER)) {
  626. iser_warn("T10-PI requested but not supported on %s, "
  627. "continue without T10-PI\n",
  628. ib_conn->device->ib_device->name);
  629. ib_conn->pi_support = false;
  630. } else {
  631. ib_conn->pi_support = true;
  632. }
  633. }
  634. iser_calc_scsi_params(iser_conn, iser_max_sectors);
  635. ret = rdma_resolve_route(cma_id, 1000);
  636. if (ret) {
  637. iser_err("resolve route failed: %d\n", ret);
  638. iser_connect_error(cma_id);
  639. return;
  640. }
  641. }
  642. /**
  643. * Called with state mutex held
  644. **/
  645. static void iser_route_handler(struct rdma_cm_id *cma_id)
  646. {
  647. struct rdma_conn_param conn_param;
  648. int ret;
  649. struct iser_cm_hdr req_hdr;
  650. struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
  651. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  652. struct iser_device *device = ib_conn->device;
  653. if (iser_conn->state != ISER_CONN_PENDING)
  654. /* bailout */
  655. return;
  656. ret = iser_create_ib_conn_res(ib_conn);
  657. if (ret)
  658. goto failure;
  659. memset(&conn_param, 0, sizeof conn_param);
  660. conn_param.responder_resources = device->ib_device->attrs.max_qp_rd_atom;
  661. conn_param.initiator_depth = 1;
  662. conn_param.retry_count = 7;
  663. conn_param.rnr_retry_count = 6;
  664. memset(&req_hdr, 0, sizeof(req_hdr));
  665. req_hdr.flags = ISER_ZBVA_NOT_SUP;
  666. if (!device->remote_inv_sup)
  667. req_hdr.flags |= ISER_SEND_W_INV_NOT_SUP;
  668. conn_param.private_data = (void *)&req_hdr;
  669. conn_param.private_data_len = sizeof(struct iser_cm_hdr);
  670. ret = rdma_connect(cma_id, &conn_param);
  671. if (ret) {
  672. iser_err("failure connecting: %d\n", ret);
  673. goto failure;
  674. }
  675. return;
  676. failure:
  677. iser_connect_error(cma_id);
  678. }
  679. static void iser_connected_handler(struct rdma_cm_id *cma_id,
  680. const void *private_data)
  681. {
  682. struct iser_conn *iser_conn;
  683. struct ib_qp_attr attr;
  684. struct ib_qp_init_attr init_attr;
  685. iser_conn = (struct iser_conn *)cma_id->context;
  686. if (iser_conn->state != ISER_CONN_PENDING)
  687. /* bailout */
  688. return;
  689. (void)ib_query_qp(cma_id->qp, &attr, ~0, &init_attr);
  690. iser_info("remote qpn:%x my qpn:%x\n", attr.dest_qp_num, cma_id->qp->qp_num);
  691. if (private_data) {
  692. u8 flags = *(u8 *)private_data;
  693. iser_conn->snd_w_inv = !(flags & ISER_SEND_W_INV_NOT_SUP);
  694. }
  695. iser_info("conn %p: negotiated %s invalidation\n",
  696. iser_conn, iser_conn->snd_w_inv ? "remote" : "local");
  697. iser_conn->state = ISER_CONN_UP;
  698. complete(&iser_conn->up_completion);
  699. }
  700. static void iser_disconnected_handler(struct rdma_cm_id *cma_id)
  701. {
  702. struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
  703. if (iser_conn_terminate(iser_conn)) {
  704. if (iser_conn->iscsi_conn)
  705. iscsi_conn_failure(iser_conn->iscsi_conn,
  706. ISCSI_ERR_CONN_FAILED);
  707. else
  708. iser_err("iscsi_iser connection isn't bound\n");
  709. }
  710. }
  711. static void iser_cleanup_handler(struct rdma_cm_id *cma_id,
  712. bool destroy)
  713. {
  714. struct iser_conn *iser_conn = (struct iser_conn *)cma_id->context;
  715. /*
  716. * We are not guaranteed that we visited disconnected_handler
  717. * by now, call it here to be safe that we handle CM drep
  718. * and flush errors.
  719. */
  720. iser_disconnected_handler(cma_id);
  721. iser_free_ib_conn_res(iser_conn, destroy);
  722. complete(&iser_conn->ib_completion);
  723. };
  724. static int iser_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
  725. {
  726. struct iser_conn *iser_conn;
  727. int ret = 0;
  728. iser_conn = (struct iser_conn *)cma_id->context;
  729. iser_info("%s (%d): status %d conn %p id %p\n",
  730. rdma_event_msg(event->event), event->event,
  731. event->status, cma_id->context, cma_id);
  732. mutex_lock(&iser_conn->state_mutex);
  733. switch (event->event) {
  734. case RDMA_CM_EVENT_ADDR_RESOLVED:
  735. iser_addr_handler(cma_id);
  736. break;
  737. case RDMA_CM_EVENT_ROUTE_RESOLVED:
  738. iser_route_handler(cma_id);
  739. break;
  740. case RDMA_CM_EVENT_ESTABLISHED:
  741. iser_connected_handler(cma_id, event->param.conn.private_data);
  742. break;
  743. case RDMA_CM_EVENT_REJECTED:
  744. iser_info("Connection rejected: %s\n",
  745. rdma_reject_msg(cma_id, event->status));
  746. /* FALLTHROUGH */
  747. case RDMA_CM_EVENT_ADDR_ERROR:
  748. case RDMA_CM_EVENT_ROUTE_ERROR:
  749. case RDMA_CM_EVENT_CONNECT_ERROR:
  750. case RDMA_CM_EVENT_UNREACHABLE:
  751. iser_connect_error(cma_id);
  752. break;
  753. case RDMA_CM_EVENT_DISCONNECTED:
  754. case RDMA_CM_EVENT_ADDR_CHANGE:
  755. case RDMA_CM_EVENT_TIMEWAIT_EXIT:
  756. iser_cleanup_handler(cma_id, false);
  757. break;
  758. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  759. /*
  760. * we *must* destroy the device as we cannot rely
  761. * on iscsid to be around to initiate error handling.
  762. * also if we are not in state DOWN implicitly destroy
  763. * the cma_id.
  764. */
  765. iser_cleanup_handler(cma_id, true);
  766. if (iser_conn->state != ISER_CONN_DOWN) {
  767. iser_conn->ib_conn.cma_id = NULL;
  768. ret = 1;
  769. }
  770. break;
  771. default:
  772. iser_err("Unexpected RDMA CM event: %s (%d)\n",
  773. rdma_event_msg(event->event), event->event);
  774. break;
  775. }
  776. mutex_unlock(&iser_conn->state_mutex);
  777. return ret;
  778. }
  779. void iser_conn_init(struct iser_conn *iser_conn)
  780. {
  781. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  782. iser_conn->state = ISER_CONN_INIT;
  783. init_completion(&iser_conn->stop_completion);
  784. init_completion(&iser_conn->ib_completion);
  785. init_completion(&iser_conn->up_completion);
  786. INIT_LIST_HEAD(&iser_conn->conn_list);
  787. mutex_init(&iser_conn->state_mutex);
  788. ib_conn->post_recv_buf_count = 0;
  789. ib_conn->reg_cqe.done = iser_reg_comp;
  790. }
  791. /**
  792. * starts the process of connecting to the target
  793. * sleeps until the connection is established or rejected
  794. */
  795. int iser_connect(struct iser_conn *iser_conn,
  796. struct sockaddr *src_addr,
  797. struct sockaddr *dst_addr,
  798. int non_blocking)
  799. {
  800. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  801. int err = 0;
  802. mutex_lock(&iser_conn->state_mutex);
  803. sprintf(iser_conn->name, "%pISp", dst_addr);
  804. iser_info("connecting to: %s\n", iser_conn->name);
  805. /* the device is known only --after-- address resolution */
  806. ib_conn->device = NULL;
  807. iser_conn->state = ISER_CONN_PENDING;
  808. ib_conn->cma_id = rdma_create_id(&init_net, iser_cma_handler,
  809. (void *)iser_conn,
  810. RDMA_PS_TCP, IB_QPT_RC);
  811. if (IS_ERR(ib_conn->cma_id)) {
  812. err = PTR_ERR(ib_conn->cma_id);
  813. iser_err("rdma_create_id failed: %d\n", err);
  814. goto id_failure;
  815. }
  816. err = rdma_resolve_addr(ib_conn->cma_id, src_addr, dst_addr, 1000);
  817. if (err) {
  818. iser_err("rdma_resolve_addr failed: %d\n", err);
  819. goto addr_failure;
  820. }
  821. if (!non_blocking) {
  822. wait_for_completion_interruptible(&iser_conn->up_completion);
  823. if (iser_conn->state != ISER_CONN_UP) {
  824. err = -EIO;
  825. goto connect_failure;
  826. }
  827. }
  828. mutex_unlock(&iser_conn->state_mutex);
  829. mutex_lock(&ig.connlist_mutex);
  830. list_add(&iser_conn->conn_list, &ig.connlist);
  831. mutex_unlock(&ig.connlist_mutex);
  832. return 0;
  833. id_failure:
  834. ib_conn->cma_id = NULL;
  835. addr_failure:
  836. iser_conn->state = ISER_CONN_DOWN;
  837. connect_failure:
  838. mutex_unlock(&iser_conn->state_mutex);
  839. iser_conn_release(iser_conn);
  840. return err;
  841. }
  842. int iser_post_recvl(struct iser_conn *iser_conn)
  843. {
  844. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  845. struct iser_login_desc *desc = &iser_conn->login_desc;
  846. struct ib_recv_wr wr, *wr_failed;
  847. int ib_ret;
  848. desc->sge.addr = desc->rsp_dma;
  849. desc->sge.length = ISER_RX_LOGIN_SIZE;
  850. desc->sge.lkey = ib_conn->device->pd->local_dma_lkey;
  851. desc->cqe.done = iser_login_rsp;
  852. wr.wr_cqe = &desc->cqe;
  853. wr.sg_list = &desc->sge;
  854. wr.num_sge = 1;
  855. wr.next = NULL;
  856. ib_conn->post_recv_buf_count++;
  857. ib_ret = ib_post_recv(ib_conn->qp, &wr, &wr_failed);
  858. if (ib_ret) {
  859. iser_err("ib_post_recv failed ret=%d\n", ib_ret);
  860. ib_conn->post_recv_buf_count--;
  861. }
  862. return ib_ret;
  863. }
  864. int iser_post_recvm(struct iser_conn *iser_conn, int count)
  865. {
  866. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  867. unsigned int my_rx_head = iser_conn->rx_desc_head;
  868. struct iser_rx_desc *rx_desc;
  869. struct ib_recv_wr *wr, *wr_failed;
  870. int i, ib_ret;
  871. for (wr = ib_conn->rx_wr, i = 0; i < count; i++, wr++) {
  872. rx_desc = &iser_conn->rx_descs[my_rx_head];
  873. rx_desc->cqe.done = iser_task_rsp;
  874. wr->wr_cqe = &rx_desc->cqe;
  875. wr->sg_list = &rx_desc->rx_sg;
  876. wr->num_sge = 1;
  877. wr->next = wr + 1;
  878. my_rx_head = (my_rx_head + 1) & iser_conn->qp_max_recv_dtos_mask;
  879. }
  880. wr--;
  881. wr->next = NULL; /* mark end of work requests list */
  882. ib_conn->post_recv_buf_count += count;
  883. ib_ret = ib_post_recv(ib_conn->qp, ib_conn->rx_wr, &wr_failed);
  884. if (ib_ret) {
  885. iser_err("ib_post_recv failed ret=%d\n", ib_ret);
  886. ib_conn->post_recv_buf_count -= count;
  887. } else
  888. iser_conn->rx_desc_head = my_rx_head;
  889. return ib_ret;
  890. }
  891. /**
  892. * iser_start_send - Initiate a Send DTO operation
  893. *
  894. * returns 0 on success, -1 on failure
  895. */
  896. int iser_post_send(struct ib_conn *ib_conn, struct iser_tx_desc *tx_desc,
  897. bool signal)
  898. {
  899. struct ib_send_wr *bad_wr, *wr = iser_tx_next_wr(tx_desc);
  900. int ib_ret;
  901. ib_dma_sync_single_for_device(ib_conn->device->ib_device,
  902. tx_desc->dma_addr, ISER_HEADERS_LEN,
  903. DMA_TO_DEVICE);
  904. wr->next = NULL;
  905. wr->wr_cqe = &tx_desc->cqe;
  906. wr->sg_list = tx_desc->tx_sg;
  907. wr->num_sge = tx_desc->num_sge;
  908. wr->opcode = IB_WR_SEND;
  909. wr->send_flags = signal ? IB_SEND_SIGNALED : 0;
  910. ib_ret = ib_post_send(ib_conn->qp, &tx_desc->wrs[0].send, &bad_wr);
  911. if (ib_ret)
  912. iser_err("ib_post_send failed, ret:%d opcode:%d\n",
  913. ib_ret, bad_wr->opcode);
  914. return ib_ret;
  915. }
  916. u8 iser_check_task_pi_status(struct iscsi_iser_task *iser_task,
  917. enum iser_data_dir cmd_dir, sector_t *sector)
  918. {
  919. struct iser_mem_reg *reg = &iser_task->rdma_reg[cmd_dir];
  920. struct iser_fr_desc *desc = reg->mem_h;
  921. unsigned long sector_size = iser_task->sc->device->sector_size;
  922. struct ib_mr_status mr_status;
  923. int ret;
  924. if (desc && desc->pi_ctx->sig_protected) {
  925. desc->pi_ctx->sig_protected = 0;
  926. ret = ib_check_mr_status(desc->pi_ctx->sig_mr,
  927. IB_MR_CHECK_SIG_STATUS, &mr_status);
  928. if (ret) {
  929. pr_err("ib_check_mr_status failed, ret %d\n", ret);
  930. goto err;
  931. }
  932. if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
  933. sector_t sector_off = mr_status.sig_err.sig_err_offset;
  934. sector_div(sector_off, sector_size + 8);
  935. *sector = scsi_get_lba(iser_task->sc) + sector_off;
  936. pr_err("PI error found type %d at sector %llx "
  937. "expected %x vs actual %x\n",
  938. mr_status.sig_err.err_type,
  939. (unsigned long long)*sector,
  940. mr_status.sig_err.expected,
  941. mr_status.sig_err.actual);
  942. switch (mr_status.sig_err.err_type) {
  943. case IB_SIG_BAD_GUARD:
  944. return 0x1;
  945. case IB_SIG_BAD_REFTAG:
  946. return 0x3;
  947. case IB_SIG_BAD_APPTAG:
  948. return 0x2;
  949. }
  950. }
  951. }
  952. return 0;
  953. err:
  954. /* Not alot we can do here, return ambiguous guard error */
  955. return 0x1;
  956. }
  957. void iser_err_comp(struct ib_wc *wc, const char *type)
  958. {
  959. if (wc->status != IB_WC_WR_FLUSH_ERR) {
  960. struct iser_conn *iser_conn = to_iser_conn(wc->qp->qp_context);
  961. iser_err("%s failure: %s (%d) vend_err %x\n", type,
  962. ib_wc_status_msg(wc->status), wc->status,
  963. wc->vendor_err);
  964. if (iser_conn->iscsi_conn)
  965. iscsi_conn_failure(iser_conn->iscsi_conn,
  966. ISCSI_ERR_CONN_FAILED);
  967. } else {
  968. iser_dbg("%s failure: %s (%d)\n", type,
  969. ib_wc_status_msg(wc->status), wc->status);
  970. }
  971. }