xref: /openbmc/linux/net/sunrpc/xprtrdma/svc_rdma_rw.c (revision f4e53e1ce3e56a799c47fef5f4f94cb815b52804)
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 
10f13193f5SChuck Lever #include <linux/sunrpc/rpc_rdma.h>
11f13193f5SChuck Lever #include <linux/sunrpc/svc_rdma.h>
12f13193f5SChuck Lever #include <linux/sunrpc/debug.h>
13f13193f5SChuck Lever 
1498895edbSChuck Lever #include "xprt_rdma.h"
1598895edbSChuck Lever #include <trace/events/rpcrdma.h>
16f13193f5SChuck Lever 
17f13193f5SChuck Lever #define RPCDBG_FACILITY	RPCDBG_SVCXPRT
18f13193f5SChuck Lever 
19026d958bSChuck Lever static void svc_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc);
20026d958bSChuck Lever static void svc_rdma_wc_read_done(struct ib_cq *cq, struct ib_wc *wc);
21026d958bSChuck Lever 
22f13193f5SChuck Lever /* Each R/W context contains state for one chain of RDMA Read or
23f13193f5SChuck Lever  * Write Work Requests.
24f13193f5SChuck Lever  *
25f13193f5SChuck Lever  * Each WR chain handles a single contiguous server-side buffer,
26f13193f5SChuck Lever  * because scatterlist entries after the first have to start on
27f13193f5SChuck Lever  * page alignment. xdr_buf iovecs cannot guarantee alignment.
28f13193f5SChuck Lever  *
29f13193f5SChuck Lever  * Each WR chain handles only one R_key. Each RPC-over-RDMA segment
30f13193f5SChuck Lever  * from a client may contain a unique R_key, so each WR chain moves
31f13193f5SChuck Lever  * up to one segment at a time.
32f13193f5SChuck Lever  *
33f13193f5SChuck Lever  * The scatterlist makes this data structure over 4KB in size. To
34f13193f5SChuck Lever  * make it less likely to fail, and to handle the allocation for
35f13193f5SChuck Lever  * smaller I/O requests without disabling bottom-halves, these
36f13193f5SChuck Lever  * contexts are created on demand, but cached and reused until the
37f13193f5SChuck Lever  * controlling svcxprt_rdma is destroyed.
38f13193f5SChuck Lever  */
39f13193f5SChuck Lever struct svc_rdma_rw_ctxt {
40f13193f5SChuck Lever 	struct list_head	rw_list;
41f13193f5SChuck Lever 	struct rdma_rw_ctx	rw_ctx;
422abfbe7eSChuck Lever 	unsigned int		rw_nents;
43f13193f5SChuck Lever 	struct sg_table		rw_sg_table;
44c0fb23f8SGustavo A. R. Silva 	struct scatterlist	rw_first_sgl[];
45f13193f5SChuck Lever };
46f13193f5SChuck Lever 
47f13193f5SChuck Lever static inline struct svc_rdma_rw_ctxt *
48f13193f5SChuck Lever svc_rdma_next_ctxt(struct list_head *list)
49f13193f5SChuck Lever {
50f13193f5SChuck Lever 	return list_first_entry_or_null(list, struct svc_rdma_rw_ctxt,
51f13193f5SChuck Lever 					rw_list);
52f13193f5SChuck Lever }
53f13193f5SChuck Lever 
54f13193f5SChuck Lever static struct svc_rdma_rw_ctxt *
55f13193f5SChuck Lever svc_rdma_get_rw_ctxt(struct svcxprt_rdma *rdma, unsigned int sges)
56f13193f5SChuck Lever {
57f13193f5SChuck Lever 	struct svc_rdma_rw_ctxt *ctxt;
58f13193f5SChuck Lever 
59f13193f5SChuck Lever 	spin_lock(&rdma->sc_rw_ctxt_lock);
60f13193f5SChuck Lever 
61f13193f5SChuck Lever 	ctxt = svc_rdma_next_ctxt(&rdma->sc_rw_ctxts);
62f13193f5SChuck Lever 	if (ctxt) {
63f13193f5SChuck Lever 		list_del(&ctxt->rw_list);
64f13193f5SChuck Lever 		spin_unlock(&rdma->sc_rw_ctxt_lock);
65f13193f5SChuck Lever 	} else {
66f13193f5SChuck Lever 		spin_unlock(&rdma->sc_rw_ctxt_lock);
6714cfbd94SGustavo A. R. Silva 		ctxt = kmalloc(struct_size(ctxt, rw_first_sgl, SG_CHUNK_SIZE),
68f13193f5SChuck Lever 			       GFP_KERNEL);
69f13193f5SChuck Lever 		if (!ctxt)
70*f4e53e1cSChuck Lever 			goto out_noctx;
71f13193f5SChuck Lever 		INIT_LIST_HEAD(&ctxt->rw_list);
72f13193f5SChuck Lever 	}
73f13193f5SChuck Lever 
74f13193f5SChuck Lever 	ctxt->rw_sg_table.sgl = ctxt->rw_first_sgl;
75f13193f5SChuck Lever 	if (sg_alloc_table_chained(&ctxt->rw_sg_table, sges,
764635873cSMing Lei 				   ctxt->rw_sg_table.sgl,
77*f4e53e1cSChuck Lever 				   SG_CHUNK_SIZE))
78*f4e53e1cSChuck Lever 		goto out_free;
79f13193f5SChuck Lever 	return ctxt;
80*f4e53e1cSChuck Lever 
81*f4e53e1cSChuck Lever out_free:
82*f4e53e1cSChuck Lever 	kfree(ctxt);
83*f4e53e1cSChuck Lever out_noctx:
84*f4e53e1cSChuck Lever 	trace_svcrdma_no_rwctx_err(rdma, sges);
85*f4e53e1cSChuck Lever 	return NULL;
86f13193f5SChuck Lever }
87f13193f5SChuck Lever 
88f13193f5SChuck Lever static void svc_rdma_put_rw_ctxt(struct svcxprt_rdma *rdma,
89f13193f5SChuck Lever 				 struct svc_rdma_rw_ctxt *ctxt)
90f13193f5SChuck Lever {
914635873cSMing Lei 	sg_free_table_chained(&ctxt->rw_sg_table, SG_CHUNK_SIZE);
92f13193f5SChuck Lever 
93f13193f5SChuck Lever 	spin_lock(&rdma->sc_rw_ctxt_lock);
94f13193f5SChuck Lever 	list_add(&ctxt->rw_list, &rdma->sc_rw_ctxts);
95f13193f5SChuck Lever 	spin_unlock(&rdma->sc_rw_ctxt_lock);
96f13193f5SChuck Lever }
97f13193f5SChuck Lever 
98f13193f5SChuck Lever /**
99f13193f5SChuck Lever  * svc_rdma_destroy_rw_ctxts - Free accumulated R/W contexts
100f13193f5SChuck Lever  * @rdma: transport about to be destroyed
101f13193f5SChuck Lever  *
102f13193f5SChuck Lever  */
103f13193f5SChuck Lever void svc_rdma_destroy_rw_ctxts(struct svcxprt_rdma *rdma)
104f13193f5SChuck Lever {
105f13193f5SChuck Lever 	struct svc_rdma_rw_ctxt *ctxt;
106f13193f5SChuck Lever 
107f13193f5SChuck Lever 	while ((ctxt = svc_rdma_next_ctxt(&rdma->sc_rw_ctxts)) != NULL) {
108f13193f5SChuck Lever 		list_del(&ctxt->rw_list);
109f13193f5SChuck Lever 		kfree(ctxt);
110f13193f5SChuck Lever 	}
111f13193f5SChuck Lever }
112f13193f5SChuck Lever 
1132abfbe7eSChuck Lever /**
1142abfbe7eSChuck Lever  * svc_rdma_rw_ctx_init - Prepare a R/W context for I/O
1152abfbe7eSChuck Lever  * @rdma: controlling transport instance
1162abfbe7eSChuck Lever  * @ctxt: R/W context to prepare
1172abfbe7eSChuck Lever  * @offset: RDMA offset
1182abfbe7eSChuck Lever  * @handle: RDMA tag/handle
1192abfbe7eSChuck Lever  * @direction: I/O direction
1202abfbe7eSChuck Lever  *
1212abfbe7eSChuck Lever  * Returns on success, the number of WQEs that will be needed
1222abfbe7eSChuck Lever  * on the workqueue, or a negative errno.
1232abfbe7eSChuck Lever  */
1242abfbe7eSChuck Lever static int svc_rdma_rw_ctx_init(struct svcxprt_rdma *rdma,
1252abfbe7eSChuck Lever 				struct svc_rdma_rw_ctxt *ctxt,
1262abfbe7eSChuck Lever 				u64 offset, u32 handle,
1272abfbe7eSChuck Lever 				enum dma_data_direction direction)
1282abfbe7eSChuck Lever {
1292abfbe7eSChuck Lever 	int ret;
1302abfbe7eSChuck Lever 
1312abfbe7eSChuck Lever 	ret = rdma_rw_ctx_init(&ctxt->rw_ctx, rdma->sc_qp, rdma->sc_port_num,
1322abfbe7eSChuck Lever 			       ctxt->rw_sg_table.sgl, ctxt->rw_nents,
1332abfbe7eSChuck Lever 			       0, offset, handle, direction);
1342abfbe7eSChuck Lever 	if (unlikely(ret < 0)) {
1352abfbe7eSChuck Lever 		svc_rdma_put_rw_ctxt(rdma, ctxt);
1362abfbe7eSChuck Lever 		trace_svcrdma_dma_map_rw_err(rdma, ctxt->rw_nents, ret);
1372abfbe7eSChuck Lever 	}
1382abfbe7eSChuck Lever 	return ret;
1392abfbe7eSChuck Lever }
1402abfbe7eSChuck Lever 
141f13193f5SChuck Lever /* A chunk context tracks all I/O for moving one Read or Write
142f13193f5SChuck Lever  * chunk. This is a a set of rdma_rw's that handle data movement
143f13193f5SChuck Lever  * for all segments of one chunk.
144f13193f5SChuck Lever  *
145f13193f5SChuck Lever  * These are small, acquired with a single allocator call, and
146f13193f5SChuck Lever  * no more than one is needed per chunk. They are allocated on
147f13193f5SChuck Lever  * demand, and not cached.
148f13193f5SChuck Lever  */
149f13193f5SChuck Lever struct svc_rdma_chunk_ctxt {
150f13193f5SChuck Lever 	struct ib_cqe		cc_cqe;
151f13193f5SChuck Lever 	struct svcxprt_rdma	*cc_rdma;
152f13193f5SChuck Lever 	struct list_head	cc_rwctxts;
153f13193f5SChuck Lever 	int			cc_sqecount;
154f13193f5SChuck Lever };
155f13193f5SChuck Lever 
156f13193f5SChuck Lever static void svc_rdma_cc_init(struct svcxprt_rdma *rdma,
15735a30fc3SChuck Lever 			     struct svc_rdma_chunk_ctxt *cc)
158f13193f5SChuck Lever {
159f13193f5SChuck Lever 	cc->cc_rdma = rdma;
160f13193f5SChuck Lever 	svc_xprt_get(&rdma->sc_xprt);
161f13193f5SChuck Lever 
162f13193f5SChuck Lever 	INIT_LIST_HEAD(&cc->cc_rwctxts);
163f13193f5SChuck Lever 	cc->cc_sqecount = 0;
164f13193f5SChuck Lever }
165f13193f5SChuck Lever 
16635a30fc3SChuck Lever static void svc_rdma_cc_release(struct svc_rdma_chunk_ctxt *cc,
16735a30fc3SChuck Lever 				enum dma_data_direction dir)
168f13193f5SChuck Lever {
169f13193f5SChuck Lever 	struct svcxprt_rdma *rdma = cc->cc_rdma;
170f13193f5SChuck Lever 	struct svc_rdma_rw_ctxt *ctxt;
171f13193f5SChuck Lever 
172f13193f5SChuck Lever 	while ((ctxt = svc_rdma_next_ctxt(&cc->cc_rwctxts)) != NULL) {
173f13193f5SChuck Lever 		list_del(&ctxt->rw_list);
174f13193f5SChuck Lever 
175f13193f5SChuck Lever 		rdma_rw_ctx_destroy(&ctxt->rw_ctx, rdma->sc_qp,
176f13193f5SChuck Lever 				    rdma->sc_port_num, ctxt->rw_sg_table.sgl,
17735a30fc3SChuck Lever 				    ctxt->rw_nents, dir);
178f13193f5SChuck Lever 		svc_rdma_put_rw_ctxt(rdma, ctxt);
179f13193f5SChuck Lever 	}
180f13193f5SChuck Lever 	svc_xprt_put(&rdma->sc_xprt);
181f13193f5SChuck Lever }
182f13193f5SChuck Lever 
183f13193f5SChuck Lever /* State for sending a Write or Reply chunk.
184f13193f5SChuck Lever  *  - Tracks progress of writing one chunk over all its segments
185f13193f5SChuck Lever  *  - Stores arguments for the SGL constructor functions
186f13193f5SChuck Lever  */
187f13193f5SChuck Lever struct svc_rdma_write_info {
188f13193f5SChuck Lever 	/* write state of this chunk */
189f13193f5SChuck Lever 	unsigned int		wi_seg_off;
190f13193f5SChuck Lever 	unsigned int		wi_seg_no;
191f13193f5SChuck Lever 	unsigned int		wi_nsegs;
192f13193f5SChuck Lever 	__be32			*wi_segs;
193f13193f5SChuck Lever 
194f13193f5SChuck Lever 	/* SGL constructor arguments */
195f13193f5SChuck Lever 	struct xdr_buf		*wi_xdr;
196f13193f5SChuck Lever 	unsigned char		*wi_base;
197f13193f5SChuck Lever 	unsigned int		wi_next_off;
198f13193f5SChuck Lever 
199f13193f5SChuck Lever 	struct svc_rdma_chunk_ctxt	wi_cc;
200f13193f5SChuck Lever };
201f13193f5SChuck Lever 
202f13193f5SChuck Lever static struct svc_rdma_write_info *
203f13193f5SChuck Lever svc_rdma_write_info_alloc(struct svcxprt_rdma *rdma, __be32 *chunk)
204f13193f5SChuck Lever {
205f13193f5SChuck Lever 	struct svc_rdma_write_info *info;
206f13193f5SChuck Lever 
207f13193f5SChuck Lever 	info = kmalloc(sizeof(*info), GFP_KERNEL);
208f13193f5SChuck Lever 	if (!info)
209f13193f5SChuck Lever 		return info;
210f13193f5SChuck Lever 
211f13193f5SChuck Lever 	info->wi_seg_off = 0;
212f13193f5SChuck Lever 	info->wi_seg_no = 0;
213f13193f5SChuck Lever 	info->wi_nsegs = be32_to_cpup(++chunk);
214f13193f5SChuck Lever 	info->wi_segs = ++chunk;
21535a30fc3SChuck Lever 	svc_rdma_cc_init(rdma, &info->wi_cc);
216026d958bSChuck Lever 	info->wi_cc.cc_cqe.done = svc_rdma_write_done;
217f13193f5SChuck Lever 	return info;
218f13193f5SChuck Lever }
219f13193f5SChuck Lever 
220f13193f5SChuck Lever static void svc_rdma_write_info_free(struct svc_rdma_write_info *info)
221f13193f5SChuck Lever {
22235a30fc3SChuck Lever 	svc_rdma_cc_release(&info->wi_cc, DMA_TO_DEVICE);
223f13193f5SChuck Lever 	kfree(info);
224f13193f5SChuck Lever }
225f13193f5SChuck Lever 
226f13193f5SChuck Lever /**
227f13193f5SChuck Lever  * svc_rdma_write_done - Write chunk completion
228f13193f5SChuck Lever  * @cq: controlling Completion Queue
229f13193f5SChuck Lever  * @wc: Work Completion
230f13193f5SChuck Lever  *
231f13193f5SChuck Lever  * Pages under I/O are freed by a subsequent Send completion.
232f13193f5SChuck Lever  */
233f13193f5SChuck Lever static void svc_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
234f13193f5SChuck Lever {
235f13193f5SChuck Lever 	struct ib_cqe *cqe = wc->wr_cqe;
236f13193f5SChuck Lever 	struct svc_rdma_chunk_ctxt *cc =
237f13193f5SChuck Lever 			container_of(cqe, struct svc_rdma_chunk_ctxt, cc_cqe);
238f13193f5SChuck Lever 	struct svcxprt_rdma *rdma = cc->cc_rdma;
239f13193f5SChuck Lever 	struct svc_rdma_write_info *info =
240f13193f5SChuck Lever 			container_of(cc, struct svc_rdma_write_info, wi_cc);
241f13193f5SChuck Lever 
242bd2abef3SChuck Lever 	trace_svcrdma_wc_write(wc);
243bd2abef3SChuck Lever 
244f13193f5SChuck Lever 	atomic_add(cc->cc_sqecount, &rdma->sc_sq_avail);
245f13193f5SChuck Lever 	wake_up(&rdma->sc_send_wait);
246f13193f5SChuck Lever 
2478820bcaaSChuck Lever 	if (unlikely(wc->status != IB_WC_SUCCESS))
248f13193f5SChuck Lever 		set_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags);
249f13193f5SChuck Lever 
250f13193f5SChuck Lever 	svc_rdma_write_info_free(info);
251f13193f5SChuck Lever }
252f13193f5SChuck Lever 
253026d958bSChuck Lever /* State for pulling a Read chunk.
254026d958bSChuck Lever  */
255026d958bSChuck Lever struct svc_rdma_read_info {
256ecf85b23SChuck Lever 	struct svc_rdma_recv_ctxt	*ri_readctxt;
257026d958bSChuck Lever 	unsigned int			ri_position;
258026d958bSChuck Lever 	unsigned int			ri_pageno;
259026d958bSChuck Lever 	unsigned int			ri_pageoff;
260026d958bSChuck Lever 	unsigned int			ri_chunklen;
261026d958bSChuck Lever 
262026d958bSChuck Lever 	struct svc_rdma_chunk_ctxt	ri_cc;
263026d958bSChuck Lever };
264026d958bSChuck Lever 
265026d958bSChuck Lever static struct svc_rdma_read_info *
266026d958bSChuck Lever svc_rdma_read_info_alloc(struct svcxprt_rdma *rdma)
267026d958bSChuck Lever {
268026d958bSChuck Lever 	struct svc_rdma_read_info *info;
269026d958bSChuck Lever 
270026d958bSChuck Lever 	info = kmalloc(sizeof(*info), GFP_KERNEL);
271026d958bSChuck Lever 	if (!info)
272026d958bSChuck Lever 		return info;
273026d958bSChuck Lever 
27435a30fc3SChuck Lever 	svc_rdma_cc_init(rdma, &info->ri_cc);
275026d958bSChuck Lever 	info->ri_cc.cc_cqe.done = svc_rdma_wc_read_done;
276026d958bSChuck Lever 	return info;
277026d958bSChuck Lever }
278026d958bSChuck Lever 
279026d958bSChuck Lever static void svc_rdma_read_info_free(struct svc_rdma_read_info *info)
280026d958bSChuck Lever {
28135a30fc3SChuck Lever 	svc_rdma_cc_release(&info->ri_cc, DMA_FROM_DEVICE);
282026d958bSChuck Lever 	kfree(info);
283026d958bSChuck Lever }
284026d958bSChuck Lever 
285026d958bSChuck Lever /**
286026d958bSChuck Lever  * svc_rdma_wc_read_done - Handle completion of an RDMA Read ctx
287026d958bSChuck Lever  * @cq: controlling Completion Queue
288026d958bSChuck Lever  * @wc: Work Completion
289026d958bSChuck Lever  *
290026d958bSChuck Lever  */
291026d958bSChuck Lever static void svc_rdma_wc_read_done(struct ib_cq *cq, struct ib_wc *wc)
292026d958bSChuck Lever {
293026d958bSChuck Lever 	struct ib_cqe *cqe = wc->wr_cqe;
294026d958bSChuck Lever 	struct svc_rdma_chunk_ctxt *cc =
295026d958bSChuck Lever 			container_of(cqe, struct svc_rdma_chunk_ctxt, cc_cqe);
296026d958bSChuck Lever 	struct svcxprt_rdma *rdma = cc->cc_rdma;
297026d958bSChuck Lever 	struct svc_rdma_read_info *info =
298026d958bSChuck Lever 			container_of(cc, struct svc_rdma_read_info, ri_cc);
299026d958bSChuck Lever 
300bd2abef3SChuck Lever 	trace_svcrdma_wc_read(wc);
301bd2abef3SChuck Lever 
302026d958bSChuck Lever 	atomic_add(cc->cc_sqecount, &rdma->sc_sq_avail);
303026d958bSChuck Lever 	wake_up(&rdma->sc_send_wait);
304026d958bSChuck Lever 
305026d958bSChuck Lever 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
306026d958bSChuck Lever 		set_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags);
3071e5f4160SChuck Lever 		svc_rdma_recv_ctxt_put(rdma, info->ri_readctxt);
308026d958bSChuck Lever 	} else {
309026d958bSChuck Lever 		spin_lock(&rdma->sc_rq_dto_lock);
310ecf85b23SChuck Lever 		list_add_tail(&info->ri_readctxt->rc_list,
311026d958bSChuck Lever 			      &rdma->sc_read_complete_q);
31295503d29SJ. Bruce Fields 		/* Note the unlock pairs with the smp_rmb in svc_xprt_ready: */
31395503d29SJ. Bruce Fields 		set_bit(XPT_DATA, &rdma->sc_xprt.xpt_flags);
314026d958bSChuck Lever 		spin_unlock(&rdma->sc_rq_dto_lock);
315026d958bSChuck Lever 
316026d958bSChuck Lever 		svc_xprt_enqueue(&rdma->sc_xprt);
317026d958bSChuck Lever 	}
318026d958bSChuck Lever 
319026d958bSChuck Lever 	svc_rdma_read_info_free(info);
320026d958bSChuck Lever }
321026d958bSChuck Lever 
322f13193f5SChuck Lever /* This function sleeps when the transport's Send Queue is congested.
323f13193f5SChuck Lever  *
324f13193f5SChuck Lever  * Assumptions:
325f13193f5SChuck Lever  * - If ib_post_send() succeeds, only one completion is expected,
326f13193f5SChuck Lever  *   even if one or more WRs are flushed. This is true when posting
327f13193f5SChuck Lever  *   an rdma_rw_ctx or when posting a single signaled WR.
328f13193f5SChuck Lever  */
329f13193f5SChuck Lever static int svc_rdma_post_chunk_ctxt(struct svc_rdma_chunk_ctxt *cc)
330f13193f5SChuck Lever {
331f13193f5SChuck Lever 	struct svcxprt_rdma *rdma = cc->cc_rdma;
332f13193f5SChuck Lever 	struct svc_xprt *xprt = &rdma->sc_xprt;
333d34ac5cdSBart Van Assche 	struct ib_send_wr *first_wr;
334d34ac5cdSBart Van Assche 	const struct ib_send_wr *bad_wr;
335f13193f5SChuck Lever 	struct list_head *tmp;
336f13193f5SChuck Lever 	struct ib_cqe *cqe;
337f13193f5SChuck Lever 	int ret;
338f13193f5SChuck Lever 
339107c1d0aSChuck Lever 	if (cc->cc_sqecount > rdma->sc_sq_depth)
340107c1d0aSChuck Lever 		return -EINVAL;
341107c1d0aSChuck Lever 
342f13193f5SChuck Lever 	first_wr = NULL;
343f13193f5SChuck Lever 	cqe = &cc->cc_cqe;
344f13193f5SChuck Lever 	list_for_each(tmp, &cc->cc_rwctxts) {
345f13193f5SChuck Lever 		struct svc_rdma_rw_ctxt *ctxt;
346f13193f5SChuck Lever 
347f13193f5SChuck Lever 		ctxt = list_entry(tmp, struct svc_rdma_rw_ctxt, rw_list);
348f13193f5SChuck Lever 		first_wr = rdma_rw_ctx_wrs(&ctxt->rw_ctx, rdma->sc_qp,
349f13193f5SChuck Lever 					   rdma->sc_port_num, cqe, first_wr);
350f13193f5SChuck Lever 		cqe = NULL;
351f13193f5SChuck Lever 	}
352f13193f5SChuck Lever 
353f13193f5SChuck Lever 	do {
354f13193f5SChuck Lever 		if (atomic_sub_return(cc->cc_sqecount,
355f13193f5SChuck Lever 				      &rdma->sc_sq_avail) > 0) {
3565d85a822SJason Gunthorpe 			ret = ib_post_send(rdma->sc_qp, first_wr, &bad_wr);
357f13193f5SChuck Lever 			if (ret)
358f13193f5SChuck Lever 				break;
359f13193f5SChuck Lever 			return 0;
360f13193f5SChuck Lever 		}
361f13193f5SChuck Lever 
362bd2abef3SChuck Lever 		trace_svcrdma_sq_full(rdma);
363f13193f5SChuck Lever 		atomic_add(cc->cc_sqecount, &rdma->sc_sq_avail);
364f13193f5SChuck Lever 		wait_event(rdma->sc_send_wait,
365f13193f5SChuck Lever 			   atomic_read(&rdma->sc_sq_avail) > cc->cc_sqecount);
366bd2abef3SChuck Lever 		trace_svcrdma_sq_retry(rdma);
367f13193f5SChuck Lever 	} while (1);
368f13193f5SChuck Lever 
369e28b4fc6SChuck Lever 	trace_svcrdma_sq_post_err(rdma, ret);
370f13193f5SChuck Lever 	set_bit(XPT_CLOSE, &xprt->xpt_flags);
371f13193f5SChuck Lever 
372f13193f5SChuck Lever 	/* If even one was posted, there will be a completion. */
373f13193f5SChuck Lever 	if (bad_wr != first_wr)
374f13193f5SChuck Lever 		return 0;
375f13193f5SChuck Lever 
376f13193f5SChuck Lever 	atomic_add(cc->cc_sqecount, &rdma->sc_sq_avail);
377f13193f5SChuck Lever 	wake_up(&rdma->sc_send_wait);
378f13193f5SChuck Lever 	return -ENOTCONN;
379f13193f5SChuck Lever }
380f13193f5SChuck Lever 
381f13193f5SChuck Lever /* Build and DMA-map an SGL that covers one kvec in an xdr_buf
382f13193f5SChuck Lever  */
383f13193f5SChuck Lever static void svc_rdma_vec_to_sg(struct svc_rdma_write_info *info,
384f13193f5SChuck Lever 			       unsigned int len,
385f13193f5SChuck Lever 			       struct svc_rdma_rw_ctxt *ctxt)
386f13193f5SChuck Lever {
387f13193f5SChuck Lever 	struct scatterlist *sg = ctxt->rw_sg_table.sgl;
388f13193f5SChuck Lever 
389f13193f5SChuck Lever 	sg_set_buf(&sg[0], info->wi_base, len);
390f13193f5SChuck Lever 	info->wi_base += len;
391f13193f5SChuck Lever 
392f13193f5SChuck Lever 	ctxt->rw_nents = 1;
393f13193f5SChuck Lever }
394f13193f5SChuck Lever 
395f13193f5SChuck Lever /* Build and DMA-map an SGL that covers part of an xdr_buf's pagelist.
396f13193f5SChuck Lever  */
397f13193f5SChuck Lever static void svc_rdma_pagelist_to_sg(struct svc_rdma_write_info *info,
398f13193f5SChuck Lever 				    unsigned int remaining,
399f13193f5SChuck Lever 				    struct svc_rdma_rw_ctxt *ctxt)
400f13193f5SChuck Lever {
401f13193f5SChuck Lever 	unsigned int sge_no, sge_bytes, page_off, page_no;
402f13193f5SChuck Lever 	struct xdr_buf *xdr = info->wi_xdr;
403f13193f5SChuck Lever 	struct scatterlist *sg;
404f13193f5SChuck Lever 	struct page **page;
405f13193f5SChuck Lever 
40691b022ecSChuck Lever 	page_off = info->wi_next_off + xdr->page_base;
40791b022ecSChuck Lever 	page_no = page_off >> PAGE_SHIFT;
40891b022ecSChuck Lever 	page_off = offset_in_page(page_off);
409f13193f5SChuck Lever 	page = xdr->pages + page_no;
410f13193f5SChuck Lever 	info->wi_next_off += remaining;
411f13193f5SChuck Lever 	sg = ctxt->rw_sg_table.sgl;
412f13193f5SChuck Lever 	sge_no = 0;
413f13193f5SChuck Lever 	do {
414f13193f5SChuck Lever 		sge_bytes = min_t(unsigned int, remaining,
415f13193f5SChuck Lever 				  PAGE_SIZE - page_off);
416f13193f5SChuck Lever 		sg_set_page(sg, *page, sge_bytes, page_off);
417f13193f5SChuck Lever 
418f13193f5SChuck Lever 		remaining -= sge_bytes;
419f13193f5SChuck Lever 		sg = sg_next(sg);
420f13193f5SChuck Lever 		page_off = 0;
421f13193f5SChuck Lever 		sge_no++;
422f13193f5SChuck Lever 		page++;
423f13193f5SChuck Lever 	} while (remaining);
424f13193f5SChuck Lever 
425f13193f5SChuck Lever 	ctxt->rw_nents = sge_no;
426f13193f5SChuck Lever }
427f13193f5SChuck Lever 
428f13193f5SChuck Lever /* Construct RDMA Write WRs to send a portion of an xdr_buf containing
429f13193f5SChuck Lever  * an RPC Reply.
430f13193f5SChuck Lever  */
431f13193f5SChuck Lever static int
432f13193f5SChuck Lever svc_rdma_build_writes(struct svc_rdma_write_info *info,
433f13193f5SChuck Lever 		      void (*constructor)(struct svc_rdma_write_info *info,
434f13193f5SChuck Lever 					  unsigned int len,
435f13193f5SChuck Lever 					  struct svc_rdma_rw_ctxt *ctxt),
436f13193f5SChuck Lever 		      unsigned int remaining)
437f13193f5SChuck Lever {
438f13193f5SChuck Lever 	struct svc_rdma_chunk_ctxt *cc = &info->wi_cc;
439f13193f5SChuck Lever 	struct svcxprt_rdma *rdma = cc->cc_rdma;
440f13193f5SChuck Lever 	struct svc_rdma_rw_ctxt *ctxt;
441f13193f5SChuck Lever 	__be32 *seg;
442f13193f5SChuck Lever 	int ret;
443f13193f5SChuck Lever 
444f13193f5SChuck Lever 	seg = info->wi_segs + info->wi_seg_no * rpcrdma_segment_maxsz;
445f13193f5SChuck Lever 	do {
446f13193f5SChuck Lever 		unsigned int write_len;
447f13193f5SChuck Lever 		u32 seg_length, seg_handle;
448f13193f5SChuck Lever 		u64 seg_offset;
449f13193f5SChuck Lever 
450f13193f5SChuck Lever 		if (info->wi_seg_no >= info->wi_nsegs)
451f13193f5SChuck Lever 			goto out_overflow;
452f13193f5SChuck Lever 
453f13193f5SChuck Lever 		seg_handle = be32_to_cpup(seg);
454f13193f5SChuck Lever 		seg_length = be32_to_cpup(seg + 1);
455f13193f5SChuck Lever 		xdr_decode_hyper(seg + 2, &seg_offset);
456f13193f5SChuck Lever 		seg_offset += info->wi_seg_off;
457f13193f5SChuck Lever 
458f13193f5SChuck Lever 		write_len = min(remaining, seg_length - info->wi_seg_off);
459f13193f5SChuck Lever 		ctxt = svc_rdma_get_rw_ctxt(rdma,
460f13193f5SChuck Lever 					    (write_len >> PAGE_SHIFT) + 2);
461f13193f5SChuck Lever 		if (!ctxt)
462*f4e53e1cSChuck Lever 			return -ENOMEM;
463f13193f5SChuck Lever 
464f13193f5SChuck Lever 		constructor(info, write_len, ctxt);
4652abfbe7eSChuck Lever 		ret = svc_rdma_rw_ctx_init(rdma, ctxt, seg_offset, seg_handle,
4662abfbe7eSChuck Lever 					   DMA_TO_DEVICE);
467f13193f5SChuck Lever 		if (ret < 0)
4682abfbe7eSChuck Lever 			return -EIO;
469f13193f5SChuck Lever 
470a406c563SChuck Lever 		trace_svcrdma_send_wseg(seg_handle, write_len, seg_offset);
471a406c563SChuck Lever 
472f13193f5SChuck Lever 		list_add(&ctxt->rw_list, &cc->cc_rwctxts);
473f13193f5SChuck Lever 		cc->cc_sqecount += ret;
474f13193f5SChuck Lever 		if (write_len == seg_length - info->wi_seg_off) {
475f13193f5SChuck Lever 			seg += 4;
476f13193f5SChuck Lever 			info->wi_seg_no++;
477f13193f5SChuck Lever 			info->wi_seg_off = 0;
478f13193f5SChuck Lever 		} else {
479f13193f5SChuck Lever 			info->wi_seg_off += write_len;
480f13193f5SChuck Lever 		}
481f13193f5SChuck Lever 		remaining -= write_len;
482f13193f5SChuck Lever 	} while (remaining);
483f13193f5SChuck Lever 
484f13193f5SChuck Lever 	return 0;
485f13193f5SChuck Lever 
486f13193f5SChuck Lever out_overflow:
487f13193f5SChuck Lever 	dprintk("svcrdma: inadequate space in Write chunk (%u)\n",
488f13193f5SChuck Lever 		info->wi_nsegs);
489f13193f5SChuck Lever 	return -E2BIG;
490f13193f5SChuck Lever }
491f13193f5SChuck Lever 
492f13193f5SChuck Lever /* Send one of an xdr_buf's kvecs by itself. To send a Reply
493f13193f5SChuck Lever  * chunk, the whole RPC Reply is written back to the client.
494f13193f5SChuck Lever  * This function writes either the head or tail of the xdr_buf
495f13193f5SChuck Lever  * containing the Reply.
496f13193f5SChuck Lever  */
497f13193f5SChuck Lever static int svc_rdma_send_xdr_kvec(struct svc_rdma_write_info *info,
498f13193f5SChuck Lever 				  struct kvec *vec)
499f13193f5SChuck Lever {
500f13193f5SChuck Lever 	info->wi_base = vec->iov_base;
501f13193f5SChuck Lever 	return svc_rdma_build_writes(info, svc_rdma_vec_to_sg,
502f13193f5SChuck Lever 				     vec->iov_len);
503f13193f5SChuck Lever }
504f13193f5SChuck Lever 
50541205539SChuck Lever /* Send an xdr_buf's page list by itself. A Write chunk is just
50641205539SChuck Lever  * the page list. A Reply chunk is @xdr's head, page list, and
50741205539SChuck Lever  * tail. This function is shared between the two types of chunk.
508f13193f5SChuck Lever  */
509f13193f5SChuck Lever static int svc_rdma_send_xdr_pagelist(struct svc_rdma_write_info *info,
51041205539SChuck Lever 				      struct xdr_buf *xdr,
51141205539SChuck Lever 				      unsigned int offset,
51241205539SChuck Lever 				      unsigned long length)
513f13193f5SChuck Lever {
514f13193f5SChuck Lever 	info->wi_xdr = xdr;
51541205539SChuck Lever 	info->wi_next_off = offset - xdr->head[0].iov_len;
516f13193f5SChuck Lever 	return svc_rdma_build_writes(info, svc_rdma_pagelist_to_sg,
51741205539SChuck Lever 				     length);
518f13193f5SChuck Lever }
519f13193f5SChuck Lever 
520f13193f5SChuck Lever /**
521f13193f5SChuck Lever  * svc_rdma_send_write_chunk - Write all segments in a Write chunk
522f13193f5SChuck Lever  * @rdma: controlling RDMA transport
523f13193f5SChuck Lever  * @wr_ch: Write chunk provided by client
524f13193f5SChuck Lever  * @xdr: xdr_buf containing the data payload
52541205539SChuck Lever  * @offset: payload's byte offset in @xdr
52641205539SChuck Lever  * @length: size of payload, in bytes
527f13193f5SChuck Lever  *
528f13193f5SChuck Lever  * Returns a non-negative number of bytes the chunk consumed, or
529f13193f5SChuck Lever  *	%-E2BIG if the payload was larger than the Write chunk,
530107c1d0aSChuck Lever  *	%-EINVAL if client provided too many segments,
531f13193f5SChuck Lever  *	%-ENOMEM if rdma_rw context pool was exhausted,
532f13193f5SChuck Lever  *	%-ENOTCONN if posting failed (connection is lost),
533f13193f5SChuck Lever  *	%-EIO if rdma_rw initialization failed (DMA mapping, etc).
534f13193f5SChuck Lever  */
535f13193f5SChuck Lever int svc_rdma_send_write_chunk(struct svcxprt_rdma *rdma, __be32 *wr_ch,
53641205539SChuck Lever 			      struct xdr_buf *xdr,
53741205539SChuck Lever 			      unsigned int offset, unsigned long length)
538f13193f5SChuck Lever {
539f13193f5SChuck Lever 	struct svc_rdma_write_info *info;
540f13193f5SChuck Lever 	int ret;
541f13193f5SChuck Lever 
54241205539SChuck Lever 	if (!length)
543f13193f5SChuck Lever 		return 0;
544f13193f5SChuck Lever 
545f13193f5SChuck Lever 	info = svc_rdma_write_info_alloc(rdma, wr_ch);
546f13193f5SChuck Lever 	if (!info)
547f13193f5SChuck Lever 		return -ENOMEM;
548f13193f5SChuck Lever 
54941205539SChuck Lever 	ret = svc_rdma_send_xdr_pagelist(info, xdr, offset, length);
550f13193f5SChuck Lever 	if (ret < 0)
551f13193f5SChuck Lever 		goto out_err;
552f13193f5SChuck Lever 
553f13193f5SChuck Lever 	ret = svc_rdma_post_chunk_ctxt(&info->wi_cc);
554f13193f5SChuck Lever 	if (ret < 0)
555f13193f5SChuck Lever 		goto out_err;
55698895edbSChuck Lever 
557a406c563SChuck Lever 	trace_svcrdma_send_write_chunk(xdr->page_len);
55841205539SChuck Lever 	return length;
559f13193f5SChuck Lever 
560f13193f5SChuck Lever out_err:
561f13193f5SChuck Lever 	svc_rdma_write_info_free(info);
562f13193f5SChuck Lever 	return ret;
563f13193f5SChuck Lever }
564f13193f5SChuck Lever 
565f13193f5SChuck Lever /**
566f13193f5SChuck Lever  * svc_rdma_send_reply_chunk - Write all segments in the Reply chunk
567f13193f5SChuck Lever  * @rdma: controlling RDMA transport
5686fa5785eSChuck Lever  * @rctxt: Write and Reply chunks from client
569f13193f5SChuck Lever  * @xdr: xdr_buf containing an RPC Reply
570f13193f5SChuck Lever  *
571f13193f5SChuck Lever  * Returns a non-negative number of bytes the chunk consumed, or
572f13193f5SChuck Lever  *	%-E2BIG if the payload was larger than the Reply chunk,
573107c1d0aSChuck Lever  *	%-EINVAL if client provided too many segments,
574f13193f5SChuck Lever  *	%-ENOMEM if rdma_rw context pool was exhausted,
575f13193f5SChuck Lever  *	%-ENOTCONN if posting failed (connection is lost),
576f13193f5SChuck Lever  *	%-EIO if rdma_rw initialization failed (DMA mapping, etc).
577f13193f5SChuck Lever  */
5786fa5785eSChuck Lever int svc_rdma_send_reply_chunk(struct svcxprt_rdma *rdma,
5796fa5785eSChuck Lever 			      const struct svc_rdma_recv_ctxt *rctxt,
5806fa5785eSChuck Lever 			      struct xdr_buf *xdr)
581f13193f5SChuck Lever {
582f13193f5SChuck Lever 	struct svc_rdma_write_info *info;
583f13193f5SChuck Lever 	int consumed, ret;
584f13193f5SChuck Lever 
5856fa5785eSChuck Lever 	info = svc_rdma_write_info_alloc(rdma, rctxt->rc_reply_chunk);
586f13193f5SChuck Lever 	if (!info)
587f13193f5SChuck Lever 		return -ENOMEM;
588f13193f5SChuck Lever 
589f13193f5SChuck Lever 	ret = svc_rdma_send_xdr_kvec(info, &xdr->head[0]);
590f13193f5SChuck Lever 	if (ret < 0)
591f13193f5SChuck Lever 		goto out_err;
592f13193f5SChuck Lever 	consumed = xdr->head[0].iov_len;
593f13193f5SChuck Lever 
594f13193f5SChuck Lever 	/* Send the page list in the Reply chunk only if the
595f13193f5SChuck Lever 	 * client did not provide Write chunks.
596f13193f5SChuck Lever 	 */
5976fa5785eSChuck Lever 	if (!rctxt->rc_write_list && xdr->page_len) {
59841205539SChuck Lever 		ret = svc_rdma_send_xdr_pagelist(info, xdr,
59941205539SChuck Lever 						 xdr->head[0].iov_len,
60041205539SChuck Lever 						 xdr->page_len);
601f13193f5SChuck Lever 		if (ret < 0)
602f13193f5SChuck Lever 			goto out_err;
603f13193f5SChuck Lever 		consumed += xdr->page_len;
604f13193f5SChuck Lever 	}
605f13193f5SChuck Lever 
606f13193f5SChuck Lever 	if (xdr->tail[0].iov_len) {
607f13193f5SChuck Lever 		ret = svc_rdma_send_xdr_kvec(info, &xdr->tail[0]);
608f13193f5SChuck Lever 		if (ret < 0)
609f13193f5SChuck Lever 			goto out_err;
610f13193f5SChuck Lever 		consumed += xdr->tail[0].iov_len;
611f13193f5SChuck Lever 	}
612f13193f5SChuck Lever 
613f13193f5SChuck Lever 	ret = svc_rdma_post_chunk_ctxt(&info->wi_cc);
614f13193f5SChuck Lever 	if (ret < 0)
615f13193f5SChuck Lever 		goto out_err;
61698895edbSChuck Lever 
617a406c563SChuck Lever 	trace_svcrdma_send_reply_chunk(consumed);
618f13193f5SChuck Lever 	return consumed;
619f13193f5SChuck Lever 
620f13193f5SChuck Lever out_err:
621f13193f5SChuck Lever 	svc_rdma_write_info_free(info);
622f13193f5SChuck Lever 	return ret;
623f13193f5SChuck Lever }
624026d958bSChuck Lever 
625026d958bSChuck Lever static int svc_rdma_build_read_segment(struct svc_rdma_read_info *info,
626026d958bSChuck Lever 				       struct svc_rqst *rqstp,
627026d958bSChuck Lever 				       u32 rkey, u32 len, u64 offset)
628026d958bSChuck Lever {
629ecf85b23SChuck Lever 	struct svc_rdma_recv_ctxt *head = info->ri_readctxt;
630026d958bSChuck Lever 	struct svc_rdma_chunk_ctxt *cc = &info->ri_cc;
631026d958bSChuck Lever 	struct svc_rdma_rw_ctxt *ctxt;
632026d958bSChuck Lever 	unsigned int sge_no, seg_len;
633026d958bSChuck Lever 	struct scatterlist *sg;
634026d958bSChuck Lever 	int ret;
635026d958bSChuck Lever 
636026d958bSChuck Lever 	sge_no = PAGE_ALIGN(info->ri_pageoff + len) >> PAGE_SHIFT;
637026d958bSChuck Lever 	ctxt = svc_rdma_get_rw_ctxt(cc->cc_rdma, sge_no);
638026d958bSChuck Lever 	if (!ctxt)
639*f4e53e1cSChuck Lever 		return -ENOMEM;
640026d958bSChuck Lever 	ctxt->rw_nents = sge_no;
641026d958bSChuck Lever 
642026d958bSChuck Lever 	sg = ctxt->rw_sg_table.sgl;
643026d958bSChuck Lever 	for (sge_no = 0; sge_no < ctxt->rw_nents; sge_no++) {
644026d958bSChuck Lever 		seg_len = min_t(unsigned int, len,
645026d958bSChuck Lever 				PAGE_SIZE - info->ri_pageoff);
646026d958bSChuck Lever 
647ecf85b23SChuck Lever 		head->rc_arg.pages[info->ri_pageno] =
648026d958bSChuck Lever 			rqstp->rq_pages[info->ri_pageno];
649026d958bSChuck Lever 		if (!info->ri_pageoff)
650ecf85b23SChuck Lever 			head->rc_page_count++;
651026d958bSChuck Lever 
652026d958bSChuck Lever 		sg_set_page(sg, rqstp->rq_pages[info->ri_pageno],
653026d958bSChuck Lever 			    seg_len, info->ri_pageoff);
654026d958bSChuck Lever 		sg = sg_next(sg);
655026d958bSChuck Lever 
656026d958bSChuck Lever 		info->ri_pageoff += seg_len;
657026d958bSChuck Lever 		if (info->ri_pageoff == PAGE_SIZE) {
658026d958bSChuck Lever 			info->ri_pageno++;
659026d958bSChuck Lever 			info->ri_pageoff = 0;
660026d958bSChuck Lever 		}
661026d958bSChuck Lever 		len -= seg_len;
662026d958bSChuck Lever 
663026d958bSChuck Lever 		/* Safety check */
664026d958bSChuck Lever 		if (len &&
665026d958bSChuck Lever 		    &rqstp->rq_pages[info->ri_pageno + 1] > rqstp->rq_page_end)
666026d958bSChuck Lever 			goto out_overrun;
667026d958bSChuck Lever 	}
668026d958bSChuck Lever 
6692abfbe7eSChuck Lever 	ret = svc_rdma_rw_ctx_init(cc->cc_rdma, ctxt, offset, rkey,
6702abfbe7eSChuck Lever 				   DMA_FROM_DEVICE);
671026d958bSChuck Lever 	if (ret < 0)
6722abfbe7eSChuck Lever 		return -EIO;
673026d958bSChuck Lever 
674026d958bSChuck Lever 	list_add(&ctxt->rw_list, &cc->cc_rwctxts);
675026d958bSChuck Lever 	cc->cc_sqecount += ret;
676026d958bSChuck Lever 	return 0;
677026d958bSChuck Lever 
678026d958bSChuck Lever out_overrun:
679026d958bSChuck Lever 	dprintk("svcrdma: request overruns rq_pages\n");
680026d958bSChuck Lever 	return -EINVAL;
681026d958bSChuck Lever }
682026d958bSChuck Lever 
6837075a867SChuck Lever /* Walk the segments in the Read chunk starting at @p and construct
6847075a867SChuck Lever  * RDMA Read operations to pull the chunk to the server.
6857075a867SChuck Lever  */
686026d958bSChuck Lever static int svc_rdma_build_read_chunk(struct svc_rqst *rqstp,
687026d958bSChuck Lever 				     struct svc_rdma_read_info *info,
688026d958bSChuck Lever 				     __be32 *p)
689026d958bSChuck Lever {
69007d0ff3bSChuck Lever 	unsigned int i;
691026d958bSChuck Lever 	int ret;
692026d958bSChuck Lever 
6937075a867SChuck Lever 	ret = -EINVAL;
694026d958bSChuck Lever 	info->ri_chunklen = 0;
6957075a867SChuck Lever 	while (*p++ != xdr_zero && be32_to_cpup(p++) == info->ri_position) {
696026d958bSChuck Lever 		u32 rs_handle, rs_length;
697026d958bSChuck Lever 		u64 rs_offset;
698026d958bSChuck Lever 
699026d958bSChuck Lever 		rs_handle = be32_to_cpup(p++);
700026d958bSChuck Lever 		rs_length = be32_to_cpup(p++);
701026d958bSChuck Lever 		p = xdr_decode_hyper(p, &rs_offset);
702026d958bSChuck Lever 
703026d958bSChuck Lever 		ret = svc_rdma_build_read_segment(info, rqstp,
704026d958bSChuck Lever 						  rs_handle, rs_length,
705026d958bSChuck Lever 						  rs_offset);
706026d958bSChuck Lever 		if (ret < 0)
707026d958bSChuck Lever 			break;
708026d958bSChuck Lever 
709a406c563SChuck Lever 		trace_svcrdma_send_rseg(rs_handle, rs_length, rs_offset);
710026d958bSChuck Lever 		info->ri_chunklen += rs_length;
711026d958bSChuck Lever 	}
712026d958bSChuck Lever 
71307d0ff3bSChuck Lever 	/* Pages under I/O have been copied to head->rc_pages.
71407d0ff3bSChuck Lever 	 * Prevent their premature release by svc_xprt_release() .
71507d0ff3bSChuck Lever 	 */
71607d0ff3bSChuck Lever 	for (i = 0; i < info->ri_readctxt->rc_page_count; i++)
71707d0ff3bSChuck Lever 		rqstp->rq_pages[i] = NULL;
71807d0ff3bSChuck Lever 
719026d958bSChuck Lever 	return ret;
720026d958bSChuck Lever }
721026d958bSChuck Lever 
722026d958bSChuck Lever /* Construct RDMA Reads to pull over a normal Read chunk. The chunk
723ecf85b23SChuck Lever  * data lands in the page list of head->rc_arg.pages.
724026d958bSChuck Lever  *
725ecf85b23SChuck Lever  * Currently NFSD does not look at the head->rc_arg.tail[0] iovec.
726026d958bSChuck Lever  * Therefore, XDR round-up of the Read chunk and trailing
727026d958bSChuck Lever  * inline content must both be added at the end of the pagelist.
728026d958bSChuck Lever  */
729026d958bSChuck Lever static int svc_rdma_build_normal_read_chunk(struct svc_rqst *rqstp,
730026d958bSChuck Lever 					    struct svc_rdma_read_info *info,
731026d958bSChuck Lever 					    __be32 *p)
732026d958bSChuck Lever {
733ecf85b23SChuck Lever 	struct svc_rdma_recv_ctxt *head = info->ri_readctxt;
734026d958bSChuck Lever 	int ret;
735026d958bSChuck Lever 
736026d958bSChuck Lever 	ret = svc_rdma_build_read_chunk(rqstp, info, p);
737026d958bSChuck Lever 	if (ret < 0)
738026d958bSChuck Lever 		goto out;
739026d958bSChuck Lever 
740a406c563SChuck Lever 	trace_svcrdma_send_read_chunk(info->ri_chunklen, info->ri_position);
74198895edbSChuck Lever 
7423316f063SChuck Lever 	head->rc_hdr_count = 0;
7433316f063SChuck Lever 
744193bcb7bSChuck Lever 	/* Split the Receive buffer between the head and tail
745193bcb7bSChuck Lever 	 * buffers at Read chunk's position. XDR roundup of the
746193bcb7bSChuck Lever 	 * chunk is not included in either the pagelist or in
747193bcb7bSChuck Lever 	 * the tail.
748026d958bSChuck Lever 	 */
749ecf85b23SChuck Lever 	head->rc_arg.tail[0].iov_base =
750ecf85b23SChuck Lever 		head->rc_arg.head[0].iov_base + info->ri_position;
751ecf85b23SChuck Lever 	head->rc_arg.tail[0].iov_len =
752ecf85b23SChuck Lever 		head->rc_arg.head[0].iov_len - info->ri_position;
753ecf85b23SChuck Lever 	head->rc_arg.head[0].iov_len = info->ri_position;
754026d958bSChuck Lever 
755175e0310SChuck Lever 	/* Read chunk may need XDR roundup (see RFC 8166, s. 3.4.5.2).
756193bcb7bSChuck Lever 	 *
757175e0310SChuck Lever 	 * If the client already rounded up the chunk length, the
758175e0310SChuck Lever 	 * length does not change. Otherwise, the length of the page
759175e0310SChuck Lever 	 * list is increased to include XDR round-up.
760175e0310SChuck Lever 	 *
761175e0310SChuck Lever 	 * Currently these chunks always start at page offset 0,
762175e0310SChuck Lever 	 * thus the rounded-up length never crosses a page boundary.
763026d958bSChuck Lever 	 */
764175e0310SChuck Lever 	info->ri_chunklen = XDR_QUADLEN(info->ri_chunklen) << 2;
765026d958bSChuck Lever 
766ecf85b23SChuck Lever 	head->rc_arg.page_len = info->ri_chunklen;
767ecf85b23SChuck Lever 	head->rc_arg.len += info->ri_chunklen;
768ecf85b23SChuck Lever 	head->rc_arg.buflen += info->ri_chunklen;
769026d958bSChuck Lever 
770026d958bSChuck Lever out:
771026d958bSChuck Lever 	return ret;
772026d958bSChuck Lever }
773026d958bSChuck Lever 
774026d958bSChuck Lever /* Construct RDMA Reads to pull over a Position Zero Read chunk.
775026d958bSChuck Lever  * The start of the data lands in the first page just after
776026d958bSChuck Lever  * the Transport header, and the rest lands in the page list of
777ecf85b23SChuck Lever  * head->rc_arg.pages.
778026d958bSChuck Lever  *
779026d958bSChuck Lever  * Assumptions:
780026d958bSChuck Lever  *	- A PZRC has an XDR-aligned length (no implicit round-up).
781026d958bSChuck Lever  *	- There can be no trailing inline content (IOW, we assume
782026d958bSChuck Lever  *	  a PZRC is never sent in an RDMA_MSG message, though it's
783026d958bSChuck Lever  *	  allowed by spec).
784026d958bSChuck Lever  */
785026d958bSChuck Lever static int svc_rdma_build_pz_read_chunk(struct svc_rqst *rqstp,
786026d958bSChuck Lever 					struct svc_rdma_read_info *info,
787026d958bSChuck Lever 					__be32 *p)
788026d958bSChuck Lever {
789ecf85b23SChuck Lever 	struct svc_rdma_recv_ctxt *head = info->ri_readctxt;
790026d958bSChuck Lever 	int ret;
791026d958bSChuck Lever 
792026d958bSChuck Lever 	ret = svc_rdma_build_read_chunk(rqstp, info, p);
793026d958bSChuck Lever 	if (ret < 0)
794026d958bSChuck Lever 		goto out;
795026d958bSChuck Lever 
796a406c563SChuck Lever 	trace_svcrdma_send_pzr(info->ri_chunklen);
79798895edbSChuck Lever 
798ecf85b23SChuck Lever 	head->rc_arg.len += info->ri_chunklen;
799ecf85b23SChuck Lever 	head->rc_arg.buflen += info->ri_chunklen;
800026d958bSChuck Lever 
8013316f063SChuck Lever 	head->rc_hdr_count = 1;
8023316f063SChuck Lever 	head->rc_arg.head[0].iov_base = page_address(head->rc_pages[0]);
8033316f063SChuck Lever 	head->rc_arg.head[0].iov_len = min_t(size_t, PAGE_SIZE,
8043316f063SChuck Lever 					     info->ri_chunklen);
8053316f063SChuck Lever 
8063316f063SChuck Lever 	head->rc_arg.page_len = info->ri_chunklen -
8073316f063SChuck Lever 				head->rc_arg.head[0].iov_len;
808026d958bSChuck Lever 
809026d958bSChuck Lever out:
810026d958bSChuck Lever 	return ret;
811026d958bSChuck Lever }
812026d958bSChuck Lever 
813026d958bSChuck Lever /**
814026d958bSChuck Lever  * svc_rdma_recv_read_chunk - Pull a Read chunk from the client
815026d958bSChuck Lever  * @rdma: controlling RDMA transport
816026d958bSChuck Lever  * @rqstp: set of pages to use as Read sink buffers
817026d958bSChuck Lever  * @head: pages under I/O collect here
818026d958bSChuck Lever  * @p: pointer to start of Read chunk
819026d958bSChuck Lever  *
820026d958bSChuck Lever  * Returns:
821026d958bSChuck Lever  *	%0 if all needed RDMA Reads were posted successfully,
822026d958bSChuck Lever  *	%-EINVAL if client provided too many segments,
823026d958bSChuck Lever  *	%-ENOMEM if rdma_rw context pool was exhausted,
824026d958bSChuck Lever  *	%-ENOTCONN if posting failed (connection is lost),
825026d958bSChuck Lever  *	%-EIO if rdma_rw initialization failed (DMA mapping, etc).
826026d958bSChuck Lever  *
827026d958bSChuck Lever  * Assumptions:
828026d958bSChuck Lever  * - All Read segments in @p have the same Position value.
829026d958bSChuck Lever  */
830026d958bSChuck Lever int svc_rdma_recv_read_chunk(struct svcxprt_rdma *rdma, struct svc_rqst *rqstp,
831ecf85b23SChuck Lever 			     struct svc_rdma_recv_ctxt *head, __be32 *p)
832026d958bSChuck Lever {
833026d958bSChuck Lever 	struct svc_rdma_read_info *info;
834026d958bSChuck Lever 	int ret;
835026d958bSChuck Lever 
836026d958bSChuck Lever 	/* The request (with page list) is constructed in
837ecf85b23SChuck Lever 	 * head->rc_arg. Pages involved with RDMA Read I/O are
838026d958bSChuck Lever 	 * transferred there.
839026d958bSChuck Lever 	 */
840ecf85b23SChuck Lever 	head->rc_arg.head[0] = rqstp->rq_arg.head[0];
841ecf85b23SChuck Lever 	head->rc_arg.tail[0] = rqstp->rq_arg.tail[0];
842ecf85b23SChuck Lever 	head->rc_arg.pages = head->rc_pages;
843ecf85b23SChuck Lever 	head->rc_arg.page_base = 0;
844ecf85b23SChuck Lever 	head->rc_arg.page_len = 0;
845ecf85b23SChuck Lever 	head->rc_arg.len = rqstp->rq_arg.len;
846ecf85b23SChuck Lever 	head->rc_arg.buflen = rqstp->rq_arg.buflen;
847026d958bSChuck Lever 
848026d958bSChuck Lever 	info = svc_rdma_read_info_alloc(rdma);
849026d958bSChuck Lever 	if (!info)
850026d958bSChuck Lever 		return -ENOMEM;
851026d958bSChuck Lever 	info->ri_readctxt = head;
8523316f063SChuck Lever 	info->ri_pageno = 0;
8533316f063SChuck Lever 	info->ri_pageoff = 0;
854026d958bSChuck Lever 
855026d958bSChuck Lever 	info->ri_position = be32_to_cpup(p + 1);
856026d958bSChuck Lever 	if (info->ri_position)
857026d958bSChuck Lever 		ret = svc_rdma_build_normal_read_chunk(rqstp, info, p);
858026d958bSChuck Lever 	else
859026d958bSChuck Lever 		ret = svc_rdma_build_pz_read_chunk(rqstp, info, p);
860026d958bSChuck Lever 	if (ret < 0)
86107d0ff3bSChuck Lever 		goto out_err;
862026d958bSChuck Lever 
863026d958bSChuck Lever 	ret = svc_rdma_post_chunk_ctxt(&info->ri_cc);
864026d958bSChuck Lever 	if (ret < 0)
86507d0ff3bSChuck Lever 		goto out_err;
86607d0ff3bSChuck Lever 	return 0;
86707d0ff3bSChuck Lever 
86807d0ff3bSChuck Lever out_err:
869026d958bSChuck Lever 	svc_rdma_read_info_free(info);
870026d958bSChuck Lever 	return ret;
871026d958bSChuck Lever }
872