iser_verbs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /*
  2. * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved.
  3. * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/module.h>
  35. #include <linux/delay.h>
  36. #include "iscsi_iser.h"
  37. #define ISCSI_ISER_MAX_CONN 8
  38. #define ISER_MAX_RX_CQ_LEN (ISER_QP_MAX_RECV_DTOS * ISCSI_ISER_MAX_CONN)
  39. #define ISER_MAX_TX_CQ_LEN (ISER_QP_MAX_REQ_DTOS * ISCSI_ISER_MAX_CONN)
  40. static void iser_cq_tasklet_fn(unsigned long data);
  41. static void iser_cq_callback(struct ib_cq *cq, void *cq_context);
  42. static void iser_cq_event_callback(struct ib_event *cause, void *context)
  43. {
  44. iser_err("got cq event %d \n", cause->event);
  45. }
  46. static void iser_qp_event_callback(struct ib_event *cause, void *context)
  47. {
  48. iser_err("got qp event %d\n",cause->event);
  49. }
  50. /**
  51. * iser_create_device_ib_res - creates Protection Domain (PD), Completion
  52. * Queue (CQ), DMA Memory Region (DMA MR) with the device associated with
  53. * the adapator.
  54. *
  55. * returns 0 on success, -1 on failure
  56. */
  57. static int iser_create_device_ib_res(struct iser_device *device)
  58. {
  59. device->pd = ib_alloc_pd(device->ib_device);
  60. if (IS_ERR(device->pd))
  61. goto pd_err;
  62. device->rx_cq = ib_create_cq(device->ib_device,
  63. iser_cq_callback,
  64. iser_cq_event_callback,
  65. (void *)device,
  66. ISER_MAX_RX_CQ_LEN, 0);
  67. if (IS_ERR(device->rx_cq))
  68. goto rx_cq_err;
  69. device->tx_cq = ib_create_cq(device->ib_device,
  70. NULL, iser_cq_event_callback,
  71. (void *)device,
  72. ISER_MAX_TX_CQ_LEN, 0);
  73. if (IS_ERR(device->tx_cq))
  74. goto tx_cq_err;
  75. if (ib_req_notify_cq(device->rx_cq, IB_CQ_NEXT_COMP))
  76. goto cq_arm_err;
  77. tasklet_init(&device->cq_tasklet,
  78. iser_cq_tasklet_fn,
  79. (unsigned long)device);
  80. device->mr = ib_get_dma_mr(device->pd, IB_ACCESS_LOCAL_WRITE |
  81. IB_ACCESS_REMOTE_WRITE |
  82. IB_ACCESS_REMOTE_READ);
  83. if (IS_ERR(device->mr))
  84. goto dma_mr_err;
  85. return 0;
  86. dma_mr_err:
  87. tasklet_kill(&device->cq_tasklet);
  88. cq_arm_err:
  89. ib_destroy_cq(device->tx_cq);
  90. tx_cq_err:
  91. ib_destroy_cq(device->rx_cq);
  92. rx_cq_err:
  93. ib_dealloc_pd(device->pd);
  94. pd_err:
  95. iser_err("failed to allocate an IB resource\n");
  96. return -1;
  97. }
  98. /**
  99. * iser_free_device_ib_res - destroy/dealloc/dereg the DMA MR,
  100. * CQ and PD created with the device associated with the adapator.
  101. */
  102. static void iser_free_device_ib_res(struct iser_device *device)
  103. {
  104. BUG_ON(device->mr == NULL);
  105. tasklet_kill(&device->cq_tasklet);
  106. (void)ib_dereg_mr(device->mr);
  107. (void)ib_destroy_cq(device->tx_cq);
  108. (void)ib_destroy_cq(device->rx_cq);
  109. (void)ib_dealloc_pd(device->pd);
  110. device->mr = NULL;
  111. device->tx_cq = NULL;
  112. device->rx_cq = NULL;
  113. device->pd = NULL;
  114. }
  115. /**
  116. * iser_create_ib_conn_res - Creates FMR pool and Queue-Pair (QP)
  117. *
  118. * returns 0 on success, -1 on failure
  119. */
  120. static int iser_create_ib_conn_res(struct iser_conn *ib_conn)
  121. {
  122. struct iser_device *device;
  123. struct ib_qp_init_attr init_attr;
  124. int ret = -ENOMEM;
  125. struct ib_fmr_pool_param params;
  126. BUG_ON(ib_conn->device == NULL);
  127. device = ib_conn->device;
  128. ib_conn->login_buf = kmalloc(ISER_RX_LOGIN_SIZE, GFP_KERNEL);
  129. if (!ib_conn->login_buf) {
  130. goto alloc_err;
  131. ret = -ENOMEM;
  132. }
  133. ib_conn->login_dma = ib_dma_map_single(ib_conn->device->ib_device,
  134. (void *)ib_conn->login_buf, ISER_RX_LOGIN_SIZE,
  135. DMA_FROM_DEVICE);
  136. ib_conn->page_vec = kmalloc(sizeof(struct iser_page_vec) +
  137. (sizeof(u64) * (ISCSI_ISER_SG_TABLESIZE +1)),
  138. GFP_KERNEL);
  139. if (!ib_conn->page_vec) {
  140. ret = -ENOMEM;
  141. goto alloc_err;
  142. }
  143. ib_conn->page_vec->pages = (u64 *) (ib_conn->page_vec + 1);
  144. params.page_shift = SHIFT_4K;
  145. /* when the first/last SG element are not start/end *
  146. * page aligned, the map whould be of N+1 pages */
  147. params.max_pages_per_fmr = ISCSI_ISER_SG_TABLESIZE + 1;
  148. /* make the pool size twice the max number of SCSI commands *
  149. * the ML is expected to queue, watermark for unmap at 50% */
  150. params.pool_size = ISCSI_DEF_XMIT_CMDS_MAX * 2;
  151. params.dirty_watermark = ISCSI_DEF_XMIT_CMDS_MAX;
  152. params.cache = 0;
  153. params.flush_function = NULL;
  154. params.access = (IB_ACCESS_LOCAL_WRITE |
  155. IB_ACCESS_REMOTE_WRITE |
  156. IB_ACCESS_REMOTE_READ);
  157. ib_conn->fmr_pool = ib_create_fmr_pool(device->pd, &params);
  158. if (IS_ERR(ib_conn->fmr_pool)) {
  159. ret = PTR_ERR(ib_conn->fmr_pool);
  160. goto fmr_pool_err;
  161. }
  162. memset(&init_attr, 0, sizeof init_attr);
  163. init_attr.event_handler = iser_qp_event_callback;
  164. init_attr.qp_context = (void *)ib_conn;
  165. init_attr.send_cq = device->tx_cq;
  166. init_attr.recv_cq = device->rx_cq;
  167. init_attr.cap.max_send_wr = ISER_QP_MAX_REQ_DTOS;
  168. init_attr.cap.max_recv_wr = ISER_QP_MAX_RECV_DTOS;
  169. init_attr.cap.max_send_sge = 2;
  170. init_attr.cap.max_recv_sge = 1;
  171. init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  172. init_attr.qp_type = IB_QPT_RC;
  173. ret = rdma_create_qp(ib_conn->cma_id, device->pd, &init_attr);
  174. if (ret)
  175. goto qp_err;
  176. ib_conn->qp = ib_conn->cma_id->qp;
  177. iser_err("setting conn %p cma_id %p: fmr_pool %p qp %p\n",
  178. ib_conn, ib_conn->cma_id,
  179. ib_conn->fmr_pool, ib_conn->cma_id->qp);
  180. return ret;
  181. qp_err:
  182. (void)ib_destroy_fmr_pool(ib_conn->fmr_pool);
  183. fmr_pool_err:
  184. kfree(ib_conn->page_vec);
  185. kfree(ib_conn->login_buf);
  186. alloc_err:
  187. iser_err("unable to alloc mem or create resource, err %d\n", ret);
  188. return ret;
  189. }
  190. /**
  191. * releases the FMR pool, QP and CMA ID objects, returns 0 on success,
  192. * -1 on failure
  193. */
  194. static int iser_free_ib_conn_res(struct iser_conn *ib_conn)
  195. {
  196. BUG_ON(ib_conn == NULL);
  197. iser_err("freeing conn %p cma_id %p fmr pool %p qp %p\n",
  198. ib_conn, ib_conn->cma_id,
  199. ib_conn->fmr_pool, ib_conn->qp);
  200. /* qp is created only once both addr & route are resolved */
  201. if (ib_conn->fmr_pool != NULL)
  202. ib_destroy_fmr_pool(ib_conn->fmr_pool);
  203. if (ib_conn->qp != NULL)
  204. rdma_destroy_qp(ib_conn->cma_id);
  205. if (ib_conn->cma_id != NULL)
  206. rdma_destroy_id(ib_conn->cma_id);
  207. ib_conn->fmr_pool = NULL;
  208. ib_conn->qp = NULL;
  209. ib_conn->cma_id = NULL;
  210. kfree(ib_conn->page_vec);
  211. return 0;
  212. }
  213. /**
  214. * based on the resolved device node GUID see if there already allocated
  215. * device for this device. If there's no such, create one.
  216. */
  217. static
  218. struct iser_device *iser_device_find_by_ib_device(struct rdma_cm_id *cma_id)
  219. {
  220. struct iser_device *device;
  221. mutex_lock(&ig.device_list_mutex);
  222. list_for_each_entry(device, &ig.device_list, ig_list)
  223. /* find if there's a match using the node GUID */
  224. if (device->ib_device->node_guid == cma_id->device->node_guid)
  225. goto inc_refcnt;
  226. device = kzalloc(sizeof *device, GFP_KERNEL);
  227. if (device == NULL)
  228. goto out;
  229. /* assign this device to the device */
  230. device->ib_device = cma_id->device;
  231. /* init the device and link it into ig device list */
  232. if (iser_create_device_ib_res(device)) {
  233. kfree(device);
  234. device = NULL;
  235. goto out;
  236. }
  237. list_add(&device->ig_list, &ig.device_list);
  238. inc_refcnt:
  239. device->refcount++;
  240. out:
  241. mutex_unlock(&ig.device_list_mutex);
  242. return device;
  243. }
  244. /* if there's no demand for this device, release it */
  245. static void iser_device_try_release(struct iser_device *device)
  246. {
  247. mutex_lock(&ig.device_list_mutex);
  248. device->refcount--;
  249. iser_err("device %p refcount %d\n",device,device->refcount);
  250. if (!device->refcount) {
  251. iser_free_device_ib_res(device);
  252. list_del(&device->ig_list);
  253. kfree(device);
  254. }
  255. mutex_unlock(&ig.device_list_mutex);
  256. }
  257. static int iser_conn_state_comp_exch(struct iser_conn *ib_conn,
  258. enum iser_ib_conn_state comp,
  259. enum iser_ib_conn_state exch)
  260. {
  261. int ret;
  262. spin_lock_bh(&ib_conn->lock);
  263. if ((ret = (ib_conn->state == comp)))
  264. ib_conn->state = exch;
  265. spin_unlock_bh(&ib_conn->lock);
  266. return ret;
  267. }
  268. /**
  269. * Frees all conn objects and deallocs conn descriptor
  270. */
  271. static void iser_conn_release(struct iser_conn *ib_conn)
  272. {
  273. struct iser_device *device = ib_conn->device;
  274. BUG_ON(ib_conn->state != ISER_CONN_DOWN);
  275. mutex_lock(&ig.connlist_mutex);
  276. list_del(&ib_conn->conn_list);
  277. mutex_unlock(&ig.connlist_mutex);
  278. iser_free_rx_descriptors(ib_conn);
  279. iser_free_ib_conn_res(ib_conn);
  280. ib_conn->device = NULL;
  281. /* on EVENT_ADDR_ERROR there's no device yet for this conn */
  282. if (device != NULL)
  283. iser_device_try_release(device);
  284. if (ib_conn->iser_conn)
  285. ib_conn->iser_conn->ib_conn = NULL;
  286. iscsi_destroy_endpoint(ib_conn->ep);
  287. }
  288. void iser_conn_get(struct iser_conn *ib_conn)
  289. {
  290. atomic_inc(&ib_conn->refcount);
  291. }
  292. void iser_conn_put(struct iser_conn *ib_conn)
  293. {
  294. if (atomic_dec_and_test(&ib_conn->refcount))
  295. iser_conn_release(ib_conn);
  296. }
  297. /**
  298. * triggers start of the disconnect procedures and wait for them to be done
  299. */
  300. void iser_conn_terminate(struct iser_conn *ib_conn)
  301. {
  302. int err = 0;
  303. /* change the ib conn state only if the conn is UP, however always call
  304. * rdma_disconnect since this is the only way to cause the CMA to change
  305. * the QP state to ERROR
  306. */
  307. iser_conn_state_comp_exch(ib_conn, ISER_CONN_UP, ISER_CONN_TERMINATING);
  308. err = rdma_disconnect(ib_conn->cma_id);
  309. if (err)
  310. iser_err("Failed to disconnect, conn: 0x%p err %d\n",
  311. ib_conn,err);
  312. wait_event_interruptible(ib_conn->wait,
  313. ib_conn->state == ISER_CONN_DOWN);
  314. iser_conn_put(ib_conn);
  315. }
  316. static void iser_connect_error(struct rdma_cm_id *cma_id)
  317. {
  318. struct iser_conn *ib_conn;
  319. ib_conn = (struct iser_conn *)cma_id->context;
  320. ib_conn->state = ISER_CONN_DOWN;
  321. wake_up_interruptible(&ib_conn->wait);
  322. }
  323. static void iser_addr_handler(struct rdma_cm_id *cma_id)
  324. {
  325. struct iser_device *device;
  326. struct iser_conn *ib_conn;
  327. int ret;
  328. device = iser_device_find_by_ib_device(cma_id);
  329. if (!device) {
  330. iser_err("device lookup/creation failed\n");
  331. iser_connect_error(cma_id);
  332. return;
  333. }
  334. ib_conn = (struct iser_conn *)cma_id->context;
  335. ib_conn->device = device;
  336. ret = rdma_resolve_route(cma_id, 1000);
  337. if (ret) {
  338. iser_err("resolve route failed: %d\n", ret);
  339. iser_connect_error(cma_id);
  340. }
  341. }
  342. static void iser_route_handler(struct rdma_cm_id *cma_id)
  343. {
  344. struct rdma_conn_param conn_param;
  345. int ret;
  346. ret = iser_create_ib_conn_res((struct iser_conn *)cma_id->context);
  347. if (ret)
  348. goto failure;
  349. memset(&conn_param, 0, sizeof conn_param);
  350. conn_param.responder_resources = 4;
  351. conn_param.initiator_depth = 1;
  352. conn_param.retry_count = 7;
  353. conn_param.rnr_retry_count = 6;
  354. ret = rdma_connect(cma_id, &conn_param);
  355. if (ret) {
  356. iser_err("failure connecting: %d\n", ret);
  357. goto failure;
  358. }
  359. return;
  360. failure:
  361. iser_connect_error(cma_id);
  362. }
  363. static void iser_connected_handler(struct rdma_cm_id *cma_id)
  364. {
  365. struct iser_conn *ib_conn;
  366. ib_conn = (struct iser_conn *)cma_id->context;
  367. ib_conn->state = ISER_CONN_UP;
  368. wake_up_interruptible(&ib_conn->wait);
  369. }
  370. static void iser_disconnected_handler(struct rdma_cm_id *cma_id)
  371. {
  372. struct iser_conn *ib_conn;
  373. ib_conn = (struct iser_conn *)cma_id->context;
  374. ib_conn->disc_evt_flag = 1;
  375. /* getting here when the state is UP means that the conn is being *
  376. * terminated asynchronously from the iSCSI layer's perspective. */
  377. if (iser_conn_state_comp_exch(ib_conn, ISER_CONN_UP,
  378. ISER_CONN_TERMINATING))
  379. iscsi_conn_failure(ib_conn->iser_conn->iscsi_conn,
  380. ISCSI_ERR_CONN_FAILED);
  381. /* Complete the termination process if no posts are pending */
  382. if (ib_conn->post_recv_buf_count == 0 &&
  383. (atomic_read(&ib_conn->post_send_buf_count) == 0)) {
  384. ib_conn->state = ISER_CONN_DOWN;
  385. wake_up_interruptible(&ib_conn->wait);
  386. }
  387. }
  388. static int iser_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
  389. {
  390. int ret = 0;
  391. iser_err("event %d conn %p id %p\n",event->event,cma_id->context,cma_id);
  392. switch (event->event) {
  393. case RDMA_CM_EVENT_ADDR_RESOLVED:
  394. iser_addr_handler(cma_id);
  395. break;
  396. case RDMA_CM_EVENT_ROUTE_RESOLVED:
  397. iser_route_handler(cma_id);
  398. break;
  399. case RDMA_CM_EVENT_ESTABLISHED:
  400. iser_connected_handler(cma_id);
  401. break;
  402. case RDMA_CM_EVENT_ADDR_ERROR:
  403. case RDMA_CM_EVENT_ROUTE_ERROR:
  404. case RDMA_CM_EVENT_CONNECT_ERROR:
  405. case RDMA_CM_EVENT_UNREACHABLE:
  406. case RDMA_CM_EVENT_REJECTED:
  407. iser_err("event: %d, error: %d\n", event->event, event->status);
  408. iser_connect_error(cma_id);
  409. break;
  410. case RDMA_CM_EVENT_DISCONNECTED:
  411. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  412. case RDMA_CM_EVENT_ADDR_CHANGE:
  413. iser_disconnected_handler(cma_id);
  414. break;
  415. default:
  416. iser_err("Unexpected RDMA CM event (%d)\n", event->event);
  417. break;
  418. }
  419. return ret;
  420. }
  421. void iser_conn_init(struct iser_conn *ib_conn)
  422. {
  423. ib_conn->state = ISER_CONN_INIT;
  424. init_waitqueue_head(&ib_conn->wait);
  425. ib_conn->post_recv_buf_count = 0;
  426. atomic_set(&ib_conn->post_send_buf_count, 0);
  427. atomic_set(&ib_conn->refcount, 1);
  428. INIT_LIST_HEAD(&ib_conn->conn_list);
  429. spin_lock_init(&ib_conn->lock);
  430. }
  431. /**
  432. * starts the process of connecting to the target
  433. * sleeps until the connection is established or rejected
  434. */
  435. int iser_connect(struct iser_conn *ib_conn,
  436. struct sockaddr_in *src_addr,
  437. struct sockaddr_in *dst_addr,
  438. int non_blocking)
  439. {
  440. struct sockaddr *src, *dst;
  441. int err = 0;
  442. sprintf(ib_conn->name, "%pI4:%d",
  443. &dst_addr->sin_addr.s_addr, dst_addr->sin_port);
  444. /* the device is known only --after-- address resolution */
  445. ib_conn->device = NULL;
  446. iser_err("connecting to: %pI4, port 0x%x\n",
  447. &dst_addr->sin_addr, dst_addr->sin_port);
  448. ib_conn->state = ISER_CONN_PENDING;
  449. ib_conn->cma_id = rdma_create_id(iser_cma_handler,
  450. (void *)ib_conn,
  451. RDMA_PS_TCP);
  452. if (IS_ERR(ib_conn->cma_id)) {
  453. err = PTR_ERR(ib_conn->cma_id);
  454. iser_err("rdma_create_id failed: %d\n", err);
  455. goto id_failure;
  456. }
  457. src = (struct sockaddr *)src_addr;
  458. dst = (struct sockaddr *)dst_addr;
  459. err = rdma_resolve_addr(ib_conn->cma_id, src, dst, 1000);
  460. if (err) {
  461. iser_err("rdma_resolve_addr failed: %d\n", err);
  462. goto addr_failure;
  463. }
  464. if (!non_blocking) {
  465. wait_event_interruptible(ib_conn->wait,
  466. (ib_conn->state != ISER_CONN_PENDING));
  467. if (ib_conn->state != ISER_CONN_UP) {
  468. err = -EIO;
  469. goto connect_failure;
  470. }
  471. }
  472. mutex_lock(&ig.connlist_mutex);
  473. list_add(&ib_conn->conn_list, &ig.connlist);
  474. mutex_unlock(&ig.connlist_mutex);
  475. return 0;
  476. id_failure:
  477. ib_conn->cma_id = NULL;
  478. addr_failure:
  479. ib_conn->state = ISER_CONN_DOWN;
  480. connect_failure:
  481. iser_conn_release(ib_conn);
  482. return err;
  483. }
  484. /**
  485. * iser_reg_page_vec - Register physical memory
  486. *
  487. * returns: 0 on success, errno code on failure
  488. */
  489. int iser_reg_page_vec(struct iser_conn *ib_conn,
  490. struct iser_page_vec *page_vec,
  491. struct iser_mem_reg *mem_reg)
  492. {
  493. struct ib_pool_fmr *mem;
  494. u64 io_addr;
  495. u64 *page_list;
  496. int status;
  497. page_list = page_vec->pages;
  498. io_addr = page_list[0];
  499. mem = ib_fmr_pool_map_phys(ib_conn->fmr_pool,
  500. page_list,
  501. page_vec->length,
  502. io_addr);
  503. if (IS_ERR(mem)) {
  504. status = (int)PTR_ERR(mem);
  505. iser_err("ib_fmr_pool_map_phys failed: %d\n", status);
  506. return status;
  507. }
  508. mem_reg->lkey = mem->fmr->lkey;
  509. mem_reg->rkey = mem->fmr->rkey;
  510. mem_reg->len = page_vec->length * SIZE_4K;
  511. mem_reg->va = io_addr;
  512. mem_reg->is_fmr = 1;
  513. mem_reg->mem_h = (void *)mem;
  514. mem_reg->va += page_vec->offset;
  515. mem_reg->len = page_vec->data_size;
  516. iser_dbg("PHYSICAL Mem.register, [PHYS p_array: 0x%p, sz: %d, "
  517. "entry[0]: (0x%08lx,%ld)] -> "
  518. "[lkey: 0x%08X mem_h: 0x%p va: 0x%08lX sz: %ld]\n",
  519. page_vec, page_vec->length,
  520. (unsigned long)page_vec->pages[0],
  521. (unsigned long)page_vec->data_size,
  522. (unsigned int)mem_reg->lkey, mem_reg->mem_h,
  523. (unsigned long)mem_reg->va, (unsigned long)mem_reg->len);
  524. return 0;
  525. }
  526. /**
  527. * Unregister (previosuly registered) memory.
  528. */
  529. void iser_unreg_mem(struct iser_mem_reg *reg)
  530. {
  531. int ret;
  532. iser_dbg("PHYSICAL Mem.Unregister mem_h %p\n",reg->mem_h);
  533. ret = ib_fmr_pool_unmap((struct ib_pool_fmr *)reg->mem_h);
  534. if (ret)
  535. iser_err("ib_fmr_pool_unmap failed %d\n", ret);
  536. reg->mem_h = NULL;
  537. }
  538. int iser_post_recvl(struct iser_conn *ib_conn)
  539. {
  540. struct ib_recv_wr rx_wr, *rx_wr_failed;
  541. struct ib_sge sge;
  542. int ib_ret;
  543. sge.addr = ib_conn->login_dma;
  544. sge.length = ISER_RX_LOGIN_SIZE;
  545. sge.lkey = ib_conn->device->mr->lkey;
  546. rx_wr.wr_id = (unsigned long)ib_conn->login_buf;
  547. rx_wr.sg_list = &sge;
  548. rx_wr.num_sge = 1;
  549. rx_wr.next = NULL;
  550. ib_conn->post_recv_buf_count++;
  551. ib_ret = ib_post_recv(ib_conn->qp, &rx_wr, &rx_wr_failed);
  552. if (ib_ret) {
  553. iser_err("ib_post_recv failed ret=%d\n", ib_ret);
  554. ib_conn->post_recv_buf_count--;
  555. }
  556. return ib_ret;
  557. }
  558. int iser_post_recvm(struct iser_conn *ib_conn, int count)
  559. {
  560. struct ib_recv_wr *rx_wr, *rx_wr_failed;
  561. int i, ib_ret;
  562. unsigned int my_rx_head = ib_conn->rx_desc_head;
  563. struct iser_rx_desc *rx_desc;
  564. for (rx_wr = ib_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
  565. rx_desc = &ib_conn->rx_descs[my_rx_head];
  566. rx_wr->wr_id = (unsigned long)rx_desc;
  567. rx_wr->sg_list = &rx_desc->rx_sg;
  568. rx_wr->num_sge = 1;
  569. rx_wr->next = rx_wr + 1;
  570. my_rx_head = (my_rx_head + 1) & (ISER_QP_MAX_RECV_DTOS - 1);
  571. }
  572. rx_wr--;
  573. rx_wr->next = NULL; /* mark end of work requests list */
  574. ib_conn->post_recv_buf_count += count;
  575. ib_ret = ib_post_recv(ib_conn->qp, ib_conn->rx_wr, &rx_wr_failed);
  576. if (ib_ret) {
  577. iser_err("ib_post_recv failed ret=%d\n", ib_ret);
  578. ib_conn->post_recv_buf_count -= count;
  579. } else
  580. ib_conn->rx_desc_head = my_rx_head;
  581. return ib_ret;
  582. }
  583. /**
  584. * iser_start_send - Initiate a Send DTO operation
  585. *
  586. * returns 0 on success, -1 on failure
  587. */
  588. int iser_post_send(struct iser_conn *ib_conn, struct iser_tx_desc *tx_desc)
  589. {
  590. int ib_ret;
  591. struct ib_send_wr send_wr, *send_wr_failed;
  592. ib_dma_sync_single_for_device(ib_conn->device->ib_device,
  593. tx_desc->dma_addr, ISER_HEADERS_LEN, DMA_TO_DEVICE);
  594. send_wr.next = NULL;
  595. send_wr.wr_id = (unsigned long)tx_desc;
  596. send_wr.sg_list = tx_desc->tx_sg;
  597. send_wr.num_sge = tx_desc->num_sge;
  598. send_wr.opcode = IB_WR_SEND;
  599. send_wr.send_flags = IB_SEND_SIGNALED;
  600. atomic_inc(&ib_conn->post_send_buf_count);
  601. ib_ret = ib_post_send(ib_conn->qp, &send_wr, &send_wr_failed);
  602. if (ib_ret) {
  603. iser_err("ib_post_send failed, ret:%d\n", ib_ret);
  604. atomic_dec(&ib_conn->post_send_buf_count);
  605. }
  606. return ib_ret;
  607. }
  608. static void iser_handle_comp_error(struct iser_tx_desc *desc,
  609. struct iser_conn *ib_conn)
  610. {
  611. if (desc && desc->type == ISCSI_TX_DATAOUT)
  612. kmem_cache_free(ig.desc_cache, desc);
  613. if (ib_conn->post_recv_buf_count == 0 &&
  614. atomic_read(&ib_conn->post_send_buf_count) == 0) {
  615. /* getting here when the state is UP means that the conn is *
  616. * being terminated asynchronously from the iSCSI layer's *
  617. * perspective. */
  618. if (iser_conn_state_comp_exch(ib_conn, ISER_CONN_UP,
  619. ISER_CONN_TERMINATING))
  620. iscsi_conn_failure(ib_conn->iser_conn->iscsi_conn,
  621. ISCSI_ERR_CONN_FAILED);
  622. /* complete the termination process if disconnect event was delivered *
  623. * note there are no more non completed posts to the QP */
  624. if (ib_conn->disc_evt_flag) {
  625. ib_conn->state = ISER_CONN_DOWN;
  626. wake_up_interruptible(&ib_conn->wait);
  627. }
  628. }
  629. }
  630. static int iser_drain_tx_cq(struct iser_device *device)
  631. {
  632. struct ib_cq *cq = device->tx_cq;
  633. struct ib_wc wc;
  634. struct iser_tx_desc *tx_desc;
  635. struct iser_conn *ib_conn;
  636. int completed_tx = 0;
  637. while (ib_poll_cq(cq, 1, &wc) == 1) {
  638. tx_desc = (struct iser_tx_desc *) (unsigned long) wc.wr_id;
  639. ib_conn = wc.qp->qp_context;
  640. if (wc.status == IB_WC_SUCCESS) {
  641. if (wc.opcode == IB_WC_SEND)
  642. iser_snd_completion(tx_desc, ib_conn);
  643. else
  644. iser_err("expected opcode %d got %d\n",
  645. IB_WC_SEND, wc.opcode);
  646. } else {
  647. iser_err("tx id %llx status %d vend_err %x\n",
  648. wc.wr_id, wc.status, wc.vendor_err);
  649. atomic_dec(&ib_conn->post_send_buf_count);
  650. iser_handle_comp_error(tx_desc, ib_conn);
  651. }
  652. completed_tx++;
  653. }
  654. return completed_tx;
  655. }
  656. static void iser_cq_tasklet_fn(unsigned long data)
  657. {
  658. struct iser_device *device = (struct iser_device *)data;
  659. struct ib_cq *cq = device->rx_cq;
  660. struct ib_wc wc;
  661. struct iser_rx_desc *desc;
  662. unsigned long xfer_len;
  663. struct iser_conn *ib_conn;
  664. int completed_tx, completed_rx;
  665. completed_tx = completed_rx = 0;
  666. while (ib_poll_cq(cq, 1, &wc) == 1) {
  667. desc = (struct iser_rx_desc *) (unsigned long) wc.wr_id;
  668. BUG_ON(desc == NULL);
  669. ib_conn = wc.qp->qp_context;
  670. if (wc.status == IB_WC_SUCCESS) {
  671. if (wc.opcode == IB_WC_RECV) {
  672. xfer_len = (unsigned long)wc.byte_len;
  673. iser_rcv_completion(desc, xfer_len, ib_conn);
  674. } else
  675. iser_err("expected opcode %d got %d\n",
  676. IB_WC_RECV, wc.opcode);
  677. } else {
  678. if (wc.status != IB_WC_WR_FLUSH_ERR)
  679. iser_err("rx id %llx status %d vend_err %x\n",
  680. wc.wr_id, wc.status, wc.vendor_err);
  681. ib_conn->post_recv_buf_count--;
  682. iser_handle_comp_error(NULL, ib_conn);
  683. }
  684. completed_rx++;
  685. if (!(completed_rx & 63))
  686. completed_tx += iser_drain_tx_cq(device);
  687. }
  688. /* #warning "it is assumed here that arming CQ only once its empty" *
  689. * " would not cause interrupts to be missed" */
  690. ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
  691. completed_tx += iser_drain_tx_cq(device);
  692. iser_dbg("got %d rx %d tx completions\n", completed_rx, completed_tx);
  693. }
  694. static void iser_cq_callback(struct ib_cq *cq, void *cq_context)
  695. {
  696. struct iser_device *device = (struct iser_device *)cq_context;
  697. tasklet_schedule(&device->cq_tasklet);
  698. }