]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/sunrpc/xprtrdma/svc_rdma_transport.c
b25c50992a95c163e97951292a4b34bdf0101feb
[karo-tx-linux.git] / net / sunrpc / xprtrdma / svc_rdma_transport.c
1 /*
2  * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved.
3  * Copyright (c) 2005-2007 Network Appliance, Inc. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the BSD-type
9  * license below:
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  *      Redistributions of source code must retain the above copyright
16  *      notice, this list of conditions and the following disclaimer.
17  *
18  *      Redistributions in binary form must reproduce the above
19  *      copyright notice, this list of conditions and the following
20  *      disclaimer in the documentation and/or other materials provided
21  *      with the distribution.
22  *
23  *      Neither the name of the Network Appliance, Inc. nor the names of
24  *      its contributors may be used to endorse or promote products
25  *      derived from this software without specific prior written
26  *      permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  *
40  * Author: Tom Tucker <tom@opengridcomputing.com>
41  */
42
43 #include <linux/sunrpc/svc_xprt.h>
44 #include <linux/sunrpc/addr.h>
45 #include <linux/sunrpc/debug.h>
46 #include <linux/sunrpc/rpc_rdma.h>
47 #include <linux/interrupt.h>
48 #include <linux/sched.h>
49 #include <linux/slab.h>
50 #include <linux/spinlock.h>
51 #include <linux/workqueue.h>
52 #include <rdma/ib_verbs.h>
53 #include <rdma/rdma_cm.h>
54 #include <linux/sunrpc/svc_rdma.h>
55 #include <linux/export.h>
56 #include "xprt_rdma.h"
57
58 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
59
60 static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *, int);
61 static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
62                                         struct net *net,
63                                         struct sockaddr *sa, int salen,
64                                         int flags);
65 static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt);
66 static void svc_rdma_release_rqst(struct svc_rqst *);
67 static void svc_rdma_detach(struct svc_xprt *xprt);
68 static void svc_rdma_free(struct svc_xprt *xprt);
69 static int svc_rdma_has_wspace(struct svc_xprt *xprt);
70 static int svc_rdma_secure_port(struct svc_rqst *);
71 static void svc_rdma_kill_temp_xprt(struct svc_xprt *);
72
73 static struct svc_xprt_ops svc_rdma_ops = {
74         .xpo_create = svc_rdma_create,
75         .xpo_recvfrom = svc_rdma_recvfrom,
76         .xpo_sendto = svc_rdma_sendto,
77         .xpo_release_rqst = svc_rdma_release_rqst,
78         .xpo_detach = svc_rdma_detach,
79         .xpo_free = svc_rdma_free,
80         .xpo_prep_reply_hdr = svc_rdma_prep_reply_hdr,
81         .xpo_has_wspace = svc_rdma_has_wspace,
82         .xpo_accept = svc_rdma_accept,
83         .xpo_secure_port = svc_rdma_secure_port,
84         .xpo_kill_temp_xprt = svc_rdma_kill_temp_xprt,
85 };
86
87 struct svc_xprt_class svc_rdma_class = {
88         .xcl_name = "rdma",
89         .xcl_owner = THIS_MODULE,
90         .xcl_ops = &svc_rdma_ops,
91         .xcl_max_payload = RPCSVC_MAXPAYLOAD_RDMA,
92         .xcl_ident = XPRT_TRANSPORT_RDMA,
93 };
94
95 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
96 static struct svc_xprt *svc_rdma_bc_create(struct svc_serv *, struct net *,
97                                            struct sockaddr *, int, int);
98 static void svc_rdma_bc_detach(struct svc_xprt *);
99 static void svc_rdma_bc_free(struct svc_xprt *);
100
101 static struct svc_xprt_ops svc_rdma_bc_ops = {
102         .xpo_create = svc_rdma_bc_create,
103         .xpo_detach = svc_rdma_bc_detach,
104         .xpo_free = svc_rdma_bc_free,
105         .xpo_prep_reply_hdr = svc_rdma_prep_reply_hdr,
106         .xpo_secure_port = svc_rdma_secure_port,
107 };
108
109 struct svc_xprt_class svc_rdma_bc_class = {
110         .xcl_name = "rdma-bc",
111         .xcl_owner = THIS_MODULE,
112         .xcl_ops = &svc_rdma_bc_ops,
113         .xcl_max_payload = (1024 - RPCRDMA_HDRLEN_MIN)
114 };
115
116 static struct svc_xprt *svc_rdma_bc_create(struct svc_serv *serv,
117                                            struct net *net,
118                                            struct sockaddr *sa, int salen,
119                                            int flags)
120 {
121         struct svcxprt_rdma *cma_xprt;
122         struct svc_xprt *xprt;
123
124         cma_xprt = rdma_create_xprt(serv, 0);
125         if (!cma_xprt)
126                 return ERR_PTR(-ENOMEM);
127         xprt = &cma_xprt->sc_xprt;
128
129         svc_xprt_init(net, &svc_rdma_bc_class, xprt, serv);
130         set_bit(XPT_CONG_CTRL, &xprt->xpt_flags);
131         serv->sv_bc_xprt = xprt;
132
133         dprintk("svcrdma: %s(%p)\n", __func__, xprt);
134         return xprt;
135 }
136
137 static void svc_rdma_bc_detach(struct svc_xprt *xprt)
138 {
139         dprintk("svcrdma: %s(%p)\n", __func__, xprt);
140 }
141
142 static void svc_rdma_bc_free(struct svc_xprt *xprt)
143 {
144         struct svcxprt_rdma *rdma =
145                 container_of(xprt, struct svcxprt_rdma, sc_xprt);
146
147         dprintk("svcrdma: %s(%p)\n", __func__, xprt);
148         if (xprt)
149                 kfree(rdma);
150 }
151 #endif  /* CONFIG_SUNRPC_BACKCHANNEL */
152
153 static struct svc_rdma_op_ctxt *alloc_ctxt(struct svcxprt_rdma *xprt,
154                                            gfp_t flags)
155 {
156         struct svc_rdma_op_ctxt *ctxt;
157
158         ctxt = kmalloc(sizeof(*ctxt), flags);
159         if (ctxt) {
160                 ctxt->xprt = xprt;
161                 INIT_LIST_HEAD(&ctxt->list);
162         }
163         return ctxt;
164 }
165
166 static bool svc_rdma_prealloc_ctxts(struct svcxprt_rdma *xprt)
167 {
168         unsigned int i;
169
170         /* Each RPC/RDMA credit can consume a number of send
171          * and receive WQEs. One ctxt is allocated for each.
172          */
173         i = xprt->sc_sq_depth + xprt->sc_rq_depth;
174
175         while (i--) {
176                 struct svc_rdma_op_ctxt *ctxt;
177
178                 ctxt = alloc_ctxt(xprt, GFP_KERNEL);
179                 if (!ctxt) {
180                         dprintk("svcrdma: No memory for RDMA ctxt\n");
181                         return false;
182                 }
183                 list_add(&ctxt->list, &xprt->sc_ctxts);
184         }
185         return true;
186 }
187
188 struct svc_rdma_op_ctxt *svc_rdma_get_context(struct svcxprt_rdma *xprt)
189 {
190         struct svc_rdma_op_ctxt *ctxt = NULL;
191
192         spin_lock(&xprt->sc_ctxt_lock);
193         xprt->sc_ctxt_used++;
194         if (list_empty(&xprt->sc_ctxts))
195                 goto out_empty;
196
197         ctxt = list_first_entry(&xprt->sc_ctxts,
198                                 struct svc_rdma_op_ctxt, list);
199         list_del(&ctxt->list);
200         spin_unlock(&xprt->sc_ctxt_lock);
201
202 out:
203         ctxt->count = 0;
204         ctxt->mapped_sges = 0;
205         ctxt->frmr = NULL;
206         return ctxt;
207
208 out_empty:
209         /* Either pre-allocation missed the mark, or send
210          * queue accounting is broken.
211          */
212         spin_unlock(&xprt->sc_ctxt_lock);
213
214         ctxt = alloc_ctxt(xprt, GFP_NOIO);
215         if (ctxt)
216                 goto out;
217
218         spin_lock(&xprt->sc_ctxt_lock);
219         xprt->sc_ctxt_used--;
220         spin_unlock(&xprt->sc_ctxt_lock);
221         WARN_ONCE(1, "svcrdma: empty RDMA ctxt list?\n");
222         return NULL;
223 }
224
225 void svc_rdma_unmap_dma(struct svc_rdma_op_ctxt *ctxt)
226 {
227         struct svcxprt_rdma *xprt = ctxt->xprt;
228         struct ib_device *device = xprt->sc_cm_id->device;
229         u32 lkey = xprt->sc_pd->local_dma_lkey;
230         unsigned int i;
231
232         for (i = 0; i < ctxt->mapped_sges; i++) {
233                 /*
234                  * Unmap the DMA addr in the SGE if the lkey matches
235                  * the local_dma_lkey, otherwise, ignore it since it is
236                  * an FRMR lkey and will be unmapped later when the
237                  * last WR that uses it completes.
238                  */
239                 if (ctxt->sge[i].lkey == lkey)
240                         ib_dma_unmap_page(device,
241                                             ctxt->sge[i].addr,
242                                             ctxt->sge[i].length,
243                                             ctxt->direction);
244         }
245         ctxt->mapped_sges = 0;
246 }
247
248 void svc_rdma_put_context(struct svc_rdma_op_ctxt *ctxt, int free_pages)
249 {
250         struct svcxprt_rdma *xprt = ctxt->xprt;
251         int i;
252
253         if (free_pages)
254                 for (i = 0; i < ctxt->count; i++)
255                         put_page(ctxt->pages[i]);
256
257         spin_lock(&xprt->sc_ctxt_lock);
258         xprt->sc_ctxt_used--;
259         list_add(&ctxt->list, &xprt->sc_ctxts);
260         spin_unlock(&xprt->sc_ctxt_lock);
261 }
262
263 static void svc_rdma_destroy_ctxts(struct svcxprt_rdma *xprt)
264 {
265         while (!list_empty(&xprt->sc_ctxts)) {
266                 struct svc_rdma_op_ctxt *ctxt;
267
268                 ctxt = list_first_entry(&xprt->sc_ctxts,
269                                         struct svc_rdma_op_ctxt, list);
270                 list_del(&ctxt->list);
271                 kfree(ctxt);
272         }
273 }
274
275 static struct svc_rdma_req_map *alloc_req_map(gfp_t flags)
276 {
277         struct svc_rdma_req_map *map;
278
279         map = kmalloc(sizeof(*map), flags);
280         if (map)
281                 INIT_LIST_HEAD(&map->free);
282         return map;
283 }
284
285 static bool svc_rdma_prealloc_maps(struct svcxprt_rdma *xprt)
286 {
287         unsigned int i;
288
289         /* One for each receive buffer on this connection. */
290         i = xprt->sc_max_requests;
291
292         while (i--) {
293                 struct svc_rdma_req_map *map;
294
295                 map = alloc_req_map(GFP_KERNEL);
296                 if (!map) {
297                         dprintk("svcrdma: No memory for request map\n");
298                         return false;
299                 }
300                 list_add(&map->free, &xprt->sc_maps);
301         }
302         return true;
303 }
304
305 struct svc_rdma_req_map *svc_rdma_get_req_map(struct svcxprt_rdma *xprt)
306 {
307         struct svc_rdma_req_map *map = NULL;
308
309         spin_lock(&xprt->sc_map_lock);
310         if (list_empty(&xprt->sc_maps))
311                 goto out_empty;
312
313         map = list_first_entry(&xprt->sc_maps,
314                                struct svc_rdma_req_map, free);
315         list_del_init(&map->free);
316         spin_unlock(&xprt->sc_map_lock);
317
318 out:
319         map->count = 0;
320         return map;
321
322 out_empty:
323         spin_unlock(&xprt->sc_map_lock);
324
325         /* Pre-allocation amount was incorrect */
326         map = alloc_req_map(GFP_NOIO);
327         if (map)
328                 goto out;
329
330         WARN_ONCE(1, "svcrdma: empty request map list?\n");
331         return NULL;
332 }
333
334 void svc_rdma_put_req_map(struct svcxprt_rdma *xprt,
335                           struct svc_rdma_req_map *map)
336 {
337         spin_lock(&xprt->sc_map_lock);
338         list_add(&map->free, &xprt->sc_maps);
339         spin_unlock(&xprt->sc_map_lock);
340 }
341
342 static void svc_rdma_destroy_maps(struct svcxprt_rdma *xprt)
343 {
344         while (!list_empty(&xprt->sc_maps)) {
345                 struct svc_rdma_req_map *map;
346
347                 map = list_first_entry(&xprt->sc_maps,
348                                        struct svc_rdma_req_map, free);
349                 list_del(&map->free);
350                 kfree(map);
351         }
352 }
353
354 /* QP event handler */
355 static void qp_event_handler(struct ib_event *event, void *context)
356 {
357         struct svc_xprt *xprt = context;
358
359         switch (event->event) {
360         /* These are considered benign events */
361         case IB_EVENT_PATH_MIG:
362         case IB_EVENT_COMM_EST:
363         case IB_EVENT_SQ_DRAINED:
364         case IB_EVENT_QP_LAST_WQE_REACHED:
365                 dprintk("svcrdma: QP event %s (%d) received for QP=%p\n",
366                         ib_event_msg(event->event), event->event,
367                         event->element.qp);
368                 break;
369         /* These are considered fatal events */
370         case IB_EVENT_PATH_MIG_ERR:
371         case IB_EVENT_QP_FATAL:
372         case IB_EVENT_QP_REQ_ERR:
373         case IB_EVENT_QP_ACCESS_ERR:
374         case IB_EVENT_DEVICE_FATAL:
375         default:
376                 dprintk("svcrdma: QP ERROR event %s (%d) received for QP=%p, "
377                         "closing transport\n",
378                         ib_event_msg(event->event), event->event,
379                         event->element.qp);
380                 set_bit(XPT_CLOSE, &xprt->xpt_flags);
381                 break;
382         }
383 }
384
385 /**
386  * svc_rdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
387  * @cq:        completion queue
388  * @wc:        completed WR
389  *
390  */
391 static void svc_rdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
392 {
393         struct svcxprt_rdma *xprt = cq->cq_context;
394         struct ib_cqe *cqe = wc->wr_cqe;
395         struct svc_rdma_op_ctxt *ctxt;
396
397         /* WARNING: Only wc->wr_cqe and wc->status are reliable */
398         ctxt = container_of(cqe, struct svc_rdma_op_ctxt, cqe);
399         svc_rdma_unmap_dma(ctxt);
400
401         if (wc->status != IB_WC_SUCCESS)
402                 goto flushed;
403
404         /* All wc fields are now known to be valid */
405         ctxt->byte_len = wc->byte_len;
406         spin_lock(&xprt->sc_rq_dto_lock);
407         list_add_tail(&ctxt->list, &xprt->sc_rq_dto_q);
408         spin_unlock(&xprt->sc_rq_dto_lock);
409
410         set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
411         if (test_bit(RDMAXPRT_CONN_PENDING, &xprt->sc_flags))
412                 goto out;
413         svc_xprt_enqueue(&xprt->sc_xprt);
414         goto out;
415
416 flushed:
417         if (wc->status != IB_WC_WR_FLUSH_ERR)
418                 pr_warn("svcrdma: receive: %s (%u/0x%x)\n",
419                         ib_wc_status_msg(wc->status),
420                         wc->status, wc->vendor_err);
421         set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
422         svc_rdma_put_context(ctxt, 1);
423
424 out:
425         svc_xprt_put(&xprt->sc_xprt);
426 }
427
428 static void svc_rdma_send_wc_common(struct svcxprt_rdma *xprt,
429                                     struct ib_wc *wc,
430                                     const char *opname)
431 {
432         if (wc->status != IB_WC_SUCCESS)
433                 goto err;
434
435 out:
436         atomic_inc(&xprt->sc_sq_avail);
437         wake_up(&xprt->sc_send_wait);
438         return;
439
440 err:
441         set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
442         if (wc->status != IB_WC_WR_FLUSH_ERR)
443                 pr_err("svcrdma: %s: %s (%u/0x%x)\n",
444                        opname, ib_wc_status_msg(wc->status),
445                        wc->status, wc->vendor_err);
446         goto out;
447 }
448
449 static void svc_rdma_send_wc_common_put(struct ib_cq *cq, struct ib_wc *wc,
450                                         const char *opname)
451 {
452         struct svcxprt_rdma *xprt = cq->cq_context;
453
454         svc_rdma_send_wc_common(xprt, wc, opname);
455         svc_xprt_put(&xprt->sc_xprt);
456 }
457
458 /**
459  * svc_rdma_wc_send - Invoked by RDMA provider for each polled Send WC
460  * @cq:        completion queue
461  * @wc:        completed WR
462  *
463  */
464 void svc_rdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
465 {
466         struct ib_cqe *cqe = wc->wr_cqe;
467         struct svc_rdma_op_ctxt *ctxt;
468
469         svc_rdma_send_wc_common_put(cq, wc, "send");
470
471         ctxt = container_of(cqe, struct svc_rdma_op_ctxt, cqe);
472         svc_rdma_unmap_dma(ctxt);
473         svc_rdma_put_context(ctxt, 1);
474 }
475
476 /**
477  * svc_rdma_wc_write - Invoked by RDMA provider for each polled Write WC
478  * @cq:        completion queue
479  * @wc:        completed WR
480  *
481  */
482 void svc_rdma_wc_write(struct ib_cq *cq, struct ib_wc *wc)
483 {
484         struct ib_cqe *cqe = wc->wr_cqe;
485         struct svc_rdma_op_ctxt *ctxt;
486
487         svc_rdma_send_wc_common_put(cq, wc, "write");
488
489         ctxt = container_of(cqe, struct svc_rdma_op_ctxt, cqe);
490         svc_rdma_unmap_dma(ctxt);
491         svc_rdma_put_context(ctxt, 0);
492 }
493
494 /**
495  * svc_rdma_wc_reg - Invoked by RDMA provider for each polled FASTREG WC
496  * @cq:        completion queue
497  * @wc:        completed WR
498  *
499  */
500 void svc_rdma_wc_reg(struct ib_cq *cq, struct ib_wc *wc)
501 {
502         svc_rdma_send_wc_common_put(cq, wc, "fastreg");
503 }
504
505 /**
506  * svc_rdma_wc_read - Invoked by RDMA provider for each polled Read WC
507  * @cq:        completion queue
508  * @wc:        completed WR
509  *
510  */
511 void svc_rdma_wc_read(struct ib_cq *cq, struct ib_wc *wc)
512 {
513         struct svcxprt_rdma *xprt = cq->cq_context;
514         struct ib_cqe *cqe = wc->wr_cqe;
515         struct svc_rdma_op_ctxt *ctxt;
516
517         svc_rdma_send_wc_common(xprt, wc, "read");
518
519         ctxt = container_of(cqe, struct svc_rdma_op_ctxt, cqe);
520         svc_rdma_unmap_dma(ctxt);
521         svc_rdma_put_frmr(xprt, ctxt->frmr);
522
523         if (test_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags)) {
524                 struct svc_rdma_op_ctxt *read_hdr;
525
526                 read_hdr = ctxt->read_hdr;
527                 spin_lock(&xprt->sc_rq_dto_lock);
528                 list_add_tail(&read_hdr->list,
529                               &xprt->sc_read_complete_q);
530                 spin_unlock(&xprt->sc_rq_dto_lock);
531
532                 set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
533                 svc_xprt_enqueue(&xprt->sc_xprt);
534         }
535
536         svc_rdma_put_context(ctxt, 0);
537         svc_xprt_put(&xprt->sc_xprt);
538 }
539
540 /**
541  * svc_rdma_wc_inv - Invoked by RDMA provider for each polled LOCAL_INV WC
542  * @cq:        completion queue
543  * @wc:        completed WR
544  *
545  */
546 void svc_rdma_wc_inv(struct ib_cq *cq, struct ib_wc *wc)
547 {
548         svc_rdma_send_wc_common_put(cq, wc, "localInv");
549 }
550
551 static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *serv,
552                                              int listener)
553 {
554         struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
555
556         if (!cma_xprt)
557                 return NULL;
558         svc_xprt_init(&init_net, &svc_rdma_class, &cma_xprt->sc_xprt, serv);
559         INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
560         INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
561         INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
562         INIT_LIST_HEAD(&cma_xprt->sc_frmr_q);
563         INIT_LIST_HEAD(&cma_xprt->sc_ctxts);
564         INIT_LIST_HEAD(&cma_xprt->sc_rw_ctxts);
565         INIT_LIST_HEAD(&cma_xprt->sc_maps);
566         init_waitqueue_head(&cma_xprt->sc_send_wait);
567
568         spin_lock_init(&cma_xprt->sc_lock);
569         spin_lock_init(&cma_xprt->sc_rq_dto_lock);
570         spin_lock_init(&cma_xprt->sc_frmr_q_lock);
571         spin_lock_init(&cma_xprt->sc_ctxt_lock);
572         spin_lock_init(&cma_xprt->sc_rw_ctxt_lock);
573         spin_lock_init(&cma_xprt->sc_map_lock);
574
575         /*
576          * Note that this implies that the underlying transport support
577          * has some form of congestion control (see RFC 7530 section 3.1
578          * paragraph 2). For now, we assume that all supported RDMA
579          * transports are suitable here.
580          */
581         set_bit(XPT_CONG_CTRL, &cma_xprt->sc_xprt.xpt_flags);
582
583         if (listener)
584                 set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
585
586         return cma_xprt;
587 }
588
589 int svc_rdma_post_recv(struct svcxprt_rdma *xprt, gfp_t flags)
590 {
591         struct ib_recv_wr recv_wr, *bad_recv_wr;
592         struct svc_rdma_op_ctxt *ctxt;
593         struct page *page;
594         dma_addr_t pa;
595         int sge_no;
596         int buflen;
597         int ret;
598
599         ctxt = svc_rdma_get_context(xprt);
600         buflen = 0;
601         ctxt->direction = DMA_FROM_DEVICE;
602         ctxt->cqe.done = svc_rdma_wc_receive;
603         for (sge_no = 0; buflen < xprt->sc_max_req_size; sge_no++) {
604                 if (sge_no >= xprt->sc_max_sge) {
605                         pr_err("svcrdma: Too many sges (%d)\n", sge_no);
606                         goto err_put_ctxt;
607                 }
608                 page = alloc_page(flags);
609                 if (!page)
610                         goto err_put_ctxt;
611                 ctxt->pages[sge_no] = page;
612                 pa = ib_dma_map_page(xprt->sc_cm_id->device,
613                                      page, 0, PAGE_SIZE,
614                                      DMA_FROM_DEVICE);
615                 if (ib_dma_mapping_error(xprt->sc_cm_id->device, pa))
616                         goto err_put_ctxt;
617                 svc_rdma_count_mappings(xprt, ctxt);
618                 ctxt->sge[sge_no].addr = pa;
619                 ctxt->sge[sge_no].length = PAGE_SIZE;
620                 ctxt->sge[sge_no].lkey = xprt->sc_pd->local_dma_lkey;
621                 ctxt->count = sge_no + 1;
622                 buflen += PAGE_SIZE;
623         }
624         recv_wr.next = NULL;
625         recv_wr.sg_list = &ctxt->sge[0];
626         recv_wr.num_sge = ctxt->count;
627         recv_wr.wr_cqe = &ctxt->cqe;
628
629         svc_xprt_get(&xprt->sc_xprt);
630         ret = ib_post_recv(xprt->sc_qp, &recv_wr, &bad_recv_wr);
631         if (ret) {
632                 svc_rdma_unmap_dma(ctxt);
633                 svc_rdma_put_context(ctxt, 1);
634                 svc_xprt_put(&xprt->sc_xprt);
635         }
636         return ret;
637
638  err_put_ctxt:
639         svc_rdma_unmap_dma(ctxt);
640         svc_rdma_put_context(ctxt, 1);
641         return -ENOMEM;
642 }
643
644 int svc_rdma_repost_recv(struct svcxprt_rdma *xprt, gfp_t flags)
645 {
646         int ret = 0;
647
648         ret = svc_rdma_post_recv(xprt, flags);
649         if (ret) {
650                 pr_err("svcrdma: could not post a receive buffer, err=%d.\n",
651                        ret);
652                 pr_err("svcrdma: closing transport %p.\n", xprt);
653                 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
654                 ret = -ENOTCONN;
655         }
656         return ret;
657 }
658
659 static void
660 svc_rdma_parse_connect_private(struct svcxprt_rdma *newxprt,
661                                struct rdma_conn_param *param)
662 {
663         const struct rpcrdma_connect_private *pmsg = param->private_data;
664
665         if (pmsg &&
666             pmsg->cp_magic == rpcrdma_cmp_magic &&
667             pmsg->cp_version == RPCRDMA_CMP_VERSION) {
668                 newxprt->sc_snd_w_inv = pmsg->cp_flags &
669                                         RPCRDMA_CMP_F_SND_W_INV_OK;
670
671                 dprintk("svcrdma: client send_size %u, recv_size %u "
672                         "remote inv %ssupported\n",
673                         rpcrdma_decode_buffer_size(pmsg->cp_send_size),
674                         rpcrdma_decode_buffer_size(pmsg->cp_recv_size),
675                         newxprt->sc_snd_w_inv ? "" : "un");
676         }
677 }
678
679 /*
680  * This function handles the CONNECT_REQUEST event on a listening
681  * endpoint. It is passed the cma_id for the _new_ connection. The context in
682  * this cma_id is inherited from the listening cma_id and is the svc_xprt
683  * structure for the listening endpoint.
684  *
685  * This function creates a new xprt for the new connection and enqueues it on
686  * the accept queue for the listent xprt. When the listen thread is kicked, it
687  * will call the recvfrom method on the listen xprt which will accept the new
688  * connection.
689  */
690 static void handle_connect_req(struct rdma_cm_id *new_cma_id,
691                                struct rdma_conn_param *param)
692 {
693         struct svcxprt_rdma *listen_xprt = new_cma_id->context;
694         struct svcxprt_rdma *newxprt;
695         struct sockaddr *sa;
696
697         /* Create a new transport */
698         newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0);
699         if (!newxprt) {
700                 dprintk("svcrdma: failed to create new transport\n");
701                 return;
702         }
703         newxprt->sc_cm_id = new_cma_id;
704         new_cma_id->context = newxprt;
705         dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
706                 newxprt, newxprt->sc_cm_id, listen_xprt);
707         svc_rdma_parse_connect_private(newxprt, param);
708
709         /* Save client advertised inbound read limit for use later in accept. */
710         newxprt->sc_ord = param->initiator_depth;
711
712         /* Set the local and remote addresses in the transport */
713         sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
714         svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
715         sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
716         svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
717
718         /*
719          * Enqueue the new transport on the accept queue of the listening
720          * transport
721          */
722         spin_lock_bh(&listen_xprt->sc_lock);
723         list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
724         spin_unlock_bh(&listen_xprt->sc_lock);
725
726         set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
727         svc_xprt_enqueue(&listen_xprt->sc_xprt);
728 }
729
730 /*
731  * Handles events generated on the listening endpoint. These events will be
732  * either be incoming connect requests or adapter removal  events.
733  */
734 static int rdma_listen_handler(struct rdma_cm_id *cma_id,
735                                struct rdma_cm_event *event)
736 {
737         struct svcxprt_rdma *xprt = cma_id->context;
738         int ret = 0;
739
740         switch (event->event) {
741         case RDMA_CM_EVENT_CONNECT_REQUEST:
742                 dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
743                         "event = %s (%d)\n", cma_id, cma_id->context,
744                         rdma_event_msg(event->event), event->event);
745                 handle_connect_req(cma_id, &event->param.conn);
746                 break;
747
748         case RDMA_CM_EVENT_ESTABLISHED:
749                 /* Accept complete */
750                 dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
751                         "cm_id=%p\n", xprt, cma_id);
752                 break;
753
754         case RDMA_CM_EVENT_DEVICE_REMOVAL:
755                 dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
756                         xprt, cma_id);
757                 if (xprt)
758                         set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
759                 break;
760
761         default:
762                 dprintk("svcrdma: Unexpected event on listening endpoint %p, "
763                         "event = %s (%d)\n", cma_id,
764                         rdma_event_msg(event->event), event->event);
765                 break;
766         }
767
768         return ret;
769 }
770
771 static int rdma_cma_handler(struct rdma_cm_id *cma_id,
772                             struct rdma_cm_event *event)
773 {
774         struct svc_xprt *xprt = cma_id->context;
775         struct svcxprt_rdma *rdma =
776                 container_of(xprt, struct svcxprt_rdma, sc_xprt);
777         switch (event->event) {
778         case RDMA_CM_EVENT_ESTABLISHED:
779                 /* Accept complete */
780                 svc_xprt_get(xprt);
781                 dprintk("svcrdma: Connection completed on DTO xprt=%p, "
782                         "cm_id=%p\n", xprt, cma_id);
783                 clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
784                 svc_xprt_enqueue(xprt);
785                 break;
786         case RDMA_CM_EVENT_DISCONNECTED:
787                 dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
788                         xprt, cma_id);
789                 if (xprt) {
790                         set_bit(XPT_CLOSE, &xprt->xpt_flags);
791                         svc_xprt_enqueue(xprt);
792                         svc_xprt_put(xprt);
793                 }
794                 break;
795         case RDMA_CM_EVENT_DEVICE_REMOVAL:
796                 dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
797                         "event = %s (%d)\n", cma_id, xprt,
798                         rdma_event_msg(event->event), event->event);
799                 if (xprt) {
800                         set_bit(XPT_CLOSE, &xprt->xpt_flags);
801                         svc_xprt_enqueue(xprt);
802                         svc_xprt_put(xprt);
803                 }
804                 break;
805         default:
806                 dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
807                         "event = %s (%d)\n", cma_id,
808                         rdma_event_msg(event->event), event->event);
809                 break;
810         }
811         return 0;
812 }
813
814 /*
815  * Create a listening RDMA service endpoint.
816  */
817 static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
818                                         struct net *net,
819                                         struct sockaddr *sa, int salen,
820                                         int flags)
821 {
822         struct rdma_cm_id *listen_id;
823         struct svcxprt_rdma *cma_xprt;
824         int ret;
825
826         dprintk("svcrdma: Creating RDMA socket\n");
827         if ((sa->sa_family != AF_INET) && (sa->sa_family != AF_INET6)) {
828                 dprintk("svcrdma: Address family %d is not supported.\n", sa->sa_family);
829                 return ERR_PTR(-EAFNOSUPPORT);
830         }
831         cma_xprt = rdma_create_xprt(serv, 1);
832         if (!cma_xprt)
833                 return ERR_PTR(-ENOMEM);
834
835         listen_id = rdma_create_id(&init_net, rdma_listen_handler, cma_xprt,
836                                    RDMA_PS_TCP, IB_QPT_RC);
837         if (IS_ERR(listen_id)) {
838                 ret = PTR_ERR(listen_id);
839                 dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
840                 goto err0;
841         }
842
843         /* Allow both IPv4 and IPv6 sockets to bind a single port
844          * at the same time.
845          */
846 #if IS_ENABLED(CONFIG_IPV6)
847         ret = rdma_set_afonly(listen_id, 1);
848         if (ret) {
849                 dprintk("svcrdma: rdma_set_afonly failed = %d\n", ret);
850                 goto err1;
851         }
852 #endif
853         ret = rdma_bind_addr(listen_id, sa);
854         if (ret) {
855                 dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
856                 goto err1;
857         }
858         cma_xprt->sc_cm_id = listen_id;
859
860         ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
861         if (ret) {
862                 dprintk("svcrdma: rdma_listen failed = %d\n", ret);
863                 goto err1;
864         }
865
866         /*
867          * We need to use the address from the cm_id in case the
868          * caller specified 0 for the port number.
869          */
870         sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
871         svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
872
873         return &cma_xprt->sc_xprt;
874
875  err1:
876         rdma_destroy_id(listen_id);
877  err0:
878         kfree(cma_xprt);
879         return ERR_PTR(ret);
880 }
881
882 static struct svc_rdma_fastreg_mr *rdma_alloc_frmr(struct svcxprt_rdma *xprt)
883 {
884         struct ib_mr *mr;
885         struct scatterlist *sg;
886         struct svc_rdma_fastreg_mr *frmr;
887         u32 num_sg;
888
889         frmr = kmalloc(sizeof(*frmr), GFP_KERNEL);
890         if (!frmr)
891                 goto err;
892
893         num_sg = min_t(u32, RPCSVC_MAXPAGES, xprt->sc_frmr_pg_list_len);
894         mr = ib_alloc_mr(xprt->sc_pd, IB_MR_TYPE_MEM_REG, num_sg);
895         if (IS_ERR(mr))
896                 goto err_free_frmr;
897
898         sg = kcalloc(RPCSVC_MAXPAGES, sizeof(*sg), GFP_KERNEL);
899         if (!sg)
900                 goto err_free_mr;
901
902         sg_init_table(sg, RPCSVC_MAXPAGES);
903
904         frmr->mr = mr;
905         frmr->sg = sg;
906         INIT_LIST_HEAD(&frmr->frmr_list);
907         return frmr;
908
909  err_free_mr:
910         ib_dereg_mr(mr);
911  err_free_frmr:
912         kfree(frmr);
913  err:
914         return ERR_PTR(-ENOMEM);
915 }
916
917 static void rdma_dealloc_frmr_q(struct svcxprt_rdma *xprt)
918 {
919         struct svc_rdma_fastreg_mr *frmr;
920
921         while (!list_empty(&xprt->sc_frmr_q)) {
922                 frmr = list_entry(xprt->sc_frmr_q.next,
923                                   struct svc_rdma_fastreg_mr, frmr_list);
924                 list_del_init(&frmr->frmr_list);
925                 kfree(frmr->sg);
926                 ib_dereg_mr(frmr->mr);
927                 kfree(frmr);
928         }
929 }
930
931 struct svc_rdma_fastreg_mr *svc_rdma_get_frmr(struct svcxprt_rdma *rdma)
932 {
933         struct svc_rdma_fastreg_mr *frmr = NULL;
934
935         spin_lock(&rdma->sc_frmr_q_lock);
936         if (!list_empty(&rdma->sc_frmr_q)) {
937                 frmr = list_entry(rdma->sc_frmr_q.next,
938                                   struct svc_rdma_fastreg_mr, frmr_list);
939                 list_del_init(&frmr->frmr_list);
940                 frmr->sg_nents = 0;
941         }
942         spin_unlock(&rdma->sc_frmr_q_lock);
943         if (frmr)
944                 return frmr;
945
946         return rdma_alloc_frmr(rdma);
947 }
948
949 void svc_rdma_put_frmr(struct svcxprt_rdma *rdma,
950                        struct svc_rdma_fastreg_mr *frmr)
951 {
952         if (frmr) {
953                 ib_dma_unmap_sg(rdma->sc_cm_id->device,
954                                 frmr->sg, frmr->sg_nents, frmr->direction);
955                 spin_lock(&rdma->sc_frmr_q_lock);
956                 WARN_ON_ONCE(!list_empty(&frmr->frmr_list));
957                 list_add(&frmr->frmr_list, &rdma->sc_frmr_q);
958                 spin_unlock(&rdma->sc_frmr_q_lock);
959         }
960 }
961
962 /*
963  * This is the xpo_recvfrom function for listening endpoints. Its
964  * purpose is to accept incoming connections. The CMA callback handler
965  * has already created a new transport and attached it to the new CMA
966  * ID.
967  *
968  * There is a queue of pending connections hung on the listening
969  * transport. This queue contains the new svc_xprt structure. This
970  * function takes svc_xprt structures off the accept_q and completes
971  * the connection.
972  */
973 static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
974 {
975         struct svcxprt_rdma *listen_rdma;
976         struct svcxprt_rdma *newxprt = NULL;
977         struct rdma_conn_param conn_param;
978         struct rpcrdma_connect_private pmsg;
979         struct ib_qp_init_attr qp_attr;
980         struct ib_device *dev;
981         struct sockaddr *sap;
982         unsigned int i;
983         int ret = 0;
984
985         listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
986         clear_bit(XPT_CONN, &xprt->xpt_flags);
987         /* Get the next entry off the accept list */
988         spin_lock_bh(&listen_rdma->sc_lock);
989         if (!list_empty(&listen_rdma->sc_accept_q)) {
990                 newxprt = list_entry(listen_rdma->sc_accept_q.next,
991                                      struct svcxprt_rdma, sc_accept_q);
992                 list_del_init(&newxprt->sc_accept_q);
993         }
994         if (!list_empty(&listen_rdma->sc_accept_q))
995                 set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
996         spin_unlock_bh(&listen_rdma->sc_lock);
997         if (!newxprt)
998                 return NULL;
999
1000         dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
1001                 newxprt, newxprt->sc_cm_id);
1002
1003         dev = newxprt->sc_cm_id->device;
1004         newxprt->sc_port_num = newxprt->sc_cm_id->port_num;
1005
1006         /* Qualify the transport resource defaults with the
1007          * capabilities of this particular device */
1008         newxprt->sc_max_sge = min((size_t)dev->attrs.max_sge,
1009                                   (size_t)RPCSVC_MAXPAGES);
1010         newxprt->sc_max_sge_rd = min_t(size_t, dev->attrs.max_sge_rd,
1011                                        RPCSVC_MAXPAGES);
1012         newxprt->sc_max_req_size = svcrdma_max_req_size;
1013         newxprt->sc_max_requests = min_t(u32, dev->attrs.max_qp_wr,
1014                                          svcrdma_max_requests);
1015         newxprt->sc_fc_credits = cpu_to_be32(newxprt->sc_max_requests);
1016         newxprt->sc_max_bc_requests = min_t(u32, dev->attrs.max_qp_wr,
1017                                             svcrdma_max_bc_requests);
1018         newxprt->sc_rq_depth = newxprt->sc_max_requests +
1019                                newxprt->sc_max_bc_requests;
1020         newxprt->sc_sq_depth = newxprt->sc_rq_depth;
1021         atomic_set(&newxprt->sc_sq_avail, newxprt->sc_sq_depth);
1022
1023         if (!svc_rdma_prealloc_ctxts(newxprt))
1024                 goto errout;
1025         if (!svc_rdma_prealloc_maps(newxprt))
1026                 goto errout;
1027
1028         /*
1029          * Limit ORD based on client limit, local device limit, and
1030          * configured svcrdma limit.
1031          */
1032         newxprt->sc_ord = min_t(size_t, dev->attrs.max_qp_rd_atom, newxprt->sc_ord);
1033         newxprt->sc_ord = min_t(size_t, svcrdma_ord, newxprt->sc_ord);
1034
1035         newxprt->sc_pd = ib_alloc_pd(dev, 0);
1036         if (IS_ERR(newxprt->sc_pd)) {
1037                 dprintk("svcrdma: error creating PD for connect request\n");
1038                 goto errout;
1039         }
1040         newxprt->sc_sq_cq = ib_alloc_cq(dev, newxprt, newxprt->sc_sq_depth,
1041                                         0, IB_POLL_WORKQUEUE);
1042         if (IS_ERR(newxprt->sc_sq_cq)) {
1043                 dprintk("svcrdma: error creating SQ CQ for connect request\n");
1044                 goto errout;
1045         }
1046         newxprt->sc_rq_cq = ib_alloc_cq(dev, newxprt, newxprt->sc_rq_depth,
1047                                         0, IB_POLL_WORKQUEUE);
1048         if (IS_ERR(newxprt->sc_rq_cq)) {
1049                 dprintk("svcrdma: error creating RQ CQ for connect request\n");
1050                 goto errout;
1051         }
1052
1053         memset(&qp_attr, 0, sizeof qp_attr);
1054         qp_attr.event_handler = qp_event_handler;
1055         qp_attr.qp_context = &newxprt->sc_xprt;
1056         qp_attr.cap.max_send_wr = newxprt->sc_sq_depth;
1057         qp_attr.cap.max_recv_wr = newxprt->sc_rq_depth;
1058         qp_attr.cap.max_send_sge = newxprt->sc_max_sge;
1059         qp_attr.cap.max_recv_sge = newxprt->sc_max_sge;
1060         qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
1061         qp_attr.qp_type = IB_QPT_RC;
1062         qp_attr.send_cq = newxprt->sc_sq_cq;
1063         qp_attr.recv_cq = newxprt->sc_rq_cq;
1064         dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n",
1065                 newxprt->sc_cm_id, newxprt->sc_pd);
1066         dprintk("    cap.max_send_wr = %d, cap.max_recv_wr = %d\n",
1067                 qp_attr.cap.max_send_wr, qp_attr.cap.max_recv_wr);
1068         dprintk("    cap.max_send_sge = %d, cap.max_recv_sge = %d\n",
1069                 qp_attr.cap.max_send_sge, qp_attr.cap.max_recv_sge);
1070
1071         ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
1072         if (ret) {
1073                 dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
1074                 goto errout;
1075         }
1076         newxprt->sc_qp = newxprt->sc_cm_id->qp;
1077
1078         /*
1079          * Use the most secure set of MR resources based on the
1080          * transport type and available memory management features in
1081          * the device. Here's the table implemented below:
1082          *
1083          *              Fast    Global  DMA     Remote WR
1084          *              Reg     LKEY    MR      Access
1085          *              Sup'd   Sup'd   Needed  Needed
1086          *
1087          * IWARP        N       N       Y       Y
1088          *              N       Y       Y       Y
1089          *              Y       N       Y       N
1090          *              Y       Y       N       -
1091          *
1092          * IB           N       N       Y       N
1093          *              N       Y       N       -
1094          *              Y       N       Y       N
1095          *              Y       Y       N       -
1096          *
1097          * NB:  iWARP requires remote write access for the data sink
1098          *      of an RDMA_READ. IB does not.
1099          */
1100         newxprt->sc_reader = rdma_read_chunk_lcl;
1101         if (dev->attrs.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
1102                 newxprt->sc_frmr_pg_list_len =
1103                         dev->attrs.max_fast_reg_page_list_len;
1104                 newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_FAST_REG;
1105                 newxprt->sc_reader = rdma_read_chunk_frmr;
1106         } else
1107                 newxprt->sc_snd_w_inv = false;
1108
1109         /*
1110          * Determine if a DMA MR is required and if so, what privs are required
1111          */
1112         if (!rdma_protocol_iwarp(dev, newxprt->sc_cm_id->port_num) &&
1113             !rdma_ib_or_roce(dev, newxprt->sc_cm_id->port_num))
1114                 goto errout;
1115
1116         if (rdma_protocol_iwarp(dev, newxprt->sc_cm_id->port_num))
1117                 newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_READ_W_INV;
1118
1119         /* Post receive buffers */
1120         for (i = 0; i < newxprt->sc_max_requests; i++) {
1121                 ret = svc_rdma_post_recv(newxprt, GFP_KERNEL);
1122                 if (ret) {
1123                         dprintk("svcrdma: failure posting receive buffers\n");
1124                         goto errout;
1125                 }
1126         }
1127
1128         /* Swap out the handler */
1129         newxprt->sc_cm_id->event_handler = rdma_cma_handler;
1130
1131         /* Construct RDMA-CM private message */
1132         pmsg.cp_magic = rpcrdma_cmp_magic;
1133         pmsg.cp_version = RPCRDMA_CMP_VERSION;
1134         pmsg.cp_flags = 0;
1135         pmsg.cp_send_size = pmsg.cp_recv_size =
1136                 rpcrdma_encode_buffer_size(newxprt->sc_max_req_size);
1137
1138         /* Accept Connection */
1139         set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
1140         memset(&conn_param, 0, sizeof conn_param);
1141         conn_param.responder_resources = 0;
1142         conn_param.initiator_depth = newxprt->sc_ord;
1143         conn_param.private_data = &pmsg;
1144         conn_param.private_data_len = sizeof(pmsg);
1145         ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
1146         if (ret) {
1147                 dprintk("svcrdma: failed to accept new connection, ret=%d\n",
1148                        ret);
1149                 goto errout;
1150         }
1151
1152         dprintk("svcrdma: new connection %p accepted:\n", newxprt);
1153         sap = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
1154         dprintk("    local address   : %pIS:%u\n", sap, rpc_get_port(sap));
1155         sap = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
1156         dprintk("    remote address  : %pIS:%u\n", sap, rpc_get_port(sap));
1157         dprintk("    max_sge         : %d\n", newxprt->sc_max_sge);
1158         dprintk("    max_sge_rd      : %d\n", newxprt->sc_max_sge_rd);
1159         dprintk("    sq_depth        : %d\n", newxprt->sc_sq_depth);
1160         dprintk("    max_requests    : %d\n", newxprt->sc_max_requests);
1161         dprintk("    ord             : %d\n", newxprt->sc_ord);
1162
1163         return &newxprt->sc_xprt;
1164
1165  errout:
1166         dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
1167         /* Take a reference in case the DTO handler runs */
1168         svc_xprt_get(&newxprt->sc_xprt);
1169         if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
1170                 ib_destroy_qp(newxprt->sc_qp);
1171         rdma_destroy_id(newxprt->sc_cm_id);
1172         /* This call to put will destroy the transport */
1173         svc_xprt_put(&newxprt->sc_xprt);
1174         return NULL;
1175 }
1176
1177 static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
1178 {
1179 }
1180
1181 /*
1182  * When connected, an svc_xprt has at least two references:
1183  *
1184  * - A reference held by the cm_id between the ESTABLISHED and
1185  *   DISCONNECTED events. If the remote peer disconnected first, this
1186  *   reference could be gone.
1187  *
1188  * - A reference held by the svc_recv code that called this function
1189  *   as part of close processing.
1190  *
1191  * At a minimum one references should still be held.
1192  */
1193 static void svc_rdma_detach(struct svc_xprt *xprt)
1194 {
1195         struct svcxprt_rdma *rdma =
1196                 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1197         dprintk("svc: svc_rdma_detach(%p)\n", xprt);
1198
1199         /* Disconnect and flush posted WQE */
1200         rdma_disconnect(rdma->sc_cm_id);
1201 }
1202
1203 static void __svc_rdma_free(struct work_struct *work)
1204 {
1205         struct svcxprt_rdma *rdma =
1206                 container_of(work, struct svcxprt_rdma, sc_work);
1207         struct svc_xprt *xprt = &rdma->sc_xprt;
1208
1209         dprintk("svcrdma: %s(%p)\n", __func__, rdma);
1210
1211         if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
1212                 ib_drain_qp(rdma->sc_qp);
1213
1214         /* We should only be called from kref_put */
1215         if (kref_read(&xprt->xpt_ref) != 0)
1216                 pr_err("svcrdma: sc_xprt still in use? (%d)\n",
1217                        kref_read(&xprt->xpt_ref));
1218
1219         /*
1220          * Destroy queued, but not processed read completions. Note
1221          * that this cleanup has to be done before destroying the
1222          * cm_id because the device ptr is needed to unmap the dma in
1223          * svc_rdma_put_context.
1224          */
1225         while (!list_empty(&rdma->sc_read_complete_q)) {
1226                 struct svc_rdma_op_ctxt *ctxt;
1227                 ctxt = list_first_entry(&rdma->sc_read_complete_q,
1228                                         struct svc_rdma_op_ctxt, list);
1229                 list_del(&ctxt->list);
1230                 svc_rdma_put_context(ctxt, 1);
1231         }
1232
1233         /* Destroy queued, but not processed recv completions */
1234         while (!list_empty(&rdma->sc_rq_dto_q)) {
1235                 struct svc_rdma_op_ctxt *ctxt;
1236                 ctxt = list_first_entry(&rdma->sc_rq_dto_q,
1237                                         struct svc_rdma_op_ctxt, list);
1238                 list_del(&ctxt->list);
1239                 svc_rdma_put_context(ctxt, 1);
1240         }
1241
1242         /* Warn if we leaked a resource or under-referenced */
1243         if (rdma->sc_ctxt_used != 0)
1244                 pr_err("svcrdma: ctxt still in use? (%d)\n",
1245                        rdma->sc_ctxt_used);
1246
1247         /* Final put of backchannel client transport */
1248         if (xprt->xpt_bc_xprt) {
1249                 xprt_put(xprt->xpt_bc_xprt);
1250                 xprt->xpt_bc_xprt = NULL;
1251         }
1252
1253         rdma_dealloc_frmr_q(rdma);
1254         svc_rdma_destroy_rw_ctxts(rdma);
1255         svc_rdma_destroy_ctxts(rdma);
1256         svc_rdma_destroy_maps(rdma);
1257
1258         /* Destroy the QP if present (not a listener) */
1259         if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
1260                 ib_destroy_qp(rdma->sc_qp);
1261
1262         if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
1263                 ib_free_cq(rdma->sc_sq_cq);
1264
1265         if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
1266                 ib_free_cq(rdma->sc_rq_cq);
1267
1268         if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
1269                 ib_dealloc_pd(rdma->sc_pd);
1270
1271         /* Destroy the CM ID */
1272         rdma_destroy_id(rdma->sc_cm_id);
1273
1274         kfree(rdma);
1275 }
1276
1277 static void svc_rdma_free(struct svc_xprt *xprt)
1278 {
1279         struct svcxprt_rdma *rdma =
1280                 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1281         INIT_WORK(&rdma->sc_work, __svc_rdma_free);
1282         queue_work(svc_rdma_wq, &rdma->sc_work);
1283 }
1284
1285 static int svc_rdma_has_wspace(struct svc_xprt *xprt)
1286 {
1287         struct svcxprt_rdma *rdma =
1288                 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1289
1290         /*
1291          * If there are already waiters on the SQ,
1292          * return false.
1293          */
1294         if (waitqueue_active(&rdma->sc_send_wait))
1295                 return 0;
1296
1297         /* Otherwise return true. */
1298         return 1;
1299 }
1300
1301 static int svc_rdma_secure_port(struct svc_rqst *rqstp)
1302 {
1303         return 1;
1304 }
1305
1306 static void svc_rdma_kill_temp_xprt(struct svc_xprt *xprt)
1307 {
1308 }
1309
1310 int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr)
1311 {
1312         struct ib_send_wr *bad_wr, *n_wr;
1313         int wr_count;
1314         int i;
1315         int ret;
1316
1317         if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1318                 return -ENOTCONN;
1319
1320         wr_count = 1;
1321         for (n_wr = wr->next; n_wr; n_wr = n_wr->next)
1322                 wr_count++;
1323
1324         /* If the SQ is full, wait until an SQ entry is available */
1325         while (1) {
1326                 if ((atomic_sub_return(wr_count, &xprt->sc_sq_avail) < 0)) {
1327                         atomic_inc(&rdma_stat_sq_starve);
1328
1329                         /* Wait until SQ WR available if SQ still full */
1330                         atomic_add(wr_count, &xprt->sc_sq_avail);
1331                         wait_event(xprt->sc_send_wait,
1332                                    atomic_read(&xprt->sc_sq_avail) > wr_count);
1333                         if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1334                                 return -ENOTCONN;
1335                         continue;
1336                 }
1337                 /* Take a transport ref for each WR posted */
1338                 for (i = 0; i < wr_count; i++)
1339                         svc_xprt_get(&xprt->sc_xprt);
1340
1341                 /* Bump used SQ WR count and post */
1342                 ret = ib_post_send(xprt->sc_qp, wr, &bad_wr);
1343                 if (ret) {
1344                         set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
1345                         for (i = 0; i < wr_count; i ++)
1346                                 svc_xprt_put(&xprt->sc_xprt);
1347                         dprintk("svcrdma: failed to post SQ WR rc=%d\n", ret);
1348                         dprintk("    sc_sq_avail=%d, sc_sq_depth=%d\n",
1349                                 atomic_read(&xprt->sc_sq_avail),
1350                                 xprt->sc_sq_depth);
1351                         wake_up(&xprt->sc_send_wait);
1352                 }
1353                 break;
1354         }
1355         return ret;
1356 }