frwr_ops.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2015, 2017 Oracle. All rights reserved.
  4. * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
  5. */
  6. /* Lightweight memory registration using Fast Registration Work
  7. * Requests (FRWR).
  8. *
  9. * FRWR features ordered asynchronous registration and invalidation
  10. * of arbitrarily-sized memory regions. This is the fastest and safest
  11. * but most complex memory registration mode.
  12. */
  13. /* Normal operation
  14. *
  15. * A Memory Region is prepared for RDMA Read or Write using a FAST_REG
  16. * Work Request (frwr_map). When the RDMA operation is finished, this
  17. * Memory Region is invalidated using a LOCAL_INV Work Request
  18. * (frwr_unmap_async and frwr_unmap_sync).
  19. *
  20. * Typically FAST_REG Work Requests are not signaled, and neither are
  21. * RDMA Send Work Requests (with the exception of signaling occasionally
  22. * to prevent provider work queue overflows). This greatly reduces HCA
  23. * interrupt workload.
  24. */
  25. /* Transport recovery
  26. *
  27. * frwr_map and frwr_unmap_* cannot run at the same time the transport
  28. * connect worker is running. The connect worker holds the transport
  29. * send lock, just as ->send_request does. This prevents frwr_map and
  30. * the connect worker from running concurrently. When a connection is
  31. * closed, the Receive completion queue is drained before the allowing
  32. * the connect worker to get control. This prevents frwr_unmap and the
  33. * connect worker from running concurrently.
  34. *
  35. * When the underlying transport disconnects, MRs that are in flight
  36. * are flushed and are likely unusable. Thus all MRs are destroyed.
  37. * New MRs are created on demand.
  38. */
  39. #include <linux/sunrpc/svc_rdma.h>
  40. #include "xprt_rdma.h"
  41. #include <trace/events/rpcrdma.h>
  42. static void frwr_cid_init(struct rpcrdma_ep *ep,
  43. struct rpcrdma_mr *mr)
  44. {
  45. struct rpc_rdma_cid *cid = &mr->mr_cid;
  46. cid->ci_queue_id = ep->re_attr.send_cq->res.id;
  47. cid->ci_completion_id = mr->mr_ibmr->res.id;
  48. }
  49. static void frwr_mr_unmap(struct rpcrdma_mr *mr)
  50. {
  51. if (mr->mr_device) {
  52. trace_xprtrdma_mr_unmap(mr);
  53. ib_dma_unmap_sg(mr->mr_device, mr->mr_sg, mr->mr_nents,
  54. mr->mr_dir);
  55. mr->mr_device = NULL;
  56. }
  57. }
  58. /**
  59. * frwr_mr_release - Destroy one MR
  60. * @mr: MR allocated by frwr_mr_init
  61. *
  62. */
  63. void frwr_mr_release(struct rpcrdma_mr *mr)
  64. {
  65. int rc;
  66. frwr_mr_unmap(mr);
  67. rc = ib_dereg_mr(mr->mr_ibmr);
  68. if (rc)
  69. trace_xprtrdma_frwr_dereg(mr, rc);
  70. kfree(mr->mr_sg);
  71. kfree(mr);
  72. }
  73. static void frwr_mr_put(struct rpcrdma_mr *mr)
  74. {
  75. frwr_mr_unmap(mr);
  76. /* The MR is returned to the req's MR free list instead
  77. * of to the xprt's MR free list. No spinlock is needed.
  78. */
  79. rpcrdma_mr_push(mr, &mr->mr_req->rl_free_mrs);
  80. }
  81. /**
  82. * frwr_reset - Place MRs back on @req's free list
  83. * @req: request to reset
  84. *
  85. * Used after a failed marshal. For FRWR, this means the MRs
  86. * don't have to be fully released and recreated.
  87. *
  88. * NB: This is safe only as long as none of @req's MRs are
  89. * involved with an ongoing asynchronous FAST_REG or LOCAL_INV
  90. * Work Request.
  91. */
  92. void frwr_reset(struct rpcrdma_req *req)
  93. {
  94. struct rpcrdma_mr *mr;
  95. while ((mr = rpcrdma_mr_pop(&req->rl_registered)))
  96. frwr_mr_put(mr);
  97. }
  98. /**
  99. * frwr_mr_init - Initialize one MR
  100. * @r_xprt: controlling transport instance
  101. * @mr: generic MR to prepare for FRWR
  102. *
  103. * Returns zero if successful. Otherwise a negative errno
  104. * is returned.
  105. */
  106. int frwr_mr_init(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr *mr)
  107. {
  108. struct rpcrdma_ep *ep = r_xprt->rx_ep;
  109. unsigned int depth = ep->re_max_fr_depth;
  110. struct scatterlist *sg;
  111. struct ib_mr *frmr;
  112. sg = kcalloc_node(depth, sizeof(*sg), XPRTRDMA_GFP_FLAGS,
  113. ibdev_to_node(ep->re_id->device));
  114. if (!sg)
  115. return -ENOMEM;
  116. frmr = ib_alloc_mr(ep->re_pd, ep->re_mrtype, depth);
  117. if (IS_ERR(frmr))
  118. goto out_mr_err;
  119. mr->mr_xprt = r_xprt;
  120. mr->mr_ibmr = frmr;
  121. mr->mr_device = NULL;
  122. INIT_LIST_HEAD(&mr->mr_list);
  123. init_completion(&mr->mr_linv_done);
  124. frwr_cid_init(ep, mr);
  125. sg_init_table(sg, depth);
  126. mr->mr_sg = sg;
  127. return 0;
  128. out_mr_err:
  129. kfree(sg);
  130. trace_xprtrdma_frwr_alloc(mr, PTR_ERR(frmr));
  131. return PTR_ERR(frmr);
  132. }
  133. /**
  134. * frwr_query_device - Prepare a transport for use with FRWR
  135. * @ep: endpoint to fill in
  136. * @device: RDMA device to query
  137. *
  138. * On success, sets:
  139. * ep->re_attr
  140. * ep->re_max_requests
  141. * ep->re_max_rdma_segs
  142. * ep->re_max_fr_depth
  143. * ep->re_mrtype
  144. *
  145. * Return values:
  146. * On success, returns zero.
  147. * %-EINVAL - the device does not support FRWR memory registration
  148. * %-ENOMEM - the device is not sufficiently capable for NFS/RDMA
  149. */
  150. int frwr_query_device(struct rpcrdma_ep *ep, const struct ib_device *device)
  151. {
  152. const struct ib_device_attr *attrs = &device->attrs;
  153. int max_qp_wr, depth, delta;
  154. unsigned int max_sge;
  155. if (!(attrs->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) ||
  156. attrs->max_fast_reg_page_list_len == 0) {
  157. pr_err("rpcrdma: 'frwr' mode is not supported by device %s\n",
  158. device->name);
  159. return -EINVAL;
  160. }
  161. max_sge = min_t(unsigned int, attrs->max_send_sge,
  162. RPCRDMA_MAX_SEND_SGES);
  163. if (max_sge < RPCRDMA_MIN_SEND_SGES) {
  164. pr_err("rpcrdma: HCA provides only %u send SGEs\n", max_sge);
  165. return -ENOMEM;
  166. }
  167. ep->re_attr.cap.max_send_sge = max_sge;
  168. ep->re_attr.cap.max_recv_sge = 1;
  169. ep->re_mrtype = IB_MR_TYPE_MEM_REG;
  170. if (attrs->kernel_cap_flags & IBK_SG_GAPS_REG)
  171. ep->re_mrtype = IB_MR_TYPE_SG_GAPS;
  172. /* Quirk: Some devices advertise a large max_fast_reg_page_list_len
  173. * capability, but perform optimally when the MRs are not larger
  174. * than a page.
  175. */
  176. if (attrs->max_sge_rd > RPCRDMA_MAX_HDR_SEGS)
  177. ep->re_max_fr_depth = attrs->max_sge_rd;
  178. else
  179. ep->re_max_fr_depth = attrs->max_fast_reg_page_list_len;
  180. if (ep->re_max_fr_depth > RPCRDMA_MAX_DATA_SEGS)
  181. ep->re_max_fr_depth = RPCRDMA_MAX_DATA_SEGS;
  182. /* Add room for frwr register and invalidate WRs.
  183. * 1. FRWR reg WR for head
  184. * 2. FRWR invalidate WR for head
  185. * 3. N FRWR reg WRs for pagelist
  186. * 4. N FRWR invalidate WRs for pagelist
  187. * 5. FRWR reg WR for tail
  188. * 6. FRWR invalidate WR for tail
  189. * 7. The RDMA_SEND WR
  190. */
  191. depth = 7;
  192. /* Calculate N if the device max FRWR depth is smaller than
  193. * RPCRDMA_MAX_DATA_SEGS.
  194. */
  195. if (ep->re_max_fr_depth < RPCRDMA_MAX_DATA_SEGS) {
  196. delta = RPCRDMA_MAX_DATA_SEGS - ep->re_max_fr_depth;
  197. do {
  198. depth += 2; /* FRWR reg + invalidate */
  199. delta -= ep->re_max_fr_depth;
  200. } while (delta > 0);
  201. }
  202. max_qp_wr = attrs->max_qp_wr;
  203. max_qp_wr -= RPCRDMA_BACKWARD_WRS;
  204. max_qp_wr -= 1;
  205. if (max_qp_wr < RPCRDMA_MIN_SLOT_TABLE)
  206. return -ENOMEM;
  207. if (ep->re_max_requests > max_qp_wr)
  208. ep->re_max_requests = max_qp_wr;
  209. ep->re_attr.cap.max_send_wr = ep->re_max_requests * depth;
  210. if (ep->re_attr.cap.max_send_wr > max_qp_wr) {
  211. ep->re_max_requests = max_qp_wr / depth;
  212. if (!ep->re_max_requests)
  213. return -ENOMEM;
  214. ep->re_attr.cap.max_send_wr = ep->re_max_requests * depth;
  215. }
  216. ep->re_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS;
  217. ep->re_attr.cap.max_send_wr += 1; /* for ib_drain_sq */
  218. ep->re_attr.cap.max_recv_wr = ep->re_max_requests;
  219. ep->re_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS;
  220. ep->re_attr.cap.max_recv_wr += RPCRDMA_MAX_RECV_BATCH;
  221. ep->re_attr.cap.max_recv_wr += 1; /* for ib_drain_rq */
  222. ep->re_max_rdma_segs =
  223. DIV_ROUND_UP(RPCRDMA_MAX_DATA_SEGS, ep->re_max_fr_depth);
  224. /* Reply chunks require segments for head and tail buffers */
  225. ep->re_max_rdma_segs += 2;
  226. if (ep->re_max_rdma_segs > RPCRDMA_MAX_HDR_SEGS)
  227. ep->re_max_rdma_segs = RPCRDMA_MAX_HDR_SEGS;
  228. /* Ensure the underlying device is capable of conveying the
  229. * largest r/wsize NFS will ask for. This guarantees that
  230. * failing over from one RDMA device to another will not
  231. * break NFS I/O.
  232. */
  233. if ((ep->re_max_rdma_segs * ep->re_max_fr_depth) < RPCRDMA_MAX_SEGS)
  234. return -ENOMEM;
  235. return 0;
  236. }
  237. /**
  238. * frwr_map - Register a memory region
  239. * @r_xprt: controlling transport
  240. * @seg: memory region co-ordinates
  241. * @nsegs: number of segments remaining
  242. * @writing: true when RDMA Write will be used
  243. * @xid: XID of RPC using the registered memory
  244. * @mr: MR to fill in
  245. *
  246. * Prepare a REG_MR Work Request to register a memory region
  247. * for remote access via RDMA READ or RDMA WRITE.
  248. *
  249. * Returns the next segment or a negative errno pointer.
  250. * On success, @mr is filled in.
  251. */
  252. struct rpcrdma_mr_seg *frwr_map(struct rpcrdma_xprt *r_xprt,
  253. struct rpcrdma_mr_seg *seg,
  254. int nsegs, bool writing, __be32 xid,
  255. struct rpcrdma_mr *mr)
  256. {
  257. struct rpcrdma_ep *ep = r_xprt->rx_ep;
  258. struct ib_reg_wr *reg_wr;
  259. int i, n, dma_nents;
  260. struct ib_mr *ibmr;
  261. u8 key;
  262. if (nsegs > ep->re_max_fr_depth)
  263. nsegs = ep->re_max_fr_depth;
  264. for (i = 0; i < nsegs;) {
  265. sg_set_page(&mr->mr_sg[i], seg->mr_page,
  266. seg->mr_len, seg->mr_offset);
  267. ++seg;
  268. ++i;
  269. if (ep->re_mrtype == IB_MR_TYPE_SG_GAPS)
  270. continue;
  271. if ((i < nsegs && seg->mr_offset) ||
  272. offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len))
  273. break;
  274. }
  275. mr->mr_dir = rpcrdma_data_dir(writing);
  276. mr->mr_nents = i;
  277. dma_nents = ib_dma_map_sg(ep->re_id->device, mr->mr_sg, mr->mr_nents,
  278. mr->mr_dir);
  279. if (!dma_nents)
  280. goto out_dmamap_err;
  281. mr->mr_device = ep->re_id->device;
  282. ibmr = mr->mr_ibmr;
  283. n = ib_map_mr_sg(ibmr, mr->mr_sg, dma_nents, NULL, PAGE_SIZE);
  284. if (n != dma_nents)
  285. goto out_mapmr_err;
  286. ibmr->iova &= 0x00000000ffffffff;
  287. ibmr->iova |= ((u64)be32_to_cpu(xid)) << 32;
  288. key = (u8)(ibmr->rkey & 0x000000FF);
  289. ib_update_fast_reg_key(ibmr, ++key);
  290. reg_wr = &mr->mr_regwr;
  291. reg_wr->mr = ibmr;
  292. reg_wr->key = ibmr->rkey;
  293. reg_wr->access = writing ?
  294. IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
  295. IB_ACCESS_REMOTE_READ;
  296. mr->mr_handle = ibmr->rkey;
  297. mr->mr_length = ibmr->length;
  298. mr->mr_offset = ibmr->iova;
  299. trace_xprtrdma_mr_map(mr);
  300. return seg;
  301. out_dmamap_err:
  302. trace_xprtrdma_frwr_sgerr(mr, i);
  303. return ERR_PTR(-EIO);
  304. out_mapmr_err:
  305. trace_xprtrdma_frwr_maperr(mr, n);
  306. return ERR_PTR(-EIO);
  307. }
  308. /**
  309. * frwr_wc_fastreg - Invoked by RDMA provider for a flushed FastReg WC
  310. * @cq: completion queue
  311. * @wc: WCE for a completed FastReg WR
  312. *
  313. * Each flushed MR gets destroyed after the QP has drained.
  314. */
  315. static void frwr_wc_fastreg(struct ib_cq *cq, struct ib_wc *wc)
  316. {
  317. struct ib_cqe *cqe = wc->wr_cqe;
  318. struct rpcrdma_mr *mr = container_of(cqe, struct rpcrdma_mr, mr_cqe);
  319. /* WARNING: Only wr_cqe and status are reliable at this point */
  320. trace_xprtrdma_wc_fastreg(wc, &mr->mr_cid);
  321. rpcrdma_flush_disconnect(cq->cq_context, wc);
  322. }
  323. /**
  324. * frwr_send - post Send WRs containing the RPC Call message
  325. * @r_xprt: controlling transport instance
  326. * @req: prepared RPC Call
  327. *
  328. * For FRWR, chain any FastReg WRs to the Send WR. Only a
  329. * single ib_post_send call is needed to register memory
  330. * and then post the Send WR.
  331. *
  332. * Returns the return code from ib_post_send.
  333. *
  334. * Caller must hold the transport send lock to ensure that the
  335. * pointers to the transport's rdma_cm_id and QP are stable.
  336. */
  337. int frwr_send(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req)
  338. {
  339. struct ib_send_wr *post_wr, *send_wr = &req->rl_wr;
  340. struct rpcrdma_ep *ep = r_xprt->rx_ep;
  341. struct rpcrdma_mr *mr;
  342. unsigned int num_wrs;
  343. int ret;
  344. num_wrs = 1;
  345. post_wr = send_wr;
  346. list_for_each_entry(mr, &req->rl_registered, mr_list) {
  347. trace_xprtrdma_mr_fastreg(mr);
  348. mr->mr_cqe.done = frwr_wc_fastreg;
  349. mr->mr_regwr.wr.next = post_wr;
  350. mr->mr_regwr.wr.wr_cqe = &mr->mr_cqe;
  351. mr->mr_regwr.wr.num_sge = 0;
  352. mr->mr_regwr.wr.opcode = IB_WR_REG_MR;
  353. mr->mr_regwr.wr.send_flags = 0;
  354. post_wr = &mr->mr_regwr.wr;
  355. ++num_wrs;
  356. }
  357. if ((kref_read(&req->rl_kref) > 1) || num_wrs > ep->re_send_count) {
  358. send_wr->send_flags |= IB_SEND_SIGNALED;
  359. ep->re_send_count = min_t(unsigned int, ep->re_send_batch,
  360. num_wrs - ep->re_send_count);
  361. } else {
  362. send_wr->send_flags &= ~IB_SEND_SIGNALED;
  363. ep->re_send_count -= num_wrs;
  364. }
  365. trace_xprtrdma_post_send(req);
  366. ret = ib_post_send(ep->re_id->qp, post_wr, NULL);
  367. if (ret)
  368. trace_xprtrdma_post_send_err(r_xprt, req, ret);
  369. return ret;
  370. }
  371. /**
  372. * frwr_reminv - handle a remotely invalidated mr on the @mrs list
  373. * @rep: Received reply
  374. * @mrs: list of MRs to check
  375. *
  376. */
  377. void frwr_reminv(struct rpcrdma_rep *rep, struct list_head *mrs)
  378. {
  379. struct rpcrdma_mr *mr;
  380. list_for_each_entry(mr, mrs, mr_list)
  381. if (mr->mr_handle == rep->rr_inv_rkey) {
  382. list_del_init(&mr->mr_list);
  383. trace_xprtrdma_mr_reminv(mr);
  384. frwr_mr_put(mr);
  385. break; /* only one invalidated MR per RPC */
  386. }
  387. }
  388. static void frwr_mr_done(struct ib_wc *wc, struct rpcrdma_mr *mr)
  389. {
  390. if (likely(wc->status == IB_WC_SUCCESS))
  391. frwr_mr_put(mr);
  392. }
  393. /**
  394. * frwr_wc_localinv - Invoked by RDMA provider for a LOCAL_INV WC
  395. * @cq: completion queue
  396. * @wc: WCE for a completed LocalInv WR
  397. *
  398. */
  399. static void frwr_wc_localinv(struct ib_cq *cq, struct ib_wc *wc)
  400. {
  401. struct ib_cqe *cqe = wc->wr_cqe;
  402. struct rpcrdma_mr *mr = container_of(cqe, struct rpcrdma_mr, mr_cqe);
  403. /* WARNING: Only wr_cqe and status are reliable at this point */
  404. trace_xprtrdma_wc_li(wc, &mr->mr_cid);
  405. frwr_mr_done(wc, mr);
  406. rpcrdma_flush_disconnect(cq->cq_context, wc);
  407. }
  408. /**
  409. * frwr_wc_localinv_wake - Invoked by RDMA provider for a LOCAL_INV WC
  410. * @cq: completion queue
  411. * @wc: WCE for a completed LocalInv WR
  412. *
  413. * Awaken anyone waiting for an MR to finish being fenced.
  414. */
  415. static void frwr_wc_localinv_wake(struct ib_cq *cq, struct ib_wc *wc)
  416. {
  417. struct ib_cqe *cqe = wc->wr_cqe;
  418. struct rpcrdma_mr *mr = container_of(cqe, struct rpcrdma_mr, mr_cqe);
  419. /* WARNING: Only wr_cqe and status are reliable at this point */
  420. trace_xprtrdma_wc_li_wake(wc, &mr->mr_cid);
  421. frwr_mr_done(wc, mr);
  422. complete(&mr->mr_linv_done);
  423. rpcrdma_flush_disconnect(cq->cq_context, wc);
  424. }
  425. /**
  426. * frwr_unmap_sync - invalidate memory regions that were registered for @req
  427. * @r_xprt: controlling transport instance
  428. * @req: rpcrdma_req with a non-empty list of MRs to process
  429. *
  430. * Sleeps until it is safe for the host CPU to access the previously mapped
  431. * memory regions. This guarantees that registered MRs are properly fenced
  432. * from the server before the RPC consumer accesses the data in them. It
  433. * also ensures proper Send flow control: waking the next RPC waits until
  434. * this RPC has relinquished all its Send Queue entries.
  435. */
  436. void frwr_unmap_sync(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req)
  437. {
  438. struct ib_send_wr *first, **prev, *last;
  439. struct rpcrdma_ep *ep = r_xprt->rx_ep;
  440. const struct ib_send_wr *bad_wr;
  441. struct rpcrdma_mr *mr;
  442. int rc;
  443. /* ORDER: Invalidate all of the MRs first
  444. *
  445. * Chain the LOCAL_INV Work Requests and post them with
  446. * a single ib_post_send() call.
  447. */
  448. prev = &first;
  449. mr = rpcrdma_mr_pop(&req->rl_registered);
  450. do {
  451. trace_xprtrdma_mr_localinv(mr);
  452. r_xprt->rx_stats.local_inv_needed++;
  453. last = &mr->mr_invwr;
  454. last->next = NULL;
  455. last->wr_cqe = &mr->mr_cqe;
  456. last->sg_list = NULL;
  457. last->num_sge = 0;
  458. last->opcode = IB_WR_LOCAL_INV;
  459. last->send_flags = IB_SEND_SIGNALED;
  460. last->ex.invalidate_rkey = mr->mr_handle;
  461. last->wr_cqe->done = frwr_wc_localinv;
  462. *prev = last;
  463. prev = &last->next;
  464. } while ((mr = rpcrdma_mr_pop(&req->rl_registered)));
  465. mr = container_of(last, struct rpcrdma_mr, mr_invwr);
  466. /* Strong send queue ordering guarantees that when the
  467. * last WR in the chain completes, all WRs in the chain
  468. * are complete.
  469. */
  470. last->wr_cqe->done = frwr_wc_localinv_wake;
  471. reinit_completion(&mr->mr_linv_done);
  472. /* Transport disconnect drains the receive CQ before it
  473. * replaces the QP. The RPC reply handler won't call us
  474. * unless re_id->qp is a valid pointer.
  475. */
  476. bad_wr = NULL;
  477. rc = ib_post_send(ep->re_id->qp, first, &bad_wr);
  478. /* The final LOCAL_INV WR in the chain is supposed to
  479. * do the wake. If it was never posted, the wake will
  480. * not happen, so don't wait in that case.
  481. */
  482. if (bad_wr != first)
  483. wait_for_completion(&mr->mr_linv_done);
  484. if (!rc)
  485. return;
  486. /* On error, the MRs get destroyed once the QP has drained. */
  487. trace_xprtrdma_post_linv_err(req, rc);
  488. /* Force a connection loss to ensure complete recovery.
  489. */
  490. rpcrdma_force_disconnect(ep);
  491. }
  492. /**
  493. * frwr_wc_localinv_done - Invoked by RDMA provider for a signaled LOCAL_INV WC
  494. * @cq: completion queue
  495. * @wc: WCE for a completed LocalInv WR
  496. *
  497. */
  498. static void frwr_wc_localinv_done(struct ib_cq *cq, struct ib_wc *wc)
  499. {
  500. struct ib_cqe *cqe = wc->wr_cqe;
  501. struct rpcrdma_mr *mr = container_of(cqe, struct rpcrdma_mr, mr_cqe);
  502. struct rpcrdma_rep *rep;
  503. /* WARNING: Only wr_cqe and status are reliable at this point */
  504. trace_xprtrdma_wc_li_done(wc, &mr->mr_cid);
  505. /* Ensure that @rep is generated before the MR is released */
  506. rep = mr->mr_req->rl_reply;
  507. smp_rmb();
  508. if (wc->status != IB_WC_SUCCESS) {
  509. if (rep)
  510. rpcrdma_unpin_rqst(rep);
  511. rpcrdma_flush_disconnect(cq->cq_context, wc);
  512. return;
  513. }
  514. frwr_mr_put(mr);
  515. rpcrdma_complete_rqst(rep);
  516. }
  517. /**
  518. * frwr_unmap_async - invalidate memory regions that were registered for @req
  519. * @r_xprt: controlling transport instance
  520. * @req: rpcrdma_req with a non-empty list of MRs to process
  521. *
  522. * This guarantees that registered MRs are properly fenced from the
  523. * server before the RPC consumer accesses the data in them. It also
  524. * ensures proper Send flow control: waking the next RPC waits until
  525. * this RPC has relinquished all its Send Queue entries.
  526. */
  527. void frwr_unmap_async(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req)
  528. {
  529. struct ib_send_wr *first, *last, **prev;
  530. struct rpcrdma_ep *ep = r_xprt->rx_ep;
  531. struct rpcrdma_mr *mr;
  532. int rc;
  533. /* Chain the LOCAL_INV Work Requests and post them with
  534. * a single ib_post_send() call.
  535. */
  536. prev = &first;
  537. mr = rpcrdma_mr_pop(&req->rl_registered);
  538. do {
  539. trace_xprtrdma_mr_localinv(mr);
  540. r_xprt->rx_stats.local_inv_needed++;
  541. last = &mr->mr_invwr;
  542. last->next = NULL;
  543. last->wr_cqe = &mr->mr_cqe;
  544. last->sg_list = NULL;
  545. last->num_sge = 0;
  546. last->opcode = IB_WR_LOCAL_INV;
  547. last->send_flags = IB_SEND_SIGNALED;
  548. last->ex.invalidate_rkey = mr->mr_handle;
  549. last->wr_cqe->done = frwr_wc_localinv;
  550. *prev = last;
  551. prev = &last->next;
  552. } while ((mr = rpcrdma_mr_pop(&req->rl_registered)));
  553. /* Strong send queue ordering guarantees that when the
  554. * last WR in the chain completes, all WRs in the chain
  555. * are complete. The last completion will wake up the
  556. * RPC waiter.
  557. */
  558. last->wr_cqe->done = frwr_wc_localinv_done;
  559. /* Transport disconnect drains the receive CQ before it
  560. * replaces the QP. The RPC reply handler won't call us
  561. * unless re_id->qp is a valid pointer.
  562. */
  563. rc = ib_post_send(ep->re_id->qp, first, NULL);
  564. if (!rc)
  565. return;
  566. /* On error, the MRs get destroyed once the QP has drained. */
  567. trace_xprtrdma_post_linv_err(req, rc);
  568. /* The final LOCAL_INV WR in the chain is supposed to
  569. * do the wake. If it was never posted, the wake does
  570. * not happen. Unpin the rqst in preparation for its
  571. * retransmission.
  572. */
  573. rpcrdma_unpin_rqst(req->rl_reply);
  574. /* Force a connection loss to ensure complete recovery.
  575. */
  576. rpcrdma_force_disconnect(ep);
  577. }
  578. /**
  579. * frwr_wp_create - Create an MR for padding Write chunks
  580. * @r_xprt: transport resources to use
  581. *
  582. * Return 0 on success, negative errno on failure.
  583. */
  584. int frwr_wp_create(struct rpcrdma_xprt *r_xprt)
  585. {
  586. struct rpcrdma_ep *ep = r_xprt->rx_ep;
  587. struct rpcrdma_mr_seg seg;
  588. struct rpcrdma_mr *mr;
  589. mr = rpcrdma_mr_get(r_xprt);
  590. if (!mr)
  591. return -EAGAIN;
  592. mr->mr_req = NULL;
  593. ep->re_write_pad_mr = mr;
  594. seg.mr_len = XDR_UNIT;
  595. seg.mr_page = virt_to_page(ep->re_write_pad);
  596. seg.mr_offset = offset_in_page(ep->re_write_pad);
  597. if (IS_ERR(frwr_map(r_xprt, &seg, 1, true, xdr_zero, mr)))
  598. return -EIO;
  599. trace_xprtrdma_mr_fastreg(mr);
  600. mr->mr_cqe.done = frwr_wc_fastreg;
  601. mr->mr_regwr.wr.next = NULL;
  602. mr->mr_regwr.wr.wr_cqe = &mr->mr_cqe;
  603. mr->mr_regwr.wr.num_sge = 0;
  604. mr->mr_regwr.wr.opcode = IB_WR_REG_MR;
  605. mr->mr_regwr.wr.send_flags = 0;
  606. return ib_post_send(ep->re_id->qp, &mr->mr_regwr.wr, NULL);
  607. }