1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2f13193f5SChuck Lever /*
3ecf85b23SChuck Lever * Copyright (c) 2016-2018 Oracle. All rights reserved.
4f13193f5SChuck Lever *
5f13193f5SChuck Lever * Use the core R/W API to move RPC-over-RDMA Read and Write chunks.
6f13193f5SChuck Lever */
7f13193f5SChuck Lever
898895edbSChuck Lever #include <rdma/rw.h>
998895edbSChuck Lever
10f60a0869SChuck Lever #include <linux/sunrpc/xdr.h>
11f13193f5SChuck Lever #include <linux/sunrpc/rpc_rdma.h>
12f13193f5SChuck Lever #include <linux/sunrpc/svc_rdma.h>
13f13193f5SChuck Lever
1498895edbSChuck Lever #include "xprt_rdma.h"
1598895edbSChuck Lever #include <trace/events/rpcrdma.h>
16f13193f5SChuck Lever
17026d958bSChuck Lever static void svc_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc);
18026d958bSChuck Lever static void svc_rdma_wc_read_done(struct ib_cq *cq, struct ib_wc *wc);
19026d958bSChuck Lever
20f13193f5SChuck Lever /* Each R/W context contains state for one chain of RDMA Read or
21f13193f5SChuck Lever * Write Work Requests.
22f13193f5SChuck Lever *
23f13193f5SChuck Lever * Each WR chain handles a single contiguous server-side buffer,
24f13193f5SChuck Lever * because scatterlist entries after the first have to start on
25f13193f5SChuck Lever * page alignment. xdr_buf iovecs cannot guarantee alignment.
26f13193f5SChuck Lever *
27f13193f5SChuck Lever * Each WR chain handles only one R_key. Each RPC-over-RDMA segment
28f13193f5SChuck Lever * from a client may contain a unique R_key, so each WR chain moves
29f13193f5SChuck Lever * up to one segment at a time.
30f13193f5SChuck Lever *
31f13193f5SChuck Lever * The scatterlist makes this data structure over 4KB in size. To
32f13193f5SChuck Lever * make it less likely to fail, and to handle the allocation for
33f13193f5SChuck Lever * smaller I/O requests without disabling bottom-halves, these
34f13193f5SChuck Lever * contexts are created on demand, but cached and reused until the
35f13193f5SChuck Lever * controlling svcxprt_rdma is destroyed.
36f13193f5SChuck Lever */
37f13193f5SChuck Lever struct svc_rdma_rw_ctxt {
3807a92d00SChuck Lever struct llist_node rw_node;
39f13193f5SChuck Lever struct list_head rw_list;
40f13193f5SChuck Lever struct rdma_rw_ctx rw_ctx;
412abfbe7eSChuck Lever unsigned int rw_nents;
42f13193f5SChuck Lever struct sg_table rw_sg_table;
43c0fb23f8SGustavo A. R. Silva struct scatterlist rw_first_sgl[];
44f13193f5SChuck Lever };
45f13193f5SChuck Lever
46f13193f5SChuck Lever static inline struct svc_rdma_rw_ctxt *
svc_rdma_next_ctxt(struct list_head * list)47f13193f5SChuck Lever svc_rdma_next_ctxt(struct list_head *list)
48f13193f5SChuck Lever {
49f13193f5SChuck Lever return list_first_entry_or_null(list, struct svc_rdma_rw_ctxt,
50f13193f5SChuck Lever rw_list);
51f13193f5SChuck Lever }
52f13193f5SChuck Lever
53f13193f5SChuck Lever static struct svc_rdma_rw_ctxt *
svc_rdma_get_rw_ctxt(struct svcxprt_rdma * rdma,unsigned int sges)54f13193f5SChuck Lever svc_rdma_get_rw_ctxt(struct svcxprt_rdma *rdma, unsigned int sges)
55f13193f5SChuck Lever {
56f13193f5SChuck Lever struct svc_rdma_rw_ctxt *ctxt;
5707a92d00SChuck Lever struct llist_node *node;
58f13193f5SChuck Lever
59f13193f5SChuck Lever spin_lock(&rdma->sc_rw_ctxt_lock);
6007a92d00SChuck Lever node = llist_del_first(&rdma->sc_rw_ctxts);
61f13193f5SChuck Lever spin_unlock(&rdma->sc_rw_ctxt_lock);
6207a92d00SChuck Lever if (node) {
6307a92d00SChuck Lever ctxt = llist_entry(node, struct svc_rdma_rw_ctxt, rw_node);
64f13193f5SChuck Lever } else {
65ac3c32bbSChuck Lever ctxt = kmalloc_node(struct_size(ctxt, rw_first_sgl, SG_CHUNK_SIZE),
66ac3c32bbSChuck Lever GFP_KERNEL, ibdev_to_node(rdma->sc_cm_id->device));
67f13193f5SChuck Lever if (!ctxt)
68f4e53e1cSChuck Lever goto out_noctx;
6907a92d00SChuck Lever
70f13193f5SChuck Lever INIT_LIST_HEAD(&ctxt->rw_list);
71f13193f5SChuck Lever }
72f13193f5SChuck Lever
73f13193f5SChuck Lever ctxt->rw_sg_table.sgl = ctxt->rw_first_sgl;
74f13193f5SChuck Lever if (sg_alloc_table_chained(&ctxt->rw_sg_table, sges,
754635873cSMing Lei ctxt->rw_sg_table.sgl,
76f4e53e1cSChuck Lever SG_CHUNK_SIZE))
77f4e53e1cSChuck Lever goto out_free;
78f13193f5SChuck Lever return ctxt;
79f4e53e1cSChuck Lever
80f4e53e1cSChuck Lever out_free:
81f4e53e1cSChuck Lever kfree(ctxt);
82f4e53e1cSChuck Lever out_noctx:
83f4e53e1cSChuck Lever trace_svcrdma_no_rwctx_err(rdma, sges);
84f4e53e1cSChuck Lever return NULL;
85f13193f5SChuck Lever }
86f13193f5SChuck Lever
__svc_rdma_put_rw_ctxt(struct svc_rdma_rw_ctxt * ctxt,struct llist_head * list)87*b55c6333SChuck Lever static void __svc_rdma_put_rw_ctxt(struct svc_rdma_rw_ctxt *ctxt,
8807a92d00SChuck Lever struct llist_head *list)
8907a92d00SChuck Lever {
9007a92d00SChuck Lever sg_free_table_chained(&ctxt->rw_sg_table, SG_CHUNK_SIZE);
9107a92d00SChuck Lever llist_add(&ctxt->rw_node, list);
9207a92d00SChuck Lever }
9307a92d00SChuck Lever
svc_rdma_put_rw_ctxt(struct svcxprt_rdma * rdma,struct svc_rdma_rw_ctxt * ctxt)94f13193f5SChuck Lever static void svc_rdma_put_rw_ctxt(struct svcxprt_rdma *rdma,
95f13193f5SChuck Lever struct svc_rdma_rw_ctxt *ctxt)
96f13193f5SChuck Lever {
97*b55c6333SChuck Lever __svc_rdma_put_rw_ctxt(ctxt, &rdma->sc_rw_ctxts);
98f13193f5SChuck Lever }
99f13193f5SChuck Lever
100f13193f5SChuck Lever /**
101f13193f5SChuck Lever * svc_rdma_destroy_rw_ctxts - Free accumulated R/W contexts
102f13193f5SChuck Lever * @rdma: transport about to be destroyed
103f13193f5SChuck Lever *
104f13193f5SChuck Lever */
svc_rdma_destroy_rw_ctxts(struct svcxprt_rdma * rdma)105f13193f5SChuck Lever void svc_rdma_destroy_rw_ctxts(struct svcxprt_rdma *rdma)
106f13193f5SChuck Lever {
107f13193f5SChuck Lever struct svc_rdma_rw_ctxt *ctxt;
10807a92d00SChuck Lever struct llist_node *node;
109f13193f5SChuck Lever
11007a92d00SChuck Lever while ((node = llist_del_first(&rdma->sc_rw_ctxts)) != NULL) {
11107a92d00SChuck Lever ctxt = llist_entry(node, struct svc_rdma_rw_ctxt, rw_node);
112f13193f5SChuck Lever kfree(ctxt);
113f13193f5SChuck Lever }
114f13193f5SChuck Lever }
115f13193f5SChuck Lever
1162abfbe7eSChuck Lever /**
1172abfbe7eSChuck Lever * svc_rdma_rw_ctx_init - Prepare a R/W context for I/O
1182abfbe7eSChuck Lever * @rdma: controlling transport instance
1192abfbe7eSChuck Lever * @ctxt: R/W context to prepare
1202abfbe7eSChuck Lever * @offset: RDMA offset
1212abfbe7eSChuck Lever * @handle: RDMA tag/handle
1222abfbe7eSChuck Lever * @direction: I/O direction
1232abfbe7eSChuck Lever *
1242abfbe7eSChuck Lever * Returns on success, the number of WQEs that will be needed
1252abfbe7eSChuck Lever * on the workqueue, or a negative errno.
1262abfbe7eSChuck Lever */
svc_rdma_rw_ctx_init(struct svcxprt_rdma * rdma,struct svc_rdma_rw_ctxt * ctxt,u64 offset,u32 handle,enum dma_data_direction direction)1272abfbe7eSChuck Lever static int svc_rdma_rw_ctx_init(struct svcxprt_rdma *rdma,
1282abfbe7eSChuck Lever struct svc_rdma_rw_ctxt *ctxt,
1292abfbe7eSChuck Lever u64 offset, u32 handle,
1302abfbe7eSChuck Lever enum dma_data_direction direction)
1312abfbe7eSChuck Lever {
1322abfbe7eSChuck Lever int ret;
1332abfbe7eSChuck Lever
1342abfbe7eSChuck Lever ret = rdma_rw_ctx_init(&ctxt->rw_ctx, rdma->sc_qp, rdma->sc_port_num,
1352abfbe7eSChuck Lever ctxt->rw_sg_table.sgl, ctxt->rw_nents,
1362abfbe7eSChuck Lever 0, offset, handle, direction);
1372abfbe7eSChuck Lever if (unlikely(ret < 0)) {
1382abfbe7eSChuck Lever svc_rdma_put_rw_ctxt(rdma, ctxt);
1392abfbe7eSChuck Lever trace_svcrdma_dma_map_rw_err(rdma, ctxt->rw_nents, ret);
1402abfbe7eSChuck Lever }
1412abfbe7eSChuck Lever return ret;
1422abfbe7eSChuck Lever }
1432abfbe7eSChuck Lever
144f13193f5SChuck Lever /* A chunk context tracks all I/O for moving one Read or Write
1451cc5213bSRandy Dunlap * chunk. This is a set of rdma_rw's that handle data movement
146f13193f5SChuck Lever * for all segments of one chunk.
147f13193f5SChuck Lever *
148f13193f5SChuck Lever * These are small, acquired with a single allocator call, and
149f13193f5SChuck Lever * no more than one is needed per chunk. They are allocated on
150f13193f5SChuck Lever * demand, and not cached.
151f13193f5SChuck Lever */
152f13193f5SChuck Lever struct svc_rdma_chunk_ctxt {
1536787f0beSChuck Lever struct rpc_rdma_cid cc_cid;
154f13193f5SChuck Lever struct ib_cqe cc_cqe;
155f13193f5SChuck Lever struct svcxprt_rdma *cc_rdma;
156f13193f5SChuck Lever struct list_head cc_rwctxts;
15745f13584SChuck Lever ktime_t cc_posttime;
158f13193f5SChuck Lever int cc_sqecount;
1597d81ee87SChuck Lever enum ib_wc_status cc_status;
1607d81ee87SChuck Lever struct completion cc_done;
161f13193f5SChuck Lever };
162f13193f5SChuck Lever
svc_rdma_cc_cid_init(struct svcxprt_rdma * rdma,struct rpc_rdma_cid * cid)1636787f0beSChuck Lever static void svc_rdma_cc_cid_init(struct svcxprt_rdma *rdma,
1646787f0beSChuck Lever struct rpc_rdma_cid *cid)
1656787f0beSChuck Lever {
1666787f0beSChuck Lever cid->ci_queue_id = rdma->sc_sq_cq->res.id;
1676787f0beSChuck Lever cid->ci_completion_id = atomic_inc_return(&rdma->sc_completion_ids);
1686787f0beSChuck Lever }
1696787f0beSChuck Lever
svc_rdma_cc_init(struct svcxprt_rdma * rdma,struct svc_rdma_chunk_ctxt * cc)170f13193f5SChuck Lever static void svc_rdma_cc_init(struct svcxprt_rdma *rdma,
17135a30fc3SChuck Lever struct svc_rdma_chunk_ctxt *cc)
172f13193f5SChuck Lever {
1736787f0beSChuck Lever svc_rdma_cc_cid_init(rdma, &cc->cc_cid);
174f13193f5SChuck Lever cc->cc_rdma = rdma;
175f13193f5SChuck Lever
176f13193f5SChuck Lever INIT_LIST_HEAD(&cc->cc_rwctxts);
177f13193f5SChuck Lever cc->cc_sqecount = 0;
178f13193f5SChuck Lever }
179f13193f5SChuck Lever
18007a92d00SChuck Lever /*
18107a92d00SChuck Lever * The consumed rw_ctx's are cleaned and placed on a local llist so
18207a92d00SChuck Lever * that only one atomic llist operation is needed to put them all
18307a92d00SChuck Lever * back on the free list.
18407a92d00SChuck Lever */
svc_rdma_cc_release(struct svc_rdma_chunk_ctxt * cc,enum dma_data_direction dir)18535a30fc3SChuck Lever static void svc_rdma_cc_release(struct svc_rdma_chunk_ctxt *cc,
18635a30fc3SChuck Lever enum dma_data_direction dir)
187f13193f5SChuck Lever {
188f13193f5SChuck Lever struct svcxprt_rdma *rdma = cc->cc_rdma;
18907a92d00SChuck Lever struct llist_node *first, *last;
190f13193f5SChuck Lever struct svc_rdma_rw_ctxt *ctxt;
19107a92d00SChuck Lever LLIST_HEAD(free);
192f13193f5SChuck Lever
193a23c76e9SChuck Lever trace_svcrdma_cc_release(&cc->cc_cid, cc->cc_sqecount);
194a23c76e9SChuck Lever
19507a92d00SChuck Lever first = last = NULL;
196f13193f5SChuck Lever while ((ctxt = svc_rdma_next_ctxt(&cc->cc_rwctxts)) != NULL) {
197f13193f5SChuck Lever list_del(&ctxt->rw_list);
198f13193f5SChuck Lever
199f13193f5SChuck Lever rdma_rw_ctx_destroy(&ctxt->rw_ctx, rdma->sc_qp,
200f13193f5SChuck Lever rdma->sc_port_num, ctxt->rw_sg_table.sgl,
20135a30fc3SChuck Lever ctxt->rw_nents, dir);
202*b55c6333SChuck Lever __svc_rdma_put_rw_ctxt(ctxt, &free);
20307a92d00SChuck Lever
20407a92d00SChuck Lever ctxt->rw_node.next = first;
20507a92d00SChuck Lever first = &ctxt->rw_node;
20607a92d00SChuck Lever if (!last)
20707a92d00SChuck Lever last = first;
208f13193f5SChuck Lever }
20907a92d00SChuck Lever if (first)
21007a92d00SChuck Lever llist_add_batch(first, last, &rdma->sc_rw_ctxts);
211f13193f5SChuck Lever }
212f13193f5SChuck Lever
213f13193f5SChuck Lever /* State for sending a Write or Reply chunk.
214f13193f5SChuck Lever * - Tracks progress of writing one chunk over all its segments
215f13193f5SChuck Lever * - Stores arguments for the SGL constructor functions
216f13193f5SChuck Lever */
217f13193f5SChuck Lever struct svc_rdma_write_info {
2187a1cbfa1SChuck Lever const struct svc_rdma_chunk *wi_chunk;
2197a1cbfa1SChuck Lever
220f13193f5SChuck Lever /* write state of this chunk */
221f13193f5SChuck Lever unsigned int wi_seg_off;
222f13193f5SChuck Lever unsigned int wi_seg_no;
223f13193f5SChuck Lever
224f13193f5SChuck Lever /* SGL constructor arguments */
22551bad8ccSChuck Lever const struct xdr_buf *wi_xdr;
226f13193f5SChuck Lever unsigned char *wi_base;
227f13193f5SChuck Lever unsigned int wi_next_off;
228f13193f5SChuck Lever
229f13193f5SChuck Lever struct svc_rdma_chunk_ctxt wi_cc;
230f13193f5SChuck Lever };
231f13193f5SChuck Lever
232f13193f5SChuck Lever static struct svc_rdma_write_info *
svc_rdma_write_info_alloc(struct svcxprt_rdma * rdma,const struct svc_rdma_chunk * chunk)2337a1cbfa1SChuck Lever svc_rdma_write_info_alloc(struct svcxprt_rdma *rdma,
2347a1cbfa1SChuck Lever const struct svc_rdma_chunk *chunk)
235f13193f5SChuck Lever {
236f13193f5SChuck Lever struct svc_rdma_write_info *info;
237f13193f5SChuck Lever
238ac3c32bbSChuck Lever info = kmalloc_node(sizeof(*info), GFP_KERNEL,
239ac3c32bbSChuck Lever ibdev_to_node(rdma->sc_cm_id->device));
240f13193f5SChuck Lever if (!info)
241f13193f5SChuck Lever return info;
242f13193f5SChuck Lever
2437a1cbfa1SChuck Lever info->wi_chunk = chunk;
244f13193f5SChuck Lever info->wi_seg_off = 0;
245f13193f5SChuck Lever info->wi_seg_no = 0;
24635a30fc3SChuck Lever svc_rdma_cc_init(rdma, &info->wi_cc);
247026d958bSChuck Lever info->wi_cc.cc_cqe.done = svc_rdma_write_done;
248f13193f5SChuck Lever return info;
249f13193f5SChuck Lever }
250f13193f5SChuck Lever
svc_rdma_write_info_free(struct svc_rdma_write_info * info)251f13193f5SChuck Lever static void svc_rdma_write_info_free(struct svc_rdma_write_info *info)
252f13193f5SChuck Lever {
25335a30fc3SChuck Lever svc_rdma_cc_release(&info->wi_cc, DMA_TO_DEVICE);
254f13193f5SChuck Lever kfree(info);
255f13193f5SChuck Lever }
256f13193f5SChuck Lever
257f13193f5SChuck Lever /**
258f13193f5SChuck Lever * svc_rdma_write_done - Write chunk completion
259f13193f5SChuck Lever * @cq: controlling Completion Queue
260f13193f5SChuck Lever * @wc: Work Completion
261f13193f5SChuck Lever *
262f13193f5SChuck Lever * Pages under I/O are freed by a subsequent Send completion.
263f13193f5SChuck Lever */
svc_rdma_write_done(struct ib_cq * cq,struct ib_wc * wc)264f13193f5SChuck Lever static void svc_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
265f13193f5SChuck Lever {
266f13193f5SChuck Lever struct ib_cqe *cqe = wc->wr_cqe;
267f13193f5SChuck Lever struct svc_rdma_chunk_ctxt *cc =
268f13193f5SChuck Lever container_of(cqe, struct svc_rdma_chunk_ctxt, cc_cqe);
269f13193f5SChuck Lever struct svcxprt_rdma *rdma = cc->cc_rdma;
270f13193f5SChuck Lever struct svc_rdma_write_info *info =
271f13193f5SChuck Lever container_of(cc, struct svc_rdma_write_info, wi_cc);
272f13193f5SChuck Lever
27345f13584SChuck Lever switch (wc->status) {
27445f13584SChuck Lever case IB_WC_SUCCESS:
2756787f0beSChuck Lever trace_svcrdma_wc_write(wc, &cc->cc_cid);
27645f13584SChuck Lever break;
27745f13584SChuck Lever case IB_WC_WR_FLUSH_ERR:
27845f13584SChuck Lever trace_svcrdma_wc_write_flush(wc, &cc->cc_cid);
27945f13584SChuck Lever break;
28045f13584SChuck Lever default:
28145f13584SChuck Lever trace_svcrdma_wc_write_err(wc, &cc->cc_cid);
28245f13584SChuck Lever }
283bd2abef3SChuck Lever
2846c8c84f5SChuck Lever svc_rdma_wake_send_waiters(rdma, cc->cc_sqecount);
285f13193f5SChuck Lever
2868820bcaaSChuck Lever if (unlikely(wc->status != IB_WC_SUCCESS))
287e844d307SChuck Lever svc_xprt_deferred_close(&rdma->sc_xprt);
288f13193f5SChuck Lever
289f13193f5SChuck Lever svc_rdma_write_info_free(info);
290f13193f5SChuck Lever }
291f13193f5SChuck Lever
292026d958bSChuck Lever /* State for pulling a Read chunk.
293026d958bSChuck Lever */
294026d958bSChuck Lever struct svc_rdma_read_info {
295d96962e6SChuck Lever struct svc_rqst *ri_rqst;
296ecf85b23SChuck Lever struct svc_rdma_recv_ctxt *ri_readctxt;
297026d958bSChuck Lever unsigned int ri_pageno;
298026d958bSChuck Lever unsigned int ri_pageoff;
299bafe9c27SChuck Lever unsigned int ri_totalbytes;
300026d958bSChuck Lever
301026d958bSChuck Lever struct svc_rdma_chunk_ctxt ri_cc;
302026d958bSChuck Lever };
303026d958bSChuck Lever
304026d958bSChuck Lever static struct svc_rdma_read_info *
svc_rdma_read_info_alloc(struct svcxprt_rdma * rdma)305026d958bSChuck Lever svc_rdma_read_info_alloc(struct svcxprt_rdma *rdma)
306026d958bSChuck Lever {
307026d958bSChuck Lever struct svc_rdma_read_info *info;
308026d958bSChuck Lever
309ac3c32bbSChuck Lever info = kmalloc_node(sizeof(*info), GFP_KERNEL,
310ac3c32bbSChuck Lever ibdev_to_node(rdma->sc_cm_id->device));
311026d958bSChuck Lever if (!info)
312026d958bSChuck Lever return info;
313026d958bSChuck Lever
31435a30fc3SChuck Lever svc_rdma_cc_init(rdma, &info->ri_cc);
315026d958bSChuck Lever info->ri_cc.cc_cqe.done = svc_rdma_wc_read_done;
316026d958bSChuck Lever return info;
317026d958bSChuck Lever }
318026d958bSChuck Lever
svc_rdma_read_info_free(struct svc_rdma_read_info * info)319026d958bSChuck Lever static void svc_rdma_read_info_free(struct svc_rdma_read_info *info)
320026d958bSChuck Lever {
32135a30fc3SChuck Lever svc_rdma_cc_release(&info->ri_cc, DMA_FROM_DEVICE);
322026d958bSChuck Lever kfree(info);
323026d958bSChuck Lever }
324026d958bSChuck Lever
325026d958bSChuck Lever /**
326026d958bSChuck Lever * svc_rdma_wc_read_done - Handle completion of an RDMA Read ctx
327026d958bSChuck Lever * @cq: controlling Completion Queue
328026d958bSChuck Lever * @wc: Work Completion
329026d958bSChuck Lever *
330026d958bSChuck Lever */
svc_rdma_wc_read_done(struct ib_cq * cq,struct ib_wc * wc)331026d958bSChuck Lever static void svc_rdma_wc_read_done(struct ib_cq *cq, struct ib_wc *wc)
332026d958bSChuck Lever {
333026d958bSChuck Lever struct ib_cqe *cqe = wc->wr_cqe;
334026d958bSChuck Lever struct svc_rdma_chunk_ctxt *cc =
335026d958bSChuck Lever container_of(cqe, struct svc_rdma_chunk_ctxt, cc_cqe);
33645f13584SChuck Lever struct svc_rdma_read_info *info;
337026d958bSChuck Lever
33845f13584SChuck Lever switch (wc->status) {
33945f13584SChuck Lever case IB_WC_SUCCESS:
34045f13584SChuck Lever info = container_of(cc, struct svc_rdma_read_info, ri_cc);
34145f13584SChuck Lever trace_svcrdma_wc_read(wc, &cc->cc_cid, info->ri_totalbytes,
34245f13584SChuck Lever cc->cc_posttime);
34345f13584SChuck Lever break;
34445f13584SChuck Lever case IB_WC_WR_FLUSH_ERR:
34545f13584SChuck Lever trace_svcrdma_wc_read_flush(wc, &cc->cc_cid);
34645f13584SChuck Lever break;
34745f13584SChuck Lever default:
34845f13584SChuck Lever trace_svcrdma_wc_read_err(wc, &cc->cc_cid);
34945f13584SChuck Lever }
350bd2abef3SChuck Lever
35145f13584SChuck Lever svc_rdma_wake_send_waiters(cc->cc_rdma, cc->cc_sqecount);
3527d81ee87SChuck Lever cc->cc_status = wc->status;
3537d81ee87SChuck Lever complete(&cc->cc_done);
3547d81ee87SChuck Lever return;
355026d958bSChuck Lever }
356026d958bSChuck Lever
35791f8ce28SChuck Lever /*
358f13193f5SChuck Lever * Assumptions:
359f13193f5SChuck Lever * - If ib_post_send() succeeds, only one completion is expected,
360f13193f5SChuck Lever * even if one or more WRs are flushed. This is true when posting
361f13193f5SChuck Lever * an rdma_rw_ctx or when posting a single signaled WR.
362f13193f5SChuck Lever */
svc_rdma_post_chunk_ctxt(struct svc_rdma_chunk_ctxt * cc)363f13193f5SChuck Lever static int svc_rdma_post_chunk_ctxt(struct svc_rdma_chunk_ctxt *cc)
364f13193f5SChuck Lever {
365f13193f5SChuck Lever struct svcxprt_rdma *rdma = cc->cc_rdma;
366d34ac5cdSBart Van Assche struct ib_send_wr *first_wr;
367d34ac5cdSBart Van Assche const struct ib_send_wr *bad_wr;
368f13193f5SChuck Lever struct list_head *tmp;
369f13193f5SChuck Lever struct ib_cqe *cqe;
370f13193f5SChuck Lever int ret;
371f13193f5SChuck Lever
37291f8ce28SChuck Lever might_sleep();
37391f8ce28SChuck Lever
374107c1d0aSChuck Lever if (cc->cc_sqecount > rdma->sc_sq_depth)
375107c1d0aSChuck Lever return -EINVAL;
376107c1d0aSChuck Lever
377f13193f5SChuck Lever first_wr = NULL;
378f13193f5SChuck Lever cqe = &cc->cc_cqe;
379f13193f5SChuck Lever list_for_each(tmp, &cc->cc_rwctxts) {
380f13193f5SChuck Lever struct svc_rdma_rw_ctxt *ctxt;
381f13193f5SChuck Lever
382f13193f5SChuck Lever ctxt = list_entry(tmp, struct svc_rdma_rw_ctxt, rw_list);
383f13193f5SChuck Lever first_wr = rdma_rw_ctx_wrs(&ctxt->rw_ctx, rdma->sc_qp,
384f13193f5SChuck Lever rdma->sc_port_num, cqe, first_wr);
385f13193f5SChuck Lever cqe = NULL;
386f13193f5SChuck Lever }
387f13193f5SChuck Lever
388f13193f5SChuck Lever do {
389f13193f5SChuck Lever if (atomic_sub_return(cc->cc_sqecount,
390f13193f5SChuck Lever &rdma->sc_sq_avail) > 0) {
39145f13584SChuck Lever cc->cc_posttime = ktime_get();
3925d85a822SJason Gunthorpe ret = ib_post_send(rdma->sc_qp, first_wr, &bad_wr);
393f13193f5SChuck Lever if (ret)
394f13193f5SChuck Lever break;
395f13193f5SChuck Lever return 0;
396f13193f5SChuck Lever }
397f13193f5SChuck Lever
39822df5a22SChuck Lever percpu_counter_inc(&svcrdma_stat_sq_starve);
399bd2abef3SChuck Lever trace_svcrdma_sq_full(rdma);
400f13193f5SChuck Lever atomic_add(cc->cc_sqecount, &rdma->sc_sq_avail);
401f13193f5SChuck Lever wait_event(rdma->sc_send_wait,
402f13193f5SChuck Lever atomic_read(&rdma->sc_sq_avail) > cc->cc_sqecount);
403bd2abef3SChuck Lever trace_svcrdma_sq_retry(rdma);
404f13193f5SChuck Lever } while (1);
405f13193f5SChuck Lever
406e28b4fc6SChuck Lever trace_svcrdma_sq_post_err(rdma, ret);
407e844d307SChuck Lever svc_xprt_deferred_close(&rdma->sc_xprt);
408f13193f5SChuck Lever
409f13193f5SChuck Lever /* If even one was posted, there will be a completion. */
410f13193f5SChuck Lever if (bad_wr != first_wr)
411f13193f5SChuck Lever return 0;
412f13193f5SChuck Lever
413f13193f5SChuck Lever atomic_add(cc->cc_sqecount, &rdma->sc_sq_avail);
414f13193f5SChuck Lever wake_up(&rdma->sc_send_wait);
415f13193f5SChuck Lever return -ENOTCONN;
416f13193f5SChuck Lever }
417f13193f5SChuck Lever
418f13193f5SChuck Lever /* Build and DMA-map an SGL that covers one kvec in an xdr_buf
419f13193f5SChuck Lever */
svc_rdma_vec_to_sg(struct svc_rdma_write_info * info,unsigned int len,struct svc_rdma_rw_ctxt * ctxt)420f13193f5SChuck Lever static void svc_rdma_vec_to_sg(struct svc_rdma_write_info *info,
421f13193f5SChuck Lever unsigned int len,
422f13193f5SChuck Lever struct svc_rdma_rw_ctxt *ctxt)
423f13193f5SChuck Lever {
424f13193f5SChuck Lever struct scatterlist *sg = ctxt->rw_sg_table.sgl;
425f13193f5SChuck Lever
426f13193f5SChuck Lever sg_set_buf(&sg[0], info->wi_base, len);
427f13193f5SChuck Lever info->wi_base += len;
428f13193f5SChuck Lever
429f13193f5SChuck Lever ctxt->rw_nents = 1;
430f13193f5SChuck Lever }
431f13193f5SChuck Lever
432f13193f5SChuck Lever /* Build and DMA-map an SGL that covers part of an xdr_buf's pagelist.
433f13193f5SChuck Lever */
svc_rdma_pagelist_to_sg(struct svc_rdma_write_info * info,unsigned int remaining,struct svc_rdma_rw_ctxt * ctxt)434f13193f5SChuck Lever static void svc_rdma_pagelist_to_sg(struct svc_rdma_write_info *info,
435f13193f5SChuck Lever unsigned int remaining,
436f13193f5SChuck Lever struct svc_rdma_rw_ctxt *ctxt)
437f13193f5SChuck Lever {
438f13193f5SChuck Lever unsigned int sge_no, sge_bytes, page_off, page_no;
43951bad8ccSChuck Lever const struct xdr_buf *xdr = info->wi_xdr;
440f13193f5SChuck Lever struct scatterlist *sg;
441f13193f5SChuck Lever struct page **page;
442f13193f5SChuck Lever
44391b022ecSChuck Lever page_off = info->wi_next_off + xdr->page_base;
44491b022ecSChuck Lever page_no = page_off >> PAGE_SHIFT;
44591b022ecSChuck Lever page_off = offset_in_page(page_off);
446f13193f5SChuck Lever page = xdr->pages + page_no;
447f13193f5SChuck Lever info->wi_next_off += remaining;
448f13193f5SChuck Lever sg = ctxt->rw_sg_table.sgl;
449f13193f5SChuck Lever sge_no = 0;
450f13193f5SChuck Lever do {
451f13193f5SChuck Lever sge_bytes = min_t(unsigned int, remaining,
452f13193f5SChuck Lever PAGE_SIZE - page_off);
453f13193f5SChuck Lever sg_set_page(sg, *page, sge_bytes, page_off);
454f13193f5SChuck Lever
455f13193f5SChuck Lever remaining -= sge_bytes;
456f13193f5SChuck Lever sg = sg_next(sg);
457f13193f5SChuck Lever page_off = 0;
458f13193f5SChuck Lever sge_no++;
459f13193f5SChuck Lever page++;
460f13193f5SChuck Lever } while (remaining);
461f13193f5SChuck Lever
462f13193f5SChuck Lever ctxt->rw_nents = sge_no;
463f13193f5SChuck Lever }
464f13193f5SChuck Lever
465f13193f5SChuck Lever /* Construct RDMA Write WRs to send a portion of an xdr_buf containing
466f13193f5SChuck Lever * an RPC Reply.
467f13193f5SChuck Lever */
468f13193f5SChuck Lever static int
svc_rdma_build_writes(struct svc_rdma_write_info * info,void (* constructor)(struct svc_rdma_write_info * info,unsigned int len,struct svc_rdma_rw_ctxt * ctxt),unsigned int remaining)469f13193f5SChuck Lever svc_rdma_build_writes(struct svc_rdma_write_info *info,
470f13193f5SChuck Lever void (*constructor)(struct svc_rdma_write_info *info,
471f13193f5SChuck Lever unsigned int len,
472f13193f5SChuck Lever struct svc_rdma_rw_ctxt *ctxt),
473f13193f5SChuck Lever unsigned int remaining)
474f13193f5SChuck Lever {
475f13193f5SChuck Lever struct svc_rdma_chunk_ctxt *cc = &info->wi_cc;
476f13193f5SChuck Lever struct svcxprt_rdma *rdma = cc->cc_rdma;
4777a1cbfa1SChuck Lever const struct svc_rdma_segment *seg;
478f13193f5SChuck Lever struct svc_rdma_rw_ctxt *ctxt;
479f13193f5SChuck Lever int ret;
480f13193f5SChuck Lever
481f13193f5SChuck Lever do {
482f13193f5SChuck Lever unsigned int write_len;
483f60a0869SChuck Lever u64 offset;
484f13193f5SChuck Lever
485f012e95bSChuck Lever if (info->wi_seg_no >= info->wi_chunk->ch_segcount)
486f13193f5SChuck Lever goto out_overflow;
487f13193f5SChuck Lever
488f012e95bSChuck Lever seg = &info->wi_chunk->ch_segments[info->wi_seg_no];
4897a1cbfa1SChuck Lever write_len = min(remaining, seg->rs_length - info->wi_seg_off);
490e5decb2eSChuck Lever if (!write_len)
491e5decb2eSChuck Lever goto out_overflow;
492f13193f5SChuck Lever ctxt = svc_rdma_get_rw_ctxt(rdma,
493f13193f5SChuck Lever (write_len >> PAGE_SHIFT) + 2);
494f13193f5SChuck Lever if (!ctxt)
495f4e53e1cSChuck Lever return -ENOMEM;
496f13193f5SChuck Lever
497f13193f5SChuck Lever constructor(info, write_len, ctxt);
4987a1cbfa1SChuck Lever offset = seg->rs_offset + info->wi_seg_off;
4997a1cbfa1SChuck Lever ret = svc_rdma_rw_ctx_init(rdma, ctxt, offset, seg->rs_handle,
5002abfbe7eSChuck Lever DMA_TO_DEVICE);
501f13193f5SChuck Lever if (ret < 0)
5022abfbe7eSChuck Lever return -EIO;
5031e7e5573SChuck Lever percpu_counter_inc(&svcrdma_stat_write);
504f13193f5SChuck Lever
505f13193f5SChuck Lever list_add(&ctxt->rw_list, &cc->cc_rwctxts);
506f13193f5SChuck Lever cc->cc_sqecount += ret;
5077a1cbfa1SChuck Lever if (write_len == seg->rs_length - info->wi_seg_off) {
508f13193f5SChuck Lever info->wi_seg_no++;
509f13193f5SChuck Lever info->wi_seg_off = 0;
510f13193f5SChuck Lever } else {
511f13193f5SChuck Lever info->wi_seg_off += write_len;
512f13193f5SChuck Lever }
513f13193f5SChuck Lever remaining -= write_len;
514f13193f5SChuck Lever } while (remaining);
515f13193f5SChuck Lever
516f13193f5SChuck Lever return 0;
517f13193f5SChuck Lever
518f13193f5SChuck Lever out_overflow:
519dbc17acdSChuck Lever trace_svcrdma_small_wrch_err(rdma, remaining, info->wi_seg_no,
5207a1cbfa1SChuck Lever info->wi_chunk->ch_segcount);
521f13193f5SChuck Lever return -E2BIG;
522f13193f5SChuck Lever }
523f13193f5SChuck Lever
524ab1394eeSChuck Lever /**
525ab1394eeSChuck Lever * svc_rdma_iov_write - Construct RDMA Writes from an iov
526ab1394eeSChuck Lever * @info: pointer to write arguments
527ab1394eeSChuck Lever * @iov: kvec to write
528ab1394eeSChuck Lever *
529ab1394eeSChuck Lever * Returns:
530d5029525SZheng Yongjun * On success, returns zero
531ab1394eeSChuck Lever * %-E2BIG if the client-provided Write chunk is too small
532ab1394eeSChuck Lever * %-ENOMEM if a resource has been exhausted
533ab1394eeSChuck Lever * %-EIO if an rdma-rw error occurred
534f13193f5SChuck Lever */
svc_rdma_iov_write(struct svc_rdma_write_info * info,const struct kvec * iov)535ab1394eeSChuck Lever static int svc_rdma_iov_write(struct svc_rdma_write_info *info,
536ab1394eeSChuck Lever const struct kvec *iov)
537f13193f5SChuck Lever {
538ab1394eeSChuck Lever info->wi_base = iov->iov_base;
539f13193f5SChuck Lever return svc_rdma_build_writes(info, svc_rdma_vec_to_sg,
540ab1394eeSChuck Lever iov->iov_len);
541f13193f5SChuck Lever }
542f13193f5SChuck Lever
543ab1394eeSChuck Lever /**
544ab1394eeSChuck Lever * svc_rdma_pages_write - Construct RDMA Writes from pages
545ab1394eeSChuck Lever * @info: pointer to write arguments
546ab1394eeSChuck Lever * @xdr: xdr_buf with pages to write
547ab1394eeSChuck Lever * @offset: offset into the content of @xdr
548ab1394eeSChuck Lever * @length: number of bytes to write
549ab1394eeSChuck Lever *
550ab1394eeSChuck Lever * Returns:
551d5029525SZheng Yongjun * On success, returns zero
552ab1394eeSChuck Lever * %-E2BIG if the client-provided Write chunk is too small
553ab1394eeSChuck Lever * %-ENOMEM if a resource has been exhausted
554ab1394eeSChuck Lever * %-EIO if an rdma-rw error occurred
555f13193f5SChuck Lever */
svc_rdma_pages_write(struct svc_rdma_write_info * info,const struct xdr_buf * xdr,unsigned int offset,unsigned long length)556ab1394eeSChuck Lever static int svc_rdma_pages_write(struct svc_rdma_write_info *info,
557ab1394eeSChuck Lever const struct xdr_buf *xdr,
55841205539SChuck Lever unsigned int offset,
55941205539SChuck Lever unsigned long length)
560f13193f5SChuck Lever {
561f13193f5SChuck Lever info->wi_xdr = xdr;
56241205539SChuck Lever info->wi_next_off = offset - xdr->head[0].iov_len;
563f13193f5SChuck Lever return svc_rdma_build_writes(info, svc_rdma_pagelist_to_sg,
56441205539SChuck Lever length);
565f13193f5SChuck Lever }
566f13193f5SChuck Lever
567f13193f5SChuck Lever /**
568f6ad7759SChuck Lever * svc_rdma_xb_write - Construct RDMA Writes to write an xdr_buf
569f6ad7759SChuck Lever * @xdr: xdr_buf to write
57041bc163fSChuck Lever * @data: pointer to write arguments
571f6ad7759SChuck Lever *
572f6ad7759SChuck Lever * Returns:
573d5029525SZheng Yongjun * On success, returns zero
574f6ad7759SChuck Lever * %-E2BIG if the client-provided Write chunk is too small
575f6ad7759SChuck Lever * %-ENOMEM if a resource has been exhausted
576f6ad7759SChuck Lever * %-EIO if an rdma-rw error occurred
577f6ad7759SChuck Lever */
svc_rdma_xb_write(const struct xdr_buf * xdr,void * data)57841bc163fSChuck Lever static int svc_rdma_xb_write(const struct xdr_buf *xdr, void *data)
579f6ad7759SChuck Lever {
58041bc163fSChuck Lever struct svc_rdma_write_info *info = data;
581f6ad7759SChuck Lever int ret;
582f6ad7759SChuck Lever
583f6ad7759SChuck Lever if (xdr->head[0].iov_len) {
584f6ad7759SChuck Lever ret = svc_rdma_iov_write(info, &xdr->head[0]);
585f6ad7759SChuck Lever if (ret < 0)
586f6ad7759SChuck Lever return ret;
587f6ad7759SChuck Lever }
588f6ad7759SChuck Lever
589f6ad7759SChuck Lever if (xdr->page_len) {
590f6ad7759SChuck Lever ret = svc_rdma_pages_write(info, xdr, xdr->head[0].iov_len,
591f6ad7759SChuck Lever xdr->page_len);
592f6ad7759SChuck Lever if (ret < 0)
593f6ad7759SChuck Lever return ret;
594f6ad7759SChuck Lever }
595f6ad7759SChuck Lever
596f6ad7759SChuck Lever if (xdr->tail[0].iov_len) {
597f6ad7759SChuck Lever ret = svc_rdma_iov_write(info, &xdr->tail[0]);
598f6ad7759SChuck Lever if (ret < 0)
599f6ad7759SChuck Lever return ret;
600f6ad7759SChuck Lever }
601f6ad7759SChuck Lever
602f6ad7759SChuck Lever return xdr->len;
603f6ad7759SChuck Lever }
604f6ad7759SChuck Lever
605f6ad7759SChuck Lever /**
606f13193f5SChuck Lever * svc_rdma_send_write_chunk - Write all segments in a Write chunk
607f13193f5SChuck Lever * @rdma: controlling RDMA transport
6087a1cbfa1SChuck Lever * @chunk: Write chunk provided by the client
609f13193f5SChuck Lever * @xdr: xdr_buf containing the data payload
610f13193f5SChuck Lever *
611f13193f5SChuck Lever * Returns a non-negative number of bytes the chunk consumed, or
612f13193f5SChuck Lever * %-E2BIG if the payload was larger than the Write chunk,
613107c1d0aSChuck Lever * %-EINVAL if client provided too many segments,
614f13193f5SChuck Lever * %-ENOMEM if rdma_rw context pool was exhausted,
615f13193f5SChuck Lever * %-ENOTCONN if posting failed (connection is lost),
616f13193f5SChuck Lever * %-EIO if rdma_rw initialization failed (DMA mapping, etc).
617f13193f5SChuck Lever */
svc_rdma_send_write_chunk(struct svcxprt_rdma * rdma,const struct svc_rdma_chunk * chunk,const struct xdr_buf * xdr)6187a1cbfa1SChuck Lever int svc_rdma_send_write_chunk(struct svcxprt_rdma *rdma,
6197a1cbfa1SChuck Lever const struct svc_rdma_chunk *chunk,
620f6ad7759SChuck Lever const struct xdr_buf *xdr)
621f13193f5SChuck Lever {
622f13193f5SChuck Lever struct svc_rdma_write_info *info;
623b704be09SChuck Lever struct svc_rdma_chunk_ctxt *cc;
624f13193f5SChuck Lever int ret;
625f13193f5SChuck Lever
6267a1cbfa1SChuck Lever info = svc_rdma_write_info_alloc(rdma, chunk);
627f13193f5SChuck Lever if (!info)
628f13193f5SChuck Lever return -ENOMEM;
629b704be09SChuck Lever cc = &info->wi_cc;
630f13193f5SChuck Lever
631f6ad7759SChuck Lever ret = svc_rdma_xb_write(xdr, info);
632f6ad7759SChuck Lever if (ret != xdr->len)
633f13193f5SChuck Lever goto out_err;
634f13193f5SChuck Lever
635b704be09SChuck Lever trace_svcrdma_post_write_chunk(&cc->cc_cid, cc->cc_sqecount);
636b704be09SChuck Lever ret = svc_rdma_post_chunk_ctxt(cc);
637f13193f5SChuck Lever if (ret < 0)
638f13193f5SChuck Lever goto out_err;
639f6ad7759SChuck Lever return xdr->len;
640f13193f5SChuck Lever
641f13193f5SChuck Lever out_err:
642f13193f5SChuck Lever svc_rdma_write_info_free(info);
643f13193f5SChuck Lever return ret;
644f13193f5SChuck Lever }
645f13193f5SChuck Lever
646f13193f5SChuck Lever /**
647f13193f5SChuck Lever * svc_rdma_send_reply_chunk - Write all segments in the Reply chunk
648f13193f5SChuck Lever * @rdma: controlling RDMA transport
6496fa5785eSChuck Lever * @rctxt: Write and Reply chunks from client
650f13193f5SChuck Lever * @xdr: xdr_buf containing an RPC Reply
651f13193f5SChuck Lever *
652f13193f5SChuck Lever * Returns a non-negative number of bytes the chunk consumed, or
653f13193f5SChuck Lever * %-E2BIG if the payload was larger than the Reply chunk,
654107c1d0aSChuck Lever * %-EINVAL if client provided too many segments,
655f13193f5SChuck Lever * %-ENOMEM if rdma_rw context pool was exhausted,
656f13193f5SChuck Lever * %-ENOTCONN if posting failed (connection is lost),
657f13193f5SChuck Lever * %-EIO if rdma_rw initialization failed (DMA mapping, etc).
658f13193f5SChuck Lever */
svc_rdma_send_reply_chunk(struct svcxprt_rdma * rdma,const struct svc_rdma_recv_ctxt * rctxt,const struct xdr_buf * xdr)6596fa5785eSChuck Lever int svc_rdma_send_reply_chunk(struct svcxprt_rdma *rdma,
6606fa5785eSChuck Lever const struct svc_rdma_recv_ctxt *rctxt,
66141bc163fSChuck Lever const struct xdr_buf *xdr)
662f13193f5SChuck Lever {
663f13193f5SChuck Lever struct svc_rdma_write_info *info;
664b704be09SChuck Lever struct svc_rdma_chunk_ctxt *cc;
6657a1cbfa1SChuck Lever struct svc_rdma_chunk *chunk;
66641bc163fSChuck Lever int ret;
667f13193f5SChuck Lever
6687a1cbfa1SChuck Lever if (pcl_is_empty(&rctxt->rc_reply_pcl))
669ded380f1SChuck Lever return 0;
670ded380f1SChuck Lever
6717a1cbfa1SChuck Lever chunk = pcl_first_chunk(&rctxt->rc_reply_pcl);
6727a1cbfa1SChuck Lever info = svc_rdma_write_info_alloc(rdma, chunk);
673f13193f5SChuck Lever if (!info)
674f13193f5SChuck Lever return -ENOMEM;
675b704be09SChuck Lever cc = &info->wi_cc;
676f13193f5SChuck Lever
67741bc163fSChuck Lever ret = pcl_process_nonpayloads(&rctxt->rc_write_pcl, xdr,
67841bc163fSChuck Lever svc_rdma_xb_write, info);
679f13193f5SChuck Lever if (ret < 0)
680f13193f5SChuck Lever goto out_err;
681f13193f5SChuck Lever
682b704be09SChuck Lever trace_svcrdma_post_reply_chunk(&cc->cc_cid, cc->cc_sqecount);
683b704be09SChuck Lever ret = svc_rdma_post_chunk_ctxt(cc);
684f13193f5SChuck Lever if (ret < 0)
685f13193f5SChuck Lever goto out_err;
68698895edbSChuck Lever
68741bc163fSChuck Lever return xdr->len;
688f13193f5SChuck Lever
689f13193f5SChuck Lever out_err:
690f13193f5SChuck Lever svc_rdma_write_info_free(info);
691f13193f5SChuck Lever return ret;
692f13193f5SChuck Lever }
693026d958bSChuck Lever
694d96962e6SChuck Lever /**
695d96962e6SChuck Lever * svc_rdma_build_read_segment - Build RDMA Read WQEs to pull one RDMA segment
696d96962e6SChuck Lever * @info: context for ongoing I/O
697d96962e6SChuck Lever * @segment: co-ordinates of remote memory to be read
698d96962e6SChuck Lever *
699d96962e6SChuck Lever * Returns:
700d96962e6SChuck Lever * %0: the Read WR chain was constructed successfully
701d96962e6SChuck Lever * %-EINVAL: there were not enough rq_pages to finish
702d96962e6SChuck Lever * %-ENOMEM: allocating a local resources failed
703d96962e6SChuck Lever * %-EIO: a DMA mapping error occurred
704d96962e6SChuck Lever */
svc_rdma_build_read_segment(struct svc_rdma_read_info * info,const struct svc_rdma_segment * segment)705026d958bSChuck Lever static int svc_rdma_build_read_segment(struct svc_rdma_read_info *info,
706d96962e6SChuck Lever const struct svc_rdma_segment *segment)
707026d958bSChuck Lever {
708ecf85b23SChuck Lever struct svc_rdma_recv_ctxt *head = info->ri_readctxt;
709026d958bSChuck Lever struct svc_rdma_chunk_ctxt *cc = &info->ri_cc;
710d96962e6SChuck Lever struct svc_rqst *rqstp = info->ri_rqst;
711d96962e6SChuck Lever unsigned int sge_no, seg_len, len;
7127d81ee87SChuck Lever struct svc_rdma_rw_ctxt *ctxt;
713026d958bSChuck Lever struct scatterlist *sg;
714026d958bSChuck Lever int ret;
715026d958bSChuck Lever
716d96962e6SChuck Lever len = segment->rs_length;
717026d958bSChuck Lever sge_no = PAGE_ALIGN(info->ri_pageoff + len) >> PAGE_SHIFT;
718026d958bSChuck Lever ctxt = svc_rdma_get_rw_ctxt(cc->cc_rdma, sge_no);
719026d958bSChuck Lever if (!ctxt)
720f4e53e1cSChuck Lever return -ENOMEM;
721026d958bSChuck Lever ctxt->rw_nents = sge_no;
722026d958bSChuck Lever
723026d958bSChuck Lever sg = ctxt->rw_sg_table.sgl;
724026d958bSChuck Lever for (sge_no = 0; sge_no < ctxt->rw_nents; sge_no++) {
725026d958bSChuck Lever seg_len = min_t(unsigned int, len,
726026d958bSChuck Lever PAGE_SIZE - info->ri_pageoff);
727026d958bSChuck Lever
728026d958bSChuck Lever if (!info->ri_pageoff)
729ecf85b23SChuck Lever head->rc_page_count++;
730026d958bSChuck Lever
731026d958bSChuck Lever sg_set_page(sg, rqstp->rq_pages[info->ri_pageno],
732026d958bSChuck Lever seg_len, info->ri_pageoff);
733026d958bSChuck Lever sg = sg_next(sg);
734026d958bSChuck Lever
735026d958bSChuck Lever info->ri_pageoff += seg_len;
736026d958bSChuck Lever if (info->ri_pageoff == PAGE_SIZE) {
737026d958bSChuck Lever info->ri_pageno++;
738026d958bSChuck Lever info->ri_pageoff = 0;
739026d958bSChuck Lever }
740026d958bSChuck Lever len -= seg_len;
741026d958bSChuck Lever
742026d958bSChuck Lever /* Safety check */
743026d958bSChuck Lever if (len &&
744026d958bSChuck Lever &rqstp->rq_pages[info->ri_pageno + 1] > rqstp->rq_page_end)
745026d958bSChuck Lever goto out_overrun;
746026d958bSChuck Lever }
747026d958bSChuck Lever
748d96962e6SChuck Lever ret = svc_rdma_rw_ctx_init(cc->cc_rdma, ctxt, segment->rs_offset,
749d96962e6SChuck Lever segment->rs_handle, DMA_FROM_DEVICE);
750026d958bSChuck Lever if (ret < 0)
7512abfbe7eSChuck Lever return -EIO;
7521e7e5573SChuck Lever percpu_counter_inc(&svcrdma_stat_read);
753026d958bSChuck Lever
754026d958bSChuck Lever list_add(&ctxt->rw_list, &cc->cc_rwctxts);
755026d958bSChuck Lever cc->cc_sqecount += ret;
756026d958bSChuck Lever return 0;
757026d958bSChuck Lever
758026d958bSChuck Lever out_overrun:
7599d200638SChuck Lever trace_svcrdma_page_overrun_err(cc->cc_rdma, rqstp, info->ri_pageno);
760026d958bSChuck Lever return -EINVAL;
761026d958bSChuck Lever }
762026d958bSChuck Lever
763d96962e6SChuck Lever /**
764d96962e6SChuck Lever * svc_rdma_build_read_chunk - Build RDMA Read WQEs to pull one RDMA chunk
765d96962e6SChuck Lever * @info: context for ongoing I/O
766d96962e6SChuck Lever * @chunk: Read chunk to pull
767d96962e6SChuck Lever *
768d96962e6SChuck Lever * Return values:
769d96962e6SChuck Lever * %0: the Read WR chain was constructed successfully
770d96962e6SChuck Lever * %-EINVAL: there were not enough resources to finish
771d96962e6SChuck Lever * %-ENOMEM: allocating a local resources failed
772d96962e6SChuck Lever * %-EIO: a DMA mapping error occurred
7737075a867SChuck Lever */
svc_rdma_build_read_chunk(struct svc_rdma_read_info * info,const struct svc_rdma_chunk * chunk)774d96962e6SChuck Lever static int svc_rdma_build_read_chunk(struct svc_rdma_read_info *info,
775d96962e6SChuck Lever const struct svc_rdma_chunk *chunk)
776026d958bSChuck Lever {
777d96962e6SChuck Lever const struct svc_rdma_segment *segment;
778026d958bSChuck Lever int ret;
779026d958bSChuck Lever
7807075a867SChuck Lever ret = -EINVAL;
781d96962e6SChuck Lever pcl_for_each_segment(segment, chunk) {
782d96962e6SChuck Lever ret = svc_rdma_build_read_segment(info, segment);
783026d958bSChuck Lever if (ret < 0)
784026d958bSChuck Lever break;
785d96962e6SChuck Lever info->ri_totalbytes += segment->rs_length;
786026d958bSChuck Lever }
787026d958bSChuck Lever return ret;
788026d958bSChuck Lever }
789026d958bSChuck Lever
790d96962e6SChuck Lever /**
791d7cc7397SChuck Lever * svc_rdma_copy_inline_range - Copy part of the inline content into pages
792d7cc7397SChuck Lever * @info: context for RDMA Reads
793d7cc7397SChuck Lever * @offset: offset into the Receive buffer of region to copy
794d7cc7397SChuck Lever * @remaining: length of region to copy
795d7cc7397SChuck Lever *
796d7cc7397SChuck Lever * Take a page at a time from rqstp->rq_pages and copy the inline
797d7cc7397SChuck Lever * content from the Receive buffer into that page. Update
798d7cc7397SChuck Lever * info->ri_pageno and info->ri_pageoff so that the next RDMA Read
799d7cc7397SChuck Lever * result will land contiguously with the copied content.
800d7cc7397SChuck Lever *
801d7cc7397SChuck Lever * Return values:
802d7cc7397SChuck Lever * %0: Inline content was successfully copied
803d7cc7397SChuck Lever * %-EINVAL: offset or length was incorrect
804d7cc7397SChuck Lever */
svc_rdma_copy_inline_range(struct svc_rdma_read_info * info,unsigned int offset,unsigned int remaining)805d7cc7397SChuck Lever static int svc_rdma_copy_inline_range(struct svc_rdma_read_info *info,
806d7cc7397SChuck Lever unsigned int offset,
807d7cc7397SChuck Lever unsigned int remaining)
808d7cc7397SChuck Lever {
809d7cc7397SChuck Lever struct svc_rdma_recv_ctxt *head = info->ri_readctxt;
810d7cc7397SChuck Lever unsigned char *dst, *src = head->rc_recv_buf;
811d7cc7397SChuck Lever struct svc_rqst *rqstp = info->ri_rqst;
812d7cc7397SChuck Lever unsigned int page_no, numpages;
813d7cc7397SChuck Lever
814d7cc7397SChuck Lever numpages = PAGE_ALIGN(info->ri_pageoff + remaining) >> PAGE_SHIFT;
815d7cc7397SChuck Lever for (page_no = 0; page_no < numpages; page_no++) {
816d7cc7397SChuck Lever unsigned int page_len;
817d7cc7397SChuck Lever
818d7cc7397SChuck Lever page_len = min_t(unsigned int, remaining,
819d7cc7397SChuck Lever PAGE_SIZE - info->ri_pageoff);
820d7cc7397SChuck Lever
821d7cc7397SChuck Lever if (!info->ri_pageoff)
822d7cc7397SChuck Lever head->rc_page_count++;
823d7cc7397SChuck Lever
8247d81ee87SChuck Lever dst = page_address(rqstp->rq_pages[info->ri_pageno]);
825d7cc7397SChuck Lever memcpy(dst + info->ri_pageno, src + offset, page_len);
826d7cc7397SChuck Lever
827d7cc7397SChuck Lever info->ri_totalbytes += page_len;
828d7cc7397SChuck Lever info->ri_pageoff += page_len;
829d7cc7397SChuck Lever if (info->ri_pageoff == PAGE_SIZE) {
830d7cc7397SChuck Lever info->ri_pageno++;
831d7cc7397SChuck Lever info->ri_pageoff = 0;
832d7cc7397SChuck Lever }
833d7cc7397SChuck Lever remaining -= page_len;
834d7cc7397SChuck Lever offset += page_len;
835d7cc7397SChuck Lever }
836d7cc7397SChuck Lever
837d7cc7397SChuck Lever return -EINVAL;
838d7cc7397SChuck Lever }
839d7cc7397SChuck Lever
840d7cc7397SChuck Lever /**
841d7cc7397SChuck Lever * svc_rdma_read_multiple_chunks - Construct RDMA Reads to pull data item Read chunks
842d7cc7397SChuck Lever * @info: context for RDMA Reads
843d7cc7397SChuck Lever *
8447d81ee87SChuck Lever * The chunk data lands in rqstp->rq_arg as a series of contiguous pages,
845d7cc7397SChuck Lever * like an incoming TCP call.
846d7cc7397SChuck Lever *
847d7cc7397SChuck Lever * Return values:
848d7cc7397SChuck Lever * %0: RDMA Read WQEs were successfully built
849d7cc7397SChuck Lever * %-EINVAL: client provided too many chunks or segments,
850d7cc7397SChuck Lever * %-ENOMEM: rdma_rw context pool was exhausted,
851d7cc7397SChuck Lever * %-ENOTCONN: posting failed (connection is lost),
852d7cc7397SChuck Lever * %-EIO: rdma_rw initialization failed (DMA mapping, etc).
853d7cc7397SChuck Lever */
svc_rdma_read_multiple_chunks(struct svc_rdma_read_info * info)854d7cc7397SChuck Lever static noinline int svc_rdma_read_multiple_chunks(struct svc_rdma_read_info *info)
855d7cc7397SChuck Lever {
856d7cc7397SChuck Lever struct svc_rdma_recv_ctxt *head = info->ri_readctxt;
857d7cc7397SChuck Lever const struct svc_rdma_pcl *pcl = &head->rc_read_pcl;
8587d81ee87SChuck Lever struct xdr_buf *buf = &info->ri_rqst->rq_arg;
859d7cc7397SChuck Lever struct svc_rdma_chunk *chunk, *next;
860d7cc7397SChuck Lever unsigned int start, length;
861d7cc7397SChuck Lever int ret;
862d7cc7397SChuck Lever
863d7cc7397SChuck Lever start = 0;
864d7cc7397SChuck Lever chunk = pcl_first_chunk(pcl);
865d7cc7397SChuck Lever length = chunk->ch_position;
866d7cc7397SChuck Lever ret = svc_rdma_copy_inline_range(info, start, length);
867d7cc7397SChuck Lever if (ret < 0)
868d7cc7397SChuck Lever return ret;
869d7cc7397SChuck Lever
870d7cc7397SChuck Lever pcl_for_each_chunk(chunk, pcl) {
871d7cc7397SChuck Lever ret = svc_rdma_build_read_chunk(info, chunk);
872d7cc7397SChuck Lever if (ret < 0)
873d7cc7397SChuck Lever return ret;
874d7cc7397SChuck Lever
875d7cc7397SChuck Lever next = pcl_next_chunk(pcl, chunk);
876d7cc7397SChuck Lever if (!next)
877d7cc7397SChuck Lever break;
878d7cc7397SChuck Lever
879d7cc7397SChuck Lever start += length;
880d7cc7397SChuck Lever length = next->ch_position - info->ri_totalbytes;
881d7cc7397SChuck Lever ret = svc_rdma_copy_inline_range(info, start, length);
882d7cc7397SChuck Lever if (ret < 0)
883d7cc7397SChuck Lever return ret;
884d7cc7397SChuck Lever }
885d7cc7397SChuck Lever
886d7cc7397SChuck Lever start += length;
887d7cc7397SChuck Lever length = head->rc_byte_len - start;
888d7cc7397SChuck Lever ret = svc_rdma_copy_inline_range(info, start, length);
889d7cc7397SChuck Lever if (ret < 0)
890d7cc7397SChuck Lever return ret;
891d7cc7397SChuck Lever
892d7cc7397SChuck Lever buf->len += info->ri_totalbytes;
893d7cc7397SChuck Lever buf->buflen += info->ri_totalbytes;
894d7cc7397SChuck Lever
8957d81ee87SChuck Lever buf->head[0].iov_base = page_address(info->ri_rqst->rq_pages[0]);
896d7cc7397SChuck Lever buf->head[0].iov_len = min_t(size_t, PAGE_SIZE, info->ri_totalbytes);
8977d81ee87SChuck Lever buf->pages = &info->ri_rqst->rq_pages[1];
898d7cc7397SChuck Lever buf->page_len = info->ri_totalbytes - buf->head[0].iov_len;
899d7cc7397SChuck Lever return 0;
900d7cc7397SChuck Lever }
901d7cc7397SChuck Lever
902d7cc7397SChuck Lever /**
903d7cc7397SChuck Lever * svc_rdma_read_data_item - Construct RDMA Reads to pull data item Read chunks
904d96962e6SChuck Lever * @info: context for RDMA Reads
905d96962e6SChuck Lever *
9067d81ee87SChuck Lever * The chunk data lands in the page list of rqstp->rq_arg.pages.
907026d958bSChuck Lever *
9087d81ee87SChuck Lever * Currently NFSD does not look at the rqstp->rq_arg.tail[0] kvec.
909026d958bSChuck Lever * Therefore, XDR round-up of the Read chunk and trailing
910026d958bSChuck Lever * inline content must both be added at the end of the pagelist.
911d96962e6SChuck Lever *
912d96962e6SChuck Lever * Return values:
913d96962e6SChuck Lever * %0: RDMA Read WQEs were successfully built
914d96962e6SChuck Lever * %-EINVAL: client provided too many chunks or segments,
915d96962e6SChuck Lever * %-ENOMEM: rdma_rw context pool was exhausted,
916d96962e6SChuck Lever * %-ENOTCONN: posting failed (connection is lost),
917d96962e6SChuck Lever * %-EIO: rdma_rw initialization failed (DMA mapping, etc).
918026d958bSChuck Lever */
svc_rdma_read_data_item(struct svc_rdma_read_info * info)919d7cc7397SChuck Lever static int svc_rdma_read_data_item(struct svc_rdma_read_info *info)
920026d958bSChuck Lever {
921ecf85b23SChuck Lever struct svc_rdma_recv_ctxt *head = info->ri_readctxt;
9227d81ee87SChuck Lever struct xdr_buf *buf = &info->ri_rqst->rq_arg;
923d96962e6SChuck Lever struct svc_rdma_chunk *chunk;
924bafe9c27SChuck Lever unsigned int length;
925026d958bSChuck Lever int ret;
926026d958bSChuck Lever
927d96962e6SChuck Lever chunk = pcl_first_chunk(&head->rc_read_pcl);
928d96962e6SChuck Lever ret = svc_rdma_build_read_chunk(info, chunk);
929026d958bSChuck Lever if (ret < 0)
930026d958bSChuck Lever goto out;
931026d958bSChuck Lever
932193bcb7bSChuck Lever /* Split the Receive buffer between the head and tail
933193bcb7bSChuck Lever * buffers at Read chunk's position. XDR roundup of the
934193bcb7bSChuck Lever * chunk is not included in either the pagelist or in
935193bcb7bSChuck Lever * the tail.
936026d958bSChuck Lever */
937d96962e6SChuck Lever buf->tail[0].iov_base = buf->head[0].iov_base + chunk->ch_position;
938d96962e6SChuck Lever buf->tail[0].iov_len = buf->head[0].iov_len - chunk->ch_position;
939d96962e6SChuck Lever buf->head[0].iov_len = chunk->ch_position;
940026d958bSChuck Lever
941175e0310SChuck Lever /* Read chunk may need XDR roundup (see RFC 8166, s. 3.4.5.2).
942193bcb7bSChuck Lever *
943175e0310SChuck Lever * If the client already rounded up the chunk length, the
944175e0310SChuck Lever * length does not change. Otherwise, the length of the page
945175e0310SChuck Lever * list is increased to include XDR round-up.
946175e0310SChuck Lever *
947175e0310SChuck Lever * Currently these chunks always start at page offset 0,
948175e0310SChuck Lever * thus the rounded-up length never crosses a page boundary.
949026d958bSChuck Lever */
9507d81ee87SChuck Lever buf->pages = &info->ri_rqst->rq_pages[0];
9517d81ee87SChuck Lever length = xdr_align_size(chunk->ch_length);
952bafe9c27SChuck Lever buf->page_len = length;
953bafe9c27SChuck Lever buf->len += length;
954bafe9c27SChuck Lever buf->buflen += length;
955026d958bSChuck Lever
956026d958bSChuck Lever out:
957026d958bSChuck Lever return ret;
958026d958bSChuck Lever }
959026d958bSChuck Lever
960d96962e6SChuck Lever /**
961d7cc7397SChuck Lever * svc_rdma_read_chunk_range - Build RDMA Read WQEs for portion of a chunk
962d7cc7397SChuck Lever * @info: context for RDMA Reads
963d7cc7397SChuck Lever * @chunk: parsed Call chunk to pull
964d7cc7397SChuck Lever * @offset: offset of region to pull
965d7cc7397SChuck Lever * @length: length of region to pull
966d7cc7397SChuck Lever *
967d7cc7397SChuck Lever * Return values:
968d7cc7397SChuck Lever * %0: RDMA Read WQEs were successfully built
969d7cc7397SChuck Lever * %-EINVAL: there were not enough resources to finish
970d7cc7397SChuck Lever * %-ENOMEM: rdma_rw context pool was exhausted,
971d7cc7397SChuck Lever * %-ENOTCONN: posting failed (connection is lost),
972d7cc7397SChuck Lever * %-EIO: rdma_rw initialization failed (DMA mapping, etc).
973d7cc7397SChuck Lever */
svc_rdma_read_chunk_range(struct svc_rdma_read_info * info,const struct svc_rdma_chunk * chunk,unsigned int offset,unsigned int length)974d7cc7397SChuck Lever static int svc_rdma_read_chunk_range(struct svc_rdma_read_info *info,
975d7cc7397SChuck Lever const struct svc_rdma_chunk *chunk,
976d7cc7397SChuck Lever unsigned int offset, unsigned int length)
977d7cc7397SChuck Lever {
978d7cc7397SChuck Lever const struct svc_rdma_segment *segment;
979d7cc7397SChuck Lever int ret;
980d7cc7397SChuck Lever
981d7cc7397SChuck Lever ret = -EINVAL;
982d7cc7397SChuck Lever pcl_for_each_segment(segment, chunk) {
983d7cc7397SChuck Lever struct svc_rdma_segment dummy;
984d7cc7397SChuck Lever
985d7cc7397SChuck Lever if (offset > segment->rs_length) {
986d7cc7397SChuck Lever offset -= segment->rs_length;
987d7cc7397SChuck Lever continue;
988d7cc7397SChuck Lever }
989d7cc7397SChuck Lever
990d7cc7397SChuck Lever dummy.rs_handle = segment->rs_handle;
991d7cc7397SChuck Lever dummy.rs_length = min_t(u32, length, segment->rs_length) - offset;
992d7cc7397SChuck Lever dummy.rs_offset = segment->rs_offset + offset;
993d7cc7397SChuck Lever
994d7cc7397SChuck Lever ret = svc_rdma_build_read_segment(info, &dummy);
995d7cc7397SChuck Lever if (ret < 0)
996d7cc7397SChuck Lever break;
997d7cc7397SChuck Lever
998d7cc7397SChuck Lever info->ri_totalbytes += dummy.rs_length;
999d7cc7397SChuck Lever length -= dummy.rs_length;
1000d7cc7397SChuck Lever offset = 0;
1001d7cc7397SChuck Lever }
1002d7cc7397SChuck Lever return ret;
1003d7cc7397SChuck Lever }
1004d7cc7397SChuck Lever
1005d7cc7397SChuck Lever /**
1006d7cc7397SChuck Lever * svc_rdma_read_call_chunk - Build RDMA Read WQEs to pull a Long Message
1007d7cc7397SChuck Lever * @info: context for RDMA Reads
1008d7cc7397SChuck Lever *
1009d7cc7397SChuck Lever * Return values:
1010d7cc7397SChuck Lever * %0: RDMA Read WQEs were successfully built
1011d7cc7397SChuck Lever * %-EINVAL: there were not enough resources to finish
1012d7cc7397SChuck Lever * %-ENOMEM: rdma_rw context pool was exhausted,
1013d7cc7397SChuck Lever * %-ENOTCONN: posting failed (connection is lost),
1014d7cc7397SChuck Lever * %-EIO: rdma_rw initialization failed (DMA mapping, etc).
1015d7cc7397SChuck Lever */
svc_rdma_read_call_chunk(struct svc_rdma_read_info * info)1016d7cc7397SChuck Lever static int svc_rdma_read_call_chunk(struct svc_rdma_read_info *info)
1017d7cc7397SChuck Lever {
1018d7cc7397SChuck Lever struct svc_rdma_recv_ctxt *head = info->ri_readctxt;
1019d7cc7397SChuck Lever const struct svc_rdma_chunk *call_chunk =
1020d7cc7397SChuck Lever pcl_first_chunk(&head->rc_call_pcl);
1021d7cc7397SChuck Lever const struct svc_rdma_pcl *pcl = &head->rc_read_pcl;
1022d7cc7397SChuck Lever struct svc_rdma_chunk *chunk, *next;
1023d7cc7397SChuck Lever unsigned int start, length;
1024d7cc7397SChuck Lever int ret;
1025d7cc7397SChuck Lever
1026d7cc7397SChuck Lever if (pcl_is_empty(pcl))
1027d7cc7397SChuck Lever return svc_rdma_build_read_chunk(info, call_chunk);
1028d7cc7397SChuck Lever
1029d7cc7397SChuck Lever start = 0;
1030d7cc7397SChuck Lever chunk = pcl_first_chunk(pcl);
1031d7cc7397SChuck Lever length = chunk->ch_position;
1032d7cc7397SChuck Lever ret = svc_rdma_read_chunk_range(info, call_chunk, start, length);
1033d7cc7397SChuck Lever if (ret < 0)
1034d7cc7397SChuck Lever return ret;
1035d7cc7397SChuck Lever
1036d7cc7397SChuck Lever pcl_for_each_chunk(chunk, pcl) {
1037d7cc7397SChuck Lever ret = svc_rdma_build_read_chunk(info, chunk);
1038d7cc7397SChuck Lever if (ret < 0)
1039d7cc7397SChuck Lever return ret;
1040d7cc7397SChuck Lever
1041d7cc7397SChuck Lever next = pcl_next_chunk(pcl, chunk);
1042d7cc7397SChuck Lever if (!next)
1043d7cc7397SChuck Lever break;
1044d7cc7397SChuck Lever
1045d7cc7397SChuck Lever start += length;
1046d7cc7397SChuck Lever length = next->ch_position - info->ri_totalbytes;
1047d7cc7397SChuck Lever ret = svc_rdma_read_chunk_range(info, call_chunk,
1048d7cc7397SChuck Lever start, length);
1049d7cc7397SChuck Lever if (ret < 0)
1050d7cc7397SChuck Lever return ret;
1051d7cc7397SChuck Lever }
1052d7cc7397SChuck Lever
1053d7cc7397SChuck Lever start += length;
1054d7cc7397SChuck Lever length = call_chunk->ch_length - start;
1055d7cc7397SChuck Lever return svc_rdma_read_chunk_range(info, call_chunk, start, length);
1056d7cc7397SChuck Lever }
1057d7cc7397SChuck Lever
1058d7cc7397SChuck Lever /**
1059d96962e6SChuck Lever * svc_rdma_read_special - Build RDMA Read WQEs to pull a Long Message
1060d96962e6SChuck Lever * @info: context for RDMA Reads
1061d96962e6SChuck Lever *
1062d96962e6SChuck Lever * The start of the data lands in the first page just after the
10637d81ee87SChuck Lever * Transport header, and the rest lands in rqstp->rq_arg.pages.
1064026d958bSChuck Lever *
1065026d958bSChuck Lever * Assumptions:
1066d96962e6SChuck Lever * - A PZRC is never sent in an RDMA_MSG message, though it's
1067d96962e6SChuck Lever * allowed by spec.
1068d96962e6SChuck Lever *
1069d96962e6SChuck Lever * Return values:
1070d96962e6SChuck Lever * %0: RDMA Read WQEs were successfully built
1071d96962e6SChuck Lever * %-EINVAL: client provided too many chunks or segments,
1072d96962e6SChuck Lever * %-ENOMEM: rdma_rw context pool was exhausted,
1073d96962e6SChuck Lever * %-ENOTCONN: posting failed (connection is lost),
1074d96962e6SChuck Lever * %-EIO: rdma_rw initialization failed (DMA mapping, etc).
1075026d958bSChuck Lever */
svc_rdma_read_special(struct svc_rdma_read_info * info)1076d7cc7397SChuck Lever static noinline int svc_rdma_read_special(struct svc_rdma_read_info *info)
1077026d958bSChuck Lever {
10787d81ee87SChuck Lever struct xdr_buf *buf = &info->ri_rqst->rq_arg;
1079026d958bSChuck Lever int ret;
1080026d958bSChuck Lever
1081d7cc7397SChuck Lever ret = svc_rdma_read_call_chunk(info);
1082026d958bSChuck Lever if (ret < 0)
1083026d958bSChuck Lever goto out;
1084026d958bSChuck Lever
1085bafe9c27SChuck Lever buf->len += info->ri_totalbytes;
1086bafe9c27SChuck Lever buf->buflen += info->ri_totalbytes;
1087026d958bSChuck Lever
10887d81ee87SChuck Lever buf->head[0].iov_base = page_address(info->ri_rqst->rq_pages[0]);
1089bafe9c27SChuck Lever buf->head[0].iov_len = min_t(size_t, PAGE_SIZE, info->ri_totalbytes);
10907d81ee87SChuck Lever buf->pages = &info->ri_rqst->rq_pages[1];
1091bafe9c27SChuck Lever buf->page_len = info->ri_totalbytes - buf->head[0].iov_len;
1092026d958bSChuck Lever
1093026d958bSChuck Lever out:
1094026d958bSChuck Lever return ret;
1095026d958bSChuck Lever }
1096026d958bSChuck Lever
1097026d958bSChuck Lever /**
1098d96962e6SChuck Lever * svc_rdma_process_read_list - Pull list of Read chunks from the client
1099026d958bSChuck Lever * @rdma: controlling RDMA transport
1100026d958bSChuck Lever * @rqstp: set of pages to use as Read sink buffers
1101026d958bSChuck Lever * @head: pages under I/O collect here
1102026d958bSChuck Lever *
1103d96962e6SChuck Lever * The RPC/RDMA protocol assumes that the upper layer's XDR decoders
1104d96962e6SChuck Lever * pull each Read chunk as they decode an incoming RPC message.
1105026d958bSChuck Lever *
1106d96962e6SChuck Lever * On Linux, however, the server needs to have a fully-constructed RPC
1107d96962e6SChuck Lever * message in rqstp->rq_arg when there is a positive return code from
1108d96962e6SChuck Lever * ->xpo_recvfrom. So the Read list is safety-checked immediately when
1109d96962e6SChuck Lever * it is received, then here the whole Read list is pulled all at once.
1110d96962e6SChuck Lever * The ingress RPC message is fully reconstructed once all associated
1111d96962e6SChuck Lever * RDMA Reads have completed.
1112d96962e6SChuck Lever *
1113d96962e6SChuck Lever * Return values:
1114d96962e6SChuck Lever * %1: all needed RDMA Reads were posted successfully,
1115d96962e6SChuck Lever * %-EINVAL: client provided too many chunks or segments,
1116d96962e6SChuck Lever * %-ENOMEM: rdma_rw context pool was exhausted,
1117d96962e6SChuck Lever * %-ENOTCONN: posting failed (connection is lost),
1118d96962e6SChuck Lever * %-EIO: rdma_rw initialization failed (DMA mapping, etc).
1119026d958bSChuck Lever */
svc_rdma_process_read_list(struct svcxprt_rdma * rdma,struct svc_rqst * rqstp,struct svc_rdma_recv_ctxt * head)1120d96962e6SChuck Lever int svc_rdma_process_read_list(struct svcxprt_rdma *rdma,
1121d96962e6SChuck Lever struct svc_rqst *rqstp,
1122d96962e6SChuck Lever struct svc_rdma_recv_ctxt *head)
1123026d958bSChuck Lever {
1124026d958bSChuck Lever struct svc_rdma_read_info *info;
1125b704be09SChuck Lever struct svc_rdma_chunk_ctxt *cc;
1126026d958bSChuck Lever int ret;
1127026d958bSChuck Lever
1128026d958bSChuck Lever info = svc_rdma_read_info_alloc(rdma);
1129026d958bSChuck Lever if (!info)
1130026d958bSChuck Lever return -ENOMEM;
1131b704be09SChuck Lever cc = &info->ri_cc;
1132d96962e6SChuck Lever info->ri_rqst = rqstp;
1133026d958bSChuck Lever info->ri_readctxt = head;
11343316f063SChuck Lever info->ri_pageno = 0;
11353316f063SChuck Lever info->ri_pageoff = 0;
1136bafe9c27SChuck Lever info->ri_totalbytes = 0;
1137026d958bSChuck Lever
1138d7cc7397SChuck Lever if (pcl_is_empty(&head->rc_call_pcl)) {
1139d7cc7397SChuck Lever if (head->rc_read_pcl.cl_count == 1)
1140d7cc7397SChuck Lever ret = svc_rdma_read_data_item(info);
1141026d958bSChuck Lever else
1142d7cc7397SChuck Lever ret = svc_rdma_read_multiple_chunks(info);
1143d7cc7397SChuck Lever } else
1144d96962e6SChuck Lever ret = svc_rdma_read_special(info);
1145026d958bSChuck Lever if (ret < 0)
114607d0ff3bSChuck Lever goto out_err;
1147026d958bSChuck Lever
1148b704be09SChuck Lever trace_svcrdma_post_read_chunk(&cc->cc_cid, cc->cc_sqecount);
11497d81ee87SChuck Lever init_completion(&cc->cc_done);
1150b704be09SChuck Lever ret = svc_rdma_post_chunk_ctxt(cc);
1151026d958bSChuck Lever if (ret < 0)
115207d0ff3bSChuck Lever goto out_err;
11537d81ee87SChuck Lever
11547d81ee87SChuck Lever ret = 1;
11557d81ee87SChuck Lever wait_for_completion(&cc->cc_done);
11567d81ee87SChuck Lever if (cc->cc_status != IB_WC_SUCCESS)
11577d81ee87SChuck Lever ret = -EIO;
11587d81ee87SChuck Lever
11597d81ee87SChuck Lever /* rq_respages starts after the last arg page */
11607d81ee87SChuck Lever rqstp->rq_respages = &rqstp->rq_pages[head->rc_page_count];
11617d81ee87SChuck Lever rqstp->rq_next_page = rqstp->rq_respages + 1;
11627d81ee87SChuck Lever
11637d81ee87SChuck Lever /* Ensure svc_rdma_recv_ctxt_put() does not try to release pages */
11647d81ee87SChuck Lever head->rc_page_count = 0;
116507d0ff3bSChuck Lever
116607d0ff3bSChuck Lever out_err:
1167026d958bSChuck Lever svc_rdma_read_info_free(info);
1168026d958bSChuck Lever return ret;
1169026d958bSChuck Lever }
1170