2 * Copyright (c) 2005-2006 Network Appliance, Inc. All rights reserved.
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
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
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.
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
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.
39 * Author: Tom Tucker <tom@opengridcomputing.com>
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>
50 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
52 /* Encode an XDR as an array of IB SGE
55 * - head[0] is physically contiguous.
56 * - tail[0] is physically contiguous.
57 * - pages[] is not physically or virtually contigous and consists of
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.
66 * 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.
72 static void xdr_to_sge(struct svcxprt_rdma
*xprt
,
74 struct svc_rdma_req_map
*vec
)
76 int sge_max
= (xdr
->len
+PAGE_SIZE
-1) / PAGE_SIZE
+ 3;
84 (xdr
->head
[0].iov_len
+ xdr
->page_len
+ xdr
->tail
[0].iov_len
));
86 /* Skip the first sge, this is for the RPCRDMA header */
90 vec
->sge
[sge_no
].iov_base
= xdr
->head
[0].iov_base
;
91 vec
->sge
[sge_no
].iov_len
= xdr
->head
[0].iov_len
;
96 page_bytes
= xdr
->page_len
;
97 page_off
= xdr
->page_base
;
99 vec
->sge
[sge_no
].iov_base
=
100 page_address(xdr
->pages
[page_no
]) + page_off
;
101 sge_bytes
= min_t(u32
, page_bytes
, (PAGE_SIZE
- page_off
));
102 page_bytes
-= sge_bytes
;
103 vec
->sge
[sge_no
].iov_len
= sge_bytes
;
107 page_off
= 0; /* reset for next time through loop */
111 if (xdr
->tail
[0].iov_len
) {
112 vec
->sge
[sge_no
].iov_base
= xdr
->tail
[0].iov_base
;
113 vec
->sge
[sge_no
].iov_len
= xdr
->tail
[0].iov_len
;
117 BUG_ON(sge_no
> sge_max
);
122 * - The specified write_len can be represented in sc_max_sge * PAGE_SIZE
124 static int send_write(struct svcxprt_rdma
*xprt
, struct svc_rqst
*rqstp
,
126 u32 xdr_off
, int write_len
,
127 struct svc_rdma_req_map
*vec
)
129 struct ib_send_wr write_wr
;
136 struct svc_rdma_op_ctxt
*ctxt
;
138 BUG_ON(vec
->count
> RPCSVC_MAXPAGES
);
139 dprintk("svcrdma: RDMA_WRITE rmr=%x, to=%llx, xdr_off=%d, "
140 "write_len=%d, vec->sge=%p, vec->count=%lu\n",
141 rmr
, (unsigned long long)to
, xdr_off
,
142 write_len
, vec
->sge
, vec
->count
);
144 ctxt
= svc_rdma_get_context(xprt
);
145 ctxt
->direction
= DMA_TO_DEVICE
;
148 /* Find the SGE associated with xdr_off */
149 for (bc
= xdr_off
, xdr_sge_no
= 1; bc
&& xdr_sge_no
< vec
->count
;
151 if (vec
->sge
[xdr_sge_no
].iov_len
> bc
)
153 bc
-= vec
->sge
[xdr_sge_no
].iov_len
;
160 /* Copy the remaining SGE */
161 while (bc
!= 0 && xdr_sge_no
< vec
->count
) {
162 sge
[sge_no
].lkey
= xprt
->sc_phys_mr
->lkey
;
163 sge_bytes
= min((size_t)bc
,
164 (size_t)(vec
->sge
[xdr_sge_no
].iov_len
-sge_off
));
165 sge
[sge_no
].length
= sge_bytes
;
166 atomic_inc(&xprt
->sc_dma_used
);
168 ib_dma_map_single(xprt
->sc_cm_id
->device
,
170 vec
->sge
[xdr_sge_no
].iov_base
+ sge_off
,
171 sge_bytes
, DMA_TO_DEVICE
);
172 if (dma_mapping_error(sge
[sge_no
].addr
))
182 BUG_ON(xdr_sge_no
> vec
->count
);
184 /* Prepare WRITE WR */
185 memset(&write_wr
, 0, sizeof write_wr
);
186 ctxt
->wr_op
= IB_WR_RDMA_WRITE
;
187 write_wr
.wr_id
= (unsigned long)ctxt
;
188 write_wr
.sg_list
= &sge
[0];
189 write_wr
.num_sge
= sge_no
;
190 write_wr
.opcode
= IB_WR_RDMA_WRITE
;
191 write_wr
.send_flags
= IB_SEND_SIGNALED
;
192 write_wr
.wr
.rdma
.rkey
= rmr
;
193 write_wr
.wr
.rdma
.remote_addr
= to
;
196 atomic_inc(&rdma_stat_write
);
197 if (svc_rdma_send(xprt
, &write_wr
))
201 svc_rdma_put_context(ctxt
, 0);
202 /* Fatal error, close transport */
206 static int send_write_chunks(struct svcxprt_rdma
*xprt
,
207 struct rpcrdma_msg
*rdma_argp
,
208 struct rpcrdma_msg
*rdma_resp
,
209 struct svc_rqst
*rqstp
,
210 struct svc_rdma_req_map
*vec
)
212 u32 xfer_len
= rqstp
->rq_res
.page_len
+ rqstp
->rq_res
.tail
[0].iov_len
;
218 struct rpcrdma_write_array
*arg_ary
;
219 struct rpcrdma_write_array
*res_ary
;
222 arg_ary
= svc_rdma_get_write_array(rdma_argp
);
225 res_ary
= (struct rpcrdma_write_array
*)
226 &rdma_resp
->rm_body
.rm_chunks
[1];
228 max_write
= xprt
->sc_max_sge
* PAGE_SIZE
;
230 /* Write chunks start at the pagelist */
231 for (xdr_off
= rqstp
->rq_res
.head
[0].iov_len
, chunk_no
= 0;
232 xfer_len
&& chunk_no
< arg_ary
->wc_nchunks
;
234 struct rpcrdma_segment
*arg_ch
;
237 arg_ch
= &arg_ary
->wc_array
[chunk_no
].wc_target
;
238 write_len
= min(xfer_len
, arg_ch
->rs_length
);
240 /* Prepare the response chunk given the length actually
242 rs_offset
= get_unaligned(&(arg_ch
->rs_offset
));
243 svc_rdma_xdr_encode_array_chunk(res_ary
, chunk_no
,
250 this_write
= min(write_len
, max_write
);
251 ret
= send_write(xprt
, rqstp
,
253 rs_offset
+ chunk_off
,
258 dprintk("svcrdma: RDMA_WRITE failed, ret=%d\n",
262 chunk_off
+= this_write
;
263 xdr_off
+= this_write
;
264 xfer_len
-= this_write
;
265 write_len
-= this_write
;
268 /* Update the req with the number of chunks actually used */
269 svc_rdma_xdr_encode_write_list(rdma_resp
, chunk_no
);
271 return rqstp
->rq_res
.page_len
+ rqstp
->rq_res
.tail
[0].iov_len
;
274 static int send_reply_chunks(struct svcxprt_rdma
*xprt
,
275 struct rpcrdma_msg
*rdma_argp
,
276 struct rpcrdma_msg
*rdma_resp
,
277 struct svc_rqst
*rqstp
,
278 struct svc_rdma_req_map
*vec
)
280 u32 xfer_len
= rqstp
->rq_res
.len
;
286 struct rpcrdma_segment
*ch
;
287 struct rpcrdma_write_array
*arg_ary
;
288 struct rpcrdma_write_array
*res_ary
;
291 arg_ary
= svc_rdma_get_reply_array(rdma_argp
);
294 /* XXX: need to fix when reply lists occur with read-list and or
296 res_ary
= (struct rpcrdma_write_array
*)
297 &rdma_resp
->rm_body
.rm_chunks
[2];
299 max_write
= xprt
->sc_max_sge
* PAGE_SIZE
;
301 /* xdr offset starts at RPC message */
302 for (xdr_off
= 0, chunk_no
= 0;
303 xfer_len
&& chunk_no
< arg_ary
->wc_nchunks
;
306 ch
= &arg_ary
->wc_array
[chunk_no
].wc_target
;
307 write_len
= min(xfer_len
, ch
->rs_length
);
310 /* Prepare the reply chunk given the length actually
312 rs_offset
= get_unaligned(&(ch
->rs_offset
));
313 svc_rdma_xdr_encode_array_chunk(res_ary
, chunk_no
,
314 ch
->rs_handle
, rs_offset
,
320 this_write
= min(write_len
, max_write
);
321 ret
= send_write(xprt
, rqstp
,
323 rs_offset
+ chunk_off
,
328 dprintk("svcrdma: RDMA_WRITE failed, ret=%d\n",
332 chunk_off
+= this_write
;
333 xdr_off
+= this_write
;
334 xfer_len
-= this_write
;
335 write_len
-= this_write
;
338 /* Update the req with the number of chunks actually used */
339 svc_rdma_xdr_encode_reply_array(res_ary
, chunk_no
);
341 return rqstp
->rq_res
.len
;
344 /* This function prepares the portion of the RPCRDMA message to be
345 * sent in the RDMA_SEND. This function is called after data sent via
346 * RDMA has already been transmitted. There are three cases:
347 * - The RPCRDMA header, RPC header, and payload are all sent in a
348 * single RDMA_SEND. This is the "inline" case.
349 * - The RPCRDMA header and some portion of the RPC header and data
350 * are sent via this RDMA_SEND and another portion of the data is
352 * - The RPCRDMA header [NOMSG] is sent in this RDMA_SEND and the RPC
353 * header and data are all transmitted via RDMA.
354 * In all three cases, this function prepares the RPCRDMA header in
355 * sge[0], the 'type' parameter indicates the type to place in the
356 * RPCRDMA header, and the 'byte_count' field indicates how much of
357 * the XDR to include in this RDMA_SEND.
359 static int send_reply(struct svcxprt_rdma
*rdma
,
360 struct svc_rqst
*rqstp
,
362 struct rpcrdma_msg
*rdma_resp
,
363 struct svc_rdma_op_ctxt
*ctxt
,
364 struct svc_rdma_req_map
*vec
,
367 struct ib_send_wr send_wr
;
373 /* Post a recv buffer to handle another request. */
374 ret
= svc_rdma_post_recv(rdma
);
377 "svcrdma: could not post a receive buffer, err=%d."
378 "Closing transport %p.\n", ret
, rdma
);
379 set_bit(XPT_CLOSE
, &rdma
->sc_xprt
.xpt_flags
);
380 svc_rdma_put_context(ctxt
, 0);
384 /* Prepare the context */
385 ctxt
->pages
[0] = page
;
388 /* Prepare the SGE for the RPCRDMA Header */
389 atomic_inc(&rdma
->sc_dma_used
);
391 ib_dma_map_page(rdma
->sc_cm_id
->device
,
392 page
, 0, PAGE_SIZE
, DMA_TO_DEVICE
);
393 ctxt
->direction
= DMA_TO_DEVICE
;
394 ctxt
->sge
[0].length
= svc_rdma_xdr_get_reply_hdr_len(rdma_resp
);
395 ctxt
->sge
[0].lkey
= rdma
->sc_phys_mr
->lkey
;
397 /* Determine how many of our SGE are to be transmitted */
398 for (sge_no
= 1; byte_count
&& sge_no
< vec
->count
; sge_no
++) {
399 sge_bytes
= min_t(size_t, vec
->sge
[sge_no
].iov_len
, byte_count
);
400 byte_count
-= sge_bytes
;
401 atomic_inc(&rdma
->sc_dma_used
);
402 ctxt
->sge
[sge_no
].addr
=
403 ib_dma_map_single(rdma
->sc_cm_id
->device
,
404 vec
->sge
[sge_no
].iov_base
,
405 sge_bytes
, DMA_TO_DEVICE
);
406 ctxt
->sge
[sge_no
].length
= sge_bytes
;
407 ctxt
->sge
[sge_no
].lkey
= rdma
->sc_phys_mr
->lkey
;
409 BUG_ON(byte_count
!= 0);
411 /* Save all respages in the ctxt and remove them from the
412 * respages array. They are our pages until the I/O
415 for (page_no
= 0; page_no
< rqstp
->rq_resused
; page_no
++) {
416 ctxt
->pages
[page_no
+1] = rqstp
->rq_respages
[page_no
];
418 rqstp
->rq_respages
[page_no
] = NULL
;
419 /* If there are more pages than SGE, terminate SGE list */
420 if (page_no
+1 >= sge_no
)
421 ctxt
->sge
[page_no
+1].length
= 0;
423 BUG_ON(sge_no
> rdma
->sc_max_sge
);
424 memset(&send_wr
, 0, sizeof send_wr
);
425 ctxt
->wr_op
= IB_WR_SEND
;
426 send_wr
.wr_id
= (unsigned long)ctxt
;
427 send_wr
.sg_list
= ctxt
->sge
;
428 send_wr
.num_sge
= sge_no
;
429 send_wr
.opcode
= IB_WR_SEND
;
430 send_wr
.send_flags
= IB_SEND_SIGNALED
;
432 ret
= svc_rdma_send(rdma
, &send_wr
);
434 svc_rdma_put_context(ctxt
, 1);
439 void svc_rdma_prep_reply_hdr(struct svc_rqst
*rqstp
)
444 * Return the start of an xdr buffer.
446 static void *xdr_start(struct xdr_buf
*xdr
)
448 return xdr
->head
[0].iov_base
-
451 xdr
->tail
[0].iov_len
-
452 xdr
->head
[0].iov_len
);
455 int svc_rdma_sendto(struct svc_rqst
*rqstp
)
457 struct svc_xprt
*xprt
= rqstp
->rq_xprt
;
458 struct svcxprt_rdma
*rdma
=
459 container_of(xprt
, struct svcxprt_rdma
, sc_xprt
);
460 struct rpcrdma_msg
*rdma_argp
;
461 struct rpcrdma_msg
*rdma_resp
;
462 struct rpcrdma_write_array
*reply_ary
;
463 enum rpcrdma_proc reply_type
;
466 struct page
*res_page
;
467 struct svc_rdma_op_ctxt
*ctxt
;
468 struct svc_rdma_req_map
*vec
;
470 dprintk("svcrdma: sending response for rqstp=%p\n", rqstp
);
472 /* Get the RDMA request header. */
473 rdma_argp
= xdr_start(&rqstp
->rq_arg
);
475 /* Build an req vec for the XDR */
476 ctxt
= svc_rdma_get_context(rdma
);
477 ctxt
->direction
= DMA_TO_DEVICE
;
478 vec
= svc_rdma_get_req_map();
479 xdr_to_sge(rdma
, &rqstp
->rq_res
, vec
);
481 inline_bytes
= rqstp
->rq_res
.len
;
483 /* Create the RDMA response header */
484 res_page
= svc_rdma_get_page();
485 rdma_resp
= page_address(res_page
);
486 reply_ary
= svc_rdma_get_reply_array(rdma_argp
);
488 reply_type
= RDMA_NOMSG
;
490 reply_type
= RDMA_MSG
;
491 svc_rdma_xdr_encode_reply_header(rdma
, rdma_argp
,
492 rdma_resp
, reply_type
);
494 /* Send any write-chunk data and build resp write-list */
495 ret
= send_write_chunks(rdma
, rdma_argp
, rdma_resp
,
498 printk(KERN_ERR
"svcrdma: failed to send write chunks, rc=%d\n",
504 /* Send any reply-list data and update resp reply-list */
505 ret
= send_reply_chunks(rdma
, rdma_argp
, rdma_resp
,
508 printk(KERN_ERR
"svcrdma: failed to send reply chunks, rc=%d\n",
514 ret
= send_reply(rdma
, rqstp
, res_page
, rdma_resp
, ctxt
, vec
,
516 svc_rdma_put_req_map(vec
);
517 dprintk("svcrdma: send_reply returns %d\n", ret
);
520 svc_rdma_put_req_map(vec
);
521 svc_rdma_put_context(ctxt
, 0);