blob: 210949562786665ea1a2990e2e9e6f886e580af6 [file] [log] [blame]
Chuck Levera0ce85f2015-03-30 14:34:21 -04001/*
2 * Copyright (c) 2015 Oracle. All rights reserved.
3 * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
4 */
5
6/* Lightweight memory registration using Fast Registration Work
7 * Requests (FRWR). Also referred to sometimes as FRMR mode.
8 *
9 * FRWR features ordered asynchronous registration and deregistration
10 * of arbitrarily sized memory regions. This is the fastest and safest
11 * but most complex memory registration mode.
12 */
13
Chuck Leverc14d86e2015-05-26 11:52:35 -040014/* Normal operation
15 *
16 * A Memory Region is prepared for RDMA READ or WRITE using a FAST_REG
17 * Work Request (frmr_op_map). When the RDMA operation is finished, this
18 * Memory Region is invalidated using a LOCAL_INV Work Request
19 * (frmr_op_unmap).
20 *
21 * Typically these Work Requests are not signaled, and neither are RDMA
22 * SEND Work Requests (with the exception of signaling occasionally to
23 * prevent provider work queue overflows). This greatly reduces HCA
24 * interrupt workload.
25 *
26 * As an optimization, frwr_op_unmap marks MRs INVALID before the
27 * LOCAL_INV WR is posted. If posting succeeds, the MR is placed on
28 * rb_mws immediately so that no work (like managing a linked list
29 * under a spinlock) is needed in the completion upcall.
30 *
31 * But this means that frwr_op_map() can occasionally encounter an MR
32 * that is INVALID but the LOCAL_INV WR has not completed. Work Queue
33 * ordering prevents a subsequent FAST_REG WR from executing against
34 * that MR while it is still being invalidated.
35 */
36
37/* Transport recovery
38 *
39 * ->op_map and the transport connect worker cannot run at the same
40 * time, but ->op_unmap can fire while the transport connect worker
41 * is running. Thus MR recovery is handled in ->op_map, to guarantee
42 * that recovered MRs are owned by a sending RPC, and not one where
43 * ->op_unmap could fire at the same time transport reconnect is
44 * being done.
45 *
46 * When the underlying transport disconnects, MRs are left in one of
47 * three states:
48 *
49 * INVALID: The MR was not in use before the QP entered ERROR state.
50 * (Or, the LOCAL_INV WR has not completed or flushed yet).
51 *
52 * STALE: The MR was being registered or unregistered when the QP
53 * entered ERROR state, and the pending WR was flushed.
54 *
55 * VALID: The MR was registered before the QP entered ERROR state.
56 *
57 * When frwr_op_map encounters STALE and VALID MRs, they are recovered
58 * with ib_dereg_mr and then are re-initialized. Beause MR recovery
59 * allocates fresh resources, it is deferred to a workqueue, and the
60 * recovered MRs are placed back on the rb_mws list when recovery is
61 * complete. frwr_op_map allocates another MR for the current RPC while
62 * the broken MR is reset.
63 *
64 * To ensure that frwr_op_map doesn't encounter an MR that is marked
65 * INVALID but that is about to be flushed due to a previous transport
66 * disconnect, the transport connect worker attempts to drain all
67 * pending send queue WRs before the transport is reconnected.
68 */
69
Chuck Leverc8b920b2016-09-15 10:57:16 -040070#include <linux/sunrpc/rpc_rdma.h>
71
Chuck Levera0ce85f2015-03-30 14:34:21 -040072#include "xprt_rdma.h"
73
74#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
75# define RPCDBG_FACILITY RPCDBG_TRANS
76#endif
77
Chuck Leverb54054c2016-06-29 13:53:27 -040078bool
79frwr_is_supported(struct rpcrdma_ia *ia)
80{
81 struct ib_device_attr *attrs = &ia->ri_device->attrs;
82
83 if (!(attrs->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS))
84 goto out_not_supported;
85 if (attrs->max_fast_reg_page_list_len == 0)
86 goto out_not_supported;
87 return true;
88
89out_not_supported:
90 pr_info("rpcrdma: 'frwr' mode is not supported by device %s\n",
91 ia->ri_device->name);
92 return false;
93}
94
Chuck Leverd7a21c12016-05-02 14:42:12 -040095static int
Chuck Levere2ac2362016-06-29 13:54:00 -040096frwr_op_init_mr(struct rpcrdma_ia *ia, struct rpcrdma_mw *r)
Chuck Leverd48b1d22016-06-29 13:52:29 -040097{
Chuck Levere2ac2362016-06-29 13:54:00 -040098 unsigned int depth = ia->ri_max_frmr_depth;
Chuck Leverd48b1d22016-06-29 13:52:29 -040099 struct rpcrdma_frmr *f = &r->frmr;
100 int rc;
101
Chuck Levere2ac2362016-06-29 13:54:00 -0400102 f->fr_mr = ib_alloc_mr(ia->ri_pd, IB_MR_TYPE_MEM_REG, depth);
Chuck Leverd48b1d22016-06-29 13:52:29 -0400103 if (IS_ERR(f->fr_mr))
104 goto out_mr_err;
105
106 r->mw_sg = kcalloc(depth, sizeof(*r->mw_sg), GFP_KERNEL);
107 if (!r->mw_sg)
108 goto out_list_err;
109
110 sg_init_table(r->mw_sg, depth);
111 init_completion(&f->fr_linv_done);
112 return 0;
113
114out_mr_err:
115 rc = PTR_ERR(f->fr_mr);
116 dprintk("RPC: %s: ib_alloc_mr status %i\n",
117 __func__, rc);
118 return rc;
119
120out_list_err:
121 rc = -ENOMEM;
122 dprintk("RPC: %s: sg allocation failure\n",
123 __func__);
124 ib_dereg_mr(f->fr_mr);
125 return rc;
126}
127
128static void
Chuck Levere2ac2362016-06-29 13:54:00 -0400129frwr_op_release_mr(struct rpcrdma_mw *r)
Chuck Leverd48b1d22016-06-29 13:52:29 -0400130{
131 int rc;
132
Chuck Lever9d6b0402016-06-29 13:54:16 -0400133 /* Ensure MW is not on any rl_registered list */
134 if (!list_empty(&r->mw_list))
135 list_del(&r->mw_list);
136
Chuck Leverd48b1d22016-06-29 13:52:29 -0400137 rc = ib_dereg_mr(r->frmr.fr_mr);
138 if (rc)
139 pr_err("rpcrdma: final ib_dereg_mr for %p returned %i\n",
140 r, rc);
141 kfree(r->mw_sg);
Chuck Levere2ac2362016-06-29 13:54:00 -0400142 kfree(r);
Chuck Leverd48b1d22016-06-29 13:52:29 -0400143}
144
145static int
Chuck Leverd7a21c12016-05-02 14:42:12 -0400146__frwr_reset_mr(struct rpcrdma_ia *ia, struct rpcrdma_mw *r)
147{
148 struct rpcrdma_frmr *f = &r->frmr;
149 int rc;
150
151 rc = ib_dereg_mr(f->fr_mr);
152 if (rc) {
153 pr_warn("rpcrdma: ib_dereg_mr status %d, frwr %p orphaned\n",
154 rc, r);
155 return rc;
156 }
157
158 f->fr_mr = ib_alloc_mr(ia->ri_pd, IB_MR_TYPE_MEM_REG,
159 ia->ri_max_frmr_depth);
160 if (IS_ERR(f->fr_mr)) {
161 pr_warn("rpcrdma: ib_alloc_mr status %ld, frwr %p orphaned\n",
162 PTR_ERR(f->fr_mr), r);
163 return PTR_ERR(f->fr_mr);
164 }
165
Chuck Levereeb30612016-09-15 10:57:40 -0400166 dprintk("RPC: %s: recovered FRMR %p\n", __func__, f);
Chuck Leverd7a21c12016-05-02 14:42:12 -0400167 f->fr_state = FRMR_IS_INVALID;
168 return 0;
169}
170
Chuck Lever505bbe62016-06-29 13:52:54 -0400171/* Reset of a single FRMR. Generate a fresh rkey by replacing the MR.
172 *
173 * There's no recovery if this fails. The FRMR is abandoned, but
174 * remains in rb_all. It will be cleaned up when the transport is
175 * destroyed.
176 */
Chuck Lever660bb492016-05-02 14:42:21 -0400177static void
Chuck Lever505bbe62016-06-29 13:52:54 -0400178frwr_op_recover_mr(struct rpcrdma_mw *mw)
Chuck Lever660bb492016-05-02 14:42:21 -0400179{
Chuck Lever564471d2016-06-29 13:52:21 -0400180 struct rpcrdma_xprt *r_xprt = mw->mw_xprt;
Chuck Lever660bb492016-05-02 14:42:21 -0400181 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
Chuck Lever660bb492016-05-02 14:42:21 -0400182 int rc;
183
184 rc = __frwr_reset_mr(ia, mw);
Chuck Lever564471d2016-06-29 13:52:21 -0400185 ib_dma_unmap_sg(ia->ri_device, mw->mw_sg, mw->mw_nents, mw->mw_dir);
Chuck Lever2ffc8712016-06-29 13:54:08 -0400186 if (rc)
187 goto out_release;
Chuck Lever505bbe62016-06-29 13:52:54 -0400188
Chuck Lever660bb492016-05-02 14:42:21 -0400189 rpcrdma_put_mw(r_xprt, mw);
Chuck Lever505bbe62016-06-29 13:52:54 -0400190 r_xprt->rx_stats.mrs_recovered++;
Chuck Lever2ffc8712016-06-29 13:54:08 -0400191 return;
192
193out_release:
194 pr_err("rpcrdma: FRMR reset failed %d, %p release\n", rc, mw);
195 r_xprt->rx_stats.mrs_orphaned++;
196
197 spin_lock(&r_xprt->rx_buf.rb_mwlock);
198 list_del(&mw->mw_all);
199 spin_unlock(&r_xprt->rx_buf.rb_mwlock);
200
201 frwr_op_release_mr(mw);
Chuck Lever951e7212015-05-26 11:52:25 -0400202}
203
Chuck Lever91e70e72015-03-30 14:34:58 -0400204static int
Chuck Lever3968cb52015-03-30 14:35:26 -0400205frwr_op_open(struct rpcrdma_ia *ia, struct rpcrdma_ep *ep,
206 struct rpcrdma_create_data_internal *cdata)
207{
Chuck Lever3968cb52015-03-30 14:35:26 -0400208 int depth, delta;
209
210 ia->ri_max_frmr_depth =
211 min_t(unsigned int, RPCRDMA_MAX_DATA_SEGS,
Or Gerlitze3e45b12015-12-18 10:59:48 +0200212 ia->ri_device->attrs.max_fast_reg_page_list_len);
Chuck Lever3968cb52015-03-30 14:35:26 -0400213 dprintk("RPC: %s: device's max FR page list len = %u\n",
214 __func__, ia->ri_max_frmr_depth);
215
216 /* Add room for frmr register and invalidate WRs.
217 * 1. FRMR reg WR for head
218 * 2. FRMR invalidate WR for head
219 * 3. N FRMR reg WRs for pagelist
220 * 4. N FRMR invalidate WRs for pagelist
221 * 5. FRMR reg WR for tail
222 * 6. FRMR invalidate WR for tail
223 * 7. The RDMA_SEND WR
224 */
225 depth = 7;
226
227 /* Calculate N if the device max FRMR depth is smaller than
228 * RPCRDMA_MAX_DATA_SEGS.
229 */
230 if (ia->ri_max_frmr_depth < RPCRDMA_MAX_DATA_SEGS) {
231 delta = RPCRDMA_MAX_DATA_SEGS - ia->ri_max_frmr_depth;
232 do {
233 depth += 2; /* FRMR reg + invalidate */
234 delta -= ia->ri_max_frmr_depth;
235 } while (delta > 0);
236 }
237
238 ep->rep_attr.cap.max_send_wr *= depth;
Or Gerlitze3e45b12015-12-18 10:59:48 +0200239 if (ep->rep_attr.cap.max_send_wr > ia->ri_device->attrs.max_qp_wr) {
240 cdata->max_requests = ia->ri_device->attrs.max_qp_wr / depth;
Chuck Lever3968cb52015-03-30 14:35:26 -0400241 if (!cdata->max_requests)
242 return -EINVAL;
243 ep->rep_attr.cap.max_send_wr = cdata->max_requests *
244 depth;
245 }
246
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400247 ia->ri_max_segs = max_t(unsigned int, 1, RPCRDMA_MAX_DATA_SEGS /
248 ia->ri_max_frmr_depth);
Chuck Lever3968cb52015-03-30 14:35:26 -0400249 return 0;
250}
251
Chuck Lever1c9351e2015-03-30 14:34:30 -0400252/* FRWR mode conveys a list of pages per chunk segment. The
253 * maximum length of that list is the FRWR page list depth.
254 */
255static size_t
256frwr_op_maxpages(struct rpcrdma_xprt *r_xprt)
257{
258 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
259
260 return min_t(unsigned int, RPCRDMA_MAX_DATA_SEGS,
Chuck Lever94931742016-05-02 14:40:56 -0400261 RPCRDMA_MAX_HDR_SEGS * ia->ri_max_frmr_depth);
Chuck Lever1c9351e2015-03-30 14:34:30 -0400262}
263
Chuck Levere46ac342015-03-30 14:35:35 -0400264static void
Chuck Lever2fa8f882016-03-04 11:28:53 -0500265__frwr_sendcompletion_flush(struct ib_wc *wc, struct rpcrdma_frmr *frmr,
266 const char *wr)
Chuck Levere46ac342015-03-30 14:35:35 -0400267{
Chuck Lever2fa8f882016-03-04 11:28:53 -0500268 frmr->fr_state = FRMR_IS_STALE;
269 if (wc->status != IB_WC_WR_FLUSH_ERR)
270 pr_err("rpcrdma: %s: %s (%u/0x%x)\n",
271 wr, ib_wc_status_msg(wc->status),
272 wc->status, wc->vendor_err);
Chuck Levere46ac342015-03-30 14:35:35 -0400273}
274
Chuck Lever2fa8f882016-03-04 11:28:53 -0500275/**
276 * frwr_wc_fastreg - Invoked by RDMA provider for each polled FastReg WC
277 * @cq: completion queue (ignored)
278 * @wc: completed WR
279 *
280 */
Chuck Leverc9918ff2015-12-16 17:22:47 -0500281static void
Chuck Lever2fa8f882016-03-04 11:28:53 -0500282frwr_wc_fastreg(struct ib_cq *cq, struct ib_wc *wc)
Chuck Leverc9918ff2015-12-16 17:22:47 -0500283{
Chuck Lever2fa8f882016-03-04 11:28:53 -0500284 struct rpcrdma_frmr *frmr;
285 struct ib_cqe *cqe;
Chuck Leverc9918ff2015-12-16 17:22:47 -0500286
Chuck Lever2fa8f882016-03-04 11:28:53 -0500287 /* WARNING: Only wr_cqe and status are reliable at this point */
288 if (wc->status != IB_WC_SUCCESS) {
289 cqe = wc->wr_cqe;
290 frmr = container_of(cqe, struct rpcrdma_frmr, fr_cqe);
291 __frwr_sendcompletion_flush(wc, frmr, "fastreg");
292 }
293}
Chuck Leverc9918ff2015-12-16 17:22:47 -0500294
Chuck Lever2fa8f882016-03-04 11:28:53 -0500295/**
296 * frwr_wc_localinv - Invoked by RDMA provider for each polled LocalInv WC
297 * @cq: completion queue (ignored)
298 * @wc: completed WR
299 *
300 */
301static void
302frwr_wc_localinv(struct ib_cq *cq, struct ib_wc *wc)
303{
304 struct rpcrdma_frmr *frmr;
305 struct ib_cqe *cqe;
306
307 /* WARNING: Only wr_cqe and status are reliable at this point */
308 if (wc->status != IB_WC_SUCCESS) {
309 cqe = wc->wr_cqe;
310 frmr = container_of(cqe, struct rpcrdma_frmr, fr_cqe);
311 __frwr_sendcompletion_flush(wc, frmr, "localinv");
312 }
313}
314
315/**
316 * frwr_wc_localinv - Invoked by RDMA provider for each polled LocalInv WC
317 * @cq: completion queue (ignored)
318 * @wc: completed WR
319 *
320 * Awaken anyone waiting for an MR to finish being fenced.
321 */
322static void
323frwr_wc_localinv_wake(struct ib_cq *cq, struct ib_wc *wc)
324{
325 struct rpcrdma_frmr *frmr;
326 struct ib_cqe *cqe;
327
328 /* WARNING: Only wr_cqe and status are reliable at this point */
329 cqe = wc->wr_cqe;
330 frmr = container_of(cqe, struct rpcrdma_frmr, fr_cqe);
331 if (wc->status != IB_WC_SUCCESS)
332 __frwr_sendcompletion_flush(wc, frmr, "localinv");
Daniel Wagner5690a222016-09-23 10:41:57 +0200333 complete(&frmr->fr_linv_done);
Chuck Leverc9918ff2015-12-16 17:22:47 -0500334}
335
Chuck Lever564471d2016-06-29 13:52:21 -0400336/* Post a REG_MR Work Request to register a memory region
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400337 * for remote access via RDMA READ or RDMA WRITE.
338 */
339static int
340frwr_op_map(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr_seg *seg,
Chuck Lever9d6b0402016-06-29 13:54:16 -0400341 int nsegs, bool writing, struct rpcrdma_mw **out)
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400342{
343 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
Chuck Leverc14d86e2015-05-26 11:52:35 -0400344 struct rpcrdma_mw *mw;
345 struct rpcrdma_frmr *frmr;
346 struct ib_mr *mr;
Chuck Lever3cf4e162015-12-16 17:22:31 -0500347 struct ib_reg_wr *reg_wr;
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100348 struct ib_send_wr *bad_wr;
Sagi Grimberg4143f342015-10-13 19:11:35 +0300349 int rc, i, n, dma_nents;
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400350 u8 key;
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400351
Chuck Lever9d6b0402016-06-29 13:54:16 -0400352 mw = NULL;
Chuck Leverc14d86e2015-05-26 11:52:35 -0400353 do {
354 if (mw)
Chuck Lever505bbe62016-06-29 13:52:54 -0400355 rpcrdma_defer_mr_recovery(mw);
Chuck Leverc14d86e2015-05-26 11:52:35 -0400356 mw = rpcrdma_get_mw(r_xprt);
357 if (!mw)
Chuck Lever7a89f9c2016-06-29 13:53:43 -0400358 return -ENOBUFS;
Chuck Leverc882a652016-03-04 11:28:45 -0500359 } while (mw->frmr.fr_state != FRMR_IS_INVALID);
360 frmr = &mw->frmr;
Chuck Leverc14d86e2015-05-26 11:52:35 -0400361 frmr->fr_state = FRMR_IS_VALID;
Sagi Grimberg4143f342015-10-13 19:11:35 +0300362 mr = frmr->fr_mr;
Chuck Lever3cf4e162015-12-16 17:22:31 -0500363 reg_wr = &frmr->fr_regwr;
Chuck Leverc14d86e2015-05-26 11:52:35 -0400364
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400365 if (nsegs > ia->ri_max_frmr_depth)
366 nsegs = ia->ri_max_frmr_depth;
Sagi Grimberg4143f342015-10-13 19:11:35 +0300367 for (i = 0; i < nsegs;) {
368 if (seg->mr_page)
Chuck Lever564471d2016-06-29 13:52:21 -0400369 sg_set_page(&mw->mw_sg[i],
Sagi Grimberg4143f342015-10-13 19:11:35 +0300370 seg->mr_page,
371 seg->mr_len,
372 offset_in_page(seg->mr_offset));
373 else
Chuck Lever564471d2016-06-29 13:52:21 -0400374 sg_set_buf(&mw->mw_sg[i], seg->mr_offset,
Sagi Grimberg4143f342015-10-13 19:11:35 +0300375 seg->mr_len);
376
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400377 ++seg;
378 ++i;
Sagi Grimberg4143f342015-10-13 19:11:35 +0300379
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400380 /* Check for holes */
381 if ((i < nsegs && offset_in_page(seg->mr_offset)) ||
382 offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len))
383 break;
384 }
Chuck Lever564471d2016-06-29 13:52:21 -0400385 mw->mw_nents = i;
386 mw->mw_dir = rpcrdma_data_dir(writing);
Chuck Levera54d4052016-06-29 13:53:52 -0400387 if (i == 0)
388 goto out_dmamap_err;
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400389
Chuck Lever564471d2016-06-29 13:52:21 -0400390 dma_nents = ib_dma_map_sg(ia->ri_device,
391 mw->mw_sg, mw->mw_nents, mw->mw_dir);
392 if (!dma_nents)
393 goto out_dmamap_err;
Sagi Grimberg4143f342015-10-13 19:11:35 +0300394
Chuck Lever564471d2016-06-29 13:52:21 -0400395 n = ib_map_mr_sg(mr, mw->mw_sg, mw->mw_nents, NULL, PAGE_SIZE);
396 if (unlikely(n != mw->mw_nents))
397 goto out_mapmr_err;
Sagi Grimberg4143f342015-10-13 19:11:35 +0300398
399 dprintk("RPC: %s: Using frmr %p to map %u segments (%u bytes)\n",
Chuck Levereeb30612016-09-15 10:57:40 -0400400 __func__, frmr, mw->mw_nents, mr->length);
Sagi Grimberg4143f342015-10-13 19:11:35 +0300401
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400402 key = (u8)(mr->rkey & 0x000000FF);
403 ib_update_fast_reg_key(mr, ++key);
Sagi Grimberg4143f342015-10-13 19:11:35 +0300404
Chuck Lever3cf4e162015-12-16 17:22:31 -0500405 reg_wr->wr.next = NULL;
406 reg_wr->wr.opcode = IB_WR_REG_MR;
Chuck Lever2fa8f882016-03-04 11:28:53 -0500407 frmr->fr_cqe.done = frwr_wc_fastreg;
408 reg_wr->wr.wr_cqe = &frmr->fr_cqe;
Chuck Lever3cf4e162015-12-16 17:22:31 -0500409 reg_wr->wr.num_sge = 0;
410 reg_wr->wr.send_flags = 0;
411 reg_wr->mr = mr;
412 reg_wr->key = mr->rkey;
413 reg_wr->access = writing ?
414 IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
415 IB_ACCESS_REMOTE_READ;
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400416
417 DECR_CQCOUNT(&r_xprt->rx_ep);
Chuck Lever3cf4e162015-12-16 17:22:31 -0500418 rc = ib_post_send(ia->ri_id->qp, &reg_wr->wr, &bad_wr);
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400419 if (rc)
420 goto out_senderr;
421
Chuck Lever9d6b0402016-06-29 13:54:16 -0400422 mw->mw_handle = mr->rkey;
423 mw->mw_length = mr->length;
424 mw->mw_offset = mr->iova;
Sagi Grimberg4143f342015-10-13 19:11:35 +0300425
Chuck Lever9d6b0402016-06-29 13:54:16 -0400426 *out = mw;
Chuck Lever564471d2016-06-29 13:52:21 -0400427 return mw->mw_nents;
428
429out_dmamap_err:
430 pr_err("rpcrdma: failed to dma map sg %p sg_nents %u\n",
431 mw->mw_sg, mw->mw_nents);
Chuck Lever42fe28f2016-06-29 13:53:02 -0400432 rpcrdma_defer_mr_recovery(mw);
Chuck Lever7a89f9c2016-06-29 13:53:43 -0400433 return -EIO;
Chuck Lever564471d2016-06-29 13:52:21 -0400434
435out_mapmr_err:
436 pr_err("rpcrdma: failed to map mr %p (%u/%u)\n",
437 frmr->fr_mr, n, mw->mw_nents);
Chuck Lever505bbe62016-06-29 13:52:54 -0400438 rpcrdma_defer_mr_recovery(mw);
Chuck Lever7a89f9c2016-06-29 13:53:43 -0400439 return -EIO;
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400440
441out_senderr:
Chuck Lever7a89f9c2016-06-29 13:53:43 -0400442 pr_err("rpcrdma: FRMR registration ib_post_send returned %i\n", rc);
Chuck Lever505bbe62016-06-29 13:52:54 -0400443 rpcrdma_defer_mr_recovery(mw);
Chuck Lever7a89f9c2016-06-29 13:53:43 -0400444 return -ENOTCONN;
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400445}
446
Chuck Leverc9918ff2015-12-16 17:22:47 -0500447static struct ib_send_wr *
Chuck Lever9d6b0402016-06-29 13:54:16 -0400448__frwr_prepare_linv_wr(struct rpcrdma_mw *mw)
Chuck Leverc9918ff2015-12-16 17:22:47 -0500449{
Chuck Leverc882a652016-03-04 11:28:45 -0500450 struct rpcrdma_frmr *f = &mw->frmr;
Chuck Leverc9918ff2015-12-16 17:22:47 -0500451 struct ib_send_wr *invalidate_wr;
452
Chuck Levereeb30612016-09-15 10:57:40 -0400453 dprintk("RPC: %s: invalidating frmr %p\n", __func__, f);
454
Chuck Leverc9918ff2015-12-16 17:22:47 -0500455 f->fr_state = FRMR_IS_INVALID;
456 invalidate_wr = &f->fr_invwr;
457
458 memset(invalidate_wr, 0, sizeof(*invalidate_wr));
Chuck Lever2fa8f882016-03-04 11:28:53 -0500459 f->fr_cqe.done = frwr_wc_localinv;
460 invalidate_wr->wr_cqe = &f->fr_cqe;
Chuck Leverc9918ff2015-12-16 17:22:47 -0500461 invalidate_wr->opcode = IB_WR_LOCAL_INV;
462 invalidate_wr->ex.invalidate_rkey = f->fr_mr->rkey;
463
464 return invalidate_wr;
465}
466
Chuck Leverc9918ff2015-12-16 17:22:47 -0500467/* Invalidate all memory regions that were registered for "req".
468 *
469 * Sleeps until it is safe for the host CPU to access the
470 * previously mapped memory regions.
Chuck Lever9d6b0402016-06-29 13:54:16 -0400471 *
472 * Caller ensures that req->rl_registered is not empty.
Chuck Leverc9918ff2015-12-16 17:22:47 -0500473 */
474static void
475frwr_op_unmap_sync(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req)
476{
477 struct ib_send_wr *invalidate_wrs, *pos, *prev, *bad_wr;
Chuck Leverc8b920b2016-09-15 10:57:16 -0400478 struct rpcrdma_rep *rep = req->rl_reply;
Chuck Leverc9918ff2015-12-16 17:22:47 -0500479 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
Chuck Lever9d6b0402016-06-29 13:54:16 -0400480 struct rpcrdma_mw *mw, *tmp;
Chuck Leverc9918ff2015-12-16 17:22:47 -0500481 struct rpcrdma_frmr *f;
482 int rc;
483
484 dprintk("RPC: %s: req %p\n", __func__, req);
485
486 /* ORDER: Invalidate all of the req's MRs first
487 *
488 * Chain the LOCAL_INV Work Requests and post them with
489 * a single ib_post_send() call.
490 */
Chuck Lever9d6b0402016-06-29 13:54:16 -0400491 f = NULL;
Chuck Leverc9918ff2015-12-16 17:22:47 -0500492 invalidate_wrs = pos = prev = NULL;
Chuck Lever9d6b0402016-06-29 13:54:16 -0400493 list_for_each_entry(mw, &req->rl_registered, mw_list) {
Chuck Leverc8b920b2016-09-15 10:57:16 -0400494 if ((rep->rr_wc_flags & IB_WC_WITH_INVALIDATE) &&
495 (mw->mw_handle == rep->rr_inv_rkey)) {
496 mw->frmr.fr_state = FRMR_IS_INVALID;
497 continue;
498 }
499
Chuck Lever9d6b0402016-06-29 13:54:16 -0400500 pos = __frwr_prepare_linv_wr(mw);
Chuck Leverc9918ff2015-12-16 17:22:47 -0500501
502 if (!invalidate_wrs)
503 invalidate_wrs = pos;
504 else
505 prev->next = pos;
506 prev = pos;
Chuck Lever9d6b0402016-06-29 13:54:16 -0400507 f = &mw->frmr;
Chuck Leverc9918ff2015-12-16 17:22:47 -0500508 }
Chuck Leverc8b920b2016-09-15 10:57:16 -0400509 if (!f)
510 goto unmap;
Chuck Leverc9918ff2015-12-16 17:22:47 -0500511
512 /* Strong send queue ordering guarantees that when the
513 * last WR in the chain completes, all WRs in the chain
514 * are complete.
515 */
516 f->fr_invwr.send_flags = IB_SEND_SIGNALED;
Chuck Lever2fa8f882016-03-04 11:28:53 -0500517 f->fr_cqe.done = frwr_wc_localinv_wake;
518 reinit_completion(&f->fr_linv_done);
Chuck Leverc9918ff2015-12-16 17:22:47 -0500519 INIT_CQCOUNT(&r_xprt->rx_ep);
520
521 /* Transport disconnect drains the receive CQ before it
522 * replaces the QP. The RPC reply handler won't call us
523 * unless ri_id->qp is a valid pointer.
524 */
Chuck Leverc8b920b2016-09-15 10:57:16 -0400525 r_xprt->rx_stats.local_inv_needed++;
Chuck Leverc9918ff2015-12-16 17:22:47 -0500526 rc = ib_post_send(ia->ri_id->qp, invalidate_wrs, &bad_wr);
Chuck Leverd7a21c12016-05-02 14:42:12 -0400527 if (rc)
528 goto reset_mrs;
Chuck Leverc9918ff2015-12-16 17:22:47 -0500529
530 wait_for_completion(&f->fr_linv_done);
531
532 /* ORDER: Now DMA unmap all of the req's MRs, and return
533 * them to the free MW list.
534 */
Chuck Leverb892a692016-03-04 11:28:01 -0500535unmap:
Chuck Lever9d6b0402016-06-29 13:54:16 -0400536 list_for_each_entry_safe(mw, tmp, &req->rl_registered, mw_list) {
Chuck Levereeb30612016-09-15 10:57:40 -0400537 dprintk("RPC: %s: unmapping frmr %p\n",
538 __func__, &mw->frmr);
Chuck Lever9d6b0402016-06-29 13:54:16 -0400539 list_del_init(&mw->mw_list);
Chuck Lever564471d2016-06-29 13:52:21 -0400540 ib_dma_unmap_sg(ia->ri_device,
541 mw->mw_sg, mw->mw_nents, mw->mw_dir);
Chuck Leverd7a21c12016-05-02 14:42:12 -0400542 rpcrdma_put_mw(r_xprt, mw);
Chuck Leverc9918ff2015-12-16 17:22:47 -0500543 }
Chuck Leverd7a21c12016-05-02 14:42:12 -0400544 return;
545
546reset_mrs:
Chuck Lever7a89f9c2016-06-29 13:53:43 -0400547 pr_err("rpcrdma: FRMR invalidate ib_post_send returned %i\n", rc);
548 rdma_disconnect(ia->ri_id);
Chuck Leverd7a21c12016-05-02 14:42:12 -0400549
550 /* Find and reset the MRs in the LOCAL_INV WRs that did not
551 * get posted. This is synchronous, and slow.
552 */
Chuck Lever9d6b0402016-06-29 13:54:16 -0400553 list_for_each_entry(mw, &req->rl_registered, mw_list) {
Chuck Leverd7a21c12016-05-02 14:42:12 -0400554 f = &mw->frmr;
Chuck Leverd7a21c12016-05-02 14:42:12 -0400555 if (mw->frmr.fr_mr->rkey == bad_wr->ex.invalidate_rkey) {
556 __frwr_reset_mr(ia, mw);
557 bad_wr = bad_wr->next;
558 }
Chuck Leverd7a21c12016-05-02 14:42:12 -0400559 }
560 goto unmap;
Chuck Leverc9918ff2015-12-16 17:22:47 -0500561}
562
Chuck Leveread3f262016-05-02 14:42:46 -0400563/* Use a slow, safe mechanism to invalidate all memory regions
564 * that were registered for "req".
Chuck Lever6814bae2015-03-30 14:34:48 -0400565 */
Chuck Leveread3f262016-05-02 14:42:46 -0400566static void
567frwr_op_unmap_safe(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req,
568 bool sync)
Chuck Lever6814bae2015-03-30 14:34:48 -0400569{
Chuck Leveread3f262016-05-02 14:42:46 -0400570 struct rpcrdma_mw *mw;
Chuck Lever6814bae2015-03-30 14:34:48 -0400571
Chuck Lever9d6b0402016-06-29 13:54:16 -0400572 while (!list_empty(&req->rl_registered)) {
573 mw = list_first_entry(&req->rl_registered,
574 struct rpcrdma_mw, mw_list);
575 list_del_init(&mw->mw_list);
Chuck Leverc14d86e2015-05-26 11:52:35 -0400576
Chuck Leveread3f262016-05-02 14:42:46 -0400577 if (sync)
Chuck Lever505bbe62016-06-29 13:52:54 -0400578 frwr_op_recover_mr(mw);
Chuck Leveread3f262016-05-02 14:42:46 -0400579 else
Chuck Lever505bbe62016-06-29 13:52:54 -0400580 rpcrdma_defer_mr_recovery(mw);
Chuck Leveread3f262016-05-02 14:42:46 -0400581 }
Chuck Lever6814bae2015-03-30 14:34:48 -0400582}
583
Chuck Levera0ce85f2015-03-30 14:34:21 -0400584const struct rpcrdma_memreg_ops rpcrdma_frwr_memreg_ops = {
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400585 .ro_map = frwr_op_map,
Chuck Leverc9918ff2015-12-16 17:22:47 -0500586 .ro_unmap_sync = frwr_op_unmap_sync,
Chuck Leveread3f262016-05-02 14:42:46 -0400587 .ro_unmap_safe = frwr_op_unmap_safe,
Chuck Lever505bbe62016-06-29 13:52:54 -0400588 .ro_recover_mr = frwr_op_recover_mr,
Chuck Lever3968cb52015-03-30 14:35:26 -0400589 .ro_open = frwr_op_open,
Chuck Lever1c9351e2015-03-30 14:34:30 -0400590 .ro_maxpages = frwr_op_maxpages,
Chuck Levere2ac2362016-06-29 13:54:00 -0400591 .ro_init_mr = frwr_op_init_mr,
592 .ro_release_mr = frwr_op_release_mr,
Chuck Levera0ce85f2015-03-30 14:34:21 -0400593 .ro_displayname = "frwr",
Chuck Leverc8b920b2016-09-15 10:57:16 -0400594 .ro_send_w_inv_ok = RPCRDMA_CMP_F_SND_W_INV_OK,
Chuck Levera0ce85f2015-03-30 14:34:21 -0400595};