blob: c296468a6f965101db7c6b202368d10ccbf3d9f2 [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
52#ifdef RPC_DEBUG
53# define RPCDBG_FACILITY RPCDBG_TRANS
54#endif
55
56enum rpcrdma_chunktype {
57 rpcrdma_noch = 0,
58 rpcrdma_readch,
59 rpcrdma_areadch,
60 rpcrdma_writech,
61 rpcrdma_replych
62};
63
64#ifdef RPC_DEBUG
65static const char transfertypes[][12] = {
66 "pure inline", /* no chunks */
67 " read chunk", /* some argument via rdma read */
68 "*read chunk", /* entire request via rdma read */
69 "write chunk", /* some result via rdma write */
70 "reply chunk" /* entire reply via rdma write */
71};
72#endif
73
74/*
75 * Chunk assembly from upper layer xdr_buf.
76 *
77 * Prepare the passed-in xdr_buf into representation as RPC/RDMA chunk
78 * elements. Segments are then coalesced when registered, if possible
79 * within the selected memreg mode.
80 *
81 * Note, this routine is never called if the connection's memory
82 * registration strategy is 0 (bounce buffers).
83 */
84
85static int
Chuck Lever2a428b22007-10-26 13:30:43 -040086rpcrdma_convert_iovs(struct xdr_buf *xdrbuf, unsigned int pos,
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -040087 enum rpcrdma_chunktype type, struct rpcrdma_mr_seg *seg, int nsegs)
88{
89 int len, n = 0, p;
Tom Tuckerbd7ea312011-02-09 19:45:28 +000090 int page_base;
91 struct page **ppages;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -040092
93 if (pos == 0 && xdrbuf->head[0].iov_len) {
94 seg[n].mr_page = NULL;
95 seg[n].mr_offset = xdrbuf->head[0].iov_base;
96 seg[n].mr_len = xdrbuf->head[0].iov_len;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -040097 ++n;
98 }
99
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000100 len = xdrbuf->page_len;
101 ppages = xdrbuf->pages + (xdrbuf->page_base >> PAGE_SHIFT);
102 page_base = xdrbuf->page_base & ~PAGE_MASK;
103 p = 0;
104 while (len && n < nsegs) {
105 seg[n].mr_page = ppages[p];
106 seg[n].mr_offset = (void *)(unsigned long) page_base;
107 seg[n].mr_len = min_t(u32, PAGE_SIZE - page_base, len);
108 BUG_ON(seg[n].mr_len > PAGE_SIZE);
109 len -= seg[n].mr_len;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400110 ++n;
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000111 ++p;
112 page_base = 0; /* page offset only applies to first page */
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400113 }
114
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000115 /* Message overflows the seg array */
116 if (len && n == nsegs)
117 return 0;
118
James Lentini50e10922007-12-10 11:24:48 -0500119 if (xdrbuf->tail[0].iov_len) {
Tom Talpey9191ca32008-10-09 15:01:11 -0400120 /* the rpcrdma protocol allows us to omit any trailing
121 * xdr pad bytes, saving the server an RDMA operation. */
122 if (xdrbuf->tail[0].iov_len < 4 && xprt_rdma_pad_optimize)
123 return n;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400124 if (n == nsegs)
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000125 /* Tail remains, but we're out of segments */
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400126 return 0;
127 seg[n].mr_page = NULL;
128 seg[n].mr_offset = xdrbuf->tail[0].iov_base;
129 seg[n].mr_len = xdrbuf->tail[0].iov_len;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400130 ++n;
131 }
132
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400133 return n;
134}
135
136/*
137 * Create read/write chunk lists, and reply chunks, for RDMA
138 *
139 * Assume check against THRESHOLD has been done, and chunks are required.
140 * Assume only encoding one list entry for read|write chunks. The NFSv3
141 * protocol is simple enough to allow this as it only has a single "bulk
142 * result" in each procedure - complicated NFSv4 COMPOUNDs are not. (The
143 * RDMA/Sessions NFSv4 proposal addresses this for future v4 revs.)
144 *
145 * When used for a single reply chunk (which is a special write
146 * chunk used for the entire reply, rather than just the data), it
147 * is used primarily for READDIR and READLINK which would otherwise
148 * be severely size-limited by a small rdma inline read max. The server
149 * response will come back as an RDMA Write, followed by a message
150 * of type RDMA_NOMSG carrying the xid and length. As a result, reply
151 * chunks do not provide data alignment, however they do not require
152 * "fixup" (moving the response to the upper layer buffer) either.
153 *
154 * Encoding key for single-list chunks (HLOO = Handle32 Length32 Offset64):
155 *
156 * Read chunklist (a linked list):
157 * N elements, position P (same P for all chunks of same arg!):
158 * 1 - PHLOO - 1 - PHLOO - ... - 1 - PHLOO - 0
159 *
160 * Write chunklist (a list of (one) counted array):
161 * N elements:
162 * 1 - N - HLOO - HLOO - ... - HLOO - 0
163 *
164 * Reply chunk (a counted array):
165 * N elements:
166 * 1 - N - HLOO - HLOO - ... - HLOO
167 */
168
169static unsigned int
170rpcrdma_create_chunks(struct rpc_rqst *rqst, struct xdr_buf *target,
171 struct rpcrdma_msg *headerp, enum rpcrdma_chunktype type)
172{
173 struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
Trond Myklebusta4f08352013-01-08 09:10:21 -0500174 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400175 int nsegs, nchunks = 0;
Chuck Lever2a428b22007-10-26 13:30:43 -0400176 unsigned int pos;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400177 struct rpcrdma_mr_seg *seg = req->rl_segments;
178 struct rpcrdma_read_chunk *cur_rchunk = NULL;
179 struct rpcrdma_write_array *warray = NULL;
180 struct rpcrdma_write_chunk *cur_wchunk = NULL;
Al Viro2d8a9722007-10-29 04:37:58 +0000181 __be32 *iptr = headerp->rm_body.rm_chunks;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400182
183 if (type == rpcrdma_readch || type == rpcrdma_areadch) {
184 /* a read chunk - server will RDMA Read our memory */
185 cur_rchunk = (struct rpcrdma_read_chunk *) iptr;
186 } else {
187 /* a write or reply chunk - server will RDMA Write our memory */
188 *iptr++ = xdr_zero; /* encode a NULL read chunk list */
189 if (type == rpcrdma_replych)
190 *iptr++ = xdr_zero; /* a NULL write chunk list */
191 warray = (struct rpcrdma_write_array *) iptr;
192 cur_wchunk = (struct rpcrdma_write_chunk *) (warray + 1);
193 }
194
195 if (type == rpcrdma_replych || type == rpcrdma_areadch)
196 pos = 0;
197 else
198 pos = target->head[0].iov_len;
199
200 nsegs = rpcrdma_convert_iovs(target, pos, type, seg, RPCRDMA_MAX_SEGS);
201 if (nsegs == 0)
202 return 0;
203
204 do {
205 /* bind/register the memory, then build chunk from result. */
206 int n = rpcrdma_register_external(seg, nsegs,
207 cur_wchunk != NULL, r_xprt);
208 if (n <= 0)
209 goto out;
210 if (cur_rchunk) { /* read */
211 cur_rchunk->rc_discrim = xdr_one;
212 /* all read chunks have the same "position" */
213 cur_rchunk->rc_position = htonl(pos);
214 cur_rchunk->rc_target.rs_handle = htonl(seg->mr_rkey);
215 cur_rchunk->rc_target.rs_length = htonl(seg->mr_len);
216 xdr_encode_hyper(
Al Viro2d8a9722007-10-29 04:37:58 +0000217 (__be32 *)&cur_rchunk->rc_target.rs_offset,
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400218 seg->mr_base);
219 dprintk("RPC: %s: read chunk "
Chuck Lever2a428b22007-10-26 13:30:43 -0400220 "elem %d@0x%llx:0x%x pos %u (%s)\n", __func__,
Stephen Rothwelle08a1322007-10-30 00:44:32 -0700221 seg->mr_len, (unsigned long long)seg->mr_base,
222 seg->mr_rkey, pos, n < nsegs ? "more" : "last");
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400223 cur_rchunk++;
224 r_xprt->rx_stats.read_chunk_count++;
225 } else { /* write/reply */
226 cur_wchunk->wc_target.rs_handle = htonl(seg->mr_rkey);
227 cur_wchunk->wc_target.rs_length = htonl(seg->mr_len);
228 xdr_encode_hyper(
Al Viro2d8a9722007-10-29 04:37:58 +0000229 (__be32 *)&cur_wchunk->wc_target.rs_offset,
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400230 seg->mr_base);
231 dprintk("RPC: %s: %s chunk "
232 "elem %d@0x%llx:0x%x (%s)\n", __func__,
233 (type == rpcrdma_replych) ? "reply" : "write",
Stephen Rothwelle08a1322007-10-30 00:44:32 -0700234 seg->mr_len, (unsigned long long)seg->mr_base,
235 seg->mr_rkey, n < nsegs ? "more" : "last");
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400236 cur_wchunk++;
237 if (type == rpcrdma_replych)
238 r_xprt->rx_stats.reply_chunk_count++;
239 else
240 r_xprt->rx_stats.write_chunk_count++;
241 r_xprt->rx_stats.total_rdma_request += seg->mr_len;
242 }
243 nchunks++;
244 seg += n;
245 nsegs -= n;
246 } while (nsegs);
247
248 /* success. all failures return above */
249 req->rl_nchunks = nchunks;
250
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400251 /*
252 * finish off header. If write, marshal discrim and nchunks.
253 */
254 if (cur_rchunk) {
Al Viro2d8a9722007-10-29 04:37:58 +0000255 iptr = (__be32 *) cur_rchunk;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400256 *iptr++ = xdr_zero; /* finish the read chunk list */
257 *iptr++ = xdr_zero; /* encode a NULL write chunk list */
258 *iptr++ = xdr_zero; /* encode a NULL reply chunk */
259 } else {
260 warray->wc_discrim = xdr_one;
261 warray->wc_nchunks = htonl(nchunks);
Al Viro2d8a9722007-10-29 04:37:58 +0000262 iptr = (__be32 *) cur_wchunk;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400263 if (type == rpcrdma_writech) {
264 *iptr++ = xdr_zero; /* finish the write chunk list */
265 *iptr++ = xdr_zero; /* encode a NULL reply chunk */
266 }
267 }
268
269 /*
270 * Return header size.
271 */
272 return (unsigned char *)iptr - (unsigned char *)headerp;
273
274out:
275 for (pos = 0; nchunks--;)
276 pos += rpcrdma_deregister_external(
277 &req->rl_segments[pos], r_xprt, NULL);
278 return 0;
279}
280
281/*
282 * Copy write data inline.
283 * This function is used for "small" requests. Data which is passed
284 * to RPC via iovecs (or page list) is copied directly into the
285 * pre-registered memory buffer for this request. For small amounts
286 * of data, this is efficient. The cutoff value is tunable.
287 */
288static int
289rpcrdma_inline_pullup(struct rpc_rqst *rqst, int pad)
290{
291 int i, npages, curlen;
292 int copy_len;
293 unsigned char *srcp, *destp;
294 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000295 int page_base;
296 struct page **ppages;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400297
298 destp = rqst->rq_svec[0].iov_base;
299 curlen = rqst->rq_svec[0].iov_len;
300 destp += curlen;
301 /*
302 * Do optional padding where it makes sense. Alignment of write
303 * payload can help the server, if our setting is accurate.
304 */
305 pad -= (curlen + 36/*sizeof(struct rpcrdma_msg_padded)*/);
306 if (pad < 0 || rqst->rq_slen - curlen < RPCRDMA_INLINE_PAD_THRESH)
307 pad = 0; /* don't pad this request */
308
309 dprintk("RPC: %s: pad %d destp 0x%p len %d hdrlen %d\n",
310 __func__, pad, destp, rqst->rq_slen, curlen);
311
312 copy_len = rqst->rq_snd_buf.page_len;
Tom Talpeyb38ab402009-03-11 14:37:55 -0400313
314 if (rqst->rq_snd_buf.tail[0].iov_len) {
315 curlen = rqst->rq_snd_buf.tail[0].iov_len;
316 if (destp + copy_len != rqst->rq_snd_buf.tail[0].iov_base) {
317 memmove(destp + copy_len,
318 rqst->rq_snd_buf.tail[0].iov_base, curlen);
319 r_xprt->rx_stats.pullup_copy_count += curlen;
320 }
321 dprintk("RPC: %s: tail destp 0x%p len %d\n",
322 __func__, destp + copy_len, curlen);
323 rqst->rq_svec[0].iov_len += curlen;
324 }
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400325 r_xprt->rx_stats.pullup_copy_count += copy_len;
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000326
327 page_base = rqst->rq_snd_buf.page_base;
328 ppages = rqst->rq_snd_buf.pages + (page_base >> PAGE_SHIFT);
329 page_base &= ~PAGE_MASK;
330 npages = PAGE_ALIGN(page_base+copy_len) >> PAGE_SHIFT;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400331 for (i = 0; copy_len && i < npages; i++) {
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000332 curlen = PAGE_SIZE - page_base;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400333 if (curlen > copy_len)
334 curlen = copy_len;
335 dprintk("RPC: %s: page %d destp 0x%p len %d curlen %d\n",
336 __func__, i, destp, copy_len, curlen);
Cong Wangb8541782011-11-25 23:14:40 +0800337 srcp = kmap_atomic(ppages[i]);
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000338 memcpy(destp, srcp+page_base, curlen);
Cong Wangb8541782011-11-25 23:14:40 +0800339 kunmap_atomic(srcp);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400340 rqst->rq_svec[0].iov_len += curlen;
341 destp += curlen;
342 copy_len -= curlen;
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000343 page_base = 0;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400344 }
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400345 /* header now contains entire send message */
346 return pad;
347}
348
349/*
350 * Marshal a request: the primary job of this routine is to choose
351 * the transfer modes. See comments below.
352 *
353 * Uses multiple RDMA IOVs for a request:
354 * [0] -- RPC RDMA header, which uses memory from the *start* of the
355 * preregistered buffer that already holds the RPC data in
356 * its middle.
357 * [1] -- the RPC header/data, marshaled by RPC and the NFS protocol.
358 * [2] -- optional padding.
359 * [3] -- if padded, header only in [1] and data here.
360 */
361
362int
363rpcrdma_marshal_req(struct rpc_rqst *rqst)
364{
Trond Myklebusta4f08352013-01-08 09:10:21 -0500365 struct rpc_xprt *xprt = rqst->rq_xprt;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400366 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
367 struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
368 char *base;
369 size_t hdrlen, rpclen, padlen;
370 enum rpcrdma_chunktype rtype, wtype;
371 struct rpcrdma_msg *headerp;
372
373 /*
374 * rpclen gets amount of data in first buffer, which is the
375 * pre-registered buffer.
376 */
377 base = rqst->rq_svec[0].iov_base;
378 rpclen = rqst->rq_svec[0].iov_len;
379
380 /* build RDMA header in private area at front */
381 headerp = (struct rpcrdma_msg *) req->rl_base;
382 /* don't htonl XID, it's already done in request */
383 headerp->rm_xid = rqst->rq_xid;
384 headerp->rm_vers = xdr_one;
385 headerp->rm_credit = htonl(r_xprt->rx_buf.rb_max_requests);
YOSHIFUJI Hideaki8d614432007-12-12 03:55:42 +0900386 headerp->rm_type = htonl(RDMA_MSG);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400387
388 /*
389 * Chunks needed for results?
390 *
391 * o If the expected result is under the inline threshold, all ops
392 * return as inline (but see later).
393 * o Large non-read ops return as a single reply chunk.
394 * o Large read ops return data as write chunk(s), header as inline.
395 *
396 * Note: the NFS code sending down multiple result segments implies
397 * the op is one of read, readdir[plus], readlink or NFSv4 getacl.
398 */
399
400 /*
401 * This code can handle read chunks, write chunks OR reply
402 * chunks -- only one type. If the request is too big to fit
403 * inline, then we will choose read chunks. If the request is
404 * a READ, then use write chunks to separate the file data
405 * into pages; otherwise use reply chunks.
406 */
407 if (rqst->rq_rcv_buf.buflen <= RPCRDMA_INLINE_READ_THRESHOLD(rqst))
408 wtype = rpcrdma_noch;
409 else if (rqst->rq_rcv_buf.page_len == 0)
410 wtype = rpcrdma_replych;
411 else if (rqst->rq_rcv_buf.flags & XDRBUF_READ)
412 wtype = rpcrdma_writech;
413 else
414 wtype = rpcrdma_replych;
415
416 /*
417 * Chunks needed for arguments?
418 *
419 * o If the total request is under the inline threshold, all ops
420 * are sent as inline.
421 * o Large non-write ops are sent with the entire message as a
422 * single read chunk (protocol 0-position special case).
423 * o Large write ops transmit data as read chunk(s), header as
424 * inline.
425 *
426 * Note: the NFS code sending down multiple argument segments
427 * implies the op is a write.
428 * TBD check NFSv4 setacl
429 */
430 if (rqst->rq_snd_buf.len <= RPCRDMA_INLINE_WRITE_THRESHOLD(rqst))
431 rtype = rpcrdma_noch;
432 else if (rqst->rq_snd_buf.page_len == 0)
433 rtype = rpcrdma_areadch;
434 else
435 rtype = rpcrdma_readch;
436
437 /* The following simplification is not true forever */
438 if (rtype != rpcrdma_noch && wtype == rpcrdma_replych)
439 wtype = rpcrdma_noch;
440 BUG_ON(rtype != rpcrdma_noch && wtype != rpcrdma_noch);
441
442 if (r_xprt->rx_ia.ri_memreg_strategy == RPCRDMA_BOUNCEBUFFERS &&
443 (rtype != rpcrdma_noch || wtype != rpcrdma_noch)) {
444 /* forced to "pure inline"? */
445 dprintk("RPC: %s: too much data (%d/%d) for inline\n",
446 __func__, rqst->rq_rcv_buf.len, rqst->rq_snd_buf.len);
447 return -1;
448 }
449
450 hdrlen = 28; /*sizeof *headerp;*/
451 padlen = 0;
452
453 /*
454 * Pull up any extra send data into the preregistered buffer.
455 * When padding is in use and applies to the transfer, insert
456 * it and change the message type.
457 */
458 if (rtype == rpcrdma_noch) {
459
460 padlen = rpcrdma_inline_pullup(rqst,
461 RPCRDMA_INLINE_PAD_VALUE(rqst));
462
463 if (padlen) {
YOSHIFUJI Hideaki8d614432007-12-12 03:55:42 +0900464 headerp->rm_type = htonl(RDMA_MSGP);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400465 headerp->rm_body.rm_padded.rm_align =
466 htonl(RPCRDMA_INLINE_PAD_VALUE(rqst));
467 headerp->rm_body.rm_padded.rm_thresh =
YOSHIFUJI Hideaki8d614432007-12-12 03:55:42 +0900468 htonl(RPCRDMA_INLINE_PAD_THRESH);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400469 headerp->rm_body.rm_padded.rm_pempty[0] = xdr_zero;
470 headerp->rm_body.rm_padded.rm_pempty[1] = xdr_zero;
471 headerp->rm_body.rm_padded.rm_pempty[2] = xdr_zero;
472 hdrlen += 2 * sizeof(u32); /* extra words in padhdr */
473 BUG_ON(wtype != rpcrdma_noch);
474
475 } else {
476 headerp->rm_body.rm_nochunks.rm_empty[0] = xdr_zero;
477 headerp->rm_body.rm_nochunks.rm_empty[1] = xdr_zero;
478 headerp->rm_body.rm_nochunks.rm_empty[2] = xdr_zero;
479 /* new length after pullup */
480 rpclen = rqst->rq_svec[0].iov_len;
481 /*
482 * Currently we try to not actually use read inline.
483 * Reply chunks have the desirable property that
484 * they land, packed, directly in the target buffers
485 * without headers, so they require no fixup. The
486 * additional RDMA Write op sends the same amount
487 * of data, streams on-the-wire and adds no overhead
488 * on receive. Therefore, we request a reply chunk
489 * for non-writes wherever feasible and efficient.
490 */
491 if (wtype == rpcrdma_noch &&
492 r_xprt->rx_ia.ri_memreg_strategy > RPCRDMA_REGISTER)
493 wtype = rpcrdma_replych;
494 }
495 }
496
497 /*
498 * Marshal chunks. This routine will return the header length
499 * consumed by marshaling.
500 */
501 if (rtype != rpcrdma_noch) {
502 hdrlen = rpcrdma_create_chunks(rqst,
503 &rqst->rq_snd_buf, headerp, rtype);
504 wtype = rtype; /* simplify dprintk */
505
506 } else if (wtype != rpcrdma_noch) {
507 hdrlen = rpcrdma_create_chunks(rqst,
508 &rqst->rq_rcv_buf, headerp, wtype);
509 }
510
511 if (hdrlen == 0)
512 return -1;
513
Tom Talpey5f37d562008-10-09 15:01:52 -0400514 dprintk("RPC: %s: %s: hdrlen %zd rpclen %zd padlen %zd"
515 " headerp 0x%p base 0x%p lkey 0x%x\n",
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400516 __func__, transfertypes[wtype], hdrlen, rpclen, padlen,
517 headerp, base, req->rl_iov.lkey);
518
519 /*
520 * initialize send_iov's - normally only two: rdma chunk header and
521 * single preregistered RPC header buffer, but if padding is present,
522 * then use a preregistered (and zeroed) pad buffer between the RPC
523 * header and any write data. In all non-rdma cases, any following
524 * data has been copied into the RPC header buffer.
525 */
526 req->rl_send_iov[0].addr = req->rl_iov.addr;
527 req->rl_send_iov[0].length = hdrlen;
528 req->rl_send_iov[0].lkey = req->rl_iov.lkey;
529
530 req->rl_send_iov[1].addr = req->rl_iov.addr + (base - req->rl_base);
531 req->rl_send_iov[1].length = rpclen;
532 req->rl_send_iov[1].lkey = req->rl_iov.lkey;
533
534 req->rl_niovs = 2;
535
536 if (padlen) {
537 struct rpcrdma_ep *ep = &r_xprt->rx_ep;
538
539 req->rl_send_iov[2].addr = ep->rep_pad.addr;
540 req->rl_send_iov[2].length = padlen;
541 req->rl_send_iov[2].lkey = ep->rep_pad.lkey;
542
543 req->rl_send_iov[3].addr = req->rl_send_iov[1].addr + rpclen;
544 req->rl_send_iov[3].length = rqst->rq_slen - rpclen;
545 req->rl_send_iov[3].lkey = req->rl_iov.lkey;
546
547 req->rl_niovs = 4;
548 }
549
550 return 0;
551}
552
553/*
554 * Chase down a received write or reply chunklist to get length
555 * RDMA'd by server. See map at rpcrdma_create_chunks()! :-)
556 */
557static int
Chuck Leverd4b37ff2007-10-26 13:30:49 -0400558rpcrdma_count_chunks(struct rpcrdma_rep *rep, unsigned int max, int wrchunk, __be32 **iptrp)
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400559{
560 unsigned int i, total_len;
561 struct rpcrdma_write_chunk *cur_wchunk;
562
563 i = ntohl(**iptrp); /* get array count */
564 if (i > max)
565 return -1;
566 cur_wchunk = (struct rpcrdma_write_chunk *) (*iptrp + 1);
567 total_len = 0;
568 while (i--) {
569 struct rpcrdma_segment *seg = &cur_wchunk->wc_target;
570 ifdebug(FACILITY) {
571 u64 off;
Al Viro2d8a9722007-10-29 04:37:58 +0000572 xdr_decode_hyper((__be32 *)&seg->rs_offset, &off);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400573 dprintk("RPC: %s: chunk %d@0x%llx:0x%x\n",
574 __func__,
575 ntohl(seg->rs_length),
Stephen Rothwelle08a1322007-10-30 00:44:32 -0700576 (unsigned long long)off,
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400577 ntohl(seg->rs_handle));
578 }
579 total_len += ntohl(seg->rs_length);
580 ++cur_wchunk;
581 }
582 /* check and adjust for properly terminated write chunk */
583 if (wrchunk) {
Al Viro2d8a9722007-10-29 04:37:58 +0000584 __be32 *w = (__be32 *) cur_wchunk;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400585 if (*w++ != xdr_zero)
586 return -1;
587 cur_wchunk = (struct rpcrdma_write_chunk *) w;
588 }
589 if ((char *) cur_wchunk > rep->rr_base + rep->rr_len)
590 return -1;
591
Al Viro2d8a9722007-10-29 04:37:58 +0000592 *iptrp = (__be32 *) cur_wchunk;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400593 return total_len;
594}
595
596/*
597 * Scatter inline received data back into provided iov's.
598 */
599static void
Tom Talpey9191ca32008-10-09 15:01:11 -0400600rpcrdma_inline_fixup(struct rpc_rqst *rqst, char *srcp, int copy_len, int pad)
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400601{
602 int i, npages, curlen, olen;
603 char *destp;
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000604 struct page **ppages;
605 int page_base;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400606
607 curlen = rqst->rq_rcv_buf.head[0].iov_len;
608 if (curlen > copy_len) { /* write chunk header fixup */
609 curlen = copy_len;
610 rqst->rq_rcv_buf.head[0].iov_len = curlen;
611 }
612
613 dprintk("RPC: %s: srcp 0x%p len %d hdrlen %d\n",
614 __func__, srcp, copy_len, curlen);
615
616 /* Shift pointer for first receive segment only */
617 rqst->rq_rcv_buf.head[0].iov_base = srcp;
618 srcp += curlen;
619 copy_len -= curlen;
620
621 olen = copy_len;
622 i = 0;
623 rpcx_to_rdmax(rqst->rq_xprt)->rx_stats.fixup_copy_count += olen;
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000624 page_base = rqst->rq_rcv_buf.page_base;
625 ppages = rqst->rq_rcv_buf.pages + (page_base >> PAGE_SHIFT);
626 page_base &= ~PAGE_MASK;
627
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400628 if (copy_len && rqst->rq_rcv_buf.page_len) {
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000629 npages = PAGE_ALIGN(page_base +
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400630 rqst->rq_rcv_buf.page_len) >> PAGE_SHIFT;
631 for (; i < npages; i++) {
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000632 curlen = PAGE_SIZE - page_base;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400633 if (curlen > copy_len)
634 curlen = copy_len;
635 dprintk("RPC: %s: page %d"
636 " srcp 0x%p len %d curlen %d\n",
637 __func__, i, srcp, copy_len, curlen);
Cong Wangb8541782011-11-25 23:14:40 +0800638 destp = kmap_atomic(ppages[i]);
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000639 memcpy(destp + page_base, srcp, curlen);
640 flush_dcache_page(ppages[i]);
Cong Wangb8541782011-11-25 23:14:40 +0800641 kunmap_atomic(destp);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400642 srcp += curlen;
643 copy_len -= curlen;
644 if (copy_len == 0)
645 break;
Tom Tuckerbd7ea312011-02-09 19:45:28 +0000646 page_base = 0;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400647 }
Chuck Lever2b7bbc92014-03-12 12:51:30 -0400648 }
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400649
650 if (copy_len && rqst->rq_rcv_buf.tail[0].iov_len) {
651 curlen = copy_len;
652 if (curlen > rqst->rq_rcv_buf.tail[0].iov_len)
653 curlen = rqst->rq_rcv_buf.tail[0].iov_len;
654 if (rqst->rq_rcv_buf.tail[0].iov_base != srcp)
Tom Talpeyb38ab402009-03-11 14:37:55 -0400655 memmove(rqst->rq_rcv_buf.tail[0].iov_base, srcp, curlen);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400656 dprintk("RPC: %s: tail srcp 0x%p len %d curlen %d\n",
657 __func__, srcp, copy_len, curlen);
658 rqst->rq_rcv_buf.tail[0].iov_len = curlen;
659 copy_len -= curlen; ++i;
660 } else
661 rqst->rq_rcv_buf.tail[0].iov_len = 0;
662
Tom Talpey9191ca32008-10-09 15:01:11 -0400663 if (pad) {
664 /* implicit padding on terminal chunk */
665 unsigned char *p = rqst->rq_rcv_buf.tail[0].iov_base;
666 while (pad--)
667 p[rqst->rq_rcv_buf.tail[0].iov_len++] = 0;
668 }
669
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400670 if (copy_len)
671 dprintk("RPC: %s: %d bytes in"
672 " %d extra segments (%d lost)\n",
673 __func__, olen, i, copy_len);
674
675 /* TBD avoid a warning from call_decode() */
676 rqst->rq_private_buf = rqst->rq_rcv_buf;
677}
678
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400679void
Chuck Lever254f91e2014-05-28 10:32:17 -0400680rpcrdma_connect_worker(struct work_struct *work)
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400681{
Chuck Lever254f91e2014-05-28 10:32:17 -0400682 struct rpcrdma_ep *ep =
683 container_of(work, struct rpcrdma_ep, rep_connect_worker.work);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400684 struct rpc_xprt *xprt = ep->rep_xprt;
685
686 spin_lock_bh(&xprt->transport_lock);
Tom Talpey575448b2008-10-09 15:00:40 -0400687 if (++xprt->connect_cookie == 0) /* maintain a reserved value */
688 ++xprt->connect_cookie;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400689 if (ep->rep_connected > 0) {
690 if (!xprt_test_and_set_connected(xprt))
691 xprt_wake_pending_tasks(xprt, 0);
692 } else {
693 if (xprt_test_and_clear_connected(xprt))
Tom Talpey926449b2008-10-09 15:01:21 -0400694 xprt_wake_pending_tasks(xprt, -ENOTCONN);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400695 }
696 spin_unlock_bh(&xprt->transport_lock);
697}
698
699/*
Chuck Lever254f91e2014-05-28 10:32:17 -0400700 * This function is called when an async event is posted to
701 * the connection which changes the connection state. All it
702 * does at this point is mark the connection up/down, the rpc
703 * timers do the rest.
704 */
705void
706rpcrdma_conn_func(struct rpcrdma_ep *ep)
707{
708 schedule_delayed_work(&ep->rep_connect_worker, 0);
709}
710
711/*
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400712 * This function is called when memory window unbind which we are waiting
713 * for completes. Just use rr_func (zeroed by upcall) to signal completion.
714 */
715static void
716rpcrdma_unbind_func(struct rpcrdma_rep *rep)
717{
718 wake_up(&rep->rr_unbind);
719}
720
721/*
722 * Called as a tasklet to do req/reply match and complete a request
723 * Errors must result in the RPC task either being awakened, or
724 * allowed to timeout, to discover the errors at that time.
725 */
726void
727rpcrdma_reply_handler(struct rpcrdma_rep *rep)
728{
729 struct rpcrdma_msg *headerp;
730 struct rpcrdma_req *req;
731 struct rpc_rqst *rqst;
732 struct rpc_xprt *xprt = rep->rr_xprt;
733 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
Al Viro2d8a9722007-10-29 04:37:58 +0000734 __be32 *iptr;
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400735 int i, rdmalen, status;
736
737 /* Check status. If bad, signal disconnect and return rep to pool */
738 if (rep->rr_len == ~0U) {
739 rpcrdma_recv_buffer_put(rep);
740 if (r_xprt->rx_ep.rep_connected == 1) {
741 r_xprt->rx_ep.rep_connected = -EIO;
742 rpcrdma_conn_func(&r_xprt->rx_ep);
743 }
744 return;
745 }
746 if (rep->rr_len < 28) {
747 dprintk("RPC: %s: short/invalid reply\n", __func__);
748 goto repost;
749 }
750 headerp = (struct rpcrdma_msg *) rep->rr_base;
751 if (headerp->rm_vers != xdr_one) {
752 dprintk("RPC: %s: invalid version %d\n",
753 __func__, ntohl(headerp->rm_vers));
754 goto repost;
755 }
756
757 /* Get XID and try for a match. */
758 spin_lock(&xprt->transport_lock);
759 rqst = xprt_lookup_rqst(xprt, headerp->rm_xid);
760 if (rqst == NULL) {
761 spin_unlock(&xprt->transport_lock);
762 dprintk("RPC: %s: reply 0x%p failed "
763 "to match any request xid 0x%08x len %d\n",
764 __func__, rep, headerp->rm_xid, rep->rr_len);
765repost:
766 r_xprt->rx_stats.bad_reply_count++;
767 rep->rr_func = rpcrdma_reply_handler;
768 if (rpcrdma_ep_post_recv(&r_xprt->rx_ia, &r_xprt->rx_ep, rep))
769 rpcrdma_recv_buffer_put(rep);
770
771 return;
772 }
773
774 /* get request object */
775 req = rpcr_to_rdmar(rqst);
Tom Tucker4a6862b2012-02-20 13:07:42 -0600776 if (req->rl_reply) {
777 spin_unlock(&xprt->transport_lock);
778 dprintk("RPC: %s: duplicate reply 0x%p to RPC "
779 "request 0x%p: xid 0x%08x\n", __func__, rep, req,
780 headerp->rm_xid);
781 goto repost;
782 }
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400783
784 dprintk("RPC: %s: reply 0x%p completes request 0x%p\n"
785 " RPC request 0x%p xid 0x%08x\n",
786 __func__, rep, req, rqst, headerp->rm_xid);
787
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400788 /* from here on, the reply is no longer an orphan */
789 req->rl_reply = rep;
790
791 /* check for expected message types */
792 /* The order of some of these tests is important. */
793 switch (headerp->rm_type) {
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700794 case htonl(RDMA_MSG):
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400795 /* never expect read chunks */
796 /* never expect reply chunks (two ways to check) */
797 /* never expect write chunks without having offered RDMA */
798 if (headerp->rm_body.rm_chunks[0] != xdr_zero ||
799 (headerp->rm_body.rm_chunks[1] == xdr_zero &&
800 headerp->rm_body.rm_chunks[2] != xdr_zero) ||
801 (headerp->rm_body.rm_chunks[1] != xdr_zero &&
802 req->rl_nchunks == 0))
803 goto badheader;
804 if (headerp->rm_body.rm_chunks[1] != xdr_zero) {
805 /* count any expected write chunks in read reply */
806 /* start at write chunk array count */
807 iptr = &headerp->rm_body.rm_chunks[2];
808 rdmalen = rpcrdma_count_chunks(rep,
809 req->rl_nchunks, 1, &iptr);
810 /* check for validity, and no reply chunk after */
811 if (rdmalen < 0 || *iptr++ != xdr_zero)
812 goto badheader;
813 rep->rr_len -=
814 ((unsigned char *)iptr - (unsigned char *)headerp);
815 status = rep->rr_len + rdmalen;
816 r_xprt->rx_stats.total_rdma_reply += rdmalen;
Tom Talpey9191ca32008-10-09 15:01:11 -0400817 /* special case - last chunk may omit padding */
818 if (rdmalen &= 3) {
819 rdmalen = 4 - rdmalen;
820 status += rdmalen;
821 }
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400822 } else {
823 /* else ordinary inline */
Tom Talpey9191ca32008-10-09 15:01:11 -0400824 rdmalen = 0;
Al Viro2d8a9722007-10-29 04:37:58 +0000825 iptr = (__be32 *)((unsigned char *)headerp + 28);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400826 rep->rr_len -= 28; /*sizeof *headerp;*/
827 status = rep->rr_len;
828 }
829 /* Fix up the rpc results for upper layer */
Tom Talpey9191ca32008-10-09 15:01:11 -0400830 rpcrdma_inline_fixup(rqst, (char *)iptr, rep->rr_len, rdmalen);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400831 break;
832
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700833 case htonl(RDMA_NOMSG):
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400834 /* never expect read or write chunks, always reply chunks */
835 if (headerp->rm_body.rm_chunks[0] != xdr_zero ||
836 headerp->rm_body.rm_chunks[1] != xdr_zero ||
837 headerp->rm_body.rm_chunks[2] != xdr_one ||
838 req->rl_nchunks == 0)
839 goto badheader;
Al Viro2d8a9722007-10-29 04:37:58 +0000840 iptr = (__be32 *)((unsigned char *)headerp + 28);
\"Talpey, Thomas\e9601822007-09-10 13:50:42 -0400841 rdmalen = rpcrdma_count_chunks(rep, req->rl_nchunks, 0, &iptr);
842 if (rdmalen < 0)
843 goto badheader;
844 r_xprt->rx_stats.total_rdma_reply += rdmalen;
845 /* Reply chunk buffer already is the reply vector - no fixup. */
846 status = rdmalen;
847 break;
848
849badheader:
850 default:
851 dprintk("%s: invalid rpcrdma reply header (type %d):"
852 " chunks[012] == %d %d %d"
853 " expected chunks <= %d\n",
854 __func__, ntohl(headerp->rm_type),
855 headerp->rm_body.rm_chunks[0],
856 headerp->rm_body.rm_chunks[1],
857 headerp->rm_body.rm_chunks[2],
858 req->rl_nchunks);
859 status = -EIO;
860 r_xprt->rx_stats.bad_reply_count++;
861 break;
862 }
863
864 /* If using mw bind, start the deregister process now. */
865 /* (Note: if mr_free(), cannot perform it here, in tasklet context) */
866 if (req->rl_nchunks) switch (r_xprt->rx_ia.ri_memreg_strategy) {
867 case RPCRDMA_MEMWINDOWS:
868 for (i = 0; req->rl_nchunks-- > 1;)
869 i += rpcrdma_deregister_external(
870 &req->rl_segments[i], r_xprt, NULL);
871 /* Optionally wait (not here) for unbinds to complete */
872 rep->rr_func = rpcrdma_unbind_func;
873 (void) rpcrdma_deregister_external(&req->rl_segments[i],
874 r_xprt, rep);
875 break;
876 case RPCRDMA_MEMWINDOWS_ASYNC:
877 for (i = 0; req->rl_nchunks--;)
878 i += rpcrdma_deregister_external(&req->rl_segments[i],
879 r_xprt, NULL);
880 break;
881 default:
882 break;
883 }
884
885 dprintk("RPC: %s: xprt_complete_rqst(0x%p, 0x%p, %d)\n",
886 __func__, xprt, rqst, status);
887 xprt_complete_rqst(rqst->rq_task, status);
888 spin_unlock(&xprt->transport_lock);
889}