1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright (c) 2016-2018 Oracle. All rights reserved.
4 * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved.
5 * Copyright (c) 2005-2006 Network Appliance, Inc. All rights reserved.
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the BSD-type
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
17 * Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
20 * Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials provided
23 * with the distribution.
25 * Neither the name of the Network Appliance, Inc. nor the names of
26 * its contributors may be used to endorse or promote products
27 * derived from this software without specific prior written
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 * Author: Tom Tucker <tom@opengridcomputing.com>
47 * The main entry point is svc_rdma_sendto. This is called by the
48 * RPC server when an RPC Reply is ready to be transmitted to a client.
50 * The passed-in svc_rqst contains a struct xdr_buf which holds an
51 * XDR-encoded RPC Reply message. sendto must construct the RPC-over-RDMA
52 * transport header, post all Write WRs needed for this Reply, then post
53 * a Send WR conveying the transport header and the RPC message itself to
56 * svc_rdma_sendto must fully transmit the Reply before returning, as
57 * the svc_rqst will be recycled as soon as sendto returns. Remaining
58 * resources referred to by the svc_rqst are also recycled at that time.
59 * Therefore any resources that must remain longer must be detached
60 * from the svc_rqst and released later.
64 * The I/O that performs Reply transmission is asynchronous, and may
65 * complete well after sendto returns. Thus pages under I/O must be
66 * removed from the svc_rqst before sendto returns.
68 * The logic here depends on Send Queue and completion ordering. Since
69 * the Send WR is always posted last, it will always complete last. Thus
70 * when it completes, it is guaranteed that all previous Write WRs have
73 * Write WRs are constructed and posted. Each Write segment gets its own
74 * svc_rdma_rw_ctxt, allowing the Write completion handler to find and
75 * DMA-unmap the pages under I/O for that Write segment. The Write
76 * completion handler does not release any pages.
78 * When the Send WR is constructed, it also gets its own svc_rdma_send_ctxt.
79 * The ownership of all of the Reply's pages are transferred into that
80 * ctxt, the Send WR is posted, and sendto returns.
82 * The svc_rdma_send_ctxt is presented when the Send WR completes. The
83 * Send completion handler finally releases the Reply's pages.
85 * This mechanism also assumes that completions on the transport's Send
86 * Completion Queue do not run in parallel. Otherwise a Write completion
87 * and Send completion running at the same time could release pages that
88 * are still DMA-mapped.
92 * - If the Send WR is posted successfully, it will either complete
93 * successfully, or get flushed. Either way, the Send completion
94 * handler releases the Reply's pages.
95 * - If the Send WR cannot be not posted, the forward path releases
98 * This handles the case, without the use of page reference counting,
99 * where two different Write segments send portions of the same page.
102 #include <linux/spinlock.h>
103 #include <asm/unaligned.h>
105 #include <rdma/ib_verbs.h>
106 #include <rdma/rdma_cm.h>
108 #include <linux/sunrpc/debug.h>
109 #include <linux/sunrpc/svc_rdma.h>
111 #include "xprt_rdma.h"
112 #include <trace/events/rpcrdma.h>
114 static void svc_rdma_wc_send(struct ib_cq *cq, struct ib_wc *wc);
116 static void svc_rdma_send_cid_init(struct svcxprt_rdma *rdma,
117 struct rpc_rdma_cid *cid)
119 cid->ci_queue_id = rdma->sc_sq_cq->res.id;
120 cid->ci_completion_id = atomic_inc_return(&rdma->sc_completion_ids);
123 static struct svc_rdma_send_ctxt *
124 svc_rdma_send_ctxt_alloc(struct svcxprt_rdma *rdma)
126 struct svc_rdma_send_ctxt *ctxt;
132 size = sizeof(*ctxt);
133 size += rdma->sc_max_send_sges * sizeof(struct ib_sge);
134 ctxt = kmalloc(size, GFP_KERNEL);
137 buffer = kmalloc(rdma->sc_max_req_size, GFP_KERNEL);
140 addr = ib_dma_map_single(rdma->sc_pd->device, buffer,
141 rdma->sc_max_req_size, DMA_TO_DEVICE);
142 if (ib_dma_mapping_error(rdma->sc_pd->device, addr))
145 svc_rdma_send_cid_init(rdma, &ctxt->sc_cid);
147 ctxt->sc_send_wr.next = NULL;
148 ctxt->sc_send_wr.wr_cqe = &ctxt->sc_cqe;
149 ctxt->sc_send_wr.sg_list = ctxt->sc_sges;
150 ctxt->sc_send_wr.send_flags = IB_SEND_SIGNALED;
151 init_completion(&ctxt->sc_done);
152 ctxt->sc_cqe.done = svc_rdma_wc_send;
153 ctxt->sc_xprt_buf = buffer;
154 xdr_buf_init(&ctxt->sc_hdrbuf, ctxt->sc_xprt_buf,
155 rdma->sc_max_req_size);
156 ctxt->sc_sges[0].addr = addr;
158 for (i = 0; i < rdma->sc_max_send_sges; i++)
159 ctxt->sc_sges[i].lkey = rdma->sc_pd->local_dma_lkey;
171 * svc_rdma_send_ctxts_destroy - Release all send_ctxt's for an xprt
172 * @rdma: svcxprt_rdma being torn down
175 void svc_rdma_send_ctxts_destroy(struct svcxprt_rdma *rdma)
177 struct svc_rdma_send_ctxt *ctxt;
178 struct llist_node *node;
180 while ((node = llist_del_first(&rdma->sc_send_ctxts)) != NULL) {
181 ctxt = llist_entry(node, struct svc_rdma_send_ctxt, sc_node);
182 ib_dma_unmap_single(rdma->sc_pd->device,
183 ctxt->sc_sges[0].addr,
184 rdma->sc_max_req_size,
186 kfree(ctxt->sc_xprt_buf);
192 * svc_rdma_send_ctxt_get - Get a free send_ctxt
193 * @rdma: controlling svcxprt_rdma
195 * Returns a ready-to-use send_ctxt, or NULL if none are
196 * available and a fresh one cannot be allocated.
198 struct svc_rdma_send_ctxt *svc_rdma_send_ctxt_get(struct svcxprt_rdma *rdma)
200 struct svc_rdma_send_ctxt *ctxt;
201 struct llist_node *node;
203 spin_lock(&rdma->sc_send_lock);
204 node = llist_del_first(&rdma->sc_send_ctxts);
207 ctxt = llist_entry(node, struct svc_rdma_send_ctxt, sc_node);
208 spin_unlock(&rdma->sc_send_lock);
211 rpcrdma_set_xdrlen(&ctxt->sc_hdrbuf, 0);
212 xdr_init_encode(&ctxt->sc_stream, &ctxt->sc_hdrbuf,
213 ctxt->sc_xprt_buf, NULL);
215 ctxt->sc_send_wr.num_sge = 0;
216 ctxt->sc_cur_sge_no = 0;
220 spin_unlock(&rdma->sc_send_lock);
221 ctxt = svc_rdma_send_ctxt_alloc(rdma);
228 * svc_rdma_send_ctxt_put - Return send_ctxt to free list
229 * @rdma: controlling svcxprt_rdma
230 * @ctxt: object to return to the free list
232 void svc_rdma_send_ctxt_put(struct svcxprt_rdma *rdma,
233 struct svc_rdma_send_ctxt *ctxt)
235 struct ib_device *device = rdma->sc_cm_id->device;
238 /* The first SGE contains the transport header, which
239 * remains mapped until @ctxt is destroyed.
241 for (i = 1; i < ctxt->sc_send_wr.num_sge; i++) {
242 ib_dma_unmap_page(device,
243 ctxt->sc_sges[i].addr,
244 ctxt->sc_sges[i].length,
246 trace_svcrdma_dma_unmap_page(rdma,
247 ctxt->sc_sges[i].addr,
248 ctxt->sc_sges[i].length);
251 llist_add(&ctxt->sc_node, &rdma->sc_send_ctxts);
255 * svc_rdma_wake_send_waiters - manage Send Queue accounting
256 * @rdma: controlling transport
257 * @avail: Number of additional SQEs that are now available
260 void svc_rdma_wake_send_waiters(struct svcxprt_rdma *rdma, int avail)
262 atomic_add(avail, &rdma->sc_sq_avail);
263 smp_mb__after_atomic();
264 if (unlikely(waitqueue_active(&rdma->sc_send_wait)))
265 wake_up(&rdma->sc_send_wait);
269 * svc_rdma_wc_send - Invoked by RDMA provider for each polled Send WC
270 * @cq: Completion Queue context
271 * @wc: Work Completion object
273 * NB: The svc_xprt/svcxprt_rdma is pinned whenever it's possible that
274 * the Send completion handler could be running.
276 static void svc_rdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
278 struct svcxprt_rdma *rdma = cq->cq_context;
279 struct ib_cqe *cqe = wc->wr_cqe;
280 struct svc_rdma_send_ctxt *ctxt =
281 container_of(cqe, struct svc_rdma_send_ctxt, sc_cqe);
283 trace_svcrdma_wc_send(wc, &ctxt->sc_cid);
285 svc_rdma_wake_send_waiters(rdma, 1);
286 complete(&ctxt->sc_done);
288 if (unlikely(wc->status != IB_WC_SUCCESS))
289 svc_xprt_deferred_close(&rdma->sc_xprt);
293 * svc_rdma_send - Post a single Send WR
294 * @rdma: transport on which to post the WR
295 * @ctxt: send ctxt with a Send WR ready to post
297 * Returns zero if the Send WR was posted successfully. Otherwise, a
298 * negative errno is returned.
300 int svc_rdma_send(struct svcxprt_rdma *rdma, struct svc_rdma_send_ctxt *ctxt)
302 struct ib_send_wr *wr = &ctxt->sc_send_wr;
305 reinit_completion(&ctxt->sc_done);
307 /* Sync the transport header buffer */
308 ib_dma_sync_single_for_device(rdma->sc_pd->device,
310 wr->sg_list[0].length,
313 /* If the SQ is full, wait until an SQ entry is available */
315 if ((atomic_dec_return(&rdma->sc_sq_avail) < 0)) {
316 percpu_counter_inc(&svcrdma_stat_sq_starve);
317 trace_svcrdma_sq_full(rdma);
318 atomic_inc(&rdma->sc_sq_avail);
319 wait_event(rdma->sc_send_wait,
320 atomic_read(&rdma->sc_sq_avail) > 1);
321 if (test_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags))
323 trace_svcrdma_sq_retry(rdma);
327 trace_svcrdma_post_send(ctxt);
328 ret = ib_post_send(rdma->sc_qp, wr, NULL);
334 trace_svcrdma_sq_post_err(rdma, ret);
335 svc_xprt_deferred_close(&rdma->sc_xprt);
336 wake_up(&rdma->sc_send_wait);
341 * svc_rdma_encode_read_list - Encode RPC Reply's Read chunk list
342 * @sctxt: Send context for the RPC Reply
345 * On success, returns length in bytes of the Reply XDR buffer
346 * that was consumed by the Reply Read list
347 * %-EMSGSIZE on XDR buffer overflow
349 static ssize_t svc_rdma_encode_read_list(struct svc_rdma_send_ctxt *sctxt)
351 /* RPC-over-RDMA version 1 replies never have a Read list. */
352 return xdr_stream_encode_item_absent(&sctxt->sc_stream);
356 * svc_rdma_encode_write_segment - Encode one Write segment
357 * @sctxt: Send context for the RPC Reply
358 * @chunk: Write chunk to push
359 * @remaining: remaining bytes of the payload left in the Write chunk
360 * @segno: which segment in the chunk
363 * On success, returns length in bytes of the Reply XDR buffer
364 * that was consumed by the Write segment, and updates @remaining
365 * %-EMSGSIZE on XDR buffer overflow
367 static ssize_t svc_rdma_encode_write_segment(struct svc_rdma_send_ctxt *sctxt,
368 const struct svc_rdma_chunk *chunk,
369 u32 *remaining, unsigned int segno)
371 const struct svc_rdma_segment *segment = &chunk->ch_segments[segno];
372 const size_t len = rpcrdma_segment_maxsz * sizeof(__be32);
376 p = xdr_reserve_space(&sctxt->sc_stream, len);
380 length = min_t(u32, *remaining, segment->rs_length);
381 *remaining -= length;
382 xdr_encode_rdma_segment(p, segment->rs_handle, length,
384 trace_svcrdma_encode_wseg(sctxt, segno, segment->rs_handle, length,
390 * svc_rdma_encode_write_chunk - Encode one Write chunk
391 * @sctxt: Send context for the RPC Reply
392 * @chunk: Write chunk to push
394 * Copy a Write chunk from the Call transport header to the
395 * Reply transport header. Update each segment's length field
396 * to reflect the number of bytes written in that segment.
399 * On success, returns length in bytes of the Reply XDR buffer
400 * that was consumed by the Write chunk
401 * %-EMSGSIZE on XDR buffer overflow
403 static ssize_t svc_rdma_encode_write_chunk(struct svc_rdma_send_ctxt *sctxt,
404 const struct svc_rdma_chunk *chunk)
406 u32 remaining = chunk->ch_payload_length;
411 ret = xdr_stream_encode_item_present(&sctxt->sc_stream);
416 ret = xdr_stream_encode_u32(&sctxt->sc_stream, chunk->ch_segcount);
421 for (segno = 0; segno < chunk->ch_segcount; segno++) {
422 ret = svc_rdma_encode_write_segment(sctxt, chunk, &remaining, segno);
432 * svc_rdma_encode_write_list - Encode RPC Reply's Write chunk list
433 * @rctxt: Reply context with information about the RPC Call
434 * @sctxt: Send context for the RPC Reply
437 * On success, returns length in bytes of the Reply XDR buffer
438 * that was consumed by the Reply's Write list
439 * %-EMSGSIZE on XDR buffer overflow
441 static ssize_t svc_rdma_encode_write_list(struct svc_rdma_recv_ctxt *rctxt,
442 struct svc_rdma_send_ctxt *sctxt)
444 struct svc_rdma_chunk *chunk;
448 pcl_for_each_chunk(chunk, &rctxt->rc_write_pcl) {
449 ret = svc_rdma_encode_write_chunk(sctxt, chunk);
455 /* Terminate the Write list */
456 ret = xdr_stream_encode_item_absent(&sctxt->sc_stream);
464 * svc_rdma_encode_reply_chunk - Encode RPC Reply's Reply chunk
465 * @rctxt: Reply context with information about the RPC Call
466 * @sctxt: Send context for the RPC Reply
467 * @length: size in bytes of the payload in the Reply chunk
470 * On success, returns length in bytes of the Reply XDR buffer
471 * that was consumed by the Reply's Reply chunk
472 * %-EMSGSIZE on XDR buffer overflow
473 * %-E2BIG if the RPC message is larger than the Reply chunk
476 svc_rdma_encode_reply_chunk(struct svc_rdma_recv_ctxt *rctxt,
477 struct svc_rdma_send_ctxt *sctxt,
480 struct svc_rdma_chunk *chunk;
482 if (pcl_is_empty(&rctxt->rc_reply_pcl))
483 return xdr_stream_encode_item_absent(&sctxt->sc_stream);
485 chunk = pcl_first_chunk(&rctxt->rc_reply_pcl);
486 if (length > chunk->ch_length)
489 chunk->ch_payload_length = length;
490 return svc_rdma_encode_write_chunk(sctxt, chunk);
493 struct svc_rdma_map_data {
494 struct svcxprt_rdma *md_rdma;
495 struct svc_rdma_send_ctxt *md_ctxt;
499 * svc_rdma_page_dma_map - DMA map one page
500 * @data: pointer to arguments
501 * @page: struct page to DMA map
502 * @offset: offset into the page
503 * @len: number of bytes to map
506 * %0 if DMA mapping was successful
507 * %-EIO if the page cannot be DMA mapped
509 static int svc_rdma_page_dma_map(void *data, struct page *page,
510 unsigned long offset, unsigned int len)
512 struct svc_rdma_map_data *args = data;
513 struct svcxprt_rdma *rdma = args->md_rdma;
514 struct svc_rdma_send_ctxt *ctxt = args->md_ctxt;
515 struct ib_device *dev = rdma->sc_cm_id->device;
518 ++ctxt->sc_cur_sge_no;
520 dma_addr = ib_dma_map_page(dev, page, offset, len, DMA_TO_DEVICE);
521 if (ib_dma_mapping_error(dev, dma_addr))
524 trace_svcrdma_dma_map_page(rdma, dma_addr, len);
525 ctxt->sc_sges[ctxt->sc_cur_sge_no].addr = dma_addr;
526 ctxt->sc_sges[ctxt->sc_cur_sge_no].length = len;
527 ctxt->sc_send_wr.num_sge++;
531 trace_svcrdma_dma_map_err(rdma, dma_addr, len);
536 * svc_rdma_iov_dma_map - DMA map an iovec
537 * @data: pointer to arguments
538 * @iov: kvec to DMA map
540 * ib_dma_map_page() is used here because svc_rdma_dma_unmap()
541 * handles DMA-unmap and it uses ib_dma_unmap_page() exclusively.
544 * %0 if DMA mapping was successful
545 * %-EIO if the iovec cannot be DMA mapped
547 static int svc_rdma_iov_dma_map(void *data, const struct kvec *iov)
551 return svc_rdma_page_dma_map(data, virt_to_page(iov->iov_base),
552 offset_in_page(iov->iov_base),
557 * svc_rdma_xb_dma_map - DMA map all segments of an xdr_buf
558 * @xdr: xdr_buf containing portion of an RPC message to transmit
559 * @data: pointer to arguments
562 * %0 if DMA mapping was successful
563 * %-EIO if DMA mapping failed
565 * On failure, any DMA mappings that have been already done must be
566 * unmapped by the caller.
568 static int svc_rdma_xb_dma_map(const struct xdr_buf *xdr, void *data)
570 unsigned int len, remaining;
571 unsigned long pageoff;
572 struct page **ppages;
575 ret = svc_rdma_iov_dma_map(data, &xdr->head[0]);
579 ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT);
580 pageoff = offset_in_page(xdr->page_base);
581 remaining = xdr->page_len;
583 len = min_t(u32, PAGE_SIZE - pageoff, remaining);
585 ret = svc_rdma_page_dma_map(data, *ppages++, pageoff, len);
593 ret = svc_rdma_iov_dma_map(data, &xdr->tail[0]);
600 struct svc_rdma_pullup_data {
602 unsigned int pd_length;
603 unsigned int pd_num_sges;
607 * svc_rdma_xb_count_sges - Count how many SGEs will be needed
608 * @xdr: xdr_buf containing portion of an RPC message to transmit
609 * @data: pointer to arguments
612 * Number of SGEs needed to Send the contents of @xdr inline
614 static int svc_rdma_xb_count_sges(const struct xdr_buf *xdr,
617 struct svc_rdma_pullup_data *args = data;
618 unsigned int remaining;
619 unsigned long offset;
621 if (xdr->head[0].iov_len)
624 offset = offset_in_page(xdr->page_base);
625 remaining = xdr->page_len;
628 remaining -= min_t(u32, PAGE_SIZE - offset, remaining);
632 if (xdr->tail[0].iov_len)
635 args->pd_length += xdr->len;
640 * svc_rdma_pull_up_needed - Determine whether to use pull-up
641 * @rdma: controlling transport
642 * @sctxt: send_ctxt for the Send WR
643 * @rctxt: Write and Reply chunks provided by client
644 * @xdr: xdr_buf containing RPC message to transmit
647 * %true if pull-up must be used
650 static bool svc_rdma_pull_up_needed(const struct svcxprt_rdma *rdma,
651 const struct svc_rdma_send_ctxt *sctxt,
652 const struct svc_rdma_recv_ctxt *rctxt,
653 const struct xdr_buf *xdr)
655 /* Resources needed for the transport header */
656 struct svc_rdma_pullup_data args = {
657 .pd_length = sctxt->sc_hdrbuf.len,
662 ret = pcl_process_nonpayloads(&rctxt->rc_write_pcl, xdr,
663 svc_rdma_xb_count_sges, &args);
667 if (args.pd_length < RPCRDMA_PULLUP_THRESH)
669 return args.pd_num_sges >= rdma->sc_max_send_sges;
673 * svc_rdma_xb_linearize - Copy region of xdr_buf to flat buffer
674 * @xdr: xdr_buf containing portion of an RPC message to copy
675 * @data: pointer to arguments
680 static int svc_rdma_xb_linearize(const struct xdr_buf *xdr,
683 struct svc_rdma_pullup_data *args = data;
684 unsigned int len, remaining;
685 unsigned long pageoff;
686 struct page **ppages;
688 if (xdr->head[0].iov_len) {
689 memcpy(args->pd_dest, xdr->head[0].iov_base, xdr->head[0].iov_len);
690 args->pd_dest += xdr->head[0].iov_len;
693 ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT);
694 pageoff = offset_in_page(xdr->page_base);
695 remaining = xdr->page_len;
697 len = min_t(u32, PAGE_SIZE - pageoff, remaining);
698 memcpy(args->pd_dest, page_address(*ppages) + pageoff, len);
700 args->pd_dest += len;
705 if (xdr->tail[0].iov_len) {
706 memcpy(args->pd_dest, xdr->tail[0].iov_base, xdr->tail[0].iov_len);
707 args->pd_dest += xdr->tail[0].iov_len;
710 args->pd_length += xdr->len;
715 * svc_rdma_pull_up_reply_msg - Copy Reply into a single buffer
716 * @rdma: controlling transport
717 * @sctxt: send_ctxt for the Send WR; xprt hdr is already prepared
718 * @rctxt: Write and Reply chunks provided by client
719 * @xdr: prepared xdr_buf containing RPC message
721 * The device is not capable of sending the reply directly.
722 * Assemble the elements of @xdr into the transport header buffer.
725 * pull_up_needed has determined that @xdr will fit in the buffer.
728 * %0 if pull-up was successful
729 * %-EMSGSIZE if a buffer manipulation problem occurred
731 static int svc_rdma_pull_up_reply_msg(const struct svcxprt_rdma *rdma,
732 struct svc_rdma_send_ctxt *sctxt,
733 const struct svc_rdma_recv_ctxt *rctxt,
734 const struct xdr_buf *xdr)
736 struct svc_rdma_pullup_data args = {
737 .pd_dest = sctxt->sc_xprt_buf + sctxt->sc_hdrbuf.len,
741 ret = pcl_process_nonpayloads(&rctxt->rc_write_pcl, xdr,
742 svc_rdma_xb_linearize, &args);
746 sctxt->sc_sges[0].length = sctxt->sc_hdrbuf.len + args.pd_length;
747 trace_svcrdma_send_pullup(sctxt, args.pd_length);
751 /* svc_rdma_map_reply_msg - DMA map the buffer holding RPC message
752 * @rdma: controlling transport
753 * @sctxt: send_ctxt for the Send WR
754 * @rctxt: Write and Reply chunks provided by client
755 * @xdr: prepared xdr_buf containing RPC message
758 * %0 if DMA mapping was successful.
759 * %-EMSGSIZE if a buffer manipulation problem occurred
760 * %-EIO if DMA mapping failed
762 * The Send WR's num_sge field is set in all cases.
764 int svc_rdma_map_reply_msg(struct svcxprt_rdma *rdma,
765 struct svc_rdma_send_ctxt *sctxt,
766 const struct svc_rdma_recv_ctxt *rctxt,
767 const struct xdr_buf *xdr)
769 struct svc_rdma_map_data args = {
774 /* Set up the (persistently-mapped) transport header SGE. */
775 sctxt->sc_send_wr.num_sge = 1;
776 sctxt->sc_sges[0].length = sctxt->sc_hdrbuf.len;
778 /* If there is a Reply chunk, nothing follows the transport
779 * header, and we're done here.
781 if (!pcl_is_empty(&rctxt->rc_reply_pcl))
784 /* For pull-up, svc_rdma_send() will sync the transport header.
785 * No additional DMA mapping is necessary.
787 if (svc_rdma_pull_up_needed(rdma, sctxt, rctxt, xdr))
788 return svc_rdma_pull_up_reply_msg(rdma, sctxt, rctxt, xdr);
790 return pcl_process_nonpayloads(&rctxt->rc_write_pcl, xdr,
791 svc_rdma_xb_dma_map, &args);
794 /* Prepare the portion of the RPC Reply that will be transmitted
795 * via RDMA Send. The RPC-over-RDMA transport header is prepared
796 * in sc_sges[0], and the RPC xdr_buf is prepared in following sges.
798 * Depending on whether a Write list or Reply chunk is present,
799 * the server may send all, a portion of, or none of the xdr_buf.
800 * In the latter case, only the transport header (sc_sges[0]) is
803 * RDMA Send is the last step of transmitting an RPC reply. Pages
804 * involved in the earlier RDMA Writes are here transferred out
805 * of the rqstp and into the sctxt's page array. These pages are
806 * DMA unmapped by each Write completion, but the subsequent Send
807 * completion finally releases these pages.
810 * - The Reply's transport header will never be larger than a page.
812 static int svc_rdma_send_reply_msg(struct svcxprt_rdma *rdma,
813 struct svc_rdma_send_ctxt *sctxt,
814 const struct svc_rdma_recv_ctxt *rctxt,
815 struct svc_rqst *rqstp)
819 ret = svc_rdma_map_reply_msg(rdma, sctxt, rctxt, &rqstp->rq_res);
823 if (rctxt->rc_inv_rkey) {
824 sctxt->sc_send_wr.opcode = IB_WR_SEND_WITH_INV;
825 sctxt->sc_send_wr.ex.invalidate_rkey = rctxt->rc_inv_rkey;
827 sctxt->sc_send_wr.opcode = IB_WR_SEND;
830 ret = svc_rdma_send(rdma, sctxt);
834 ret = wait_for_completion_killable(&sctxt->sc_done);
835 svc_rdma_send_ctxt_put(rdma, sctxt);
840 * svc_rdma_send_error_msg - Send an RPC/RDMA v1 error response
841 * @rdma: controlling transport context
842 * @sctxt: Send context for the response
843 * @rctxt: Receive context for incoming bad message
844 * @status: negative errno indicating error that occurred
846 * Given the client-provided Read, Write, and Reply chunks, the
847 * server was not able to parse the Call or form a complete Reply.
848 * Return an RDMA_ERROR message so the client can retire the RPC
851 * The caller does not have to release @sctxt. It is released by
852 * Send completion, or by this function on error.
854 void svc_rdma_send_error_msg(struct svcxprt_rdma *rdma,
855 struct svc_rdma_send_ctxt *sctxt,
856 struct svc_rdma_recv_ctxt *rctxt,
859 __be32 *rdma_argp = rctxt->rc_recv_buf;
862 rpcrdma_set_xdrlen(&sctxt->sc_hdrbuf, 0);
863 xdr_init_encode(&sctxt->sc_stream, &sctxt->sc_hdrbuf,
864 sctxt->sc_xprt_buf, NULL);
866 p = xdr_reserve_space(&sctxt->sc_stream,
867 rpcrdma_fixed_maxsz * sizeof(*p));
872 *p++ = *(rdma_argp + 1);
873 *p++ = rdma->sc_fc_credits;
877 case -EPROTONOSUPPORT:
878 p = xdr_reserve_space(&sctxt->sc_stream, 3 * sizeof(*p));
883 *p++ = rpcrdma_version;
884 *p = rpcrdma_version;
885 trace_svcrdma_err_vers(*rdma_argp);
888 p = xdr_reserve_space(&sctxt->sc_stream, sizeof(*p));
893 trace_svcrdma_err_chunk(*rdma_argp);
896 /* Remote Invalidation is skipped for simplicity. */
897 sctxt->sc_send_wr.num_sge = 1;
898 sctxt->sc_send_wr.opcode = IB_WR_SEND;
899 sctxt->sc_sges[0].length = sctxt->sc_hdrbuf.len;
900 if (svc_rdma_send(rdma, sctxt))
903 wait_for_completion_killable(&sctxt->sc_done);
906 svc_rdma_send_ctxt_put(rdma, sctxt);
910 * svc_rdma_sendto - Transmit an RPC reply
911 * @rqstp: processed RPC request, reply XDR already in ::rq_res
913 * Any resources still associated with @rqstp are released upon return.
914 * If no reply message was possible, the connection is closed.
917 * %0 if an RPC reply has been successfully posted,
918 * %-ENOMEM if a resource shortage occurred (connection is lost),
919 * %-ENOTCONN if posting failed (connection is lost).
921 int svc_rdma_sendto(struct svc_rqst *rqstp)
923 struct svc_xprt *xprt = rqstp->rq_xprt;
924 struct svcxprt_rdma *rdma =
925 container_of(xprt, struct svcxprt_rdma, sc_xprt);
926 struct svc_rdma_recv_ctxt *rctxt = rqstp->rq_xprt_ctxt;
927 __be32 *rdma_argp = rctxt->rc_recv_buf;
928 struct svc_rdma_send_ctxt *sctxt;
929 unsigned int rc_size;
934 if (svc_xprt_is_dead(xprt))
935 goto drop_connection;
938 sctxt = svc_rdma_send_ctxt_get(rdma);
940 goto drop_connection;
943 p = xdr_reserve_space(&sctxt->sc_stream,
944 rpcrdma_fixed_maxsz * sizeof(*p));
948 ret = svc_rdma_send_reply_chunk(rdma, rctxt, &rqstp->rq_res);
954 *p++ = *(rdma_argp + 1);
955 *p++ = rdma->sc_fc_credits;
956 *p = pcl_is_empty(&rctxt->rc_reply_pcl) ? rdma_msg : rdma_nomsg;
958 ret = svc_rdma_encode_read_list(sctxt);
961 ret = svc_rdma_encode_write_list(rctxt, sctxt);
964 ret = svc_rdma_encode_reply_chunk(rctxt, sctxt, rc_size);
968 ret = svc_rdma_send_reply_msg(rdma, sctxt, rctxt, rqstp);
972 /* Prevent svc_xprt_release() from releasing the page backing
973 * rq_res.head[0].iov_base. It's no longer being accessed by
975 rqstp->rq_respages++;
979 if (ret != -E2BIG && ret != -EINVAL)
982 svc_rdma_send_error_msg(rdma, sctxt, rctxt, ret);
986 svc_rdma_send_ctxt_put(rdma, sctxt);
988 trace_svcrdma_send_err(rqstp, ret);
989 svc_xprt_deferred_close(&rdma->sc_xprt);
994 * svc_rdma_result_payload - special processing for a result payload
995 * @rqstp: svc_rqst to operate on
996 * @offset: payload's byte offset in @xdr
997 * @length: size of payload, in bytes
1000 * %0 if successful or nothing needed to be done
1001 * %-EMSGSIZE on XDR buffer overflow
1002 * %-E2BIG if the payload was larger than the Write chunk
1003 * %-EINVAL if client provided too many segments
1004 * %-ENOMEM if rdma_rw context pool was exhausted
1005 * %-ENOTCONN if posting failed (connection is lost)
1006 * %-EIO if rdma_rw initialization failed (DMA mapping, etc)
1008 int svc_rdma_result_payload(struct svc_rqst *rqstp, unsigned int offset,
1009 unsigned int length)
1011 struct svc_rdma_recv_ctxt *rctxt = rqstp->rq_xprt_ctxt;
1012 struct svc_rdma_chunk *chunk;
1013 struct svcxprt_rdma *rdma;
1014 struct xdr_buf subbuf;
1017 chunk = rctxt->rc_cur_result_payload;
1018 if (!length || !chunk)
1020 rctxt->rc_cur_result_payload =
1021 pcl_next_chunk(&rctxt->rc_write_pcl, chunk);
1022 if (length > chunk->ch_length)
1025 chunk->ch_position = offset;
1026 chunk->ch_payload_length = length;
1028 if (xdr_buf_subsegment(&rqstp->rq_res, &subbuf, offset, length))
1031 rdma = container_of(rqstp->rq_xprt, struct svcxprt_rdma, sc_xprt);
1032 ret = svc_rdma_send_write_chunk(rdma, chunk, &subbuf);