blob: b3b7bb85844d9466ab396c63b45b79775597831f [file] [log] [blame]
Tom Tuckerd5b31be2007-12-12 16:13:23 -06001/*
Steve Wise0bf48282014-05-28 15:12:01 -05002 * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved.
Tom Tuckerd5b31be2007-12-12 16:13:23 -06003 * Copyright (c) 2005-2006 Network Appliance, Inc. All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the BSD-type
9 * license below:
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 *
18 * Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials provided
21 * with the distribution.
22 *
23 * Neither the name of the Network Appliance, Inc. nor the names of
24 * its contributors may be used to endorse or promote products
25 * derived from this software without specific prior written
26 * permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Author: Tom Tucker <tom@opengridcomputing.com>
41 */
42
43#include <linux/sunrpc/debug.h>
44#include <linux/sunrpc/rpc_rdma.h>
45#include <linux/spinlock.h>
Chuck Levere560e3b2014-07-22 16:00:40 -040046#include <linux/highmem.h>
Tom Tuckerd5b31be2007-12-12 16:13:23 -060047#include <asm/unaligned.h>
48#include <rdma/ib_verbs.h>
49#include <rdma/rdma_cm.h>
50#include <linux/sunrpc/svc_rdma.h>
51
52#define RPCDBG_FACILITY RPCDBG_SVCXPRT
53
54/*
55 * Replace the pages in the rq_argpages array with the pages from the SGE in
56 * the RDMA_RECV completion. The SGL should contain full pages up until the
57 * last one.
58 */
59static void rdma_build_arg_xdr(struct svc_rqst *rqstp,
60 struct svc_rdma_op_ctxt *ctxt,
61 u32 byte_count)
62{
63 struct page *page;
64 u32 bc;
65 int sge_no;
66
67 /* Swap the page in the SGE with the page in argpages */
68 page = ctxt->pages[0];
69 put_page(rqstp->rq_pages[0]);
70 rqstp->rq_pages[0] = page;
71
72 /* Set up the XDR head */
73 rqstp->rq_arg.head[0].iov_base = page_address(page);
Steve Wise0bf48282014-05-28 15:12:01 -050074 rqstp->rq_arg.head[0].iov_len =
75 min_t(size_t, byte_count, ctxt->sge[0].length);
Tom Tuckerd5b31be2007-12-12 16:13:23 -060076 rqstp->rq_arg.len = byte_count;
77 rqstp->rq_arg.buflen = byte_count;
78
79 /* Compute bytes past head in the SGL */
80 bc = byte_count - rqstp->rq_arg.head[0].iov_len;
81
82 /* If data remains, store it in the pagelist */
83 rqstp->rq_arg.page_len = bc;
84 rqstp->rq_arg.page_base = 0;
85 rqstp->rq_arg.pages = &rqstp->rq_pages[1];
86 sge_no = 1;
87 while (bc && sge_no < ctxt->count) {
88 page = ctxt->pages[sge_no];
89 put_page(rqstp->rq_pages[sge_no]);
90 rqstp->rq_pages[sge_no] = page;
Steve Wise0bf48282014-05-28 15:12:01 -050091 bc -= min_t(u32, bc, ctxt->sge[sge_no].length);
Tom Tuckerd5b31be2007-12-12 16:13:23 -060092 rqstp->rq_arg.buflen += ctxt->sge[sge_no].length;
93 sge_no++;
94 }
95 rqstp->rq_respages = &rqstp->rq_pages[sge_no];
Tom Tucker7e4359e2014-03-25 15:14:57 -050096 rqstp->rq_next_page = rqstp->rq_respages + 1;
Tom Tuckerd5b31be2007-12-12 16:13:23 -060097
98 /* We should never run out of SGE because the limit is defined to
99 * support the max allowed RPC data length
100 */
101 BUG_ON(bc && (sge_no == ctxt->count));
102 BUG_ON((rqstp->rq_arg.head[0].iov_len + rqstp->rq_arg.page_len)
103 != byte_count);
104 BUG_ON(rqstp->rq_arg.len != byte_count);
105
106 /* If not all pages were used from the SGL, free the remaining ones */
107 bc = sge_no;
108 while (sge_no < ctxt->count) {
109 page = ctxt->pages[sge_no++];
110 put_page(page);
111 }
112 ctxt->count = bc;
113
114 /* Set up tail */
115 rqstp->rq_arg.tail[0].iov_base = NULL;
116 rqstp->rq_arg.tail[0].iov_len = 0;
117}
118
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600119static int rdma_read_max_sge(struct svcxprt_rdma *xprt, int sge_count)
120{
Steve Wise0bf48282014-05-28 15:12:01 -0500121 if (rdma_node_get_transport(xprt->sc_cm_id->device->node_type) ==
122 RDMA_TRANSPORT_IWARP)
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600123 return 1;
124 else
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600125 return min_t(int, sge_count, xprt->sc_max_sge);
126}
127
Steve Wise0bf48282014-05-28 15:12:01 -0500128typedef int (*rdma_reader_fn)(struct svcxprt_rdma *xprt,
129 struct svc_rqst *rqstp,
130 struct svc_rdma_op_ctxt *head,
131 int *page_no,
132 u32 *page_offset,
133 u32 rs_handle,
134 u32 rs_length,
135 u64 rs_offset,
136 int last);
137
138/* Issue an RDMA_READ using the local lkey to map the data sink */
139static int rdma_read_chunk_lcl(struct svcxprt_rdma *xprt,
140 struct svc_rqst *rqstp,
141 struct svc_rdma_op_ctxt *head,
142 int *page_no,
143 u32 *page_offset,
144 u32 rs_handle,
145 u32 rs_length,
146 u64 rs_offset,
147 int last)
148{
149 struct ib_send_wr read_wr;
150 int pages_needed = PAGE_ALIGN(*page_offset + rs_length) >> PAGE_SHIFT;
151 struct svc_rdma_op_ctxt *ctxt = svc_rdma_get_context(xprt);
152 int ret, read, pno;
153 u32 pg_off = *page_offset;
154 u32 pg_no = *page_no;
155
156 ctxt->direction = DMA_FROM_DEVICE;
157 ctxt->read_hdr = head;
158 pages_needed =
159 min_t(int, pages_needed, rdma_read_max_sge(xprt, pages_needed));
160 read = min_t(int, pages_needed << PAGE_SHIFT, rs_length);
161
162 for (pno = 0; pno < pages_needed; pno++) {
163 int len = min_t(int, rs_length, PAGE_SIZE - pg_off);
164
165 head->arg.pages[pg_no] = rqstp->rq_arg.pages[pg_no];
166 head->arg.page_len += len;
167 head->arg.len += len;
168 if (!pg_off)
169 head->count++;
170 rqstp->rq_respages = &rqstp->rq_arg.pages[pg_no+1];
171 rqstp->rq_next_page = rqstp->rq_respages + 1;
172 ctxt->sge[pno].addr =
173 ib_dma_map_page(xprt->sc_cm_id->device,
174 head->arg.pages[pg_no], pg_off,
175 PAGE_SIZE - pg_off,
176 DMA_FROM_DEVICE);
177 ret = ib_dma_mapping_error(xprt->sc_cm_id->device,
178 ctxt->sge[pno].addr);
179 if (ret)
180 goto err;
181 atomic_inc(&xprt->sc_dma_used);
182
183 /* The lkey here is either a local dma lkey or a dma_mr lkey */
184 ctxt->sge[pno].lkey = xprt->sc_dma_lkey;
185 ctxt->sge[pno].length = len;
186 ctxt->count++;
187
188 /* adjust offset and wrap to next page if needed */
189 pg_off += len;
190 if (pg_off == PAGE_SIZE) {
191 pg_off = 0;
192 pg_no++;
193 }
194 rs_length -= len;
195 }
196
197 if (last && rs_length == 0)
198 set_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags);
199 else
200 clear_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags);
201
202 memset(&read_wr, 0, sizeof(read_wr));
203 read_wr.wr_id = (unsigned long)ctxt;
204 read_wr.opcode = IB_WR_RDMA_READ;
205 ctxt->wr_op = read_wr.opcode;
206 read_wr.send_flags = IB_SEND_SIGNALED;
207 read_wr.wr.rdma.rkey = rs_handle;
208 read_wr.wr.rdma.remote_addr = rs_offset;
209 read_wr.sg_list = ctxt->sge;
210 read_wr.num_sge = pages_needed;
211
212 ret = svc_rdma_send(xprt, &read_wr);
213 if (ret) {
214 pr_err("svcrdma: Error %d posting RDMA_READ\n", ret);
215 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
216 goto err;
217 }
218
219 /* return current location in page array */
220 *page_no = pg_no;
221 *page_offset = pg_off;
222 ret = read;
223 atomic_inc(&rdma_stat_read);
224 return ret;
225 err:
226 svc_rdma_unmap_dma(ctxt);
227 svc_rdma_put_context(ctxt, 0);
228 return ret;
229}
230
231/* Issue an RDMA_READ using an FRMR to map the data sink */
232static int rdma_read_chunk_frmr(struct svcxprt_rdma *xprt,
233 struct svc_rqst *rqstp,
234 struct svc_rdma_op_ctxt *head,
235 int *page_no,
236 u32 *page_offset,
237 u32 rs_handle,
238 u32 rs_length,
239 u64 rs_offset,
240 int last)
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600241{
242 struct ib_send_wr read_wr;
Tom Tucker146b6df2008-08-12 15:12:10 -0500243 struct ib_send_wr inv_wr;
Steve Wise0bf48282014-05-28 15:12:01 -0500244 struct ib_send_wr fastreg_wr;
245 u8 key;
246 int pages_needed = PAGE_ALIGN(*page_offset + rs_length) >> PAGE_SHIFT;
247 struct svc_rdma_op_ctxt *ctxt = svc_rdma_get_context(xprt);
248 struct svc_rdma_fastreg_mr *frmr = svc_rdma_get_frmr(xprt);
249 int ret, read, pno;
250 u32 pg_off = *page_offset;
251 u32 pg_no = *page_no;
252
253 if (IS_ERR(frmr))
254 return -ENOMEM;
255
256 ctxt->direction = DMA_FROM_DEVICE;
257 ctxt->frmr = frmr;
258 pages_needed = min_t(int, pages_needed, xprt->sc_frmr_pg_list_len);
259 read = min_t(int, pages_needed << PAGE_SHIFT, rs_length);
260
261 frmr->kva = page_address(rqstp->rq_arg.pages[pg_no]);
262 frmr->direction = DMA_FROM_DEVICE;
263 frmr->access_flags = (IB_ACCESS_LOCAL_WRITE|IB_ACCESS_REMOTE_WRITE);
264 frmr->map_len = pages_needed << PAGE_SHIFT;
265 frmr->page_list_len = pages_needed;
266
267 for (pno = 0; pno < pages_needed; pno++) {
268 int len = min_t(int, rs_length, PAGE_SIZE - pg_off);
269
270 head->arg.pages[pg_no] = rqstp->rq_arg.pages[pg_no];
271 head->arg.page_len += len;
272 head->arg.len += len;
273 if (!pg_off)
274 head->count++;
275 rqstp->rq_respages = &rqstp->rq_arg.pages[pg_no+1];
276 rqstp->rq_next_page = rqstp->rq_respages + 1;
277 frmr->page_list->page_list[pno] =
278 ib_dma_map_page(xprt->sc_cm_id->device,
279 head->arg.pages[pg_no], 0,
280 PAGE_SIZE, DMA_FROM_DEVICE);
281 ret = ib_dma_mapping_error(xprt->sc_cm_id->device,
282 frmr->page_list->page_list[pno]);
283 if (ret)
284 goto err;
285 atomic_inc(&xprt->sc_dma_used);
286
287 /* adjust offset and wrap to next page if needed */
288 pg_off += len;
289 if (pg_off == PAGE_SIZE) {
290 pg_off = 0;
291 pg_no++;
292 }
293 rs_length -= len;
294 }
295
296 if (last && rs_length == 0)
297 set_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags);
298 else
299 clear_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags);
300
301 /* Bump the key */
302 key = (u8)(frmr->mr->lkey & 0x000000FF);
303 ib_update_fast_reg_key(frmr->mr, ++key);
304
305 ctxt->sge[0].addr = (unsigned long)frmr->kva + *page_offset;
306 ctxt->sge[0].lkey = frmr->mr->lkey;
307 ctxt->sge[0].length = read;
308 ctxt->count = 1;
309 ctxt->read_hdr = head;
310
311 /* Prepare FASTREG WR */
312 memset(&fastreg_wr, 0, sizeof(fastreg_wr));
313 fastreg_wr.opcode = IB_WR_FAST_REG_MR;
314 fastreg_wr.send_flags = IB_SEND_SIGNALED;
315 fastreg_wr.wr.fast_reg.iova_start = (unsigned long)frmr->kva;
316 fastreg_wr.wr.fast_reg.page_list = frmr->page_list;
317 fastreg_wr.wr.fast_reg.page_list_len = frmr->page_list_len;
318 fastreg_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
319 fastreg_wr.wr.fast_reg.length = frmr->map_len;
320 fastreg_wr.wr.fast_reg.access_flags = frmr->access_flags;
321 fastreg_wr.wr.fast_reg.rkey = frmr->mr->lkey;
322 fastreg_wr.next = &read_wr;
323
324 /* Prepare RDMA_READ */
325 memset(&read_wr, 0, sizeof(read_wr));
326 read_wr.send_flags = IB_SEND_SIGNALED;
327 read_wr.wr.rdma.rkey = rs_handle;
328 read_wr.wr.rdma.remote_addr = rs_offset;
329 read_wr.sg_list = ctxt->sge;
330 read_wr.num_sge = 1;
331 if (xprt->sc_dev_caps & SVCRDMA_DEVCAP_READ_W_INV) {
332 read_wr.opcode = IB_WR_RDMA_READ_WITH_INV;
333 read_wr.wr_id = (unsigned long)ctxt;
334 read_wr.ex.invalidate_rkey = ctxt->frmr->mr->lkey;
335 } else {
336 read_wr.opcode = IB_WR_RDMA_READ;
337 read_wr.next = &inv_wr;
338 /* Prepare invalidate */
339 memset(&inv_wr, 0, sizeof(inv_wr));
340 inv_wr.wr_id = (unsigned long)ctxt;
341 inv_wr.opcode = IB_WR_LOCAL_INV;
Steve Wise83710fc2014-06-05 09:54:31 -0500342 inv_wr.send_flags = IB_SEND_SIGNALED | IB_SEND_FENCE;
Steve Wise0bf48282014-05-28 15:12:01 -0500343 inv_wr.ex.invalidate_rkey = frmr->mr->lkey;
344 }
345 ctxt->wr_op = read_wr.opcode;
346
347 /* Post the chain */
348 ret = svc_rdma_send(xprt, &fastreg_wr);
349 if (ret) {
350 pr_err("svcrdma: Error %d posting RDMA_READ\n", ret);
351 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
352 goto err;
353 }
354
355 /* return current location in page array */
356 *page_no = pg_no;
357 *page_offset = pg_off;
358 ret = read;
359 atomic_inc(&rdma_stat_read);
360 return ret;
361 err:
362 svc_rdma_unmap_dma(ctxt);
363 svc_rdma_put_context(ctxt, 0);
364 svc_rdma_put_frmr(xprt, frmr);
365 return ret;
366}
367
Chuck Lever2397aa82015-01-13 11:02:54 -0500368static unsigned int
369rdma_rcl_chunk_count(struct rpcrdma_read_chunk *ch)
370{
371 unsigned int count;
372
373 for (count = 0; ch->rc_discrim != xdr_zero; ch++)
374 count++;
375 return count;
376}
377
Steve Wise0bf48282014-05-28 15:12:01 -0500378static int rdma_read_chunks(struct svcxprt_rdma *xprt,
379 struct rpcrdma_msg *rmsgp,
380 struct svc_rqst *rqstp,
381 struct svc_rdma_op_ctxt *head)
382{
Chuck Lever2397aa82015-01-13 11:02:54 -0500383 int page_no, ret;
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600384 struct rpcrdma_read_chunk *ch;
Steve Wise0bf48282014-05-28 15:12:01 -0500385 u32 page_offset, byte_count;
386 u64 rs_offset;
387 rdma_reader_fn reader;
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600388
389 /* If no read list is present, return 0 */
390 ch = svc_rdma_get_read_chunk(rmsgp);
391 if (!ch)
392 return 0;
393
Chuck Lever2397aa82015-01-13 11:02:54 -0500394 if (rdma_rcl_chunk_count(ch) > RPCSVC_MAXPAGES)
Tom Tuckera6f911c2008-05-13 09:16:05 -0500395 return -EINVAL;
Tom Tucker146b6df2008-08-12 15:12:10 -0500396
Steve Wise0bf48282014-05-28 15:12:01 -0500397 /* The request is completed when the RDMA_READs complete. The
398 * head context keeps all the pages that comprise the
399 * request.
400 */
401 head->arg.head[0] = rqstp->rq_arg.head[0];
402 head->arg.tail[0] = rqstp->rq_arg.tail[0];
403 head->arg.pages = &head->pages[head->count];
404 head->hdr_count = head->count;
405 head->arg.page_base = 0;
406 head->arg.page_len = 0;
407 head->arg.len = rqstp->rq_arg.len;
408 head->arg.buflen = rqstp->rq_arg.buflen;
Christian Engelmayer59fb3062009-06-14 00:05:26 +0200409
Steve Wise0bf48282014-05-28 15:12:01 -0500410 /* Use FRMR if supported */
411 if (xprt->sc_dev_caps & SVCRDMA_DEVCAP_FAST_REG)
412 reader = rdma_read_chunk_frmr;
Tom Tucker146b6df2008-08-12 15:12:10 -0500413 else
Steve Wise0bf48282014-05-28 15:12:01 -0500414 reader = rdma_read_chunk_lcl;
Tom Tucker146b6df2008-08-12 15:12:10 -0500415
Steve Wise0bf48282014-05-28 15:12:01 -0500416 page_no = 0; page_offset = 0;
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600417 for (ch = (struct rpcrdma_read_chunk *)&rmsgp->rm_body.rm_chunks[0];
Steve Wise0bf48282014-05-28 15:12:01 -0500418 ch->rc_discrim != 0; ch++) {
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600419
Tom Tuckercec56c82012-02-15 11:30:00 -0600420 xdr_decode_hyper((__be32 *)&ch->rc_target.rs_offset,
421 &rs_offset);
Steve Wise0bf48282014-05-28 15:12:01 -0500422 byte_count = ntohl(ch->rc_target.rs_length);
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600423
Steve Wise0bf48282014-05-28 15:12:01 -0500424 while (byte_count > 0) {
425 ret = reader(xprt, rqstp, head,
426 &page_no, &page_offset,
427 ntohl(ch->rc_target.rs_handle),
428 byte_count, rs_offset,
429 ((ch+1)->rc_discrim == 0) /* last */
430 );
431 if (ret < 0)
432 goto err;
433 byte_count -= ret;
434 rs_offset += ret;
435 head->arg.buflen += ret;
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600436 }
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600437 }
Steve Wise0bf48282014-05-28 15:12:01 -0500438 ret = 1;
439 err:
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600440 /* Detach arg pages. svc_recv will replenish them */
Steve Wise0bf48282014-05-28 15:12:01 -0500441 for (page_no = 0;
442 &rqstp->rq_pages[page_no] < rqstp->rq_respages; page_no++)
443 rqstp->rq_pages[page_no] = NULL;
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600444
Steve Wise0bf48282014-05-28 15:12:01 -0500445 return ret;
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600446}
447
Chuck Levere560e3b2014-07-22 16:00:40 -0400448/*
449 * To avoid a separate RDMA READ just for a handful of zero bytes,
450 * RFC 5666 section 3.7 allows the client to omit the XDR zero pad
451 * in chunk lists.
452 */
453static void
454rdma_fix_xdr_pad(struct xdr_buf *buf)
455{
456 unsigned int page_len = buf->page_len;
457 unsigned int size = (XDR_QUADLEN(page_len) << 2) - page_len;
458 unsigned int offset, pg_no;
459 char *p;
460
461 if (size == 0)
462 return;
463
464 pg_no = page_len >> PAGE_SHIFT;
465 offset = page_len & ~PAGE_MASK;
466 p = page_address(buf->pages[pg_no]);
467 memset(p + offset, 0, size);
468
469 buf->page_len += size;
470 buf->buflen += size;
471 buf->len += size;
472}
473
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600474static int rdma_read_complete(struct svc_rqst *rqstp,
Tom Tucker02e74522008-04-30 19:50:56 -0500475 struct svc_rdma_op_ctxt *head)
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600476{
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600477 int page_no;
478 int ret;
479
480 BUG_ON(!head);
481
482 /* Copy RPC pages */
483 for (page_no = 0; page_no < head->count; page_no++) {
484 put_page(rqstp->rq_pages[page_no]);
485 rqstp->rq_pages[page_no] = head->pages[page_no];
486 }
487 /* Point rq_arg.pages past header */
Chuck Levere560e3b2014-07-22 16:00:40 -0400488 rdma_fix_xdr_pad(&head->arg);
Tom Tuckerf820c572008-05-27 17:03:14 -0500489 rqstp->rq_arg.pages = &rqstp->rq_pages[head->hdr_count];
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600490 rqstp->rq_arg.page_len = head->arg.page_len;
491 rqstp->rq_arg.page_base = head->arg.page_base;
492
493 /* rq_respages starts after the last arg page */
494 rqstp->rq_respages = &rqstp->rq_arg.pages[page_no];
Tom Tucker7e4359e2014-03-25 15:14:57 -0500495 rqstp->rq_next_page = rqstp->rq_respages + 1;
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600496
497 /* Rebuild rq_arg head and tail. */
498 rqstp->rq_arg.head[0] = head->arg.head[0];
499 rqstp->rq_arg.tail[0] = head->arg.tail[0];
500 rqstp->rq_arg.len = head->arg.len;
501 rqstp->rq_arg.buflen = head->arg.buflen;
502
Tom Tucker02e74522008-04-30 19:50:56 -0500503 /* Free the context */
504 svc_rdma_put_context(head, 0);
505
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600506 /* XXX: What should this be? */
507 rqstp->rq_prot = IPPROTO_MAX;
Tom Tucker69500c42008-05-07 13:49:58 -0500508 svc_xprt_copy_addrs(rqstp, rqstp->rq_xprt);
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600509
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600510 ret = rqstp->rq_arg.head[0].iov_len
511 + rqstp->rq_arg.page_len
512 + rqstp->rq_arg.tail[0].iov_len;
Chuck Lever597561b2015-01-13 11:02:37 -0500513 dprintk("svcrdma: deferred read ret=%d, rq_arg.len=%u, "
514 "rq_arg.head[0].iov_base=%p, rq_arg.head[0].iov_len=%zu\n",
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600515 ret, rqstp->rq_arg.len, rqstp->rq_arg.head[0].iov_base,
516 rqstp->rq_arg.head[0].iov_len);
517
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600518 return ret;
519}
520
521/*
522 * Set up the rqstp thread context to point to the RQ buffer. If
523 * necessary, pull additional data from the client with an RDMA_READ
524 * request.
525 */
526int svc_rdma_recvfrom(struct svc_rqst *rqstp)
527{
528 struct svc_xprt *xprt = rqstp->rq_xprt;
529 struct svcxprt_rdma *rdma_xprt =
530 container_of(xprt, struct svcxprt_rdma, sc_xprt);
531 struct svc_rdma_op_ctxt *ctxt = NULL;
532 struct rpcrdma_msg *rmsgp;
533 int ret = 0;
534 int len;
535
536 dprintk("svcrdma: rqstp=%p\n", rqstp);
537
Tom Tucker24b8b442008-08-13 11:05:41 -0500538 spin_lock_bh(&rdma_xprt->sc_rq_dto_lock);
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600539 if (!list_empty(&rdma_xprt->sc_read_complete_q)) {
540 ctxt = list_entry(rdma_xprt->sc_read_complete_q.next,
541 struct svc_rdma_op_ctxt,
542 dto_q);
543 list_del_init(&ctxt->dto_q);
Tom Tucker24b8b442008-08-13 11:05:41 -0500544 spin_unlock_bh(&rdma_xprt->sc_rq_dto_lock);
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600545 return rdma_read_complete(rqstp, ctxt);
Steve Wise0bf48282014-05-28 15:12:01 -0500546 } else if (!list_empty(&rdma_xprt->sc_rq_dto_q)) {
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600547 ctxt = list_entry(rdma_xprt->sc_rq_dto_q.next,
548 struct svc_rdma_op_ctxt,
549 dto_q);
550 list_del_init(&ctxt->dto_q);
551 } else {
552 atomic_inc(&rdma_stat_rq_starve);
553 clear_bit(XPT_DATA, &xprt->xpt_flags);
554 ctxt = NULL;
555 }
556 spin_unlock_bh(&rdma_xprt->sc_rq_dto_lock);
557 if (!ctxt) {
558 /* This is the EAGAIN path. The svc_recv routine will
559 * return -EAGAIN, the nfsd thread will go to call into
560 * svc_recv again and we shouldn't be on the active
561 * transport list
562 */
563 if (test_bit(XPT_CLOSE, &xprt->xpt_flags))
564 goto close_out;
565
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600566 goto out;
567 }
568 dprintk("svcrdma: processing ctxt=%p on xprt=%p, rqstp=%p, status=%d\n",
569 ctxt, rdma_xprt, rqstp, ctxt->wc_status);
570 BUG_ON(ctxt->wc_status != IB_WC_SUCCESS);
571 atomic_inc(&rdma_stat_recv);
572
573 /* Build up the XDR from the receive buffers. */
574 rdma_build_arg_xdr(rqstp, ctxt, ctxt->byte_len);
575
576 /* Decode the RDMA header. */
577 len = svc_rdma_xdr_decode_req(&rmsgp, rqstp);
578 rqstp->rq_xprt_hlen = len;
579
580 /* If the request is invalid, reply with an error */
581 if (len < 0) {
582 if (len == -ENOSYS)
Tom Tucker008fdbc2008-05-07 15:47:42 -0500583 svc_rdma_send_error(rdma_xprt, rmsgp, ERR_VERS);
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600584 goto close_out;
585 }
586
Tom Tuckerd16d4002008-05-06 10:04:50 -0500587 /* Read read-list data. */
Steve Wise0bf48282014-05-28 15:12:01 -0500588 ret = rdma_read_chunks(rdma_xprt, rmsgp, rqstp, ctxt);
Tom Tuckerd16d4002008-05-06 10:04:50 -0500589 if (ret > 0) {
590 /* read-list posted, defer until data received from client. */
Ilpo Järvinenb1721d22008-12-14 23:19:48 -0800591 goto defer;
Steve Wise0bf48282014-05-28 15:12:01 -0500592 } else if (ret < 0) {
Tom Tuckerd16d4002008-05-06 10:04:50 -0500593 /* Post of read-list failed, free context. */
594 svc_rdma_put_context(ctxt, 1);
595 return 0;
596 }
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600597
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600598 ret = rqstp->rq_arg.head[0].iov_len
599 + rqstp->rq_arg.page_len
600 + rqstp->rq_arg.tail[0].iov_len;
601 svc_rdma_put_context(ctxt, 0);
602 out:
Chuck Lever597561b2015-01-13 11:02:37 -0500603 dprintk("svcrdma: ret=%d, rq_arg.len=%u, "
604 "rq_arg.head[0].iov_base=%p, rq_arg.head[0].iov_len=%zd\n",
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600605 ret, rqstp->rq_arg.len,
606 rqstp->rq_arg.head[0].iov_base,
607 rqstp->rq_arg.head[0].iov_len);
608 rqstp->rq_prot = IPPROTO_MAX;
609 svc_xprt_copy_addrs(rqstp, xprt);
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600610 return ret;
611
612 close_out:
Tom Tucker0e7f0112008-04-23 16:49:54 -0500613 if (ctxt)
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600614 svc_rdma_put_context(ctxt, 1);
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600615 dprintk("svcrdma: transport %p is closing\n", xprt);
616 /*
617 * Set the close bit and enqueue it. svc_recv will see the
618 * close bit and call svc_xprt_delete
619 */
620 set_bit(XPT_CLOSE, &xprt->xpt_flags);
Ilpo Järvinenb1721d22008-12-14 23:19:48 -0800621defer:
Tom Tuckerd5b31be2007-12-12 16:13:23 -0600622 return 0;
623}