blob: 9a7a8e7ae038fbb255dd34880e7882feb8eff322 [file] [log] [blame]
Tom Tuckerc06b5402007-12-12 16:13:25 -06001/*
2 * Copyright (c) 2005-2006 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 * Author: Tom Tucker <tom@opengridcomputing.com>
40 */
41
42#include <linux/sunrpc/debug.h>
43#include <linux/sunrpc/rpc_rdma.h>
44#include <linux/spinlock.h>
45#include <asm/unaligned.h>
46#include <rdma/ib_verbs.h>
47#include <rdma/rdma_cm.h>
48#include <linux/sunrpc/svc_rdma.h>
49
50#define RPCDBG_FACILITY RPCDBG_SVCXPRT
51
52/* Encode an XDR as an array of IB SGE
53 *
54 * Assumptions:
55 * - head[0] is physically contiguous.
56 * - tail[0] is physically contiguous.
57 * - pages[] is not physically or virtually contigous and consists of
58 * PAGE_SIZE elements.
59 *
60 * Output:
61 * SGE[0] reserved for RCPRDMA header
62 * SGE[1] data from xdr->head[]
63 * SGE[2..sge_count-2] data from xdr->pages[]
64 * SGE[sge_count-1] data from xdr->tail.
65 *
Tom Tucker34d16e42008-07-02 14:56:13 -050066 * The max SGE we need is the length of the XDR / pagesize + one for
67 * head + one for tail + one for RPCRDMA header. Since RPCSVC_MAXPAGES
68 * reserves a page for both the request and the reply header, and this
69 * array is only concerned with the reply we are assured that we have
70 * on extra page for the RPCRMDA header.
Tom Tuckerc06b5402007-12-12 16:13:25 -060071 */
Tom Tuckerafd566e2008-10-03 15:45:03 -050072int fast_reg_xdr(struct svcxprt_rdma *xprt,
73 struct xdr_buf *xdr,
74 struct svc_rdma_req_map *vec)
75{
76 int sge_no;
77 u32 sge_bytes;
78 u32 page_bytes;
79 u32 page_off;
80 int page_no = 0;
81 u8 *frva;
82 struct svc_rdma_fastreg_mr *frmr;
83
84 frmr = svc_rdma_get_frmr(xprt);
85 if (IS_ERR(frmr))
86 return -ENOMEM;
87 vec->frmr = frmr;
88
89 /* Skip the RPCRDMA header */
90 sge_no = 1;
91
92 /* Map the head. */
93 frva = (void *)((unsigned long)(xdr->head[0].iov_base) & PAGE_MASK);
94 vec->sge[sge_no].iov_base = xdr->head[0].iov_base;
95 vec->sge[sge_no].iov_len = xdr->head[0].iov_len;
96 vec->count = 2;
97 sge_no++;
98
99 /* Build the FRMR */
100 frmr->kva = frva;
101 frmr->direction = DMA_TO_DEVICE;
102 frmr->access_flags = 0;
103 frmr->map_len = PAGE_SIZE;
104 frmr->page_list_len = 1;
105 frmr->page_list->page_list[page_no] =
106 ib_dma_map_single(xprt->sc_cm_id->device,
107 (void *)xdr->head[0].iov_base,
108 PAGE_SIZE, DMA_TO_DEVICE);
109 if (ib_dma_mapping_error(xprt->sc_cm_id->device,
110 frmr->page_list->page_list[page_no]))
111 goto fatal_err;
112 atomic_inc(&xprt->sc_dma_used);
113
114 page_off = xdr->page_base;
115 page_bytes = xdr->page_len + page_off;
116 if (!page_bytes)
117 goto encode_tail;
118
119 /* Map the pages */
120 vec->sge[sge_no].iov_base = frva + frmr->map_len + page_off;
121 vec->sge[sge_no].iov_len = page_bytes;
122 sge_no++;
123 while (page_bytes) {
124 struct page *page;
125
126 page = xdr->pages[page_no++];
127 sge_bytes = min_t(u32, page_bytes, (PAGE_SIZE - page_off));
128 page_bytes -= sge_bytes;
129
130 frmr->page_list->page_list[page_no] =
131 ib_dma_map_page(xprt->sc_cm_id->device, page, 0,
132 PAGE_SIZE, DMA_TO_DEVICE);
133 if (ib_dma_mapping_error(xprt->sc_cm_id->device,
134 frmr->page_list->page_list[page_no]))
135 goto fatal_err;
136
137 atomic_inc(&xprt->sc_dma_used);
138 page_off = 0; /* reset for next time through loop */
139 frmr->map_len += PAGE_SIZE;
140 frmr->page_list_len++;
141 }
142 vec->count++;
143
144 encode_tail:
145 /* Map tail */
146 if (0 == xdr->tail[0].iov_len)
147 goto done;
148
149 vec->count++;
150 vec->sge[sge_no].iov_len = xdr->tail[0].iov_len;
151
152 if (((unsigned long)xdr->tail[0].iov_base & PAGE_MASK) ==
153 ((unsigned long)xdr->head[0].iov_base & PAGE_MASK)) {
154 /*
155 * If head and tail use the same page, we don't need
156 * to map it again.
157 */
158 vec->sge[sge_no].iov_base = xdr->tail[0].iov_base;
159 } else {
160 void *va;
161
162 /* Map another page for the tail */
163 page_off = (unsigned long)xdr->tail[0].iov_base & ~PAGE_MASK;
164 va = (void *)((unsigned long)xdr->tail[0].iov_base & PAGE_MASK);
165 vec->sge[sge_no].iov_base = frva + frmr->map_len + page_off;
166
167 frmr->page_list->page_list[page_no] =
168 ib_dma_map_single(xprt->sc_cm_id->device, va, PAGE_SIZE,
169 DMA_TO_DEVICE);
170 if (ib_dma_mapping_error(xprt->sc_cm_id->device,
171 frmr->page_list->page_list[page_no]))
172 goto fatal_err;
173 atomic_inc(&xprt->sc_dma_used);
174 frmr->map_len += PAGE_SIZE;
175 frmr->page_list_len++;
176 }
177
178 done:
179 if (svc_rdma_fastreg(xprt, frmr))
180 goto fatal_err;
181
182 return 0;
183
184 fatal_err:
185 printk("svcrdma: Error fast registering memory for xprt %p\n", xprt);
186 svc_rdma_put_frmr(xprt, frmr);
187 return -EIO;
188}
189
190static int map_xdr(struct svcxprt_rdma *xprt,
191 struct xdr_buf *xdr,
192 struct svc_rdma_req_map *vec)
Tom Tuckerc06b5402007-12-12 16:13:25 -0600193{
Tom Tuckerc06b5402007-12-12 16:13:25 -0600194 int sge_max = (xdr->len+PAGE_SIZE-1) / PAGE_SIZE + 3;
195 int sge_no;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600196 u32 sge_bytes;
197 u32 page_bytes;
Tom Tucker34d16e42008-07-02 14:56:13 -0500198 u32 page_off;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600199 int page_no;
200
Tom Tucker34d16e42008-07-02 14:56:13 -0500201 BUG_ON(xdr->len !=
202 (xdr->head[0].iov_len + xdr->page_len + xdr->tail[0].iov_len));
203
Tom Tuckerafd566e2008-10-03 15:45:03 -0500204 if (xprt->sc_frmr_pg_list_len)
205 return fast_reg_xdr(xprt, xdr, vec);
206
Tom Tuckerc06b5402007-12-12 16:13:25 -0600207 /* Skip the first sge, this is for the RPCRDMA header */
208 sge_no = 1;
209
210 /* Head SGE */
Tom Tucker34d16e42008-07-02 14:56:13 -0500211 vec->sge[sge_no].iov_base = xdr->head[0].iov_base;
212 vec->sge[sge_no].iov_len = xdr->head[0].iov_len;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600213 sge_no++;
214
215 /* pages SGE */
216 page_no = 0;
217 page_bytes = xdr->page_len;
218 page_off = xdr->page_base;
Tom Tucker34d16e42008-07-02 14:56:13 -0500219 while (page_bytes) {
220 vec->sge[sge_no].iov_base =
221 page_address(xdr->pages[page_no]) + page_off;
222 sge_bytes = min_t(u32, page_bytes, (PAGE_SIZE - page_off));
Tom Tuckerc06b5402007-12-12 16:13:25 -0600223 page_bytes -= sge_bytes;
Tom Tucker34d16e42008-07-02 14:56:13 -0500224 vec->sge[sge_no].iov_len = sge_bytes;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600225
226 sge_no++;
227 page_no++;
228 page_off = 0; /* reset for next time through loop */
229 }
230
231 /* Tail SGE */
Tom Tucker34d16e42008-07-02 14:56:13 -0500232 if (xdr->tail[0].iov_len) {
233 vec->sge[sge_no].iov_base = xdr->tail[0].iov_base;
234 vec->sge[sge_no].iov_len = xdr->tail[0].iov_len;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600235 sge_no++;
236 }
237
238 BUG_ON(sge_no > sge_max);
Tom Tucker34d16e42008-07-02 14:56:13 -0500239 vec->count = sge_no;
Tom Tuckerafd566e2008-10-03 15:45:03 -0500240 return 0;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600241}
242
Tom Tuckerc06b5402007-12-12 16:13:25 -0600243/* Assumptions:
Tom Tuckerafd566e2008-10-03 15:45:03 -0500244 * - We are using FRMR
245 * - or -
Tom Tuckerc06b5402007-12-12 16:13:25 -0600246 * - The specified write_len can be represented in sc_max_sge * PAGE_SIZE
247 */
248static int send_write(struct svcxprt_rdma *xprt, struct svc_rqst *rqstp,
249 u32 rmr, u64 to,
250 u32 xdr_off, int write_len,
Tom Tucker34d16e42008-07-02 14:56:13 -0500251 struct svc_rdma_req_map *vec)
Tom Tuckerc06b5402007-12-12 16:13:25 -0600252{
Tom Tuckerc06b5402007-12-12 16:13:25 -0600253 struct ib_send_wr write_wr;
254 struct ib_sge *sge;
255 int xdr_sge_no;
256 int sge_no;
257 int sge_bytes;
258 int sge_off;
259 int bc;
260 struct svc_rdma_op_ctxt *ctxt;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600261
Tom Tucker34d16e42008-07-02 14:56:13 -0500262 BUG_ON(vec->count > RPCSVC_MAXPAGES);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600263 dprintk("svcrdma: RDMA_WRITE rmr=%x, to=%llx, xdr_off=%d, "
Tom Tucker34d16e42008-07-02 14:56:13 -0500264 "write_len=%d, vec->sge=%p, vec->count=%lu\n",
Roland Dreierbb50c802008-02-08 16:02:04 -0800265 rmr, (unsigned long long)to, xdr_off,
Tom Tucker34d16e42008-07-02 14:56:13 -0500266 write_len, vec->sge, vec->count);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600267
268 ctxt = svc_rdma_get_context(xprt);
Tom Tucker34d16e42008-07-02 14:56:13 -0500269 ctxt->direction = DMA_TO_DEVICE;
270 sge = ctxt->sge;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600271
272 /* Find the SGE associated with xdr_off */
Tom Tucker34d16e42008-07-02 14:56:13 -0500273 for (bc = xdr_off, xdr_sge_no = 1; bc && xdr_sge_no < vec->count;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600274 xdr_sge_no++) {
Tom Tucker34d16e42008-07-02 14:56:13 -0500275 if (vec->sge[xdr_sge_no].iov_len > bc)
Tom Tuckerc06b5402007-12-12 16:13:25 -0600276 break;
Tom Tucker34d16e42008-07-02 14:56:13 -0500277 bc -= vec->sge[xdr_sge_no].iov_len;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600278 }
279
280 sge_off = bc;
281 bc = write_len;
282 sge_no = 0;
283
284 /* Copy the remaining SGE */
Tom Tuckerafd566e2008-10-03 15:45:03 -0500285 while (bc != 0) {
286 sge_bytes = min_t(size_t,
287 bc, vec->sge[xdr_sge_no].iov_len-sge_off);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600288 sge[sge_no].length = sge_bytes;
Tom Tuckerafd566e2008-10-03 15:45:03 -0500289 if (!vec->frmr) {
290 sge[sge_no].addr =
291 ib_dma_map_single(xprt->sc_cm_id->device,
292 (void *)
293 vec->sge[xdr_sge_no].iov_base + sge_off,
294 sge_bytes, DMA_TO_DEVICE);
295 if (ib_dma_mapping_error(xprt->sc_cm_id->device,
296 sge[sge_no].addr))
297 goto err;
298 atomic_inc(&xprt->sc_dma_used);
299 sge[sge_no].lkey = xprt->sc_dma_lkey;
300 } else {
301 sge[sge_no].addr = (unsigned long)
302 vec->sge[xdr_sge_no].iov_base + sge_off;
303 sge[sge_no].lkey = vec->frmr->mr->lkey;
304 }
305 ctxt->count++;
306 ctxt->frmr = vec->frmr;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600307 sge_off = 0;
308 sge_no++;
309 xdr_sge_no++;
Tom Tuckerafd566e2008-10-03 15:45:03 -0500310 BUG_ON(xdr_sge_no > vec->count);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600311 bc -= sge_bytes;
312 }
313
Tom Tuckerc06b5402007-12-12 16:13:25 -0600314 /* Prepare WRITE WR */
315 memset(&write_wr, 0, sizeof write_wr);
316 ctxt->wr_op = IB_WR_RDMA_WRITE;
317 write_wr.wr_id = (unsigned long)ctxt;
318 write_wr.sg_list = &sge[0];
319 write_wr.num_sge = sge_no;
320 write_wr.opcode = IB_WR_RDMA_WRITE;
321 write_wr.send_flags = IB_SEND_SIGNALED;
322 write_wr.wr.rdma.rkey = rmr;
323 write_wr.wr.rdma.remote_addr = to;
324
325 /* Post It */
326 atomic_inc(&rdma_stat_write);
Tom Tucker34d16e42008-07-02 14:56:13 -0500327 if (svc_rdma_send(xprt, &write_wr))
328 goto err;
329 return 0;
330 err:
331 svc_rdma_put_context(ctxt, 0);
332 /* Fatal error, close transport */
333 return -EIO;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600334}
335
336static int send_write_chunks(struct svcxprt_rdma *xprt,
337 struct rpcrdma_msg *rdma_argp,
338 struct rpcrdma_msg *rdma_resp,
339 struct svc_rqst *rqstp,
Tom Tucker34d16e42008-07-02 14:56:13 -0500340 struct svc_rdma_req_map *vec)
Tom Tuckerc06b5402007-12-12 16:13:25 -0600341{
342 u32 xfer_len = rqstp->rq_res.page_len + rqstp->rq_res.tail[0].iov_len;
343 int write_len;
344 int max_write;
345 u32 xdr_off;
346 int chunk_off;
347 int chunk_no;
348 struct rpcrdma_write_array *arg_ary;
349 struct rpcrdma_write_array *res_ary;
350 int ret;
351
352 arg_ary = svc_rdma_get_write_array(rdma_argp);
353 if (!arg_ary)
354 return 0;
355 res_ary = (struct rpcrdma_write_array *)
356 &rdma_resp->rm_body.rm_chunks[1];
357
Tom Tuckerafd566e2008-10-03 15:45:03 -0500358 if (vec->frmr)
359 max_write = vec->frmr->map_len;
360 else
361 max_write = xprt->sc_max_sge * PAGE_SIZE;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600362
363 /* Write chunks start at the pagelist */
364 for (xdr_off = rqstp->rq_res.head[0].iov_len, chunk_no = 0;
365 xfer_len && chunk_no < arg_ary->wc_nchunks;
366 chunk_no++) {
367 struct rpcrdma_segment *arg_ch;
368 u64 rs_offset;
369
370 arg_ch = &arg_ary->wc_array[chunk_no].wc_target;
371 write_len = min(xfer_len, arg_ch->rs_length);
372
373 /* Prepare the response chunk given the length actually
374 * written */
375 rs_offset = get_unaligned(&(arg_ch->rs_offset));
376 svc_rdma_xdr_encode_array_chunk(res_ary, chunk_no,
377 arg_ch->rs_handle,
378 rs_offset,
379 write_len);
380 chunk_off = 0;
381 while (write_len) {
382 int this_write;
383 this_write = min(write_len, max_write);
384 ret = send_write(xprt, rqstp,
385 arg_ch->rs_handle,
386 rs_offset + chunk_off,
387 xdr_off,
388 this_write,
Tom Tucker34d16e42008-07-02 14:56:13 -0500389 vec);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600390 if (ret) {
391 dprintk("svcrdma: RDMA_WRITE failed, ret=%d\n",
392 ret);
393 return -EIO;
394 }
395 chunk_off += this_write;
396 xdr_off += this_write;
397 xfer_len -= this_write;
398 write_len -= this_write;
399 }
400 }
401 /* Update the req with the number of chunks actually used */
402 svc_rdma_xdr_encode_write_list(rdma_resp, chunk_no);
403
404 return rqstp->rq_res.page_len + rqstp->rq_res.tail[0].iov_len;
405}
406
407static int send_reply_chunks(struct svcxprt_rdma *xprt,
408 struct rpcrdma_msg *rdma_argp,
409 struct rpcrdma_msg *rdma_resp,
410 struct svc_rqst *rqstp,
Tom Tucker34d16e42008-07-02 14:56:13 -0500411 struct svc_rdma_req_map *vec)
Tom Tuckerc06b5402007-12-12 16:13:25 -0600412{
413 u32 xfer_len = rqstp->rq_res.len;
414 int write_len;
415 int max_write;
416 u32 xdr_off;
417 int chunk_no;
418 int chunk_off;
419 struct rpcrdma_segment *ch;
420 struct rpcrdma_write_array *arg_ary;
421 struct rpcrdma_write_array *res_ary;
422 int ret;
423
424 arg_ary = svc_rdma_get_reply_array(rdma_argp);
425 if (!arg_ary)
426 return 0;
427 /* XXX: need to fix when reply lists occur with read-list and or
428 * write-list */
429 res_ary = (struct rpcrdma_write_array *)
430 &rdma_resp->rm_body.rm_chunks[2];
431
Tom Tuckerafd566e2008-10-03 15:45:03 -0500432 if (vec->frmr)
433 max_write = vec->frmr->map_len;
434 else
435 max_write = xprt->sc_max_sge * PAGE_SIZE;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600436
437 /* xdr offset starts at RPC message */
438 for (xdr_off = 0, chunk_no = 0;
439 xfer_len && chunk_no < arg_ary->wc_nchunks;
440 chunk_no++) {
441 u64 rs_offset;
442 ch = &arg_ary->wc_array[chunk_no].wc_target;
443 write_len = min(xfer_len, ch->rs_length);
444
Tom Tuckerc06b5402007-12-12 16:13:25 -0600445 /* Prepare the reply chunk given the length actually
446 * written */
447 rs_offset = get_unaligned(&(ch->rs_offset));
448 svc_rdma_xdr_encode_array_chunk(res_ary, chunk_no,
449 ch->rs_handle, rs_offset,
450 write_len);
451 chunk_off = 0;
452 while (write_len) {
453 int this_write;
454
455 this_write = min(write_len, max_write);
456 ret = send_write(xprt, rqstp,
457 ch->rs_handle,
458 rs_offset + chunk_off,
459 xdr_off,
460 this_write,
Tom Tucker34d16e42008-07-02 14:56:13 -0500461 vec);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600462 if (ret) {
463 dprintk("svcrdma: RDMA_WRITE failed, ret=%d\n",
464 ret);
465 return -EIO;
466 }
467 chunk_off += this_write;
468 xdr_off += this_write;
469 xfer_len -= this_write;
470 write_len -= this_write;
471 }
472 }
473 /* Update the req with the number of chunks actually used */
474 svc_rdma_xdr_encode_reply_array(res_ary, chunk_no);
475
476 return rqstp->rq_res.len;
477}
478
479/* This function prepares the portion of the RPCRDMA message to be
480 * sent in the RDMA_SEND. This function is called after data sent via
481 * RDMA has already been transmitted. There are three cases:
482 * - The RPCRDMA header, RPC header, and payload are all sent in a
483 * single RDMA_SEND. This is the "inline" case.
484 * - The RPCRDMA header and some portion of the RPC header and data
485 * are sent via this RDMA_SEND and another portion of the data is
486 * sent via RDMA.
487 * - The RPCRDMA header [NOMSG] is sent in this RDMA_SEND and the RPC
488 * header and data are all transmitted via RDMA.
489 * In all three cases, this function prepares the RPCRDMA header in
490 * sge[0], the 'type' parameter indicates the type to place in the
491 * RPCRDMA header, and the 'byte_count' field indicates how much of
492 * the XDR to include in this RDMA_SEND.
493 */
494static int send_reply(struct svcxprt_rdma *rdma,
495 struct svc_rqst *rqstp,
496 struct page *page,
497 struct rpcrdma_msg *rdma_resp,
498 struct svc_rdma_op_ctxt *ctxt,
Tom Tucker34d16e42008-07-02 14:56:13 -0500499 struct svc_rdma_req_map *vec,
Tom Tuckerc06b5402007-12-12 16:13:25 -0600500 int byte_count)
501{
502 struct ib_send_wr send_wr;
Tom Tuckerafd566e2008-10-03 15:45:03 -0500503 struct ib_send_wr inv_wr;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600504 int sge_no;
505 int sge_bytes;
506 int page_no;
507 int ret;
508
Tom Tucker0e7f0112008-04-23 16:49:54 -0500509 /* Post a recv buffer to handle another request. */
510 ret = svc_rdma_post_recv(rdma);
511 if (ret) {
512 printk(KERN_INFO
513 "svcrdma: could not post a receive buffer, err=%d."
514 "Closing transport %p.\n", ret, rdma);
515 set_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags);
Tom Tucker5ac461a2008-04-25 18:08:59 -0500516 svc_rdma_put_context(ctxt, 0);
517 return -ENOTCONN;
Tom Tucker0e7f0112008-04-23 16:49:54 -0500518 }
519
Tom Tuckerc06b5402007-12-12 16:13:25 -0600520 /* Prepare the context */
521 ctxt->pages[0] = page;
522 ctxt->count = 1;
Tom Tuckerafd566e2008-10-03 15:45:03 -0500523 ctxt->frmr = vec->frmr;
524 if (vec->frmr)
525 set_bit(RDMACTXT_F_FAST_UNREG, &ctxt->flags);
526 else
527 clear_bit(RDMACTXT_F_FAST_UNREG, &ctxt->flags);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600528
529 /* Prepare the SGE for the RPCRDMA Header */
530 ctxt->sge[0].addr =
531 ib_dma_map_page(rdma->sc_cm_id->device,
532 page, 0, PAGE_SIZE, DMA_TO_DEVICE);
Tom Tuckerafd566e2008-10-03 15:45:03 -0500533 if (ib_dma_mapping_error(rdma->sc_cm_id->device, ctxt->sge[0].addr))
534 goto err;
535 atomic_inc(&rdma->sc_dma_used);
536
Tom Tuckerc06b5402007-12-12 16:13:25 -0600537 ctxt->direction = DMA_TO_DEVICE;
Tom Tuckerafd566e2008-10-03 15:45:03 -0500538
Tom Tuckerc06b5402007-12-12 16:13:25 -0600539 ctxt->sge[0].length = svc_rdma_xdr_get_reply_hdr_len(rdma_resp);
Tom Tuckerafd566e2008-10-03 15:45:03 -0500540 ctxt->sge[0].lkey = rdma->sc_dma_lkey;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600541
542 /* Determine how many of our SGE are to be transmitted */
Tom Tucker34d16e42008-07-02 14:56:13 -0500543 for (sge_no = 1; byte_count && sge_no < vec->count; sge_no++) {
544 sge_bytes = min_t(size_t, vec->sge[sge_no].iov_len, byte_count);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600545 byte_count -= sge_bytes;
Tom Tuckerafd566e2008-10-03 15:45:03 -0500546 if (!vec->frmr) {
547 ctxt->sge[sge_no].addr =
548 ib_dma_map_single(rdma->sc_cm_id->device,
549 vec->sge[sge_no].iov_base,
550 sge_bytes, DMA_TO_DEVICE);
551 if (ib_dma_mapping_error(rdma->sc_cm_id->device,
552 ctxt->sge[sge_no].addr))
553 goto err;
554 atomic_inc(&rdma->sc_dma_used);
555 ctxt->sge[sge_no].lkey = rdma->sc_dma_lkey;
556 } else {
557 ctxt->sge[sge_no].addr = (unsigned long)
558 vec->sge[sge_no].iov_base;
559 ctxt->sge[sge_no].lkey = vec->frmr->mr->lkey;
560 }
Tom Tucker34d16e42008-07-02 14:56:13 -0500561 ctxt->sge[sge_no].length = sge_bytes;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600562 }
563 BUG_ON(byte_count != 0);
564
565 /* Save all respages in the ctxt and remove them from the
566 * respages array. They are our pages until the I/O
567 * completes.
568 */
569 for (page_no = 0; page_no < rqstp->rq_resused; page_no++) {
570 ctxt->pages[page_no+1] = rqstp->rq_respages[page_no];
571 ctxt->count++;
572 rqstp->rq_respages[page_no] = NULL;
Tom Tuckerafd566e2008-10-03 15:45:03 -0500573 /*
574 * If there are more pages than SGE, terminate SGE
575 * list so that svc_rdma_unmap_dma doesn't attempt to
576 * unmap garbage.
577 */
Tom Tucker34d16e42008-07-02 14:56:13 -0500578 if (page_no+1 >= sge_no)
579 ctxt->sge[page_no+1].length = 0;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600580 }
Tom Tuckerc06b5402007-12-12 16:13:25 -0600581 BUG_ON(sge_no > rdma->sc_max_sge);
Tom Tuckerafd566e2008-10-03 15:45:03 -0500582 BUG_ON(sge_no > ctxt->count);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600583 memset(&send_wr, 0, sizeof send_wr);
584 ctxt->wr_op = IB_WR_SEND;
585 send_wr.wr_id = (unsigned long)ctxt;
586 send_wr.sg_list = ctxt->sge;
587 send_wr.num_sge = sge_no;
588 send_wr.opcode = IB_WR_SEND;
589 send_wr.send_flags = IB_SEND_SIGNALED;
Tom Tuckerafd566e2008-10-03 15:45:03 -0500590 if (vec->frmr) {
591 /* Prepare INVALIDATE WR */
592 memset(&inv_wr, 0, sizeof inv_wr);
593 inv_wr.opcode = IB_WR_LOCAL_INV;
594 inv_wr.send_flags = IB_SEND_SIGNALED;
595 inv_wr.ex.invalidate_rkey =
596 vec->frmr->mr->lkey;
597 send_wr.next = &inv_wr;
598 }
Tom Tuckerc06b5402007-12-12 16:13:25 -0600599
600 ret = svc_rdma_send(rdma, &send_wr);
601 if (ret)
Tom Tuckerafd566e2008-10-03 15:45:03 -0500602 goto err;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600603
Tom Tuckerafd566e2008-10-03 15:45:03 -0500604 return 0;
605
606 err:
607 svc_rdma_put_frmr(rdma, vec->frmr);
608 svc_rdma_put_context(ctxt, 1);
609 return -EIO;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600610}
611
612void svc_rdma_prep_reply_hdr(struct svc_rqst *rqstp)
613{
614}
615
616/*
617 * Return the start of an xdr buffer.
618 */
619static void *xdr_start(struct xdr_buf *xdr)
620{
621 return xdr->head[0].iov_base -
622 (xdr->len -
623 xdr->page_len -
624 xdr->tail[0].iov_len -
625 xdr->head[0].iov_len);
626}
627
628int svc_rdma_sendto(struct svc_rqst *rqstp)
629{
630 struct svc_xprt *xprt = rqstp->rq_xprt;
631 struct svcxprt_rdma *rdma =
632 container_of(xprt, struct svcxprt_rdma, sc_xprt);
633 struct rpcrdma_msg *rdma_argp;
634 struct rpcrdma_msg *rdma_resp;
635 struct rpcrdma_write_array *reply_ary;
636 enum rpcrdma_proc reply_type;
637 int ret;
638 int inline_bytes;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600639 struct page *res_page;
640 struct svc_rdma_op_ctxt *ctxt;
Tom Tucker34d16e42008-07-02 14:56:13 -0500641 struct svc_rdma_req_map *vec;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600642
643 dprintk("svcrdma: sending response for rqstp=%p\n", rqstp);
644
645 /* Get the RDMA request header. */
646 rdma_argp = xdr_start(&rqstp->rq_arg);
647
Tom Tucker34d16e42008-07-02 14:56:13 -0500648 /* Build an req vec for the XDR */
Tom Tuckerc06b5402007-12-12 16:13:25 -0600649 ctxt = svc_rdma_get_context(rdma);
650 ctxt->direction = DMA_TO_DEVICE;
Tom Tucker34d16e42008-07-02 14:56:13 -0500651 vec = svc_rdma_get_req_map();
Tom Tuckerafd566e2008-10-03 15:45:03 -0500652 ret = map_xdr(rdma, &rqstp->rq_res, vec);
653 if (ret)
654 goto err0;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600655 inline_bytes = rqstp->rq_res.len;
656
657 /* Create the RDMA response header */
658 res_page = svc_rdma_get_page();
659 rdma_resp = page_address(res_page);
660 reply_ary = svc_rdma_get_reply_array(rdma_argp);
661 if (reply_ary)
662 reply_type = RDMA_NOMSG;
663 else
664 reply_type = RDMA_MSG;
665 svc_rdma_xdr_encode_reply_header(rdma, rdma_argp,
666 rdma_resp, reply_type);
667
668 /* Send any write-chunk data and build resp write-list */
669 ret = send_write_chunks(rdma, rdma_argp, rdma_resp,
Tom Tucker34d16e42008-07-02 14:56:13 -0500670 rqstp, vec);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600671 if (ret < 0) {
672 printk(KERN_ERR "svcrdma: failed to send write chunks, rc=%d\n",
673 ret);
Tom Tuckerafd566e2008-10-03 15:45:03 -0500674 goto err1;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600675 }
676 inline_bytes -= ret;
677
678 /* Send any reply-list data and update resp reply-list */
679 ret = send_reply_chunks(rdma, rdma_argp, rdma_resp,
Tom Tucker34d16e42008-07-02 14:56:13 -0500680 rqstp, vec);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600681 if (ret < 0) {
682 printk(KERN_ERR "svcrdma: failed to send reply chunks, rc=%d\n",
683 ret);
Tom Tuckerafd566e2008-10-03 15:45:03 -0500684 goto err1;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600685 }
686 inline_bytes -= ret;
687
Tom Tucker34d16e42008-07-02 14:56:13 -0500688 ret = send_reply(rdma, rqstp, res_page, rdma_resp, ctxt, vec,
Tom Tuckerc06b5402007-12-12 16:13:25 -0600689 inline_bytes);
Tom Tucker34d16e42008-07-02 14:56:13 -0500690 svc_rdma_put_req_map(vec);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600691 dprintk("svcrdma: send_reply returns %d\n", ret);
692 return ret;
Tom Tuckerafd566e2008-10-03 15:45:03 -0500693
694 err1:
695 put_page(res_page);
696 err0:
Tom Tucker34d16e42008-07-02 14:56:13 -0500697 svc_rdma_put_req_map(vec);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600698 svc_rdma_put_context(ctxt, 0);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600699 return ret;
700}