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