1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (c) 2015-2018 Oracle. All rights reserved.
4  * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved.
5  * Copyright (c) 2005-2007 Network Appliance, Inc. All rights reserved.
6  *
7  * This software is available to you under a choice of one of two
8  * licenses.  You may choose to be licensed under the terms of the GNU
9  * General Public License (GPL) Version 2, available from the file
10  * COPYING in the main directory of this source tree, or the BSD-type
11  * license below:
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  *      Redistributions of source code must retain the above copyright
18  *      notice, this list of conditions and the following disclaimer.
19  *
20  *      Redistributions in binary form must reproduce the above
21  *      copyright notice, this list of conditions and the following
22  *      disclaimer in the documentation and/or other materials provided
23  *      with the distribution.
24  *
25  *      Neither the name of the Network Appliance, Inc. nor the names of
26  *      its contributors may be used to endorse or promote products
27  *      derived from this software without specific prior written
28  *      permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41  *
42  * Author: Tom Tucker <tom@opengridcomputing.com>
43  */
44 
45 #include <linux/interrupt.h>
46 #include <linux/sched.h>
47 #include <linux/slab.h>
48 #include <linux/spinlock.h>
49 #include <linux/workqueue.h>
50 #include <linux/export.h>
51 
52 #include <rdma/ib_verbs.h>
53 #include <rdma/rdma_cm.h>
54 #include <rdma/rw.h>
55 
56 #include <linux/sunrpc/addr.h>
57 #include <linux/sunrpc/debug.h>
58 #include <linux/sunrpc/rpc_rdma.h>
59 #include <linux/sunrpc/svc_xprt.h>
60 #include <linux/sunrpc/svc_rdma.h>
61 
62 #include "xprt_rdma.h"
63 #include <trace/events/rpcrdma.h>
64 
65 #define RPCDBG_FACILITY	RPCDBG_SVCXPRT
66 
67 static struct svcxprt_rdma *svc_rdma_create_xprt(struct svc_serv *serv,
68 						 struct net *net);
69 static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
70 					struct net *net,
71 					struct sockaddr *sa, int salen,
72 					int flags);
73 static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt);
74 static void svc_rdma_release_rqst(struct svc_rqst *);
75 static void svc_rdma_detach(struct svc_xprt *xprt);
76 static void svc_rdma_free(struct svc_xprt *xprt);
77 static int svc_rdma_has_wspace(struct svc_xprt *xprt);
78 static void svc_rdma_secure_port(struct svc_rqst *);
79 static void svc_rdma_kill_temp_xprt(struct svc_xprt *);
80 
81 static const struct svc_xprt_ops svc_rdma_ops = {
82 	.xpo_create = svc_rdma_create,
83 	.xpo_recvfrom = svc_rdma_recvfrom,
84 	.xpo_sendto = svc_rdma_sendto,
85 	.xpo_read_payload = svc_rdma_read_payload,
86 	.xpo_release_rqst = svc_rdma_release_rqst,
87 	.xpo_detach = svc_rdma_detach,
88 	.xpo_free = svc_rdma_free,
89 	.xpo_has_wspace = svc_rdma_has_wspace,
90 	.xpo_accept = svc_rdma_accept,
91 	.xpo_secure_port = svc_rdma_secure_port,
92 	.xpo_kill_temp_xprt = svc_rdma_kill_temp_xprt,
93 };
94 
95 struct svc_xprt_class svc_rdma_class = {
96 	.xcl_name = "rdma",
97 	.xcl_owner = THIS_MODULE,
98 	.xcl_ops = &svc_rdma_ops,
99 	.xcl_max_payload = RPCSVC_MAXPAYLOAD_RDMA,
100 	.xcl_ident = XPRT_TRANSPORT_RDMA,
101 };
102 
103 /* QP event handler */
104 static void qp_event_handler(struct ib_event *event, void *context)
105 {
106 	struct svc_xprt *xprt = context;
107 
108 	trace_svcrdma_qp_error(event, (struct sockaddr *)&xprt->xpt_remote);
109 	switch (event->event) {
110 	/* These are considered benign events */
111 	case IB_EVENT_PATH_MIG:
112 	case IB_EVENT_COMM_EST:
113 	case IB_EVENT_SQ_DRAINED:
114 	case IB_EVENT_QP_LAST_WQE_REACHED:
115 		break;
116 
117 	/* These are considered fatal events */
118 	case IB_EVENT_PATH_MIG_ERR:
119 	case IB_EVENT_QP_FATAL:
120 	case IB_EVENT_QP_REQ_ERR:
121 	case IB_EVENT_QP_ACCESS_ERR:
122 	case IB_EVENT_DEVICE_FATAL:
123 	default:
124 		set_bit(XPT_CLOSE, &xprt->xpt_flags);
125 		svc_xprt_enqueue(xprt);
126 		break;
127 	}
128 }
129 
130 static struct svcxprt_rdma *svc_rdma_create_xprt(struct svc_serv *serv,
131 						 struct net *net)
132 {
133 	struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
134 
135 	if (!cma_xprt) {
136 		dprintk("svcrdma: failed to create new transport\n");
137 		return NULL;
138 	}
139 	svc_xprt_init(net, &svc_rdma_class, &cma_xprt->sc_xprt, serv);
140 	INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
141 	INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
142 	INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
143 	INIT_LIST_HEAD(&cma_xprt->sc_send_ctxts);
144 	init_llist_head(&cma_xprt->sc_recv_ctxts);
145 	INIT_LIST_HEAD(&cma_xprt->sc_rw_ctxts);
146 	init_waitqueue_head(&cma_xprt->sc_send_wait);
147 
148 	spin_lock_init(&cma_xprt->sc_lock);
149 	spin_lock_init(&cma_xprt->sc_rq_dto_lock);
150 	spin_lock_init(&cma_xprt->sc_send_lock);
151 	spin_lock_init(&cma_xprt->sc_rw_ctxt_lock);
152 
153 	/*
154 	 * Note that this implies that the underlying transport support
155 	 * has some form of congestion control (see RFC 7530 section 3.1
156 	 * paragraph 2). For now, we assume that all supported RDMA
157 	 * transports are suitable here.
158 	 */
159 	set_bit(XPT_CONG_CTRL, &cma_xprt->sc_xprt.xpt_flags);
160 
161 	return cma_xprt;
162 }
163 
164 static void
165 svc_rdma_parse_connect_private(struct svcxprt_rdma *newxprt,
166 			       struct rdma_conn_param *param)
167 {
168 	const struct rpcrdma_connect_private *pmsg = param->private_data;
169 
170 	if (pmsg &&
171 	    pmsg->cp_magic == rpcrdma_cmp_magic &&
172 	    pmsg->cp_version == RPCRDMA_CMP_VERSION) {
173 		newxprt->sc_snd_w_inv = pmsg->cp_flags &
174 					RPCRDMA_CMP_F_SND_W_INV_OK;
175 
176 		dprintk("svcrdma: client send_size %u, recv_size %u "
177 			"remote inv %ssupported\n",
178 			rpcrdma_decode_buffer_size(pmsg->cp_send_size),
179 			rpcrdma_decode_buffer_size(pmsg->cp_recv_size),
180 			newxprt->sc_snd_w_inv ? "" : "un");
181 	}
182 }
183 
184 /*
185  * This function handles the CONNECT_REQUEST event on a listening
186  * endpoint. It is passed the cma_id for the _new_ connection. The context in
187  * this cma_id is inherited from the listening cma_id and is the svc_xprt
188  * structure for the listening endpoint.
189  *
190  * This function creates a new xprt for the new connection and enqueues it on
191  * the accept queue for the listent xprt. When the listen thread is kicked, it
192  * will call the recvfrom method on the listen xprt which will accept the new
193  * connection.
194  */
195 static void handle_connect_req(struct rdma_cm_id *new_cma_id,
196 			       struct rdma_conn_param *param)
197 {
198 	struct svcxprt_rdma *listen_xprt = new_cma_id->context;
199 	struct svcxprt_rdma *newxprt;
200 	struct sockaddr *sa;
201 
202 	/* Create a new transport */
203 	newxprt = svc_rdma_create_xprt(listen_xprt->sc_xprt.xpt_server,
204 				       listen_xprt->sc_xprt.xpt_net);
205 	if (!newxprt)
206 		return;
207 	newxprt->sc_cm_id = new_cma_id;
208 	new_cma_id->context = newxprt;
209 	svc_rdma_parse_connect_private(newxprt, param);
210 
211 	/* Save client advertised inbound read limit for use later in accept. */
212 	newxprt->sc_ord = param->initiator_depth;
213 
214 	sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
215 	svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
216 	/* The remote port is arbitrary and not under the control of the
217 	 * client ULP. Set it to a fixed value so that the DRC continues
218 	 * to be effective after a reconnect.
219 	 */
220 	rpc_set_port((struct sockaddr *)&newxprt->sc_xprt.xpt_remote, 0);
221 
222 	sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
223 	svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
224 
225 	/*
226 	 * Enqueue the new transport on the accept queue of the listening
227 	 * transport
228 	 */
229 	spin_lock(&listen_xprt->sc_lock);
230 	list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
231 	spin_unlock(&listen_xprt->sc_lock);
232 
233 	set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
234 	svc_xprt_enqueue(&listen_xprt->sc_xprt);
235 }
236 
237 /*
238  * Handles events generated on the listening endpoint. These events will be
239  * either be incoming connect requests or adapter removal  events.
240  */
241 static int rdma_listen_handler(struct rdma_cm_id *cma_id,
242 			       struct rdma_cm_event *event)
243 {
244 	switch (event->event) {
245 	case RDMA_CM_EVENT_CONNECT_REQUEST:
246 		dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
247 			"event = %s (%d)\n", cma_id, cma_id->context,
248 			rdma_event_msg(event->event), event->event);
249 		handle_connect_req(cma_id, &event->param.conn);
250 		break;
251 	default:
252 		/* NB: No device removal upcall for INADDR_ANY listeners */
253 		dprintk("svcrdma: Unexpected event on listening endpoint %p, "
254 			"event = %s (%d)\n", cma_id,
255 			rdma_event_msg(event->event), event->event);
256 		break;
257 	}
258 
259 	return 0;
260 }
261 
262 static int rdma_cma_handler(struct rdma_cm_id *cma_id,
263 			    struct rdma_cm_event *event)
264 {
265 	struct svcxprt_rdma *rdma = cma_id->context;
266 	struct svc_xprt *xprt = &rdma->sc_xprt;
267 
268 	switch (event->event) {
269 	case RDMA_CM_EVENT_ESTABLISHED:
270 		/* Accept complete */
271 		svc_xprt_get(xprt);
272 		dprintk("svcrdma: Connection completed on DTO xprt=%p, "
273 			"cm_id=%p\n", xprt, cma_id);
274 		clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
275 		svc_xprt_enqueue(xprt);
276 		break;
277 	case RDMA_CM_EVENT_DISCONNECTED:
278 		dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
279 			xprt, cma_id);
280 		set_bit(XPT_CLOSE, &xprt->xpt_flags);
281 		svc_xprt_enqueue(xprt);
282 		svc_xprt_put(xprt);
283 		break;
284 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
285 		dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
286 			"event = %s (%d)\n", cma_id, xprt,
287 			rdma_event_msg(event->event), event->event);
288 		set_bit(XPT_CLOSE, &xprt->xpt_flags);
289 		svc_xprt_enqueue(xprt);
290 		svc_xprt_put(xprt);
291 		break;
292 	default:
293 		dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
294 			"event = %s (%d)\n", cma_id,
295 			rdma_event_msg(event->event), event->event);
296 		break;
297 	}
298 	return 0;
299 }
300 
301 /*
302  * Create a listening RDMA service endpoint.
303  */
304 static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
305 					struct net *net,
306 					struct sockaddr *sa, int salen,
307 					int flags)
308 {
309 	struct rdma_cm_id *listen_id;
310 	struct svcxprt_rdma *cma_xprt;
311 	int ret;
312 
313 	dprintk("svcrdma: Creating RDMA listener\n");
314 	if ((sa->sa_family != AF_INET) && (sa->sa_family != AF_INET6)) {
315 		dprintk("svcrdma: Address family %d is not supported.\n", sa->sa_family);
316 		return ERR_PTR(-EAFNOSUPPORT);
317 	}
318 	cma_xprt = svc_rdma_create_xprt(serv, net);
319 	if (!cma_xprt)
320 		return ERR_PTR(-ENOMEM);
321 	set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
322 	strcpy(cma_xprt->sc_xprt.xpt_remotebuf, "listener");
323 
324 	listen_id = rdma_create_id(net, rdma_listen_handler, cma_xprt,
325 				   RDMA_PS_TCP, IB_QPT_RC);
326 	if (IS_ERR(listen_id)) {
327 		ret = PTR_ERR(listen_id);
328 		dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
329 		goto err0;
330 	}
331 
332 	/* Allow both IPv4 and IPv6 sockets to bind a single port
333 	 * at the same time.
334 	 */
335 #if IS_ENABLED(CONFIG_IPV6)
336 	ret = rdma_set_afonly(listen_id, 1);
337 	if (ret) {
338 		dprintk("svcrdma: rdma_set_afonly failed = %d\n", ret);
339 		goto err1;
340 	}
341 #endif
342 	ret = rdma_bind_addr(listen_id, sa);
343 	if (ret) {
344 		dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
345 		goto err1;
346 	}
347 	cma_xprt->sc_cm_id = listen_id;
348 
349 	ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
350 	if (ret) {
351 		dprintk("svcrdma: rdma_listen failed = %d\n", ret);
352 		goto err1;
353 	}
354 
355 	/*
356 	 * We need to use the address from the cm_id in case the
357 	 * caller specified 0 for the port number.
358 	 */
359 	sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
360 	svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
361 
362 	return &cma_xprt->sc_xprt;
363 
364  err1:
365 	rdma_destroy_id(listen_id);
366  err0:
367 	kfree(cma_xprt);
368 	return ERR_PTR(ret);
369 }
370 
371 /*
372  * This is the xpo_recvfrom function for listening endpoints. Its
373  * purpose is to accept incoming connections. The CMA callback handler
374  * has already created a new transport and attached it to the new CMA
375  * ID.
376  *
377  * There is a queue of pending connections hung on the listening
378  * transport. This queue contains the new svc_xprt structure. This
379  * function takes svc_xprt structures off the accept_q and completes
380  * the connection.
381  */
382 static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
383 {
384 	struct svcxprt_rdma *listen_rdma;
385 	struct svcxprt_rdma *newxprt = NULL;
386 	struct rdma_conn_param conn_param;
387 	struct rpcrdma_connect_private pmsg;
388 	struct ib_qp_init_attr qp_attr;
389 	unsigned int ctxts, rq_depth;
390 	struct ib_device *dev;
391 	int ret = 0;
392 	RPC_IFDEBUG(struct sockaddr *sap);
393 
394 	listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
395 	clear_bit(XPT_CONN, &xprt->xpt_flags);
396 	/* Get the next entry off the accept list */
397 	spin_lock(&listen_rdma->sc_lock);
398 	if (!list_empty(&listen_rdma->sc_accept_q)) {
399 		newxprt = list_entry(listen_rdma->sc_accept_q.next,
400 				     struct svcxprt_rdma, sc_accept_q);
401 		list_del_init(&newxprt->sc_accept_q);
402 	}
403 	if (!list_empty(&listen_rdma->sc_accept_q))
404 		set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
405 	spin_unlock(&listen_rdma->sc_lock);
406 	if (!newxprt)
407 		return NULL;
408 
409 	dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
410 		newxprt, newxprt->sc_cm_id);
411 
412 	dev = newxprt->sc_cm_id->device;
413 	newxprt->sc_port_num = newxprt->sc_cm_id->port_num;
414 
415 	/* Qualify the transport resource defaults with the
416 	 * capabilities of this particular device */
417 	/* Transport header, head iovec, tail iovec */
418 	newxprt->sc_max_send_sges = 3;
419 	/* Add one SGE per page list entry */
420 	newxprt->sc_max_send_sges += (svcrdma_max_req_size / PAGE_SIZE) + 1;
421 	if (newxprt->sc_max_send_sges > dev->attrs.max_send_sge)
422 		newxprt->sc_max_send_sges = dev->attrs.max_send_sge;
423 	newxprt->sc_max_req_size = svcrdma_max_req_size;
424 	newxprt->sc_max_requests = svcrdma_max_requests;
425 	newxprt->sc_max_bc_requests = svcrdma_max_bc_requests;
426 	rq_depth = newxprt->sc_max_requests + newxprt->sc_max_bc_requests;
427 	if (rq_depth > dev->attrs.max_qp_wr) {
428 		pr_warn("svcrdma: reducing receive depth to %d\n",
429 			dev->attrs.max_qp_wr);
430 		rq_depth = dev->attrs.max_qp_wr;
431 		newxprt->sc_max_requests = rq_depth - 2;
432 		newxprt->sc_max_bc_requests = 2;
433 	}
434 	newxprt->sc_fc_credits = cpu_to_be32(newxprt->sc_max_requests);
435 	ctxts = rdma_rw_mr_factor(dev, newxprt->sc_port_num, RPCSVC_MAXPAGES);
436 	ctxts *= newxprt->sc_max_requests;
437 	newxprt->sc_sq_depth = rq_depth + ctxts;
438 	if (newxprt->sc_sq_depth > dev->attrs.max_qp_wr) {
439 		pr_warn("svcrdma: reducing send depth to %d\n",
440 			dev->attrs.max_qp_wr);
441 		newxprt->sc_sq_depth = dev->attrs.max_qp_wr;
442 	}
443 	atomic_set(&newxprt->sc_sq_avail, newxprt->sc_sq_depth);
444 
445 	newxprt->sc_pd = ib_alloc_pd(dev, 0);
446 	if (IS_ERR(newxprt->sc_pd)) {
447 		dprintk("svcrdma: error creating PD for connect request\n");
448 		goto errout;
449 	}
450 	newxprt->sc_sq_cq = ib_alloc_cq_any(dev, newxprt, newxprt->sc_sq_depth,
451 					    IB_POLL_WORKQUEUE);
452 	if (IS_ERR(newxprt->sc_sq_cq)) {
453 		dprintk("svcrdma: error creating SQ CQ for connect request\n");
454 		goto errout;
455 	}
456 	newxprt->sc_rq_cq =
457 		ib_alloc_cq_any(dev, newxprt, rq_depth, IB_POLL_WORKQUEUE);
458 	if (IS_ERR(newxprt->sc_rq_cq)) {
459 		dprintk("svcrdma: error creating RQ CQ for connect request\n");
460 		goto errout;
461 	}
462 
463 	memset(&qp_attr, 0, sizeof qp_attr);
464 	qp_attr.event_handler = qp_event_handler;
465 	qp_attr.qp_context = &newxprt->sc_xprt;
466 	qp_attr.port_num = newxprt->sc_port_num;
467 	qp_attr.cap.max_rdma_ctxs = ctxts;
468 	qp_attr.cap.max_send_wr = newxprt->sc_sq_depth - ctxts;
469 	qp_attr.cap.max_recv_wr = rq_depth;
470 	qp_attr.cap.max_send_sge = newxprt->sc_max_send_sges;
471 	qp_attr.cap.max_recv_sge = 1;
472 	qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
473 	qp_attr.qp_type = IB_QPT_RC;
474 	qp_attr.send_cq = newxprt->sc_sq_cq;
475 	qp_attr.recv_cq = newxprt->sc_rq_cq;
476 	dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n",
477 		newxprt->sc_cm_id, newxprt->sc_pd);
478 	dprintk("    cap.max_send_wr = %d, cap.max_recv_wr = %d\n",
479 		qp_attr.cap.max_send_wr, qp_attr.cap.max_recv_wr);
480 	dprintk("    cap.max_send_sge = %d, cap.max_recv_sge = %d\n",
481 		qp_attr.cap.max_send_sge, qp_attr.cap.max_recv_sge);
482 
483 	ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
484 	if (ret) {
485 		dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
486 		goto errout;
487 	}
488 	newxprt->sc_qp = newxprt->sc_cm_id->qp;
489 
490 	if (!(dev->attrs.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS))
491 		newxprt->sc_snd_w_inv = false;
492 	if (!rdma_protocol_iwarp(dev, newxprt->sc_port_num) &&
493 	    !rdma_ib_or_roce(dev, newxprt->sc_port_num))
494 		goto errout;
495 
496 	if (!svc_rdma_post_recvs(newxprt))
497 		goto errout;
498 
499 	/* Swap out the handler */
500 	newxprt->sc_cm_id->event_handler = rdma_cma_handler;
501 
502 	/* Construct RDMA-CM private message */
503 	pmsg.cp_magic = rpcrdma_cmp_magic;
504 	pmsg.cp_version = RPCRDMA_CMP_VERSION;
505 	pmsg.cp_flags = 0;
506 	pmsg.cp_send_size = pmsg.cp_recv_size =
507 		rpcrdma_encode_buffer_size(newxprt->sc_max_req_size);
508 
509 	/* Accept Connection */
510 	set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
511 	memset(&conn_param, 0, sizeof conn_param);
512 	conn_param.responder_resources = 0;
513 	conn_param.initiator_depth = min_t(int, newxprt->sc_ord,
514 					   dev->attrs.max_qp_init_rd_atom);
515 	if (!conn_param.initiator_depth) {
516 		dprintk("svcrdma: invalid ORD setting\n");
517 		ret = -EINVAL;
518 		goto errout;
519 	}
520 	conn_param.private_data = &pmsg;
521 	conn_param.private_data_len = sizeof(pmsg);
522 	ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
523 	if (ret)
524 		goto errout;
525 
526 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
527 	dprintk("svcrdma: new connection %p accepted:\n", newxprt);
528 	sap = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
529 	dprintk("    local address   : %pIS:%u\n", sap, rpc_get_port(sap));
530 	sap = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
531 	dprintk("    remote address  : %pIS:%u\n", sap, rpc_get_port(sap));
532 	dprintk("    max_sge         : %d\n", newxprt->sc_max_send_sges);
533 	dprintk("    sq_depth        : %d\n", newxprt->sc_sq_depth);
534 	dprintk("    rdma_rw_ctxs    : %d\n", ctxts);
535 	dprintk("    max_requests    : %d\n", newxprt->sc_max_requests);
536 	dprintk("    ord             : %d\n", conn_param.initiator_depth);
537 #endif
538 
539 	trace_svcrdma_xprt_accept(&newxprt->sc_xprt);
540 	return &newxprt->sc_xprt;
541 
542  errout:
543 	dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
544 	trace_svcrdma_xprt_fail(&newxprt->sc_xprt);
545 	/* Take a reference in case the DTO handler runs */
546 	svc_xprt_get(&newxprt->sc_xprt);
547 	if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
548 		ib_destroy_qp(newxprt->sc_qp);
549 	rdma_destroy_id(newxprt->sc_cm_id);
550 	/* This call to put will destroy the transport */
551 	svc_xprt_put(&newxprt->sc_xprt);
552 	return NULL;
553 }
554 
555 static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
556 {
557 }
558 
559 /*
560  * When connected, an svc_xprt has at least two references:
561  *
562  * - A reference held by the cm_id between the ESTABLISHED and
563  *   DISCONNECTED events. If the remote peer disconnected first, this
564  *   reference could be gone.
565  *
566  * - A reference held by the svc_recv code that called this function
567  *   as part of close processing.
568  *
569  * At a minimum one references should still be held.
570  */
571 static void svc_rdma_detach(struct svc_xprt *xprt)
572 {
573 	struct svcxprt_rdma *rdma =
574 		container_of(xprt, struct svcxprt_rdma, sc_xprt);
575 
576 	/* Disconnect and flush posted WQE */
577 	rdma_disconnect(rdma->sc_cm_id);
578 }
579 
580 static void __svc_rdma_free(struct work_struct *work)
581 {
582 	struct svcxprt_rdma *rdma =
583 		container_of(work, struct svcxprt_rdma, sc_work);
584 	struct svc_xprt *xprt = &rdma->sc_xprt;
585 
586 	trace_svcrdma_xprt_free(xprt);
587 
588 	if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
589 		ib_drain_qp(rdma->sc_qp);
590 
591 	svc_rdma_flush_recv_queues(rdma);
592 
593 	/* Final put of backchannel client transport */
594 	if (xprt->xpt_bc_xprt) {
595 		xprt_put(xprt->xpt_bc_xprt);
596 		xprt->xpt_bc_xprt = NULL;
597 	}
598 
599 	svc_rdma_destroy_rw_ctxts(rdma);
600 	svc_rdma_send_ctxts_destroy(rdma);
601 	svc_rdma_recv_ctxts_destroy(rdma);
602 
603 	/* Destroy the QP if present (not a listener) */
604 	if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
605 		ib_destroy_qp(rdma->sc_qp);
606 
607 	if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
608 		ib_free_cq(rdma->sc_sq_cq);
609 
610 	if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
611 		ib_free_cq(rdma->sc_rq_cq);
612 
613 	if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
614 		ib_dealloc_pd(rdma->sc_pd);
615 
616 	/* Destroy the CM ID */
617 	rdma_destroy_id(rdma->sc_cm_id);
618 
619 	kfree(rdma);
620 }
621 
622 static void svc_rdma_free(struct svc_xprt *xprt)
623 {
624 	struct svcxprt_rdma *rdma =
625 		container_of(xprt, struct svcxprt_rdma, sc_xprt);
626 
627 	INIT_WORK(&rdma->sc_work, __svc_rdma_free);
628 	schedule_work(&rdma->sc_work);
629 }
630 
631 static int svc_rdma_has_wspace(struct svc_xprt *xprt)
632 {
633 	struct svcxprt_rdma *rdma =
634 		container_of(xprt, struct svcxprt_rdma, sc_xprt);
635 
636 	/*
637 	 * If there are already waiters on the SQ,
638 	 * return false.
639 	 */
640 	if (waitqueue_active(&rdma->sc_send_wait))
641 		return 0;
642 
643 	/* Otherwise return true. */
644 	return 1;
645 }
646 
647 static void svc_rdma_secure_port(struct svc_rqst *rqstp)
648 {
649 	set_bit(RQ_SECURE, &rqstp->rq_flags);
650 }
651 
652 static void svc_rdma_kill_temp_xprt(struct svc_xprt *xprt)
653 {
654 }
655