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>
54 #include "xprt_rdma.h"
56 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
58 static struct svc_xprt
*svc_rdma_create(struct svc_serv
*serv
,
60 struct sockaddr
*sa
, int salen
,
62 static struct svc_xprt
*svc_rdma_accept(struct svc_xprt
*xprt
);
63 static void svc_rdma_release_rqst(struct svc_rqst
*);
64 static void dto_tasklet_func(unsigned long data
);
65 static void svc_rdma_detach(struct svc_xprt
*xprt
);
66 static void svc_rdma_free(struct svc_xprt
*xprt
);
67 static int svc_rdma_has_wspace(struct svc_xprt
*xprt
);
68 static void rq_cq_reap(struct svcxprt_rdma
*xprt
);
69 static void sq_cq_reap(struct svcxprt_rdma
*xprt
);
71 static DECLARE_TASKLET(dto_tasklet
, dto_tasklet_func
, 0UL);
72 static DEFINE_SPINLOCK(dto_lock
);
73 static LIST_HEAD(dto_xprt_q
);
75 static struct svc_xprt_ops svc_rdma_ops
= {
76 .xpo_create
= svc_rdma_create
,
77 .xpo_recvfrom
= svc_rdma_recvfrom
,
78 .xpo_sendto
= svc_rdma_sendto
,
79 .xpo_release_rqst
= svc_rdma_release_rqst
,
80 .xpo_detach
= svc_rdma_detach
,
81 .xpo_free
= svc_rdma_free
,
82 .xpo_prep_reply_hdr
= svc_rdma_prep_reply_hdr
,
83 .xpo_has_wspace
= svc_rdma_has_wspace
,
84 .xpo_accept
= svc_rdma_accept
,
87 struct svc_xprt_class svc_rdma_class
= {
89 .xcl_owner
= THIS_MODULE
,
90 .xcl_ops
= &svc_rdma_ops
,
91 .xcl_max_payload
= RPCSVC_MAXPAYLOAD_TCP
,
94 struct svc_rdma_op_ctxt
*svc_rdma_get_context(struct svcxprt_rdma
*xprt
)
96 struct svc_rdma_op_ctxt
*ctxt
;
99 ctxt
= kmem_cache_alloc(svc_rdma_ctxt_cachep
, GFP_KERNEL
);
102 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
105 INIT_LIST_HEAD(&ctxt
->dto_q
);
108 atomic_inc(&xprt
->sc_ctxt_used
);
112 void svc_rdma_unmap_dma(struct svc_rdma_op_ctxt
*ctxt
)
114 struct svcxprt_rdma
*xprt
= ctxt
->xprt
;
116 for (i
= 0; i
< ctxt
->count
&& ctxt
->sge
[i
].length
; i
++) {
118 * Unmap the DMA addr in the SGE if the lkey matches
119 * the sc_dma_lkey, otherwise, ignore it since it is
120 * an FRMR lkey and will be unmapped later when the
121 * last WR that uses it completes.
123 if (ctxt
->sge
[i
].lkey
== xprt
->sc_dma_lkey
) {
124 atomic_dec(&xprt
->sc_dma_used
);
125 ib_dma_unmap_page(xprt
->sc_cm_id
->device
,
133 void svc_rdma_put_context(struct svc_rdma_op_ctxt
*ctxt
, int free_pages
)
135 struct svcxprt_rdma
*xprt
;
141 for (i
= 0; i
< ctxt
->count
; i
++)
142 put_page(ctxt
->pages
[i
]);
144 kmem_cache_free(svc_rdma_ctxt_cachep
, ctxt
);
145 atomic_dec(&xprt
->sc_ctxt_used
);
149 * Temporary NFS req mappings are shared across all transport
150 * instances. These are short lived and should be bounded by the number
151 * of concurrent server threads * depth of the SQ.
153 struct svc_rdma_req_map
*svc_rdma_get_req_map(void)
155 struct svc_rdma_req_map
*map
;
157 map
= kmem_cache_alloc(svc_rdma_map_cachep
, GFP_KERNEL
);
160 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
167 void svc_rdma_put_req_map(struct svc_rdma_req_map
*map
)
169 kmem_cache_free(svc_rdma_map_cachep
, map
);
172 /* ib_cq event handler */
173 static void cq_event_handler(struct ib_event
*event
, void *context
)
175 struct svc_xprt
*xprt
= context
;
176 dprintk("svcrdma: received CQ event id=%d, context=%p\n",
177 event
->event
, context
);
178 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
181 /* QP event handler */
182 static void qp_event_handler(struct ib_event
*event
, void *context
)
184 struct svc_xprt
*xprt
= context
;
186 switch (event
->event
) {
187 /* These are considered benign events */
188 case IB_EVENT_PATH_MIG
:
189 case IB_EVENT_COMM_EST
:
190 case IB_EVENT_SQ_DRAINED
:
191 case IB_EVENT_QP_LAST_WQE_REACHED
:
192 dprintk("svcrdma: QP event %d received for QP=%p\n",
193 event
->event
, event
->element
.qp
);
195 /* These are considered fatal events */
196 case IB_EVENT_PATH_MIG_ERR
:
197 case IB_EVENT_QP_FATAL
:
198 case IB_EVENT_QP_REQ_ERR
:
199 case IB_EVENT_QP_ACCESS_ERR
:
200 case IB_EVENT_DEVICE_FATAL
:
202 dprintk("svcrdma: QP ERROR event %d received for QP=%p, "
203 "closing transport\n",
204 event
->event
, event
->element
.qp
);
205 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
211 * Data Transfer Operation Tasklet
213 * Walks a list of transports with I/O pending, removing entries as
214 * they are added to the server's I/O pending list. Two bits indicate
215 * if SQ, RQ, or both have I/O pending. The dto_lock is an irqsave
216 * spinlock that serializes access to the transport list with the RQ
217 * and SQ interrupt handlers.
219 static void dto_tasklet_func(unsigned long data
)
221 struct svcxprt_rdma
*xprt
;
224 spin_lock_irqsave(&dto_lock
, flags
);
225 while (!list_empty(&dto_xprt_q
)) {
226 xprt
= list_entry(dto_xprt_q
.next
,
227 struct svcxprt_rdma
, sc_dto_q
);
228 list_del_init(&xprt
->sc_dto_q
);
229 spin_unlock_irqrestore(&dto_lock
, flags
);
234 svc_xprt_put(&xprt
->sc_xprt
);
235 spin_lock_irqsave(&dto_lock
, flags
);
237 spin_unlock_irqrestore(&dto_lock
, flags
);
241 * Receive Queue Completion Handler
243 * Since an RQ completion handler is called on interrupt context, we
244 * need to defer the handling of the I/O to a tasklet
246 static void rq_comp_handler(struct ib_cq
*cq
, void *cq_context
)
248 struct svcxprt_rdma
*xprt
= cq_context
;
251 /* Guard against unconditional flush call for destroyed QP */
252 if (atomic_read(&xprt
->sc_xprt
.xpt_ref
.refcount
)==0)
256 * Set the bit regardless of whether or not it's on the list
257 * because it may be on the list already due to an SQ
260 set_bit(RDMAXPRT_RQ_PENDING
, &xprt
->sc_flags
);
263 * If this transport is not already on the DTO transport queue,
266 spin_lock_irqsave(&dto_lock
, flags
);
267 if (list_empty(&xprt
->sc_dto_q
)) {
268 svc_xprt_get(&xprt
->sc_xprt
);
269 list_add_tail(&xprt
->sc_dto_q
, &dto_xprt_q
);
271 spin_unlock_irqrestore(&dto_lock
, flags
);
273 /* Tasklet does all the work to avoid irqsave locks. */
274 tasklet_schedule(&dto_tasklet
);
278 * rq_cq_reap - Process the RQ CQ.
280 * Take all completing WC off the CQE and enqueue the associated DTO
281 * context on the dto_q for the transport.
283 * Note that caller must hold a transport reference.
285 static void rq_cq_reap(struct svcxprt_rdma
*xprt
)
289 struct svc_rdma_op_ctxt
*ctxt
= NULL
;
291 if (!test_and_clear_bit(RDMAXPRT_RQ_PENDING
, &xprt
->sc_flags
))
294 ib_req_notify_cq(xprt
->sc_rq_cq
, IB_CQ_NEXT_COMP
);
295 atomic_inc(&rdma_stat_rq_poll
);
297 while ((ret
= ib_poll_cq(xprt
->sc_rq_cq
, 1, &wc
)) > 0) {
298 ctxt
= (struct svc_rdma_op_ctxt
*)(unsigned long)wc
.wr_id
;
299 ctxt
->wc_status
= wc
.status
;
300 ctxt
->byte_len
= wc
.byte_len
;
301 svc_rdma_unmap_dma(ctxt
);
302 if (wc
.status
!= IB_WC_SUCCESS
) {
303 /* Close the transport */
304 dprintk("svcrdma: transport closing putting ctxt %p\n", ctxt
);
305 set_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
);
306 svc_rdma_put_context(ctxt
, 1);
307 svc_xprt_put(&xprt
->sc_xprt
);
310 spin_lock_bh(&xprt
->sc_rq_dto_lock
);
311 list_add_tail(&ctxt
->dto_q
, &xprt
->sc_rq_dto_q
);
312 spin_unlock_bh(&xprt
->sc_rq_dto_lock
);
313 svc_xprt_put(&xprt
->sc_xprt
);
317 atomic_inc(&rdma_stat_rq_prod
);
319 set_bit(XPT_DATA
, &xprt
->sc_xprt
.xpt_flags
);
321 * If data arrived before established event,
322 * don't enqueue. This defers RPC I/O until the
323 * RDMA connection is complete.
325 if (!test_bit(RDMAXPRT_CONN_PENDING
, &xprt
->sc_flags
))
326 svc_xprt_enqueue(&xprt
->sc_xprt
);
330 * Process a completion context
332 static void process_context(struct svcxprt_rdma
*xprt
,
333 struct svc_rdma_op_ctxt
*ctxt
)
335 svc_rdma_unmap_dma(ctxt
);
337 switch (ctxt
->wr_op
) {
339 if (test_bit(RDMACTXT_F_FAST_UNREG
, &ctxt
->flags
))
340 svc_rdma_put_frmr(xprt
, ctxt
->frmr
);
341 svc_rdma_put_context(ctxt
, 1);
344 case IB_WR_RDMA_WRITE
:
345 svc_rdma_put_context(ctxt
, 0);
348 case IB_WR_RDMA_READ
:
349 case IB_WR_RDMA_READ_WITH_INV
:
350 if (test_bit(RDMACTXT_F_LAST_CTXT
, &ctxt
->flags
)) {
351 struct svc_rdma_op_ctxt
*read_hdr
= ctxt
->read_hdr
;
353 if (test_bit(RDMACTXT_F_FAST_UNREG
, &ctxt
->flags
))
354 svc_rdma_put_frmr(xprt
, ctxt
->frmr
);
355 spin_lock_bh(&xprt
->sc_rq_dto_lock
);
356 set_bit(XPT_DATA
, &xprt
->sc_xprt
.xpt_flags
);
357 list_add_tail(&read_hdr
->dto_q
,
358 &xprt
->sc_read_complete_q
);
359 spin_unlock_bh(&xprt
->sc_rq_dto_lock
);
360 svc_xprt_enqueue(&xprt
->sc_xprt
);
362 svc_rdma_put_context(ctxt
, 0);
366 printk(KERN_ERR
"svcrdma: unexpected completion type, "
374 * Send Queue Completion Handler - potentially called on interrupt context.
376 * Note that caller must hold a transport reference.
378 static void sq_cq_reap(struct svcxprt_rdma
*xprt
)
380 struct svc_rdma_op_ctxt
*ctxt
= NULL
;
382 struct ib_cq
*cq
= xprt
->sc_sq_cq
;
385 if (!test_and_clear_bit(RDMAXPRT_SQ_PENDING
, &xprt
->sc_flags
))
388 ib_req_notify_cq(xprt
->sc_sq_cq
, IB_CQ_NEXT_COMP
);
389 atomic_inc(&rdma_stat_sq_poll
);
390 while ((ret
= ib_poll_cq(cq
, 1, &wc
)) > 0) {
391 if (wc
.status
!= IB_WC_SUCCESS
)
392 /* Close the transport */
393 set_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
);
395 /* Decrement used SQ WR count */
396 atomic_dec(&xprt
->sc_sq_count
);
397 wake_up(&xprt
->sc_send_wait
);
399 ctxt
= (struct svc_rdma_op_ctxt
*)(unsigned long)wc
.wr_id
;
401 process_context(xprt
, ctxt
);
403 svc_xprt_put(&xprt
->sc_xprt
);
407 atomic_inc(&rdma_stat_sq_prod
);
410 static void sq_comp_handler(struct ib_cq
*cq
, void *cq_context
)
412 struct svcxprt_rdma
*xprt
= cq_context
;
415 /* Guard against unconditional flush call for destroyed QP */
416 if (atomic_read(&xprt
->sc_xprt
.xpt_ref
.refcount
)==0)
420 * Set the bit regardless of whether or not it's on the list
421 * because it may be on the list already due to an RQ
424 set_bit(RDMAXPRT_SQ_PENDING
, &xprt
->sc_flags
);
427 * If this transport is not already on the DTO transport queue,
430 spin_lock_irqsave(&dto_lock
, flags
);
431 if (list_empty(&xprt
->sc_dto_q
)) {
432 svc_xprt_get(&xprt
->sc_xprt
);
433 list_add_tail(&xprt
->sc_dto_q
, &dto_xprt_q
);
435 spin_unlock_irqrestore(&dto_lock
, flags
);
437 /* Tasklet does all the work to avoid irqsave locks. */
438 tasklet_schedule(&dto_tasklet
);
441 static struct svcxprt_rdma
*rdma_create_xprt(struct svc_serv
*serv
,
444 struct svcxprt_rdma
*cma_xprt
= kzalloc(sizeof *cma_xprt
, GFP_KERNEL
);
448 svc_xprt_init(&init_net
, &svc_rdma_class
, &cma_xprt
->sc_xprt
, serv
);
449 INIT_LIST_HEAD(&cma_xprt
->sc_accept_q
);
450 INIT_LIST_HEAD(&cma_xprt
->sc_dto_q
);
451 INIT_LIST_HEAD(&cma_xprt
->sc_rq_dto_q
);
452 INIT_LIST_HEAD(&cma_xprt
->sc_read_complete_q
);
453 INIT_LIST_HEAD(&cma_xprt
->sc_frmr_q
);
454 init_waitqueue_head(&cma_xprt
->sc_send_wait
);
456 spin_lock_init(&cma_xprt
->sc_lock
);
457 spin_lock_init(&cma_xprt
->sc_rq_dto_lock
);
458 spin_lock_init(&cma_xprt
->sc_frmr_q_lock
);
460 cma_xprt
->sc_ord
= svcrdma_ord
;
462 cma_xprt
->sc_max_req_size
= svcrdma_max_req_size
;
463 cma_xprt
->sc_max_requests
= svcrdma_max_requests
;
464 cma_xprt
->sc_sq_depth
= svcrdma_max_requests
* RPCRDMA_SQ_DEPTH_MULT
;
465 atomic_set(&cma_xprt
->sc_sq_count
, 0);
466 atomic_set(&cma_xprt
->sc_ctxt_used
, 0);
469 set_bit(XPT_LISTENER
, &cma_xprt
->sc_xprt
.xpt_flags
);
474 struct page
*svc_rdma_get_page(void)
478 while ((page
= alloc_page(GFP_KERNEL
)) == NULL
) {
479 /* If we can't get memory, wait a bit and try again */
480 printk(KERN_INFO
"svcrdma: out of memory...retrying in 1000 "
482 schedule_timeout_uninterruptible(msecs_to_jiffies(1000));
487 int svc_rdma_post_recv(struct svcxprt_rdma
*xprt
)
489 struct ib_recv_wr recv_wr
, *bad_recv_wr
;
490 struct svc_rdma_op_ctxt
*ctxt
;
497 ctxt
= svc_rdma_get_context(xprt
);
499 ctxt
->direction
= DMA_FROM_DEVICE
;
500 for (sge_no
= 0; buflen
< xprt
->sc_max_req_size
; sge_no
++) {
501 BUG_ON(sge_no
>= xprt
->sc_max_sge
);
502 page
= svc_rdma_get_page();
503 ctxt
->pages
[sge_no
] = page
;
504 pa
= ib_dma_map_page(xprt
->sc_cm_id
->device
,
507 if (ib_dma_mapping_error(xprt
->sc_cm_id
->device
, pa
))
509 atomic_inc(&xprt
->sc_dma_used
);
510 ctxt
->sge
[sge_no
].addr
= pa
;
511 ctxt
->sge
[sge_no
].length
= PAGE_SIZE
;
512 ctxt
->sge
[sge_no
].lkey
= xprt
->sc_dma_lkey
;
513 ctxt
->count
= sge_no
+ 1;
517 recv_wr
.sg_list
= &ctxt
->sge
[0];
518 recv_wr
.num_sge
= ctxt
->count
;
519 recv_wr
.wr_id
= (u64
)(unsigned long)ctxt
;
521 svc_xprt_get(&xprt
->sc_xprt
);
522 ret
= ib_post_recv(xprt
->sc_qp
, &recv_wr
, &bad_recv_wr
);
524 svc_rdma_unmap_dma(ctxt
);
525 svc_rdma_put_context(ctxt
, 1);
526 svc_xprt_put(&xprt
->sc_xprt
);
531 svc_rdma_unmap_dma(ctxt
);
532 svc_rdma_put_context(ctxt
, 1);
537 * This function handles the CONNECT_REQUEST event on a listening
538 * endpoint. It is passed the cma_id for the _new_ connection. The context in
539 * this cma_id is inherited from the listening cma_id and is the svc_xprt
540 * structure for the listening endpoint.
542 * This function creates a new xprt for the new connection and enqueues it on
543 * the accept queue for the listent xprt. When the listen thread is kicked, it
544 * will call the recvfrom method on the listen xprt which will accept the new
547 static void handle_connect_req(struct rdma_cm_id
*new_cma_id
, size_t client_ird
)
549 struct svcxprt_rdma
*listen_xprt
= new_cma_id
->context
;
550 struct svcxprt_rdma
*newxprt
;
553 /* Create a new transport */
554 newxprt
= rdma_create_xprt(listen_xprt
->sc_xprt
.xpt_server
, 0);
556 dprintk("svcrdma: failed to create new transport\n");
559 newxprt
->sc_cm_id
= new_cma_id
;
560 new_cma_id
->context
= newxprt
;
561 dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
562 newxprt
, newxprt
->sc_cm_id
, listen_xprt
);
564 /* Save client advertised inbound read limit for use later in accept. */
565 newxprt
->sc_ord
= client_ird
;
567 /* Set the local and remote addresses in the transport */
568 sa
= (struct sockaddr
*)&newxprt
->sc_cm_id
->route
.addr
.dst_addr
;
569 svc_xprt_set_remote(&newxprt
->sc_xprt
, sa
, svc_addr_len(sa
));
570 sa
= (struct sockaddr
*)&newxprt
->sc_cm_id
->route
.addr
.src_addr
;
571 svc_xprt_set_local(&newxprt
->sc_xprt
, sa
, svc_addr_len(sa
));
574 * Enqueue the new transport on the accept queue of the listening
577 spin_lock_bh(&listen_xprt
->sc_lock
);
578 list_add_tail(&newxprt
->sc_accept_q
, &listen_xprt
->sc_accept_q
);
579 spin_unlock_bh(&listen_xprt
->sc_lock
);
582 * Can't use svc_xprt_received here because we are not on a
585 set_bit(XPT_CONN
, &listen_xprt
->sc_xprt
.xpt_flags
);
586 svc_xprt_enqueue(&listen_xprt
->sc_xprt
);
590 * Handles events generated on the listening endpoint. These events will be
591 * either be incoming connect requests or adapter removal events.
593 static int rdma_listen_handler(struct rdma_cm_id
*cma_id
,
594 struct rdma_cm_event
*event
)
596 struct svcxprt_rdma
*xprt
= cma_id
->context
;
599 switch (event
->event
) {
600 case RDMA_CM_EVENT_CONNECT_REQUEST
:
601 dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
602 "event=%d\n", cma_id
, cma_id
->context
, event
->event
);
603 handle_connect_req(cma_id
,
604 event
->param
.conn
.initiator_depth
);
607 case RDMA_CM_EVENT_ESTABLISHED
:
608 /* Accept complete */
609 dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
610 "cm_id=%p\n", xprt
, cma_id
);
613 case RDMA_CM_EVENT_DEVICE_REMOVAL
:
614 dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
617 set_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
);
621 dprintk("svcrdma: Unexpected event on listening endpoint %p, "
622 "event=%d\n", cma_id
, event
->event
);
629 static int rdma_cma_handler(struct rdma_cm_id
*cma_id
,
630 struct rdma_cm_event
*event
)
632 struct svc_xprt
*xprt
= cma_id
->context
;
633 struct svcxprt_rdma
*rdma
=
634 container_of(xprt
, struct svcxprt_rdma
, sc_xprt
);
635 switch (event
->event
) {
636 case RDMA_CM_EVENT_ESTABLISHED
:
637 /* Accept complete */
639 dprintk("svcrdma: Connection completed on DTO xprt=%p, "
640 "cm_id=%p\n", xprt
, cma_id
);
641 clear_bit(RDMAXPRT_CONN_PENDING
, &rdma
->sc_flags
);
642 svc_xprt_enqueue(xprt
);
644 case RDMA_CM_EVENT_DISCONNECTED
:
645 dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
648 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
649 svc_xprt_enqueue(xprt
);
653 case RDMA_CM_EVENT_DEVICE_REMOVAL
:
654 dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
655 "event=%d\n", cma_id
, xprt
, event
->event
);
657 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
658 svc_xprt_enqueue(xprt
);
662 dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
663 "event=%d\n", cma_id
, event
->event
);
670 * Create a listening RDMA service endpoint.
672 static struct svc_xprt
*svc_rdma_create(struct svc_serv
*serv
,
674 struct sockaddr
*sa
, int salen
,
677 struct rdma_cm_id
*listen_id
;
678 struct svcxprt_rdma
*cma_xprt
;
679 struct svc_xprt
*xprt
;
682 dprintk("svcrdma: Creating RDMA socket\n");
683 if (sa
->sa_family
!= AF_INET
) {
684 dprintk("svcrdma: Address family %d is not supported.\n", sa
->sa_family
);
685 return ERR_PTR(-EAFNOSUPPORT
);
687 cma_xprt
= rdma_create_xprt(serv
, 1);
689 return ERR_PTR(-ENOMEM
);
690 xprt
= &cma_xprt
->sc_xprt
;
692 listen_id
= rdma_create_id(rdma_listen_handler
, cma_xprt
, RDMA_PS_TCP
,
694 if (IS_ERR(listen_id
)) {
695 ret
= PTR_ERR(listen_id
);
696 dprintk("svcrdma: rdma_create_id failed = %d\n", ret
);
700 ret
= rdma_bind_addr(listen_id
, sa
);
702 dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret
);
705 cma_xprt
->sc_cm_id
= listen_id
;
707 ret
= rdma_listen(listen_id
, RPCRDMA_LISTEN_BACKLOG
);
709 dprintk("svcrdma: rdma_listen failed = %d\n", ret
);
714 * We need to use the address from the cm_id in case the
715 * caller specified 0 for the port number.
717 sa
= (struct sockaddr
*)&cma_xprt
->sc_cm_id
->route
.addr
.src_addr
;
718 svc_xprt_set_local(&cma_xprt
->sc_xprt
, sa
, salen
);
720 return &cma_xprt
->sc_xprt
;
723 rdma_destroy_id(listen_id
);
729 static struct svc_rdma_fastreg_mr
*rdma_alloc_frmr(struct svcxprt_rdma
*xprt
)
732 struct ib_fast_reg_page_list
*pl
;
733 struct svc_rdma_fastreg_mr
*frmr
;
735 frmr
= kmalloc(sizeof(*frmr
), GFP_KERNEL
);
739 mr
= ib_alloc_fast_reg_mr(xprt
->sc_pd
, RPCSVC_MAXPAGES
);
743 pl
= ib_alloc_fast_reg_page_list(xprt
->sc_cm_id
->device
,
749 frmr
->page_list
= pl
;
750 INIT_LIST_HEAD(&frmr
->frmr_list
);
758 return ERR_PTR(-ENOMEM
);
761 static void rdma_dealloc_frmr_q(struct svcxprt_rdma
*xprt
)
763 struct svc_rdma_fastreg_mr
*frmr
;
765 while (!list_empty(&xprt
->sc_frmr_q
)) {
766 frmr
= list_entry(xprt
->sc_frmr_q
.next
,
767 struct svc_rdma_fastreg_mr
, frmr_list
);
768 list_del_init(&frmr
->frmr_list
);
769 ib_dereg_mr(frmr
->mr
);
770 ib_free_fast_reg_page_list(frmr
->page_list
);
775 struct svc_rdma_fastreg_mr
*svc_rdma_get_frmr(struct svcxprt_rdma
*rdma
)
777 struct svc_rdma_fastreg_mr
*frmr
= NULL
;
779 spin_lock_bh(&rdma
->sc_frmr_q_lock
);
780 if (!list_empty(&rdma
->sc_frmr_q
)) {
781 frmr
= list_entry(rdma
->sc_frmr_q
.next
,
782 struct svc_rdma_fastreg_mr
, frmr_list
);
783 list_del_init(&frmr
->frmr_list
);
785 frmr
->page_list_len
= 0;
787 spin_unlock_bh(&rdma
->sc_frmr_q_lock
);
791 return rdma_alloc_frmr(rdma
);
794 static void frmr_unmap_dma(struct svcxprt_rdma
*xprt
,
795 struct svc_rdma_fastreg_mr
*frmr
)
798 for (page_no
= 0; page_no
< frmr
->page_list_len
; page_no
++) {
799 dma_addr_t addr
= frmr
->page_list
->page_list
[page_no
];
800 if (ib_dma_mapping_error(frmr
->mr
->device
, addr
))
802 atomic_dec(&xprt
->sc_dma_used
);
803 ib_dma_unmap_page(frmr
->mr
->device
, addr
, PAGE_SIZE
,
808 void svc_rdma_put_frmr(struct svcxprt_rdma
*rdma
,
809 struct svc_rdma_fastreg_mr
*frmr
)
812 frmr_unmap_dma(rdma
, frmr
);
813 spin_lock_bh(&rdma
->sc_frmr_q_lock
);
814 BUG_ON(!list_empty(&frmr
->frmr_list
));
815 list_add(&frmr
->frmr_list
, &rdma
->sc_frmr_q
);
816 spin_unlock_bh(&rdma
->sc_frmr_q_lock
);
821 * This is the xpo_recvfrom function for listening endpoints. Its
822 * purpose is to accept incoming connections. The CMA callback handler
823 * has already created a new transport and attached it to the new CMA
826 * There is a queue of pending connections hung on the listening
827 * transport. This queue contains the new svc_xprt structure. This
828 * function takes svc_xprt structures off the accept_q and completes
831 static struct svc_xprt
*svc_rdma_accept(struct svc_xprt
*xprt
)
833 struct svcxprt_rdma
*listen_rdma
;
834 struct svcxprt_rdma
*newxprt
= NULL
;
835 struct rdma_conn_param conn_param
;
836 struct ib_qp_init_attr qp_attr
;
837 struct ib_device_attr devattr
;
838 int uninitialized_var(dma_mr_acc
);
843 listen_rdma
= container_of(xprt
, struct svcxprt_rdma
, sc_xprt
);
844 clear_bit(XPT_CONN
, &xprt
->xpt_flags
);
845 /* Get the next entry off the accept list */
846 spin_lock_bh(&listen_rdma
->sc_lock
);
847 if (!list_empty(&listen_rdma
->sc_accept_q
)) {
848 newxprt
= list_entry(listen_rdma
->sc_accept_q
.next
,
849 struct svcxprt_rdma
, sc_accept_q
);
850 list_del_init(&newxprt
->sc_accept_q
);
852 if (!list_empty(&listen_rdma
->sc_accept_q
))
853 set_bit(XPT_CONN
, &listen_rdma
->sc_xprt
.xpt_flags
);
854 spin_unlock_bh(&listen_rdma
->sc_lock
);
858 dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
859 newxprt
, newxprt
->sc_cm_id
);
861 ret
= ib_query_device(newxprt
->sc_cm_id
->device
, &devattr
);
863 dprintk("svcrdma: could not query device attributes on "
864 "device %p, rc=%d\n", newxprt
->sc_cm_id
->device
, ret
);
868 /* Qualify the transport resource defaults with the
869 * capabilities of this particular device */
870 newxprt
->sc_max_sge
= min((size_t)devattr
.max_sge
,
871 (size_t)RPCSVC_MAXPAGES
);
872 newxprt
->sc_max_requests
= min((size_t)devattr
.max_qp_wr
,
873 (size_t)svcrdma_max_requests
);
874 newxprt
->sc_sq_depth
= RPCRDMA_SQ_DEPTH_MULT
* newxprt
->sc_max_requests
;
877 * Limit ORD based on client limit, local device limit, and
878 * configured svcrdma limit.
880 newxprt
->sc_ord
= min_t(size_t, devattr
.max_qp_rd_atom
, newxprt
->sc_ord
);
881 newxprt
->sc_ord
= min_t(size_t, svcrdma_ord
, newxprt
->sc_ord
);
883 newxprt
->sc_pd
= ib_alloc_pd(newxprt
->sc_cm_id
->device
);
884 if (IS_ERR(newxprt
->sc_pd
)) {
885 dprintk("svcrdma: error creating PD for connect request\n");
888 newxprt
->sc_sq_cq
= ib_create_cq(newxprt
->sc_cm_id
->device
,
892 newxprt
->sc_sq_depth
,
894 if (IS_ERR(newxprt
->sc_sq_cq
)) {
895 dprintk("svcrdma: error creating SQ CQ for connect request\n");
898 newxprt
->sc_rq_cq
= ib_create_cq(newxprt
->sc_cm_id
->device
,
902 newxprt
->sc_max_requests
,
904 if (IS_ERR(newxprt
->sc_rq_cq
)) {
905 dprintk("svcrdma: error creating RQ CQ for connect request\n");
909 memset(&qp_attr
, 0, sizeof qp_attr
);
910 qp_attr
.event_handler
= qp_event_handler
;
911 qp_attr
.qp_context
= &newxprt
->sc_xprt
;
912 qp_attr
.cap
.max_send_wr
= newxprt
->sc_sq_depth
;
913 qp_attr
.cap
.max_recv_wr
= newxprt
->sc_max_requests
;
914 qp_attr
.cap
.max_send_sge
= newxprt
->sc_max_sge
;
915 qp_attr
.cap
.max_recv_sge
= newxprt
->sc_max_sge
;
916 qp_attr
.sq_sig_type
= IB_SIGNAL_REQ_WR
;
917 qp_attr
.qp_type
= IB_QPT_RC
;
918 qp_attr
.send_cq
= newxprt
->sc_sq_cq
;
919 qp_attr
.recv_cq
= newxprt
->sc_rq_cq
;
920 dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n"
921 " cm_id->device=%p, sc_pd->device=%p\n"
922 " cap.max_send_wr = %d\n"
923 " cap.max_recv_wr = %d\n"
924 " cap.max_send_sge = %d\n"
925 " cap.max_recv_sge = %d\n",
926 newxprt
->sc_cm_id
, newxprt
->sc_pd
,
927 newxprt
->sc_cm_id
->device
, newxprt
->sc_pd
->device
,
928 qp_attr
.cap
.max_send_wr
,
929 qp_attr
.cap
.max_recv_wr
,
930 qp_attr
.cap
.max_send_sge
,
931 qp_attr
.cap
.max_recv_sge
);
933 ret
= rdma_create_qp(newxprt
->sc_cm_id
, newxprt
->sc_pd
, &qp_attr
);
936 * XXX: This is a hack. We need a xx_request_qp interface
937 * that will adjust the qp_attr's with a best-effort
940 qp_attr
.cap
.max_send_sge
-= 2;
941 qp_attr
.cap
.max_recv_sge
-= 2;
942 ret
= rdma_create_qp(newxprt
->sc_cm_id
, newxprt
->sc_pd
,
945 dprintk("svcrdma: failed to create QP, ret=%d\n", ret
);
948 newxprt
->sc_max_sge
= qp_attr
.cap
.max_send_sge
;
949 newxprt
->sc_max_sge
= qp_attr
.cap
.max_recv_sge
;
950 newxprt
->sc_sq_depth
= qp_attr
.cap
.max_send_wr
;
951 newxprt
->sc_max_requests
= qp_attr
.cap
.max_recv_wr
;
953 newxprt
->sc_qp
= newxprt
->sc_cm_id
->qp
;
956 * Use the most secure set of MR resources based on the
957 * transport type and available memory management features in
958 * the device. Here's the table implemented below:
960 * Fast Global DMA Remote WR
962 * Sup'd Sup'd Needed Needed
974 * NB: iWARP requires remote write access for the data sink
975 * of an RDMA_READ. IB does not.
977 if (devattr
.device_cap_flags
& IB_DEVICE_MEM_MGT_EXTENSIONS
) {
978 newxprt
->sc_frmr_pg_list_len
=
979 devattr
.max_fast_reg_page_list_len
;
980 newxprt
->sc_dev_caps
|= SVCRDMA_DEVCAP_FAST_REG
;
984 * Determine if a DMA MR is required and if so, what privs are required
986 switch (rdma_node_get_transport(newxprt
->sc_cm_id
->device
->node_type
)) {
987 case RDMA_TRANSPORT_IWARP
:
988 newxprt
->sc_dev_caps
|= SVCRDMA_DEVCAP_READ_W_INV
;
989 if (!(newxprt
->sc_dev_caps
& SVCRDMA_DEVCAP_FAST_REG
)) {
992 (IB_ACCESS_LOCAL_WRITE
|
993 IB_ACCESS_REMOTE_WRITE
);
994 } else if (!(devattr
.device_cap_flags
& IB_DEVICE_LOCAL_DMA_LKEY
)) {
996 dma_mr_acc
= IB_ACCESS_LOCAL_WRITE
;
1000 case RDMA_TRANSPORT_IB
:
1001 if (!(devattr
.device_cap_flags
& IB_DEVICE_LOCAL_DMA_LKEY
)) {
1003 dma_mr_acc
= IB_ACCESS_LOCAL_WRITE
;
1011 /* Create the DMA MR if needed, otherwise, use the DMA LKEY */
1013 /* Register all of physical memory */
1014 newxprt
->sc_phys_mr
=
1015 ib_get_dma_mr(newxprt
->sc_pd
, dma_mr_acc
);
1016 if (IS_ERR(newxprt
->sc_phys_mr
)) {
1017 dprintk("svcrdma: Failed to create DMA MR ret=%d\n",
1021 newxprt
->sc_dma_lkey
= newxprt
->sc_phys_mr
->lkey
;
1023 newxprt
->sc_dma_lkey
=
1024 newxprt
->sc_cm_id
->device
->local_dma_lkey
;
1026 /* Post receive buffers */
1027 for (i
= 0; i
< newxprt
->sc_max_requests
; i
++) {
1028 ret
= svc_rdma_post_recv(newxprt
);
1030 dprintk("svcrdma: failure posting receive buffers\n");
1035 /* Swap out the handler */
1036 newxprt
->sc_cm_id
->event_handler
= rdma_cma_handler
;
1039 * Arm the CQs for the SQ and RQ before accepting so we can't
1040 * miss the first message
1042 ib_req_notify_cq(newxprt
->sc_sq_cq
, IB_CQ_NEXT_COMP
);
1043 ib_req_notify_cq(newxprt
->sc_rq_cq
, IB_CQ_NEXT_COMP
);
1045 /* Accept Connection */
1046 set_bit(RDMAXPRT_CONN_PENDING
, &newxprt
->sc_flags
);
1047 memset(&conn_param
, 0, sizeof conn_param
);
1048 conn_param
.responder_resources
= 0;
1049 conn_param
.initiator_depth
= newxprt
->sc_ord
;
1050 ret
= rdma_accept(newxprt
->sc_cm_id
, &conn_param
);
1052 dprintk("svcrdma: failed to accept new connection, ret=%d\n",
1057 dprintk("svcrdma: new connection %p accepted with the following "
1059 " local_ip : %pI4\n"
1060 " local_port : %d\n"
1061 " remote_ip : %pI4\n"
1062 " remote_port : %d\n"
1065 " max_requests : %d\n"
1068 &((struct sockaddr_in
*)&newxprt
->sc_cm_id
->
1069 route
.addr
.src_addr
)->sin_addr
.s_addr
,
1070 ntohs(((struct sockaddr_in
*)&newxprt
->sc_cm_id
->
1071 route
.addr
.src_addr
)->sin_port
),
1072 &((struct sockaddr_in
*)&newxprt
->sc_cm_id
->
1073 route
.addr
.dst_addr
)->sin_addr
.s_addr
,
1074 ntohs(((struct sockaddr_in
*)&newxprt
->sc_cm_id
->
1075 route
.addr
.dst_addr
)->sin_port
),
1076 newxprt
->sc_max_sge
,
1077 newxprt
->sc_sq_depth
,
1078 newxprt
->sc_max_requests
,
1081 return &newxprt
->sc_xprt
;
1084 dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret
);
1085 /* Take a reference in case the DTO handler runs */
1086 svc_xprt_get(&newxprt
->sc_xprt
);
1087 if (newxprt
->sc_qp
&& !IS_ERR(newxprt
->sc_qp
))
1088 ib_destroy_qp(newxprt
->sc_qp
);
1089 rdma_destroy_id(newxprt
->sc_cm_id
);
1090 /* This call to put will destroy the transport */
1091 svc_xprt_put(&newxprt
->sc_xprt
);
1095 static void svc_rdma_release_rqst(struct svc_rqst
*rqstp
)
1100 * When connected, an svc_xprt has at least two references:
1102 * - A reference held by the cm_id between the ESTABLISHED and
1103 * DISCONNECTED events. If the remote peer disconnected first, this
1104 * reference could be gone.
1106 * - A reference held by the svc_recv code that called this function
1107 * as part of close processing.
1109 * At a minimum one references should still be held.
1111 static void svc_rdma_detach(struct svc_xprt
*xprt
)
1113 struct svcxprt_rdma
*rdma
=
1114 container_of(xprt
, struct svcxprt_rdma
, sc_xprt
);
1115 dprintk("svc: svc_rdma_detach(%p)\n", xprt
);
1117 /* Disconnect and flush posted WQE */
1118 rdma_disconnect(rdma
->sc_cm_id
);
1121 static void __svc_rdma_free(struct work_struct
*work
)
1123 struct svcxprt_rdma
*rdma
=
1124 container_of(work
, struct svcxprt_rdma
, sc_work
);
1125 dprintk("svcrdma: svc_rdma_free(%p)\n", rdma
);
1127 /* We should only be called from kref_put */
1128 BUG_ON(atomic_read(&rdma
->sc_xprt
.xpt_ref
.refcount
) != 0);
1131 * Destroy queued, but not processed read completions. Note
1132 * that this cleanup has to be done before destroying the
1133 * cm_id because the device ptr is needed to unmap the dma in
1134 * svc_rdma_put_context.
1136 while (!list_empty(&rdma
->sc_read_complete_q
)) {
1137 struct svc_rdma_op_ctxt
*ctxt
;
1138 ctxt
= list_entry(rdma
->sc_read_complete_q
.next
,
1139 struct svc_rdma_op_ctxt
,
1141 list_del_init(&ctxt
->dto_q
);
1142 svc_rdma_put_context(ctxt
, 1);
1145 /* Destroy queued, but not processed recv completions */
1146 while (!list_empty(&rdma
->sc_rq_dto_q
)) {
1147 struct svc_rdma_op_ctxt
*ctxt
;
1148 ctxt
= list_entry(rdma
->sc_rq_dto_q
.next
,
1149 struct svc_rdma_op_ctxt
,
1151 list_del_init(&ctxt
->dto_q
);
1152 svc_rdma_put_context(ctxt
, 1);
1155 /* Warn if we leaked a resource or under-referenced */
1156 WARN_ON(atomic_read(&rdma
->sc_ctxt_used
) != 0);
1157 WARN_ON(atomic_read(&rdma
->sc_dma_used
) != 0);
1159 /* De-allocate fastreg mr */
1160 rdma_dealloc_frmr_q(rdma
);
1162 /* Destroy the QP if present (not a listener) */
1163 if (rdma
->sc_qp
&& !IS_ERR(rdma
->sc_qp
))
1164 ib_destroy_qp(rdma
->sc_qp
);
1166 if (rdma
->sc_sq_cq
&& !IS_ERR(rdma
->sc_sq_cq
))
1167 ib_destroy_cq(rdma
->sc_sq_cq
);
1169 if (rdma
->sc_rq_cq
&& !IS_ERR(rdma
->sc_rq_cq
))
1170 ib_destroy_cq(rdma
->sc_rq_cq
);
1172 if (rdma
->sc_phys_mr
&& !IS_ERR(rdma
->sc_phys_mr
))
1173 ib_dereg_mr(rdma
->sc_phys_mr
);
1175 if (rdma
->sc_pd
&& !IS_ERR(rdma
->sc_pd
))
1176 ib_dealloc_pd(rdma
->sc_pd
);
1178 /* Destroy the CM ID */
1179 rdma_destroy_id(rdma
->sc_cm_id
);
1184 static void svc_rdma_free(struct svc_xprt
*xprt
)
1186 struct svcxprt_rdma
*rdma
=
1187 container_of(xprt
, struct svcxprt_rdma
, sc_xprt
);
1188 INIT_WORK(&rdma
->sc_work
, __svc_rdma_free
);
1189 queue_work(svc_rdma_wq
, &rdma
->sc_work
);
1192 static int svc_rdma_has_wspace(struct svc_xprt
*xprt
)
1194 struct svcxprt_rdma
*rdma
=
1195 container_of(xprt
, struct svcxprt_rdma
, sc_xprt
);
1198 * If there are fewer SQ WR available than required to send a
1199 * simple response, return false.
1201 if ((rdma
->sc_sq_depth
- atomic_read(&rdma
->sc_sq_count
) < 3))
1205 * ...or there are already waiters on the SQ,
1208 if (waitqueue_active(&rdma
->sc_send_wait
))
1211 /* Otherwise return true. */
1216 * Attempt to register the kvec representing the RPC memory with the
1220 * NULL : The device does not support fastreg or there were no more
1222 * frmr : The kvec register request was successfully posted.
1223 * <0 : An error was encountered attempting to register the kvec.
1225 int svc_rdma_fastreg(struct svcxprt_rdma
*xprt
,
1226 struct svc_rdma_fastreg_mr
*frmr
)
1228 struct ib_send_wr fastreg_wr
;
1232 key
= (u8
)(frmr
->mr
->lkey
& 0x000000FF);
1233 ib_update_fast_reg_key(frmr
->mr
, ++key
);
1235 /* Prepare FASTREG WR */
1236 memset(&fastreg_wr
, 0, sizeof fastreg_wr
);
1237 fastreg_wr
.opcode
= IB_WR_FAST_REG_MR
;
1238 fastreg_wr
.send_flags
= IB_SEND_SIGNALED
;
1239 fastreg_wr
.wr
.fast_reg
.iova_start
= (unsigned long)frmr
->kva
;
1240 fastreg_wr
.wr
.fast_reg
.page_list
= frmr
->page_list
;
1241 fastreg_wr
.wr
.fast_reg
.page_list_len
= frmr
->page_list_len
;
1242 fastreg_wr
.wr
.fast_reg
.page_shift
= PAGE_SHIFT
;
1243 fastreg_wr
.wr
.fast_reg
.length
= frmr
->map_len
;
1244 fastreg_wr
.wr
.fast_reg
.access_flags
= frmr
->access_flags
;
1245 fastreg_wr
.wr
.fast_reg
.rkey
= frmr
->mr
->lkey
;
1246 return svc_rdma_send(xprt
, &fastreg_wr
);
1249 int svc_rdma_send(struct svcxprt_rdma
*xprt
, struct ib_send_wr
*wr
)
1251 struct ib_send_wr
*bad_wr
, *n_wr
;
1256 if (test_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
))
1259 BUG_ON(wr
->send_flags
!= IB_SEND_SIGNALED
);
1261 for (n_wr
= wr
->next
; n_wr
; n_wr
= n_wr
->next
)
1264 /* If the SQ is full, wait until an SQ entry is available */
1266 spin_lock_bh(&xprt
->sc_lock
);
1267 if (xprt
->sc_sq_depth
< atomic_read(&xprt
->sc_sq_count
) + wr_count
) {
1268 spin_unlock_bh(&xprt
->sc_lock
);
1269 atomic_inc(&rdma_stat_sq_starve
);
1271 /* See if we can opportunistically reap SQ WR to make room */
1274 /* Wait until SQ WR available if SQ still full */
1275 wait_event(xprt
->sc_send_wait
,
1276 atomic_read(&xprt
->sc_sq_count
) <
1278 if (test_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
))
1282 /* Take a transport ref for each WR posted */
1283 for (i
= 0; i
< wr_count
; i
++)
1284 svc_xprt_get(&xprt
->sc_xprt
);
1286 /* Bump used SQ WR count and post */
1287 atomic_add(wr_count
, &xprt
->sc_sq_count
);
1288 ret
= ib_post_send(xprt
->sc_qp
, wr
, &bad_wr
);
1290 set_bit(XPT_CLOSE
, &xprt
->sc_xprt
.xpt_flags
);
1291 atomic_sub(wr_count
, &xprt
->sc_sq_count
);
1292 for (i
= 0; i
< wr_count
; i
++)
1293 svc_xprt_put(&xprt
->sc_xprt
);
1294 dprintk("svcrdma: failed to post SQ WR rc=%d, "
1295 "sc_sq_count=%d, sc_sq_depth=%d\n",
1296 ret
, atomic_read(&xprt
->sc_sq_count
),
1299 spin_unlock_bh(&xprt
->sc_lock
);
1301 wake_up(&xprt
->sc_send_wait
);
1307 void svc_rdma_send_error(struct svcxprt_rdma
*xprt
, struct rpcrdma_msg
*rmsgp
,
1308 enum rpcrdma_errcode err
)
1310 struct ib_send_wr err_wr
;
1312 struct svc_rdma_op_ctxt
*ctxt
;
1317 p
= svc_rdma_get_page();
1318 va
= page_address(p
);
1320 /* XDR encode error */
1321 length
= svc_rdma_xdr_encode_error(xprt
, rmsgp
, err
, va
);
1323 ctxt
= svc_rdma_get_context(xprt
);
1324 ctxt
->direction
= DMA_FROM_DEVICE
;
1328 /* Prepare SGE for local address */
1329 ctxt
->sge
[0].addr
= ib_dma_map_page(xprt
->sc_cm_id
->device
,
1330 p
, 0, length
, DMA_FROM_DEVICE
);
1331 if (ib_dma_mapping_error(xprt
->sc_cm_id
->device
, ctxt
->sge
[0].addr
)) {
1333 svc_rdma_put_context(ctxt
, 1);
1336 atomic_inc(&xprt
->sc_dma_used
);
1337 ctxt
->sge
[0].lkey
= xprt
->sc_dma_lkey
;
1338 ctxt
->sge
[0].length
= length
;
1340 /* Prepare SEND WR */
1341 memset(&err_wr
, 0, sizeof err_wr
);
1342 ctxt
->wr_op
= IB_WR_SEND
;
1343 err_wr
.wr_id
= (unsigned long)ctxt
;
1344 err_wr
.sg_list
= ctxt
->sge
;
1346 err_wr
.opcode
= IB_WR_SEND
;
1347 err_wr
.send_flags
= IB_SEND_SIGNALED
;
1350 ret
= svc_rdma_send(xprt
, &err_wr
);
1352 dprintk("svcrdma: Error %d posting send for protocol error\n",
1354 svc_rdma_unmap_dma(ctxt
);
1355 svc_rdma_put_context(ctxt
, 1);