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