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