]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/sunrpc/xprtrdma/transport.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / net / sunrpc / xprtrdma / transport.c
CommitLineData
f58851e6 1/*
62b56a67 2 * Copyright (c) 2014-2017 Oracle. All rights reserved.
f58851e6
TT
3 * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the BSD-type
9 * license below:
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 *
18 * Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials provided
21 * with the distribution.
22 *
23 * Neither the name of the Network Appliance, Inc. nor the names of
24 * its contributors may be used to endorse or promote products
25 * derived from this software without specific prior written
26 * permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 */
40
41/*
42 * transport.c
43 *
44 * This file contains the top-level implementation of an RPC RDMA
45 * transport.
46 *
47 * Naming convention: functions beginning with xprt_ are part of the
48 * transport switch. All others are RPC RDMA internal.
49 */
50
51#include <linux/module.h>
5a0e3ad6 52#include <linux/slab.h>
f58851e6 53#include <linux/seq_file.h>
5976687a 54#include <linux/sunrpc/addr.h>
f58851e6
TT
55
56#include "xprt_rdma.h"
57
f895b252 58#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
f58851e6
TT
59# define RPCDBG_FACILITY RPCDBG_TRANS
60#endif
61
f58851e6
TT
62/*
63 * tunables
64 */
65
66static unsigned int xprt_rdma_slot_table_entries = RPCRDMA_DEF_SLOT_TABLE;
5d252f90 67unsigned int xprt_rdma_max_inline_read = RPCRDMA_DEF_INLINE;
f58851e6
TT
68static unsigned int xprt_rdma_max_inline_write = RPCRDMA_DEF_INLINE;
69static unsigned int xprt_rdma_inline_write_padding;
fff09594
CL
70unsigned int xprt_rdma_memreg_strategy = RPCRDMA_FRMR;
71int xprt_rdma_pad_optimize;
f58851e6 72
f895b252 73#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
f58851e6
TT
74
75static unsigned int min_slot_table_size = RPCRDMA_MIN_SLOT_TABLE;
76static unsigned int max_slot_table_size = RPCRDMA_MAX_SLOT_TABLE;
29c55422
CL
77static unsigned int min_inline_size = RPCRDMA_MIN_INLINE;
78static unsigned int max_inline_size = RPCRDMA_MAX_INLINE;
f58851e6
TT
79static unsigned int zero;
80static unsigned int max_padding = PAGE_SIZE;
81static unsigned int min_memreg = RPCRDMA_BOUNCEBUFFERS;
82static unsigned int max_memreg = RPCRDMA_LAST - 1;
83
84static struct ctl_table_header *sunrpc_table_header;
85
fe2c6338 86static struct ctl_table xr_tunables_table[] = {
f58851e6 87 {
f58851e6
TT
88 .procname = "rdma_slot_table_entries",
89 .data = &xprt_rdma_slot_table_entries,
90 .maxlen = sizeof(unsigned int),
91 .mode = 0644,
6d456111 92 .proc_handler = proc_dointvec_minmax,
f58851e6
TT
93 .extra1 = &min_slot_table_size,
94 .extra2 = &max_slot_table_size
95 },
96 {
f58851e6
TT
97 .procname = "rdma_max_inline_read",
98 .data = &xprt_rdma_max_inline_read,
99 .maxlen = sizeof(unsigned int),
100 .mode = 0644,
44829d02 101 .proc_handler = proc_dointvec_minmax,
29c55422
CL
102 .extra1 = &min_inline_size,
103 .extra2 = &max_inline_size,
f58851e6
TT
104 },
105 {
f58851e6
TT
106 .procname = "rdma_max_inline_write",
107 .data = &xprt_rdma_max_inline_write,
108 .maxlen = sizeof(unsigned int),
109 .mode = 0644,
44829d02 110 .proc_handler = proc_dointvec_minmax,
29c55422
CL
111 .extra1 = &min_inline_size,
112 .extra2 = &max_inline_size,
f58851e6
TT
113 },
114 {
f58851e6
TT
115 .procname = "rdma_inline_write_padding",
116 .data = &xprt_rdma_inline_write_padding,
117 .maxlen = sizeof(unsigned int),
118 .mode = 0644,
6d456111 119 .proc_handler = proc_dointvec_minmax,
f58851e6
TT
120 .extra1 = &zero,
121 .extra2 = &max_padding,
122 },
123 {
f58851e6
TT
124 .procname = "rdma_memreg_strategy",
125 .data = &xprt_rdma_memreg_strategy,
126 .maxlen = sizeof(unsigned int),
127 .mode = 0644,
6d456111 128 .proc_handler = proc_dointvec_minmax,
f58851e6
TT
129 .extra1 = &min_memreg,
130 .extra2 = &max_memreg,
131 },
9191ca3b 132 {
9191ca3b
TT
133 .procname = "rdma_pad_optimize",
134 .data = &xprt_rdma_pad_optimize,
135 .maxlen = sizeof(unsigned int),
136 .mode = 0644,
6d456111 137 .proc_handler = proc_dointvec,
9191ca3b 138 },
f8572d8f 139 { },
f58851e6
TT
140};
141
fe2c6338 142static struct ctl_table sunrpc_table[] = {
f58851e6 143 {
f58851e6
TT
144 .procname = "sunrpc",
145 .mode = 0555,
146 .child = xr_tunables_table
147 },
f8572d8f 148 { },
f58851e6
TT
149};
150
151#endif
152
d31ae254 153static const struct rpc_xprt_ops xprt_rdma_procs;
f58851e6 154
0dd39cae
CL
155static void
156xprt_rdma_format_addresses4(struct rpc_xprt *xprt, struct sockaddr *sap)
157{
158 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
159 char buf[20];
160
161 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
162 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
163
164 xprt->address_strings[RPC_DISPLAY_NETID] = RPCBIND_NETID_RDMA;
165}
166
167static void
168xprt_rdma_format_addresses6(struct rpc_xprt *xprt, struct sockaddr *sap)
169{
170 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
171 char buf[40];
172
173 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
174 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
175
176 xprt->address_strings[RPC_DISPLAY_NETID] = RPCBIND_NETID_RDMA6;
177}
178
5d252f90 179void
5231eb97 180xprt_rdma_format_addresses(struct rpc_xprt *xprt, struct sockaddr *sap)
f58851e6 181{
0dd39cae
CL
182 char buf[128];
183
184 switch (sap->sa_family) {
185 case AF_INET:
186 xprt_rdma_format_addresses4(xprt, sap);
187 break;
188 case AF_INET6:
189 xprt_rdma_format_addresses6(xprt, sap);
190 break;
191 default:
192 pr_err("rpcrdma: Unrecognized address family\n");
193 return;
194 }
f58851e6 195
c877b849
CL
196 (void)rpc_ntop(sap, buf, sizeof(buf));
197 xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL);
f58851e6 198
81160e66 199 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
c877b849 200 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
f58851e6 201
81160e66 202 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
c877b849 203 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
f58851e6 204
0dd39cae 205 xprt->address_strings[RPC_DISPLAY_PROTO] = "rdma";
f58851e6
TT
206}
207
5d252f90 208void
f58851e6
TT
209xprt_rdma_free_addresses(struct rpc_xprt *xprt)
210{
33e01dc7
CL
211 unsigned int i;
212
213 for (i = 0; i < RPC_DISPLAY_MAX; i++)
214 switch (i) {
215 case RPC_DISPLAY_PROTO:
216 case RPC_DISPLAY_NETID:
217 continue;
218 default:
219 kfree(xprt->address_strings[i]);
220 }
f58851e6
TT
221}
222
3a72dc77
CL
223void
224rpcrdma_conn_func(struct rpcrdma_ep *ep)
225{
226 schedule_delayed_work(&ep->rep_connect_worker, 0);
227}
228
229void
230rpcrdma_connect_worker(struct work_struct *work)
231{
232 struct rpcrdma_ep *ep =
233 container_of(work, struct rpcrdma_ep, rep_connect_worker.work);
234 struct rpcrdma_xprt *r_xprt =
235 container_of(ep, struct rpcrdma_xprt, rx_ep);
236 struct rpc_xprt *xprt = &r_xprt->rx_xprt;
237
238 spin_lock_bh(&xprt->transport_lock);
239 if (++xprt->connect_cookie == 0) /* maintain a reserved value */
240 ++xprt->connect_cookie;
241 if (ep->rep_connected > 0) {
7868c4a5
CL
242 if (!xprt_test_and_set_connected(xprt)) {
243 xprt->stat.connect_count++;
244 xprt->stat.connect_time += (long)jiffies -
245 xprt->stat.connect_start;
3a72dc77 246 xprt_wake_pending_tasks(xprt, 0);
7868c4a5 247 }
3a72dc77
CL
248 } else {
249 if (xprt_test_and_clear_connected(xprt))
250 xprt_wake_pending_tasks(xprt, -ENOTCONN);
251 }
252 spin_unlock_bh(&xprt->transport_lock);
253}
254
f58851e6
TT
255static void
256xprt_rdma_connect_worker(struct work_struct *work)
257{
5abefb86
CL
258 struct rpcrdma_xprt *r_xprt = container_of(work, struct rpcrdma_xprt,
259 rx_connect_worker.work);
260 struct rpc_xprt *xprt = &r_xprt->rx_xprt;
f58851e6
TT
261 int rc = 0;
262
d19751e7
TM
263 xprt_clear_connected(xprt);
264
265 dprintk("RPC: %s: %sconnect\n", __func__,
266 r_xprt->rx_ep.rep_connected != 0 ? "re" : "");
267 rc = rpcrdma_ep_connect(&r_xprt->rx_ep, &r_xprt->rx_ia);
268 if (rc)
269 xprt_wake_pending_tasks(xprt, rc);
270
f58851e6
TT
271 dprintk("RPC: %s: exit\n", __func__);
272 xprt_clear_connecting(xprt);
273}
274
4a068258
CL
275static void
276xprt_rdma_inject_disconnect(struct rpc_xprt *xprt)
277{
278 struct rpcrdma_xprt *r_xprt = container_of(xprt, struct rpcrdma_xprt,
279 rx_xprt);
280
281 pr_info("rpcrdma: injecting transport disconnect on xprt=%p\n", xprt);
282 rdma_disconnect(r_xprt->rx_ia.ri_id);
283}
284
f58851e6
TT
285/*
286 * xprt_rdma_destroy
287 *
288 * Destroy the xprt.
289 * Free all memory associated with the object, including its own.
290 * NOTE: none of the *destroy methods free memory for their top-level
291 * objects, even though they may have allocated it (they do free
292 * private memory). It's up to the caller to handle it. In this
293 * case (RDMA transport), all structure memory is inlined with the
294 * struct rpcrdma_xprt.
295 */
296static void
297xprt_rdma_destroy(struct rpc_xprt *xprt)
298{
299 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
f58851e6
TT
300
301 dprintk("RPC: %s: called\n", __func__);
302
5abefb86 303 cancel_delayed_work_sync(&r_xprt->rx_connect_worker);
f58851e6
TT
304
305 xprt_clear_connected(xprt);
306
7f1d5419 307 rpcrdma_ep_destroy(&r_xprt->rx_ep, &r_xprt->rx_ia);
72c02173 308 rpcrdma_buffer_destroy(&r_xprt->rx_buf);
f58851e6
TT
309 rpcrdma_ia_close(&r_xprt->rx_ia);
310
311 xprt_rdma_free_addresses(xprt);
312
e204e621 313 xprt_free(xprt);
f58851e6
TT
314
315 dprintk("RPC: %s: returning\n", __func__);
316
317 module_put(THIS_MODULE);
318}
319
2881ae74
TM
320static const struct rpc_timeout xprt_rdma_default_timeout = {
321 .to_initval = 60 * HZ,
322 .to_maxval = 60 * HZ,
323};
324
f58851e6
TT
325/**
326 * xprt_setup_rdma - Set up transport to use RDMA
327 *
328 * @args: rpc transport arguments
329 */
330static struct rpc_xprt *
331xprt_setup_rdma(struct xprt_create *args)
332{
333 struct rpcrdma_create_data_internal cdata;
334 struct rpc_xprt *xprt;
335 struct rpcrdma_xprt *new_xprt;
336 struct rpcrdma_ep *new_ep;
5231eb97 337 struct sockaddr *sap;
f58851e6
TT
338 int rc;
339
340 if (args->addrlen > sizeof(xprt->addr)) {
341 dprintk("RPC: %s: address too large\n", __func__);
342 return ERR_PTR(-EBADF);
343 }
344
37aa2133 345 xprt = xprt_alloc(args->net, sizeof(struct rpcrdma_xprt),
d9ba131d 346 xprt_rdma_slot_table_entries,
bd1722d4 347 xprt_rdma_slot_table_entries);
f58851e6
TT
348 if (xprt == NULL) {
349 dprintk("RPC: %s: couldn't allocate rpcrdma_xprt\n",
350 __func__);
351 return ERR_PTR(-ENOMEM);
352 }
353
f58851e6 354 /* 60 second timeout, no retries */
ba7392bb 355 xprt->timeout = &xprt_rdma_default_timeout;
bfaee096
CL
356 xprt->bind_timeout = RPCRDMA_BIND_TO;
357 xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
358 xprt->idle_timeout = RPCRDMA_IDLE_DISC_TO;
f58851e6
TT
359
360 xprt->resvport = 0; /* privileged port not needed */
361 xprt->tsh_size = 0; /* RPC-RDMA handles framing */
f58851e6
TT
362 xprt->ops = &xprt_rdma_procs;
363
364 /*
365 * Set up RDMA-specific connect data.
366 */
367
5231eb97
CL
368 sap = (struct sockaddr *)&cdata.addr;
369 memcpy(sap, args->dstaddr, args->addrlen);
f58851e6
TT
370
371 /* Ensure xprt->addr holds valid server TCP (not RDMA)
372 * address, for any side protocols which peek at it */
373 xprt->prot = IPPROTO_TCP;
374 xprt->addrlen = args->addrlen;
5231eb97 375 memcpy(&xprt->addr, sap, xprt->addrlen);
f58851e6 376
5231eb97 377 if (rpc_get_port(sap))
f58851e6
TT
378 xprt_set_bound(xprt);
379
f58851e6
TT
380 cdata.max_requests = xprt->max_reqs;
381
f58851e6
TT
382 cdata.rsize = RPCRDMA_MAX_SEGS * PAGE_SIZE; /* RDMA write max */
383 cdata.wsize = RPCRDMA_MAX_SEGS * PAGE_SIZE; /* RDMA read max */
384
385 cdata.inline_wsize = xprt_rdma_max_inline_write;
386 if (cdata.inline_wsize > cdata.wsize)
387 cdata.inline_wsize = cdata.wsize;
388
389 cdata.inline_rsize = xprt_rdma_max_inline_read;
390 if (cdata.inline_rsize > cdata.rsize)
391 cdata.inline_rsize = cdata.rsize;
392
393 cdata.padding = xprt_rdma_inline_write_padding;
394
395 /*
396 * Create new transport instance, which includes initialized
397 * o ia
398 * o endpoint
399 * o buffers
400 */
401
402 new_xprt = rpcx_to_rdmax(xprt);
403
fff09594 404 rc = rpcrdma_ia_open(new_xprt, sap);
f58851e6
TT
405 if (rc)
406 goto out1;
407
408 /*
409 * initialize and create ep
410 */
411 new_xprt->rx_data = cdata;
412 new_ep = &new_xprt->rx_ep;
413 new_ep->rep_remote_addr = cdata.addr;
414
415 rc = rpcrdma_ep_create(&new_xprt->rx_ep,
416 &new_xprt->rx_ia, &new_xprt->rx_data);
417 if (rc)
418 goto out2;
419
420 /*
421 * Allocate pre-registered send and receive buffers for headers and
422 * any inline data. Also specify any padding which will be provided
423 * from a preregistered zero buffer.
424 */
ac920d04 425 rc = rpcrdma_buffer_create(new_xprt);
f58851e6
TT
426 if (rc)
427 goto out3;
428
429 /*
430 * Register a callback for connection events. This is necessary because
431 * connection loss notification is async. We also catch connection loss
432 * when reaping receives.
433 */
5abefb86
CL
434 INIT_DELAYED_WORK(&new_xprt->rx_connect_worker,
435 xprt_rdma_connect_worker);
f58851e6 436
5231eb97 437 xprt_rdma_format_addresses(xprt, sap);
1c9351ee
CL
438 xprt->max_payload = new_xprt->rx_ia.ri_ops->ro_maxpages(new_xprt);
439 if (xprt->max_payload == 0)
440 goto out4;
441 xprt->max_payload <<= PAGE_SHIFT;
43e95988
CL
442 dprintk("RPC: %s: transport data payload maximum: %zu bytes\n",
443 __func__, xprt->max_payload);
f58851e6
TT
444
445 if (!try_module_get(THIS_MODULE))
446 goto out4;
447
5231eb97
CL
448 dprintk("RPC: %s: %s:%s\n", __func__,
449 xprt->address_strings[RPC_DISPLAY_ADDR],
450 xprt->address_strings[RPC_DISPLAY_PORT]);
f58851e6
TT
451 return xprt;
452
453out4:
454 xprt_rdma_free_addresses(xprt);
455 rc = -EINVAL;
456out3:
7f1d5419 457 rpcrdma_ep_destroy(new_ep, &new_xprt->rx_ia);
f58851e6
TT
458out2:
459 rpcrdma_ia_close(&new_xprt->rx_ia);
460out1:
e204e621 461 xprt_free(xprt);
f58851e6
TT
462 return ERR_PTR(rc);
463}
464
bebd0318
CL
465/**
466 * xprt_rdma_close - Close down RDMA connection
467 * @xprt: generic transport to be closed
468 *
469 * Called during transport shutdown reconnect, or device
470 * removal. Caller holds the transport's write lock.
f58851e6
TT
471 */
472static void
473xprt_rdma_close(struct rpc_xprt *xprt)
474{
475 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
bebd0318
CL
476 struct rpcrdma_ep *ep = &r_xprt->rx_ep;
477 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
478
479 dprintk("RPC: %s: closing xprt %p\n", __func__, xprt);
f58851e6 480
bebd0318
CL
481 if (test_and_clear_bit(RPCRDMA_IAF_REMOVING, &ia->ri_flags)) {
482 xprt_clear_connected(xprt);
483 rpcrdma_ia_remove(ia);
484 return;
485 }
486 if (ep->rep_connected == -ENODEV)
487 return;
488 if (ep->rep_connected > 0)
08ca0dce 489 xprt->reestablish_timeout = 0;
62da3b24 490 xprt_disconnect_done(xprt);
bebd0318 491 rpcrdma_ep_disconnect(ep, ia);
20ad9726
CL
492
493 /* Prepare @xprt for the next connection by reinitializing
494 * its credit grant to one (see RFC 8166, Section 3.3.3).
495 */
496 r_xprt->rx_buf.rb_credits = 1;
497 xprt->cwnd = RPC_CWNDSHIFT;
f58851e6
TT
498}
499
500static void
501xprt_rdma_set_port(struct rpc_xprt *xprt, u16 port)
502{
503 struct sockaddr_in *sap;
504
505 sap = (struct sockaddr_in *)&xprt->addr;
506 sap->sin_port = htons(port);
507 sap = (struct sockaddr_in *)&rpcx_to_rdmad(xprt).addr;
508 sap->sin_port = htons(port);
509 dprintk("RPC: %s: %u\n", __func__, port);
510}
511
33849792
CL
512/**
513 * xprt_rdma_timer - invoked when an RPC times out
514 * @xprt: controlling RPC transport
515 * @task: RPC task that timed out
516 *
517 * Invoked when the transport is still connected, but an RPC
518 * retransmit timeout occurs.
519 *
520 * Since RDMA connections don't have a keep-alive, forcibly
521 * disconnect and retry to connect. This drives full
522 * detection of the network path, and retransmissions of
523 * all pending RPCs.
524 */
525static void
526xprt_rdma_timer(struct rpc_xprt *xprt, struct rpc_task *task)
527{
528 dprintk("RPC: %5u %s: xprt = %p\n", task->tk_pid, __func__, xprt);
529
530 xprt_force_disconnect(xprt);
531}
532
f58851e6 533static void
1b092092 534xprt_rdma_connect(struct rpc_xprt *xprt, struct rpc_task *task)
f58851e6 535{
f58851e6
TT
536 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
537
0b9e7943
TM
538 if (r_xprt->rx_ep.rep_connected != 0) {
539 /* Reconnect */
5abefb86
CL
540 schedule_delayed_work(&r_xprt->rx_connect_worker,
541 xprt->reestablish_timeout);
0b9e7943 542 xprt->reestablish_timeout <<= 1;
bfaee096
CL
543 if (xprt->reestablish_timeout > RPCRDMA_MAX_REEST_TO)
544 xprt->reestablish_timeout = RPCRDMA_MAX_REEST_TO;
545 else if (xprt->reestablish_timeout < RPCRDMA_INIT_REEST_TO)
546 xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
0b9e7943 547 } else {
5abefb86 548 schedule_delayed_work(&r_xprt->rx_connect_worker, 0);
0b9e7943 549 if (!RPC_IS_ASYNC(task))
5abefb86 550 flush_delayed_work(&r_xprt->rx_connect_worker);
f58851e6
TT
551 }
552}
553
9c40c49f
CL
554/* Allocate a fixed-size buffer in which to construct and send the
555 * RPC-over-RDMA header for this request.
556 */
557static bool
558rpcrdma_get_rdmabuf(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req,
559 gfp_t flags)
560{
08cf2efd 561 size_t size = RPCRDMA_HDRBUF_SIZE;
9c40c49f
CL
562 struct rpcrdma_regbuf *rb;
563
564 if (req->rl_rdmabuf)
565 return true;
566
13650c23 567 rb = rpcrdma_alloc_regbuf(size, DMA_TO_DEVICE, flags);
9c40c49f
CL
568 if (IS_ERR(rb))
569 return false;
570
571 r_xprt->rx_stats.hardway_register_count += size;
572 req->rl_rdmabuf = rb;
7a80f3f0 573 xdr_buf_init(&req->rl_hdrbuf, rb->rg_base, rdmab_length(rb));
9c40c49f
CL
574 return true;
575}
576
9c40c49f
CL
577static bool
578rpcrdma_get_sendbuf(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req,
579 size_t size, gfp_t flags)
580{
581 struct rpcrdma_regbuf *rb;
9c40c49f
CL
582
583 if (req->rl_sendbuf && rdmab_length(req->rl_sendbuf) >= size)
584 return true;
585
655fec69 586 rb = rpcrdma_alloc_regbuf(size, DMA_TO_DEVICE, flags);
9c40c49f
CL
587 if (IS_ERR(rb))
588 return false;
589
13650c23 590 rpcrdma_free_regbuf(req->rl_sendbuf);
655fec69 591 r_xprt->rx_stats.hardway_register_count += size;
9c40c49f
CL
592 req->rl_sendbuf = rb;
593 return true;
594}
595
596/* The rq_rcv_buf is used only if a Reply chunk is necessary.
597 * The decision to use a Reply chunk is made later in
598 * rpcrdma_marshal_req. This buffer is registered at that time.
599 *
600 * Otherwise, the associated RPC Reply arrives in a separate
601 * Receive buffer, arbitrarily chosen by the HCA. The buffer
602 * allocated here for the RPC Reply is not utilized in that
603 * case. See rpcrdma_inline_fixup.
604 *
605 * A regbuf is used here to remember the buffer size.
606 */
607static bool
608rpcrdma_get_recvbuf(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req,
609 size_t size, gfp_t flags)
610{
611 struct rpcrdma_regbuf *rb;
612
613 if (req->rl_recvbuf && rdmab_length(req->rl_recvbuf) >= size)
614 return true;
615
13650c23 616 rb = rpcrdma_alloc_regbuf(size, DMA_NONE, flags);
9c40c49f
CL
617 if (IS_ERR(rb))
618 return false;
619
13650c23 620 rpcrdma_free_regbuf(req->rl_recvbuf);
9c40c49f
CL
621 r_xprt->rx_stats.hardway_register_count += size;
622 req->rl_recvbuf = rb;
623 return true;
624}
625
5fe6eaa1
CL
626/**
627 * xprt_rdma_allocate - allocate transport resources for an RPC
628 * @task: RPC task
629 *
630 * Return values:
631 * 0: Success; rq_buffer points to RPC buffer to use
632 * ENOMEM: Out of memory, call again later
633 * EIO: A permanent error occurred, do not retry
634 *
f58851e6 635 * The RDMA allocate/free functions need the task structure as a place
9c40c49f
CL
636 * to hide the struct rpcrdma_req, which is necessary for the actual
637 * send/recv sequence.
0ca77dc3 638 *
9c40c49f
CL
639 * xprt_rdma_allocate provides buffers that are already mapped for
640 * DMA, and a local DMA lkey is provided for each.
f58851e6 641 */
5fe6eaa1
CL
642static int
643xprt_rdma_allocate(struct rpc_task *task)
f58851e6 644{
5fe6eaa1 645 struct rpc_rqst *rqst = task->tk_rqstp;
5fe6eaa1 646 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
0ca77dc3 647 struct rpcrdma_req *req;
a0a1d50c 648 gfp_t flags;
f58851e6 649
0ca77dc3 650 req = rpcrdma_buffer_get(&r_xprt->rx_buf);
c977dea2 651 if (req == NULL)
5fe6eaa1 652 return -ENOMEM;
f58851e6 653
5d252f90 654 flags = RPCRDMA_DEF_GFP;
a0a1d50c
CL
655 if (RPC_IS_SWAPPER(task))
656 flags = __GFP_MEMALLOC | GFP_NOWAIT | __GFP_NOWARN;
657
9c40c49f
CL
658 if (!rpcrdma_get_rdmabuf(r_xprt, req, flags))
659 goto out_fail;
660 if (!rpcrdma_get_sendbuf(r_xprt, req, rqst->rq_callsize, flags))
661 goto out_fail;
662 if (!rpcrdma_get_recvbuf(r_xprt, req, rqst->rq_rcvsize, flags))
663 goto out_fail;
664
665 dprintk("RPC: %5u %s: send size = %zd, recv size = %zd, req = %p\n",
666 task->tk_pid, __func__, rqst->rq_callsize,
667 rqst->rq_rcvsize, req);
0ca77dc3 668
575448bd 669 req->rl_connect_cookie = 0; /* our reserved value */
5a6d1db4 670 rpcrdma_set_xprtdata(rqst, req);
5fe6eaa1 671 rqst->rq_buffer = req->rl_sendbuf->rg_base;
9c40c49f 672 rqst->rq_rbuffer = req->rl_recvbuf->rg_base;
5fe6eaa1 673 return 0;
0ca77dc3 674
0ca77dc3 675out_fail:
f58851e6 676 rpcrdma_buffer_put(req);
5fe6eaa1 677 return -ENOMEM;
f58851e6
TT
678}
679
3435c74a
CL
680/**
681 * xprt_rdma_free - release resources allocated by xprt_rdma_allocate
682 * @task: RPC task
683 *
684 * Caller guarantees rqst->rq_buffer is non-NULL.
f58851e6
TT
685 */
686static void
3435c74a 687xprt_rdma_free(struct rpc_task *task)
f58851e6 688{
3435c74a
CL
689 struct rpc_rqst *rqst = task->tk_rqstp;
690 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
691 struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
f58851e6 692
531cca0c 693 if (test_bit(RPCRDMA_REQ_F_BACKCHANNEL, &req->rl_flags))
ffc4d9b1
CL
694 return;
695
0ca77dc3 696 dprintk("RPC: %s: called on 0x%p\n", __func__, req->rl_reply);
f58851e6 697
0ba6f370
CL
698 if (test_bit(RPCRDMA_REQ_F_PENDING, &req->rl_flags))
699 rpcrdma_release_rqst(r_xprt, req);
f58851e6
TT
700 rpcrdma_buffer_put(req);
701}
702
7a89f9c6
CL
703/**
704 * xprt_rdma_send_request - marshal and send an RPC request
705 * @task: RPC task with an RPC message in rq_snd_buf
706 *
bebd0318
CL
707 * Caller holds the transport's write lock.
708 *
7a89f9c6
CL
709 * Return values:
710 * 0: The request has been sent
711 * ENOTCONN: Caller needs to invoke connect logic then call again
712 * ENOBUFS: Call again later to send the request
713 * EIO: A permanent error occurred. The request was not sent,
714 * and don't try it again
715 *
f58851e6 716 * send_request invokes the meat of RPC RDMA. It must do the following:
7a89f9c6 717 *
f58851e6
TT
718 * 1. Marshal the RPC request into an RPC RDMA request, which means
719 * putting a header in front of data, and creating IOVs for RDMA
720 * from those in the request.
721 * 2. In marshaling, detect opportunities for RDMA, and use them.
722 * 3. Post a recv message to set up asynch completion, then send
723 * the request (rpcrdma_ep_post).
724 * 4. No partial sends are possible in the RPC-RDMA protocol (as in UDP).
725 */
f58851e6
TT
726static int
727xprt_rdma_send_request(struct rpc_task *task)
728{
729 struct rpc_rqst *rqst = task->tk_rqstp;
a4f0835c 730 struct rpc_xprt *xprt = rqst->rq_xprt;
f58851e6
TT
731 struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
732 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
6ab59945 733 int rc = 0;
f58851e6 734
bebd0318
CL
735 if (!xprt_connected(xprt))
736 goto drop_connection;
737
9d6b0409 738 /* On retransmit, remove any previously registered chunks */
48016dce 739 if (unlikely(!list_empty(&req->rl_registered)))
4ce6c04c
CL
740 r_xprt->rx_ia.ri_ops->ro_unmap_sync(r_xprt,
741 &req->rl_registered);
9d6b0409 742
09e60641 743 rc = rpcrdma_marshal_req(r_xprt, rqst);
6ab59945
CL
744 if (rc < 0)
745 goto failed_marshal;
f58851e6
TT
746
747 if (req->rl_reply == NULL) /* e.g. reconnection */
748 rpcrdma_recv_buffer_get(req);
749
575448bd
TT
750 /* Must suppress retransmit to maintain credits */
751 if (req->rl_connect_cookie == xprt->connect_cookie)
752 goto drop_connection;
753 req->rl_connect_cookie = xprt->connect_cookie;
754
0ba6f370 755 set_bit(RPCRDMA_REQ_F_PENDING, &req->rl_flags);
575448bd
TT
756 if (rpcrdma_ep_post(&r_xprt->rx_ia, &r_xprt->rx_ep, req))
757 goto drop_connection;
f58851e6 758
d60dbb20 759 rqst->rq_xmit_bytes_sent += rqst->rq_snd_buf.len;
f58851e6
TT
760 rqst->rq_bytes_sent = 0;
761 return 0;
575448bd 762
c93c6223 763failed_marshal:
7a89f9c6
CL
764 if (rc != -ENOTCONN)
765 return rc;
575448bd
TT
766drop_connection:
767 xprt_disconnect_done(xprt);
768 return -ENOTCONN; /* implies disconnect */
f58851e6
TT
769}
770
5d252f90 771void xprt_rdma_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
f58851e6
TT
772{
773 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
774 long idle_time = 0;
775
776 if (xprt_connected(xprt))
777 idle_time = (long)(jiffies - xprt->last_used) / HZ;
778
763f7e4e
CL
779 seq_puts(seq, "\txprt:\trdma ");
780 seq_printf(seq, "%u %lu %lu %lu %ld %lu %lu %lu %llu %llu ",
781 0, /* need a local port? */
782 xprt->stat.bind_count,
783 xprt->stat.connect_count,
784 xprt->stat.connect_time,
785 idle_time,
786 xprt->stat.sends,
787 xprt->stat.recvs,
788 xprt->stat.bad_xids,
789 xprt->stat.req_u,
790 xprt->stat.bklog_u);
505bbe64 791 seq_printf(seq, "%lu %lu %lu %llu %llu %llu %llu %lu %lu %lu %lu ",
763f7e4e
CL
792 r_xprt->rx_stats.read_chunk_count,
793 r_xprt->rx_stats.write_chunk_count,
794 r_xprt->rx_stats.reply_chunk_count,
795 r_xprt->rx_stats.total_rdma_request,
796 r_xprt->rx_stats.total_rdma_reply,
797 r_xprt->rx_stats.pullup_copy_count,
798 r_xprt->rx_stats.fixup_copy_count,
799 r_xprt->rx_stats.hardway_register_count,
800 r_xprt->rx_stats.failed_marshal_count,
860477d1
CL
801 r_xprt->rx_stats.bad_reply_count,
802 r_xprt->rx_stats.nomsg_call_count);
01bb35c8 803 seq_printf(seq, "%lu %lu %lu %lu %lu %lu\n",
505bbe64 804 r_xprt->rx_stats.mrs_recovered,
e2ac236c 805 r_xprt->rx_stats.mrs_orphaned,
c8b920bb 806 r_xprt->rx_stats.mrs_allocated,
ae72950a 807 r_xprt->rx_stats.local_inv_needed,
01bb35c8
CL
808 r_xprt->rx_stats.empty_sendctx_q,
809 r_xprt->rx_stats.reply_waits_for_send);
f58851e6
TT
810}
811
d67fa4d8
JL
812static int
813xprt_rdma_enable_swap(struct rpc_xprt *xprt)
814{
a0451788 815 return 0;
d67fa4d8
JL
816}
817
818static void
819xprt_rdma_disable_swap(struct rpc_xprt *xprt)
820{
821}
822
f58851e6
TT
823/*
824 * Plumbing for rpc transport switch and kernel module
825 */
826
d31ae254 827static const struct rpc_xprt_ops xprt_rdma_procs = {
e7ce710a 828 .reserve_xprt = xprt_reserve_xprt_cong,
f58851e6 829 .release_xprt = xprt_release_xprt_cong, /* sunrpc/xprt.c */
f39c1bfb 830 .alloc_slot = xprt_alloc_slot,
f58851e6
TT
831 .release_request = xprt_release_rqst_cong, /* ditto */
832 .set_retrans_timeout = xprt_set_retrans_timeout_def, /* ditto */
33849792 833 .timer = xprt_rdma_timer,
f58851e6
TT
834 .rpcbind = rpcb_getport_async, /* sunrpc/rpcb_clnt.c */
835 .set_port = xprt_rdma_set_port,
836 .connect = xprt_rdma_connect,
837 .buf_alloc = xprt_rdma_allocate,
838 .buf_free = xprt_rdma_free,
839 .send_request = xprt_rdma_send_request,
840 .close = xprt_rdma_close,
841 .destroy = xprt_rdma_destroy,
d67fa4d8
JL
842 .print_stats = xprt_rdma_print_stats,
843 .enable_swap = xprt_rdma_enable_swap,
844 .disable_swap = xprt_rdma_disable_swap,
f531a5db
CL
845 .inject_disconnect = xprt_rdma_inject_disconnect,
846#if defined(CONFIG_SUNRPC_BACKCHANNEL)
847 .bc_setup = xprt_rdma_bc_setup,
76566773 848 .bc_up = xprt_rdma_bc_up,
6b26cc8c 849 .bc_maxpayload = xprt_rdma_bc_maxpayload,
f531a5db
CL
850 .bc_free_rqst = xprt_rdma_bc_free_rqst,
851 .bc_destroy = xprt_rdma_bc_destroy,
852#endif
f58851e6
TT
853};
854
855static struct xprt_class xprt_rdma = {
856 .list = LIST_HEAD_INIT(xprt_rdma.list),
857 .name = "rdma",
858 .owner = THIS_MODULE,
859 .ident = XPRT_TRANSPORT_RDMA,
860 .setup = xprt_setup_rdma,
861};
862
ffe1f0df 863void xprt_rdma_cleanup(void)
f58851e6
TT
864{
865 int rc;
866
3a0799a9 867 dprintk("RPCRDMA Module Removed, deregister RPC RDMA transport\n");
f895b252 868#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
f58851e6
TT
869 if (sunrpc_table_header) {
870 unregister_sysctl_table(sunrpc_table_header);
871 sunrpc_table_header = NULL;
872 }
873#endif
874 rc = xprt_unregister_transport(&xprt_rdma);
875 if (rc)
876 dprintk("RPC: %s: xprt_unregister returned %i\n",
877 __func__, rc);
951e721c 878
fe97b47c 879 rpcrdma_destroy_wq();
5d252f90
CL
880
881 rc = xprt_unregister_transport(&xprt_rdma_bc);
882 if (rc)
883 dprintk("RPC: %s: xprt_unregister(bc) returned %i\n",
884 __func__, rc);
f58851e6
TT
885}
886
ffe1f0df 887int xprt_rdma_init(void)
f58851e6
TT
888{
889 int rc;
890
fe97b47c 891 rc = rpcrdma_alloc_wq();
505bbe64 892 if (rc)
fe97b47c 893 return rc;
fe97b47c 894
951e721c
CL
895 rc = xprt_register_transport(&xprt_rdma);
896 if (rc) {
fe97b47c 897 rpcrdma_destroy_wq();
951e721c
CL
898 return rc;
899 }
900
5d252f90
CL
901 rc = xprt_register_transport(&xprt_rdma_bc);
902 if (rc) {
903 xprt_unregister_transport(&xprt_rdma);
904 rpcrdma_destroy_wq();
5d252f90
CL
905 return rc;
906 }
907
3a0799a9 908 dprintk("RPCRDMA Module Init, register RPC RDMA transport\n");
f58851e6 909
3a0799a9
CL
910 dprintk("Defaults:\n");
911 dprintk("\tSlots %d\n"
f58851e6
TT
912 "\tMaxInlineRead %d\n\tMaxInlineWrite %d\n",
913 xprt_rdma_slot_table_entries,
914 xprt_rdma_max_inline_read, xprt_rdma_max_inline_write);
3a0799a9 915 dprintk("\tPadding %d\n\tMemreg %d\n",
f58851e6
TT
916 xprt_rdma_inline_write_padding, xprt_rdma_memreg_strategy);
917
f895b252 918#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
f58851e6
TT
919 if (!sunrpc_table_header)
920 sunrpc_table_header = register_sysctl_table(sunrpc_table);
921#endif
922 return 0;
923}