blob: 31a434d2f14360691d1eb07009165d667eb34bcc [file] [log] [blame]
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -04001/*
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -04002 * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the BSD-type
8 * license below:
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials provided
20 * with the distribution.
21 *
22 * Neither the name of the Network Appliance, Inc. nor the names of
23 * its contributors may be used to endorse or promote products
24 * derived from this software without specific prior written
25 * permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39
40/*
41 * rpc_rdma.c
42 *
43 * This file contains the guts of the RPC RDMA protocol, and
44 * does marshaling/unmarshaling, etc. It is also where interfacing
45 * to the Linux RPC framework lives.
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040046 */
47
48#include "xprt_rdma.h"
49
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -040050#include <linux/highmem.h>
51
Jeff Laytonf895b252014-11-17 16:58:04 -050052#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -040053# define RPCDBG_FACILITY RPCDBG_TRANS
54#endif
55
Chuck Levere2377942015-03-30 14:33:53 -040056enum rpcrdma_chunktype {
57 rpcrdma_noch = 0,
58 rpcrdma_readch,
59 rpcrdma_areadch,
60 rpcrdma_writech,
61 rpcrdma_replych
62};
63
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -040064static const char transfertypes[][12] = {
Chuck Lever94f58c52016-05-02 14:41:30 -040065 "inline", /* no chunks */
66 "read list", /* some argument via rdma read */
67 "*read list", /* entire request via rdma read */
68 "write list", /* some result via rdma write */
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -040069 "reply chunk" /* entire reply via rdma write */
70};
Chuck Lever302d3de2016-05-02 14:41:05 -040071
72/* Returns size of largest RPC-over-RDMA header in a Call message
73 *
Chuck Lever94f58c52016-05-02 14:41:30 -040074 * The largest Call header contains a full-size Read list and a
75 * minimal Reply chunk.
Chuck Lever302d3de2016-05-02 14:41:05 -040076 */
77static unsigned int rpcrdma_max_call_header_size(unsigned int maxsegs)
78{
79 unsigned int size;
80
81 /* Fixed header fields and list discriminators */
82 size = RPCRDMA_HDRLEN_MIN;
83
84 /* Maximum Read list size */
85 maxsegs += 2; /* segment for head and tail buffers */
86 size = maxsegs * sizeof(struct rpcrdma_read_chunk);
87
Chuck Lever94f58c52016-05-02 14:41:30 -040088 /* Minimal Read chunk size */
89 size += sizeof(__be32); /* segment count */
90 size += sizeof(struct rpcrdma_segment);
91 size += sizeof(__be32); /* list discriminator */
92
Chuck Lever302d3de2016-05-02 14:41:05 -040093 dprintk("RPC: %s: max call header size = %u\n",
94 __func__, size);
95 return size;
96}
97
98/* Returns size of largest RPC-over-RDMA header in a Reply message
99 *
100 * There is only one Write list or one Reply chunk per Reply
101 * message. The larger list is the Write list.
102 */
103static unsigned int rpcrdma_max_reply_header_size(unsigned int maxsegs)
104{
105 unsigned int size;
106
107 /* Fixed header fields and list discriminators */
108 size = RPCRDMA_HDRLEN_MIN;
109
110 /* Maximum Write list size */
111 maxsegs += 2; /* segment for head and tail buffers */
112 size = sizeof(__be32); /* segment count */
113 size += maxsegs * sizeof(struct rpcrdma_segment);
114 size += sizeof(__be32); /* list discriminator */
115
116 dprintk("RPC: %s: max reply header size = %u\n",
117 __func__, size);
118 return size;
119}
120
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400121void rpcrdma_set_max_header_sizes(struct rpcrdma_xprt *r_xprt)
Chuck Lever302d3de2016-05-02 14:41:05 -0400122{
Chuck Lever87cfb9a2016-09-15 10:57:07 -0400123 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
124 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
125 unsigned int maxsegs = ia->ri_max_segs;
126
Chuck Lever302d3de2016-05-02 14:41:05 -0400127 ia->ri_max_inline_write = cdata->inline_wsize -
128 rpcrdma_max_call_header_size(maxsegs);
129 ia->ri_max_inline_read = cdata->inline_rsize -
130 rpcrdma_max_reply_header_size(maxsegs);
131}
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400132
Chuck Lever5457ced2015-08-03 13:03:49 -0400133/* The client can send a request inline as long as the RPCRDMA header
134 * plus the RPC call fit under the transport's inline limit. If the
135 * combined call message size exceeds that limit, the client must use
136 * the read chunk list for this operation.
137 */
Chuck Lever302d3de2016-05-02 14:41:05 -0400138static bool rpcrdma_args_inline(struct rpcrdma_xprt *r_xprt,
139 struct rpc_rqst *rqst)
Chuck Lever5457ced2015-08-03 13:03:49 -0400140{
Chuck Lever302d3de2016-05-02 14:41:05 -0400141 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
Chuck Lever5457ced2015-08-03 13:03:49 -0400142
Chuck Lever302d3de2016-05-02 14:41:05 -0400143 return rqst->rq_snd_buf.len <= ia->ri_max_inline_write;
Chuck Lever5457ced2015-08-03 13:03:49 -0400144}
145
146/* The client can't know how large the actual reply will be. Thus it
147 * plans for the largest possible reply for that particular ULP
148 * operation. If the maximum combined reply message size exceeds that
149 * limit, the client must provide a write list or a reply chunk for
150 * this request.
151 */
Chuck Lever302d3de2016-05-02 14:41:05 -0400152static bool rpcrdma_results_inline(struct rpcrdma_xprt *r_xprt,
153 struct rpc_rqst *rqst)
Chuck Lever5457ced2015-08-03 13:03:49 -0400154{
Chuck Lever302d3de2016-05-02 14:41:05 -0400155 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
Chuck Lever5457ced2015-08-03 13:03:49 -0400156
Chuck Lever302d3de2016-05-02 14:41:05 -0400157 return rqst->rq_rcv_buf.buflen <= ia->ri_max_inline_read;
Chuck Lever5457ced2015-08-03 13:03:49 -0400158}
159
Chuck Lever677eb172015-08-03 13:04:17 -0400160static int
161rpcrdma_tail_pullup(struct xdr_buf *buf)
162{
163 size_t tlen = buf->tail[0].iov_len;
164 size_t skip = tlen & 3;
165
166 /* Do not include the tail if it is only an XDR pad */
167 if (tlen < 4)
168 return 0;
169
170 /* xdr_write_pages() adds a pad at the beginning of the tail
171 * if the content in "buf->pages" is unaligned. Force the
172 * tail's actual content to land at the next XDR position
173 * after the head instead.
174 */
175 if (skip) {
176 unsigned char *src, *dst;
177 unsigned int count;
178
179 src = buf->tail[0].iov_base;
180 dst = buf->head[0].iov_base;
181 dst += buf->head[0].iov_len;
182
183 src += skip;
184 tlen -= skip;
185
186 dprintk("RPC: %s: skip=%zu, memmove(%p, %p, %zu)\n",
187 __func__, skip, dst, src, tlen);
188
189 for (count = tlen; count; count--)
190 *dst++ = *src++;
191 }
192
193 return tlen;
194}
195
Chuck Lever821c7912016-03-04 11:27:52 -0500196/* Split "vec" on page boundaries into segments. FMR registers pages,
197 * not a byte range. Other modes coalesce these segments into a single
198 * MR when they can.
199 */
200static int
Chuck Lever5ab81422016-06-29 13:54:25 -0400201rpcrdma_convert_kvec(struct kvec *vec, struct rpcrdma_mr_seg *seg, int n)
Chuck Lever821c7912016-03-04 11:27:52 -0500202{
203 size_t page_offset;
204 u32 remaining;
205 char *base;
206
207 base = vec->iov_base;
208 page_offset = offset_in_page(base);
209 remaining = vec->iov_len;
Chuck Lever5ab81422016-06-29 13:54:25 -0400210 while (remaining && n < RPCRDMA_MAX_SEGS) {
Chuck Lever821c7912016-03-04 11:27:52 -0500211 seg[n].mr_page = NULL;
212 seg[n].mr_offset = base;
213 seg[n].mr_len = min_t(u32, PAGE_SIZE - page_offset, remaining);
214 remaining -= seg[n].mr_len;
215 base += seg[n].mr_len;
216 ++n;
217 page_offset = 0;
218 }
219 return n;
220}
221
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400222/*
223 * Chunk assembly from upper layer xdr_buf.
224 *
225 * Prepare the passed-in xdr_buf into representation as RPC/RDMA chunk
226 * elements. Segments are then coalesced when registered, if possible
227 * within the selected memreg mode.
Chuck Leverc93c6222014-05-28 10:35:14 -0400228 *
229 * Returns positive number of segments converted, or a negative errno.
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400230 */
231
232static int
Chuck Lever2a428b22007-10-26 13:30:43 -0400233rpcrdma_convert_iovs(struct xdr_buf *xdrbuf, unsigned int pos,
Chuck Leverc8b920b2016-09-15 10:57:16 -0400234 enum rpcrdma_chunktype type, struct rpcrdma_mr_seg *seg,
235 bool reminv_expected)
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400236{
Chuck Lever5ab81422016-06-29 13:54:25 -0400237 int len, n, p, page_base;
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000238 struct page **ppages;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400239
Chuck Lever5ab81422016-06-29 13:54:25 -0400240 n = 0;
Chuck Lever821c7912016-03-04 11:27:52 -0500241 if (pos == 0) {
Chuck Lever5ab81422016-06-29 13:54:25 -0400242 n = rpcrdma_convert_kvec(&xdrbuf->head[0], seg, n);
243 if (n == RPCRDMA_MAX_SEGS)
244 goto out_overflow;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400245 }
246
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000247 len = xdrbuf->page_len;
248 ppages = xdrbuf->pages + (xdrbuf->page_base >> PAGE_SHIFT);
249 page_base = xdrbuf->page_base & ~PAGE_MASK;
250 p = 0;
Chuck Lever5ab81422016-06-29 13:54:25 -0400251 while (len && n < RPCRDMA_MAX_SEGS) {
Shirley Ma196c6992014-05-28 10:34:24 -0400252 if (!ppages[p]) {
253 /* alloc the pagelist for receiving buffer */
254 ppages[p] = alloc_page(GFP_ATOMIC);
255 if (!ppages[p])
Chuck Lever7a89f9c2016-06-29 13:53:43 -0400256 return -EAGAIN;
Shirley Ma196c6992014-05-28 10:34:24 -0400257 }
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000258 seg[n].mr_page = ppages[p];
259 seg[n].mr_offset = (void *)(unsigned long) page_base;
260 seg[n].mr_len = min_t(u32, PAGE_SIZE - page_base, len);
Chuck Leverc93c6222014-05-28 10:35:14 -0400261 if (seg[n].mr_len > PAGE_SIZE)
Chuck Lever5ab81422016-06-29 13:54:25 -0400262 goto out_overflow;
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000263 len -= seg[n].mr_len;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400264 ++n;
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000265 ++p;
266 page_base = 0; /* page offset only applies to first page */
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400267 }
268
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000269 /* Message overflows the seg array */
Chuck Lever5ab81422016-06-29 13:54:25 -0400270 if (len && n == RPCRDMA_MAX_SEGS)
271 goto out_overflow;
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000272
Chuck Lever677eb172015-08-03 13:04:17 -0400273 /* When encoding the read list, the tail is always sent inline */
274 if (type == rpcrdma_readch)
275 return n;
276
Chuck Leverc8b920b2016-09-15 10:57:16 -0400277 /* When encoding the Write list, some servers need to see an extra
278 * segment for odd-length Write chunks. The upper layer provides
279 * space in the tail iovec for this purpose.
280 */
281 if (type == rpcrdma_writech && reminv_expected)
282 return n;
283
James Lentini50e10922007-12-10 11:24:48 -0500284 if (xdrbuf->tail[0].iov_len) {
Tom Talpey9191ca32008-10-09 15:01:11 -0400285 /* the rpcrdma protocol allows us to omit any trailing
286 * xdr pad bytes, saving the server an RDMA operation. */
287 if (xdrbuf->tail[0].iov_len < 4 && xprt_rdma_pad_optimize)
288 return n;
Chuck Lever5ab81422016-06-29 13:54:25 -0400289 n = rpcrdma_convert_kvec(&xdrbuf->tail[0], seg, n);
290 if (n == RPCRDMA_MAX_SEGS)
291 goto out_overflow;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400292 }
293
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400294 return n;
Chuck Lever5ab81422016-06-29 13:54:25 -0400295
296out_overflow:
297 pr_err("rpcrdma: segment array overflow\n");
298 return -EIO;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400299}
300
Chuck Lever94f58c52016-05-02 14:41:30 -0400301static inline __be32 *
Chuck Lever9d6b0402016-06-29 13:54:16 -0400302xdr_encode_rdma_segment(__be32 *iptr, struct rpcrdma_mw *mw)
Chuck Lever94f58c52016-05-02 14:41:30 -0400303{
Chuck Lever9d6b0402016-06-29 13:54:16 -0400304 *iptr++ = cpu_to_be32(mw->mw_handle);
305 *iptr++ = cpu_to_be32(mw->mw_length);
306 return xdr_encode_hyper(iptr, mw->mw_offset);
Chuck Lever94f58c52016-05-02 14:41:30 -0400307}
308
309/* XDR-encode the Read list. Supports encoding a list of read
310 * segments that belong to a single read chunk.
311 *
312 * Encoding key for single-list chunks (HLOO = Handle32 Length32 Offset64):
313 *
314 * Read chunklist (a linked list):
315 * N elements, position P (same P for all chunks of same arg!):
316 * 1 - PHLOO - 1 - PHLOO - ... - 1 - PHLOO - 0
317 *
318 * Returns a pointer to the XDR word in the RDMA header following
319 * the end of the Read list, or an error pointer.
320 */
321static __be32 *
322rpcrdma_encode_read_list(struct rpcrdma_xprt *r_xprt,
323 struct rpcrdma_req *req, struct rpc_rqst *rqst,
324 __be32 *iptr, enum rpcrdma_chunktype rtype)
325{
Chuck Lever5ab81422016-06-29 13:54:25 -0400326 struct rpcrdma_mr_seg *seg;
Chuck Lever9d6b0402016-06-29 13:54:16 -0400327 struct rpcrdma_mw *mw;
Chuck Lever94f58c52016-05-02 14:41:30 -0400328 unsigned int pos;
329 int n, nsegs;
330
331 if (rtype == rpcrdma_noch) {
332 *iptr++ = xdr_zero; /* item not present */
333 return iptr;
334 }
335
336 pos = rqst->rq_snd_buf.head[0].iov_len;
337 if (rtype == rpcrdma_areadch)
338 pos = 0;
Chuck Lever5ab81422016-06-29 13:54:25 -0400339 seg = req->rl_segments;
Chuck Leverc8b920b2016-09-15 10:57:16 -0400340 nsegs = rpcrdma_convert_iovs(&rqst->rq_snd_buf, pos, rtype, seg, false);
Chuck Lever94f58c52016-05-02 14:41:30 -0400341 if (nsegs < 0)
342 return ERR_PTR(nsegs);
343
344 do {
Chuck Lever9d6b0402016-06-29 13:54:16 -0400345 n = r_xprt->rx_ia.ri_ops->ro_map(r_xprt, seg, nsegs,
346 false, &mw);
Chuck Levera54d4052016-06-29 13:53:52 -0400347 if (n < 0)
Chuck Lever94f58c52016-05-02 14:41:30 -0400348 return ERR_PTR(n);
Chuck Lever9d6b0402016-06-29 13:54:16 -0400349 list_add(&mw->mw_list, &req->rl_registered);
Chuck Lever94f58c52016-05-02 14:41:30 -0400350
351 *iptr++ = xdr_one; /* item present */
352
353 /* All read segments in this chunk
354 * have the same "position".
355 */
356 *iptr++ = cpu_to_be32(pos);
Chuck Lever9d6b0402016-06-29 13:54:16 -0400357 iptr = xdr_encode_rdma_segment(iptr, mw);
Chuck Lever94f58c52016-05-02 14:41:30 -0400358
Chuck Lever9d6b0402016-06-29 13:54:16 -0400359 dprintk("RPC: %5u %s: pos %u %u@0x%016llx:0x%08x (%s)\n",
Chuck Lever94f58c52016-05-02 14:41:30 -0400360 rqst->rq_task->tk_pid, __func__, pos,
Chuck Lever9d6b0402016-06-29 13:54:16 -0400361 mw->mw_length, (unsigned long long)mw->mw_offset,
362 mw->mw_handle, n < nsegs ? "more" : "last");
Chuck Lever94f58c52016-05-02 14:41:30 -0400363
364 r_xprt->rx_stats.read_chunk_count++;
Chuck Lever94f58c52016-05-02 14:41:30 -0400365 seg += n;
366 nsegs -= n;
367 } while (nsegs);
Chuck Lever94f58c52016-05-02 14:41:30 -0400368
369 /* Finish Read list */
370 *iptr++ = xdr_zero; /* Next item not present */
371 return iptr;
372}
373
374/* XDR-encode the Write list. Supports encoding a list containing
375 * one array of plain segments that belong to a single write chunk.
376 *
377 * Encoding key for single-list chunks (HLOO = Handle32 Length32 Offset64):
378 *
379 * Write chunklist (a list of (one) counted array):
380 * N elements:
381 * 1 - N - HLOO - HLOO - ... - HLOO - 0
382 *
383 * Returns a pointer to the XDR word in the RDMA header following
384 * the end of the Write list, or an error pointer.
385 */
386static __be32 *
387rpcrdma_encode_write_list(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req,
388 struct rpc_rqst *rqst, __be32 *iptr,
389 enum rpcrdma_chunktype wtype)
390{
Chuck Lever5ab81422016-06-29 13:54:25 -0400391 struct rpcrdma_mr_seg *seg;
Chuck Lever9d6b0402016-06-29 13:54:16 -0400392 struct rpcrdma_mw *mw;
Chuck Lever94f58c52016-05-02 14:41:30 -0400393 int n, nsegs, nchunks;
394 __be32 *segcount;
395
396 if (wtype != rpcrdma_writech) {
397 *iptr++ = xdr_zero; /* no Write list present */
398 return iptr;
399 }
400
Chuck Lever5ab81422016-06-29 13:54:25 -0400401 seg = req->rl_segments;
Chuck Lever94f58c52016-05-02 14:41:30 -0400402 nsegs = rpcrdma_convert_iovs(&rqst->rq_rcv_buf,
403 rqst->rq_rcv_buf.head[0].iov_len,
Chuck Leverc8b920b2016-09-15 10:57:16 -0400404 wtype, seg,
405 r_xprt->rx_ia.ri_reminv_expected);
Chuck Lever94f58c52016-05-02 14:41:30 -0400406 if (nsegs < 0)
407 return ERR_PTR(nsegs);
408
409 *iptr++ = xdr_one; /* Write list present */
410 segcount = iptr++; /* save location of segment count */
411
412 nchunks = 0;
413 do {
Chuck Lever9d6b0402016-06-29 13:54:16 -0400414 n = r_xprt->rx_ia.ri_ops->ro_map(r_xprt, seg, nsegs,
415 true, &mw);
Chuck Levera54d4052016-06-29 13:53:52 -0400416 if (n < 0)
Chuck Lever94f58c52016-05-02 14:41:30 -0400417 return ERR_PTR(n);
Chuck Lever9d6b0402016-06-29 13:54:16 -0400418 list_add(&mw->mw_list, &req->rl_registered);
Chuck Lever94f58c52016-05-02 14:41:30 -0400419
Chuck Lever9d6b0402016-06-29 13:54:16 -0400420 iptr = xdr_encode_rdma_segment(iptr, mw);
Chuck Lever94f58c52016-05-02 14:41:30 -0400421
Chuck Lever9d6b0402016-06-29 13:54:16 -0400422 dprintk("RPC: %5u %s: %u@0x016%llx:0x%08x (%s)\n",
Chuck Lever94f58c52016-05-02 14:41:30 -0400423 rqst->rq_task->tk_pid, __func__,
Chuck Lever9d6b0402016-06-29 13:54:16 -0400424 mw->mw_length, (unsigned long long)mw->mw_offset,
425 mw->mw_handle, n < nsegs ? "more" : "last");
Chuck Lever94f58c52016-05-02 14:41:30 -0400426
427 r_xprt->rx_stats.write_chunk_count++;
428 r_xprt->rx_stats.total_rdma_request += seg->mr_len;
Chuck Lever94f58c52016-05-02 14:41:30 -0400429 nchunks++;
430 seg += n;
431 nsegs -= n;
432 } while (nsegs);
Chuck Lever94f58c52016-05-02 14:41:30 -0400433
434 /* Update count of segments in this Write chunk */
435 *segcount = cpu_to_be32(nchunks);
436
437 /* Finish Write list */
438 *iptr++ = xdr_zero; /* Next item not present */
439 return iptr;
440}
441
442/* XDR-encode the Reply chunk. Supports encoding an array of plain
443 * segments that belong to a single write (reply) chunk.
444 *
445 * Encoding key for single-list chunks (HLOO = Handle32 Length32 Offset64):
446 *
447 * Reply chunk (a counted array):
448 * N elements:
449 * 1 - N - HLOO - HLOO - ... - HLOO
450 *
451 * Returns a pointer to the XDR word in the RDMA header following
452 * the end of the Reply chunk, or an error pointer.
453 */
454static __be32 *
455rpcrdma_encode_reply_chunk(struct rpcrdma_xprt *r_xprt,
456 struct rpcrdma_req *req, struct rpc_rqst *rqst,
457 __be32 *iptr, enum rpcrdma_chunktype wtype)
458{
Chuck Lever5ab81422016-06-29 13:54:25 -0400459 struct rpcrdma_mr_seg *seg;
Chuck Lever9d6b0402016-06-29 13:54:16 -0400460 struct rpcrdma_mw *mw;
Chuck Lever94f58c52016-05-02 14:41:30 -0400461 int n, nsegs, nchunks;
462 __be32 *segcount;
463
464 if (wtype != rpcrdma_replych) {
465 *iptr++ = xdr_zero; /* no Reply chunk present */
466 return iptr;
467 }
468
Chuck Lever5ab81422016-06-29 13:54:25 -0400469 seg = req->rl_segments;
Chuck Leverc8b920b2016-09-15 10:57:16 -0400470 nsegs = rpcrdma_convert_iovs(&rqst->rq_rcv_buf, 0, wtype, seg,
471 r_xprt->rx_ia.ri_reminv_expected);
Chuck Lever94f58c52016-05-02 14:41:30 -0400472 if (nsegs < 0)
473 return ERR_PTR(nsegs);
474
475 *iptr++ = xdr_one; /* Reply chunk present */
476 segcount = iptr++; /* save location of segment count */
477
478 nchunks = 0;
479 do {
Chuck Lever9d6b0402016-06-29 13:54:16 -0400480 n = r_xprt->rx_ia.ri_ops->ro_map(r_xprt, seg, nsegs,
481 true, &mw);
Chuck Levera54d4052016-06-29 13:53:52 -0400482 if (n < 0)
Chuck Lever94f58c52016-05-02 14:41:30 -0400483 return ERR_PTR(n);
Chuck Lever9d6b0402016-06-29 13:54:16 -0400484 list_add(&mw->mw_list, &req->rl_registered);
Chuck Lever94f58c52016-05-02 14:41:30 -0400485
Chuck Lever9d6b0402016-06-29 13:54:16 -0400486 iptr = xdr_encode_rdma_segment(iptr, mw);
Chuck Lever94f58c52016-05-02 14:41:30 -0400487
Chuck Lever9d6b0402016-06-29 13:54:16 -0400488 dprintk("RPC: %5u %s: %u@0x%016llx:0x%08x (%s)\n",
Chuck Lever94f58c52016-05-02 14:41:30 -0400489 rqst->rq_task->tk_pid, __func__,
Chuck Lever9d6b0402016-06-29 13:54:16 -0400490 mw->mw_length, (unsigned long long)mw->mw_offset,
491 mw->mw_handle, n < nsegs ? "more" : "last");
Chuck Lever94f58c52016-05-02 14:41:30 -0400492
493 r_xprt->rx_stats.reply_chunk_count++;
494 r_xprt->rx_stats.total_rdma_request += seg->mr_len;
Chuck Lever94f58c52016-05-02 14:41:30 -0400495 nchunks++;
496 seg += n;
497 nsegs -= n;
498 } while (nsegs);
Chuck Lever94f58c52016-05-02 14:41:30 -0400499
500 /* Update count of segments in the Reply chunk */
501 *segcount = cpu_to_be32(nchunks);
502
503 return iptr;
504}
505
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400506/*
507 * Copy write data inline.
508 * This function is used for "small" requests. Data which is passed
509 * to RPC via iovecs (or page list) is copied directly into the
510 * pre-registered memory buffer for this request. For small amounts
511 * of data, this is efficient. The cutoff value is tunable.
512 */
Chuck Leverb3221d62015-08-03 13:03:39 -0400513static void rpcrdma_inline_pullup(struct rpc_rqst *rqst)
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400514{
515 int i, npages, curlen;
516 int copy_len;
517 unsigned char *srcp, *destp;
518 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000519 int page_base;
520 struct page **ppages;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400521
522 destp = rqst->rq_svec[0].iov_base;
523 curlen = rqst->rq_svec[0].iov_len;
524 destp += curlen;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400525
Chuck Leverb3221d62015-08-03 13:03:39 -0400526 dprintk("RPC: %s: destp 0x%p len %d hdrlen %d\n",
527 __func__, destp, rqst->rq_slen, curlen);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400528
529 copy_len = rqst->rq_snd_buf.page_len;
Tom Talpeyb38ab402009-03-11 14:37:55 -0400530
531 if (rqst->rq_snd_buf.tail[0].iov_len) {
532 curlen = rqst->rq_snd_buf.tail[0].iov_len;
533 if (destp + copy_len != rqst->rq_snd_buf.tail[0].iov_base) {
534 memmove(destp + copy_len,
535 rqst->rq_snd_buf.tail[0].iov_base, curlen);
536 r_xprt->rx_stats.pullup_copy_count += curlen;
537 }
538 dprintk("RPC: %s: tail destp 0x%p len %d\n",
539 __func__, destp + copy_len, curlen);
540 rqst->rq_svec[0].iov_len += curlen;
541 }
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400542 r_xprt->rx_stats.pullup_copy_count += copy_len;
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000543
544 page_base = rqst->rq_snd_buf.page_base;
545 ppages = rqst->rq_snd_buf.pages + (page_base >> PAGE_SHIFT);
546 page_base &= ~PAGE_MASK;
547 npages = PAGE_ALIGN(page_base+copy_len) >> PAGE_SHIFT;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400548 for (i = 0; copy_len && i < npages; i++) {
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000549 curlen = PAGE_SIZE - page_base;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400550 if (curlen > copy_len)
551 curlen = copy_len;
552 dprintk("RPC: %s: page %d destp 0x%p len %d curlen %d\n",
553 __func__, i, destp, copy_len, curlen);
Cong Wangb8541782011-11-25 23:14:40 +0800554 srcp = kmap_atomic(ppages[i]);
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000555 memcpy(destp, srcp+page_base, curlen);
Cong Wangb8541782011-11-25 23:14:40 +0800556 kunmap_atomic(srcp);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400557 rqst->rq_svec[0].iov_len += curlen;
558 destp += curlen;
559 copy_len -= curlen;
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000560 page_base = 0;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400561 }
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400562 /* header now contains entire send message */
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400563}
564
565/*
566 * Marshal a request: the primary job of this routine is to choose
567 * the transfer modes. See comments below.
568 *
Chuck Lever88b18a12016-05-02 14:41:22 -0400569 * Prepares up to two IOVs per Call message:
570 *
571 * [0] -- RPC RDMA header
572 * [1] -- the RPC header/data
Chuck Leverc93c6222014-05-28 10:35:14 -0400573 *
574 * Returns zero on success, otherwise a negative errno.
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400575 */
576
577int
578rpcrdma_marshal_req(struct rpc_rqst *rqst)
579{
Trond Myklebusta4f08352013-01-08 09:10:21 -0500580 struct rpc_xprt *xprt = rqst->rq_xprt;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400581 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
582 struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
Chuck Levere2377942015-03-30 14:33:53 -0400583 enum rpcrdma_chunktype rtype, wtype;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400584 struct rpcrdma_msg *headerp;
Chuck Lever65b80172016-06-29 13:55:06 -0400585 bool ddp_allowed;
Chuck Lever94f58c52016-05-02 14:41:30 -0400586 ssize_t hdrlen;
587 size_t rpclen;
588 __be32 *iptr;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400589
Chuck Lever83128a62015-10-24 17:27:59 -0400590#if defined(CONFIG_SUNRPC_BACKCHANNEL)
591 if (test_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state))
592 return rpcrdma_bc_marshal_reply(rqst);
593#endif
594
Chuck Lever85275c82015-01-21 11:04:16 -0500595 headerp = rdmab_to_msg(req->rl_rdmabuf);
Chuck Lever284f4902015-01-21 11:02:13 -0500596 /* don't byte-swap XID, it's already done in request */
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400597 headerp->rm_xid = rqst->rq_xid;
Chuck Lever284f4902015-01-21 11:02:13 -0500598 headerp->rm_vers = rpcrdma_version;
599 headerp->rm_credit = cpu_to_be32(r_xprt->rx_buf.rb_max_requests);
600 headerp->rm_type = rdma_msg;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400601
Chuck Lever65b80172016-06-29 13:55:06 -0400602 /* When the ULP employs a GSS flavor that guarantees integrity
603 * or privacy, direct data placement of individual data items
604 * is not allowed.
605 */
606 ddp_allowed = !(rqst->rq_cred->cr_auth->au_flags &
607 RPCAUTH_AUTH_DATATOUCH);
608
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400609 /*
610 * Chunks needed for results?
611 *
612 * o If the expected result is under the inline threshold, all ops
Chuck Lever33943b22015-08-03 13:04:08 -0400613 * return as inline.
Chuck Levercce6dee2016-05-02 14:41:14 -0400614 * o Large read ops return data as write chunk(s), header as
615 * inline.
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400616 * o Large non-read ops return as a single reply chunk.
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400617 */
Chuck Levercce6dee2016-05-02 14:41:14 -0400618 if (rpcrdma_results_inline(r_xprt, rqst))
Chuck Lever02eb57d82015-08-03 13:03:58 -0400619 wtype = rpcrdma_noch;
Chuck Lever65b80172016-06-29 13:55:06 -0400620 else if (ddp_allowed && rqst->rq_rcv_buf.flags & XDRBUF_READ)
Chuck Levercce6dee2016-05-02 14:41:14 -0400621 wtype = rpcrdma_writech;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400622 else
Chuck Levere2377942015-03-30 14:33:53 -0400623 wtype = rpcrdma_replych;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400624
625 /*
626 * Chunks needed for arguments?
627 *
628 * o If the total request is under the inline threshold, all ops
629 * are sent as inline.
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400630 * o Large write ops transmit data as read chunk(s), header as
631 * inline.
Chuck Lever2fcc2132015-08-03 13:04:26 -0400632 * o Large non-write ops are sent with the entire message as a
633 * single read chunk (protocol 0-position special case).
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400634 *
Chuck Lever2fcc2132015-08-03 13:04:26 -0400635 * This assumes that the upper layer does not present a request
636 * that both has a data payload, and whose non-data arguments
637 * by themselves are larger than the inline threshold.
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400638 */
Chuck Lever302d3de2016-05-02 14:41:05 -0400639 if (rpcrdma_args_inline(r_xprt, rqst)) {
Chuck Levere2377942015-03-30 14:33:53 -0400640 rtype = rpcrdma_noch;
Chuck Lever94f58c52016-05-02 14:41:30 -0400641 rpcrdma_inline_pullup(rqst);
642 rpclen = rqst->rq_svec[0].iov_len;
Chuck Lever65b80172016-06-29 13:55:06 -0400643 } else if (ddp_allowed && rqst->rq_snd_buf.flags & XDRBUF_WRITE) {
Chuck Levere2377942015-03-30 14:33:53 -0400644 rtype = rpcrdma_readch;
Chuck Lever94f58c52016-05-02 14:41:30 -0400645 rpclen = rqst->rq_svec[0].iov_len;
646 rpclen += rpcrdma_tail_pullup(&rqst->rq_snd_buf);
Chuck Lever2fcc2132015-08-03 13:04:26 -0400647 } else {
Chuck Lever860477d2015-08-03 13:04:45 -0400648 r_xprt->rx_stats.nomsg_call_count++;
Chuck Lever2fcc2132015-08-03 13:04:26 -0400649 headerp->rm_type = htonl(RDMA_NOMSG);
650 rtype = rpcrdma_areadch;
651 rpclen = 0;
652 }
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400653
Chuck Lever94f58c52016-05-02 14:41:30 -0400654 /* This implementation supports the following combinations
655 * of chunk lists in one RPC-over-RDMA Call message:
656 *
657 * - Read list
658 * - Write list
659 * - Reply chunk
660 * - Read list + Reply chunk
661 *
662 * It might not yet support the following combinations:
663 *
664 * - Read list + Write list
665 *
666 * It does not support the following combinations:
667 *
668 * - Write list + Reply chunk
669 * - Read list + Write list + Reply chunk
670 *
671 * This implementation supports only a single chunk in each
672 * Read or Write list. Thus for example the client cannot
673 * send a Call message with a Position Zero Read chunk and a
674 * regular Read chunk at the same time.
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400675 */
Chuck Lever94f58c52016-05-02 14:41:30 -0400676 iptr = headerp->rm_body.rm_chunks;
677 iptr = rpcrdma_encode_read_list(r_xprt, req, rqst, iptr, rtype);
678 if (IS_ERR(iptr))
679 goto out_unmap;
680 iptr = rpcrdma_encode_write_list(r_xprt, req, rqst, iptr, wtype);
681 if (IS_ERR(iptr))
682 goto out_unmap;
683 iptr = rpcrdma_encode_reply_chunk(r_xprt, req, rqst, iptr, wtype);
684 if (IS_ERR(iptr))
685 goto out_unmap;
686 hdrlen = (unsigned char *)iptr - (unsigned char *)headerp;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400687
Chuck Levereb342e92016-09-15 10:55:04 -0400688 if (hdrlen + rpclen > r_xprt->rx_data.inline_wsize)
Chuck Lever302d3de2016-05-02 14:41:05 -0400689 goto out_overflow;
690
Chuck Lever94f58c52016-05-02 14:41:30 -0400691 dprintk("RPC: %5u %s: %s/%s: hdrlen %zd rpclen %zd\n",
692 rqst->rq_task->tk_pid, __func__,
693 transfertypes[rtype], transfertypes[wtype],
694 hdrlen, rpclen);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400695
Chuck Lever54cbd6b2016-09-15 10:56:18 -0400696 if (!rpcrdma_dma_map_regbuf(&r_xprt->rx_ia, req->rl_rdmabuf))
697 goto out_map;
Chuck Lever85275c82015-01-21 11:04:16 -0500698 req->rl_send_iov[0].addr = rdmab_addr(req->rl_rdmabuf);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400699 req->rl_send_iov[0].length = hdrlen;
Chuck Lever85275c82015-01-21 11:04:16 -0500700 req->rl_send_iov[0].lkey = rdmab_lkey(req->rl_rdmabuf);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400701
Chuck Lever90aab602016-09-15 10:56:43 -0400702 req->rl_send_wr.num_sge = 1;
Chuck Lever2fcc2132015-08-03 13:04:26 -0400703 if (rtype == rpcrdma_areadch)
704 return 0;
705
Chuck Lever54cbd6b2016-09-15 10:56:18 -0400706 if (!rpcrdma_dma_map_regbuf(&r_xprt->rx_ia, req->rl_sendbuf))
707 goto out_map;
Chuck Lever0ca77dc2015-01-21 11:04:08 -0500708 req->rl_send_iov[1].addr = rdmab_addr(req->rl_sendbuf);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400709 req->rl_send_iov[1].length = rpclen;
Chuck Lever0ca77dc2015-01-21 11:04:08 -0500710 req->rl_send_iov[1].lkey = rdmab_lkey(req->rl_sendbuf);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400711
Chuck Lever90aab602016-09-15 10:56:43 -0400712 req->rl_send_wr.num_sge = 2;
713
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400714 return 0;
Chuck Lever302d3de2016-05-02 14:41:05 -0400715
716out_overflow:
Chuck Lever94f58c52016-05-02 14:41:30 -0400717 pr_err("rpcrdma: send overflow: hdrlen %zd rpclen %zu %s/%s\n",
718 hdrlen, rpclen, transfertypes[rtype], transfertypes[wtype]);
Chuck Lever9d6b0402016-06-29 13:54:16 -0400719 iptr = ERR_PTR(-EIO);
Chuck Lever94f58c52016-05-02 14:41:30 -0400720
721out_unmap:
Chuck Leveread3f262016-05-02 14:42:46 -0400722 r_xprt->rx_ia.ri_ops->ro_unmap_safe(r_xprt, req, false);
Chuck Lever94f58c52016-05-02 14:41:30 -0400723 return PTR_ERR(iptr);
Chuck Lever54cbd6b2016-09-15 10:56:18 -0400724
725out_map:
726 pr_err("rpcrdma: failed to DMA map a Send buffer\n");
727 iptr = ERR_PTR(-EIO);
728 goto out_unmap;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400729}
730
731/*
732 * Chase down a received write or reply chunklist to get length
733 * RDMA'd by server. See map at rpcrdma_create_chunks()! :-)
734 */
735static int
Chuck Lever9d6b0402016-06-29 13:54:16 -0400736rpcrdma_count_chunks(struct rpcrdma_rep *rep, int wrchunk, __be32 **iptrp)
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400737{
738 unsigned int i, total_len;
739 struct rpcrdma_write_chunk *cur_wchunk;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500740 char *base = (char *)rdmab_to_msg(rep->rr_rdmabuf);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400741
Chuck Lever284f4902015-01-21 11:02:13 -0500742 i = be32_to_cpu(**iptrp);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400743 cur_wchunk = (struct rpcrdma_write_chunk *) (*iptrp + 1);
744 total_len = 0;
745 while (i--) {
746 struct rpcrdma_segment *seg = &cur_wchunk->wc_target;
747 ifdebug(FACILITY) {
748 u64 off;
Al Viro2d8a9722007-10-29 04:37:58 +0000749 xdr_decode_hyper((__be32 *)&seg->rs_offset, &off);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400750 dprintk("RPC: %s: chunk %d@0x%llx:0x%x\n",
751 __func__,
Chuck Lever284f4902015-01-21 11:02:13 -0500752 be32_to_cpu(seg->rs_length),
Stephen Rothwelle08a1322007-10-30 00:44:32 -0700753 (unsigned long long)off,
Chuck Lever284f4902015-01-21 11:02:13 -0500754 be32_to_cpu(seg->rs_handle));
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400755 }
Chuck Lever284f4902015-01-21 11:02:13 -0500756 total_len += be32_to_cpu(seg->rs_length);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400757 ++cur_wchunk;
758 }
759 /* check and adjust for properly terminated write chunk */
760 if (wrchunk) {
Al Viro2d8a9722007-10-29 04:37:58 +0000761 __be32 *w = (__be32 *) cur_wchunk;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400762 if (*w++ != xdr_zero)
763 return -1;
764 cur_wchunk = (struct rpcrdma_write_chunk *) w;
765 }
Chuck Lever6b1184c2015-01-21 11:04:25 -0500766 if ((char *)cur_wchunk > base + rep->rr_len)
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400767 return -1;
768
Al Viro2d8a9722007-10-29 04:37:58 +0000769 *iptrp = (__be32 *) cur_wchunk;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400770 return total_len;
771}
772
Chuck Levercb0ae1f2016-06-29 13:54:41 -0400773/**
774 * rpcrdma_inline_fixup - Scatter inline received data into rqst's iovecs
775 * @rqst: controlling RPC request
776 * @srcp: points to RPC message payload in receive buffer
777 * @copy_len: remaining length of receive buffer content
778 * @pad: Write chunk pad bytes needed (zero for pure inline)
779 *
780 * The upper layer has set the maximum number of bytes it can
781 * receive in each component of rq_rcv_buf. These values are set in
782 * the head.iov_len, page_len, tail.iov_len, and buflen fields.
Chuck Levercfabe2c2016-06-29 13:54:49 -0400783 *
784 * Unlike the TCP equivalent (xdr_partial_copy_from_skb), in
785 * many cases this function simply updates iov_base pointers in
786 * rq_rcv_buf to point directly to the received reply data, to
787 * avoid copying reply data.
Chuck Lever64695bde2016-06-29 13:54:58 -0400788 *
789 * Returns the count of bytes which had to be memcopied.
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400790 */
Chuck Lever64695bde2016-06-29 13:54:58 -0400791static unsigned long
Tom Talpey9191ca32008-10-09 15:01:11 -0400792rpcrdma_inline_fixup(struct rpc_rqst *rqst, char *srcp, int copy_len, int pad)
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400793{
Chuck Lever64695bde2016-06-29 13:54:58 -0400794 unsigned long fixup_copy_count;
795 int i, npages, curlen;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400796 char *destp;
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000797 struct page **ppages;
798 int page_base;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400799
Chuck Levercb0ae1f2016-06-29 13:54:41 -0400800 /* The head iovec is redirected to the RPC reply message
801 * in the receive buffer, to avoid a memcopy.
802 */
803 rqst->rq_rcv_buf.head[0].iov_base = srcp;
Chuck Levercfabe2c2016-06-29 13:54:49 -0400804 rqst->rq_private_buf.head[0].iov_base = srcp;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400805
Chuck Levercb0ae1f2016-06-29 13:54:41 -0400806 /* The contents of the receive buffer that follow
807 * head.iov_len bytes are copied into the page list.
808 */
809 curlen = rqst->rq_rcv_buf.head[0].iov_len;
810 if (curlen > copy_len)
811 curlen = copy_len;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400812 dprintk("RPC: %s: srcp 0x%p len %d hdrlen %d\n",
813 __func__, srcp, copy_len, curlen);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400814 srcp += curlen;
815 copy_len -= curlen;
816
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000817 page_base = rqst->rq_rcv_buf.page_base;
818 ppages = rqst->rq_rcv_buf.pages + (page_base >> PAGE_SHIFT);
819 page_base &= ~PAGE_MASK;
Chuck Lever64695bde2016-06-29 13:54:58 -0400820 fixup_copy_count = 0;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400821 if (copy_len && rqst->rq_rcv_buf.page_len) {
Chuck Lever80414ab2016-06-29 13:54:33 -0400822 int pagelist_len;
823
824 pagelist_len = rqst->rq_rcv_buf.page_len;
825 if (pagelist_len > copy_len)
826 pagelist_len = copy_len;
827 npages = PAGE_ALIGN(page_base + pagelist_len) >> PAGE_SHIFT;
Chuck Lever64695bde2016-06-29 13:54:58 -0400828 for (i = 0; i < npages; i++) {
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000829 curlen = PAGE_SIZE - page_base;
Chuck Lever80414ab2016-06-29 13:54:33 -0400830 if (curlen > pagelist_len)
831 curlen = pagelist_len;
832
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400833 dprintk("RPC: %s: page %d"
834 " srcp 0x%p len %d curlen %d\n",
835 __func__, i, srcp, copy_len, curlen);
Cong Wangb8541782011-11-25 23:14:40 +0800836 destp = kmap_atomic(ppages[i]);
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000837 memcpy(destp + page_base, srcp, curlen);
838 flush_dcache_page(ppages[i]);
Cong Wangb8541782011-11-25 23:14:40 +0800839 kunmap_atomic(destp);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400840 srcp += curlen;
841 copy_len -= curlen;
Chuck Lever64695bde2016-06-29 13:54:58 -0400842 fixup_copy_count += curlen;
Chuck Lever80414ab2016-06-29 13:54:33 -0400843 pagelist_len -= curlen;
844 if (!pagelist_len)
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400845 break;
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000846 page_base = 0;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400847 }
Chuck Levercb0ae1f2016-06-29 13:54:41 -0400848
849 /* Implicit padding for the last segment in a Write
850 * chunk is inserted inline at the front of the tail
851 * iovec. The upper layer ignores the content of
852 * the pad. Simply ensure inline content in the tail
853 * that follows the Write chunk is properly aligned.
854 */
855 if (pad)
856 srcp -= pad;
Chuck Lever2b7bbc92014-03-12 12:51:30 -0400857 }
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400858
Chuck Levercb0ae1f2016-06-29 13:54:41 -0400859 /* The tail iovec is redirected to the remaining data
860 * in the receive buffer, to avoid a memcopy.
861 */
Chuck Levercfabe2c2016-06-29 13:54:49 -0400862 if (copy_len || pad) {
Chuck Levercb0ae1f2016-06-29 13:54:41 -0400863 rqst->rq_rcv_buf.tail[0].iov_base = srcp;
Chuck Levercfabe2c2016-06-29 13:54:49 -0400864 rqst->rq_private_buf.tail[0].iov_base = srcp;
865 }
Tom Talpey9191ca32008-10-09 15:01:11 -0400866
Chuck Lever64695bde2016-06-29 13:54:58 -0400867 return fixup_copy_count;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400868}
869
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400870void
Chuck Lever254f91e2014-05-28 10:32:17 -0400871rpcrdma_connect_worker(struct work_struct *work)
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400872{
Chuck Lever254f91e2014-05-28 10:32:17 -0400873 struct rpcrdma_ep *ep =
874 container_of(work, struct rpcrdma_ep, rep_connect_worker.work);
Chuck Leverafadc462015-01-21 11:03:11 -0500875 struct rpcrdma_xprt *r_xprt =
876 container_of(ep, struct rpcrdma_xprt, rx_ep);
877 struct rpc_xprt *xprt = &r_xprt->rx_xprt;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400878
879 spin_lock_bh(&xprt->transport_lock);
Tom Talpey575448b2008-10-09 15:00:40 -0400880 if (++xprt->connect_cookie == 0) /* maintain a reserved value */
881 ++xprt->connect_cookie;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400882 if (ep->rep_connected > 0) {
883 if (!xprt_test_and_set_connected(xprt))
884 xprt_wake_pending_tasks(xprt, 0);
885 } else {
886 if (xprt_test_and_clear_connected(xprt))
Tom Talpey926449b2008-10-09 15:01:21 -0400887 xprt_wake_pending_tasks(xprt, -ENOTCONN);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400888 }
889 spin_unlock_bh(&xprt->transport_lock);
890}
891
Chuck Lever63cae472015-10-24 17:28:08 -0400892#if defined(CONFIG_SUNRPC_BACKCHANNEL)
893/* By convention, backchannel calls arrive via rdma_msg type
894 * messages, and never populate the chunk lists. This makes
895 * the RPC/RDMA header small and fixed in size, so it is
896 * straightforward to check the RPC header's direction field.
897 */
898static bool
899rpcrdma_is_bcall(struct rpcrdma_msg *headerp)
900{
901 __be32 *p = (__be32 *)headerp;
902
903 if (headerp->rm_type != rdma_msg)
904 return false;
905 if (headerp->rm_body.rm_chunks[0] != xdr_zero)
906 return false;
907 if (headerp->rm_body.rm_chunks[1] != xdr_zero)
908 return false;
909 if (headerp->rm_body.rm_chunks[2] != xdr_zero)
910 return false;
911
912 /* sanity */
913 if (p[7] != headerp->rm_xid)
914 return false;
915 /* call direction */
916 if (p[8] != cpu_to_be32(RPC_CALL))
917 return false;
918
919 return true;
920}
921#endif /* CONFIG_SUNRPC_BACKCHANNEL */
922
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400923/*
Chuck Lever254f91e2014-05-28 10:32:17 -0400924 * This function is called when an async event is posted to
925 * the connection which changes the connection state. All it
926 * does at this point is mark the connection up/down, the rpc
927 * timers do the rest.
928 */
929void
930rpcrdma_conn_func(struct rpcrdma_ep *ep)
931{
932 schedule_delayed_work(&ep->rep_connect_worker, 0);
933}
934
Chuck Leverfe97b472015-10-24 17:27:10 -0400935/* Process received RPC/RDMA messages.
936 *
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400937 * Errors must result in the RPC task either being awakened, or
938 * allowed to timeout, to discover the errors at that time.
939 */
940void
941rpcrdma_reply_handler(struct rpcrdma_rep *rep)
942{
943 struct rpcrdma_msg *headerp;
944 struct rpcrdma_req *req;
945 struct rpc_rqst *rqst;
Chuck Leverfed171b2015-05-26 11:51:37 -0400946 struct rpcrdma_xprt *r_xprt = rep->rr_rxprt;
947 struct rpc_xprt *xprt = &r_xprt->rx_xprt;
Al Viro2d8a9722007-10-29 04:37:58 +0000948 __be32 *iptr;
Chuck Lever59aa1f92016-03-04 11:28:18 -0500949 int rdmalen, status, rmerr;
Chuck Levere7ce7102014-05-28 10:34:57 -0400950 unsigned long cwnd;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400951
Chuck Leverb0e178a2015-10-24 17:26:54 -0400952 dprintk("RPC: %s: incoming rep %p\n", __func__, rep);
953
954 if (rep->rr_len == RPCRDMA_BAD_LEN)
955 goto out_badstatus;
Chuck Lever59aa1f92016-03-04 11:28:18 -0500956 if (rep->rr_len < RPCRDMA_HDRLEN_ERR)
Chuck Leverb0e178a2015-10-24 17:26:54 -0400957 goto out_shortreply;
958
Chuck Lever6b1184c2015-01-21 11:04:25 -0500959 headerp = rdmab_to_msg(rep->rr_rdmabuf);
Chuck Lever63cae472015-10-24 17:28:08 -0400960#if defined(CONFIG_SUNRPC_BACKCHANNEL)
961 if (rpcrdma_is_bcall(headerp))
962 goto out_bcall;
963#endif
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400964
Chuck Leverfe97b472015-10-24 17:27:10 -0400965 /* Match incoming rpcrdma_rep to an rpcrdma_req to
966 * get context for handling any incoming chunks.
967 */
968 spin_lock_bh(&xprt->transport_lock);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400969 rqst = xprt_lookup_rqst(xprt, headerp->rm_xid);
Chuck Leverb0e178a2015-10-24 17:26:54 -0400970 if (!rqst)
971 goto out_nomatch;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400972
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400973 req = rpcr_to_rdmar(rqst);
Chuck Leverb0e178a2015-10-24 17:26:54 -0400974 if (req->rl_reply)
975 goto out_duplicate;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400976
Chuck Lever68791642015-12-16 17:23:11 -0500977 /* Sanity checking has passed. We are now committed
978 * to complete this transaction.
979 */
980 list_del_init(&rqst->rq_list);
981 spin_unlock_bh(&xprt->transport_lock);
Chuck Leveraf0f16e2016-03-04 11:27:43 -0500982 dprintk("RPC: %s: reply %p completes request %p (xid 0x%08x)\n",
983 __func__, rep, req, be32_to_cpu(headerp->rm_xid));
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400984
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400985 /* from here on, the reply is no longer an orphan */
986 req->rl_reply = rep;
Chuck Lever18906972014-05-28 10:34:41 -0400987 xprt->reestablish_timeout = 0;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400988
Chuck Lever59aa1f92016-03-04 11:28:18 -0500989 if (headerp->rm_vers != rpcrdma_version)
990 goto out_badversion;
991
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400992 /* check for expected message types */
993 /* The order of some of these tests is important. */
994 switch (headerp->rm_type) {
Chuck Lever284f4902015-01-21 11:02:13 -0500995 case rdma_msg:
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400996 /* never expect read chunks */
997 /* never expect reply chunks (two ways to check) */
998 /* never expect write chunks without having offered RDMA */
999 if (headerp->rm_body.rm_chunks[0] != xdr_zero ||
1000 (headerp->rm_body.rm_chunks[1] == xdr_zero &&
1001 headerp->rm_body.rm_chunks[2] != xdr_zero) ||
1002 (headerp->rm_body.rm_chunks[1] != xdr_zero &&
Chuck Lever9d6b0402016-06-29 13:54:16 -04001003 list_empty(&req->rl_registered)))
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -04001004 goto badheader;
1005 if (headerp->rm_body.rm_chunks[1] != xdr_zero) {
1006 /* count any expected write chunks in read reply */
1007 /* start at write chunk array count */
1008 iptr = &headerp->rm_body.rm_chunks[2];
Chuck Lever9d6b0402016-06-29 13:54:16 -04001009 rdmalen = rpcrdma_count_chunks(rep, 1, &iptr);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -04001010 /* check for validity, and no reply chunk after */
1011 if (rdmalen < 0 || *iptr++ != xdr_zero)
1012 goto badheader;
1013 rep->rr_len -=
1014 ((unsigned char *)iptr - (unsigned char *)headerp);
1015 status = rep->rr_len + rdmalen;
1016 r_xprt->rx_stats.total_rdma_reply += rdmalen;
Tom Talpey9191ca32008-10-09 15:01:11 -04001017 /* special case - last chunk may omit padding */
1018 if (rdmalen &= 3) {
1019 rdmalen = 4 - rdmalen;
1020 status += rdmalen;
1021 }
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -04001022 } else {
1023 /* else ordinary inline */
Tom Talpey9191ca32008-10-09 15:01:11 -04001024 rdmalen = 0;
Chuck Leverf2846482015-01-21 11:02:29 -05001025 iptr = (__be32 *)((unsigned char *)headerp +
1026 RPCRDMA_HDRLEN_MIN);
1027 rep->rr_len -= RPCRDMA_HDRLEN_MIN;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -04001028 status = rep->rr_len;
1029 }
Chuck Lever64695bde2016-06-29 13:54:58 -04001030
1031 r_xprt->rx_stats.fixup_copy_count +=
1032 rpcrdma_inline_fixup(rqst, (char *)iptr, rep->rr_len,
1033 rdmalen);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -04001034 break;
1035
Chuck Lever284f4902015-01-21 11:02:13 -05001036 case rdma_nomsg:
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -04001037 /* never expect read or write chunks, always reply chunks */
1038 if (headerp->rm_body.rm_chunks[0] != xdr_zero ||
1039 headerp->rm_body.rm_chunks[1] != xdr_zero ||
1040 headerp->rm_body.rm_chunks[2] != xdr_one ||
Chuck Lever9d6b0402016-06-29 13:54:16 -04001041 list_empty(&req->rl_registered))
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -04001042 goto badheader;
Chuck Leverf2846482015-01-21 11:02:29 -05001043 iptr = (__be32 *)((unsigned char *)headerp +
1044 RPCRDMA_HDRLEN_MIN);
Chuck Lever9d6b0402016-06-29 13:54:16 -04001045 rdmalen = rpcrdma_count_chunks(rep, 0, &iptr);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -04001046 if (rdmalen < 0)
1047 goto badheader;
1048 r_xprt->rx_stats.total_rdma_reply += rdmalen;
1049 /* Reply chunk buffer already is the reply vector - no fixup. */
1050 status = rdmalen;
1051 break;
1052
Chuck Lever59aa1f92016-03-04 11:28:18 -05001053 case rdma_error:
1054 goto out_rdmaerr;
1055
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -04001056badheader:
1057 default:
Chuck Lever9d6b0402016-06-29 13:54:16 -04001058 dprintk("RPC: %5u %s: invalid rpcrdma reply (type %u)\n",
1059 rqst->rq_task->tk_pid, __func__,
1060 be32_to_cpu(headerp->rm_type));
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -04001061 status = -EIO;
1062 r_xprt->rx_stats.bad_reply_count++;
1063 break;
1064 }
1065
Chuck Lever59aa1f92016-03-04 11:28:18 -05001066out:
Chuck Lever68791642015-12-16 17:23:11 -05001067 /* Invalidate and flush the data payloads before waking the
1068 * waiting application. This guarantees the memory region is
1069 * properly fenced from the server before the application
1070 * accesses the data. It also ensures proper send flow
1071 * control: waking the next RPC waits until this RPC has
1072 * relinquished all its Send Queue entries.
1073 */
Chuck Lever9d6b0402016-06-29 13:54:16 -04001074 if (!list_empty(&req->rl_registered))
Chuck Lever68791642015-12-16 17:23:11 -05001075 r_xprt->rx_ia.ri_ops->ro_unmap_sync(r_xprt, req);
1076
Chuck Lever68791642015-12-16 17:23:11 -05001077 spin_lock_bh(&xprt->transport_lock);
Chuck Levere7ce7102014-05-28 10:34:57 -04001078 cwnd = xprt->cwnd;
Chuck Lever23826c72016-03-04 11:28:27 -05001079 xprt->cwnd = atomic_read(&r_xprt->rx_buf.rb_credits) << RPC_CWNDSHIFT;
Chuck Levere7ce7102014-05-28 10:34:57 -04001080 if (xprt->cwnd > cwnd)
1081 xprt_release_rqst_cong(rqst->rq_task);
1082
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -04001083 xprt_complete_rqst(rqst->rq_task, status);
Chuck Leverfe97b472015-10-24 17:27:10 -04001084 spin_unlock_bh(&xprt->transport_lock);
Chuck Leverb0e178a2015-10-24 17:26:54 -04001085 dprintk("RPC: %s: xprt_complete_rqst(0x%p, 0x%p, %d)\n",
1086 __func__, xprt, rqst, status);
1087 return;
1088
1089out_badstatus:
1090 rpcrdma_recv_buffer_put(rep);
1091 if (r_xprt->rx_ep.rep_connected == 1) {
1092 r_xprt->rx_ep.rep_connected = -EIO;
1093 rpcrdma_conn_func(&r_xprt->rx_ep);
1094 }
1095 return;
1096
Chuck Lever63cae472015-10-24 17:28:08 -04001097#if defined(CONFIG_SUNRPC_BACKCHANNEL)
1098out_bcall:
1099 rpcrdma_bc_receive_call(r_xprt, rep);
1100 return;
1101#endif
1102
Chuck Lever59aa1f92016-03-04 11:28:18 -05001103/* If the incoming reply terminated a pending RPC, the next
1104 * RPC call will post a replacement receive buffer as it is
1105 * being marshaled.
1106 */
Chuck Leverb0e178a2015-10-24 17:26:54 -04001107out_badversion:
1108 dprintk("RPC: %s: invalid version %d\n",
1109 __func__, be32_to_cpu(headerp->rm_vers));
Chuck Lever59aa1f92016-03-04 11:28:18 -05001110 status = -EIO;
1111 r_xprt->rx_stats.bad_reply_count++;
1112 goto out;
1113
1114out_rdmaerr:
1115 rmerr = be32_to_cpu(headerp->rm_body.rm_error.rm_err);
1116 switch (rmerr) {
1117 case ERR_VERS:
1118 pr_err("%s: server reports header version error (%u-%u)\n",
1119 __func__,
1120 be32_to_cpu(headerp->rm_body.rm_error.rm_vers_low),
1121 be32_to_cpu(headerp->rm_body.rm_error.rm_vers_high));
1122 break;
1123 case ERR_CHUNK:
1124 pr_err("%s: server reports header decoding error\n",
1125 __func__);
1126 break;
1127 default:
1128 pr_err("%s: server reports unknown error %d\n",
1129 __func__, rmerr);
1130 }
1131 status = -EREMOTEIO;
1132 r_xprt->rx_stats.bad_reply_count++;
1133 goto out;
1134
1135/* If no pending RPC transaction was matched, post a replacement
1136 * receive buffer before returning.
1137 */
1138out_shortreply:
1139 dprintk("RPC: %s: short/invalid reply\n", __func__);
Chuck Leverb0e178a2015-10-24 17:26:54 -04001140 goto repost;
1141
1142out_nomatch:
Chuck Leverfe97b472015-10-24 17:27:10 -04001143 spin_unlock_bh(&xprt->transport_lock);
Chuck Leverb0e178a2015-10-24 17:26:54 -04001144 dprintk("RPC: %s: no match for incoming xid 0x%08x len %d\n",
1145 __func__, be32_to_cpu(headerp->rm_xid),
1146 rep->rr_len);
1147 goto repost;
1148
1149out_duplicate:
Chuck Leverfe97b472015-10-24 17:27:10 -04001150 spin_unlock_bh(&xprt->transport_lock);
Chuck Leverb0e178a2015-10-24 17:26:54 -04001151 dprintk("RPC: %s: "
1152 "duplicate reply %p to RPC request %p: xid 0x%08x\n",
1153 __func__, rep, req, be32_to_cpu(headerp->rm_xid));
1154
1155repost:
1156 r_xprt->rx_stats.bad_reply_count++;
Chuck Leverb1573802016-09-15 10:56:35 -04001157 if (rpcrdma_ep_post_recv(&r_xprt->rx_ia, rep))
Chuck Leverb0e178a2015-10-24 17:26:54 -04001158 rpcrdma_recv_buffer_put(rep);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -04001159}