iser_verbs.c 22 KB

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