xprtrdma: Remove BUG_ON() call sites
[deliverable/linux.git] / net / sunrpc / xprtrdma / rpc_rdma.c
CommitLineData
f58851e6 1/*
e9601828
TT
2 * 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.
f58851e6
TT
46 */
47
48#include "xprt_rdma.h"
49
e9601828
TT
50#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.
e9601828
TT
80 */
81
82static int
2a428b2b 83rpcrdma_convert_iovs(struct xdr_buf *xdrbuf, unsigned int pos,
e9601828
TT
84 enum rpcrdma_chunktype type, struct rpcrdma_mr_seg *seg, int nsegs)
85{
86 int len, n = 0, p;
bd7ea31b
TT
87 int page_base;
88 struct page **ppages;
e9601828
TT
89
90 if (pos == 0 && xdrbuf->head[0].iov_len) {
91 seg[n].mr_page = NULL;
92 seg[n].mr_offset = xdrbuf->head[0].iov_base;
93 seg[n].mr_len = xdrbuf->head[0].iov_len;
e9601828
TT
94 ++n;
95 }
96
bd7ea31b
TT
97 len = xdrbuf->page_len;
98 ppages = xdrbuf->pages + (xdrbuf->page_base >> PAGE_SHIFT);
99 page_base = xdrbuf->page_base & ~PAGE_MASK;
100 p = 0;
101 while (len && n < nsegs) {
196c6998
SM
102 if (!ppages[p]) {
103 /* alloc the pagelist for receiving buffer */
104 ppages[p] = alloc_page(GFP_ATOMIC);
105 if (!ppages[p])
106 return 0;
107 }
bd7ea31b
TT
108 seg[n].mr_page = ppages[p];
109 seg[n].mr_offset = (void *)(unsigned long) page_base;
110 seg[n].mr_len = min_t(u32, PAGE_SIZE - page_base, len);
111 BUG_ON(seg[n].mr_len > PAGE_SIZE);
112 len -= seg[n].mr_len;
e9601828 113 ++n;
bd7ea31b
TT
114 ++p;
115 page_base = 0; /* page offset only applies to first page */
e9601828
TT
116 }
117
bd7ea31b
TT
118 /* Message overflows the seg array */
119 if (len && n == nsegs)
120 return 0;
121
50e1092b 122 if (xdrbuf->tail[0].iov_len) {
9191ca3b
TT
123 /* the rpcrdma protocol allows us to omit any trailing
124 * xdr pad bytes, saving the server an RDMA operation. */
125 if (xdrbuf->tail[0].iov_len < 4 && xprt_rdma_pad_optimize)
126 return n;
e9601828 127 if (n == nsegs)
bd7ea31b 128 /* Tail remains, but we're out of segments */
e9601828
TT
129 return 0;
130 seg[n].mr_page = NULL;
131 seg[n].mr_offset = xdrbuf->tail[0].iov_base;
132 seg[n].mr_len = xdrbuf->tail[0].iov_len;
e9601828
TT
133 ++n;
134 }
135
e9601828
TT
136 return n;
137}
138
139/*
140 * Create read/write chunk lists, and reply chunks, for RDMA
141 *
142 * Assume check against THRESHOLD has been done, and chunks are required.
143 * Assume only encoding one list entry for read|write chunks. The NFSv3
144 * protocol is simple enough to allow this as it only has a single "bulk
145 * result" in each procedure - complicated NFSv4 COMPOUNDs are not. (The
146 * RDMA/Sessions NFSv4 proposal addresses this for future v4 revs.)
147 *
148 * When used for a single reply chunk (which is a special write
149 * chunk used for the entire reply, rather than just the data), it
150 * is used primarily for READDIR and READLINK which would otherwise
151 * be severely size-limited by a small rdma inline read max. The server
152 * response will come back as an RDMA Write, followed by a message
153 * of type RDMA_NOMSG carrying the xid and length. As a result, reply
154 * chunks do not provide data alignment, however they do not require
155 * "fixup" (moving the response to the upper layer buffer) either.
156 *
157 * Encoding key for single-list chunks (HLOO = Handle32 Length32 Offset64):
158 *
159 * Read chunklist (a linked list):
160 * N elements, position P (same P for all chunks of same arg!):
161 * 1 - PHLOO - 1 - PHLOO - ... - 1 - PHLOO - 0
162 *
163 * Write chunklist (a list of (one) counted array):
164 * N elements:
165 * 1 - N - HLOO - HLOO - ... - HLOO - 0
166 *
167 * Reply chunk (a counted array):
168 * N elements:
169 * 1 - N - HLOO - HLOO - ... - HLOO
170 */
171
172static unsigned int
173rpcrdma_create_chunks(struct rpc_rqst *rqst, struct xdr_buf *target,
174 struct rpcrdma_msg *headerp, enum rpcrdma_chunktype type)
175{
176 struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
a4f0835c 177 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
e9601828 178 int nsegs, nchunks = 0;
2a428b2b 179 unsigned int pos;
e9601828
TT
180 struct rpcrdma_mr_seg *seg = req->rl_segments;
181 struct rpcrdma_read_chunk *cur_rchunk = NULL;
182 struct rpcrdma_write_array *warray = NULL;
183 struct rpcrdma_write_chunk *cur_wchunk = NULL;
2d8a9726 184 __be32 *iptr = headerp->rm_body.rm_chunks;
e9601828
TT
185
186 if (type == rpcrdma_readch || type == rpcrdma_areadch) {
187 /* a read chunk - server will RDMA Read our memory */
188 cur_rchunk = (struct rpcrdma_read_chunk *) iptr;
189 } else {
190 /* a write or reply chunk - server will RDMA Write our memory */
191 *iptr++ = xdr_zero; /* encode a NULL read chunk list */
192 if (type == rpcrdma_replych)
193 *iptr++ = xdr_zero; /* a NULL write chunk list */
194 warray = (struct rpcrdma_write_array *) iptr;
195 cur_wchunk = (struct rpcrdma_write_chunk *) (warray + 1);
196 }
197
198 if (type == rpcrdma_replych || type == rpcrdma_areadch)
199 pos = 0;
200 else
201 pos = target->head[0].iov_len;
202
203 nsegs = rpcrdma_convert_iovs(target, pos, type, seg, RPCRDMA_MAX_SEGS);
204 if (nsegs == 0)
205 return 0;
206
207 do {
e9601828
TT
208 int n = rpcrdma_register_external(seg, nsegs,
209 cur_wchunk != NULL, r_xprt);
210 if (n <= 0)
211 goto out;
212 if (cur_rchunk) { /* read */
213 cur_rchunk->rc_discrim = xdr_one;
214 /* all read chunks have the same "position" */
215 cur_rchunk->rc_position = htonl(pos);
216 cur_rchunk->rc_target.rs_handle = htonl(seg->mr_rkey);
217 cur_rchunk->rc_target.rs_length = htonl(seg->mr_len);
218 xdr_encode_hyper(
2d8a9726 219 (__be32 *)&cur_rchunk->rc_target.rs_offset,
e9601828
TT
220 seg->mr_base);
221 dprintk("RPC: %s: read chunk "
2a428b2b 222 "elem %d@0x%llx:0x%x pos %u (%s)\n", __func__,
e08a132b
SR
223 seg->mr_len, (unsigned long long)seg->mr_base,
224 seg->mr_rkey, pos, n < nsegs ? "more" : "last");
e9601828
TT
225 cur_rchunk++;
226 r_xprt->rx_stats.read_chunk_count++;
227 } else { /* write/reply */
228 cur_wchunk->wc_target.rs_handle = htonl(seg->mr_rkey);
229 cur_wchunk->wc_target.rs_length = htonl(seg->mr_len);
230 xdr_encode_hyper(
2d8a9726 231 (__be32 *)&cur_wchunk->wc_target.rs_offset,
e9601828
TT
232 seg->mr_base);
233 dprintk("RPC: %s: %s chunk "
234 "elem %d@0x%llx:0x%x (%s)\n", __func__,
235 (type == rpcrdma_replych) ? "reply" : "write",
e08a132b
SR
236 seg->mr_len, (unsigned long long)seg->mr_base,
237 seg->mr_rkey, n < nsegs ? "more" : "last");
e9601828
TT
238 cur_wchunk++;
239 if (type == rpcrdma_replych)
240 r_xprt->rx_stats.reply_chunk_count++;
241 else
242 r_xprt->rx_stats.write_chunk_count++;
243 r_xprt->rx_stats.total_rdma_request += seg->mr_len;
244 }
245 nchunks++;
246 seg += n;
247 nsegs -= n;
248 } while (nsegs);
249
250 /* success. all failures return above */
251 req->rl_nchunks = nchunks;
252
e9601828
TT
253 /*
254 * finish off header. If write, marshal discrim and nchunks.
255 */
256 if (cur_rchunk) {
2d8a9726 257 iptr = (__be32 *) cur_rchunk;
e9601828
TT
258 *iptr++ = xdr_zero; /* finish the read chunk list */
259 *iptr++ = xdr_zero; /* encode a NULL write chunk list */
260 *iptr++ = xdr_zero; /* encode a NULL reply chunk */
261 } else {
262 warray->wc_discrim = xdr_one;
263 warray->wc_nchunks = htonl(nchunks);
2d8a9726 264 iptr = (__be32 *) cur_wchunk;
e9601828
TT
265 if (type == rpcrdma_writech) {
266 *iptr++ = xdr_zero; /* finish the write chunk list */
267 *iptr++ = xdr_zero; /* encode a NULL reply chunk */
268 }
269 }
270
271 /*
272 * Return header size.
273 */
274 return (unsigned char *)iptr - (unsigned char *)headerp;
275
276out:
277 for (pos = 0; nchunks--;)
278 pos += rpcrdma_deregister_external(
13c9ff8f 279 &req->rl_segments[pos], r_xprt);
e9601828
TT
280 return 0;
281}
282
283/*
284 * Copy write data inline.
285 * This function is used for "small" requests. Data which is passed
286 * to RPC via iovecs (or page list) is copied directly into the
287 * pre-registered memory buffer for this request. For small amounts
288 * of data, this is efficient. The cutoff value is tunable.
289 */
290static int
291rpcrdma_inline_pullup(struct rpc_rqst *rqst, int pad)
292{
293 int i, npages, curlen;
294 int copy_len;
295 unsigned char *srcp, *destp;
296 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
bd7ea31b
TT
297 int page_base;
298 struct page **ppages;
e9601828
TT
299
300 destp = rqst->rq_svec[0].iov_base;
301 curlen = rqst->rq_svec[0].iov_len;
302 destp += curlen;
303 /*
304 * Do optional padding where it makes sense. Alignment of write
305 * payload can help the server, if our setting is accurate.
306 */
307 pad -= (curlen + 36/*sizeof(struct rpcrdma_msg_padded)*/);
308 if (pad < 0 || rqst->rq_slen - curlen < RPCRDMA_INLINE_PAD_THRESH)
309 pad = 0; /* don't pad this request */
310
311 dprintk("RPC: %s: pad %d destp 0x%p len %d hdrlen %d\n",
312 __func__, pad, destp, rqst->rq_slen, curlen);
313
314 copy_len = rqst->rq_snd_buf.page_len;
b38ab40a
TT
315
316 if (rqst->rq_snd_buf.tail[0].iov_len) {
317 curlen = rqst->rq_snd_buf.tail[0].iov_len;
318 if (destp + copy_len != rqst->rq_snd_buf.tail[0].iov_base) {
319 memmove(destp + copy_len,
320 rqst->rq_snd_buf.tail[0].iov_base, curlen);
321 r_xprt->rx_stats.pullup_copy_count += curlen;
322 }
323 dprintk("RPC: %s: tail destp 0x%p len %d\n",
324 __func__, destp + copy_len, curlen);
325 rqst->rq_svec[0].iov_len += curlen;
326 }
e9601828 327 r_xprt->rx_stats.pullup_copy_count += copy_len;
bd7ea31b
TT
328
329 page_base = rqst->rq_snd_buf.page_base;
330 ppages = rqst->rq_snd_buf.pages + (page_base >> PAGE_SHIFT);
331 page_base &= ~PAGE_MASK;
332 npages = PAGE_ALIGN(page_base+copy_len) >> PAGE_SHIFT;
e9601828 333 for (i = 0; copy_len && i < npages; i++) {
bd7ea31b 334 curlen = PAGE_SIZE - page_base;
e9601828
TT
335 if (curlen > copy_len)
336 curlen = copy_len;
337 dprintk("RPC: %s: page %d destp 0x%p len %d curlen %d\n",
338 __func__, i, destp, copy_len, curlen);
b8541786 339 srcp = kmap_atomic(ppages[i]);
bd7ea31b 340 memcpy(destp, srcp+page_base, curlen);
b8541786 341 kunmap_atomic(srcp);
e9601828
TT
342 rqst->rq_svec[0].iov_len += curlen;
343 destp += curlen;
344 copy_len -= curlen;
bd7ea31b 345 page_base = 0;
e9601828 346 }
e9601828
TT
347 /* header now contains entire send message */
348 return pad;
349}
350
351/*
352 * Marshal a request: the primary job of this routine is to choose
353 * the transfer modes. See comments below.
354 *
355 * Uses multiple RDMA IOVs for a request:
356 * [0] -- RPC RDMA header, which uses memory from the *start* of the
357 * preregistered buffer that already holds the RPC data in
358 * its middle.
359 * [1] -- the RPC header/data, marshaled by RPC and the NFS protocol.
360 * [2] -- optional padding.
361 * [3] -- if padded, header only in [1] and data here.
362 */
363
364int
365rpcrdma_marshal_req(struct rpc_rqst *rqst)
366{
a4f0835c 367 struct rpc_xprt *xprt = rqst->rq_xprt;
e9601828
TT
368 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
369 struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
370 char *base;
371 size_t hdrlen, rpclen, padlen;
372 enum rpcrdma_chunktype rtype, wtype;
373 struct rpcrdma_msg *headerp;
374
375 /*
376 * rpclen gets amount of data in first buffer, which is the
377 * pre-registered buffer.
378 */
379 base = rqst->rq_svec[0].iov_base;
380 rpclen = rqst->rq_svec[0].iov_len;
381
382 /* build RDMA header in private area at front */
383 headerp = (struct rpcrdma_msg *) req->rl_base;
384 /* don't htonl XID, it's already done in request */
385 headerp->rm_xid = rqst->rq_xid;
386 headerp->rm_vers = xdr_one;
387 headerp->rm_credit = htonl(r_xprt->rx_buf.rb_max_requests);
8d614434 388 headerp->rm_type = htonl(RDMA_MSG);
e9601828
TT
389
390 /*
391 * Chunks needed for results?
392 *
393 * o If the expected result is under the inline threshold, all ops
394 * return as inline (but see later).
395 * o Large non-read ops return as a single reply chunk.
396 * o Large read ops return data as write chunk(s), header as inline.
397 *
398 * Note: the NFS code sending down multiple result segments implies
399 * the op is one of read, readdir[plus], readlink or NFSv4 getacl.
400 */
401
402 /*
403 * This code can handle read chunks, write chunks OR reply
404 * chunks -- only one type. If the request is too big to fit
405 * inline, then we will choose read chunks. If the request is
406 * a READ, then use write chunks to separate the file data
407 * into pages; otherwise use reply chunks.
408 */
409 if (rqst->rq_rcv_buf.buflen <= RPCRDMA_INLINE_READ_THRESHOLD(rqst))
410 wtype = rpcrdma_noch;
411 else if (rqst->rq_rcv_buf.page_len == 0)
412 wtype = rpcrdma_replych;
413 else if (rqst->rq_rcv_buf.flags & XDRBUF_READ)
414 wtype = rpcrdma_writech;
415 else
416 wtype = rpcrdma_replych;
417
418 /*
419 * Chunks needed for arguments?
420 *
421 * o If the total request is under the inline threshold, all ops
422 * are sent as inline.
423 * o Large non-write ops are sent with the entire message as a
424 * single read chunk (protocol 0-position special case).
425 * o Large write ops transmit data as read chunk(s), header as
426 * inline.
427 *
428 * Note: the NFS code sending down multiple argument segments
429 * implies the op is a write.
430 * TBD check NFSv4 setacl
431 */
432 if (rqst->rq_snd_buf.len <= RPCRDMA_INLINE_WRITE_THRESHOLD(rqst))
433 rtype = rpcrdma_noch;
434 else if (rqst->rq_snd_buf.page_len == 0)
435 rtype = rpcrdma_areadch;
436 else
437 rtype = rpcrdma_readch;
438
439 /* The following simplification is not true forever */
440 if (rtype != rpcrdma_noch && wtype == rpcrdma_replych)
441 wtype = rpcrdma_noch;
442 BUG_ON(rtype != rpcrdma_noch && wtype != rpcrdma_noch);
443
e9601828
TT
444 hdrlen = 28; /*sizeof *headerp;*/
445 padlen = 0;
446
447 /*
448 * Pull up any extra send data into the preregistered buffer.
449 * When padding is in use and applies to the transfer, insert
450 * it and change the message type.
451 */
452 if (rtype == rpcrdma_noch) {
453
454 padlen = rpcrdma_inline_pullup(rqst,
455 RPCRDMA_INLINE_PAD_VALUE(rqst));
456
457 if (padlen) {
8d614434 458 headerp->rm_type = htonl(RDMA_MSGP);
e9601828
TT
459 headerp->rm_body.rm_padded.rm_align =
460 htonl(RPCRDMA_INLINE_PAD_VALUE(rqst));
461 headerp->rm_body.rm_padded.rm_thresh =
8d614434 462 htonl(RPCRDMA_INLINE_PAD_THRESH);
e9601828
TT
463 headerp->rm_body.rm_padded.rm_pempty[0] = xdr_zero;
464 headerp->rm_body.rm_padded.rm_pempty[1] = xdr_zero;
465 headerp->rm_body.rm_padded.rm_pempty[2] = xdr_zero;
466 hdrlen += 2 * sizeof(u32); /* extra words in padhdr */
467 BUG_ON(wtype != rpcrdma_noch);
468
469 } else {
470 headerp->rm_body.rm_nochunks.rm_empty[0] = xdr_zero;
471 headerp->rm_body.rm_nochunks.rm_empty[1] = xdr_zero;
472 headerp->rm_body.rm_nochunks.rm_empty[2] = xdr_zero;
473 /* new length after pullup */
474 rpclen = rqst->rq_svec[0].iov_len;
475 /*
476 * Currently we try to not actually use read inline.
477 * Reply chunks have the desirable property that
478 * they land, packed, directly in the target buffers
479 * without headers, so they require no fixup. The
480 * additional RDMA Write op sends the same amount
481 * of data, streams on-the-wire and adds no overhead
482 * on receive. Therefore, we request a reply chunk
483 * for non-writes wherever feasible and efficient.
484 */
0ac531c1 485 if (wtype == rpcrdma_noch)
e9601828
TT
486 wtype = rpcrdma_replych;
487 }
488 }
489
490 /*
491 * Marshal chunks. This routine will return the header length
492 * consumed by marshaling.
493 */
494 if (rtype != rpcrdma_noch) {
495 hdrlen = rpcrdma_create_chunks(rqst,
496 &rqst->rq_snd_buf, headerp, rtype);
497 wtype = rtype; /* simplify dprintk */
498
499 } else if (wtype != rpcrdma_noch) {
500 hdrlen = rpcrdma_create_chunks(rqst,
501 &rqst->rq_rcv_buf, headerp, wtype);
502 }
503
504 if (hdrlen == 0)
505 return -1;
506
5f37d561
TT
507 dprintk("RPC: %s: %s: hdrlen %zd rpclen %zd padlen %zd"
508 " headerp 0x%p base 0x%p lkey 0x%x\n",
e9601828
TT
509 __func__, transfertypes[wtype], hdrlen, rpclen, padlen,
510 headerp, base, req->rl_iov.lkey);
511
512 /*
513 * initialize send_iov's - normally only two: rdma chunk header and
514 * single preregistered RPC header buffer, but if padding is present,
515 * then use a preregistered (and zeroed) pad buffer between the RPC
516 * header and any write data. In all non-rdma cases, any following
517 * data has been copied into the RPC header buffer.
518 */
519 req->rl_send_iov[0].addr = req->rl_iov.addr;
520 req->rl_send_iov[0].length = hdrlen;
521 req->rl_send_iov[0].lkey = req->rl_iov.lkey;
522
523 req->rl_send_iov[1].addr = req->rl_iov.addr + (base - req->rl_base);
524 req->rl_send_iov[1].length = rpclen;
525 req->rl_send_iov[1].lkey = req->rl_iov.lkey;
526
527 req->rl_niovs = 2;
528
529 if (padlen) {
530 struct rpcrdma_ep *ep = &r_xprt->rx_ep;
531
532 req->rl_send_iov[2].addr = ep->rep_pad.addr;
533 req->rl_send_iov[2].length = padlen;
534 req->rl_send_iov[2].lkey = ep->rep_pad.lkey;
535
536 req->rl_send_iov[3].addr = req->rl_send_iov[1].addr + rpclen;
537 req->rl_send_iov[3].length = rqst->rq_slen - rpclen;
538 req->rl_send_iov[3].lkey = req->rl_iov.lkey;
539
540 req->rl_niovs = 4;
541 }
542
543 return 0;
544}
545
546/*
547 * Chase down a received write or reply chunklist to get length
548 * RDMA'd by server. See map at rpcrdma_create_chunks()! :-)
549 */
550static int
d4b37ff7 551rpcrdma_count_chunks(struct rpcrdma_rep *rep, unsigned int max, int wrchunk, __be32 **iptrp)
e9601828
TT
552{
553 unsigned int i, total_len;
554 struct rpcrdma_write_chunk *cur_wchunk;
555
556 i = ntohl(**iptrp); /* get array count */
557 if (i > max)
558 return -1;
559 cur_wchunk = (struct rpcrdma_write_chunk *) (*iptrp + 1);
560 total_len = 0;
561 while (i--) {
562 struct rpcrdma_segment *seg = &cur_wchunk->wc_target;
563 ifdebug(FACILITY) {
564 u64 off;
2d8a9726 565 xdr_decode_hyper((__be32 *)&seg->rs_offset, &off);
e9601828
TT
566 dprintk("RPC: %s: chunk %d@0x%llx:0x%x\n",
567 __func__,
568 ntohl(seg->rs_length),
e08a132b 569 (unsigned long long)off,
e9601828
TT
570 ntohl(seg->rs_handle));
571 }
572 total_len += ntohl(seg->rs_length);
573 ++cur_wchunk;
574 }
575 /* check and adjust for properly terminated write chunk */
576 if (wrchunk) {
2d8a9726 577 __be32 *w = (__be32 *) cur_wchunk;
e9601828
TT
578 if (*w++ != xdr_zero)
579 return -1;
580 cur_wchunk = (struct rpcrdma_write_chunk *) w;
581 }
582 if ((char *) cur_wchunk > rep->rr_base + rep->rr_len)
583 return -1;
584
2d8a9726 585 *iptrp = (__be32 *) cur_wchunk;
e9601828
TT
586 return total_len;
587}
588
589/*
590 * Scatter inline received data back into provided iov's.
591 */
592static void
9191ca3b 593rpcrdma_inline_fixup(struct rpc_rqst *rqst, char *srcp, int copy_len, int pad)
e9601828
TT
594{
595 int i, npages, curlen, olen;
596 char *destp;
bd7ea31b
TT
597 struct page **ppages;
598 int page_base;
e9601828
TT
599
600 curlen = rqst->rq_rcv_buf.head[0].iov_len;
601 if (curlen > copy_len) { /* write chunk header fixup */
602 curlen = copy_len;
603 rqst->rq_rcv_buf.head[0].iov_len = curlen;
604 }
605
606 dprintk("RPC: %s: srcp 0x%p len %d hdrlen %d\n",
607 __func__, srcp, copy_len, curlen);
608
609 /* Shift pointer for first receive segment only */
610 rqst->rq_rcv_buf.head[0].iov_base = srcp;
611 srcp += curlen;
612 copy_len -= curlen;
613
614 olen = copy_len;
615 i = 0;
616 rpcx_to_rdmax(rqst->rq_xprt)->rx_stats.fixup_copy_count += olen;
bd7ea31b
TT
617 page_base = rqst->rq_rcv_buf.page_base;
618 ppages = rqst->rq_rcv_buf.pages + (page_base >> PAGE_SHIFT);
619 page_base &= ~PAGE_MASK;
620
e9601828 621 if (copy_len && rqst->rq_rcv_buf.page_len) {
bd7ea31b 622 npages = PAGE_ALIGN(page_base +
e9601828
TT
623 rqst->rq_rcv_buf.page_len) >> PAGE_SHIFT;
624 for (; i < npages; i++) {
bd7ea31b 625 curlen = PAGE_SIZE - page_base;
e9601828
TT
626 if (curlen > copy_len)
627 curlen = copy_len;
628 dprintk("RPC: %s: page %d"
629 " srcp 0x%p len %d curlen %d\n",
630 __func__, i, srcp, copy_len, curlen);
b8541786 631 destp = kmap_atomic(ppages[i]);
bd7ea31b
TT
632 memcpy(destp + page_base, srcp, curlen);
633 flush_dcache_page(ppages[i]);
b8541786 634 kunmap_atomic(destp);
e9601828
TT
635 srcp += curlen;
636 copy_len -= curlen;
637 if (copy_len == 0)
638 break;
bd7ea31b 639 page_base = 0;
e9601828 640 }
2b7bbc96 641 }
e9601828
TT
642
643 if (copy_len && rqst->rq_rcv_buf.tail[0].iov_len) {
644 curlen = copy_len;
645 if (curlen > rqst->rq_rcv_buf.tail[0].iov_len)
646 curlen = rqst->rq_rcv_buf.tail[0].iov_len;
647 if (rqst->rq_rcv_buf.tail[0].iov_base != srcp)
b38ab40a 648 memmove(rqst->rq_rcv_buf.tail[0].iov_base, srcp, curlen);
e9601828
TT
649 dprintk("RPC: %s: tail srcp 0x%p len %d curlen %d\n",
650 __func__, srcp, copy_len, curlen);
651 rqst->rq_rcv_buf.tail[0].iov_len = curlen;
652 copy_len -= curlen; ++i;
653 } else
654 rqst->rq_rcv_buf.tail[0].iov_len = 0;
655
9191ca3b
TT
656 if (pad) {
657 /* implicit padding on terminal chunk */
658 unsigned char *p = rqst->rq_rcv_buf.tail[0].iov_base;
659 while (pad--)
660 p[rqst->rq_rcv_buf.tail[0].iov_len++] = 0;
661 }
662
e9601828
TT
663 if (copy_len)
664 dprintk("RPC: %s: %d bytes in"
665 " %d extra segments (%d lost)\n",
666 __func__, olen, i, copy_len);
667
668 /* TBD avoid a warning from call_decode() */
669 rqst->rq_private_buf = rqst->rq_rcv_buf;
670}
671
e9601828 672void
254f91e2 673rpcrdma_connect_worker(struct work_struct *work)
e9601828 674{
254f91e2
CL
675 struct rpcrdma_ep *ep =
676 container_of(work, struct rpcrdma_ep, rep_connect_worker.work);
e9601828
TT
677 struct rpc_xprt *xprt = ep->rep_xprt;
678
679 spin_lock_bh(&xprt->transport_lock);
575448bd
TT
680 if (++xprt->connect_cookie == 0) /* maintain a reserved value */
681 ++xprt->connect_cookie;
e9601828
TT
682 if (ep->rep_connected > 0) {
683 if (!xprt_test_and_set_connected(xprt))
684 xprt_wake_pending_tasks(xprt, 0);
685 } else {
686 if (xprt_test_and_clear_connected(xprt))
926449ba 687 xprt_wake_pending_tasks(xprt, -ENOTCONN);
e9601828
TT
688 }
689 spin_unlock_bh(&xprt->transport_lock);
690}
691
254f91e2
CL
692/*
693 * This function is called when an async event is posted to
694 * the connection which changes the connection state. All it
695 * does at this point is mark the connection up/down, the rpc
696 * timers do the rest.
697 */
698void
699rpcrdma_conn_func(struct rpcrdma_ep *ep)
700{
701 schedule_delayed_work(&ep->rep_connect_worker, 0);
702}
703
e9601828
TT
704/*
705 * Called as a tasklet to do req/reply match and complete a request
706 * Errors must result in the RPC task either being awakened, or
707 * allowed to timeout, to discover the errors at that time.
708 */
709void
710rpcrdma_reply_handler(struct rpcrdma_rep *rep)
711{
712 struct rpcrdma_msg *headerp;
713 struct rpcrdma_req *req;
714 struct rpc_rqst *rqst;
715 struct rpc_xprt *xprt = rep->rr_xprt;
716 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
2d8a9726 717 __be32 *iptr;
b45ccfd2 718 int rdmalen, status;
e7ce710a 719 unsigned long cwnd;
e9601828
TT
720
721 /* Check status. If bad, signal disconnect and return rep to pool */
722 if (rep->rr_len == ~0U) {
723 rpcrdma_recv_buffer_put(rep);
724 if (r_xprt->rx_ep.rep_connected == 1) {
725 r_xprt->rx_ep.rep_connected = -EIO;
726 rpcrdma_conn_func(&r_xprt->rx_ep);
727 }
728 return;
729 }
730 if (rep->rr_len < 28) {
731 dprintk("RPC: %s: short/invalid reply\n", __func__);
732 goto repost;
733 }
734 headerp = (struct rpcrdma_msg *) rep->rr_base;
735 if (headerp->rm_vers != xdr_one) {
736 dprintk("RPC: %s: invalid version %d\n",
737 __func__, ntohl(headerp->rm_vers));
738 goto repost;
739 }
740
741 /* Get XID and try for a match. */
742 spin_lock(&xprt->transport_lock);
743 rqst = xprt_lookup_rqst(xprt, headerp->rm_xid);
744 if (rqst == NULL) {
745 spin_unlock(&xprt->transport_lock);
746 dprintk("RPC: %s: reply 0x%p failed "
747 "to match any request xid 0x%08x len %d\n",
748 __func__, rep, headerp->rm_xid, rep->rr_len);
749repost:
750 r_xprt->rx_stats.bad_reply_count++;
751 rep->rr_func = rpcrdma_reply_handler;
752 if (rpcrdma_ep_post_recv(&r_xprt->rx_ia, &r_xprt->rx_ep, rep))
753 rpcrdma_recv_buffer_put(rep);
754
755 return;
756 }
757
758 /* get request object */
759 req = rpcr_to_rdmar(rqst);
4a6862b3
TT
760 if (req->rl_reply) {
761 spin_unlock(&xprt->transport_lock);
762 dprintk("RPC: %s: duplicate reply 0x%p to RPC "
763 "request 0x%p: xid 0x%08x\n", __func__, rep, req,
764 headerp->rm_xid);
765 goto repost;
766 }
e9601828
TT
767
768 dprintk("RPC: %s: reply 0x%p completes request 0x%p\n"
769 " RPC request 0x%p xid 0x%08x\n",
770 __func__, rep, req, rqst, headerp->rm_xid);
771
e9601828
TT
772 /* from here on, the reply is no longer an orphan */
773 req->rl_reply = rep;
18906972 774 xprt->reestablish_timeout = 0;
e9601828
TT
775
776 /* check for expected message types */
777 /* The order of some of these tests is important. */
778 switch (headerp->rm_type) {
60678040 779 case htonl(RDMA_MSG):
e9601828
TT
780 /* never expect read chunks */
781 /* never expect reply chunks (two ways to check) */
782 /* never expect write chunks without having offered RDMA */
783 if (headerp->rm_body.rm_chunks[0] != xdr_zero ||
784 (headerp->rm_body.rm_chunks[1] == xdr_zero &&
785 headerp->rm_body.rm_chunks[2] != xdr_zero) ||
786 (headerp->rm_body.rm_chunks[1] != xdr_zero &&
787 req->rl_nchunks == 0))
788 goto badheader;
789 if (headerp->rm_body.rm_chunks[1] != xdr_zero) {
790 /* count any expected write chunks in read reply */
791 /* start at write chunk array count */
792 iptr = &headerp->rm_body.rm_chunks[2];
793 rdmalen = rpcrdma_count_chunks(rep,
794 req->rl_nchunks, 1, &iptr);
795 /* check for validity, and no reply chunk after */
796 if (rdmalen < 0 || *iptr++ != xdr_zero)
797 goto badheader;
798 rep->rr_len -=
799 ((unsigned char *)iptr - (unsigned char *)headerp);
800 status = rep->rr_len + rdmalen;
801 r_xprt->rx_stats.total_rdma_reply += rdmalen;
9191ca3b
TT
802 /* special case - last chunk may omit padding */
803 if (rdmalen &= 3) {
804 rdmalen = 4 - rdmalen;
805 status += rdmalen;
806 }
e9601828
TT
807 } else {
808 /* else ordinary inline */
9191ca3b 809 rdmalen = 0;
2d8a9726 810 iptr = (__be32 *)((unsigned char *)headerp + 28);
e9601828
TT
811 rep->rr_len -= 28; /*sizeof *headerp;*/
812 status = rep->rr_len;
813 }
814 /* Fix up the rpc results for upper layer */
9191ca3b 815 rpcrdma_inline_fixup(rqst, (char *)iptr, rep->rr_len, rdmalen);
e9601828
TT
816 break;
817
60678040 818 case htonl(RDMA_NOMSG):
e9601828
TT
819 /* never expect read or write chunks, always reply chunks */
820 if (headerp->rm_body.rm_chunks[0] != xdr_zero ||
821 headerp->rm_body.rm_chunks[1] != xdr_zero ||
822 headerp->rm_body.rm_chunks[2] != xdr_one ||
823 req->rl_nchunks == 0)
824 goto badheader;
2d8a9726 825 iptr = (__be32 *)((unsigned char *)headerp + 28);
e9601828
TT
826 rdmalen = rpcrdma_count_chunks(rep, req->rl_nchunks, 0, &iptr);
827 if (rdmalen < 0)
828 goto badheader;
829 r_xprt->rx_stats.total_rdma_reply += rdmalen;
830 /* Reply chunk buffer already is the reply vector - no fixup. */
831 status = rdmalen;
832 break;
833
834badheader:
835 default:
836 dprintk("%s: invalid rpcrdma reply header (type %d):"
837 " chunks[012] == %d %d %d"
838 " expected chunks <= %d\n",
839 __func__, ntohl(headerp->rm_type),
840 headerp->rm_body.rm_chunks[0],
841 headerp->rm_body.rm_chunks[1],
842 headerp->rm_body.rm_chunks[2],
843 req->rl_nchunks);
844 status = -EIO;
845 r_xprt->rx_stats.bad_reply_count++;
846 break;
847 }
848
e7ce710a
CL
849 cwnd = xprt->cwnd;
850 xprt->cwnd = atomic_read(&r_xprt->rx_buf.rb_credits) << RPC_CWNDSHIFT;
851 if (xprt->cwnd > cwnd)
852 xprt_release_rqst_cong(rqst->rq_task);
853
e9601828
TT
854 dprintk("RPC: %s: xprt_complete_rqst(0x%p, 0x%p, %d)\n",
855 __func__, xprt, rqst, status);
856 xprt_complete_rqst(rqst->rq_task, status);
857 spin_unlock(&xprt->transport_lock);
858}
This page took 0.529728 seconds and 5 git commands to generate.