iser_verbs.c 29 KB

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