OSDN Git Service

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