iser_verbs.c 25 KB

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