iser_verbs.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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-2014 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/slab.h>
  36. #include <linux/delay.h>
  37. #include "iscsi_iser.h"
  38. static void iser_qp_event_callback(struct ib_event *cause, void *context)
  39. {
  40. iser_err("qp event %s (%d)\n",
  41. ib_event_msg(cause->event), cause->event);
  42. }
  43. static void iser_event_handler(struct ib_event_handler *handler,
  44. struct ib_event *event)
  45. {
  46. iser_err("async event %s (%d) on device %s port %d\n",
  47. ib_event_msg(event->event), event->event,
  48. dev_name(&event->device->dev), event->element.port_num);
  49. }
  50. /*
  51. * iser_create_device_ib_res - creates Protection Domain (PD), Completion
  52. * Queue (CQ), DMA Memory Region (DMA MR) with the device associated with
  53. * the adaptor.
  54. *
  55. * Return: 0 on success, -1 on failure
  56. */
  57. static int iser_create_device_ib_res(struct iser_device *device)
  58. {
  59. struct ib_device *ib_dev = device->ib_device;
  60. if (!(ib_dev->attrs.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS)) {
  61. iser_err("IB device does not support memory registrations\n");
  62. return -1;
  63. }
  64. device->pd = ib_alloc_pd(ib_dev,
  65. iser_always_reg ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
  66. if (IS_ERR(device->pd))
  67. goto pd_err;
  68. INIT_IB_EVENT_HANDLER(&device->event_handler, ib_dev,
  69. iser_event_handler);
  70. ib_register_event_handler(&device->event_handler);
  71. return 0;
  72. pd_err:
  73. iser_err("failed to allocate an IB resource\n");
  74. return -1;
  75. }
  76. /*
  77. * iser_free_device_ib_res - destroy/dealloc/dereg the DMA MR,
  78. * CQ and PD created with the device associated with the adaptor.
  79. */
  80. static void iser_free_device_ib_res(struct iser_device *device)
  81. {
  82. ib_unregister_event_handler(&device->event_handler);
  83. ib_dealloc_pd(device->pd);
  84. device->pd = NULL;
  85. }
  86. static struct iser_fr_desc *
  87. iser_create_fastreg_desc(struct iser_device *device,
  88. struct ib_pd *pd,
  89. bool pi_enable,
  90. unsigned int size)
  91. {
  92. struct iser_fr_desc *desc;
  93. struct ib_device *ib_dev = device->ib_device;
  94. enum ib_mr_type mr_type;
  95. int ret;
  96. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  97. if (!desc)
  98. return ERR_PTR(-ENOMEM);
  99. if (ib_dev->attrs.kernel_cap_flags & IBK_SG_GAPS_REG)
  100. mr_type = IB_MR_TYPE_SG_GAPS;
  101. else
  102. mr_type = IB_MR_TYPE_MEM_REG;
  103. desc->rsc.mr = ib_alloc_mr(pd, mr_type, size);
  104. if (IS_ERR(desc->rsc.mr)) {
  105. ret = PTR_ERR(desc->rsc.mr);
  106. iser_err("Failed to allocate ib_fast_reg_mr err=%d\n", ret);
  107. goto err_alloc_mr;
  108. }
  109. if (pi_enable) {
  110. desc->rsc.sig_mr = ib_alloc_mr_integrity(pd, size, size);
  111. if (IS_ERR(desc->rsc.sig_mr)) {
  112. ret = PTR_ERR(desc->rsc.sig_mr);
  113. iser_err("Failed to allocate sig_mr err=%d\n", ret);
  114. goto err_alloc_mr_integrity;
  115. }
  116. }
  117. return desc;
  118. err_alloc_mr_integrity:
  119. ib_dereg_mr(desc->rsc.mr);
  120. err_alloc_mr:
  121. kfree(desc);
  122. return ERR_PTR(ret);
  123. }
  124. static void iser_destroy_fastreg_desc(struct iser_fr_desc *desc)
  125. {
  126. struct iser_reg_resources *res = &desc->rsc;
  127. ib_dereg_mr(res->mr);
  128. if (res->sig_mr) {
  129. ib_dereg_mr(res->sig_mr);
  130. res->sig_mr = NULL;
  131. }
  132. kfree(desc);
  133. }
  134. /**
  135. * iser_alloc_fastreg_pool - Creates pool of fast_reg descriptors
  136. * for fast registration work requests.
  137. * @ib_conn: connection RDMA resources
  138. * @cmds_max: max number of SCSI commands for this connection
  139. * @size: max number of pages per map request
  140. *
  141. * Return: 0 on success, or errno code on failure
  142. */
  143. int iser_alloc_fastreg_pool(struct ib_conn *ib_conn,
  144. unsigned cmds_max,
  145. unsigned int size)
  146. {
  147. struct iser_device *device = ib_conn->device;
  148. struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
  149. struct iser_fr_desc *desc;
  150. int i, ret;
  151. INIT_LIST_HEAD(&fr_pool->list);
  152. INIT_LIST_HEAD(&fr_pool->all_list);
  153. spin_lock_init(&fr_pool->lock);
  154. fr_pool->size = 0;
  155. for (i = 0; i < cmds_max; i++) {
  156. desc = iser_create_fastreg_desc(device, device->pd,
  157. ib_conn->pi_support, size);
  158. if (IS_ERR(desc)) {
  159. ret = PTR_ERR(desc);
  160. goto err;
  161. }
  162. list_add_tail(&desc->list, &fr_pool->list);
  163. list_add_tail(&desc->all_list, &fr_pool->all_list);
  164. fr_pool->size++;
  165. }
  166. return 0;
  167. err:
  168. iser_free_fastreg_pool(ib_conn);
  169. return ret;
  170. }
  171. /**
  172. * iser_free_fastreg_pool - releases the pool of fast_reg descriptors
  173. * @ib_conn: connection RDMA resources
  174. */
  175. void iser_free_fastreg_pool(struct ib_conn *ib_conn)
  176. {
  177. struct iser_fr_pool *fr_pool = &ib_conn->fr_pool;
  178. struct iser_fr_desc *desc, *tmp;
  179. int i = 0;
  180. if (list_empty(&fr_pool->all_list))
  181. return;
  182. iser_info("freeing conn %p fr pool\n", ib_conn);
  183. list_for_each_entry_safe(desc, tmp, &fr_pool->all_list, all_list) {
  184. list_del(&desc->all_list);
  185. iser_destroy_fastreg_desc(desc);
  186. ++i;
  187. }
  188. if (i < fr_pool->size)
  189. iser_warn("pool still has %d regions registered\n",
  190. fr_pool->size - i);
  191. }
  192. /*
  193. * iser_create_ib_conn_res - Queue-Pair (QP)
  194. *
  195. * Return: 0 on success, -1 on failure
  196. */
  197. static int iser_create_ib_conn_res(struct ib_conn *ib_conn)
  198. {
  199. struct iser_conn *iser_conn = to_iser_conn(ib_conn);
  200. struct iser_device *device;
  201. struct ib_device *ib_dev;
  202. struct ib_qp_init_attr init_attr;
  203. int ret = -ENOMEM;
  204. unsigned int max_send_wr, cq_size;
  205. BUG_ON(ib_conn->device == NULL);
  206. device = ib_conn->device;
  207. ib_dev = device->ib_device;
  208. /* +1 for drain */
  209. if (ib_conn->pi_support)
  210. max_send_wr = ISER_QP_SIG_MAX_REQ_DTOS + 1;
  211. else
  212. max_send_wr = ISER_QP_MAX_REQ_DTOS + 1;
  213. max_send_wr = min_t(unsigned int, max_send_wr,
  214. (unsigned int)ib_dev->attrs.max_qp_wr);
  215. cq_size = max_send_wr + ISER_QP_MAX_RECV_DTOS;
  216. ib_conn->cq = ib_cq_pool_get(ib_dev, cq_size, -1, IB_POLL_SOFTIRQ);
  217. if (IS_ERR(ib_conn->cq)) {
  218. ret = PTR_ERR(ib_conn->cq);
  219. goto cq_err;
  220. }
  221. ib_conn->cq_size = cq_size;
  222. memset(&init_attr, 0, sizeof(init_attr));
  223. init_attr.event_handler = iser_qp_event_callback;
  224. init_attr.qp_context = (void *)ib_conn;
  225. init_attr.send_cq = ib_conn->cq;
  226. init_attr.recv_cq = ib_conn->cq;
  227. /* +1 for drain */
  228. init_attr.cap.max_recv_wr = ISER_QP_MAX_RECV_DTOS + 1;
  229. init_attr.cap.max_send_sge = 2;
  230. init_attr.cap.max_recv_sge = 1;
  231. init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  232. init_attr.qp_type = IB_QPT_RC;
  233. init_attr.cap.max_send_wr = max_send_wr;
  234. if (ib_conn->pi_support)
  235. init_attr.create_flags |= IB_QP_CREATE_INTEGRITY_EN;
  236. iser_conn->max_cmds = ISER_GET_MAX_XMIT_CMDS(max_send_wr - 1);
  237. ret = rdma_create_qp(ib_conn->cma_id, device->pd, &init_attr);
  238. if (ret)
  239. goto out_err;
  240. ib_conn->qp = ib_conn->cma_id->qp;
  241. iser_info("setting conn %p cma_id %p qp %p max_send_wr %d\n", ib_conn,
  242. ib_conn->cma_id, ib_conn->cma_id->qp, max_send_wr);
  243. return ret;
  244. out_err:
  245. ib_cq_pool_put(ib_conn->cq, ib_conn->cq_size);
  246. cq_err:
  247. iser_err("unable to alloc mem or create resource, err %d\n", ret);
  248. return ret;
  249. }
  250. /*
  251. * based on the resolved device node GUID see if there already allocated
  252. * device for this device. If there's no such, create one.
  253. */
  254. static
  255. struct iser_device *iser_device_find_by_ib_device(struct rdma_cm_id *cma_id)
  256. {
  257. struct iser_device *device;
  258. mutex_lock(&ig.device_list_mutex);
  259. list_for_each_entry(device, &ig.device_list, ig_list)
  260. /* find if there's a match using the node GUID */
  261. if (device->ib_device->node_guid == cma_id->device->node_guid)
  262. goto inc_refcnt;
  263. device = kzalloc(sizeof *device, GFP_KERNEL);
  264. if (!device)
  265. goto out;
  266. /* assign this device to the device */
  267. device->ib_device = cma_id->device;
  268. /* init the device and link it into ig device list */
  269. if (iser_create_device_ib_res(device)) {
  270. kfree(device);
  271. device = NULL;
  272. goto out;
  273. }
  274. list_add(&device->ig_list, &ig.device_list);
  275. inc_refcnt:
  276. device->refcount++;
  277. out:
  278. mutex_unlock(&ig.device_list_mutex);
  279. return device;
  280. }
  281. /* if there's no demand for this device, release it */
  282. static void iser_device_try_release(struct iser_device *device)
  283. {
  284. mutex_lock(&ig.device_list_mutex);
  285. device->refcount--;
  286. iser_info("device %p refcount %d\n", device, device->refcount);
  287. if (!device->refcount) {
  288. iser_free_device_ib_res(device);
  289. list_del(&device->ig_list);
  290. kfree(device);
  291. }
  292. mutex_unlock(&ig.device_list_mutex);
  293. }
  294. void iser_release_work(struct work_struct *work)
  295. {
  296. struct iser_conn *iser_conn;
  297. iser_conn = container_of(work, struct iser_conn, release_work);
  298. /* Wait for conn_stop to complete */
  299. wait_for_completion(&iser_conn->stop_completion);
  300. /* Wait for IB resouces cleanup to complete */
  301. wait_for_completion(&iser_conn->ib_completion);
  302. mutex_lock(&iser_conn->state_mutex);
  303. iser_conn->state = ISER_CONN_DOWN;
  304. mutex_unlock(&iser_conn->state_mutex);
  305. iser_conn_release(iser_conn);
  306. }
  307. /**
  308. * iser_free_ib_conn_res - release IB related resources
  309. * @iser_conn: iser connection struct
  310. * @destroy: indicator if we need to try to release the
  311. * iser device and memory regoins pool (only iscsi
  312. * shutdown and DEVICE_REMOVAL will use this).
  313. *
  314. * This routine is called with the iser state mutex held
  315. * so the cm_id removal is out of here. It is Safe to
  316. * be invoked multiple times.
  317. */
  318. static void iser_free_ib_conn_res(struct iser_conn *iser_conn, bool destroy)
  319. {
  320. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  321. struct iser_device *device = ib_conn->device;
  322. iser_info("freeing conn %p cma_id %p qp %p\n",
  323. iser_conn, ib_conn->cma_id, ib_conn->qp);
  324. if (ib_conn->qp) {
  325. rdma_destroy_qp(ib_conn->cma_id);
  326. ib_cq_pool_put(ib_conn->cq, ib_conn->cq_size);
  327. ib_conn->qp = NULL;
  328. }
  329. if (destroy) {
  330. if (iser_conn->rx_descs)
  331. iser_free_rx_descriptors(iser_conn);
  332. if (device) {
  333. iser_device_try_release(device);
  334. ib_conn->device = NULL;
  335. }
  336. }
  337. }
  338. /**
  339. * iser_conn_release - Frees all conn objects and deallocs conn descriptor
  340. * @iser_conn: iSER connection context
  341. */
  342. void iser_conn_release(struct iser_conn *iser_conn)
  343. {
  344. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  345. mutex_lock(&ig.connlist_mutex);
  346. list_del(&iser_conn->conn_list);
  347. mutex_unlock(&ig.connlist_mutex);
  348. mutex_lock(&iser_conn->state_mutex);
  349. /* In case we endup here without ep_disconnect being invoked. */
  350. if (iser_conn->state != ISER_CONN_DOWN) {
  351. iser_warn("iser conn %p state %d, expected state down.\n",
  352. iser_conn, iser_conn->state);
  353. iscsi_destroy_endpoint(iser_conn->ep);
  354. iser_conn->state = ISER_CONN_DOWN;
  355. }
  356. /*
  357. * In case we never got to bind stage, we still need to
  358. * release IB resources (which is safe to call more than once).
  359. */
  360. iser_free_ib_conn_res(iser_conn, true);
  361. mutex_unlock(&iser_conn->state_mutex);
  362. if (ib_conn->cma_id) {
  363. rdma_destroy_id(ib_conn->cma_id);
  364. ib_conn->cma_id = NULL;
  365. }
  366. kfree(iser_conn);
  367. }
  368. /**
  369. * iser_conn_terminate - triggers start of the disconnect procedures and
  370. * waits for them to be done
  371. * @iser_conn: iSER connection context
  372. *
  373. * Called with state mutex held
  374. */
  375. int iser_conn_terminate(struct iser_conn *iser_conn)
  376. {
  377. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  378. int err = 0;
  379. lockdep_assert_held(&iser_conn->state_mutex);
  380. /* terminate the iser conn only if the conn state is UP */
  381. if (iser_conn->state != ISER_CONN_UP)
  382. return 0;
  383. iser_conn->state = ISER_CONN_TERMINATING;
  384. iser_info("iser_conn %p state %d\n", iser_conn, iser_conn->state);
  385. /* suspend queuing of new iscsi commands */
  386. if (iser_conn->iscsi_conn)
  387. iscsi_suspend_queue(iser_conn->iscsi_conn);
  388. /*
  389. * In case we didn't already clean up the cma_id (peer initiated
  390. * a disconnection), we need to Cause the CMA to change the QP
  391. * state to ERROR.
  392. */
  393. if (ib_conn->cma_id) {
  394. err = rdma_disconnect(ib_conn->cma_id);
  395. if (err)
  396. iser_err("Failed to disconnect, conn: 0x%p err %d\n",
  397. iser_conn, err);
  398. /* block until all flush errors are consumed */
  399. ib_drain_qp(ib_conn->qp);
  400. }
  401. return 1;
  402. }
  403. /*
  404. * Called with state mutex held
  405. */
  406. static void iser_connect_error(struct rdma_cm_id *cma_id)
  407. {
  408. struct iser_conn *iser_conn = cma_id->context;
  409. lockdep_assert_held(&iser_conn->state_mutex);
  410. iser_conn->state = ISER_CONN_TERMINATING;
  411. }
  412. static void iser_calc_scsi_params(struct iser_conn *iser_conn,
  413. unsigned int max_sectors)
  414. {
  415. struct iser_device *device = iser_conn->ib_conn.device;
  416. struct ib_device_attr *attr = &device->ib_device->attrs;
  417. unsigned short sg_tablesize, sup_sg_tablesize;
  418. unsigned short reserved_mr_pages;
  419. u32 max_num_sg;
  420. /*
  421. * FRs without SG_GAPS can only map up to a (device) page per entry,
  422. * but if the first entry is misaligned we'll end up using two entries
  423. * (head and tail) for a single page worth data, so one additional
  424. * entry is required.
  425. */
  426. if (attr->kernel_cap_flags & IBK_SG_GAPS_REG)
  427. reserved_mr_pages = 0;
  428. else
  429. reserved_mr_pages = 1;
  430. if (iser_conn->ib_conn.pi_support)
  431. max_num_sg = attr->max_pi_fast_reg_page_list_len;
  432. else
  433. max_num_sg = attr->max_fast_reg_page_list_len;
  434. sg_tablesize = DIV_ROUND_UP(max_sectors * SECTOR_SIZE, SZ_4K);
  435. sup_sg_tablesize = min_t(uint, ISCSI_ISER_MAX_SG_TABLESIZE,
  436. max_num_sg - reserved_mr_pages);
  437. iser_conn->scsi_sg_tablesize = min(sg_tablesize, sup_sg_tablesize);
  438. iser_conn->pages_per_mr =
  439. iser_conn->scsi_sg_tablesize + reserved_mr_pages;
  440. }
  441. /*
  442. * Called with state mutex held
  443. */
  444. static void iser_addr_handler(struct rdma_cm_id *cma_id)
  445. {
  446. struct iser_conn *iser_conn = cma_id->context;
  447. struct iser_device *device;
  448. struct ib_conn *ib_conn;
  449. int ret;
  450. lockdep_assert_held(&iser_conn->state_mutex);
  451. if (iser_conn->state != ISER_CONN_PENDING)
  452. /* bailout */
  453. return;
  454. ib_conn = &iser_conn->ib_conn;
  455. device = iser_device_find_by_ib_device(cma_id);
  456. if (!device) {
  457. iser_err("device lookup/creation failed\n");
  458. iser_connect_error(cma_id);
  459. return;
  460. }
  461. ib_conn->device = device;
  462. /* connection T10-PI support */
  463. if (iser_pi_enable) {
  464. if (!(device->ib_device->attrs.kernel_cap_flags &
  465. IBK_INTEGRITY_HANDOVER)) {
  466. iser_warn("T10-PI requested but not supported on %s, "
  467. "continue without T10-PI\n",
  468. dev_name(&ib_conn->device->ib_device->dev));
  469. ib_conn->pi_support = false;
  470. } else {
  471. ib_conn->pi_support = true;
  472. }
  473. }
  474. iser_calc_scsi_params(iser_conn, iser_max_sectors);
  475. ret = rdma_resolve_route(cma_id, 1000);
  476. if (ret) {
  477. iser_err("resolve route failed: %d\n", ret);
  478. iser_connect_error(cma_id);
  479. return;
  480. }
  481. }
  482. /*
  483. * Called with state mutex held
  484. */
  485. static void iser_route_handler(struct rdma_cm_id *cma_id)
  486. {
  487. struct rdma_conn_param conn_param;
  488. int ret;
  489. struct iser_cm_hdr req_hdr;
  490. struct iser_conn *iser_conn = cma_id->context;
  491. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  492. struct ib_device *ib_dev = ib_conn->device->ib_device;
  493. lockdep_assert_held(&iser_conn->state_mutex);
  494. if (iser_conn->state != ISER_CONN_PENDING)
  495. /* bailout */
  496. return;
  497. ret = iser_create_ib_conn_res(ib_conn);
  498. if (ret)
  499. goto failure;
  500. memset(&conn_param, 0, sizeof conn_param);
  501. conn_param.responder_resources = ib_dev->attrs.max_qp_rd_atom;
  502. conn_param.initiator_depth = 1;
  503. conn_param.retry_count = 7;
  504. conn_param.rnr_retry_count = 6;
  505. memset(&req_hdr, 0, sizeof(req_hdr));
  506. req_hdr.flags = ISER_ZBVA_NOT_SUP;
  507. if (!iser_always_reg)
  508. req_hdr.flags |= ISER_SEND_W_INV_NOT_SUP;
  509. conn_param.private_data = (void *)&req_hdr;
  510. conn_param.private_data_len = sizeof(struct iser_cm_hdr);
  511. ret = rdma_connect_locked(cma_id, &conn_param);
  512. if (ret) {
  513. iser_err("failure connecting: %d\n", ret);
  514. goto failure;
  515. }
  516. return;
  517. failure:
  518. iser_connect_error(cma_id);
  519. }
  520. /*
  521. * Called with state mutex held
  522. */
  523. static void iser_connected_handler(struct rdma_cm_id *cma_id,
  524. const void *private_data)
  525. {
  526. struct iser_conn *iser_conn = cma_id->context;
  527. struct ib_qp_attr attr;
  528. struct ib_qp_init_attr init_attr;
  529. lockdep_assert_held(&iser_conn->state_mutex);
  530. if (iser_conn->state != ISER_CONN_PENDING)
  531. /* bailout */
  532. return;
  533. (void)ib_query_qp(cma_id->qp, &attr, ~0, &init_attr);
  534. iser_info("remote qpn:%x my qpn:%x\n", attr.dest_qp_num, cma_id->qp->qp_num);
  535. if (private_data) {
  536. u8 flags = *(u8 *)private_data;
  537. iser_conn->snd_w_inv = !(flags & ISER_SEND_W_INV_NOT_SUP);
  538. }
  539. iser_info("conn %p: negotiated %s invalidation\n",
  540. iser_conn, iser_conn->snd_w_inv ? "remote" : "local");
  541. iser_conn->state = ISER_CONN_UP;
  542. complete(&iser_conn->up_completion);
  543. }
  544. /*
  545. * Called with state mutex held
  546. */
  547. static void iser_cleanup_handler(struct rdma_cm_id *cma_id,
  548. bool destroy)
  549. {
  550. struct iser_conn *iser_conn = cma_id->context;
  551. lockdep_assert_held(&iser_conn->state_mutex);
  552. /*
  553. * We are not guaranteed that we visited disconnected_handler
  554. * by now, call it here to be safe that we handle CM drep
  555. * and flush errors.
  556. */
  557. if (iser_conn_terminate(iser_conn)) {
  558. if (iser_conn->iscsi_conn)
  559. iscsi_conn_failure(iser_conn->iscsi_conn,
  560. ISCSI_ERR_CONN_FAILED);
  561. else
  562. iser_err("iscsi_iser connection isn't bound\n");
  563. }
  564. iser_free_ib_conn_res(iser_conn, destroy);
  565. complete(&iser_conn->ib_completion);
  566. }
  567. static int iser_cma_handler(struct rdma_cm_id *cma_id,
  568. struct rdma_cm_event *event)
  569. {
  570. struct iser_conn *iser_conn;
  571. int ret = 0;
  572. iser_conn = cma_id->context;
  573. iser_info("%s (%d): status %d conn %p id %p\n",
  574. rdma_event_msg(event->event), event->event,
  575. event->status, cma_id->context, cma_id);
  576. mutex_lock(&iser_conn->state_mutex);
  577. switch (event->event) {
  578. case RDMA_CM_EVENT_ADDR_RESOLVED:
  579. iser_addr_handler(cma_id);
  580. break;
  581. case RDMA_CM_EVENT_ROUTE_RESOLVED:
  582. iser_route_handler(cma_id);
  583. break;
  584. case RDMA_CM_EVENT_ESTABLISHED:
  585. iser_connected_handler(cma_id, event->param.conn.private_data);
  586. break;
  587. case RDMA_CM_EVENT_REJECTED:
  588. iser_info("Connection rejected: %s\n",
  589. rdma_reject_msg(cma_id, event->status));
  590. fallthrough;
  591. case RDMA_CM_EVENT_ADDR_ERROR:
  592. case RDMA_CM_EVENT_ROUTE_ERROR:
  593. case RDMA_CM_EVENT_CONNECT_ERROR:
  594. case RDMA_CM_EVENT_UNREACHABLE:
  595. iser_connect_error(cma_id);
  596. break;
  597. case RDMA_CM_EVENT_DISCONNECTED:
  598. case RDMA_CM_EVENT_ADDR_CHANGE:
  599. case RDMA_CM_EVENT_TIMEWAIT_EXIT:
  600. iser_cleanup_handler(cma_id, false);
  601. break;
  602. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  603. /*
  604. * we *must* destroy the device as we cannot rely
  605. * on iscsid to be around to initiate error handling.
  606. * also if we are not in state DOWN implicitly destroy
  607. * the cma_id.
  608. */
  609. iser_cleanup_handler(cma_id, true);
  610. if (iser_conn->state != ISER_CONN_DOWN) {
  611. iser_conn->ib_conn.cma_id = NULL;
  612. ret = 1;
  613. }
  614. break;
  615. default:
  616. iser_err("Unexpected RDMA CM event: %s (%d)\n",
  617. rdma_event_msg(event->event), event->event);
  618. break;
  619. }
  620. mutex_unlock(&iser_conn->state_mutex);
  621. return ret;
  622. }
  623. void iser_conn_init(struct iser_conn *iser_conn)
  624. {
  625. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  626. iser_conn->state = ISER_CONN_INIT;
  627. init_completion(&iser_conn->stop_completion);
  628. init_completion(&iser_conn->ib_completion);
  629. init_completion(&iser_conn->up_completion);
  630. INIT_LIST_HEAD(&iser_conn->conn_list);
  631. mutex_init(&iser_conn->state_mutex);
  632. ib_conn->reg_cqe.done = iser_reg_comp;
  633. }
  634. /*
  635. * starts the process of connecting to the target
  636. * sleeps until the connection is established or rejected
  637. */
  638. int iser_connect(struct iser_conn *iser_conn, struct sockaddr *src_addr,
  639. struct sockaddr *dst_addr, int non_blocking)
  640. {
  641. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  642. int err = 0;
  643. mutex_lock(&iser_conn->state_mutex);
  644. sprintf(iser_conn->name, "%pISp", dst_addr);
  645. iser_info("connecting to: %s\n", iser_conn->name);
  646. /* the device is known only --after-- address resolution */
  647. ib_conn->device = NULL;
  648. iser_conn->state = ISER_CONN_PENDING;
  649. ib_conn->cma_id = rdma_create_id(&init_net, iser_cma_handler,
  650. iser_conn, RDMA_PS_TCP, IB_QPT_RC);
  651. if (IS_ERR(ib_conn->cma_id)) {
  652. err = PTR_ERR(ib_conn->cma_id);
  653. iser_err("rdma_create_id failed: %d\n", err);
  654. goto id_failure;
  655. }
  656. err = rdma_resolve_addr(ib_conn->cma_id, src_addr, dst_addr, 1000);
  657. if (err) {
  658. iser_err("rdma_resolve_addr failed: %d\n", err);
  659. goto addr_failure;
  660. }
  661. if (!non_blocking) {
  662. wait_for_completion_interruptible(&iser_conn->up_completion);
  663. if (iser_conn->state != ISER_CONN_UP) {
  664. err = -EIO;
  665. goto connect_failure;
  666. }
  667. }
  668. mutex_unlock(&iser_conn->state_mutex);
  669. mutex_lock(&ig.connlist_mutex);
  670. list_add(&iser_conn->conn_list, &ig.connlist);
  671. mutex_unlock(&ig.connlist_mutex);
  672. return 0;
  673. id_failure:
  674. ib_conn->cma_id = NULL;
  675. addr_failure:
  676. iser_conn->state = ISER_CONN_DOWN;
  677. connect_failure:
  678. mutex_unlock(&iser_conn->state_mutex);
  679. iser_conn_release(iser_conn);
  680. return err;
  681. }
  682. int iser_post_recvl(struct iser_conn *iser_conn)
  683. {
  684. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  685. struct iser_login_desc *desc = &iser_conn->login_desc;
  686. struct ib_recv_wr wr;
  687. int ret;
  688. desc->sge.addr = desc->rsp_dma;
  689. desc->sge.length = ISER_RX_LOGIN_SIZE;
  690. desc->sge.lkey = ib_conn->device->pd->local_dma_lkey;
  691. desc->cqe.done = iser_login_rsp;
  692. wr.wr_cqe = &desc->cqe;
  693. wr.sg_list = &desc->sge;
  694. wr.num_sge = 1;
  695. wr.next = NULL;
  696. ret = ib_post_recv(ib_conn->qp, &wr, NULL);
  697. if (unlikely(ret))
  698. iser_err("ib_post_recv login failed ret=%d\n", ret);
  699. return ret;
  700. }
  701. int iser_post_recvm(struct iser_conn *iser_conn, struct iser_rx_desc *rx_desc)
  702. {
  703. struct ib_conn *ib_conn = &iser_conn->ib_conn;
  704. struct ib_recv_wr wr;
  705. int ret;
  706. rx_desc->cqe.done = iser_task_rsp;
  707. wr.wr_cqe = &rx_desc->cqe;
  708. wr.sg_list = &rx_desc->rx_sg;
  709. wr.num_sge = 1;
  710. wr.next = NULL;
  711. ret = ib_post_recv(ib_conn->qp, &wr, NULL);
  712. if (unlikely(ret))
  713. iser_err("ib_post_recv failed ret=%d\n", ret);
  714. return ret;
  715. }
  716. /**
  717. * iser_post_send - Initiate a Send DTO operation
  718. * @ib_conn: connection RDMA resources
  719. * @tx_desc: iSER TX descriptor
  720. *
  721. * Return: 0 on success, -1 on failure
  722. */
  723. int iser_post_send(struct ib_conn *ib_conn, struct iser_tx_desc *tx_desc)
  724. {
  725. struct ib_send_wr *wr = &tx_desc->send_wr;
  726. struct ib_send_wr *first_wr;
  727. int ret;
  728. ib_dma_sync_single_for_device(ib_conn->device->ib_device,
  729. tx_desc->dma_addr, ISER_HEADERS_LEN,
  730. DMA_TO_DEVICE);
  731. wr->next = NULL;
  732. wr->wr_cqe = &tx_desc->cqe;
  733. wr->sg_list = tx_desc->tx_sg;
  734. wr->num_sge = tx_desc->num_sge;
  735. wr->opcode = IB_WR_SEND;
  736. wr->send_flags = IB_SEND_SIGNALED;
  737. if (tx_desc->inv_wr.next)
  738. first_wr = &tx_desc->inv_wr;
  739. else if (tx_desc->reg_wr.wr.next)
  740. first_wr = &tx_desc->reg_wr.wr;
  741. else
  742. first_wr = wr;
  743. ret = ib_post_send(ib_conn->qp, first_wr, NULL);
  744. if (unlikely(ret))
  745. iser_err("ib_post_send failed, ret:%d opcode:%d\n",
  746. ret, wr->opcode);
  747. return ret;
  748. }
  749. u8 iser_check_task_pi_status(struct iscsi_iser_task *iser_task,
  750. enum iser_data_dir cmd_dir, sector_t *sector)
  751. {
  752. struct iser_mem_reg *reg = &iser_task->rdma_reg[cmd_dir];
  753. struct iser_fr_desc *desc = reg->desc;
  754. unsigned long sector_size = iser_task->sc->device->sector_size;
  755. struct ib_mr_status mr_status;
  756. int ret;
  757. if (desc && desc->sig_protected) {
  758. desc->sig_protected = false;
  759. ret = ib_check_mr_status(desc->rsc.sig_mr,
  760. IB_MR_CHECK_SIG_STATUS, &mr_status);
  761. if (ret) {
  762. iser_err("ib_check_mr_status failed, ret %d\n", ret);
  763. /* Not a lot we can do, return ambiguous guard error */
  764. *sector = 0;
  765. return 0x1;
  766. }
  767. if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
  768. sector_t sector_off = mr_status.sig_err.sig_err_offset;
  769. sector_div(sector_off, sector_size + 8);
  770. *sector = scsi_get_sector(iser_task->sc) + sector_off;
  771. iser_err("PI error found type %d at sector %llx "
  772. "expected %x vs actual %x\n",
  773. mr_status.sig_err.err_type,
  774. (unsigned long long)*sector,
  775. mr_status.sig_err.expected,
  776. mr_status.sig_err.actual);
  777. switch (mr_status.sig_err.err_type) {
  778. case IB_SIG_BAD_GUARD:
  779. return 0x1;
  780. case IB_SIG_BAD_REFTAG:
  781. return 0x3;
  782. case IB_SIG_BAD_APPTAG:
  783. return 0x2;
  784. }
  785. }
  786. }
  787. return 0;
  788. }
  789. void iser_err_comp(struct ib_wc *wc, const char *type)
  790. {
  791. if (wc->status != IB_WC_WR_FLUSH_ERR) {
  792. struct iser_conn *iser_conn = to_iser_conn(wc->qp->qp_context);
  793. iser_err("%s failure: %s (%d) vend_err %#x\n", type,
  794. ib_wc_status_msg(wc->status), wc->status,
  795. wc->vendor_err);
  796. if (iser_conn->iscsi_conn)
  797. iscsi_conn_failure(iser_conn->iscsi_conn,
  798. ISCSI_ERR_CONN_FAILED);
  799. } else {
  800. iser_dbg("%s failure: %s (%d)\n", type,
  801. ib_wc_status_msg(wc->status), wc->status);
  802. }
  803. }