blob: a8fab9968891e94cf87c5ce55e35bf9931590955 [file] [log] [blame]
Tom Tuckerc06b5402007-12-12 16:13:25 -06001/*
Steve Wise0bf48282014-05-28 15:12:01 -05002 * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved.
Tom Tuckerc06b5402007-12-12 16:13:25 -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>
46#include <asm/unaligned.h>
47#include <rdma/ib_verbs.h>
48#include <rdma/rdma_cm.h>
49#include <linux/sunrpc/svc_rdma.h>
50
51#define RPCDBG_FACILITY RPCDBG_SVCXPRT
52
Chuck Levercf570a92016-03-01 13:05:45 -050053static u32 xdr_padsize(u32 len)
54{
55 return (len & 3) ? (4 - (len & 3)) : 0;
56}
57
Chuck Leverba986c92016-01-07 14:49:53 -050058int svc_rdma_map_xdr(struct svcxprt_rdma *xprt,
59 struct xdr_buf *xdr,
Chuck Leverf6763c22016-03-01 13:05:54 -050060 struct svc_rdma_req_map *vec,
61 bool write_chunk_present)
Tom Tuckerc06b5402007-12-12 16:13:25 -060062{
Tom Tuckerc06b5402007-12-12 16:13:25 -060063 int sge_no;
Tom Tuckerc06b5402007-12-12 16:13:25 -060064 u32 sge_bytes;
65 u32 page_bytes;
Tom Tucker34d16e42008-07-02 14:56:13 -050066 u32 page_off;
Tom Tuckerc06b5402007-12-12 16:13:25 -060067 int page_no;
68
Chuck Lever3fe04ee2015-01-13 11:03:03 -050069 if (xdr->len !=
70 (xdr->head[0].iov_len + xdr->page_len + xdr->tail[0].iov_len)) {
Chuck Leverba986c92016-01-07 14:49:53 -050071 pr_err("svcrdma: %s: XDR buffer length error\n", __func__);
Chuck Lever3fe04ee2015-01-13 11:03:03 -050072 return -EIO;
73 }
Tom Tucker34d16e42008-07-02 14:56:13 -050074
Tom Tuckerc06b5402007-12-12 16:13:25 -060075 /* Skip the first sge, this is for the RPCRDMA header */
76 sge_no = 1;
77
78 /* Head SGE */
Tom Tucker34d16e42008-07-02 14:56:13 -050079 vec->sge[sge_no].iov_base = xdr->head[0].iov_base;
80 vec->sge[sge_no].iov_len = xdr->head[0].iov_len;
Tom Tuckerc06b5402007-12-12 16:13:25 -060081 sge_no++;
82
83 /* pages SGE */
84 page_no = 0;
85 page_bytes = xdr->page_len;
86 page_off = xdr->page_base;
Tom Tucker34d16e42008-07-02 14:56:13 -050087 while (page_bytes) {
88 vec->sge[sge_no].iov_base =
89 page_address(xdr->pages[page_no]) + page_off;
90 sge_bytes = min_t(u32, page_bytes, (PAGE_SIZE - page_off));
Tom Tuckerc06b5402007-12-12 16:13:25 -060091 page_bytes -= sge_bytes;
Tom Tucker34d16e42008-07-02 14:56:13 -050092 vec->sge[sge_no].iov_len = sge_bytes;
Tom Tuckerc06b5402007-12-12 16:13:25 -060093
94 sge_no++;
95 page_no++;
96 page_off = 0; /* reset for next time through loop */
97 }
98
99 /* Tail SGE */
Tom Tucker34d16e42008-07-02 14:56:13 -0500100 if (xdr->tail[0].iov_len) {
Chuck Leverf6763c22016-03-01 13:05:54 -0500101 unsigned char *base = xdr->tail[0].iov_base;
102 size_t len = xdr->tail[0].iov_len;
103 u32 xdr_pad = xdr_padsize(xdr->page_len);
104
105 if (write_chunk_present && xdr_pad) {
106 base += xdr_pad;
107 len -= xdr_pad;
108 }
109
110 if (len) {
111 vec->sge[sge_no].iov_base = base;
112 vec->sge[sge_no].iov_len = len;
113 sge_no++;
114 }
Tom Tuckerc06b5402007-12-12 16:13:25 -0600115 }
116
Chuck Leverba986c92016-01-07 14:49:53 -0500117 dprintk("svcrdma: %s: sge_no %d page_no %d "
Tom Talpey2e3c2302009-03-12 22:21:21 -0400118 "page_base %u page_len %u head_len %zu tail_len %zu\n",
Chuck Leverba986c92016-01-07 14:49:53 -0500119 __func__, sge_no, page_no, xdr->page_base, xdr->page_len,
Tom Talpeyb1e1e152009-03-11 14:37:55 -0400120 xdr->head[0].iov_len, xdr->tail[0].iov_len);
121
Tom Tucker34d16e42008-07-02 14:56:13 -0500122 vec->count = sge_no;
Tom Tuckerafd566e2008-10-03 15:45:03 -0500123 return 0;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600124}
125
Tom Tuckerb432e6b2010-10-12 15:33:52 -0500126static dma_addr_t dma_map_xdr(struct svcxprt_rdma *xprt,
127 struct xdr_buf *xdr,
128 u32 xdr_off, size_t len, int dir)
129{
130 struct page *page;
131 dma_addr_t dma_addr;
132 if (xdr_off < xdr->head[0].iov_len) {
133 /* This offset is in the head */
134 xdr_off += (unsigned long)xdr->head[0].iov_base & ~PAGE_MASK;
135 page = virt_to_page(xdr->head[0].iov_base);
136 } else {
137 xdr_off -= xdr->head[0].iov_len;
138 if (xdr_off < xdr->page_len) {
139 /* This offset is in the page list */
Jeff Layton3cbe01a2014-03-17 13:10:05 -0400140 xdr_off += xdr->page_base;
Tom Tuckerb432e6b2010-10-12 15:33:52 -0500141 page = xdr->pages[xdr_off >> PAGE_SHIFT];
142 xdr_off &= ~PAGE_MASK;
143 } else {
144 /* This offset is in the tail */
145 xdr_off -= xdr->page_len;
146 xdr_off += (unsigned long)
147 xdr->tail[0].iov_base & ~PAGE_MASK;
148 page = virt_to_page(xdr->tail[0].iov_base);
149 }
150 }
151 dma_addr = ib_dma_map_page(xprt->sc_cm_id->device, page, xdr_off,
152 min_t(size_t, PAGE_SIZE, len), dir);
153 return dma_addr;
154}
155
Chuck Lever10dc4512015-07-09 16:45:28 -0400156/* Returns the address of the first read chunk or <nul> if no read chunk
157 * is present
158 */
159struct rpcrdma_read_chunk *
160svc_rdma_get_read_chunk(struct rpcrdma_msg *rmsgp)
161{
162 struct rpcrdma_read_chunk *ch =
163 (struct rpcrdma_read_chunk *)&rmsgp->rm_body.rm_chunks[0];
164
165 if (ch->rc_discrim == xdr_zero)
166 return NULL;
167 return ch;
168}
169
170/* Returns the address of the first read write array element or <nul>
171 * if no write array list is present
172 */
173static struct rpcrdma_write_array *
174svc_rdma_get_write_array(struct rpcrdma_msg *rmsgp)
175{
176 if (rmsgp->rm_body.rm_chunks[0] != xdr_zero ||
177 rmsgp->rm_body.rm_chunks[1] == xdr_zero)
178 return NULL;
179 return (struct rpcrdma_write_array *)&rmsgp->rm_body.rm_chunks[1];
180}
181
182/* Returns the address of the first reply array element or <nul> if no
183 * reply array is present
184 */
185static struct rpcrdma_write_array *
Chuck Lever08ae4e72016-03-01 13:05:36 -0500186svc_rdma_get_reply_array(struct rpcrdma_msg *rmsgp,
187 struct rpcrdma_write_array *wr_ary)
Chuck Lever10dc4512015-07-09 16:45:28 -0400188{
189 struct rpcrdma_read_chunk *rch;
Chuck Lever10dc4512015-07-09 16:45:28 -0400190 struct rpcrdma_write_array *rp_ary;
191
192 /* XXX: Need to fix when reply chunk may occur with read list
193 * and/or write list.
194 */
195 if (rmsgp->rm_body.rm_chunks[0] != xdr_zero ||
196 rmsgp->rm_body.rm_chunks[1] != xdr_zero)
197 return NULL;
198
199 rch = svc_rdma_get_read_chunk(rmsgp);
200 if (rch) {
201 while (rch->rc_discrim != xdr_zero)
202 rch++;
203
204 /* The reply chunk follows an empty write array located
205 * at 'rc_position' here. The reply array is at rc_target.
206 */
207 rp_ary = (struct rpcrdma_write_array *)&rch->rc_target;
208 goto found_it;
209 }
210
Chuck Lever10dc4512015-07-09 16:45:28 -0400211 if (wr_ary) {
212 int chunk = be32_to_cpu(wr_ary->wc_nchunks);
213
214 rp_ary = (struct rpcrdma_write_array *)
215 &wr_ary->wc_array[chunk].wc_target.rs_length;
216 goto found_it;
217 }
218
219 /* No read list, no write list */
220 rp_ary = (struct rpcrdma_write_array *)&rmsgp->rm_body.rm_chunks[2];
221
222 found_it:
223 if (rp_ary->wc_discrim == xdr_zero)
224 return NULL;
225 return rp_ary;
226}
227
Tom Tuckerc06b5402007-12-12 16:13:25 -0600228/* Assumptions:
229 * - The specified write_len can be represented in sc_max_sge * PAGE_SIZE
230 */
231static int send_write(struct svcxprt_rdma *xprt, struct svc_rqst *rqstp,
232 u32 rmr, u64 to,
233 u32 xdr_off, int write_len,
Tom Tucker34d16e42008-07-02 14:56:13 -0500234 struct svc_rdma_req_map *vec)
Tom Tuckerc06b5402007-12-12 16:13:25 -0600235{
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100236 struct ib_rdma_wr write_wr;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600237 struct ib_sge *sge;
238 int xdr_sge_no;
239 int sge_no;
240 int sge_bytes;
241 int sge_off;
242 int bc;
243 struct svc_rdma_op_ctxt *ctxt;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600244
Chuck Lever3fe04ee2015-01-13 11:03:03 -0500245 if (vec->count > RPCSVC_MAXPAGES) {
246 pr_err("svcrdma: Too many pages (%lu)\n", vec->count);
247 return -EIO;
248 }
249
Tom Tuckerc06b5402007-12-12 16:13:25 -0600250 dprintk("svcrdma: RDMA_WRITE rmr=%x, to=%llx, xdr_off=%d, "
Tom Tucker34d16e42008-07-02 14:56:13 -0500251 "write_len=%d, vec->sge=%p, vec->count=%lu\n",
Roland Dreierbb50c802008-02-08 16:02:04 -0800252 rmr, (unsigned long long)to, xdr_off,
Tom Tucker34d16e42008-07-02 14:56:13 -0500253 write_len, vec->sge, vec->count);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600254
255 ctxt = svc_rdma_get_context(xprt);
Tom Tucker34d16e42008-07-02 14:56:13 -0500256 ctxt->direction = DMA_TO_DEVICE;
257 sge = ctxt->sge;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600258
259 /* Find the SGE associated with xdr_off */
Tom Tucker34d16e42008-07-02 14:56:13 -0500260 for (bc = xdr_off, xdr_sge_no = 1; bc && xdr_sge_no < vec->count;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600261 xdr_sge_no++) {
Tom Tucker34d16e42008-07-02 14:56:13 -0500262 if (vec->sge[xdr_sge_no].iov_len > bc)
Tom Tuckerc06b5402007-12-12 16:13:25 -0600263 break;
Tom Tucker34d16e42008-07-02 14:56:13 -0500264 bc -= vec->sge[xdr_sge_no].iov_len;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600265 }
266
267 sge_off = bc;
268 bc = write_len;
269 sge_no = 0;
270
271 /* Copy the remaining SGE */
Tom Tuckerafd566e2008-10-03 15:45:03 -0500272 while (bc != 0) {
273 sge_bytes = min_t(size_t,
274 bc, vec->sge[xdr_sge_no].iov_len-sge_off);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600275 sge[sge_no].length = sge_bytes;
Steve Wise0bf48282014-05-28 15:12:01 -0500276 sge[sge_no].addr =
277 dma_map_xdr(xprt, &rqstp->rq_res, xdr_off,
278 sge_bytes, DMA_TO_DEVICE);
279 xdr_off += sge_bytes;
280 if (ib_dma_mapping_error(xprt->sc_cm_id->device,
281 sge[sge_no].addr))
282 goto err;
283 atomic_inc(&xprt->sc_dma_used);
Christoph Hellwig5fe10432016-01-07 23:53:41 -0800284 sge[sge_no].lkey = xprt->sc_pd->local_dma_lkey;
Tom Tuckerafd566e2008-10-03 15:45:03 -0500285 ctxt->count++;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600286 sge_off = 0;
287 sge_no++;
288 xdr_sge_no++;
Chuck Lever3fe04ee2015-01-13 11:03:03 -0500289 if (xdr_sge_no > vec->count) {
290 pr_err("svcrdma: Too many sges (%d)\n", xdr_sge_no);
291 goto err;
292 }
Tom Tuckerc06b5402007-12-12 16:13:25 -0600293 bc -= sge_bytes;
Steve Wise25594292014-07-09 13:49:15 -0500294 if (sge_no == xprt->sc_max_sge)
295 break;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600296 }
297
Tom Tuckerc06b5402007-12-12 16:13:25 -0600298 /* Prepare WRITE WR */
299 memset(&write_wr, 0, sizeof write_wr);
300 ctxt->wr_op = IB_WR_RDMA_WRITE;
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100301 write_wr.wr.wr_id = (unsigned long)ctxt;
302 write_wr.wr.sg_list = &sge[0];
303 write_wr.wr.num_sge = sge_no;
304 write_wr.wr.opcode = IB_WR_RDMA_WRITE;
305 write_wr.wr.send_flags = IB_SEND_SIGNALED;
306 write_wr.rkey = rmr;
307 write_wr.remote_addr = to;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600308
309 /* Post It */
310 atomic_inc(&rdma_stat_write);
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100311 if (svc_rdma_send(xprt, &write_wr.wr))
Tom Tucker34d16e42008-07-02 14:56:13 -0500312 goto err;
Steve Wise25594292014-07-09 13:49:15 -0500313 return write_len - bc;
Tom Tucker34d16e42008-07-02 14:56:13 -0500314 err:
Tom Tucker4a843862010-10-12 15:33:57 -0500315 svc_rdma_unmap_dma(ctxt);
Tom Tucker34d16e42008-07-02 14:56:13 -0500316 svc_rdma_put_context(ctxt, 0);
317 /* Fatal error, close transport */
318 return -EIO;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600319}
320
Chuck Lever08ae4e72016-03-01 13:05:36 -0500321noinline
Tom Tuckerc06b5402007-12-12 16:13:25 -0600322static int send_write_chunks(struct svcxprt_rdma *xprt,
Chuck Lever08ae4e72016-03-01 13:05:36 -0500323 struct rpcrdma_write_array *wr_ary,
Tom Tuckerc06b5402007-12-12 16:13:25 -0600324 struct rpcrdma_msg *rdma_resp,
325 struct svc_rqst *rqstp,
Tom Tucker34d16e42008-07-02 14:56:13 -0500326 struct svc_rdma_req_map *vec)
Tom Tuckerc06b5402007-12-12 16:13:25 -0600327{
Chuck Levercf570a92016-03-01 13:05:45 -0500328 u32 xfer_len = rqstp->rq_res.page_len;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600329 int write_len;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600330 u32 xdr_off;
331 int chunk_off;
332 int chunk_no;
Chuck Lever70747c22015-06-04 11:20:39 -0400333 int nchunks;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600334 struct rpcrdma_write_array *res_ary;
335 int ret;
336
Tom Tuckerc06b5402007-12-12 16:13:25 -0600337 res_ary = (struct rpcrdma_write_array *)
338 &rdma_resp->rm_body.rm_chunks[1];
339
Tom Tuckerc06b5402007-12-12 16:13:25 -0600340 /* Write chunks start at the pagelist */
Chuck Lever08ae4e72016-03-01 13:05:36 -0500341 nchunks = be32_to_cpu(wr_ary->wc_nchunks);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600342 for (xdr_off = rqstp->rq_res.head[0].iov_len, chunk_no = 0;
Chuck Lever70747c22015-06-04 11:20:39 -0400343 xfer_len && chunk_no < nchunks;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600344 chunk_no++) {
345 struct rpcrdma_segment *arg_ch;
346 u64 rs_offset;
347
Chuck Lever08ae4e72016-03-01 13:05:36 -0500348 arg_ch = &wr_ary->wc_array[chunk_no].wc_target;
Chuck Lever70747c22015-06-04 11:20:39 -0400349 write_len = min(xfer_len, be32_to_cpu(arg_ch->rs_length));
Tom Tuckerc06b5402007-12-12 16:13:25 -0600350
351 /* Prepare the response chunk given the length actually
352 * written */
Tom Tuckercec56c82012-02-15 11:30:00 -0600353 xdr_decode_hyper((__be32 *)&arg_ch->rs_offset, &rs_offset);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600354 svc_rdma_xdr_encode_array_chunk(res_ary, chunk_no,
Tom Tuckercec56c82012-02-15 11:30:00 -0600355 arg_ch->rs_handle,
356 arg_ch->rs_offset,
357 write_len);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600358 chunk_off = 0;
359 while (write_len) {
Tom Tuckerc06b5402007-12-12 16:13:25 -0600360 ret = send_write(xprt, rqstp,
Chuck Lever70747c22015-06-04 11:20:39 -0400361 be32_to_cpu(arg_ch->rs_handle),
Tom Tuckerc06b5402007-12-12 16:13:25 -0600362 rs_offset + chunk_off,
363 xdr_off,
Steve Wise25594292014-07-09 13:49:15 -0500364 write_len,
Tom Tucker34d16e42008-07-02 14:56:13 -0500365 vec);
Chuck Lever08ae4e72016-03-01 13:05:36 -0500366 if (ret <= 0)
367 goto out_err;
Steve Wise25594292014-07-09 13:49:15 -0500368 chunk_off += ret;
369 xdr_off += ret;
370 xfer_len -= ret;
371 write_len -= ret;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600372 }
373 }
374 /* Update the req with the number of chunks actually used */
375 svc_rdma_xdr_encode_write_list(rdma_resp, chunk_no);
376
Chuck Levercf570a92016-03-01 13:05:45 -0500377 return rqstp->rq_res.page_len;
Chuck Lever08ae4e72016-03-01 13:05:36 -0500378
379out_err:
380 pr_err("svcrdma: failed to send write chunks, rc=%d\n", ret);
381 return -EIO;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600382}
383
Chuck Lever08ae4e72016-03-01 13:05:36 -0500384noinline
Tom Tuckerc06b5402007-12-12 16:13:25 -0600385static int send_reply_chunks(struct svcxprt_rdma *xprt,
Chuck Lever08ae4e72016-03-01 13:05:36 -0500386 struct rpcrdma_write_array *rp_ary,
Tom Tuckerc06b5402007-12-12 16:13:25 -0600387 struct rpcrdma_msg *rdma_resp,
388 struct svc_rqst *rqstp,
Tom Tucker34d16e42008-07-02 14:56:13 -0500389 struct svc_rdma_req_map *vec)
Tom Tuckerc06b5402007-12-12 16:13:25 -0600390{
391 u32 xfer_len = rqstp->rq_res.len;
392 int write_len;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600393 u32 xdr_off;
394 int chunk_no;
395 int chunk_off;
Tom Tuckercec56c82012-02-15 11:30:00 -0600396 int nchunks;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600397 struct rpcrdma_segment *ch;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600398 struct rpcrdma_write_array *res_ary;
399 int ret;
400
Tom Tuckerc06b5402007-12-12 16:13:25 -0600401 /* XXX: need to fix when reply lists occur with read-list and or
402 * write-list */
403 res_ary = (struct rpcrdma_write_array *)
404 &rdma_resp->rm_body.rm_chunks[2];
405
Tom Tuckerc06b5402007-12-12 16:13:25 -0600406 /* xdr offset starts at RPC message */
Chuck Lever08ae4e72016-03-01 13:05:36 -0500407 nchunks = be32_to_cpu(rp_ary->wc_nchunks);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600408 for (xdr_off = 0, chunk_no = 0;
Tom Tuckercec56c82012-02-15 11:30:00 -0600409 xfer_len && chunk_no < nchunks;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600410 chunk_no++) {
411 u64 rs_offset;
Chuck Lever08ae4e72016-03-01 13:05:36 -0500412 ch = &rp_ary->wc_array[chunk_no].wc_target;
Chuck Lever70747c22015-06-04 11:20:39 -0400413 write_len = min(xfer_len, be32_to_cpu(ch->rs_length));
Tom Tuckerc06b5402007-12-12 16:13:25 -0600414
Tom Tuckerc06b5402007-12-12 16:13:25 -0600415 /* Prepare the reply chunk given the length actually
416 * written */
Tom Tuckercec56c82012-02-15 11:30:00 -0600417 xdr_decode_hyper((__be32 *)&ch->rs_offset, &rs_offset);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600418 svc_rdma_xdr_encode_array_chunk(res_ary, chunk_no,
Tom Tuckercec56c82012-02-15 11:30:00 -0600419 ch->rs_handle, ch->rs_offset,
420 write_len);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600421 chunk_off = 0;
422 while (write_len) {
Tom Tuckerc06b5402007-12-12 16:13:25 -0600423 ret = send_write(xprt, rqstp,
Chuck Lever70747c22015-06-04 11:20:39 -0400424 be32_to_cpu(ch->rs_handle),
Tom Tuckerc06b5402007-12-12 16:13:25 -0600425 rs_offset + chunk_off,
426 xdr_off,
Steve Wise25594292014-07-09 13:49:15 -0500427 write_len,
Tom Tucker34d16e42008-07-02 14:56:13 -0500428 vec);
Chuck Lever08ae4e72016-03-01 13:05:36 -0500429 if (ret <= 0)
430 goto out_err;
Steve Wise25594292014-07-09 13:49:15 -0500431 chunk_off += ret;
432 xdr_off += ret;
433 xfer_len -= ret;
434 write_len -= ret;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600435 }
436 }
437 /* Update the req with the number of chunks actually used */
438 svc_rdma_xdr_encode_reply_array(res_ary, chunk_no);
439
440 return rqstp->rq_res.len;
Chuck Lever08ae4e72016-03-01 13:05:36 -0500441
442out_err:
443 pr_err("svcrdma: failed to send reply chunks, rc=%d\n", ret);
444 return -EIO;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600445}
446
447/* This function prepares the portion of the RPCRDMA message to be
448 * sent in the RDMA_SEND. This function is called after data sent via
449 * RDMA has already been transmitted. There are three cases:
450 * - The RPCRDMA header, RPC header, and payload are all sent in a
451 * single RDMA_SEND. This is the "inline" case.
452 * - The RPCRDMA header and some portion of the RPC header and data
453 * are sent via this RDMA_SEND and another portion of the data is
454 * sent via RDMA.
455 * - The RPCRDMA header [NOMSG] is sent in this RDMA_SEND and the RPC
456 * header and data are all transmitted via RDMA.
457 * In all three cases, this function prepares the RPCRDMA header in
458 * sge[0], the 'type' parameter indicates the type to place in the
459 * RPCRDMA header, and the 'byte_count' field indicates how much of
Tom Tuckerb432e6b2010-10-12 15:33:52 -0500460 * the XDR to include in this RDMA_SEND. NB: The offset of the payload
461 * to send is zero in the XDR.
Tom Tuckerc06b5402007-12-12 16:13:25 -0600462 */
463static int send_reply(struct svcxprt_rdma *rdma,
464 struct svc_rqst *rqstp,
465 struct page *page,
466 struct rpcrdma_msg *rdma_resp,
467 struct svc_rdma_op_ctxt *ctxt,
Tom Tucker34d16e42008-07-02 14:56:13 -0500468 struct svc_rdma_req_map *vec,
Tom Tuckerc06b5402007-12-12 16:13:25 -0600469 int byte_count)
470{
471 struct ib_send_wr send_wr;
Chuck Lever9d11b512015-07-09 16:45:18 -0400472 u32 xdr_off;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600473 int sge_no;
474 int sge_bytes;
475 int page_no;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500476 int pages;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600477 int ret;
478
Tom Tucker0e7f0112008-04-23 16:49:54 -0500479 /* Post a recv buffer to handle another request. */
Chuck Lever39b09a12016-01-07 14:49:37 -0500480 ret = svc_rdma_post_recv(rdma, GFP_KERNEL);
Tom Tucker0e7f0112008-04-23 16:49:54 -0500481 if (ret) {
482 printk(KERN_INFO
483 "svcrdma: could not post a receive buffer, err=%d."
484 "Closing transport %p.\n", ret, rdma);
485 set_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags);
Tom Tucker5ac461a2008-04-25 18:08:59 -0500486 svc_rdma_put_context(ctxt, 0);
487 return -ENOTCONN;
Tom Tucker0e7f0112008-04-23 16:49:54 -0500488 }
489
Tom Tuckerc06b5402007-12-12 16:13:25 -0600490 /* Prepare the context */
491 ctxt->pages[0] = page;
492 ctxt->count = 1;
493
494 /* Prepare the SGE for the RPCRDMA Header */
Christoph Hellwig5fe10432016-01-07 23:53:41 -0800495 ctxt->sge[0].lkey = rdma->sc_pd->local_dma_lkey;
Steve Wise98779be2009-05-14 16:34:28 -0500496 ctxt->sge[0].length = svc_rdma_xdr_get_reply_hdr_len(rdma_resp);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600497 ctxt->sge[0].addr =
Tom Tuckerb432e6b2010-10-12 15:33:52 -0500498 ib_dma_map_page(rdma->sc_cm_id->device, page, 0,
499 ctxt->sge[0].length, DMA_TO_DEVICE);
Tom Tuckerafd566e2008-10-03 15:45:03 -0500500 if (ib_dma_mapping_error(rdma->sc_cm_id->device, ctxt->sge[0].addr))
501 goto err;
502 atomic_inc(&rdma->sc_dma_used);
503
Tom Tuckerc06b5402007-12-12 16:13:25 -0600504 ctxt->direction = DMA_TO_DEVICE;
Tom Tuckerafd566e2008-10-03 15:45:03 -0500505
Tom Tuckerb432e6b2010-10-12 15:33:52 -0500506 /* Map the payload indicated by 'byte_count' */
Chuck Lever9d11b512015-07-09 16:45:18 -0400507 xdr_off = 0;
Tom Tucker34d16e42008-07-02 14:56:13 -0500508 for (sge_no = 1; byte_count && sge_no < vec->count; sge_no++) {
509 sge_bytes = min_t(size_t, vec->sge[sge_no].iov_len, byte_count);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600510 byte_count -= sge_bytes;
Steve Wise0bf48282014-05-28 15:12:01 -0500511 ctxt->sge[sge_no].addr =
512 dma_map_xdr(rdma, &rqstp->rq_res, xdr_off,
513 sge_bytes, DMA_TO_DEVICE);
514 xdr_off += sge_bytes;
515 if (ib_dma_mapping_error(rdma->sc_cm_id->device,
516 ctxt->sge[sge_no].addr))
517 goto err;
518 atomic_inc(&rdma->sc_dma_used);
Christoph Hellwig5fe10432016-01-07 23:53:41 -0800519 ctxt->sge[sge_no].lkey = rdma->sc_pd->local_dma_lkey;
Tom Tucker34d16e42008-07-02 14:56:13 -0500520 ctxt->sge[sge_no].length = sge_bytes;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600521 }
Chuck Lever3fe04ee2015-01-13 11:03:03 -0500522 if (byte_count != 0) {
523 pr_err("svcrdma: Could not map %d bytes\n", byte_count);
524 goto err;
525 }
Tom Tuckerc06b5402007-12-12 16:13:25 -0600526
527 /* Save all respages in the ctxt and remove them from the
528 * respages array. They are our pages until the I/O
529 * completes.
530 */
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500531 pages = rqstp->rq_next_page - rqstp->rq_respages;
532 for (page_no = 0; page_no < pages; page_no++) {
Tom Tuckerc06b5402007-12-12 16:13:25 -0600533 ctxt->pages[page_no+1] = rqstp->rq_respages[page_no];
534 ctxt->count++;
535 rqstp->rq_respages[page_no] = NULL;
Tom Tuckerafd566e2008-10-03 15:45:03 -0500536 /*
537 * If there are more pages than SGE, terminate SGE
538 * list so that svc_rdma_unmap_dma doesn't attempt to
539 * unmap garbage.
540 */
Tom Tucker34d16e42008-07-02 14:56:13 -0500541 if (page_no+1 >= sge_no)
542 ctxt->sge[page_no+1].length = 0;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600543 }
Tom Tucker7e4359e2014-03-25 15:14:57 -0500544 rqstp->rq_next_page = rqstp->rq_respages + 1;
Steve Wise0bf48282014-05-28 15:12:01 -0500545
Chuck Lever9d11b512015-07-09 16:45:18 -0400546 /* The loop above bumps sc_dma_used for each sge. The
547 * xdr_buf.tail gets a separate sge, but resides in the
548 * same page as xdr_buf.head. Don't count it twice.
549 */
550 if (sge_no > ctxt->count)
551 atomic_dec(&rdma->sc_dma_used);
552
Chuck Lever3fe04ee2015-01-13 11:03:03 -0500553 if (sge_no > rdma->sc_max_sge) {
554 pr_err("svcrdma: Too many sges (%d)\n", sge_no);
555 goto err;
556 }
Tom Tuckerc06b5402007-12-12 16:13:25 -0600557 memset(&send_wr, 0, sizeof send_wr);
558 ctxt->wr_op = IB_WR_SEND;
559 send_wr.wr_id = (unsigned long)ctxt;
560 send_wr.sg_list = ctxt->sge;
561 send_wr.num_sge = sge_no;
562 send_wr.opcode = IB_WR_SEND;
563 send_wr.send_flags = IB_SEND_SIGNALED;
564
565 ret = svc_rdma_send(rdma, &send_wr);
566 if (ret)
Tom Tuckerafd566e2008-10-03 15:45:03 -0500567 goto err;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600568
Tom Tuckerafd566e2008-10-03 15:45:03 -0500569 return 0;
570
571 err:
Steve Wise21515e42009-04-29 14:14:00 -0500572 svc_rdma_unmap_dma(ctxt);
Tom Tuckerafd566e2008-10-03 15:45:03 -0500573 svc_rdma_put_context(ctxt, 1);
574 return -EIO;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600575}
576
577void svc_rdma_prep_reply_hdr(struct svc_rqst *rqstp)
578{
579}
580
Tom Tuckerc06b5402007-12-12 16:13:25 -0600581int svc_rdma_sendto(struct svc_rqst *rqstp)
582{
583 struct svc_xprt *xprt = rqstp->rq_xprt;
584 struct svcxprt_rdma *rdma =
585 container_of(xprt, struct svcxprt_rdma, sc_xprt);
586 struct rpcrdma_msg *rdma_argp;
587 struct rpcrdma_msg *rdma_resp;
Chuck Lever08ae4e72016-03-01 13:05:36 -0500588 struct rpcrdma_write_array *wr_ary, *rp_ary;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600589 enum rpcrdma_proc reply_type;
590 int ret;
591 int inline_bytes;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600592 struct page *res_page;
593 struct svc_rdma_op_ctxt *ctxt;
Tom Tucker34d16e42008-07-02 14:56:13 -0500594 struct svc_rdma_req_map *vec;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600595
596 dprintk("svcrdma: sending response for rqstp=%p\n", rqstp);
597
Chuck Levere5523bd2015-01-13 11:03:11 -0500598 /* Get the RDMA request header. The receive logic always
599 * places this at the start of page 0.
600 */
601 rdma_argp = page_address(rqstp->rq_pages[0]);
Chuck Lever08ae4e72016-03-01 13:05:36 -0500602 wr_ary = svc_rdma_get_write_array(rdma_argp);
603 rp_ary = svc_rdma_get_reply_array(rdma_argp, wr_ary);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600604
Tom Tucker34d16e42008-07-02 14:56:13 -0500605 /* Build an req vec for the XDR */
Tom Tuckerc06b5402007-12-12 16:13:25 -0600606 ctxt = svc_rdma_get_context(rdma);
607 ctxt->direction = DMA_TO_DEVICE;
Chuck Lever2fe81b22016-01-07 14:49:20 -0500608 vec = svc_rdma_get_req_map(rdma);
Chuck Leverf6763c22016-03-01 13:05:54 -0500609 ret = svc_rdma_map_xdr(rdma, &rqstp->rq_res, vec, wr_ary != NULL);
Tom Tuckerafd566e2008-10-03 15:45:03 -0500610 if (ret)
611 goto err0;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600612 inline_bytes = rqstp->rq_res.len;
613
614 /* Create the RDMA response header */
Chuck Lever78da2b32016-01-07 14:49:45 -0500615 ret = -ENOMEM;
616 res_page = alloc_page(GFP_KERNEL);
617 if (!res_page)
618 goto err0;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600619 rdma_resp = page_address(res_page);
Chuck Lever08ae4e72016-03-01 13:05:36 -0500620 if (rp_ary)
Tom Tuckerc06b5402007-12-12 16:13:25 -0600621 reply_type = RDMA_NOMSG;
622 else
623 reply_type = RDMA_MSG;
624 svc_rdma_xdr_encode_reply_header(rdma, rdma_argp,
625 rdma_resp, reply_type);
626
627 /* Send any write-chunk data and build resp write-list */
Chuck Lever08ae4e72016-03-01 13:05:36 -0500628 if (wr_ary) {
629 ret = send_write_chunks(rdma, wr_ary, rdma_resp, rqstp, vec);
630 if (ret < 0)
631 goto err1;
Chuck Levercf570a92016-03-01 13:05:45 -0500632 inline_bytes -= ret + xdr_padsize(ret);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600633 }
Tom Tuckerc06b5402007-12-12 16:13:25 -0600634
635 /* Send any reply-list data and update resp reply-list */
Chuck Lever08ae4e72016-03-01 13:05:36 -0500636 if (rp_ary) {
637 ret = send_reply_chunks(rdma, rp_ary, rdma_resp, rqstp, vec);
638 if (ret < 0)
639 goto err1;
640 inline_bytes -= ret;
Tom Tuckerc06b5402007-12-12 16:13:25 -0600641 }
Tom Tuckerc06b5402007-12-12 16:13:25 -0600642
Tom Tucker34d16e42008-07-02 14:56:13 -0500643 ret = send_reply(rdma, rqstp, res_page, rdma_resp, ctxt, vec,
Tom Tuckerc06b5402007-12-12 16:13:25 -0600644 inline_bytes);
Chuck Lever2fe81b22016-01-07 14:49:20 -0500645 svc_rdma_put_req_map(rdma, vec);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600646 dprintk("svcrdma: send_reply returns %d\n", ret);
647 return ret;
Tom Tuckerafd566e2008-10-03 15:45:03 -0500648
649 err1:
650 put_page(res_page);
651 err0:
Chuck Lever2fe81b22016-01-07 14:49:20 -0500652 svc_rdma_put_req_map(rdma, vec);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600653 svc_rdma_put_context(ctxt, 0);
Tom Tuckerc06b5402007-12-12 16:13:25 -0600654 return ret;
655}