1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) 2014-2017 Oracle. All rights reserved.
4*4882a593Smuzhiyun * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * This software is available to you under a choice of one of two
7*4882a593Smuzhiyun * licenses. You may choose to be licensed under the terms of the GNU
8*4882a593Smuzhiyun * General Public License (GPL) Version 2, available from the file
9*4882a593Smuzhiyun * COPYING in the main directory of this source tree, or the BSD-type
10*4882a593Smuzhiyun * license below:
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or without
13*4882a593Smuzhiyun * modification, are permitted provided that the following conditions
14*4882a593Smuzhiyun * are met:
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * Redistributions of source code must retain the above copyright
17*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer.
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * Redistributions in binary form must reproduce the above
20*4882a593Smuzhiyun * copyright notice, this list of conditions and the following
21*4882a593Smuzhiyun * disclaimer in the documentation and/or other materials provided
22*4882a593Smuzhiyun * with the distribution.
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * Neither the name of the Network Appliance, Inc. nor the names of
25*4882a593Smuzhiyun * its contributors may be used to endorse or promote products
26*4882a593Smuzhiyun * derived from this software without specific prior written
27*4882a593Smuzhiyun * permission.
28*4882a593Smuzhiyun *
29*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30*4882a593Smuzhiyun * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31*4882a593Smuzhiyun * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32*4882a593Smuzhiyun * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33*4882a593Smuzhiyun * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34*4882a593Smuzhiyun * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35*4882a593Smuzhiyun * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36*4882a593Smuzhiyun * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37*4882a593Smuzhiyun * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38*4882a593Smuzhiyun * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39*4882a593Smuzhiyun * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40*4882a593Smuzhiyun */
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /*
43*4882a593Smuzhiyun * rpc_rdma.c
44*4882a593Smuzhiyun *
45*4882a593Smuzhiyun * This file contains the guts of the RPC RDMA protocol, and
46*4882a593Smuzhiyun * does marshaling/unmarshaling, etc. It is also where interfacing
47*4882a593Smuzhiyun * to the Linux RPC framework lives.
48*4882a593Smuzhiyun */
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun #include <linux/highmem.h>
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun #include <linux/sunrpc/svc_rdma.h>
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun #include "xprt_rdma.h"
55*4882a593Smuzhiyun #include <trace/events/rpcrdma.h>
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
58*4882a593Smuzhiyun # define RPCDBG_FACILITY RPCDBG_TRANS
59*4882a593Smuzhiyun #endif
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun /* Returns size of largest RPC-over-RDMA header in a Call message
62*4882a593Smuzhiyun *
63*4882a593Smuzhiyun * The largest Call header contains a full-size Read list and a
64*4882a593Smuzhiyun * minimal Reply chunk.
65*4882a593Smuzhiyun */
rpcrdma_max_call_header_size(unsigned int maxsegs)66*4882a593Smuzhiyun static unsigned int rpcrdma_max_call_header_size(unsigned int maxsegs)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun unsigned int size;
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun /* Fixed header fields and list discriminators */
71*4882a593Smuzhiyun size = RPCRDMA_HDRLEN_MIN;
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun /* Maximum Read list size */
74*4882a593Smuzhiyun size += maxsegs * rpcrdma_readchunk_maxsz * sizeof(__be32);
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun /* Minimal Read chunk size */
77*4882a593Smuzhiyun size += sizeof(__be32); /* segment count */
78*4882a593Smuzhiyun size += rpcrdma_segment_maxsz * sizeof(__be32);
79*4882a593Smuzhiyun size += sizeof(__be32); /* list discriminator */
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun return size;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun /* Returns size of largest RPC-over-RDMA header in a Reply message
85*4882a593Smuzhiyun *
86*4882a593Smuzhiyun * There is only one Write list or one Reply chunk per Reply
87*4882a593Smuzhiyun * message. The larger list is the Write list.
88*4882a593Smuzhiyun */
rpcrdma_max_reply_header_size(unsigned int maxsegs)89*4882a593Smuzhiyun static unsigned int rpcrdma_max_reply_header_size(unsigned int maxsegs)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun unsigned int size;
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /* Fixed header fields and list discriminators */
94*4882a593Smuzhiyun size = RPCRDMA_HDRLEN_MIN;
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun /* Maximum Write list size */
97*4882a593Smuzhiyun size += sizeof(__be32); /* segment count */
98*4882a593Smuzhiyun size += maxsegs * rpcrdma_segment_maxsz * sizeof(__be32);
99*4882a593Smuzhiyun size += sizeof(__be32); /* list discriminator */
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun return size;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /**
105*4882a593Smuzhiyun * rpcrdma_set_max_header_sizes - Initialize inline payload sizes
106*4882a593Smuzhiyun * @ep: endpoint to initialize
107*4882a593Smuzhiyun *
108*4882a593Smuzhiyun * The max_inline fields contain the maximum size of an RPC message
109*4882a593Smuzhiyun * so the marshaling code doesn't have to repeat this calculation
110*4882a593Smuzhiyun * for every RPC.
111*4882a593Smuzhiyun */
rpcrdma_set_max_header_sizes(struct rpcrdma_ep * ep)112*4882a593Smuzhiyun void rpcrdma_set_max_header_sizes(struct rpcrdma_ep *ep)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun unsigned int maxsegs = ep->re_max_rdma_segs;
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun ep->re_max_inline_send =
117*4882a593Smuzhiyun ep->re_inline_send - rpcrdma_max_call_header_size(maxsegs);
118*4882a593Smuzhiyun ep->re_max_inline_recv =
119*4882a593Smuzhiyun ep->re_inline_recv - rpcrdma_max_reply_header_size(maxsegs);
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun /* The client can send a request inline as long as the RPCRDMA header
123*4882a593Smuzhiyun * plus the RPC call fit under the transport's inline limit. If the
124*4882a593Smuzhiyun * combined call message size exceeds that limit, the client must use
125*4882a593Smuzhiyun * a Read chunk for this operation.
126*4882a593Smuzhiyun *
127*4882a593Smuzhiyun * A Read chunk is also required if sending the RPC call inline would
128*4882a593Smuzhiyun * exceed this device's max_sge limit.
129*4882a593Smuzhiyun */
rpcrdma_args_inline(struct rpcrdma_xprt * r_xprt,struct rpc_rqst * rqst)130*4882a593Smuzhiyun static bool rpcrdma_args_inline(struct rpcrdma_xprt *r_xprt,
131*4882a593Smuzhiyun struct rpc_rqst *rqst)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun struct xdr_buf *xdr = &rqst->rq_snd_buf;
134*4882a593Smuzhiyun struct rpcrdma_ep *ep = r_xprt->rx_ep;
135*4882a593Smuzhiyun unsigned int count, remaining, offset;
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun if (xdr->len > ep->re_max_inline_send)
138*4882a593Smuzhiyun return false;
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun if (xdr->page_len) {
141*4882a593Smuzhiyun remaining = xdr->page_len;
142*4882a593Smuzhiyun offset = offset_in_page(xdr->page_base);
143*4882a593Smuzhiyun count = RPCRDMA_MIN_SEND_SGES;
144*4882a593Smuzhiyun while (remaining) {
145*4882a593Smuzhiyun remaining -= min_t(unsigned int,
146*4882a593Smuzhiyun PAGE_SIZE - offset, remaining);
147*4882a593Smuzhiyun offset = 0;
148*4882a593Smuzhiyun if (++count > ep->re_attr.cap.max_send_sge)
149*4882a593Smuzhiyun return false;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun return true;
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun /* The client can't know how large the actual reply will be. Thus it
157*4882a593Smuzhiyun * plans for the largest possible reply for that particular ULP
158*4882a593Smuzhiyun * operation. If the maximum combined reply message size exceeds that
159*4882a593Smuzhiyun * limit, the client must provide a write list or a reply chunk for
160*4882a593Smuzhiyun * this request.
161*4882a593Smuzhiyun */
rpcrdma_results_inline(struct rpcrdma_xprt * r_xprt,struct rpc_rqst * rqst)162*4882a593Smuzhiyun static bool rpcrdma_results_inline(struct rpcrdma_xprt *r_xprt,
163*4882a593Smuzhiyun struct rpc_rqst *rqst)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun return rqst->rq_rcv_buf.buflen <= r_xprt->rx_ep->re_max_inline_recv;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun /* The client is required to provide a Reply chunk if the maximum
169*4882a593Smuzhiyun * size of the non-payload part of the RPC Reply is larger than
170*4882a593Smuzhiyun * the inline threshold.
171*4882a593Smuzhiyun */
172*4882a593Smuzhiyun static bool
rpcrdma_nonpayload_inline(const struct rpcrdma_xprt * r_xprt,const struct rpc_rqst * rqst)173*4882a593Smuzhiyun rpcrdma_nonpayload_inline(const struct rpcrdma_xprt *r_xprt,
174*4882a593Smuzhiyun const struct rpc_rqst *rqst)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun const struct xdr_buf *buf = &rqst->rq_rcv_buf;
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun return (buf->head[0].iov_len + buf->tail[0].iov_len) <
179*4882a593Smuzhiyun r_xprt->rx_ep->re_max_inline_recv;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun /* ACL likes to be lazy in allocating pages. For TCP, these
183*4882a593Smuzhiyun * pages can be allocated during receive processing. Not true
184*4882a593Smuzhiyun * for RDMA, which must always provision receive buffers
185*4882a593Smuzhiyun * up front.
186*4882a593Smuzhiyun */
187*4882a593Smuzhiyun static noinline int
rpcrdma_alloc_sparse_pages(struct xdr_buf * buf)188*4882a593Smuzhiyun rpcrdma_alloc_sparse_pages(struct xdr_buf *buf)
189*4882a593Smuzhiyun {
190*4882a593Smuzhiyun struct page **ppages;
191*4882a593Smuzhiyun int len;
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun len = buf->page_len;
194*4882a593Smuzhiyun ppages = buf->pages + (buf->page_base >> PAGE_SHIFT);
195*4882a593Smuzhiyun while (len > 0) {
196*4882a593Smuzhiyun if (!*ppages)
197*4882a593Smuzhiyun *ppages = alloc_page(GFP_NOWAIT | __GFP_NOWARN);
198*4882a593Smuzhiyun if (!*ppages)
199*4882a593Smuzhiyun return -ENOBUFS;
200*4882a593Smuzhiyun ppages++;
201*4882a593Smuzhiyun len -= PAGE_SIZE;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun return 0;
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun /* Split @vec on page boundaries into SGEs. FMR registers pages, not
208*4882a593Smuzhiyun * a byte range. Other modes coalesce these SGEs into a single MR
209*4882a593Smuzhiyun * when they can.
210*4882a593Smuzhiyun *
211*4882a593Smuzhiyun * Returns pointer to next available SGE, and bumps the total number
212*4882a593Smuzhiyun * of SGEs consumed.
213*4882a593Smuzhiyun */
214*4882a593Smuzhiyun static struct rpcrdma_mr_seg *
rpcrdma_convert_kvec(struct kvec * vec,struct rpcrdma_mr_seg * seg,unsigned int * n)215*4882a593Smuzhiyun rpcrdma_convert_kvec(struct kvec *vec, struct rpcrdma_mr_seg *seg,
216*4882a593Smuzhiyun unsigned int *n)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun u32 remaining, page_offset;
219*4882a593Smuzhiyun char *base;
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun base = vec->iov_base;
222*4882a593Smuzhiyun page_offset = offset_in_page(base);
223*4882a593Smuzhiyun remaining = vec->iov_len;
224*4882a593Smuzhiyun while (remaining) {
225*4882a593Smuzhiyun seg->mr_page = NULL;
226*4882a593Smuzhiyun seg->mr_offset = base;
227*4882a593Smuzhiyun seg->mr_len = min_t(u32, PAGE_SIZE - page_offset, remaining);
228*4882a593Smuzhiyun remaining -= seg->mr_len;
229*4882a593Smuzhiyun base += seg->mr_len;
230*4882a593Smuzhiyun ++seg;
231*4882a593Smuzhiyun ++(*n);
232*4882a593Smuzhiyun page_offset = 0;
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun return seg;
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun /* Convert @xdrbuf into SGEs no larger than a page each. As they
238*4882a593Smuzhiyun * are registered, these SGEs are then coalesced into RDMA segments
239*4882a593Smuzhiyun * when the selected memreg mode supports it.
240*4882a593Smuzhiyun *
241*4882a593Smuzhiyun * Returns positive number of SGEs consumed, or a negative errno.
242*4882a593Smuzhiyun */
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun static int
rpcrdma_convert_iovs(struct rpcrdma_xprt * r_xprt,struct xdr_buf * xdrbuf,unsigned int pos,enum rpcrdma_chunktype type,struct rpcrdma_mr_seg * seg)245*4882a593Smuzhiyun rpcrdma_convert_iovs(struct rpcrdma_xprt *r_xprt, struct xdr_buf *xdrbuf,
246*4882a593Smuzhiyun unsigned int pos, enum rpcrdma_chunktype type,
247*4882a593Smuzhiyun struct rpcrdma_mr_seg *seg)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun unsigned long page_base;
250*4882a593Smuzhiyun unsigned int len, n;
251*4882a593Smuzhiyun struct page **ppages;
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun n = 0;
254*4882a593Smuzhiyun if (pos == 0)
255*4882a593Smuzhiyun seg = rpcrdma_convert_kvec(&xdrbuf->head[0], seg, &n);
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun len = xdrbuf->page_len;
258*4882a593Smuzhiyun ppages = xdrbuf->pages + (xdrbuf->page_base >> PAGE_SHIFT);
259*4882a593Smuzhiyun page_base = offset_in_page(xdrbuf->page_base);
260*4882a593Smuzhiyun while (len) {
261*4882a593Smuzhiyun seg->mr_page = *ppages;
262*4882a593Smuzhiyun seg->mr_offset = (char *)page_base;
263*4882a593Smuzhiyun seg->mr_len = min_t(u32, PAGE_SIZE - page_base, len);
264*4882a593Smuzhiyun len -= seg->mr_len;
265*4882a593Smuzhiyun ++ppages;
266*4882a593Smuzhiyun ++seg;
267*4882a593Smuzhiyun ++n;
268*4882a593Smuzhiyun page_base = 0;
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun /* When encoding a Read chunk, the tail iovec contains an
272*4882a593Smuzhiyun * XDR pad and may be omitted.
273*4882a593Smuzhiyun */
274*4882a593Smuzhiyun if (type == rpcrdma_readch && r_xprt->rx_ep->re_implicit_roundup)
275*4882a593Smuzhiyun goto out;
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun /* When encoding a Write chunk, some servers need to see an
278*4882a593Smuzhiyun * extra segment for non-XDR-aligned Write chunks. The upper
279*4882a593Smuzhiyun * layer provides space in the tail iovec that may be used
280*4882a593Smuzhiyun * for this purpose.
281*4882a593Smuzhiyun */
282*4882a593Smuzhiyun if (type == rpcrdma_writech && r_xprt->rx_ep->re_implicit_roundup)
283*4882a593Smuzhiyun goto out;
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun if (xdrbuf->tail[0].iov_len)
286*4882a593Smuzhiyun seg = rpcrdma_convert_kvec(&xdrbuf->tail[0], seg, &n);
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun out:
289*4882a593Smuzhiyun if (unlikely(n > RPCRDMA_MAX_SEGS))
290*4882a593Smuzhiyun return -EIO;
291*4882a593Smuzhiyun return n;
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun static int
encode_rdma_segment(struct xdr_stream * xdr,struct rpcrdma_mr * mr)295*4882a593Smuzhiyun encode_rdma_segment(struct xdr_stream *xdr, struct rpcrdma_mr *mr)
296*4882a593Smuzhiyun {
297*4882a593Smuzhiyun __be32 *p;
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun p = xdr_reserve_space(xdr, 4 * sizeof(*p));
300*4882a593Smuzhiyun if (unlikely(!p))
301*4882a593Smuzhiyun return -EMSGSIZE;
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun xdr_encode_rdma_segment(p, mr->mr_handle, mr->mr_length, mr->mr_offset);
304*4882a593Smuzhiyun return 0;
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun static int
encode_read_segment(struct xdr_stream * xdr,struct rpcrdma_mr * mr,u32 position)308*4882a593Smuzhiyun encode_read_segment(struct xdr_stream *xdr, struct rpcrdma_mr *mr,
309*4882a593Smuzhiyun u32 position)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun __be32 *p;
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun p = xdr_reserve_space(xdr, 6 * sizeof(*p));
314*4882a593Smuzhiyun if (unlikely(!p))
315*4882a593Smuzhiyun return -EMSGSIZE;
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun *p++ = xdr_one; /* Item present */
318*4882a593Smuzhiyun xdr_encode_read_segment(p, position, mr->mr_handle, mr->mr_length,
319*4882a593Smuzhiyun mr->mr_offset);
320*4882a593Smuzhiyun return 0;
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun
rpcrdma_mr_prepare(struct rpcrdma_xprt * r_xprt,struct rpcrdma_req * req,struct rpcrdma_mr_seg * seg,int nsegs,bool writing,struct rpcrdma_mr ** mr)323*4882a593Smuzhiyun static struct rpcrdma_mr_seg *rpcrdma_mr_prepare(struct rpcrdma_xprt *r_xprt,
324*4882a593Smuzhiyun struct rpcrdma_req *req,
325*4882a593Smuzhiyun struct rpcrdma_mr_seg *seg,
326*4882a593Smuzhiyun int nsegs, bool writing,
327*4882a593Smuzhiyun struct rpcrdma_mr **mr)
328*4882a593Smuzhiyun {
329*4882a593Smuzhiyun *mr = rpcrdma_mr_pop(&req->rl_free_mrs);
330*4882a593Smuzhiyun if (!*mr) {
331*4882a593Smuzhiyun *mr = rpcrdma_mr_get(r_xprt);
332*4882a593Smuzhiyun if (!*mr)
333*4882a593Smuzhiyun goto out_getmr_err;
334*4882a593Smuzhiyun trace_xprtrdma_mr_get(req);
335*4882a593Smuzhiyun (*mr)->mr_req = req;
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun rpcrdma_mr_push(*mr, &req->rl_registered);
339*4882a593Smuzhiyun return frwr_map(r_xprt, seg, nsegs, writing, req->rl_slot.rq_xid, *mr);
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun out_getmr_err:
342*4882a593Smuzhiyun trace_xprtrdma_nomrs(req);
343*4882a593Smuzhiyun xprt_wait_for_buffer_space(&r_xprt->rx_xprt);
344*4882a593Smuzhiyun rpcrdma_mrs_refresh(r_xprt);
345*4882a593Smuzhiyun return ERR_PTR(-EAGAIN);
346*4882a593Smuzhiyun }
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun /* Register and XDR encode the Read list. Supports encoding a list of read
349*4882a593Smuzhiyun * segments that belong to a single read chunk.
350*4882a593Smuzhiyun *
351*4882a593Smuzhiyun * Encoding key for single-list chunks (HLOO = Handle32 Length32 Offset64):
352*4882a593Smuzhiyun *
353*4882a593Smuzhiyun * Read chunklist (a linked list):
354*4882a593Smuzhiyun * N elements, position P (same P for all chunks of same arg!):
355*4882a593Smuzhiyun * 1 - PHLOO - 1 - PHLOO - ... - 1 - PHLOO - 0
356*4882a593Smuzhiyun *
357*4882a593Smuzhiyun * Returns zero on success, or a negative errno if a failure occurred.
358*4882a593Smuzhiyun * @xdr is advanced to the next position in the stream.
359*4882a593Smuzhiyun *
360*4882a593Smuzhiyun * Only a single @pos value is currently supported.
361*4882a593Smuzhiyun */
rpcrdma_encode_read_list(struct rpcrdma_xprt * r_xprt,struct rpcrdma_req * req,struct rpc_rqst * rqst,enum rpcrdma_chunktype rtype)362*4882a593Smuzhiyun static int rpcrdma_encode_read_list(struct rpcrdma_xprt *r_xprt,
363*4882a593Smuzhiyun struct rpcrdma_req *req,
364*4882a593Smuzhiyun struct rpc_rqst *rqst,
365*4882a593Smuzhiyun enum rpcrdma_chunktype rtype)
366*4882a593Smuzhiyun {
367*4882a593Smuzhiyun struct xdr_stream *xdr = &req->rl_stream;
368*4882a593Smuzhiyun struct rpcrdma_mr_seg *seg;
369*4882a593Smuzhiyun struct rpcrdma_mr *mr;
370*4882a593Smuzhiyun unsigned int pos;
371*4882a593Smuzhiyun int nsegs;
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun if (rtype == rpcrdma_noch_pullup || rtype == rpcrdma_noch_mapped)
374*4882a593Smuzhiyun goto done;
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun pos = rqst->rq_snd_buf.head[0].iov_len;
377*4882a593Smuzhiyun if (rtype == rpcrdma_areadch)
378*4882a593Smuzhiyun pos = 0;
379*4882a593Smuzhiyun seg = req->rl_segments;
380*4882a593Smuzhiyun nsegs = rpcrdma_convert_iovs(r_xprt, &rqst->rq_snd_buf, pos,
381*4882a593Smuzhiyun rtype, seg);
382*4882a593Smuzhiyun if (nsegs < 0)
383*4882a593Smuzhiyun return nsegs;
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun do {
386*4882a593Smuzhiyun seg = rpcrdma_mr_prepare(r_xprt, req, seg, nsegs, false, &mr);
387*4882a593Smuzhiyun if (IS_ERR(seg))
388*4882a593Smuzhiyun return PTR_ERR(seg);
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun if (encode_read_segment(xdr, mr, pos) < 0)
391*4882a593Smuzhiyun return -EMSGSIZE;
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun trace_xprtrdma_chunk_read(rqst->rq_task, pos, mr, nsegs);
394*4882a593Smuzhiyun r_xprt->rx_stats.read_chunk_count++;
395*4882a593Smuzhiyun nsegs -= mr->mr_nents;
396*4882a593Smuzhiyun } while (nsegs);
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun done:
399*4882a593Smuzhiyun if (xdr_stream_encode_item_absent(xdr) < 0)
400*4882a593Smuzhiyun return -EMSGSIZE;
401*4882a593Smuzhiyun return 0;
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun /* Register and XDR encode the Write list. Supports encoding a list
405*4882a593Smuzhiyun * containing one array of plain segments that belong to a single
406*4882a593Smuzhiyun * write chunk.
407*4882a593Smuzhiyun *
408*4882a593Smuzhiyun * Encoding key for single-list chunks (HLOO = Handle32 Length32 Offset64):
409*4882a593Smuzhiyun *
410*4882a593Smuzhiyun * Write chunklist (a list of (one) counted array):
411*4882a593Smuzhiyun * N elements:
412*4882a593Smuzhiyun * 1 - N - HLOO - HLOO - ... - HLOO - 0
413*4882a593Smuzhiyun *
414*4882a593Smuzhiyun * Returns zero on success, or a negative errno if a failure occurred.
415*4882a593Smuzhiyun * @xdr is advanced to the next position in the stream.
416*4882a593Smuzhiyun *
417*4882a593Smuzhiyun * Only a single Write chunk is currently supported.
418*4882a593Smuzhiyun */
rpcrdma_encode_write_list(struct rpcrdma_xprt * r_xprt,struct rpcrdma_req * req,struct rpc_rqst * rqst,enum rpcrdma_chunktype wtype)419*4882a593Smuzhiyun static int rpcrdma_encode_write_list(struct rpcrdma_xprt *r_xprt,
420*4882a593Smuzhiyun struct rpcrdma_req *req,
421*4882a593Smuzhiyun struct rpc_rqst *rqst,
422*4882a593Smuzhiyun enum rpcrdma_chunktype wtype)
423*4882a593Smuzhiyun {
424*4882a593Smuzhiyun struct xdr_stream *xdr = &req->rl_stream;
425*4882a593Smuzhiyun struct rpcrdma_mr_seg *seg;
426*4882a593Smuzhiyun struct rpcrdma_mr *mr;
427*4882a593Smuzhiyun int nsegs, nchunks;
428*4882a593Smuzhiyun __be32 *segcount;
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun if (wtype != rpcrdma_writech)
431*4882a593Smuzhiyun goto done;
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun seg = req->rl_segments;
434*4882a593Smuzhiyun nsegs = rpcrdma_convert_iovs(r_xprt, &rqst->rq_rcv_buf,
435*4882a593Smuzhiyun rqst->rq_rcv_buf.head[0].iov_len,
436*4882a593Smuzhiyun wtype, seg);
437*4882a593Smuzhiyun if (nsegs < 0)
438*4882a593Smuzhiyun return nsegs;
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun if (xdr_stream_encode_item_present(xdr) < 0)
441*4882a593Smuzhiyun return -EMSGSIZE;
442*4882a593Smuzhiyun segcount = xdr_reserve_space(xdr, sizeof(*segcount));
443*4882a593Smuzhiyun if (unlikely(!segcount))
444*4882a593Smuzhiyun return -EMSGSIZE;
445*4882a593Smuzhiyun /* Actual value encoded below */
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun nchunks = 0;
448*4882a593Smuzhiyun do {
449*4882a593Smuzhiyun seg = rpcrdma_mr_prepare(r_xprt, req, seg, nsegs, true, &mr);
450*4882a593Smuzhiyun if (IS_ERR(seg))
451*4882a593Smuzhiyun return PTR_ERR(seg);
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun if (encode_rdma_segment(xdr, mr) < 0)
454*4882a593Smuzhiyun return -EMSGSIZE;
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun trace_xprtrdma_chunk_write(rqst->rq_task, mr, nsegs);
457*4882a593Smuzhiyun r_xprt->rx_stats.write_chunk_count++;
458*4882a593Smuzhiyun r_xprt->rx_stats.total_rdma_request += mr->mr_length;
459*4882a593Smuzhiyun nchunks++;
460*4882a593Smuzhiyun nsegs -= mr->mr_nents;
461*4882a593Smuzhiyun } while (nsegs);
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun /* Update count of segments in this Write chunk */
464*4882a593Smuzhiyun *segcount = cpu_to_be32(nchunks);
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun done:
467*4882a593Smuzhiyun if (xdr_stream_encode_item_absent(xdr) < 0)
468*4882a593Smuzhiyun return -EMSGSIZE;
469*4882a593Smuzhiyun return 0;
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun /* Register and XDR encode the Reply chunk. Supports encoding an array
473*4882a593Smuzhiyun * of plain segments that belong to a single write (reply) chunk.
474*4882a593Smuzhiyun *
475*4882a593Smuzhiyun * Encoding key for single-list chunks (HLOO = Handle32 Length32 Offset64):
476*4882a593Smuzhiyun *
477*4882a593Smuzhiyun * Reply chunk (a counted array):
478*4882a593Smuzhiyun * N elements:
479*4882a593Smuzhiyun * 1 - N - HLOO - HLOO - ... - HLOO
480*4882a593Smuzhiyun *
481*4882a593Smuzhiyun * Returns zero on success, or a negative errno if a failure occurred.
482*4882a593Smuzhiyun * @xdr is advanced to the next position in the stream.
483*4882a593Smuzhiyun */
rpcrdma_encode_reply_chunk(struct rpcrdma_xprt * r_xprt,struct rpcrdma_req * req,struct rpc_rqst * rqst,enum rpcrdma_chunktype wtype)484*4882a593Smuzhiyun static int rpcrdma_encode_reply_chunk(struct rpcrdma_xprt *r_xprt,
485*4882a593Smuzhiyun struct rpcrdma_req *req,
486*4882a593Smuzhiyun struct rpc_rqst *rqst,
487*4882a593Smuzhiyun enum rpcrdma_chunktype wtype)
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun struct xdr_stream *xdr = &req->rl_stream;
490*4882a593Smuzhiyun struct rpcrdma_mr_seg *seg;
491*4882a593Smuzhiyun struct rpcrdma_mr *mr;
492*4882a593Smuzhiyun int nsegs, nchunks;
493*4882a593Smuzhiyun __be32 *segcount;
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun if (wtype != rpcrdma_replych) {
496*4882a593Smuzhiyun if (xdr_stream_encode_item_absent(xdr) < 0)
497*4882a593Smuzhiyun return -EMSGSIZE;
498*4882a593Smuzhiyun return 0;
499*4882a593Smuzhiyun }
500*4882a593Smuzhiyun
501*4882a593Smuzhiyun seg = req->rl_segments;
502*4882a593Smuzhiyun nsegs = rpcrdma_convert_iovs(r_xprt, &rqst->rq_rcv_buf, 0, wtype, seg);
503*4882a593Smuzhiyun if (nsegs < 0)
504*4882a593Smuzhiyun return nsegs;
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun if (xdr_stream_encode_item_present(xdr) < 0)
507*4882a593Smuzhiyun return -EMSGSIZE;
508*4882a593Smuzhiyun segcount = xdr_reserve_space(xdr, sizeof(*segcount));
509*4882a593Smuzhiyun if (unlikely(!segcount))
510*4882a593Smuzhiyun return -EMSGSIZE;
511*4882a593Smuzhiyun /* Actual value encoded below */
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun nchunks = 0;
514*4882a593Smuzhiyun do {
515*4882a593Smuzhiyun seg = rpcrdma_mr_prepare(r_xprt, req, seg, nsegs, true, &mr);
516*4882a593Smuzhiyun if (IS_ERR(seg))
517*4882a593Smuzhiyun return PTR_ERR(seg);
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun if (encode_rdma_segment(xdr, mr) < 0)
520*4882a593Smuzhiyun return -EMSGSIZE;
521*4882a593Smuzhiyun
522*4882a593Smuzhiyun trace_xprtrdma_chunk_reply(rqst->rq_task, mr, nsegs);
523*4882a593Smuzhiyun r_xprt->rx_stats.reply_chunk_count++;
524*4882a593Smuzhiyun r_xprt->rx_stats.total_rdma_request += mr->mr_length;
525*4882a593Smuzhiyun nchunks++;
526*4882a593Smuzhiyun nsegs -= mr->mr_nents;
527*4882a593Smuzhiyun } while (nsegs);
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun /* Update count of segments in the Reply chunk */
530*4882a593Smuzhiyun *segcount = cpu_to_be32(nchunks);
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun return 0;
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun
rpcrdma_sendctx_done(struct kref * kref)535*4882a593Smuzhiyun static void rpcrdma_sendctx_done(struct kref *kref)
536*4882a593Smuzhiyun {
537*4882a593Smuzhiyun struct rpcrdma_req *req =
538*4882a593Smuzhiyun container_of(kref, struct rpcrdma_req, rl_kref);
539*4882a593Smuzhiyun struct rpcrdma_rep *rep = req->rl_reply;
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun rpcrdma_complete_rqst(rep);
542*4882a593Smuzhiyun rep->rr_rxprt->rx_stats.reply_waits_for_send++;
543*4882a593Smuzhiyun }
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun /**
546*4882a593Smuzhiyun * rpcrdma_sendctx_unmap - DMA-unmap Send buffer
547*4882a593Smuzhiyun * @sc: sendctx containing SGEs to unmap
548*4882a593Smuzhiyun *
549*4882a593Smuzhiyun */
rpcrdma_sendctx_unmap(struct rpcrdma_sendctx * sc)550*4882a593Smuzhiyun void rpcrdma_sendctx_unmap(struct rpcrdma_sendctx *sc)
551*4882a593Smuzhiyun {
552*4882a593Smuzhiyun struct rpcrdma_regbuf *rb = sc->sc_req->rl_sendbuf;
553*4882a593Smuzhiyun struct ib_sge *sge;
554*4882a593Smuzhiyun
555*4882a593Smuzhiyun if (!sc->sc_unmap_count)
556*4882a593Smuzhiyun return;
557*4882a593Smuzhiyun
558*4882a593Smuzhiyun /* The first two SGEs contain the transport header and
559*4882a593Smuzhiyun * the inline buffer. These are always left mapped so
560*4882a593Smuzhiyun * they can be cheaply re-used.
561*4882a593Smuzhiyun */
562*4882a593Smuzhiyun for (sge = &sc->sc_sges[2]; sc->sc_unmap_count;
563*4882a593Smuzhiyun ++sge, --sc->sc_unmap_count)
564*4882a593Smuzhiyun ib_dma_unmap_page(rdmab_device(rb), sge->addr, sge->length,
565*4882a593Smuzhiyun DMA_TO_DEVICE);
566*4882a593Smuzhiyun
567*4882a593Smuzhiyun kref_put(&sc->sc_req->rl_kref, rpcrdma_sendctx_done);
568*4882a593Smuzhiyun }
569*4882a593Smuzhiyun
570*4882a593Smuzhiyun /* Prepare an SGE for the RPC-over-RDMA transport header.
571*4882a593Smuzhiyun */
rpcrdma_prepare_hdr_sge(struct rpcrdma_xprt * r_xprt,struct rpcrdma_req * req,u32 len)572*4882a593Smuzhiyun static void rpcrdma_prepare_hdr_sge(struct rpcrdma_xprt *r_xprt,
573*4882a593Smuzhiyun struct rpcrdma_req *req, u32 len)
574*4882a593Smuzhiyun {
575*4882a593Smuzhiyun struct rpcrdma_sendctx *sc = req->rl_sendctx;
576*4882a593Smuzhiyun struct rpcrdma_regbuf *rb = req->rl_rdmabuf;
577*4882a593Smuzhiyun struct ib_sge *sge = &sc->sc_sges[req->rl_wr.num_sge++];
578*4882a593Smuzhiyun
579*4882a593Smuzhiyun sge->addr = rdmab_addr(rb);
580*4882a593Smuzhiyun sge->length = len;
581*4882a593Smuzhiyun sge->lkey = rdmab_lkey(rb);
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun ib_dma_sync_single_for_device(rdmab_device(rb), sge->addr, sge->length,
584*4882a593Smuzhiyun DMA_TO_DEVICE);
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun
587*4882a593Smuzhiyun /* The head iovec is straightforward, as it is usually already
588*4882a593Smuzhiyun * DMA-mapped. Sync the content that has changed.
589*4882a593Smuzhiyun */
rpcrdma_prepare_head_iov(struct rpcrdma_xprt * r_xprt,struct rpcrdma_req * req,unsigned int len)590*4882a593Smuzhiyun static bool rpcrdma_prepare_head_iov(struct rpcrdma_xprt *r_xprt,
591*4882a593Smuzhiyun struct rpcrdma_req *req, unsigned int len)
592*4882a593Smuzhiyun {
593*4882a593Smuzhiyun struct rpcrdma_sendctx *sc = req->rl_sendctx;
594*4882a593Smuzhiyun struct ib_sge *sge = &sc->sc_sges[req->rl_wr.num_sge++];
595*4882a593Smuzhiyun struct rpcrdma_regbuf *rb = req->rl_sendbuf;
596*4882a593Smuzhiyun
597*4882a593Smuzhiyun if (!rpcrdma_regbuf_dma_map(r_xprt, rb))
598*4882a593Smuzhiyun return false;
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun sge->addr = rdmab_addr(rb);
601*4882a593Smuzhiyun sge->length = len;
602*4882a593Smuzhiyun sge->lkey = rdmab_lkey(rb);
603*4882a593Smuzhiyun
604*4882a593Smuzhiyun ib_dma_sync_single_for_device(rdmab_device(rb), sge->addr, sge->length,
605*4882a593Smuzhiyun DMA_TO_DEVICE);
606*4882a593Smuzhiyun return true;
607*4882a593Smuzhiyun }
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun /* If there is a page list present, DMA map and prepare an
610*4882a593Smuzhiyun * SGE for each page to be sent.
611*4882a593Smuzhiyun */
rpcrdma_prepare_pagelist(struct rpcrdma_req * req,struct xdr_buf * xdr)612*4882a593Smuzhiyun static bool rpcrdma_prepare_pagelist(struct rpcrdma_req *req,
613*4882a593Smuzhiyun struct xdr_buf *xdr)
614*4882a593Smuzhiyun {
615*4882a593Smuzhiyun struct rpcrdma_sendctx *sc = req->rl_sendctx;
616*4882a593Smuzhiyun struct rpcrdma_regbuf *rb = req->rl_sendbuf;
617*4882a593Smuzhiyun unsigned int page_base, len, remaining;
618*4882a593Smuzhiyun struct page **ppages;
619*4882a593Smuzhiyun struct ib_sge *sge;
620*4882a593Smuzhiyun
621*4882a593Smuzhiyun ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT);
622*4882a593Smuzhiyun page_base = offset_in_page(xdr->page_base);
623*4882a593Smuzhiyun remaining = xdr->page_len;
624*4882a593Smuzhiyun while (remaining) {
625*4882a593Smuzhiyun sge = &sc->sc_sges[req->rl_wr.num_sge++];
626*4882a593Smuzhiyun len = min_t(unsigned int, PAGE_SIZE - page_base, remaining);
627*4882a593Smuzhiyun sge->addr = ib_dma_map_page(rdmab_device(rb), *ppages,
628*4882a593Smuzhiyun page_base, len, DMA_TO_DEVICE);
629*4882a593Smuzhiyun if (ib_dma_mapping_error(rdmab_device(rb), sge->addr))
630*4882a593Smuzhiyun goto out_mapping_err;
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun sge->length = len;
633*4882a593Smuzhiyun sge->lkey = rdmab_lkey(rb);
634*4882a593Smuzhiyun
635*4882a593Smuzhiyun sc->sc_unmap_count++;
636*4882a593Smuzhiyun ppages++;
637*4882a593Smuzhiyun remaining -= len;
638*4882a593Smuzhiyun page_base = 0;
639*4882a593Smuzhiyun }
640*4882a593Smuzhiyun
641*4882a593Smuzhiyun return true;
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun out_mapping_err:
644*4882a593Smuzhiyun trace_xprtrdma_dma_maperr(sge->addr);
645*4882a593Smuzhiyun return false;
646*4882a593Smuzhiyun }
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun /* The tail iovec may include an XDR pad for the page list,
649*4882a593Smuzhiyun * as well as additional content, and may not reside in the
650*4882a593Smuzhiyun * same page as the head iovec.
651*4882a593Smuzhiyun */
rpcrdma_prepare_tail_iov(struct rpcrdma_req * req,struct xdr_buf * xdr,unsigned int page_base,unsigned int len)652*4882a593Smuzhiyun static bool rpcrdma_prepare_tail_iov(struct rpcrdma_req *req,
653*4882a593Smuzhiyun struct xdr_buf *xdr,
654*4882a593Smuzhiyun unsigned int page_base, unsigned int len)
655*4882a593Smuzhiyun {
656*4882a593Smuzhiyun struct rpcrdma_sendctx *sc = req->rl_sendctx;
657*4882a593Smuzhiyun struct ib_sge *sge = &sc->sc_sges[req->rl_wr.num_sge++];
658*4882a593Smuzhiyun struct rpcrdma_regbuf *rb = req->rl_sendbuf;
659*4882a593Smuzhiyun struct page *page = virt_to_page(xdr->tail[0].iov_base);
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun sge->addr = ib_dma_map_page(rdmab_device(rb), page, page_base, len,
662*4882a593Smuzhiyun DMA_TO_DEVICE);
663*4882a593Smuzhiyun if (ib_dma_mapping_error(rdmab_device(rb), sge->addr))
664*4882a593Smuzhiyun goto out_mapping_err;
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun sge->length = len;
667*4882a593Smuzhiyun sge->lkey = rdmab_lkey(rb);
668*4882a593Smuzhiyun ++sc->sc_unmap_count;
669*4882a593Smuzhiyun return true;
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun out_mapping_err:
672*4882a593Smuzhiyun trace_xprtrdma_dma_maperr(sge->addr);
673*4882a593Smuzhiyun return false;
674*4882a593Smuzhiyun }
675*4882a593Smuzhiyun
676*4882a593Smuzhiyun /* Copy the tail to the end of the head buffer.
677*4882a593Smuzhiyun */
rpcrdma_pullup_tail_iov(struct rpcrdma_xprt * r_xprt,struct rpcrdma_req * req,struct xdr_buf * xdr)678*4882a593Smuzhiyun static void rpcrdma_pullup_tail_iov(struct rpcrdma_xprt *r_xprt,
679*4882a593Smuzhiyun struct rpcrdma_req *req,
680*4882a593Smuzhiyun struct xdr_buf *xdr)
681*4882a593Smuzhiyun {
682*4882a593Smuzhiyun unsigned char *dst;
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun dst = (unsigned char *)xdr->head[0].iov_base;
685*4882a593Smuzhiyun dst += xdr->head[0].iov_len + xdr->page_len;
686*4882a593Smuzhiyun memmove(dst, xdr->tail[0].iov_base, xdr->tail[0].iov_len);
687*4882a593Smuzhiyun r_xprt->rx_stats.pullup_copy_count += xdr->tail[0].iov_len;
688*4882a593Smuzhiyun }
689*4882a593Smuzhiyun
690*4882a593Smuzhiyun /* Copy pagelist content into the head buffer.
691*4882a593Smuzhiyun */
rpcrdma_pullup_pagelist(struct rpcrdma_xprt * r_xprt,struct rpcrdma_req * req,struct xdr_buf * xdr)692*4882a593Smuzhiyun static void rpcrdma_pullup_pagelist(struct rpcrdma_xprt *r_xprt,
693*4882a593Smuzhiyun struct rpcrdma_req *req,
694*4882a593Smuzhiyun struct xdr_buf *xdr)
695*4882a593Smuzhiyun {
696*4882a593Smuzhiyun unsigned int len, page_base, remaining;
697*4882a593Smuzhiyun struct page **ppages;
698*4882a593Smuzhiyun unsigned char *src, *dst;
699*4882a593Smuzhiyun
700*4882a593Smuzhiyun dst = (unsigned char *)xdr->head[0].iov_base;
701*4882a593Smuzhiyun dst += xdr->head[0].iov_len;
702*4882a593Smuzhiyun ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT);
703*4882a593Smuzhiyun page_base = offset_in_page(xdr->page_base);
704*4882a593Smuzhiyun remaining = xdr->page_len;
705*4882a593Smuzhiyun while (remaining) {
706*4882a593Smuzhiyun src = page_address(*ppages);
707*4882a593Smuzhiyun src += page_base;
708*4882a593Smuzhiyun len = min_t(unsigned int, PAGE_SIZE - page_base, remaining);
709*4882a593Smuzhiyun memcpy(dst, src, len);
710*4882a593Smuzhiyun r_xprt->rx_stats.pullup_copy_count += len;
711*4882a593Smuzhiyun
712*4882a593Smuzhiyun ppages++;
713*4882a593Smuzhiyun dst += len;
714*4882a593Smuzhiyun remaining -= len;
715*4882a593Smuzhiyun page_base = 0;
716*4882a593Smuzhiyun }
717*4882a593Smuzhiyun }
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun /* Copy the contents of @xdr into @rl_sendbuf and DMA sync it.
720*4882a593Smuzhiyun * When the head, pagelist, and tail are small, a pull-up copy
721*4882a593Smuzhiyun * is considerably less costly than DMA mapping the components
722*4882a593Smuzhiyun * of @xdr.
723*4882a593Smuzhiyun *
724*4882a593Smuzhiyun * Assumptions:
725*4882a593Smuzhiyun * - the caller has already verified that the total length
726*4882a593Smuzhiyun * of the RPC Call body will fit into @rl_sendbuf.
727*4882a593Smuzhiyun */
rpcrdma_prepare_noch_pullup(struct rpcrdma_xprt * r_xprt,struct rpcrdma_req * req,struct xdr_buf * xdr)728*4882a593Smuzhiyun static bool rpcrdma_prepare_noch_pullup(struct rpcrdma_xprt *r_xprt,
729*4882a593Smuzhiyun struct rpcrdma_req *req,
730*4882a593Smuzhiyun struct xdr_buf *xdr)
731*4882a593Smuzhiyun {
732*4882a593Smuzhiyun if (unlikely(xdr->tail[0].iov_len))
733*4882a593Smuzhiyun rpcrdma_pullup_tail_iov(r_xprt, req, xdr);
734*4882a593Smuzhiyun
735*4882a593Smuzhiyun if (unlikely(xdr->page_len))
736*4882a593Smuzhiyun rpcrdma_pullup_pagelist(r_xprt, req, xdr);
737*4882a593Smuzhiyun
738*4882a593Smuzhiyun /* The whole RPC message resides in the head iovec now */
739*4882a593Smuzhiyun return rpcrdma_prepare_head_iov(r_xprt, req, xdr->len);
740*4882a593Smuzhiyun }
741*4882a593Smuzhiyun
rpcrdma_prepare_noch_mapped(struct rpcrdma_xprt * r_xprt,struct rpcrdma_req * req,struct xdr_buf * xdr)742*4882a593Smuzhiyun static bool rpcrdma_prepare_noch_mapped(struct rpcrdma_xprt *r_xprt,
743*4882a593Smuzhiyun struct rpcrdma_req *req,
744*4882a593Smuzhiyun struct xdr_buf *xdr)
745*4882a593Smuzhiyun {
746*4882a593Smuzhiyun struct kvec *tail = &xdr->tail[0];
747*4882a593Smuzhiyun
748*4882a593Smuzhiyun if (!rpcrdma_prepare_head_iov(r_xprt, req, xdr->head[0].iov_len))
749*4882a593Smuzhiyun return false;
750*4882a593Smuzhiyun if (xdr->page_len)
751*4882a593Smuzhiyun if (!rpcrdma_prepare_pagelist(req, xdr))
752*4882a593Smuzhiyun return false;
753*4882a593Smuzhiyun if (tail->iov_len)
754*4882a593Smuzhiyun if (!rpcrdma_prepare_tail_iov(req, xdr,
755*4882a593Smuzhiyun offset_in_page(tail->iov_base),
756*4882a593Smuzhiyun tail->iov_len))
757*4882a593Smuzhiyun return false;
758*4882a593Smuzhiyun
759*4882a593Smuzhiyun if (req->rl_sendctx->sc_unmap_count)
760*4882a593Smuzhiyun kref_get(&req->rl_kref);
761*4882a593Smuzhiyun return true;
762*4882a593Smuzhiyun }
763*4882a593Smuzhiyun
rpcrdma_prepare_readch(struct rpcrdma_xprt * r_xprt,struct rpcrdma_req * req,struct xdr_buf * xdr)764*4882a593Smuzhiyun static bool rpcrdma_prepare_readch(struct rpcrdma_xprt *r_xprt,
765*4882a593Smuzhiyun struct rpcrdma_req *req,
766*4882a593Smuzhiyun struct xdr_buf *xdr)
767*4882a593Smuzhiyun {
768*4882a593Smuzhiyun if (!rpcrdma_prepare_head_iov(r_xprt, req, xdr->head[0].iov_len))
769*4882a593Smuzhiyun return false;
770*4882a593Smuzhiyun
771*4882a593Smuzhiyun /* If there is a Read chunk, the page list is being handled
772*4882a593Smuzhiyun * via explicit RDMA, and thus is skipped here.
773*4882a593Smuzhiyun */
774*4882a593Smuzhiyun
775*4882a593Smuzhiyun /* Do not include the tail if it is only an XDR pad */
776*4882a593Smuzhiyun if (xdr->tail[0].iov_len > 3) {
777*4882a593Smuzhiyun unsigned int page_base, len;
778*4882a593Smuzhiyun
779*4882a593Smuzhiyun /* If the content in the page list is an odd length,
780*4882a593Smuzhiyun * xdr_write_pages() adds a pad at the beginning of
781*4882a593Smuzhiyun * the tail iovec. Force the tail's non-pad content to
782*4882a593Smuzhiyun * land at the next XDR position in the Send message.
783*4882a593Smuzhiyun */
784*4882a593Smuzhiyun page_base = offset_in_page(xdr->tail[0].iov_base);
785*4882a593Smuzhiyun len = xdr->tail[0].iov_len;
786*4882a593Smuzhiyun page_base += len & 3;
787*4882a593Smuzhiyun len -= len & 3;
788*4882a593Smuzhiyun if (!rpcrdma_prepare_tail_iov(req, xdr, page_base, len))
789*4882a593Smuzhiyun return false;
790*4882a593Smuzhiyun kref_get(&req->rl_kref);
791*4882a593Smuzhiyun }
792*4882a593Smuzhiyun
793*4882a593Smuzhiyun return true;
794*4882a593Smuzhiyun }
795*4882a593Smuzhiyun
796*4882a593Smuzhiyun /**
797*4882a593Smuzhiyun * rpcrdma_prepare_send_sges - Construct SGEs for a Send WR
798*4882a593Smuzhiyun * @r_xprt: controlling transport
799*4882a593Smuzhiyun * @req: context of RPC Call being marshalled
800*4882a593Smuzhiyun * @hdrlen: size of transport header, in bytes
801*4882a593Smuzhiyun * @xdr: xdr_buf containing RPC Call
802*4882a593Smuzhiyun * @rtype: chunk type being encoded
803*4882a593Smuzhiyun *
804*4882a593Smuzhiyun * Returns 0 on success; otherwise a negative errno is returned.
805*4882a593Smuzhiyun */
rpcrdma_prepare_send_sges(struct rpcrdma_xprt * r_xprt,struct rpcrdma_req * req,u32 hdrlen,struct xdr_buf * xdr,enum rpcrdma_chunktype rtype)806*4882a593Smuzhiyun inline int rpcrdma_prepare_send_sges(struct rpcrdma_xprt *r_xprt,
807*4882a593Smuzhiyun struct rpcrdma_req *req, u32 hdrlen,
808*4882a593Smuzhiyun struct xdr_buf *xdr,
809*4882a593Smuzhiyun enum rpcrdma_chunktype rtype)
810*4882a593Smuzhiyun {
811*4882a593Smuzhiyun int ret;
812*4882a593Smuzhiyun
813*4882a593Smuzhiyun ret = -EAGAIN;
814*4882a593Smuzhiyun req->rl_sendctx = rpcrdma_sendctx_get_locked(r_xprt);
815*4882a593Smuzhiyun if (!req->rl_sendctx)
816*4882a593Smuzhiyun goto out_nosc;
817*4882a593Smuzhiyun req->rl_sendctx->sc_unmap_count = 0;
818*4882a593Smuzhiyun req->rl_sendctx->sc_req = req;
819*4882a593Smuzhiyun kref_init(&req->rl_kref);
820*4882a593Smuzhiyun req->rl_wr.wr_cqe = &req->rl_sendctx->sc_cqe;
821*4882a593Smuzhiyun req->rl_wr.sg_list = req->rl_sendctx->sc_sges;
822*4882a593Smuzhiyun req->rl_wr.num_sge = 0;
823*4882a593Smuzhiyun req->rl_wr.opcode = IB_WR_SEND;
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun rpcrdma_prepare_hdr_sge(r_xprt, req, hdrlen);
826*4882a593Smuzhiyun
827*4882a593Smuzhiyun ret = -EIO;
828*4882a593Smuzhiyun switch (rtype) {
829*4882a593Smuzhiyun case rpcrdma_noch_pullup:
830*4882a593Smuzhiyun if (!rpcrdma_prepare_noch_pullup(r_xprt, req, xdr))
831*4882a593Smuzhiyun goto out_unmap;
832*4882a593Smuzhiyun break;
833*4882a593Smuzhiyun case rpcrdma_noch_mapped:
834*4882a593Smuzhiyun if (!rpcrdma_prepare_noch_mapped(r_xprt, req, xdr))
835*4882a593Smuzhiyun goto out_unmap;
836*4882a593Smuzhiyun break;
837*4882a593Smuzhiyun case rpcrdma_readch:
838*4882a593Smuzhiyun if (!rpcrdma_prepare_readch(r_xprt, req, xdr))
839*4882a593Smuzhiyun goto out_unmap;
840*4882a593Smuzhiyun break;
841*4882a593Smuzhiyun case rpcrdma_areadch:
842*4882a593Smuzhiyun break;
843*4882a593Smuzhiyun default:
844*4882a593Smuzhiyun goto out_unmap;
845*4882a593Smuzhiyun }
846*4882a593Smuzhiyun
847*4882a593Smuzhiyun return 0;
848*4882a593Smuzhiyun
849*4882a593Smuzhiyun out_unmap:
850*4882a593Smuzhiyun rpcrdma_sendctx_unmap(req->rl_sendctx);
851*4882a593Smuzhiyun out_nosc:
852*4882a593Smuzhiyun trace_xprtrdma_prepsend_failed(&req->rl_slot, ret);
853*4882a593Smuzhiyun return ret;
854*4882a593Smuzhiyun }
855*4882a593Smuzhiyun
856*4882a593Smuzhiyun /**
857*4882a593Smuzhiyun * rpcrdma_marshal_req - Marshal and send one RPC request
858*4882a593Smuzhiyun * @r_xprt: controlling transport
859*4882a593Smuzhiyun * @rqst: RPC request to be marshaled
860*4882a593Smuzhiyun *
861*4882a593Smuzhiyun * For the RPC in "rqst", this function:
862*4882a593Smuzhiyun * - Chooses the transfer mode (eg., RDMA_MSG or RDMA_NOMSG)
863*4882a593Smuzhiyun * - Registers Read, Write, and Reply chunks
864*4882a593Smuzhiyun * - Constructs the transport header
865*4882a593Smuzhiyun * - Posts a Send WR to send the transport header and request
866*4882a593Smuzhiyun *
867*4882a593Smuzhiyun * Returns:
868*4882a593Smuzhiyun * %0 if the RPC was sent successfully,
869*4882a593Smuzhiyun * %-ENOTCONN if the connection was lost,
870*4882a593Smuzhiyun * %-EAGAIN if the caller should call again with the same arguments,
871*4882a593Smuzhiyun * %-ENOBUFS if the caller should call again after a delay,
872*4882a593Smuzhiyun * %-EMSGSIZE if the transport header is too small,
873*4882a593Smuzhiyun * %-EIO if a permanent problem occurred while marshaling.
874*4882a593Smuzhiyun */
875*4882a593Smuzhiyun int
rpcrdma_marshal_req(struct rpcrdma_xprt * r_xprt,struct rpc_rqst * rqst)876*4882a593Smuzhiyun rpcrdma_marshal_req(struct rpcrdma_xprt *r_xprt, struct rpc_rqst *rqst)
877*4882a593Smuzhiyun {
878*4882a593Smuzhiyun struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
879*4882a593Smuzhiyun struct xdr_stream *xdr = &req->rl_stream;
880*4882a593Smuzhiyun enum rpcrdma_chunktype rtype, wtype;
881*4882a593Smuzhiyun struct xdr_buf *buf = &rqst->rq_snd_buf;
882*4882a593Smuzhiyun bool ddp_allowed;
883*4882a593Smuzhiyun __be32 *p;
884*4882a593Smuzhiyun int ret;
885*4882a593Smuzhiyun
886*4882a593Smuzhiyun if (unlikely(rqst->rq_rcv_buf.flags & XDRBUF_SPARSE_PAGES)) {
887*4882a593Smuzhiyun ret = rpcrdma_alloc_sparse_pages(&rqst->rq_rcv_buf);
888*4882a593Smuzhiyun if (ret)
889*4882a593Smuzhiyun return ret;
890*4882a593Smuzhiyun }
891*4882a593Smuzhiyun
892*4882a593Smuzhiyun rpcrdma_set_xdrlen(&req->rl_hdrbuf, 0);
893*4882a593Smuzhiyun xdr_init_encode(xdr, &req->rl_hdrbuf, rdmab_data(req->rl_rdmabuf),
894*4882a593Smuzhiyun rqst);
895*4882a593Smuzhiyun
896*4882a593Smuzhiyun /* Fixed header fields */
897*4882a593Smuzhiyun ret = -EMSGSIZE;
898*4882a593Smuzhiyun p = xdr_reserve_space(xdr, 4 * sizeof(*p));
899*4882a593Smuzhiyun if (!p)
900*4882a593Smuzhiyun goto out_err;
901*4882a593Smuzhiyun *p++ = rqst->rq_xid;
902*4882a593Smuzhiyun *p++ = rpcrdma_version;
903*4882a593Smuzhiyun *p++ = r_xprt->rx_buf.rb_max_requests;
904*4882a593Smuzhiyun
905*4882a593Smuzhiyun /* When the ULP employs a GSS flavor that guarantees integrity
906*4882a593Smuzhiyun * or privacy, direct data placement of individual data items
907*4882a593Smuzhiyun * is not allowed.
908*4882a593Smuzhiyun */
909*4882a593Smuzhiyun ddp_allowed = !test_bit(RPCAUTH_AUTH_DATATOUCH,
910*4882a593Smuzhiyun &rqst->rq_cred->cr_auth->au_flags);
911*4882a593Smuzhiyun
912*4882a593Smuzhiyun /*
913*4882a593Smuzhiyun * Chunks needed for results?
914*4882a593Smuzhiyun *
915*4882a593Smuzhiyun * o If the expected result is under the inline threshold, all ops
916*4882a593Smuzhiyun * return as inline.
917*4882a593Smuzhiyun * o Large read ops return data as write chunk(s), header as
918*4882a593Smuzhiyun * inline.
919*4882a593Smuzhiyun * o Large non-read ops return as a single reply chunk.
920*4882a593Smuzhiyun */
921*4882a593Smuzhiyun if (rpcrdma_results_inline(r_xprt, rqst))
922*4882a593Smuzhiyun wtype = rpcrdma_noch;
923*4882a593Smuzhiyun else if ((ddp_allowed && rqst->rq_rcv_buf.flags & XDRBUF_READ) &&
924*4882a593Smuzhiyun rpcrdma_nonpayload_inline(r_xprt, rqst))
925*4882a593Smuzhiyun wtype = rpcrdma_writech;
926*4882a593Smuzhiyun else
927*4882a593Smuzhiyun wtype = rpcrdma_replych;
928*4882a593Smuzhiyun
929*4882a593Smuzhiyun /*
930*4882a593Smuzhiyun * Chunks needed for arguments?
931*4882a593Smuzhiyun *
932*4882a593Smuzhiyun * o If the total request is under the inline threshold, all ops
933*4882a593Smuzhiyun * are sent as inline.
934*4882a593Smuzhiyun * o Large write ops transmit data as read chunk(s), header as
935*4882a593Smuzhiyun * inline.
936*4882a593Smuzhiyun * o Large non-write ops are sent with the entire message as a
937*4882a593Smuzhiyun * single read chunk (protocol 0-position special case).
938*4882a593Smuzhiyun *
939*4882a593Smuzhiyun * This assumes that the upper layer does not present a request
940*4882a593Smuzhiyun * that both has a data payload, and whose non-data arguments
941*4882a593Smuzhiyun * by themselves are larger than the inline threshold.
942*4882a593Smuzhiyun */
943*4882a593Smuzhiyun if (rpcrdma_args_inline(r_xprt, rqst)) {
944*4882a593Smuzhiyun *p++ = rdma_msg;
945*4882a593Smuzhiyun rtype = buf->len < rdmab_length(req->rl_sendbuf) ?
946*4882a593Smuzhiyun rpcrdma_noch_pullup : rpcrdma_noch_mapped;
947*4882a593Smuzhiyun } else if (ddp_allowed && buf->flags & XDRBUF_WRITE) {
948*4882a593Smuzhiyun *p++ = rdma_msg;
949*4882a593Smuzhiyun rtype = rpcrdma_readch;
950*4882a593Smuzhiyun } else {
951*4882a593Smuzhiyun r_xprt->rx_stats.nomsg_call_count++;
952*4882a593Smuzhiyun *p++ = rdma_nomsg;
953*4882a593Smuzhiyun rtype = rpcrdma_areadch;
954*4882a593Smuzhiyun }
955*4882a593Smuzhiyun
956*4882a593Smuzhiyun /* This implementation supports the following combinations
957*4882a593Smuzhiyun * of chunk lists in one RPC-over-RDMA Call message:
958*4882a593Smuzhiyun *
959*4882a593Smuzhiyun * - Read list
960*4882a593Smuzhiyun * - Write list
961*4882a593Smuzhiyun * - Reply chunk
962*4882a593Smuzhiyun * - Read list + Reply chunk
963*4882a593Smuzhiyun *
964*4882a593Smuzhiyun * It might not yet support the following combinations:
965*4882a593Smuzhiyun *
966*4882a593Smuzhiyun * - Read list + Write list
967*4882a593Smuzhiyun *
968*4882a593Smuzhiyun * It does not support the following combinations:
969*4882a593Smuzhiyun *
970*4882a593Smuzhiyun * - Write list + Reply chunk
971*4882a593Smuzhiyun * - Read list + Write list + Reply chunk
972*4882a593Smuzhiyun *
973*4882a593Smuzhiyun * This implementation supports only a single chunk in each
974*4882a593Smuzhiyun * Read or Write list. Thus for example the client cannot
975*4882a593Smuzhiyun * send a Call message with a Position Zero Read chunk and a
976*4882a593Smuzhiyun * regular Read chunk at the same time.
977*4882a593Smuzhiyun */
978*4882a593Smuzhiyun ret = rpcrdma_encode_read_list(r_xprt, req, rqst, rtype);
979*4882a593Smuzhiyun if (ret)
980*4882a593Smuzhiyun goto out_err;
981*4882a593Smuzhiyun ret = rpcrdma_encode_write_list(r_xprt, req, rqst, wtype);
982*4882a593Smuzhiyun if (ret)
983*4882a593Smuzhiyun goto out_err;
984*4882a593Smuzhiyun ret = rpcrdma_encode_reply_chunk(r_xprt, req, rqst, wtype);
985*4882a593Smuzhiyun if (ret)
986*4882a593Smuzhiyun goto out_err;
987*4882a593Smuzhiyun
988*4882a593Smuzhiyun ret = rpcrdma_prepare_send_sges(r_xprt, req, req->rl_hdrbuf.len,
989*4882a593Smuzhiyun buf, rtype);
990*4882a593Smuzhiyun if (ret)
991*4882a593Smuzhiyun goto out_err;
992*4882a593Smuzhiyun
993*4882a593Smuzhiyun trace_xprtrdma_marshal(req, rtype, wtype);
994*4882a593Smuzhiyun return 0;
995*4882a593Smuzhiyun
996*4882a593Smuzhiyun out_err:
997*4882a593Smuzhiyun trace_xprtrdma_marshal_failed(rqst, ret);
998*4882a593Smuzhiyun r_xprt->rx_stats.failed_marshal_count++;
999*4882a593Smuzhiyun frwr_reset(req);
1000*4882a593Smuzhiyun return ret;
1001*4882a593Smuzhiyun }
1002*4882a593Smuzhiyun
__rpcrdma_update_cwnd_locked(struct rpc_xprt * xprt,struct rpcrdma_buffer * buf,u32 grant)1003*4882a593Smuzhiyun static void __rpcrdma_update_cwnd_locked(struct rpc_xprt *xprt,
1004*4882a593Smuzhiyun struct rpcrdma_buffer *buf,
1005*4882a593Smuzhiyun u32 grant)
1006*4882a593Smuzhiyun {
1007*4882a593Smuzhiyun buf->rb_credits = grant;
1008*4882a593Smuzhiyun xprt->cwnd = grant << RPC_CWNDSHIFT;
1009*4882a593Smuzhiyun }
1010*4882a593Smuzhiyun
rpcrdma_update_cwnd(struct rpcrdma_xprt * r_xprt,u32 grant)1011*4882a593Smuzhiyun static void rpcrdma_update_cwnd(struct rpcrdma_xprt *r_xprt, u32 grant)
1012*4882a593Smuzhiyun {
1013*4882a593Smuzhiyun struct rpc_xprt *xprt = &r_xprt->rx_xprt;
1014*4882a593Smuzhiyun
1015*4882a593Smuzhiyun spin_lock(&xprt->transport_lock);
1016*4882a593Smuzhiyun __rpcrdma_update_cwnd_locked(xprt, &r_xprt->rx_buf, grant);
1017*4882a593Smuzhiyun spin_unlock(&xprt->transport_lock);
1018*4882a593Smuzhiyun }
1019*4882a593Smuzhiyun
1020*4882a593Smuzhiyun /**
1021*4882a593Smuzhiyun * rpcrdma_reset_cwnd - Reset the xprt's congestion window
1022*4882a593Smuzhiyun * @r_xprt: controlling transport instance
1023*4882a593Smuzhiyun *
1024*4882a593Smuzhiyun * Prepare @r_xprt for the next connection by reinitializing
1025*4882a593Smuzhiyun * its credit grant to one (see RFC 8166, Section 3.3.3).
1026*4882a593Smuzhiyun */
rpcrdma_reset_cwnd(struct rpcrdma_xprt * r_xprt)1027*4882a593Smuzhiyun void rpcrdma_reset_cwnd(struct rpcrdma_xprt *r_xprt)
1028*4882a593Smuzhiyun {
1029*4882a593Smuzhiyun struct rpc_xprt *xprt = &r_xprt->rx_xprt;
1030*4882a593Smuzhiyun
1031*4882a593Smuzhiyun spin_lock(&xprt->transport_lock);
1032*4882a593Smuzhiyun xprt->cong = 0;
1033*4882a593Smuzhiyun __rpcrdma_update_cwnd_locked(xprt, &r_xprt->rx_buf, 1);
1034*4882a593Smuzhiyun spin_unlock(&xprt->transport_lock);
1035*4882a593Smuzhiyun }
1036*4882a593Smuzhiyun
1037*4882a593Smuzhiyun /**
1038*4882a593Smuzhiyun * rpcrdma_inline_fixup - Scatter inline received data into rqst's iovecs
1039*4882a593Smuzhiyun * @rqst: controlling RPC request
1040*4882a593Smuzhiyun * @srcp: points to RPC message payload in receive buffer
1041*4882a593Smuzhiyun * @copy_len: remaining length of receive buffer content
1042*4882a593Smuzhiyun * @pad: Write chunk pad bytes needed (zero for pure inline)
1043*4882a593Smuzhiyun *
1044*4882a593Smuzhiyun * The upper layer has set the maximum number of bytes it can
1045*4882a593Smuzhiyun * receive in each component of rq_rcv_buf. These values are set in
1046*4882a593Smuzhiyun * the head.iov_len, page_len, tail.iov_len, and buflen fields.
1047*4882a593Smuzhiyun *
1048*4882a593Smuzhiyun * Unlike the TCP equivalent (xdr_partial_copy_from_skb), in
1049*4882a593Smuzhiyun * many cases this function simply updates iov_base pointers in
1050*4882a593Smuzhiyun * rq_rcv_buf to point directly to the received reply data, to
1051*4882a593Smuzhiyun * avoid copying reply data.
1052*4882a593Smuzhiyun *
1053*4882a593Smuzhiyun * Returns the count of bytes which had to be memcopied.
1054*4882a593Smuzhiyun */
1055*4882a593Smuzhiyun static unsigned long
rpcrdma_inline_fixup(struct rpc_rqst * rqst,char * srcp,int copy_len,int pad)1056*4882a593Smuzhiyun rpcrdma_inline_fixup(struct rpc_rqst *rqst, char *srcp, int copy_len, int pad)
1057*4882a593Smuzhiyun {
1058*4882a593Smuzhiyun unsigned long fixup_copy_count;
1059*4882a593Smuzhiyun int i, npages, curlen;
1060*4882a593Smuzhiyun char *destp;
1061*4882a593Smuzhiyun struct page **ppages;
1062*4882a593Smuzhiyun int page_base;
1063*4882a593Smuzhiyun
1064*4882a593Smuzhiyun /* The head iovec is redirected to the RPC reply message
1065*4882a593Smuzhiyun * in the receive buffer, to avoid a memcopy.
1066*4882a593Smuzhiyun */
1067*4882a593Smuzhiyun rqst->rq_rcv_buf.head[0].iov_base = srcp;
1068*4882a593Smuzhiyun rqst->rq_private_buf.head[0].iov_base = srcp;
1069*4882a593Smuzhiyun
1070*4882a593Smuzhiyun /* The contents of the receive buffer that follow
1071*4882a593Smuzhiyun * head.iov_len bytes are copied into the page list.
1072*4882a593Smuzhiyun */
1073*4882a593Smuzhiyun curlen = rqst->rq_rcv_buf.head[0].iov_len;
1074*4882a593Smuzhiyun if (curlen > copy_len)
1075*4882a593Smuzhiyun curlen = copy_len;
1076*4882a593Smuzhiyun srcp += curlen;
1077*4882a593Smuzhiyun copy_len -= curlen;
1078*4882a593Smuzhiyun
1079*4882a593Smuzhiyun ppages = rqst->rq_rcv_buf.pages +
1080*4882a593Smuzhiyun (rqst->rq_rcv_buf.page_base >> PAGE_SHIFT);
1081*4882a593Smuzhiyun page_base = offset_in_page(rqst->rq_rcv_buf.page_base);
1082*4882a593Smuzhiyun fixup_copy_count = 0;
1083*4882a593Smuzhiyun if (copy_len && rqst->rq_rcv_buf.page_len) {
1084*4882a593Smuzhiyun int pagelist_len;
1085*4882a593Smuzhiyun
1086*4882a593Smuzhiyun pagelist_len = rqst->rq_rcv_buf.page_len;
1087*4882a593Smuzhiyun if (pagelist_len > copy_len)
1088*4882a593Smuzhiyun pagelist_len = copy_len;
1089*4882a593Smuzhiyun npages = PAGE_ALIGN(page_base + pagelist_len) >> PAGE_SHIFT;
1090*4882a593Smuzhiyun for (i = 0; i < npages; i++) {
1091*4882a593Smuzhiyun curlen = PAGE_SIZE - page_base;
1092*4882a593Smuzhiyun if (curlen > pagelist_len)
1093*4882a593Smuzhiyun curlen = pagelist_len;
1094*4882a593Smuzhiyun
1095*4882a593Smuzhiyun destp = kmap_atomic(ppages[i]);
1096*4882a593Smuzhiyun memcpy(destp + page_base, srcp, curlen);
1097*4882a593Smuzhiyun flush_dcache_page(ppages[i]);
1098*4882a593Smuzhiyun kunmap_atomic(destp);
1099*4882a593Smuzhiyun srcp += curlen;
1100*4882a593Smuzhiyun copy_len -= curlen;
1101*4882a593Smuzhiyun fixup_copy_count += curlen;
1102*4882a593Smuzhiyun pagelist_len -= curlen;
1103*4882a593Smuzhiyun if (!pagelist_len)
1104*4882a593Smuzhiyun break;
1105*4882a593Smuzhiyun page_base = 0;
1106*4882a593Smuzhiyun }
1107*4882a593Smuzhiyun
1108*4882a593Smuzhiyun /* Implicit padding for the last segment in a Write
1109*4882a593Smuzhiyun * chunk is inserted inline at the front of the tail
1110*4882a593Smuzhiyun * iovec. The upper layer ignores the content of
1111*4882a593Smuzhiyun * the pad. Simply ensure inline content in the tail
1112*4882a593Smuzhiyun * that follows the Write chunk is properly aligned.
1113*4882a593Smuzhiyun */
1114*4882a593Smuzhiyun if (pad)
1115*4882a593Smuzhiyun srcp -= pad;
1116*4882a593Smuzhiyun }
1117*4882a593Smuzhiyun
1118*4882a593Smuzhiyun /* The tail iovec is redirected to the remaining data
1119*4882a593Smuzhiyun * in the receive buffer, to avoid a memcopy.
1120*4882a593Smuzhiyun */
1121*4882a593Smuzhiyun if (copy_len || pad) {
1122*4882a593Smuzhiyun rqst->rq_rcv_buf.tail[0].iov_base = srcp;
1123*4882a593Smuzhiyun rqst->rq_private_buf.tail[0].iov_base = srcp;
1124*4882a593Smuzhiyun }
1125*4882a593Smuzhiyun
1126*4882a593Smuzhiyun if (fixup_copy_count)
1127*4882a593Smuzhiyun trace_xprtrdma_fixup(rqst, fixup_copy_count);
1128*4882a593Smuzhiyun return fixup_copy_count;
1129*4882a593Smuzhiyun }
1130*4882a593Smuzhiyun
1131*4882a593Smuzhiyun /* By convention, backchannel calls arrive via rdma_msg type
1132*4882a593Smuzhiyun * messages, and never populate the chunk lists. This makes
1133*4882a593Smuzhiyun * the RPC/RDMA header small and fixed in size, so it is
1134*4882a593Smuzhiyun * straightforward to check the RPC header's direction field.
1135*4882a593Smuzhiyun */
1136*4882a593Smuzhiyun static bool
rpcrdma_is_bcall(struct rpcrdma_xprt * r_xprt,struct rpcrdma_rep * rep)1137*4882a593Smuzhiyun rpcrdma_is_bcall(struct rpcrdma_xprt *r_xprt, struct rpcrdma_rep *rep)
1138*4882a593Smuzhiyun #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1139*4882a593Smuzhiyun {
1140*4882a593Smuzhiyun struct rpc_xprt *xprt = &r_xprt->rx_xprt;
1141*4882a593Smuzhiyun struct xdr_stream *xdr = &rep->rr_stream;
1142*4882a593Smuzhiyun __be32 *p;
1143*4882a593Smuzhiyun
1144*4882a593Smuzhiyun if (rep->rr_proc != rdma_msg)
1145*4882a593Smuzhiyun return false;
1146*4882a593Smuzhiyun
1147*4882a593Smuzhiyun /* Peek at stream contents without advancing. */
1148*4882a593Smuzhiyun p = xdr_inline_decode(xdr, 0);
1149*4882a593Smuzhiyun
1150*4882a593Smuzhiyun /* Chunk lists */
1151*4882a593Smuzhiyun if (xdr_item_is_present(p++))
1152*4882a593Smuzhiyun return false;
1153*4882a593Smuzhiyun if (xdr_item_is_present(p++))
1154*4882a593Smuzhiyun return false;
1155*4882a593Smuzhiyun if (xdr_item_is_present(p++))
1156*4882a593Smuzhiyun return false;
1157*4882a593Smuzhiyun
1158*4882a593Smuzhiyun /* RPC header */
1159*4882a593Smuzhiyun if (*p++ != rep->rr_xid)
1160*4882a593Smuzhiyun return false;
1161*4882a593Smuzhiyun if (*p != cpu_to_be32(RPC_CALL))
1162*4882a593Smuzhiyun return false;
1163*4882a593Smuzhiyun
1164*4882a593Smuzhiyun /* No bc service. */
1165*4882a593Smuzhiyun if (xprt->bc_serv == NULL)
1166*4882a593Smuzhiyun return false;
1167*4882a593Smuzhiyun
1168*4882a593Smuzhiyun /* Now that we are sure this is a backchannel call,
1169*4882a593Smuzhiyun * advance to the RPC header.
1170*4882a593Smuzhiyun */
1171*4882a593Smuzhiyun p = xdr_inline_decode(xdr, 3 * sizeof(*p));
1172*4882a593Smuzhiyun if (unlikely(!p))
1173*4882a593Smuzhiyun goto out_short;
1174*4882a593Smuzhiyun
1175*4882a593Smuzhiyun rpcrdma_bc_receive_call(r_xprt, rep);
1176*4882a593Smuzhiyun return true;
1177*4882a593Smuzhiyun
1178*4882a593Smuzhiyun out_short:
1179*4882a593Smuzhiyun pr_warn("RPC/RDMA short backward direction call\n");
1180*4882a593Smuzhiyun return true;
1181*4882a593Smuzhiyun }
1182*4882a593Smuzhiyun #else /* CONFIG_SUNRPC_BACKCHANNEL */
1183*4882a593Smuzhiyun {
1184*4882a593Smuzhiyun return false;
1185*4882a593Smuzhiyun }
1186*4882a593Smuzhiyun #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1187*4882a593Smuzhiyun
decode_rdma_segment(struct xdr_stream * xdr,u32 * length)1188*4882a593Smuzhiyun static int decode_rdma_segment(struct xdr_stream *xdr, u32 *length)
1189*4882a593Smuzhiyun {
1190*4882a593Smuzhiyun u32 handle;
1191*4882a593Smuzhiyun u64 offset;
1192*4882a593Smuzhiyun __be32 *p;
1193*4882a593Smuzhiyun
1194*4882a593Smuzhiyun p = xdr_inline_decode(xdr, 4 * sizeof(*p));
1195*4882a593Smuzhiyun if (unlikely(!p))
1196*4882a593Smuzhiyun return -EIO;
1197*4882a593Smuzhiyun
1198*4882a593Smuzhiyun xdr_decode_rdma_segment(p, &handle, length, &offset);
1199*4882a593Smuzhiyun trace_xprtrdma_decode_seg(handle, *length, offset);
1200*4882a593Smuzhiyun return 0;
1201*4882a593Smuzhiyun }
1202*4882a593Smuzhiyun
decode_write_chunk(struct xdr_stream * xdr,u32 * length)1203*4882a593Smuzhiyun static int decode_write_chunk(struct xdr_stream *xdr, u32 *length)
1204*4882a593Smuzhiyun {
1205*4882a593Smuzhiyun u32 segcount, seglength;
1206*4882a593Smuzhiyun __be32 *p;
1207*4882a593Smuzhiyun
1208*4882a593Smuzhiyun p = xdr_inline_decode(xdr, sizeof(*p));
1209*4882a593Smuzhiyun if (unlikely(!p))
1210*4882a593Smuzhiyun return -EIO;
1211*4882a593Smuzhiyun
1212*4882a593Smuzhiyun *length = 0;
1213*4882a593Smuzhiyun segcount = be32_to_cpup(p);
1214*4882a593Smuzhiyun while (segcount--) {
1215*4882a593Smuzhiyun if (decode_rdma_segment(xdr, &seglength))
1216*4882a593Smuzhiyun return -EIO;
1217*4882a593Smuzhiyun *length += seglength;
1218*4882a593Smuzhiyun }
1219*4882a593Smuzhiyun
1220*4882a593Smuzhiyun return 0;
1221*4882a593Smuzhiyun }
1222*4882a593Smuzhiyun
1223*4882a593Smuzhiyun /* In RPC-over-RDMA Version One replies, a Read list is never
1224*4882a593Smuzhiyun * expected. This decoder is a stub that returns an error if
1225*4882a593Smuzhiyun * a Read list is present.
1226*4882a593Smuzhiyun */
decode_read_list(struct xdr_stream * xdr)1227*4882a593Smuzhiyun static int decode_read_list(struct xdr_stream *xdr)
1228*4882a593Smuzhiyun {
1229*4882a593Smuzhiyun __be32 *p;
1230*4882a593Smuzhiyun
1231*4882a593Smuzhiyun p = xdr_inline_decode(xdr, sizeof(*p));
1232*4882a593Smuzhiyun if (unlikely(!p))
1233*4882a593Smuzhiyun return -EIO;
1234*4882a593Smuzhiyun if (unlikely(xdr_item_is_present(p)))
1235*4882a593Smuzhiyun return -EIO;
1236*4882a593Smuzhiyun return 0;
1237*4882a593Smuzhiyun }
1238*4882a593Smuzhiyun
1239*4882a593Smuzhiyun /* Supports only one Write chunk in the Write list
1240*4882a593Smuzhiyun */
decode_write_list(struct xdr_stream * xdr,u32 * length)1241*4882a593Smuzhiyun static int decode_write_list(struct xdr_stream *xdr, u32 *length)
1242*4882a593Smuzhiyun {
1243*4882a593Smuzhiyun u32 chunklen;
1244*4882a593Smuzhiyun bool first;
1245*4882a593Smuzhiyun __be32 *p;
1246*4882a593Smuzhiyun
1247*4882a593Smuzhiyun *length = 0;
1248*4882a593Smuzhiyun first = true;
1249*4882a593Smuzhiyun do {
1250*4882a593Smuzhiyun p = xdr_inline_decode(xdr, sizeof(*p));
1251*4882a593Smuzhiyun if (unlikely(!p))
1252*4882a593Smuzhiyun return -EIO;
1253*4882a593Smuzhiyun if (xdr_item_is_absent(p))
1254*4882a593Smuzhiyun break;
1255*4882a593Smuzhiyun if (!first)
1256*4882a593Smuzhiyun return -EIO;
1257*4882a593Smuzhiyun
1258*4882a593Smuzhiyun if (decode_write_chunk(xdr, &chunklen))
1259*4882a593Smuzhiyun return -EIO;
1260*4882a593Smuzhiyun *length += chunklen;
1261*4882a593Smuzhiyun first = false;
1262*4882a593Smuzhiyun } while (true);
1263*4882a593Smuzhiyun return 0;
1264*4882a593Smuzhiyun }
1265*4882a593Smuzhiyun
decode_reply_chunk(struct xdr_stream * xdr,u32 * length)1266*4882a593Smuzhiyun static int decode_reply_chunk(struct xdr_stream *xdr, u32 *length)
1267*4882a593Smuzhiyun {
1268*4882a593Smuzhiyun __be32 *p;
1269*4882a593Smuzhiyun
1270*4882a593Smuzhiyun p = xdr_inline_decode(xdr, sizeof(*p));
1271*4882a593Smuzhiyun if (unlikely(!p))
1272*4882a593Smuzhiyun return -EIO;
1273*4882a593Smuzhiyun
1274*4882a593Smuzhiyun *length = 0;
1275*4882a593Smuzhiyun if (xdr_item_is_present(p))
1276*4882a593Smuzhiyun if (decode_write_chunk(xdr, length))
1277*4882a593Smuzhiyun return -EIO;
1278*4882a593Smuzhiyun return 0;
1279*4882a593Smuzhiyun }
1280*4882a593Smuzhiyun
1281*4882a593Smuzhiyun static int
rpcrdma_decode_msg(struct rpcrdma_xprt * r_xprt,struct rpcrdma_rep * rep,struct rpc_rqst * rqst)1282*4882a593Smuzhiyun rpcrdma_decode_msg(struct rpcrdma_xprt *r_xprt, struct rpcrdma_rep *rep,
1283*4882a593Smuzhiyun struct rpc_rqst *rqst)
1284*4882a593Smuzhiyun {
1285*4882a593Smuzhiyun struct xdr_stream *xdr = &rep->rr_stream;
1286*4882a593Smuzhiyun u32 writelist, replychunk, rpclen;
1287*4882a593Smuzhiyun char *base;
1288*4882a593Smuzhiyun
1289*4882a593Smuzhiyun /* Decode the chunk lists */
1290*4882a593Smuzhiyun if (decode_read_list(xdr))
1291*4882a593Smuzhiyun return -EIO;
1292*4882a593Smuzhiyun if (decode_write_list(xdr, &writelist))
1293*4882a593Smuzhiyun return -EIO;
1294*4882a593Smuzhiyun if (decode_reply_chunk(xdr, &replychunk))
1295*4882a593Smuzhiyun return -EIO;
1296*4882a593Smuzhiyun
1297*4882a593Smuzhiyun /* RDMA_MSG sanity checks */
1298*4882a593Smuzhiyun if (unlikely(replychunk))
1299*4882a593Smuzhiyun return -EIO;
1300*4882a593Smuzhiyun
1301*4882a593Smuzhiyun /* Build the RPC reply's Payload stream in rqst->rq_rcv_buf */
1302*4882a593Smuzhiyun base = (char *)xdr_inline_decode(xdr, 0);
1303*4882a593Smuzhiyun rpclen = xdr_stream_remaining(xdr);
1304*4882a593Smuzhiyun r_xprt->rx_stats.fixup_copy_count +=
1305*4882a593Smuzhiyun rpcrdma_inline_fixup(rqst, base, rpclen, writelist & 3);
1306*4882a593Smuzhiyun
1307*4882a593Smuzhiyun r_xprt->rx_stats.total_rdma_reply += writelist;
1308*4882a593Smuzhiyun return rpclen + xdr_align_size(writelist);
1309*4882a593Smuzhiyun }
1310*4882a593Smuzhiyun
1311*4882a593Smuzhiyun static noinline int
rpcrdma_decode_nomsg(struct rpcrdma_xprt * r_xprt,struct rpcrdma_rep * rep)1312*4882a593Smuzhiyun rpcrdma_decode_nomsg(struct rpcrdma_xprt *r_xprt, struct rpcrdma_rep *rep)
1313*4882a593Smuzhiyun {
1314*4882a593Smuzhiyun struct xdr_stream *xdr = &rep->rr_stream;
1315*4882a593Smuzhiyun u32 writelist, replychunk;
1316*4882a593Smuzhiyun
1317*4882a593Smuzhiyun /* Decode the chunk lists */
1318*4882a593Smuzhiyun if (decode_read_list(xdr))
1319*4882a593Smuzhiyun return -EIO;
1320*4882a593Smuzhiyun if (decode_write_list(xdr, &writelist))
1321*4882a593Smuzhiyun return -EIO;
1322*4882a593Smuzhiyun if (decode_reply_chunk(xdr, &replychunk))
1323*4882a593Smuzhiyun return -EIO;
1324*4882a593Smuzhiyun
1325*4882a593Smuzhiyun /* RDMA_NOMSG sanity checks */
1326*4882a593Smuzhiyun if (unlikely(writelist))
1327*4882a593Smuzhiyun return -EIO;
1328*4882a593Smuzhiyun if (unlikely(!replychunk))
1329*4882a593Smuzhiyun return -EIO;
1330*4882a593Smuzhiyun
1331*4882a593Smuzhiyun /* Reply chunk buffer already is the reply vector */
1332*4882a593Smuzhiyun r_xprt->rx_stats.total_rdma_reply += replychunk;
1333*4882a593Smuzhiyun return replychunk;
1334*4882a593Smuzhiyun }
1335*4882a593Smuzhiyun
1336*4882a593Smuzhiyun static noinline int
rpcrdma_decode_error(struct rpcrdma_xprt * r_xprt,struct rpcrdma_rep * rep,struct rpc_rqst * rqst)1337*4882a593Smuzhiyun rpcrdma_decode_error(struct rpcrdma_xprt *r_xprt, struct rpcrdma_rep *rep,
1338*4882a593Smuzhiyun struct rpc_rqst *rqst)
1339*4882a593Smuzhiyun {
1340*4882a593Smuzhiyun struct xdr_stream *xdr = &rep->rr_stream;
1341*4882a593Smuzhiyun __be32 *p;
1342*4882a593Smuzhiyun
1343*4882a593Smuzhiyun p = xdr_inline_decode(xdr, sizeof(*p));
1344*4882a593Smuzhiyun if (unlikely(!p))
1345*4882a593Smuzhiyun return -EIO;
1346*4882a593Smuzhiyun
1347*4882a593Smuzhiyun switch (*p) {
1348*4882a593Smuzhiyun case err_vers:
1349*4882a593Smuzhiyun p = xdr_inline_decode(xdr, 2 * sizeof(*p));
1350*4882a593Smuzhiyun if (!p)
1351*4882a593Smuzhiyun break;
1352*4882a593Smuzhiyun dprintk("RPC: %s: server reports "
1353*4882a593Smuzhiyun "version error (%u-%u), xid %08x\n", __func__,
1354*4882a593Smuzhiyun be32_to_cpup(p), be32_to_cpu(*(p + 1)),
1355*4882a593Smuzhiyun be32_to_cpu(rep->rr_xid));
1356*4882a593Smuzhiyun break;
1357*4882a593Smuzhiyun case err_chunk:
1358*4882a593Smuzhiyun dprintk("RPC: %s: server reports "
1359*4882a593Smuzhiyun "header decoding error, xid %08x\n", __func__,
1360*4882a593Smuzhiyun be32_to_cpu(rep->rr_xid));
1361*4882a593Smuzhiyun break;
1362*4882a593Smuzhiyun default:
1363*4882a593Smuzhiyun dprintk("RPC: %s: server reports "
1364*4882a593Smuzhiyun "unrecognized error %d, xid %08x\n", __func__,
1365*4882a593Smuzhiyun be32_to_cpup(p), be32_to_cpu(rep->rr_xid));
1366*4882a593Smuzhiyun }
1367*4882a593Smuzhiyun
1368*4882a593Smuzhiyun return -EIO;
1369*4882a593Smuzhiyun }
1370*4882a593Smuzhiyun
1371*4882a593Smuzhiyun /* Perform XID lookup, reconstruction of the RPC reply, and
1372*4882a593Smuzhiyun * RPC completion while holding the transport lock to ensure
1373*4882a593Smuzhiyun * the rep, rqst, and rq_task pointers remain stable.
1374*4882a593Smuzhiyun */
rpcrdma_complete_rqst(struct rpcrdma_rep * rep)1375*4882a593Smuzhiyun void rpcrdma_complete_rqst(struct rpcrdma_rep *rep)
1376*4882a593Smuzhiyun {
1377*4882a593Smuzhiyun struct rpcrdma_xprt *r_xprt = rep->rr_rxprt;
1378*4882a593Smuzhiyun struct rpc_xprt *xprt = &r_xprt->rx_xprt;
1379*4882a593Smuzhiyun struct rpc_rqst *rqst = rep->rr_rqst;
1380*4882a593Smuzhiyun int status;
1381*4882a593Smuzhiyun
1382*4882a593Smuzhiyun switch (rep->rr_proc) {
1383*4882a593Smuzhiyun case rdma_msg:
1384*4882a593Smuzhiyun status = rpcrdma_decode_msg(r_xprt, rep, rqst);
1385*4882a593Smuzhiyun break;
1386*4882a593Smuzhiyun case rdma_nomsg:
1387*4882a593Smuzhiyun status = rpcrdma_decode_nomsg(r_xprt, rep);
1388*4882a593Smuzhiyun break;
1389*4882a593Smuzhiyun case rdma_error:
1390*4882a593Smuzhiyun status = rpcrdma_decode_error(r_xprt, rep, rqst);
1391*4882a593Smuzhiyun break;
1392*4882a593Smuzhiyun default:
1393*4882a593Smuzhiyun status = -EIO;
1394*4882a593Smuzhiyun }
1395*4882a593Smuzhiyun if (status < 0)
1396*4882a593Smuzhiyun goto out_badheader;
1397*4882a593Smuzhiyun
1398*4882a593Smuzhiyun out:
1399*4882a593Smuzhiyun spin_lock(&xprt->queue_lock);
1400*4882a593Smuzhiyun xprt_complete_rqst(rqst->rq_task, status);
1401*4882a593Smuzhiyun xprt_unpin_rqst(rqst);
1402*4882a593Smuzhiyun spin_unlock(&xprt->queue_lock);
1403*4882a593Smuzhiyun return;
1404*4882a593Smuzhiyun
1405*4882a593Smuzhiyun out_badheader:
1406*4882a593Smuzhiyun trace_xprtrdma_reply_hdr(rep);
1407*4882a593Smuzhiyun r_xprt->rx_stats.bad_reply_count++;
1408*4882a593Smuzhiyun rqst->rq_task->tk_status = status;
1409*4882a593Smuzhiyun status = 0;
1410*4882a593Smuzhiyun goto out;
1411*4882a593Smuzhiyun }
1412*4882a593Smuzhiyun
rpcrdma_reply_done(struct kref * kref)1413*4882a593Smuzhiyun static void rpcrdma_reply_done(struct kref *kref)
1414*4882a593Smuzhiyun {
1415*4882a593Smuzhiyun struct rpcrdma_req *req =
1416*4882a593Smuzhiyun container_of(kref, struct rpcrdma_req, rl_kref);
1417*4882a593Smuzhiyun
1418*4882a593Smuzhiyun rpcrdma_complete_rqst(req->rl_reply);
1419*4882a593Smuzhiyun }
1420*4882a593Smuzhiyun
1421*4882a593Smuzhiyun /**
1422*4882a593Smuzhiyun * rpcrdma_reply_handler - Process received RPC/RDMA messages
1423*4882a593Smuzhiyun * @rep: Incoming rpcrdma_rep object to process
1424*4882a593Smuzhiyun *
1425*4882a593Smuzhiyun * Errors must result in the RPC task either being awakened, or
1426*4882a593Smuzhiyun * allowed to timeout, to discover the errors at that time.
1427*4882a593Smuzhiyun */
rpcrdma_reply_handler(struct rpcrdma_rep * rep)1428*4882a593Smuzhiyun void rpcrdma_reply_handler(struct rpcrdma_rep *rep)
1429*4882a593Smuzhiyun {
1430*4882a593Smuzhiyun struct rpcrdma_xprt *r_xprt = rep->rr_rxprt;
1431*4882a593Smuzhiyun struct rpc_xprt *xprt = &r_xprt->rx_xprt;
1432*4882a593Smuzhiyun struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1433*4882a593Smuzhiyun struct rpcrdma_req *req;
1434*4882a593Smuzhiyun struct rpc_rqst *rqst;
1435*4882a593Smuzhiyun u32 credits;
1436*4882a593Smuzhiyun __be32 *p;
1437*4882a593Smuzhiyun
1438*4882a593Smuzhiyun /* Any data means we had a useful conversation, so
1439*4882a593Smuzhiyun * then we don't need to delay the next reconnect.
1440*4882a593Smuzhiyun */
1441*4882a593Smuzhiyun if (xprt->reestablish_timeout)
1442*4882a593Smuzhiyun xprt->reestablish_timeout = 0;
1443*4882a593Smuzhiyun
1444*4882a593Smuzhiyun /* Fixed transport header fields */
1445*4882a593Smuzhiyun xdr_init_decode(&rep->rr_stream, &rep->rr_hdrbuf,
1446*4882a593Smuzhiyun rep->rr_hdrbuf.head[0].iov_base, NULL);
1447*4882a593Smuzhiyun p = xdr_inline_decode(&rep->rr_stream, 4 * sizeof(*p));
1448*4882a593Smuzhiyun if (unlikely(!p))
1449*4882a593Smuzhiyun goto out_shortreply;
1450*4882a593Smuzhiyun rep->rr_xid = *p++;
1451*4882a593Smuzhiyun rep->rr_vers = *p++;
1452*4882a593Smuzhiyun credits = be32_to_cpu(*p++);
1453*4882a593Smuzhiyun rep->rr_proc = *p++;
1454*4882a593Smuzhiyun
1455*4882a593Smuzhiyun if (rep->rr_vers != rpcrdma_version)
1456*4882a593Smuzhiyun goto out_badversion;
1457*4882a593Smuzhiyun
1458*4882a593Smuzhiyun if (rpcrdma_is_bcall(r_xprt, rep))
1459*4882a593Smuzhiyun return;
1460*4882a593Smuzhiyun
1461*4882a593Smuzhiyun /* Match incoming rpcrdma_rep to an rpcrdma_req to
1462*4882a593Smuzhiyun * get context for handling any incoming chunks.
1463*4882a593Smuzhiyun */
1464*4882a593Smuzhiyun spin_lock(&xprt->queue_lock);
1465*4882a593Smuzhiyun rqst = xprt_lookup_rqst(xprt, rep->rr_xid);
1466*4882a593Smuzhiyun if (!rqst)
1467*4882a593Smuzhiyun goto out_norqst;
1468*4882a593Smuzhiyun xprt_pin_rqst(rqst);
1469*4882a593Smuzhiyun spin_unlock(&xprt->queue_lock);
1470*4882a593Smuzhiyun
1471*4882a593Smuzhiyun if (credits == 0)
1472*4882a593Smuzhiyun credits = 1; /* don't deadlock */
1473*4882a593Smuzhiyun else if (credits > r_xprt->rx_ep->re_max_requests)
1474*4882a593Smuzhiyun credits = r_xprt->rx_ep->re_max_requests;
1475*4882a593Smuzhiyun rpcrdma_post_recvs(r_xprt, credits + (buf->rb_bc_srv_max_requests << 1),
1476*4882a593Smuzhiyun false);
1477*4882a593Smuzhiyun if (buf->rb_credits != credits)
1478*4882a593Smuzhiyun rpcrdma_update_cwnd(r_xprt, credits);
1479*4882a593Smuzhiyun
1480*4882a593Smuzhiyun req = rpcr_to_rdmar(rqst);
1481*4882a593Smuzhiyun if (req->rl_reply) {
1482*4882a593Smuzhiyun trace_xprtrdma_leaked_rep(rqst, req->rl_reply);
1483*4882a593Smuzhiyun rpcrdma_recv_buffer_put(req->rl_reply);
1484*4882a593Smuzhiyun }
1485*4882a593Smuzhiyun req->rl_reply = rep;
1486*4882a593Smuzhiyun rep->rr_rqst = rqst;
1487*4882a593Smuzhiyun
1488*4882a593Smuzhiyun trace_xprtrdma_reply(rqst->rq_task, rep, req, credits);
1489*4882a593Smuzhiyun
1490*4882a593Smuzhiyun if (rep->rr_wc_flags & IB_WC_WITH_INVALIDATE)
1491*4882a593Smuzhiyun frwr_reminv(rep, &req->rl_registered);
1492*4882a593Smuzhiyun if (!list_empty(&req->rl_registered))
1493*4882a593Smuzhiyun frwr_unmap_async(r_xprt, req);
1494*4882a593Smuzhiyun /* LocalInv completion will complete the RPC */
1495*4882a593Smuzhiyun else
1496*4882a593Smuzhiyun kref_put(&req->rl_kref, rpcrdma_reply_done);
1497*4882a593Smuzhiyun return;
1498*4882a593Smuzhiyun
1499*4882a593Smuzhiyun out_badversion:
1500*4882a593Smuzhiyun trace_xprtrdma_reply_vers(rep);
1501*4882a593Smuzhiyun goto out;
1502*4882a593Smuzhiyun
1503*4882a593Smuzhiyun out_norqst:
1504*4882a593Smuzhiyun spin_unlock(&xprt->queue_lock);
1505*4882a593Smuzhiyun trace_xprtrdma_reply_rqst(rep);
1506*4882a593Smuzhiyun goto out;
1507*4882a593Smuzhiyun
1508*4882a593Smuzhiyun out_shortreply:
1509*4882a593Smuzhiyun trace_xprtrdma_reply_short(rep);
1510*4882a593Smuzhiyun
1511*4882a593Smuzhiyun out:
1512*4882a593Smuzhiyun rpcrdma_recv_buffer_put(rep);
1513*4882a593Smuzhiyun }
1514