powerpc/powernv: Report size of OPAL memcons log
[linux/fpc-iii.git] / net / sunrpc / xprtrdma / svc_rdma_transport.c
blobca2799af05a6846980e2dfa2bd7c58c655a6f069
1 /*
2 * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved.
3 * Copyright (c) 2005-2007 Network Appliance, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
15 * Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
40 * Author: Tom Tucker <tom@opengridcomputing.com>
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"
58 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
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 *);
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,
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,
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 *);
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,
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)
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)
121 struct svcxprt_rdma *cma_xprt;
122 struct svc_xprt *xprt;
124 cma_xprt = rdma_create_xprt(serv, 0);
125 if (!cma_xprt)
126 return ERR_PTR(-ENOMEM);
127 xprt = &cma_xprt->sc_xprt;
129 svc_xprt_init(net, &svc_rdma_bc_class, xprt, serv);
130 serv->sv_bc_xprt = xprt;
132 dprintk("svcrdma: %s(%p)\n", __func__, xprt);
133 return xprt;
136 static void svc_rdma_bc_detach(struct svc_xprt *xprt)
138 dprintk("svcrdma: %s(%p)\n", __func__, xprt);
141 static void svc_rdma_bc_free(struct svc_xprt *xprt)
143 struct svcxprt_rdma *rdma =
144 container_of(xprt, struct svcxprt_rdma, sc_xprt);
146 dprintk("svcrdma: %s(%p)\n", __func__, xprt);
147 if (xprt)
148 kfree(rdma);
150 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
152 static struct svc_rdma_op_ctxt *alloc_ctxt(struct svcxprt_rdma *xprt,
153 gfp_t flags)
155 struct svc_rdma_op_ctxt *ctxt;
157 ctxt = kmalloc(sizeof(*ctxt), flags);
158 if (ctxt) {
159 ctxt->xprt = xprt;
160 INIT_LIST_HEAD(&ctxt->free);
161 INIT_LIST_HEAD(&ctxt->dto_q);
163 return ctxt;
166 static bool svc_rdma_prealloc_ctxts(struct svcxprt_rdma *xprt)
168 unsigned int i;
170 /* Each RPC/RDMA credit can consume a number of send
171 * and receive WQEs. One ctxt is allocated for each.
173 i = xprt->sc_sq_depth + xprt->sc_rq_depth;
175 while (i--) {
176 struct svc_rdma_op_ctxt *ctxt;
178 ctxt = alloc_ctxt(xprt, GFP_KERNEL);
179 if (!ctxt) {
180 dprintk("svcrdma: No memory for RDMA ctxt\n");
181 return false;
183 list_add(&ctxt->free, &xprt->sc_ctxts);
185 return true;
188 struct svc_rdma_op_ctxt *svc_rdma_get_context(struct svcxprt_rdma *xprt)
190 struct svc_rdma_op_ctxt *ctxt = NULL;
192 spin_lock_bh(&xprt->sc_ctxt_lock);
193 xprt->sc_ctxt_used++;
194 if (list_empty(&xprt->sc_ctxts))
195 goto out_empty;
197 ctxt = list_first_entry(&xprt->sc_ctxts,
198 struct svc_rdma_op_ctxt, free);
199 list_del_init(&ctxt->free);
200 spin_unlock_bh(&xprt->sc_ctxt_lock);
202 out:
203 ctxt->count = 0;
204 ctxt->mapped_sges = 0;
205 ctxt->frmr = NULL;
206 return ctxt;
208 out_empty:
209 /* Either pre-allocation missed the mark, or send
210 * queue accounting is broken.
212 spin_unlock_bh(&xprt->sc_ctxt_lock);
214 ctxt = alloc_ctxt(xprt, GFP_NOIO);
215 if (ctxt)
216 goto out;
218 spin_lock_bh(&xprt->sc_ctxt_lock);
219 xprt->sc_ctxt_used--;
220 spin_unlock_bh(&xprt->sc_ctxt_lock);
221 WARN_ONCE(1, "svcrdma: empty RDMA ctxt list?\n");
222 return NULL;
225 void svc_rdma_unmap_dma(struct svc_rdma_op_ctxt *ctxt)
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;
232 for (i = 0; i < ctxt->mapped_sges; i++) {
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.
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);
245 ctxt->mapped_sges = 0;
248 void svc_rdma_put_context(struct svc_rdma_op_ctxt *ctxt, int free_pages)
250 struct svcxprt_rdma *xprt = ctxt->xprt;
251 int i;
253 if (free_pages)
254 for (i = 0; i < ctxt->count; i++)
255 put_page(ctxt->pages[i]);
257 spin_lock_bh(&xprt->sc_ctxt_lock);
258 xprt->sc_ctxt_used--;
259 list_add(&ctxt->free, &xprt->sc_ctxts);
260 spin_unlock_bh(&xprt->sc_ctxt_lock);
263 static void svc_rdma_destroy_ctxts(struct svcxprt_rdma *xprt)
265 while (!list_empty(&xprt->sc_ctxts)) {
266 struct svc_rdma_op_ctxt *ctxt;
268 ctxt = list_first_entry(&xprt->sc_ctxts,
269 struct svc_rdma_op_ctxt, free);
270 list_del(&ctxt->free);
271 kfree(ctxt);
275 static struct svc_rdma_req_map *alloc_req_map(gfp_t flags)
277 struct svc_rdma_req_map *map;
279 map = kmalloc(sizeof(*map), flags);
280 if (map)
281 INIT_LIST_HEAD(&map->free);
282 return map;
285 static bool svc_rdma_prealloc_maps(struct svcxprt_rdma *xprt)
287 unsigned int i;
289 /* One for each receive buffer on this connection. */
290 i = xprt->sc_max_requests;
292 while (i--) {
293 struct svc_rdma_req_map *map;
295 map = alloc_req_map(GFP_KERNEL);
296 if (!map) {
297 dprintk("svcrdma: No memory for request map\n");
298 return false;
300 list_add(&map->free, &xprt->sc_maps);
302 return true;
305 struct svc_rdma_req_map *svc_rdma_get_req_map(struct svcxprt_rdma *xprt)
307 struct svc_rdma_req_map *map = NULL;
309 spin_lock(&xprt->sc_map_lock);
310 if (list_empty(&xprt->sc_maps))
311 goto out_empty;
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);
318 out:
319 map->count = 0;
320 return map;
322 out_empty:
323 spin_unlock(&xprt->sc_map_lock);
325 /* Pre-allocation amount was incorrect */
326 map = alloc_req_map(GFP_NOIO);
327 if (map)
328 goto out;
330 WARN_ONCE(1, "svcrdma: empty request map list?\n");
331 return NULL;
334 void svc_rdma_put_req_map(struct svcxprt_rdma *xprt,
335 struct svc_rdma_req_map *map)
337 spin_lock(&xprt->sc_map_lock);
338 list_add(&map->free, &xprt->sc_maps);
339 spin_unlock(&xprt->sc_map_lock);
342 static void svc_rdma_destroy_maps(struct svcxprt_rdma *xprt)
344 while (!list_empty(&xprt->sc_maps)) {
345 struct svc_rdma_req_map *map;
347 map = list_first_entry(&xprt->sc_maps,
348 struct svc_rdma_req_map, free);
349 list_del(&map->free);
350 kfree(map);
354 /* QP event handler */
355 static void qp_event_handler(struct ib_event *event, void *context)
357 struct svc_xprt *xprt = context;
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;
386 * svc_rdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
387 * @cq: completion queue
388 * @wc: completed WR
391 static void svc_rdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
393 struct svcxprt_rdma *xprt = cq->cq_context;
394 struct ib_cqe *cqe = wc->wr_cqe;
395 struct svc_rdma_op_ctxt *ctxt;
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);
401 if (wc->status != IB_WC_SUCCESS)
402 goto flushed;
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->dto_q, &xprt->sc_rq_dto_q);
408 spin_unlock(&xprt->sc_rq_dto_lock);
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;
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);
424 out:
425 svc_xprt_put(&xprt->sc_xprt);
428 static void svc_rdma_send_wc_common(struct svcxprt_rdma *xprt,
429 struct ib_wc *wc,
430 const char *opname)
432 if (wc->status != IB_WC_SUCCESS)
433 goto err;
435 out:
436 atomic_inc(&xprt->sc_sq_avail);
437 wake_up(&xprt->sc_send_wait);
438 return;
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;
449 static void svc_rdma_send_wc_common_put(struct ib_cq *cq, struct ib_wc *wc,
450 const char *opname)
452 struct svcxprt_rdma *xprt = cq->cq_context;
454 svc_rdma_send_wc_common(xprt, wc, opname);
455 svc_xprt_put(&xprt->sc_xprt);
459 * svc_rdma_wc_send - Invoked by RDMA provider for each polled Send WC
460 * @cq: completion queue
461 * @wc: completed WR
464 void svc_rdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
466 struct ib_cqe *cqe = wc->wr_cqe;
467 struct svc_rdma_op_ctxt *ctxt;
469 svc_rdma_send_wc_common_put(cq, wc, "send");
471 ctxt = container_of(cqe, struct svc_rdma_op_ctxt, cqe);
472 svc_rdma_unmap_dma(ctxt);
473 svc_rdma_put_context(ctxt, 1);
477 * svc_rdma_wc_write - Invoked by RDMA provider for each polled Write WC
478 * @cq: completion queue
479 * @wc: completed WR
482 void svc_rdma_wc_write(struct ib_cq *cq, struct ib_wc *wc)
484 struct ib_cqe *cqe = wc->wr_cqe;
485 struct svc_rdma_op_ctxt *ctxt;
487 svc_rdma_send_wc_common_put(cq, wc, "write");
489 ctxt = container_of(cqe, struct svc_rdma_op_ctxt, cqe);
490 svc_rdma_unmap_dma(ctxt);
491 svc_rdma_put_context(ctxt, 0);
495 * svc_rdma_wc_reg - Invoked by RDMA provider for each polled FASTREG WC
496 * @cq: completion queue
497 * @wc: completed WR
500 void svc_rdma_wc_reg(struct ib_cq *cq, struct ib_wc *wc)
502 svc_rdma_send_wc_common_put(cq, wc, "fastreg");
506 * svc_rdma_wc_read - Invoked by RDMA provider for each polled Read WC
507 * @cq: completion queue
508 * @wc: completed WR
511 void svc_rdma_wc_read(struct ib_cq *cq, struct ib_wc *wc)
513 struct svcxprt_rdma *xprt = cq->cq_context;
514 struct ib_cqe *cqe = wc->wr_cqe;
515 struct svc_rdma_op_ctxt *ctxt;
517 svc_rdma_send_wc_common(xprt, wc, "read");
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);
523 if (test_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags)) {
524 struct svc_rdma_op_ctxt *read_hdr;
526 read_hdr = ctxt->read_hdr;
527 spin_lock(&xprt->sc_rq_dto_lock);
528 list_add_tail(&read_hdr->dto_q,
529 &xprt->sc_read_complete_q);
530 spin_unlock(&xprt->sc_rq_dto_lock);
532 set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
533 svc_xprt_enqueue(&xprt->sc_xprt);
536 svc_rdma_put_context(ctxt, 0);
537 svc_xprt_put(&xprt->sc_xprt);
541 * svc_rdma_wc_inv - Invoked by RDMA provider for each polled LOCAL_INV WC
542 * @cq: completion queue
543 * @wc: completed WR
546 void svc_rdma_wc_inv(struct ib_cq *cq, struct ib_wc *wc)
548 svc_rdma_send_wc_common_put(cq, wc, "localInv");
551 static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *serv,
552 int listener)
554 struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
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_dto_q);
561 INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
562 INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
563 INIT_LIST_HEAD(&cma_xprt->sc_frmr_q);
564 INIT_LIST_HEAD(&cma_xprt->sc_ctxts);
565 INIT_LIST_HEAD(&cma_xprt->sc_maps);
566 init_waitqueue_head(&cma_xprt->sc_send_wait);
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_map_lock);
574 if (listener)
575 set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
577 return cma_xprt;
580 int svc_rdma_post_recv(struct svcxprt_rdma *xprt, gfp_t flags)
582 struct ib_recv_wr recv_wr, *bad_recv_wr;
583 struct svc_rdma_op_ctxt *ctxt;
584 struct page *page;
585 dma_addr_t pa;
586 int sge_no;
587 int buflen;
588 int ret;
590 ctxt = svc_rdma_get_context(xprt);
591 buflen = 0;
592 ctxt->direction = DMA_FROM_DEVICE;
593 ctxt->cqe.done = svc_rdma_wc_receive;
594 for (sge_no = 0; buflen < xprt->sc_max_req_size; sge_no++) {
595 if (sge_no >= xprt->sc_max_sge) {
596 pr_err("svcrdma: Too many sges (%d)\n", sge_no);
597 goto err_put_ctxt;
599 page = alloc_page(flags);
600 if (!page)
601 goto err_put_ctxt;
602 ctxt->pages[sge_no] = page;
603 pa = ib_dma_map_page(xprt->sc_cm_id->device,
604 page, 0, PAGE_SIZE,
605 DMA_FROM_DEVICE);
606 if (ib_dma_mapping_error(xprt->sc_cm_id->device, pa))
607 goto err_put_ctxt;
608 svc_rdma_count_mappings(xprt, ctxt);
609 ctxt->sge[sge_no].addr = pa;
610 ctxt->sge[sge_no].length = PAGE_SIZE;
611 ctxt->sge[sge_no].lkey = xprt->sc_pd->local_dma_lkey;
612 ctxt->count = sge_no + 1;
613 buflen += PAGE_SIZE;
615 recv_wr.next = NULL;
616 recv_wr.sg_list = &ctxt->sge[0];
617 recv_wr.num_sge = ctxt->count;
618 recv_wr.wr_cqe = &ctxt->cqe;
620 svc_xprt_get(&xprt->sc_xprt);
621 ret = ib_post_recv(xprt->sc_qp, &recv_wr, &bad_recv_wr);
622 if (ret) {
623 svc_rdma_unmap_dma(ctxt);
624 svc_rdma_put_context(ctxt, 1);
625 svc_xprt_put(&xprt->sc_xprt);
627 return ret;
629 err_put_ctxt:
630 svc_rdma_unmap_dma(ctxt);
631 svc_rdma_put_context(ctxt, 1);
632 return -ENOMEM;
635 int svc_rdma_repost_recv(struct svcxprt_rdma *xprt, gfp_t flags)
637 int ret = 0;
639 ret = svc_rdma_post_recv(xprt, flags);
640 if (ret) {
641 pr_err("svcrdma: could not post a receive buffer, err=%d.\n",
642 ret);
643 pr_err("svcrdma: closing transport %p.\n", xprt);
644 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
645 ret = -ENOTCONN;
647 return ret;
650 static void
651 svc_rdma_parse_connect_private(struct svcxprt_rdma *newxprt,
652 struct rdma_conn_param *param)
654 const struct rpcrdma_connect_private *pmsg = param->private_data;
656 if (pmsg &&
657 pmsg->cp_magic == rpcrdma_cmp_magic &&
658 pmsg->cp_version == RPCRDMA_CMP_VERSION) {
659 newxprt->sc_snd_w_inv = pmsg->cp_flags &
660 RPCRDMA_CMP_F_SND_W_INV_OK;
662 dprintk("svcrdma: client send_size %u, recv_size %u "
663 "remote inv %ssupported\n",
664 rpcrdma_decode_buffer_size(pmsg->cp_send_size),
665 rpcrdma_decode_buffer_size(pmsg->cp_recv_size),
666 newxprt->sc_snd_w_inv ? "" : "un");
671 * This function handles the CONNECT_REQUEST event on a listening
672 * endpoint. It is passed the cma_id for the _new_ connection. The context in
673 * this cma_id is inherited from the listening cma_id and is the svc_xprt
674 * structure for the listening endpoint.
676 * This function creates a new xprt for the new connection and enqueues it on
677 * the accept queue for the listent xprt. When the listen thread is kicked, it
678 * will call the recvfrom method on the listen xprt which will accept the new
679 * connection.
681 static void handle_connect_req(struct rdma_cm_id *new_cma_id,
682 struct rdma_conn_param *param)
684 struct svcxprt_rdma *listen_xprt = new_cma_id->context;
685 struct svcxprt_rdma *newxprt;
686 struct sockaddr *sa;
688 /* Create a new transport */
689 newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0);
690 if (!newxprt) {
691 dprintk("svcrdma: failed to create new transport\n");
692 return;
694 newxprt->sc_cm_id = new_cma_id;
695 new_cma_id->context = newxprt;
696 dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
697 newxprt, newxprt->sc_cm_id, listen_xprt);
698 svc_rdma_parse_connect_private(newxprt, param);
700 /* Save client advertised inbound read limit for use later in accept. */
701 newxprt->sc_ord = param->initiator_depth;
703 /* Set the local and remote addresses in the transport */
704 sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
705 svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
706 sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
707 svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
710 * Enqueue the new transport on the accept queue of the listening
711 * transport
713 spin_lock_bh(&listen_xprt->sc_lock);
714 list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
715 spin_unlock_bh(&listen_xprt->sc_lock);
717 set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
718 svc_xprt_enqueue(&listen_xprt->sc_xprt);
722 * Handles events generated on the listening endpoint. These events will be
723 * either be incoming connect requests or adapter removal events.
725 static int rdma_listen_handler(struct rdma_cm_id *cma_id,
726 struct rdma_cm_event *event)
728 struct svcxprt_rdma *xprt = cma_id->context;
729 int ret = 0;
731 switch (event->event) {
732 case RDMA_CM_EVENT_CONNECT_REQUEST:
733 dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
734 "event = %s (%d)\n", cma_id, cma_id->context,
735 rdma_event_msg(event->event), event->event);
736 handle_connect_req(cma_id, &event->param.conn);
737 break;
739 case RDMA_CM_EVENT_ESTABLISHED:
740 /* Accept complete */
741 dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
742 "cm_id=%p\n", xprt, cma_id);
743 break;
745 case RDMA_CM_EVENT_DEVICE_REMOVAL:
746 dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
747 xprt, cma_id);
748 if (xprt)
749 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
750 break;
752 default:
753 dprintk("svcrdma: Unexpected event on listening endpoint %p, "
754 "event = %s (%d)\n", cma_id,
755 rdma_event_msg(event->event), event->event);
756 break;
759 return ret;
762 static int rdma_cma_handler(struct rdma_cm_id *cma_id,
763 struct rdma_cm_event *event)
765 struct svc_xprt *xprt = cma_id->context;
766 struct svcxprt_rdma *rdma =
767 container_of(xprt, struct svcxprt_rdma, sc_xprt);
768 switch (event->event) {
769 case RDMA_CM_EVENT_ESTABLISHED:
770 /* Accept complete */
771 svc_xprt_get(xprt);
772 dprintk("svcrdma: Connection completed on DTO xprt=%p, "
773 "cm_id=%p\n", xprt, cma_id);
774 clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
775 svc_xprt_enqueue(xprt);
776 break;
777 case RDMA_CM_EVENT_DISCONNECTED:
778 dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
779 xprt, cma_id);
780 if (xprt) {
781 set_bit(XPT_CLOSE, &xprt->xpt_flags);
782 svc_xprt_enqueue(xprt);
783 svc_xprt_put(xprt);
785 break;
786 case RDMA_CM_EVENT_DEVICE_REMOVAL:
787 dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
788 "event = %s (%d)\n", cma_id, xprt,
789 rdma_event_msg(event->event), event->event);
790 if (xprt) {
791 set_bit(XPT_CLOSE, &xprt->xpt_flags);
792 svc_xprt_enqueue(xprt);
793 svc_xprt_put(xprt);
795 break;
796 default:
797 dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
798 "event = %s (%d)\n", cma_id,
799 rdma_event_msg(event->event), event->event);
800 break;
802 return 0;
806 * Create a listening RDMA service endpoint.
808 static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
809 struct net *net,
810 struct sockaddr *sa, int salen,
811 int flags)
813 struct rdma_cm_id *listen_id;
814 struct svcxprt_rdma *cma_xprt;
815 int ret;
817 dprintk("svcrdma: Creating RDMA socket\n");
818 if ((sa->sa_family != AF_INET) && (sa->sa_family != AF_INET6)) {
819 dprintk("svcrdma: Address family %d is not supported.\n", sa->sa_family);
820 return ERR_PTR(-EAFNOSUPPORT);
822 cma_xprt = rdma_create_xprt(serv, 1);
823 if (!cma_xprt)
824 return ERR_PTR(-ENOMEM);
826 listen_id = rdma_create_id(&init_net, rdma_listen_handler, cma_xprt,
827 RDMA_PS_TCP, IB_QPT_RC);
828 if (IS_ERR(listen_id)) {
829 ret = PTR_ERR(listen_id);
830 dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
831 goto err0;
834 /* Allow both IPv4 and IPv6 sockets to bind a single port
835 * at the same time.
837 #if IS_ENABLED(CONFIG_IPV6)
838 ret = rdma_set_afonly(listen_id, 1);
839 if (ret) {
840 dprintk("svcrdma: rdma_set_afonly failed = %d\n", ret);
841 goto err1;
843 #endif
844 ret = rdma_bind_addr(listen_id, sa);
845 if (ret) {
846 dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
847 goto err1;
849 cma_xprt->sc_cm_id = listen_id;
851 ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
852 if (ret) {
853 dprintk("svcrdma: rdma_listen failed = %d\n", ret);
854 goto err1;
858 * We need to use the address from the cm_id in case the
859 * caller specified 0 for the port number.
861 sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
862 svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
864 return &cma_xprt->sc_xprt;
866 err1:
867 rdma_destroy_id(listen_id);
868 err0:
869 kfree(cma_xprt);
870 return ERR_PTR(ret);
873 static struct svc_rdma_fastreg_mr *rdma_alloc_frmr(struct svcxprt_rdma *xprt)
875 struct ib_mr *mr;
876 struct scatterlist *sg;
877 struct svc_rdma_fastreg_mr *frmr;
878 u32 num_sg;
880 frmr = kmalloc(sizeof(*frmr), GFP_KERNEL);
881 if (!frmr)
882 goto err;
884 num_sg = min_t(u32, RPCSVC_MAXPAGES, xprt->sc_frmr_pg_list_len);
885 mr = ib_alloc_mr(xprt->sc_pd, IB_MR_TYPE_MEM_REG, num_sg);
886 if (IS_ERR(mr))
887 goto err_free_frmr;
889 sg = kcalloc(RPCSVC_MAXPAGES, sizeof(*sg), GFP_KERNEL);
890 if (!sg)
891 goto err_free_mr;
893 sg_init_table(sg, RPCSVC_MAXPAGES);
895 frmr->mr = mr;
896 frmr->sg = sg;
897 INIT_LIST_HEAD(&frmr->frmr_list);
898 return frmr;
900 err_free_mr:
901 ib_dereg_mr(mr);
902 err_free_frmr:
903 kfree(frmr);
904 err:
905 return ERR_PTR(-ENOMEM);
908 static void rdma_dealloc_frmr_q(struct svcxprt_rdma *xprt)
910 struct svc_rdma_fastreg_mr *frmr;
912 while (!list_empty(&xprt->sc_frmr_q)) {
913 frmr = list_entry(xprt->sc_frmr_q.next,
914 struct svc_rdma_fastreg_mr, frmr_list);
915 list_del_init(&frmr->frmr_list);
916 kfree(frmr->sg);
917 ib_dereg_mr(frmr->mr);
918 kfree(frmr);
922 struct svc_rdma_fastreg_mr *svc_rdma_get_frmr(struct svcxprt_rdma *rdma)
924 struct svc_rdma_fastreg_mr *frmr = NULL;
926 spin_lock_bh(&rdma->sc_frmr_q_lock);
927 if (!list_empty(&rdma->sc_frmr_q)) {
928 frmr = list_entry(rdma->sc_frmr_q.next,
929 struct svc_rdma_fastreg_mr, frmr_list);
930 list_del_init(&frmr->frmr_list);
931 frmr->sg_nents = 0;
933 spin_unlock_bh(&rdma->sc_frmr_q_lock);
934 if (frmr)
935 return frmr;
937 return rdma_alloc_frmr(rdma);
940 void svc_rdma_put_frmr(struct svcxprt_rdma *rdma,
941 struct svc_rdma_fastreg_mr *frmr)
943 if (frmr) {
944 ib_dma_unmap_sg(rdma->sc_cm_id->device,
945 frmr->sg, frmr->sg_nents, frmr->direction);
946 spin_lock_bh(&rdma->sc_frmr_q_lock);
947 WARN_ON_ONCE(!list_empty(&frmr->frmr_list));
948 list_add(&frmr->frmr_list, &rdma->sc_frmr_q);
949 spin_unlock_bh(&rdma->sc_frmr_q_lock);
954 * This is the xpo_recvfrom function for listening endpoints. Its
955 * purpose is to accept incoming connections. The CMA callback handler
956 * has already created a new transport and attached it to the new CMA
957 * ID.
959 * There is a queue of pending connections hung on the listening
960 * transport. This queue contains the new svc_xprt structure. This
961 * function takes svc_xprt structures off the accept_q and completes
962 * the connection.
964 static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
966 struct svcxprt_rdma *listen_rdma;
967 struct svcxprt_rdma *newxprt = NULL;
968 struct rdma_conn_param conn_param;
969 struct rpcrdma_connect_private pmsg;
970 struct ib_qp_init_attr qp_attr;
971 struct ib_device *dev;
972 struct sockaddr *sap;
973 unsigned int i;
974 int ret = 0;
976 listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
977 clear_bit(XPT_CONN, &xprt->xpt_flags);
978 /* Get the next entry off the accept list */
979 spin_lock_bh(&listen_rdma->sc_lock);
980 if (!list_empty(&listen_rdma->sc_accept_q)) {
981 newxprt = list_entry(listen_rdma->sc_accept_q.next,
982 struct svcxprt_rdma, sc_accept_q);
983 list_del_init(&newxprt->sc_accept_q);
985 if (!list_empty(&listen_rdma->sc_accept_q))
986 set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
987 spin_unlock_bh(&listen_rdma->sc_lock);
988 if (!newxprt)
989 return NULL;
991 dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
992 newxprt, newxprt->sc_cm_id);
994 dev = newxprt->sc_cm_id->device;
996 /* Qualify the transport resource defaults with the
997 * capabilities of this particular device */
998 newxprt->sc_max_sge = min((size_t)dev->attrs.max_sge,
999 (size_t)RPCSVC_MAXPAGES);
1000 newxprt->sc_max_sge_rd = min_t(size_t, dev->attrs.max_sge_rd,
1001 RPCSVC_MAXPAGES);
1002 newxprt->sc_max_req_size = svcrdma_max_req_size;
1003 newxprt->sc_max_requests = min_t(u32, dev->attrs.max_qp_wr,
1004 svcrdma_max_requests);
1005 newxprt->sc_max_bc_requests = min_t(u32, dev->attrs.max_qp_wr,
1006 svcrdma_max_bc_requests);
1007 newxprt->sc_rq_depth = newxprt->sc_max_requests +
1008 newxprt->sc_max_bc_requests;
1009 newxprt->sc_sq_depth = RPCRDMA_SQ_DEPTH_MULT * newxprt->sc_rq_depth;
1010 atomic_set(&newxprt->sc_sq_avail, newxprt->sc_sq_depth);
1012 if (!svc_rdma_prealloc_ctxts(newxprt))
1013 goto errout;
1014 if (!svc_rdma_prealloc_maps(newxprt))
1015 goto errout;
1018 * Limit ORD based on client limit, local device limit, and
1019 * configured svcrdma limit.
1021 newxprt->sc_ord = min_t(size_t, dev->attrs.max_qp_rd_atom, newxprt->sc_ord);
1022 newxprt->sc_ord = min_t(size_t, svcrdma_ord, newxprt->sc_ord);
1024 newxprt->sc_pd = ib_alloc_pd(dev, 0);
1025 if (IS_ERR(newxprt->sc_pd)) {
1026 dprintk("svcrdma: error creating PD for connect request\n");
1027 goto errout;
1029 newxprt->sc_sq_cq = ib_alloc_cq(dev, newxprt, newxprt->sc_sq_depth,
1030 0, IB_POLL_SOFTIRQ);
1031 if (IS_ERR(newxprt->sc_sq_cq)) {
1032 dprintk("svcrdma: error creating SQ CQ for connect request\n");
1033 goto errout;
1035 newxprt->sc_rq_cq = ib_alloc_cq(dev, newxprt, newxprt->sc_rq_depth,
1036 0, IB_POLL_SOFTIRQ);
1037 if (IS_ERR(newxprt->sc_rq_cq)) {
1038 dprintk("svcrdma: error creating RQ CQ for connect request\n");
1039 goto errout;
1042 memset(&qp_attr, 0, sizeof qp_attr);
1043 qp_attr.event_handler = qp_event_handler;
1044 qp_attr.qp_context = &newxprt->sc_xprt;
1045 qp_attr.cap.max_send_wr = newxprt->sc_sq_depth;
1046 qp_attr.cap.max_recv_wr = newxprt->sc_rq_depth;
1047 qp_attr.cap.max_send_sge = newxprt->sc_max_sge;
1048 qp_attr.cap.max_recv_sge = newxprt->sc_max_sge;
1049 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
1050 qp_attr.qp_type = IB_QPT_RC;
1051 qp_attr.send_cq = newxprt->sc_sq_cq;
1052 qp_attr.recv_cq = newxprt->sc_rq_cq;
1053 dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n",
1054 newxprt->sc_cm_id, newxprt->sc_pd);
1055 dprintk(" cap.max_send_wr = %d, cap.max_recv_wr = %d\n",
1056 qp_attr.cap.max_send_wr, qp_attr.cap.max_recv_wr);
1057 dprintk(" cap.max_send_sge = %d, cap.max_recv_sge = %d\n",
1058 qp_attr.cap.max_send_sge, qp_attr.cap.max_recv_sge);
1060 ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
1061 if (ret) {
1062 dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
1063 goto errout;
1065 newxprt->sc_qp = newxprt->sc_cm_id->qp;
1068 * Use the most secure set of MR resources based on the
1069 * transport type and available memory management features in
1070 * the device. Here's the table implemented below:
1072 * Fast Global DMA Remote WR
1073 * Reg LKEY MR Access
1074 * Sup'd Sup'd Needed Needed
1076 * IWARP N N Y Y
1077 * N Y Y Y
1078 * Y N Y N
1079 * Y Y N -
1081 * IB N N Y N
1082 * N Y N -
1083 * Y N Y N
1084 * Y Y N -
1086 * NB: iWARP requires remote write access for the data sink
1087 * of an RDMA_READ. IB does not.
1089 newxprt->sc_reader = rdma_read_chunk_lcl;
1090 if (dev->attrs.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
1091 newxprt->sc_frmr_pg_list_len =
1092 dev->attrs.max_fast_reg_page_list_len;
1093 newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_FAST_REG;
1094 newxprt->sc_reader = rdma_read_chunk_frmr;
1095 } else
1096 newxprt->sc_snd_w_inv = false;
1099 * Determine if a DMA MR is required and if so, what privs are required
1101 if (!rdma_protocol_iwarp(dev, newxprt->sc_cm_id->port_num) &&
1102 !rdma_ib_or_roce(dev, newxprt->sc_cm_id->port_num))
1103 goto errout;
1105 if (rdma_protocol_iwarp(dev, newxprt->sc_cm_id->port_num))
1106 newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_READ_W_INV;
1108 /* Post receive buffers */
1109 for (i = 0; i < newxprt->sc_max_requests; i++) {
1110 ret = svc_rdma_post_recv(newxprt, GFP_KERNEL);
1111 if (ret) {
1112 dprintk("svcrdma: failure posting receive buffers\n");
1113 goto errout;
1117 /* Swap out the handler */
1118 newxprt->sc_cm_id->event_handler = rdma_cma_handler;
1120 /* Construct RDMA-CM private message */
1121 pmsg.cp_magic = rpcrdma_cmp_magic;
1122 pmsg.cp_version = RPCRDMA_CMP_VERSION;
1123 pmsg.cp_flags = 0;
1124 pmsg.cp_send_size = pmsg.cp_recv_size =
1125 rpcrdma_encode_buffer_size(newxprt->sc_max_req_size);
1127 /* Accept Connection */
1128 set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
1129 memset(&conn_param, 0, sizeof conn_param);
1130 conn_param.responder_resources = 0;
1131 conn_param.initiator_depth = newxprt->sc_ord;
1132 conn_param.private_data = &pmsg;
1133 conn_param.private_data_len = sizeof(pmsg);
1134 ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
1135 if (ret) {
1136 dprintk("svcrdma: failed to accept new connection, ret=%d\n",
1137 ret);
1138 goto errout;
1141 dprintk("svcrdma: new connection %p accepted:\n", newxprt);
1142 sap = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
1143 dprintk(" local address : %pIS:%u\n", sap, rpc_get_port(sap));
1144 sap = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
1145 dprintk(" remote address : %pIS:%u\n", sap, rpc_get_port(sap));
1146 dprintk(" max_sge : %d\n", newxprt->sc_max_sge);
1147 dprintk(" max_sge_rd : %d\n", newxprt->sc_max_sge_rd);
1148 dprintk(" sq_depth : %d\n", newxprt->sc_sq_depth);
1149 dprintk(" max_requests : %d\n", newxprt->sc_max_requests);
1150 dprintk(" ord : %d\n", newxprt->sc_ord);
1152 return &newxprt->sc_xprt;
1154 errout:
1155 dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
1156 /* Take a reference in case the DTO handler runs */
1157 svc_xprt_get(&newxprt->sc_xprt);
1158 if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
1159 ib_destroy_qp(newxprt->sc_qp);
1160 rdma_destroy_id(newxprt->sc_cm_id);
1161 /* This call to put will destroy the transport */
1162 svc_xprt_put(&newxprt->sc_xprt);
1163 return NULL;
1166 static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
1171 * When connected, an svc_xprt has at least two references:
1173 * - A reference held by the cm_id between the ESTABLISHED and
1174 * DISCONNECTED events. If the remote peer disconnected first, this
1175 * reference could be gone.
1177 * - A reference held by the svc_recv code that called this function
1178 * as part of close processing.
1180 * At a minimum one references should still be held.
1182 static void svc_rdma_detach(struct svc_xprt *xprt)
1184 struct svcxprt_rdma *rdma =
1185 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1186 dprintk("svc: svc_rdma_detach(%p)\n", xprt);
1188 /* Disconnect and flush posted WQE */
1189 rdma_disconnect(rdma->sc_cm_id);
1192 static void __svc_rdma_free(struct work_struct *work)
1194 struct svcxprt_rdma *rdma =
1195 container_of(work, struct svcxprt_rdma, sc_work);
1196 struct svc_xprt *xprt = &rdma->sc_xprt;
1198 dprintk("svcrdma: %s(%p)\n", __func__, rdma);
1200 if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
1201 ib_drain_qp(rdma->sc_qp);
1203 /* We should only be called from kref_put */
1204 if (atomic_read(&xprt->xpt_ref.refcount) != 0)
1205 pr_err("svcrdma: sc_xprt still in use? (%d)\n",
1206 atomic_read(&xprt->xpt_ref.refcount));
1209 * Destroy queued, but not processed read completions. Note
1210 * that this cleanup has to be done before destroying the
1211 * cm_id because the device ptr is needed to unmap the dma in
1212 * svc_rdma_put_context.
1214 while (!list_empty(&rdma->sc_read_complete_q)) {
1215 struct svc_rdma_op_ctxt *ctxt;
1216 ctxt = list_entry(rdma->sc_read_complete_q.next,
1217 struct svc_rdma_op_ctxt,
1218 dto_q);
1219 list_del_init(&ctxt->dto_q);
1220 svc_rdma_put_context(ctxt, 1);
1223 /* Destroy queued, but not processed recv completions */
1224 while (!list_empty(&rdma->sc_rq_dto_q)) {
1225 struct svc_rdma_op_ctxt *ctxt;
1226 ctxt = list_entry(rdma->sc_rq_dto_q.next,
1227 struct svc_rdma_op_ctxt,
1228 dto_q);
1229 list_del_init(&ctxt->dto_q);
1230 svc_rdma_put_context(ctxt, 1);
1233 /* Warn if we leaked a resource or under-referenced */
1234 if (rdma->sc_ctxt_used != 0)
1235 pr_err("svcrdma: ctxt still in use? (%d)\n",
1236 rdma->sc_ctxt_used);
1238 /* Final put of backchannel client transport */
1239 if (xprt->xpt_bc_xprt) {
1240 xprt_put(xprt->xpt_bc_xprt);
1241 xprt->xpt_bc_xprt = NULL;
1244 rdma_dealloc_frmr_q(rdma);
1245 svc_rdma_destroy_ctxts(rdma);
1246 svc_rdma_destroy_maps(rdma);
1248 /* Destroy the QP if present (not a listener) */
1249 if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
1250 ib_destroy_qp(rdma->sc_qp);
1252 if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
1253 ib_free_cq(rdma->sc_sq_cq);
1255 if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
1256 ib_free_cq(rdma->sc_rq_cq);
1258 if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
1259 ib_dealloc_pd(rdma->sc_pd);
1261 /* Destroy the CM ID */
1262 rdma_destroy_id(rdma->sc_cm_id);
1264 kfree(rdma);
1267 static void svc_rdma_free(struct svc_xprt *xprt)
1269 struct svcxprt_rdma *rdma =
1270 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1271 INIT_WORK(&rdma->sc_work, __svc_rdma_free);
1272 queue_work(svc_rdma_wq, &rdma->sc_work);
1275 static int svc_rdma_has_wspace(struct svc_xprt *xprt)
1277 struct svcxprt_rdma *rdma =
1278 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1281 * If there are already waiters on the SQ,
1282 * return false.
1284 if (waitqueue_active(&rdma->sc_send_wait))
1285 return 0;
1287 /* Otherwise return true. */
1288 return 1;
1291 static int svc_rdma_secure_port(struct svc_rqst *rqstp)
1293 return 1;
1296 static void svc_rdma_kill_temp_xprt(struct svc_xprt *xprt)
1300 int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr)
1302 struct ib_send_wr *bad_wr, *n_wr;
1303 int wr_count;
1304 int i;
1305 int ret;
1307 if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1308 return -ENOTCONN;
1310 wr_count = 1;
1311 for (n_wr = wr->next; n_wr; n_wr = n_wr->next)
1312 wr_count++;
1314 /* If the SQ is full, wait until an SQ entry is available */
1315 while (1) {
1316 if ((atomic_sub_return(wr_count, &xprt->sc_sq_avail) < 0)) {
1317 atomic_inc(&rdma_stat_sq_starve);
1319 /* Wait until SQ WR available if SQ still full */
1320 atomic_add(wr_count, &xprt->sc_sq_avail);
1321 wait_event(xprt->sc_send_wait,
1322 atomic_read(&xprt->sc_sq_avail) > wr_count);
1323 if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1324 return -ENOTCONN;
1325 continue;
1327 /* Take a transport ref for each WR posted */
1328 for (i = 0; i < wr_count; i++)
1329 svc_xprt_get(&xprt->sc_xprt);
1331 /* Bump used SQ WR count and post */
1332 ret = ib_post_send(xprt->sc_qp, wr, &bad_wr);
1333 if (ret) {
1334 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
1335 for (i = 0; i < wr_count; i ++)
1336 svc_xprt_put(&xprt->sc_xprt);
1337 dprintk("svcrdma: failed to post SQ WR rc=%d\n", ret);
1338 dprintk(" sc_sq_avail=%d, sc_sq_depth=%d\n",
1339 atomic_read(&xprt->sc_sq_avail),
1340 xprt->sc_sq_depth);
1341 wake_up(&xprt->sc_send_wait);
1343 break;
1345 return ret;