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>
54 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
56 static struct svc_xprt
*svc_rdma_create(struct svc_serv
*serv
,
58 struct sockaddr
*sa
, int salen
,
60 static struct svc_xprt
*svc_rdma_accept(struct svc_xprt
*xprt
);
61 static void svc_rdma_release_rqst(struct svc_rqst
*);
62 static void dto_tasklet_func(unsigned long data
);
63 static void svc_rdma_detach(struct svc_xprt
*xprt
);
64 static void svc_rdma_free(struct svc_xprt
*xprt
);
65 static int svc_rdma_has_wspace(struct svc_xprt
*xprt
);
66 static void rq_cq_reap(struct svcxprt_rdma
*xprt
);
67 static void sq_cq_reap(struct svcxprt_rdma
*xprt
);
69 static DECLARE_TASKLET(dto_tasklet
, dto_tasklet_func
, 0UL);
70 static DEFINE_SPINLOCK(dto_lock
);
71 static LIST_HEAD(dto_xprt_q
);
73 static struct svc_xprt_ops svc_rdma_ops
= {
74 .xpo_create
= svc_rdma_create
,
75 .xpo_recvfrom
= svc_rdma_recvfrom
,
76 .xpo_sendto
= svc_rdma_sendto
,
77 .xpo_release_rqst
= svc_rdma_release_rqst
,
78 .xpo_detach
= svc_rdma_detach
,
79 .xpo_free
= svc_rdma_free
,
80 .xpo_prep_reply_hdr
= svc_rdma_prep_reply_hdr
,
81 .xpo_has_wspace
= svc_rdma_has_wspace
,
82 .xpo_accept
= svc_rdma_accept
,
85 struct svc_xprt_class svc_rdma_class
= {
87 .xcl_owner
= THIS_MODULE
,
88 .xcl_ops
= &svc_rdma_ops
,
89 .xcl_max_payload
= RPCSVC_MAXPAYLOAD_TCP
,
92 /* WR context cache. Created in svc_rdma.c */
93 extern struct kmem_cache
*svc_rdma_ctxt_cachep
;
95 /* Workqueue created in svc_rdma.c */
96 extern struct workqueue_struct
*svc_rdma_wq
;
98 struct svc_rdma_op_ctxt
*svc_rdma_get_context(struct svcxprt_rdma
*xprt
)
100 struct svc_rdma_op_ctxt
*ctxt
;
103 ctxt
= kmem_cache_alloc(svc_rdma_ctxt_cachep
, GFP_KERNEL
);
106 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
109 INIT_LIST_HEAD(&ctxt
->dto_q
);
112 atomic_inc(&xprt
->sc_ctxt_used
);
116 void svc_rdma_unmap_dma(struct svc_rdma_op_ctxt
*ctxt
)
118 struct svcxprt_rdma
*xprt
= ctxt
->xprt
;
120 for (i
= 0; i
< ctxt
->count
&& ctxt
->sge
[i
].length
; i
++) {
122 * Unmap the DMA addr in the SGE if the lkey matches
123 * the sc_dma_lkey, otherwise, ignore it since it is
124 * an FRMR lkey and will be unmapped later when the
125 * last WR that uses it completes.
127 if (ctxt
->sge
[i
].lkey
== xprt
->sc_dma_lkey
) {
128 atomic_dec(&xprt
->sc_dma_used
);
129 ib_dma_unmap_page(xprt
->sc_cm_id
->device
,
137 void svc_rdma_put_context(struct svc_rdma_op_ctxt
*ctxt
, int free_pages
)
139 struct svcxprt_rdma
*xprt
;
145 for (i
= 0; i
< ctxt
->count
; i
++)
146 put_page(ctxt
->pages
[i
]);
148 kmem_cache_free(svc_rdma_ctxt_cachep
, ctxt
);
149 atomic_dec(&xprt
->sc_ctxt_used
);
152 /* Temporary NFS request map cache. Created in svc_rdma.c */
153 extern struct kmem_cache
*svc_rdma_map_cachep
;
156 * Temporary NFS req mappings are shared across all transport
157 * instances. These are short lived and should be bounded by the number
158 * of concurrent server threads * depth of the SQ.
160 struct svc_rdma_req_map
*svc_rdma_get_req_map(void)
162 struct svc_rdma_req_map
*map
;
164 map
= kmem_cache_alloc(svc_rdma_map_cachep
, GFP_KERNEL
);
167 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
174 void svc_rdma_put_req_map(struct svc_rdma_req_map
*map
)
176 kmem_cache_free(svc_rdma_map_cachep
, map
);
179 /* ib_cq event handler */
180 static void cq_event_handler(struct ib_event
*event
, void *context
)
182 struct svc_xprt
*xprt
= context
;
183 dprintk("svcrdma: received CQ event id=%d, context=%p\n",
184 event
->event
, context
);
185 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
188 /* QP event handler */
189 static void qp_event_handler(struct ib_event
*event
, void *context
)
191 struct svc_xprt
*xprt
= context
;
193 switch (event
->event
) {
194 /* These are considered benign events */
195 case IB_EVENT_PATH_MIG
:
196 case IB_EVENT_COMM_EST
:
197 case IB_EVENT_SQ_DRAINED
:
198 case IB_EVENT_QP_LAST_WQE_REACHED
:
199 dprintk("svcrdma: QP event %d received for QP=%p\n",
200 event
->event
, event
->element
.qp
);
202 /* These are considered fatal events */
203 case IB_EVENT_PATH_MIG_ERR
:
204 case IB_EVENT_QP_FATAL
:
205 case IB_EVENT_QP_REQ_ERR
:
206 case IB_EVENT_QP_ACCESS_ERR
:
207 case IB_EVENT_DEVICE_FATAL
:
209 dprintk("svcrdma: QP ERROR event %d received for QP=%p, "
210 "closing transport\n",
211 event
->event
, event
->element
.qp
);
212 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
218 * Data Transfer Operation Tasklet
220 * Walks a list of transports with I/O pending, removing entries as
221 * they are added to the server's I/O pending list. Two bits indicate
222 * if SQ, RQ, or both have I/O pending. The dto_lock is an irqsave
223 * spinlock that serializes access to the transport list with the RQ
224 * and SQ interrupt handlers.
226 static void dto_tasklet_func(unsigned long data
)
228 struct svcxprt_rdma
*xprt
;
231 spin_lock_irqsave(&dto_lock
, flags
);
232 while (!list_empty(&dto_xprt_q
)) {
233 xprt
= list_entry(dto_xprt_q
.next
,
234 struct svcxprt_rdma
, sc_dto_q
);
235 list_del_init(&xprt
->sc_dto_q
);
236 spin_unlock_irqrestore(&dto_lock
, flags
);
241 svc_xprt_put(&xprt
->sc_xprt
);
242 spin_lock_irqsave(&dto_lock
, flags
);
244 spin_unlock_irqrestore(&dto_lock
, flags
);
248 * Receive Queue Completion Handler
250 * Since an RQ completion handler is called on interrupt context, we
251 * need to defer the handling of the I/O to a tasklet
253 static void rq_comp_handler(struct ib_cq
*cq
, void *cq_context
)
255 struct svcxprt_rdma
*xprt
= cq_context
;
258 /* Guard against unconditional flush call for destroyed QP */
259 if (atomic_read(&xprt
->sc_xprt
.xpt_ref
.refcount
)==0)
263 * Set the bit regardless of whether or not it's on the list
264 * because it may be on the list already due to an SQ
267 set_bit(RDMAXPRT_RQ_PENDING
, &xprt
->sc_flags
);
270 * If this transport is not already on the DTO transport queue,
273 spin_lock_irqsave(&dto_lock
, flags
);
274 if (list_empty(&xprt
->sc_dto_q
)) {
275 svc_xprt_get(&xprt
->sc_xprt
);
276 list_add_tail(&xprt
->sc_dto_q
, &dto_xprt_q
);
278 spin_unlock_irqrestore(&dto_lock
, flags
);
280 /* Tasklet does all the work to avoid irqsave locks. */
281 tasklet_schedule(&dto_tasklet
);
285 * rq_cq_reap - Process the RQ CQ.
287 * Take all completing WC off the CQE and enqueue the associated DTO
288 * context on the dto_q for the transport.
290 * Note that caller must hold a transport reference.
292 static void rq_cq_reap(struct svcxprt_rdma
*xprt
)
296 struct svc_rdma_op_ctxt
*ctxt
= NULL
;
298 if (!test_and_clear_bit(RDMAXPRT_RQ_PENDING
, &xprt
->sc_flags
))
301 ib_req_notify_cq(xprt
->sc_rq_cq
, IB_CQ_NEXT_COMP
);
302 atomic_inc(&rdma_stat_rq_poll
);
304 while ((ret
= ib_poll_cq(xprt
->sc_rq_cq
, 1, &wc
)) > 0) {
305 ctxt
= (struct svc_rdma_op_ctxt
*)(unsigned long)wc
.wr_id
;
306 ctxt
->wc_status
= wc
.status
;
307 ctxt
->byte_len
= wc
.byte_len
;
308 svc_rdma_unmap_dma(ctxt
);
309 if (wc
.status
!= IB_WC_SUCCESS
) {
310 /* Close the transport */
311 dprintk("svcrdma: transport closing putting ctxt %p\n", ctxt
);
312 set_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
);
313 svc_rdma_put_context(ctxt
, 1);
314 svc_xprt_put(&xprt
->sc_xprt
);
317 spin_lock_bh(&xprt
->sc_rq_dto_lock
);
318 list_add_tail(&ctxt
->dto_q
, &xprt
->sc_rq_dto_q
);
319 spin_unlock_bh(&xprt
->sc_rq_dto_lock
);
320 svc_xprt_put(&xprt
->sc_xprt
);
324 atomic_inc(&rdma_stat_rq_prod
);
326 set_bit(XPT_DATA
, &xprt
->sc_xprt
.xpt_flags
);
328 * If data arrived before established event,
329 * don't enqueue. This defers RPC I/O until the
330 * RDMA connection is complete.
332 if (!test_bit(RDMAXPRT_CONN_PENDING
, &xprt
->sc_flags
))
333 svc_xprt_enqueue(&xprt
->sc_xprt
);
337 * Process a completion context
339 static void process_context(struct svcxprt_rdma
*xprt
,
340 struct svc_rdma_op_ctxt
*ctxt
)
342 svc_rdma_unmap_dma(ctxt
);
344 switch (ctxt
->wr_op
) {
346 if (test_bit(RDMACTXT_F_FAST_UNREG
, &ctxt
->flags
))
347 svc_rdma_put_frmr(xprt
, ctxt
->frmr
);
348 svc_rdma_put_context(ctxt
, 1);
351 case IB_WR_RDMA_WRITE
:
352 svc_rdma_put_context(ctxt
, 0);
355 case IB_WR_RDMA_READ
:
356 case IB_WR_RDMA_READ_WITH_INV
:
357 if (test_bit(RDMACTXT_F_LAST_CTXT
, &ctxt
->flags
)) {
358 struct svc_rdma_op_ctxt
*read_hdr
= ctxt
->read_hdr
;
360 if (test_bit(RDMACTXT_F_FAST_UNREG
, &ctxt
->flags
))
361 svc_rdma_put_frmr(xprt
, ctxt
->frmr
);
362 spin_lock_bh(&xprt
->sc_rq_dto_lock
);
363 set_bit(XPT_DATA
, &xprt
->sc_xprt
.xpt_flags
);
364 list_add_tail(&read_hdr
->dto_q
,
365 &xprt
->sc_read_complete_q
);
366 spin_unlock_bh(&xprt
->sc_rq_dto_lock
);
367 svc_xprt_enqueue(&xprt
->sc_xprt
);
369 svc_rdma_put_context(ctxt
, 0);
373 printk(KERN_ERR
"svcrdma: unexpected completion type, "
381 * Send Queue Completion Handler - potentially called on interrupt context.
383 * Note that caller must hold a transport reference.
385 static void sq_cq_reap(struct svcxprt_rdma
*xprt
)
387 struct svc_rdma_op_ctxt
*ctxt
= NULL
;
389 struct ib_cq
*cq
= xprt
->sc_sq_cq
;
392 if (!test_and_clear_bit(RDMAXPRT_SQ_PENDING
, &xprt
->sc_flags
))
395 ib_req_notify_cq(xprt
->sc_sq_cq
, IB_CQ_NEXT_COMP
);
396 atomic_inc(&rdma_stat_sq_poll
);
397 while ((ret
= ib_poll_cq(cq
, 1, &wc
)) > 0) {
398 if (wc
.status
!= IB_WC_SUCCESS
)
399 /* Close the transport */
400 set_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
);
402 /* Decrement used SQ WR count */
403 atomic_dec(&xprt
->sc_sq_count
);
404 wake_up(&xprt
->sc_send_wait
);
406 ctxt
= (struct svc_rdma_op_ctxt
*)(unsigned long)wc
.wr_id
;
408 process_context(xprt
, ctxt
);
410 svc_xprt_put(&xprt
->sc_xprt
);
414 atomic_inc(&rdma_stat_sq_prod
);
417 static void sq_comp_handler(struct ib_cq
*cq
, void *cq_context
)
419 struct svcxprt_rdma
*xprt
= cq_context
;
422 /* Guard against unconditional flush call for destroyed QP */
423 if (atomic_read(&xprt
->sc_xprt
.xpt_ref
.refcount
)==0)
427 * Set the bit regardless of whether or not it's on the list
428 * because it may be on the list already due to an RQ
431 set_bit(RDMAXPRT_SQ_PENDING
, &xprt
->sc_flags
);
434 * If this transport is not already on the DTO transport queue,
437 spin_lock_irqsave(&dto_lock
, flags
);
438 if (list_empty(&xprt
->sc_dto_q
)) {
439 svc_xprt_get(&xprt
->sc_xprt
);
440 list_add_tail(&xprt
->sc_dto_q
, &dto_xprt_q
);
442 spin_unlock_irqrestore(&dto_lock
, flags
);
444 /* Tasklet does all the work to avoid irqsave locks. */
445 tasklet_schedule(&dto_tasklet
);
448 static struct svcxprt_rdma
*rdma_create_xprt(struct svc_serv
*serv
,
451 struct svcxprt_rdma
*cma_xprt
= kzalloc(sizeof *cma_xprt
, GFP_KERNEL
);
455 svc_xprt_init(&svc_rdma_class
, &cma_xprt
->sc_xprt
, serv
);
456 INIT_LIST_HEAD(&cma_xprt
->sc_accept_q
);
457 INIT_LIST_HEAD(&cma_xprt
->sc_dto_q
);
458 INIT_LIST_HEAD(&cma_xprt
->sc_rq_dto_q
);
459 INIT_LIST_HEAD(&cma_xprt
->sc_read_complete_q
);
460 INIT_LIST_HEAD(&cma_xprt
->sc_frmr_q
);
461 init_waitqueue_head(&cma_xprt
->sc_send_wait
);
463 spin_lock_init(&cma_xprt
->sc_lock
);
464 spin_lock_init(&cma_xprt
->sc_rq_dto_lock
);
465 spin_lock_init(&cma_xprt
->sc_frmr_q_lock
);
467 cma_xprt
->sc_ord
= svcrdma_ord
;
469 cma_xprt
->sc_max_req_size
= svcrdma_max_req_size
;
470 cma_xprt
->sc_max_requests
= svcrdma_max_requests
;
471 cma_xprt
->sc_sq_depth
= svcrdma_max_requests
* RPCRDMA_SQ_DEPTH_MULT
;
472 atomic_set(&cma_xprt
->sc_sq_count
, 0);
473 atomic_set(&cma_xprt
->sc_ctxt_used
, 0);
476 set_bit(XPT_LISTENER
, &cma_xprt
->sc_xprt
.xpt_flags
);
481 struct page
*svc_rdma_get_page(void)
485 while ((page
= alloc_page(GFP_KERNEL
)) == NULL
) {
486 /* If we can't get memory, wait a bit and try again */
487 printk(KERN_INFO
"svcrdma: out of memory...retrying in 1000 "
489 schedule_timeout_uninterruptible(msecs_to_jiffies(1000));
494 int svc_rdma_post_recv(struct svcxprt_rdma
*xprt
)
496 struct ib_recv_wr recv_wr
, *bad_recv_wr
;
497 struct svc_rdma_op_ctxt
*ctxt
;
504 ctxt
= svc_rdma_get_context(xprt
);
506 ctxt
->direction
= DMA_FROM_DEVICE
;
507 for (sge_no
= 0; buflen
< xprt
->sc_max_req_size
; sge_no
++) {
508 BUG_ON(sge_no
>= xprt
->sc_max_sge
);
509 page
= svc_rdma_get_page();
510 ctxt
->pages
[sge_no
] = page
;
511 pa
= ib_dma_map_page(xprt
->sc_cm_id
->device
,
514 if (ib_dma_mapping_error(xprt
->sc_cm_id
->device
, pa
))
516 atomic_inc(&xprt
->sc_dma_used
);
517 ctxt
->sge
[sge_no
].addr
= pa
;
518 ctxt
->sge
[sge_no
].length
= PAGE_SIZE
;
519 ctxt
->sge
[sge_no
].lkey
= xprt
->sc_dma_lkey
;
520 ctxt
->count
= sge_no
+ 1;
524 recv_wr
.sg_list
= &ctxt
->sge
[0];
525 recv_wr
.num_sge
= ctxt
->count
;
526 recv_wr
.wr_id
= (u64
)(unsigned long)ctxt
;
528 svc_xprt_get(&xprt
->sc_xprt
);
529 ret
= ib_post_recv(xprt
->sc_qp
, &recv_wr
, &bad_recv_wr
);
531 svc_rdma_unmap_dma(ctxt
);
532 svc_rdma_put_context(ctxt
, 1);
533 svc_xprt_put(&xprt
->sc_xprt
);
538 svc_rdma_unmap_dma(ctxt
);
539 svc_rdma_put_context(ctxt
, 1);
544 * This function handles the CONNECT_REQUEST event on a listening
545 * endpoint. It is passed the cma_id for the _new_ connection. The context in
546 * this cma_id is inherited from the listening cma_id and is the svc_xprt
547 * structure for the listening endpoint.
549 * This function creates a new xprt for the new connection and enqueues it on
550 * the accept queue for the listent xprt. When the listen thread is kicked, it
551 * will call the recvfrom method on the listen xprt which will accept the new
554 static void handle_connect_req(struct rdma_cm_id
*new_cma_id
, size_t client_ird
)
556 struct svcxprt_rdma
*listen_xprt
= new_cma_id
->context
;
557 struct svcxprt_rdma
*newxprt
;
560 /* Create a new transport */
561 newxprt
= rdma_create_xprt(listen_xprt
->sc_xprt
.xpt_server
, 0);
563 dprintk("svcrdma: failed to create new transport\n");
566 newxprt
->sc_cm_id
= new_cma_id
;
567 new_cma_id
->context
= newxprt
;
568 dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
569 newxprt
, newxprt
->sc_cm_id
, listen_xprt
);
571 /* Save client advertised inbound read limit for use later in accept. */
572 newxprt
->sc_ord
= client_ird
;
574 /* Set the local and remote addresses in the transport */
575 sa
= (struct sockaddr
*)&newxprt
->sc_cm_id
->route
.addr
.dst_addr
;
576 svc_xprt_set_remote(&newxprt
->sc_xprt
, sa
, svc_addr_len(sa
));
577 sa
= (struct sockaddr
*)&newxprt
->sc_cm_id
->route
.addr
.src_addr
;
578 svc_xprt_set_local(&newxprt
->sc_xprt
, sa
, svc_addr_len(sa
));
581 * Enqueue the new transport on the accept queue of the listening
584 spin_lock_bh(&listen_xprt
->sc_lock
);
585 list_add_tail(&newxprt
->sc_accept_q
, &listen_xprt
->sc_accept_q
);
586 spin_unlock_bh(&listen_xprt
->sc_lock
);
589 * Can't use svc_xprt_received here because we are not on a
592 set_bit(XPT_CONN
, &listen_xprt
->sc_xprt
.xpt_flags
);
593 svc_xprt_enqueue(&listen_xprt
->sc_xprt
);
597 * Handles events generated on the listening endpoint. These events will be
598 * either be incoming connect requests or adapter removal events.
600 static int rdma_listen_handler(struct rdma_cm_id
*cma_id
,
601 struct rdma_cm_event
*event
)
603 struct svcxprt_rdma
*xprt
= cma_id
->context
;
606 switch (event
->event
) {
607 case RDMA_CM_EVENT_CONNECT_REQUEST
:
608 dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
609 "event=%d\n", cma_id
, cma_id
->context
, event
->event
);
610 handle_connect_req(cma_id
,
611 event
->param
.conn
.initiator_depth
);
614 case RDMA_CM_EVENT_ESTABLISHED
:
615 /* Accept complete */
616 dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
617 "cm_id=%p\n", xprt
, cma_id
);
620 case RDMA_CM_EVENT_DEVICE_REMOVAL
:
621 dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
624 set_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
);
628 dprintk("svcrdma: Unexpected event on listening endpoint %p, "
629 "event=%d\n", cma_id
, event
->event
);
636 static int rdma_cma_handler(struct rdma_cm_id
*cma_id
,
637 struct rdma_cm_event
*event
)
639 struct svc_xprt
*xprt
= cma_id
->context
;
640 struct svcxprt_rdma
*rdma
=
641 container_of(xprt
, struct svcxprt_rdma
, sc_xprt
);
642 switch (event
->event
) {
643 case RDMA_CM_EVENT_ESTABLISHED
:
644 /* Accept complete */
646 dprintk("svcrdma: Connection completed on DTO xprt=%p, "
647 "cm_id=%p\n", xprt
, cma_id
);
648 clear_bit(RDMAXPRT_CONN_PENDING
, &rdma
->sc_flags
);
649 svc_xprt_enqueue(xprt
);
651 case RDMA_CM_EVENT_DISCONNECTED
:
652 dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
655 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
656 svc_xprt_enqueue(xprt
);
660 case RDMA_CM_EVENT_DEVICE_REMOVAL
:
661 dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
662 "event=%d\n", cma_id
, xprt
, event
->event
);
664 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
665 svc_xprt_enqueue(xprt
);
669 dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
670 "event=%d\n", cma_id
, event
->event
);
677 * Create a listening RDMA service endpoint.
679 static struct svc_xprt
*svc_rdma_create(struct svc_serv
*serv
,
681 struct sockaddr
*sa
, int salen
,
684 struct rdma_cm_id
*listen_id
;
685 struct svcxprt_rdma
*cma_xprt
;
686 struct svc_xprt
*xprt
;
689 dprintk("svcrdma: Creating RDMA socket\n");
690 if (sa
->sa_family
!= AF_INET
) {
691 dprintk("svcrdma: Address family %d is not supported.\n", sa
->sa_family
);
692 return ERR_PTR(-EAFNOSUPPORT
);
694 cma_xprt
= rdma_create_xprt(serv
, 1);
696 return ERR_PTR(-ENOMEM
);
697 xprt
= &cma_xprt
->sc_xprt
;
699 listen_id
= rdma_create_id(rdma_listen_handler
, cma_xprt
, RDMA_PS_TCP
,
701 if (IS_ERR(listen_id
)) {
702 ret
= PTR_ERR(listen_id
);
703 dprintk("svcrdma: rdma_create_id failed = %d\n", ret
);
707 ret
= rdma_bind_addr(listen_id
, sa
);
709 dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret
);
712 cma_xprt
->sc_cm_id
= listen_id
;
714 ret
= rdma_listen(listen_id
, RPCRDMA_LISTEN_BACKLOG
);
716 dprintk("svcrdma: rdma_listen failed = %d\n", ret
);
721 * We need to use the address from the cm_id in case the
722 * caller specified 0 for the port number.
724 sa
= (struct sockaddr
*)&cma_xprt
->sc_cm_id
->route
.addr
.src_addr
;
725 svc_xprt_set_local(&cma_xprt
->sc_xprt
, sa
, salen
);
727 return &cma_xprt
->sc_xprt
;
730 rdma_destroy_id(listen_id
);
736 static struct svc_rdma_fastreg_mr
*rdma_alloc_frmr(struct svcxprt_rdma
*xprt
)
739 struct ib_fast_reg_page_list
*pl
;
740 struct svc_rdma_fastreg_mr
*frmr
;
742 frmr
= kmalloc(sizeof(*frmr
), GFP_KERNEL
);
746 mr
= ib_alloc_fast_reg_mr(xprt
->sc_pd
, RPCSVC_MAXPAGES
);
750 pl
= ib_alloc_fast_reg_page_list(xprt
->sc_cm_id
->device
,
756 frmr
->page_list
= pl
;
757 INIT_LIST_HEAD(&frmr
->frmr_list
);
765 return ERR_PTR(-ENOMEM
);
768 static void rdma_dealloc_frmr_q(struct svcxprt_rdma
*xprt
)
770 struct svc_rdma_fastreg_mr
*frmr
;
772 while (!list_empty(&xprt
->sc_frmr_q
)) {
773 frmr
= list_entry(xprt
->sc_frmr_q
.next
,
774 struct svc_rdma_fastreg_mr
, frmr_list
);
775 list_del_init(&frmr
->frmr_list
);
776 ib_dereg_mr(frmr
->mr
);
777 ib_free_fast_reg_page_list(frmr
->page_list
);
782 struct svc_rdma_fastreg_mr
*svc_rdma_get_frmr(struct svcxprt_rdma
*rdma
)
784 struct svc_rdma_fastreg_mr
*frmr
= NULL
;
786 spin_lock_bh(&rdma
->sc_frmr_q_lock
);
787 if (!list_empty(&rdma
->sc_frmr_q
)) {
788 frmr
= list_entry(rdma
->sc_frmr_q
.next
,
789 struct svc_rdma_fastreg_mr
, frmr_list
);
790 list_del_init(&frmr
->frmr_list
);
792 frmr
->page_list_len
= 0;
794 spin_unlock_bh(&rdma
->sc_frmr_q_lock
);
798 return rdma_alloc_frmr(rdma
);
801 static void frmr_unmap_dma(struct svcxprt_rdma
*xprt
,
802 struct svc_rdma_fastreg_mr
*frmr
)
805 for (page_no
= 0; page_no
< frmr
->page_list_len
; page_no
++) {
806 dma_addr_t addr
= frmr
->page_list
->page_list
[page_no
];
807 if (ib_dma_mapping_error(frmr
->mr
->device
, addr
))
809 atomic_dec(&xprt
->sc_dma_used
);
810 ib_dma_unmap_page(frmr
->mr
->device
, addr
, PAGE_SIZE
,
815 void svc_rdma_put_frmr(struct svcxprt_rdma
*rdma
,
816 struct svc_rdma_fastreg_mr
*frmr
)
819 frmr_unmap_dma(rdma
, frmr
);
820 spin_lock_bh(&rdma
->sc_frmr_q_lock
);
821 BUG_ON(!list_empty(&frmr
->frmr_list
));
822 list_add(&frmr
->frmr_list
, &rdma
->sc_frmr_q
);
823 spin_unlock_bh(&rdma
->sc_frmr_q_lock
);
828 * This is the xpo_recvfrom function for listening endpoints. Its
829 * purpose is to accept incoming connections. The CMA callback handler
830 * has already created a new transport and attached it to the new CMA
833 * There is a queue of pending connections hung on the listening
834 * transport. This queue contains the new svc_xprt structure. This
835 * function takes svc_xprt structures off the accept_q and completes
838 static struct svc_xprt
*svc_rdma_accept(struct svc_xprt
*xprt
)
840 struct svcxprt_rdma
*listen_rdma
;
841 struct svcxprt_rdma
*newxprt
= NULL
;
842 struct rdma_conn_param conn_param
;
843 struct ib_qp_init_attr qp_attr
;
844 struct ib_device_attr devattr
;
845 int uninitialized_var(dma_mr_acc
);
850 listen_rdma
= container_of(xprt
, struct svcxprt_rdma
, sc_xprt
);
851 clear_bit(XPT_CONN
, &xprt
->xpt_flags
);
852 /* Get the next entry off the accept list */
853 spin_lock_bh(&listen_rdma
->sc_lock
);
854 if (!list_empty(&listen_rdma
->sc_accept_q
)) {
855 newxprt
= list_entry(listen_rdma
->sc_accept_q
.next
,
856 struct svcxprt_rdma
, sc_accept_q
);
857 list_del_init(&newxprt
->sc_accept_q
);
859 if (!list_empty(&listen_rdma
->sc_accept_q
))
860 set_bit(XPT_CONN
, &listen_rdma
->sc_xprt
.xpt_flags
);
861 spin_unlock_bh(&listen_rdma
->sc_lock
);
865 dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
866 newxprt
, newxprt
->sc_cm_id
);
868 ret
= ib_query_device(newxprt
->sc_cm_id
->device
, &devattr
);
870 dprintk("svcrdma: could not query device attributes on "
871 "device %p, rc=%d\n", newxprt
->sc_cm_id
->device
, ret
);
875 /* Qualify the transport resource defaults with the
876 * capabilities of this particular device */
877 newxprt
->sc_max_sge
= min((size_t)devattr
.max_sge
,
878 (size_t)RPCSVC_MAXPAGES
);
879 newxprt
->sc_max_requests
= min((size_t)devattr
.max_qp_wr
,
880 (size_t)svcrdma_max_requests
);
881 newxprt
->sc_sq_depth
= RPCRDMA_SQ_DEPTH_MULT
* newxprt
->sc_max_requests
;
884 * Limit ORD based on client limit, local device limit, and
885 * configured svcrdma limit.
887 newxprt
->sc_ord
= min_t(size_t, devattr
.max_qp_rd_atom
, newxprt
->sc_ord
);
888 newxprt
->sc_ord
= min_t(size_t, svcrdma_ord
, newxprt
->sc_ord
);
890 newxprt
->sc_pd
= ib_alloc_pd(newxprt
->sc_cm_id
->device
);
891 if (IS_ERR(newxprt
->sc_pd
)) {
892 dprintk("svcrdma: error creating PD for connect request\n");
895 newxprt
->sc_sq_cq
= ib_create_cq(newxprt
->sc_cm_id
->device
,
899 newxprt
->sc_sq_depth
,
901 if (IS_ERR(newxprt
->sc_sq_cq
)) {
902 dprintk("svcrdma: error creating SQ CQ for connect request\n");
905 newxprt
->sc_rq_cq
= ib_create_cq(newxprt
->sc_cm_id
->device
,
909 newxprt
->sc_max_requests
,
911 if (IS_ERR(newxprt
->sc_rq_cq
)) {
912 dprintk("svcrdma: error creating RQ CQ for connect request\n");
916 memset(&qp_attr
, 0, sizeof qp_attr
);
917 qp_attr
.event_handler
= qp_event_handler
;
918 qp_attr
.qp_context
= &newxprt
->sc_xprt
;
919 qp_attr
.cap
.max_send_wr
= newxprt
->sc_sq_depth
;
920 qp_attr
.cap
.max_recv_wr
= newxprt
->sc_max_requests
;
921 qp_attr
.cap
.max_send_sge
= newxprt
->sc_max_sge
;
922 qp_attr
.cap
.max_recv_sge
= newxprt
->sc_max_sge
;
923 qp_attr
.sq_sig_type
= IB_SIGNAL_REQ_WR
;
924 qp_attr
.qp_type
= IB_QPT_RC
;
925 qp_attr
.send_cq
= newxprt
->sc_sq_cq
;
926 qp_attr
.recv_cq
= newxprt
->sc_rq_cq
;
927 dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n"
928 " cm_id->device=%p, sc_pd->device=%p\n"
929 " cap.max_send_wr = %d\n"
930 " cap.max_recv_wr = %d\n"
931 " cap.max_send_sge = %d\n"
932 " cap.max_recv_sge = %d\n",
933 newxprt
->sc_cm_id
, newxprt
->sc_pd
,
934 newxprt
->sc_cm_id
->device
, newxprt
->sc_pd
->device
,
935 qp_attr
.cap
.max_send_wr
,
936 qp_attr
.cap
.max_recv_wr
,
937 qp_attr
.cap
.max_send_sge
,
938 qp_attr
.cap
.max_recv_sge
);
940 ret
= rdma_create_qp(newxprt
->sc_cm_id
, newxprt
->sc_pd
, &qp_attr
);
943 * XXX: This is a hack. We need a xx_request_qp interface
944 * that will adjust the qp_attr's with a best-effort
947 qp_attr
.cap
.max_send_sge
-= 2;
948 qp_attr
.cap
.max_recv_sge
-= 2;
949 ret
= rdma_create_qp(newxprt
->sc_cm_id
, newxprt
->sc_pd
,
952 dprintk("svcrdma: failed to create QP, ret=%d\n", ret
);
955 newxprt
->sc_max_sge
= qp_attr
.cap
.max_send_sge
;
956 newxprt
->sc_max_sge
= qp_attr
.cap
.max_recv_sge
;
957 newxprt
->sc_sq_depth
= qp_attr
.cap
.max_send_wr
;
958 newxprt
->sc_max_requests
= qp_attr
.cap
.max_recv_wr
;
960 newxprt
->sc_qp
= newxprt
->sc_cm_id
->qp
;
963 * Use the most secure set of MR resources based on the
964 * transport type and available memory management features in
965 * the device. Here's the table implemented below:
967 * Fast Global DMA Remote WR
969 * Sup'd Sup'd Needed Needed
981 * NB: iWARP requires remote write access for the data sink
982 * of an RDMA_READ. IB does not.
984 if (devattr
.device_cap_flags
& IB_DEVICE_MEM_MGT_EXTENSIONS
) {
985 newxprt
->sc_frmr_pg_list_len
=
986 devattr
.max_fast_reg_page_list_len
;
987 newxprt
->sc_dev_caps
|= SVCRDMA_DEVCAP_FAST_REG
;
991 * Determine if a DMA MR is required and if so, what privs are required
993 switch (rdma_node_get_transport(newxprt
->sc_cm_id
->device
->node_type
)) {
994 case RDMA_TRANSPORT_IWARP
:
995 newxprt
->sc_dev_caps
|= SVCRDMA_DEVCAP_READ_W_INV
;
996 if (!(newxprt
->sc_dev_caps
& SVCRDMA_DEVCAP_FAST_REG
)) {
999 (IB_ACCESS_LOCAL_WRITE
|
1000 IB_ACCESS_REMOTE_WRITE
);
1001 } else if (!(devattr
.device_cap_flags
& IB_DEVICE_LOCAL_DMA_LKEY
)) {
1003 dma_mr_acc
= IB_ACCESS_LOCAL_WRITE
;
1007 case RDMA_TRANSPORT_IB
:
1008 if (!(devattr
.device_cap_flags
& IB_DEVICE_LOCAL_DMA_LKEY
)) {
1010 dma_mr_acc
= IB_ACCESS_LOCAL_WRITE
;
1018 /* Create the DMA MR if needed, otherwise, use the DMA LKEY */
1020 /* Register all of physical memory */
1021 newxprt
->sc_phys_mr
=
1022 ib_get_dma_mr(newxprt
->sc_pd
, dma_mr_acc
);
1023 if (IS_ERR(newxprt
->sc_phys_mr
)) {
1024 dprintk("svcrdma: Failed to create DMA MR ret=%d\n",
1028 newxprt
->sc_dma_lkey
= newxprt
->sc_phys_mr
->lkey
;
1030 newxprt
->sc_dma_lkey
=
1031 newxprt
->sc_cm_id
->device
->local_dma_lkey
;
1033 /* Post receive buffers */
1034 for (i
= 0; i
< newxprt
->sc_max_requests
; i
++) {
1035 ret
= svc_rdma_post_recv(newxprt
);
1037 dprintk("svcrdma: failure posting receive buffers\n");
1042 /* Swap out the handler */
1043 newxprt
->sc_cm_id
->event_handler
= rdma_cma_handler
;
1046 * Arm the CQs for the SQ and RQ before accepting so we can't
1047 * miss the first message
1049 ib_req_notify_cq(newxprt
->sc_sq_cq
, IB_CQ_NEXT_COMP
);
1050 ib_req_notify_cq(newxprt
->sc_rq_cq
, IB_CQ_NEXT_COMP
);
1052 /* Accept Connection */
1053 set_bit(RDMAXPRT_CONN_PENDING
, &newxprt
->sc_flags
);
1054 memset(&conn_param
, 0, sizeof conn_param
);
1055 conn_param
.responder_resources
= 0;
1056 conn_param
.initiator_depth
= newxprt
->sc_ord
;
1057 ret
= rdma_accept(newxprt
->sc_cm_id
, &conn_param
);
1059 dprintk("svcrdma: failed to accept new connection, ret=%d\n",
1064 dprintk("svcrdma: new connection %p accepted with the following "
1066 " local_ip : %pI4\n"
1067 " local_port : %d\n"
1068 " remote_ip : %pI4\n"
1069 " remote_port : %d\n"
1072 " max_requests : %d\n"
1075 &((struct sockaddr_in
*)&newxprt
->sc_cm_id
->
1076 route
.addr
.src_addr
)->sin_addr
.s_addr
,
1077 ntohs(((struct sockaddr_in
*)&newxprt
->sc_cm_id
->
1078 route
.addr
.src_addr
)->sin_port
),
1079 &((struct sockaddr_in
*)&newxprt
->sc_cm_id
->
1080 route
.addr
.dst_addr
)->sin_addr
.s_addr
,
1081 ntohs(((struct sockaddr_in
*)&newxprt
->sc_cm_id
->
1082 route
.addr
.dst_addr
)->sin_port
),
1083 newxprt
->sc_max_sge
,
1084 newxprt
->sc_sq_depth
,
1085 newxprt
->sc_max_requests
,
1088 return &newxprt
->sc_xprt
;
1091 dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret
);
1092 /* Take a reference in case the DTO handler runs */
1093 svc_xprt_get(&newxprt
->sc_xprt
);
1094 if (newxprt
->sc_qp
&& !IS_ERR(newxprt
->sc_qp
))
1095 ib_destroy_qp(newxprt
->sc_qp
);
1096 rdma_destroy_id(newxprt
->sc_cm_id
);
1097 /* This call to put will destroy the transport */
1098 svc_xprt_put(&newxprt
->sc_xprt
);
1102 static void svc_rdma_release_rqst(struct svc_rqst
*rqstp
)
1107 * When connected, an svc_xprt has at least two references:
1109 * - A reference held by the cm_id between the ESTABLISHED and
1110 * DISCONNECTED events. If the remote peer disconnected first, this
1111 * reference could be gone.
1113 * - A reference held by the svc_recv code that called this function
1114 * as part of close processing.
1116 * At a minimum one references should still be held.
1118 static void svc_rdma_detach(struct svc_xprt
*xprt
)
1120 struct svcxprt_rdma
*rdma
=
1121 container_of(xprt
, struct svcxprt_rdma
, sc_xprt
);
1122 dprintk("svc: svc_rdma_detach(%p)\n", xprt
);
1124 /* Disconnect and flush posted WQE */
1125 rdma_disconnect(rdma
->sc_cm_id
);
1128 static void __svc_rdma_free(struct work_struct
*work
)
1130 struct svcxprt_rdma
*rdma
=
1131 container_of(work
, struct svcxprt_rdma
, sc_work
);
1132 dprintk("svcrdma: svc_rdma_free(%p)\n", rdma
);
1134 /* We should only be called from kref_put */
1135 BUG_ON(atomic_read(&rdma
->sc_xprt
.xpt_ref
.refcount
) != 0);
1138 * Destroy queued, but not processed read completions. Note
1139 * that this cleanup has to be done before destroying the
1140 * cm_id because the device ptr is needed to unmap the dma in
1141 * svc_rdma_put_context.
1143 while (!list_empty(&rdma
->sc_read_complete_q
)) {
1144 struct svc_rdma_op_ctxt
*ctxt
;
1145 ctxt
= list_entry(rdma
->sc_read_complete_q
.next
,
1146 struct svc_rdma_op_ctxt
,
1148 list_del_init(&ctxt
->dto_q
);
1149 svc_rdma_put_context(ctxt
, 1);
1152 /* Destroy queued, but not processed recv completions */
1153 while (!list_empty(&rdma
->sc_rq_dto_q
)) {
1154 struct svc_rdma_op_ctxt
*ctxt
;
1155 ctxt
= list_entry(rdma
->sc_rq_dto_q
.next
,
1156 struct svc_rdma_op_ctxt
,
1158 list_del_init(&ctxt
->dto_q
);
1159 svc_rdma_put_context(ctxt
, 1);
1162 /* Warn if we leaked a resource or under-referenced */
1163 WARN_ON(atomic_read(&rdma
->sc_ctxt_used
) != 0);
1164 WARN_ON(atomic_read(&rdma
->sc_dma_used
) != 0);
1166 /* De-allocate fastreg mr */
1167 rdma_dealloc_frmr_q(rdma
);
1169 /* Destroy the QP if present (not a listener) */
1170 if (rdma
->sc_qp
&& !IS_ERR(rdma
->sc_qp
))
1171 ib_destroy_qp(rdma
->sc_qp
);
1173 if (rdma
->sc_sq_cq
&& !IS_ERR(rdma
->sc_sq_cq
))
1174 ib_destroy_cq(rdma
->sc_sq_cq
);
1176 if (rdma
->sc_rq_cq
&& !IS_ERR(rdma
->sc_rq_cq
))
1177 ib_destroy_cq(rdma
->sc_rq_cq
);
1179 if (rdma
->sc_phys_mr
&& !IS_ERR(rdma
->sc_phys_mr
))
1180 ib_dereg_mr(rdma
->sc_phys_mr
);
1182 if (rdma
->sc_pd
&& !IS_ERR(rdma
->sc_pd
))
1183 ib_dealloc_pd(rdma
->sc_pd
);
1185 /* Destroy the CM ID */
1186 rdma_destroy_id(rdma
->sc_cm_id
);
1191 static void svc_rdma_free(struct svc_xprt
*xprt
)
1193 struct svcxprt_rdma
*rdma
=
1194 container_of(xprt
, struct svcxprt_rdma
, sc_xprt
);
1195 INIT_WORK(&rdma
->sc_work
, __svc_rdma_free
);
1196 queue_work(svc_rdma_wq
, &rdma
->sc_work
);
1199 static int svc_rdma_has_wspace(struct svc_xprt
*xprt
)
1201 struct svcxprt_rdma
*rdma
=
1202 container_of(xprt
, struct svcxprt_rdma
, sc_xprt
);
1205 * If there are fewer SQ WR available than required to send a
1206 * simple response, return false.
1208 if ((rdma
->sc_sq_depth
- atomic_read(&rdma
->sc_sq_count
) < 3))
1212 * ...or there are already waiters on the SQ,
1215 if (waitqueue_active(&rdma
->sc_send_wait
))
1218 /* Otherwise return true. */
1223 * Attempt to register the kvec representing the RPC memory with the
1227 * NULL : The device does not support fastreg or there were no more
1229 * frmr : The kvec register request was successfully posted.
1230 * <0 : An error was encountered attempting to register the kvec.
1232 int svc_rdma_fastreg(struct svcxprt_rdma
*xprt
,
1233 struct svc_rdma_fastreg_mr
*frmr
)
1235 struct ib_send_wr fastreg_wr
;
1239 key
= (u8
)(frmr
->mr
->lkey
& 0x000000FF);
1240 ib_update_fast_reg_key(frmr
->mr
, ++key
);
1242 /* Prepare FASTREG WR */
1243 memset(&fastreg_wr
, 0, sizeof fastreg_wr
);
1244 fastreg_wr
.opcode
= IB_WR_FAST_REG_MR
;
1245 fastreg_wr
.send_flags
= IB_SEND_SIGNALED
;
1246 fastreg_wr
.wr
.fast_reg
.iova_start
= (unsigned long)frmr
->kva
;
1247 fastreg_wr
.wr
.fast_reg
.page_list
= frmr
->page_list
;
1248 fastreg_wr
.wr
.fast_reg
.page_list_len
= frmr
->page_list_len
;
1249 fastreg_wr
.wr
.fast_reg
.page_shift
= PAGE_SHIFT
;
1250 fastreg_wr
.wr
.fast_reg
.length
= frmr
->map_len
;
1251 fastreg_wr
.wr
.fast_reg
.access_flags
= frmr
->access_flags
;
1252 fastreg_wr
.wr
.fast_reg
.rkey
= frmr
->mr
->lkey
;
1253 return svc_rdma_send(xprt
, &fastreg_wr
);
1256 int svc_rdma_send(struct svcxprt_rdma
*xprt
, struct ib_send_wr
*wr
)
1258 struct ib_send_wr
*bad_wr
, *n_wr
;
1263 if (test_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
))
1266 BUG_ON(wr
->send_flags
!= IB_SEND_SIGNALED
);
1268 for (n_wr
= wr
->next
; n_wr
; n_wr
= n_wr
->next
)
1271 /* If the SQ is full, wait until an SQ entry is available */
1273 spin_lock_bh(&xprt
->sc_lock
);
1274 if (xprt
->sc_sq_depth
< atomic_read(&xprt
->sc_sq_count
) + wr_count
) {
1275 spin_unlock_bh(&xprt
->sc_lock
);
1276 atomic_inc(&rdma_stat_sq_starve
);
1278 /* See if we can opportunistically reap SQ WR to make room */
1281 /* Wait until SQ WR available if SQ still full */
1282 wait_event(xprt
->sc_send_wait
,
1283 atomic_read(&xprt
->sc_sq_count
) <
1285 if (test_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
))
1289 /* Take a transport ref for each WR posted */
1290 for (i
= 0; i
< wr_count
; i
++)
1291 svc_xprt_get(&xprt
->sc_xprt
);
1293 /* Bump used SQ WR count and post */
1294 atomic_add(wr_count
, &xprt
->sc_sq_count
);
1295 ret
= ib_post_send(xprt
->sc_qp
, wr
, &bad_wr
);
1297 set_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
);
1298 atomic_sub(wr_count
, &xprt
->sc_sq_count
);
1299 for (i
= 0; i
< wr_count
; i
++)
1300 svc_xprt_put(&xprt
->sc_xprt
);
1301 dprintk("svcrdma: failed to post SQ WR rc=%d, "
1302 "sc_sq_count=%d, sc_sq_depth=%d\n",
1303 ret
, atomic_read(&xprt
->sc_sq_count
),
1306 spin_unlock_bh(&xprt
->sc_lock
);
1308 wake_up(&xprt
->sc_send_wait
);
1314 void svc_rdma_send_error(struct svcxprt_rdma
*xprt
, struct rpcrdma_msg
*rmsgp
,
1315 enum rpcrdma_errcode err
)
1317 struct ib_send_wr err_wr
;
1319 struct svc_rdma_op_ctxt
*ctxt
;
1324 p
= svc_rdma_get_page();
1325 va
= page_address(p
);
1327 /* XDR encode error */
1328 length
= svc_rdma_xdr_encode_error(xprt
, rmsgp
, err
, va
);
1330 ctxt
= svc_rdma_get_context(xprt
);
1331 ctxt
->direction
= DMA_FROM_DEVICE
;
1335 /* Prepare SGE for local address */
1336 ctxt
->sge
[0].addr
= ib_dma_map_page(xprt
->sc_cm_id
->device
,
1337 p
, 0, length
, DMA_FROM_DEVICE
);
1338 if (ib_dma_mapping_error(xprt
->sc_cm_id
->device
, ctxt
->sge
[0].addr
)) {
1340 svc_rdma_put_context(ctxt
, 1);
1343 atomic_inc(&xprt
->sc_dma_used
);
1344 ctxt
->sge
[0].lkey
= xprt
->sc_dma_lkey
;
1345 ctxt
->sge
[0].length
= length
;
1347 /* Prepare SEND WR */
1348 memset(&err_wr
, 0, sizeof err_wr
);
1349 ctxt
->wr_op
= IB_WR_SEND
;
1350 err_wr
.wr_id
= (unsigned long)ctxt
;
1351 err_wr
.sg_list
= ctxt
->sge
;
1353 err_wr
.opcode
= IB_WR_SEND
;
1354 err_wr
.send_flags
= IB_SEND_SIGNALED
;
1357 ret
= svc_rdma_send(xprt
, &err_wr
);
1359 dprintk("svcrdma: Error %d posting send for protocol error\n",
1361 svc_rdma_unmap_dma(ctxt
);
1362 svc_rdma_put_context(ctxt
, 1);