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