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 static int rdma_bump_context_cache(struct svcxprt_rdma *xprt)
91 struct svc_rdma_op_ctxt *ctxt;
93 target = min(xprt->sc_ctxt_cnt + xprt->sc_ctxt_bump,
96 spin_lock_bh(&xprt->sc_ctxt_lock);
97 while (xprt->sc_ctxt_cnt < target) {
99 spin_unlock_bh(&xprt->sc_ctxt_lock);
101 ctxt = kmalloc(sizeof(*ctxt), GFP_KERNEL);
103 spin_lock_bh(&xprt->sc_ctxt_lock);
106 ctxt->next = xprt->sc_ctxt_head;
107 xprt->sc_ctxt_head = ctxt;
109 /* kmalloc failed...give up for now */
114 spin_unlock_bh(&xprt->sc_ctxt_lock);
115 dprintk("svcrdma: sc_ctxt_max=%d, sc_ctxt_cnt=%d\n",
116 xprt->sc_ctxt_max, xprt->sc_ctxt_cnt);
120 struct svc_rdma_op_ctxt *svc_rdma_get_context(struct svcxprt_rdma *xprt)
122 struct svc_rdma_op_ctxt *ctxt;
125 spin_lock_bh(&xprt->sc_ctxt_lock);
126 if (unlikely(xprt->sc_ctxt_head == NULL)) {
127 /* Try to bump my cache. */
128 spin_unlock_bh(&xprt->sc_ctxt_lock);
130 if (rdma_bump_context_cache(xprt))
133 printk(KERN_INFO "svcrdma: sleeping waiting for "
134 "context memory on xprt=%p\n",
136 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
139 ctxt = xprt->sc_ctxt_head;
140 xprt->sc_ctxt_head = ctxt->next;
141 spin_unlock_bh(&xprt->sc_ctxt_lock);
143 INIT_LIST_HEAD(&ctxt->dto_q);
150 void svc_rdma_put_context(struct svc_rdma_op_ctxt *ctxt, int free_pages)
152 struct svcxprt_rdma *xprt;
158 for (i = 0; i < ctxt->count; i++)
159 put_page(ctxt->pages[i]);
161 for (i = 0; i < ctxt->count; i++)
162 dma_unmap_single(xprt->sc_cm_id->device->dma_device,
166 spin_lock_bh(&xprt->sc_ctxt_lock);
167 ctxt->next = xprt->sc_ctxt_head;
168 xprt->sc_ctxt_head = ctxt;
169 spin_unlock_bh(&xprt->sc_ctxt_lock);
172 /* ib_cq event handler */
173 static void cq_event_handler(struct ib_event *event, void *context)
175 struct svc_xprt *xprt = context;
176 dprintk("svcrdma: received CQ event id=%d, context=%p\n",
177 event->event, context);
178 set_bit(XPT_CLOSE, &xprt->xpt_flags);
181 /* QP event handler */
182 static void qp_event_handler(struct ib_event *event, void *context)
184 struct svc_xprt *xprt = context;
186 switch (event->event) {
187 /* These are considered benign events */
188 case IB_EVENT_PATH_MIG:
189 case IB_EVENT_COMM_EST:
190 case IB_EVENT_SQ_DRAINED:
191 case IB_EVENT_QP_LAST_WQE_REACHED:
192 dprintk("svcrdma: QP event %d received for QP=%p\n",
193 event->event, event->element.qp);
195 /* These are considered fatal events */
196 case IB_EVENT_PATH_MIG_ERR:
197 case IB_EVENT_QP_FATAL:
198 case IB_EVENT_QP_REQ_ERR:
199 case IB_EVENT_QP_ACCESS_ERR:
200 case IB_EVENT_DEVICE_FATAL:
202 dprintk("svcrdma: QP ERROR event %d received for QP=%p, "
203 "closing transport\n",
204 event->event, event->element.qp);
205 set_bit(XPT_CLOSE, &xprt->xpt_flags);
211 * Data Transfer Operation Tasklet
213 * Walks a list of transports with I/O pending, removing entries as
214 * they are added to the server's I/O pending list. Two bits indicate
215 * if SQ, RQ, or both have I/O pending. The dto_lock is an irqsave
216 * spinlock that serializes access to the transport list with the RQ
217 * and SQ interrupt handlers.
219 static void dto_tasklet_func(unsigned long data)
221 struct svcxprt_rdma *xprt;
224 spin_lock_irqsave(&dto_lock, flags);
225 while (!list_empty(&dto_xprt_q)) {
226 xprt = list_entry(dto_xprt_q.next,
227 struct svcxprt_rdma, sc_dto_q);
228 list_del_init(&xprt->sc_dto_q);
229 spin_unlock_irqrestore(&dto_lock, flags);
234 svc_xprt_put(&xprt->sc_xprt);
235 spin_lock_irqsave(&dto_lock, flags);
237 spin_unlock_irqrestore(&dto_lock, flags);
241 * Receive Queue Completion Handler
243 * Since an RQ completion handler is called on interrupt context, we
244 * need to defer the handling of the I/O to a tasklet
246 static void rq_comp_handler(struct ib_cq *cq, void *cq_context)
248 struct svcxprt_rdma *xprt = cq_context;
252 * Set the bit regardless of whether or not it's on the list
253 * because it may be on the list already due to an SQ
256 set_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags);
259 * If this transport is not already on the DTO transport queue,
262 spin_lock_irqsave(&dto_lock, flags);
263 if (list_empty(&xprt->sc_dto_q)) {
264 svc_xprt_get(&xprt->sc_xprt);
265 list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
267 spin_unlock_irqrestore(&dto_lock, flags);
269 /* Tasklet does all the work to avoid irqsave locks. */
270 tasklet_schedule(&dto_tasklet);
274 * rq_cq_reap - Process the RQ CQ.
276 * Take all completing WC off the CQE and enqueue the associated DTO
277 * context on the dto_q for the transport.
279 static void rq_cq_reap(struct svcxprt_rdma *xprt)
283 struct svc_rdma_op_ctxt *ctxt = NULL;
285 if (!test_and_clear_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags))
288 ib_req_notify_cq(xprt->sc_rq_cq, IB_CQ_NEXT_COMP);
289 atomic_inc(&rdma_stat_rq_poll);
291 spin_lock_bh(&xprt->sc_rq_dto_lock);
292 while ((ret = ib_poll_cq(xprt->sc_rq_cq, 1, &wc)) > 0) {
293 ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
294 ctxt->wc_status = wc.status;
295 ctxt->byte_len = wc.byte_len;
296 if (wc.status != IB_WC_SUCCESS) {
297 /* Close the transport */
298 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
299 svc_rdma_put_context(ctxt, 1);
302 list_add_tail(&ctxt->dto_q, &xprt->sc_rq_dto_q);
304 spin_unlock_bh(&xprt->sc_rq_dto_lock);
307 atomic_inc(&rdma_stat_rq_prod);
309 set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
311 * If data arrived before established event,
312 * don't enqueue. This defers RPC I/O until the
313 * RDMA connection is complete.
315 if (!test_bit(RDMAXPRT_CONN_PENDING, &xprt->sc_flags))
316 svc_xprt_enqueue(&xprt->sc_xprt);
320 * Send Queue Completion Handler - potentially called on interrupt context.
322 static void sq_cq_reap(struct svcxprt_rdma *xprt)
324 struct svc_rdma_op_ctxt *ctxt = NULL;
326 struct ib_cq *cq = xprt->sc_sq_cq;
330 if (!test_and_clear_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags))
333 ib_req_notify_cq(xprt->sc_sq_cq, IB_CQ_NEXT_COMP);
334 atomic_inc(&rdma_stat_sq_poll);
335 while ((ret = ib_poll_cq(cq, 1, &wc)) > 0) {
336 ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
339 if (wc.status != IB_WC_SUCCESS)
340 /* Close the transport */
341 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
343 /* Decrement used SQ WR count */
344 atomic_dec(&xprt->sc_sq_count);
345 wake_up(&xprt->sc_send_wait);
347 switch (ctxt->wr_op) {
349 case IB_WR_RDMA_WRITE:
350 svc_rdma_put_context(ctxt, 1);
353 case IB_WR_RDMA_READ:
354 if (test_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags)) {
355 set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
356 set_bit(RDMACTXT_F_READ_DONE, &ctxt->flags);
357 spin_lock_bh(&xprt->sc_read_complete_lock);
358 list_add_tail(&ctxt->dto_q,
359 &xprt->sc_read_complete_q);
360 spin_unlock_bh(&xprt->sc_read_complete_lock);
361 svc_xprt_enqueue(&xprt->sc_xprt);
366 printk(KERN_ERR "svcrdma: unexpected completion type, "
367 "opcode=%d, status=%d\n",
368 wc.opcode, wc.status);
374 atomic_inc(&rdma_stat_sq_prod);
377 static void sq_comp_handler(struct ib_cq *cq, void *cq_context)
379 struct svcxprt_rdma *xprt = cq_context;
383 * Set the bit regardless of whether or not it's on the list
384 * because it may be on the list already due to an RQ
387 set_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags);
390 * If this transport is not already on the DTO transport queue,
393 spin_lock_irqsave(&dto_lock, flags);
394 if (list_empty(&xprt->sc_dto_q)) {
395 svc_xprt_get(&xprt->sc_xprt);
396 list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
398 spin_unlock_irqrestore(&dto_lock, flags);
400 /* Tasklet does all the work to avoid irqsave locks. */
401 tasklet_schedule(&dto_tasklet);
404 static void create_context_cache(struct svcxprt_rdma *xprt,
405 int ctxt_count, int ctxt_bump, int ctxt_max)
407 struct svc_rdma_op_ctxt *ctxt;
410 xprt->sc_ctxt_max = ctxt_max;
411 xprt->sc_ctxt_bump = ctxt_bump;
412 xprt->sc_ctxt_cnt = 0;
413 xprt->sc_ctxt_head = NULL;
414 for (i = 0; i < ctxt_count; i++) {
415 ctxt = kmalloc(sizeof(*ctxt), GFP_KERNEL);
417 ctxt->next = xprt->sc_ctxt_head;
418 xprt->sc_ctxt_head = ctxt;
424 static void destroy_context_cache(struct svc_rdma_op_ctxt *ctxt)
426 struct svc_rdma_op_ctxt *next;
437 static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *serv,
440 struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
444 svc_xprt_init(&svc_rdma_class, &cma_xprt->sc_xprt, serv);
445 INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
446 INIT_LIST_HEAD(&cma_xprt->sc_dto_q);
447 INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
448 INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
449 init_waitqueue_head(&cma_xprt->sc_send_wait);
451 spin_lock_init(&cma_xprt->sc_lock);
452 spin_lock_init(&cma_xprt->sc_read_complete_lock);
453 spin_lock_init(&cma_xprt->sc_ctxt_lock);
454 spin_lock_init(&cma_xprt->sc_rq_dto_lock);
456 cma_xprt->sc_ord = svcrdma_ord;
458 cma_xprt->sc_max_req_size = svcrdma_max_req_size;
459 cma_xprt->sc_max_requests = svcrdma_max_requests;
460 cma_xprt->sc_sq_depth = svcrdma_max_requests * RPCRDMA_SQ_DEPTH_MULT;
461 atomic_set(&cma_xprt->sc_sq_count, 0);
464 int reqs = cma_xprt->sc_max_requests;
465 create_context_cache(cma_xprt,
466 reqs << 1, /* starting size */
467 reqs, /* bump amount */
469 cma_xprt->sc_sq_depth +
470 RPCRDMA_MAX_THREADS + 1); /* max */
471 if (!cma_xprt->sc_ctxt_head) {
475 clear_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
477 set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
482 struct page *svc_rdma_get_page(void)
486 while ((page = alloc_page(GFP_KERNEL)) == NULL) {
487 /* If we can't get memory, wait a bit and try again */
488 printk(KERN_INFO "svcrdma: out of memory...retrying in 1000 "
490 schedule_timeout_uninterruptible(msecs_to_jiffies(1000));
495 int svc_rdma_post_recv(struct svcxprt_rdma *xprt)
497 struct ib_recv_wr recv_wr, *bad_recv_wr;
498 struct svc_rdma_op_ctxt *ctxt;
505 ctxt = svc_rdma_get_context(xprt);
507 ctxt->direction = DMA_FROM_DEVICE;
508 for (sge_no = 0; buflen < xprt->sc_max_req_size; sge_no++) {
509 BUG_ON(sge_no >= xprt->sc_max_sge);
510 page = svc_rdma_get_page();
511 ctxt->pages[sge_no] = page;
512 pa = ib_dma_map_page(xprt->sc_cm_id->device,
515 ctxt->sge[sge_no].addr = pa;
516 ctxt->sge[sge_no].length = PAGE_SIZE;
517 ctxt->sge[sge_no].lkey = xprt->sc_phys_mr->lkey;
520 ctxt->count = sge_no;
522 recv_wr.sg_list = &ctxt->sge[0];
523 recv_wr.num_sge = ctxt->count;
524 recv_wr.wr_id = (u64)(unsigned long)ctxt;
526 ret = ib_post_recv(xprt->sc_qp, &recv_wr, &bad_recv_wr);
531 * This function handles the CONNECT_REQUEST event on a listening
532 * endpoint. It is passed the cma_id for the _new_ connection. The context in
533 * this cma_id is inherited from the listening cma_id and is the svc_xprt
534 * structure for the listening endpoint.
536 * This function creates a new xprt for the new connection and enqueues it on
537 * the accept queue for the listent xprt. When the listen thread is kicked, it
538 * will call the recvfrom method on the listen xprt which will accept the new
541 static void handle_connect_req(struct rdma_cm_id *new_cma_id)
543 struct svcxprt_rdma *listen_xprt = new_cma_id->context;
544 struct svcxprt_rdma *newxprt;
546 /* Create a new transport */
547 newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0);
549 dprintk("svcrdma: failed to create new transport\n");
552 newxprt->sc_cm_id = new_cma_id;
553 new_cma_id->context = newxprt;
554 dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
555 newxprt, newxprt->sc_cm_id, listen_xprt);
558 * Enqueue the new transport on the accept queue of the listening
561 spin_lock_bh(&listen_xprt->sc_lock);
562 list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
563 spin_unlock_bh(&listen_xprt->sc_lock);
566 * Can't use svc_xprt_received here because we are not on a
569 set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
570 svc_xprt_enqueue(&listen_xprt->sc_xprt);
574 * Handles events generated on the listening endpoint. These events will be
575 * either be incoming connect requests or adapter removal events.
577 static int rdma_listen_handler(struct rdma_cm_id *cma_id,
578 struct rdma_cm_event *event)
580 struct svcxprt_rdma *xprt = cma_id->context;
583 switch (event->event) {
584 case RDMA_CM_EVENT_CONNECT_REQUEST:
585 dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
586 "event=%d\n", cma_id, cma_id->context, event->event);
587 handle_connect_req(cma_id);
590 case RDMA_CM_EVENT_ESTABLISHED:
591 /* Accept complete */
592 dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
593 "cm_id=%p\n", xprt, cma_id);
596 case RDMA_CM_EVENT_DEVICE_REMOVAL:
597 dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
600 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
604 dprintk("svcrdma: Unexpected event on listening endpoint %p, "
605 "event=%d\n", cma_id, event->event);
612 static int rdma_cma_handler(struct rdma_cm_id *cma_id,
613 struct rdma_cm_event *event)
615 struct svc_xprt *xprt = cma_id->context;
616 struct svcxprt_rdma *rdma =
617 container_of(xprt, struct svcxprt_rdma, sc_xprt);
618 switch (event->event) {
619 case RDMA_CM_EVENT_ESTABLISHED:
620 /* Accept complete */
622 dprintk("svcrdma: Connection completed on DTO xprt=%p, "
623 "cm_id=%p\n", xprt, cma_id);
624 clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
625 svc_xprt_enqueue(xprt);
627 case RDMA_CM_EVENT_DISCONNECTED:
628 dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
631 set_bit(XPT_CLOSE, &xprt->xpt_flags);
632 svc_xprt_enqueue(xprt);
635 case RDMA_CM_EVENT_DEVICE_REMOVAL:
636 dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
637 "event=%d\n", cma_id, xprt, event->event);
639 set_bit(XPT_CLOSE, &xprt->xpt_flags);
640 svc_xprt_enqueue(xprt);
644 dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
645 "event=%d\n", cma_id, event->event);
652 * Create a listening RDMA service endpoint.
654 static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
655 struct sockaddr *sa, int salen,
658 struct rdma_cm_id *listen_id;
659 struct svcxprt_rdma *cma_xprt;
660 struct svc_xprt *xprt;
663 dprintk("svcrdma: Creating RDMA socket\n");
665 cma_xprt = rdma_create_xprt(serv, 1);
667 return ERR_PTR(ENOMEM);
668 xprt = &cma_xprt->sc_xprt;
670 listen_id = rdma_create_id(rdma_listen_handler, cma_xprt, RDMA_PS_TCP);
671 if (IS_ERR(listen_id)) {
672 svc_xprt_put(&cma_xprt->sc_xprt);
673 dprintk("svcrdma: rdma_create_id failed = %ld\n",
675 return (void *)listen_id;
677 ret = rdma_bind_addr(listen_id, sa);
679 rdma_destroy_id(listen_id);
680 svc_xprt_put(&cma_xprt->sc_xprt);
681 dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
684 cma_xprt->sc_cm_id = listen_id;
686 ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
688 rdma_destroy_id(listen_id);
689 svc_xprt_put(&cma_xprt->sc_xprt);
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;
705 * This is the xpo_recvfrom function for listening endpoints. Its
706 * purpose is to accept incoming connections. The CMA callback handler
707 * has already created a new transport and attached it to the new CMA
710 * There is a queue of pending connections hung on the listening
711 * transport. This queue contains the new svc_xprt structure. This
712 * function takes svc_xprt structures off the accept_q and completes
715 static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
717 struct svcxprt_rdma *listen_rdma;
718 struct svcxprt_rdma *newxprt = NULL;
719 struct rdma_conn_param conn_param;
720 struct ib_qp_init_attr qp_attr;
721 struct ib_device_attr devattr;
726 listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
727 clear_bit(XPT_CONN, &xprt->xpt_flags);
728 /* Get the next entry off the accept list */
729 spin_lock_bh(&listen_rdma->sc_lock);
730 if (!list_empty(&listen_rdma->sc_accept_q)) {
731 newxprt = list_entry(listen_rdma->sc_accept_q.next,
732 struct svcxprt_rdma, sc_accept_q);
733 list_del_init(&newxprt->sc_accept_q);
735 if (!list_empty(&listen_rdma->sc_accept_q))
736 set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
737 spin_unlock_bh(&listen_rdma->sc_lock);
741 dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
742 newxprt, newxprt->sc_cm_id);
744 ret = ib_query_device(newxprt->sc_cm_id->device, &devattr);
746 dprintk("svcrdma: could not query device attributes on "
747 "device %p, rc=%d\n", newxprt->sc_cm_id->device, ret);
751 /* Qualify the transport resource defaults with the
752 * capabilities of this particular device */
753 newxprt->sc_max_sge = min((size_t)devattr.max_sge,
754 (size_t)RPCSVC_MAXPAGES);
755 newxprt->sc_max_requests = min((size_t)devattr.max_qp_wr,
756 (size_t)svcrdma_max_requests);
757 newxprt->sc_sq_depth = RPCRDMA_SQ_DEPTH_MULT * newxprt->sc_max_requests;
759 newxprt->sc_ord = min((size_t)devattr.max_qp_rd_atom,
760 (size_t)svcrdma_ord);
762 newxprt->sc_pd = ib_alloc_pd(newxprt->sc_cm_id->device);
763 if (IS_ERR(newxprt->sc_pd)) {
764 dprintk("svcrdma: error creating PD for connect request\n");
767 newxprt->sc_sq_cq = ib_create_cq(newxprt->sc_cm_id->device,
771 newxprt->sc_sq_depth,
773 if (IS_ERR(newxprt->sc_sq_cq)) {
774 dprintk("svcrdma: error creating SQ CQ for connect request\n");
777 newxprt->sc_rq_cq = ib_create_cq(newxprt->sc_cm_id->device,
781 newxprt->sc_max_requests,
783 if (IS_ERR(newxprt->sc_rq_cq)) {
784 dprintk("svcrdma: error creating RQ CQ for connect request\n");
788 memset(&qp_attr, 0, sizeof qp_attr);
789 qp_attr.event_handler = qp_event_handler;
790 qp_attr.qp_context = &newxprt->sc_xprt;
791 qp_attr.cap.max_send_wr = newxprt->sc_sq_depth;
792 qp_attr.cap.max_recv_wr = newxprt->sc_max_requests;
793 qp_attr.cap.max_send_sge = newxprt->sc_max_sge;
794 qp_attr.cap.max_recv_sge = newxprt->sc_max_sge;
795 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
796 qp_attr.qp_type = IB_QPT_RC;
797 qp_attr.send_cq = newxprt->sc_sq_cq;
798 qp_attr.recv_cq = newxprt->sc_rq_cq;
799 dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n"
800 " cm_id->device=%p, sc_pd->device=%p\n"
801 " cap.max_send_wr = %d\n"
802 " cap.max_recv_wr = %d\n"
803 " cap.max_send_sge = %d\n"
804 " cap.max_recv_sge = %d\n",
805 newxprt->sc_cm_id, newxprt->sc_pd,
806 newxprt->sc_cm_id->device, newxprt->sc_pd->device,
807 qp_attr.cap.max_send_wr,
808 qp_attr.cap.max_recv_wr,
809 qp_attr.cap.max_send_sge,
810 qp_attr.cap.max_recv_sge);
812 ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
815 * XXX: This is a hack. We need a xx_request_qp interface
816 * that will adjust the qp_attr's with a best-effort
819 qp_attr.cap.max_send_sge -= 2;
820 qp_attr.cap.max_recv_sge -= 2;
821 ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd,
824 dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
827 newxprt->sc_max_sge = qp_attr.cap.max_send_sge;
828 newxprt->sc_max_sge = qp_attr.cap.max_recv_sge;
829 newxprt->sc_sq_depth = qp_attr.cap.max_send_wr;
830 newxprt->sc_max_requests = qp_attr.cap.max_recv_wr;
832 svc_xprt_get(&newxprt->sc_xprt);
833 newxprt->sc_qp = newxprt->sc_cm_id->qp;
835 /* Register all of physical memory */
836 newxprt->sc_phys_mr = ib_get_dma_mr(newxprt->sc_pd,
837 IB_ACCESS_LOCAL_WRITE |
838 IB_ACCESS_REMOTE_WRITE);
839 if (IS_ERR(newxprt->sc_phys_mr)) {
840 dprintk("svcrdma: Failed to create DMA MR ret=%d\n", ret);
844 /* Post receive buffers */
845 for (i = 0; i < newxprt->sc_max_requests; i++) {
846 ret = svc_rdma_post_recv(newxprt);
848 dprintk("svcrdma: failure posting receive buffers\n");
853 /* Swap out the handler */
854 newxprt->sc_cm_id->event_handler = rdma_cma_handler;
856 /* Accept Connection */
857 set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
858 memset(&conn_param, 0, sizeof conn_param);
859 conn_param.responder_resources = 0;
860 conn_param.initiator_depth = newxprt->sc_ord;
861 ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
863 dprintk("svcrdma: failed to accept new connection, ret=%d\n",
868 dprintk("svcrdma: new connection %p accepted with the following "
870 " local_ip : %d.%d.%d.%d\n"
872 " remote_ip : %d.%d.%d.%d\n"
873 " remote_port : %d\n"
876 " max_requests : %d\n"
879 NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id->
880 route.addr.src_addr)->sin_addr.s_addr),
881 ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
882 route.addr.src_addr)->sin_port),
883 NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id->
884 route.addr.dst_addr)->sin_addr.s_addr),
885 ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
886 route.addr.dst_addr)->sin_port),
888 newxprt->sc_sq_depth,
889 newxprt->sc_max_requests,
892 /* Set the local and remote addresses in the transport */
893 sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
894 svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
895 sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
896 svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
898 ib_req_notify_cq(newxprt->sc_sq_cq, IB_CQ_NEXT_COMP);
899 ib_req_notify_cq(newxprt->sc_rq_cq, IB_CQ_NEXT_COMP);
900 return &newxprt->sc_xprt;
903 dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
904 /* Take a reference in case the DTO handler runs */
905 svc_xprt_get(&newxprt->sc_xprt);
906 if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp)) {
907 ib_destroy_qp(newxprt->sc_qp);
908 svc_xprt_put(&newxprt->sc_xprt);
910 rdma_destroy_id(newxprt->sc_cm_id);
911 /* This call to put will destroy the transport */
912 svc_xprt_put(&newxprt->sc_xprt);
916 static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
921 * When connected, an svc_xprt has at least three references:
923 * - A reference held by the QP. We still hold that here because this
924 * code deletes the QP and puts the reference.
926 * - A reference held by the cm_id between the ESTABLISHED and
927 * DISCONNECTED events. If the remote peer disconnected first, this
928 * reference could be gone.
930 * - A reference held by the svc_recv code that called this function
931 * as part of close processing.
933 * At a minimum two references should still be held.
935 static void svc_rdma_detach(struct svc_xprt *xprt)
937 struct svcxprt_rdma *rdma =
938 container_of(xprt, struct svcxprt_rdma, sc_xprt);
939 dprintk("svc: svc_rdma_detach(%p)\n", xprt);
941 /* Disconnect and flush posted WQE */
942 rdma_disconnect(rdma->sc_cm_id);
944 /* Destroy the QP if present (not a listener) */
945 if (rdma->sc_qp && !IS_ERR(rdma->sc_qp)) {
946 ib_destroy_qp(rdma->sc_qp);
950 /* Destroy the CM ID */
951 rdma_destroy_id(rdma->sc_cm_id);
954 static void svc_rdma_free(struct svc_xprt *xprt)
956 struct svcxprt_rdma *rdma = (struct svcxprt_rdma *)xprt;
957 dprintk("svcrdma: svc_rdma_free(%p)\n", rdma);
958 /* We should only be called from kref_put */
959 BUG_ON(atomic_read(&xprt->xpt_ref.refcount) != 0);
960 if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
961 ib_destroy_cq(rdma->sc_sq_cq);
963 if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
964 ib_destroy_cq(rdma->sc_rq_cq);
966 if (rdma->sc_phys_mr && !IS_ERR(rdma->sc_phys_mr))
967 ib_dereg_mr(rdma->sc_phys_mr);
969 if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
970 ib_dealloc_pd(rdma->sc_pd);
972 destroy_context_cache(rdma->sc_ctxt_head);
976 static int svc_rdma_has_wspace(struct svc_xprt *xprt)
978 struct svcxprt_rdma *rdma =
979 container_of(xprt, struct svcxprt_rdma, sc_xprt);
982 * If there are fewer SQ WR available than required to send a
983 * simple response, return false.
985 if ((rdma->sc_sq_depth - atomic_read(&rdma->sc_sq_count) < 3))
989 * ...or there are already waiters on the SQ,
992 if (waitqueue_active(&rdma->sc_send_wait))
995 /* Otherwise return true. */
999 int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr)
1001 struct ib_send_wr *bad_wr;
1004 if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1007 BUG_ON(wr->send_flags != IB_SEND_SIGNALED);
1008 BUG_ON(((struct svc_rdma_op_ctxt *)(unsigned long)wr->wr_id)->wr_op !=
1010 /* If the SQ is full, wait until an SQ entry is available */
1012 spin_lock_bh(&xprt->sc_lock);
1013 if (xprt->sc_sq_depth == atomic_read(&xprt->sc_sq_count)) {
1014 spin_unlock_bh(&xprt->sc_lock);
1015 atomic_inc(&rdma_stat_sq_starve);
1017 /* See if we can opportunistically reap SQ WR to make room */
1020 /* Wait until SQ WR available if SQ still full */
1021 wait_event(xprt->sc_send_wait,
1022 atomic_read(&xprt->sc_sq_count) <
1024 if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1028 /* Bumped used SQ WR count and post */
1029 ret = ib_post_send(xprt->sc_qp, wr, &bad_wr);
1031 atomic_inc(&xprt->sc_sq_count);
1033 dprintk("svcrdma: failed to post SQ WR rc=%d, "
1034 "sc_sq_count=%d, sc_sq_depth=%d\n",
1035 ret, atomic_read(&xprt->sc_sq_count),
1037 spin_unlock_bh(&xprt->sc_lock);
1043 int svc_rdma_send_error(struct svcxprt_rdma *xprt, struct rpcrdma_msg *rmsgp,
1044 enum rpcrdma_errcode err)
1046 struct ib_send_wr err_wr;
1049 struct svc_rdma_op_ctxt *ctxt;
1054 p = svc_rdma_get_page();
1055 va = page_address(p);
1057 /* XDR encode error */
1058 length = svc_rdma_xdr_encode_error(xprt, rmsgp, err, va);
1060 /* Prepare SGE for local address */
1061 sge.addr = ib_dma_map_page(xprt->sc_cm_id->device,
1062 p, 0, PAGE_SIZE, DMA_FROM_DEVICE);
1063 sge.lkey = xprt->sc_phys_mr->lkey;
1064 sge.length = length;
1066 ctxt = svc_rdma_get_context(xprt);
1070 /* Prepare SEND WR */
1071 memset(&err_wr, 0, sizeof err_wr);
1072 ctxt->wr_op = IB_WR_SEND;
1073 err_wr.wr_id = (unsigned long)ctxt;
1074 err_wr.sg_list = &sge;
1076 err_wr.opcode = IB_WR_SEND;
1077 err_wr.send_flags = IB_SEND_SIGNALED;
1080 ret = svc_rdma_send(xprt, &err_wr);
1082 dprintk("svcrdma: Error posting send = %d\n", ret);
1083 svc_rdma_put_context(ctxt, 1);