iser_verbs.c 22 KB

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