2 * Copyright (c) 2005-2007 Network Appliance, Inc. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the BSD-type
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials provided
20 * with the distribution.
22 * Neither the name of the Network Appliance, Inc. nor the names of
23 * its contributors may be used to endorse or promote products
24 * derived from this software without specific prior written
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * Author: Tom Tucker <tom@opengridcomputing.com>
42 #include <linux/sunrpc/svc_xprt.h>
43 #include <linux/sunrpc/debug.h>
44 #include <linux/sunrpc/rpc_rdma.h>
45 #include <linux/spinlock.h>
46 #include <rdma/ib_verbs.h>
47 #include <rdma/rdma_cm.h>
48 #include <linux/sunrpc/svc_rdma.h>
50 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
52 static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
53 struct sockaddr *sa, int salen,
55 static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt);
56 static void svc_rdma_release_rqst(struct svc_rqst *);
57 static void dto_tasklet_func(unsigned long data);
58 static void svc_rdma_detach(struct svc_xprt *xprt);
59 static void svc_rdma_free(struct svc_xprt *xprt);
60 static int svc_rdma_has_wspace(struct svc_xprt *xprt);
61 static void rq_cq_reap(struct svcxprt_rdma *xprt);
62 static void sq_cq_reap(struct svcxprt_rdma *xprt);
64 DECLARE_TASKLET(dto_tasklet, dto_tasklet_func, 0UL);
65 static DEFINE_SPINLOCK(dto_lock);
66 static LIST_HEAD(dto_xprt_q);
68 static struct svc_xprt_ops svc_rdma_ops = {
69 .xpo_create = svc_rdma_create,
70 .xpo_recvfrom = svc_rdma_recvfrom,
71 .xpo_sendto = svc_rdma_sendto,
72 .xpo_release_rqst = svc_rdma_release_rqst,
73 .xpo_detach = svc_rdma_detach,
74 .xpo_free = svc_rdma_free,
75 .xpo_prep_reply_hdr = svc_rdma_prep_reply_hdr,
76 .xpo_has_wspace = svc_rdma_has_wspace,
77 .xpo_accept = svc_rdma_accept,
80 struct svc_xprt_class svc_rdma_class = {
82 .xcl_owner = THIS_MODULE,
83 .xcl_ops = &svc_rdma_ops,
84 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
87 /* WR context cache. Created in svc_rdma.c */
88 extern struct kmem_cache *svc_rdma_ctxt_cachep;
90 struct svc_rdma_op_ctxt *svc_rdma_get_context(struct svcxprt_rdma *xprt)
92 struct svc_rdma_op_ctxt *ctxt;
95 ctxt = kmem_cache_alloc(svc_rdma_ctxt_cachep, GFP_KERNEL);
98 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
101 INIT_LIST_HEAD(&ctxt->dto_q);
104 atomic_inc(&xprt->sc_ctxt_used);
108 static void svc_rdma_unmap_dma(struct svc_rdma_op_ctxt *ctxt)
110 struct svcxprt_rdma *xprt = ctxt->xprt;
112 for (i = 0; i < ctxt->count && ctxt->sge[i].length; i++) {
114 * Unmap the DMA addr in the SGE if the lkey matches
115 * the sc_dma_lkey, otherwise, ignore it since it is
116 * an FRMR lkey and will be unmapped later when the
117 * last WR that uses it completes.
119 if (ctxt->sge[i].lkey == xprt->sc_dma_lkey) {
120 atomic_dec(&xprt->sc_dma_used);
121 ib_dma_unmap_single(xprt->sc_cm_id->device,
129 void svc_rdma_put_context(struct svc_rdma_op_ctxt *ctxt, int free_pages)
131 struct svcxprt_rdma *xprt;
137 for (i = 0; i < ctxt->count; i++)
138 put_page(ctxt->pages[i]);
140 kmem_cache_free(svc_rdma_ctxt_cachep, ctxt);
141 atomic_dec(&xprt->sc_ctxt_used);
144 /* Temporary NFS request map cache. Created in svc_rdma.c */
145 extern struct kmem_cache *svc_rdma_map_cachep;
148 * Temporary NFS req mappings are shared across all transport
149 * instances. These are short lived and should be bounded by the number
150 * of concurrent server threads * depth of the SQ.
152 struct svc_rdma_req_map *svc_rdma_get_req_map(void)
154 struct svc_rdma_req_map *map;
156 map = kmem_cache_alloc(svc_rdma_map_cachep, GFP_KERNEL);
159 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
166 void svc_rdma_put_req_map(struct svc_rdma_req_map *map)
168 kmem_cache_free(svc_rdma_map_cachep, map);
171 /* ib_cq event handler */
172 static void cq_event_handler(struct ib_event *event, void *context)
174 struct svc_xprt *xprt = context;
175 dprintk("svcrdma: received CQ event id=%d, context=%p\n",
176 event->event, context);
177 set_bit(XPT_CLOSE, &xprt->xpt_flags);
180 /* QP event handler */
181 static void qp_event_handler(struct ib_event *event, void *context)
183 struct svc_xprt *xprt = context;
185 switch (event->event) {
186 /* These are considered benign events */
187 case IB_EVENT_PATH_MIG:
188 case IB_EVENT_COMM_EST:
189 case IB_EVENT_SQ_DRAINED:
190 case IB_EVENT_QP_LAST_WQE_REACHED:
191 dprintk("svcrdma: QP event %d received for QP=%p\n",
192 event->event, event->element.qp);
194 /* These are considered fatal events */
195 case IB_EVENT_PATH_MIG_ERR:
196 case IB_EVENT_QP_FATAL:
197 case IB_EVENT_QP_REQ_ERR:
198 case IB_EVENT_QP_ACCESS_ERR:
199 case IB_EVENT_DEVICE_FATAL:
201 dprintk("svcrdma: QP ERROR event %d received for QP=%p, "
202 "closing transport\n",
203 event->event, event->element.qp);
204 set_bit(XPT_CLOSE, &xprt->xpt_flags);
210 * Data Transfer Operation Tasklet
212 * Walks a list of transports with I/O pending, removing entries as
213 * they are added to the server's I/O pending list. Two bits indicate
214 * if SQ, RQ, or both have I/O pending. The dto_lock is an irqsave
215 * spinlock that serializes access to the transport list with the RQ
216 * and SQ interrupt handlers.
218 static void dto_tasklet_func(unsigned long data)
220 struct svcxprt_rdma *xprt;
223 spin_lock_irqsave(&dto_lock, flags);
224 while (!list_empty(&dto_xprt_q)) {
225 xprt = list_entry(dto_xprt_q.next,
226 struct svcxprt_rdma, sc_dto_q);
227 list_del_init(&xprt->sc_dto_q);
228 spin_unlock_irqrestore(&dto_lock, flags);
233 svc_xprt_put(&xprt->sc_xprt);
234 spin_lock_irqsave(&dto_lock, flags);
236 spin_unlock_irqrestore(&dto_lock, flags);
240 * Receive Queue Completion Handler
242 * Since an RQ completion handler is called on interrupt context, we
243 * need to defer the handling of the I/O to a tasklet
245 static void rq_comp_handler(struct ib_cq *cq, void *cq_context)
247 struct svcxprt_rdma *xprt = cq_context;
250 /* Guard against unconditional flush call for destroyed QP */
251 if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
255 * Set the bit regardless of whether or not it's on the list
256 * because it may be on the list already due to an SQ
259 set_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags);
262 * If this transport is not already on the DTO transport queue,
265 spin_lock_irqsave(&dto_lock, flags);
266 if (list_empty(&xprt->sc_dto_q)) {
267 svc_xprt_get(&xprt->sc_xprt);
268 list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
270 spin_unlock_irqrestore(&dto_lock, flags);
272 /* Tasklet does all the work to avoid irqsave locks. */
273 tasklet_schedule(&dto_tasklet);
277 * rq_cq_reap - Process the RQ CQ.
279 * Take all completing WC off the CQE and enqueue the associated DTO
280 * context on the dto_q for the transport.
282 * Note that caller must hold a transport reference.
284 static void rq_cq_reap(struct svcxprt_rdma *xprt)
288 struct svc_rdma_op_ctxt *ctxt = NULL;
290 if (!test_and_clear_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags))
293 ib_req_notify_cq(xprt->sc_rq_cq, IB_CQ_NEXT_COMP);
294 atomic_inc(&rdma_stat_rq_poll);
296 while ((ret = ib_poll_cq(xprt->sc_rq_cq, 1, &wc)) > 0) {
297 ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
298 ctxt->wc_status = wc.status;
299 ctxt->byte_len = wc.byte_len;
300 svc_rdma_unmap_dma(ctxt);
301 if (wc.status != IB_WC_SUCCESS) {
302 /* Close the transport */
303 dprintk("svcrdma: transport closing putting ctxt %p\n", ctxt);
304 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
305 svc_rdma_put_context(ctxt, 1);
306 svc_xprt_put(&xprt->sc_xprt);
309 spin_lock_bh(&xprt->sc_rq_dto_lock);
310 list_add_tail(&ctxt->dto_q, &xprt->sc_rq_dto_q);
311 spin_unlock_bh(&xprt->sc_rq_dto_lock);
312 svc_xprt_put(&xprt->sc_xprt);
316 atomic_inc(&rdma_stat_rq_prod);
318 set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
320 * If data arrived before established event,
321 * don't enqueue. This defers RPC I/O until the
322 * RDMA connection is complete.
324 if (!test_bit(RDMAXPRT_CONN_PENDING, &xprt->sc_flags))
325 svc_xprt_enqueue(&xprt->sc_xprt);
329 * Processs a completion context
331 static void process_context(struct svcxprt_rdma *xprt,
332 struct svc_rdma_op_ctxt *ctxt)
334 svc_rdma_unmap_dma(ctxt);
336 switch (ctxt->wr_op) {
338 svc_rdma_put_context(ctxt, 1);
341 case IB_WR_RDMA_WRITE:
342 svc_rdma_put_context(ctxt, 0);
345 case IB_WR_RDMA_READ:
346 if (test_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags)) {
347 struct svc_rdma_op_ctxt *read_hdr = ctxt->read_hdr;
349 spin_lock_bh(&xprt->sc_rq_dto_lock);
350 set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
351 list_add_tail(&read_hdr->dto_q,
352 &xprt->sc_read_complete_q);
353 spin_unlock_bh(&xprt->sc_rq_dto_lock);
354 svc_xprt_enqueue(&xprt->sc_xprt);
356 svc_rdma_put_context(ctxt, 0);
360 printk(KERN_ERR "svcrdma: unexpected completion type, "
368 * Send Queue Completion Handler - potentially called on interrupt context.
370 * Note that caller must hold a transport reference.
372 static void sq_cq_reap(struct svcxprt_rdma *xprt)
374 struct svc_rdma_op_ctxt *ctxt = NULL;
376 struct ib_cq *cq = xprt->sc_sq_cq;
379 if (!test_and_clear_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags))
382 ib_req_notify_cq(xprt->sc_sq_cq, IB_CQ_NEXT_COMP);
383 atomic_inc(&rdma_stat_sq_poll);
384 while ((ret = ib_poll_cq(cq, 1, &wc)) > 0) {
385 if (wc.status != IB_WC_SUCCESS)
386 /* Close the transport */
387 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
389 /* Decrement used SQ WR count */
390 atomic_dec(&xprt->sc_sq_count);
391 wake_up(&xprt->sc_send_wait);
393 ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
395 process_context(xprt, ctxt);
397 svc_xprt_put(&xprt->sc_xprt);
401 atomic_inc(&rdma_stat_sq_prod);
404 static void sq_comp_handler(struct ib_cq *cq, void *cq_context)
406 struct svcxprt_rdma *xprt = cq_context;
409 /* Guard against unconditional flush call for destroyed QP */
410 if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
414 * Set the bit regardless of whether or not it's on the list
415 * because it may be on the list already due to an RQ
418 set_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags);
421 * If this transport is not already on the DTO transport queue,
424 spin_lock_irqsave(&dto_lock, flags);
425 if (list_empty(&xprt->sc_dto_q)) {
426 svc_xprt_get(&xprt->sc_xprt);
427 list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
429 spin_unlock_irqrestore(&dto_lock, flags);
431 /* Tasklet does all the work to avoid irqsave locks. */
432 tasklet_schedule(&dto_tasklet);
435 static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *serv,
438 struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
442 svc_xprt_init(&svc_rdma_class, &cma_xprt->sc_xprt, serv);
443 INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
444 INIT_LIST_HEAD(&cma_xprt->sc_dto_q);
445 INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
446 INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
447 INIT_LIST_HEAD(&cma_xprt->sc_frmr_q);
448 init_waitqueue_head(&cma_xprt->sc_send_wait);
450 spin_lock_init(&cma_xprt->sc_lock);
451 spin_lock_init(&cma_xprt->sc_rq_dto_lock);
452 spin_lock_init(&cma_xprt->sc_frmr_q_lock);
454 cma_xprt->sc_ord = svcrdma_ord;
456 cma_xprt->sc_max_req_size = svcrdma_max_req_size;
457 cma_xprt->sc_max_requests = svcrdma_max_requests;
458 cma_xprt->sc_sq_depth = svcrdma_max_requests * RPCRDMA_SQ_DEPTH_MULT;
459 atomic_set(&cma_xprt->sc_sq_count, 0);
460 atomic_set(&cma_xprt->sc_ctxt_used, 0);
463 set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
468 struct page *svc_rdma_get_page(void)
472 while ((page = alloc_page(GFP_KERNEL)) == NULL) {
473 /* If we can't get memory, wait a bit and try again */
474 printk(KERN_INFO "svcrdma: out of memory...retrying in 1000 "
476 schedule_timeout_uninterruptible(msecs_to_jiffies(1000));
481 int svc_rdma_post_recv(struct svcxprt_rdma *xprt)
483 struct ib_recv_wr recv_wr, *bad_recv_wr;
484 struct svc_rdma_op_ctxt *ctxt;
491 ctxt = svc_rdma_get_context(xprt);
493 ctxt->direction = DMA_FROM_DEVICE;
494 for (sge_no = 0; buflen < xprt->sc_max_req_size; sge_no++) {
495 BUG_ON(sge_no >= xprt->sc_max_sge);
496 page = svc_rdma_get_page();
497 ctxt->pages[sge_no] = page;
498 atomic_inc(&xprt->sc_dma_used);
499 pa = ib_dma_map_page(xprt->sc_cm_id->device,
502 ctxt->sge[sge_no].addr = pa;
503 ctxt->sge[sge_no].length = PAGE_SIZE;
504 ctxt->sge[sge_no].lkey = xprt->sc_phys_mr->lkey;
507 ctxt->count = sge_no;
509 recv_wr.sg_list = &ctxt->sge[0];
510 recv_wr.num_sge = ctxt->count;
511 recv_wr.wr_id = (u64)(unsigned long)ctxt;
513 svc_xprt_get(&xprt->sc_xprt);
514 ret = ib_post_recv(xprt->sc_qp, &recv_wr, &bad_recv_wr);
516 svc_xprt_put(&xprt->sc_xprt);
517 svc_rdma_put_context(ctxt, 1);
523 * This function handles the CONNECT_REQUEST event on a listening
524 * endpoint. It is passed the cma_id for the _new_ connection. The context in
525 * this cma_id is inherited from the listening cma_id and is the svc_xprt
526 * structure for the listening endpoint.
528 * This function creates a new xprt for the new connection and enqueues it on
529 * the accept queue for the listent xprt. When the listen thread is kicked, it
530 * will call the recvfrom method on the listen xprt which will accept the new
533 static void handle_connect_req(struct rdma_cm_id *new_cma_id, size_t client_ird)
535 struct svcxprt_rdma *listen_xprt = new_cma_id->context;
536 struct svcxprt_rdma *newxprt;
539 /* Create a new transport */
540 newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0);
542 dprintk("svcrdma: failed to create new transport\n");
545 newxprt->sc_cm_id = new_cma_id;
546 new_cma_id->context = newxprt;
547 dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
548 newxprt, newxprt->sc_cm_id, listen_xprt);
550 /* Save client advertised inbound read limit for use later in accept. */
551 newxprt->sc_ord = client_ird;
553 /* Set the local and remote addresses in the transport */
554 sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
555 svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
556 sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
557 svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
560 * Enqueue the new transport on the accept queue of the listening
563 spin_lock_bh(&listen_xprt->sc_lock);
564 list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
565 spin_unlock_bh(&listen_xprt->sc_lock);
568 * Can't use svc_xprt_received here because we are not on a
571 set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
572 svc_xprt_enqueue(&listen_xprt->sc_xprt);
576 * Handles events generated on the listening endpoint. These events will be
577 * either be incoming connect requests or adapter removal events.
579 static int rdma_listen_handler(struct rdma_cm_id *cma_id,
580 struct rdma_cm_event *event)
582 struct svcxprt_rdma *xprt = cma_id->context;
585 switch (event->event) {
586 case RDMA_CM_EVENT_CONNECT_REQUEST:
587 dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
588 "event=%d\n", cma_id, cma_id->context, event->event);
589 handle_connect_req(cma_id,
590 event->param.conn.responder_resources);
593 case RDMA_CM_EVENT_ESTABLISHED:
594 /* Accept complete */
595 dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
596 "cm_id=%p\n", xprt, cma_id);
599 case RDMA_CM_EVENT_DEVICE_REMOVAL:
600 dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
603 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
607 dprintk("svcrdma: Unexpected event on listening endpoint %p, "
608 "event=%d\n", cma_id, event->event);
615 static int rdma_cma_handler(struct rdma_cm_id *cma_id,
616 struct rdma_cm_event *event)
618 struct svc_xprt *xprt = cma_id->context;
619 struct svcxprt_rdma *rdma =
620 container_of(xprt, struct svcxprt_rdma, sc_xprt);
621 switch (event->event) {
622 case RDMA_CM_EVENT_ESTABLISHED:
623 /* Accept complete */
625 dprintk("svcrdma: Connection completed on DTO xprt=%p, "
626 "cm_id=%p\n", xprt, cma_id);
627 clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
628 svc_xprt_enqueue(xprt);
630 case RDMA_CM_EVENT_DISCONNECTED:
631 dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
634 set_bit(XPT_CLOSE, &xprt->xpt_flags);
635 svc_xprt_enqueue(xprt);
639 case RDMA_CM_EVENT_DEVICE_REMOVAL:
640 dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
641 "event=%d\n", cma_id, xprt, event->event);
643 set_bit(XPT_CLOSE, &xprt->xpt_flags);
644 svc_xprt_enqueue(xprt);
648 dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
649 "event=%d\n", cma_id, event->event);
656 * Create a listening RDMA service endpoint.
658 static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
659 struct sockaddr *sa, int salen,
662 struct rdma_cm_id *listen_id;
663 struct svcxprt_rdma *cma_xprt;
664 struct svc_xprt *xprt;
667 dprintk("svcrdma: Creating RDMA socket\n");
669 cma_xprt = rdma_create_xprt(serv, 1);
671 return ERR_PTR(-ENOMEM);
672 xprt = &cma_xprt->sc_xprt;
674 listen_id = rdma_create_id(rdma_listen_handler, cma_xprt, RDMA_PS_TCP);
675 if (IS_ERR(listen_id)) {
676 ret = PTR_ERR(listen_id);
677 dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
681 ret = rdma_bind_addr(listen_id, sa);
683 dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
686 cma_xprt->sc_cm_id = listen_id;
688 ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
690 dprintk("svcrdma: rdma_listen failed = %d\n", ret);
695 * We need to use the address from the cm_id in case the
696 * caller specified 0 for the port number.
698 sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
699 svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
701 return &cma_xprt->sc_xprt;
704 rdma_destroy_id(listen_id);
710 static struct svc_rdma_fastreg_mr *rdma_alloc_frmr(struct svcxprt_rdma *xprt)
713 struct ib_fast_reg_page_list *pl;
714 struct svc_rdma_fastreg_mr *frmr;
716 frmr = kmalloc(sizeof(*frmr), GFP_KERNEL);
720 mr = ib_alloc_fast_reg_mr(xprt->sc_pd, RPCSVC_MAXPAGES);
724 pl = ib_alloc_fast_reg_page_list(xprt->sc_cm_id->device,
730 frmr->page_list = pl;
731 INIT_LIST_HEAD(&frmr->frmr_list);
739 return ERR_PTR(-ENOMEM);
742 static void rdma_dealloc_frmr_q(struct svcxprt_rdma *xprt)
744 struct svc_rdma_fastreg_mr *frmr;
746 while (!list_empty(&xprt->sc_frmr_q)) {
747 frmr = list_entry(xprt->sc_frmr_q.next,
748 struct svc_rdma_fastreg_mr, frmr_list);
749 list_del_init(&frmr->frmr_list);
750 ib_dereg_mr(frmr->mr);
751 ib_free_fast_reg_page_list(frmr->page_list);
756 struct svc_rdma_fastreg_mr *svc_rdma_get_frmr(struct svcxprt_rdma *rdma)
758 struct svc_rdma_fastreg_mr *frmr = NULL;
760 spin_lock_bh(&rdma->sc_frmr_q_lock);
761 if (!list_empty(&rdma->sc_frmr_q)) {
762 frmr = list_entry(rdma->sc_frmr_q.next,
763 struct svc_rdma_fastreg_mr, frmr_list);
764 list_del_init(&frmr->frmr_list);
766 frmr->page_list_len = 0;
768 spin_unlock_bh(&rdma->sc_frmr_q_lock);
772 return rdma_alloc_frmr(rdma);
775 static void frmr_unmap_dma(struct svcxprt_rdma *xprt,
776 struct svc_rdma_fastreg_mr *frmr)
779 for (page_no = 0; page_no < frmr->page_list_len; page_no++) {
780 dma_addr_t addr = frmr->page_list->page_list[page_no];
781 if (ib_dma_mapping_error(frmr->mr->device, addr))
783 atomic_dec(&xprt->sc_dma_used);
784 ib_dma_unmap_single(frmr->mr->device, addr, PAGE_SIZE,
789 void svc_rdma_put_frmr(struct svcxprt_rdma *rdma,
790 struct svc_rdma_fastreg_mr *frmr)
793 frmr_unmap_dma(rdma, frmr);
794 spin_lock_bh(&rdma->sc_frmr_q_lock);
795 BUG_ON(!list_empty(&frmr->frmr_list));
796 list_add(&frmr->frmr_list, &rdma->sc_frmr_q);
797 spin_unlock_bh(&rdma->sc_frmr_q_lock);
802 * This is the xpo_recvfrom function for listening endpoints. Its
803 * purpose is to accept incoming connections. The CMA callback handler
804 * has already created a new transport and attached it to the new CMA
807 * There is a queue of pending connections hung on the listening
808 * transport. This queue contains the new svc_xprt structure. This
809 * function takes svc_xprt structures off the accept_q and completes
812 static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
814 struct svcxprt_rdma *listen_rdma;
815 struct svcxprt_rdma *newxprt = NULL;
816 struct rdma_conn_param conn_param;
817 struct ib_qp_init_attr qp_attr;
818 struct ib_device_attr devattr;
824 listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
825 clear_bit(XPT_CONN, &xprt->xpt_flags);
826 /* Get the next entry off the accept list */
827 spin_lock_bh(&listen_rdma->sc_lock);
828 if (!list_empty(&listen_rdma->sc_accept_q)) {
829 newxprt = list_entry(listen_rdma->sc_accept_q.next,
830 struct svcxprt_rdma, sc_accept_q);
831 list_del_init(&newxprt->sc_accept_q);
833 if (!list_empty(&listen_rdma->sc_accept_q))
834 set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
835 spin_unlock_bh(&listen_rdma->sc_lock);
839 dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
840 newxprt, newxprt->sc_cm_id);
842 ret = ib_query_device(newxprt->sc_cm_id->device, &devattr);
844 dprintk("svcrdma: could not query device attributes on "
845 "device %p, rc=%d\n", newxprt->sc_cm_id->device, ret);
849 /* Qualify the transport resource defaults with the
850 * capabilities of this particular device */
851 newxprt->sc_max_sge = min((size_t)devattr.max_sge,
852 (size_t)RPCSVC_MAXPAGES);
853 newxprt->sc_max_requests = min((size_t)devattr.max_qp_wr,
854 (size_t)svcrdma_max_requests);
855 newxprt->sc_sq_depth = RPCRDMA_SQ_DEPTH_MULT * newxprt->sc_max_requests;
858 * Limit ORD based on client limit, local device limit, and
859 * configured svcrdma limit.
861 newxprt->sc_ord = min_t(size_t, devattr.max_qp_rd_atom, newxprt->sc_ord);
862 newxprt->sc_ord = min_t(size_t, svcrdma_ord, newxprt->sc_ord);
864 newxprt->sc_pd = ib_alloc_pd(newxprt->sc_cm_id->device);
865 if (IS_ERR(newxprt->sc_pd)) {
866 dprintk("svcrdma: error creating PD for connect request\n");
869 newxprt->sc_sq_cq = ib_create_cq(newxprt->sc_cm_id->device,
873 newxprt->sc_sq_depth,
875 if (IS_ERR(newxprt->sc_sq_cq)) {
876 dprintk("svcrdma: error creating SQ CQ for connect request\n");
879 newxprt->sc_rq_cq = ib_create_cq(newxprt->sc_cm_id->device,
883 newxprt->sc_max_requests,
885 if (IS_ERR(newxprt->sc_rq_cq)) {
886 dprintk("svcrdma: error creating RQ CQ for connect request\n");
890 memset(&qp_attr, 0, sizeof qp_attr);
891 qp_attr.event_handler = qp_event_handler;
892 qp_attr.qp_context = &newxprt->sc_xprt;
893 qp_attr.cap.max_send_wr = newxprt->sc_sq_depth;
894 qp_attr.cap.max_recv_wr = newxprt->sc_max_requests;
895 qp_attr.cap.max_send_sge = newxprt->sc_max_sge;
896 qp_attr.cap.max_recv_sge = newxprt->sc_max_sge;
897 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
898 qp_attr.qp_type = IB_QPT_RC;
899 qp_attr.send_cq = newxprt->sc_sq_cq;
900 qp_attr.recv_cq = newxprt->sc_rq_cq;
901 dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n"
902 " cm_id->device=%p, sc_pd->device=%p\n"
903 " cap.max_send_wr = %d\n"
904 " cap.max_recv_wr = %d\n"
905 " cap.max_send_sge = %d\n"
906 " cap.max_recv_sge = %d\n",
907 newxprt->sc_cm_id, newxprt->sc_pd,
908 newxprt->sc_cm_id->device, newxprt->sc_pd->device,
909 qp_attr.cap.max_send_wr,
910 qp_attr.cap.max_recv_wr,
911 qp_attr.cap.max_send_sge,
912 qp_attr.cap.max_recv_sge);
914 ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
917 * XXX: This is a hack. We need a xx_request_qp interface
918 * that will adjust the qp_attr's with a best-effort
921 qp_attr.cap.max_send_sge -= 2;
922 qp_attr.cap.max_recv_sge -= 2;
923 ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd,
926 dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
929 newxprt->sc_max_sge = qp_attr.cap.max_send_sge;
930 newxprt->sc_max_sge = qp_attr.cap.max_recv_sge;
931 newxprt->sc_sq_depth = qp_attr.cap.max_send_wr;
932 newxprt->sc_max_requests = qp_attr.cap.max_recv_wr;
934 newxprt->sc_qp = newxprt->sc_cm_id->qp;
937 * Use the most secure set of MR resources based on the
938 * transport type and available memory management features in
939 * the device. Here's the table implemented below:
941 * Fast Global DMA Remote WR
943 * Sup'd Sup'd Needed Needed
955 * NB: iWARP requires remote write access for the data sink
956 * of an RDMA_READ. IB does not.
958 if (devattr.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
959 newxprt->sc_frmr_pg_list_len =
960 devattr.max_fast_reg_page_list_len;
961 newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_FAST_REG;
965 * Determine if a DMA MR is required and if so, what privs are required
967 switch (rdma_node_get_transport(newxprt->sc_cm_id->device->node_type)) {
968 case RDMA_TRANSPORT_IWARP:
969 newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_READ_W_INV;
970 if (!(newxprt->sc_dev_caps & SVCRDMA_DEVCAP_FAST_REG)) {
973 (IB_ACCESS_LOCAL_WRITE |
974 IB_ACCESS_REMOTE_WRITE);
975 } else if (!(devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)) {
977 dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
981 case RDMA_TRANSPORT_IB:
982 if (!(devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)) {
984 dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
992 /* Create the DMA MR if needed, otherwise, use the DMA LKEY */
994 /* Register all of physical memory */
995 newxprt->sc_phys_mr =
996 ib_get_dma_mr(newxprt->sc_pd, dma_mr_acc);
997 if (IS_ERR(newxprt->sc_phys_mr)) {
998 dprintk("svcrdma: Failed to create DMA MR ret=%d\n",
1002 newxprt->sc_dma_lkey = newxprt->sc_phys_mr->lkey;
1004 newxprt->sc_dma_lkey =
1005 newxprt->sc_cm_id->device->local_dma_lkey;
1007 /* Post receive buffers */
1008 for (i = 0; i < newxprt->sc_max_requests; i++) {
1009 ret = svc_rdma_post_recv(newxprt);
1011 dprintk("svcrdma: failure posting receive buffers\n");
1016 /* Swap out the handler */
1017 newxprt->sc_cm_id->event_handler = rdma_cma_handler;
1020 * Arm the CQs for the SQ and RQ before accepting so we can't
1021 * miss the first message
1023 ib_req_notify_cq(newxprt->sc_sq_cq, IB_CQ_NEXT_COMP);
1024 ib_req_notify_cq(newxprt->sc_rq_cq, IB_CQ_NEXT_COMP);
1026 /* Accept Connection */
1027 set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
1028 memset(&conn_param, 0, sizeof conn_param);
1029 conn_param.responder_resources = 0;
1030 conn_param.initiator_depth = newxprt->sc_ord;
1031 ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
1033 dprintk("svcrdma: failed to accept new connection, ret=%d\n",
1038 dprintk("svcrdma: new connection %p accepted with the following "
1040 " local_ip : %d.%d.%d.%d\n"
1041 " local_port : %d\n"
1042 " remote_ip : %d.%d.%d.%d\n"
1043 " remote_port : %d\n"
1046 " max_requests : %d\n"
1049 NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id->
1050 route.addr.src_addr)->sin_addr.s_addr),
1051 ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
1052 route.addr.src_addr)->sin_port),
1053 NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id->
1054 route.addr.dst_addr)->sin_addr.s_addr),
1055 ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
1056 route.addr.dst_addr)->sin_port),
1057 newxprt->sc_max_sge,
1058 newxprt->sc_sq_depth,
1059 newxprt->sc_max_requests,
1062 return &newxprt->sc_xprt;
1065 dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
1066 /* Take a reference in case the DTO handler runs */
1067 svc_xprt_get(&newxprt->sc_xprt);
1068 if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
1069 ib_destroy_qp(newxprt->sc_qp);
1070 rdma_destroy_id(newxprt->sc_cm_id);
1071 /* This call to put will destroy the transport */
1072 svc_xprt_put(&newxprt->sc_xprt);
1076 static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
1081 * When connected, an svc_xprt has at least two references:
1083 * - A reference held by the cm_id between the ESTABLISHED and
1084 * DISCONNECTED events. If the remote peer disconnected first, this
1085 * reference could be gone.
1087 * - A reference held by the svc_recv code that called this function
1088 * as part of close processing.
1090 * At a minimum one references should still be held.
1092 static void svc_rdma_detach(struct svc_xprt *xprt)
1094 struct svcxprt_rdma *rdma =
1095 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1096 dprintk("svc: svc_rdma_detach(%p)\n", xprt);
1098 /* Disconnect and flush posted WQE */
1099 rdma_disconnect(rdma->sc_cm_id);
1102 static void __svc_rdma_free(struct work_struct *work)
1104 struct svcxprt_rdma *rdma =
1105 container_of(work, struct svcxprt_rdma, sc_work);
1106 dprintk("svcrdma: svc_rdma_free(%p)\n", rdma);
1108 /* We should only be called from kref_put */
1109 BUG_ON(atomic_read(&rdma->sc_xprt.xpt_ref.refcount) != 0);
1112 * Destroy queued, but not processed read completions. Note
1113 * that this cleanup has to be done before destroying the
1114 * cm_id because the device ptr is needed to unmap the dma in
1115 * svc_rdma_put_context.
1117 while (!list_empty(&rdma->sc_read_complete_q)) {
1118 struct svc_rdma_op_ctxt *ctxt;
1119 ctxt = list_entry(rdma->sc_read_complete_q.next,
1120 struct svc_rdma_op_ctxt,
1122 list_del_init(&ctxt->dto_q);
1123 svc_rdma_put_context(ctxt, 1);
1126 /* Destroy queued, but not processed recv completions */
1127 while (!list_empty(&rdma->sc_rq_dto_q)) {
1128 struct svc_rdma_op_ctxt *ctxt;
1129 ctxt = list_entry(rdma->sc_rq_dto_q.next,
1130 struct svc_rdma_op_ctxt,
1132 list_del_init(&ctxt->dto_q);
1133 svc_rdma_put_context(ctxt, 1);
1136 /* Warn if we leaked a resource or under-referenced */
1137 WARN_ON(atomic_read(&rdma->sc_ctxt_used) != 0);
1138 WARN_ON(atomic_read(&rdma->sc_dma_used) != 0);
1140 /* De-allocate fastreg mr */
1141 rdma_dealloc_frmr_q(rdma);
1143 /* Destroy the QP if present (not a listener) */
1144 if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
1145 ib_destroy_qp(rdma->sc_qp);
1147 if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
1148 ib_destroy_cq(rdma->sc_sq_cq);
1150 if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
1151 ib_destroy_cq(rdma->sc_rq_cq);
1153 if (rdma->sc_phys_mr && !IS_ERR(rdma->sc_phys_mr))
1154 ib_dereg_mr(rdma->sc_phys_mr);
1156 if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
1157 ib_dealloc_pd(rdma->sc_pd);
1159 /* Destroy the CM ID */
1160 rdma_destroy_id(rdma->sc_cm_id);
1165 static void svc_rdma_free(struct svc_xprt *xprt)
1167 struct svcxprt_rdma *rdma =
1168 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1169 INIT_WORK(&rdma->sc_work, __svc_rdma_free);
1170 schedule_work(&rdma->sc_work);
1173 static int svc_rdma_has_wspace(struct svc_xprt *xprt)
1175 struct svcxprt_rdma *rdma =
1176 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1179 * If there are fewer SQ WR available than required to send a
1180 * simple response, return false.
1182 if ((rdma->sc_sq_depth - atomic_read(&rdma->sc_sq_count) < 3))
1186 * ...or there are already waiters on the SQ,
1189 if (waitqueue_active(&rdma->sc_send_wait))
1192 /* Otherwise return true. */
1197 * Attempt to register the kvec representing the RPC memory with the
1201 * NULL : The device does not support fastreg or there were no more
1203 * frmr : The kvec register request was successfully posted.
1204 * <0 : An error was encountered attempting to register the kvec.
1206 int svc_rdma_fastreg(struct svcxprt_rdma *xprt,
1207 struct svc_rdma_fastreg_mr *frmr)
1209 struct ib_send_wr fastreg_wr;
1213 key = (u8)(frmr->mr->lkey & 0x000000FF);
1214 ib_update_fast_reg_key(frmr->mr, ++key);
1216 /* Prepare FASTREG WR */
1217 memset(&fastreg_wr, 0, sizeof fastreg_wr);
1218 fastreg_wr.opcode = IB_WR_FAST_REG_MR;
1219 fastreg_wr.send_flags = IB_SEND_SIGNALED;
1220 fastreg_wr.wr.fast_reg.iova_start = (unsigned long)frmr->kva;
1221 fastreg_wr.wr.fast_reg.page_list = frmr->page_list;
1222 fastreg_wr.wr.fast_reg.page_list_len = frmr->page_list_len;
1223 fastreg_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
1224 fastreg_wr.wr.fast_reg.length = frmr->map_len;
1225 fastreg_wr.wr.fast_reg.access_flags = frmr->access_flags;
1226 fastreg_wr.wr.fast_reg.rkey = frmr->mr->lkey;
1227 return svc_rdma_send(xprt, &fastreg_wr);
1230 int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr)
1232 struct ib_send_wr *bad_wr;
1235 if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1238 BUG_ON(wr->send_flags != IB_SEND_SIGNALED);
1239 /* If the SQ is full, wait until an SQ entry is available */
1241 spin_lock_bh(&xprt->sc_lock);
1242 if (xprt->sc_sq_depth == atomic_read(&xprt->sc_sq_count)) {
1243 spin_unlock_bh(&xprt->sc_lock);
1244 atomic_inc(&rdma_stat_sq_starve);
1246 /* See if we can opportunistically reap SQ WR to make room */
1249 /* Wait until SQ WR available if SQ still full */
1250 wait_event(xprt->sc_send_wait,
1251 atomic_read(&xprt->sc_sq_count) <
1253 if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1257 /* Bumped used SQ WR count and post */
1258 svc_xprt_get(&xprt->sc_xprt);
1259 ret = ib_post_send(xprt->sc_qp, wr, &bad_wr);
1261 atomic_inc(&xprt->sc_sq_count);
1263 svc_xprt_put(&xprt->sc_xprt);
1264 dprintk("svcrdma: failed to post SQ WR rc=%d, "
1265 "sc_sq_count=%d, sc_sq_depth=%d\n",
1266 ret, atomic_read(&xprt->sc_sq_count),
1269 spin_unlock_bh(&xprt->sc_lock);
1275 void svc_rdma_send_error(struct svcxprt_rdma *xprt, struct rpcrdma_msg *rmsgp,
1276 enum rpcrdma_errcode err)
1278 struct ib_send_wr err_wr;
1281 struct svc_rdma_op_ctxt *ctxt;
1286 p = svc_rdma_get_page();
1287 va = page_address(p);
1289 /* XDR encode error */
1290 length = svc_rdma_xdr_encode_error(xprt, rmsgp, err, va);
1292 /* Prepare SGE for local address */
1293 atomic_inc(&xprt->sc_dma_used);
1294 sge.addr = ib_dma_map_page(xprt->sc_cm_id->device,
1295 p, 0, PAGE_SIZE, DMA_FROM_DEVICE);
1296 sge.lkey = xprt->sc_phys_mr->lkey;
1297 sge.length = length;
1299 ctxt = svc_rdma_get_context(xprt);
1303 /* Prepare SEND WR */
1304 memset(&err_wr, 0, sizeof err_wr);
1305 ctxt->wr_op = IB_WR_SEND;
1306 err_wr.wr_id = (unsigned long)ctxt;
1307 err_wr.sg_list = &sge;
1309 err_wr.opcode = IB_WR_SEND;
1310 err_wr.send_flags = IB_SEND_SIGNALED;
1313 ret = svc_rdma_send(xprt, &err_wr);
1315 dprintk("svcrdma: Error %d posting send for protocol error\n",
1317 svc_rdma_put_context(ctxt, 1);