]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/sunrpc/xprtrdma/frwr_ops.c
Merge tag 'reset-fixes-for-4.14' of git://git.pengutronix.de/git/pza/linux into fixes
[mirror_ubuntu-bionic-kernel.git] / net / sunrpc / xprtrdma / frwr_ops.c
CommitLineData
a0ce85f5
CL
1/*
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
c14d86e5
CL
14/* 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
62bdf94a 47 * four states:
c14d86e5
CL
48 *
49 * INVALID: The MR was not in use before the QP entered ERROR state.
c14d86e5
CL
50 *
51 * VALID: The MR was registered before the QP entered ERROR state.
52 *
62bdf94a
CL
53 * FLUSHED_FR: The MR was being registered when the QP entered ERROR
54 * state, and the pending WR was flushed.
55 *
56 * FLUSHED_LI: The MR was being invalidated when the QP entered ERROR
57 * state, and the pending WR was flushed.
58 *
59 * When frwr_op_map encounters FLUSHED and VALID MRs, they are recovered
60 * with ib_dereg_mr and then are re-initialized. Because MR recovery
c14d86e5
CL
61 * allocates fresh resources, it is deferred to a workqueue, and the
62 * recovered MRs are placed back on the rb_mws list when recovery is
63 * complete. frwr_op_map allocates another MR for the current RPC while
64 * the broken MR is reset.
65 *
66 * To ensure that frwr_op_map doesn't encounter an MR that is marked
67 * INVALID but that is about to be flushed due to a previous transport
68 * disconnect, the transport connect worker attempts to drain all
69 * pending send queue WRs before the transport is reconnected.
70 */
71
c8b920bb
CL
72#include <linux/sunrpc/rpc_rdma.h>
73
a0ce85f5
CL
74#include "xprt_rdma.h"
75
76#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
77# define RPCDBG_FACILITY RPCDBG_TRANS
78#endif
79
b54054ca
CL
80bool
81frwr_is_supported(struct rpcrdma_ia *ia)
82{
83 struct ib_device_attr *attrs = &ia->ri_device->attrs;
84
85 if (!(attrs->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS))
86 goto out_not_supported;
87 if (attrs->max_fast_reg_page_list_len == 0)
88 goto out_not_supported;
89 return true;
90
91out_not_supported:
92 pr_info("rpcrdma: 'frwr' mode is not supported by device %s\n",
93 ia->ri_device->name);
94 return false;
95}
96
d48b1d29 97static int
e2ac236c 98frwr_op_init_mr(struct rpcrdma_ia *ia, struct rpcrdma_mw *r)
d48b1d29 99{
e2ac236c 100 unsigned int depth = ia->ri_max_frmr_depth;
d48b1d29
CL
101 struct rpcrdma_frmr *f = &r->frmr;
102 int rc;
103
5e9fc6a0 104 f->fr_mr = ib_alloc_mr(ia->ri_pd, ia->ri_mrtype, depth);
d48b1d29
CL
105 if (IS_ERR(f->fr_mr))
106 goto out_mr_err;
107
108 r->mw_sg = kcalloc(depth, sizeof(*r->mw_sg), GFP_KERNEL);
109 if (!r->mw_sg)
110 goto out_list_err;
111
112 sg_init_table(r->mw_sg, depth);
113 init_completion(&f->fr_linv_done);
114 return 0;
115
116out_mr_err:
117 rc = PTR_ERR(f->fr_mr);
118 dprintk("RPC: %s: ib_alloc_mr status %i\n",
119 __func__, rc);
120 return rc;
121
122out_list_err:
123 rc = -ENOMEM;
124 dprintk("RPC: %s: sg allocation failure\n",
125 __func__);
126 ib_dereg_mr(f->fr_mr);
127 return rc;
128}
129
130static void
e2ac236c 131frwr_op_release_mr(struct rpcrdma_mw *r)
d48b1d29
CL
132{
133 int rc;
134
9d6b0409
CL
135 /* Ensure MW is not on any rl_registered list */
136 if (!list_empty(&r->mw_list))
137 list_del(&r->mw_list);
138
d48b1d29
CL
139 rc = ib_dereg_mr(r->frmr.fr_mr);
140 if (rc)
141 pr_err("rpcrdma: final ib_dereg_mr for %p returned %i\n",
142 r, rc);
143 kfree(r->mw_sg);
e2ac236c 144 kfree(r);
d48b1d29
CL
145}
146
d7a21c1b
CL
147static int
148__frwr_reset_mr(struct rpcrdma_ia *ia, struct rpcrdma_mw *r)
149{
150 struct rpcrdma_frmr *f = &r->frmr;
151 int rc;
152
153 rc = ib_dereg_mr(f->fr_mr);
154 if (rc) {
155 pr_warn("rpcrdma: ib_dereg_mr status %d, frwr %p orphaned\n",
156 rc, r);
157 return rc;
158 }
159
5e9fc6a0 160 f->fr_mr = ib_alloc_mr(ia->ri_pd, ia->ri_mrtype,
d7a21c1b
CL
161 ia->ri_max_frmr_depth);
162 if (IS_ERR(f->fr_mr)) {
163 pr_warn("rpcrdma: ib_alloc_mr status %ld, frwr %p orphaned\n",
164 PTR_ERR(f->fr_mr), r);
165 return PTR_ERR(f->fr_mr);
166 }
167
eeb30613 168 dprintk("RPC: %s: recovered FRMR %p\n", __func__, f);
d7a21c1b
CL
169 f->fr_state = FRMR_IS_INVALID;
170 return 0;
171}
172
505bbe64 173/* Reset of a single FRMR. Generate a fresh rkey by replacing the MR.
505bbe64 174 */
660bb497 175static void
505bbe64 176frwr_op_recover_mr(struct rpcrdma_mw *mw)
660bb497 177{
62bdf94a 178 enum rpcrdma_frmr_state state = mw->frmr.fr_state;
564471d2 179 struct rpcrdma_xprt *r_xprt = mw->mw_xprt;
660bb497 180 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
660bb497
CL
181 int rc;
182
183 rc = __frwr_reset_mr(ia, mw);
62bdf94a
CL
184 if (state != FRMR_FLUSHED_LI)
185 ib_dma_unmap_sg(ia->ri_device,
186 mw->mw_sg, mw->mw_nents, mw->mw_dir);
2ffc871a
CL
187 if (rc)
188 goto out_release;
951e721c 189
505bbe64
CL
190 rpcrdma_put_mw(r_xprt, mw);
191 r_xprt->rx_stats.mrs_recovered++;
2ffc871a
CL
192 return;
193
194out_release:
195 pr_err("rpcrdma: FRMR reset failed %d, %p release\n", rc, mw);
196 r_xprt->rx_stats.mrs_orphaned++;
197
198 spin_lock(&r_xprt->rx_buf.rb_mwlock);
199 list_del(&mw->mw_all);
200 spin_unlock(&r_xprt->rx_buf.rb_mwlock);
201
202 frwr_op_release_mr(mw);
951e721c
CL
203}
204
3968cb58
CL
205static int
206frwr_op_open(struct rpcrdma_ia *ia, struct rpcrdma_ep *ep,
207 struct rpcrdma_create_data_internal *cdata)
208{
5e9fc6a0 209 struct ib_device_attr *attrs = &ia->ri_device->attrs;
3968cb58
CL
210 int depth, delta;
211
5e9fc6a0
CL
212 ia->ri_mrtype = IB_MR_TYPE_MEM_REG;
213 if (attrs->device_cap_flags & IB_DEVICE_SG_GAPS_REG)
214 ia->ri_mrtype = IB_MR_TYPE_SG_GAPS;
215
3968cb58
CL
216 ia->ri_max_frmr_depth =
217 min_t(unsigned int, RPCRDMA_MAX_DATA_SEGS,
5e9fc6a0 218 attrs->max_fast_reg_page_list_len);
3968cb58
CL
219 dprintk("RPC: %s: device's max FR page list len = %u\n",
220 __func__, ia->ri_max_frmr_depth);
221
222 /* Add room for frmr register and invalidate WRs.
223 * 1. FRMR reg WR for head
224 * 2. FRMR invalidate WR for head
225 * 3. N FRMR reg WRs for pagelist
226 * 4. N FRMR invalidate WRs for pagelist
227 * 5. FRMR reg WR for tail
228 * 6. FRMR invalidate WR for tail
229 * 7. The RDMA_SEND WR
230 */
231 depth = 7;
232
233 /* Calculate N if the device max FRMR depth is smaller than
234 * RPCRDMA_MAX_DATA_SEGS.
235 */
236 if (ia->ri_max_frmr_depth < RPCRDMA_MAX_DATA_SEGS) {
237 delta = RPCRDMA_MAX_DATA_SEGS - ia->ri_max_frmr_depth;
238 do {
239 depth += 2; /* FRMR reg + invalidate */
240 delta -= ia->ri_max_frmr_depth;
241 } while (delta > 0);
242 }
243
244 ep->rep_attr.cap.max_send_wr *= depth;
5e9fc6a0
CL
245 if (ep->rep_attr.cap.max_send_wr > attrs->max_qp_wr) {
246 cdata->max_requests = attrs->max_qp_wr / depth;
3968cb58
CL
247 if (!cdata->max_requests)
248 return -EINVAL;
249 ep->rep_attr.cap.max_send_wr = cdata->max_requests *
250 depth;
251 }
252
87cfb9a0
CL
253 ia->ri_max_segs = max_t(unsigned int, 1, RPCRDMA_MAX_DATA_SEGS /
254 ia->ri_max_frmr_depth);
3968cb58
CL
255 return 0;
256}
257
1c9351ee
CL
258/* FRWR mode conveys a list of pages per chunk segment. The
259 * maximum length of that list is the FRWR page list depth.
260 */
261static size_t
262frwr_op_maxpages(struct rpcrdma_xprt *r_xprt)
263{
264 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
265
266 return min_t(unsigned int, RPCRDMA_MAX_DATA_SEGS,
94931746 267 RPCRDMA_MAX_HDR_SEGS * ia->ri_max_frmr_depth);
1c9351ee
CL
268}
269
2fa8f88d 270static void
62bdf94a 271__frwr_sendcompletion_flush(struct ib_wc *wc, const char *wr)
2fa8f88d 272{
2fa8f88d
CL
273 if (wc->status != IB_WC_WR_FLUSH_ERR)
274 pr_err("rpcrdma: %s: %s (%u/0x%x)\n",
275 wr, ib_wc_status_msg(wc->status),
276 wc->status, wc->vendor_err);
277}
278
279/**
6afafa77 280 * frwr_wc_fastreg - Invoked by RDMA provider for a flushed FastReg WC
2fa8f88d
CL
281 * @cq: completion queue (ignored)
282 * @wc: completed WR
c9918ff5 283 *
c9918ff5 284 */
e46ac34c 285static void
2fa8f88d 286frwr_wc_fastreg(struct ib_cq *cq, struct ib_wc *wc)
e46ac34c 287{
2fa8f88d
CL
288 struct rpcrdma_frmr *frmr;
289 struct ib_cqe *cqe;
c9918ff5 290
2fa8f88d
CL
291 /* WARNING: Only wr_cqe and status are reliable at this point */
292 if (wc->status != IB_WC_SUCCESS) {
293 cqe = wc->wr_cqe;
294 frmr = container_of(cqe, struct rpcrdma_frmr, fr_cqe);
62bdf94a
CL
295 frmr->fr_state = FRMR_FLUSHED_FR;
296 __frwr_sendcompletion_flush(wc, "fastreg");
2fa8f88d 297 }
e46ac34c
CL
298}
299
2fa8f88d 300/**
6afafa77 301 * frwr_wc_localinv - Invoked by RDMA provider for a flushed LocalInv WC
2fa8f88d
CL
302 * @cq: completion queue (ignored)
303 * @wc: completed WR
304 *
305 */
c9918ff5 306static void
2fa8f88d 307frwr_wc_localinv(struct ib_cq *cq, struct ib_wc *wc)
c9918ff5 308{
2fa8f88d
CL
309 struct rpcrdma_frmr *frmr;
310 struct ib_cqe *cqe;
c9918ff5 311
2fa8f88d
CL
312 /* WARNING: Only wr_cqe and status are reliable at this point */
313 if (wc->status != IB_WC_SUCCESS) {
314 cqe = wc->wr_cqe;
315 frmr = container_of(cqe, struct rpcrdma_frmr, fr_cqe);
62bdf94a
CL
316 frmr->fr_state = FRMR_FLUSHED_LI;
317 __frwr_sendcompletion_flush(wc, "localinv");
2fa8f88d
CL
318 }
319}
c9918ff5 320
2fa8f88d 321/**
6afafa77 322 * frwr_wc_localinv_wake - Invoked by RDMA provider for a signaled LocalInv WC
2fa8f88d
CL
323 * @cq: completion queue (ignored)
324 * @wc: completed WR
325 *
326 * Awaken anyone waiting for an MR to finish being fenced.
327 */
328static void
329frwr_wc_localinv_wake(struct ib_cq *cq, struct ib_wc *wc)
330{
331 struct rpcrdma_frmr *frmr;
332 struct ib_cqe *cqe;
333
334 /* WARNING: Only wr_cqe and status are reliable at this point */
335 cqe = wc->wr_cqe;
336 frmr = container_of(cqe, struct rpcrdma_frmr, fr_cqe);
62bdf94a
CL
337 if (wc->status != IB_WC_SUCCESS) {
338 frmr->fr_state = FRMR_FLUSHED_LI;
339 __frwr_sendcompletion_flush(wc, "localinv");
340 }
5690a22d 341 complete(&frmr->fr_linv_done);
c9918ff5
CL
342}
343
564471d2 344/* Post a REG_MR Work Request to register a memory region
9c1b4d77
CL
345 * for remote access via RDMA READ or RDMA WRITE.
346 */
6748b0ca 347static struct rpcrdma_mr_seg *
9c1b4d77 348frwr_op_map(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr_seg *seg,
9d6b0409 349 int nsegs, bool writing, struct rpcrdma_mw **out)
9c1b4d77
CL
350{
351 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
5e9fc6a0 352 bool holes_ok = ia->ri_mrtype == IB_MR_TYPE_SG_GAPS;
c14d86e5
CL
353 struct rpcrdma_mw *mw;
354 struct rpcrdma_frmr *frmr;
355 struct ib_mr *mr;
3cf4e169 356 struct ib_reg_wr *reg_wr;
e622f2f4 357 struct ib_send_wr *bad_wr;
1f541895 358 int rc, i, n;
9c1b4d77 359 u8 key;
9c1b4d77 360
9d6b0409 361 mw = NULL;
c14d86e5
CL
362 do {
363 if (mw)
505bbe64 364 rpcrdma_defer_mr_recovery(mw);
c14d86e5
CL
365 mw = rpcrdma_get_mw(r_xprt);
366 if (!mw)
6748b0ca 367 return ERR_PTR(-ENOBUFS);
c882a655
CL
368 } while (mw->frmr.fr_state != FRMR_IS_INVALID);
369 frmr = &mw->frmr;
c14d86e5 370 frmr->fr_state = FRMR_IS_VALID;
4143f34e 371 mr = frmr->fr_mr;
3cf4e169 372 reg_wr = &frmr->fr_regwr;
c14d86e5 373
9c1b4d77
CL
374 if (nsegs > ia->ri_max_frmr_depth)
375 nsegs = ia->ri_max_frmr_depth;
4143f34e
SG
376 for (i = 0; i < nsegs;) {
377 if (seg->mr_page)
564471d2 378 sg_set_page(&mw->mw_sg[i],
4143f34e
SG
379 seg->mr_page,
380 seg->mr_len,
381 offset_in_page(seg->mr_offset));
382 else
564471d2 383 sg_set_buf(&mw->mw_sg[i], seg->mr_offset,
4143f34e
SG
384 seg->mr_len);
385
9c1b4d77
CL
386 ++seg;
387 ++i;
5e9fc6a0
CL
388 if (holes_ok)
389 continue;
9c1b4d77
CL
390 if ((i < nsegs && offset_in_page(seg->mr_offset)) ||
391 offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len))
392 break;
393 }
564471d2 394 mw->mw_dir = rpcrdma_data_dir(writing);
4143f34e 395
1f541895
CL
396 mw->mw_nents = ib_dma_map_sg(ia->ri_device, mw->mw_sg, i, mw->mw_dir);
397 if (!mw->mw_nents)
564471d2
CL
398 goto out_dmamap_err;
399
400 n = ib_map_mr_sg(mr, mw->mw_sg, mw->mw_nents, NULL, PAGE_SIZE);
401 if (unlikely(n != mw->mw_nents))
402 goto out_mapmr_err;
4143f34e 403
edd31551 404 dprintk("RPC: %s: Using frmr %p to map %u segments (%llu bytes)\n",
eeb30613 405 __func__, frmr, mw->mw_nents, mr->length);
4143f34e 406
9c1b4d77
CL
407 key = (u8)(mr->rkey & 0x000000FF);
408 ib_update_fast_reg_key(mr, ++key);
4143f34e 409
3cf4e169
CL
410 reg_wr->wr.next = NULL;
411 reg_wr->wr.opcode = IB_WR_REG_MR;
2fa8f88d
CL
412 frmr->fr_cqe.done = frwr_wc_fastreg;
413 reg_wr->wr.wr_cqe = &frmr->fr_cqe;
3cf4e169
CL
414 reg_wr->wr.num_sge = 0;
415 reg_wr->wr.send_flags = 0;
416 reg_wr->mr = mr;
417 reg_wr->key = mr->rkey;
418 reg_wr->access = writing ?
419 IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
420 IB_ACCESS_REMOTE_READ;
9c1b4d77 421
8d38de65 422 rpcrdma_set_signaled(&r_xprt->rx_ep, &reg_wr->wr);
3cf4e169 423 rc = ib_post_send(ia->ri_id->qp, &reg_wr->wr, &bad_wr);
9c1b4d77
CL
424 if (rc)
425 goto out_senderr;
426
9d6b0409
CL
427 mw->mw_handle = mr->rkey;
428 mw->mw_length = mr->length;
429 mw->mw_offset = mr->iova;
4143f34e 430
9d6b0409 431 *out = mw;
6748b0ca 432 return seg;
564471d2
CL
433
434out_dmamap_err:
1f541895
CL
435 pr_err("rpcrdma: failed to DMA map sg %p sg_nents %d\n",
436 mw->mw_sg, i);
437 frmr->fr_state = FRMR_IS_INVALID;
438 rpcrdma_put_mw(r_xprt, mw);
6748b0ca 439 return ERR_PTR(-EIO);
564471d2
CL
440
441out_mapmr_err:
1f541895 442 pr_err("rpcrdma: failed to map mr %p (%d/%d)\n",
564471d2 443 frmr->fr_mr, n, mw->mw_nents);
505bbe64 444 rpcrdma_defer_mr_recovery(mw);
6748b0ca 445 return ERR_PTR(-EIO);
9c1b4d77
CL
446
447out_senderr:
7a89f9c6 448 pr_err("rpcrdma: FRMR registration ib_post_send returned %i\n", rc);
505bbe64 449 rpcrdma_defer_mr_recovery(mw);
6748b0ca 450 return ERR_PTR(-ENOTCONN);
9c1b4d77
CL
451}
452
c9918ff5
CL
453/* Invalidate all memory regions that were registered for "req".
454 *
455 * Sleeps until it is safe for the host CPU to access the
456 * previously mapped memory regions.
9d6b0409 457 *
451d26e1
CL
458 * Caller ensures that @mws is not empty before the call. This
459 * function empties the list.
c9918ff5
CL
460 */
461static void
451d26e1 462frwr_op_unmap_sync(struct rpcrdma_xprt *r_xprt, struct list_head *mws)
c9918ff5 463{
a100fda1 464 struct ib_send_wr *first, **prev, *last, *bad_wr;
c9918ff5 465 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
c9918ff5 466 struct rpcrdma_frmr *f;
9a5c63e9 467 struct rpcrdma_mw *mw;
8d38de65 468 int count, rc;
c9918ff5 469
451d26e1 470 /* ORDER: Invalidate all of the MRs first
c9918ff5
CL
471 *
472 * Chain the LOCAL_INV Work Requests and post them with
473 * a single ib_post_send() call.
474 */
9d6b0409 475 f = NULL;
8d38de65 476 count = 0;
a100fda1 477 prev = &first;
451d26e1 478 list_for_each_entry(mw, mws, mw_list) {
a100fda1
CL
479 mw->frmr.fr_state = FRMR_IS_INVALID;
480
4b196dc6 481 if (mw->mw_flags & RPCRDMA_MW_F_RI)
c8b920bb 482 continue;
c8b920bb 483
a100fda1
CL
484 f = &mw->frmr;
485 dprintk("RPC: %s: invalidating frmr %p\n",
486 __func__, f);
487
488 f->fr_cqe.done = frwr_wc_localinv;
489 last = &f->fr_invwr;
490 memset(last, 0, sizeof(*last));
491 last->wr_cqe = &f->fr_cqe;
492 last->opcode = IB_WR_LOCAL_INV;
493 last->ex.invalidate_rkey = mw->mw_handle;
8d38de65 494 count++;
c9918ff5 495
a100fda1
CL
496 *prev = last;
497 prev = &last->next;
c9918ff5 498 }
c8b920bb
CL
499 if (!f)
500 goto unmap;
c9918ff5
CL
501
502 /* Strong send queue ordering guarantees that when the
503 * last WR in the chain completes, all WRs in the chain
504 * are complete.
505 */
a100fda1 506 last->send_flags = IB_SEND_SIGNALED;
2fa8f88d
CL
507 f->fr_cqe.done = frwr_wc_localinv_wake;
508 reinit_completion(&f->fr_linv_done);
8d38de65
CL
509
510 /* Initialize CQ count, since there is always a signaled
511 * WR being posted here. The new cqcount depends on how
512 * many SQEs are about to be consumed.
513 */
514 rpcrdma_init_cqcount(&r_xprt->rx_ep, count);
c9918ff5
CL
515
516 /* Transport disconnect drains the receive CQ before it
517 * replaces the QP. The RPC reply handler won't call us
518 * unless ri_id->qp is a valid pointer.
519 */
c8b920bb 520 r_xprt->rx_stats.local_inv_needed++;
8d75483a 521 bad_wr = NULL;
a100fda1 522 rc = ib_post_send(ia->ri_id->qp, first, &bad_wr);
8d75483a
CL
523 if (bad_wr != first)
524 wait_for_completion(&f->fr_linv_done);
d7a21c1b
CL
525 if (rc)
526 goto reset_mrs;
c9918ff5 527
451d26e1 528 /* ORDER: Now DMA unmap all of the MRs, and return
c9918ff5
CL
529 * them to the free MW list.
530 */
b892a699 531unmap:
451d26e1
CL
532 while (!list_empty(mws)) {
533 mw = rpcrdma_pop_mw(mws);
a100fda1 534 dprintk("RPC: %s: DMA unmapping frmr %p\n",
eeb30613 535 __func__, &mw->frmr);
564471d2
CL
536 ib_dma_unmap_sg(ia->ri_device,
537 mw->mw_sg, mw->mw_nents, mw->mw_dir);
d7a21c1b 538 rpcrdma_put_mw(r_xprt, mw);
c9918ff5 539 }
d7a21c1b 540 return;
c9918ff5 541
d7a21c1b 542reset_mrs:
7a89f9c6 543 pr_err("rpcrdma: FRMR invalidate ib_post_send returned %i\n", rc);
6814baea 544
d7a21c1b 545 /* Find and reset the MRs in the LOCAL_INV WRs that did not
8d75483a 546 * get posted.
d7a21c1b 547 */
8d75483a
CL
548 rpcrdma_init_cqcount(&r_xprt->rx_ep, -count);
549 while (bad_wr) {
550 f = container_of(bad_wr, struct rpcrdma_frmr,
551 fr_invwr);
552 mw = container_of(f, struct rpcrdma_mw, frmr);
553
554 __frwr_reset_mr(ia, mw);
555
556 bad_wr = bad_wr->next;
d7a21c1b
CL
557 }
558 goto unmap;
c9918ff5 559}
6814baea 560
ead3f26e
CL
561/* Use a slow, safe mechanism to invalidate all memory regions
562 * that were registered for "req".
563 */
564static void
565frwr_op_unmap_safe(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req,
566 bool sync)
567{
ead3f26e 568 struct rpcrdma_mw *mw;
c14d86e5 569
9d6b0409 570 while (!list_empty(&req->rl_registered)) {
9a5c63e9 571 mw = rpcrdma_pop_mw(&req->rl_registered);
ead3f26e 572 if (sync)
505bbe64 573 frwr_op_recover_mr(mw);
ead3f26e 574 else
505bbe64 575 rpcrdma_defer_mr_recovery(mw);
ead3f26e 576 }
6814baea
CL
577}
578
a0ce85f5 579const struct rpcrdma_memreg_ops rpcrdma_frwr_memreg_ops = {
9c1b4d77 580 .ro_map = frwr_op_map,
c9918ff5 581 .ro_unmap_sync = frwr_op_unmap_sync,
ead3f26e 582 .ro_unmap_safe = frwr_op_unmap_safe,
505bbe64 583 .ro_recover_mr = frwr_op_recover_mr,
3968cb58 584 .ro_open = frwr_op_open,
1c9351ee 585 .ro_maxpages = frwr_op_maxpages,
e2ac236c
CL
586 .ro_init_mr = frwr_op_init_mr,
587 .ro_release_mr = frwr_op_release_mr,
a0ce85f5 588 .ro_displayname = "frwr",
c8b920bb 589 .ro_send_w_inv_ok = RPCRDMA_CMP_F_SND_W_INV_OK,
a0ce85f5 590};