xref: /openbmc/linux/net/sunrpc/xprtrdma/svc_rdma_rw.c (revision a23c76e92d8215456ca2fbd5cf6c1ff71b744c1d)
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 *
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 *
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 
8707a92d00SChuck Lever static void __svc_rdma_put_rw_ctxt(struct svcxprt_rdma *rdma,
8807a92d00SChuck Lever 				   struct svc_rdma_rw_ctxt *ctxt,
8907a92d00SChuck Lever 				   struct llist_head *list)
9007a92d00SChuck Lever {
9107a92d00SChuck Lever 	sg_free_table_chained(&ctxt->rw_sg_table, SG_CHUNK_SIZE);
9207a92d00SChuck Lever 	llist_add(&ctxt->rw_node, list);
9307a92d00SChuck Lever }
9407a92d00SChuck Lever 
95f13193f5SChuck Lever static void svc_rdma_put_rw_ctxt(struct svcxprt_rdma *rdma,
96f13193f5SChuck Lever 				 struct svc_rdma_rw_ctxt *ctxt)
97f13193f5SChuck Lever {
9807a92d00SChuck Lever 	__svc_rdma_put_rw_ctxt(rdma, ctxt, &rdma->sc_rw_ctxts);
99f13193f5SChuck Lever }
100f13193f5SChuck Lever 
101f13193f5SChuck Lever /**
102f13193f5SChuck Lever  * svc_rdma_destroy_rw_ctxts - Free accumulated R/W contexts
103f13193f5SChuck Lever  * @rdma: transport about to be destroyed
104f13193f5SChuck Lever  *
105f13193f5SChuck Lever  */
106f13193f5SChuck Lever void svc_rdma_destroy_rw_ctxts(struct svcxprt_rdma *rdma)
107f13193f5SChuck Lever {
108f13193f5SChuck Lever 	struct svc_rdma_rw_ctxt *ctxt;
10907a92d00SChuck Lever 	struct llist_node *node;
110f13193f5SChuck Lever 
11107a92d00SChuck Lever 	while ((node = llist_del_first(&rdma->sc_rw_ctxts)) != NULL) {
11207a92d00SChuck Lever 		ctxt = llist_entry(node, struct svc_rdma_rw_ctxt, rw_node);
113f13193f5SChuck Lever 		kfree(ctxt);
114f13193f5SChuck Lever 	}
115f13193f5SChuck Lever }
116f13193f5SChuck Lever 
1172abfbe7eSChuck Lever /**
1182abfbe7eSChuck Lever  * svc_rdma_rw_ctx_init - Prepare a R/W context for I/O
1192abfbe7eSChuck Lever  * @rdma: controlling transport instance
1202abfbe7eSChuck Lever  * @ctxt: R/W context to prepare
1212abfbe7eSChuck Lever  * @offset: RDMA offset
1222abfbe7eSChuck Lever  * @handle: RDMA tag/handle
1232abfbe7eSChuck Lever  * @direction: I/O direction
1242abfbe7eSChuck Lever  *
1252abfbe7eSChuck Lever  * Returns on success, the number of WQEs that will be needed
1262abfbe7eSChuck Lever  * on the workqueue, or a negative errno.
1272abfbe7eSChuck Lever  */
1282abfbe7eSChuck Lever static int svc_rdma_rw_ctx_init(struct svcxprt_rdma *rdma,
1292abfbe7eSChuck Lever 				struct svc_rdma_rw_ctxt *ctxt,
1302abfbe7eSChuck Lever 				u64 offset, u32 handle,
1312abfbe7eSChuck Lever 				enum dma_data_direction direction)
1322abfbe7eSChuck Lever {
1332abfbe7eSChuck Lever 	int ret;
1342abfbe7eSChuck Lever 
1352abfbe7eSChuck Lever 	ret = rdma_rw_ctx_init(&ctxt->rw_ctx, rdma->sc_qp, rdma->sc_port_num,
1362abfbe7eSChuck Lever 			       ctxt->rw_sg_table.sgl, ctxt->rw_nents,
1372abfbe7eSChuck Lever 			       0, offset, handle, direction);
1382abfbe7eSChuck Lever 	if (unlikely(ret < 0)) {
1392abfbe7eSChuck Lever 		svc_rdma_put_rw_ctxt(rdma, ctxt);
1402abfbe7eSChuck Lever 		trace_svcrdma_dma_map_rw_err(rdma, ctxt->rw_nents, ret);
1412abfbe7eSChuck Lever 	}
1422abfbe7eSChuck Lever 	return ret;
1432abfbe7eSChuck Lever }
1442abfbe7eSChuck Lever 
145f13193f5SChuck Lever /* A chunk context tracks all I/O for moving one Read or Write
1461cc5213bSRandy Dunlap  * chunk. This is a set of rdma_rw's that handle data movement
147f13193f5SChuck Lever  * for all segments of one chunk.
148f13193f5SChuck Lever  *
149f13193f5SChuck Lever  * These are small, acquired with a single allocator call, and
150f13193f5SChuck Lever  * no more than one is needed per chunk. They are allocated on
151f13193f5SChuck Lever  * demand, and not cached.
152f13193f5SChuck Lever  */
153f13193f5SChuck Lever struct svc_rdma_chunk_ctxt {
1546787f0beSChuck Lever 	struct rpc_rdma_cid	cc_cid;
155f13193f5SChuck Lever 	struct ib_cqe		cc_cqe;
156f13193f5SChuck Lever 	struct svcxprt_rdma	*cc_rdma;
157f13193f5SChuck Lever 	struct list_head	cc_rwctxts;
15845f13584SChuck Lever 	ktime_t			cc_posttime;
159f13193f5SChuck Lever 	int			cc_sqecount;
1607d81ee87SChuck Lever 	enum ib_wc_status	cc_status;
1617d81ee87SChuck Lever 	struct completion	cc_done;
162f13193f5SChuck Lever };
163f13193f5SChuck Lever 
1646787f0beSChuck Lever static void svc_rdma_cc_cid_init(struct svcxprt_rdma *rdma,
1656787f0beSChuck Lever 				 struct rpc_rdma_cid *cid)
1666787f0beSChuck Lever {
1676787f0beSChuck Lever 	cid->ci_queue_id = rdma->sc_sq_cq->res.id;
1686787f0beSChuck Lever 	cid->ci_completion_id = atomic_inc_return(&rdma->sc_completion_ids);
1696787f0beSChuck Lever }
1706787f0beSChuck Lever 
171f13193f5SChuck Lever static void svc_rdma_cc_init(struct svcxprt_rdma *rdma,
17235a30fc3SChuck Lever 			     struct svc_rdma_chunk_ctxt *cc)
173f13193f5SChuck Lever {
1746787f0beSChuck Lever 	svc_rdma_cc_cid_init(rdma, &cc->cc_cid);
175f13193f5SChuck Lever 	cc->cc_rdma = rdma;
176f13193f5SChuck Lever 
177f13193f5SChuck Lever 	INIT_LIST_HEAD(&cc->cc_rwctxts);
178f13193f5SChuck Lever 	cc->cc_sqecount = 0;
179f13193f5SChuck Lever }
180f13193f5SChuck Lever 
18107a92d00SChuck Lever /*
18207a92d00SChuck Lever  * The consumed rw_ctx's are cleaned and placed on a local llist so
18307a92d00SChuck Lever  * that only one atomic llist operation is needed to put them all
18407a92d00SChuck Lever  * back on the free list.
18507a92d00SChuck Lever  */
18635a30fc3SChuck Lever static void svc_rdma_cc_release(struct svc_rdma_chunk_ctxt *cc,
18735a30fc3SChuck Lever 				enum dma_data_direction dir)
188f13193f5SChuck Lever {
189f13193f5SChuck Lever 	struct svcxprt_rdma *rdma = cc->cc_rdma;
19007a92d00SChuck Lever 	struct llist_node *first, *last;
191f13193f5SChuck Lever 	struct svc_rdma_rw_ctxt *ctxt;
19207a92d00SChuck Lever 	LLIST_HEAD(free);
193f13193f5SChuck Lever 
194*a23c76e9SChuck Lever 	trace_svcrdma_cc_release(&cc->cc_cid, cc->cc_sqecount);
195*a23c76e9SChuck Lever 
19607a92d00SChuck Lever 	first = last = NULL;
197f13193f5SChuck Lever 	while ((ctxt = svc_rdma_next_ctxt(&cc->cc_rwctxts)) != NULL) {
198f13193f5SChuck Lever 		list_del(&ctxt->rw_list);
199f13193f5SChuck Lever 
200f13193f5SChuck Lever 		rdma_rw_ctx_destroy(&ctxt->rw_ctx, rdma->sc_qp,
201f13193f5SChuck Lever 				    rdma->sc_port_num, ctxt->rw_sg_table.sgl,
20235a30fc3SChuck Lever 				    ctxt->rw_nents, dir);
20307a92d00SChuck Lever 		__svc_rdma_put_rw_ctxt(rdma, ctxt, &free);
20407a92d00SChuck Lever 
20507a92d00SChuck Lever 		ctxt->rw_node.next = first;
20607a92d00SChuck Lever 		first = &ctxt->rw_node;
20707a92d00SChuck Lever 		if (!last)
20807a92d00SChuck Lever 			last = first;
209f13193f5SChuck Lever 	}
21007a92d00SChuck Lever 	if (first)
21107a92d00SChuck Lever 		llist_add_batch(first, last, &rdma->sc_rw_ctxts);
212f13193f5SChuck Lever }
213f13193f5SChuck Lever 
214f13193f5SChuck Lever /* State for sending a Write or Reply chunk.
215f13193f5SChuck Lever  *  - Tracks progress of writing one chunk over all its segments
216f13193f5SChuck Lever  *  - Stores arguments for the SGL constructor functions
217f13193f5SChuck Lever  */
218f13193f5SChuck Lever struct svc_rdma_write_info {
2197a1cbfa1SChuck Lever 	const struct svc_rdma_chunk	*wi_chunk;
2207a1cbfa1SChuck Lever 
221f13193f5SChuck Lever 	/* write state of this chunk */
222f13193f5SChuck Lever 	unsigned int		wi_seg_off;
223f13193f5SChuck Lever 	unsigned int		wi_seg_no;
224f13193f5SChuck Lever 
225f13193f5SChuck Lever 	/* SGL constructor arguments */
22651bad8ccSChuck Lever 	const struct xdr_buf	*wi_xdr;
227f13193f5SChuck Lever 	unsigned char		*wi_base;
228f13193f5SChuck Lever 	unsigned int		wi_next_off;
229f13193f5SChuck Lever 
230f13193f5SChuck Lever 	struct svc_rdma_chunk_ctxt	wi_cc;
231f13193f5SChuck Lever };
232f13193f5SChuck Lever 
233f13193f5SChuck Lever static struct svc_rdma_write_info *
2347a1cbfa1SChuck Lever svc_rdma_write_info_alloc(struct svcxprt_rdma *rdma,
2357a1cbfa1SChuck Lever 			  const struct svc_rdma_chunk *chunk)
236f13193f5SChuck Lever {
237f13193f5SChuck Lever 	struct svc_rdma_write_info *info;
238f13193f5SChuck Lever 
239ac3c32bbSChuck Lever 	info = kmalloc_node(sizeof(*info), GFP_KERNEL,
240ac3c32bbSChuck Lever 			    ibdev_to_node(rdma->sc_cm_id->device));
241f13193f5SChuck Lever 	if (!info)
242f13193f5SChuck Lever 		return info;
243f13193f5SChuck Lever 
2447a1cbfa1SChuck Lever 	info->wi_chunk = chunk;
245f13193f5SChuck Lever 	info->wi_seg_off = 0;
246f13193f5SChuck Lever 	info->wi_seg_no = 0;
24735a30fc3SChuck Lever 	svc_rdma_cc_init(rdma, &info->wi_cc);
248026d958bSChuck Lever 	info->wi_cc.cc_cqe.done = svc_rdma_write_done;
249f13193f5SChuck Lever 	return info;
250f13193f5SChuck Lever }
251f13193f5SChuck Lever 
252f13193f5SChuck Lever static void svc_rdma_write_info_free(struct svc_rdma_write_info *info)
253f13193f5SChuck Lever {
25435a30fc3SChuck Lever 	svc_rdma_cc_release(&info->wi_cc, DMA_TO_DEVICE);
255f13193f5SChuck Lever 	kfree(info);
256f13193f5SChuck Lever }
257f13193f5SChuck Lever 
258f13193f5SChuck Lever /**
259f13193f5SChuck Lever  * svc_rdma_write_done - Write chunk completion
260f13193f5SChuck Lever  * @cq: controlling Completion Queue
261f13193f5SChuck Lever  * @wc: Work Completion
262f13193f5SChuck Lever  *
263f13193f5SChuck Lever  * Pages under I/O are freed by a subsequent Send completion.
264f13193f5SChuck Lever  */
265f13193f5SChuck Lever static void svc_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
266f13193f5SChuck Lever {
267f13193f5SChuck Lever 	struct ib_cqe *cqe = wc->wr_cqe;
268f13193f5SChuck Lever 	struct svc_rdma_chunk_ctxt *cc =
269f13193f5SChuck Lever 			container_of(cqe, struct svc_rdma_chunk_ctxt, cc_cqe);
270f13193f5SChuck Lever 	struct svcxprt_rdma *rdma = cc->cc_rdma;
271f13193f5SChuck Lever 	struct svc_rdma_write_info *info =
272f13193f5SChuck Lever 			container_of(cc, struct svc_rdma_write_info, wi_cc);
273f13193f5SChuck Lever 
27445f13584SChuck Lever 	switch (wc->status) {
27545f13584SChuck Lever 	case IB_WC_SUCCESS:
2766787f0beSChuck Lever 		trace_svcrdma_wc_write(wc, &cc->cc_cid);
27745f13584SChuck Lever 		break;
27845f13584SChuck Lever 	case IB_WC_WR_FLUSH_ERR:
27945f13584SChuck Lever 		trace_svcrdma_wc_write_flush(wc, &cc->cc_cid);
28045f13584SChuck Lever 		break;
28145f13584SChuck Lever 	default:
28245f13584SChuck Lever 		trace_svcrdma_wc_write_err(wc, &cc->cc_cid);
28345f13584SChuck Lever 	}
284bd2abef3SChuck Lever 
2856c8c84f5SChuck Lever 	svc_rdma_wake_send_waiters(rdma, cc->cc_sqecount);
286f13193f5SChuck Lever 
2878820bcaaSChuck Lever 	if (unlikely(wc->status != IB_WC_SUCCESS))
288e844d307SChuck Lever 		svc_xprt_deferred_close(&rdma->sc_xprt);
289f13193f5SChuck Lever 
290f13193f5SChuck Lever 	svc_rdma_write_info_free(info);
291f13193f5SChuck Lever }
292f13193f5SChuck Lever 
293026d958bSChuck Lever /* State for pulling a Read chunk.
294026d958bSChuck Lever  */
295026d958bSChuck Lever struct svc_rdma_read_info {
296d96962e6SChuck Lever 	struct svc_rqst			*ri_rqst;
297ecf85b23SChuck Lever 	struct svc_rdma_recv_ctxt	*ri_readctxt;
298026d958bSChuck Lever 	unsigned int			ri_pageno;
299026d958bSChuck Lever 	unsigned int			ri_pageoff;
300bafe9c27SChuck Lever 	unsigned int			ri_totalbytes;
301026d958bSChuck Lever 
302026d958bSChuck Lever 	struct svc_rdma_chunk_ctxt	ri_cc;
303026d958bSChuck Lever };
304026d958bSChuck Lever 
305026d958bSChuck Lever static struct svc_rdma_read_info *
306026d958bSChuck Lever svc_rdma_read_info_alloc(struct svcxprt_rdma *rdma)
307026d958bSChuck Lever {
308026d958bSChuck Lever 	struct svc_rdma_read_info *info;
309026d958bSChuck Lever 
310ac3c32bbSChuck Lever 	info = kmalloc_node(sizeof(*info), GFP_KERNEL,
311ac3c32bbSChuck Lever 			    ibdev_to_node(rdma->sc_cm_id->device));
312026d958bSChuck Lever 	if (!info)
313026d958bSChuck Lever 		return info;
314026d958bSChuck Lever 
31535a30fc3SChuck Lever 	svc_rdma_cc_init(rdma, &info->ri_cc);
316026d958bSChuck Lever 	info->ri_cc.cc_cqe.done = svc_rdma_wc_read_done;
317026d958bSChuck Lever 	return info;
318026d958bSChuck Lever }
319026d958bSChuck Lever 
320026d958bSChuck Lever static void svc_rdma_read_info_free(struct svc_rdma_read_info *info)
321026d958bSChuck Lever {
32235a30fc3SChuck Lever 	svc_rdma_cc_release(&info->ri_cc, DMA_FROM_DEVICE);
323026d958bSChuck Lever 	kfree(info);
324026d958bSChuck Lever }
325026d958bSChuck Lever 
326026d958bSChuck Lever /**
327026d958bSChuck Lever  * svc_rdma_wc_read_done - Handle completion of an RDMA Read ctx
328026d958bSChuck Lever  * @cq: controlling Completion Queue
329026d958bSChuck Lever  * @wc: Work Completion
330026d958bSChuck Lever  *
331026d958bSChuck Lever  */
332026d958bSChuck Lever static void svc_rdma_wc_read_done(struct ib_cq *cq, struct ib_wc *wc)
333026d958bSChuck Lever {
334026d958bSChuck Lever 	struct ib_cqe *cqe = wc->wr_cqe;
335026d958bSChuck Lever 	struct svc_rdma_chunk_ctxt *cc =
336026d958bSChuck Lever 			container_of(cqe, struct svc_rdma_chunk_ctxt, cc_cqe);
33745f13584SChuck Lever 	struct svc_rdma_read_info *info;
338026d958bSChuck Lever 
33945f13584SChuck Lever 	switch (wc->status) {
34045f13584SChuck Lever 	case IB_WC_SUCCESS:
34145f13584SChuck Lever 		info = container_of(cc, struct svc_rdma_read_info, ri_cc);
34245f13584SChuck Lever 		trace_svcrdma_wc_read(wc, &cc->cc_cid, info->ri_totalbytes,
34345f13584SChuck Lever 				      cc->cc_posttime);
34445f13584SChuck Lever 		break;
34545f13584SChuck Lever 	case IB_WC_WR_FLUSH_ERR:
34645f13584SChuck Lever 		trace_svcrdma_wc_read_flush(wc, &cc->cc_cid);
34745f13584SChuck Lever 		break;
34845f13584SChuck Lever 	default:
34945f13584SChuck Lever 		trace_svcrdma_wc_read_err(wc, &cc->cc_cid);
35045f13584SChuck Lever 	}
351bd2abef3SChuck Lever 
35245f13584SChuck Lever 	svc_rdma_wake_send_waiters(cc->cc_rdma, cc->cc_sqecount);
3537d81ee87SChuck Lever 	cc->cc_status = wc->status;
3547d81ee87SChuck Lever 	complete(&cc->cc_done);
3557d81ee87SChuck Lever 	return;
356026d958bSChuck Lever }
357026d958bSChuck Lever 
35891f8ce28SChuck Lever /*
359f13193f5SChuck Lever  * Assumptions:
360f13193f5SChuck Lever  * - If ib_post_send() succeeds, only one completion is expected,
361f13193f5SChuck Lever  *   even if one or more WRs are flushed. This is true when posting
362f13193f5SChuck Lever  *   an rdma_rw_ctx or when posting a single signaled WR.
363f13193f5SChuck Lever  */
364f13193f5SChuck Lever static int svc_rdma_post_chunk_ctxt(struct svc_rdma_chunk_ctxt *cc)
365f13193f5SChuck Lever {
366f13193f5SChuck Lever 	struct svcxprt_rdma *rdma = cc->cc_rdma;
367d34ac5cdSBart Van Assche 	struct ib_send_wr *first_wr;
368d34ac5cdSBart Van Assche 	const struct ib_send_wr *bad_wr;
369f13193f5SChuck Lever 	struct list_head *tmp;
370f13193f5SChuck Lever 	struct ib_cqe *cqe;
371f13193f5SChuck Lever 	int ret;
372f13193f5SChuck Lever 
37391f8ce28SChuck Lever 	might_sleep();
37491f8ce28SChuck Lever 
375107c1d0aSChuck Lever 	if (cc->cc_sqecount > rdma->sc_sq_depth)
376107c1d0aSChuck Lever 		return -EINVAL;
377107c1d0aSChuck Lever 
378f13193f5SChuck Lever 	first_wr = NULL;
379f13193f5SChuck Lever 	cqe = &cc->cc_cqe;
380f13193f5SChuck Lever 	list_for_each(tmp, &cc->cc_rwctxts) {
381f13193f5SChuck Lever 		struct svc_rdma_rw_ctxt *ctxt;
382f13193f5SChuck Lever 
383f13193f5SChuck Lever 		ctxt = list_entry(tmp, struct svc_rdma_rw_ctxt, rw_list);
384f13193f5SChuck Lever 		first_wr = rdma_rw_ctx_wrs(&ctxt->rw_ctx, rdma->sc_qp,
385f13193f5SChuck Lever 					   rdma->sc_port_num, cqe, first_wr);
386f13193f5SChuck Lever 		cqe = NULL;
387f13193f5SChuck Lever 	}
388f13193f5SChuck Lever 
389f13193f5SChuck Lever 	do {
390f13193f5SChuck Lever 		if (atomic_sub_return(cc->cc_sqecount,
391f13193f5SChuck Lever 				      &rdma->sc_sq_avail) > 0) {
39245f13584SChuck Lever 			cc->cc_posttime = ktime_get();
3935d85a822SJason Gunthorpe 			ret = ib_post_send(rdma->sc_qp, first_wr, &bad_wr);
394f13193f5SChuck Lever 			if (ret)
395f13193f5SChuck Lever 				break;
396f13193f5SChuck Lever 			return 0;
397f13193f5SChuck Lever 		}
398f13193f5SChuck Lever 
39922df5a22SChuck Lever 		percpu_counter_inc(&svcrdma_stat_sq_starve);
400bd2abef3SChuck Lever 		trace_svcrdma_sq_full(rdma);
401f13193f5SChuck Lever 		atomic_add(cc->cc_sqecount, &rdma->sc_sq_avail);
402f13193f5SChuck Lever 		wait_event(rdma->sc_send_wait,
403f13193f5SChuck Lever 			   atomic_read(&rdma->sc_sq_avail) > cc->cc_sqecount);
404bd2abef3SChuck Lever 		trace_svcrdma_sq_retry(rdma);
405f13193f5SChuck Lever 	} while (1);
406f13193f5SChuck Lever 
407e28b4fc6SChuck Lever 	trace_svcrdma_sq_post_err(rdma, ret);
408e844d307SChuck Lever 	svc_xprt_deferred_close(&rdma->sc_xprt);
409f13193f5SChuck Lever 
410f13193f5SChuck Lever 	/* If even one was posted, there will be a completion. */
411f13193f5SChuck Lever 	if (bad_wr != first_wr)
412f13193f5SChuck Lever 		return 0;
413f13193f5SChuck Lever 
414f13193f5SChuck Lever 	atomic_add(cc->cc_sqecount, &rdma->sc_sq_avail);
415f13193f5SChuck Lever 	wake_up(&rdma->sc_send_wait);
416f13193f5SChuck Lever 	return -ENOTCONN;
417f13193f5SChuck Lever }
418f13193f5SChuck Lever 
419f13193f5SChuck Lever /* Build and DMA-map an SGL that covers one kvec in an xdr_buf
420f13193f5SChuck Lever  */
421f13193f5SChuck Lever static void svc_rdma_vec_to_sg(struct svc_rdma_write_info *info,
422f13193f5SChuck Lever 			       unsigned int len,
423f13193f5SChuck Lever 			       struct svc_rdma_rw_ctxt *ctxt)
424f13193f5SChuck Lever {
425f13193f5SChuck Lever 	struct scatterlist *sg = ctxt->rw_sg_table.sgl;
426f13193f5SChuck Lever 
427f13193f5SChuck Lever 	sg_set_buf(&sg[0], info->wi_base, len);
428f13193f5SChuck Lever 	info->wi_base += len;
429f13193f5SChuck Lever 
430f13193f5SChuck Lever 	ctxt->rw_nents = 1;
431f13193f5SChuck Lever }
432f13193f5SChuck Lever 
433f13193f5SChuck Lever /* Build and DMA-map an SGL that covers part of an xdr_buf's pagelist.
434f13193f5SChuck Lever  */
435f13193f5SChuck Lever static void svc_rdma_pagelist_to_sg(struct svc_rdma_write_info *info,
436f13193f5SChuck Lever 				    unsigned int remaining,
437f13193f5SChuck Lever 				    struct svc_rdma_rw_ctxt *ctxt)
438f13193f5SChuck Lever {
439f13193f5SChuck Lever 	unsigned int sge_no, sge_bytes, page_off, page_no;
44051bad8ccSChuck Lever 	const struct xdr_buf *xdr = info->wi_xdr;
441f13193f5SChuck Lever 	struct scatterlist *sg;
442f13193f5SChuck Lever 	struct page **page;
443f13193f5SChuck Lever 
44491b022ecSChuck Lever 	page_off = info->wi_next_off + xdr->page_base;
44591b022ecSChuck Lever 	page_no = page_off >> PAGE_SHIFT;
44691b022ecSChuck Lever 	page_off = offset_in_page(page_off);
447f13193f5SChuck Lever 	page = xdr->pages + page_no;
448f13193f5SChuck Lever 	info->wi_next_off += remaining;
449f13193f5SChuck Lever 	sg = ctxt->rw_sg_table.sgl;
450f13193f5SChuck Lever 	sge_no = 0;
451f13193f5SChuck Lever 	do {
452f13193f5SChuck Lever 		sge_bytes = min_t(unsigned int, remaining,
453f13193f5SChuck Lever 				  PAGE_SIZE - page_off);
454f13193f5SChuck Lever 		sg_set_page(sg, *page, sge_bytes, page_off);
455f13193f5SChuck Lever 
456f13193f5SChuck Lever 		remaining -= sge_bytes;
457f13193f5SChuck Lever 		sg = sg_next(sg);
458f13193f5SChuck Lever 		page_off = 0;
459f13193f5SChuck Lever 		sge_no++;
460f13193f5SChuck Lever 		page++;
461f13193f5SChuck Lever 	} while (remaining);
462f13193f5SChuck Lever 
463f13193f5SChuck Lever 	ctxt->rw_nents = sge_no;
464f13193f5SChuck Lever }
465f13193f5SChuck Lever 
466f13193f5SChuck Lever /* Construct RDMA Write WRs to send a portion of an xdr_buf containing
467f13193f5SChuck Lever  * an RPC Reply.
468f13193f5SChuck Lever  */
469f13193f5SChuck Lever static int
470f13193f5SChuck Lever svc_rdma_build_writes(struct svc_rdma_write_info *info,
471f13193f5SChuck Lever 		      void (*constructor)(struct svc_rdma_write_info *info,
472f13193f5SChuck Lever 					  unsigned int len,
473f13193f5SChuck Lever 					  struct svc_rdma_rw_ctxt *ctxt),
474f13193f5SChuck Lever 		      unsigned int remaining)
475f13193f5SChuck Lever {
476f13193f5SChuck Lever 	struct svc_rdma_chunk_ctxt *cc = &info->wi_cc;
477f13193f5SChuck Lever 	struct svcxprt_rdma *rdma = cc->cc_rdma;
4787a1cbfa1SChuck Lever 	const struct svc_rdma_segment *seg;
479f13193f5SChuck Lever 	struct svc_rdma_rw_ctxt *ctxt;
480f13193f5SChuck Lever 	int ret;
481f13193f5SChuck Lever 
482f13193f5SChuck Lever 	do {
483f13193f5SChuck Lever 		unsigned int write_len;
484f60a0869SChuck Lever 		u64 offset;
485f13193f5SChuck Lever 
486f012e95bSChuck Lever 		if (info->wi_seg_no >= info->wi_chunk->ch_segcount)
487f13193f5SChuck Lever 			goto out_overflow;
488f13193f5SChuck Lever 
489f012e95bSChuck Lever 		seg = &info->wi_chunk->ch_segments[info->wi_seg_no];
4907a1cbfa1SChuck Lever 		write_len = min(remaining, seg->rs_length - info->wi_seg_off);
491e5decb2eSChuck Lever 		if (!write_len)
492e5decb2eSChuck Lever 			goto out_overflow;
493f13193f5SChuck Lever 		ctxt = svc_rdma_get_rw_ctxt(rdma,
494f13193f5SChuck Lever 					    (write_len >> PAGE_SHIFT) + 2);
495f13193f5SChuck Lever 		if (!ctxt)
496f4e53e1cSChuck Lever 			return -ENOMEM;
497f13193f5SChuck Lever 
498f13193f5SChuck Lever 		constructor(info, write_len, ctxt);
4997a1cbfa1SChuck Lever 		offset = seg->rs_offset + info->wi_seg_off;
5007a1cbfa1SChuck Lever 		ret = svc_rdma_rw_ctx_init(rdma, ctxt, offset, seg->rs_handle,
5012abfbe7eSChuck Lever 					   DMA_TO_DEVICE);
502f13193f5SChuck Lever 		if (ret < 0)
5032abfbe7eSChuck Lever 			return -EIO;
5041e7e5573SChuck Lever 		percpu_counter_inc(&svcrdma_stat_write);
505f13193f5SChuck Lever 
506f13193f5SChuck Lever 		list_add(&ctxt->rw_list, &cc->cc_rwctxts);
507f13193f5SChuck Lever 		cc->cc_sqecount += ret;
5087a1cbfa1SChuck Lever 		if (write_len == seg->rs_length - info->wi_seg_off) {
509f13193f5SChuck Lever 			info->wi_seg_no++;
510f13193f5SChuck Lever 			info->wi_seg_off = 0;
511f13193f5SChuck Lever 		} else {
512f13193f5SChuck Lever 			info->wi_seg_off += write_len;
513f13193f5SChuck Lever 		}
514f13193f5SChuck Lever 		remaining -= write_len;
515f13193f5SChuck Lever 	} while (remaining);
516f13193f5SChuck Lever 
517f13193f5SChuck Lever 	return 0;
518f13193f5SChuck Lever 
519f13193f5SChuck Lever out_overflow:
520dbc17acdSChuck Lever 	trace_svcrdma_small_wrch_err(rdma, remaining, info->wi_seg_no,
5217a1cbfa1SChuck Lever 				     info->wi_chunk->ch_segcount);
522f13193f5SChuck Lever 	return -E2BIG;
523f13193f5SChuck Lever }
524f13193f5SChuck Lever 
525ab1394eeSChuck Lever /**
526ab1394eeSChuck Lever  * svc_rdma_iov_write - Construct RDMA Writes from an iov
527ab1394eeSChuck Lever  * @info: pointer to write arguments
528ab1394eeSChuck Lever  * @iov: kvec to write
529ab1394eeSChuck Lever  *
530ab1394eeSChuck Lever  * Returns:
531d5029525SZheng Yongjun  *   On success, returns zero
532ab1394eeSChuck Lever  *   %-E2BIG if the client-provided Write chunk is too small
533ab1394eeSChuck Lever  *   %-ENOMEM if a resource has been exhausted
534ab1394eeSChuck Lever  *   %-EIO if an rdma-rw error occurred
535f13193f5SChuck Lever  */
536ab1394eeSChuck Lever static int svc_rdma_iov_write(struct svc_rdma_write_info *info,
537ab1394eeSChuck Lever 			      const struct kvec *iov)
538f13193f5SChuck Lever {
539ab1394eeSChuck Lever 	info->wi_base = iov->iov_base;
540f13193f5SChuck Lever 	return svc_rdma_build_writes(info, svc_rdma_vec_to_sg,
541ab1394eeSChuck Lever 				     iov->iov_len);
542f13193f5SChuck Lever }
543f13193f5SChuck Lever 
544ab1394eeSChuck Lever /**
545ab1394eeSChuck Lever  * svc_rdma_pages_write - Construct RDMA Writes from pages
546ab1394eeSChuck Lever  * @info: pointer to write arguments
547ab1394eeSChuck Lever  * @xdr: xdr_buf with pages to write
548ab1394eeSChuck Lever  * @offset: offset into the content of @xdr
549ab1394eeSChuck Lever  * @length: number of bytes to write
550ab1394eeSChuck Lever  *
551ab1394eeSChuck Lever  * Returns:
552d5029525SZheng Yongjun  *   On success, returns zero
553ab1394eeSChuck Lever  *   %-E2BIG if the client-provided Write chunk is too small
554ab1394eeSChuck Lever  *   %-ENOMEM if a resource has been exhausted
555ab1394eeSChuck Lever  *   %-EIO if an rdma-rw error occurred
556f13193f5SChuck Lever  */
557ab1394eeSChuck Lever static int svc_rdma_pages_write(struct svc_rdma_write_info *info,
558ab1394eeSChuck Lever 				const struct xdr_buf *xdr,
55941205539SChuck Lever 				unsigned int offset,
56041205539SChuck Lever 				unsigned long length)
561f13193f5SChuck Lever {
562f13193f5SChuck Lever 	info->wi_xdr = xdr;
56341205539SChuck Lever 	info->wi_next_off = offset - xdr->head[0].iov_len;
564f13193f5SChuck Lever 	return svc_rdma_build_writes(info, svc_rdma_pagelist_to_sg,
56541205539SChuck Lever 				     length);
566f13193f5SChuck Lever }
567f13193f5SChuck Lever 
568f13193f5SChuck Lever /**
569f6ad7759SChuck Lever  * svc_rdma_xb_write - Construct RDMA Writes to write an xdr_buf
570f6ad7759SChuck Lever  * @xdr: xdr_buf to write
57141bc163fSChuck Lever  * @data: pointer to write arguments
572f6ad7759SChuck Lever  *
573f6ad7759SChuck Lever  * Returns:
574d5029525SZheng Yongjun  *   On success, returns zero
575f6ad7759SChuck Lever  *   %-E2BIG if the client-provided Write chunk is too small
576f6ad7759SChuck Lever  *   %-ENOMEM if a resource has been exhausted
577f6ad7759SChuck Lever  *   %-EIO if an rdma-rw error occurred
578f6ad7759SChuck Lever  */
57941bc163fSChuck Lever static int svc_rdma_xb_write(const struct xdr_buf *xdr, void *data)
580f6ad7759SChuck Lever {
58141bc163fSChuck Lever 	struct svc_rdma_write_info *info = data;
582f6ad7759SChuck Lever 	int ret;
583f6ad7759SChuck Lever 
584f6ad7759SChuck Lever 	if (xdr->head[0].iov_len) {
585f6ad7759SChuck Lever 		ret = svc_rdma_iov_write(info, &xdr->head[0]);
586f6ad7759SChuck Lever 		if (ret < 0)
587f6ad7759SChuck Lever 			return ret;
588f6ad7759SChuck Lever 	}
589f6ad7759SChuck Lever 
590f6ad7759SChuck Lever 	if (xdr->page_len) {
591f6ad7759SChuck Lever 		ret = svc_rdma_pages_write(info, xdr, xdr->head[0].iov_len,
592f6ad7759SChuck Lever 					   xdr->page_len);
593f6ad7759SChuck Lever 		if (ret < 0)
594f6ad7759SChuck Lever 			return ret;
595f6ad7759SChuck Lever 	}
596f6ad7759SChuck Lever 
597f6ad7759SChuck Lever 	if (xdr->tail[0].iov_len) {
598f6ad7759SChuck Lever 		ret = svc_rdma_iov_write(info, &xdr->tail[0]);
599f6ad7759SChuck Lever 		if (ret < 0)
600f6ad7759SChuck Lever 			return ret;
601f6ad7759SChuck Lever 	}
602f6ad7759SChuck Lever 
603f6ad7759SChuck Lever 	return xdr->len;
604f6ad7759SChuck Lever }
605f6ad7759SChuck Lever 
606f6ad7759SChuck Lever /**
607f13193f5SChuck Lever  * svc_rdma_send_write_chunk - Write all segments in a Write chunk
608f13193f5SChuck Lever  * @rdma: controlling RDMA transport
6097a1cbfa1SChuck Lever  * @chunk: Write chunk provided by the client
610f13193f5SChuck Lever  * @xdr: xdr_buf containing the data payload
611f13193f5SChuck Lever  *
612f13193f5SChuck Lever  * Returns a non-negative number of bytes the chunk consumed, or
613f13193f5SChuck Lever  *	%-E2BIG if the payload was larger than the Write chunk,
614107c1d0aSChuck Lever  *	%-EINVAL if client provided too many segments,
615f13193f5SChuck Lever  *	%-ENOMEM if rdma_rw context pool was exhausted,
616f13193f5SChuck Lever  *	%-ENOTCONN if posting failed (connection is lost),
617f13193f5SChuck Lever  *	%-EIO if rdma_rw initialization failed (DMA mapping, etc).
618f13193f5SChuck Lever  */
6197a1cbfa1SChuck Lever int svc_rdma_send_write_chunk(struct svcxprt_rdma *rdma,
6207a1cbfa1SChuck Lever 			      const struct svc_rdma_chunk *chunk,
621f6ad7759SChuck Lever 			      const struct xdr_buf *xdr)
622f13193f5SChuck Lever {
623f13193f5SChuck Lever 	struct svc_rdma_write_info *info;
624b704be09SChuck Lever 	struct svc_rdma_chunk_ctxt *cc;
625f13193f5SChuck Lever 	int ret;
626f13193f5SChuck Lever 
6277a1cbfa1SChuck Lever 	info = svc_rdma_write_info_alloc(rdma, chunk);
628f13193f5SChuck Lever 	if (!info)
629f13193f5SChuck Lever 		return -ENOMEM;
630b704be09SChuck Lever 	cc = &info->wi_cc;
631f13193f5SChuck Lever 
632f6ad7759SChuck Lever 	ret = svc_rdma_xb_write(xdr, info);
633f6ad7759SChuck Lever 	if (ret != xdr->len)
634f13193f5SChuck Lever 		goto out_err;
635f13193f5SChuck Lever 
636b704be09SChuck Lever 	trace_svcrdma_post_write_chunk(&cc->cc_cid, cc->cc_sqecount);
637b704be09SChuck Lever 	ret = svc_rdma_post_chunk_ctxt(cc);
638f13193f5SChuck Lever 	if (ret < 0)
639f13193f5SChuck Lever 		goto out_err;
640f6ad7759SChuck Lever 	return xdr->len;
641f13193f5SChuck Lever 
642f13193f5SChuck Lever out_err:
643f13193f5SChuck Lever 	svc_rdma_write_info_free(info);
644f13193f5SChuck Lever 	return ret;
645f13193f5SChuck Lever }
646f13193f5SChuck Lever 
647f13193f5SChuck Lever /**
648f13193f5SChuck Lever  * svc_rdma_send_reply_chunk - Write all segments in the Reply chunk
649f13193f5SChuck Lever  * @rdma: controlling RDMA transport
6506fa5785eSChuck Lever  * @rctxt: Write and Reply chunks from client
651f13193f5SChuck Lever  * @xdr: xdr_buf containing an RPC Reply
652f13193f5SChuck Lever  *
653f13193f5SChuck Lever  * Returns a non-negative number of bytes the chunk consumed, or
654f13193f5SChuck Lever  *	%-E2BIG if the payload was larger than the Reply chunk,
655107c1d0aSChuck Lever  *	%-EINVAL if client provided too many segments,
656f13193f5SChuck Lever  *	%-ENOMEM if rdma_rw context pool was exhausted,
657f13193f5SChuck Lever  *	%-ENOTCONN if posting failed (connection is lost),
658f13193f5SChuck Lever  *	%-EIO if rdma_rw initialization failed (DMA mapping, etc).
659f13193f5SChuck Lever  */
6606fa5785eSChuck Lever int svc_rdma_send_reply_chunk(struct svcxprt_rdma *rdma,
6616fa5785eSChuck Lever 			      const struct svc_rdma_recv_ctxt *rctxt,
66241bc163fSChuck Lever 			      const struct xdr_buf *xdr)
663f13193f5SChuck Lever {
664f13193f5SChuck Lever 	struct svc_rdma_write_info *info;
665b704be09SChuck Lever 	struct svc_rdma_chunk_ctxt *cc;
6667a1cbfa1SChuck Lever 	struct svc_rdma_chunk *chunk;
66741bc163fSChuck Lever 	int ret;
668f13193f5SChuck Lever 
6697a1cbfa1SChuck Lever 	if (pcl_is_empty(&rctxt->rc_reply_pcl))
670ded380f1SChuck Lever 		return 0;
671ded380f1SChuck Lever 
6727a1cbfa1SChuck Lever 	chunk = pcl_first_chunk(&rctxt->rc_reply_pcl);
6737a1cbfa1SChuck Lever 	info = svc_rdma_write_info_alloc(rdma, chunk);
674f13193f5SChuck Lever 	if (!info)
675f13193f5SChuck Lever 		return -ENOMEM;
676b704be09SChuck Lever 	cc = &info->wi_cc;
677f13193f5SChuck Lever 
67841bc163fSChuck Lever 	ret = pcl_process_nonpayloads(&rctxt->rc_write_pcl, xdr,
67941bc163fSChuck Lever 				      svc_rdma_xb_write, info);
680f13193f5SChuck Lever 	if (ret < 0)
681f13193f5SChuck Lever 		goto out_err;
682f13193f5SChuck Lever 
683b704be09SChuck Lever 	trace_svcrdma_post_reply_chunk(&cc->cc_cid, cc->cc_sqecount);
684b704be09SChuck Lever 	ret = svc_rdma_post_chunk_ctxt(cc);
685f13193f5SChuck Lever 	if (ret < 0)
686f13193f5SChuck Lever 		goto out_err;
68798895edbSChuck Lever 
68841bc163fSChuck Lever 	return xdr->len;
689f13193f5SChuck Lever 
690f13193f5SChuck Lever out_err:
691f13193f5SChuck Lever 	svc_rdma_write_info_free(info);
692f13193f5SChuck Lever 	return ret;
693f13193f5SChuck Lever }
694026d958bSChuck Lever 
695d96962e6SChuck Lever /**
696d96962e6SChuck Lever  * svc_rdma_build_read_segment - Build RDMA Read WQEs to pull one RDMA segment
697d96962e6SChuck Lever  * @info: context for ongoing I/O
698d96962e6SChuck Lever  * @segment: co-ordinates of remote memory to be read
699d96962e6SChuck Lever  *
700d96962e6SChuck Lever  * Returns:
701d96962e6SChuck Lever  *   %0: the Read WR chain was constructed successfully
702d96962e6SChuck Lever  *   %-EINVAL: there were not enough rq_pages to finish
703d96962e6SChuck Lever  *   %-ENOMEM: allocating a local resources failed
704d96962e6SChuck Lever  *   %-EIO: a DMA mapping error occurred
705d96962e6SChuck Lever  */
706026d958bSChuck Lever static int svc_rdma_build_read_segment(struct svc_rdma_read_info *info,
707d96962e6SChuck Lever 				       const struct svc_rdma_segment *segment)
708026d958bSChuck Lever {
709ecf85b23SChuck Lever 	struct svc_rdma_recv_ctxt *head = info->ri_readctxt;
710026d958bSChuck Lever 	struct svc_rdma_chunk_ctxt *cc = &info->ri_cc;
711d96962e6SChuck Lever 	struct svc_rqst *rqstp = info->ri_rqst;
712d96962e6SChuck Lever 	unsigned int sge_no, seg_len, len;
7137d81ee87SChuck Lever 	struct svc_rdma_rw_ctxt *ctxt;
714026d958bSChuck Lever 	struct scatterlist *sg;
715026d958bSChuck Lever 	int ret;
716026d958bSChuck Lever 
717d96962e6SChuck Lever 	len = segment->rs_length;
718026d958bSChuck Lever 	sge_no = PAGE_ALIGN(info->ri_pageoff + len) >> PAGE_SHIFT;
719026d958bSChuck Lever 	ctxt = svc_rdma_get_rw_ctxt(cc->cc_rdma, sge_no);
720026d958bSChuck Lever 	if (!ctxt)
721f4e53e1cSChuck Lever 		return -ENOMEM;
722026d958bSChuck Lever 	ctxt->rw_nents = sge_no;
723026d958bSChuck Lever 
724026d958bSChuck Lever 	sg = ctxt->rw_sg_table.sgl;
725026d958bSChuck Lever 	for (sge_no = 0; sge_no < ctxt->rw_nents; sge_no++) {
726026d958bSChuck Lever 		seg_len = min_t(unsigned int, len,
727026d958bSChuck Lever 				PAGE_SIZE - info->ri_pageoff);
728026d958bSChuck Lever 
729026d958bSChuck Lever 		if (!info->ri_pageoff)
730ecf85b23SChuck Lever 			head->rc_page_count++;
731026d958bSChuck Lever 
732026d958bSChuck Lever 		sg_set_page(sg, rqstp->rq_pages[info->ri_pageno],
733026d958bSChuck Lever 			    seg_len, info->ri_pageoff);
734026d958bSChuck Lever 		sg = sg_next(sg);
735026d958bSChuck Lever 
736026d958bSChuck Lever 		info->ri_pageoff += seg_len;
737026d958bSChuck Lever 		if (info->ri_pageoff == PAGE_SIZE) {
738026d958bSChuck Lever 			info->ri_pageno++;
739026d958bSChuck Lever 			info->ri_pageoff = 0;
740026d958bSChuck Lever 		}
741026d958bSChuck Lever 		len -= seg_len;
742026d958bSChuck Lever 
743026d958bSChuck Lever 		/* Safety check */
744026d958bSChuck Lever 		if (len &&
745026d958bSChuck Lever 		    &rqstp->rq_pages[info->ri_pageno + 1] > rqstp->rq_page_end)
746026d958bSChuck Lever 			goto out_overrun;
747026d958bSChuck Lever 	}
748026d958bSChuck Lever 
749d96962e6SChuck Lever 	ret = svc_rdma_rw_ctx_init(cc->cc_rdma, ctxt, segment->rs_offset,
750d96962e6SChuck Lever 				   segment->rs_handle, DMA_FROM_DEVICE);
751026d958bSChuck Lever 	if (ret < 0)
7522abfbe7eSChuck Lever 		return -EIO;
7531e7e5573SChuck Lever 	percpu_counter_inc(&svcrdma_stat_read);
754026d958bSChuck Lever 
755026d958bSChuck Lever 	list_add(&ctxt->rw_list, &cc->cc_rwctxts);
756026d958bSChuck Lever 	cc->cc_sqecount += ret;
757026d958bSChuck Lever 	return 0;
758026d958bSChuck Lever 
759026d958bSChuck Lever out_overrun:
7609d200638SChuck Lever 	trace_svcrdma_page_overrun_err(cc->cc_rdma, rqstp, info->ri_pageno);
761026d958bSChuck Lever 	return -EINVAL;
762026d958bSChuck Lever }
763026d958bSChuck Lever 
764d96962e6SChuck Lever /**
765d96962e6SChuck Lever  * svc_rdma_build_read_chunk - Build RDMA Read WQEs to pull one RDMA chunk
766d96962e6SChuck Lever  * @info: context for ongoing I/O
767d96962e6SChuck Lever  * @chunk: Read chunk to pull
768d96962e6SChuck Lever  *
769d96962e6SChuck Lever  * Return values:
770d96962e6SChuck Lever  *   %0: the Read WR chain was constructed successfully
771d96962e6SChuck Lever  *   %-EINVAL: there were not enough resources to finish
772d96962e6SChuck Lever  *   %-ENOMEM: allocating a local resources failed
773d96962e6SChuck Lever  *   %-EIO: a DMA mapping error occurred
7747075a867SChuck Lever  */
775d96962e6SChuck Lever static int svc_rdma_build_read_chunk(struct svc_rdma_read_info *info,
776d96962e6SChuck Lever 				     const struct svc_rdma_chunk *chunk)
777026d958bSChuck Lever {
778d96962e6SChuck Lever 	const struct svc_rdma_segment *segment;
779026d958bSChuck Lever 	int ret;
780026d958bSChuck Lever 
7817075a867SChuck Lever 	ret = -EINVAL;
782d96962e6SChuck Lever 	pcl_for_each_segment(segment, chunk) {
783d96962e6SChuck Lever 		ret = svc_rdma_build_read_segment(info, segment);
784026d958bSChuck Lever 		if (ret < 0)
785026d958bSChuck Lever 			break;
786d96962e6SChuck Lever 		info->ri_totalbytes += segment->rs_length;
787026d958bSChuck Lever 	}
788026d958bSChuck Lever 	return ret;
789026d958bSChuck Lever }
790026d958bSChuck Lever 
791d96962e6SChuck Lever /**
792d7cc7397SChuck Lever  * svc_rdma_copy_inline_range - Copy part of the inline content into pages
793d7cc7397SChuck Lever  * @info: context for RDMA Reads
794d7cc7397SChuck Lever  * @offset: offset into the Receive buffer of region to copy
795d7cc7397SChuck Lever  * @remaining: length of region to copy
796d7cc7397SChuck Lever  *
797d7cc7397SChuck Lever  * Take a page at a time from rqstp->rq_pages and copy the inline
798d7cc7397SChuck Lever  * content from the Receive buffer into that page. Update
799d7cc7397SChuck Lever  * info->ri_pageno and info->ri_pageoff so that the next RDMA Read
800d7cc7397SChuck Lever  * result will land contiguously with the copied content.
801d7cc7397SChuck Lever  *
802d7cc7397SChuck Lever  * Return values:
803d7cc7397SChuck Lever  *   %0: Inline content was successfully copied
804d7cc7397SChuck Lever  *   %-EINVAL: offset or length was incorrect
805d7cc7397SChuck Lever  */
806d7cc7397SChuck Lever static int svc_rdma_copy_inline_range(struct svc_rdma_read_info *info,
807d7cc7397SChuck Lever 				      unsigned int offset,
808d7cc7397SChuck Lever 				      unsigned int remaining)
809d7cc7397SChuck Lever {
810d7cc7397SChuck Lever 	struct svc_rdma_recv_ctxt *head = info->ri_readctxt;
811d7cc7397SChuck Lever 	unsigned char *dst, *src = head->rc_recv_buf;
812d7cc7397SChuck Lever 	struct svc_rqst *rqstp = info->ri_rqst;
813d7cc7397SChuck Lever 	unsigned int page_no, numpages;
814d7cc7397SChuck Lever 
815d7cc7397SChuck Lever 	numpages = PAGE_ALIGN(info->ri_pageoff + remaining) >> PAGE_SHIFT;
816d7cc7397SChuck Lever 	for (page_no = 0; page_no < numpages; page_no++) {
817d7cc7397SChuck Lever 		unsigned int page_len;
818d7cc7397SChuck Lever 
819d7cc7397SChuck Lever 		page_len = min_t(unsigned int, remaining,
820d7cc7397SChuck Lever 				 PAGE_SIZE - info->ri_pageoff);
821d7cc7397SChuck Lever 
822d7cc7397SChuck Lever 		if (!info->ri_pageoff)
823d7cc7397SChuck Lever 			head->rc_page_count++;
824d7cc7397SChuck Lever 
8257d81ee87SChuck Lever 		dst = page_address(rqstp->rq_pages[info->ri_pageno]);
826d7cc7397SChuck Lever 		memcpy(dst + info->ri_pageno, src + offset, page_len);
827d7cc7397SChuck Lever 
828d7cc7397SChuck Lever 		info->ri_totalbytes += page_len;
829d7cc7397SChuck Lever 		info->ri_pageoff += page_len;
830d7cc7397SChuck Lever 		if (info->ri_pageoff == PAGE_SIZE) {
831d7cc7397SChuck Lever 			info->ri_pageno++;
832d7cc7397SChuck Lever 			info->ri_pageoff = 0;
833d7cc7397SChuck Lever 		}
834d7cc7397SChuck Lever 		remaining -= page_len;
835d7cc7397SChuck Lever 		offset += page_len;
836d7cc7397SChuck Lever 	}
837d7cc7397SChuck Lever 
838d7cc7397SChuck Lever 	return -EINVAL;
839d7cc7397SChuck Lever }
840d7cc7397SChuck Lever 
841d7cc7397SChuck Lever /**
842d7cc7397SChuck Lever  * svc_rdma_read_multiple_chunks - Construct RDMA Reads to pull data item Read chunks
843d7cc7397SChuck Lever  * @info: context for RDMA Reads
844d7cc7397SChuck Lever  *
8457d81ee87SChuck Lever  * The chunk data lands in rqstp->rq_arg as a series of contiguous pages,
846d7cc7397SChuck Lever  * like an incoming TCP call.
847d7cc7397SChuck Lever  *
848d7cc7397SChuck Lever  * Return values:
849d7cc7397SChuck Lever  *   %0: RDMA Read WQEs were successfully built
850d7cc7397SChuck Lever  *   %-EINVAL: client provided too many chunks or segments,
851d7cc7397SChuck Lever  *   %-ENOMEM: rdma_rw context pool was exhausted,
852d7cc7397SChuck Lever  *   %-ENOTCONN: posting failed (connection is lost),
853d7cc7397SChuck Lever  *   %-EIO: rdma_rw initialization failed (DMA mapping, etc).
854d7cc7397SChuck Lever  */
855d7cc7397SChuck Lever static noinline int svc_rdma_read_multiple_chunks(struct svc_rdma_read_info *info)
856d7cc7397SChuck Lever {
857d7cc7397SChuck Lever 	struct svc_rdma_recv_ctxt *head = info->ri_readctxt;
858d7cc7397SChuck Lever 	const struct svc_rdma_pcl *pcl = &head->rc_read_pcl;
8597d81ee87SChuck Lever 	struct xdr_buf *buf = &info->ri_rqst->rq_arg;
860d7cc7397SChuck Lever 	struct svc_rdma_chunk *chunk, *next;
861d7cc7397SChuck Lever 	unsigned int start, length;
862d7cc7397SChuck Lever 	int ret;
863d7cc7397SChuck Lever 
864d7cc7397SChuck Lever 	start = 0;
865d7cc7397SChuck Lever 	chunk = pcl_first_chunk(pcl);
866d7cc7397SChuck Lever 	length = chunk->ch_position;
867d7cc7397SChuck Lever 	ret = svc_rdma_copy_inline_range(info, start, length);
868d7cc7397SChuck Lever 	if (ret < 0)
869d7cc7397SChuck Lever 		return ret;
870d7cc7397SChuck Lever 
871d7cc7397SChuck Lever 	pcl_for_each_chunk(chunk, pcl) {
872d7cc7397SChuck Lever 		ret = svc_rdma_build_read_chunk(info, chunk);
873d7cc7397SChuck Lever 		if (ret < 0)
874d7cc7397SChuck Lever 			return ret;
875d7cc7397SChuck Lever 
876d7cc7397SChuck Lever 		next = pcl_next_chunk(pcl, chunk);
877d7cc7397SChuck Lever 		if (!next)
878d7cc7397SChuck Lever 			break;
879d7cc7397SChuck Lever 
880d7cc7397SChuck Lever 		start += length;
881d7cc7397SChuck Lever 		length = next->ch_position - info->ri_totalbytes;
882d7cc7397SChuck Lever 		ret = svc_rdma_copy_inline_range(info, start, length);
883d7cc7397SChuck Lever 		if (ret < 0)
884d7cc7397SChuck Lever 			return ret;
885d7cc7397SChuck Lever 	}
886d7cc7397SChuck Lever 
887d7cc7397SChuck Lever 	start += length;
888d7cc7397SChuck Lever 	length = head->rc_byte_len - start;
889d7cc7397SChuck Lever 	ret = svc_rdma_copy_inline_range(info, start, length);
890d7cc7397SChuck Lever 	if (ret < 0)
891d7cc7397SChuck Lever 		return ret;
892d7cc7397SChuck Lever 
893d7cc7397SChuck Lever 	buf->len += info->ri_totalbytes;
894d7cc7397SChuck Lever 	buf->buflen += info->ri_totalbytes;
895d7cc7397SChuck Lever 
8967d81ee87SChuck Lever 	buf->head[0].iov_base = page_address(info->ri_rqst->rq_pages[0]);
897d7cc7397SChuck Lever 	buf->head[0].iov_len = min_t(size_t, PAGE_SIZE, info->ri_totalbytes);
8987d81ee87SChuck Lever 	buf->pages = &info->ri_rqst->rq_pages[1];
899d7cc7397SChuck Lever 	buf->page_len = info->ri_totalbytes - buf->head[0].iov_len;
900d7cc7397SChuck Lever 	return 0;
901d7cc7397SChuck Lever }
902d7cc7397SChuck Lever 
903d7cc7397SChuck Lever /**
904d7cc7397SChuck Lever  * svc_rdma_read_data_item - Construct RDMA Reads to pull data item Read chunks
905d96962e6SChuck Lever  * @info: context for RDMA Reads
906d96962e6SChuck Lever  *
9077d81ee87SChuck Lever  * The chunk data lands in the page list of rqstp->rq_arg.pages.
908026d958bSChuck Lever  *
9097d81ee87SChuck Lever  * Currently NFSD does not look at the rqstp->rq_arg.tail[0] kvec.
910026d958bSChuck Lever  * Therefore, XDR round-up of the Read chunk and trailing
911026d958bSChuck Lever  * inline content must both be added at the end of the pagelist.
912d96962e6SChuck Lever  *
913d96962e6SChuck Lever  * Return values:
914d96962e6SChuck Lever  *   %0: RDMA Read WQEs were successfully built
915d96962e6SChuck Lever  *   %-EINVAL: client provided too many chunks or segments,
916d96962e6SChuck Lever  *   %-ENOMEM: rdma_rw context pool was exhausted,
917d96962e6SChuck Lever  *   %-ENOTCONN: posting failed (connection is lost),
918d96962e6SChuck Lever  *   %-EIO: rdma_rw initialization failed (DMA mapping, etc).
919026d958bSChuck Lever  */
920d7cc7397SChuck Lever static int svc_rdma_read_data_item(struct svc_rdma_read_info *info)
921026d958bSChuck Lever {
922ecf85b23SChuck Lever 	struct svc_rdma_recv_ctxt *head = info->ri_readctxt;
9237d81ee87SChuck Lever 	struct xdr_buf *buf = &info->ri_rqst->rq_arg;
924d96962e6SChuck Lever 	struct svc_rdma_chunk *chunk;
925bafe9c27SChuck Lever 	unsigned int length;
926026d958bSChuck Lever 	int ret;
927026d958bSChuck Lever 
928d96962e6SChuck Lever 	chunk = pcl_first_chunk(&head->rc_read_pcl);
929d96962e6SChuck Lever 	ret = svc_rdma_build_read_chunk(info, chunk);
930026d958bSChuck Lever 	if (ret < 0)
931026d958bSChuck Lever 		goto out;
932026d958bSChuck Lever 
933193bcb7bSChuck Lever 	/* Split the Receive buffer between the head and tail
934193bcb7bSChuck Lever 	 * buffers at Read chunk's position. XDR roundup of the
935193bcb7bSChuck Lever 	 * chunk is not included in either the pagelist or in
936193bcb7bSChuck Lever 	 * the tail.
937026d958bSChuck Lever 	 */
938d96962e6SChuck Lever 	buf->tail[0].iov_base = buf->head[0].iov_base + chunk->ch_position;
939d96962e6SChuck Lever 	buf->tail[0].iov_len = buf->head[0].iov_len - chunk->ch_position;
940d96962e6SChuck Lever 	buf->head[0].iov_len = chunk->ch_position;
941026d958bSChuck Lever 
942175e0310SChuck Lever 	/* Read chunk may need XDR roundup (see RFC 8166, s. 3.4.5.2).
943193bcb7bSChuck Lever 	 *
944175e0310SChuck Lever 	 * If the client already rounded up the chunk length, the
945175e0310SChuck Lever 	 * length does not change. Otherwise, the length of the page
946175e0310SChuck Lever 	 * list is increased to include XDR round-up.
947175e0310SChuck Lever 	 *
948175e0310SChuck Lever 	 * Currently these chunks always start at page offset 0,
949175e0310SChuck Lever 	 * thus the rounded-up length never crosses a page boundary.
950026d958bSChuck Lever 	 */
9517d81ee87SChuck Lever 	buf->pages = &info->ri_rqst->rq_pages[0];
9527d81ee87SChuck Lever 	length = xdr_align_size(chunk->ch_length);
953bafe9c27SChuck Lever 	buf->page_len = length;
954bafe9c27SChuck Lever 	buf->len += length;
955bafe9c27SChuck Lever 	buf->buflen += length;
956026d958bSChuck Lever 
957026d958bSChuck Lever out:
958026d958bSChuck Lever 	return ret;
959026d958bSChuck Lever }
960026d958bSChuck Lever 
961d96962e6SChuck Lever /**
962d7cc7397SChuck Lever  * svc_rdma_read_chunk_range - Build RDMA Read WQEs for portion of a chunk
963d7cc7397SChuck Lever  * @info: context for RDMA Reads
964d7cc7397SChuck Lever  * @chunk: parsed Call chunk to pull
965d7cc7397SChuck Lever  * @offset: offset of region to pull
966d7cc7397SChuck Lever  * @length: length of region to pull
967d7cc7397SChuck Lever  *
968d7cc7397SChuck Lever  * Return values:
969d7cc7397SChuck Lever  *   %0: RDMA Read WQEs were successfully built
970d7cc7397SChuck Lever  *   %-EINVAL: there were not enough resources to finish
971d7cc7397SChuck Lever  *   %-ENOMEM: rdma_rw context pool was exhausted,
972d7cc7397SChuck Lever  *   %-ENOTCONN: posting failed (connection is lost),
973d7cc7397SChuck Lever  *   %-EIO: rdma_rw initialization failed (DMA mapping, etc).
974d7cc7397SChuck Lever  */
975d7cc7397SChuck Lever static int svc_rdma_read_chunk_range(struct svc_rdma_read_info *info,
976d7cc7397SChuck Lever 				     const struct svc_rdma_chunk *chunk,
977d7cc7397SChuck Lever 				     unsigned int offset, unsigned int length)
978d7cc7397SChuck Lever {
979d7cc7397SChuck Lever 	const struct svc_rdma_segment *segment;
980d7cc7397SChuck Lever 	int ret;
981d7cc7397SChuck Lever 
982d7cc7397SChuck Lever 	ret = -EINVAL;
983d7cc7397SChuck Lever 	pcl_for_each_segment(segment, chunk) {
984d7cc7397SChuck Lever 		struct svc_rdma_segment dummy;
985d7cc7397SChuck Lever 
986d7cc7397SChuck Lever 		if (offset > segment->rs_length) {
987d7cc7397SChuck Lever 			offset -= segment->rs_length;
988d7cc7397SChuck Lever 			continue;
989d7cc7397SChuck Lever 		}
990d7cc7397SChuck Lever 
991d7cc7397SChuck Lever 		dummy.rs_handle = segment->rs_handle;
992d7cc7397SChuck Lever 		dummy.rs_length = min_t(u32, length, segment->rs_length) - offset;
993d7cc7397SChuck Lever 		dummy.rs_offset = segment->rs_offset + offset;
994d7cc7397SChuck Lever 
995d7cc7397SChuck Lever 		ret = svc_rdma_build_read_segment(info, &dummy);
996d7cc7397SChuck Lever 		if (ret < 0)
997d7cc7397SChuck Lever 			break;
998d7cc7397SChuck Lever 
999d7cc7397SChuck Lever 		info->ri_totalbytes += dummy.rs_length;
1000d7cc7397SChuck Lever 		length -= dummy.rs_length;
1001d7cc7397SChuck Lever 		offset = 0;
1002d7cc7397SChuck Lever 	}
1003d7cc7397SChuck Lever 	return ret;
1004d7cc7397SChuck Lever }
1005d7cc7397SChuck Lever 
1006d7cc7397SChuck Lever /**
1007d7cc7397SChuck Lever  * svc_rdma_read_call_chunk - Build RDMA Read WQEs to pull a Long Message
1008d7cc7397SChuck Lever  * @info: context for RDMA Reads
1009d7cc7397SChuck Lever  *
1010d7cc7397SChuck Lever  * Return values:
1011d7cc7397SChuck Lever  *   %0: RDMA Read WQEs were successfully built
1012d7cc7397SChuck Lever  *   %-EINVAL: there were not enough resources to finish
1013d7cc7397SChuck Lever  *   %-ENOMEM: rdma_rw context pool was exhausted,
1014d7cc7397SChuck Lever  *   %-ENOTCONN: posting failed (connection is lost),
1015d7cc7397SChuck Lever  *   %-EIO: rdma_rw initialization failed (DMA mapping, etc).
1016d7cc7397SChuck Lever  */
1017d7cc7397SChuck Lever static int svc_rdma_read_call_chunk(struct svc_rdma_read_info *info)
1018d7cc7397SChuck Lever {
1019d7cc7397SChuck Lever 	struct svc_rdma_recv_ctxt *head = info->ri_readctxt;
1020d7cc7397SChuck Lever 	const struct svc_rdma_chunk *call_chunk =
1021d7cc7397SChuck Lever 			pcl_first_chunk(&head->rc_call_pcl);
1022d7cc7397SChuck Lever 	const struct svc_rdma_pcl *pcl = &head->rc_read_pcl;
1023d7cc7397SChuck Lever 	struct svc_rdma_chunk *chunk, *next;
1024d7cc7397SChuck Lever 	unsigned int start, length;
1025d7cc7397SChuck Lever 	int ret;
1026d7cc7397SChuck Lever 
1027d7cc7397SChuck Lever 	if (pcl_is_empty(pcl))
1028d7cc7397SChuck Lever 		return svc_rdma_build_read_chunk(info, call_chunk);
1029d7cc7397SChuck Lever 
1030d7cc7397SChuck Lever 	start = 0;
1031d7cc7397SChuck Lever 	chunk = pcl_first_chunk(pcl);
1032d7cc7397SChuck Lever 	length = chunk->ch_position;
1033d7cc7397SChuck Lever 	ret = svc_rdma_read_chunk_range(info, call_chunk, start, length);
1034d7cc7397SChuck Lever 	if (ret < 0)
1035d7cc7397SChuck Lever 		return ret;
1036d7cc7397SChuck Lever 
1037d7cc7397SChuck Lever 	pcl_for_each_chunk(chunk, pcl) {
1038d7cc7397SChuck Lever 		ret = svc_rdma_build_read_chunk(info, chunk);
1039d7cc7397SChuck Lever 		if (ret < 0)
1040d7cc7397SChuck Lever 			return ret;
1041d7cc7397SChuck Lever 
1042d7cc7397SChuck Lever 		next = pcl_next_chunk(pcl, chunk);
1043d7cc7397SChuck Lever 		if (!next)
1044d7cc7397SChuck Lever 			break;
1045d7cc7397SChuck Lever 
1046d7cc7397SChuck Lever 		start += length;
1047d7cc7397SChuck Lever 		length = next->ch_position - info->ri_totalbytes;
1048d7cc7397SChuck Lever 		ret = svc_rdma_read_chunk_range(info, call_chunk,
1049d7cc7397SChuck Lever 						start, length);
1050d7cc7397SChuck Lever 		if (ret < 0)
1051d7cc7397SChuck Lever 			return ret;
1052d7cc7397SChuck Lever 	}
1053d7cc7397SChuck Lever 
1054d7cc7397SChuck Lever 	start += length;
1055d7cc7397SChuck Lever 	length = call_chunk->ch_length - start;
1056d7cc7397SChuck Lever 	return svc_rdma_read_chunk_range(info, call_chunk, start, length);
1057d7cc7397SChuck Lever }
1058d7cc7397SChuck Lever 
1059d7cc7397SChuck Lever /**
1060d96962e6SChuck Lever  * svc_rdma_read_special - Build RDMA Read WQEs to pull a Long Message
1061d96962e6SChuck Lever  * @info: context for RDMA Reads
1062d96962e6SChuck Lever  *
1063d96962e6SChuck Lever  * The start of the data lands in the first page just after the
10647d81ee87SChuck Lever  * Transport header, and the rest lands in rqstp->rq_arg.pages.
1065026d958bSChuck Lever  *
1066026d958bSChuck Lever  * Assumptions:
1067d96962e6SChuck Lever  *	- A PZRC is never sent in an RDMA_MSG message, though it's
1068d96962e6SChuck Lever  *	  allowed by spec.
1069d96962e6SChuck Lever  *
1070d96962e6SChuck Lever  * Return values:
1071d96962e6SChuck Lever  *   %0: RDMA Read WQEs were successfully built
1072d96962e6SChuck Lever  *   %-EINVAL: client provided too many chunks or segments,
1073d96962e6SChuck Lever  *   %-ENOMEM: rdma_rw context pool was exhausted,
1074d96962e6SChuck Lever  *   %-ENOTCONN: posting failed (connection is lost),
1075d96962e6SChuck Lever  *   %-EIO: rdma_rw initialization failed (DMA mapping, etc).
1076026d958bSChuck Lever  */
1077d7cc7397SChuck Lever static noinline int svc_rdma_read_special(struct svc_rdma_read_info *info)
1078026d958bSChuck Lever {
10797d81ee87SChuck Lever 	struct xdr_buf *buf = &info->ri_rqst->rq_arg;
1080026d958bSChuck Lever 	int ret;
1081026d958bSChuck Lever 
1082d7cc7397SChuck Lever 	ret = svc_rdma_read_call_chunk(info);
1083026d958bSChuck Lever 	if (ret < 0)
1084026d958bSChuck Lever 		goto out;
1085026d958bSChuck Lever 
1086bafe9c27SChuck Lever 	buf->len += info->ri_totalbytes;
1087bafe9c27SChuck Lever 	buf->buflen += info->ri_totalbytes;
1088026d958bSChuck Lever 
10897d81ee87SChuck Lever 	buf->head[0].iov_base = page_address(info->ri_rqst->rq_pages[0]);
1090bafe9c27SChuck Lever 	buf->head[0].iov_len = min_t(size_t, PAGE_SIZE, info->ri_totalbytes);
10917d81ee87SChuck Lever 	buf->pages = &info->ri_rqst->rq_pages[1];
1092bafe9c27SChuck Lever 	buf->page_len = info->ri_totalbytes - buf->head[0].iov_len;
1093026d958bSChuck Lever 
1094026d958bSChuck Lever out:
1095026d958bSChuck Lever 	return ret;
1096026d958bSChuck Lever }
1097026d958bSChuck Lever 
1098026d958bSChuck Lever /**
1099d96962e6SChuck Lever  * svc_rdma_process_read_list - Pull list of Read chunks from the client
1100026d958bSChuck Lever  * @rdma: controlling RDMA transport
1101026d958bSChuck Lever  * @rqstp: set of pages to use as Read sink buffers
1102026d958bSChuck Lever  * @head: pages under I/O collect here
1103026d958bSChuck Lever  *
1104d96962e6SChuck Lever  * The RPC/RDMA protocol assumes that the upper layer's XDR decoders
1105d96962e6SChuck Lever  * pull each Read chunk as they decode an incoming RPC message.
1106026d958bSChuck Lever  *
1107d96962e6SChuck Lever  * On Linux, however, the server needs to have a fully-constructed RPC
1108d96962e6SChuck Lever  * message in rqstp->rq_arg when there is a positive return code from
1109d96962e6SChuck Lever  * ->xpo_recvfrom. So the Read list is safety-checked immediately when
1110d96962e6SChuck Lever  * it is received, then here the whole Read list is pulled all at once.
1111d96962e6SChuck Lever  * The ingress RPC message is fully reconstructed once all associated
1112d96962e6SChuck Lever  * RDMA Reads have completed.
1113d96962e6SChuck Lever  *
1114d96962e6SChuck Lever  * Return values:
1115d96962e6SChuck Lever  *   %1: all needed RDMA Reads were posted successfully,
1116d96962e6SChuck Lever  *   %-EINVAL: client provided too many chunks or segments,
1117d96962e6SChuck Lever  *   %-ENOMEM: rdma_rw context pool was exhausted,
1118d96962e6SChuck Lever  *   %-ENOTCONN: posting failed (connection is lost),
1119d96962e6SChuck Lever  *   %-EIO: rdma_rw initialization failed (DMA mapping, etc).
1120026d958bSChuck Lever  */
1121d96962e6SChuck Lever int svc_rdma_process_read_list(struct svcxprt_rdma *rdma,
1122d96962e6SChuck Lever 			       struct svc_rqst *rqstp,
1123d96962e6SChuck Lever 			       struct svc_rdma_recv_ctxt *head)
1124026d958bSChuck Lever {
1125026d958bSChuck Lever 	struct svc_rdma_read_info *info;
1126b704be09SChuck Lever 	struct svc_rdma_chunk_ctxt *cc;
1127026d958bSChuck Lever 	int ret;
1128026d958bSChuck Lever 
1129026d958bSChuck Lever 	info = svc_rdma_read_info_alloc(rdma);
1130026d958bSChuck Lever 	if (!info)
1131026d958bSChuck Lever 		return -ENOMEM;
1132b704be09SChuck Lever 	cc = &info->ri_cc;
1133d96962e6SChuck Lever 	info->ri_rqst = rqstp;
1134026d958bSChuck Lever 	info->ri_readctxt = head;
11353316f063SChuck Lever 	info->ri_pageno = 0;
11363316f063SChuck Lever 	info->ri_pageoff = 0;
1137bafe9c27SChuck Lever 	info->ri_totalbytes = 0;
1138026d958bSChuck Lever 
1139d7cc7397SChuck Lever 	if (pcl_is_empty(&head->rc_call_pcl)) {
1140d7cc7397SChuck Lever 		if (head->rc_read_pcl.cl_count == 1)
1141d7cc7397SChuck Lever 			ret = svc_rdma_read_data_item(info);
1142026d958bSChuck Lever 		else
1143d7cc7397SChuck Lever 			ret = svc_rdma_read_multiple_chunks(info);
1144d7cc7397SChuck Lever 	} else
1145d96962e6SChuck Lever 		ret = svc_rdma_read_special(info);
1146026d958bSChuck Lever 	if (ret < 0)
114707d0ff3bSChuck Lever 		goto out_err;
1148026d958bSChuck Lever 
1149b704be09SChuck Lever 	trace_svcrdma_post_read_chunk(&cc->cc_cid, cc->cc_sqecount);
11507d81ee87SChuck Lever 	init_completion(&cc->cc_done);
1151b704be09SChuck Lever 	ret = svc_rdma_post_chunk_ctxt(cc);
1152026d958bSChuck Lever 	if (ret < 0)
115307d0ff3bSChuck Lever 		goto out_err;
11547d81ee87SChuck Lever 
11557d81ee87SChuck Lever 	ret = 1;
11567d81ee87SChuck Lever 	wait_for_completion(&cc->cc_done);
11577d81ee87SChuck Lever 	if (cc->cc_status != IB_WC_SUCCESS)
11587d81ee87SChuck Lever 		ret = -EIO;
11597d81ee87SChuck Lever 
11607d81ee87SChuck Lever 	/* rq_respages starts after the last arg page */
11617d81ee87SChuck Lever 	rqstp->rq_respages = &rqstp->rq_pages[head->rc_page_count];
11627d81ee87SChuck Lever 	rqstp->rq_next_page = rqstp->rq_respages + 1;
11637d81ee87SChuck Lever 
11647d81ee87SChuck Lever 	/* Ensure svc_rdma_recv_ctxt_put() does not try to release pages */
11657d81ee87SChuck Lever 	head->rc_page_count = 0;
116607d0ff3bSChuck Lever 
116707d0ff3bSChuck Lever out_err:
1168026d958bSChuck Lever 	svc_rdma_read_info_free(info);
1169026d958bSChuck Lever 	return ret;
1170026d958bSChuck Lever }
1171