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