iser_verbs.c 29 KB

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