]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - net/sunrpc/xprtrdma/transport.c
xprtrdma: Clean up after adding regbuf management
[thirdparty/kernel/stable.git] / net / sunrpc / xprtrdma / transport.c
CommitLineData
f58851e6
TT
1/*
2 * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the BSD-type
8 * license below:
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials provided
20 * with the distribution.
21 *
22 * Neither the name of the Network Appliance, Inc. nor the names of
23 * its contributors may be used to endorse or promote products
24 * derived from this software without specific prior written
25 * permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39
40/*
41 * transport.c
42 *
43 * This file contains the top-level implementation of an RPC RDMA
44 * transport.
45 *
46 * Naming convention: functions beginning with xprt_ are part of the
47 * transport switch. All others are RPC RDMA internal.
48 */
49
50#include <linux/module.h>
51#include <linux/init.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
62MODULE_LICENSE("Dual BSD/GPL");
63
64MODULE_DESCRIPTION("RPC/RDMA Transport for Linux kernel NFS");
65MODULE_AUTHOR("Network Appliance, Inc.");
66
67/*
68 * tunables
69 */
70
71static unsigned int xprt_rdma_slot_table_entries = RPCRDMA_DEF_SLOT_TABLE;
72static unsigned int xprt_rdma_max_inline_read = RPCRDMA_DEF_INLINE;
73static unsigned int xprt_rdma_max_inline_write = RPCRDMA_DEF_INLINE;
74static unsigned int xprt_rdma_inline_write_padding;
3197d309 75static unsigned int xprt_rdma_memreg_strategy = RPCRDMA_FRMR;
d5440e27 76 int xprt_rdma_pad_optimize = 1;
f58851e6 77
f895b252 78#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
f58851e6
TT
79
80static unsigned int min_slot_table_size = RPCRDMA_MIN_SLOT_TABLE;
81static unsigned int max_slot_table_size = RPCRDMA_MAX_SLOT_TABLE;
82static unsigned int zero;
83static unsigned int max_padding = PAGE_SIZE;
84static unsigned int min_memreg = RPCRDMA_BOUNCEBUFFERS;
85static unsigned int max_memreg = RPCRDMA_LAST - 1;
86
87static struct ctl_table_header *sunrpc_table_header;
88
fe2c6338 89static struct ctl_table xr_tunables_table[] = {
f58851e6 90 {
f58851e6
TT
91 .procname = "rdma_slot_table_entries",
92 .data = &xprt_rdma_slot_table_entries,
93 .maxlen = sizeof(unsigned int),
94 .mode = 0644,
6d456111 95 .proc_handler = proc_dointvec_minmax,
f58851e6
TT
96 .extra1 = &min_slot_table_size,
97 .extra2 = &max_slot_table_size
98 },
99 {
f58851e6
TT
100 .procname = "rdma_max_inline_read",
101 .data = &xprt_rdma_max_inline_read,
102 .maxlen = sizeof(unsigned int),
103 .mode = 0644,
6d456111 104 .proc_handler = proc_dointvec,
f58851e6
TT
105 },
106 {
f58851e6
TT
107 .procname = "rdma_max_inline_write",
108 .data = &xprt_rdma_max_inline_write,
109 .maxlen = sizeof(unsigned int),
110 .mode = 0644,
6d456111 111 .proc_handler = proc_dointvec,
f58851e6
TT
112 },
113 {
f58851e6
TT
114 .procname = "rdma_inline_write_padding",
115 .data = &xprt_rdma_inline_write_padding,
116 .maxlen = sizeof(unsigned int),
117 .mode = 0644,
6d456111 118 .proc_handler = proc_dointvec_minmax,
f58851e6
TT
119 .extra1 = &zero,
120 .extra2 = &max_padding,
121 },
122 {
f58851e6
TT
123 .procname = "rdma_memreg_strategy",
124 .data = &xprt_rdma_memreg_strategy,
125 .maxlen = sizeof(unsigned int),
126 .mode = 0644,
6d456111 127 .proc_handler = proc_dointvec_minmax,
f58851e6
TT
128 .extra1 = &min_memreg,
129 .extra2 = &max_memreg,
130 },
9191ca3b 131 {
9191ca3b
TT
132 .procname = "rdma_pad_optimize",
133 .data = &xprt_rdma_pad_optimize,
134 .maxlen = sizeof(unsigned int),
135 .mode = 0644,
6d456111 136 .proc_handler = proc_dointvec,
9191ca3b 137 },
f8572d8f 138 { },
f58851e6
TT
139};
140
fe2c6338 141static struct ctl_table sunrpc_table[] = {
f58851e6 142 {
f58851e6
TT
143 .procname = "sunrpc",
144 .mode = 0555,
145 .child = xr_tunables_table
146 },
f8572d8f 147 { },
f58851e6
TT
148};
149
150#endif
151
bfaee096
CL
152#define RPCRDMA_BIND_TO (60U * HZ)
153#define RPCRDMA_INIT_REEST_TO (5U * HZ)
154#define RPCRDMA_MAX_REEST_TO (30U * HZ)
155#define RPCRDMA_IDLE_DISC_TO (5U * 60 * HZ)
156
f58851e6
TT
157static struct rpc_xprt_ops xprt_rdma_procs; /* forward reference */
158
159static void
160xprt_rdma_format_addresses(struct rpc_xprt *xprt)
161{
c877b849 162 struct sockaddr *sap = (struct sockaddr *)
f58851e6 163 &rpcx_to_rdmad(xprt).addr;
c877b849
CL
164 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
165 char buf[64];
f58851e6 166
c877b849
CL
167 (void)rpc_ntop(sap, buf, sizeof(buf));
168 xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL);
f58851e6 169
81160e66 170 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
c877b849 171 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
f58851e6
TT
172
173 xprt->address_strings[RPC_DISPLAY_PROTO] = "rdma";
174
81160e66 175 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
c877b849
CL
176 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
177
81160e66 178 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
c877b849 179 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
f58851e6 180
f58851e6
TT
181 /* netid */
182 xprt->address_strings[RPC_DISPLAY_NETID] = "rdma";
183}
184
185static void
186xprt_rdma_free_addresses(struct rpc_xprt *xprt)
187{
33e01dc7
CL
188 unsigned int i;
189
190 for (i = 0; i < RPC_DISPLAY_MAX; i++)
191 switch (i) {
192 case RPC_DISPLAY_PROTO:
193 case RPC_DISPLAY_NETID:
194 continue;
195 default:
196 kfree(xprt->address_strings[i]);
197 }
f58851e6
TT
198}
199
200static void
201xprt_rdma_connect_worker(struct work_struct *work)
202{
5abefb86
CL
203 struct rpcrdma_xprt *r_xprt = container_of(work, struct rpcrdma_xprt,
204 rx_connect_worker.work);
205 struct rpc_xprt *xprt = &r_xprt->rx_xprt;
f58851e6
TT
206 int rc = 0;
207
d19751e7
TM
208 xprt_clear_connected(xprt);
209
210 dprintk("RPC: %s: %sconnect\n", __func__,
211 r_xprt->rx_ep.rep_connected != 0 ? "re" : "");
212 rc = rpcrdma_ep_connect(&r_xprt->rx_ep, &r_xprt->rx_ia);
213 if (rc)
214 xprt_wake_pending_tasks(xprt, rc);
215
f58851e6
TT
216 dprintk("RPC: %s: exit\n", __func__);
217 xprt_clear_connecting(xprt);
218}
219
220/*
221 * xprt_rdma_destroy
222 *
223 * Destroy the xprt.
224 * Free all memory associated with the object, including its own.
225 * NOTE: none of the *destroy methods free memory for their top-level
226 * objects, even though they may have allocated it (they do free
227 * private memory). It's up to the caller to handle it. In this
228 * case (RDMA transport), all structure memory is inlined with the
229 * struct rpcrdma_xprt.
230 */
231static void
232xprt_rdma_destroy(struct rpc_xprt *xprt)
233{
234 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
f58851e6
TT
235
236 dprintk("RPC: %s: called\n", __func__);
237
5abefb86 238 cancel_delayed_work_sync(&r_xprt->rx_connect_worker);
f58851e6
TT
239
240 xprt_clear_connected(xprt);
241
242 rpcrdma_buffer_destroy(&r_xprt->rx_buf);
7f1d5419 243 rpcrdma_ep_destroy(&r_xprt->rx_ep, &r_xprt->rx_ia);
f58851e6
TT
244 rpcrdma_ia_close(&r_xprt->rx_ia);
245
246 xprt_rdma_free_addresses(xprt);
247
e204e621 248 xprt_free(xprt);
f58851e6
TT
249
250 dprintk("RPC: %s: returning\n", __func__);
251
252 module_put(THIS_MODULE);
253}
254
2881ae74
TM
255static const struct rpc_timeout xprt_rdma_default_timeout = {
256 .to_initval = 60 * HZ,
257 .to_maxval = 60 * HZ,
258};
259
f58851e6
TT
260/**
261 * xprt_setup_rdma - Set up transport to use RDMA
262 *
263 * @args: rpc transport arguments
264 */
265static struct rpc_xprt *
266xprt_setup_rdma(struct xprt_create *args)
267{
268 struct rpcrdma_create_data_internal cdata;
269 struct rpc_xprt *xprt;
270 struct rpcrdma_xprt *new_xprt;
271 struct rpcrdma_ep *new_ep;
272 struct sockaddr_in *sin;
273 int rc;
274
275 if (args->addrlen > sizeof(xprt->addr)) {
276 dprintk("RPC: %s: address too large\n", __func__);
277 return ERR_PTR(-EBADF);
278 }
279
37aa2133 280 xprt = xprt_alloc(args->net, sizeof(struct rpcrdma_xprt),
d9ba131d 281 xprt_rdma_slot_table_entries,
bd1722d4 282 xprt_rdma_slot_table_entries);
f58851e6
TT
283 if (xprt == NULL) {
284 dprintk("RPC: %s: couldn't allocate rpcrdma_xprt\n",
285 __func__);
286 return ERR_PTR(-ENOMEM);
287 }
288
f58851e6 289 /* 60 second timeout, no retries */
ba7392bb 290 xprt->timeout = &xprt_rdma_default_timeout;
bfaee096
CL
291 xprt->bind_timeout = RPCRDMA_BIND_TO;
292 xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
293 xprt->idle_timeout = RPCRDMA_IDLE_DISC_TO;
f58851e6
TT
294
295 xprt->resvport = 0; /* privileged port not needed */
296 xprt->tsh_size = 0; /* RPC-RDMA handles framing */
f58851e6
TT
297 xprt->ops = &xprt_rdma_procs;
298
299 /*
300 * Set up RDMA-specific connect data.
301 */
302
303 /* Put server RDMA address in local cdata */
304 memcpy(&cdata.addr, args->dstaddr, args->addrlen);
305
306 /* Ensure xprt->addr holds valid server TCP (not RDMA)
307 * address, for any side protocols which peek at it */
308 xprt->prot = IPPROTO_TCP;
309 xprt->addrlen = args->addrlen;
310 memcpy(&xprt->addr, &cdata.addr, xprt->addrlen);
311
312 sin = (struct sockaddr_in *)&cdata.addr;
313 if (ntohs(sin->sin_port) != 0)
314 xprt_set_bound(xprt);
315
21454aaa
HH
316 dprintk("RPC: %s: %pI4:%u\n",
317 __func__, &sin->sin_addr.s_addr, ntohs(sin->sin_port));
f58851e6
TT
318
319 /* Set max requests */
320 cdata.max_requests = xprt->max_reqs;
321
322 /* Set some length limits */
323 cdata.rsize = RPCRDMA_MAX_SEGS * PAGE_SIZE; /* RDMA write max */
324 cdata.wsize = RPCRDMA_MAX_SEGS * PAGE_SIZE; /* RDMA read max */
325
326 cdata.inline_wsize = xprt_rdma_max_inline_write;
327 if (cdata.inline_wsize > cdata.wsize)
328 cdata.inline_wsize = cdata.wsize;
329
330 cdata.inline_rsize = xprt_rdma_max_inline_read;
331 if (cdata.inline_rsize > cdata.rsize)
332 cdata.inline_rsize = cdata.rsize;
333
334 cdata.padding = xprt_rdma_inline_write_padding;
335
336 /*
337 * Create new transport instance, which includes initialized
338 * o ia
339 * o endpoint
340 * o buffers
341 */
342
343 new_xprt = rpcx_to_rdmax(xprt);
344
345 rc = rpcrdma_ia_open(new_xprt, (struct sockaddr *) &cdata.addr,
346 xprt_rdma_memreg_strategy);
347 if (rc)
348 goto out1;
349
350 /*
351 * initialize and create ep
352 */
353 new_xprt->rx_data = cdata;
354 new_ep = &new_xprt->rx_ep;
355 new_ep->rep_remote_addr = cdata.addr;
356
357 rc = rpcrdma_ep_create(&new_xprt->rx_ep,
358 &new_xprt->rx_ia, &new_xprt->rx_data);
359 if (rc)
360 goto out2;
361
362 /*
363 * Allocate pre-registered send and receive buffers for headers and
364 * any inline data. Also specify any padding which will be provided
365 * from a preregistered zero buffer.
366 */
ac920d04 367 rc = rpcrdma_buffer_create(new_xprt);
f58851e6
TT
368 if (rc)
369 goto out3;
370
371 /*
372 * Register a callback for connection events. This is necessary because
373 * connection loss notification is async. We also catch connection loss
374 * when reaping receives.
375 */
5abefb86
CL
376 INIT_DELAYED_WORK(&new_xprt->rx_connect_worker,
377 xprt_rdma_connect_worker);
f58851e6
TT
378
379 xprt_rdma_format_addresses(xprt);
43e95988
CL
380 xprt->max_payload = rpcrdma_max_payload(new_xprt);
381 dprintk("RPC: %s: transport data payload maximum: %zu bytes\n",
382 __func__, xprt->max_payload);
f58851e6
TT
383
384 if (!try_module_get(THIS_MODULE))
385 goto out4;
386
387 return xprt;
388
389out4:
390 xprt_rdma_free_addresses(xprt);
391 rc = -EINVAL;
392out3:
7f1d5419 393 rpcrdma_ep_destroy(new_ep, &new_xprt->rx_ia);
f58851e6
TT
394out2:
395 rpcrdma_ia_close(&new_xprt->rx_ia);
396out1:
e204e621 397 xprt_free(xprt);
f58851e6
TT
398 return ERR_PTR(rc);
399}
400
401/*
402 * Close a connection, during shutdown or timeout/reconnect
403 */
404static void
405xprt_rdma_close(struct rpc_xprt *xprt)
406{
407 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
408
409 dprintk("RPC: %s: closing\n", __func__);
08ca0dce
TT
410 if (r_xprt->rx_ep.rep_connected > 0)
411 xprt->reestablish_timeout = 0;
62da3b24 412 xprt_disconnect_done(xprt);
282191cb 413 rpcrdma_ep_disconnect(&r_xprt->rx_ep, &r_xprt->rx_ia);
f58851e6
TT
414}
415
416static void
417xprt_rdma_set_port(struct rpc_xprt *xprt, u16 port)
418{
419 struct sockaddr_in *sap;
420
421 sap = (struct sockaddr_in *)&xprt->addr;
422 sap->sin_port = htons(port);
423 sap = (struct sockaddr_in *)&rpcx_to_rdmad(xprt).addr;
424 sap->sin_port = htons(port);
425 dprintk("RPC: %s: %u\n", __func__, port);
426}
427
428static void
1b092092 429xprt_rdma_connect(struct rpc_xprt *xprt, struct rpc_task *task)
f58851e6 430{
f58851e6
TT
431 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
432
0b9e7943
TM
433 if (r_xprt->rx_ep.rep_connected != 0) {
434 /* Reconnect */
5abefb86
CL
435 schedule_delayed_work(&r_xprt->rx_connect_worker,
436 xprt->reestablish_timeout);
0b9e7943 437 xprt->reestablish_timeout <<= 1;
bfaee096
CL
438 if (xprt->reestablish_timeout > RPCRDMA_MAX_REEST_TO)
439 xprt->reestablish_timeout = RPCRDMA_MAX_REEST_TO;
440 else if (xprt->reestablish_timeout < RPCRDMA_INIT_REEST_TO)
441 xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
0b9e7943 442 } else {
5abefb86 443 schedule_delayed_work(&r_xprt->rx_connect_worker, 0);
0b9e7943 444 if (!RPC_IS_ASYNC(task))
5abefb86 445 flush_delayed_work(&r_xprt->rx_connect_worker);
f58851e6
TT
446 }
447}
448
f58851e6
TT
449/*
450 * The RDMA allocate/free functions need the task structure as a place
451 * to hide the struct rpcrdma_req, which is necessary for the actual send/recv
0ca77dc3
CL
452 * sequence.
453 *
454 * The RPC layer allocates both send and receive buffers in the same call
455 * (rq_send_buf and rq_rcv_buf are both part of a single contiguous buffer).
456 * We may register rq_rcv_buf when using reply chunks.
f58851e6
TT
457 */
458static void *
459xprt_rdma_allocate(struct rpc_task *task, size_t size)
460{
a4f0835c 461 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
0ca77dc3
CL
462 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
463 struct rpcrdma_regbuf *rb;
464 struct rpcrdma_req *req;
465 size_t min_size;
466 gfp_t flags = task->tk_flags & RPC_TASK_SWAPPER ?
467 GFP_ATOMIC : GFP_NOFS;
f58851e6 468
0ca77dc3 469 req = rpcrdma_buffer_get(&r_xprt->rx_buf);
c977dea2
CL
470 if (req == NULL)
471 return NULL;
f58851e6 472
85275c87
CL
473 if (req->rl_rdmabuf == NULL)
474 goto out_rdmabuf;
0ca77dc3
CL
475 if (req->rl_sendbuf == NULL)
476 goto out_sendbuf;
477 if (size > req->rl_sendbuf->rg_size)
478 goto out_sendbuf;
479
480out:
f58851e6 481 dprintk("RPC: %s: size %zd, request 0x%p\n", __func__, size, req);
575448bd 482 req->rl_connect_cookie = 0; /* our reserved value */
0ca77dc3
CL
483 return req->rl_sendbuf->rg_base;
484
85275c87
CL
485out_rdmabuf:
486 min_size = RPCRDMA_INLINE_WRITE_THRESHOLD(task->tk_rqstp);
487 rb = rpcrdma_alloc_regbuf(&r_xprt->rx_ia, min_size, flags);
488 if (IS_ERR(rb))
489 goto out_fail;
490 req->rl_rdmabuf = rb;
491
0ca77dc3
CL
492out_sendbuf:
493 /* XDR encoding and RPC/RDMA marshaling of this request has not
494 * yet occurred. Thus a lower bound is needed to prevent buffer
495 * overrun during marshaling.
496 *
497 * RPC/RDMA marshaling may choose to send payload bearing ops
498 * inline, if the result is smaller than the inline threshold.
499 * The value of the "size" argument accounts for header
500 * requirements but not for the payload in these cases.
501 *
502 * Likewise, allocate enough space to receive a reply up to the
503 * size of the inline threshold.
504 *
505 * It's unlikely that both the send header and the received
506 * reply will be large, but slush is provided here to allow
507 * flexibility when marshaling.
508 */
509 min_size = RPCRDMA_INLINE_READ_THRESHOLD(task->tk_rqstp);
510 min_size += RPCRDMA_INLINE_WRITE_THRESHOLD(task->tk_rqstp);
511 if (size < min_size)
512 size = min_size;
513
514 rb = rpcrdma_alloc_regbuf(&r_xprt->rx_ia, size, flags);
515 if (IS_ERR(rb))
516 goto out_fail;
517 rb->rg_owner = req;
518
519 r_xprt->rx_stats.hardway_register_count += size;
520 rpcrdma_free_regbuf(&r_xprt->rx_ia, req->rl_sendbuf);
521 req->rl_sendbuf = rb;
522 goto out;
523
524out_fail:
f58851e6 525 rpcrdma_buffer_put(req);
0ca77dc3 526 r_xprt->rx_stats.failed_marshal_count++;
f58851e6
TT
527 return NULL;
528}
529
530/*
531 * This function returns all RDMA resources to the pool.
532 */
533static void
534xprt_rdma_free(void *buffer)
535{
536 struct rpcrdma_req *req;
537 struct rpcrdma_xprt *r_xprt;
0ca77dc3 538 struct rpcrdma_regbuf *rb;
f58851e6
TT
539 int i;
540
541 if (buffer == NULL)
542 return;
543
0ca77dc3
CL
544 rb = container_of(buffer, struct rpcrdma_regbuf, rg_base[0]);
545 req = rb->rg_owner;
546 r_xprt = container_of(req->rl_buffer, struct rpcrdma_xprt, rx_buf);
f58851e6 547
0ca77dc3 548 dprintk("RPC: %s: called on 0x%p\n", __func__, req->rl_reply);
f58851e6 549
f58851e6
TT
550 for (i = 0; req->rl_nchunks;) {
551 --req->rl_nchunks;
552 i += rpcrdma_deregister_external(
13c9ff8f 553 &req->rl_segments[i], r_xprt);
f58851e6
TT
554 }
555
f58851e6
TT
556 rpcrdma_buffer_put(req);
557}
558
559/*
560 * send_request invokes the meat of RPC RDMA. It must do the following:
561 * 1. Marshal the RPC request into an RPC RDMA request, which means
562 * putting a header in front of data, and creating IOVs for RDMA
563 * from those in the request.
564 * 2. In marshaling, detect opportunities for RDMA, and use them.
565 * 3. Post a recv message to set up asynch completion, then send
566 * the request (rpcrdma_ep_post).
567 * 4. No partial sends are possible in the RPC-RDMA protocol (as in UDP).
568 */
569
570static int
571xprt_rdma_send_request(struct rpc_task *task)
572{
573 struct rpc_rqst *rqst = task->tk_rqstp;
a4f0835c 574 struct rpc_xprt *xprt = rqst->rq_xprt;
f58851e6
TT
575 struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
576 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
6ab59945 577 int rc = 0;
f58851e6 578
6ab59945 579 if (req->rl_niovs == 0)
c93c6223 580 rc = rpcrdma_marshal_req(rqst);
467c9674 581 else if (r_xprt->rx_ia.ri_memreg_strategy != RPCRDMA_ALLPHYSICAL)
6ab59945
CL
582 rc = rpcrdma_marshal_chunks(rqst, 0);
583 if (rc < 0)
584 goto failed_marshal;
f58851e6
TT
585
586 if (req->rl_reply == NULL) /* e.g. reconnection */
587 rpcrdma_recv_buffer_get(req);
588
589 if (req->rl_reply) {
590 req->rl_reply->rr_func = rpcrdma_reply_handler;
591 /* this need only be done once, but... */
592 req->rl_reply->rr_xprt = xprt;
593 }
594
575448bd
TT
595 /* Must suppress retransmit to maintain credits */
596 if (req->rl_connect_cookie == xprt->connect_cookie)
597 goto drop_connection;
598 req->rl_connect_cookie = xprt->connect_cookie;
599
600 if (rpcrdma_ep_post(&r_xprt->rx_ia, &r_xprt->rx_ep, req))
601 goto drop_connection;
f58851e6 602
d60dbb20 603 rqst->rq_xmit_bytes_sent += rqst->rq_snd_buf.len;
f58851e6
TT
604 rqst->rq_bytes_sent = 0;
605 return 0;
575448bd 606
c93c6223
CL
607failed_marshal:
608 r_xprt->rx_stats.failed_marshal_count++;
609 dprintk("RPC: %s: rpcrdma_marshal_req failed, status %i\n",
610 __func__, rc);
611 if (rc == -EIO)
612 return -EIO;
575448bd
TT
613drop_connection:
614 xprt_disconnect_done(xprt);
615 return -ENOTCONN; /* implies disconnect */
f58851e6
TT
616}
617
618static void xprt_rdma_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
619{
620 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
621 long idle_time = 0;
622
623 if (xprt_connected(xprt))
624 idle_time = (long)(jiffies - xprt->last_used) / HZ;
625
626 seq_printf(seq,
627 "\txprt:\trdma %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu "
628 "%lu %lu %lu %Lu %Lu %Lu %Lu %lu %lu %lu\n",
629
630 0, /* need a local port? */
631 xprt->stat.bind_count,
632 xprt->stat.connect_count,
633 xprt->stat.connect_time,
634 idle_time,
635 xprt->stat.sends,
636 xprt->stat.recvs,
637 xprt->stat.bad_xids,
638 xprt->stat.req_u,
639 xprt->stat.bklog_u,
640
641 r_xprt->rx_stats.read_chunk_count,
642 r_xprt->rx_stats.write_chunk_count,
643 r_xprt->rx_stats.reply_chunk_count,
644 r_xprt->rx_stats.total_rdma_request,
645 r_xprt->rx_stats.total_rdma_reply,
646 r_xprt->rx_stats.pullup_copy_count,
647 r_xprt->rx_stats.fixup_copy_count,
648 r_xprt->rx_stats.hardway_register_count,
649 r_xprt->rx_stats.failed_marshal_count,
650 r_xprt->rx_stats.bad_reply_count);
651}
652
653/*
654 * Plumbing for rpc transport switch and kernel module
655 */
656
657static struct rpc_xprt_ops xprt_rdma_procs = {
e7ce710a 658 .reserve_xprt = xprt_reserve_xprt_cong,
f58851e6 659 .release_xprt = xprt_release_xprt_cong, /* sunrpc/xprt.c */
f39c1bfb 660 .alloc_slot = xprt_alloc_slot,
f58851e6
TT
661 .release_request = xprt_release_rqst_cong, /* ditto */
662 .set_retrans_timeout = xprt_set_retrans_timeout_def, /* ditto */
663 .rpcbind = rpcb_getport_async, /* sunrpc/rpcb_clnt.c */
664 .set_port = xprt_rdma_set_port,
665 .connect = xprt_rdma_connect,
666 .buf_alloc = xprt_rdma_allocate,
667 .buf_free = xprt_rdma_free,
668 .send_request = xprt_rdma_send_request,
669 .close = xprt_rdma_close,
670 .destroy = xprt_rdma_destroy,
671 .print_stats = xprt_rdma_print_stats
672};
673
674static struct xprt_class xprt_rdma = {
675 .list = LIST_HEAD_INIT(xprt_rdma.list),
676 .name = "rdma",
677 .owner = THIS_MODULE,
678 .ident = XPRT_TRANSPORT_RDMA,
679 .setup = xprt_setup_rdma,
680};
681
682static void __exit xprt_rdma_cleanup(void)
683{
684 int rc;
685
3a0799a9 686 dprintk("RPCRDMA Module Removed, deregister RPC RDMA transport\n");
f895b252 687#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
f58851e6
TT
688 if (sunrpc_table_header) {
689 unregister_sysctl_table(sunrpc_table_header);
690 sunrpc_table_header = NULL;
691 }
692#endif
693 rc = xprt_unregister_transport(&xprt_rdma);
694 if (rc)
695 dprintk("RPC: %s: xprt_unregister returned %i\n",
696 __func__, rc);
697}
698
699static int __init xprt_rdma_init(void)
700{
701 int rc;
702
703 rc = xprt_register_transport(&xprt_rdma);
704
705 if (rc)
706 return rc;
707
3a0799a9 708 dprintk("RPCRDMA Module Init, register RPC RDMA transport\n");
f58851e6 709
3a0799a9
CL
710 dprintk("Defaults:\n");
711 dprintk("\tSlots %d\n"
f58851e6
TT
712 "\tMaxInlineRead %d\n\tMaxInlineWrite %d\n",
713 xprt_rdma_slot_table_entries,
714 xprt_rdma_max_inline_read, xprt_rdma_max_inline_write);
3a0799a9 715 dprintk("\tPadding %d\n\tMemreg %d\n",
f58851e6
TT
716 xprt_rdma_inline_write_padding, xprt_rdma_memreg_strategy);
717
f895b252 718#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
f58851e6
TT
719 if (!sunrpc_table_header)
720 sunrpc_table_header = register_sysctl_table(sunrpc_table);
721#endif
722 return 0;
723}
724
725module_init(xprt_rdma_init);
726module_exit(xprt_rdma_cleanup);