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/interrupt.h>
46 #include <linux/sched.h>
47 #include <linux/slab.h>
48 #include <linux/spinlock.h>
49 #include <linux/workqueue.h>
50 #include <rdma/ib_verbs.h>
51 #include <rdma/rdma_cm.h>
52 #include <linux/sunrpc/svc_rdma.h>
53 #include <linux/export.h>
55 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
57 static struct svc_xprt
*svc_rdma_create(struct svc_serv
*serv
,
59 struct sockaddr
*sa
, int salen
,
61 static struct svc_xprt
*svc_rdma_accept(struct svc_xprt
*xprt
);
62 static void svc_rdma_release_rqst(struct svc_rqst
*);
63 static void dto_tasklet_func(unsigned long data
);
64 static void svc_rdma_detach(struct svc_xprt
*xprt
);
65 static void svc_rdma_free(struct svc_xprt
*xprt
);
66 static int svc_rdma_has_wspace(struct svc_xprt
*xprt
);
67 static void rq_cq_reap(struct svcxprt_rdma
*xprt
);
68 static void sq_cq_reap(struct svcxprt_rdma
*xprt
);
70 static DECLARE_TASKLET(dto_tasklet
, dto_tasklet_func
, 0UL);
71 static DEFINE_SPINLOCK(dto_lock
);
72 static LIST_HEAD(dto_xprt_q
);
74 static struct svc_xprt_ops svc_rdma_ops
= {
75 .xpo_create
= svc_rdma_create
,
76 .xpo_recvfrom
= svc_rdma_recvfrom
,
77 .xpo_sendto
= svc_rdma_sendto
,
78 .xpo_release_rqst
= svc_rdma_release_rqst
,
79 .xpo_detach
= svc_rdma_detach
,
80 .xpo_free
= svc_rdma_free
,
81 .xpo_prep_reply_hdr
= svc_rdma_prep_reply_hdr
,
82 .xpo_has_wspace
= svc_rdma_has_wspace
,
83 .xpo_accept
= svc_rdma_accept
,
86 struct svc_xprt_class svc_rdma_class
= {
88 .xcl_owner
= THIS_MODULE
,
89 .xcl_ops
= &svc_rdma_ops
,
90 .xcl_max_payload
= RPCSVC_MAXPAYLOAD_TCP
,
93 /* WR context cache. Created in svc_rdma.c */
94 extern struct kmem_cache
*svc_rdma_ctxt_cachep
;
96 /* Workqueue created in svc_rdma.c */
97 extern struct workqueue_struct
*svc_rdma_wq
;
99 struct svc_rdma_op_ctxt
*svc_rdma_get_context(struct svcxprt_rdma
*xprt
)
101 struct svc_rdma_op_ctxt
*ctxt
;
104 ctxt
= kmem_cache_alloc(svc_rdma_ctxt_cachep
, GFP_KERNEL
);
107 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
110 INIT_LIST_HEAD(&ctxt
->dto_q
);
113 atomic_inc(&xprt
->sc_ctxt_used
);
117 void svc_rdma_unmap_dma(struct svc_rdma_op_ctxt
*ctxt
)
119 struct svcxprt_rdma
*xprt
= ctxt
->xprt
;
121 for (i
= 0; i
< ctxt
->count
&& ctxt
->sge
[i
].length
; i
++) {
123 * Unmap the DMA addr in the SGE if the lkey matches
124 * the sc_dma_lkey, otherwise, ignore it since it is
125 * an FRMR lkey and will be unmapped later when the
126 * last WR that uses it completes.
128 if (ctxt
->sge
[i
].lkey
== xprt
->sc_dma_lkey
) {
129 atomic_dec(&xprt
->sc_dma_used
);
130 ib_dma_unmap_page(xprt
->sc_cm_id
->device
,
138 void svc_rdma_put_context(struct svc_rdma_op_ctxt
*ctxt
, int free_pages
)
140 struct svcxprt_rdma
*xprt
;
146 for (i
= 0; i
< ctxt
->count
; i
++)
147 put_page(ctxt
->pages
[i
]);
149 kmem_cache_free(svc_rdma_ctxt_cachep
, ctxt
);
150 atomic_dec(&xprt
->sc_ctxt_used
);
153 /* Temporary NFS request map cache. Created in svc_rdma.c */
154 extern struct kmem_cache
*svc_rdma_map_cachep
;
157 * Temporary NFS req mappings are shared across all transport
158 * instances. These are short lived and should be bounded by the number
159 * of concurrent server threads * depth of the SQ.
161 struct svc_rdma_req_map
*svc_rdma_get_req_map(void)
163 struct svc_rdma_req_map
*map
;
165 map
= kmem_cache_alloc(svc_rdma_map_cachep
, GFP_KERNEL
);
168 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
175 void svc_rdma_put_req_map(struct svc_rdma_req_map
*map
)
177 kmem_cache_free(svc_rdma_map_cachep
, map
);
180 /* ib_cq event handler */
181 static void cq_event_handler(struct ib_event
*event
, void *context
)
183 struct svc_xprt
*xprt
= context
;
184 dprintk("svcrdma: received CQ event id=%d, context=%p\n",
185 event
->event
, context
);
186 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
189 /* QP event handler */
190 static void qp_event_handler(struct ib_event
*event
, void *context
)
192 struct svc_xprt
*xprt
= context
;
194 switch (event
->event
) {
195 /* These are considered benign events */
196 case IB_EVENT_PATH_MIG
:
197 case IB_EVENT_COMM_EST
:
198 case IB_EVENT_SQ_DRAINED
:
199 case IB_EVENT_QP_LAST_WQE_REACHED
:
200 dprintk("svcrdma: QP event %d received for QP=%p\n",
201 event
->event
, event
->element
.qp
);
203 /* These are considered fatal events */
204 case IB_EVENT_PATH_MIG_ERR
:
205 case IB_EVENT_QP_FATAL
:
206 case IB_EVENT_QP_REQ_ERR
:
207 case IB_EVENT_QP_ACCESS_ERR
:
208 case IB_EVENT_DEVICE_FATAL
:
210 dprintk("svcrdma: QP ERROR event %d received for QP=%p, "
211 "closing transport\n",
212 event
->event
, event
->element
.qp
);
213 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
219 * Data Transfer Operation Tasklet
221 * Walks a list of transports with I/O pending, removing entries as
222 * they are added to the server's I/O pending list. Two bits indicate
223 * if SQ, RQ, or both have I/O pending. The dto_lock is an irqsave
224 * spinlock that serializes access to the transport list with the RQ
225 * and SQ interrupt handlers.
227 static void dto_tasklet_func(unsigned long data
)
229 struct svcxprt_rdma
*xprt
;
232 spin_lock_irqsave(&dto_lock
, flags
);
233 while (!list_empty(&dto_xprt_q
)) {
234 xprt
= list_entry(dto_xprt_q
.next
,
235 struct svcxprt_rdma
, sc_dto_q
);
236 list_del_init(&xprt
->sc_dto_q
);
237 spin_unlock_irqrestore(&dto_lock
, flags
);
242 svc_xprt_put(&xprt
->sc_xprt
);
243 spin_lock_irqsave(&dto_lock
, flags
);
245 spin_unlock_irqrestore(&dto_lock
, flags
);
249 * Receive Queue Completion Handler
251 * Since an RQ completion handler is called on interrupt context, we
252 * need to defer the handling of the I/O to a tasklet
254 static void rq_comp_handler(struct ib_cq
*cq
, void *cq_context
)
256 struct svcxprt_rdma
*xprt
= cq_context
;
259 /* Guard against unconditional flush call for destroyed QP */
260 if (atomic_read(&xprt
->sc_xprt
.xpt_ref
.refcount
)==0)
264 * Set the bit regardless of whether or not it's on the list
265 * because it may be on the list already due to an SQ
268 set_bit(RDMAXPRT_RQ_PENDING
, &xprt
->sc_flags
);
271 * If this transport is not already on the DTO transport queue,
274 spin_lock_irqsave(&dto_lock
, flags
);
275 if (list_empty(&xprt
->sc_dto_q
)) {
276 svc_xprt_get(&xprt
->sc_xprt
);
277 list_add_tail(&xprt
->sc_dto_q
, &dto_xprt_q
);
279 spin_unlock_irqrestore(&dto_lock
, flags
);
281 /* Tasklet does all the work to avoid irqsave locks. */
282 tasklet_schedule(&dto_tasklet
);
286 * rq_cq_reap - Process the RQ CQ.
288 * Take all completing WC off the CQE and enqueue the associated DTO
289 * context on the dto_q for the transport.
291 * Note that caller must hold a transport reference.
293 static void rq_cq_reap(struct svcxprt_rdma
*xprt
)
297 struct svc_rdma_op_ctxt
*ctxt
= NULL
;
299 if (!test_and_clear_bit(RDMAXPRT_RQ_PENDING
, &xprt
->sc_flags
))
302 ib_req_notify_cq(xprt
->sc_rq_cq
, IB_CQ_NEXT_COMP
);
303 atomic_inc(&rdma_stat_rq_poll
);
305 while ((ret
= ib_poll_cq(xprt
->sc_rq_cq
, 1, &wc
)) > 0) {
306 ctxt
= (struct svc_rdma_op_ctxt
*)(unsigned long)wc
.wr_id
;
307 ctxt
->wc_status
= wc
.status
;
308 ctxt
->byte_len
= wc
.byte_len
;
309 svc_rdma_unmap_dma(ctxt
);
310 if (wc
.status
!= IB_WC_SUCCESS
) {
311 /* Close the transport */
312 dprintk("svcrdma: transport closing putting ctxt %p\n", ctxt
);
313 set_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
);
314 svc_rdma_put_context(ctxt
, 1);
315 svc_xprt_put(&xprt
->sc_xprt
);
318 spin_lock_bh(&xprt
->sc_rq_dto_lock
);
319 list_add_tail(&ctxt
->dto_q
, &xprt
->sc_rq_dto_q
);
320 spin_unlock_bh(&xprt
->sc_rq_dto_lock
);
321 svc_xprt_put(&xprt
->sc_xprt
);
325 atomic_inc(&rdma_stat_rq_prod
);
327 set_bit(XPT_DATA
, &xprt
->sc_xprt
.xpt_flags
);
329 * If data arrived before established event,
330 * don't enqueue. This defers RPC I/O until the
331 * RDMA connection is complete.
333 if (!test_bit(RDMAXPRT_CONN_PENDING
, &xprt
->sc_flags
))
334 svc_xprt_enqueue(&xprt
->sc_xprt
);
338 * Process a completion context
340 static void process_context(struct svcxprt_rdma
*xprt
,
341 struct svc_rdma_op_ctxt
*ctxt
)
343 svc_rdma_unmap_dma(ctxt
);
345 switch (ctxt
->wr_op
) {
347 if (test_bit(RDMACTXT_F_FAST_UNREG
, &ctxt
->flags
))
348 svc_rdma_put_frmr(xprt
, ctxt
->frmr
);
349 svc_rdma_put_context(ctxt
, 1);
352 case IB_WR_RDMA_WRITE
:
353 svc_rdma_put_context(ctxt
, 0);
356 case IB_WR_RDMA_READ
:
357 case IB_WR_RDMA_READ_WITH_INV
:
358 if (test_bit(RDMACTXT_F_LAST_CTXT
, &ctxt
->flags
)) {
359 struct svc_rdma_op_ctxt
*read_hdr
= ctxt
->read_hdr
;
361 if (test_bit(RDMACTXT_F_FAST_UNREG
, &ctxt
->flags
))
362 svc_rdma_put_frmr(xprt
, ctxt
->frmr
);
363 spin_lock_bh(&xprt
->sc_rq_dto_lock
);
364 set_bit(XPT_DATA
, &xprt
->sc_xprt
.xpt_flags
);
365 list_add_tail(&read_hdr
->dto_q
,
366 &xprt
->sc_read_complete_q
);
367 spin_unlock_bh(&xprt
->sc_rq_dto_lock
);
368 svc_xprt_enqueue(&xprt
->sc_xprt
);
370 svc_rdma_put_context(ctxt
, 0);
374 printk(KERN_ERR
"svcrdma: unexpected completion type, "
382 * Send Queue Completion Handler - potentially called on interrupt context.
384 * Note that caller must hold a transport reference.
386 static void sq_cq_reap(struct svcxprt_rdma
*xprt
)
388 struct svc_rdma_op_ctxt
*ctxt
= NULL
;
390 struct ib_cq
*cq
= xprt
->sc_sq_cq
;
393 if (!test_and_clear_bit(RDMAXPRT_SQ_PENDING
, &xprt
->sc_flags
))
396 ib_req_notify_cq(xprt
->sc_sq_cq
, IB_CQ_NEXT_COMP
);
397 atomic_inc(&rdma_stat_sq_poll
);
398 while ((ret
= ib_poll_cq(cq
, 1, &wc
)) > 0) {
399 if (wc
.status
!= IB_WC_SUCCESS
)
400 /* Close the transport */
401 set_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
);
403 /* Decrement used SQ WR count */
404 atomic_dec(&xprt
->sc_sq_count
);
405 wake_up(&xprt
->sc_send_wait
);
407 ctxt
= (struct svc_rdma_op_ctxt
*)(unsigned long)wc
.wr_id
;
409 process_context(xprt
, ctxt
);
411 svc_xprt_put(&xprt
->sc_xprt
);
415 atomic_inc(&rdma_stat_sq_prod
);
418 static void sq_comp_handler(struct ib_cq
*cq
, void *cq_context
)
420 struct svcxprt_rdma
*xprt
= cq_context
;
423 /* Guard against unconditional flush call for destroyed QP */
424 if (atomic_read(&xprt
->sc_xprt
.xpt_ref
.refcount
)==0)
428 * Set the bit regardless of whether or not it's on the list
429 * because it may be on the list already due to an RQ
432 set_bit(RDMAXPRT_SQ_PENDING
, &xprt
->sc_flags
);
435 * If this transport is not already on the DTO transport queue,
438 spin_lock_irqsave(&dto_lock
, flags
);
439 if (list_empty(&xprt
->sc_dto_q
)) {
440 svc_xprt_get(&xprt
->sc_xprt
);
441 list_add_tail(&xprt
->sc_dto_q
, &dto_xprt_q
);
443 spin_unlock_irqrestore(&dto_lock
, flags
);
445 /* Tasklet does all the work to avoid irqsave locks. */
446 tasklet_schedule(&dto_tasklet
);
449 static struct svcxprt_rdma
*rdma_create_xprt(struct svc_serv
*serv
,
452 struct svcxprt_rdma
*cma_xprt
= kzalloc(sizeof *cma_xprt
, GFP_KERNEL
);
456 svc_xprt_init(&init_net
, &svc_rdma_class
, &cma_xprt
->sc_xprt
, serv
);
457 INIT_LIST_HEAD(&cma_xprt
->sc_accept_q
);
458 INIT_LIST_HEAD(&cma_xprt
->sc_dto_q
);
459 INIT_LIST_HEAD(&cma_xprt
->sc_rq_dto_q
);
460 INIT_LIST_HEAD(&cma_xprt
->sc_read_complete_q
);
461 INIT_LIST_HEAD(&cma_xprt
->sc_frmr_q
);
462 init_waitqueue_head(&cma_xprt
->sc_send_wait
);
464 spin_lock_init(&cma_xprt
->sc_lock
);
465 spin_lock_init(&cma_xprt
->sc_rq_dto_lock
);
466 spin_lock_init(&cma_xprt
->sc_frmr_q_lock
);
468 cma_xprt
->sc_ord
= svcrdma_ord
;
470 cma_xprt
->sc_max_req_size
= svcrdma_max_req_size
;
471 cma_xprt
->sc_max_requests
= svcrdma_max_requests
;
472 cma_xprt
->sc_sq_depth
= svcrdma_max_requests
* RPCRDMA_SQ_DEPTH_MULT
;
473 atomic_set(&cma_xprt
->sc_sq_count
, 0);
474 atomic_set(&cma_xprt
->sc_ctxt_used
, 0);
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 if (ib_dma_mapping_error(xprt
->sc_cm_id
->device
, pa
))
517 atomic_inc(&xprt
->sc_dma_used
);
518 ctxt
->sge
[sge_no
].addr
= pa
;
519 ctxt
->sge
[sge_no
].length
= PAGE_SIZE
;
520 ctxt
->sge
[sge_no
].lkey
= xprt
->sc_dma_lkey
;
521 ctxt
->count
= sge_no
+ 1;
525 recv_wr
.sg_list
= &ctxt
->sge
[0];
526 recv_wr
.num_sge
= ctxt
->count
;
527 recv_wr
.wr_id
= (u64
)(unsigned long)ctxt
;
529 svc_xprt_get(&xprt
->sc_xprt
);
530 ret
= ib_post_recv(xprt
->sc_qp
, &recv_wr
, &bad_recv_wr
);
532 svc_rdma_unmap_dma(ctxt
);
533 svc_rdma_put_context(ctxt
, 1);
534 svc_xprt_put(&xprt
->sc_xprt
);
539 svc_rdma_unmap_dma(ctxt
);
540 svc_rdma_put_context(ctxt
, 1);
545 * This function handles the CONNECT_REQUEST event on a listening
546 * endpoint. It is passed the cma_id for the _new_ connection. The context in
547 * this cma_id is inherited from the listening cma_id and is the svc_xprt
548 * structure for the listening endpoint.
550 * This function creates a new xprt for the new connection and enqueues it on
551 * the accept queue for the listent xprt. When the listen thread is kicked, it
552 * will call the recvfrom method on the listen xprt which will accept the new
555 static void handle_connect_req(struct rdma_cm_id
*new_cma_id
, size_t client_ird
)
557 struct svcxprt_rdma
*listen_xprt
= new_cma_id
->context
;
558 struct svcxprt_rdma
*newxprt
;
561 /* Create a new transport */
562 newxprt
= rdma_create_xprt(listen_xprt
->sc_xprt
.xpt_server
, 0);
564 dprintk("svcrdma: failed to create new transport\n");
567 newxprt
->sc_cm_id
= new_cma_id
;
568 new_cma_id
->context
= newxprt
;
569 dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
570 newxprt
, newxprt
->sc_cm_id
, listen_xprt
);
572 /* Save client advertised inbound read limit for use later in accept. */
573 newxprt
->sc_ord
= client_ird
;
575 /* Set the local and remote addresses in the transport */
576 sa
= (struct sockaddr
*)&newxprt
->sc_cm_id
->route
.addr
.dst_addr
;
577 svc_xprt_set_remote(&newxprt
->sc_xprt
, sa
, svc_addr_len(sa
));
578 sa
= (struct sockaddr
*)&newxprt
->sc_cm_id
->route
.addr
.src_addr
;
579 svc_xprt_set_local(&newxprt
->sc_xprt
, sa
, svc_addr_len(sa
));
582 * Enqueue the new transport on the accept queue of the listening
585 spin_lock_bh(&listen_xprt
->sc_lock
);
586 list_add_tail(&newxprt
->sc_accept_q
, &listen_xprt
->sc_accept_q
);
587 spin_unlock_bh(&listen_xprt
->sc_lock
);
590 * Can't use svc_xprt_received here because we are not on a
593 set_bit(XPT_CONN
, &listen_xprt
->sc_xprt
.xpt_flags
);
594 svc_xprt_enqueue(&listen_xprt
->sc_xprt
);
598 * Handles events generated on the listening endpoint. These events will be
599 * either be incoming connect requests or adapter removal events.
601 static int rdma_listen_handler(struct rdma_cm_id
*cma_id
,
602 struct rdma_cm_event
*event
)
604 struct svcxprt_rdma
*xprt
= cma_id
->context
;
607 switch (event
->event
) {
608 case RDMA_CM_EVENT_CONNECT_REQUEST
:
609 dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
610 "event=%d\n", cma_id
, cma_id
->context
, event
->event
);
611 handle_connect_req(cma_id
,
612 event
->param
.conn
.initiator_depth
);
615 case RDMA_CM_EVENT_ESTABLISHED
:
616 /* Accept complete */
617 dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
618 "cm_id=%p\n", xprt
, cma_id
);
621 case RDMA_CM_EVENT_DEVICE_REMOVAL
:
622 dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
625 set_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
);
629 dprintk("svcrdma: Unexpected event on listening endpoint %p, "
630 "event=%d\n", cma_id
, event
->event
);
637 static int rdma_cma_handler(struct rdma_cm_id
*cma_id
,
638 struct rdma_cm_event
*event
)
640 struct svc_xprt
*xprt
= cma_id
->context
;
641 struct svcxprt_rdma
*rdma
=
642 container_of(xprt
, struct svcxprt_rdma
, sc_xprt
);
643 switch (event
->event
) {
644 case RDMA_CM_EVENT_ESTABLISHED
:
645 /* Accept complete */
647 dprintk("svcrdma: Connection completed on DTO xprt=%p, "
648 "cm_id=%p\n", xprt
, cma_id
);
649 clear_bit(RDMAXPRT_CONN_PENDING
, &rdma
->sc_flags
);
650 svc_xprt_enqueue(xprt
);
652 case RDMA_CM_EVENT_DISCONNECTED
:
653 dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
656 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
657 svc_xprt_enqueue(xprt
);
661 case RDMA_CM_EVENT_DEVICE_REMOVAL
:
662 dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
663 "event=%d\n", cma_id
, xprt
, event
->event
);
665 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
666 svc_xprt_enqueue(xprt
);
670 dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
671 "event=%d\n", cma_id
, event
->event
);
678 * Create a listening RDMA service endpoint.
680 static struct svc_xprt
*svc_rdma_create(struct svc_serv
*serv
,
682 struct sockaddr
*sa
, int salen
,
685 struct rdma_cm_id
*listen_id
;
686 struct svcxprt_rdma
*cma_xprt
;
687 struct svc_xprt
*xprt
;
690 dprintk("svcrdma: Creating RDMA socket\n");
691 if (sa
->sa_family
!= AF_INET
) {
692 dprintk("svcrdma: Address family %d is not supported.\n", sa
->sa_family
);
693 return ERR_PTR(-EAFNOSUPPORT
);
695 cma_xprt
= rdma_create_xprt(serv
, 1);
697 return ERR_PTR(-ENOMEM
);
698 xprt
= &cma_xprt
->sc_xprt
;
700 listen_id
= rdma_create_id(rdma_listen_handler
, cma_xprt
, RDMA_PS_TCP
,
702 if (IS_ERR(listen_id
)) {
703 ret
= PTR_ERR(listen_id
);
704 dprintk("svcrdma: rdma_create_id failed = %d\n", ret
);
708 ret
= rdma_bind_addr(listen_id
, sa
);
710 dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret
);
713 cma_xprt
->sc_cm_id
= listen_id
;
715 ret
= rdma_listen(listen_id
, RPCRDMA_LISTEN_BACKLOG
);
717 dprintk("svcrdma: rdma_listen failed = %d\n", ret
);
722 * We need to use the address from the cm_id in case the
723 * caller specified 0 for the port number.
725 sa
= (struct sockaddr
*)&cma_xprt
->sc_cm_id
->route
.addr
.src_addr
;
726 svc_xprt_set_local(&cma_xprt
->sc_xprt
, sa
, salen
);
728 return &cma_xprt
->sc_xprt
;
731 rdma_destroy_id(listen_id
);
737 static struct svc_rdma_fastreg_mr
*rdma_alloc_frmr(struct svcxprt_rdma
*xprt
)
740 struct ib_fast_reg_page_list
*pl
;
741 struct svc_rdma_fastreg_mr
*frmr
;
743 frmr
= kmalloc(sizeof(*frmr
), GFP_KERNEL
);
747 mr
= ib_alloc_fast_reg_mr(xprt
->sc_pd
, RPCSVC_MAXPAGES
);
751 pl
= ib_alloc_fast_reg_page_list(xprt
->sc_cm_id
->device
,
757 frmr
->page_list
= pl
;
758 INIT_LIST_HEAD(&frmr
->frmr_list
);
766 return ERR_PTR(-ENOMEM
);
769 static void rdma_dealloc_frmr_q(struct svcxprt_rdma
*xprt
)
771 struct svc_rdma_fastreg_mr
*frmr
;
773 while (!list_empty(&xprt
->sc_frmr_q
)) {
774 frmr
= list_entry(xprt
->sc_frmr_q
.next
,
775 struct svc_rdma_fastreg_mr
, frmr_list
);
776 list_del_init(&frmr
->frmr_list
);
777 ib_dereg_mr(frmr
->mr
);
778 ib_free_fast_reg_page_list(frmr
->page_list
);
783 struct svc_rdma_fastreg_mr
*svc_rdma_get_frmr(struct svcxprt_rdma
*rdma
)
785 struct svc_rdma_fastreg_mr
*frmr
= NULL
;
787 spin_lock_bh(&rdma
->sc_frmr_q_lock
);
788 if (!list_empty(&rdma
->sc_frmr_q
)) {
789 frmr
= list_entry(rdma
->sc_frmr_q
.next
,
790 struct svc_rdma_fastreg_mr
, frmr_list
);
791 list_del_init(&frmr
->frmr_list
);
793 frmr
->page_list_len
= 0;
795 spin_unlock_bh(&rdma
->sc_frmr_q_lock
);
799 return rdma_alloc_frmr(rdma
);
802 static void frmr_unmap_dma(struct svcxprt_rdma
*xprt
,
803 struct svc_rdma_fastreg_mr
*frmr
)
806 for (page_no
= 0; page_no
< frmr
->page_list_len
; page_no
++) {
807 dma_addr_t addr
= frmr
->page_list
->page_list
[page_no
];
808 if (ib_dma_mapping_error(frmr
->mr
->device
, addr
))
810 atomic_dec(&xprt
->sc_dma_used
);
811 ib_dma_unmap_page(frmr
->mr
->device
, addr
, PAGE_SIZE
,
816 void svc_rdma_put_frmr(struct svcxprt_rdma
*rdma
,
817 struct svc_rdma_fastreg_mr
*frmr
)
820 frmr_unmap_dma(rdma
, frmr
);
821 spin_lock_bh(&rdma
->sc_frmr_q_lock
);
822 BUG_ON(!list_empty(&frmr
->frmr_list
));
823 list_add(&frmr
->frmr_list
, &rdma
->sc_frmr_q
);
824 spin_unlock_bh(&rdma
->sc_frmr_q_lock
);
829 * This is the xpo_recvfrom function for listening endpoints. Its
830 * purpose is to accept incoming connections. The CMA callback handler
831 * has already created a new transport and attached it to the new CMA
834 * There is a queue of pending connections hung on the listening
835 * transport. This queue contains the new svc_xprt structure. This
836 * function takes svc_xprt structures off the accept_q and completes
839 static struct svc_xprt
*svc_rdma_accept(struct svc_xprt
*xprt
)
841 struct svcxprt_rdma
*listen_rdma
;
842 struct svcxprt_rdma
*newxprt
= NULL
;
843 struct rdma_conn_param conn_param
;
844 struct ib_qp_init_attr qp_attr
;
845 struct ib_device_attr devattr
;
846 int uninitialized_var(dma_mr_acc
);
851 listen_rdma
= container_of(xprt
, struct svcxprt_rdma
, sc_xprt
);
852 clear_bit(XPT_CONN
, &xprt
->xpt_flags
);
853 /* Get the next entry off the accept list */
854 spin_lock_bh(&listen_rdma
->sc_lock
);
855 if (!list_empty(&listen_rdma
->sc_accept_q
)) {
856 newxprt
= list_entry(listen_rdma
->sc_accept_q
.next
,
857 struct svcxprt_rdma
, sc_accept_q
);
858 list_del_init(&newxprt
->sc_accept_q
);
860 if (!list_empty(&listen_rdma
->sc_accept_q
))
861 set_bit(XPT_CONN
, &listen_rdma
->sc_xprt
.xpt_flags
);
862 spin_unlock_bh(&listen_rdma
->sc_lock
);
866 dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
867 newxprt
, newxprt
->sc_cm_id
);
869 ret
= ib_query_device(newxprt
->sc_cm_id
->device
, &devattr
);
871 dprintk("svcrdma: could not query device attributes on "
872 "device %p, rc=%d\n", newxprt
->sc_cm_id
->device
, ret
);
876 /* Qualify the transport resource defaults with the
877 * capabilities of this particular device */
878 newxprt
->sc_max_sge
= min((size_t)devattr
.max_sge
,
879 (size_t)RPCSVC_MAXPAGES
);
880 newxprt
->sc_max_requests
= min((size_t)devattr
.max_qp_wr
,
881 (size_t)svcrdma_max_requests
);
882 newxprt
->sc_sq_depth
= RPCRDMA_SQ_DEPTH_MULT
* newxprt
->sc_max_requests
;
885 * Limit ORD based on client limit, local device limit, and
886 * configured svcrdma limit.
888 newxprt
->sc_ord
= min_t(size_t, devattr
.max_qp_rd_atom
, newxprt
->sc_ord
);
889 newxprt
->sc_ord
= min_t(size_t, svcrdma_ord
, newxprt
->sc_ord
);
891 newxprt
->sc_pd
= ib_alloc_pd(newxprt
->sc_cm_id
->device
);
892 if (IS_ERR(newxprt
->sc_pd
)) {
893 dprintk("svcrdma: error creating PD for connect request\n");
896 newxprt
->sc_sq_cq
= ib_create_cq(newxprt
->sc_cm_id
->device
,
900 newxprt
->sc_sq_depth
,
902 if (IS_ERR(newxprt
->sc_sq_cq
)) {
903 dprintk("svcrdma: error creating SQ CQ for connect request\n");
906 newxprt
->sc_rq_cq
= ib_create_cq(newxprt
->sc_cm_id
->device
,
910 newxprt
->sc_max_requests
,
912 if (IS_ERR(newxprt
->sc_rq_cq
)) {
913 dprintk("svcrdma: error creating RQ CQ for connect request\n");
917 memset(&qp_attr
, 0, sizeof qp_attr
);
918 qp_attr
.event_handler
= qp_event_handler
;
919 qp_attr
.qp_context
= &newxprt
->sc_xprt
;
920 qp_attr
.cap
.max_send_wr
= newxprt
->sc_sq_depth
;
921 qp_attr
.cap
.max_recv_wr
= newxprt
->sc_max_requests
;
922 qp_attr
.cap
.max_send_sge
= newxprt
->sc_max_sge
;
923 qp_attr
.cap
.max_recv_sge
= newxprt
->sc_max_sge
;
924 qp_attr
.sq_sig_type
= IB_SIGNAL_REQ_WR
;
925 qp_attr
.qp_type
= IB_QPT_RC
;
926 qp_attr
.send_cq
= newxprt
->sc_sq_cq
;
927 qp_attr
.recv_cq
= newxprt
->sc_rq_cq
;
928 dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n"
929 " cm_id->device=%p, sc_pd->device=%p\n"
930 " cap.max_send_wr = %d\n"
931 " cap.max_recv_wr = %d\n"
932 " cap.max_send_sge = %d\n"
933 " cap.max_recv_sge = %d\n",
934 newxprt
->sc_cm_id
, newxprt
->sc_pd
,
935 newxprt
->sc_cm_id
->device
, newxprt
->sc_pd
->device
,
936 qp_attr
.cap
.max_send_wr
,
937 qp_attr
.cap
.max_recv_wr
,
938 qp_attr
.cap
.max_send_sge
,
939 qp_attr
.cap
.max_recv_sge
);
941 ret
= rdma_create_qp(newxprt
->sc_cm_id
, newxprt
->sc_pd
, &qp_attr
);
944 * XXX: This is a hack. We need a xx_request_qp interface
945 * that will adjust the qp_attr's with a best-effort
948 qp_attr
.cap
.max_send_sge
-= 2;
949 qp_attr
.cap
.max_recv_sge
-= 2;
950 ret
= rdma_create_qp(newxprt
->sc_cm_id
, newxprt
->sc_pd
,
953 dprintk("svcrdma: failed to create QP, ret=%d\n", ret
);
956 newxprt
->sc_max_sge
= qp_attr
.cap
.max_send_sge
;
957 newxprt
->sc_max_sge
= qp_attr
.cap
.max_recv_sge
;
958 newxprt
->sc_sq_depth
= qp_attr
.cap
.max_send_wr
;
959 newxprt
->sc_max_requests
= qp_attr
.cap
.max_recv_wr
;
961 newxprt
->sc_qp
= newxprt
->sc_cm_id
->qp
;
964 * Use the most secure set of MR resources based on the
965 * transport type and available memory management features in
966 * the device. Here's the table implemented below:
968 * Fast Global DMA Remote WR
970 * Sup'd Sup'd Needed Needed
982 * NB: iWARP requires remote write access for the data sink
983 * of an RDMA_READ. IB does not.
985 if (devattr
.device_cap_flags
& IB_DEVICE_MEM_MGT_EXTENSIONS
) {
986 newxprt
->sc_frmr_pg_list_len
=
987 devattr
.max_fast_reg_page_list_len
;
988 newxprt
->sc_dev_caps
|= SVCRDMA_DEVCAP_FAST_REG
;
992 * Determine if a DMA MR is required and if so, what privs are required
994 switch (rdma_node_get_transport(newxprt
->sc_cm_id
->device
->node_type
)) {
995 case RDMA_TRANSPORT_IWARP
:
996 newxprt
->sc_dev_caps
|= SVCRDMA_DEVCAP_READ_W_INV
;
997 if (!(newxprt
->sc_dev_caps
& SVCRDMA_DEVCAP_FAST_REG
)) {
1000 (IB_ACCESS_LOCAL_WRITE
|
1001 IB_ACCESS_REMOTE_WRITE
);
1002 } else if (!(devattr
.device_cap_flags
& IB_DEVICE_LOCAL_DMA_LKEY
)) {
1004 dma_mr_acc
= IB_ACCESS_LOCAL_WRITE
;
1008 case RDMA_TRANSPORT_IB
:
1009 if (!(devattr
.device_cap_flags
& IB_DEVICE_LOCAL_DMA_LKEY
)) {
1011 dma_mr_acc
= IB_ACCESS_LOCAL_WRITE
;
1019 /* Create the DMA MR if needed, otherwise, use the DMA LKEY */
1021 /* Register all of physical memory */
1022 newxprt
->sc_phys_mr
=
1023 ib_get_dma_mr(newxprt
->sc_pd
, dma_mr_acc
);
1024 if (IS_ERR(newxprt
->sc_phys_mr
)) {
1025 dprintk("svcrdma: Failed to create DMA MR ret=%d\n",
1029 newxprt
->sc_dma_lkey
= newxprt
->sc_phys_mr
->lkey
;
1031 newxprt
->sc_dma_lkey
=
1032 newxprt
->sc_cm_id
->device
->local_dma_lkey
;
1034 /* Post receive buffers */
1035 for (i
= 0; i
< newxprt
->sc_max_requests
; i
++) {
1036 ret
= svc_rdma_post_recv(newxprt
);
1038 dprintk("svcrdma: failure posting receive buffers\n");
1043 /* Swap out the handler */
1044 newxprt
->sc_cm_id
->event_handler
= rdma_cma_handler
;
1047 * Arm the CQs for the SQ and RQ before accepting so we can't
1048 * miss the first message
1050 ib_req_notify_cq(newxprt
->sc_sq_cq
, IB_CQ_NEXT_COMP
);
1051 ib_req_notify_cq(newxprt
->sc_rq_cq
, IB_CQ_NEXT_COMP
);
1053 /* Accept Connection */
1054 set_bit(RDMAXPRT_CONN_PENDING
, &newxprt
->sc_flags
);
1055 memset(&conn_param
, 0, sizeof conn_param
);
1056 conn_param
.responder_resources
= 0;
1057 conn_param
.initiator_depth
= newxprt
->sc_ord
;
1058 ret
= rdma_accept(newxprt
->sc_cm_id
, &conn_param
);
1060 dprintk("svcrdma: failed to accept new connection, ret=%d\n",
1065 dprintk("svcrdma: new connection %p accepted with the following "
1067 " local_ip : %pI4\n"
1068 " local_port : %d\n"
1069 " remote_ip : %pI4\n"
1070 " remote_port : %d\n"
1073 " max_requests : %d\n"
1076 &((struct sockaddr_in
*)&newxprt
->sc_cm_id
->
1077 route
.addr
.src_addr
)->sin_addr
.s_addr
,
1078 ntohs(((struct sockaddr_in
*)&newxprt
->sc_cm_id
->
1079 route
.addr
.src_addr
)->sin_port
),
1080 &((struct sockaddr_in
*)&newxprt
->sc_cm_id
->
1081 route
.addr
.dst_addr
)->sin_addr
.s_addr
,
1082 ntohs(((struct sockaddr_in
*)&newxprt
->sc_cm_id
->
1083 route
.addr
.dst_addr
)->sin_port
),
1084 newxprt
->sc_max_sge
,
1085 newxprt
->sc_sq_depth
,
1086 newxprt
->sc_max_requests
,
1089 return &newxprt
->sc_xprt
;
1092 dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret
);
1093 /* Take a reference in case the DTO handler runs */
1094 svc_xprt_get(&newxprt
->sc_xprt
);
1095 if (newxprt
->sc_qp
&& !IS_ERR(newxprt
->sc_qp
))
1096 ib_destroy_qp(newxprt
->sc_qp
);
1097 rdma_destroy_id(newxprt
->sc_cm_id
);
1098 /* This call to put will destroy the transport */
1099 svc_xprt_put(&newxprt
->sc_xprt
);
1103 static void svc_rdma_release_rqst(struct svc_rqst
*rqstp
)
1108 * When connected, an svc_xprt has at least two references:
1110 * - A reference held by the cm_id between the ESTABLISHED and
1111 * DISCONNECTED events. If the remote peer disconnected first, this
1112 * reference could be gone.
1114 * - A reference held by the svc_recv code that called this function
1115 * as part of close processing.
1117 * At a minimum one references should still be held.
1119 static void svc_rdma_detach(struct svc_xprt
*xprt
)
1121 struct svcxprt_rdma
*rdma
=
1122 container_of(xprt
, struct svcxprt_rdma
, sc_xprt
);
1123 dprintk("svc: svc_rdma_detach(%p)\n", xprt
);
1125 /* Disconnect and flush posted WQE */
1126 rdma_disconnect(rdma
->sc_cm_id
);
1129 static void __svc_rdma_free(struct work_struct
*work
)
1131 struct svcxprt_rdma
*rdma
=
1132 container_of(work
, struct svcxprt_rdma
, sc_work
);
1133 dprintk("svcrdma: svc_rdma_free(%p)\n", rdma
);
1135 /* We should only be called from kref_put */
1136 BUG_ON(atomic_read(&rdma
->sc_xprt
.xpt_ref
.refcount
) != 0);
1139 * Destroy queued, but not processed read completions. Note
1140 * that this cleanup has to be done before destroying the
1141 * cm_id because the device ptr is needed to unmap the dma in
1142 * svc_rdma_put_context.
1144 while (!list_empty(&rdma
->sc_read_complete_q
)) {
1145 struct svc_rdma_op_ctxt
*ctxt
;
1146 ctxt
= list_entry(rdma
->sc_read_complete_q
.next
,
1147 struct svc_rdma_op_ctxt
,
1149 list_del_init(&ctxt
->dto_q
);
1150 svc_rdma_put_context(ctxt
, 1);
1153 /* Destroy queued, but not processed recv completions */
1154 while (!list_empty(&rdma
->sc_rq_dto_q
)) {
1155 struct svc_rdma_op_ctxt
*ctxt
;
1156 ctxt
= list_entry(rdma
->sc_rq_dto_q
.next
,
1157 struct svc_rdma_op_ctxt
,
1159 list_del_init(&ctxt
->dto_q
);
1160 svc_rdma_put_context(ctxt
, 1);
1163 /* Warn if we leaked a resource or under-referenced */
1164 WARN_ON(atomic_read(&rdma
->sc_ctxt_used
) != 0);
1165 WARN_ON(atomic_read(&rdma
->sc_dma_used
) != 0);
1167 /* De-allocate fastreg mr */
1168 rdma_dealloc_frmr_q(rdma
);
1170 /* Destroy the QP if present (not a listener) */
1171 if (rdma
->sc_qp
&& !IS_ERR(rdma
->sc_qp
))
1172 ib_destroy_qp(rdma
->sc_qp
);
1174 if (rdma
->sc_sq_cq
&& !IS_ERR(rdma
->sc_sq_cq
))
1175 ib_destroy_cq(rdma
->sc_sq_cq
);
1177 if (rdma
->sc_rq_cq
&& !IS_ERR(rdma
->sc_rq_cq
))
1178 ib_destroy_cq(rdma
->sc_rq_cq
);
1180 if (rdma
->sc_phys_mr
&& !IS_ERR(rdma
->sc_phys_mr
))
1181 ib_dereg_mr(rdma
->sc_phys_mr
);
1183 if (rdma
->sc_pd
&& !IS_ERR(rdma
->sc_pd
))
1184 ib_dealloc_pd(rdma
->sc_pd
);
1186 /* Destroy the CM ID */
1187 rdma_destroy_id(rdma
->sc_cm_id
);
1192 static void svc_rdma_free(struct svc_xprt
*xprt
)
1194 struct svcxprt_rdma
*rdma
=
1195 container_of(xprt
, struct svcxprt_rdma
, sc_xprt
);
1196 INIT_WORK(&rdma
->sc_work
, __svc_rdma_free
);
1197 queue_work(svc_rdma_wq
, &rdma
->sc_work
);
1200 static int svc_rdma_has_wspace(struct svc_xprt
*xprt
)
1202 struct svcxprt_rdma
*rdma
=
1203 container_of(xprt
, struct svcxprt_rdma
, sc_xprt
);
1206 * If there are fewer SQ WR available than required to send a
1207 * simple response, return false.
1209 if ((rdma
->sc_sq_depth
- atomic_read(&rdma
->sc_sq_count
) < 3))
1213 * ...or there are already waiters on the SQ,
1216 if (waitqueue_active(&rdma
->sc_send_wait
))
1219 /* Otherwise return true. */
1224 * Attempt to register the kvec representing the RPC memory with the
1228 * NULL : The device does not support fastreg or there were no more
1230 * frmr : The kvec register request was successfully posted.
1231 * <0 : An error was encountered attempting to register the kvec.
1233 int svc_rdma_fastreg(struct svcxprt_rdma
*xprt
,
1234 struct svc_rdma_fastreg_mr
*frmr
)
1236 struct ib_send_wr fastreg_wr
;
1240 key
= (u8
)(frmr
->mr
->lkey
& 0x000000FF);
1241 ib_update_fast_reg_key(frmr
->mr
, ++key
);
1243 /* Prepare FASTREG WR */
1244 memset(&fastreg_wr
, 0, sizeof fastreg_wr
);
1245 fastreg_wr
.opcode
= IB_WR_FAST_REG_MR
;
1246 fastreg_wr
.send_flags
= IB_SEND_SIGNALED
;
1247 fastreg_wr
.wr
.fast_reg
.iova_start
= (unsigned long)frmr
->kva
;
1248 fastreg_wr
.wr
.fast_reg
.page_list
= frmr
->page_list
;
1249 fastreg_wr
.wr
.fast_reg
.page_list_len
= frmr
->page_list_len
;
1250 fastreg_wr
.wr
.fast_reg
.page_shift
= PAGE_SHIFT
;
1251 fastreg_wr
.wr
.fast_reg
.length
= frmr
->map_len
;
1252 fastreg_wr
.wr
.fast_reg
.access_flags
= frmr
->access_flags
;
1253 fastreg_wr
.wr
.fast_reg
.rkey
= frmr
->mr
->lkey
;
1254 return svc_rdma_send(xprt
, &fastreg_wr
);
1257 int svc_rdma_send(struct svcxprt_rdma
*xprt
, struct ib_send_wr
*wr
)
1259 struct ib_send_wr
*bad_wr
, *n_wr
;
1264 if (test_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
))
1267 BUG_ON(wr
->send_flags
!= IB_SEND_SIGNALED
);
1269 for (n_wr
= wr
->next
; n_wr
; n_wr
= n_wr
->next
)
1272 /* If the SQ is full, wait until an SQ entry is available */
1274 spin_lock_bh(&xprt
->sc_lock
);
1275 if (xprt
->sc_sq_depth
< atomic_read(&xprt
->sc_sq_count
) + wr_count
) {
1276 spin_unlock_bh(&xprt
->sc_lock
);
1277 atomic_inc(&rdma_stat_sq_starve
);
1279 /* See if we can opportunistically reap SQ WR to make room */
1282 /* Wait until SQ WR available if SQ still full */
1283 wait_event(xprt
->sc_send_wait
,
1284 atomic_read(&xprt
->sc_sq_count
) <
1286 if (test_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
))
1290 /* Take a transport ref for each WR posted */
1291 for (i
= 0; i
< wr_count
; i
++)
1292 svc_xprt_get(&xprt
->sc_xprt
);
1294 /* Bump used SQ WR count and post */
1295 atomic_add(wr_count
, &xprt
->sc_sq_count
);
1296 ret
= ib_post_send(xprt
->sc_qp
, wr
, &bad_wr
);
1298 set_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
);
1299 atomic_sub(wr_count
, &xprt
->sc_sq_count
);
1300 for (i
= 0; i
< wr_count
; i
++)
1301 svc_xprt_put(&xprt
->sc_xprt
);
1302 dprintk("svcrdma: failed to post SQ WR rc=%d, "
1303 "sc_sq_count=%d, sc_sq_depth=%d\n",
1304 ret
, atomic_read(&xprt
->sc_sq_count
),
1307 spin_unlock_bh(&xprt
->sc_lock
);
1309 wake_up(&xprt
->sc_send_wait
);
1315 void svc_rdma_send_error(struct svcxprt_rdma
*xprt
, struct rpcrdma_msg
*rmsgp
,
1316 enum rpcrdma_errcode err
)
1318 struct ib_send_wr err_wr
;
1320 struct svc_rdma_op_ctxt
*ctxt
;
1325 p
= svc_rdma_get_page();
1326 va
= page_address(p
);
1328 /* XDR encode error */
1329 length
= svc_rdma_xdr_encode_error(xprt
, rmsgp
, err
, va
);
1331 ctxt
= svc_rdma_get_context(xprt
);
1332 ctxt
->direction
= DMA_FROM_DEVICE
;
1336 /* Prepare SGE for local address */
1337 ctxt
->sge
[0].addr
= ib_dma_map_page(xprt
->sc_cm_id
->device
,
1338 p
, 0, length
, DMA_FROM_DEVICE
);
1339 if (ib_dma_mapping_error(xprt
->sc_cm_id
->device
, ctxt
->sge
[0].addr
)) {
1341 svc_rdma_put_context(ctxt
, 1);
1344 atomic_inc(&xprt
->sc_dma_used
);
1345 ctxt
->sge
[0].lkey
= xprt
->sc_dma_lkey
;
1346 ctxt
->sge
[0].length
= length
;
1348 /* Prepare SEND WR */
1349 memset(&err_wr
, 0, sizeof err_wr
);
1350 ctxt
->wr_op
= IB_WR_SEND
;
1351 err_wr
.wr_id
= (unsigned long)ctxt
;
1352 err_wr
.sg_list
= ctxt
->sge
;
1354 err_wr
.opcode
= IB_WR_SEND
;
1355 err_wr
.send_flags
= IB_SEND_SIGNALED
;
1358 ret
= svc_rdma_send(xprt
, &err_wr
);
1360 dprintk("svcrdma: Error %d posting send for protocol error\n",
1362 svc_rdma_unmap_dma(ctxt
);
1363 svc_rdma_put_context(ctxt
, 1);