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