xref: /linux/net/sunrpc/xprtrdma/svc_rdma_transport.c (revision b2128290c29902315e632ea59e0504d6bc9e9b42)
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/svc_xprt.h>
59 #include <linux/sunrpc/svc_rdma.h>
60 
61 #include "xprt_rdma.h"
62 #include <trace/events/rpcrdma.h>
63 
64 #define RPCDBG_FACILITY	RPCDBG_SVCXPRT
65 
66 static struct svcxprt_rdma *svc_rdma_create_xprt(struct svc_serv *serv,
67 						 struct net *net, int node);
68 static int svc_rdma_listen_handler(struct rdma_cm_id *cma_id,
69 				   struct rdma_cm_event *event);
70 static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
71 					struct net *net,
72 					struct sockaddr *sa, int salen,
73 					int flags);
74 static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt);
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_kill_temp_xprt(struct svc_xprt *);
79 
80 static const struct svc_xprt_ops svc_rdma_ops = {
81 	.xpo_create = svc_rdma_create,
82 	.xpo_recvfrom = svc_rdma_recvfrom,
83 	.xpo_sendto = svc_rdma_sendto,
84 	.xpo_result_payload = svc_rdma_result_payload,
85 	.xpo_release_ctxt = svc_rdma_release_ctxt,
86 	.xpo_detach = svc_rdma_detach,
87 	.xpo_free = svc_rdma_free,
88 	.xpo_has_wspace = svc_rdma_has_wspace,
89 	.xpo_accept = svc_rdma_accept,
90 	.xpo_kill_temp_xprt = svc_rdma_kill_temp_xprt,
91 };
92 
93 struct svc_xprt_class svc_rdma_class = {
94 	.xcl_name = "rdma",
95 	.xcl_owner = THIS_MODULE,
96 	.xcl_ops = &svc_rdma_ops,
97 	.xcl_max_payload = RPCSVC_MAXPAYLOAD_RDMA,
98 	.xcl_ident = XPRT_TRANSPORT_RDMA,
99 };
100 
101 /**
102  * svc_rdma_xprt_deferred_close - Close an RDMA transport (deferred)
103  * @rdma: transport to close
104  */
105 void svc_rdma_xprt_deferred_close(struct svcxprt_rdma *rdma)
106 {
107 	svc_xprt_deferred_close(&rdma->sc_xprt);
108 
109 	/* Release parked sc_sq_ticket_wait and sc_send_wait waiters.
110 	 * Once XPT_CLOSE is observed each returns -ENOTCONN.
111 	 */
112 	wake_up_all(&rdma->sc_sq_ticket_wait);
113 	wake_up_all(&rdma->sc_send_wait);
114 }
115 
116 /* QP event handler */
117 static void qp_event_handler(struct ib_event *event, void *context)
118 {
119 	struct svc_xprt *xprt = context;
120 	struct svcxprt_rdma *rdma =
121 		container_of(xprt, struct svcxprt_rdma, sc_xprt);
122 
123 	trace_svcrdma_qp_error(event, (struct sockaddr *)&xprt->xpt_remote);
124 	switch (event->event) {
125 	/* These are considered benign events */
126 	case IB_EVENT_PATH_MIG:
127 	case IB_EVENT_COMM_EST:
128 	case IB_EVENT_SQ_DRAINED:
129 	case IB_EVENT_QP_LAST_WQE_REACHED:
130 		break;
131 
132 	/* These are considered fatal events */
133 	case IB_EVENT_PATH_MIG_ERR:
134 	case IB_EVENT_QP_FATAL:
135 	case IB_EVENT_QP_REQ_ERR:
136 	case IB_EVENT_QP_ACCESS_ERR:
137 	case IB_EVENT_DEVICE_FATAL:
138 	default:
139 		svc_rdma_xprt_deferred_close(rdma);
140 		break;
141 	}
142 }
143 
144 static struct rdma_cm_id *
145 svc_rdma_create_listen_id(struct net *net, struct sockaddr *sap,
146 			  void *context)
147 {
148 	struct rdma_cm_id *listen_id;
149 	int ret;
150 
151 	listen_id = rdma_create_id(net, svc_rdma_listen_handler, context,
152 				   RDMA_PS_TCP, IB_QPT_RC);
153 	if (IS_ERR(listen_id))
154 		return listen_id;
155 
156 	/* Allow both IPv4 and IPv6 sockets to bind a single port
157 	 * at the same time.
158 	 */
159 #if IS_ENABLED(CONFIG_IPV6)
160 	ret = rdma_set_afonly(listen_id, 1);
161 	if (ret)
162 		goto out_destroy;
163 #endif
164 	ret = rdma_bind_addr(listen_id, sap);
165 	if (ret)
166 		goto out_destroy;
167 
168 	ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
169 	if (ret)
170 		goto out_destroy;
171 
172 	return listen_id;
173 
174 out_destroy:
175 	rdma_destroy_id(listen_id);
176 	return ERR_PTR(ret);
177 }
178 
179 static struct svcxprt_rdma *svc_rdma_create_xprt(struct svc_serv *serv,
180 						 struct net *net, int node)
181 {
182 	static struct lock_class_key svcrdma_rwctx_lock;
183 	static struct lock_class_key svcrdma_sctx_lock;
184 	static struct lock_class_key svcrdma_dto_lock;
185 	struct svcxprt_rdma *cma_xprt;
186 
187 	cma_xprt = kzalloc_node(sizeof(*cma_xprt), GFP_KERNEL, node);
188 	if (!cma_xprt)
189 		return NULL;
190 
191 	svc_xprt_init(net, &svc_rdma_class, &cma_xprt->sc_xprt, serv);
192 	INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
193 	INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
194 	INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
195 	init_llist_head(&cma_xprt->sc_send_ctxts);
196 	init_llist_head(&cma_xprt->sc_recv_ctxts);
197 	init_llist_head(&cma_xprt->sc_rw_ctxts);
198 	init_llist_head(&cma_xprt->sc_send_release_list);
199 	init_waitqueue_head(&cma_xprt->sc_send_wait);
200 	init_waitqueue_head(&cma_xprt->sc_sq_ticket_wait);
201 
202 	spin_lock_init(&cma_xprt->sc_lock);
203 	spin_lock_init(&cma_xprt->sc_rq_dto_lock);
204 	lockdep_set_class(&cma_xprt->sc_rq_dto_lock, &svcrdma_dto_lock);
205 	spin_lock_init(&cma_xprt->sc_send_lock);
206 	lockdep_set_class(&cma_xprt->sc_send_lock, &svcrdma_sctx_lock);
207 	spin_lock_init(&cma_xprt->sc_rw_ctxt_lock);
208 	lockdep_set_class(&cma_xprt->sc_rw_ctxt_lock, &svcrdma_rwctx_lock);
209 
210 	/*
211 	 * Note that this implies that the underlying transport support
212 	 * has some form of congestion control (see RFC 7530 section 3.1
213 	 * paragraph 2). For now, we assume that all supported RDMA
214 	 * transports are suitable here.
215 	 */
216 	set_bit(XPT_CONG_CTRL, &cma_xprt->sc_xprt.xpt_flags);
217 
218 	return cma_xprt;
219 }
220 
221 static void
222 svc_rdma_parse_connect_private(struct svcxprt_rdma *newxprt,
223 			       struct rdma_conn_param *param)
224 {
225 	const struct rpcrdma_connect_private *pmsg = param->private_data;
226 
227 	if (pmsg &&
228 	    pmsg->cp_magic == rpcrdma_cmp_magic &&
229 	    pmsg->cp_version == RPCRDMA_CMP_VERSION) {
230 		newxprt->sc_snd_w_inv = pmsg->cp_flags &
231 					RPCRDMA_CMP_F_SND_W_INV_OK;
232 
233 		dprintk("svcrdma: client send_size %u, recv_size %u "
234 			"remote inv %ssupported\n",
235 			rpcrdma_decode_buffer_size(pmsg->cp_send_size),
236 			rpcrdma_decode_buffer_size(pmsg->cp_recv_size),
237 			newxprt->sc_snd_w_inv ? "" : "un");
238 	}
239 }
240 
241 /*
242  * This function handles the CONNECT_REQUEST event on a listening
243  * endpoint. It is passed the cma_id for the _new_ connection. The context in
244  * this cma_id is inherited from the listening cma_id and is the svc_xprt
245  * structure for the listening endpoint.
246  *
247  * This function creates a new xprt for the new connection and enqueues it on
248  * the accept queue for the listent xprt. When the listen thread is kicked, it
249  * will call the recvfrom method on the listen xprt which will accept the new
250  * connection.
251  */
252 static void handle_connect_req(struct rdma_cm_id *new_cma_id,
253 			       struct rdma_conn_param *param)
254 {
255 	struct svcxprt_rdma *listen_xprt = new_cma_id->context;
256 	struct svcxprt_rdma *newxprt;
257 	struct sockaddr *sa;
258 
259 	newxprt = svc_rdma_create_xprt(listen_xprt->sc_xprt.xpt_server,
260 				       listen_xprt->sc_xprt.xpt_net,
261 				       ibdev_to_node(new_cma_id->device));
262 	if (!newxprt)
263 		return;
264 	newxprt->sc_cm_id = new_cma_id;
265 	new_cma_id->context = newxprt;
266 	svc_rdma_parse_connect_private(newxprt, param);
267 
268 	/* Save client advertised inbound read limit for use later in accept. */
269 	newxprt->sc_ord = param->initiator_depth;
270 
271 	sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
272 	newxprt->sc_xprt.xpt_remotelen = svc_addr_len(sa);
273 	memcpy(&newxprt->sc_xprt.xpt_remote, sa,
274 	       newxprt->sc_xprt.xpt_remotelen);
275 	snprintf(newxprt->sc_xprt.xpt_remotebuf,
276 		 sizeof(newxprt->sc_xprt.xpt_remotebuf) - 1, "%pISc", sa);
277 
278 	/* The remote port is arbitrary and not under the control of the
279 	 * client ULP. Set it to a fixed value so that the DRC continues
280 	 * to be effective after a reconnect.
281 	 */
282 	rpc_set_port((struct sockaddr *)&newxprt->sc_xprt.xpt_remote, 0);
283 
284 	sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
285 	svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
286 
287 	/*
288 	 * Enqueue the new transport on the accept queue of the listening
289 	 * transport
290 	 */
291 	spin_lock(&listen_xprt->sc_lock);
292 	list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
293 	spin_unlock(&listen_xprt->sc_lock);
294 
295 	set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
296 	svc_xprt_enqueue(&listen_xprt->sc_xprt);
297 }
298 
299 /**
300  * svc_rdma_listen_handler - Handle CM events generated on a listening endpoint
301  * @cma_id: the server's listener rdma_cm_id
302  * @event: details of the event
303  *
304  * Return values:
305  *     %0: Do not destroy @cma_id
306  *     %1: Destroy @cma_id
307  *
308  * NB: There is never a DEVICE_REMOVAL event for INADDR_ANY listeners.
309  */
310 static int svc_rdma_listen_handler(struct rdma_cm_id *cma_id,
311 				   struct rdma_cm_event *event)
312 {
313 	struct sockaddr *sap = (struct sockaddr *)&cma_id->route.addr.src_addr;
314 	struct svcxprt_rdma *cma_xprt = cma_id->context;
315 	struct svc_xprt *cma_rdma = &cma_xprt->sc_xprt;
316 	struct rdma_cm_id *listen_id;
317 
318 	switch (event->event) {
319 	case RDMA_CM_EVENT_CONNECT_REQUEST:
320 		handle_connect_req(cma_id, &event->param.conn);
321 		break;
322 	case RDMA_CM_EVENT_ADDR_CHANGE:
323 		listen_id = svc_rdma_create_listen_id(cma_rdma->xpt_net,
324 						      sap, cma_xprt);
325 		if (IS_ERR(listen_id)) {
326 			pr_err("Listener dead, address change failed for device %s\n",
327 				cma_id->device->name);
328 		} else
329 			cma_xprt->sc_cm_id = listen_id;
330 		return 1;
331 	default:
332 		break;
333 	}
334 	return 0;
335 }
336 
337 /**
338  * svc_rdma_cma_handler - Handle CM events on client connections
339  * @cma_id: the server's listener rdma_cm_id
340  * @event: details of the event
341  *
342  * Return values:
343  *     %0: Do not destroy @cma_id
344  *     %1: Destroy @cma_id (never returned here)
345  */
346 static int svc_rdma_cma_handler(struct rdma_cm_id *cma_id,
347 				struct rdma_cm_event *event)
348 {
349 	struct svcxprt_rdma *rdma = cma_id->context;
350 	struct svc_xprt *xprt = &rdma->sc_xprt;
351 
352 	switch (event->event) {
353 	case RDMA_CM_EVENT_ESTABLISHED:
354 		clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
355 
356 		/* Handle any requests that were received while
357 		 * CONN_PENDING was set. */
358 		svc_xprt_enqueue(xprt);
359 		break;
360 	case RDMA_CM_EVENT_DISCONNECTED:
361 		svc_rdma_xprt_deferred_close(rdma);
362 		break;
363 	default:
364 		break;
365 	}
366 	return 0;
367 }
368 
369 /*
370  * Create a listening RDMA service endpoint.
371  */
372 static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
373 					struct net *net,
374 					struct sockaddr *sa, int salen,
375 					int flags)
376 {
377 	struct rdma_cm_id *listen_id;
378 	struct svcxprt_rdma *cma_xprt;
379 
380 	if (sa->sa_family != AF_INET && sa->sa_family != AF_INET6)
381 		return ERR_PTR(-EAFNOSUPPORT);
382 	cma_xprt = svc_rdma_create_xprt(serv, net, NUMA_NO_NODE);
383 	if (!cma_xprt)
384 		return ERR_PTR(-ENOMEM);
385 	set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
386 	strcpy(cma_xprt->sc_xprt.xpt_remotebuf, "listener");
387 
388 	listen_id = svc_rdma_create_listen_id(net, sa, cma_xprt);
389 	if (IS_ERR(listen_id)) {
390 		kfree(cma_xprt);
391 		return ERR_CAST(listen_id);
392 	}
393 	cma_xprt->sc_cm_id = listen_id;
394 
395 	/*
396 	 * We need to use the address from the cm_id in case the
397 	 * caller specified 0 for the port number.
398 	 */
399 	sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
400 	svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
401 
402 	return &cma_xprt->sc_xprt;
403 }
404 
405 static void svc_rdma_xprt_done(struct rpcrdma_notification *rn)
406 {
407 	struct svcxprt_rdma *rdma = container_of(rn, struct svcxprt_rdma,
408 						 sc_rn);
409 	struct rdma_cm_id *id = rdma->sc_cm_id;
410 
411 	trace_svcrdma_device_removal(id);
412 	svc_xprt_close(&rdma->sc_xprt);
413 }
414 
415 /*
416  * This is the xpo_recvfrom function for listening endpoints. Its
417  * purpose is to accept incoming connections. The CMA callback handler
418  * has already created a new transport and attached it to the new CMA
419  * ID.
420  *
421  * There is a queue of pending connections hung on the listening
422  * transport. This queue contains the new svc_xprt structure. This
423  * function takes svc_xprt structures off the accept_q and completes
424  * the connection.
425  */
426 static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
427 {
428 	unsigned int ctxts, rq_depth, maxpayload;
429 	struct svcxprt_rdma *listen_rdma;
430 	struct svcxprt_rdma *newxprt = NULL;
431 	struct rdma_conn_param conn_param;
432 	struct rpcrdma_connect_private pmsg;
433 	struct ib_qp_init_attr qp_attr;
434 	struct ib_device *dev;
435 	int ret = 0;
436 
437 	listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
438 	clear_bit(XPT_CONN, &xprt->xpt_flags);
439 	/* Get the next entry off the accept list */
440 	spin_lock(&listen_rdma->sc_lock);
441 	if (!list_empty(&listen_rdma->sc_accept_q)) {
442 		newxprt = list_entry(listen_rdma->sc_accept_q.next,
443 				     struct svcxprt_rdma, sc_accept_q);
444 		list_del_init(&newxprt->sc_accept_q);
445 	}
446 	if (!list_empty(&listen_rdma->sc_accept_q))
447 		set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
448 	spin_unlock(&listen_rdma->sc_lock);
449 	if (!newxprt)
450 		return NULL;
451 
452 	dev = newxprt->sc_cm_id->device;
453 	newxprt->sc_port_num = newxprt->sc_cm_id->port_num;
454 
455 	if (rpcrdma_rn_register(dev, &newxprt->sc_rn, svc_rdma_xprt_done))
456 		goto errout;
457 
458 	newxprt->sc_max_req_size = svcrdma_max_req_size;
459 	newxprt->sc_max_requests = svcrdma_max_requests;
460 	newxprt->sc_max_bc_requests = svcrdma_max_bc_requests;
461 	newxprt->sc_recv_batch = RPCRDMA_MAX_RECV_BATCH;
462 	newxprt->sc_fc_credits = cpu_to_be32(newxprt->sc_max_requests);
463 
464 	/* Qualify the transport's resource defaults with the
465 	 * capabilities of this particular device.
466 	 */
467 
468 	/* Transport header, head iovec, tail iovec */
469 	newxprt->sc_max_send_sges = 3;
470 	/* Add one SGE per page list entry */
471 	newxprt->sc_max_send_sges += (svcrdma_max_req_size / PAGE_SIZE) + 1;
472 	if (newxprt->sc_max_send_sges > dev->attrs.max_send_sge)
473 		newxprt->sc_max_send_sges = dev->attrs.max_send_sge;
474 	rq_depth = newxprt->sc_max_requests + newxprt->sc_max_bc_requests +
475 		   newxprt->sc_recv_batch + 1 /* drain */;
476 	if (rq_depth > dev->attrs.max_qp_wr) {
477 		rq_depth = dev->attrs.max_qp_wr;
478 		newxprt->sc_recv_batch = 1;
479 		newxprt->sc_max_requests = rq_depth - 2;
480 		newxprt->sc_max_bc_requests = 2;
481 	}
482 
483 	/* Estimate the needed number of rdma_rw contexts. The maximum
484 	 * Read and Write chunks have one segment each. Each request
485 	 * can involve one Read chunk and either a Write chunk or Reply
486 	 * chunk; thus a factor of three.
487 	 */
488 	maxpayload = min(xprt->xpt_server->sv_max_payload,
489 			 RPCSVC_MAXPAYLOAD_RDMA);
490 	ctxts = newxprt->sc_max_requests * 3 *
491 		rdma_rw_mr_factor(dev, newxprt->sc_port_num,
492 				  maxpayload >> PAGE_SHIFT);
493 
494 	newxprt->sc_sq_depth = rq_depth +
495 		rdma_rw_max_send_wr(dev, newxprt->sc_port_num, ctxts, 0);
496 	if (newxprt->sc_sq_depth > dev->attrs.max_qp_wr)
497 		newxprt->sc_sq_depth = dev->attrs.max_qp_wr;
498 	atomic_set(&newxprt->sc_sq_avail, newxprt->sc_sq_depth);
499 	atomic_set(&newxprt->sc_sq_ticket_head, 0);
500 	atomic_set(&newxprt->sc_sq_ticket_tail, 0);
501 
502 	newxprt->sc_pd = ib_alloc_pd(dev, 0);
503 	if (IS_ERR(newxprt->sc_pd)) {
504 		trace_svcrdma_pd_err(newxprt, PTR_ERR(newxprt->sc_pd));
505 		goto errout;
506 	}
507 	newxprt->sc_sq_cq = ib_alloc_cq_any(dev, newxprt, newxprt->sc_sq_depth,
508 					    IB_POLL_WORKQUEUE);
509 	if (IS_ERR(newxprt->sc_sq_cq))
510 		goto errout;
511 	newxprt->sc_rq_cq =
512 		ib_alloc_cq_any(dev, newxprt, rq_depth, IB_POLL_WORKQUEUE);
513 	if (IS_ERR(newxprt->sc_rq_cq))
514 		goto errout;
515 
516 	memset(&qp_attr, 0, sizeof qp_attr);
517 	qp_attr.event_handler = qp_event_handler;
518 	qp_attr.qp_context = &newxprt->sc_xprt;
519 	qp_attr.port_num = newxprt->sc_port_num;
520 	qp_attr.cap.max_rdma_ctxs = ctxts;
521 	qp_attr.cap.max_send_wr = newxprt->sc_sq_depth - ctxts;
522 	qp_attr.cap.max_recv_wr = rq_depth;
523 	qp_attr.cap.max_send_sge = newxprt->sc_max_send_sges;
524 	qp_attr.cap.max_recv_sge = 1;
525 	qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
526 	qp_attr.qp_type = IB_QPT_RC;
527 	qp_attr.send_cq = newxprt->sc_sq_cq;
528 	qp_attr.recv_cq = newxprt->sc_rq_cq;
529 	dprintk("    cap.max_send_wr = %d, cap.max_recv_wr = %d\n",
530 		qp_attr.cap.max_send_wr, qp_attr.cap.max_recv_wr);
531 	dprintk("    cap.max_send_sge = %d, cap.max_recv_sge = %d\n",
532 		qp_attr.cap.max_send_sge, qp_attr.cap.max_recv_sge);
533 	dprintk("    send CQ depth = %u, recv CQ depth = %u\n",
534 		newxprt->sc_sq_depth, rq_depth);
535 	ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
536 	if (ret) {
537 		trace_svcrdma_qp_err(newxprt, ret);
538 		goto errout;
539 	}
540 	newxprt->sc_max_send_sges = qp_attr.cap.max_send_sge;
541 	newxprt->sc_qp = newxprt->sc_cm_id->qp;
542 
543 	if (!(dev->attrs.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS))
544 		newxprt->sc_snd_w_inv = false;
545 	if (!rdma_protocol_iwarp(dev, newxprt->sc_port_num) &&
546 	    !rdma_ib_or_roce(dev, newxprt->sc_port_num)) {
547 		trace_svcrdma_fabric_err(newxprt, -EINVAL);
548 		goto errout;
549 	}
550 
551 	if (!svc_rdma_post_recvs(newxprt))
552 		goto errout;
553 
554 	/* Construct RDMA-CM private message */
555 	pmsg.cp_magic = rpcrdma_cmp_magic;
556 	pmsg.cp_version = RPCRDMA_CMP_VERSION;
557 	pmsg.cp_flags = 0;
558 	pmsg.cp_send_size = pmsg.cp_recv_size =
559 		rpcrdma_encode_buffer_size(newxprt->sc_max_req_size);
560 
561 	/* Accept Connection */
562 	set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
563 	memset(&conn_param, 0, sizeof conn_param);
564 	conn_param.responder_resources = 0;
565 	conn_param.initiator_depth = min_t(int, newxprt->sc_ord,
566 					   dev->attrs.max_qp_init_rd_atom);
567 	if (!conn_param.initiator_depth) {
568 		ret = -EINVAL;
569 		trace_svcrdma_initdepth_err(newxprt, ret);
570 		goto errout;
571 	}
572 	conn_param.private_data = &pmsg;
573 	conn_param.private_data_len = sizeof(pmsg);
574 	rdma_lock_handler(newxprt->sc_cm_id);
575 	newxprt->sc_cm_id->event_handler = svc_rdma_cma_handler;
576 	ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
577 	rdma_unlock_handler(newxprt->sc_cm_id);
578 	if (ret) {
579 		trace_svcrdma_accept_err(newxprt, ret);
580 		goto errout;
581 	}
582 
583 	if (IS_ENABLED(CONFIG_SUNRPC_DEBUG)) {
584 		struct sockaddr *sap;
585 
586 		dprintk("svcrdma: new connection accepted on device %s:\n", dev->name);
587 		sap = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
588 		dprintk("    local address   : %pIS:%u\n", sap, rpc_get_port(sap));
589 		sap = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
590 		dprintk("    remote address  : %pIS:%u\n", sap, rpc_get_port(sap));
591 		dprintk("    max_sge         : %d\n", newxprt->sc_max_send_sges);
592 		dprintk("    sq_depth        : %d\n", newxprt->sc_sq_depth);
593 		dprintk("    rdma_rw_ctxs    : %d\n", ctxts);
594 		dprintk("    max_requests    : %d\n", newxprt->sc_max_requests);
595 		dprintk("    ord             : %d\n", conn_param.initiator_depth);
596 	}
597 
598 	return &newxprt->sc_xprt;
599 
600  errout:
601 	/* Take a reference in case the DTO handler runs */
602 	svc_xprt_get(&newxprt->sc_xprt);
603 	if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
604 		ib_destroy_qp(newxprt->sc_qp);
605 	rdma_destroy_id(newxprt->sc_cm_id);
606 	rpcrdma_rn_unregister(dev, &newxprt->sc_rn);
607 	/* This call to put will destroy the transport */
608 	svc_xprt_put(&newxprt->sc_xprt);
609 	return NULL;
610 }
611 
612 static void svc_rdma_detach(struct svc_xprt *xprt)
613 {
614 	struct svcxprt_rdma *rdma =
615 		container_of(xprt, struct svcxprt_rdma, sc_xprt);
616 
617 	rdma_disconnect(rdma->sc_cm_id);
618 
619 	/*
620 	 * Most close paths go through svc_rdma_xprt_deferred_close(),
621 	 * which wakes the SQ waitqueues. svc_xprt_close() reaches
622 	 * detach without that helper, so wake any threads parked in
623 	 * svc_rdma_sq_wait() here as well.
624 	 */
625 	wake_up_all(&rdma->sc_sq_ticket_wait);
626 	wake_up_all(&rdma->sc_send_wait);
627 }
628 
629 /**
630  * svc_rdma_free - Release class-specific transport resources
631  * @xprt: Generic svc transport object
632  */
633 static void svc_rdma_free(struct svc_xprt *xprt)
634 {
635 	struct svcxprt_rdma *rdma =
636 		container_of(xprt, struct svcxprt_rdma, sc_xprt);
637 	struct ib_device *device = rdma->sc_cm_id->device;
638 
639 	might_sleep();
640 
641 	/* This blocks until the Completion Queues are empty */
642 	if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
643 		ib_drain_qp(rdma->sc_qp);
644 	svc_rdma_send_ctxts_drain(rdma);
645 
646 	svc_rdma_flush_recv_queues(rdma);
647 
648 	svc_rdma_destroy_rw_ctxts(rdma);
649 	svc_rdma_send_ctxts_destroy(rdma);
650 	svc_rdma_recv_ctxts_destroy(rdma);
651 
652 	/* Destroy the QP if present (not a listener) */
653 	if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
654 		ib_destroy_qp(rdma->sc_qp);
655 
656 	if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
657 		ib_free_cq(rdma->sc_sq_cq);
658 
659 	if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
660 		ib_free_cq(rdma->sc_rq_cq);
661 
662 	if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
663 		ib_dealloc_pd(rdma->sc_pd);
664 
665 	/* Destroy the CM ID */
666 	rdma_destroy_id(rdma->sc_cm_id);
667 
668 	if (!test_bit(XPT_LISTENER, &rdma->sc_xprt.xpt_flags))
669 		rpcrdma_rn_unregister(device, &rdma->sc_rn);
670 	kfree(rdma);
671 }
672 
673 static int svc_rdma_has_wspace(struct svc_xprt *xprt)
674 {
675 	struct svcxprt_rdma *rdma =
676 		container_of(xprt, struct svcxprt_rdma, sc_xprt);
677 
678 	/*
679 	 * If there are already waiters on the SQ,
680 	 * return false.
681 	 */
682 	if (waitqueue_active(&rdma->sc_send_wait) ||
683 	    waitqueue_active(&rdma->sc_sq_ticket_wait))
684 		return 0;
685 
686 	/* Otherwise return true. */
687 	return 1;
688 }
689 
690 static void svc_rdma_kill_temp_xprt(struct svc_xprt *xprt)
691 {
692 }
693