1 /*
2  * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved.
3  * 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 
53 static int map_xdr(struct svcxprt_rdma *xprt,
54 		   struct xdr_buf *xdr,
55 		   struct svc_rdma_req_map *vec)
56 {
57 	int sge_no;
58 	u32 sge_bytes;
59 	u32 page_bytes;
60 	u32 page_off;
61 	int page_no;
62 
63 	BUG_ON(xdr->len !=
64 	       (xdr->head[0].iov_len + xdr->page_len + xdr->tail[0].iov_len));
65 
66 	/* Skip the first sge, this is for the RPCRDMA header */
67 	sge_no = 1;
68 
69 	/* Head SGE */
70 	vec->sge[sge_no].iov_base = xdr->head[0].iov_base;
71 	vec->sge[sge_no].iov_len = xdr->head[0].iov_len;
72 	sge_no++;
73 
74 	/* pages SGE */
75 	page_no = 0;
76 	page_bytes = xdr->page_len;
77 	page_off = xdr->page_base;
78 	while (page_bytes) {
79 		vec->sge[sge_no].iov_base =
80 			page_address(xdr->pages[page_no]) + page_off;
81 		sge_bytes = min_t(u32, page_bytes, (PAGE_SIZE - page_off));
82 		page_bytes -= sge_bytes;
83 		vec->sge[sge_no].iov_len = sge_bytes;
84 
85 		sge_no++;
86 		page_no++;
87 		page_off = 0; /* reset for next time through loop */
88 	}
89 
90 	/* Tail SGE */
91 	if (xdr->tail[0].iov_len) {
92 		vec->sge[sge_no].iov_base = xdr->tail[0].iov_base;
93 		vec->sge[sge_no].iov_len = xdr->tail[0].iov_len;
94 		sge_no++;
95 	}
96 
97 	dprintk("svcrdma: map_xdr: sge_no %d page_no %d "
98 		"page_base %u page_len %u head_len %zu tail_len %zu\n",
99 		sge_no, page_no, xdr->page_base, xdr->page_len,
100 		xdr->head[0].iov_len, xdr->tail[0].iov_len);
101 
102 	vec->count = sge_no;
103 	return 0;
104 }
105 
106 static dma_addr_t dma_map_xdr(struct svcxprt_rdma *xprt,
107 			      struct xdr_buf *xdr,
108 			      u32 xdr_off, size_t len, int dir)
109 {
110 	struct page *page;
111 	dma_addr_t dma_addr;
112 	if (xdr_off < xdr->head[0].iov_len) {
113 		/* This offset is in the head */
114 		xdr_off += (unsigned long)xdr->head[0].iov_base & ~PAGE_MASK;
115 		page = virt_to_page(xdr->head[0].iov_base);
116 	} else {
117 		xdr_off -= xdr->head[0].iov_len;
118 		if (xdr_off < xdr->page_len) {
119 			/* This offset is in the page list */
120 			xdr_off += xdr->page_base;
121 			page = xdr->pages[xdr_off >> PAGE_SHIFT];
122 			xdr_off &= ~PAGE_MASK;
123 		} else {
124 			/* This offset is in the tail */
125 			xdr_off -= xdr->page_len;
126 			xdr_off += (unsigned long)
127 				xdr->tail[0].iov_base & ~PAGE_MASK;
128 			page = virt_to_page(xdr->tail[0].iov_base);
129 		}
130 	}
131 	dma_addr = ib_dma_map_page(xprt->sc_cm_id->device, page, xdr_off,
132 				   min_t(size_t, PAGE_SIZE, len), dir);
133 	return dma_addr;
134 }
135 
136 /* Assumptions:
137  * - The specified write_len can be represented in sc_max_sge * PAGE_SIZE
138  */
139 static int send_write(struct svcxprt_rdma *xprt, struct svc_rqst *rqstp,
140 		      u32 rmr, u64 to,
141 		      u32 xdr_off, int write_len,
142 		      struct svc_rdma_req_map *vec)
143 {
144 	struct ib_send_wr write_wr;
145 	struct ib_sge *sge;
146 	int xdr_sge_no;
147 	int sge_no;
148 	int sge_bytes;
149 	int sge_off;
150 	int bc;
151 	struct svc_rdma_op_ctxt *ctxt;
152 
153 	BUG_ON(vec->count > RPCSVC_MAXPAGES);
154 	dprintk("svcrdma: RDMA_WRITE rmr=%x, to=%llx, xdr_off=%d, "
155 		"write_len=%d, vec->sge=%p, vec->count=%lu\n",
156 		rmr, (unsigned long long)to, xdr_off,
157 		write_len, vec->sge, vec->count);
158 
159 	ctxt = svc_rdma_get_context(xprt);
160 	ctxt->direction = DMA_TO_DEVICE;
161 	sge = ctxt->sge;
162 
163 	/* Find the SGE associated with xdr_off */
164 	for (bc = xdr_off, xdr_sge_no = 1; bc && xdr_sge_no < vec->count;
165 	     xdr_sge_no++) {
166 		if (vec->sge[xdr_sge_no].iov_len > bc)
167 			break;
168 		bc -= vec->sge[xdr_sge_no].iov_len;
169 	}
170 
171 	sge_off = bc;
172 	bc = write_len;
173 	sge_no = 0;
174 
175 	/* Copy the remaining SGE */
176 	while (bc != 0) {
177 		sge_bytes = min_t(size_t,
178 			  bc, vec->sge[xdr_sge_no].iov_len-sge_off);
179 		sge[sge_no].length = sge_bytes;
180 		sge[sge_no].addr =
181 			dma_map_xdr(xprt, &rqstp->rq_res, xdr_off,
182 				    sge_bytes, DMA_TO_DEVICE);
183 		xdr_off += sge_bytes;
184 		if (ib_dma_mapping_error(xprt->sc_cm_id->device,
185 					 sge[sge_no].addr))
186 			goto err;
187 		atomic_inc(&xprt->sc_dma_used);
188 		sge[sge_no].lkey = xprt->sc_dma_lkey;
189 		ctxt->count++;
190 		sge_off = 0;
191 		sge_no++;
192 		xdr_sge_no++;
193 		BUG_ON(xdr_sge_no > vec->count);
194 		bc -= sge_bytes;
195 	}
196 
197 	/* Prepare WRITE WR */
198 	memset(&write_wr, 0, sizeof write_wr);
199 	ctxt->wr_op = IB_WR_RDMA_WRITE;
200 	write_wr.wr_id = (unsigned long)ctxt;
201 	write_wr.sg_list = &sge[0];
202 	write_wr.num_sge = sge_no;
203 	write_wr.opcode = IB_WR_RDMA_WRITE;
204 	write_wr.send_flags = IB_SEND_SIGNALED;
205 	write_wr.wr.rdma.rkey = rmr;
206 	write_wr.wr.rdma.remote_addr = to;
207 
208 	/* Post It */
209 	atomic_inc(&rdma_stat_write);
210 	if (svc_rdma_send(xprt, &write_wr))
211 		goto err;
212 	return 0;
213  err:
214 	svc_rdma_unmap_dma(ctxt);
215 	svc_rdma_put_context(ctxt, 0);
216 	/* Fatal error, close transport */
217 	return -EIO;
218 }
219 
220 static int send_write_chunks(struct svcxprt_rdma *xprt,
221 			     struct rpcrdma_msg *rdma_argp,
222 			     struct rpcrdma_msg *rdma_resp,
223 			     struct svc_rqst *rqstp,
224 			     struct svc_rdma_req_map *vec)
225 {
226 	u32 xfer_len = rqstp->rq_res.page_len + rqstp->rq_res.tail[0].iov_len;
227 	int write_len;
228 	int max_write;
229 	u32 xdr_off;
230 	int chunk_off;
231 	int chunk_no;
232 	struct rpcrdma_write_array *arg_ary;
233 	struct rpcrdma_write_array *res_ary;
234 	int ret;
235 
236 	arg_ary = svc_rdma_get_write_array(rdma_argp);
237 	if (!arg_ary)
238 		return 0;
239 	res_ary = (struct rpcrdma_write_array *)
240 		&rdma_resp->rm_body.rm_chunks[1];
241 
242 	max_write = xprt->sc_max_sge * PAGE_SIZE;
243 
244 	/* Write chunks start at the pagelist */
245 	for (xdr_off = rqstp->rq_res.head[0].iov_len, chunk_no = 0;
246 	     xfer_len && chunk_no < arg_ary->wc_nchunks;
247 	     chunk_no++) {
248 		struct rpcrdma_segment *arg_ch;
249 		u64 rs_offset;
250 
251 		arg_ch = &arg_ary->wc_array[chunk_no].wc_target;
252 		write_len = min(xfer_len, ntohl(arg_ch->rs_length));
253 
254 		/* Prepare the response chunk given the length actually
255 		 * written */
256 		xdr_decode_hyper((__be32 *)&arg_ch->rs_offset, &rs_offset);
257 		svc_rdma_xdr_encode_array_chunk(res_ary, chunk_no,
258 						arg_ch->rs_handle,
259 						arg_ch->rs_offset,
260 						write_len);
261 		chunk_off = 0;
262 		while (write_len) {
263 			int this_write;
264 			this_write = min(write_len, max_write);
265 			ret = send_write(xprt, rqstp,
266 					 ntohl(arg_ch->rs_handle),
267 					 rs_offset + chunk_off,
268 					 xdr_off,
269 					 this_write,
270 					 vec);
271 			if (ret) {
272 				dprintk("svcrdma: RDMA_WRITE failed, ret=%d\n",
273 					ret);
274 				return -EIO;
275 			}
276 			chunk_off += this_write;
277 			xdr_off += this_write;
278 			xfer_len -= this_write;
279 			write_len -= this_write;
280 		}
281 	}
282 	/* Update the req with the number of chunks actually used */
283 	svc_rdma_xdr_encode_write_list(rdma_resp, chunk_no);
284 
285 	return rqstp->rq_res.page_len + rqstp->rq_res.tail[0].iov_len;
286 }
287 
288 static int send_reply_chunks(struct svcxprt_rdma *xprt,
289 			     struct rpcrdma_msg *rdma_argp,
290 			     struct rpcrdma_msg *rdma_resp,
291 			     struct svc_rqst *rqstp,
292 			     struct svc_rdma_req_map *vec)
293 {
294 	u32 xfer_len = rqstp->rq_res.len;
295 	int write_len;
296 	int max_write;
297 	u32 xdr_off;
298 	int chunk_no;
299 	int chunk_off;
300 	int nchunks;
301 	struct rpcrdma_segment *ch;
302 	struct rpcrdma_write_array *arg_ary;
303 	struct rpcrdma_write_array *res_ary;
304 	int ret;
305 
306 	arg_ary = svc_rdma_get_reply_array(rdma_argp);
307 	if (!arg_ary)
308 		return 0;
309 	/* XXX: need to fix when reply lists occur with read-list and or
310 	 * write-list */
311 	res_ary = (struct rpcrdma_write_array *)
312 		&rdma_resp->rm_body.rm_chunks[2];
313 
314 	max_write = xprt->sc_max_sge * PAGE_SIZE;
315 
316 	/* xdr offset starts at RPC message */
317 	nchunks = ntohl(arg_ary->wc_nchunks);
318 	for (xdr_off = 0, chunk_no = 0;
319 	     xfer_len && chunk_no < nchunks;
320 	     chunk_no++) {
321 		u64 rs_offset;
322 		ch = &arg_ary->wc_array[chunk_no].wc_target;
323 		write_len = min(xfer_len, htonl(ch->rs_length));
324 
325 		/* Prepare the reply chunk given the length actually
326 		 * written */
327 		xdr_decode_hyper((__be32 *)&ch->rs_offset, &rs_offset);
328 		svc_rdma_xdr_encode_array_chunk(res_ary, chunk_no,
329 						ch->rs_handle, ch->rs_offset,
330 						write_len);
331 		chunk_off = 0;
332 		while (write_len) {
333 			int this_write;
334 
335 			this_write = min(write_len, max_write);
336 			ret = send_write(xprt, rqstp,
337 					 ntohl(ch->rs_handle),
338 					 rs_offset + chunk_off,
339 					 xdr_off,
340 					 this_write,
341 					 vec);
342 			if (ret) {
343 				dprintk("svcrdma: RDMA_WRITE failed, ret=%d\n",
344 					ret);
345 				return -EIO;
346 			}
347 			chunk_off += this_write;
348 			xdr_off += this_write;
349 			xfer_len -= this_write;
350 			write_len -= this_write;
351 		}
352 	}
353 	/* Update the req with the number of chunks actually used */
354 	svc_rdma_xdr_encode_reply_array(res_ary, chunk_no);
355 
356 	return rqstp->rq_res.len;
357 }
358 
359 /* This function prepares the portion of the RPCRDMA message to be
360  * sent in the RDMA_SEND. This function is called after data sent via
361  * RDMA has already been transmitted. There are three cases:
362  * - The RPCRDMA header, RPC header, and payload are all sent in a
363  *   single RDMA_SEND. This is the "inline" case.
364  * - The RPCRDMA header and some portion of the RPC header and data
365  *   are sent via this RDMA_SEND and another portion of the data is
366  *   sent via RDMA.
367  * - The RPCRDMA header [NOMSG] is sent in this RDMA_SEND and the RPC
368  *   header and data are all transmitted via RDMA.
369  * In all three cases, this function prepares the RPCRDMA header in
370  * sge[0], the 'type' parameter indicates the type to place in the
371  * RPCRDMA header, and the 'byte_count' field indicates how much of
372  * the XDR to include in this RDMA_SEND. NB: The offset of the payload
373  * to send is zero in the XDR.
374  */
375 static int send_reply(struct svcxprt_rdma *rdma,
376 		      struct svc_rqst *rqstp,
377 		      struct page *page,
378 		      struct rpcrdma_msg *rdma_resp,
379 		      struct svc_rdma_op_ctxt *ctxt,
380 		      struct svc_rdma_req_map *vec,
381 		      int byte_count)
382 {
383 	struct ib_send_wr send_wr;
384 	int sge_no;
385 	int sge_bytes;
386 	int page_no;
387 	int pages;
388 	int ret;
389 
390 	/* Post a recv buffer to handle another request. */
391 	ret = svc_rdma_post_recv(rdma);
392 	if (ret) {
393 		printk(KERN_INFO
394 		       "svcrdma: could not post a receive buffer, err=%d."
395 		       "Closing transport %p.\n", ret, rdma);
396 		set_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags);
397 		svc_rdma_put_context(ctxt, 0);
398 		return -ENOTCONN;
399 	}
400 
401 	/* Prepare the context */
402 	ctxt->pages[0] = page;
403 	ctxt->count = 1;
404 
405 	/* Prepare the SGE for the RPCRDMA Header */
406 	ctxt->sge[0].lkey = rdma->sc_dma_lkey;
407 	ctxt->sge[0].length = svc_rdma_xdr_get_reply_hdr_len(rdma_resp);
408 	ctxt->sge[0].addr =
409 	    ib_dma_map_page(rdma->sc_cm_id->device, page, 0,
410 			    ctxt->sge[0].length, DMA_TO_DEVICE);
411 	if (ib_dma_mapping_error(rdma->sc_cm_id->device, ctxt->sge[0].addr))
412 		goto err;
413 	atomic_inc(&rdma->sc_dma_used);
414 
415 	ctxt->direction = DMA_TO_DEVICE;
416 
417 	/* Map the payload indicated by 'byte_count' */
418 	for (sge_no = 1; byte_count && sge_no < vec->count; sge_no++) {
419 		int xdr_off = 0;
420 		sge_bytes = min_t(size_t, vec->sge[sge_no].iov_len, byte_count);
421 		byte_count -= sge_bytes;
422 		ctxt->sge[sge_no].addr =
423 			dma_map_xdr(rdma, &rqstp->rq_res, xdr_off,
424 				    sge_bytes, DMA_TO_DEVICE);
425 		xdr_off += sge_bytes;
426 		if (ib_dma_mapping_error(rdma->sc_cm_id->device,
427 					 ctxt->sge[sge_no].addr))
428 			goto err;
429 		atomic_inc(&rdma->sc_dma_used);
430 		ctxt->sge[sge_no].lkey = rdma->sc_dma_lkey;
431 		ctxt->sge[sge_no].length = sge_bytes;
432 	}
433 	BUG_ON(byte_count != 0);
434 
435 	/* Save all respages in the ctxt and remove them from the
436 	 * respages array. They are our pages until the I/O
437 	 * completes.
438 	 */
439 	pages = rqstp->rq_next_page - rqstp->rq_respages;
440 	for (page_no = 0; page_no < pages; page_no++) {
441 		ctxt->pages[page_no+1] = rqstp->rq_respages[page_no];
442 		ctxt->count++;
443 		rqstp->rq_respages[page_no] = NULL;
444 		/*
445 		 * If there are more pages than SGE, terminate SGE
446 		 * list so that svc_rdma_unmap_dma doesn't attempt to
447 		 * unmap garbage.
448 		 */
449 		if (page_no+1 >= sge_no)
450 			ctxt->sge[page_no+1].length = 0;
451 	}
452 	rqstp->rq_next_page = rqstp->rq_respages + 1;
453 
454 	BUG_ON(sge_no > rdma->sc_max_sge);
455 	memset(&send_wr, 0, sizeof send_wr);
456 	ctxt->wr_op = IB_WR_SEND;
457 	send_wr.wr_id = (unsigned long)ctxt;
458 	send_wr.sg_list = ctxt->sge;
459 	send_wr.num_sge = sge_no;
460 	send_wr.opcode = IB_WR_SEND;
461 	send_wr.send_flags =  IB_SEND_SIGNALED;
462 
463 	ret = svc_rdma_send(rdma, &send_wr);
464 	if (ret)
465 		goto err;
466 
467 	return 0;
468 
469  err:
470 	svc_rdma_unmap_dma(ctxt);
471 	svc_rdma_put_context(ctxt, 1);
472 	return -EIO;
473 }
474 
475 void svc_rdma_prep_reply_hdr(struct svc_rqst *rqstp)
476 {
477 }
478 
479 /*
480  * Return the start of an xdr buffer.
481  */
482 static void *xdr_start(struct xdr_buf *xdr)
483 {
484 	return xdr->head[0].iov_base -
485 		(xdr->len -
486 		 xdr->page_len -
487 		 xdr->tail[0].iov_len -
488 		 xdr->head[0].iov_len);
489 }
490 
491 int svc_rdma_sendto(struct svc_rqst *rqstp)
492 {
493 	struct svc_xprt *xprt = rqstp->rq_xprt;
494 	struct svcxprt_rdma *rdma =
495 		container_of(xprt, struct svcxprt_rdma, sc_xprt);
496 	struct rpcrdma_msg *rdma_argp;
497 	struct rpcrdma_msg *rdma_resp;
498 	struct rpcrdma_write_array *reply_ary;
499 	enum rpcrdma_proc reply_type;
500 	int ret;
501 	int inline_bytes;
502 	struct page *res_page;
503 	struct svc_rdma_op_ctxt *ctxt;
504 	struct svc_rdma_req_map *vec;
505 
506 	dprintk("svcrdma: sending response for rqstp=%p\n", rqstp);
507 
508 	/* Get the RDMA request header. */
509 	rdma_argp = xdr_start(&rqstp->rq_arg);
510 
511 	/* Build an req vec for the XDR */
512 	ctxt = svc_rdma_get_context(rdma);
513 	ctxt->direction = DMA_TO_DEVICE;
514 	vec = svc_rdma_get_req_map();
515 	ret = map_xdr(rdma, &rqstp->rq_res, vec);
516 	if (ret)
517 		goto err0;
518 	inline_bytes = rqstp->rq_res.len;
519 
520 	/* Create the RDMA response header */
521 	res_page = svc_rdma_get_page();
522 	rdma_resp = page_address(res_page);
523 	reply_ary = svc_rdma_get_reply_array(rdma_argp);
524 	if (reply_ary)
525 		reply_type = RDMA_NOMSG;
526 	else
527 		reply_type = RDMA_MSG;
528 	svc_rdma_xdr_encode_reply_header(rdma, rdma_argp,
529 					 rdma_resp, reply_type);
530 
531 	/* Send any write-chunk data and build resp write-list */
532 	ret = send_write_chunks(rdma, rdma_argp, rdma_resp,
533 				rqstp, vec);
534 	if (ret < 0) {
535 		printk(KERN_ERR "svcrdma: failed to send write chunks, rc=%d\n",
536 		       ret);
537 		goto err1;
538 	}
539 	inline_bytes -= ret;
540 
541 	/* Send any reply-list data and update resp reply-list */
542 	ret = send_reply_chunks(rdma, rdma_argp, rdma_resp,
543 				rqstp, vec);
544 	if (ret < 0) {
545 		printk(KERN_ERR "svcrdma: failed to send reply chunks, rc=%d\n",
546 		       ret);
547 		goto err1;
548 	}
549 	inline_bytes -= ret;
550 
551 	ret = send_reply(rdma, rqstp, res_page, rdma_resp, ctxt, vec,
552 			 inline_bytes);
553 	svc_rdma_put_req_map(vec);
554 	dprintk("svcrdma: send_reply returns %d\n", ret);
555 	return ret;
556 
557  err1:
558 	put_page(res_page);
559  err0:
560 	svc_rdma_put_req_map(vec);
561 	svc_rdma_put_context(ctxt, 0);
562 	return ret;
563 }
564