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