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