1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) 2015-2018 Oracle. All rights reserved.
4*4882a593Smuzhiyun * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved.
5*4882a593Smuzhiyun * Copyright (c) 2005-2007 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 #include <linux/interrupt.h>
46*4882a593Smuzhiyun #include <linux/sched.h>
47*4882a593Smuzhiyun #include <linux/slab.h>
48*4882a593Smuzhiyun #include <linux/spinlock.h>
49*4882a593Smuzhiyun #include <linux/workqueue.h>
50*4882a593Smuzhiyun #include <linux/export.h>
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun #include <rdma/ib_verbs.h>
53*4882a593Smuzhiyun #include <rdma/rdma_cm.h>
54*4882a593Smuzhiyun #include <rdma/rw.h>
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun #include <linux/sunrpc/addr.h>
57*4882a593Smuzhiyun #include <linux/sunrpc/debug.h>
58*4882a593Smuzhiyun #include <linux/sunrpc/svc_xprt.h>
59*4882a593Smuzhiyun #include <linux/sunrpc/svc_rdma.h>
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun #include "xprt_rdma.h"
62*4882a593Smuzhiyun #include <trace/events/rpcrdma.h>
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun #define RPCDBG_FACILITY RPCDBG_SVCXPRT
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun static struct svcxprt_rdma *svc_rdma_create_xprt(struct svc_serv *serv,
67*4882a593Smuzhiyun struct net *net);
68*4882a593Smuzhiyun static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
69*4882a593Smuzhiyun struct net *net,
70*4882a593Smuzhiyun struct sockaddr *sa, int salen,
71*4882a593Smuzhiyun int flags);
72*4882a593Smuzhiyun static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt);
73*4882a593Smuzhiyun static void svc_rdma_detach(struct svc_xprt *xprt);
74*4882a593Smuzhiyun static void svc_rdma_free(struct svc_xprt *xprt);
75*4882a593Smuzhiyun static int svc_rdma_has_wspace(struct svc_xprt *xprt);
76*4882a593Smuzhiyun static void svc_rdma_secure_port(struct svc_rqst *);
77*4882a593Smuzhiyun static void svc_rdma_kill_temp_xprt(struct svc_xprt *);
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun static const struct svc_xprt_ops svc_rdma_ops = {
80*4882a593Smuzhiyun .xpo_create = svc_rdma_create,
81*4882a593Smuzhiyun .xpo_recvfrom = svc_rdma_recvfrom,
82*4882a593Smuzhiyun .xpo_sendto = svc_rdma_sendto,
83*4882a593Smuzhiyun .xpo_read_payload = svc_rdma_read_payload,
84*4882a593Smuzhiyun .xpo_release_rqst = svc_rdma_release_rqst,
85*4882a593Smuzhiyun .xpo_detach = svc_rdma_detach,
86*4882a593Smuzhiyun .xpo_free = svc_rdma_free,
87*4882a593Smuzhiyun .xpo_has_wspace = svc_rdma_has_wspace,
88*4882a593Smuzhiyun .xpo_accept = svc_rdma_accept,
89*4882a593Smuzhiyun .xpo_secure_port = svc_rdma_secure_port,
90*4882a593Smuzhiyun .xpo_kill_temp_xprt = svc_rdma_kill_temp_xprt,
91*4882a593Smuzhiyun };
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun struct svc_xprt_class svc_rdma_class = {
94*4882a593Smuzhiyun .xcl_name = "rdma",
95*4882a593Smuzhiyun .xcl_owner = THIS_MODULE,
96*4882a593Smuzhiyun .xcl_ops = &svc_rdma_ops,
97*4882a593Smuzhiyun .xcl_max_payload = RPCSVC_MAXPAYLOAD_RDMA,
98*4882a593Smuzhiyun .xcl_ident = XPRT_TRANSPORT_RDMA,
99*4882a593Smuzhiyun };
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /* QP event handler */
qp_event_handler(struct ib_event * event,void * context)102*4882a593Smuzhiyun static void qp_event_handler(struct ib_event *event, void *context)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun struct svc_xprt *xprt = context;
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun trace_svcrdma_qp_error(event, (struct sockaddr *)&xprt->xpt_remote);
107*4882a593Smuzhiyun switch (event->event) {
108*4882a593Smuzhiyun /* These are considered benign events */
109*4882a593Smuzhiyun case IB_EVENT_PATH_MIG:
110*4882a593Smuzhiyun case IB_EVENT_COMM_EST:
111*4882a593Smuzhiyun case IB_EVENT_SQ_DRAINED:
112*4882a593Smuzhiyun case IB_EVENT_QP_LAST_WQE_REACHED:
113*4882a593Smuzhiyun break;
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun /* These are considered fatal events */
116*4882a593Smuzhiyun case IB_EVENT_PATH_MIG_ERR:
117*4882a593Smuzhiyun case IB_EVENT_QP_FATAL:
118*4882a593Smuzhiyun case IB_EVENT_QP_REQ_ERR:
119*4882a593Smuzhiyun case IB_EVENT_QP_ACCESS_ERR:
120*4882a593Smuzhiyun case IB_EVENT_DEVICE_FATAL:
121*4882a593Smuzhiyun default:
122*4882a593Smuzhiyun set_bit(XPT_CLOSE, &xprt->xpt_flags);
123*4882a593Smuzhiyun svc_xprt_enqueue(xprt);
124*4882a593Smuzhiyun break;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
svc_rdma_create_xprt(struct svc_serv * serv,struct net * net)128*4882a593Smuzhiyun static struct svcxprt_rdma *svc_rdma_create_xprt(struct svc_serv *serv,
129*4882a593Smuzhiyun struct net *net)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun if (!cma_xprt) {
134*4882a593Smuzhiyun dprintk("svcrdma: failed to create new transport\n");
135*4882a593Smuzhiyun return NULL;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun svc_xprt_init(net, &svc_rdma_class, &cma_xprt->sc_xprt, serv);
138*4882a593Smuzhiyun INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
139*4882a593Smuzhiyun INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
140*4882a593Smuzhiyun INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
141*4882a593Smuzhiyun INIT_LIST_HEAD(&cma_xprt->sc_send_ctxts);
142*4882a593Smuzhiyun init_llist_head(&cma_xprt->sc_recv_ctxts);
143*4882a593Smuzhiyun INIT_LIST_HEAD(&cma_xprt->sc_rw_ctxts);
144*4882a593Smuzhiyun init_waitqueue_head(&cma_xprt->sc_send_wait);
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun spin_lock_init(&cma_xprt->sc_lock);
147*4882a593Smuzhiyun spin_lock_init(&cma_xprt->sc_rq_dto_lock);
148*4882a593Smuzhiyun spin_lock_init(&cma_xprt->sc_send_lock);
149*4882a593Smuzhiyun spin_lock_init(&cma_xprt->sc_rw_ctxt_lock);
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun /*
152*4882a593Smuzhiyun * Note that this implies that the underlying transport support
153*4882a593Smuzhiyun * has some form of congestion control (see RFC 7530 section 3.1
154*4882a593Smuzhiyun * paragraph 2). For now, we assume that all supported RDMA
155*4882a593Smuzhiyun * transports are suitable here.
156*4882a593Smuzhiyun */
157*4882a593Smuzhiyun set_bit(XPT_CONG_CTRL, &cma_xprt->sc_xprt.xpt_flags);
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun return cma_xprt;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun static void
svc_rdma_parse_connect_private(struct svcxprt_rdma * newxprt,struct rdma_conn_param * param)163*4882a593Smuzhiyun svc_rdma_parse_connect_private(struct svcxprt_rdma *newxprt,
164*4882a593Smuzhiyun struct rdma_conn_param *param)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun const struct rpcrdma_connect_private *pmsg = param->private_data;
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun if (pmsg &&
169*4882a593Smuzhiyun pmsg->cp_magic == rpcrdma_cmp_magic &&
170*4882a593Smuzhiyun pmsg->cp_version == RPCRDMA_CMP_VERSION) {
171*4882a593Smuzhiyun newxprt->sc_snd_w_inv = pmsg->cp_flags &
172*4882a593Smuzhiyun RPCRDMA_CMP_F_SND_W_INV_OK;
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun dprintk("svcrdma: client send_size %u, recv_size %u "
175*4882a593Smuzhiyun "remote inv %ssupported\n",
176*4882a593Smuzhiyun rpcrdma_decode_buffer_size(pmsg->cp_send_size),
177*4882a593Smuzhiyun rpcrdma_decode_buffer_size(pmsg->cp_recv_size),
178*4882a593Smuzhiyun newxprt->sc_snd_w_inv ? "" : "un");
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun /*
183*4882a593Smuzhiyun * This function handles the CONNECT_REQUEST event on a listening
184*4882a593Smuzhiyun * endpoint. It is passed the cma_id for the _new_ connection. The context in
185*4882a593Smuzhiyun * this cma_id is inherited from the listening cma_id and is the svc_xprt
186*4882a593Smuzhiyun * structure for the listening endpoint.
187*4882a593Smuzhiyun *
188*4882a593Smuzhiyun * This function creates a new xprt for the new connection and enqueues it on
189*4882a593Smuzhiyun * the accept queue for the listent xprt. When the listen thread is kicked, it
190*4882a593Smuzhiyun * will call the recvfrom method on the listen xprt which will accept the new
191*4882a593Smuzhiyun * connection.
192*4882a593Smuzhiyun */
handle_connect_req(struct rdma_cm_id * new_cma_id,struct rdma_conn_param * param)193*4882a593Smuzhiyun static void handle_connect_req(struct rdma_cm_id *new_cma_id,
194*4882a593Smuzhiyun struct rdma_conn_param *param)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun struct svcxprt_rdma *listen_xprt = new_cma_id->context;
197*4882a593Smuzhiyun struct svcxprt_rdma *newxprt;
198*4882a593Smuzhiyun struct sockaddr *sa;
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun /* Create a new transport */
201*4882a593Smuzhiyun newxprt = svc_rdma_create_xprt(listen_xprt->sc_xprt.xpt_server,
202*4882a593Smuzhiyun listen_xprt->sc_xprt.xpt_net);
203*4882a593Smuzhiyun if (!newxprt)
204*4882a593Smuzhiyun return;
205*4882a593Smuzhiyun newxprt->sc_cm_id = new_cma_id;
206*4882a593Smuzhiyun new_cma_id->context = newxprt;
207*4882a593Smuzhiyun svc_rdma_parse_connect_private(newxprt, param);
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun /* Save client advertised inbound read limit for use later in accept. */
210*4882a593Smuzhiyun newxprt->sc_ord = param->initiator_depth;
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
213*4882a593Smuzhiyun newxprt->sc_xprt.xpt_remotelen = svc_addr_len(sa);
214*4882a593Smuzhiyun memcpy(&newxprt->sc_xprt.xpt_remote, sa,
215*4882a593Smuzhiyun newxprt->sc_xprt.xpt_remotelen);
216*4882a593Smuzhiyun snprintf(newxprt->sc_xprt.xpt_remotebuf,
217*4882a593Smuzhiyun sizeof(newxprt->sc_xprt.xpt_remotebuf) - 1, "%pISc", sa);
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun /* The remote port is arbitrary and not under the control of the
220*4882a593Smuzhiyun * client ULP. Set it to a fixed value so that the DRC continues
221*4882a593Smuzhiyun * to be effective after a reconnect.
222*4882a593Smuzhiyun */
223*4882a593Smuzhiyun rpc_set_port((struct sockaddr *)&newxprt->sc_xprt.xpt_remote, 0);
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
226*4882a593Smuzhiyun svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun /*
229*4882a593Smuzhiyun * Enqueue the new transport on the accept queue of the listening
230*4882a593Smuzhiyun * transport
231*4882a593Smuzhiyun */
232*4882a593Smuzhiyun spin_lock(&listen_xprt->sc_lock);
233*4882a593Smuzhiyun list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
234*4882a593Smuzhiyun spin_unlock(&listen_xprt->sc_lock);
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
237*4882a593Smuzhiyun svc_xprt_enqueue(&listen_xprt->sc_xprt);
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun /**
241*4882a593Smuzhiyun * svc_rdma_listen_handler - Handle CM events generated on a listening endpoint
242*4882a593Smuzhiyun * @cma_id: the server's listener rdma_cm_id
243*4882a593Smuzhiyun * @event: details of the event
244*4882a593Smuzhiyun *
245*4882a593Smuzhiyun * Return values:
246*4882a593Smuzhiyun * %0: Do not destroy @cma_id
247*4882a593Smuzhiyun * %1: Destroy @cma_id (never returned here)
248*4882a593Smuzhiyun *
249*4882a593Smuzhiyun * NB: There is never a DEVICE_REMOVAL event for INADDR_ANY listeners.
250*4882a593Smuzhiyun */
svc_rdma_listen_handler(struct rdma_cm_id * cma_id,struct rdma_cm_event * event)251*4882a593Smuzhiyun static int svc_rdma_listen_handler(struct rdma_cm_id *cma_id,
252*4882a593Smuzhiyun struct rdma_cm_event *event)
253*4882a593Smuzhiyun {
254*4882a593Smuzhiyun switch (event->event) {
255*4882a593Smuzhiyun case RDMA_CM_EVENT_CONNECT_REQUEST:
256*4882a593Smuzhiyun handle_connect_req(cma_id, &event->param.conn);
257*4882a593Smuzhiyun break;
258*4882a593Smuzhiyun default:
259*4882a593Smuzhiyun break;
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun return 0;
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun /**
265*4882a593Smuzhiyun * svc_rdma_cma_handler - Handle CM events on client connections
266*4882a593Smuzhiyun * @cma_id: the server's listener rdma_cm_id
267*4882a593Smuzhiyun * @event: details of the event
268*4882a593Smuzhiyun *
269*4882a593Smuzhiyun * Return values:
270*4882a593Smuzhiyun * %0: Do not destroy @cma_id
271*4882a593Smuzhiyun * %1: Destroy @cma_id (never returned here)
272*4882a593Smuzhiyun */
svc_rdma_cma_handler(struct rdma_cm_id * cma_id,struct rdma_cm_event * event)273*4882a593Smuzhiyun static int svc_rdma_cma_handler(struct rdma_cm_id *cma_id,
274*4882a593Smuzhiyun struct rdma_cm_event *event)
275*4882a593Smuzhiyun {
276*4882a593Smuzhiyun struct svcxprt_rdma *rdma = cma_id->context;
277*4882a593Smuzhiyun struct svc_xprt *xprt = &rdma->sc_xprt;
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun switch (event->event) {
280*4882a593Smuzhiyun case RDMA_CM_EVENT_ESTABLISHED:
281*4882a593Smuzhiyun clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
282*4882a593Smuzhiyun svc_xprt_enqueue(xprt);
283*4882a593Smuzhiyun break;
284*4882a593Smuzhiyun case RDMA_CM_EVENT_DISCONNECTED:
285*4882a593Smuzhiyun case RDMA_CM_EVENT_DEVICE_REMOVAL:
286*4882a593Smuzhiyun set_bit(XPT_CLOSE, &xprt->xpt_flags);
287*4882a593Smuzhiyun svc_xprt_enqueue(xprt);
288*4882a593Smuzhiyun break;
289*4882a593Smuzhiyun default:
290*4882a593Smuzhiyun break;
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun return 0;
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /*
296*4882a593Smuzhiyun * Create a listening RDMA service endpoint.
297*4882a593Smuzhiyun */
svc_rdma_create(struct svc_serv * serv,struct net * net,struct sockaddr * sa,int salen,int flags)298*4882a593Smuzhiyun static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
299*4882a593Smuzhiyun struct net *net,
300*4882a593Smuzhiyun struct sockaddr *sa, int salen,
301*4882a593Smuzhiyun int flags)
302*4882a593Smuzhiyun {
303*4882a593Smuzhiyun struct rdma_cm_id *listen_id;
304*4882a593Smuzhiyun struct svcxprt_rdma *cma_xprt;
305*4882a593Smuzhiyun int ret;
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun if (sa->sa_family != AF_INET && sa->sa_family != AF_INET6)
308*4882a593Smuzhiyun return ERR_PTR(-EAFNOSUPPORT);
309*4882a593Smuzhiyun cma_xprt = svc_rdma_create_xprt(serv, net);
310*4882a593Smuzhiyun if (!cma_xprt)
311*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
312*4882a593Smuzhiyun set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
313*4882a593Smuzhiyun strcpy(cma_xprt->sc_xprt.xpt_remotebuf, "listener");
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun listen_id = rdma_create_id(net, svc_rdma_listen_handler, cma_xprt,
316*4882a593Smuzhiyun RDMA_PS_TCP, IB_QPT_RC);
317*4882a593Smuzhiyun if (IS_ERR(listen_id)) {
318*4882a593Smuzhiyun ret = PTR_ERR(listen_id);
319*4882a593Smuzhiyun goto err0;
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun /* Allow both IPv4 and IPv6 sockets to bind a single port
323*4882a593Smuzhiyun * at the same time.
324*4882a593Smuzhiyun */
325*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_IPV6)
326*4882a593Smuzhiyun ret = rdma_set_afonly(listen_id, 1);
327*4882a593Smuzhiyun if (ret)
328*4882a593Smuzhiyun goto err1;
329*4882a593Smuzhiyun #endif
330*4882a593Smuzhiyun ret = rdma_bind_addr(listen_id, sa);
331*4882a593Smuzhiyun if (ret)
332*4882a593Smuzhiyun goto err1;
333*4882a593Smuzhiyun cma_xprt->sc_cm_id = listen_id;
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
336*4882a593Smuzhiyun if (ret)
337*4882a593Smuzhiyun goto err1;
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun /*
340*4882a593Smuzhiyun * We need to use the address from the cm_id in case the
341*4882a593Smuzhiyun * caller specified 0 for the port number.
342*4882a593Smuzhiyun */
343*4882a593Smuzhiyun sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
344*4882a593Smuzhiyun svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun return &cma_xprt->sc_xprt;
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun err1:
349*4882a593Smuzhiyun rdma_destroy_id(listen_id);
350*4882a593Smuzhiyun err0:
351*4882a593Smuzhiyun kfree(cma_xprt);
352*4882a593Smuzhiyun return ERR_PTR(ret);
353*4882a593Smuzhiyun }
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun /*
356*4882a593Smuzhiyun * This is the xpo_recvfrom function for listening endpoints. Its
357*4882a593Smuzhiyun * purpose is to accept incoming connections. The CMA callback handler
358*4882a593Smuzhiyun * has already created a new transport and attached it to the new CMA
359*4882a593Smuzhiyun * ID.
360*4882a593Smuzhiyun *
361*4882a593Smuzhiyun * There is a queue of pending connections hung on the listening
362*4882a593Smuzhiyun * transport. This queue contains the new svc_xprt structure. This
363*4882a593Smuzhiyun * function takes svc_xprt structures off the accept_q and completes
364*4882a593Smuzhiyun * the connection.
365*4882a593Smuzhiyun */
svc_rdma_accept(struct svc_xprt * xprt)366*4882a593Smuzhiyun static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
367*4882a593Smuzhiyun {
368*4882a593Smuzhiyun struct svcxprt_rdma *listen_rdma;
369*4882a593Smuzhiyun struct svcxprt_rdma *newxprt = NULL;
370*4882a593Smuzhiyun struct rdma_conn_param conn_param;
371*4882a593Smuzhiyun struct rpcrdma_connect_private pmsg;
372*4882a593Smuzhiyun struct ib_qp_init_attr qp_attr;
373*4882a593Smuzhiyun unsigned int ctxts, rq_depth;
374*4882a593Smuzhiyun struct ib_device *dev;
375*4882a593Smuzhiyun int ret = 0;
376*4882a593Smuzhiyun RPC_IFDEBUG(struct sockaddr *sap);
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
379*4882a593Smuzhiyun clear_bit(XPT_CONN, &xprt->xpt_flags);
380*4882a593Smuzhiyun /* Get the next entry off the accept list */
381*4882a593Smuzhiyun spin_lock(&listen_rdma->sc_lock);
382*4882a593Smuzhiyun if (!list_empty(&listen_rdma->sc_accept_q)) {
383*4882a593Smuzhiyun newxprt = list_entry(listen_rdma->sc_accept_q.next,
384*4882a593Smuzhiyun struct svcxprt_rdma, sc_accept_q);
385*4882a593Smuzhiyun list_del_init(&newxprt->sc_accept_q);
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun if (!list_empty(&listen_rdma->sc_accept_q))
388*4882a593Smuzhiyun set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
389*4882a593Smuzhiyun spin_unlock(&listen_rdma->sc_lock);
390*4882a593Smuzhiyun if (!newxprt)
391*4882a593Smuzhiyun return NULL;
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun dev = newxprt->sc_cm_id->device;
394*4882a593Smuzhiyun newxprt->sc_port_num = newxprt->sc_cm_id->port_num;
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun /* Qualify the transport resource defaults with the
397*4882a593Smuzhiyun * capabilities of this particular device */
398*4882a593Smuzhiyun /* Transport header, head iovec, tail iovec */
399*4882a593Smuzhiyun newxprt->sc_max_send_sges = 3;
400*4882a593Smuzhiyun /* Add one SGE per page list entry */
401*4882a593Smuzhiyun newxprt->sc_max_send_sges += (svcrdma_max_req_size / PAGE_SIZE) + 1;
402*4882a593Smuzhiyun if (newxprt->sc_max_send_sges > dev->attrs.max_send_sge)
403*4882a593Smuzhiyun newxprt->sc_max_send_sges = dev->attrs.max_send_sge;
404*4882a593Smuzhiyun newxprt->sc_max_req_size = svcrdma_max_req_size;
405*4882a593Smuzhiyun newxprt->sc_max_requests = svcrdma_max_requests;
406*4882a593Smuzhiyun newxprt->sc_max_bc_requests = svcrdma_max_bc_requests;
407*4882a593Smuzhiyun rq_depth = newxprt->sc_max_requests + newxprt->sc_max_bc_requests;
408*4882a593Smuzhiyun if (rq_depth > dev->attrs.max_qp_wr) {
409*4882a593Smuzhiyun pr_warn("svcrdma: reducing receive depth to %d\n",
410*4882a593Smuzhiyun dev->attrs.max_qp_wr);
411*4882a593Smuzhiyun rq_depth = dev->attrs.max_qp_wr;
412*4882a593Smuzhiyun newxprt->sc_max_requests = rq_depth - 2;
413*4882a593Smuzhiyun newxprt->sc_max_bc_requests = 2;
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun newxprt->sc_fc_credits = cpu_to_be32(newxprt->sc_max_requests);
416*4882a593Smuzhiyun ctxts = rdma_rw_mr_factor(dev, newxprt->sc_port_num, RPCSVC_MAXPAGES);
417*4882a593Smuzhiyun ctxts *= newxprt->sc_max_requests;
418*4882a593Smuzhiyun newxprt->sc_sq_depth = rq_depth + ctxts;
419*4882a593Smuzhiyun if (newxprt->sc_sq_depth > dev->attrs.max_qp_wr) {
420*4882a593Smuzhiyun pr_warn("svcrdma: reducing send depth to %d\n",
421*4882a593Smuzhiyun dev->attrs.max_qp_wr);
422*4882a593Smuzhiyun newxprt->sc_sq_depth = dev->attrs.max_qp_wr;
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun atomic_set(&newxprt->sc_sq_avail, newxprt->sc_sq_depth);
425*4882a593Smuzhiyun
426*4882a593Smuzhiyun newxprt->sc_pd = ib_alloc_pd(dev, 0);
427*4882a593Smuzhiyun if (IS_ERR(newxprt->sc_pd)) {
428*4882a593Smuzhiyun trace_svcrdma_pd_err(newxprt, PTR_ERR(newxprt->sc_pd));
429*4882a593Smuzhiyun goto errout;
430*4882a593Smuzhiyun }
431*4882a593Smuzhiyun newxprt->sc_sq_cq = ib_alloc_cq_any(dev, newxprt, newxprt->sc_sq_depth,
432*4882a593Smuzhiyun IB_POLL_WORKQUEUE);
433*4882a593Smuzhiyun if (IS_ERR(newxprt->sc_sq_cq))
434*4882a593Smuzhiyun goto errout;
435*4882a593Smuzhiyun newxprt->sc_rq_cq =
436*4882a593Smuzhiyun ib_alloc_cq_any(dev, newxprt, rq_depth, IB_POLL_WORKQUEUE);
437*4882a593Smuzhiyun if (IS_ERR(newxprt->sc_rq_cq))
438*4882a593Smuzhiyun goto errout;
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun memset(&qp_attr, 0, sizeof qp_attr);
441*4882a593Smuzhiyun qp_attr.event_handler = qp_event_handler;
442*4882a593Smuzhiyun qp_attr.qp_context = &newxprt->sc_xprt;
443*4882a593Smuzhiyun qp_attr.port_num = newxprt->sc_port_num;
444*4882a593Smuzhiyun qp_attr.cap.max_rdma_ctxs = ctxts;
445*4882a593Smuzhiyun qp_attr.cap.max_send_wr = newxprt->sc_sq_depth - ctxts;
446*4882a593Smuzhiyun qp_attr.cap.max_recv_wr = rq_depth;
447*4882a593Smuzhiyun qp_attr.cap.max_send_sge = newxprt->sc_max_send_sges;
448*4882a593Smuzhiyun qp_attr.cap.max_recv_sge = 1;
449*4882a593Smuzhiyun qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
450*4882a593Smuzhiyun qp_attr.qp_type = IB_QPT_RC;
451*4882a593Smuzhiyun qp_attr.send_cq = newxprt->sc_sq_cq;
452*4882a593Smuzhiyun qp_attr.recv_cq = newxprt->sc_rq_cq;
453*4882a593Smuzhiyun dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n",
454*4882a593Smuzhiyun newxprt->sc_cm_id, newxprt->sc_pd);
455*4882a593Smuzhiyun dprintk(" cap.max_send_wr = %d, cap.max_recv_wr = %d\n",
456*4882a593Smuzhiyun qp_attr.cap.max_send_wr, qp_attr.cap.max_recv_wr);
457*4882a593Smuzhiyun dprintk(" cap.max_send_sge = %d, cap.max_recv_sge = %d\n",
458*4882a593Smuzhiyun qp_attr.cap.max_send_sge, qp_attr.cap.max_recv_sge);
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
461*4882a593Smuzhiyun if (ret) {
462*4882a593Smuzhiyun trace_svcrdma_qp_err(newxprt, ret);
463*4882a593Smuzhiyun goto errout;
464*4882a593Smuzhiyun }
465*4882a593Smuzhiyun newxprt->sc_qp = newxprt->sc_cm_id->qp;
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun if (!(dev->attrs.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS))
468*4882a593Smuzhiyun newxprt->sc_snd_w_inv = false;
469*4882a593Smuzhiyun if (!rdma_protocol_iwarp(dev, newxprt->sc_port_num) &&
470*4882a593Smuzhiyun !rdma_ib_or_roce(dev, newxprt->sc_port_num)) {
471*4882a593Smuzhiyun trace_svcrdma_fabric_err(newxprt, -EINVAL);
472*4882a593Smuzhiyun goto errout;
473*4882a593Smuzhiyun }
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun if (!svc_rdma_post_recvs(newxprt))
476*4882a593Smuzhiyun goto errout;
477*4882a593Smuzhiyun
478*4882a593Smuzhiyun /* Construct RDMA-CM private message */
479*4882a593Smuzhiyun pmsg.cp_magic = rpcrdma_cmp_magic;
480*4882a593Smuzhiyun pmsg.cp_version = RPCRDMA_CMP_VERSION;
481*4882a593Smuzhiyun pmsg.cp_flags = 0;
482*4882a593Smuzhiyun pmsg.cp_send_size = pmsg.cp_recv_size =
483*4882a593Smuzhiyun rpcrdma_encode_buffer_size(newxprt->sc_max_req_size);
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun /* Accept Connection */
486*4882a593Smuzhiyun set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
487*4882a593Smuzhiyun memset(&conn_param, 0, sizeof conn_param);
488*4882a593Smuzhiyun conn_param.responder_resources = 0;
489*4882a593Smuzhiyun conn_param.initiator_depth = min_t(int, newxprt->sc_ord,
490*4882a593Smuzhiyun dev->attrs.max_qp_init_rd_atom);
491*4882a593Smuzhiyun if (!conn_param.initiator_depth) {
492*4882a593Smuzhiyun ret = -EINVAL;
493*4882a593Smuzhiyun trace_svcrdma_initdepth_err(newxprt, ret);
494*4882a593Smuzhiyun goto errout;
495*4882a593Smuzhiyun }
496*4882a593Smuzhiyun conn_param.private_data = &pmsg;
497*4882a593Smuzhiyun conn_param.private_data_len = sizeof(pmsg);
498*4882a593Smuzhiyun rdma_lock_handler(newxprt->sc_cm_id);
499*4882a593Smuzhiyun newxprt->sc_cm_id->event_handler = svc_rdma_cma_handler;
500*4882a593Smuzhiyun ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
501*4882a593Smuzhiyun rdma_unlock_handler(newxprt->sc_cm_id);
502*4882a593Smuzhiyun if (ret) {
503*4882a593Smuzhiyun trace_svcrdma_accept_err(newxprt, ret);
504*4882a593Smuzhiyun goto errout;
505*4882a593Smuzhiyun }
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
508*4882a593Smuzhiyun dprintk("svcrdma: new connection %p accepted:\n", newxprt);
509*4882a593Smuzhiyun sap = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
510*4882a593Smuzhiyun dprintk(" local address : %pIS:%u\n", sap, rpc_get_port(sap));
511*4882a593Smuzhiyun sap = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
512*4882a593Smuzhiyun dprintk(" remote address : %pIS:%u\n", sap, rpc_get_port(sap));
513*4882a593Smuzhiyun dprintk(" max_sge : %d\n", newxprt->sc_max_send_sges);
514*4882a593Smuzhiyun dprintk(" sq_depth : %d\n", newxprt->sc_sq_depth);
515*4882a593Smuzhiyun dprintk(" rdma_rw_ctxs : %d\n", ctxts);
516*4882a593Smuzhiyun dprintk(" max_requests : %d\n", newxprt->sc_max_requests);
517*4882a593Smuzhiyun dprintk(" ord : %d\n", conn_param.initiator_depth);
518*4882a593Smuzhiyun #endif
519*4882a593Smuzhiyun
520*4882a593Smuzhiyun return &newxprt->sc_xprt;
521*4882a593Smuzhiyun
522*4882a593Smuzhiyun errout:
523*4882a593Smuzhiyun /* Take a reference in case the DTO handler runs */
524*4882a593Smuzhiyun svc_xprt_get(&newxprt->sc_xprt);
525*4882a593Smuzhiyun if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
526*4882a593Smuzhiyun ib_destroy_qp(newxprt->sc_qp);
527*4882a593Smuzhiyun rdma_destroy_id(newxprt->sc_cm_id);
528*4882a593Smuzhiyun /* This call to put will destroy the transport */
529*4882a593Smuzhiyun svc_xprt_put(&newxprt->sc_xprt);
530*4882a593Smuzhiyun return NULL;
531*4882a593Smuzhiyun }
532*4882a593Smuzhiyun
svc_rdma_detach(struct svc_xprt * xprt)533*4882a593Smuzhiyun static void svc_rdma_detach(struct svc_xprt *xprt)
534*4882a593Smuzhiyun {
535*4882a593Smuzhiyun struct svcxprt_rdma *rdma =
536*4882a593Smuzhiyun container_of(xprt, struct svcxprt_rdma, sc_xprt);
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun rdma_disconnect(rdma->sc_cm_id);
539*4882a593Smuzhiyun }
540*4882a593Smuzhiyun
__svc_rdma_free(struct work_struct * work)541*4882a593Smuzhiyun static void __svc_rdma_free(struct work_struct *work)
542*4882a593Smuzhiyun {
543*4882a593Smuzhiyun struct svcxprt_rdma *rdma =
544*4882a593Smuzhiyun container_of(work, struct svcxprt_rdma, sc_work);
545*4882a593Smuzhiyun struct svc_xprt *xprt = &rdma->sc_xprt;
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun /* This blocks until the Completion Queues are empty */
548*4882a593Smuzhiyun if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
549*4882a593Smuzhiyun ib_drain_qp(rdma->sc_qp);
550*4882a593Smuzhiyun
551*4882a593Smuzhiyun svc_rdma_flush_recv_queues(rdma);
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun /* Final put of backchannel client transport */
554*4882a593Smuzhiyun if (xprt->xpt_bc_xprt) {
555*4882a593Smuzhiyun xprt_put(xprt->xpt_bc_xprt);
556*4882a593Smuzhiyun xprt->xpt_bc_xprt = NULL;
557*4882a593Smuzhiyun }
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun svc_rdma_destroy_rw_ctxts(rdma);
560*4882a593Smuzhiyun svc_rdma_send_ctxts_destroy(rdma);
561*4882a593Smuzhiyun svc_rdma_recv_ctxts_destroy(rdma);
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun /* Destroy the QP if present (not a listener) */
564*4882a593Smuzhiyun if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
565*4882a593Smuzhiyun ib_destroy_qp(rdma->sc_qp);
566*4882a593Smuzhiyun
567*4882a593Smuzhiyun if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
568*4882a593Smuzhiyun ib_free_cq(rdma->sc_sq_cq);
569*4882a593Smuzhiyun
570*4882a593Smuzhiyun if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
571*4882a593Smuzhiyun ib_free_cq(rdma->sc_rq_cq);
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
574*4882a593Smuzhiyun ib_dealloc_pd(rdma->sc_pd);
575*4882a593Smuzhiyun
576*4882a593Smuzhiyun /* Destroy the CM ID */
577*4882a593Smuzhiyun rdma_destroy_id(rdma->sc_cm_id);
578*4882a593Smuzhiyun
579*4882a593Smuzhiyun kfree(rdma);
580*4882a593Smuzhiyun }
581*4882a593Smuzhiyun
svc_rdma_free(struct svc_xprt * xprt)582*4882a593Smuzhiyun static void svc_rdma_free(struct svc_xprt *xprt)
583*4882a593Smuzhiyun {
584*4882a593Smuzhiyun struct svcxprt_rdma *rdma =
585*4882a593Smuzhiyun container_of(xprt, struct svcxprt_rdma, sc_xprt);
586*4882a593Smuzhiyun
587*4882a593Smuzhiyun INIT_WORK(&rdma->sc_work, __svc_rdma_free);
588*4882a593Smuzhiyun schedule_work(&rdma->sc_work);
589*4882a593Smuzhiyun }
590*4882a593Smuzhiyun
svc_rdma_has_wspace(struct svc_xprt * xprt)591*4882a593Smuzhiyun static int svc_rdma_has_wspace(struct svc_xprt *xprt)
592*4882a593Smuzhiyun {
593*4882a593Smuzhiyun struct svcxprt_rdma *rdma =
594*4882a593Smuzhiyun container_of(xprt, struct svcxprt_rdma, sc_xprt);
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun /*
597*4882a593Smuzhiyun * If there are already waiters on the SQ,
598*4882a593Smuzhiyun * return false.
599*4882a593Smuzhiyun */
600*4882a593Smuzhiyun if (waitqueue_active(&rdma->sc_send_wait))
601*4882a593Smuzhiyun return 0;
602*4882a593Smuzhiyun
603*4882a593Smuzhiyun /* Otherwise return true. */
604*4882a593Smuzhiyun return 1;
605*4882a593Smuzhiyun }
606*4882a593Smuzhiyun
svc_rdma_secure_port(struct svc_rqst * rqstp)607*4882a593Smuzhiyun static void svc_rdma_secure_port(struct svc_rqst *rqstp)
608*4882a593Smuzhiyun {
609*4882a593Smuzhiyun set_bit(RQ_SECURE, &rqstp->rq_flags);
610*4882a593Smuzhiyun }
611*4882a593Smuzhiyun
svc_rdma_kill_temp_xprt(struct svc_xprt * xprt)612*4882a593Smuzhiyun static void svc_rdma_kill_temp_xprt(struct svc_xprt *xprt)
613*4882a593Smuzhiyun {
614*4882a593Smuzhiyun }
615