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/sched.h>
46 #include <linux/slab.h>
47 #include <linux/spinlock.h>
48 #include <linux/workqueue.h>
49 #include <rdma/ib_verbs.h>
50 #include <rdma/rdma_cm.h>
51 #include <linux/sunrpc/svc_rdma.h>
53 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
55 static struct svc_xprt
*svc_rdma_create(struct svc_serv
*serv
,
57 struct sockaddr
*sa
, int salen
,
59 static struct svc_xprt
*svc_rdma_accept(struct svc_xprt
*xprt
);
60 static void svc_rdma_release_rqst(struct svc_rqst
*);
61 static void dto_tasklet_func(unsigned long data
);
62 static void svc_rdma_detach(struct svc_xprt
*xprt
);
63 static void svc_rdma_free(struct svc_xprt
*xprt
);
64 static int svc_rdma_has_wspace(struct svc_xprt
*xprt
);
65 static void rq_cq_reap(struct svcxprt_rdma
*xprt
);
66 static void sq_cq_reap(struct svcxprt_rdma
*xprt
);
68 static DECLARE_TASKLET(dto_tasklet
, dto_tasklet_func
, 0UL);
69 static DEFINE_SPINLOCK(dto_lock
);
70 static LIST_HEAD(dto_xprt_q
);
72 static struct svc_xprt_ops svc_rdma_ops
= {
73 .xpo_create
= svc_rdma_create
,
74 .xpo_recvfrom
= svc_rdma_recvfrom
,
75 .xpo_sendto
= svc_rdma_sendto
,
76 .xpo_release_rqst
= svc_rdma_release_rqst
,
77 .xpo_detach
= svc_rdma_detach
,
78 .xpo_free
= svc_rdma_free
,
79 .xpo_prep_reply_hdr
= svc_rdma_prep_reply_hdr
,
80 .xpo_has_wspace
= svc_rdma_has_wspace
,
81 .xpo_accept
= svc_rdma_accept
,
84 struct svc_xprt_class svc_rdma_class
= {
86 .xcl_owner
= THIS_MODULE
,
87 .xcl_ops
= &svc_rdma_ops
,
88 .xcl_max_payload
= RPCSVC_MAXPAYLOAD_TCP
,
91 /* WR context cache. Created in svc_rdma.c */
92 extern struct kmem_cache
*svc_rdma_ctxt_cachep
;
94 /* Workqueue created in svc_rdma.c */
95 extern struct workqueue_struct
*svc_rdma_wq
;
97 struct svc_rdma_op_ctxt
*svc_rdma_get_context(struct svcxprt_rdma
*xprt
)
99 struct svc_rdma_op_ctxt
*ctxt
;
102 ctxt
= kmem_cache_alloc(svc_rdma_ctxt_cachep
, GFP_KERNEL
);
105 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
108 INIT_LIST_HEAD(&ctxt
->dto_q
);
111 atomic_inc(&xprt
->sc_ctxt_used
);
115 void svc_rdma_unmap_dma(struct svc_rdma_op_ctxt
*ctxt
)
117 struct svcxprt_rdma
*xprt
= ctxt
->xprt
;
119 for (i
= 0; i
< ctxt
->count
&& ctxt
->sge
[i
].length
; i
++) {
121 * Unmap the DMA addr in the SGE if the lkey matches
122 * the sc_dma_lkey, otherwise, ignore it since it is
123 * an FRMR lkey and will be unmapped later when the
124 * last WR that uses it completes.
126 if (ctxt
->sge
[i
].lkey
== xprt
->sc_dma_lkey
) {
127 atomic_dec(&xprt
->sc_dma_used
);
128 ib_dma_unmap_page(xprt
->sc_cm_id
->device
,
136 void svc_rdma_put_context(struct svc_rdma_op_ctxt
*ctxt
, int free_pages
)
138 struct svcxprt_rdma
*xprt
;
144 for (i
= 0; i
< ctxt
->count
; i
++)
145 put_page(ctxt
->pages
[i
]);
147 kmem_cache_free(svc_rdma_ctxt_cachep
, ctxt
);
148 atomic_dec(&xprt
->sc_ctxt_used
);
151 /* Temporary NFS request map cache. Created in svc_rdma.c */
152 extern struct kmem_cache
*svc_rdma_map_cachep
;
155 * Temporary NFS req mappings are shared across all transport
156 * instances. These are short lived and should be bounded by the number
157 * of concurrent server threads * depth of the SQ.
159 struct svc_rdma_req_map
*svc_rdma_get_req_map(void)
161 struct svc_rdma_req_map
*map
;
163 map
= kmem_cache_alloc(svc_rdma_map_cachep
, GFP_KERNEL
);
166 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
173 void svc_rdma_put_req_map(struct svc_rdma_req_map
*map
)
175 kmem_cache_free(svc_rdma_map_cachep
, map
);
178 /* ib_cq event handler */
179 static void cq_event_handler(struct ib_event
*event
, void *context
)
181 struct svc_xprt
*xprt
= context
;
182 dprintk("svcrdma: received CQ event id=%d, context=%p\n",
183 event
->event
, context
);
184 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
187 /* QP event handler */
188 static void qp_event_handler(struct ib_event
*event
, void *context
)
190 struct svc_xprt
*xprt
= context
;
192 switch (event
->event
) {
193 /* These are considered benign events */
194 case IB_EVENT_PATH_MIG
:
195 case IB_EVENT_COMM_EST
:
196 case IB_EVENT_SQ_DRAINED
:
197 case IB_EVENT_QP_LAST_WQE_REACHED
:
198 dprintk("svcrdma: QP event %d received for QP=%p\n",
199 event
->event
, event
->element
.qp
);
201 /* These are considered fatal events */
202 case IB_EVENT_PATH_MIG_ERR
:
203 case IB_EVENT_QP_FATAL
:
204 case IB_EVENT_QP_REQ_ERR
:
205 case IB_EVENT_QP_ACCESS_ERR
:
206 case IB_EVENT_DEVICE_FATAL
:
208 dprintk("svcrdma: QP ERROR event %d received for QP=%p, "
209 "closing transport\n",
210 event
->event
, event
->element
.qp
);
211 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
217 * Data Transfer Operation Tasklet
219 * Walks a list of transports with I/O pending, removing entries as
220 * they are added to the server's I/O pending list. Two bits indicate
221 * if SQ, RQ, or both have I/O pending. The dto_lock is an irqsave
222 * spinlock that serializes access to the transport list with the RQ
223 * and SQ interrupt handlers.
225 static void dto_tasklet_func(unsigned long data
)
227 struct svcxprt_rdma
*xprt
;
230 spin_lock_irqsave(&dto_lock
, flags
);
231 while (!list_empty(&dto_xprt_q
)) {
232 xprt
= list_entry(dto_xprt_q
.next
,
233 struct svcxprt_rdma
, sc_dto_q
);
234 list_del_init(&xprt
->sc_dto_q
);
235 spin_unlock_irqrestore(&dto_lock
, flags
);
240 svc_xprt_put(&xprt
->sc_xprt
);
241 spin_lock_irqsave(&dto_lock
, flags
);
243 spin_unlock_irqrestore(&dto_lock
, flags
);
247 * Receive Queue Completion Handler
249 * Since an RQ completion handler is called on interrupt context, we
250 * need to defer the handling of the I/O to a tasklet
252 static void rq_comp_handler(struct ib_cq
*cq
, void *cq_context
)
254 struct svcxprt_rdma
*xprt
= cq_context
;
257 /* Guard against unconditional flush call for destroyed QP */
258 if (atomic_read(&xprt
->sc_xprt
.xpt_ref
.refcount
)==0)
262 * Set the bit regardless of whether or not it's on the list
263 * because it may be on the list already due to an SQ
266 set_bit(RDMAXPRT_RQ_PENDING
, &xprt
->sc_flags
);
269 * If this transport is not already on the DTO transport queue,
272 spin_lock_irqsave(&dto_lock
, flags
);
273 if (list_empty(&xprt
->sc_dto_q
)) {
274 svc_xprt_get(&xprt
->sc_xprt
);
275 list_add_tail(&xprt
->sc_dto_q
, &dto_xprt_q
);
277 spin_unlock_irqrestore(&dto_lock
, flags
);
279 /* Tasklet does all the work to avoid irqsave locks. */
280 tasklet_schedule(&dto_tasklet
);
284 * rq_cq_reap - Process the RQ CQ.
286 * Take all completing WC off the CQE and enqueue the associated DTO
287 * context on the dto_q for the transport.
289 * Note that caller must hold a transport reference.
291 static void rq_cq_reap(struct svcxprt_rdma
*xprt
)
295 struct svc_rdma_op_ctxt
*ctxt
= NULL
;
297 if (!test_and_clear_bit(RDMAXPRT_RQ_PENDING
, &xprt
->sc_flags
))
300 ib_req_notify_cq(xprt
->sc_rq_cq
, IB_CQ_NEXT_COMP
);
301 atomic_inc(&rdma_stat_rq_poll
);
303 while ((ret
= ib_poll_cq(xprt
->sc_rq_cq
, 1, &wc
)) > 0) {
304 ctxt
= (struct svc_rdma_op_ctxt
*)(unsigned long)wc
.wr_id
;
305 ctxt
->wc_status
= wc
.status
;
306 ctxt
->byte_len
= wc
.byte_len
;
307 svc_rdma_unmap_dma(ctxt
);
308 if (wc
.status
!= IB_WC_SUCCESS
) {
309 /* Close the transport */
310 dprintk("svcrdma: transport closing putting ctxt %p\n", ctxt
);
311 set_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
);
312 svc_rdma_put_context(ctxt
, 1);
313 svc_xprt_put(&xprt
->sc_xprt
);
316 spin_lock_bh(&xprt
->sc_rq_dto_lock
);
317 list_add_tail(&ctxt
->dto_q
, &xprt
->sc_rq_dto_q
);
318 spin_unlock_bh(&xprt
->sc_rq_dto_lock
);
319 svc_xprt_put(&xprt
->sc_xprt
);
323 atomic_inc(&rdma_stat_rq_prod
);
325 set_bit(XPT_DATA
, &xprt
->sc_xprt
.xpt_flags
);
327 * If data arrived before established event,
328 * don't enqueue. This defers RPC I/O until the
329 * RDMA connection is complete.
331 if (!test_bit(RDMAXPRT_CONN_PENDING
, &xprt
->sc_flags
))
332 svc_xprt_enqueue(&xprt
->sc_xprt
);
336 * Processs a completion context
338 static void process_context(struct svcxprt_rdma
*xprt
,
339 struct svc_rdma_op_ctxt
*ctxt
)
341 svc_rdma_unmap_dma(ctxt
);
343 switch (ctxt
->wr_op
) {
345 if (test_bit(RDMACTXT_F_FAST_UNREG
, &ctxt
->flags
))
346 svc_rdma_put_frmr(xprt
, ctxt
->frmr
);
347 svc_rdma_put_context(ctxt
, 1);
350 case IB_WR_RDMA_WRITE
:
351 svc_rdma_put_context(ctxt
, 0);
354 case IB_WR_RDMA_READ
:
355 case IB_WR_RDMA_READ_WITH_INV
:
356 if (test_bit(RDMACTXT_F_LAST_CTXT
, &ctxt
->flags
)) {
357 struct svc_rdma_op_ctxt
*read_hdr
= ctxt
->read_hdr
;
359 if (test_bit(RDMACTXT_F_FAST_UNREG
, &ctxt
->flags
))
360 svc_rdma_put_frmr(xprt
, ctxt
->frmr
);
361 spin_lock_bh(&xprt
->sc_rq_dto_lock
);
362 set_bit(XPT_DATA
, &xprt
->sc_xprt
.xpt_flags
);
363 list_add_tail(&read_hdr
->dto_q
,
364 &xprt
->sc_read_complete_q
);
365 spin_unlock_bh(&xprt
->sc_rq_dto_lock
);
366 svc_xprt_enqueue(&xprt
->sc_xprt
);
368 svc_rdma_put_context(ctxt
, 0);
372 printk(KERN_ERR
"svcrdma: unexpected completion type, "
380 * Send Queue Completion Handler - potentially called on interrupt context.
382 * Note that caller must hold a transport reference.
384 static void sq_cq_reap(struct svcxprt_rdma
*xprt
)
386 struct svc_rdma_op_ctxt
*ctxt
= NULL
;
388 struct ib_cq
*cq
= xprt
->sc_sq_cq
;
391 if (!test_and_clear_bit(RDMAXPRT_SQ_PENDING
, &xprt
->sc_flags
))
394 ib_req_notify_cq(xprt
->sc_sq_cq
, IB_CQ_NEXT_COMP
);
395 atomic_inc(&rdma_stat_sq_poll
);
396 while ((ret
= ib_poll_cq(cq
, 1, &wc
)) > 0) {
397 if (wc
.status
!= IB_WC_SUCCESS
)
398 /* Close the transport */
399 set_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
);
401 /* Decrement used SQ WR count */
402 atomic_dec(&xprt
->sc_sq_count
);
403 wake_up(&xprt
->sc_send_wait
);
405 ctxt
= (struct svc_rdma_op_ctxt
*)(unsigned long)wc
.wr_id
;
407 process_context(xprt
, ctxt
);
409 svc_xprt_put(&xprt
->sc_xprt
);
413 atomic_inc(&rdma_stat_sq_prod
);
416 static void sq_comp_handler(struct ib_cq
*cq
, void *cq_context
)
418 struct svcxprt_rdma
*xprt
= cq_context
;
421 /* Guard against unconditional flush call for destroyed QP */
422 if (atomic_read(&xprt
->sc_xprt
.xpt_ref
.refcount
)==0)
426 * Set the bit regardless of whether or not it's on the list
427 * because it may be on the list already due to an RQ
430 set_bit(RDMAXPRT_SQ_PENDING
, &xprt
->sc_flags
);
433 * If this transport is not already on the DTO transport queue,
436 spin_lock_irqsave(&dto_lock
, flags
);
437 if (list_empty(&xprt
->sc_dto_q
)) {
438 svc_xprt_get(&xprt
->sc_xprt
);
439 list_add_tail(&xprt
->sc_dto_q
, &dto_xprt_q
);
441 spin_unlock_irqrestore(&dto_lock
, flags
);
443 /* Tasklet does all the work to avoid irqsave locks. */
444 tasklet_schedule(&dto_tasklet
);
447 static struct svcxprt_rdma
*rdma_create_xprt(struct svc_serv
*serv
,
450 struct svcxprt_rdma
*cma_xprt
= kzalloc(sizeof *cma_xprt
, GFP_KERNEL
);
454 svc_xprt_init(&svc_rdma_class
, &cma_xprt
->sc_xprt
, serv
);
455 INIT_LIST_HEAD(&cma_xprt
->sc_accept_q
);
456 INIT_LIST_HEAD(&cma_xprt
->sc_dto_q
);
457 INIT_LIST_HEAD(&cma_xprt
->sc_rq_dto_q
);
458 INIT_LIST_HEAD(&cma_xprt
->sc_read_complete_q
);
459 INIT_LIST_HEAD(&cma_xprt
->sc_frmr_q
);
460 init_waitqueue_head(&cma_xprt
->sc_send_wait
);
462 spin_lock_init(&cma_xprt
->sc_lock
);
463 spin_lock_init(&cma_xprt
->sc_rq_dto_lock
);
464 spin_lock_init(&cma_xprt
->sc_frmr_q_lock
);
466 cma_xprt
->sc_ord
= svcrdma_ord
;
468 cma_xprt
->sc_max_req_size
= svcrdma_max_req_size
;
469 cma_xprt
->sc_max_requests
= svcrdma_max_requests
;
470 cma_xprt
->sc_sq_depth
= svcrdma_max_requests
* RPCRDMA_SQ_DEPTH_MULT
;
471 atomic_set(&cma_xprt
->sc_sq_count
, 0);
472 atomic_set(&cma_xprt
->sc_ctxt_used
, 0);
475 set_bit(XPT_LISTENER
, &cma_xprt
->sc_xprt
.xpt_flags
);
480 struct page
*svc_rdma_get_page(void)
484 while ((page
= alloc_page(GFP_KERNEL
)) == NULL
) {
485 /* If we can't get memory, wait a bit and try again */
486 printk(KERN_INFO
"svcrdma: out of memory...retrying in 1000 "
488 schedule_timeout_uninterruptible(msecs_to_jiffies(1000));
493 int svc_rdma_post_recv(struct svcxprt_rdma
*xprt
)
495 struct ib_recv_wr recv_wr
, *bad_recv_wr
;
496 struct svc_rdma_op_ctxt
*ctxt
;
503 ctxt
= svc_rdma_get_context(xprt
);
505 ctxt
->direction
= DMA_FROM_DEVICE
;
506 for (sge_no
= 0; buflen
< xprt
->sc_max_req_size
; sge_no
++) {
507 BUG_ON(sge_no
>= xprt
->sc_max_sge
);
508 page
= svc_rdma_get_page();
509 ctxt
->pages
[sge_no
] = page
;
510 pa
= ib_dma_map_page(xprt
->sc_cm_id
->device
,
513 if (ib_dma_mapping_error(xprt
->sc_cm_id
->device
, pa
))
515 atomic_inc(&xprt
->sc_dma_used
);
516 ctxt
->sge
[sge_no
].addr
= pa
;
517 ctxt
->sge
[sge_no
].length
= PAGE_SIZE
;
518 ctxt
->sge
[sge_no
].lkey
= xprt
->sc_dma_lkey
;
519 ctxt
->count
= sge_no
+ 1;
523 recv_wr
.sg_list
= &ctxt
->sge
[0];
524 recv_wr
.num_sge
= ctxt
->count
;
525 recv_wr
.wr_id
= (u64
)(unsigned long)ctxt
;
527 svc_xprt_get(&xprt
->sc_xprt
);
528 ret
= ib_post_recv(xprt
->sc_qp
, &recv_wr
, &bad_recv_wr
);
530 svc_rdma_unmap_dma(ctxt
);
531 svc_rdma_put_context(ctxt
, 1);
532 svc_xprt_put(&xprt
->sc_xprt
);
537 svc_rdma_unmap_dma(ctxt
);
538 svc_rdma_put_context(ctxt
, 1);
543 * This function handles the CONNECT_REQUEST event on a listening
544 * endpoint. It is passed the cma_id for the _new_ connection. The context in
545 * this cma_id is inherited from the listening cma_id and is the svc_xprt
546 * structure for the listening endpoint.
548 * This function creates a new xprt for the new connection and enqueues it on
549 * the accept queue for the listent xprt. When the listen thread is kicked, it
550 * will call the recvfrom method on the listen xprt which will accept the new
553 static void handle_connect_req(struct rdma_cm_id
*new_cma_id
, size_t client_ird
)
555 struct svcxprt_rdma
*listen_xprt
= new_cma_id
->context
;
556 struct svcxprt_rdma
*newxprt
;
559 /* Create a new transport */
560 newxprt
= rdma_create_xprt(listen_xprt
->sc_xprt
.xpt_server
, 0);
562 dprintk("svcrdma: failed to create new transport\n");
565 newxprt
->sc_cm_id
= new_cma_id
;
566 new_cma_id
->context
= newxprt
;
567 dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
568 newxprt
, newxprt
->sc_cm_id
, listen_xprt
);
570 /* Save client advertised inbound read limit for use later in accept. */
571 newxprt
->sc_ord
= client_ird
;
573 /* Set the local and remote addresses in the transport */
574 sa
= (struct sockaddr
*)&newxprt
->sc_cm_id
->route
.addr
.dst_addr
;
575 svc_xprt_set_remote(&newxprt
->sc_xprt
, sa
, svc_addr_len(sa
));
576 sa
= (struct sockaddr
*)&newxprt
->sc_cm_id
->route
.addr
.src_addr
;
577 svc_xprt_set_local(&newxprt
->sc_xprt
, sa
, svc_addr_len(sa
));
580 * Enqueue the new transport on the accept queue of the listening
583 spin_lock_bh(&listen_xprt
->sc_lock
);
584 list_add_tail(&newxprt
->sc_accept_q
, &listen_xprt
->sc_accept_q
);
585 spin_unlock_bh(&listen_xprt
->sc_lock
);
588 * Can't use svc_xprt_received here because we are not on a
591 set_bit(XPT_CONN
, &listen_xprt
->sc_xprt
.xpt_flags
);
592 svc_xprt_enqueue(&listen_xprt
->sc_xprt
);
596 * Handles events generated on the listening endpoint. These events will be
597 * either be incoming connect requests or adapter removal events.
599 static int rdma_listen_handler(struct rdma_cm_id
*cma_id
,
600 struct rdma_cm_event
*event
)
602 struct svcxprt_rdma
*xprt
= cma_id
->context
;
605 switch (event
->event
) {
606 case RDMA_CM_EVENT_CONNECT_REQUEST
:
607 dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
608 "event=%d\n", cma_id
, cma_id
->context
, event
->event
);
609 handle_connect_req(cma_id
,
610 event
->param
.conn
.initiator_depth
);
613 case RDMA_CM_EVENT_ESTABLISHED
:
614 /* Accept complete */
615 dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
616 "cm_id=%p\n", xprt
, cma_id
);
619 case RDMA_CM_EVENT_DEVICE_REMOVAL
:
620 dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
623 set_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
);
627 dprintk("svcrdma: Unexpected event on listening endpoint %p, "
628 "event=%d\n", cma_id
, event
->event
);
635 static int rdma_cma_handler(struct rdma_cm_id
*cma_id
,
636 struct rdma_cm_event
*event
)
638 struct svc_xprt
*xprt
= cma_id
->context
;
639 struct svcxprt_rdma
*rdma
=
640 container_of(xprt
, struct svcxprt_rdma
, sc_xprt
);
641 switch (event
->event
) {
642 case RDMA_CM_EVENT_ESTABLISHED
:
643 /* Accept complete */
645 dprintk("svcrdma: Connection completed on DTO xprt=%p, "
646 "cm_id=%p\n", xprt
, cma_id
);
647 clear_bit(RDMAXPRT_CONN_PENDING
, &rdma
->sc_flags
);
648 svc_xprt_enqueue(xprt
);
650 case RDMA_CM_EVENT_DISCONNECTED
:
651 dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
654 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
655 svc_xprt_enqueue(xprt
);
659 case RDMA_CM_EVENT_DEVICE_REMOVAL
:
660 dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
661 "event=%d\n", cma_id
, xprt
, event
->event
);
663 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
664 svc_xprt_enqueue(xprt
);
668 dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
669 "event=%d\n", cma_id
, event
->event
);
676 * Create a listening RDMA service endpoint.
678 static struct svc_xprt
*svc_rdma_create(struct svc_serv
*serv
,
680 struct sockaddr
*sa
, int salen
,
683 struct rdma_cm_id
*listen_id
;
684 struct svcxprt_rdma
*cma_xprt
;
685 struct svc_xprt
*xprt
;
688 dprintk("svcrdma: Creating RDMA socket\n");
689 if (sa
->sa_family
!= AF_INET
) {
690 dprintk("svcrdma: Address family %d is not supported.\n", sa
->sa_family
);
691 return ERR_PTR(-EAFNOSUPPORT
);
693 cma_xprt
= rdma_create_xprt(serv
, 1);
695 return ERR_PTR(-ENOMEM
);
696 xprt
= &cma_xprt
->sc_xprt
;
698 listen_id
= rdma_create_id(rdma_listen_handler
, cma_xprt
, RDMA_PS_TCP
);
699 if (IS_ERR(listen_id
)) {
700 ret
= PTR_ERR(listen_id
);
701 dprintk("svcrdma: rdma_create_id failed = %d\n", ret
);
705 ret
= rdma_bind_addr(listen_id
, sa
);
707 dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret
);
710 cma_xprt
->sc_cm_id
= listen_id
;
712 ret
= rdma_listen(listen_id
, RPCRDMA_LISTEN_BACKLOG
);
714 dprintk("svcrdma: rdma_listen failed = %d\n", ret
);
719 * We need to use the address from the cm_id in case the
720 * caller specified 0 for the port number.
722 sa
= (struct sockaddr
*)&cma_xprt
->sc_cm_id
->route
.addr
.src_addr
;
723 svc_xprt_set_local(&cma_xprt
->sc_xprt
, sa
, salen
);
725 return &cma_xprt
->sc_xprt
;
728 rdma_destroy_id(listen_id
);
734 static struct svc_rdma_fastreg_mr
*rdma_alloc_frmr(struct svcxprt_rdma
*xprt
)
737 struct ib_fast_reg_page_list
*pl
;
738 struct svc_rdma_fastreg_mr
*frmr
;
740 frmr
= kmalloc(sizeof(*frmr
), GFP_KERNEL
);
744 mr
= ib_alloc_fast_reg_mr(xprt
->sc_pd
, RPCSVC_MAXPAGES
);
748 pl
= ib_alloc_fast_reg_page_list(xprt
->sc_cm_id
->device
,
754 frmr
->page_list
= pl
;
755 INIT_LIST_HEAD(&frmr
->frmr_list
);
763 return ERR_PTR(-ENOMEM
);
766 static void rdma_dealloc_frmr_q(struct svcxprt_rdma
*xprt
)
768 struct svc_rdma_fastreg_mr
*frmr
;
770 while (!list_empty(&xprt
->sc_frmr_q
)) {
771 frmr
= list_entry(xprt
->sc_frmr_q
.next
,
772 struct svc_rdma_fastreg_mr
, frmr_list
);
773 list_del_init(&frmr
->frmr_list
);
774 ib_dereg_mr(frmr
->mr
);
775 ib_free_fast_reg_page_list(frmr
->page_list
);
780 struct svc_rdma_fastreg_mr
*svc_rdma_get_frmr(struct svcxprt_rdma
*rdma
)
782 struct svc_rdma_fastreg_mr
*frmr
= NULL
;
784 spin_lock_bh(&rdma
->sc_frmr_q_lock
);
785 if (!list_empty(&rdma
->sc_frmr_q
)) {
786 frmr
= list_entry(rdma
->sc_frmr_q
.next
,
787 struct svc_rdma_fastreg_mr
, frmr_list
);
788 list_del_init(&frmr
->frmr_list
);
790 frmr
->page_list_len
= 0;
792 spin_unlock_bh(&rdma
->sc_frmr_q_lock
);
796 return rdma_alloc_frmr(rdma
);
799 static void frmr_unmap_dma(struct svcxprt_rdma
*xprt
,
800 struct svc_rdma_fastreg_mr
*frmr
)
803 for (page_no
= 0; page_no
< frmr
->page_list_len
; page_no
++) {
804 dma_addr_t addr
= frmr
->page_list
->page_list
[page_no
];
805 if (ib_dma_mapping_error(frmr
->mr
->device
, addr
))
807 atomic_dec(&xprt
->sc_dma_used
);
808 ib_dma_unmap_page(frmr
->mr
->device
, addr
, PAGE_SIZE
,
813 void svc_rdma_put_frmr(struct svcxprt_rdma
*rdma
,
814 struct svc_rdma_fastreg_mr
*frmr
)
817 frmr_unmap_dma(rdma
, frmr
);
818 spin_lock_bh(&rdma
->sc_frmr_q_lock
);
819 BUG_ON(!list_empty(&frmr
->frmr_list
));
820 list_add(&frmr
->frmr_list
, &rdma
->sc_frmr_q
);
821 spin_unlock_bh(&rdma
->sc_frmr_q_lock
);
826 * This is the xpo_recvfrom function for listening endpoints. Its
827 * purpose is to accept incoming connections. The CMA callback handler
828 * has already created a new transport and attached it to the new CMA
831 * There is a queue of pending connections hung on the listening
832 * transport. This queue contains the new svc_xprt structure. This
833 * function takes svc_xprt structures off the accept_q and completes
836 static struct svc_xprt
*svc_rdma_accept(struct svc_xprt
*xprt
)
838 struct svcxprt_rdma
*listen_rdma
;
839 struct svcxprt_rdma
*newxprt
= NULL
;
840 struct rdma_conn_param conn_param
;
841 struct ib_qp_init_attr qp_attr
;
842 struct ib_device_attr devattr
;
843 int uninitialized_var(dma_mr_acc
);
848 listen_rdma
= container_of(xprt
, struct svcxprt_rdma
, sc_xprt
);
849 clear_bit(XPT_CONN
, &xprt
->xpt_flags
);
850 /* Get the next entry off the accept list */
851 spin_lock_bh(&listen_rdma
->sc_lock
);
852 if (!list_empty(&listen_rdma
->sc_accept_q
)) {
853 newxprt
= list_entry(listen_rdma
->sc_accept_q
.next
,
854 struct svcxprt_rdma
, sc_accept_q
);
855 list_del_init(&newxprt
->sc_accept_q
);
857 if (!list_empty(&listen_rdma
->sc_accept_q
))
858 set_bit(XPT_CONN
, &listen_rdma
->sc_xprt
.xpt_flags
);
859 spin_unlock_bh(&listen_rdma
->sc_lock
);
863 dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
864 newxprt
, newxprt
->sc_cm_id
);
866 ret
= ib_query_device(newxprt
->sc_cm_id
->device
, &devattr
);
868 dprintk("svcrdma: could not query device attributes on "
869 "device %p, rc=%d\n", newxprt
->sc_cm_id
->device
, ret
);
873 /* Qualify the transport resource defaults with the
874 * capabilities of this particular device */
875 newxprt
->sc_max_sge
= min((size_t)devattr
.max_sge
,
876 (size_t)RPCSVC_MAXPAGES
);
877 newxprt
->sc_max_requests
= min((size_t)devattr
.max_qp_wr
,
878 (size_t)svcrdma_max_requests
);
879 newxprt
->sc_sq_depth
= RPCRDMA_SQ_DEPTH_MULT
* newxprt
->sc_max_requests
;
882 * Limit ORD based on client limit, local device limit, and
883 * configured svcrdma limit.
885 newxprt
->sc_ord
= min_t(size_t, devattr
.max_qp_rd_atom
, newxprt
->sc_ord
);
886 newxprt
->sc_ord
= min_t(size_t, svcrdma_ord
, newxprt
->sc_ord
);
888 newxprt
->sc_pd
= ib_alloc_pd(newxprt
->sc_cm_id
->device
);
889 if (IS_ERR(newxprt
->sc_pd
)) {
890 dprintk("svcrdma: error creating PD for connect request\n");
893 newxprt
->sc_sq_cq
= ib_create_cq(newxprt
->sc_cm_id
->device
,
897 newxprt
->sc_sq_depth
,
899 if (IS_ERR(newxprt
->sc_sq_cq
)) {
900 dprintk("svcrdma: error creating SQ CQ for connect request\n");
903 newxprt
->sc_rq_cq
= ib_create_cq(newxprt
->sc_cm_id
->device
,
907 newxprt
->sc_max_requests
,
909 if (IS_ERR(newxprt
->sc_rq_cq
)) {
910 dprintk("svcrdma: error creating RQ CQ for connect request\n");
914 memset(&qp_attr
, 0, sizeof qp_attr
);
915 qp_attr
.event_handler
= qp_event_handler
;
916 qp_attr
.qp_context
= &newxprt
->sc_xprt
;
917 qp_attr
.cap
.max_send_wr
= newxprt
->sc_sq_depth
;
918 qp_attr
.cap
.max_recv_wr
= newxprt
->sc_max_requests
;
919 qp_attr
.cap
.max_send_sge
= newxprt
->sc_max_sge
;
920 qp_attr
.cap
.max_recv_sge
= newxprt
->sc_max_sge
;
921 qp_attr
.sq_sig_type
= IB_SIGNAL_REQ_WR
;
922 qp_attr
.qp_type
= IB_QPT_RC
;
923 qp_attr
.send_cq
= newxprt
->sc_sq_cq
;
924 qp_attr
.recv_cq
= newxprt
->sc_rq_cq
;
925 dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n"
926 " cm_id->device=%p, sc_pd->device=%p\n"
927 " cap.max_send_wr = %d\n"
928 " cap.max_recv_wr = %d\n"
929 " cap.max_send_sge = %d\n"
930 " cap.max_recv_sge = %d\n",
931 newxprt
->sc_cm_id
, newxprt
->sc_pd
,
932 newxprt
->sc_cm_id
->device
, newxprt
->sc_pd
->device
,
933 qp_attr
.cap
.max_send_wr
,
934 qp_attr
.cap
.max_recv_wr
,
935 qp_attr
.cap
.max_send_sge
,
936 qp_attr
.cap
.max_recv_sge
);
938 ret
= rdma_create_qp(newxprt
->sc_cm_id
, newxprt
->sc_pd
, &qp_attr
);
941 * XXX: This is a hack. We need a xx_request_qp interface
942 * that will adjust the qp_attr's with a best-effort
945 qp_attr
.cap
.max_send_sge
-= 2;
946 qp_attr
.cap
.max_recv_sge
-= 2;
947 ret
= rdma_create_qp(newxprt
->sc_cm_id
, newxprt
->sc_pd
,
950 dprintk("svcrdma: failed to create QP, ret=%d\n", ret
);
953 newxprt
->sc_max_sge
= qp_attr
.cap
.max_send_sge
;
954 newxprt
->sc_max_sge
= qp_attr
.cap
.max_recv_sge
;
955 newxprt
->sc_sq_depth
= qp_attr
.cap
.max_send_wr
;
956 newxprt
->sc_max_requests
= qp_attr
.cap
.max_recv_wr
;
958 newxprt
->sc_qp
= newxprt
->sc_cm_id
->qp
;
961 * Use the most secure set of MR resources based on the
962 * transport type and available memory management features in
963 * the device. Here's the table implemented below:
965 * Fast Global DMA Remote WR
967 * Sup'd Sup'd Needed Needed
979 * NB: iWARP requires remote write access for the data sink
980 * of an RDMA_READ. IB does not.
982 if (devattr
.device_cap_flags
& IB_DEVICE_MEM_MGT_EXTENSIONS
) {
983 newxprt
->sc_frmr_pg_list_len
=
984 devattr
.max_fast_reg_page_list_len
;
985 newxprt
->sc_dev_caps
|= SVCRDMA_DEVCAP_FAST_REG
;
989 * Determine if a DMA MR is required and if so, what privs are required
991 switch (rdma_node_get_transport(newxprt
->sc_cm_id
->device
->node_type
)) {
992 case RDMA_TRANSPORT_IWARP
:
993 newxprt
->sc_dev_caps
|= SVCRDMA_DEVCAP_READ_W_INV
;
994 if (!(newxprt
->sc_dev_caps
& SVCRDMA_DEVCAP_FAST_REG
)) {
997 (IB_ACCESS_LOCAL_WRITE
|
998 IB_ACCESS_REMOTE_WRITE
);
999 } else if (!(devattr
.device_cap_flags
& IB_DEVICE_LOCAL_DMA_LKEY
)) {
1001 dma_mr_acc
= IB_ACCESS_LOCAL_WRITE
;
1005 case RDMA_TRANSPORT_IB
:
1006 if (!(devattr
.device_cap_flags
& IB_DEVICE_LOCAL_DMA_LKEY
)) {
1008 dma_mr_acc
= IB_ACCESS_LOCAL_WRITE
;
1016 /* Create the DMA MR if needed, otherwise, use the DMA LKEY */
1018 /* Register all of physical memory */
1019 newxprt
->sc_phys_mr
=
1020 ib_get_dma_mr(newxprt
->sc_pd
, dma_mr_acc
);
1021 if (IS_ERR(newxprt
->sc_phys_mr
)) {
1022 dprintk("svcrdma: Failed to create DMA MR ret=%d\n",
1026 newxprt
->sc_dma_lkey
= newxprt
->sc_phys_mr
->lkey
;
1028 newxprt
->sc_dma_lkey
=
1029 newxprt
->sc_cm_id
->device
->local_dma_lkey
;
1031 /* Post receive buffers */
1032 for (i
= 0; i
< newxprt
->sc_max_requests
; i
++) {
1033 ret
= svc_rdma_post_recv(newxprt
);
1035 dprintk("svcrdma: failure posting receive buffers\n");
1040 /* Swap out the handler */
1041 newxprt
->sc_cm_id
->event_handler
= rdma_cma_handler
;
1044 * Arm the CQs for the SQ and RQ before accepting so we can't
1045 * miss the first message
1047 ib_req_notify_cq(newxprt
->sc_sq_cq
, IB_CQ_NEXT_COMP
);
1048 ib_req_notify_cq(newxprt
->sc_rq_cq
, IB_CQ_NEXT_COMP
);
1050 /* Accept Connection */
1051 set_bit(RDMAXPRT_CONN_PENDING
, &newxprt
->sc_flags
);
1052 memset(&conn_param
, 0, sizeof conn_param
);
1053 conn_param
.responder_resources
= 0;
1054 conn_param
.initiator_depth
= newxprt
->sc_ord
;
1055 ret
= rdma_accept(newxprt
->sc_cm_id
, &conn_param
);
1057 dprintk("svcrdma: failed to accept new connection, ret=%d\n",
1062 dprintk("svcrdma: new connection %p accepted with the following "
1064 " local_ip : %pI4\n"
1065 " local_port : %d\n"
1066 " remote_ip : %pI4\n"
1067 " remote_port : %d\n"
1070 " max_requests : %d\n"
1073 &((struct sockaddr_in
*)&newxprt
->sc_cm_id
->
1074 route
.addr
.src_addr
)->sin_addr
.s_addr
,
1075 ntohs(((struct sockaddr_in
*)&newxprt
->sc_cm_id
->
1076 route
.addr
.src_addr
)->sin_port
),
1077 &((struct sockaddr_in
*)&newxprt
->sc_cm_id
->
1078 route
.addr
.dst_addr
)->sin_addr
.s_addr
,
1079 ntohs(((struct sockaddr_in
*)&newxprt
->sc_cm_id
->
1080 route
.addr
.dst_addr
)->sin_port
),
1081 newxprt
->sc_max_sge
,
1082 newxprt
->sc_sq_depth
,
1083 newxprt
->sc_max_requests
,
1086 return &newxprt
->sc_xprt
;
1089 dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret
);
1090 /* Take a reference in case the DTO handler runs */
1091 svc_xprt_get(&newxprt
->sc_xprt
);
1092 if (newxprt
->sc_qp
&& !IS_ERR(newxprt
->sc_qp
))
1093 ib_destroy_qp(newxprt
->sc_qp
);
1094 rdma_destroy_id(newxprt
->sc_cm_id
);
1095 /* This call to put will destroy the transport */
1096 svc_xprt_put(&newxprt
->sc_xprt
);
1100 static void svc_rdma_release_rqst(struct svc_rqst
*rqstp
)
1105 * When connected, an svc_xprt has at least two references:
1107 * - A reference held by the cm_id between the ESTABLISHED and
1108 * DISCONNECTED events. If the remote peer disconnected first, this
1109 * reference could be gone.
1111 * - A reference held by the svc_recv code that called this function
1112 * as part of close processing.
1114 * At a minimum one references should still be held.
1116 static void svc_rdma_detach(struct svc_xprt
*xprt
)
1118 struct svcxprt_rdma
*rdma
=
1119 container_of(xprt
, struct svcxprt_rdma
, sc_xprt
);
1120 dprintk("svc: svc_rdma_detach(%p)\n", xprt
);
1122 /* Disconnect and flush posted WQE */
1123 rdma_disconnect(rdma
->sc_cm_id
);
1126 static void __svc_rdma_free(struct work_struct
*work
)
1128 struct svcxprt_rdma
*rdma
=
1129 container_of(work
, struct svcxprt_rdma
, sc_work
);
1130 dprintk("svcrdma: svc_rdma_free(%p)\n", rdma
);
1132 /* We should only be called from kref_put */
1133 BUG_ON(atomic_read(&rdma
->sc_xprt
.xpt_ref
.refcount
) != 0);
1136 * Destroy queued, but not processed read completions. Note
1137 * that this cleanup has to be done before destroying the
1138 * cm_id because the device ptr is needed to unmap the dma in
1139 * svc_rdma_put_context.
1141 while (!list_empty(&rdma
->sc_read_complete_q
)) {
1142 struct svc_rdma_op_ctxt
*ctxt
;
1143 ctxt
= list_entry(rdma
->sc_read_complete_q
.next
,
1144 struct svc_rdma_op_ctxt
,
1146 list_del_init(&ctxt
->dto_q
);
1147 svc_rdma_put_context(ctxt
, 1);
1150 /* Destroy queued, but not processed recv completions */
1151 while (!list_empty(&rdma
->sc_rq_dto_q
)) {
1152 struct svc_rdma_op_ctxt
*ctxt
;
1153 ctxt
= list_entry(rdma
->sc_rq_dto_q
.next
,
1154 struct svc_rdma_op_ctxt
,
1156 list_del_init(&ctxt
->dto_q
);
1157 svc_rdma_put_context(ctxt
, 1);
1160 /* Warn if we leaked a resource or under-referenced */
1161 WARN_ON(atomic_read(&rdma
->sc_ctxt_used
) != 0);
1162 WARN_ON(atomic_read(&rdma
->sc_dma_used
) != 0);
1164 /* De-allocate fastreg mr */
1165 rdma_dealloc_frmr_q(rdma
);
1167 /* Destroy the QP if present (not a listener) */
1168 if (rdma
->sc_qp
&& !IS_ERR(rdma
->sc_qp
))
1169 ib_destroy_qp(rdma
->sc_qp
);
1171 if (rdma
->sc_sq_cq
&& !IS_ERR(rdma
->sc_sq_cq
))
1172 ib_destroy_cq(rdma
->sc_sq_cq
);
1174 if (rdma
->sc_rq_cq
&& !IS_ERR(rdma
->sc_rq_cq
))
1175 ib_destroy_cq(rdma
->sc_rq_cq
);
1177 if (rdma
->sc_phys_mr
&& !IS_ERR(rdma
->sc_phys_mr
))
1178 ib_dereg_mr(rdma
->sc_phys_mr
);
1180 if (rdma
->sc_pd
&& !IS_ERR(rdma
->sc_pd
))
1181 ib_dealloc_pd(rdma
->sc_pd
);
1183 /* Destroy the CM ID */
1184 rdma_destroy_id(rdma
->sc_cm_id
);
1189 static void svc_rdma_free(struct svc_xprt
*xprt
)
1191 struct svcxprt_rdma
*rdma
=
1192 container_of(xprt
, struct svcxprt_rdma
, sc_xprt
);
1193 INIT_WORK(&rdma
->sc_work
, __svc_rdma_free
);
1194 queue_work(svc_rdma_wq
, &rdma
->sc_work
);
1197 static int svc_rdma_has_wspace(struct svc_xprt
*xprt
)
1199 struct svcxprt_rdma
*rdma
=
1200 container_of(xprt
, struct svcxprt_rdma
, sc_xprt
);
1203 * If there are fewer SQ WR available than required to send a
1204 * simple response, return false.
1206 if ((rdma
->sc_sq_depth
- atomic_read(&rdma
->sc_sq_count
) < 3))
1210 * ...or there are already waiters on the SQ,
1213 if (waitqueue_active(&rdma
->sc_send_wait
))
1216 /* Otherwise return true. */
1221 * Attempt to register the kvec representing the RPC memory with the
1225 * NULL : The device does not support fastreg or there were no more
1227 * frmr : The kvec register request was successfully posted.
1228 * <0 : An error was encountered attempting to register the kvec.
1230 int svc_rdma_fastreg(struct svcxprt_rdma
*xprt
,
1231 struct svc_rdma_fastreg_mr
*frmr
)
1233 struct ib_send_wr fastreg_wr
;
1237 key
= (u8
)(frmr
->mr
->lkey
& 0x000000FF);
1238 ib_update_fast_reg_key(frmr
->mr
, ++key
);
1240 /* Prepare FASTREG WR */
1241 memset(&fastreg_wr
, 0, sizeof fastreg_wr
);
1242 fastreg_wr
.opcode
= IB_WR_FAST_REG_MR
;
1243 fastreg_wr
.send_flags
= IB_SEND_SIGNALED
;
1244 fastreg_wr
.wr
.fast_reg
.iova_start
= (unsigned long)frmr
->kva
;
1245 fastreg_wr
.wr
.fast_reg
.page_list
= frmr
->page_list
;
1246 fastreg_wr
.wr
.fast_reg
.page_list_len
= frmr
->page_list_len
;
1247 fastreg_wr
.wr
.fast_reg
.page_shift
= PAGE_SHIFT
;
1248 fastreg_wr
.wr
.fast_reg
.length
= frmr
->map_len
;
1249 fastreg_wr
.wr
.fast_reg
.access_flags
= frmr
->access_flags
;
1250 fastreg_wr
.wr
.fast_reg
.rkey
= frmr
->mr
->lkey
;
1251 return svc_rdma_send(xprt
, &fastreg_wr
);
1254 int svc_rdma_send(struct svcxprt_rdma
*xprt
, struct ib_send_wr
*wr
)
1256 struct ib_send_wr
*bad_wr
, *n_wr
;
1261 if (test_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
))
1264 BUG_ON(wr
->send_flags
!= IB_SEND_SIGNALED
);
1266 for (n_wr
= wr
->next
; n_wr
; n_wr
= n_wr
->next
)
1269 /* If the SQ is full, wait until an SQ entry is available */
1271 spin_lock_bh(&xprt
->sc_lock
);
1272 if (xprt
->sc_sq_depth
< atomic_read(&xprt
->sc_sq_count
) + wr_count
) {
1273 spin_unlock_bh(&xprt
->sc_lock
);
1274 atomic_inc(&rdma_stat_sq_starve
);
1276 /* See if we can opportunistically reap SQ WR to make room */
1279 /* Wait until SQ WR available if SQ still full */
1280 wait_event(xprt
->sc_send_wait
,
1281 atomic_read(&xprt
->sc_sq_count
) <
1283 if (test_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
))
1287 /* Take a transport ref for each WR posted */
1288 for (i
= 0; i
< wr_count
; i
++)
1289 svc_xprt_get(&xprt
->sc_xprt
);
1291 /* Bump used SQ WR count and post */
1292 atomic_add(wr_count
, &xprt
->sc_sq_count
);
1293 ret
= ib_post_send(xprt
->sc_qp
, wr
, &bad_wr
);
1295 set_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
);
1296 atomic_sub(wr_count
, &xprt
->sc_sq_count
);
1297 for (i
= 0; i
< wr_count
; i
++)
1298 svc_xprt_put(&xprt
->sc_xprt
);
1299 dprintk("svcrdma: failed to post SQ WR rc=%d, "
1300 "sc_sq_count=%d, sc_sq_depth=%d\n",
1301 ret
, atomic_read(&xprt
->sc_sq_count
),
1304 spin_unlock_bh(&xprt
->sc_lock
);
1306 wake_up(&xprt
->sc_send_wait
);
1312 void svc_rdma_send_error(struct svcxprt_rdma
*xprt
, struct rpcrdma_msg
*rmsgp
,
1313 enum rpcrdma_errcode err
)
1315 struct ib_send_wr err_wr
;
1317 struct svc_rdma_op_ctxt
*ctxt
;
1322 p
= svc_rdma_get_page();
1323 va
= page_address(p
);
1325 /* XDR encode error */
1326 length
= svc_rdma_xdr_encode_error(xprt
, rmsgp
, err
, va
);
1328 ctxt
= svc_rdma_get_context(xprt
);
1329 ctxt
->direction
= DMA_FROM_DEVICE
;
1333 /* Prepare SGE for local address */
1334 ctxt
->sge
[0].addr
= ib_dma_map_page(xprt
->sc_cm_id
->device
,
1335 p
, 0, length
, DMA_FROM_DEVICE
);
1336 if (ib_dma_mapping_error(xprt
->sc_cm_id
->device
, ctxt
->sge
[0].addr
)) {
1338 svc_rdma_put_context(ctxt
, 1);
1341 atomic_inc(&xprt
->sc_dma_used
);
1342 ctxt
->sge
[0].lkey
= xprt
->sc_dma_lkey
;
1343 ctxt
->sge
[0].length
= length
;
1345 /* Prepare SEND WR */
1346 memset(&err_wr
, 0, sizeof err_wr
);
1347 ctxt
->wr_op
= IB_WR_SEND
;
1348 err_wr
.wr_id
= (unsigned long)ctxt
;
1349 err_wr
.sg_list
= ctxt
->sge
;
1351 err_wr
.opcode
= IB_WR_SEND
;
1352 err_wr
.send_flags
= IB_SEND_SIGNALED
;
1355 ret
= svc_rdma_send(xprt
, &err_wr
);
1357 dprintk("svcrdma: Error %d posting send for protocol error\n",
1359 svc_rdma_unmap_dma(ctxt
);
1360 svc_rdma_put_context(ctxt
, 1);