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/rpc_rdma.h>
110 #include <linux/sunrpc/svc_rdma.h>
112 #include "xprt_rdma.h"
113 #include <trace/events/rpcrdma.h>
115 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
117 static void svc_rdma_wc_send(struct ib_cq *cq, struct ib_wc *wc);
119 static inline struct svc_rdma_send_ctxt *
120 svc_rdma_next_send_ctxt(struct list_head *list)
122 return list_first_entry_or_null(list, struct svc_rdma_send_ctxt,
126 static struct svc_rdma_send_ctxt *
127 svc_rdma_send_ctxt_alloc(struct svcxprt_rdma *rdma)
129 struct svc_rdma_send_ctxt *ctxt;
135 size = sizeof(*ctxt);
136 size += rdma->sc_max_send_sges * sizeof(struct ib_sge);
137 ctxt = kmalloc(size, GFP_KERNEL);
140 buffer = kmalloc(rdma->sc_max_req_size, GFP_KERNEL);
143 addr = ib_dma_map_single(rdma->sc_pd->device, buffer,
144 rdma->sc_max_req_size, DMA_TO_DEVICE);
145 if (ib_dma_mapping_error(rdma->sc_pd->device, addr))
148 ctxt->sc_send_wr.next = NULL;
149 ctxt->sc_send_wr.wr_cqe = &ctxt->sc_cqe;
150 ctxt->sc_send_wr.sg_list = ctxt->sc_sges;
151 ctxt->sc_send_wr.send_flags = IB_SEND_SIGNALED;
152 ctxt->sc_cqe.done = svc_rdma_wc_send;
153 ctxt->sc_xprt_buf = buffer;
154 ctxt->sc_sges[0].addr = addr;
156 for (i = 0; i < rdma->sc_max_send_sges; i++)
157 ctxt->sc_sges[i].lkey = rdma->sc_pd->local_dma_lkey;
169 * svc_rdma_send_ctxts_destroy - Release all send_ctxt's for an xprt
170 * @rdma: svcxprt_rdma being torn down
173 void svc_rdma_send_ctxts_destroy(struct svcxprt_rdma *rdma)
175 struct svc_rdma_send_ctxt *ctxt;
177 while ((ctxt = svc_rdma_next_send_ctxt(&rdma->sc_send_ctxts))) {
178 list_del(&ctxt->sc_list);
179 ib_dma_unmap_single(rdma->sc_pd->device,
180 ctxt->sc_sges[0].addr,
181 rdma->sc_max_req_size,
183 kfree(ctxt->sc_xprt_buf);
189 * svc_rdma_send_ctxt_get - Get a free send_ctxt
190 * @rdma: controlling svcxprt_rdma
192 * Returns a ready-to-use send_ctxt, or NULL if none are
193 * available and a fresh one cannot be allocated.
195 struct svc_rdma_send_ctxt *svc_rdma_send_ctxt_get(struct svcxprt_rdma *rdma)
197 struct svc_rdma_send_ctxt *ctxt;
199 spin_lock(&rdma->sc_send_lock);
200 ctxt = svc_rdma_next_send_ctxt(&rdma->sc_send_ctxts);
203 list_del(&ctxt->sc_list);
204 spin_unlock(&rdma->sc_send_lock);
207 ctxt->sc_send_wr.num_sge = 0;
208 ctxt->sc_cur_sge_no = 0;
209 ctxt->sc_page_count = 0;
213 spin_unlock(&rdma->sc_send_lock);
214 ctxt = svc_rdma_send_ctxt_alloc(rdma);
221 * svc_rdma_send_ctxt_put - Return send_ctxt to free list
222 * @rdma: controlling svcxprt_rdma
223 * @ctxt: object to return to the free list
225 * Pages left in sc_pages are DMA unmapped and released.
227 void svc_rdma_send_ctxt_put(struct svcxprt_rdma *rdma,
228 struct svc_rdma_send_ctxt *ctxt)
230 struct ib_device *device = rdma->sc_cm_id->device;
233 /* The first SGE contains the transport header, which
234 * remains mapped until @ctxt is destroyed.
236 for (i = 1; i < ctxt->sc_send_wr.num_sge; i++)
237 ib_dma_unmap_page(device,
238 ctxt->sc_sges[i].addr,
239 ctxt->sc_sges[i].length,
242 for (i = 0; i < ctxt->sc_page_count; ++i)
243 put_page(ctxt->sc_pages[i]);
245 spin_lock(&rdma->sc_send_lock);
246 list_add(&ctxt->sc_list, &rdma->sc_send_ctxts);
247 spin_unlock(&rdma->sc_send_lock);
251 * svc_rdma_wc_send - Invoked by RDMA provider for each polled Send WC
252 * @cq: Completion Queue context
253 * @wc: Work Completion object
255 * NB: The svc_xprt/svcxprt_rdma is pinned whenever it's possible that
256 * the Send completion handler could be running.
258 static void svc_rdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
260 struct svcxprt_rdma *rdma = cq->cq_context;
261 struct ib_cqe *cqe = wc->wr_cqe;
262 struct svc_rdma_send_ctxt *ctxt;
264 trace_svcrdma_wc_send(wc);
266 atomic_inc(&rdma->sc_sq_avail);
267 wake_up(&rdma->sc_send_wait);
269 ctxt = container_of(cqe, struct svc_rdma_send_ctxt, sc_cqe);
270 svc_rdma_send_ctxt_put(rdma, ctxt);
272 if (unlikely(wc->status != IB_WC_SUCCESS)) {
273 set_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags);
274 svc_xprt_enqueue(&rdma->sc_xprt);
277 svc_xprt_put(&rdma->sc_xprt);
281 * svc_rdma_send - Post a single Send WR
282 * @rdma: transport on which to post the WR
283 * @wr: prepared Send WR to post
285 * Returns zero the Send WR was posted successfully. Otherwise, a
286 * negative errno is returned.
288 int svc_rdma_send(struct svcxprt_rdma *rdma, struct ib_send_wr *wr)
294 /* If the SQ is full, wait until an SQ entry is available */
296 if ((atomic_dec_return(&rdma->sc_sq_avail) < 0)) {
297 atomic_inc(&rdma_stat_sq_starve);
298 trace_svcrdma_sq_full(rdma);
299 atomic_inc(&rdma->sc_sq_avail);
300 wait_event(rdma->sc_send_wait,
301 atomic_read(&rdma->sc_sq_avail) > 1);
302 if (test_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags))
304 trace_svcrdma_sq_retry(rdma);
308 svc_xprt_get(&rdma->sc_xprt);
309 trace_svcrdma_post_send(wr);
310 ret = ib_post_send(rdma->sc_qp, wr, NULL);
316 trace_svcrdma_sq_post_err(rdma, ret);
317 set_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags);
318 svc_xprt_put(&rdma->sc_xprt);
319 wake_up(&rdma->sc_send_wait);
323 static u32 xdr_padsize(u32 len)
325 return (len & 3) ? (4 - (len & 3)) : 0;
328 /* Returns length of transport header, in bytes.
330 static unsigned int svc_rdma_reply_hdr_len(__be32 *rdma_resp)
337 /* RPC-over-RDMA V1 replies never have a Read list. */
338 p += rpcrdma_fixed_maxsz + 1;
340 /* Skip Write list. */
341 while (*p++ != xdr_zero) {
342 nsegs = be32_to_cpup(p++);
343 p += nsegs * rpcrdma_segment_maxsz;
346 /* Skip Reply chunk. */
347 if (*p++ != xdr_zero) {
348 nsegs = be32_to_cpup(p++);
349 p += nsegs * rpcrdma_segment_maxsz;
352 return (unsigned long)p - (unsigned long)rdma_resp;
355 /* One Write chunk is copied from Call transport header to Reply
356 * transport header. Each segment's length field is updated to
357 * reflect number of bytes consumed in the segment.
359 * Returns number of segments in this chunk.
361 static unsigned int xdr_encode_write_chunk(__be32 *dst, __be32 *src,
362 unsigned int remaining)
364 unsigned int i, nsegs;
367 /* Write list discriminator */
370 /* number of segments in this chunk */
371 nsegs = be32_to_cpup(src);
374 for (i = nsegs; i; i--) {
375 /* segment's RDMA handle */
378 /* bytes returned in this segment */
379 seg_len = be32_to_cpu(*src);
380 if (remaining >= seg_len) {
381 /* entire segment was consumed */
383 remaining -= seg_len;
385 /* segment only partly filled */
386 *dst = cpu_to_be32(remaining);
391 /* segment's RDMA offset */
399 /* The client provided a Write list in the Call message. Fill in
400 * the segments in the first Write chunk in the Reply's transport
401 * header with the number of bytes consumed in each segment.
402 * Remaining chunks are returned unused.
405 * - Client has provided only one Write chunk
407 static void svc_rdma_xdr_encode_write_list(__be32 *rdma_resp, __be32 *wr_ch,
408 unsigned int consumed)
413 /* RPC-over-RDMA V1 replies never have a Read list. */
414 p = rdma_resp + rpcrdma_fixed_maxsz + 1;
417 while (*q != xdr_zero) {
418 nsegs = xdr_encode_write_chunk(p, q, consumed);
419 q += 2 + nsegs * rpcrdma_segment_maxsz;
420 p += 2 + nsegs * rpcrdma_segment_maxsz;
424 /* Terminate Write list */
427 /* Reply chunk discriminator; may be replaced later */
431 /* The client provided a Reply chunk in the Call message. Fill in
432 * the segments in the Reply chunk in the Reply message with the
433 * number of bytes consumed in each segment.
436 * - Reply can always fit in the provided Reply chunk
438 static void svc_rdma_xdr_encode_reply_chunk(__be32 *rdma_resp, __be32 *rp_ch,
439 unsigned int consumed)
443 /* Find the Reply chunk in the Reply's xprt header.
444 * RPC-over-RDMA V1 replies never have a Read list.
446 p = rdma_resp + rpcrdma_fixed_maxsz + 1;
448 /* Skip past Write list */
449 while (*p++ != xdr_zero)
450 p += 1 + be32_to_cpup(p) * rpcrdma_segment_maxsz;
452 xdr_encode_write_chunk(p, rp_ch, consumed);
455 /* Parse the RPC Call's transport header.
457 static void svc_rdma_get_write_arrays(__be32 *rdma_argp,
458 __be32 **write, __be32 **reply)
462 p = rdma_argp + rpcrdma_fixed_maxsz;
465 while (*p++ != xdr_zero)
469 if (*p != xdr_zero) {
471 while (*p++ != xdr_zero)
472 p += 1 + be32_to_cpu(*p) * 4;
485 static int svc_rdma_dma_map_page(struct svcxprt_rdma *rdma,
486 struct svc_rdma_send_ctxt *ctxt,
488 unsigned long offset,
491 struct ib_device *dev = rdma->sc_cm_id->device;
494 dma_addr = ib_dma_map_page(dev, page, offset, len, DMA_TO_DEVICE);
495 if (ib_dma_mapping_error(dev, dma_addr))
498 ctxt->sc_sges[ctxt->sc_cur_sge_no].addr = dma_addr;
499 ctxt->sc_sges[ctxt->sc_cur_sge_no].length = len;
500 ctxt->sc_send_wr.num_sge++;
504 trace_svcrdma_dma_map_page(rdma, page);
508 /* ib_dma_map_page() is used here because svc_rdma_dma_unmap()
509 * handles DMA-unmap and it uses ib_dma_unmap_page() exclusively.
511 static int svc_rdma_dma_map_buf(struct svcxprt_rdma *rdma,
512 struct svc_rdma_send_ctxt *ctxt,
516 return svc_rdma_dma_map_page(rdma, ctxt, virt_to_page(base),
517 offset_in_page(base), len);
521 * svc_rdma_sync_reply_hdr - DMA sync the transport header buffer
522 * @rdma: controlling transport
523 * @ctxt: send_ctxt for the Send WR
524 * @len: length of transport header
527 void svc_rdma_sync_reply_hdr(struct svcxprt_rdma *rdma,
528 struct svc_rdma_send_ctxt *ctxt,
531 ctxt->sc_sges[0].length = len;
532 ctxt->sc_send_wr.num_sge++;
533 ib_dma_sync_single_for_device(rdma->sc_pd->device,
534 ctxt->sc_sges[0].addr, len,
538 /* If the xdr_buf has more elements than the device can
539 * transmit in a single RDMA Send, then the reply will
540 * have to be copied into a bounce buffer.
542 static bool svc_rdma_pull_up_needed(struct svcxprt_rdma *rdma,
553 unsigned int remaining;
554 unsigned long pageoff;
556 pageoff = xdr->page_base & ~PAGE_MASK;
557 remaining = xdr->page_len;
560 remaining -= min_t(u32, PAGE_SIZE - pageoff,
567 if (xdr->tail[0].iov_len)
570 /* assume 1 SGE is needed for the transport header */
571 return elements >= rdma->sc_max_send_sges;
574 /* The device is not capable of sending the reply directly.
575 * Assemble the elements of @xdr into the transport header
578 static int svc_rdma_pull_up_reply_msg(struct svcxprt_rdma *rdma,
579 struct svc_rdma_send_ctxt *ctxt,
580 struct xdr_buf *xdr, __be32 *wr_lst)
582 unsigned char *dst, *tailbase;
583 unsigned int taillen;
585 dst = ctxt->sc_xprt_buf;
586 dst += ctxt->sc_sges[0].length;
588 memcpy(dst, xdr->head[0].iov_base, xdr->head[0].iov_len);
589 dst += xdr->head[0].iov_len;
591 tailbase = xdr->tail[0].iov_base;
592 taillen = xdr->tail[0].iov_len;
596 xdrpad = xdr_padsize(xdr->page_len);
597 if (taillen && xdrpad) {
602 unsigned int len, remaining;
603 unsigned long pageoff;
604 struct page **ppages;
606 ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT);
607 pageoff = xdr->page_base & ~PAGE_MASK;
608 remaining = xdr->page_len;
610 len = min_t(u32, PAGE_SIZE - pageoff, remaining);
612 memcpy(dst, page_address(*ppages) + pageoff, len);
621 memcpy(dst, tailbase, taillen);
623 ctxt->sc_sges[0].length += xdr->len;
624 ib_dma_sync_single_for_device(rdma->sc_pd->device,
625 ctxt->sc_sges[0].addr,
626 ctxt->sc_sges[0].length,
632 /* svc_rdma_map_reply_msg - Map the buffer holding RPC message
633 * @rdma: controlling transport
634 * @ctxt: send_ctxt for the Send WR
635 * @xdr: prepared xdr_buf containing RPC message
636 * @wr_lst: pointer to Call header's Write list, or NULL
638 * Load the xdr_buf into the ctxt's sge array, and DMA map each
639 * element as it is added.
641 * Returns zero on success, or a negative errno on failure.
643 int svc_rdma_map_reply_msg(struct svcxprt_rdma *rdma,
644 struct svc_rdma_send_ctxt *ctxt,
645 struct xdr_buf *xdr, __be32 *wr_lst)
647 unsigned int len, remaining;
648 unsigned long page_off;
649 struct page **ppages;
654 if (svc_rdma_pull_up_needed(rdma, xdr, wr_lst))
655 return svc_rdma_pull_up_reply_msg(rdma, ctxt, xdr, wr_lst);
657 ++ctxt->sc_cur_sge_no;
658 ret = svc_rdma_dma_map_buf(rdma, ctxt,
659 xdr->head[0].iov_base,
660 xdr->head[0].iov_len);
664 /* If a Write chunk is present, the xdr_buf's page list
665 * is not included inline. However the Upper Layer may
666 * have added XDR padding in the tail buffer, and that
667 * should not be included inline.
670 base = xdr->tail[0].iov_base;
671 len = xdr->tail[0].iov_len;
672 xdr_pad = xdr_padsize(xdr->page_len);
674 if (len && xdr_pad) {
682 ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT);
683 page_off = xdr->page_base & ~PAGE_MASK;
684 remaining = xdr->page_len;
686 len = min_t(u32, PAGE_SIZE - page_off, remaining);
688 ++ctxt->sc_cur_sge_no;
689 ret = svc_rdma_dma_map_page(rdma, ctxt, *ppages++,
698 base = xdr->tail[0].iov_base;
699 len = xdr->tail[0].iov_len;
702 ++ctxt->sc_cur_sge_no;
703 ret = svc_rdma_dma_map_buf(rdma, ctxt, base, len);
711 /* The svc_rqst and all resources it owns are released as soon as
712 * svc_rdma_sendto returns. Transfer pages under I/O to the ctxt
713 * so they are released by the Send completion handler.
715 static void svc_rdma_save_io_pages(struct svc_rqst *rqstp,
716 struct svc_rdma_send_ctxt *ctxt)
718 int i, pages = rqstp->rq_next_page - rqstp->rq_respages;
720 ctxt->sc_page_count += pages;
721 for (i = 0; i < pages; i++) {
722 ctxt->sc_pages[i] = rqstp->rq_respages[i];
723 rqstp->rq_respages[i] = NULL;
726 /* Prevent svc_xprt_release from releasing pages in rq_pages */
727 rqstp->rq_next_page = rqstp->rq_respages;
730 /* Prepare the portion of the RPC Reply that will be transmitted
731 * via RDMA Send. The RPC-over-RDMA transport header is prepared
732 * in sc_sges[0], and the RPC xdr_buf is prepared in following sges.
734 * Depending on whether a Write list or Reply chunk is present,
735 * the server may send all, a portion of, or none of the xdr_buf.
736 * In the latter case, only the transport header (sc_sges[0]) is
739 * RDMA Send is the last step of transmitting an RPC reply. Pages
740 * involved in the earlier RDMA Writes are here transferred out
741 * of the rqstp and into the sctxt's page array. These pages are
742 * DMA unmapped by each Write completion, but the subsequent Send
743 * completion finally releases these pages.
746 * - The Reply's transport header will never be larger than a page.
748 static int svc_rdma_send_reply_msg(struct svcxprt_rdma *rdma,
749 struct svc_rdma_send_ctxt *sctxt,
750 struct svc_rdma_recv_ctxt *rctxt,
751 struct svc_rqst *rqstp,
752 __be32 *wr_lst, __be32 *rp_ch)
757 ret = svc_rdma_map_reply_msg(rdma, sctxt,
758 &rqstp->rq_res, wr_lst);
763 svc_rdma_save_io_pages(rqstp, sctxt);
765 if (rctxt->rc_inv_rkey) {
766 sctxt->sc_send_wr.opcode = IB_WR_SEND_WITH_INV;
767 sctxt->sc_send_wr.ex.invalidate_rkey = rctxt->rc_inv_rkey;
769 sctxt->sc_send_wr.opcode = IB_WR_SEND;
771 dprintk("svcrdma: posting Send WR with %u sge(s)\n",
772 sctxt->sc_send_wr.num_sge);
773 return svc_rdma_send(rdma, &sctxt->sc_send_wr);
776 /* Given the client-provided Write and Reply chunks, the server was not
777 * able to form a complete reply. Return an RDMA_ERROR message so the
778 * client can retire this RPC transaction. As above, the Send completion
779 * routine releases payload pages that were part of a previous RDMA Write.
781 * Remote Invalidation is skipped for simplicity.
783 static int svc_rdma_send_error_msg(struct svcxprt_rdma *rdma,
784 struct svc_rdma_send_ctxt *ctxt,
785 struct svc_rqst *rqstp)
790 p = ctxt->sc_xprt_buf;
791 trace_svcrdma_err_chunk(*p);
795 svc_rdma_sync_reply_hdr(rdma, ctxt, RPCRDMA_HDRLEN_ERR);
797 svc_rdma_save_io_pages(rqstp, ctxt);
799 ctxt->sc_send_wr.opcode = IB_WR_SEND;
800 ret = svc_rdma_send(rdma, &ctxt->sc_send_wr);
802 svc_rdma_send_ctxt_put(rdma, ctxt);
810 * svc_rdma_sendto - Transmit an RPC reply
811 * @rqstp: processed RPC request, reply XDR already in ::rq_res
813 * Any resources still associated with @rqstp are released upon return.
814 * If no reply message was possible, the connection is closed.
817 * %0 if an RPC reply has been successfully posted,
818 * %-ENOMEM if a resource shortage occurred (connection is lost),
819 * %-ENOTCONN if posting failed (connection is lost).
821 int svc_rdma_sendto(struct svc_rqst *rqstp)
823 struct svc_xprt *xprt = rqstp->rq_xprt;
824 struct svcxprt_rdma *rdma =
825 container_of(xprt, struct svcxprt_rdma, sc_xprt);
826 struct svc_rdma_recv_ctxt *rctxt = rqstp->rq_xprt_ctxt;
827 __be32 *p, *rdma_argp, *rdma_resp, *wr_lst, *rp_ch;
828 struct xdr_buf *xdr = &rqstp->rq_res;
829 struct svc_rdma_send_ctxt *sctxt;
832 rdma_argp = rctxt->rc_recv_buf;
833 svc_rdma_get_write_arrays(rdma_argp, &wr_lst, &rp_ch);
835 /* Create the RDMA response header. xprt->xpt_mutex,
836 * acquired in svc_send(), serializes RPC replies. The
837 * code path below that inserts the credit grant value
838 * into each transport header runs only inside this
842 sctxt = svc_rdma_send_ctxt_get(rdma);
845 rdma_resp = sctxt->sc_xprt_buf;
849 *p++ = *(rdma_argp + 1);
850 *p++ = rdma->sc_fc_credits;
851 *p++ = rp_ch ? rdma_nomsg : rdma_msg;
853 /* Start with empty chunks */
859 /* XXX: Presume the client sent only one Write chunk */
860 unsigned long offset;
863 if (rctxt->rc_read_payload_length) {
864 offset = rctxt->rc_read_payload_offset;
865 length = rctxt->rc_read_payload_length;
867 offset = xdr->head[0].iov_len;
868 length = xdr->page_len;
870 ret = svc_rdma_send_write_chunk(rdma, wr_lst, xdr, offset,
874 svc_rdma_xdr_encode_write_list(rdma_resp, wr_lst, ret);
877 ret = svc_rdma_send_reply_chunk(rdma, rp_ch, wr_lst, xdr);
880 svc_rdma_xdr_encode_reply_chunk(rdma_resp, rp_ch, ret);
883 svc_rdma_sync_reply_hdr(rdma, sctxt, svc_rdma_reply_hdr_len(rdma_resp));
884 ret = svc_rdma_send_reply_msg(rdma, sctxt, rctxt, rqstp,
891 if (ret != -E2BIG && ret != -EINVAL)
894 ret = svc_rdma_send_error_msg(rdma, sctxt, rqstp);
900 svc_rdma_send_ctxt_put(rdma, sctxt);
902 trace_svcrdma_send_failed(rqstp, ret);
903 set_bit(XPT_CLOSE, &xprt->xpt_flags);
908 * svc_rdma_read_payload - special processing for a READ payload
909 * @rqstp: svc_rqst to operate on
910 * @offset: payload's byte offset in @xdr
911 * @length: size of payload, in bytes
913 * Returns zero on success.
915 * For the moment, just record the xdr_buf location of the READ
916 * payload. svc_rdma_sendto will use that location later when
917 * we actually send the payload.
919 int svc_rdma_read_payload(struct svc_rqst *rqstp, unsigned int offset,
922 struct svc_rdma_recv_ctxt *rctxt = rqstp->rq_xprt_ctxt;
924 /* XXX: Just one READ payload slot for now, since our
925 * transport implementation currently supports only one
928 rctxt->rc_read_payload_offset = offset;
929 rctxt->rc_read_payload_length = length;