xref: /linux/net/sunrpc/xprtrdma/frwr_ops.c (revision 67f8bc848ee31831336bd478e57d2f993551902e)
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 
7 /* Lightweight memory registration using Fast Registration Work
8  * Requests (FRWR).
9  *
10  * FRWR features ordered asynchronous registration and invalidation
11  * of arbitrarily-sized memory regions. This is the fastest and safest
12  * but most complex memory registration mode.
13  */
14 
15 /* Normal operation
16  *
17  * A Memory Region is prepared for RDMA Read or Write using a FAST_REG
18  * Work Request (frwr_map). When the RDMA operation is finished, this
19  * Memory Region is invalidated using a LOCAL_INV Work Request
20  * (frwr_unmap_async and frwr_unmap_sync).
21  *
22  * Typically FAST_REG Work Requests are not signaled, and neither are
23  * RDMA Send Work Requests (with the exception of signaling occasionally
24  * to prevent provider work queue overflows). This greatly reduces HCA
25  * interrupt workload.
26  */
27 
28 /* Transport recovery
29  *
30  * frwr_map and frwr_unmap_* cannot run at the same time the transport
31  * connect worker is running. The connect worker holds the transport
32  * send lock, just as ->send_request does. This prevents frwr_map and
33  * the connect worker from running concurrently. When a connection is
34  * closed, the Receive completion queue is drained before the allowing
35  * the connect worker to get control. This prevents frwr_unmap and the
36  * connect worker from running concurrently.
37  *
38  * When the underlying transport disconnects, MRs that are in flight
39  * are flushed and are likely unusable. Thus all MRs are destroyed.
40  * New MRs are created on demand.
41  */
42 
43 #include <linux/sunrpc/svc_rdma.h>
44 
45 #include "xprt_rdma.h"
46 #include <trace/events/rpcrdma.h>
47 
48 static void frwr_cid_init(struct rpcrdma_ep *ep,
49 			  struct rpcrdma_mr *mr)
50 {
51 	struct rpc_rdma_cid *cid = &mr->mr_cid;
52 
53 	cid->ci_queue_id = ep->re_attr.send_cq->res.id;
54 	cid->ci_completion_id = mr->mr_ibmr->res.id;
55 }
56 
57 static void frwr_mr_unmap(struct rpcrdma_mr *mr)
58 {
59 	if (mr->mr_device) {
60 		trace_xprtrdma_mr_unmap(mr);
61 		ib_dma_unmap_sg(mr->mr_device, mr->mr_sg, mr->mr_nents,
62 				mr->mr_dir);
63 		mr->mr_device = NULL;
64 	}
65 }
66 
67 /**
68  * frwr_mr_release - Destroy one MR
69  * @mr: MR allocated by frwr_mr_init
70  *
71  */
72 void frwr_mr_release(struct rpcrdma_mr *mr)
73 {
74 	int rc;
75 
76 	frwr_mr_unmap(mr);
77 
78 	rc = ib_dereg_mr(mr->mr_ibmr);
79 	if (rc)
80 		trace_xprtrdma_frwr_dereg(mr, rc);
81 	kfree(mr->mr_sg);
82 	kfree(mr);
83 }
84 
85 static void frwr_mr_put(struct rpcrdma_mr *mr)
86 {
87 	frwr_mr_unmap(mr);
88 
89 	/* The MR is returned to the req's MR free list instead
90 	 * of to the xprt's MR free list. No spinlock is needed.
91 	 */
92 	rpcrdma_mr_push(mr, &mr->mr_req->rl_free_mrs);
93 }
94 
95 /**
96  * frwr_reset - Place MRs back on @req's free list
97  * @req: request to reset
98  *
99  * Used after a failed marshal. For FRWR, this means the MRs
100  * don't have to be fully released and recreated.
101  *
102  * NB: This is safe only as long as none of @req's MRs are
103  * involved with an ongoing asynchronous FAST_REG or LOCAL_INV
104  * Work Request.
105  */
106 void frwr_reset(struct rpcrdma_req *req)
107 {
108 	struct rpcrdma_mr *mr;
109 
110 	while ((mr = rpcrdma_mr_pop(&req->rl_registered)))
111 		frwr_mr_put(mr);
112 }
113 
114 /**
115  * frwr_mr_init - Initialize one MR
116  * @r_xprt: controlling transport instance
117  * @mr: generic MR to prepare for FRWR
118  *
119  * Returns zero if successful. Otherwise a negative errno
120  * is returned.
121  */
122 int frwr_mr_init(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr *mr)
123 {
124 	struct rpcrdma_ep *ep = r_xprt->rx_ep;
125 	unsigned int depth = ep->re_max_fr_depth;
126 	struct scatterlist *sg;
127 	struct ib_mr *frmr;
128 
129 	sg = kcalloc_node(depth, sizeof(*sg), XPRTRDMA_GFP_FLAGS,
130 			  ibdev_to_node(ep->re_id->device));
131 	if (!sg)
132 		return -ENOMEM;
133 
134 	frmr = ib_alloc_mr(ep->re_pd, ep->re_mrtype, depth);
135 	if (IS_ERR(frmr))
136 		goto out_mr_err;
137 
138 	mr->mr_xprt = r_xprt;
139 	mr->mr_ibmr = frmr;
140 	mr->mr_device = NULL;
141 	INIT_LIST_HEAD(&mr->mr_list);
142 	init_completion(&mr->mr_linv_done);
143 	frwr_cid_init(ep, mr);
144 
145 	sg_init_table(sg, depth);
146 	mr->mr_sg = sg;
147 	return 0;
148 
149 out_mr_err:
150 	kfree(sg);
151 	trace_xprtrdma_frwr_alloc(mr, PTR_ERR(frmr));
152 	return PTR_ERR(frmr);
153 }
154 
155 /**
156  * frwr_query_device - Prepare a transport for use with FRWR
157  * @ep: endpoint to fill in
158  * @device: RDMA device to query
159  *
160  * On success, sets:
161  *	ep->re_attr
162  *	ep->re_max_requests
163  *	ep->re_max_rdma_segs
164  *	ep->re_max_fr_depth
165  *	ep->re_mrtype
166  *
167  * Return values:
168  *   On success, returns zero.
169  *   %-EINVAL - the device does not support FRWR memory registration
170  *   %-ENOMEM - the device is not sufficiently capable for NFS/RDMA
171  */
172 int frwr_query_device(struct rpcrdma_ep *ep, const struct ib_device *device)
173 {
174 	const struct ib_device_attr *attrs = &device->attrs;
175 	unsigned int max_sge;
176 	u32 max_qp_wr;
177 	int depth, delta;
178 
179 	if (!(attrs->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) ||
180 	    attrs->max_fast_reg_page_list_len == 0) {
181 		pr_err("rpcrdma: 'frwr' mode is not supported by device %s\n",
182 		       device->name);
183 		return -EINVAL;
184 	}
185 
186 	max_sge = min_t(unsigned int, attrs->max_send_sge,
187 			RPCRDMA_MAX_SEND_SGES);
188 	if (max_sge < RPCRDMA_MIN_SEND_SGES) {
189 		pr_err("rpcrdma: HCA provides only %u send SGEs\n", max_sge);
190 		return -ENOMEM;
191 	}
192 	ep->re_attr.cap.max_send_sge = max_sge;
193 	ep->re_attr.cap.max_recv_sge = 1;
194 
195 	ep->re_mrtype = IB_MR_TYPE_MEM_REG;
196 	if (attrs->kernel_cap_flags & IBK_SG_GAPS_REG)
197 		ep->re_mrtype = IB_MR_TYPE_SG_GAPS;
198 
199 	/* Quirk: Some devices advertise a large max_fast_reg_page_list_len
200 	 * capability, but perform optimally when the MRs are not larger
201 	 * than a page.
202 	 */
203 	if (attrs->max_sge_rd > RPCRDMA_MAX_HDR_SEGS)
204 		ep->re_max_fr_depth = attrs->max_sge_rd;
205 	else
206 		ep->re_max_fr_depth = attrs->max_fast_reg_page_list_len;
207 	if (ep->re_max_fr_depth > RPCRDMA_MAX_DATA_SEGS)
208 		ep->re_max_fr_depth = RPCRDMA_MAX_DATA_SEGS;
209 
210 	/* Add room for frwr register and invalidate WRs.
211 	 * 1. FRWR reg WR for head
212 	 * 2. FRWR invalidate WR for head
213 	 * 3. N FRWR reg WRs for pagelist
214 	 * 4. N FRWR invalidate WRs for pagelist
215 	 * 5. FRWR reg WR for tail
216 	 * 6. FRWR invalidate WR for tail
217 	 * 7. The RDMA_SEND WR
218 	 */
219 	depth = 7;
220 
221 	/* Calculate N if the device max FRWR depth is smaller than
222 	 * RPCRDMA_MAX_DATA_SEGS.
223 	 */
224 	if (ep->re_max_fr_depth < RPCRDMA_MAX_DATA_SEGS) {
225 		delta = RPCRDMA_MAX_DATA_SEGS - ep->re_max_fr_depth;
226 		do {
227 			depth += 2; /* FRWR reg + invalidate */
228 			delta -= ep->re_max_fr_depth;
229 		} while (delta > 0);
230 	}
231 
232 	max_qp_wr = attrs->max_qp_wr;
233 	if (max_qp_wr < RPCRDMA_BACKWARD_WRS + 1 + RPCRDMA_MIN_SLOT_TABLE)
234 		return -ENOMEM;
235 	max_qp_wr -= RPCRDMA_BACKWARD_WRS;
236 	max_qp_wr -= 1;
237 	if (ep->re_max_requests > max_qp_wr)
238 		ep->re_max_requests = max_qp_wr;
239 	ep->re_attr.cap.max_send_wr = ep->re_max_requests * depth;
240 	if (ep->re_attr.cap.max_send_wr > max_qp_wr) {
241 		ep->re_max_requests = max_qp_wr / depth;
242 		if (!ep->re_max_requests)
243 			return -ENOMEM;
244 		ep->re_attr.cap.max_send_wr = ep->re_max_requests * depth;
245 	}
246 	ep->re_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS;
247 	ep->re_attr.cap.max_send_wr += 1; /* for ib_drain_sq */
248 	ep->re_recv_batch = ep->re_max_requests >> 2;
249 	ep->re_attr.cap.max_recv_wr = ep->re_max_requests;
250 	ep->re_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS;
251 	ep->re_attr.cap.max_recv_wr += ep->re_recv_batch;
252 	ep->re_attr.cap.max_recv_wr += 1; /* for ib_drain_rq */
253 
254 	ep->re_max_rdma_segs =
255 		DIV_ROUND_UP(RPCRDMA_MAX_DATA_SEGS, ep->re_max_fr_depth);
256 	/* Reply chunks require segments for head and tail buffers */
257 	ep->re_max_rdma_segs += 2;
258 	if (ep->re_max_rdma_segs > RPCRDMA_MAX_HDR_SEGS)
259 		ep->re_max_rdma_segs = RPCRDMA_MAX_HDR_SEGS;
260 
261 	/* Ensure the underlying device is capable of conveying the
262 	 * largest r/wsize NFS will ask for. This guarantees that
263 	 * failing over from one RDMA device to another will not
264 	 * break NFS I/O.
265 	 */
266 	if ((ep->re_max_rdma_segs * ep->re_max_fr_depth) < RPCRDMA_MAX_SEGS)
267 		return -ENOMEM;
268 
269 	return 0;
270 }
271 
272 /**
273  * frwr_map - Register a memory region from an xdr_buf cursor
274  * @r_xprt: controlling transport
275  * @cur: cursor tracking position within the xdr_buf
276  * @writing: true when RDMA Write will be used
277  * @xid: XID of RPC using the registered memory
278  * @mr: MR to fill in
279  *
280  * Prepare a REG_MR Work Request to register a memory region
281  * for remote access via RDMA READ or RDMA WRITE.
282  *
283  * Returns 0 on success (cursor advanced past consumed data,
284  * @mr populated) or a negative errno on failure.
285  */
286 int frwr_map(struct rpcrdma_xprt *r_xprt,
287 	     struct rpcrdma_xdr_cursor *cur,
288 	     bool writing, __be32 xid,
289 	     struct rpcrdma_mr *mr)
290 {
291 	struct rpcrdma_ep *ep = r_xprt->rx_ep;
292 	const struct xdr_buf *xdrbuf = cur->xc_buf;
293 	bool sg_gaps = ep->re_mrtype == IB_MR_TYPE_SG_GAPS;
294 	unsigned int max_depth = ep->re_max_fr_depth;
295 	struct ib_reg_wr *reg_wr;
296 	int i, n, dma_nents;
297 	struct ib_mr *ibmr;
298 	u8 key;
299 
300 	i = 0;
301 
302 	/* Head kvec */
303 	if (!(cur->xc_flags & XC_HEAD_DONE)) {
304 		const struct kvec *head = &xdrbuf->head[0];
305 
306 		sg_set_page(&mr->mr_sg[i],
307 			    virt_to_page(head->iov_base),
308 			    head->iov_len,
309 			    offset_in_page(head->iov_base));
310 		cur->xc_flags |= XC_HEAD_DONE;
311 		i++;
312 		/* Without sg-gap support, each non-contiguous region
313 		 * must be registered as a separate MR.  Returning
314 		 * here after the head kvec causes the caller to
315 		 * invoke frwr_map() again for the page list and
316 		 * tail.
317 		 */
318 		if (!sg_gaps)
319 			goto finish;
320 	}
321 
322 	/* Page list */
323 	if (!(cur->xc_flags & XC_PAGES_DONE) && xdrbuf->page_len) {
324 		unsigned int page_base, remaining;
325 		struct page **ppages;
326 
327 		remaining = xdrbuf->page_len - cur->xc_page_offset;
328 		page_base = offset_in_page(xdrbuf->page_base +
329 					   cur->xc_page_offset);
330 		ppages = xdrbuf->pages +
331 			 ((xdrbuf->page_base + cur->xc_page_offset)
332 			  >> PAGE_SHIFT);
333 
334 		while (remaining > 0 && i < max_depth) {
335 			unsigned int len;
336 
337 			len = min_t(unsigned int,
338 				    PAGE_SIZE - page_base, remaining);
339 			sg_set_page(&mr->mr_sg[i], *ppages,
340 				    len, page_base);
341 			cur->xc_page_offset += len;
342 			i++;
343 			ppages++;
344 			remaining -= len;
345 
346 			if (!sg_gaps && remaining > 0 &&
347 			    offset_in_page(page_base + len))
348 				goto finish;
349 			page_base = 0;
350 		}
351 		if (remaining == 0)
352 			cur->xc_flags |= XC_PAGES_DONE;
353 	} else if (!(cur->xc_flags & XC_PAGES_DONE)) {
354 		cur->xc_flags |= XC_PAGES_DONE;
355 	}
356 
357 	/* Tail kvec */
358 	if (!(cur->xc_flags & XC_TAIL_DONE) && xdrbuf->tail[0].iov_len &&
359 	    i < max_depth) {
360 		const struct kvec *tail = &xdrbuf->tail[0];
361 
362 		if (!sg_gaps && i > 0) {
363 			struct scatterlist *prev = &mr->mr_sg[i - 1];
364 
365 			if (offset_in_page(prev->offset + prev->length) ||
366 			    offset_in_page(tail->iov_base))
367 				goto finish;
368 		}
369 		sg_set_page(&mr->mr_sg[i],
370 			    virt_to_page(tail->iov_base),
371 			    tail->iov_len,
372 			    offset_in_page(tail->iov_base));
373 		cur->xc_flags |= XC_TAIL_DONE;
374 		i++;
375 	} else if (!(cur->xc_flags & XC_TAIL_DONE) &&
376 		   !xdrbuf->tail[0].iov_len) {
377 		cur->xc_flags |= XC_TAIL_DONE;
378 	}
379 
380 finish:
381 	mr->mr_dir = rpcrdma_data_dir(writing);
382 	mr->mr_nents = i;
383 
384 	dma_nents = ib_dma_map_sg(ep->re_id->device, mr->mr_sg, mr->mr_nents,
385 				  mr->mr_dir);
386 	if (!dma_nents)
387 		goto out_dmamap_err;
388 	mr->mr_device = ep->re_id->device;
389 
390 	ibmr = mr->mr_ibmr;
391 	n = ib_map_mr_sg(ibmr, mr->mr_sg, dma_nents, NULL, PAGE_SIZE);
392 	if (n != dma_nents)
393 		goto out_mapmr_err;
394 
395 	ibmr->iova &= 0x00000000ffffffff;
396 	ibmr->iova |= ((u64)be32_to_cpu(xid)) << 32;
397 	key = (u8)(ibmr->rkey & 0x000000FF);
398 	ib_update_fast_reg_key(ibmr, ++key);
399 
400 	reg_wr = &mr->mr_regwr;
401 	reg_wr->mr = ibmr;
402 	reg_wr->key = ibmr->rkey;
403 	reg_wr->access = writing ?
404 			 IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
405 			 IB_ACCESS_REMOTE_READ;
406 
407 	mr->mr_handle = ibmr->rkey;
408 	mr->mr_length = ibmr->length;
409 	mr->mr_offset = ibmr->iova;
410 	trace_xprtrdma_mr_map(mr);
411 
412 	return 0;
413 
414 out_dmamap_err:
415 	trace_xprtrdma_frwr_sgerr(mr, i);
416 	return -EIO;
417 
418 out_mapmr_err:
419 	trace_xprtrdma_frwr_maperr(mr, n);
420 	return -EIO;
421 }
422 
423 /**
424  * frwr_wc_fastreg - Invoked by RDMA provider for a flushed FastReg WC
425  * @cq: completion queue
426  * @wc: WCE for a completed FastReg WR
427  *
428  * Each flushed MR gets destroyed after the QP has drained.
429  */
430 static void frwr_wc_fastreg(struct ib_cq *cq, struct ib_wc *wc)
431 {
432 	struct ib_cqe *cqe = wc->wr_cqe;
433 	struct rpcrdma_mr *mr = container_of(cqe, struct rpcrdma_mr, mr_cqe);
434 
435 	/* WARNING: Only wr_cqe and status are reliable at this point */
436 	trace_xprtrdma_wc_fastreg(wc, &mr->mr_cid);
437 
438 	rpcrdma_flush_disconnect(cq->cq_context, wc);
439 }
440 
441 /**
442  * frwr_send - post Send WRs containing the RPC Call message
443  * @r_xprt: controlling transport instance
444  * @req: prepared RPC Call
445  *
446  * For FRWR, chain any FastReg WRs to the Send WR. Only a
447  * single ib_post_send call is needed to register memory
448  * and then post the Send WR.
449  *
450  * Returns the return code from ib_post_send.
451  *
452  * Caller must hold the transport send lock to ensure that the
453  * pointers to the transport's rdma_cm_id and QP are stable.
454  */
455 int frwr_send(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req)
456 {
457 	struct ib_send_wr *post_wr, *send_wr = &req->rl_wr;
458 	struct rpcrdma_ep *ep = r_xprt->rx_ep;
459 	struct rpcrdma_mr *mr;
460 	unsigned int num_wrs;
461 	int ret;
462 
463 	num_wrs = 1;
464 	post_wr = send_wr;
465 	list_for_each_entry(mr, &req->rl_registered, mr_list) {
466 		trace_xprtrdma_mr_fastreg(mr);
467 
468 		mr->mr_cqe.done = frwr_wc_fastreg;
469 		mr->mr_regwr.wr.next = post_wr;
470 		mr->mr_regwr.wr.wr_cqe = &mr->mr_cqe;
471 		mr->mr_regwr.wr.num_sge = 0;
472 		mr->mr_regwr.wr.opcode = IB_WR_REG_MR;
473 		mr->mr_regwr.wr.send_flags = 0;
474 		post_wr = &mr->mr_regwr.wr;
475 		++num_wrs;
476 	}
477 
478 	if (req->rl_sendctx->sc_unmap_count || num_wrs > ep->re_send_count) {
479 		send_wr->send_flags |= IB_SEND_SIGNALED;
480 		ep->re_send_count = min_t(unsigned int, ep->re_send_batch,
481 					  num_wrs - ep->re_send_count);
482 	} else {
483 		send_wr->send_flags &= ~IB_SEND_SIGNALED;
484 		ep->re_send_count -= num_wrs;
485 	}
486 
487 	trace_xprtrdma_post_send(req);
488 	ret = ib_post_send(ep->re_id->qp, post_wr, NULL);
489 	if (ret)
490 		trace_xprtrdma_post_send_err(r_xprt, req, ret);
491 	return ret;
492 }
493 
494 /**
495  * frwr_reminv - handle a remotely invalidated mr on the @mrs list
496  * @rep: Received reply
497  * @mrs: list of MRs to check
498  *
499  */
500 void frwr_reminv(struct rpcrdma_rep *rep, struct list_head *mrs)
501 {
502 	struct rpcrdma_mr *mr;
503 
504 	list_for_each_entry(mr, mrs, mr_list)
505 		if (mr->mr_handle == rep->rr_inv_rkey) {
506 			list_del_init(&mr->mr_list);
507 			trace_xprtrdma_mr_reminv(mr);
508 			frwr_mr_put(mr);
509 			break;	/* only one invalidated MR per RPC */
510 		}
511 }
512 
513 static void frwr_mr_done(struct ib_wc *wc, struct rpcrdma_mr *mr)
514 {
515 	if (likely(wc->status == IB_WC_SUCCESS))
516 		frwr_mr_put(mr);
517 }
518 
519 /**
520  * frwr_wc_localinv - Invoked by RDMA provider for a LOCAL_INV WC
521  * @cq: completion queue
522  * @wc: WCE for a completed LocalInv WR
523  *
524  */
525 static void frwr_wc_localinv(struct ib_cq *cq, struct ib_wc *wc)
526 {
527 	struct ib_cqe *cqe = wc->wr_cqe;
528 	struct rpcrdma_mr *mr = container_of(cqe, struct rpcrdma_mr, mr_cqe);
529 
530 	/* WARNING: Only wr_cqe and status are reliable at this point */
531 	trace_xprtrdma_wc_li(wc, &mr->mr_cid);
532 	frwr_mr_done(wc, mr);
533 
534 	rpcrdma_flush_disconnect(cq->cq_context, wc);
535 }
536 
537 /**
538  * frwr_wc_localinv_wake - Invoked by RDMA provider for a LOCAL_INV WC
539  * @cq: completion queue
540  * @wc: WCE for a completed LocalInv WR
541  *
542  * Awaken anyone waiting for an MR to finish being fenced.
543  */
544 static void frwr_wc_localinv_wake(struct ib_cq *cq, struct ib_wc *wc)
545 {
546 	struct ib_cqe *cqe = wc->wr_cqe;
547 	struct rpcrdma_mr *mr = container_of(cqe, struct rpcrdma_mr, mr_cqe);
548 
549 	/* WARNING: Only wr_cqe and status are reliable at this point */
550 	trace_xprtrdma_wc_li_wake(wc, &mr->mr_cid);
551 	frwr_mr_done(wc, mr);
552 	complete(&mr->mr_linv_done);
553 
554 	rpcrdma_flush_disconnect(cq->cq_context, wc);
555 }
556 
557 /**
558  * frwr_unmap_sync - invalidate memory regions that were registered for @req
559  * @r_xprt: controlling transport instance
560  * @req: rpcrdma_req with a non-empty list of MRs to process
561  *
562  * Sleeps until it is safe for the host CPU to access the previously mapped
563  * memory regions. This guarantees that registered MRs are properly fenced
564  * from the server before the RPC consumer accesses the data in them. It
565  * also ensures proper Send flow control: waking the next RPC waits until
566  * this RPC has relinquished all its Send Queue entries.
567  */
568 void frwr_unmap_sync(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req)
569 {
570 	struct ib_send_wr *first, **prev, *last;
571 	struct rpcrdma_ep *ep = r_xprt->rx_ep;
572 	const struct ib_send_wr *bad_wr;
573 	struct rpcrdma_mr *mr;
574 	int rc;
575 
576 	/* ORDER: Invalidate all of the MRs first
577 	 *
578 	 * Chain the LOCAL_INV Work Requests and post them with
579 	 * a single ib_post_send() call.
580 	 */
581 	prev = &first;
582 	mr = rpcrdma_mr_pop(&req->rl_registered);
583 	do {
584 		trace_xprtrdma_mr_localinv(mr);
585 		r_xprt->rx_stats.local_inv_needed++;
586 
587 		last = &mr->mr_invwr;
588 		last->next = NULL;
589 		last->wr_cqe = &mr->mr_cqe;
590 		last->sg_list = NULL;
591 		last->num_sge = 0;
592 		last->opcode = IB_WR_LOCAL_INV;
593 		last->send_flags = IB_SEND_SIGNALED;
594 		last->ex.invalidate_rkey = mr->mr_handle;
595 
596 		last->wr_cqe->done = frwr_wc_localinv;
597 
598 		*prev = last;
599 		prev = &last->next;
600 	} while ((mr = rpcrdma_mr_pop(&req->rl_registered)));
601 
602 	mr = container_of(last, struct rpcrdma_mr, mr_invwr);
603 
604 	/* Strong send queue ordering guarantees that when the
605 	 * last WR in the chain completes, all WRs in the chain
606 	 * are complete.
607 	 */
608 	last->wr_cqe->done = frwr_wc_localinv_wake;
609 	reinit_completion(&mr->mr_linv_done);
610 
611 	/* Transport disconnect drains the receive CQ before it
612 	 * replaces the QP. The RPC reply handler won't call us
613 	 * unless re_id->qp is a valid pointer.
614 	 */
615 	bad_wr = NULL;
616 	rc = ib_post_send(ep->re_id->qp, first, &bad_wr);
617 
618 	/* The final LOCAL_INV WR in the chain is supposed to
619 	 * do the wake. If it was never posted, the wake will
620 	 * not happen, so don't wait in that case.
621 	 */
622 	if (bad_wr != first)
623 		wait_for_completion(&mr->mr_linv_done);
624 	if (!rc)
625 		return;
626 
627 	/* On error, the MRs get destroyed once the QP has drained. */
628 	trace_xprtrdma_post_linv_err(req, rc);
629 
630 	/* Force a connection loss to ensure complete recovery.
631 	 */
632 	rpcrdma_force_disconnect(ep);
633 }
634 
635 /**
636  * frwr_wc_localinv_done - Invoked by RDMA provider for a signaled LOCAL_INV WC
637  * @cq:	completion queue
638  * @wc:	WCE for a completed LocalInv WR
639  *
640  */
641 static void frwr_wc_localinv_done(struct ib_cq *cq, struct ib_wc *wc)
642 {
643 	struct ib_cqe *cqe = wc->wr_cqe;
644 	struct rpcrdma_mr *mr = container_of(cqe, struct rpcrdma_mr, mr_cqe);
645 	struct rpcrdma_rep *rep;
646 
647 	/* WARNING: Only wr_cqe and status are reliable at this point */
648 	trace_xprtrdma_wc_li_done(wc, &mr->mr_cid);
649 
650 	/* Ensure that @rep is generated before the MR is released */
651 	rep = mr->mr_req->rl_reply;
652 	smp_rmb();
653 
654 	if (wc->status != IB_WC_SUCCESS) {
655 		if (rep)
656 			rpcrdma_unpin_rqst(rep);
657 		rpcrdma_flush_disconnect(cq->cq_context, wc);
658 		return;
659 	}
660 	frwr_mr_put(mr);
661 	rpcrdma_complete_rqst(rep);
662 }
663 
664 /**
665  * frwr_unmap_async - invalidate memory regions that were registered for @req
666  * @r_xprt: controlling transport instance
667  * @req: rpcrdma_req with a non-empty list of MRs to process
668  *
669  * This guarantees that registered MRs are properly fenced from the
670  * server before the RPC consumer accesses the data in them. It also
671  * ensures proper Send flow control: waking the next RPC waits until
672  * this RPC has relinquished all its Send Queue entries.
673  */
674 void frwr_unmap_async(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req)
675 {
676 	struct ib_send_wr *first, *last, **prev;
677 	struct rpcrdma_ep *ep = r_xprt->rx_ep;
678 	struct rpcrdma_mr *mr;
679 	int rc;
680 
681 	/* Chain the LOCAL_INV Work Requests and post them with
682 	 * a single ib_post_send() call.
683 	 */
684 	prev = &first;
685 	mr = rpcrdma_mr_pop(&req->rl_registered);
686 	do {
687 		trace_xprtrdma_mr_localinv(mr);
688 		r_xprt->rx_stats.local_inv_needed++;
689 
690 		last = &mr->mr_invwr;
691 		last->next = NULL;
692 		last->wr_cqe = &mr->mr_cqe;
693 		last->sg_list = NULL;
694 		last->num_sge = 0;
695 		last->opcode = IB_WR_LOCAL_INV;
696 		last->send_flags = IB_SEND_SIGNALED;
697 		last->ex.invalidate_rkey = mr->mr_handle;
698 
699 		last->wr_cqe->done = frwr_wc_localinv;
700 
701 		*prev = last;
702 		prev = &last->next;
703 	} while ((mr = rpcrdma_mr_pop(&req->rl_registered)));
704 
705 	/* Strong send queue ordering guarantees that when the
706 	 * last WR in the chain completes, all WRs in the chain
707 	 * are complete. The last completion will wake up the
708 	 * RPC waiter.
709 	 */
710 	last->wr_cqe->done = frwr_wc_localinv_done;
711 
712 	/* Transport disconnect drains the receive CQ before it
713 	 * replaces the QP. The RPC reply handler won't call us
714 	 * unless re_id->qp is a valid pointer.
715 	 */
716 	rc = ib_post_send(ep->re_id->qp, first, NULL);
717 	if (!rc)
718 		return;
719 
720 	/* On error, the MRs get destroyed once the QP has drained. */
721 	trace_xprtrdma_post_linv_err(req, rc);
722 
723 	/* The final LOCAL_INV WR in the chain is supposed to
724 	 * do the wake. If it was never posted, the wake does
725 	 * not happen. Unpin the rqst in preparation for its
726 	 * retransmission.
727 	 */
728 	rpcrdma_unpin_rqst(req->rl_reply);
729 
730 	/* Force a connection loss to ensure complete recovery.
731 	 */
732 	rpcrdma_force_disconnect(ep);
733 }
734 
735 /**
736  * frwr_wp_create - Create an MR for padding Write chunks
737  * @r_xprt: transport resources to use
738  *
739  * Return 0 on success, negative errno on failure.
740  */
741 int frwr_wp_create(struct rpcrdma_xprt *r_xprt)
742 {
743 	struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
744 	struct rpcrdma_ep *ep = r_xprt->rx_ep;
745 	struct ib_reg_wr *reg_wr;
746 	struct rpcrdma_mr *mr;
747 	struct ib_mr *ibmr;
748 	int dma_nents;
749 	int ret;
750 
751 	mr = rpcrdma_mr_get(r_xprt);
752 	if (!mr)
753 		return -EAGAIN;
754 	mr->mr_req = NULL;
755 	ep->re_write_pad_mr = mr;
756 
757 	sg_init_table(mr->mr_sg, 1);
758 	sg_set_page(mr->mr_sg, virt_to_page(ep->re_write_pad),
759 		    XDR_UNIT, offset_in_page(ep->re_write_pad));
760 
761 	mr->mr_dir = DMA_FROM_DEVICE;
762 	mr->mr_nents = 1;
763 	dma_nents = ib_dma_map_sg(ep->re_id->device, mr->mr_sg,
764 				  mr->mr_nents, mr->mr_dir);
765 	if (!dma_nents) {
766 		ret = -EIO;
767 		goto out_mr;
768 	}
769 	mr->mr_device = ep->re_id->device;
770 
771 	ibmr = mr->mr_ibmr;
772 	if (ib_map_mr_sg(ibmr, mr->mr_sg, dma_nents, NULL,
773 			 PAGE_SIZE) != dma_nents) {
774 		ret = -EIO;
775 		goto out_unmap;
776 	}
777 
778 	/* IOVA is not tagged with an XID; the write-pad is not RPC-specific. */
779 	ib_update_fast_reg_key(ibmr, ib_inc_rkey(ibmr->rkey));
780 
781 	reg_wr = &mr->mr_regwr;
782 	reg_wr->mr = ibmr;
783 	reg_wr->key = ibmr->rkey;
784 	reg_wr->access = IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE;
785 
786 	mr->mr_handle = ibmr->rkey;
787 	mr->mr_length = ibmr->length;
788 	mr->mr_offset = ibmr->iova;
789 
790 	trace_xprtrdma_mr_fastreg(mr);
791 
792 	mr->mr_cqe.done = frwr_wc_fastreg;
793 	mr->mr_regwr.wr.next = NULL;
794 	mr->mr_regwr.wr.wr_cqe = &mr->mr_cqe;
795 	mr->mr_regwr.wr.num_sge = 0;
796 	mr->mr_regwr.wr.opcode = IB_WR_REG_MR;
797 	mr->mr_regwr.wr.send_flags = 0;
798 
799 	ret = ib_post_send(ep->re_id->qp, &mr->mr_regwr.wr, NULL);
800 	if (!ret)
801 		return 0;
802 
803 out_unmap:
804 	frwr_mr_unmap(mr);
805 out_mr:
806 	ep->re_write_pad_mr = NULL;
807 	spin_lock(&buf->rb_lock);
808 	rpcrdma_mr_push(mr, &buf->rb_mrs);
809 	spin_unlock(&buf->rb_lock);
810 	return ret;
811 }
812