iser_verbs.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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 fast_reg_page_failure;
  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. goto fast_reg_mr_failure;
  274. }
  275. desc->valid = true;
  276. list_add_tail(&desc->list, &ib_conn->fastreg.frwr.pool);
  277. ib_conn->fastreg.frwr.pool_size++;
  278. }
  279. return 0;
  280. fast_reg_mr_failure:
  281. ib_free_fast_reg_page_list(desc->data_frpl);
  282. fast_reg_page_failure:
  283. kfree(desc);
  284. err:
  285. iser_free_frwr_pool(ib_conn);
  286. return ret;
  287. }
  288. /**
  289. * iser_free_frwr_pool - releases the pool of fast_reg descriptors
  290. */
  291. void iser_free_frwr_pool(struct iser_conn *ib_conn)
  292. {
  293. struct fast_reg_descriptor *desc, *tmp;
  294. int i = 0;
  295. if (list_empty(&ib_conn->fastreg.frwr.pool))
  296. return;
  297. iser_info("freeing conn %p frwr pool\n", ib_conn);
  298. list_for_each_entry_safe(desc, tmp, &ib_conn->fastreg.frwr.pool, list) {
  299. list_del(&desc->list);
  300. ib_free_fast_reg_page_list(desc->data_frpl);
  301. ib_dereg_mr(desc->data_mr);
  302. kfree(desc);
  303. ++i;
  304. }
  305. if (i < ib_conn->fastreg.frwr.pool_size)
  306. iser_warn("pool still has %d regions registered\n",
  307. ib_conn->fastreg.frwr.pool_size - i);
  308. }
  309. /**
  310. * iser_create_ib_conn_res - Queue-Pair (QP)
  311. *
  312. * returns 0 on success, -1 on failure
  313. */
  314. static int iser_create_ib_conn_res(struct iser_conn *ib_conn)
  315. {
  316. struct iser_device *device;
  317. struct ib_qp_init_attr init_attr;
  318. int ret = -ENOMEM;
  319. int index, min_index = 0;
  320. BUG_ON(ib_conn->device == NULL);
  321. device = ib_conn->device;
  322. memset(&init_attr, 0, sizeof init_attr);
  323. mutex_lock(&ig.connlist_mutex);
  324. /* select the CQ with the minimal number of usages */
  325. for (index = 0; index < device->cqs_used; index++)
  326. if (device->cq_active_qps[index] <
  327. device->cq_active_qps[min_index])
  328. min_index = index;
  329. device->cq_active_qps[min_index]++;
  330. mutex_unlock(&ig.connlist_mutex);
  331. iser_info("cq index %d used for ib_conn %p\n", min_index, ib_conn);
  332. init_attr.event_handler = iser_qp_event_callback;
  333. init_attr.qp_context = (void *)ib_conn;
  334. init_attr.send_cq = device->tx_cq[min_index];
  335. init_attr.recv_cq = device->rx_cq[min_index];
  336. init_attr.cap.max_send_wr = ISER_QP_MAX_REQ_DTOS;
  337. init_attr.cap.max_recv_wr = ISER_QP_MAX_RECV_DTOS;
  338. init_attr.cap.max_send_sge = 2;
  339. init_attr.cap.max_recv_sge = 1;
  340. init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  341. init_attr.qp_type = IB_QPT_RC;
  342. ret = rdma_create_qp(ib_conn->cma_id, device->pd, &init_attr);
  343. if (ret)
  344. goto out_err;
  345. ib_conn->qp = ib_conn->cma_id->qp;
  346. iser_info("setting conn %p cma_id %p qp %p\n",
  347. ib_conn, ib_conn->cma_id,
  348. ib_conn->cma_id->qp);
  349. return ret;
  350. out_err:
  351. iser_err("unable to alloc mem or create resource, err %d\n", ret);
  352. return ret;
  353. }
  354. /**
  355. * releases the QP objects, returns 0 on success,
  356. * -1 on failure
  357. */
  358. static int iser_free_ib_conn_res(struct iser_conn *ib_conn)
  359. {
  360. int cq_index;
  361. BUG_ON(ib_conn == NULL);
  362. iser_info("freeing conn %p cma_id %p qp %p\n",
  363. ib_conn, ib_conn->cma_id,
  364. ib_conn->qp);
  365. /* qp is created only once both addr & route are resolved */
  366. if (ib_conn->qp != NULL) {
  367. cq_index = ((struct iser_cq_desc *)ib_conn->qp->recv_cq->cq_context)->cq_index;
  368. ib_conn->device->cq_active_qps[cq_index]--;
  369. rdma_destroy_qp(ib_conn->cma_id);
  370. }
  371. ib_conn->qp = NULL;
  372. return 0;
  373. }
  374. /**
  375. * based on the resolved device node GUID see if there already allocated
  376. * device for this device. If there's no such, create one.
  377. */
  378. static
  379. struct iser_device *iser_device_find_by_ib_device(struct rdma_cm_id *cma_id)
  380. {
  381. struct iser_device *device;
  382. mutex_lock(&ig.device_list_mutex);
  383. list_for_each_entry(device, &ig.device_list, ig_list)
  384. /* find if there's a match using the node GUID */
  385. if (device->ib_device->node_guid == cma_id->device->node_guid)
  386. goto inc_refcnt;
  387. device = kzalloc(sizeof *device, GFP_KERNEL);
  388. if (device == NULL)
  389. goto out;
  390. /* assign this device to the device */
  391. device->ib_device = cma_id->device;
  392. /* init the device and link it into ig device list */
  393. if (iser_create_device_ib_res(device)) {
  394. kfree(device);
  395. device = NULL;
  396. goto out;
  397. }
  398. list_add(&device->ig_list, &ig.device_list);
  399. inc_refcnt:
  400. device->refcount++;
  401. out:
  402. mutex_unlock(&ig.device_list_mutex);
  403. return device;
  404. }
  405. /* if there's no demand for this device, release it */
  406. static void iser_device_try_release(struct iser_device *device)
  407. {
  408. mutex_lock(&ig.device_list_mutex);
  409. device->refcount--;
  410. iser_info("device %p refcount %d\n", device, device->refcount);
  411. if (!device->refcount) {
  412. iser_free_device_ib_res(device);
  413. list_del(&device->ig_list);
  414. kfree(device);
  415. }
  416. mutex_unlock(&ig.device_list_mutex);
  417. }
  418. static int iser_conn_state_comp_exch(struct iser_conn *ib_conn,
  419. enum iser_ib_conn_state comp,
  420. enum iser_ib_conn_state exch)
  421. {
  422. int ret;
  423. spin_lock_bh(&ib_conn->lock);
  424. if ((ret = (ib_conn->state == comp)))
  425. ib_conn->state = exch;
  426. spin_unlock_bh(&ib_conn->lock);
  427. return ret;
  428. }
  429. /**
  430. * Frees all conn objects and deallocs conn descriptor
  431. */
  432. static void iser_conn_release(struct iser_conn *ib_conn, int can_destroy_id)
  433. {
  434. struct iser_device *device = ib_conn->device;
  435. BUG_ON(ib_conn->state != ISER_CONN_DOWN);
  436. mutex_lock(&ig.connlist_mutex);
  437. list_del(&ib_conn->conn_list);
  438. mutex_unlock(&ig.connlist_mutex);
  439. iser_free_rx_descriptors(ib_conn);
  440. iser_free_ib_conn_res(ib_conn);
  441. ib_conn->device = NULL;
  442. /* on EVENT_ADDR_ERROR there's no device yet for this conn */
  443. if (device != NULL)
  444. iser_device_try_release(device);
  445. /* if cma handler context, the caller actually destroy the id */
  446. if (ib_conn->cma_id != NULL && can_destroy_id) {
  447. rdma_destroy_id(ib_conn->cma_id);
  448. ib_conn->cma_id = NULL;
  449. }
  450. iscsi_destroy_endpoint(ib_conn->ep);
  451. }
  452. void iser_conn_get(struct iser_conn *ib_conn)
  453. {
  454. atomic_inc(&ib_conn->refcount);
  455. }
  456. int iser_conn_put(struct iser_conn *ib_conn, int can_destroy_id)
  457. {
  458. if (atomic_dec_and_test(&ib_conn->refcount)) {
  459. iser_conn_release(ib_conn, can_destroy_id);
  460. return 1;
  461. }
  462. return 0;
  463. }
  464. /**
  465. * triggers start of the disconnect procedures and wait for them to be done
  466. */
  467. void iser_conn_terminate(struct iser_conn *ib_conn)
  468. {
  469. int err = 0;
  470. /* change the ib conn state only if the conn is UP, however always call
  471. * rdma_disconnect since this is the only way to cause the CMA to change
  472. * the QP state to ERROR
  473. */
  474. iser_conn_state_comp_exch(ib_conn, ISER_CONN_UP, ISER_CONN_TERMINATING);
  475. err = rdma_disconnect(ib_conn->cma_id);
  476. if (err)
  477. iser_err("Failed to disconnect, conn: 0x%p err %d\n",
  478. ib_conn,err);
  479. wait_event_interruptible(ib_conn->wait,
  480. ib_conn->state == ISER_CONN_DOWN);
  481. iser_conn_put(ib_conn, 1); /* deref ib conn deallocate */
  482. }
  483. static int iser_connect_error(struct rdma_cm_id *cma_id)
  484. {
  485. struct iser_conn *ib_conn;
  486. ib_conn = (struct iser_conn *)cma_id->context;
  487. ib_conn->state = ISER_CONN_DOWN;
  488. wake_up_interruptible(&ib_conn->wait);
  489. return iser_conn_put(ib_conn, 0); /* deref ib conn's cma id */
  490. }
  491. static int iser_addr_handler(struct rdma_cm_id *cma_id)
  492. {
  493. struct iser_device *device;
  494. struct iser_conn *ib_conn;
  495. int ret;
  496. device = iser_device_find_by_ib_device(cma_id);
  497. if (!device) {
  498. iser_err("device lookup/creation failed\n");
  499. return iser_connect_error(cma_id);
  500. }
  501. ib_conn = (struct iser_conn *)cma_id->context;
  502. ib_conn->device = device;
  503. ret = rdma_resolve_route(cma_id, 1000);
  504. if (ret) {
  505. iser_err("resolve route failed: %d\n", ret);
  506. return iser_connect_error(cma_id);
  507. }
  508. return 0;
  509. }
  510. static int iser_route_handler(struct rdma_cm_id *cma_id)
  511. {
  512. struct rdma_conn_param conn_param;
  513. int ret;
  514. struct iser_cm_hdr req_hdr;
  515. ret = iser_create_ib_conn_res((struct iser_conn *)cma_id->context);
  516. if (ret)
  517. goto failure;
  518. memset(&conn_param, 0, sizeof conn_param);
  519. conn_param.responder_resources = 4;
  520. conn_param.initiator_depth = 1;
  521. conn_param.retry_count = 7;
  522. conn_param.rnr_retry_count = 6;
  523. memset(&req_hdr, 0, sizeof(req_hdr));
  524. req_hdr.flags = (ISER_ZBVA_NOT_SUPPORTED |
  525. ISER_SEND_W_INV_NOT_SUPPORTED);
  526. conn_param.private_data = (void *)&req_hdr;
  527. conn_param.private_data_len = sizeof(struct iser_cm_hdr);
  528. ret = rdma_connect(cma_id, &conn_param);
  529. if (ret) {
  530. iser_err("failure connecting: %d\n", ret);
  531. goto failure;
  532. }
  533. return 0;
  534. failure:
  535. return iser_connect_error(cma_id);
  536. }
  537. static void iser_connected_handler(struct rdma_cm_id *cma_id)
  538. {
  539. struct iser_conn *ib_conn;
  540. ib_conn = (struct iser_conn *)cma_id->context;
  541. ib_conn->state = ISER_CONN_UP;
  542. wake_up_interruptible(&ib_conn->wait);
  543. }
  544. static int iser_disconnected_handler(struct rdma_cm_id *cma_id)
  545. {
  546. struct iser_conn *ib_conn;
  547. int ret;
  548. ib_conn = (struct iser_conn *)cma_id->context;
  549. /* getting here when the state is UP means that the conn is being *
  550. * terminated asynchronously from the iSCSI layer's perspective. */
  551. if (iser_conn_state_comp_exch(ib_conn, ISER_CONN_UP,
  552. ISER_CONN_TERMINATING)){
  553. if (ib_conn->iser_conn)
  554. iscsi_conn_failure(ib_conn->iser_conn->iscsi_conn,
  555. ISCSI_ERR_CONN_FAILED);
  556. else
  557. iser_err("iscsi_iser connection isn't bound\n");
  558. }
  559. /* Complete the termination process if no posts are pending */
  560. if (ib_conn->post_recv_buf_count == 0 &&
  561. (atomic_read(&ib_conn->post_send_buf_count) == 0)) {
  562. ib_conn->state = ISER_CONN_DOWN;
  563. wake_up_interruptible(&ib_conn->wait);
  564. }
  565. ret = iser_conn_put(ib_conn, 0); /* deref ib conn's cma id */
  566. return ret;
  567. }
  568. static int iser_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
  569. {
  570. int ret = 0;
  571. iser_info("event %d status %d conn %p id %p\n",
  572. event->event, event->status, cma_id->context, cma_id);
  573. switch (event->event) {
  574. case RDMA_CM_EVENT_ADDR_RESOLVED:
  575. ret = iser_addr_handler(cma_id);
  576. break;
  577. case RDMA_CM_EVENT_ROUTE_RESOLVED:
  578. ret = iser_route_handler(cma_id);
  579. break;
  580. case RDMA_CM_EVENT_ESTABLISHED:
  581. iser_connected_handler(cma_id);
  582. break;
  583. case RDMA_CM_EVENT_ADDR_ERROR:
  584. case RDMA_CM_EVENT_ROUTE_ERROR:
  585. case RDMA_CM_EVENT_CONNECT_ERROR:
  586. case RDMA_CM_EVENT_UNREACHABLE:
  587. case RDMA_CM_EVENT_REJECTED:
  588. ret = iser_connect_error(cma_id);
  589. break;
  590. case RDMA_CM_EVENT_DISCONNECTED:
  591. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  592. case RDMA_CM_EVENT_ADDR_CHANGE:
  593. ret = iser_disconnected_handler(cma_id);
  594. break;
  595. default:
  596. iser_err("Unexpected RDMA CM event (%d)\n", event->event);
  597. break;
  598. }
  599. return ret;
  600. }
  601. void iser_conn_init(struct iser_conn *ib_conn)
  602. {
  603. ib_conn->state = ISER_CONN_INIT;
  604. init_waitqueue_head(&ib_conn->wait);
  605. ib_conn->post_recv_buf_count = 0;
  606. atomic_set(&ib_conn->post_send_buf_count, 0);
  607. atomic_set(&ib_conn->refcount, 1); /* ref ib conn allocation */
  608. INIT_LIST_HEAD(&ib_conn->conn_list);
  609. spin_lock_init(&ib_conn->lock);
  610. }
  611. /**
  612. * starts the process of connecting to the target
  613. * sleeps until the connection is established or rejected
  614. */
  615. int iser_connect(struct iser_conn *ib_conn,
  616. struct sockaddr_in *src_addr,
  617. struct sockaddr_in *dst_addr,
  618. int non_blocking)
  619. {
  620. struct sockaddr *src, *dst;
  621. int err = 0;
  622. sprintf(ib_conn->name, "%pI4:%d",
  623. &dst_addr->sin_addr.s_addr, dst_addr->sin_port);
  624. /* the device is known only --after-- address resolution */
  625. ib_conn->device = NULL;
  626. iser_info("connecting to: %pI4, port 0x%x\n",
  627. &dst_addr->sin_addr, dst_addr->sin_port);
  628. ib_conn->state = ISER_CONN_PENDING;
  629. iser_conn_get(ib_conn); /* ref ib conn's cma id */
  630. ib_conn->cma_id = rdma_create_id(iser_cma_handler,
  631. (void *)ib_conn,
  632. RDMA_PS_TCP, IB_QPT_RC);
  633. if (IS_ERR(ib_conn->cma_id)) {
  634. err = PTR_ERR(ib_conn->cma_id);
  635. iser_err("rdma_create_id failed: %d\n", err);
  636. goto id_failure;
  637. }
  638. src = (struct sockaddr *)src_addr;
  639. dst = (struct sockaddr *)dst_addr;
  640. err = rdma_resolve_addr(ib_conn->cma_id, src, dst, 1000);
  641. if (err) {
  642. iser_err("rdma_resolve_addr failed: %d\n", err);
  643. goto addr_failure;
  644. }
  645. if (!non_blocking) {
  646. wait_event_interruptible(ib_conn->wait,
  647. (ib_conn->state != ISER_CONN_PENDING));
  648. if (ib_conn->state != ISER_CONN_UP) {
  649. err = -EIO;
  650. goto connect_failure;
  651. }
  652. }
  653. mutex_lock(&ig.connlist_mutex);
  654. list_add(&ib_conn->conn_list, &ig.connlist);
  655. mutex_unlock(&ig.connlist_mutex);
  656. return 0;
  657. id_failure:
  658. ib_conn->cma_id = NULL;
  659. addr_failure:
  660. ib_conn->state = ISER_CONN_DOWN;
  661. iser_conn_put(ib_conn, 1); /* deref ib conn's cma id */
  662. connect_failure:
  663. iser_conn_put(ib_conn, 1); /* deref ib conn deallocate */
  664. return err;
  665. }
  666. /**
  667. * iser_reg_page_vec - Register physical memory
  668. *
  669. * returns: 0 on success, errno code on failure
  670. */
  671. int iser_reg_page_vec(struct iser_conn *ib_conn,
  672. struct iser_page_vec *page_vec,
  673. struct iser_mem_reg *mem_reg)
  674. {
  675. struct ib_pool_fmr *mem;
  676. u64 io_addr;
  677. u64 *page_list;
  678. int status;
  679. page_list = page_vec->pages;
  680. io_addr = page_list[0];
  681. mem = ib_fmr_pool_map_phys(ib_conn->fastreg.fmr.pool,
  682. page_list,
  683. page_vec->length,
  684. io_addr);
  685. if (IS_ERR(mem)) {
  686. status = (int)PTR_ERR(mem);
  687. iser_err("ib_fmr_pool_map_phys failed: %d\n", status);
  688. return status;
  689. }
  690. mem_reg->lkey = mem->fmr->lkey;
  691. mem_reg->rkey = mem->fmr->rkey;
  692. mem_reg->len = page_vec->length * SIZE_4K;
  693. mem_reg->va = io_addr;
  694. mem_reg->is_mr = 1;
  695. mem_reg->mem_h = (void *)mem;
  696. mem_reg->va += page_vec->offset;
  697. mem_reg->len = page_vec->data_size;
  698. iser_dbg("PHYSICAL Mem.register, [PHYS p_array: 0x%p, sz: %d, "
  699. "entry[0]: (0x%08lx,%ld)] -> "
  700. "[lkey: 0x%08X mem_h: 0x%p va: 0x%08lX sz: %ld]\n",
  701. page_vec, page_vec->length,
  702. (unsigned long)page_vec->pages[0],
  703. (unsigned long)page_vec->data_size,
  704. (unsigned int)mem_reg->lkey, mem_reg->mem_h,
  705. (unsigned long)mem_reg->va, (unsigned long)mem_reg->len);
  706. return 0;
  707. }
  708. /**
  709. * Unregister (previosuly registered using FMR) memory.
  710. * If memory is non-FMR does nothing.
  711. */
  712. void iser_unreg_mem_fmr(struct iscsi_iser_task *iser_task,
  713. enum iser_data_dir cmd_dir)
  714. {
  715. struct iser_mem_reg *reg = &iser_task->rdma_regd[cmd_dir].reg;
  716. int ret;
  717. if (!reg->is_mr)
  718. return;
  719. iser_dbg("PHYSICAL Mem.Unregister mem_h %p\n",reg->mem_h);
  720. ret = ib_fmr_pool_unmap((struct ib_pool_fmr *)reg->mem_h);
  721. if (ret)
  722. iser_err("ib_fmr_pool_unmap failed %d\n", ret);
  723. reg->mem_h = NULL;
  724. }
  725. void iser_unreg_mem_frwr(struct iscsi_iser_task *iser_task,
  726. enum iser_data_dir cmd_dir)
  727. {
  728. struct iser_mem_reg *reg = &iser_task->rdma_regd[cmd_dir].reg;
  729. struct iser_conn *ib_conn = iser_task->iser_conn->ib_conn;
  730. struct fast_reg_descriptor *desc = reg->mem_h;
  731. if (!reg->is_mr)
  732. return;
  733. reg->mem_h = NULL;
  734. reg->is_mr = 0;
  735. spin_lock_bh(&ib_conn->lock);
  736. list_add_tail(&desc->list, &ib_conn->fastreg.frwr.pool);
  737. spin_unlock_bh(&ib_conn->lock);
  738. }
  739. int iser_post_recvl(struct iser_conn *ib_conn)
  740. {
  741. struct ib_recv_wr rx_wr, *rx_wr_failed;
  742. struct ib_sge sge;
  743. int ib_ret;
  744. sge.addr = ib_conn->login_resp_dma;
  745. sge.length = ISER_RX_LOGIN_SIZE;
  746. sge.lkey = ib_conn->device->mr->lkey;
  747. rx_wr.wr_id = (unsigned long)ib_conn->login_resp_buf;
  748. rx_wr.sg_list = &sge;
  749. rx_wr.num_sge = 1;
  750. rx_wr.next = NULL;
  751. ib_conn->post_recv_buf_count++;
  752. ib_ret = ib_post_recv(ib_conn->qp, &rx_wr, &rx_wr_failed);
  753. if (ib_ret) {
  754. iser_err("ib_post_recv failed ret=%d\n", ib_ret);
  755. ib_conn->post_recv_buf_count--;
  756. }
  757. return ib_ret;
  758. }
  759. int iser_post_recvm(struct iser_conn *ib_conn, int count)
  760. {
  761. struct ib_recv_wr *rx_wr, *rx_wr_failed;
  762. int i, ib_ret;
  763. unsigned int my_rx_head = ib_conn->rx_desc_head;
  764. struct iser_rx_desc *rx_desc;
  765. for (rx_wr = ib_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
  766. rx_desc = &ib_conn->rx_descs[my_rx_head];
  767. rx_wr->wr_id = (unsigned long)rx_desc;
  768. rx_wr->sg_list = &rx_desc->rx_sg;
  769. rx_wr->num_sge = 1;
  770. rx_wr->next = rx_wr + 1;
  771. my_rx_head = (my_rx_head + 1) & ib_conn->qp_max_recv_dtos_mask;
  772. }
  773. rx_wr--;
  774. rx_wr->next = NULL; /* mark end of work requests list */
  775. ib_conn->post_recv_buf_count += count;
  776. ib_ret = ib_post_recv(ib_conn->qp, ib_conn->rx_wr, &rx_wr_failed);
  777. if (ib_ret) {
  778. iser_err("ib_post_recv failed ret=%d\n", ib_ret);
  779. ib_conn->post_recv_buf_count -= count;
  780. } else
  781. ib_conn->rx_desc_head = my_rx_head;
  782. return ib_ret;
  783. }
  784. /**
  785. * iser_start_send - Initiate a Send DTO operation
  786. *
  787. * returns 0 on success, -1 on failure
  788. */
  789. int iser_post_send(struct iser_conn *ib_conn, struct iser_tx_desc *tx_desc)
  790. {
  791. int ib_ret;
  792. struct ib_send_wr send_wr, *send_wr_failed;
  793. ib_dma_sync_single_for_device(ib_conn->device->ib_device,
  794. tx_desc->dma_addr, ISER_HEADERS_LEN, DMA_TO_DEVICE);
  795. send_wr.next = NULL;
  796. send_wr.wr_id = (unsigned long)tx_desc;
  797. send_wr.sg_list = tx_desc->tx_sg;
  798. send_wr.num_sge = tx_desc->num_sge;
  799. send_wr.opcode = IB_WR_SEND;
  800. send_wr.send_flags = IB_SEND_SIGNALED;
  801. atomic_inc(&ib_conn->post_send_buf_count);
  802. ib_ret = ib_post_send(ib_conn->qp, &send_wr, &send_wr_failed);
  803. if (ib_ret) {
  804. iser_err("ib_post_send failed, ret:%d\n", ib_ret);
  805. atomic_dec(&ib_conn->post_send_buf_count);
  806. }
  807. return ib_ret;
  808. }
  809. static void iser_handle_comp_error(struct iser_tx_desc *desc,
  810. struct iser_conn *ib_conn)
  811. {
  812. if (desc && desc->type == ISCSI_TX_DATAOUT)
  813. kmem_cache_free(ig.desc_cache, desc);
  814. if (ib_conn->post_recv_buf_count == 0 &&
  815. atomic_read(&ib_conn->post_send_buf_count) == 0) {
  816. /* getting here when the state is UP means that the conn is *
  817. * being terminated asynchronously from the iSCSI layer's *
  818. * perspective. */
  819. if (iser_conn_state_comp_exch(ib_conn, ISER_CONN_UP,
  820. ISER_CONN_TERMINATING))
  821. iscsi_conn_failure(ib_conn->iser_conn->iscsi_conn,
  822. ISCSI_ERR_CONN_FAILED);
  823. /* no more non completed posts to the QP, complete the
  824. * termination process w.o worrying on disconnect event */
  825. ib_conn->state = ISER_CONN_DOWN;
  826. wake_up_interruptible(&ib_conn->wait);
  827. }
  828. }
  829. static int iser_drain_tx_cq(struct iser_device *device, int cq_index)
  830. {
  831. struct ib_cq *cq = device->tx_cq[cq_index];
  832. struct ib_wc wc;
  833. struct iser_tx_desc *tx_desc;
  834. struct iser_conn *ib_conn;
  835. int completed_tx = 0;
  836. while (ib_poll_cq(cq, 1, &wc) == 1) {
  837. tx_desc = (struct iser_tx_desc *) (unsigned long) wc.wr_id;
  838. ib_conn = wc.qp->qp_context;
  839. if (wc.status == IB_WC_SUCCESS) {
  840. if (wc.opcode == IB_WC_SEND)
  841. iser_snd_completion(tx_desc, ib_conn);
  842. else if (wc.opcode == IB_WC_LOCAL_INV ||
  843. wc.opcode == IB_WC_FAST_REG_MR) {
  844. atomic_dec(&ib_conn->post_send_buf_count);
  845. continue;
  846. } else
  847. iser_err("expected opcode %d got %d\n",
  848. IB_WC_SEND, wc.opcode);
  849. } else {
  850. iser_err("tx id %llx status %d vend_err %x\n",
  851. wc.wr_id, wc.status, wc.vendor_err);
  852. atomic_dec(&ib_conn->post_send_buf_count);
  853. iser_handle_comp_error(tx_desc, ib_conn);
  854. }
  855. completed_tx++;
  856. }
  857. return completed_tx;
  858. }
  859. static void iser_cq_tasklet_fn(unsigned long data)
  860. {
  861. struct iser_cq_desc *cq_desc = (struct iser_cq_desc *)data;
  862. struct iser_device *device = cq_desc->device;
  863. int cq_index = cq_desc->cq_index;
  864. struct ib_cq *cq = device->rx_cq[cq_index];
  865. struct ib_wc wc;
  866. struct iser_rx_desc *desc;
  867. unsigned long xfer_len;
  868. struct iser_conn *ib_conn;
  869. int completed_tx, completed_rx;
  870. completed_tx = completed_rx = 0;
  871. while (ib_poll_cq(cq, 1, &wc) == 1) {
  872. desc = (struct iser_rx_desc *) (unsigned long) wc.wr_id;
  873. BUG_ON(desc == NULL);
  874. ib_conn = wc.qp->qp_context;
  875. if (wc.status == IB_WC_SUCCESS) {
  876. if (wc.opcode == IB_WC_RECV) {
  877. xfer_len = (unsigned long)wc.byte_len;
  878. iser_rcv_completion(desc, xfer_len, ib_conn);
  879. } else
  880. iser_err("expected opcode %d got %d\n",
  881. IB_WC_RECV, wc.opcode);
  882. } else {
  883. if (wc.status != IB_WC_WR_FLUSH_ERR)
  884. iser_err("rx id %llx status %d vend_err %x\n",
  885. wc.wr_id, wc.status, wc.vendor_err);
  886. ib_conn->post_recv_buf_count--;
  887. iser_handle_comp_error(NULL, ib_conn);
  888. }
  889. completed_rx++;
  890. if (!(completed_rx & 63))
  891. completed_tx += iser_drain_tx_cq(device, cq_index);
  892. }
  893. /* #warning "it is assumed here that arming CQ only once its empty" *
  894. * " would not cause interrupts to be missed" */
  895. ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
  896. completed_tx += iser_drain_tx_cq(device, cq_index);
  897. iser_dbg("got %d rx %d tx completions\n", completed_rx, completed_tx);
  898. }
  899. static void iser_cq_callback(struct ib_cq *cq, void *cq_context)
  900. {
  901. struct iser_cq_desc *cq_desc = (struct iser_cq_desc *)cq_context;
  902. struct iser_device *device = cq_desc->device;
  903. int cq_index = cq_desc->cq_index;
  904. tasklet_schedule(&device->cq_tasklet[cq_index]);
  905. }