iser_verbs.c 22 KB

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