iser_verbs.c 29 KB

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