iser_verbs.c 26 KB

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