svcrdma: RDMA support not yet compatible with RPC6
[pandora-kernel.git] / net / sunrpc / xprtrdma / svc_rdma_transport.c
1 /*
2  * Copyright (c) 2005-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  * Author: Tom Tucker <tom@opengridcomputing.com>
40  */
41
42 #include <linux/sunrpc/svc_xprt.h>
43 #include <linux/sunrpc/debug.h>
44 #include <linux/sunrpc/rpc_rdma.h>
45 #include <linux/sched.h>
46 #include <linux/spinlock.h>
47 #include <rdma/ib_verbs.h>
48 #include <rdma/rdma_cm.h>
49 #include <linux/sunrpc/svc_rdma.h>
50
51 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
52
53 static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
54                                         struct sockaddr *sa, int salen,
55                                         int flags);
56 static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt);
57 static void svc_rdma_release_rqst(struct svc_rqst *);
58 static void dto_tasklet_func(unsigned long data);
59 static void svc_rdma_detach(struct svc_xprt *xprt);
60 static void svc_rdma_free(struct svc_xprt *xprt);
61 static int svc_rdma_has_wspace(struct svc_xprt *xprt);
62 static void rq_cq_reap(struct svcxprt_rdma *xprt);
63 static void sq_cq_reap(struct svcxprt_rdma *xprt);
64
65 static DECLARE_TASKLET(dto_tasklet, dto_tasklet_func, 0UL);
66 static DEFINE_SPINLOCK(dto_lock);
67 static LIST_HEAD(dto_xprt_q);
68
69 static struct svc_xprt_ops svc_rdma_ops = {
70         .xpo_create = svc_rdma_create,
71         .xpo_recvfrom = svc_rdma_recvfrom,
72         .xpo_sendto = svc_rdma_sendto,
73         .xpo_release_rqst = svc_rdma_release_rqst,
74         .xpo_detach = svc_rdma_detach,
75         .xpo_free = svc_rdma_free,
76         .xpo_prep_reply_hdr = svc_rdma_prep_reply_hdr,
77         .xpo_has_wspace = svc_rdma_has_wspace,
78         .xpo_accept = svc_rdma_accept,
79 };
80
81 struct svc_xprt_class svc_rdma_class = {
82         .xcl_name = "rdma",
83         .xcl_owner = THIS_MODULE,
84         .xcl_ops = &svc_rdma_ops,
85         .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
86 };
87
88 /* WR context cache. Created in svc_rdma.c  */
89 extern struct kmem_cache *svc_rdma_ctxt_cachep;
90
91 struct svc_rdma_op_ctxt *svc_rdma_get_context(struct svcxprt_rdma *xprt)
92 {
93         struct svc_rdma_op_ctxt *ctxt;
94
95         while (1) {
96                 ctxt = kmem_cache_alloc(svc_rdma_ctxt_cachep, GFP_KERNEL);
97                 if (ctxt)
98                         break;
99                 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
100         }
101         ctxt->xprt = xprt;
102         INIT_LIST_HEAD(&ctxt->dto_q);
103         ctxt->count = 0;
104         ctxt->frmr = NULL;
105         atomic_inc(&xprt->sc_ctxt_used);
106         return ctxt;
107 }
108
109 void svc_rdma_unmap_dma(struct svc_rdma_op_ctxt *ctxt)
110 {
111         struct svcxprt_rdma *xprt = ctxt->xprt;
112         int i;
113         for (i = 0; i < ctxt->count && ctxt->sge[i].length; i++) {
114                 /*
115                  * Unmap the DMA addr in the SGE if the lkey matches
116                  * the sc_dma_lkey, otherwise, ignore it since it is
117                  * an FRMR lkey and will be unmapped later when the
118                  * last WR that uses it completes.
119                  */
120                 if (ctxt->sge[i].lkey == xprt->sc_dma_lkey) {
121                         atomic_dec(&xprt->sc_dma_used);
122                         ib_dma_unmap_single(xprt->sc_cm_id->device,
123                                             ctxt->sge[i].addr,
124                                             ctxt->sge[i].length,
125                                             ctxt->direction);
126                 }
127         }
128 }
129
130 void svc_rdma_put_context(struct svc_rdma_op_ctxt *ctxt, int free_pages)
131 {
132         struct svcxprt_rdma *xprt;
133         int i;
134
135         BUG_ON(!ctxt);
136         xprt = ctxt->xprt;
137         if (free_pages)
138                 for (i = 0; i < ctxt->count; i++)
139                         put_page(ctxt->pages[i]);
140
141         kmem_cache_free(svc_rdma_ctxt_cachep, ctxt);
142         atomic_dec(&xprt->sc_ctxt_used);
143 }
144
145 /* Temporary NFS request map cache. Created in svc_rdma.c  */
146 extern struct kmem_cache *svc_rdma_map_cachep;
147
148 /*
149  * Temporary NFS req mappings are shared across all transport
150  * instances. These are short lived and should be bounded by the number
151  * of concurrent server threads * depth of the SQ.
152  */
153 struct svc_rdma_req_map *svc_rdma_get_req_map(void)
154 {
155         struct svc_rdma_req_map *map;
156         while (1) {
157                 map = kmem_cache_alloc(svc_rdma_map_cachep, GFP_KERNEL);
158                 if (map)
159                         break;
160                 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
161         }
162         map->count = 0;
163         map->frmr = NULL;
164         return map;
165 }
166
167 void svc_rdma_put_req_map(struct svc_rdma_req_map *map)
168 {
169         kmem_cache_free(svc_rdma_map_cachep, map);
170 }
171
172 /* ib_cq event handler */
173 static void cq_event_handler(struct ib_event *event, void *context)
174 {
175         struct svc_xprt *xprt = context;
176         dprintk("svcrdma: received CQ event id=%d, context=%p\n",
177                 event->event, context);
178         set_bit(XPT_CLOSE, &xprt->xpt_flags);
179 }
180
181 /* QP event handler */
182 static void qp_event_handler(struct ib_event *event, void *context)
183 {
184         struct svc_xprt *xprt = context;
185
186         switch (event->event) {
187         /* These are considered benign events */
188         case IB_EVENT_PATH_MIG:
189         case IB_EVENT_COMM_EST:
190         case IB_EVENT_SQ_DRAINED:
191         case IB_EVENT_QP_LAST_WQE_REACHED:
192                 dprintk("svcrdma: QP event %d received for QP=%p\n",
193                         event->event, event->element.qp);
194                 break;
195         /* These are considered fatal events */
196         case IB_EVENT_PATH_MIG_ERR:
197         case IB_EVENT_QP_FATAL:
198         case IB_EVENT_QP_REQ_ERR:
199         case IB_EVENT_QP_ACCESS_ERR:
200         case IB_EVENT_DEVICE_FATAL:
201         default:
202                 dprintk("svcrdma: QP ERROR event %d received for QP=%p, "
203                         "closing transport\n",
204                         event->event, event->element.qp);
205                 set_bit(XPT_CLOSE, &xprt->xpt_flags);
206                 break;
207         }
208 }
209
210 /*
211  * Data Transfer Operation Tasklet
212  *
213  * Walks a list of transports with I/O pending, removing entries as
214  * they are added to the server's I/O pending list. Two bits indicate
215  * if SQ, RQ, or both have I/O pending. The dto_lock is an irqsave
216  * spinlock that serializes access to the transport list with the RQ
217  * and SQ interrupt handlers.
218  */
219 static void dto_tasklet_func(unsigned long data)
220 {
221         struct svcxprt_rdma *xprt;
222         unsigned long flags;
223
224         spin_lock_irqsave(&dto_lock, flags);
225         while (!list_empty(&dto_xprt_q)) {
226                 xprt = list_entry(dto_xprt_q.next,
227                                   struct svcxprt_rdma, sc_dto_q);
228                 list_del_init(&xprt->sc_dto_q);
229                 spin_unlock_irqrestore(&dto_lock, flags);
230
231                 rq_cq_reap(xprt);
232                 sq_cq_reap(xprt);
233
234                 svc_xprt_put(&xprt->sc_xprt);
235                 spin_lock_irqsave(&dto_lock, flags);
236         }
237         spin_unlock_irqrestore(&dto_lock, flags);
238 }
239
240 /*
241  * Receive Queue Completion Handler
242  *
243  * Since an RQ completion handler is called on interrupt context, we
244  * need to defer the handling of the I/O to a tasklet
245  */
246 static void rq_comp_handler(struct ib_cq *cq, void *cq_context)
247 {
248         struct svcxprt_rdma *xprt = cq_context;
249         unsigned long flags;
250
251         /* Guard against unconditional flush call for destroyed QP */
252         if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
253                 return;
254
255         /*
256          * Set the bit regardless of whether or not it's on the list
257          * because it may be on the list already due to an SQ
258          * completion.
259          */
260         set_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags);
261
262         /*
263          * If this transport is not already on the DTO transport queue,
264          * add it
265          */
266         spin_lock_irqsave(&dto_lock, flags);
267         if (list_empty(&xprt->sc_dto_q)) {
268                 svc_xprt_get(&xprt->sc_xprt);
269                 list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
270         }
271         spin_unlock_irqrestore(&dto_lock, flags);
272
273         /* Tasklet does all the work to avoid irqsave locks. */
274         tasklet_schedule(&dto_tasklet);
275 }
276
277 /*
278  * rq_cq_reap - Process the RQ CQ.
279  *
280  * Take all completing WC off the CQE and enqueue the associated DTO
281  * context on the dto_q for the transport.
282  *
283  * Note that caller must hold a transport reference.
284  */
285 static void rq_cq_reap(struct svcxprt_rdma *xprt)
286 {
287         int ret;
288         struct ib_wc wc;
289         struct svc_rdma_op_ctxt *ctxt = NULL;
290
291         if (!test_and_clear_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags))
292                 return;
293
294         ib_req_notify_cq(xprt->sc_rq_cq, IB_CQ_NEXT_COMP);
295         atomic_inc(&rdma_stat_rq_poll);
296
297         while ((ret = ib_poll_cq(xprt->sc_rq_cq, 1, &wc)) > 0) {
298                 ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
299                 ctxt->wc_status = wc.status;
300                 ctxt->byte_len = wc.byte_len;
301                 svc_rdma_unmap_dma(ctxt);
302                 if (wc.status != IB_WC_SUCCESS) {
303                         /* Close the transport */
304                         dprintk("svcrdma: transport closing putting ctxt %p\n", ctxt);
305                         set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
306                         svc_rdma_put_context(ctxt, 1);
307                         svc_xprt_put(&xprt->sc_xprt);
308                         continue;
309                 }
310                 spin_lock_bh(&xprt->sc_rq_dto_lock);
311                 list_add_tail(&ctxt->dto_q, &xprt->sc_rq_dto_q);
312                 spin_unlock_bh(&xprt->sc_rq_dto_lock);
313                 svc_xprt_put(&xprt->sc_xprt);
314         }
315
316         if (ctxt)
317                 atomic_inc(&rdma_stat_rq_prod);
318
319         set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
320         /*
321          * If data arrived before established event,
322          * don't enqueue. This defers RPC I/O until the
323          * RDMA connection is complete.
324          */
325         if (!test_bit(RDMAXPRT_CONN_PENDING, &xprt->sc_flags))
326                 svc_xprt_enqueue(&xprt->sc_xprt);
327 }
328
329 /*
330  * Processs a completion context
331  */
332 static void process_context(struct svcxprt_rdma *xprt,
333                             struct svc_rdma_op_ctxt *ctxt)
334 {
335         svc_rdma_unmap_dma(ctxt);
336
337         switch (ctxt->wr_op) {
338         case IB_WR_SEND:
339                 if (test_bit(RDMACTXT_F_FAST_UNREG, &ctxt->flags))
340                         svc_rdma_put_frmr(xprt, ctxt->frmr);
341                 svc_rdma_put_context(ctxt, 1);
342                 break;
343
344         case IB_WR_RDMA_WRITE:
345                 svc_rdma_put_context(ctxt, 0);
346                 break;
347
348         case IB_WR_RDMA_READ:
349         case IB_WR_RDMA_READ_WITH_INV:
350                 if (test_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags)) {
351                         struct svc_rdma_op_ctxt *read_hdr = ctxt->read_hdr;
352                         BUG_ON(!read_hdr);
353                         if (test_bit(RDMACTXT_F_FAST_UNREG, &ctxt->flags))
354                                 svc_rdma_put_frmr(xprt, ctxt->frmr);
355                         spin_lock_bh(&xprt->sc_rq_dto_lock);
356                         set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
357                         list_add_tail(&read_hdr->dto_q,
358                                       &xprt->sc_read_complete_q);
359                         spin_unlock_bh(&xprt->sc_rq_dto_lock);
360                         svc_xprt_enqueue(&xprt->sc_xprt);
361                 }
362                 svc_rdma_put_context(ctxt, 0);
363                 break;
364
365         default:
366                 printk(KERN_ERR "svcrdma: unexpected completion type, "
367                        "opcode=%d\n",
368                        ctxt->wr_op);
369                 break;
370         }
371 }
372
373 /*
374  * Send Queue Completion Handler - potentially called on interrupt context.
375  *
376  * Note that caller must hold a transport reference.
377  */
378 static void sq_cq_reap(struct svcxprt_rdma *xprt)
379 {
380         struct svc_rdma_op_ctxt *ctxt = NULL;
381         struct ib_wc wc;
382         struct ib_cq *cq = xprt->sc_sq_cq;
383         int ret;
384
385         if (!test_and_clear_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags))
386                 return;
387
388         ib_req_notify_cq(xprt->sc_sq_cq, IB_CQ_NEXT_COMP);
389         atomic_inc(&rdma_stat_sq_poll);
390         while ((ret = ib_poll_cq(cq, 1, &wc)) > 0) {
391                 if (wc.status != IB_WC_SUCCESS)
392                         /* Close the transport */
393                         set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
394
395                 /* Decrement used SQ WR count */
396                 atomic_dec(&xprt->sc_sq_count);
397                 wake_up(&xprt->sc_send_wait);
398
399                 ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
400                 if (ctxt)
401                         process_context(xprt, ctxt);
402
403                 svc_xprt_put(&xprt->sc_xprt);
404         }
405
406         if (ctxt)
407                 atomic_inc(&rdma_stat_sq_prod);
408 }
409
410 static void sq_comp_handler(struct ib_cq *cq, void *cq_context)
411 {
412         struct svcxprt_rdma *xprt = cq_context;
413         unsigned long flags;
414
415         /* Guard against unconditional flush call for destroyed QP */
416         if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
417                 return;
418
419         /*
420          * Set the bit regardless of whether or not it's on the list
421          * because it may be on the list already due to an RQ
422          * completion.
423          */
424         set_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags);
425
426         /*
427          * If this transport is not already on the DTO transport queue,
428          * add it
429          */
430         spin_lock_irqsave(&dto_lock, flags);
431         if (list_empty(&xprt->sc_dto_q)) {
432                 svc_xprt_get(&xprt->sc_xprt);
433                 list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
434         }
435         spin_unlock_irqrestore(&dto_lock, flags);
436
437         /* Tasklet does all the work to avoid irqsave locks. */
438         tasklet_schedule(&dto_tasklet);
439 }
440
441 static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *serv,
442                                              int listener)
443 {
444         struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
445
446         if (!cma_xprt)
447                 return NULL;
448         svc_xprt_init(&svc_rdma_class, &cma_xprt->sc_xprt, serv);
449         INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
450         INIT_LIST_HEAD(&cma_xprt->sc_dto_q);
451         INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
452         INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
453         INIT_LIST_HEAD(&cma_xprt->sc_frmr_q);
454         init_waitqueue_head(&cma_xprt->sc_send_wait);
455
456         spin_lock_init(&cma_xprt->sc_lock);
457         spin_lock_init(&cma_xprt->sc_rq_dto_lock);
458         spin_lock_init(&cma_xprt->sc_frmr_q_lock);
459
460         cma_xprt->sc_ord = svcrdma_ord;
461
462         cma_xprt->sc_max_req_size = svcrdma_max_req_size;
463         cma_xprt->sc_max_requests = svcrdma_max_requests;
464         cma_xprt->sc_sq_depth = svcrdma_max_requests * RPCRDMA_SQ_DEPTH_MULT;
465         atomic_set(&cma_xprt->sc_sq_count, 0);
466         atomic_set(&cma_xprt->sc_ctxt_used, 0);
467
468         if (listener)
469                 set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
470
471         return cma_xprt;
472 }
473
474 struct page *svc_rdma_get_page(void)
475 {
476         struct page *page;
477
478         while ((page = alloc_page(GFP_KERNEL)) == NULL) {
479                 /* If we can't get memory, wait a bit and try again */
480                 printk(KERN_INFO "svcrdma: out of memory...retrying in 1000 "
481                        "jiffies.\n");
482                 schedule_timeout_uninterruptible(msecs_to_jiffies(1000));
483         }
484         return page;
485 }
486
487 int svc_rdma_post_recv(struct svcxprt_rdma *xprt)
488 {
489         struct ib_recv_wr recv_wr, *bad_recv_wr;
490         struct svc_rdma_op_ctxt *ctxt;
491         struct page *page;
492         dma_addr_t pa;
493         int sge_no;
494         int buflen;
495         int ret;
496
497         ctxt = svc_rdma_get_context(xprt);
498         buflen = 0;
499         ctxt->direction = DMA_FROM_DEVICE;
500         for (sge_no = 0; buflen < xprt->sc_max_req_size; sge_no++) {
501                 BUG_ON(sge_no >= xprt->sc_max_sge);
502                 page = svc_rdma_get_page();
503                 ctxt->pages[sge_no] = page;
504                 pa = ib_dma_map_single(xprt->sc_cm_id->device,
505                                      page_address(page), PAGE_SIZE,
506                                      DMA_FROM_DEVICE);
507                 if (ib_dma_mapping_error(xprt->sc_cm_id->device, pa))
508                         goto err_put_ctxt;
509                 atomic_inc(&xprt->sc_dma_used);
510                 ctxt->sge[sge_no].addr = pa;
511                 ctxt->sge[sge_no].length = PAGE_SIZE;
512                 ctxt->sge[sge_no].lkey = xprt->sc_dma_lkey;
513                 buflen += PAGE_SIZE;
514         }
515         ctxt->count = sge_no;
516         recv_wr.next = NULL;
517         recv_wr.sg_list = &ctxt->sge[0];
518         recv_wr.num_sge = ctxt->count;
519         recv_wr.wr_id = (u64)(unsigned long)ctxt;
520
521         svc_xprt_get(&xprt->sc_xprt);
522         ret = ib_post_recv(xprt->sc_qp, &recv_wr, &bad_recv_wr);
523         if (ret) {
524                 svc_rdma_unmap_dma(ctxt);
525                 svc_rdma_put_context(ctxt, 1);
526                 svc_xprt_put(&xprt->sc_xprt);
527         }
528         return ret;
529
530  err_put_ctxt:
531         svc_rdma_put_context(ctxt, 1);
532         return -ENOMEM;
533 }
534
535 /*
536  * This function handles the CONNECT_REQUEST event on a listening
537  * endpoint. It is passed the cma_id for the _new_ connection. The context in
538  * this cma_id is inherited from the listening cma_id and is the svc_xprt
539  * structure for the listening endpoint.
540  *
541  * This function creates a new xprt for the new connection and enqueues it on
542  * the accept queue for the listent xprt. When the listen thread is kicked, it
543  * will call the recvfrom method on the listen xprt which will accept the new
544  * connection.
545  */
546 static void handle_connect_req(struct rdma_cm_id *new_cma_id, size_t client_ird)
547 {
548         struct svcxprt_rdma *listen_xprt = new_cma_id->context;
549         struct svcxprt_rdma *newxprt;
550         struct sockaddr *sa;
551
552         /* Create a new transport */
553         newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0);
554         if (!newxprt) {
555                 dprintk("svcrdma: failed to create new transport\n");
556                 return;
557         }
558         newxprt->sc_cm_id = new_cma_id;
559         new_cma_id->context = newxprt;
560         dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
561                 newxprt, newxprt->sc_cm_id, listen_xprt);
562
563         /* Save client advertised inbound read limit for use later in accept. */
564         newxprt->sc_ord = client_ird;
565
566         /* Set the local and remote addresses in the transport */
567         sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
568         svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
569         sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
570         svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
571
572         /*
573          * Enqueue the new transport on the accept queue of the listening
574          * transport
575          */
576         spin_lock_bh(&listen_xprt->sc_lock);
577         list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
578         spin_unlock_bh(&listen_xprt->sc_lock);
579
580         /*
581          * Can't use svc_xprt_received here because we are not on a
582          * rqstp thread
583         */
584         set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
585         svc_xprt_enqueue(&listen_xprt->sc_xprt);
586 }
587
588 /*
589  * Handles events generated on the listening endpoint. These events will be
590  * either be incoming connect requests or adapter removal  events.
591  */
592 static int rdma_listen_handler(struct rdma_cm_id *cma_id,
593                                struct rdma_cm_event *event)
594 {
595         struct svcxprt_rdma *xprt = cma_id->context;
596         int ret = 0;
597
598         switch (event->event) {
599         case RDMA_CM_EVENT_CONNECT_REQUEST:
600                 dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
601                         "event=%d\n", cma_id, cma_id->context, event->event);
602                 handle_connect_req(cma_id,
603                                    event->param.conn.initiator_depth);
604                 break;
605
606         case RDMA_CM_EVENT_ESTABLISHED:
607                 /* Accept complete */
608                 dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
609                         "cm_id=%p\n", xprt, cma_id);
610                 break;
611
612         case RDMA_CM_EVENT_DEVICE_REMOVAL:
613                 dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
614                         xprt, cma_id);
615                 if (xprt)
616                         set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
617                 break;
618
619         default:
620                 dprintk("svcrdma: Unexpected event on listening endpoint %p, "
621                         "event=%d\n", cma_id, event->event);
622                 break;
623         }
624
625         return ret;
626 }
627
628 static int rdma_cma_handler(struct rdma_cm_id *cma_id,
629                             struct rdma_cm_event *event)
630 {
631         struct svc_xprt *xprt = cma_id->context;
632         struct svcxprt_rdma *rdma =
633                 container_of(xprt, struct svcxprt_rdma, sc_xprt);
634         switch (event->event) {
635         case RDMA_CM_EVENT_ESTABLISHED:
636                 /* Accept complete */
637                 svc_xprt_get(xprt);
638                 dprintk("svcrdma: Connection completed on DTO xprt=%p, "
639                         "cm_id=%p\n", xprt, cma_id);
640                 clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
641                 svc_xprt_enqueue(xprt);
642                 break;
643         case RDMA_CM_EVENT_DISCONNECTED:
644                 dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
645                         xprt, cma_id);
646                 if (xprt) {
647                         set_bit(XPT_CLOSE, &xprt->xpt_flags);
648                         svc_xprt_enqueue(xprt);
649                         svc_xprt_put(xprt);
650                 }
651                 break;
652         case RDMA_CM_EVENT_DEVICE_REMOVAL:
653                 dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
654                         "event=%d\n", cma_id, xprt, event->event);
655                 if (xprt) {
656                         set_bit(XPT_CLOSE, &xprt->xpt_flags);
657                         svc_xprt_enqueue(xprt);
658                 }
659                 break;
660         default:
661                 dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
662                         "event=%d\n", cma_id, event->event);
663                 break;
664         }
665         return 0;
666 }
667
668 /*
669  * Create a listening RDMA service endpoint.
670  */
671 static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
672                                         struct sockaddr *sa, int salen,
673                                         int flags)
674 {
675         struct rdma_cm_id *listen_id;
676         struct svcxprt_rdma *cma_xprt;
677         struct svc_xprt *xprt;
678         int ret;
679
680         dprintk("svcrdma: Creating RDMA socket\n");
681         if (sa->sa_family != AF_INET) {
682                 dprintk("svcrdma: Address family %d is not supported.\n", sa->sa_family);
683                 return ERR_PTR(-EAFNOSUPPORT);
684         }
685         cma_xprt = rdma_create_xprt(serv, 1);
686         if (!cma_xprt)
687                 return ERR_PTR(-ENOMEM);
688         xprt = &cma_xprt->sc_xprt;
689
690         listen_id = rdma_create_id(rdma_listen_handler, cma_xprt, RDMA_PS_TCP);
691         if (IS_ERR(listen_id)) {
692                 ret = PTR_ERR(listen_id);
693                 dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
694                 goto err0;
695         }
696
697         ret = rdma_bind_addr(listen_id, sa);
698         if (ret) {
699                 dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
700                 goto err1;
701         }
702         cma_xprt->sc_cm_id = listen_id;
703
704         ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
705         if (ret) {
706                 dprintk("svcrdma: rdma_listen failed = %d\n", ret);
707                 goto err1;
708         }
709
710         /*
711          * We need to use the address from the cm_id in case the
712          * caller specified 0 for the port number.
713          */
714         sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
715         svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
716
717         return &cma_xprt->sc_xprt;
718
719  err1:
720         rdma_destroy_id(listen_id);
721  err0:
722         kfree(cma_xprt);
723         return ERR_PTR(ret);
724 }
725
726 static struct svc_rdma_fastreg_mr *rdma_alloc_frmr(struct svcxprt_rdma *xprt)
727 {
728         struct ib_mr *mr;
729         struct ib_fast_reg_page_list *pl;
730         struct svc_rdma_fastreg_mr *frmr;
731
732         frmr = kmalloc(sizeof(*frmr), GFP_KERNEL);
733         if (!frmr)
734                 goto err;
735
736         mr = ib_alloc_fast_reg_mr(xprt->sc_pd, RPCSVC_MAXPAGES);
737         if (IS_ERR(mr))
738                 goto err_free_frmr;
739
740         pl = ib_alloc_fast_reg_page_list(xprt->sc_cm_id->device,
741                                          RPCSVC_MAXPAGES);
742         if (IS_ERR(pl))
743                 goto err_free_mr;
744
745         frmr->mr = mr;
746         frmr->page_list = pl;
747         INIT_LIST_HEAD(&frmr->frmr_list);
748         return frmr;
749
750  err_free_mr:
751         ib_dereg_mr(mr);
752  err_free_frmr:
753         kfree(frmr);
754  err:
755         return ERR_PTR(-ENOMEM);
756 }
757
758 static void rdma_dealloc_frmr_q(struct svcxprt_rdma *xprt)
759 {
760         struct svc_rdma_fastreg_mr *frmr;
761
762         while (!list_empty(&xprt->sc_frmr_q)) {
763                 frmr = list_entry(xprt->sc_frmr_q.next,
764                                   struct svc_rdma_fastreg_mr, frmr_list);
765                 list_del_init(&frmr->frmr_list);
766                 ib_dereg_mr(frmr->mr);
767                 ib_free_fast_reg_page_list(frmr->page_list);
768                 kfree(frmr);
769         }
770 }
771
772 struct svc_rdma_fastreg_mr *svc_rdma_get_frmr(struct svcxprt_rdma *rdma)
773 {
774         struct svc_rdma_fastreg_mr *frmr = NULL;
775
776         spin_lock_bh(&rdma->sc_frmr_q_lock);
777         if (!list_empty(&rdma->sc_frmr_q)) {
778                 frmr = list_entry(rdma->sc_frmr_q.next,
779                                   struct svc_rdma_fastreg_mr, frmr_list);
780                 list_del_init(&frmr->frmr_list);
781                 frmr->map_len = 0;
782                 frmr->page_list_len = 0;
783         }
784         spin_unlock_bh(&rdma->sc_frmr_q_lock);
785         if (frmr)
786                 return frmr;
787
788         return rdma_alloc_frmr(rdma);
789 }
790
791 static void frmr_unmap_dma(struct svcxprt_rdma *xprt,
792                            struct svc_rdma_fastreg_mr *frmr)
793 {
794         int page_no;
795         for (page_no = 0; page_no < frmr->page_list_len; page_no++) {
796                 dma_addr_t addr = frmr->page_list->page_list[page_no];
797                 if (ib_dma_mapping_error(frmr->mr->device, addr))
798                         continue;
799                 atomic_dec(&xprt->sc_dma_used);
800                 ib_dma_unmap_single(frmr->mr->device, addr, PAGE_SIZE,
801                                     frmr->direction);
802         }
803 }
804
805 void svc_rdma_put_frmr(struct svcxprt_rdma *rdma,
806                        struct svc_rdma_fastreg_mr *frmr)
807 {
808         if (frmr) {
809                 frmr_unmap_dma(rdma, frmr);
810                 spin_lock_bh(&rdma->sc_frmr_q_lock);
811                 BUG_ON(!list_empty(&frmr->frmr_list));
812                 list_add(&frmr->frmr_list, &rdma->sc_frmr_q);
813                 spin_unlock_bh(&rdma->sc_frmr_q_lock);
814         }
815 }
816
817 /*
818  * This is the xpo_recvfrom function for listening endpoints. Its
819  * purpose is to accept incoming connections. The CMA callback handler
820  * has already created a new transport and attached it to the new CMA
821  * ID.
822  *
823  * There is a queue of pending connections hung on the listening
824  * transport. This queue contains the new svc_xprt structure. This
825  * function takes svc_xprt structures off the accept_q and completes
826  * the connection.
827  */
828 static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
829 {
830         struct svcxprt_rdma *listen_rdma;
831         struct svcxprt_rdma *newxprt = NULL;
832         struct rdma_conn_param conn_param;
833         struct ib_qp_init_attr qp_attr;
834         struct ib_device_attr devattr;
835         int uninitialized_var(dma_mr_acc);
836         int need_dma_mr;
837         int ret;
838         int i;
839
840         listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
841         clear_bit(XPT_CONN, &xprt->xpt_flags);
842         /* Get the next entry off the accept list */
843         spin_lock_bh(&listen_rdma->sc_lock);
844         if (!list_empty(&listen_rdma->sc_accept_q)) {
845                 newxprt = list_entry(listen_rdma->sc_accept_q.next,
846                                      struct svcxprt_rdma, sc_accept_q);
847                 list_del_init(&newxprt->sc_accept_q);
848         }
849         if (!list_empty(&listen_rdma->sc_accept_q))
850                 set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
851         spin_unlock_bh(&listen_rdma->sc_lock);
852         if (!newxprt)
853                 return NULL;
854
855         dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
856                 newxprt, newxprt->sc_cm_id);
857
858         ret = ib_query_device(newxprt->sc_cm_id->device, &devattr);
859         if (ret) {
860                 dprintk("svcrdma: could not query device attributes on "
861                         "device %p, rc=%d\n", newxprt->sc_cm_id->device, ret);
862                 goto errout;
863         }
864
865         /* Qualify the transport resource defaults with the
866          * capabilities of this particular device */
867         newxprt->sc_max_sge = min((size_t)devattr.max_sge,
868                                   (size_t)RPCSVC_MAXPAGES);
869         newxprt->sc_max_requests = min((size_t)devattr.max_qp_wr,
870                                    (size_t)svcrdma_max_requests);
871         newxprt->sc_sq_depth = RPCRDMA_SQ_DEPTH_MULT * newxprt->sc_max_requests;
872
873         /*
874          * Limit ORD based on client limit, local device limit, and
875          * configured svcrdma limit.
876          */
877         newxprt->sc_ord = min_t(size_t, devattr.max_qp_rd_atom, newxprt->sc_ord);
878         newxprt->sc_ord = min_t(size_t, svcrdma_ord, newxprt->sc_ord);
879
880         newxprt->sc_pd = ib_alloc_pd(newxprt->sc_cm_id->device);
881         if (IS_ERR(newxprt->sc_pd)) {
882                 dprintk("svcrdma: error creating PD for connect request\n");
883                 goto errout;
884         }
885         newxprt->sc_sq_cq = ib_create_cq(newxprt->sc_cm_id->device,
886                                          sq_comp_handler,
887                                          cq_event_handler,
888                                          newxprt,
889                                          newxprt->sc_sq_depth,
890                                          0);
891         if (IS_ERR(newxprt->sc_sq_cq)) {
892                 dprintk("svcrdma: error creating SQ CQ for connect request\n");
893                 goto errout;
894         }
895         newxprt->sc_rq_cq = ib_create_cq(newxprt->sc_cm_id->device,
896                                          rq_comp_handler,
897                                          cq_event_handler,
898                                          newxprt,
899                                          newxprt->sc_max_requests,
900                                          0);
901         if (IS_ERR(newxprt->sc_rq_cq)) {
902                 dprintk("svcrdma: error creating RQ CQ for connect request\n");
903                 goto errout;
904         }
905
906         memset(&qp_attr, 0, sizeof qp_attr);
907         qp_attr.event_handler = qp_event_handler;
908         qp_attr.qp_context = &newxprt->sc_xprt;
909         qp_attr.cap.max_send_wr = newxprt->sc_sq_depth;
910         qp_attr.cap.max_recv_wr = newxprt->sc_max_requests;
911         qp_attr.cap.max_send_sge = newxprt->sc_max_sge;
912         qp_attr.cap.max_recv_sge = newxprt->sc_max_sge;
913         qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
914         qp_attr.qp_type = IB_QPT_RC;
915         qp_attr.send_cq = newxprt->sc_sq_cq;
916         qp_attr.recv_cq = newxprt->sc_rq_cq;
917         dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n"
918                 "    cm_id->device=%p, sc_pd->device=%p\n"
919                 "    cap.max_send_wr = %d\n"
920                 "    cap.max_recv_wr = %d\n"
921                 "    cap.max_send_sge = %d\n"
922                 "    cap.max_recv_sge = %d\n",
923                 newxprt->sc_cm_id, newxprt->sc_pd,
924                 newxprt->sc_cm_id->device, newxprt->sc_pd->device,
925                 qp_attr.cap.max_send_wr,
926                 qp_attr.cap.max_recv_wr,
927                 qp_attr.cap.max_send_sge,
928                 qp_attr.cap.max_recv_sge);
929
930         ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
931         if (ret) {
932                 /*
933                  * XXX: This is a hack. We need a xx_request_qp interface
934                  * that will adjust the qp_attr's with a best-effort
935                  * number
936                  */
937                 qp_attr.cap.max_send_sge -= 2;
938                 qp_attr.cap.max_recv_sge -= 2;
939                 ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd,
940                                      &qp_attr);
941                 if (ret) {
942                         dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
943                         goto errout;
944                 }
945                 newxprt->sc_max_sge = qp_attr.cap.max_send_sge;
946                 newxprt->sc_max_sge = qp_attr.cap.max_recv_sge;
947                 newxprt->sc_sq_depth = qp_attr.cap.max_send_wr;
948                 newxprt->sc_max_requests = qp_attr.cap.max_recv_wr;
949         }
950         newxprt->sc_qp = newxprt->sc_cm_id->qp;
951
952         /*
953          * Use the most secure set of MR resources based on the
954          * transport type and available memory management features in
955          * the device. Here's the table implemented below:
956          *
957          *              Fast    Global  DMA     Remote WR
958          *              Reg     LKEY    MR      Access
959          *              Sup'd   Sup'd   Needed  Needed
960          *
961          * IWARP        N       N       Y       Y
962          *              N       Y       Y       Y
963          *              Y       N       Y       N
964          *              Y       Y       N       -
965          *
966          * IB           N       N       Y       N
967          *              N       Y       N       -
968          *              Y       N       Y       N
969          *              Y       Y       N       -
970          *
971          * NB:  iWARP requires remote write access for the data sink
972          *      of an RDMA_READ. IB does not.
973          */
974         if (devattr.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
975                 newxprt->sc_frmr_pg_list_len =
976                         devattr.max_fast_reg_page_list_len;
977                 newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_FAST_REG;
978         }
979
980         /*
981          * Determine if a DMA MR is required and if so, what privs are required
982          */
983         switch (rdma_node_get_transport(newxprt->sc_cm_id->device->node_type)) {
984         case RDMA_TRANSPORT_IWARP:
985                 newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_READ_W_INV;
986                 if (!(newxprt->sc_dev_caps & SVCRDMA_DEVCAP_FAST_REG)) {
987                         need_dma_mr = 1;
988                         dma_mr_acc =
989                                 (IB_ACCESS_LOCAL_WRITE |
990                                  IB_ACCESS_REMOTE_WRITE);
991                 } else if (!(devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)) {
992                         need_dma_mr = 1;
993                         dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
994                 } else
995                         need_dma_mr = 0;
996                 break;
997         case RDMA_TRANSPORT_IB:
998                 if (!(devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)) {
999                         need_dma_mr = 1;
1000                         dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
1001                 } else
1002                         need_dma_mr = 0;
1003                 break;
1004         default:
1005                 goto errout;
1006         }
1007
1008         /* Create the DMA MR if needed, otherwise, use the DMA LKEY */
1009         if (need_dma_mr) {
1010                 /* Register all of physical memory */
1011                 newxprt->sc_phys_mr =
1012                         ib_get_dma_mr(newxprt->sc_pd, dma_mr_acc);
1013                 if (IS_ERR(newxprt->sc_phys_mr)) {
1014                         dprintk("svcrdma: Failed to create DMA MR ret=%d\n",
1015                                 ret);
1016                         goto errout;
1017                 }
1018                 newxprt->sc_dma_lkey = newxprt->sc_phys_mr->lkey;
1019         } else
1020                 newxprt->sc_dma_lkey =
1021                         newxprt->sc_cm_id->device->local_dma_lkey;
1022
1023         /* Post receive buffers */
1024         for (i = 0; i < newxprt->sc_max_requests; i++) {
1025                 ret = svc_rdma_post_recv(newxprt);
1026                 if (ret) {
1027                         dprintk("svcrdma: failure posting receive buffers\n");
1028                         goto errout;
1029                 }
1030         }
1031
1032         /* Swap out the handler */
1033         newxprt->sc_cm_id->event_handler = rdma_cma_handler;
1034
1035         /*
1036          * Arm the CQs for the SQ and RQ before accepting so we can't
1037          * miss the first message
1038          */
1039         ib_req_notify_cq(newxprt->sc_sq_cq, IB_CQ_NEXT_COMP);
1040         ib_req_notify_cq(newxprt->sc_rq_cq, IB_CQ_NEXT_COMP);
1041
1042         /* Accept Connection */
1043         set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
1044         memset(&conn_param, 0, sizeof conn_param);
1045         conn_param.responder_resources = 0;
1046         conn_param.initiator_depth = newxprt->sc_ord;
1047         ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
1048         if (ret) {
1049                 dprintk("svcrdma: failed to accept new connection, ret=%d\n",
1050                        ret);
1051                 goto errout;
1052         }
1053
1054         dprintk("svcrdma: new connection %p accepted with the following "
1055                 "attributes:\n"
1056                 "    local_ip        : %pI4\n"
1057                 "    local_port      : %d\n"
1058                 "    remote_ip       : %pI4\n"
1059                 "    remote_port     : %d\n"
1060                 "    max_sge         : %d\n"
1061                 "    sq_depth        : %d\n"
1062                 "    max_requests    : %d\n"
1063                 "    ord             : %d\n",
1064                 newxprt,
1065                 &((struct sockaddr_in *)&newxprt->sc_cm_id->
1066                          route.addr.src_addr)->sin_addr.s_addr,
1067                 ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
1068                        route.addr.src_addr)->sin_port),
1069                 &((struct sockaddr_in *)&newxprt->sc_cm_id->
1070                          route.addr.dst_addr)->sin_addr.s_addr,
1071                 ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
1072                        route.addr.dst_addr)->sin_port),
1073                 newxprt->sc_max_sge,
1074                 newxprt->sc_sq_depth,
1075                 newxprt->sc_max_requests,
1076                 newxprt->sc_ord);
1077
1078         return &newxprt->sc_xprt;
1079
1080  errout:
1081         dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
1082         /* Take a reference in case the DTO handler runs */
1083         svc_xprt_get(&newxprt->sc_xprt);
1084         if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
1085                 ib_destroy_qp(newxprt->sc_qp);
1086         rdma_destroy_id(newxprt->sc_cm_id);
1087         /* This call to put will destroy the transport */
1088         svc_xprt_put(&newxprt->sc_xprt);
1089         return NULL;
1090 }
1091
1092 static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
1093 {
1094 }
1095
1096 /*
1097  * When connected, an svc_xprt has at least two references:
1098  *
1099  * - A reference held by the cm_id between the ESTABLISHED and
1100  *   DISCONNECTED events. If the remote peer disconnected first, this
1101  *   reference could be gone.
1102  *
1103  * - A reference held by the svc_recv code that called this function
1104  *   as part of close processing.
1105  *
1106  * At a minimum one references should still be held.
1107  */
1108 static void svc_rdma_detach(struct svc_xprt *xprt)
1109 {
1110         struct svcxprt_rdma *rdma =
1111                 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1112         dprintk("svc: svc_rdma_detach(%p)\n", xprt);
1113
1114         /* Disconnect and flush posted WQE */
1115         rdma_disconnect(rdma->sc_cm_id);
1116 }
1117
1118 static void __svc_rdma_free(struct work_struct *work)
1119 {
1120         struct svcxprt_rdma *rdma =
1121                 container_of(work, struct svcxprt_rdma, sc_work);
1122         dprintk("svcrdma: svc_rdma_free(%p)\n", rdma);
1123
1124         /* We should only be called from kref_put */
1125         BUG_ON(atomic_read(&rdma->sc_xprt.xpt_ref.refcount) != 0);
1126
1127         /*
1128          * Destroy queued, but not processed read completions. Note
1129          * that this cleanup has to be done before destroying the
1130          * cm_id because the device ptr is needed to unmap the dma in
1131          * svc_rdma_put_context.
1132          */
1133         while (!list_empty(&rdma->sc_read_complete_q)) {
1134                 struct svc_rdma_op_ctxt *ctxt;
1135                 ctxt = list_entry(rdma->sc_read_complete_q.next,
1136                                   struct svc_rdma_op_ctxt,
1137                                   dto_q);
1138                 list_del_init(&ctxt->dto_q);
1139                 svc_rdma_put_context(ctxt, 1);
1140         }
1141
1142         /* Destroy queued, but not processed recv completions */
1143         while (!list_empty(&rdma->sc_rq_dto_q)) {
1144                 struct svc_rdma_op_ctxt *ctxt;
1145                 ctxt = list_entry(rdma->sc_rq_dto_q.next,
1146                                   struct svc_rdma_op_ctxt,
1147                                   dto_q);
1148                 list_del_init(&ctxt->dto_q);
1149                 svc_rdma_put_context(ctxt, 1);
1150         }
1151
1152         /* Warn if we leaked a resource or under-referenced */
1153         WARN_ON(atomic_read(&rdma->sc_ctxt_used) != 0);
1154         WARN_ON(atomic_read(&rdma->sc_dma_used) != 0);
1155
1156         /* De-allocate fastreg mr */
1157         rdma_dealloc_frmr_q(rdma);
1158
1159         /* Destroy the QP if present (not a listener) */
1160         if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
1161                 ib_destroy_qp(rdma->sc_qp);
1162
1163         if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
1164                 ib_destroy_cq(rdma->sc_sq_cq);
1165
1166         if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
1167                 ib_destroy_cq(rdma->sc_rq_cq);
1168
1169         if (rdma->sc_phys_mr && !IS_ERR(rdma->sc_phys_mr))
1170                 ib_dereg_mr(rdma->sc_phys_mr);
1171
1172         if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
1173                 ib_dealloc_pd(rdma->sc_pd);
1174
1175         /* Destroy the CM ID */
1176         rdma_destroy_id(rdma->sc_cm_id);
1177
1178         kfree(rdma);
1179 }
1180
1181 static void svc_rdma_free(struct svc_xprt *xprt)
1182 {
1183         struct svcxprt_rdma *rdma =
1184                 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1185         INIT_WORK(&rdma->sc_work, __svc_rdma_free);
1186         schedule_work(&rdma->sc_work);
1187 }
1188
1189 static int svc_rdma_has_wspace(struct svc_xprt *xprt)
1190 {
1191         struct svcxprt_rdma *rdma =
1192                 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1193
1194         /*
1195          * If there are fewer SQ WR available than required to send a
1196          * simple response, return false.
1197          */
1198         if ((rdma->sc_sq_depth - atomic_read(&rdma->sc_sq_count) < 3))
1199                 return 0;
1200
1201         /*
1202          * ...or there are already waiters on the SQ,
1203          * return false.
1204          */
1205         if (waitqueue_active(&rdma->sc_send_wait))
1206                 return 0;
1207
1208         /* Otherwise return true. */
1209         return 1;
1210 }
1211
1212 /*
1213  * Attempt to register the kvec representing the RPC memory with the
1214  * device.
1215  *
1216  * Returns:
1217  *  NULL : The device does not support fastreg or there were no more
1218  *         fastreg mr.
1219  *  frmr : The kvec register request was successfully posted.
1220  *    <0 : An error was encountered attempting to register the kvec.
1221  */
1222 int svc_rdma_fastreg(struct svcxprt_rdma *xprt,
1223                      struct svc_rdma_fastreg_mr *frmr)
1224 {
1225         struct ib_send_wr fastreg_wr;
1226         u8 key;
1227
1228         /* Bump the key */
1229         key = (u8)(frmr->mr->lkey & 0x000000FF);
1230         ib_update_fast_reg_key(frmr->mr, ++key);
1231
1232         /* Prepare FASTREG WR */
1233         memset(&fastreg_wr, 0, sizeof fastreg_wr);
1234         fastreg_wr.opcode = IB_WR_FAST_REG_MR;
1235         fastreg_wr.send_flags = IB_SEND_SIGNALED;
1236         fastreg_wr.wr.fast_reg.iova_start = (unsigned long)frmr->kva;
1237         fastreg_wr.wr.fast_reg.page_list = frmr->page_list;
1238         fastreg_wr.wr.fast_reg.page_list_len = frmr->page_list_len;
1239         fastreg_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
1240         fastreg_wr.wr.fast_reg.length = frmr->map_len;
1241         fastreg_wr.wr.fast_reg.access_flags = frmr->access_flags;
1242         fastreg_wr.wr.fast_reg.rkey = frmr->mr->lkey;
1243         return svc_rdma_send(xprt, &fastreg_wr);
1244 }
1245
1246 int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr)
1247 {
1248         struct ib_send_wr *bad_wr, *n_wr;
1249         int wr_count;
1250         int i;
1251         int ret;
1252
1253         if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1254                 return -ENOTCONN;
1255
1256         BUG_ON(wr->send_flags != IB_SEND_SIGNALED);
1257         wr_count = 1;
1258         for (n_wr = wr->next; n_wr; n_wr = n_wr->next)
1259                 wr_count++;
1260
1261         /* If the SQ is full, wait until an SQ entry is available */
1262         while (1) {
1263                 spin_lock_bh(&xprt->sc_lock);
1264                 if (xprt->sc_sq_depth < atomic_read(&xprt->sc_sq_count) + wr_count) {
1265                         spin_unlock_bh(&xprt->sc_lock);
1266                         atomic_inc(&rdma_stat_sq_starve);
1267
1268                         /* See if we can opportunistically reap SQ WR to make room */
1269                         sq_cq_reap(xprt);
1270
1271                         /* Wait until SQ WR available if SQ still full */
1272                         wait_event(xprt->sc_send_wait,
1273                                    atomic_read(&xprt->sc_sq_count) <
1274                                    xprt->sc_sq_depth);
1275                         if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1276                                 return 0;
1277                         continue;
1278                 }
1279                 /* Take a transport ref for each WR posted */
1280                 for (i = 0; i < wr_count; i++)
1281                         svc_xprt_get(&xprt->sc_xprt);
1282
1283                 /* Bump used SQ WR count and post */
1284                 atomic_add(wr_count, &xprt->sc_sq_count);
1285                 ret = ib_post_send(xprt->sc_qp, wr, &bad_wr);
1286                 if (ret) {
1287                         set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
1288                         atomic_sub(wr_count, &xprt->sc_sq_count);
1289                         for (i = 0; i < wr_count; i ++)
1290                                 svc_xprt_put(&xprt->sc_xprt);
1291                         dprintk("svcrdma: failed to post SQ WR rc=%d, "
1292                                "sc_sq_count=%d, sc_sq_depth=%d\n",
1293                                ret, atomic_read(&xprt->sc_sq_count),
1294                                xprt->sc_sq_depth);
1295                 }
1296                 spin_unlock_bh(&xprt->sc_lock);
1297                 if (ret)
1298                         wake_up(&xprt->sc_send_wait);
1299                 break;
1300         }
1301         return ret;
1302 }
1303
1304 void svc_rdma_send_error(struct svcxprt_rdma *xprt, struct rpcrdma_msg *rmsgp,
1305                          enum rpcrdma_errcode err)
1306 {
1307         struct ib_send_wr err_wr;
1308         struct ib_sge sge;
1309         struct page *p;
1310         struct svc_rdma_op_ctxt *ctxt;
1311         u32 *va;
1312         int length;
1313         int ret;
1314
1315         p = svc_rdma_get_page();
1316         va = page_address(p);
1317
1318         /* XDR encode error */
1319         length = svc_rdma_xdr_encode_error(xprt, rmsgp, err, va);
1320
1321         /* Prepare SGE for local address */
1322         sge.addr = ib_dma_map_single(xprt->sc_cm_id->device,
1323                                    page_address(p), PAGE_SIZE, DMA_FROM_DEVICE);
1324         if (ib_dma_mapping_error(xprt->sc_cm_id->device, sge.addr)) {
1325                 put_page(p);
1326                 return;
1327         }
1328         atomic_inc(&xprt->sc_dma_used);
1329         sge.lkey = xprt->sc_dma_lkey;
1330         sge.length = length;
1331
1332         ctxt = svc_rdma_get_context(xprt);
1333         ctxt->count = 1;
1334         ctxt->pages[0] = p;
1335
1336         /* Prepare SEND WR */
1337         memset(&err_wr, 0, sizeof err_wr);
1338         ctxt->wr_op = IB_WR_SEND;
1339         err_wr.wr_id = (unsigned long)ctxt;
1340         err_wr.sg_list = &sge;
1341         err_wr.num_sge = 1;
1342         err_wr.opcode = IB_WR_SEND;
1343         err_wr.send_flags = IB_SEND_SIGNALED;
1344
1345         /* Post It */
1346         ret = svc_rdma_send(xprt, &err_wr);
1347         if (ret) {
1348                 dprintk("svcrdma: Error %d posting send for protocol error\n",
1349                         ret);
1350                 ib_dma_unmap_single(xprt->sc_cm_id->device,
1351                                   sge.addr, PAGE_SIZE,
1352                                   DMA_FROM_DEVICE);
1353                 svc_rdma_put_context(ctxt, 1);
1354         }
1355 }