iser_verbs.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. /*
  2. * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved.
  3. * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.
  4. * Copyright (c) 2013 Mellanox Technologies. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/slab.h>
  37. #include <linux/delay.h>
  38. #include "iscsi_iser.h"
  39. #define ISCSI_ISER_MAX_CONN 8
  40. #define ISER_MAX_RX_CQ_LEN (ISER_QP_MAX_RECV_DTOS * ISCSI_ISER_MAX_CONN)
  41. #define ISER_MAX_TX_CQ_LEN (ISER_QP_MAX_REQ_DTOS * ISCSI_ISER_MAX_CONN)
  42. static void iser_cq_tasklet_fn(unsigned long data);
  43. static void iser_cq_callback(struct ib_cq *cq, void *cq_context);
  44. static void iser_cq_event_callback(struct ib_event *cause, void *context)
  45. {
  46. iser_err("got cq event %d \n", cause->event);
  47. }
  48. static void iser_qp_event_callback(struct ib_event *cause, void *context)
  49. {
  50. iser_err("got qp event %d\n",cause->event);
  51. }
  52. static void iser_event_handler(struct ib_event_handler *handler,
  53. struct ib_event *event)
  54. {
  55. iser_err("async event %d on device %s port %d\n", event->event,
  56. event->device->name, event->element.port_num);
  57. }
  58. /**
  59. * iser_create_device_ib_res - creates Protection Domain (PD), Completion
  60. * Queue (CQ), DMA Memory Region (DMA MR) with the device associated with
  61. * the adapator.
  62. *
  63. * returns 0 on success, -1 on failure
  64. */
  65. static int iser_create_device_ib_res(struct iser_device *device)
  66. {
  67. int i, j;
  68. struct iser_cq_desc *cq_desc;
  69. struct ib_device_attr *dev_attr;
  70. dev_attr = kmalloc(sizeof(*dev_attr), GFP_KERNEL);
  71. if (!dev_attr)
  72. return -ENOMEM;
  73. if (ib_query_device(device->ib_device, dev_attr)) {
  74. pr_warn("Query device failed for %s\n", device->ib_device->name);
  75. goto dev_attr_err;
  76. }
  77. /* Assign function handles - based on FMR support */
  78. if (device->ib_device->alloc_fmr && device->ib_device->dealloc_fmr &&
  79. device->ib_device->map_phys_fmr && device->ib_device->unmap_fmr) {
  80. iser_info("FMR supported, using FMR for registration\n");
  81. device->iser_alloc_rdma_reg_res = iser_create_fmr_pool;
  82. device->iser_free_rdma_reg_res = iser_free_fmr_pool;
  83. device->iser_reg_rdma_mem = iser_reg_rdma_mem_fmr;
  84. device->iser_unreg_rdma_mem = iser_unreg_mem_fmr;
  85. } else
  86. if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
  87. iser_info("FRWR supported, using FRWR for registration\n");
  88. device->iser_alloc_rdma_reg_res = iser_create_frwr_pool;
  89. device->iser_free_rdma_reg_res = iser_free_frwr_pool;
  90. device->iser_reg_rdma_mem = iser_reg_rdma_mem_frwr;
  91. device->iser_unreg_rdma_mem = iser_unreg_mem_frwr;
  92. } else {
  93. iser_err("IB device does not support FMRs nor FRWRs, can't register memory\n");
  94. goto dev_attr_err;
  95. }
  96. device->cqs_used = min(ISER_MAX_CQ, device->ib_device->num_comp_vectors);
  97. iser_info("using %d CQs, device %s supports %d vectors\n",
  98. device->cqs_used, device->ib_device->name,
  99. device->ib_device->num_comp_vectors);
  100. device->cq_desc = kmalloc(sizeof(struct iser_cq_desc) * device->cqs_used,
  101. GFP_KERNEL);
  102. if (device->cq_desc == NULL)
  103. goto cq_desc_err;
  104. cq_desc = device->cq_desc;
  105. device->pd = ib_alloc_pd(device->ib_device);
  106. if (IS_ERR(device->pd))
  107. goto pd_err;
  108. for (i = 0; i < device->cqs_used; i++) {
  109. cq_desc[i].device = device;
  110. cq_desc[i].cq_index = i;
  111. device->rx_cq[i] = ib_create_cq(device->ib_device,
  112. iser_cq_callback,
  113. iser_cq_event_callback,
  114. (void *)&cq_desc[i],
  115. ISER_MAX_RX_CQ_LEN, i);
  116. if (IS_ERR(device->rx_cq[i]))
  117. goto cq_err;
  118. device->tx_cq[i] = ib_create_cq(device->ib_device,
  119. NULL, iser_cq_event_callback,
  120. (void *)&cq_desc[i],
  121. ISER_MAX_TX_CQ_LEN, i);
  122. if (IS_ERR(device->tx_cq[i]))
  123. goto cq_err;
  124. if (ib_req_notify_cq(device->rx_cq[i], IB_CQ_NEXT_COMP))
  125. goto cq_err;
  126. tasklet_init(&device->cq_tasklet[i],
  127. iser_cq_tasklet_fn,
  128. (unsigned long)&cq_desc[i]);
  129. }
  130. device->mr = ib_get_dma_mr(device->pd, IB_ACCESS_LOCAL_WRITE |
  131. IB_ACCESS_REMOTE_WRITE |
  132. IB_ACCESS_REMOTE_READ);
  133. if (IS_ERR(device->mr))
  134. goto dma_mr_err;
  135. INIT_IB_EVENT_HANDLER(&device->event_handler, device->ib_device,
  136. iser_event_handler);
  137. if (ib_register_event_handler(&device->event_handler))
  138. goto handler_err;
  139. kfree(dev_attr);
  140. return 0;
  141. handler_err:
  142. ib_dereg_mr(device->mr);
  143. dma_mr_err:
  144. for (j = 0; j < device->cqs_used; j++)
  145. tasklet_kill(&device->cq_tasklet[j]);
  146. cq_err:
  147. for (j = 0; j < i; j++) {
  148. if (device->tx_cq[j])
  149. ib_destroy_cq(device->tx_cq[j]);
  150. if (device->rx_cq[j])
  151. ib_destroy_cq(device->rx_cq[j]);
  152. }
  153. ib_dealloc_pd(device->pd);
  154. pd_err:
  155. kfree(device->cq_desc);
  156. cq_desc_err:
  157. iser_err("failed to allocate an IB resource\n");
  158. dev_attr_err:
  159. kfree(dev_attr);
  160. return -1;
  161. }
  162. /**
  163. * iser_free_device_ib_res - destroy/dealloc/dereg the DMA MR,
  164. * CQ and PD created with the device associated with the adapator.
  165. */
  166. static void iser_free_device_ib_res(struct iser_device *device)
  167. {
  168. int i;
  169. BUG_ON(device->mr == NULL);
  170. for (i = 0; i < device->cqs_used; i++) {
  171. tasklet_kill(&device->cq_tasklet[i]);
  172. (void)ib_destroy_cq(device->tx_cq[i]);
  173. (void)ib_destroy_cq(device->rx_cq[i]);
  174. device->tx_cq[i] = NULL;
  175. device->rx_cq[i] = NULL;
  176. }
  177. (void)ib_unregister_event_handler(&device->event_handler);
  178. (void)ib_dereg_mr(device->mr);
  179. (void)ib_dealloc_pd(device->pd);
  180. kfree(device->cq_desc);
  181. device->mr = NULL;
  182. device->pd = NULL;
  183. }
  184. /**
  185. * iser_create_fmr_pool - Creates FMR pool and page_vector
  186. *
  187. * returns 0 on success, or errno code on failure
  188. */
  189. int iser_create_fmr_pool(struct iser_conn *ib_conn, unsigned cmds_max)
  190. {
  191. struct iser_device *device = ib_conn->device;
  192. struct ib_fmr_pool_param params;
  193. int ret = -ENOMEM;
  194. ib_conn->fastreg.fmr.page_vec = kmalloc(sizeof(struct iser_page_vec) +
  195. (sizeof(u64)*(ISCSI_ISER_SG_TABLESIZE + 1)),
  196. GFP_KERNEL);
  197. if (!ib_conn->fastreg.fmr.page_vec)
  198. return ret;
  199. ib_conn->fastreg.fmr.page_vec->pages = (u64 *)(ib_conn->fastreg.fmr.page_vec + 1);
  200. params.page_shift = SHIFT_4K;
  201. /* when the first/last SG element are not start/end *
  202. * page aligned, the map whould be of N+1 pages */
  203. params.max_pages_per_fmr = ISCSI_ISER_SG_TABLESIZE + 1;
  204. /* make the pool size twice the max number of SCSI commands *
  205. * the ML is expected to queue, watermark for unmap at 50% */
  206. params.pool_size = cmds_max * 2;
  207. params.dirty_watermark = cmds_max;
  208. params.cache = 0;
  209. params.flush_function = NULL;
  210. params.access = (IB_ACCESS_LOCAL_WRITE |
  211. IB_ACCESS_REMOTE_WRITE |
  212. IB_ACCESS_REMOTE_READ);
  213. ib_conn->fastreg.fmr.pool = ib_create_fmr_pool(device->pd, &params);
  214. if (!IS_ERR(ib_conn->fastreg.fmr.pool))
  215. return 0;
  216. /* no FMR => no need for page_vec */
  217. kfree(ib_conn->fastreg.fmr.page_vec);
  218. ib_conn->fastreg.fmr.page_vec = NULL;
  219. ret = PTR_ERR(ib_conn->fastreg.fmr.pool);
  220. ib_conn->fastreg.fmr.pool = NULL;
  221. if (ret != -ENOSYS) {
  222. iser_err("FMR allocation failed, err %d\n", ret);
  223. return ret;
  224. } else {
  225. iser_warn("FMRs are not supported, using unaligned mode\n");
  226. return 0;
  227. }
  228. }
  229. /**
  230. * iser_free_fmr_pool - releases the FMR pool and page vec
  231. */
  232. void iser_free_fmr_pool(struct iser_conn *ib_conn)
  233. {
  234. iser_info("freeing conn %p fmr pool %p\n",
  235. ib_conn, ib_conn->fastreg.fmr.pool);
  236. if (ib_conn->fastreg.fmr.pool != NULL)
  237. ib_destroy_fmr_pool(ib_conn->fastreg.fmr.pool);
  238. ib_conn->fastreg.fmr.pool = NULL;
  239. kfree(ib_conn->fastreg.fmr.page_vec);
  240. ib_conn->fastreg.fmr.page_vec = NULL;
  241. }
  242. /**
  243. * iser_create_frwr_pool - Creates pool of fast_reg descriptors
  244. * for fast registration work requests.
  245. * returns 0 on success, or errno code on failure
  246. */
  247. int iser_create_frwr_pool(struct iser_conn *ib_conn, unsigned cmds_max)
  248. {
  249. struct iser_device *device = ib_conn->device;
  250. struct fast_reg_descriptor *desc;
  251. int i, ret;
  252. INIT_LIST_HEAD(&ib_conn->fastreg.frwr.pool);
  253. ib_conn->fastreg.frwr.pool_size = 0;
  254. for (i = 0; i < cmds_max; i++) {
  255. desc = kmalloc(sizeof(*desc), GFP_KERNEL);
  256. if (!desc) {
  257. iser_err("Failed to allocate a new fast_reg descriptor\n");
  258. ret = -ENOMEM;
  259. goto err;
  260. }
  261. desc->data_frpl = ib_alloc_fast_reg_page_list(device->ib_device,
  262. ISCSI_ISER_SG_TABLESIZE + 1);
  263. if (IS_ERR(desc->data_frpl)) {
  264. ret = PTR_ERR(desc->data_frpl);
  265. iser_err("Failed to allocate ib_fast_reg_page_list err=%d\n", ret);
  266. goto fast_reg_page_failure;
  267. }
  268. desc->data_mr = ib_alloc_fast_reg_mr(device->pd,
  269. ISCSI_ISER_SG_TABLESIZE + 1);
  270. if (IS_ERR(desc->data_mr)) {
  271. ret = PTR_ERR(desc->data_mr);
  272. iser_err("Failed to allocate ib_fast_reg_mr err=%d\n", ret);
  273. goto fast_reg_mr_failure;
  274. }
  275. desc->valid = true;
  276. list_add_tail(&desc->list, &ib_conn->fastreg.frwr.pool);
  277. ib_conn->fastreg.frwr.pool_size++;
  278. }
  279. return 0;
  280. fast_reg_mr_failure:
  281. ib_free_fast_reg_page_list(desc->data_frpl);
  282. fast_reg_page_failure:
  283. kfree(desc);
  284. err:
  285. iser_free_frwr_pool(ib_conn);
  286. return ret;
  287. }
  288. /**
  289. * iser_free_frwr_pool - releases the pool of fast_reg descriptors
  290. */
  291. void iser_free_frwr_pool(struct iser_conn *ib_conn)
  292. {
  293. struct fast_reg_descriptor *desc, *tmp;
  294. int i = 0;
  295. if (list_empty(&ib_conn->fastreg.frwr.pool))
  296. return;
  297. iser_info("freeing conn %p frwr pool\n", ib_conn);
  298. list_for_each_entry_safe(desc, tmp, &ib_conn->fastreg.frwr.pool, list) {
  299. list_del(&desc->list);
  300. ib_free_fast_reg_page_list(desc->data_frpl);
  301. ib_dereg_mr(desc->data_mr);
  302. kfree(desc);
  303. ++i;
  304. }
  305. if (i < ib_conn->fastreg.frwr.pool_size)
  306. iser_warn("pool still has %d regions registered\n",
  307. ib_conn->fastreg.frwr.pool_size - i);
  308. }
  309. /**
  310. * iser_create_ib_conn_res - Queue-Pair (QP)
  311. *
  312. * returns 0 on success, -1 on failure
  313. */
  314. static int iser_create_ib_conn_res(struct iser_conn *ib_conn)
  315. {
  316. struct iser_device *device;
  317. struct ib_qp_init_attr init_attr;
  318. int ret = -ENOMEM;
  319. int index, min_index = 0;
  320. BUG_ON(ib_conn->device == NULL);
  321. device = ib_conn->device;
  322. memset(&init_attr, 0, sizeof init_attr);
  323. mutex_lock(&ig.connlist_mutex);
  324. /* select the CQ with the minimal number of usages */
  325. for (index = 0; index < device->cqs_used; index++)
  326. if (device->cq_active_qps[index] <
  327. device->cq_active_qps[min_index])
  328. min_index = index;
  329. device->cq_active_qps[min_index]++;
  330. mutex_unlock(&ig.connlist_mutex);
  331. iser_info("cq index %d used for ib_conn %p\n", min_index, ib_conn);
  332. init_attr.event_handler = iser_qp_event_callback;
  333. init_attr.qp_context = (void *)ib_conn;
  334. init_attr.send_cq = device->tx_cq[min_index];
  335. init_attr.recv_cq = device->rx_cq[min_index];
  336. init_attr.cap.max_send_wr = ISER_QP_MAX_REQ_DTOS;
  337. init_attr.cap.max_recv_wr = ISER_QP_MAX_RECV_DTOS;
  338. init_attr.cap.max_send_sge = 2;
  339. init_attr.cap.max_recv_sge = 1;
  340. init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  341. init_attr.qp_type = IB_QPT_RC;
  342. ret = rdma_create_qp(ib_conn->cma_id, device->pd, &init_attr);
  343. if (ret)
  344. goto out_err;
  345. ib_conn->qp = ib_conn->cma_id->qp;
  346. iser_info("setting conn %p cma_id %p qp %p\n",
  347. ib_conn, ib_conn->cma_id,
  348. ib_conn->cma_id->qp);
  349. return ret;
  350. out_err:
  351. iser_err("unable to alloc mem or create resource, err %d\n", ret);
  352. return ret;
  353. }
  354. /**
  355. * releases the QP objects, returns 0 on success,
  356. * -1 on failure
  357. */
  358. static int iser_free_ib_conn_res(struct iser_conn *ib_conn)
  359. {
  360. int cq_index;
  361. BUG_ON(ib_conn == NULL);
  362. iser_info("freeing conn %p cma_id %p qp %p\n",
  363. ib_conn, ib_conn->cma_id,
  364. ib_conn->qp);
  365. /* qp is created only once both addr & route are resolved */
  366. if (ib_conn->qp != NULL) {
  367. cq_index = ((struct iser_cq_desc *)ib_conn->qp->recv_cq->cq_context)->cq_index;
  368. ib_conn->device->cq_active_qps[cq_index]--;
  369. rdma_destroy_qp(ib_conn->cma_id);
  370. }
  371. ib_conn->qp = NULL;
  372. return 0;
  373. }
  374. /**
  375. * based on the resolved device node GUID see if there already allocated
  376. * device for this device. If there's no such, create one.
  377. */
  378. static
  379. struct iser_device *iser_device_find_by_ib_device(struct rdma_cm_id *cma_id)
  380. {
  381. struct iser_device *device;
  382. mutex_lock(&ig.device_list_mutex);
  383. list_for_each_entry(device, &ig.device_list, ig_list)
  384. /* find if there's a match using the node GUID */
  385. if (device->ib_device->node_guid == cma_id->device->node_guid)
  386. goto inc_refcnt;
  387. device = kzalloc(sizeof *device, GFP_KERNEL);
  388. if (device == NULL)
  389. goto out;
  390. /* assign this device to the device */
  391. device->ib_device = cma_id->device;
  392. /* init the device and link it into ig device list */
  393. if (iser_create_device_ib_res(device)) {
  394. kfree(device);
  395. device = NULL;
  396. goto out;
  397. }
  398. list_add(&device->ig_list, &ig.device_list);
  399. inc_refcnt:
  400. device->refcount++;
  401. out:
  402. mutex_unlock(&ig.device_list_mutex);
  403. return device;
  404. }
  405. /* if there's no demand for this device, release it */
  406. static void iser_device_try_release(struct iser_device *device)
  407. {
  408. mutex_lock(&ig.device_list_mutex);
  409. device->refcount--;
  410. iser_info("device %p refcount %d\n", device, device->refcount);
  411. if (!device->refcount) {
  412. iser_free_device_ib_res(device);
  413. list_del(&device->ig_list);
  414. kfree(device);
  415. }
  416. mutex_unlock(&ig.device_list_mutex);
  417. }
  418. static int iser_conn_state_comp_exch(struct iser_conn *ib_conn,
  419. enum iser_ib_conn_state comp,
  420. enum iser_ib_conn_state exch)
  421. {
  422. int ret;
  423. spin_lock_bh(&ib_conn->lock);
  424. if ((ret = (ib_conn->state == comp)))
  425. ib_conn->state = exch;
  426. spin_unlock_bh(&ib_conn->lock);
  427. return ret;
  428. }
  429. /**
  430. * Frees all conn objects and deallocs conn descriptor
  431. */
  432. static void iser_conn_release(struct iser_conn *ib_conn, int can_destroy_id)
  433. {
  434. struct iser_device *device = ib_conn->device;
  435. BUG_ON(ib_conn->state != ISER_CONN_DOWN);
  436. mutex_lock(&ig.connlist_mutex);
  437. list_del(&ib_conn->conn_list);
  438. mutex_unlock(&ig.connlist_mutex);
  439. iser_free_rx_descriptors(ib_conn);
  440. iser_free_ib_conn_res(ib_conn);
  441. ib_conn->device = NULL;
  442. /* on EVENT_ADDR_ERROR there's no device yet for this conn */
  443. if (device != NULL)
  444. iser_device_try_release(device);
  445. /* if cma handler context, the caller actually destroy the id */
  446. if (ib_conn->cma_id != NULL && can_destroy_id) {
  447. rdma_destroy_id(ib_conn->cma_id);
  448. ib_conn->cma_id = NULL;
  449. }
  450. iscsi_destroy_endpoint(ib_conn->ep);
  451. }
  452. void iser_conn_get(struct iser_conn *ib_conn)
  453. {
  454. atomic_inc(&ib_conn->refcount);
  455. }
  456. int iser_conn_put(struct iser_conn *ib_conn, int can_destroy_id)
  457. {
  458. if (atomic_dec_and_test(&ib_conn->refcount)) {
  459. iser_conn_release(ib_conn, can_destroy_id);
  460. return 1;
  461. }
  462. return 0;
  463. }
  464. /**
  465. * triggers start of the disconnect procedures and wait for them to be done
  466. */
  467. void iser_conn_terminate(struct iser_conn *ib_conn)
  468. {
  469. int err = 0;
  470. /* change the ib conn state only if the conn is UP, however always call
  471. * rdma_disconnect since this is the only way to cause the CMA to change
  472. * the QP state to ERROR
  473. */
  474. iser_conn_state_comp_exch(ib_conn, ISER_CONN_UP, ISER_CONN_TERMINATING);
  475. err = rdma_disconnect(ib_conn->cma_id);
  476. if (err)
  477. iser_err("Failed to disconnect, conn: 0x%p err %d\n",
  478. ib_conn,err);
  479. wait_event_interruptible(ib_conn->wait,
  480. ib_conn->state == ISER_CONN_DOWN);
  481. iser_conn_put(ib_conn, 1); /* deref ib conn deallocate */
  482. }
  483. static int iser_connect_error(struct rdma_cm_id *cma_id)
  484. {
  485. struct iser_conn *ib_conn;
  486. ib_conn = (struct iser_conn *)cma_id->context;
  487. ib_conn->state = ISER_CONN_DOWN;
  488. wake_up_interruptible(&ib_conn->wait);
  489. return iser_conn_put(ib_conn, 0); /* deref ib conn's cma id */
  490. }
  491. static int iser_addr_handler(struct rdma_cm_id *cma_id)
  492. {
  493. struct iser_device *device;
  494. struct iser_conn *ib_conn;
  495. int ret;
  496. device = iser_device_find_by_ib_device(cma_id);
  497. if (!device) {
  498. iser_err("device lookup/creation failed\n");
  499. return iser_connect_error(cma_id);
  500. }
  501. ib_conn = (struct iser_conn *)cma_id->context;
  502. ib_conn->device = device;
  503. ret = rdma_resolve_route(cma_id, 1000);
  504. if (ret) {
  505. iser_err("resolve route failed: %d\n", ret);
  506. return iser_connect_error(cma_id);
  507. }
  508. return 0;
  509. }
  510. static int iser_route_handler(struct rdma_cm_id *cma_id)
  511. {
  512. struct rdma_conn_param conn_param;
  513. int ret;
  514. struct iser_cm_hdr req_hdr;
  515. ret = iser_create_ib_conn_res((struct iser_conn *)cma_id->context);
  516. if (ret)
  517. goto failure;
  518. memset(&conn_param, 0, sizeof conn_param);
  519. conn_param.responder_resources = 4;
  520. conn_param.initiator_depth = 1;
  521. conn_param.retry_count = 7;
  522. conn_param.rnr_retry_count = 6;
  523. memset(&req_hdr, 0, sizeof(req_hdr));
  524. req_hdr.flags = (ISER_ZBVA_NOT_SUPPORTED |
  525. ISER_SEND_W_INV_NOT_SUPPORTED);
  526. conn_param.private_data = (void *)&req_hdr;
  527. conn_param.private_data_len = sizeof(struct iser_cm_hdr);
  528. ret = rdma_connect(cma_id, &conn_param);
  529. if (ret) {
  530. iser_err("failure connecting: %d\n", ret);
  531. goto failure;
  532. }
  533. return 0;
  534. failure:
  535. return iser_connect_error(cma_id);
  536. }
  537. static void iser_connected_handler(struct rdma_cm_id *cma_id)
  538. {
  539. struct iser_conn *ib_conn;
  540. ib_conn = (struct iser_conn *)cma_id->context;
  541. ib_conn->state = ISER_CONN_UP;
  542. wake_up_interruptible(&ib_conn->wait);
  543. }
  544. static int iser_disconnected_handler(struct rdma_cm_id *cma_id)
  545. {
  546. struct iser_conn *ib_conn;
  547. int ret;
  548. ib_conn = (struct iser_conn *)cma_id->context;
  549. /* getting here when the state is UP means that the conn is being *
  550. * terminated asynchronously from the iSCSI layer's perspective. */
  551. if (iser_conn_state_comp_exch(ib_conn, ISER_CONN_UP,
  552. ISER_CONN_TERMINATING))
  553. iscsi_conn_failure(ib_conn->iser_conn->iscsi_conn,
  554. ISCSI_ERR_CONN_FAILED);
  555. /* Complete the termination process if no posts are pending */
  556. if (ib_conn->post_recv_buf_count == 0 &&
  557. (atomic_read(&ib_conn->post_send_buf_count) == 0)) {
  558. ib_conn->state = ISER_CONN_DOWN;
  559. wake_up_interruptible(&ib_conn->wait);
  560. }
  561. ret = iser_conn_put(ib_conn, 0); /* deref ib conn's cma id */
  562. return ret;
  563. }
  564. static int iser_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
  565. {
  566. int ret = 0;
  567. iser_info("event %d status %d conn %p id %p\n",
  568. event->event, event->status, cma_id->context, cma_id);
  569. switch (event->event) {
  570. case RDMA_CM_EVENT_ADDR_RESOLVED:
  571. ret = iser_addr_handler(cma_id);
  572. break;
  573. case RDMA_CM_EVENT_ROUTE_RESOLVED:
  574. ret = iser_route_handler(cma_id);
  575. break;
  576. case RDMA_CM_EVENT_ESTABLISHED:
  577. iser_connected_handler(cma_id);
  578. break;
  579. case RDMA_CM_EVENT_ADDR_ERROR:
  580. case RDMA_CM_EVENT_ROUTE_ERROR:
  581. case RDMA_CM_EVENT_CONNECT_ERROR:
  582. case RDMA_CM_EVENT_UNREACHABLE:
  583. case RDMA_CM_EVENT_REJECTED:
  584. ret = iser_connect_error(cma_id);
  585. break;
  586. case RDMA_CM_EVENT_DISCONNECTED:
  587. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  588. case RDMA_CM_EVENT_ADDR_CHANGE:
  589. ret = iser_disconnected_handler(cma_id);
  590. break;
  591. default:
  592. iser_err("Unexpected RDMA CM event (%d)\n", event->event);
  593. break;
  594. }
  595. return ret;
  596. }
  597. void iser_conn_init(struct iser_conn *ib_conn)
  598. {
  599. ib_conn->state = ISER_CONN_INIT;
  600. init_waitqueue_head(&ib_conn->wait);
  601. ib_conn->post_recv_buf_count = 0;
  602. atomic_set(&ib_conn->post_send_buf_count, 0);
  603. atomic_set(&ib_conn->refcount, 1); /* ref ib conn allocation */
  604. INIT_LIST_HEAD(&ib_conn->conn_list);
  605. spin_lock_init(&ib_conn->lock);
  606. }
  607. /**
  608. * starts the process of connecting to the target
  609. * sleeps until the connection is established or rejected
  610. */
  611. int iser_connect(struct iser_conn *ib_conn,
  612. struct sockaddr_in *src_addr,
  613. struct sockaddr_in *dst_addr,
  614. int non_blocking)
  615. {
  616. struct sockaddr *src, *dst;
  617. int err = 0;
  618. sprintf(ib_conn->name, "%pI4:%d",
  619. &dst_addr->sin_addr.s_addr, dst_addr->sin_port);
  620. /* the device is known only --after-- address resolution */
  621. ib_conn->device = NULL;
  622. iser_info("connecting to: %pI4, port 0x%x\n",
  623. &dst_addr->sin_addr, dst_addr->sin_port);
  624. ib_conn->state = ISER_CONN_PENDING;
  625. iser_conn_get(ib_conn); /* ref ib conn's cma id */
  626. ib_conn->cma_id = rdma_create_id(iser_cma_handler,
  627. (void *)ib_conn,
  628. RDMA_PS_TCP, IB_QPT_RC);
  629. if (IS_ERR(ib_conn->cma_id)) {
  630. err = PTR_ERR(ib_conn->cma_id);
  631. iser_err("rdma_create_id failed: %d\n", err);
  632. goto id_failure;
  633. }
  634. src = (struct sockaddr *)src_addr;
  635. dst = (struct sockaddr *)dst_addr;
  636. err = rdma_resolve_addr(ib_conn->cma_id, src, dst, 1000);
  637. if (err) {
  638. iser_err("rdma_resolve_addr failed: %d\n", err);
  639. goto addr_failure;
  640. }
  641. if (!non_blocking) {
  642. wait_event_interruptible(ib_conn->wait,
  643. (ib_conn->state != ISER_CONN_PENDING));
  644. if (ib_conn->state != ISER_CONN_UP) {
  645. err = -EIO;
  646. goto connect_failure;
  647. }
  648. }
  649. mutex_lock(&ig.connlist_mutex);
  650. list_add(&ib_conn->conn_list, &ig.connlist);
  651. mutex_unlock(&ig.connlist_mutex);
  652. return 0;
  653. id_failure:
  654. ib_conn->cma_id = NULL;
  655. addr_failure:
  656. ib_conn->state = ISER_CONN_DOWN;
  657. iser_conn_put(ib_conn, 1); /* deref ib conn's cma id */
  658. connect_failure:
  659. iser_conn_put(ib_conn, 1); /* deref ib conn deallocate */
  660. return err;
  661. }
  662. /**
  663. * iser_reg_page_vec - Register physical memory
  664. *
  665. * returns: 0 on success, errno code on failure
  666. */
  667. int iser_reg_page_vec(struct iser_conn *ib_conn,
  668. struct iser_page_vec *page_vec,
  669. struct iser_mem_reg *mem_reg)
  670. {
  671. struct ib_pool_fmr *mem;
  672. u64 io_addr;
  673. u64 *page_list;
  674. int status;
  675. page_list = page_vec->pages;
  676. io_addr = page_list[0];
  677. mem = ib_fmr_pool_map_phys(ib_conn->fastreg.fmr.pool,
  678. page_list,
  679. page_vec->length,
  680. io_addr);
  681. if (IS_ERR(mem)) {
  682. status = (int)PTR_ERR(mem);
  683. iser_err("ib_fmr_pool_map_phys failed: %d\n", status);
  684. return status;
  685. }
  686. mem_reg->lkey = mem->fmr->lkey;
  687. mem_reg->rkey = mem->fmr->rkey;
  688. mem_reg->len = page_vec->length * SIZE_4K;
  689. mem_reg->va = io_addr;
  690. mem_reg->is_mr = 1;
  691. mem_reg->mem_h = (void *)mem;
  692. mem_reg->va += page_vec->offset;
  693. mem_reg->len = page_vec->data_size;
  694. iser_dbg("PHYSICAL Mem.register, [PHYS p_array: 0x%p, sz: %d, "
  695. "entry[0]: (0x%08lx,%ld)] -> "
  696. "[lkey: 0x%08X mem_h: 0x%p va: 0x%08lX sz: %ld]\n",
  697. page_vec, page_vec->length,
  698. (unsigned long)page_vec->pages[0],
  699. (unsigned long)page_vec->data_size,
  700. (unsigned int)mem_reg->lkey, mem_reg->mem_h,
  701. (unsigned long)mem_reg->va, (unsigned long)mem_reg->len);
  702. return 0;
  703. }
  704. /**
  705. * Unregister (previosuly registered using FMR) memory.
  706. * If memory is non-FMR does nothing.
  707. */
  708. void iser_unreg_mem_fmr(struct iscsi_iser_task *iser_task,
  709. enum iser_data_dir cmd_dir)
  710. {
  711. struct iser_mem_reg *reg = &iser_task->rdma_regd[cmd_dir].reg;
  712. int ret;
  713. if (!reg->is_mr)
  714. return;
  715. iser_dbg("PHYSICAL Mem.Unregister mem_h %p\n",reg->mem_h);
  716. ret = ib_fmr_pool_unmap((struct ib_pool_fmr *)reg->mem_h);
  717. if (ret)
  718. iser_err("ib_fmr_pool_unmap failed %d\n", ret);
  719. reg->mem_h = NULL;
  720. }
  721. void iser_unreg_mem_frwr(struct iscsi_iser_task *iser_task,
  722. enum iser_data_dir cmd_dir)
  723. {
  724. struct iser_mem_reg *reg = &iser_task->rdma_regd[cmd_dir].reg;
  725. struct iser_conn *ib_conn = iser_task->iser_conn->ib_conn;
  726. struct fast_reg_descriptor *desc = reg->mem_h;
  727. if (!reg->is_mr)
  728. return;
  729. reg->mem_h = NULL;
  730. reg->is_mr = 0;
  731. spin_lock_bh(&ib_conn->lock);
  732. list_add_tail(&desc->list, &ib_conn->fastreg.frwr.pool);
  733. spin_unlock_bh(&ib_conn->lock);
  734. }
  735. int iser_post_recvl(struct iser_conn *ib_conn)
  736. {
  737. struct ib_recv_wr rx_wr, *rx_wr_failed;
  738. struct ib_sge sge;
  739. int ib_ret;
  740. sge.addr = ib_conn->login_resp_dma;
  741. sge.length = ISER_RX_LOGIN_SIZE;
  742. sge.lkey = ib_conn->device->mr->lkey;
  743. rx_wr.wr_id = (unsigned long)ib_conn->login_resp_buf;
  744. rx_wr.sg_list = &sge;
  745. rx_wr.num_sge = 1;
  746. rx_wr.next = NULL;
  747. ib_conn->post_recv_buf_count++;
  748. ib_ret = ib_post_recv(ib_conn->qp, &rx_wr, &rx_wr_failed);
  749. if (ib_ret) {
  750. iser_err("ib_post_recv failed ret=%d\n", ib_ret);
  751. ib_conn->post_recv_buf_count--;
  752. }
  753. return ib_ret;
  754. }
  755. int iser_post_recvm(struct iser_conn *ib_conn, int count)
  756. {
  757. struct ib_recv_wr *rx_wr, *rx_wr_failed;
  758. int i, ib_ret;
  759. unsigned int my_rx_head = ib_conn->rx_desc_head;
  760. struct iser_rx_desc *rx_desc;
  761. for (rx_wr = ib_conn->rx_wr, i = 0; i < count; i++, rx_wr++) {
  762. rx_desc = &ib_conn->rx_descs[my_rx_head];
  763. rx_wr->wr_id = (unsigned long)rx_desc;
  764. rx_wr->sg_list = &rx_desc->rx_sg;
  765. rx_wr->num_sge = 1;
  766. rx_wr->next = rx_wr + 1;
  767. my_rx_head = (my_rx_head + 1) & ib_conn->qp_max_recv_dtos_mask;
  768. }
  769. rx_wr--;
  770. rx_wr->next = NULL; /* mark end of work requests list */
  771. ib_conn->post_recv_buf_count += count;
  772. ib_ret = ib_post_recv(ib_conn->qp, ib_conn->rx_wr, &rx_wr_failed);
  773. if (ib_ret) {
  774. iser_err("ib_post_recv failed ret=%d\n", ib_ret);
  775. ib_conn->post_recv_buf_count -= count;
  776. } else
  777. ib_conn->rx_desc_head = my_rx_head;
  778. return ib_ret;
  779. }
  780. /**
  781. * iser_start_send - Initiate a Send DTO operation
  782. *
  783. * returns 0 on success, -1 on failure
  784. */
  785. int iser_post_send(struct iser_conn *ib_conn, struct iser_tx_desc *tx_desc)
  786. {
  787. int ib_ret;
  788. struct ib_send_wr send_wr, *send_wr_failed;
  789. ib_dma_sync_single_for_device(ib_conn->device->ib_device,
  790. tx_desc->dma_addr, ISER_HEADERS_LEN, DMA_TO_DEVICE);
  791. send_wr.next = NULL;
  792. send_wr.wr_id = (unsigned long)tx_desc;
  793. send_wr.sg_list = tx_desc->tx_sg;
  794. send_wr.num_sge = tx_desc->num_sge;
  795. send_wr.opcode = IB_WR_SEND;
  796. send_wr.send_flags = IB_SEND_SIGNALED;
  797. atomic_inc(&ib_conn->post_send_buf_count);
  798. ib_ret = ib_post_send(ib_conn->qp, &send_wr, &send_wr_failed);
  799. if (ib_ret) {
  800. iser_err("ib_post_send failed, ret:%d\n", ib_ret);
  801. atomic_dec(&ib_conn->post_send_buf_count);
  802. }
  803. return ib_ret;
  804. }
  805. static void iser_handle_comp_error(struct iser_tx_desc *desc,
  806. struct iser_conn *ib_conn)
  807. {
  808. if (desc && desc->type == ISCSI_TX_DATAOUT)
  809. kmem_cache_free(ig.desc_cache, desc);
  810. if (ib_conn->post_recv_buf_count == 0 &&
  811. atomic_read(&ib_conn->post_send_buf_count) == 0) {
  812. /* getting here when the state is UP means that the conn is *
  813. * being terminated asynchronously from the iSCSI layer's *
  814. * perspective. */
  815. if (iser_conn_state_comp_exch(ib_conn, ISER_CONN_UP,
  816. ISER_CONN_TERMINATING))
  817. iscsi_conn_failure(ib_conn->iser_conn->iscsi_conn,
  818. ISCSI_ERR_CONN_FAILED);
  819. /* no more non completed posts to the QP, complete the
  820. * termination process w.o worrying on disconnect event */
  821. ib_conn->state = ISER_CONN_DOWN;
  822. wake_up_interruptible(&ib_conn->wait);
  823. }
  824. }
  825. static int iser_drain_tx_cq(struct iser_device *device, int cq_index)
  826. {
  827. struct ib_cq *cq = device->tx_cq[cq_index];
  828. struct ib_wc wc;
  829. struct iser_tx_desc *tx_desc;
  830. struct iser_conn *ib_conn;
  831. int completed_tx = 0;
  832. while (ib_poll_cq(cq, 1, &wc) == 1) {
  833. tx_desc = (struct iser_tx_desc *) (unsigned long) wc.wr_id;
  834. ib_conn = wc.qp->qp_context;
  835. if (wc.status == IB_WC_SUCCESS) {
  836. if (wc.opcode == IB_WC_SEND)
  837. iser_snd_completion(tx_desc, ib_conn);
  838. else if (wc.opcode == IB_WC_LOCAL_INV ||
  839. wc.opcode == IB_WC_FAST_REG_MR) {
  840. atomic_dec(&ib_conn->post_send_buf_count);
  841. continue;
  842. } else
  843. iser_err("expected opcode %d got %d\n",
  844. IB_WC_SEND, wc.opcode);
  845. } else {
  846. iser_err("tx id %llx status %d vend_err %x\n",
  847. wc.wr_id, wc.status, wc.vendor_err);
  848. atomic_dec(&ib_conn->post_send_buf_count);
  849. iser_handle_comp_error(tx_desc, ib_conn);
  850. }
  851. completed_tx++;
  852. }
  853. return completed_tx;
  854. }
  855. static void iser_cq_tasklet_fn(unsigned long data)
  856. {
  857. struct iser_cq_desc *cq_desc = (struct iser_cq_desc *)data;
  858. struct iser_device *device = cq_desc->device;
  859. int cq_index = cq_desc->cq_index;
  860. struct ib_cq *cq = device->rx_cq[cq_index];
  861. struct ib_wc wc;
  862. struct iser_rx_desc *desc;
  863. unsigned long xfer_len;
  864. struct iser_conn *ib_conn;
  865. int completed_tx, completed_rx;
  866. completed_tx = completed_rx = 0;
  867. while (ib_poll_cq(cq, 1, &wc) == 1) {
  868. desc = (struct iser_rx_desc *) (unsigned long) wc.wr_id;
  869. BUG_ON(desc == NULL);
  870. ib_conn = wc.qp->qp_context;
  871. if (wc.status == IB_WC_SUCCESS) {
  872. if (wc.opcode == IB_WC_RECV) {
  873. xfer_len = (unsigned long)wc.byte_len;
  874. iser_rcv_completion(desc, xfer_len, ib_conn);
  875. } else
  876. iser_err("expected opcode %d got %d\n",
  877. IB_WC_RECV, wc.opcode);
  878. } else {
  879. if (wc.status != IB_WC_WR_FLUSH_ERR)
  880. iser_err("rx id %llx status %d vend_err %x\n",
  881. wc.wr_id, wc.status, wc.vendor_err);
  882. ib_conn->post_recv_buf_count--;
  883. iser_handle_comp_error(NULL, ib_conn);
  884. }
  885. completed_rx++;
  886. if (!(completed_rx & 63))
  887. completed_tx += iser_drain_tx_cq(device, cq_index);
  888. }
  889. /* #warning "it is assumed here that arming CQ only once its empty" *
  890. * " would not cause interrupts to be missed" */
  891. ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
  892. completed_tx += iser_drain_tx_cq(device, cq_index);
  893. iser_dbg("got %d rx %d tx completions\n", completed_rx, completed_tx);
  894. }
  895. static void iser_cq_callback(struct ib_cq *cq, void *cq_context)
  896. {
  897. struct iser_cq_desc *cq_desc = (struct iser_cq_desc *)cq_context;
  898. struct iser_device *device = cq_desc->device;
  899. int cq_index = cq_desc->cq_index;
  900. tasklet_schedule(&device->cq_tasklet[cq_index]);
  901. }