iser_verbs.c 30 KB

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