xref: /OK3568_Linux_fs/kernel/net/sunrpc/xprtrdma/svc_rdma_recvfrom.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2016-2018 Oracle. All rights reserved.
4*4882a593Smuzhiyun  * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved.
5*4882a593Smuzhiyun  * Copyright (c) 2005-2006 Network Appliance, Inc. All rights reserved.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * This software is available to you under a choice of one of two
8*4882a593Smuzhiyun  * licenses.  You may choose to be licensed under the terms of the GNU
9*4882a593Smuzhiyun  * General Public License (GPL) Version 2, available from the file
10*4882a593Smuzhiyun  * COPYING in the main directory of this source tree, or the BSD-type
11*4882a593Smuzhiyun  * license below:
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
14*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
15*4882a593Smuzhiyun  * are met:
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  *      Redistributions of source code must retain the above copyright
18*4882a593Smuzhiyun  *      notice, this list of conditions and the following disclaimer.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  *      Redistributions in binary form must reproduce the above
21*4882a593Smuzhiyun  *      copyright notice, this list of conditions and the following
22*4882a593Smuzhiyun  *      disclaimer in the documentation and/or other materials provided
23*4882a593Smuzhiyun  *      with the distribution.
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  *      Neither the name of the Network Appliance, Inc. nor the names of
26*4882a593Smuzhiyun  *      its contributors may be used to endorse or promote products
27*4882a593Smuzhiyun  *      derived from this software without specific prior written
28*4882a593Smuzhiyun  *      permission.
29*4882a593Smuzhiyun  *
30*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31*4882a593Smuzhiyun  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32*4882a593Smuzhiyun  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33*4882a593Smuzhiyun  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34*4882a593Smuzhiyun  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35*4882a593Smuzhiyun  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36*4882a593Smuzhiyun  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37*4882a593Smuzhiyun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38*4882a593Smuzhiyun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39*4882a593Smuzhiyun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40*4882a593Smuzhiyun  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41*4882a593Smuzhiyun  *
42*4882a593Smuzhiyun  * Author: Tom Tucker <tom@opengridcomputing.com>
43*4882a593Smuzhiyun  */
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun /* Operation
46*4882a593Smuzhiyun  *
47*4882a593Smuzhiyun  * The main entry point is svc_rdma_recvfrom. This is called from
48*4882a593Smuzhiyun  * svc_recv when the transport indicates there is incoming data to
49*4882a593Smuzhiyun  * be read. "Data Ready" is signaled when an RDMA Receive completes,
50*4882a593Smuzhiyun  * or when a set of RDMA Reads complete.
51*4882a593Smuzhiyun  *
52*4882a593Smuzhiyun  * An svc_rqst is passed in. This structure contains an array of
53*4882a593Smuzhiyun  * free pages (rq_pages) that will contain the incoming RPC message.
54*4882a593Smuzhiyun  *
55*4882a593Smuzhiyun  * Short messages are moved directly into svc_rqst::rq_arg, and
56*4882a593Smuzhiyun  * the RPC Call is ready to be processed by the Upper Layer.
57*4882a593Smuzhiyun  * svc_rdma_recvfrom returns the length of the RPC Call message,
58*4882a593Smuzhiyun  * completing the reception of the RPC Call.
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  * However, when an incoming message has Read chunks,
61*4882a593Smuzhiyun  * svc_rdma_recvfrom must post RDMA Reads to pull the RPC Call's
62*4882a593Smuzhiyun  * data payload from the client. svc_rdma_recvfrom sets up the
63*4882a593Smuzhiyun  * RDMA Reads using pages in svc_rqst::rq_pages, which are
64*4882a593Smuzhiyun  * transferred to an svc_rdma_recv_ctxt for the duration of the
65*4882a593Smuzhiyun  * I/O. svc_rdma_recvfrom then returns zero, since the RPC message
66*4882a593Smuzhiyun  * is still not yet ready.
67*4882a593Smuzhiyun  *
68*4882a593Smuzhiyun  * When the Read chunk payloads have become available on the
69*4882a593Smuzhiyun  * server, "Data Ready" is raised again, and svc_recv calls
70*4882a593Smuzhiyun  * svc_rdma_recvfrom again. This second call may use a different
71*4882a593Smuzhiyun  * svc_rqst than the first one, thus any information that needs
72*4882a593Smuzhiyun  * to be preserved across these two calls is kept in an
73*4882a593Smuzhiyun  * svc_rdma_recv_ctxt.
74*4882a593Smuzhiyun  *
75*4882a593Smuzhiyun  * The second call to svc_rdma_recvfrom performs final assembly
76*4882a593Smuzhiyun  * of the RPC Call message, using the RDMA Read sink pages kept in
77*4882a593Smuzhiyun  * the svc_rdma_recv_ctxt. The xdr_buf is copied from the
78*4882a593Smuzhiyun  * svc_rdma_recv_ctxt to the second svc_rqst. The second call returns
79*4882a593Smuzhiyun  * the length of the completed RPC Call message.
80*4882a593Smuzhiyun  *
81*4882a593Smuzhiyun  * Page Management
82*4882a593Smuzhiyun  *
83*4882a593Smuzhiyun  * Pages under I/O must be transferred from the first svc_rqst to an
84*4882a593Smuzhiyun  * svc_rdma_recv_ctxt before the first svc_rdma_recvfrom call returns.
85*4882a593Smuzhiyun  *
86*4882a593Smuzhiyun  * The first svc_rqst supplies pages for RDMA Reads. These are moved
87*4882a593Smuzhiyun  * from rqstp::rq_pages into ctxt::pages. The consumed elements of
88*4882a593Smuzhiyun  * the rq_pages array are set to NULL and refilled with the first
89*4882a593Smuzhiyun  * svc_rdma_recvfrom call returns.
90*4882a593Smuzhiyun  *
91*4882a593Smuzhiyun  * During the second svc_rdma_recvfrom call, RDMA Read sink pages
92*4882a593Smuzhiyun  * are transferred from the svc_rdma_recv_ctxt to the second svc_rqst
93*4882a593Smuzhiyun  * (see rdma_read_complete() below).
94*4882a593Smuzhiyun  */
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun #include <linux/spinlock.h>
97*4882a593Smuzhiyun #include <asm/unaligned.h>
98*4882a593Smuzhiyun #include <rdma/ib_verbs.h>
99*4882a593Smuzhiyun #include <rdma/rdma_cm.h>
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun #include <linux/sunrpc/xdr.h>
102*4882a593Smuzhiyun #include <linux/sunrpc/debug.h>
103*4882a593Smuzhiyun #include <linux/sunrpc/rpc_rdma.h>
104*4882a593Smuzhiyun #include <linux/sunrpc/svc_rdma.h>
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun #include "xprt_rdma.h"
107*4882a593Smuzhiyun #include <trace/events/rpcrdma.h>
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun #define RPCDBG_FACILITY	RPCDBG_SVCXPRT
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun static void svc_rdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc);
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun static inline struct svc_rdma_recv_ctxt *
svc_rdma_next_recv_ctxt(struct list_head * list)114*4882a593Smuzhiyun svc_rdma_next_recv_ctxt(struct list_head *list)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun 	return list_first_entry_or_null(list, struct svc_rdma_recv_ctxt,
117*4882a593Smuzhiyun 					rc_list);
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun 
svc_rdma_recv_cid_init(struct svcxprt_rdma * rdma,struct rpc_rdma_cid * cid)120*4882a593Smuzhiyun static void svc_rdma_recv_cid_init(struct svcxprt_rdma *rdma,
121*4882a593Smuzhiyun 				   struct rpc_rdma_cid *cid)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	cid->ci_queue_id = rdma->sc_rq_cq->res.id;
124*4882a593Smuzhiyun 	cid->ci_completion_id = atomic_inc_return(&rdma->sc_completion_ids);
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun static struct svc_rdma_recv_ctxt *
svc_rdma_recv_ctxt_alloc(struct svcxprt_rdma * rdma)128*4882a593Smuzhiyun svc_rdma_recv_ctxt_alloc(struct svcxprt_rdma *rdma)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	struct svc_rdma_recv_ctxt *ctxt;
131*4882a593Smuzhiyun 	dma_addr_t addr;
132*4882a593Smuzhiyun 	void *buffer;
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	ctxt = kmalloc(sizeof(*ctxt), GFP_KERNEL);
135*4882a593Smuzhiyun 	if (!ctxt)
136*4882a593Smuzhiyun 		goto fail0;
137*4882a593Smuzhiyun 	buffer = kmalloc(rdma->sc_max_req_size, GFP_KERNEL);
138*4882a593Smuzhiyun 	if (!buffer)
139*4882a593Smuzhiyun 		goto fail1;
140*4882a593Smuzhiyun 	addr = ib_dma_map_single(rdma->sc_pd->device, buffer,
141*4882a593Smuzhiyun 				 rdma->sc_max_req_size, DMA_FROM_DEVICE);
142*4882a593Smuzhiyun 	if (ib_dma_mapping_error(rdma->sc_pd->device, addr))
143*4882a593Smuzhiyun 		goto fail2;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	svc_rdma_recv_cid_init(rdma, &ctxt->rc_cid);
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	ctxt->rc_recv_wr.next = NULL;
148*4882a593Smuzhiyun 	ctxt->rc_recv_wr.wr_cqe = &ctxt->rc_cqe;
149*4882a593Smuzhiyun 	ctxt->rc_recv_wr.sg_list = &ctxt->rc_recv_sge;
150*4882a593Smuzhiyun 	ctxt->rc_recv_wr.num_sge = 1;
151*4882a593Smuzhiyun 	ctxt->rc_cqe.done = svc_rdma_wc_receive;
152*4882a593Smuzhiyun 	ctxt->rc_recv_sge.addr = addr;
153*4882a593Smuzhiyun 	ctxt->rc_recv_sge.length = rdma->sc_max_req_size;
154*4882a593Smuzhiyun 	ctxt->rc_recv_sge.lkey = rdma->sc_pd->local_dma_lkey;
155*4882a593Smuzhiyun 	ctxt->rc_recv_buf = buffer;
156*4882a593Smuzhiyun 	ctxt->rc_temp = false;
157*4882a593Smuzhiyun 	return ctxt;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun fail2:
160*4882a593Smuzhiyun 	kfree(buffer);
161*4882a593Smuzhiyun fail1:
162*4882a593Smuzhiyun 	kfree(ctxt);
163*4882a593Smuzhiyun fail0:
164*4882a593Smuzhiyun 	return NULL;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun 
svc_rdma_recv_ctxt_destroy(struct svcxprt_rdma * rdma,struct svc_rdma_recv_ctxt * ctxt)167*4882a593Smuzhiyun static void svc_rdma_recv_ctxt_destroy(struct svcxprt_rdma *rdma,
168*4882a593Smuzhiyun 				       struct svc_rdma_recv_ctxt *ctxt)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun 	ib_dma_unmap_single(rdma->sc_pd->device, ctxt->rc_recv_sge.addr,
171*4882a593Smuzhiyun 			    ctxt->rc_recv_sge.length, DMA_FROM_DEVICE);
172*4882a593Smuzhiyun 	kfree(ctxt->rc_recv_buf);
173*4882a593Smuzhiyun 	kfree(ctxt);
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun /**
177*4882a593Smuzhiyun  * svc_rdma_recv_ctxts_destroy - Release all recv_ctxt's for an xprt
178*4882a593Smuzhiyun  * @rdma: svcxprt_rdma being torn down
179*4882a593Smuzhiyun  *
180*4882a593Smuzhiyun  */
svc_rdma_recv_ctxts_destroy(struct svcxprt_rdma * rdma)181*4882a593Smuzhiyun void svc_rdma_recv_ctxts_destroy(struct svcxprt_rdma *rdma)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun 	struct svc_rdma_recv_ctxt *ctxt;
184*4882a593Smuzhiyun 	struct llist_node *node;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	while ((node = llist_del_first(&rdma->sc_recv_ctxts))) {
187*4882a593Smuzhiyun 		ctxt = llist_entry(node, struct svc_rdma_recv_ctxt, rc_node);
188*4882a593Smuzhiyun 		svc_rdma_recv_ctxt_destroy(rdma, ctxt);
189*4882a593Smuzhiyun 	}
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun static struct svc_rdma_recv_ctxt *
svc_rdma_recv_ctxt_get(struct svcxprt_rdma * rdma)193*4882a593Smuzhiyun svc_rdma_recv_ctxt_get(struct svcxprt_rdma *rdma)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun 	struct svc_rdma_recv_ctxt *ctxt;
196*4882a593Smuzhiyun 	struct llist_node *node;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	node = llist_del_first(&rdma->sc_recv_ctxts);
199*4882a593Smuzhiyun 	if (!node)
200*4882a593Smuzhiyun 		goto out_empty;
201*4882a593Smuzhiyun 	ctxt = llist_entry(node, struct svc_rdma_recv_ctxt, rc_node);
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun out:
204*4882a593Smuzhiyun 	ctxt->rc_page_count = 0;
205*4882a593Smuzhiyun 	ctxt->rc_read_payload_length = 0;
206*4882a593Smuzhiyun 	return ctxt;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun out_empty:
209*4882a593Smuzhiyun 	ctxt = svc_rdma_recv_ctxt_alloc(rdma);
210*4882a593Smuzhiyun 	if (!ctxt)
211*4882a593Smuzhiyun 		return NULL;
212*4882a593Smuzhiyun 	goto out;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun /**
216*4882a593Smuzhiyun  * svc_rdma_recv_ctxt_put - Return recv_ctxt to free list
217*4882a593Smuzhiyun  * @rdma: controlling svcxprt_rdma
218*4882a593Smuzhiyun  * @ctxt: object to return to the free list
219*4882a593Smuzhiyun  *
220*4882a593Smuzhiyun  */
svc_rdma_recv_ctxt_put(struct svcxprt_rdma * rdma,struct svc_rdma_recv_ctxt * ctxt)221*4882a593Smuzhiyun void svc_rdma_recv_ctxt_put(struct svcxprt_rdma *rdma,
222*4882a593Smuzhiyun 			    struct svc_rdma_recv_ctxt *ctxt)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun 	unsigned int i;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	for (i = 0; i < ctxt->rc_page_count; i++)
227*4882a593Smuzhiyun 		put_page(ctxt->rc_pages[i]);
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	if (!ctxt->rc_temp)
230*4882a593Smuzhiyun 		llist_add(&ctxt->rc_node, &rdma->sc_recv_ctxts);
231*4882a593Smuzhiyun 	else
232*4882a593Smuzhiyun 		svc_rdma_recv_ctxt_destroy(rdma, ctxt);
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun /**
236*4882a593Smuzhiyun  * svc_rdma_release_rqst - Release transport-specific per-rqst resources
237*4882a593Smuzhiyun  * @rqstp: svc_rqst being released
238*4882a593Smuzhiyun  *
239*4882a593Smuzhiyun  * Ensure that the recv_ctxt is released whether or not a Reply
240*4882a593Smuzhiyun  * was sent. For example, the client could close the connection,
241*4882a593Smuzhiyun  * or svc_process could drop an RPC, before the Reply is sent.
242*4882a593Smuzhiyun  */
svc_rdma_release_rqst(struct svc_rqst * rqstp)243*4882a593Smuzhiyun void svc_rdma_release_rqst(struct svc_rqst *rqstp)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun 	struct svc_rdma_recv_ctxt *ctxt = rqstp->rq_xprt_ctxt;
246*4882a593Smuzhiyun 	struct svc_xprt *xprt = rqstp->rq_xprt;
247*4882a593Smuzhiyun 	struct svcxprt_rdma *rdma =
248*4882a593Smuzhiyun 		container_of(xprt, struct svcxprt_rdma, sc_xprt);
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	rqstp->rq_xprt_ctxt = NULL;
251*4882a593Smuzhiyun 	if (ctxt)
252*4882a593Smuzhiyun 		svc_rdma_recv_ctxt_put(rdma, ctxt);
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun 
__svc_rdma_post_recv(struct svcxprt_rdma * rdma,struct svc_rdma_recv_ctxt * ctxt)255*4882a593Smuzhiyun static int __svc_rdma_post_recv(struct svcxprt_rdma *rdma,
256*4882a593Smuzhiyun 				struct svc_rdma_recv_ctxt *ctxt)
257*4882a593Smuzhiyun {
258*4882a593Smuzhiyun 	int ret;
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	trace_svcrdma_post_recv(ctxt);
261*4882a593Smuzhiyun 	ret = ib_post_recv(rdma->sc_qp, &ctxt->rc_recv_wr, NULL);
262*4882a593Smuzhiyun 	if (ret)
263*4882a593Smuzhiyun 		goto err_post;
264*4882a593Smuzhiyun 	return 0;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun err_post:
267*4882a593Smuzhiyun 	trace_svcrdma_rq_post_err(rdma, ret);
268*4882a593Smuzhiyun 	svc_rdma_recv_ctxt_put(rdma, ctxt);
269*4882a593Smuzhiyun 	return ret;
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun 
svc_rdma_post_recv(struct svcxprt_rdma * rdma)272*4882a593Smuzhiyun static int svc_rdma_post_recv(struct svcxprt_rdma *rdma)
273*4882a593Smuzhiyun {
274*4882a593Smuzhiyun 	struct svc_rdma_recv_ctxt *ctxt;
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	if (test_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags))
277*4882a593Smuzhiyun 		return 0;
278*4882a593Smuzhiyun 	ctxt = svc_rdma_recv_ctxt_get(rdma);
279*4882a593Smuzhiyun 	if (!ctxt)
280*4882a593Smuzhiyun 		return -ENOMEM;
281*4882a593Smuzhiyun 	return __svc_rdma_post_recv(rdma, ctxt);
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun /**
285*4882a593Smuzhiyun  * svc_rdma_post_recvs - Post initial set of Recv WRs
286*4882a593Smuzhiyun  * @rdma: fresh svcxprt_rdma
287*4882a593Smuzhiyun  *
288*4882a593Smuzhiyun  * Returns true if successful, otherwise false.
289*4882a593Smuzhiyun  */
svc_rdma_post_recvs(struct svcxprt_rdma * rdma)290*4882a593Smuzhiyun bool svc_rdma_post_recvs(struct svcxprt_rdma *rdma)
291*4882a593Smuzhiyun {
292*4882a593Smuzhiyun 	struct svc_rdma_recv_ctxt *ctxt;
293*4882a593Smuzhiyun 	unsigned int i;
294*4882a593Smuzhiyun 	int ret;
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	for (i = 0; i < rdma->sc_max_requests; i++) {
297*4882a593Smuzhiyun 		ctxt = svc_rdma_recv_ctxt_get(rdma);
298*4882a593Smuzhiyun 		if (!ctxt)
299*4882a593Smuzhiyun 			return false;
300*4882a593Smuzhiyun 		ctxt->rc_temp = true;
301*4882a593Smuzhiyun 		ret = __svc_rdma_post_recv(rdma, ctxt);
302*4882a593Smuzhiyun 		if (ret)
303*4882a593Smuzhiyun 			return false;
304*4882a593Smuzhiyun 	}
305*4882a593Smuzhiyun 	return true;
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun /**
309*4882a593Smuzhiyun  * svc_rdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
310*4882a593Smuzhiyun  * @cq: Completion Queue context
311*4882a593Smuzhiyun  * @wc: Work Completion object
312*4882a593Smuzhiyun  *
313*4882a593Smuzhiyun  * NB: The svc_xprt/svcxprt_rdma is pinned whenever it's possible that
314*4882a593Smuzhiyun  * the Receive completion handler could be running.
315*4882a593Smuzhiyun  */
svc_rdma_wc_receive(struct ib_cq * cq,struct ib_wc * wc)316*4882a593Smuzhiyun static void svc_rdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
317*4882a593Smuzhiyun {
318*4882a593Smuzhiyun 	struct svcxprt_rdma *rdma = cq->cq_context;
319*4882a593Smuzhiyun 	struct ib_cqe *cqe = wc->wr_cqe;
320*4882a593Smuzhiyun 	struct svc_rdma_recv_ctxt *ctxt;
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	/* WARNING: Only wc->wr_cqe and wc->status are reliable */
323*4882a593Smuzhiyun 	ctxt = container_of(cqe, struct svc_rdma_recv_ctxt, rc_cqe);
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	trace_svcrdma_wc_receive(wc, &ctxt->rc_cid);
326*4882a593Smuzhiyun 	if (wc->status != IB_WC_SUCCESS)
327*4882a593Smuzhiyun 		goto flushed;
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	if (svc_rdma_post_recv(rdma))
330*4882a593Smuzhiyun 		goto post_err;
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 	/* All wc fields are now known to be valid */
333*4882a593Smuzhiyun 	ctxt->rc_byte_len = wc->byte_len;
334*4882a593Smuzhiyun 	ib_dma_sync_single_for_cpu(rdma->sc_pd->device,
335*4882a593Smuzhiyun 				   ctxt->rc_recv_sge.addr,
336*4882a593Smuzhiyun 				   wc->byte_len, DMA_FROM_DEVICE);
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	spin_lock(&rdma->sc_rq_dto_lock);
339*4882a593Smuzhiyun 	list_add_tail(&ctxt->rc_list, &rdma->sc_rq_dto_q);
340*4882a593Smuzhiyun 	/* Note the unlock pairs with the smp_rmb in svc_xprt_ready: */
341*4882a593Smuzhiyun 	set_bit(XPT_DATA, &rdma->sc_xprt.xpt_flags);
342*4882a593Smuzhiyun 	spin_unlock(&rdma->sc_rq_dto_lock);
343*4882a593Smuzhiyun 	if (!test_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags))
344*4882a593Smuzhiyun 		svc_xprt_enqueue(&rdma->sc_xprt);
345*4882a593Smuzhiyun 	return;
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun flushed:
348*4882a593Smuzhiyun post_err:
349*4882a593Smuzhiyun 	svc_rdma_recv_ctxt_put(rdma, ctxt);
350*4882a593Smuzhiyun 	set_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags);
351*4882a593Smuzhiyun 	svc_xprt_enqueue(&rdma->sc_xprt);
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun /**
355*4882a593Smuzhiyun  * svc_rdma_flush_recv_queues - Drain pending Receive work
356*4882a593Smuzhiyun  * @rdma: svcxprt_rdma being shut down
357*4882a593Smuzhiyun  *
358*4882a593Smuzhiyun  */
svc_rdma_flush_recv_queues(struct svcxprt_rdma * rdma)359*4882a593Smuzhiyun void svc_rdma_flush_recv_queues(struct svcxprt_rdma *rdma)
360*4882a593Smuzhiyun {
361*4882a593Smuzhiyun 	struct svc_rdma_recv_ctxt *ctxt;
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	while ((ctxt = svc_rdma_next_recv_ctxt(&rdma->sc_read_complete_q))) {
364*4882a593Smuzhiyun 		list_del(&ctxt->rc_list);
365*4882a593Smuzhiyun 		svc_rdma_recv_ctxt_put(rdma, ctxt);
366*4882a593Smuzhiyun 	}
367*4882a593Smuzhiyun 	while ((ctxt = svc_rdma_next_recv_ctxt(&rdma->sc_rq_dto_q))) {
368*4882a593Smuzhiyun 		list_del(&ctxt->rc_list);
369*4882a593Smuzhiyun 		svc_rdma_recv_ctxt_put(rdma, ctxt);
370*4882a593Smuzhiyun 	}
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun 
svc_rdma_build_arg_xdr(struct svc_rqst * rqstp,struct svc_rdma_recv_ctxt * ctxt)373*4882a593Smuzhiyun static void svc_rdma_build_arg_xdr(struct svc_rqst *rqstp,
374*4882a593Smuzhiyun 				   struct svc_rdma_recv_ctxt *ctxt)
375*4882a593Smuzhiyun {
376*4882a593Smuzhiyun 	struct xdr_buf *arg = &rqstp->rq_arg;
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	arg->head[0].iov_base = ctxt->rc_recv_buf;
379*4882a593Smuzhiyun 	arg->head[0].iov_len = ctxt->rc_byte_len;
380*4882a593Smuzhiyun 	arg->tail[0].iov_base = NULL;
381*4882a593Smuzhiyun 	arg->tail[0].iov_len = 0;
382*4882a593Smuzhiyun 	arg->page_len = 0;
383*4882a593Smuzhiyun 	arg->page_base = 0;
384*4882a593Smuzhiyun 	arg->buflen = ctxt->rc_byte_len;
385*4882a593Smuzhiyun 	arg->len = ctxt->rc_byte_len;
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun /* This accommodates the largest possible Write chunk.
389*4882a593Smuzhiyun  */
390*4882a593Smuzhiyun #define MAX_BYTES_WRITE_CHUNK ((u32)(RPCSVC_MAXPAGES << PAGE_SHIFT))
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun /* This accommodates the largest possible Position-Zero
393*4882a593Smuzhiyun  * Read chunk or Reply chunk.
394*4882a593Smuzhiyun  */
395*4882a593Smuzhiyun #define MAX_BYTES_SPECIAL_CHUNK ((u32)((RPCSVC_MAXPAGES + 2) << PAGE_SHIFT))
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun /* Sanity check the Read list.
398*4882a593Smuzhiyun  *
399*4882a593Smuzhiyun  * Implementation limits:
400*4882a593Smuzhiyun  * - This implementation supports only one Read chunk.
401*4882a593Smuzhiyun  *
402*4882a593Smuzhiyun  * Sanity checks:
403*4882a593Smuzhiyun  * - Read list does not overflow Receive buffer.
404*4882a593Smuzhiyun  * - Segment size limited by largest NFS data payload.
405*4882a593Smuzhiyun  *
406*4882a593Smuzhiyun  * The segment count is limited to how many segments can
407*4882a593Smuzhiyun  * fit in the transport header without overflowing the
408*4882a593Smuzhiyun  * buffer. That's about 40 Read segments for a 1KB inline
409*4882a593Smuzhiyun  * threshold.
410*4882a593Smuzhiyun  *
411*4882a593Smuzhiyun  * Return values:
412*4882a593Smuzhiyun  *       %true: Read list is valid. @rctxt's xdr_stream is updated
413*4882a593Smuzhiyun  *		to point to the first byte past the Read list.
414*4882a593Smuzhiyun  *      %false: Read list is corrupt. @rctxt's xdr_stream is left
415*4882a593Smuzhiyun  *		in an unknown state.
416*4882a593Smuzhiyun  */
xdr_check_read_list(struct svc_rdma_recv_ctxt * rctxt)417*4882a593Smuzhiyun static bool xdr_check_read_list(struct svc_rdma_recv_ctxt *rctxt)
418*4882a593Smuzhiyun {
419*4882a593Smuzhiyun 	u32 position, len;
420*4882a593Smuzhiyun 	bool first;
421*4882a593Smuzhiyun 	__be32 *p;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	p = xdr_inline_decode(&rctxt->rc_stream, sizeof(*p));
424*4882a593Smuzhiyun 	if (!p)
425*4882a593Smuzhiyun 		return false;
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	len = 0;
428*4882a593Smuzhiyun 	first = true;
429*4882a593Smuzhiyun 	while (xdr_item_is_present(p)) {
430*4882a593Smuzhiyun 		p = xdr_inline_decode(&rctxt->rc_stream,
431*4882a593Smuzhiyun 				      rpcrdma_readseg_maxsz * sizeof(*p));
432*4882a593Smuzhiyun 		if (!p)
433*4882a593Smuzhiyun 			return false;
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 		if (first) {
436*4882a593Smuzhiyun 			position = be32_to_cpup(p);
437*4882a593Smuzhiyun 			first = false;
438*4882a593Smuzhiyun 		} else if (be32_to_cpup(p) != position) {
439*4882a593Smuzhiyun 			return false;
440*4882a593Smuzhiyun 		}
441*4882a593Smuzhiyun 		p += 2;
442*4882a593Smuzhiyun 		len += be32_to_cpup(p);
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 		p = xdr_inline_decode(&rctxt->rc_stream, sizeof(*p));
445*4882a593Smuzhiyun 		if (!p)
446*4882a593Smuzhiyun 			return false;
447*4882a593Smuzhiyun 	}
448*4882a593Smuzhiyun 	return len <= MAX_BYTES_SPECIAL_CHUNK;
449*4882a593Smuzhiyun }
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun /* The segment count is limited to how many segments can
452*4882a593Smuzhiyun  * fit in the transport header without overflowing the
453*4882a593Smuzhiyun  * buffer. That's about 60 Write segments for a 1KB inline
454*4882a593Smuzhiyun  * threshold.
455*4882a593Smuzhiyun  */
xdr_check_write_chunk(struct svc_rdma_recv_ctxt * rctxt,u32 maxlen)456*4882a593Smuzhiyun static bool xdr_check_write_chunk(struct svc_rdma_recv_ctxt *rctxt, u32 maxlen)
457*4882a593Smuzhiyun {
458*4882a593Smuzhiyun 	u32 i, segcount, total;
459*4882a593Smuzhiyun 	__be32 *p;
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	p = xdr_inline_decode(&rctxt->rc_stream, sizeof(*p));
462*4882a593Smuzhiyun 	if (!p)
463*4882a593Smuzhiyun 		return false;
464*4882a593Smuzhiyun 	segcount = be32_to_cpup(p);
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	total = 0;
467*4882a593Smuzhiyun 	for (i = 0; i < segcount; i++) {
468*4882a593Smuzhiyun 		u32 handle, length;
469*4882a593Smuzhiyun 		u64 offset;
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 		p = xdr_inline_decode(&rctxt->rc_stream,
472*4882a593Smuzhiyun 				      rpcrdma_segment_maxsz * sizeof(*p));
473*4882a593Smuzhiyun 		if (!p)
474*4882a593Smuzhiyun 			return false;
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 		xdr_decode_rdma_segment(p, &handle, &length, &offset);
477*4882a593Smuzhiyun 		trace_svcrdma_decode_wseg(handle, length, offset);
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 		total += length;
480*4882a593Smuzhiyun 	}
481*4882a593Smuzhiyun 	return total <= maxlen;
482*4882a593Smuzhiyun }
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun /* Sanity check the Write list.
485*4882a593Smuzhiyun  *
486*4882a593Smuzhiyun  * Implementation limits:
487*4882a593Smuzhiyun  * - This implementation currently supports only one Write chunk.
488*4882a593Smuzhiyun  *
489*4882a593Smuzhiyun  * Sanity checks:
490*4882a593Smuzhiyun  * - Write list does not overflow Receive buffer.
491*4882a593Smuzhiyun  * - Chunk size limited by largest NFS data payload.
492*4882a593Smuzhiyun  *
493*4882a593Smuzhiyun  * Return values:
494*4882a593Smuzhiyun  *       %true: Write list is valid. @rctxt's xdr_stream is updated
495*4882a593Smuzhiyun  *		to point to the first byte past the Write list.
496*4882a593Smuzhiyun  *      %false: Write list is corrupt. @rctxt's xdr_stream is left
497*4882a593Smuzhiyun  *		in an unknown state.
498*4882a593Smuzhiyun  */
xdr_check_write_list(struct svc_rdma_recv_ctxt * rctxt)499*4882a593Smuzhiyun static bool xdr_check_write_list(struct svc_rdma_recv_ctxt *rctxt)
500*4882a593Smuzhiyun {
501*4882a593Smuzhiyun 	u32 chcount = 0;
502*4882a593Smuzhiyun 	__be32 *p;
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 	p = xdr_inline_decode(&rctxt->rc_stream, sizeof(*p));
505*4882a593Smuzhiyun 	if (!p)
506*4882a593Smuzhiyun 		return false;
507*4882a593Smuzhiyun 	rctxt->rc_write_list = p;
508*4882a593Smuzhiyun 	while (xdr_item_is_present(p)) {
509*4882a593Smuzhiyun 		if (!xdr_check_write_chunk(rctxt, MAX_BYTES_WRITE_CHUNK))
510*4882a593Smuzhiyun 			return false;
511*4882a593Smuzhiyun 		++chcount;
512*4882a593Smuzhiyun 		p = xdr_inline_decode(&rctxt->rc_stream, sizeof(*p));
513*4882a593Smuzhiyun 		if (!p)
514*4882a593Smuzhiyun 			return false;
515*4882a593Smuzhiyun 	}
516*4882a593Smuzhiyun 	if (!chcount)
517*4882a593Smuzhiyun 		rctxt->rc_write_list = NULL;
518*4882a593Smuzhiyun 	return chcount < 2;
519*4882a593Smuzhiyun }
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun /* Sanity check the Reply chunk.
522*4882a593Smuzhiyun  *
523*4882a593Smuzhiyun  * Sanity checks:
524*4882a593Smuzhiyun  * - Reply chunk does not overflow Receive buffer.
525*4882a593Smuzhiyun  * - Chunk size limited by largest NFS data payload.
526*4882a593Smuzhiyun  *
527*4882a593Smuzhiyun  * Return values:
528*4882a593Smuzhiyun  *       %true: Reply chunk is valid. @rctxt's xdr_stream is updated
529*4882a593Smuzhiyun  *		to point to the first byte past the Reply chunk.
530*4882a593Smuzhiyun  *      %false: Reply chunk is corrupt. @rctxt's xdr_stream is left
531*4882a593Smuzhiyun  *		in an unknown state.
532*4882a593Smuzhiyun  */
xdr_check_reply_chunk(struct svc_rdma_recv_ctxt * rctxt)533*4882a593Smuzhiyun static bool xdr_check_reply_chunk(struct svc_rdma_recv_ctxt *rctxt)
534*4882a593Smuzhiyun {
535*4882a593Smuzhiyun 	__be32 *p;
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	p = xdr_inline_decode(&rctxt->rc_stream, sizeof(*p));
538*4882a593Smuzhiyun 	if (!p)
539*4882a593Smuzhiyun 		return false;
540*4882a593Smuzhiyun 	rctxt->rc_reply_chunk = NULL;
541*4882a593Smuzhiyun 	if (xdr_item_is_present(p)) {
542*4882a593Smuzhiyun 		if (!xdr_check_write_chunk(rctxt, MAX_BYTES_SPECIAL_CHUNK))
543*4882a593Smuzhiyun 			return false;
544*4882a593Smuzhiyun 		rctxt->rc_reply_chunk = p;
545*4882a593Smuzhiyun 	}
546*4882a593Smuzhiyun 	return true;
547*4882a593Smuzhiyun }
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun /* RPC-over-RDMA Version One private extension: Remote Invalidation.
550*4882a593Smuzhiyun  * Responder's choice: requester signals it can handle Send With
551*4882a593Smuzhiyun  * Invalidate, and responder chooses one R_key to invalidate.
552*4882a593Smuzhiyun  *
553*4882a593Smuzhiyun  * If there is exactly one distinct R_key in the received transport
554*4882a593Smuzhiyun  * header, set rc_inv_rkey to that R_key. Otherwise, set it to zero.
555*4882a593Smuzhiyun  *
556*4882a593Smuzhiyun  * Perform this operation while the received transport header is
557*4882a593Smuzhiyun  * still in the CPU cache.
558*4882a593Smuzhiyun  */
svc_rdma_get_inv_rkey(struct svcxprt_rdma * rdma,struct svc_rdma_recv_ctxt * ctxt)559*4882a593Smuzhiyun static void svc_rdma_get_inv_rkey(struct svcxprt_rdma *rdma,
560*4882a593Smuzhiyun 				  struct svc_rdma_recv_ctxt *ctxt)
561*4882a593Smuzhiyun {
562*4882a593Smuzhiyun 	__be32 inv_rkey, *p;
563*4882a593Smuzhiyun 	u32 i, segcount;
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	ctxt->rc_inv_rkey = 0;
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 	if (!rdma->sc_snd_w_inv)
568*4882a593Smuzhiyun 		return;
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 	inv_rkey = xdr_zero;
571*4882a593Smuzhiyun 	p = ctxt->rc_recv_buf;
572*4882a593Smuzhiyun 	p += rpcrdma_fixed_maxsz;
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun 	/* Read list */
575*4882a593Smuzhiyun 	while (xdr_item_is_present(p++)) {
576*4882a593Smuzhiyun 		p++;	/* position */
577*4882a593Smuzhiyun 		if (inv_rkey == xdr_zero)
578*4882a593Smuzhiyun 			inv_rkey = *p;
579*4882a593Smuzhiyun 		else if (inv_rkey != *p)
580*4882a593Smuzhiyun 			return;
581*4882a593Smuzhiyun 		p += 4;
582*4882a593Smuzhiyun 	}
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	/* Write list */
585*4882a593Smuzhiyun 	while (xdr_item_is_present(p++)) {
586*4882a593Smuzhiyun 		segcount = be32_to_cpup(p++);
587*4882a593Smuzhiyun 		for (i = 0; i < segcount; i++) {
588*4882a593Smuzhiyun 			if (inv_rkey == xdr_zero)
589*4882a593Smuzhiyun 				inv_rkey = *p;
590*4882a593Smuzhiyun 			else if (inv_rkey != *p)
591*4882a593Smuzhiyun 				return;
592*4882a593Smuzhiyun 			p += 4;
593*4882a593Smuzhiyun 		}
594*4882a593Smuzhiyun 	}
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 	/* Reply chunk */
597*4882a593Smuzhiyun 	if (xdr_item_is_present(p++)) {
598*4882a593Smuzhiyun 		segcount = be32_to_cpup(p++);
599*4882a593Smuzhiyun 		for (i = 0; i < segcount; i++) {
600*4882a593Smuzhiyun 			if (inv_rkey == xdr_zero)
601*4882a593Smuzhiyun 				inv_rkey = *p;
602*4882a593Smuzhiyun 			else if (inv_rkey != *p)
603*4882a593Smuzhiyun 				return;
604*4882a593Smuzhiyun 			p += 4;
605*4882a593Smuzhiyun 		}
606*4882a593Smuzhiyun 	}
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	ctxt->rc_inv_rkey = be32_to_cpu(inv_rkey);
609*4882a593Smuzhiyun }
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun /**
612*4882a593Smuzhiyun  * svc_rdma_xdr_decode_req - Decode the transport header
613*4882a593Smuzhiyun  * @rq_arg: xdr_buf containing ingress RPC/RDMA message
614*4882a593Smuzhiyun  * @rctxt: state of decoding
615*4882a593Smuzhiyun  *
616*4882a593Smuzhiyun  * On entry, xdr->head[0].iov_base points to first byte of the
617*4882a593Smuzhiyun  * RPC-over-RDMA transport header.
618*4882a593Smuzhiyun  *
619*4882a593Smuzhiyun  * On successful exit, head[0] points to first byte past the
620*4882a593Smuzhiyun  * RPC-over-RDMA header. For RDMA_MSG, this is the RPC message.
621*4882a593Smuzhiyun  *
622*4882a593Smuzhiyun  * The length of the RPC-over-RDMA header is returned.
623*4882a593Smuzhiyun  *
624*4882a593Smuzhiyun  * Assumptions:
625*4882a593Smuzhiyun  * - The transport header is entirely contained in the head iovec.
626*4882a593Smuzhiyun  */
svc_rdma_xdr_decode_req(struct xdr_buf * rq_arg,struct svc_rdma_recv_ctxt * rctxt)627*4882a593Smuzhiyun static int svc_rdma_xdr_decode_req(struct xdr_buf *rq_arg,
628*4882a593Smuzhiyun 				   struct svc_rdma_recv_ctxt *rctxt)
629*4882a593Smuzhiyun {
630*4882a593Smuzhiyun 	__be32 *p, *rdma_argp;
631*4882a593Smuzhiyun 	unsigned int hdr_len;
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	rdma_argp = rq_arg->head[0].iov_base;
634*4882a593Smuzhiyun 	xdr_init_decode(&rctxt->rc_stream, rq_arg, rdma_argp, NULL);
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 	p = xdr_inline_decode(&rctxt->rc_stream,
637*4882a593Smuzhiyun 			      rpcrdma_fixed_maxsz * sizeof(*p));
638*4882a593Smuzhiyun 	if (unlikely(!p))
639*4882a593Smuzhiyun 		goto out_short;
640*4882a593Smuzhiyun 	p++;
641*4882a593Smuzhiyun 	if (*p != rpcrdma_version)
642*4882a593Smuzhiyun 		goto out_version;
643*4882a593Smuzhiyun 	p += 2;
644*4882a593Smuzhiyun 	switch (*p) {
645*4882a593Smuzhiyun 	case rdma_msg:
646*4882a593Smuzhiyun 		break;
647*4882a593Smuzhiyun 	case rdma_nomsg:
648*4882a593Smuzhiyun 		break;
649*4882a593Smuzhiyun 	case rdma_done:
650*4882a593Smuzhiyun 		goto out_drop;
651*4882a593Smuzhiyun 	case rdma_error:
652*4882a593Smuzhiyun 		goto out_drop;
653*4882a593Smuzhiyun 	default:
654*4882a593Smuzhiyun 		goto out_proc;
655*4882a593Smuzhiyun 	}
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 	if (!xdr_check_read_list(rctxt))
658*4882a593Smuzhiyun 		goto out_inval;
659*4882a593Smuzhiyun 	if (!xdr_check_write_list(rctxt))
660*4882a593Smuzhiyun 		goto out_inval;
661*4882a593Smuzhiyun 	if (!xdr_check_reply_chunk(rctxt))
662*4882a593Smuzhiyun 		goto out_inval;
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 	rq_arg->head[0].iov_base = rctxt->rc_stream.p;
665*4882a593Smuzhiyun 	hdr_len = xdr_stream_pos(&rctxt->rc_stream);
666*4882a593Smuzhiyun 	rq_arg->head[0].iov_len -= hdr_len;
667*4882a593Smuzhiyun 	rq_arg->len -= hdr_len;
668*4882a593Smuzhiyun 	trace_svcrdma_decode_rqst(rctxt, rdma_argp, hdr_len);
669*4882a593Smuzhiyun 	return hdr_len;
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun out_short:
672*4882a593Smuzhiyun 	trace_svcrdma_decode_short_err(rctxt, rq_arg->len);
673*4882a593Smuzhiyun 	return -EINVAL;
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun out_version:
676*4882a593Smuzhiyun 	trace_svcrdma_decode_badvers_err(rctxt, rdma_argp);
677*4882a593Smuzhiyun 	return -EPROTONOSUPPORT;
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun out_drop:
680*4882a593Smuzhiyun 	trace_svcrdma_decode_drop_err(rctxt, rdma_argp);
681*4882a593Smuzhiyun 	return 0;
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun out_proc:
684*4882a593Smuzhiyun 	trace_svcrdma_decode_badproc_err(rctxt, rdma_argp);
685*4882a593Smuzhiyun 	return -EINVAL;
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun out_inval:
688*4882a593Smuzhiyun 	trace_svcrdma_decode_parse_err(rctxt, rdma_argp);
689*4882a593Smuzhiyun 	return -EINVAL;
690*4882a593Smuzhiyun }
691*4882a593Smuzhiyun 
rdma_read_complete(struct svc_rqst * rqstp,struct svc_rdma_recv_ctxt * head)692*4882a593Smuzhiyun static void rdma_read_complete(struct svc_rqst *rqstp,
693*4882a593Smuzhiyun 			       struct svc_rdma_recv_ctxt *head)
694*4882a593Smuzhiyun {
695*4882a593Smuzhiyun 	int page_no;
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 	/* Move Read chunk pages to rqstp so that they will be released
698*4882a593Smuzhiyun 	 * when svc_process is done with them.
699*4882a593Smuzhiyun 	 */
700*4882a593Smuzhiyun 	for (page_no = 0; page_no < head->rc_page_count; page_no++) {
701*4882a593Smuzhiyun 		put_page(rqstp->rq_pages[page_no]);
702*4882a593Smuzhiyun 		rqstp->rq_pages[page_no] = head->rc_pages[page_no];
703*4882a593Smuzhiyun 	}
704*4882a593Smuzhiyun 	head->rc_page_count = 0;
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun 	/* Point rq_arg.pages past header */
707*4882a593Smuzhiyun 	rqstp->rq_arg.pages = &rqstp->rq_pages[head->rc_hdr_count];
708*4882a593Smuzhiyun 	rqstp->rq_arg.page_len = head->rc_arg.page_len;
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun 	/* rq_respages starts after the last arg page */
711*4882a593Smuzhiyun 	rqstp->rq_respages = &rqstp->rq_pages[page_no];
712*4882a593Smuzhiyun 	rqstp->rq_next_page = rqstp->rq_respages + 1;
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun 	/* Rebuild rq_arg head and tail. */
715*4882a593Smuzhiyun 	rqstp->rq_arg.head[0] = head->rc_arg.head[0];
716*4882a593Smuzhiyun 	rqstp->rq_arg.tail[0] = head->rc_arg.tail[0];
717*4882a593Smuzhiyun 	rqstp->rq_arg.len = head->rc_arg.len;
718*4882a593Smuzhiyun 	rqstp->rq_arg.buflen = head->rc_arg.buflen;
719*4882a593Smuzhiyun }
720*4882a593Smuzhiyun 
svc_rdma_send_error(struct svcxprt_rdma * rdma,struct svc_rdma_recv_ctxt * rctxt,int status)721*4882a593Smuzhiyun static void svc_rdma_send_error(struct svcxprt_rdma *rdma,
722*4882a593Smuzhiyun 				struct svc_rdma_recv_ctxt *rctxt,
723*4882a593Smuzhiyun 				int status)
724*4882a593Smuzhiyun {
725*4882a593Smuzhiyun 	struct svc_rdma_send_ctxt *sctxt;
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	sctxt = svc_rdma_send_ctxt_get(rdma);
728*4882a593Smuzhiyun 	if (!sctxt)
729*4882a593Smuzhiyun 		return;
730*4882a593Smuzhiyun 	svc_rdma_send_error_msg(rdma, sctxt, rctxt, status);
731*4882a593Smuzhiyun }
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun /* By convention, backchannel calls arrive via rdma_msg type
734*4882a593Smuzhiyun  * messages, and never populate the chunk lists. This makes
735*4882a593Smuzhiyun  * the RPC/RDMA header small and fixed in size, so it is
736*4882a593Smuzhiyun  * straightforward to check the RPC header's direction field.
737*4882a593Smuzhiyun  */
svc_rdma_is_backchannel_reply(struct svc_xprt * xprt,__be32 * rdma_resp)738*4882a593Smuzhiyun static bool svc_rdma_is_backchannel_reply(struct svc_xprt *xprt,
739*4882a593Smuzhiyun 					  __be32 *rdma_resp)
740*4882a593Smuzhiyun {
741*4882a593Smuzhiyun 	__be32 *p;
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	if (!xprt->xpt_bc_xprt)
744*4882a593Smuzhiyun 		return false;
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 	p = rdma_resp + 3;
747*4882a593Smuzhiyun 	if (*p++ != rdma_msg)
748*4882a593Smuzhiyun 		return false;
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	if (*p++ != xdr_zero)
751*4882a593Smuzhiyun 		return false;
752*4882a593Smuzhiyun 	if (*p++ != xdr_zero)
753*4882a593Smuzhiyun 		return false;
754*4882a593Smuzhiyun 	if (*p++ != xdr_zero)
755*4882a593Smuzhiyun 		return false;
756*4882a593Smuzhiyun 
757*4882a593Smuzhiyun 	/* XID sanity */
758*4882a593Smuzhiyun 	if (*p++ != *rdma_resp)
759*4882a593Smuzhiyun 		return false;
760*4882a593Smuzhiyun 	/* call direction */
761*4882a593Smuzhiyun 	if (*p == cpu_to_be32(RPC_CALL))
762*4882a593Smuzhiyun 		return false;
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 	return true;
765*4882a593Smuzhiyun }
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun /**
768*4882a593Smuzhiyun  * svc_rdma_recvfrom - Receive an RPC call
769*4882a593Smuzhiyun  * @rqstp: request structure into which to receive an RPC Call
770*4882a593Smuzhiyun  *
771*4882a593Smuzhiyun  * Returns:
772*4882a593Smuzhiyun  *	The positive number of bytes in the RPC Call message,
773*4882a593Smuzhiyun  *	%0 if there were no Calls ready to return,
774*4882a593Smuzhiyun  *	%-EINVAL if the Read chunk data is too large,
775*4882a593Smuzhiyun  *	%-ENOMEM if rdma_rw context pool was exhausted,
776*4882a593Smuzhiyun  *	%-ENOTCONN if posting failed (connection is lost),
777*4882a593Smuzhiyun  *	%-EIO if rdma_rw initialization failed (DMA mapping, etc).
778*4882a593Smuzhiyun  *
779*4882a593Smuzhiyun  * Called in a loop when XPT_DATA is set. XPT_DATA is cleared only
780*4882a593Smuzhiyun  * when there are no remaining ctxt's to process.
781*4882a593Smuzhiyun  *
782*4882a593Smuzhiyun  * The next ctxt is removed from the "receive" lists.
783*4882a593Smuzhiyun  *
784*4882a593Smuzhiyun  * - If the ctxt completes a Read, then finish assembling the Call
785*4882a593Smuzhiyun  *   message and return the number of bytes in the message.
786*4882a593Smuzhiyun  *
787*4882a593Smuzhiyun  * - If the ctxt completes a Receive, then construct the Call
788*4882a593Smuzhiyun  *   message from the contents of the Receive buffer.
789*4882a593Smuzhiyun  *
790*4882a593Smuzhiyun  *   - If there are no Read chunks in this message, then finish
791*4882a593Smuzhiyun  *     assembling the Call message and return the number of bytes
792*4882a593Smuzhiyun  *     in the message.
793*4882a593Smuzhiyun  *
794*4882a593Smuzhiyun  *   - If there are Read chunks in this message, post Read WRs to
795*4882a593Smuzhiyun  *     pull that payload and return 0.
796*4882a593Smuzhiyun  */
svc_rdma_recvfrom(struct svc_rqst * rqstp)797*4882a593Smuzhiyun int svc_rdma_recvfrom(struct svc_rqst *rqstp)
798*4882a593Smuzhiyun {
799*4882a593Smuzhiyun 	struct svc_xprt *xprt = rqstp->rq_xprt;
800*4882a593Smuzhiyun 	struct svcxprt_rdma *rdma_xprt =
801*4882a593Smuzhiyun 		container_of(xprt, struct svcxprt_rdma, sc_xprt);
802*4882a593Smuzhiyun 	struct svc_rdma_recv_ctxt *ctxt;
803*4882a593Smuzhiyun 	__be32 *p;
804*4882a593Smuzhiyun 	int ret;
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun 	rqstp->rq_xprt_ctxt = NULL;
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 	spin_lock(&rdma_xprt->sc_rq_dto_lock);
809*4882a593Smuzhiyun 	ctxt = svc_rdma_next_recv_ctxt(&rdma_xprt->sc_read_complete_q);
810*4882a593Smuzhiyun 	if (ctxt) {
811*4882a593Smuzhiyun 		list_del(&ctxt->rc_list);
812*4882a593Smuzhiyun 		spin_unlock(&rdma_xprt->sc_rq_dto_lock);
813*4882a593Smuzhiyun 		rdma_read_complete(rqstp, ctxt);
814*4882a593Smuzhiyun 		goto complete;
815*4882a593Smuzhiyun 	}
816*4882a593Smuzhiyun 	ctxt = svc_rdma_next_recv_ctxt(&rdma_xprt->sc_rq_dto_q);
817*4882a593Smuzhiyun 	if (!ctxt) {
818*4882a593Smuzhiyun 		/* No new incoming requests, terminate the loop */
819*4882a593Smuzhiyun 		clear_bit(XPT_DATA, &xprt->xpt_flags);
820*4882a593Smuzhiyun 		spin_unlock(&rdma_xprt->sc_rq_dto_lock);
821*4882a593Smuzhiyun 		return 0;
822*4882a593Smuzhiyun 	}
823*4882a593Smuzhiyun 	list_del(&ctxt->rc_list);
824*4882a593Smuzhiyun 	spin_unlock(&rdma_xprt->sc_rq_dto_lock);
825*4882a593Smuzhiyun 
826*4882a593Smuzhiyun 	atomic_inc(&rdma_stat_recv);
827*4882a593Smuzhiyun 
828*4882a593Smuzhiyun 	svc_rdma_build_arg_xdr(rqstp, ctxt);
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 	/* Prevent svc_xprt_release from releasing pages in rq_pages
831*4882a593Smuzhiyun 	 * if we return 0 or an error.
832*4882a593Smuzhiyun 	 */
833*4882a593Smuzhiyun 	rqstp->rq_respages = rqstp->rq_pages;
834*4882a593Smuzhiyun 	rqstp->rq_next_page = rqstp->rq_respages;
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun 	p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
837*4882a593Smuzhiyun 	ret = svc_rdma_xdr_decode_req(&rqstp->rq_arg, ctxt);
838*4882a593Smuzhiyun 	if (ret < 0)
839*4882a593Smuzhiyun 		goto out_err;
840*4882a593Smuzhiyun 	if (ret == 0)
841*4882a593Smuzhiyun 		goto out_drop;
842*4882a593Smuzhiyun 	rqstp->rq_xprt_hlen = ret;
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 	if (svc_rdma_is_backchannel_reply(xprt, p))
845*4882a593Smuzhiyun 		goto out_backchannel;
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 	svc_rdma_get_inv_rkey(rdma_xprt, ctxt);
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun 	p += rpcrdma_fixed_maxsz;
850*4882a593Smuzhiyun 	if (*p != xdr_zero)
851*4882a593Smuzhiyun 		goto out_readchunk;
852*4882a593Smuzhiyun 
853*4882a593Smuzhiyun complete:
854*4882a593Smuzhiyun 	rqstp->rq_xprt_ctxt = ctxt;
855*4882a593Smuzhiyun 	rqstp->rq_prot = IPPROTO_MAX;
856*4882a593Smuzhiyun 	svc_xprt_copy_addrs(rqstp, xprt);
857*4882a593Smuzhiyun 	return rqstp->rq_arg.len;
858*4882a593Smuzhiyun 
859*4882a593Smuzhiyun out_readchunk:
860*4882a593Smuzhiyun 	ret = svc_rdma_recv_read_chunk(rdma_xprt, rqstp, ctxt, p);
861*4882a593Smuzhiyun 	if (ret < 0)
862*4882a593Smuzhiyun 		goto out_postfail;
863*4882a593Smuzhiyun 	return 0;
864*4882a593Smuzhiyun 
865*4882a593Smuzhiyun out_err:
866*4882a593Smuzhiyun 	svc_rdma_send_error(rdma_xprt, ctxt, ret);
867*4882a593Smuzhiyun 	svc_rdma_recv_ctxt_put(rdma_xprt, ctxt);
868*4882a593Smuzhiyun 	return 0;
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun out_postfail:
871*4882a593Smuzhiyun 	if (ret == -EINVAL)
872*4882a593Smuzhiyun 		svc_rdma_send_error(rdma_xprt, ctxt, ret);
873*4882a593Smuzhiyun 	svc_rdma_recv_ctxt_put(rdma_xprt, ctxt);
874*4882a593Smuzhiyun 	return ret;
875*4882a593Smuzhiyun 
876*4882a593Smuzhiyun out_backchannel:
877*4882a593Smuzhiyun 	svc_rdma_handle_bc_reply(rqstp, ctxt);
878*4882a593Smuzhiyun out_drop:
879*4882a593Smuzhiyun 	svc_rdma_recv_ctxt_put(rdma_xprt, ctxt);
880*4882a593Smuzhiyun 	return 0;
881*4882a593Smuzhiyun }
882