xprtrdma: Allocate missing pagelist
[GitHub/moto-9609/android_kernel_motorola_exynos9610.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
58#ifdef RPC_DEBUG
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;
9191ca3b 76 int xprt_rdma_pad_optimize = 0;
f58851e6
TT
77
78#ifdef RPC_DEBUG
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
152static struct rpc_xprt_ops xprt_rdma_procs; /* forward reference */
153
154static void
155xprt_rdma_format_addresses(struct rpc_xprt *xprt)
156{
c877b849 157 struct sockaddr *sap = (struct sockaddr *)
f58851e6 158 &rpcx_to_rdmad(xprt).addr;
c877b849
CL
159 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
160 char buf[64];
f58851e6 161
c877b849
CL
162 (void)rpc_ntop(sap, buf, sizeof(buf));
163 xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL);
f58851e6 164
81160e66 165 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
c877b849 166 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
f58851e6
TT
167
168 xprt->address_strings[RPC_DISPLAY_PROTO] = "rdma";
169
81160e66 170 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
c877b849
CL
171 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
172
81160e66 173 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
c877b849 174 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
f58851e6 175
f58851e6
TT
176 /* netid */
177 xprt->address_strings[RPC_DISPLAY_NETID] = "rdma";
178}
179
180static void
181xprt_rdma_free_addresses(struct rpc_xprt *xprt)
182{
33e01dc7
CL
183 unsigned int i;
184
185 for (i = 0; i < RPC_DISPLAY_MAX; i++)
186 switch (i) {
187 case RPC_DISPLAY_PROTO:
188 case RPC_DISPLAY_NETID:
189 continue;
190 default:
191 kfree(xprt->address_strings[i]);
192 }
f58851e6
TT
193}
194
195static void
196xprt_rdma_connect_worker(struct work_struct *work)
197{
198 struct rpcrdma_xprt *r_xprt =
199 container_of(work, struct rpcrdma_xprt, rdma_connect.work);
200 struct rpc_xprt *xprt = &r_xprt->xprt;
201 int rc = 0;
202
d19751e7
TM
203 current->flags |= PF_FSTRANS;
204 xprt_clear_connected(xprt);
205
206 dprintk("RPC: %s: %sconnect\n", __func__,
207 r_xprt->rx_ep.rep_connected != 0 ? "re" : "");
208 rc = rpcrdma_ep_connect(&r_xprt->rx_ep, &r_xprt->rx_ia);
209 if (rc)
210 xprt_wake_pending_tasks(xprt, rc);
211
f58851e6
TT
212 dprintk("RPC: %s: exit\n", __func__);
213 xprt_clear_connecting(xprt);
5cf02d09 214 current->flags &= ~PF_FSTRANS;
f58851e6
TT
215}
216
217/*
218 * xprt_rdma_destroy
219 *
220 * Destroy the xprt.
221 * Free all memory associated with the object, including its own.
222 * NOTE: none of the *destroy methods free memory for their top-level
223 * objects, even though they may have allocated it (they do free
224 * private memory). It's up to the caller to handle it. In this
225 * case (RDMA transport), all structure memory is inlined with the
226 * struct rpcrdma_xprt.
227 */
228static void
229xprt_rdma_destroy(struct rpc_xprt *xprt)
230{
231 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
f58851e6
TT
232
233 dprintk("RPC: %s: called\n", __func__);
234
a25e758c 235 cancel_delayed_work_sync(&r_xprt->rdma_connect);
f58851e6
TT
236
237 xprt_clear_connected(xprt);
238
239 rpcrdma_buffer_destroy(&r_xprt->rx_buf);
7f1d5419 240 rpcrdma_ep_destroy(&r_xprt->rx_ep, &r_xprt->rx_ia);
f58851e6
TT
241 rpcrdma_ia_close(&r_xprt->rx_ia);
242
243 xprt_rdma_free_addresses(xprt);
244
e204e621 245 xprt_free(xprt);
f58851e6
TT
246
247 dprintk("RPC: %s: returning\n", __func__);
248
249 module_put(THIS_MODULE);
250}
251
2881ae74
TM
252static const struct rpc_timeout xprt_rdma_default_timeout = {
253 .to_initval = 60 * HZ,
254 .to_maxval = 60 * HZ,
255};
256
f58851e6
TT
257/**
258 * xprt_setup_rdma - Set up transport to use RDMA
259 *
260 * @args: rpc transport arguments
261 */
262static struct rpc_xprt *
263xprt_setup_rdma(struct xprt_create *args)
264{
265 struct rpcrdma_create_data_internal cdata;
266 struct rpc_xprt *xprt;
267 struct rpcrdma_xprt *new_xprt;
268 struct rpcrdma_ep *new_ep;
269 struct sockaddr_in *sin;
270 int rc;
271
272 if (args->addrlen > sizeof(xprt->addr)) {
273 dprintk("RPC: %s: address too large\n", __func__);
274 return ERR_PTR(-EBADF);
275 }
276
37aa2133 277 xprt = xprt_alloc(args->net, sizeof(struct rpcrdma_xprt),
d9ba131d 278 xprt_rdma_slot_table_entries,
bd1722d4 279 xprt_rdma_slot_table_entries);
f58851e6
TT
280 if (xprt == NULL) {
281 dprintk("RPC: %s: couldn't allocate rpcrdma_xprt\n",
282 __func__);
283 return ERR_PTR(-ENOMEM);
284 }
285
f58851e6 286 /* 60 second timeout, no retries */
ba7392bb 287 xprt->timeout = &xprt_rdma_default_timeout;
f58851e6 288 xprt->bind_timeout = (60U * HZ);
f58851e6
TT
289 xprt->reestablish_timeout = (5U * HZ);
290 xprt->idle_timeout = (5U * 60 * HZ);
291
292 xprt->resvport = 0; /* privileged port not needed */
293 xprt->tsh_size = 0; /* RPC-RDMA handles framing */
294 xprt->max_payload = RPCRDMA_MAX_DATA_SEGS * PAGE_SIZE;
295 xprt->ops = &xprt_rdma_procs;
296
297 /*
298 * Set up RDMA-specific connect data.
299 */
300
301 /* Put server RDMA address in local cdata */
302 memcpy(&cdata.addr, args->dstaddr, args->addrlen);
303
304 /* Ensure xprt->addr holds valid server TCP (not RDMA)
305 * address, for any side protocols which peek at it */
306 xprt->prot = IPPROTO_TCP;
307 xprt->addrlen = args->addrlen;
308 memcpy(&xprt->addr, &cdata.addr, xprt->addrlen);
309
310 sin = (struct sockaddr_in *)&cdata.addr;
311 if (ntohs(sin->sin_port) != 0)
312 xprt_set_bound(xprt);
313
21454aaa
HH
314 dprintk("RPC: %s: %pI4:%u\n",
315 __func__, &sin->sin_addr.s_addr, ntohs(sin->sin_port));
f58851e6
TT
316
317 /* Set max requests */
318 cdata.max_requests = xprt->max_reqs;
319
320 /* Set some length limits */
321 cdata.rsize = RPCRDMA_MAX_SEGS * PAGE_SIZE; /* RDMA write max */
322 cdata.wsize = RPCRDMA_MAX_SEGS * PAGE_SIZE; /* RDMA read max */
323
324 cdata.inline_wsize = xprt_rdma_max_inline_write;
325 if (cdata.inline_wsize > cdata.wsize)
326 cdata.inline_wsize = cdata.wsize;
327
328 cdata.inline_rsize = xprt_rdma_max_inline_read;
329 if (cdata.inline_rsize > cdata.rsize)
330 cdata.inline_rsize = cdata.rsize;
331
332 cdata.padding = xprt_rdma_inline_write_padding;
333
334 /*
335 * Create new transport instance, which includes initialized
336 * o ia
337 * o endpoint
338 * o buffers
339 */
340
341 new_xprt = rpcx_to_rdmax(xprt);
342
343 rc = rpcrdma_ia_open(new_xprt, (struct sockaddr *) &cdata.addr,
344 xprt_rdma_memreg_strategy);
345 if (rc)
346 goto out1;
347
348 /*
349 * initialize and create ep
350 */
351 new_xprt->rx_data = cdata;
352 new_ep = &new_xprt->rx_ep;
353 new_ep->rep_remote_addr = cdata.addr;
354
355 rc = rpcrdma_ep_create(&new_xprt->rx_ep,
356 &new_xprt->rx_ia, &new_xprt->rx_data);
357 if (rc)
358 goto out2;
359
360 /*
361 * Allocate pre-registered send and receive buffers for headers and
362 * any inline data. Also specify any padding which will be provided
363 * from a preregistered zero buffer.
364 */
365 rc = rpcrdma_buffer_create(&new_xprt->rx_buf, new_ep, &new_xprt->rx_ia,
366 &new_xprt->rx_data);
367 if (rc)
368 goto out3;
369
370 /*
371 * Register a callback for connection events. This is necessary because
372 * connection loss notification is async. We also catch connection loss
373 * when reaping receives.
374 */
375 INIT_DELAYED_WORK(&new_xprt->rdma_connect, xprt_rdma_connect_worker);
376 new_ep->rep_func = rpcrdma_conn_func;
377 new_ep->rep_xprt = xprt;
378
379 xprt_rdma_format_addresses(xprt);
380
381 if (!try_module_get(THIS_MODULE))
382 goto out4;
383
384 return xprt;
385
386out4:
387 xprt_rdma_free_addresses(xprt);
388 rc = -EINVAL;
389out3:
7f1d5419 390 rpcrdma_ep_destroy(new_ep, &new_xprt->rx_ia);
f58851e6
TT
391out2:
392 rpcrdma_ia_close(&new_xprt->rx_ia);
393out1:
e204e621 394 xprt_free(xprt);
f58851e6
TT
395 return ERR_PTR(rc);
396}
397
398/*
399 * Close a connection, during shutdown or timeout/reconnect
400 */
401static void
402xprt_rdma_close(struct rpc_xprt *xprt)
403{
404 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
405
406 dprintk("RPC: %s: closing\n", __func__);
08ca0dce
TT
407 if (r_xprt->rx_ep.rep_connected > 0)
408 xprt->reestablish_timeout = 0;
62da3b24 409 xprt_disconnect_done(xprt);
f58851e6
TT
410 (void) rpcrdma_ep_disconnect(&r_xprt->rx_ep, &r_xprt->rx_ia);
411}
412
413static void
414xprt_rdma_set_port(struct rpc_xprt *xprt, u16 port)
415{
416 struct sockaddr_in *sap;
417
418 sap = (struct sockaddr_in *)&xprt->addr;
419 sap->sin_port = htons(port);
420 sap = (struct sockaddr_in *)&rpcx_to_rdmad(xprt).addr;
421 sap->sin_port = htons(port);
422 dprintk("RPC: %s: %u\n", __func__, port);
423}
424
425static void
1b092092 426xprt_rdma_connect(struct rpc_xprt *xprt, struct rpc_task *task)
f58851e6 427{
f58851e6
TT
428 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
429
0b9e7943
TM
430 if (r_xprt->rx_ep.rep_connected != 0) {
431 /* Reconnect */
432 schedule_delayed_work(&r_xprt->rdma_connect,
433 xprt->reestablish_timeout);
434 xprt->reestablish_timeout <<= 1;
435 if (xprt->reestablish_timeout > (30 * HZ))
436 xprt->reestablish_timeout = (30 * HZ);
437 else if (xprt->reestablish_timeout < (5 * HZ))
438 xprt->reestablish_timeout = (5 * HZ);
439 } else {
440 schedule_delayed_work(&r_xprt->rdma_connect, 0);
441 if (!RPC_IS_ASYNC(task))
a25e758c 442 flush_delayed_work(&r_xprt->rdma_connect);
f58851e6
TT
443 }
444}
445
446static int
43cedbf0 447xprt_rdma_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
f58851e6 448{
f58851e6
TT
449 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
450 int credits = atomic_read(&r_xprt->rx_buf.rb_credits);
451
452 /* == RPC_CWNDSCALE @ init, but *after* setup */
453 if (r_xprt->rx_buf.rb_cwndscale == 0UL) {
454 r_xprt->rx_buf.rb_cwndscale = xprt->cwnd;
455 dprintk("RPC: %s: cwndscale %lu\n", __func__,
456 r_xprt->rx_buf.rb_cwndscale);
457 BUG_ON(r_xprt->rx_buf.rb_cwndscale <= 0);
458 }
459 xprt->cwnd = credits * r_xprt->rx_buf.rb_cwndscale;
43cedbf0 460 return xprt_reserve_xprt_cong(xprt, task);
f58851e6
TT
461}
462
463/*
464 * The RDMA allocate/free functions need the task structure as a place
465 * to hide the struct rpcrdma_req, which is necessary for the actual send/recv
466 * sequence. For this reason, the recv buffers are attached to send
467 * buffers for portions of the RPC. Note that the RPC layer allocates
468 * both send and receive buffers in the same call. We may register
469 * the receive buffer portion when using reply chunks.
470 */
471static void *
472xprt_rdma_allocate(struct rpc_task *task, size_t size)
473{
a4f0835c 474 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
f58851e6
TT
475 struct rpcrdma_req *req, *nreq;
476
477 req = rpcrdma_buffer_get(&rpcx_to_rdmax(xprt)->rx_buf);
478 BUG_ON(NULL == req);
479
480 if (size > req->rl_size) {
481 dprintk("RPC: %s: size %zd too large for buffer[%zd]: "
482 "prog %d vers %d proc %d\n",
483 __func__, size, req->rl_size,
484 task->tk_client->cl_prog, task->tk_client->cl_vers,
485 task->tk_msg.rpc_proc->p_proc);
486 /*
487 * Outgoing length shortage. Our inline write max must have
488 * been configured to perform direct i/o.
489 *
490 * This is therefore a large metadata operation, and the
491 * allocate call was made on the maximum possible message,
492 * e.g. containing long filename(s) or symlink data. In
493 * fact, while these metadata operations *might* carry
494 * large outgoing payloads, they rarely *do*. However, we
495 * have to commit to the request here, so reallocate and
496 * register it now. The data path will never require this
497 * reallocation.
498 *
499 * If the allocation or registration fails, the RPC framework
500 * will (doggedly) retry.
501 */
f58851e6
TT
502 if (task->tk_flags & RPC_TASK_SWAPPER)
503 nreq = kmalloc(sizeof *req + size, GFP_ATOMIC);
504 else
505 nreq = kmalloc(sizeof *req + size, GFP_NOFS);
506 if (nreq == NULL)
507 goto outfail;
508
509 if (rpcrdma_register_internal(&rpcx_to_rdmax(xprt)->rx_ia,
510 nreq->rl_base, size + sizeof(struct rpcrdma_req)
511 - offsetof(struct rpcrdma_req, rl_base),
512 &nreq->rl_handle, &nreq->rl_iov)) {
513 kfree(nreq);
514 goto outfail;
515 }
516 rpcx_to_rdmax(xprt)->rx_stats.hardway_register_count += size;
517 nreq->rl_size = size;
518 nreq->rl_niovs = 0;
519 nreq->rl_nchunks = 0;
520 nreq->rl_buffer = (struct rpcrdma_buffer *)req;
521 nreq->rl_reply = req->rl_reply;
522 memcpy(nreq->rl_segments,
523 req->rl_segments, sizeof nreq->rl_segments);
524 /* flag the swap with an unused field */
525 nreq->rl_iov.length = 0;
526 req->rl_reply = NULL;
527 req = nreq;
528 }
529 dprintk("RPC: %s: size %zd, request 0x%p\n", __func__, size, req);
575448bd 530 req->rl_connect_cookie = 0; /* our reserved value */
f58851e6
TT
531 return req->rl_xdr_buf;
532
533outfail:
534 rpcrdma_buffer_put(req);
535 rpcx_to_rdmax(xprt)->rx_stats.failed_marshal_count++;
536 return NULL;
537}
538
539/*
540 * This function returns all RDMA resources to the pool.
541 */
542static void
543xprt_rdma_free(void *buffer)
544{
545 struct rpcrdma_req *req;
546 struct rpcrdma_xprt *r_xprt;
547 struct rpcrdma_rep *rep;
548 int i;
549
550 if (buffer == NULL)
551 return;
552
553 req = container_of(buffer, struct rpcrdma_req, rl_xdr_buf[0]);
ee1a2c56
TT
554 if (req->rl_iov.length == 0) { /* see allocate above */
555 r_xprt = container_of(((struct rpcrdma_req *) req->rl_buffer)->rl_buffer,
556 struct rpcrdma_xprt, rx_buf);
557 } else
558 r_xprt = container_of(req->rl_buffer, struct rpcrdma_xprt, rx_buf);
f58851e6
TT
559 rep = req->rl_reply;
560
561 dprintk("RPC: %s: called on 0x%p%s\n",
562 __func__, rep, (rep && rep->rr_func) ? " (with waiter)" : "");
563
564 /*
b45ccfd2 565 * Finish the deregistration. The process is considered
f58851e6
TT
566 * complete when the rr_func vector becomes NULL - this
567 * was put in place during rpcrdma_reply_handler() - the wait
568 * call below will not block if the dereg is "done". If
569 * interrupted, our framework will clean up.
570 */
571 for (i = 0; req->rl_nchunks;) {
572 --req->rl_nchunks;
573 i += rpcrdma_deregister_external(
13c9ff8f 574 &req->rl_segments[i], r_xprt);
f58851e6
TT
575 }
576
f58851e6
TT
577 if (req->rl_iov.length == 0) { /* see allocate above */
578 struct rpcrdma_req *oreq = (struct rpcrdma_req *)req->rl_buffer;
579 oreq->rl_reply = req->rl_reply;
580 (void) rpcrdma_deregister_internal(&r_xprt->rx_ia,
581 req->rl_handle,
582 &req->rl_iov);
583 kfree(req);
584 req = oreq;
585 }
586
587 /* Put back request+reply buffers */
588 rpcrdma_buffer_put(req);
589}
590
591/*
592 * send_request invokes the meat of RPC RDMA. It must do the following:
593 * 1. Marshal the RPC request into an RPC RDMA request, which means
594 * putting a header in front of data, and creating IOVs for RDMA
595 * from those in the request.
596 * 2. In marshaling, detect opportunities for RDMA, and use them.
597 * 3. Post a recv message to set up asynch completion, then send
598 * the request (rpcrdma_ep_post).
599 * 4. No partial sends are possible in the RPC-RDMA protocol (as in UDP).
600 */
601
602static int
603xprt_rdma_send_request(struct rpc_task *task)
604{
605 struct rpc_rqst *rqst = task->tk_rqstp;
a4f0835c 606 struct rpc_xprt *xprt = rqst->rq_xprt;
f58851e6
TT
607 struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
608 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
609
610 /* marshal the send itself */
611 if (req->rl_niovs == 0 && rpcrdma_marshal_req(rqst) != 0) {
612 r_xprt->rx_stats.failed_marshal_count++;
613 dprintk("RPC: %s: rpcrdma_marshal_req failed\n",
614 __func__);
615 return -EIO;
616 }
617
618 if (req->rl_reply == NULL) /* e.g. reconnection */
619 rpcrdma_recv_buffer_get(req);
620
621 if (req->rl_reply) {
622 req->rl_reply->rr_func = rpcrdma_reply_handler;
623 /* this need only be done once, but... */
624 req->rl_reply->rr_xprt = xprt;
625 }
626
575448bd
TT
627 /* Must suppress retransmit to maintain credits */
628 if (req->rl_connect_cookie == xprt->connect_cookie)
629 goto drop_connection;
630 req->rl_connect_cookie = xprt->connect_cookie;
631
632 if (rpcrdma_ep_post(&r_xprt->rx_ia, &r_xprt->rx_ep, req))
633 goto drop_connection;
f58851e6 634
d60dbb20 635 rqst->rq_xmit_bytes_sent += rqst->rq_snd_buf.len;
f58851e6
TT
636 rqst->rq_bytes_sent = 0;
637 return 0;
575448bd
TT
638
639drop_connection:
640 xprt_disconnect_done(xprt);
641 return -ENOTCONN; /* implies disconnect */
f58851e6
TT
642}
643
644static void xprt_rdma_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
645{
646 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
647 long idle_time = 0;
648
649 if (xprt_connected(xprt))
650 idle_time = (long)(jiffies - xprt->last_used) / HZ;
651
652 seq_printf(seq,
653 "\txprt:\trdma %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu "
654 "%lu %lu %lu %Lu %Lu %Lu %Lu %lu %lu %lu\n",
655
656 0, /* need a local port? */
657 xprt->stat.bind_count,
658 xprt->stat.connect_count,
659 xprt->stat.connect_time,
660 idle_time,
661 xprt->stat.sends,
662 xprt->stat.recvs,
663 xprt->stat.bad_xids,
664 xprt->stat.req_u,
665 xprt->stat.bklog_u,
666
667 r_xprt->rx_stats.read_chunk_count,
668 r_xprt->rx_stats.write_chunk_count,
669 r_xprt->rx_stats.reply_chunk_count,
670 r_xprt->rx_stats.total_rdma_request,
671 r_xprt->rx_stats.total_rdma_reply,
672 r_xprt->rx_stats.pullup_copy_count,
673 r_xprt->rx_stats.fixup_copy_count,
674 r_xprt->rx_stats.hardway_register_count,
675 r_xprt->rx_stats.failed_marshal_count,
676 r_xprt->rx_stats.bad_reply_count);
677}
678
679/*
680 * Plumbing for rpc transport switch and kernel module
681 */
682
683static struct rpc_xprt_ops xprt_rdma_procs = {
684 .reserve_xprt = xprt_rdma_reserve_xprt,
685 .release_xprt = xprt_release_xprt_cong, /* sunrpc/xprt.c */
f39c1bfb 686 .alloc_slot = xprt_alloc_slot,
f58851e6
TT
687 .release_request = xprt_release_rqst_cong, /* ditto */
688 .set_retrans_timeout = xprt_set_retrans_timeout_def, /* ditto */
689 .rpcbind = rpcb_getport_async, /* sunrpc/rpcb_clnt.c */
690 .set_port = xprt_rdma_set_port,
691 .connect = xprt_rdma_connect,
692 .buf_alloc = xprt_rdma_allocate,
693 .buf_free = xprt_rdma_free,
694 .send_request = xprt_rdma_send_request,
695 .close = xprt_rdma_close,
696 .destroy = xprt_rdma_destroy,
697 .print_stats = xprt_rdma_print_stats
698};
699
700static struct xprt_class xprt_rdma = {
701 .list = LIST_HEAD_INIT(xprt_rdma.list),
702 .name = "rdma",
703 .owner = THIS_MODULE,
704 .ident = XPRT_TRANSPORT_RDMA,
705 .setup = xprt_setup_rdma,
706};
707
708static void __exit xprt_rdma_cleanup(void)
709{
710 int rc;
711
3a0799a9 712 dprintk("RPCRDMA Module Removed, deregister RPC RDMA transport\n");
f58851e6
TT
713#ifdef RPC_DEBUG
714 if (sunrpc_table_header) {
715 unregister_sysctl_table(sunrpc_table_header);
716 sunrpc_table_header = NULL;
717 }
718#endif
719 rc = xprt_unregister_transport(&xprt_rdma);
720 if (rc)
721 dprintk("RPC: %s: xprt_unregister returned %i\n",
722 __func__, rc);
723}
724
725static int __init xprt_rdma_init(void)
726{
727 int rc;
728
729 rc = xprt_register_transport(&xprt_rdma);
730
731 if (rc)
732 return rc;
733
3a0799a9 734 dprintk("RPCRDMA Module Init, register RPC RDMA transport\n");
f58851e6 735
3a0799a9
CL
736 dprintk("Defaults:\n");
737 dprintk("\tSlots %d\n"
f58851e6
TT
738 "\tMaxInlineRead %d\n\tMaxInlineWrite %d\n",
739 xprt_rdma_slot_table_entries,
740 xprt_rdma_max_inline_read, xprt_rdma_max_inline_write);
3a0799a9 741 dprintk("\tPadding %d\n\tMemreg %d\n",
f58851e6
TT
742 xprt_rdma_inline_write_padding, xprt_rdma_memreg_strategy);
743
744#ifdef RPC_DEBUG
745 if (!sunrpc_table_header)
746 sunrpc_table_header = register_sysctl_table(sunrpc_table);
747#endif
748 return 0;
749}
750
751module_init(xprt_rdma_init);
752module_exit(xprt_rdma_cleanup);