iser_verbs.c 22 KB

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