2 * Copyright (c) 2003-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.
43 * Encapsulates the major functions managing:
50 #include <linux/interrupt.h>
51 #include <linux/slab.h>
52 #include <linux/prefetch.h>
53 #include <linux/sunrpc/addr.h>
54 #include <linux/sunrpc/svc_rdma.h>
55 #include <asm/bitops.h>
56 #include <linux/module.h> /* try_module_get()/module_put() */
58 #include "xprt_rdma.h"
64 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
65 # define RPCDBG_FACILITY RPCDBG_TRANS
72 static struct workqueue_struct
*rpcrdma_receive_wq
;
75 rpcrdma_alloc_wq(void)
77 struct workqueue_struct
*recv_wq
;
79 recv_wq
= alloc_workqueue("xprtrdma_receive",
80 WQ_MEM_RECLAIM
| WQ_UNBOUND
| WQ_HIGHPRI
,
85 rpcrdma_receive_wq
= recv_wq
;
90 rpcrdma_destroy_wq(void)
92 struct workqueue_struct
*wq
;
94 if (rpcrdma_receive_wq
) {
95 wq
= rpcrdma_receive_wq
;
96 rpcrdma_receive_wq
= NULL
;
97 destroy_workqueue(wq
);
102 rpcrdma_qp_async_error_upcall(struct ib_event
*event
, void *context
)
104 struct rpcrdma_ep
*ep
= context
;
106 pr_err("RPC: %s: %s on device %s ep %p\n",
107 __func__
, ib_event_msg(event
->event
),
108 event
->device
->name
, context
);
109 if (ep
->rep_connected
== 1) {
110 ep
->rep_connected
= -EIO
;
111 rpcrdma_conn_func(ep
);
112 wake_up_all(&ep
->rep_connect_wait
);
117 * rpcrdma_wc_send - Invoked by RDMA provider for each polled Send WC
118 * @cq: completion queue (ignored)
123 rpcrdma_wc_send(struct ib_cq
*cq
, struct ib_wc
*wc
)
125 /* WARNING: Only wr_cqe and status are reliable at this point */
126 if (wc
->status
!= IB_WC_SUCCESS
&& wc
->status
!= IB_WC_WR_FLUSH_ERR
)
127 pr_err("rpcrdma: Send: %s (%u/0x%x)\n",
128 ib_wc_status_msg(wc
->status
),
129 wc
->status
, wc
->vendor_err
);
133 rpcrdma_receive_worker(struct work_struct
*work
)
135 struct rpcrdma_rep
*rep
=
136 container_of(work
, struct rpcrdma_rep
, rr_work
);
138 rpcrdma_reply_handler(rep
);
141 /* Perform basic sanity checking to avoid using garbage
142 * to update the credit grant value.
145 rpcrdma_update_granted_credits(struct rpcrdma_rep
*rep
)
147 struct rpcrdma_msg
*rmsgp
= rdmab_to_msg(rep
->rr_rdmabuf
);
148 struct rpcrdma_buffer
*buffer
= &rep
->rr_rxprt
->rx_buf
;
151 if (rep
->rr_len
< RPCRDMA_HDRLEN_ERR
)
154 credits
= be32_to_cpu(rmsgp
->rm_credit
);
156 credits
= 1; /* don't deadlock */
157 else if (credits
> buffer
->rb_max_requests
)
158 credits
= buffer
->rb_max_requests
;
160 atomic_set(&buffer
->rb_credits
, credits
);
164 * rpcrdma_receive_wc - Invoked by RDMA provider for each polled Receive WC
165 * @cq: completion queue (ignored)
170 rpcrdma_receive_wc(struct ib_cq
*cq
, struct ib_wc
*wc
)
172 struct ib_cqe
*cqe
= wc
->wr_cqe
;
173 struct rpcrdma_rep
*rep
= container_of(cqe
, struct rpcrdma_rep
,
176 /* WARNING: Only wr_id and status are reliable at this point */
177 if (wc
->status
!= IB_WC_SUCCESS
)
180 /* status == SUCCESS means all fields in wc are trustworthy */
181 if (wc
->opcode
!= IB_WC_RECV
)
184 dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
185 __func__
, rep
, wc
->byte_len
);
187 rep
->rr_len
= wc
->byte_len
;
188 ib_dma_sync_single_for_cpu(rep
->rr_device
,
189 rdmab_addr(rep
->rr_rdmabuf
),
190 rep
->rr_len
, DMA_FROM_DEVICE
);
192 rpcrdma_update_granted_credits(rep
);
195 queue_work(rpcrdma_receive_wq
, &rep
->rr_work
);
199 if (wc
->status
!= IB_WC_WR_FLUSH_ERR
)
200 pr_err("rpcrdma: Recv: %s (%u/0x%x)\n",
201 ib_wc_status_msg(wc
->status
),
202 wc
->status
, wc
->vendor_err
);
203 rep
->rr_len
= RPCRDMA_BAD_LEN
;
208 rpcrdma_conn_upcall(struct rdma_cm_id
*id
, struct rdma_cm_event
*event
)
210 struct rpcrdma_xprt
*xprt
= id
->context
;
211 struct rpcrdma_ia
*ia
= &xprt
->rx_ia
;
212 struct rpcrdma_ep
*ep
= &xprt
->rx_ep
;
213 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
214 struct sockaddr
*sap
= (struct sockaddr
*)&ep
->rep_remote_addr
;
216 struct ib_qp_attr
*attr
= &ia
->ri_qp_attr
;
217 struct ib_qp_init_attr
*iattr
= &ia
->ri_qp_init_attr
;
220 switch (event
->event
) {
221 case RDMA_CM_EVENT_ADDR_RESOLVED
:
222 case RDMA_CM_EVENT_ROUTE_RESOLVED
:
224 complete(&ia
->ri_done
);
226 case RDMA_CM_EVENT_ADDR_ERROR
:
227 ia
->ri_async_rc
= -EHOSTUNREACH
;
228 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
230 complete(&ia
->ri_done
);
232 case RDMA_CM_EVENT_ROUTE_ERROR
:
233 ia
->ri_async_rc
= -ENETUNREACH
;
234 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
236 complete(&ia
->ri_done
);
238 case RDMA_CM_EVENT_ESTABLISHED
:
240 ib_query_qp(ia
->ri_id
->qp
, attr
,
241 IB_QP_MAX_QP_RD_ATOMIC
| IB_QP_MAX_DEST_RD_ATOMIC
,
243 dprintk("RPC: %s: %d responder resources"
245 __func__
, attr
->max_dest_rd_atomic
,
246 attr
->max_rd_atomic
);
248 case RDMA_CM_EVENT_CONNECT_ERROR
:
249 connstate
= -ENOTCONN
;
251 case RDMA_CM_EVENT_UNREACHABLE
:
252 connstate
= -ENETDOWN
;
254 case RDMA_CM_EVENT_REJECTED
:
255 connstate
= -ECONNREFUSED
;
257 case RDMA_CM_EVENT_DISCONNECTED
:
258 connstate
= -ECONNABORTED
;
260 case RDMA_CM_EVENT_DEVICE_REMOVAL
:
263 dprintk("RPC: %s: %sconnected\n",
264 __func__
, connstate
> 0 ? "" : "dis");
265 atomic_set(&xprt
->rx_buf
.rb_credits
, 1);
266 ep
->rep_connected
= connstate
;
267 rpcrdma_conn_func(ep
);
268 wake_up_all(&ep
->rep_connect_wait
);
271 dprintk("RPC: %s: %pIS:%u (ep 0x%p): %s\n",
272 __func__
, sap
, rpc_get_port(sap
), ep
,
273 rdma_event_msg(event
->event
));
277 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
278 if (connstate
== 1) {
279 int ird
= attr
->max_dest_rd_atomic
;
280 int tird
= ep
->rep_remote_cma
.responder_resources
;
282 pr_info("rpcrdma: connection to %pIS:%u on %s, memreg '%s', %d credits, %d responders%s\n",
283 sap
, rpc_get_port(sap
),
285 ia
->ri_ops
->ro_displayname
,
286 xprt
->rx_buf
.rb_max_requests
,
287 ird
, ird
< 4 && ird
< tird
/ 2 ? " (low!)" : "");
288 } else if (connstate
< 0) {
289 pr_info("rpcrdma: connection to %pIS:%u closed (%d)\n",
290 sap
, rpc_get_port(sap
), connstate
);
297 static void rpcrdma_destroy_id(struct rdma_cm_id
*id
)
300 module_put(id
->device
->owner
);
305 static struct rdma_cm_id
*
306 rpcrdma_create_id(struct rpcrdma_xprt
*xprt
,
307 struct rpcrdma_ia
*ia
, struct sockaddr
*addr
)
309 struct rdma_cm_id
*id
;
312 init_completion(&ia
->ri_done
);
314 id
= rdma_create_id(&init_net
, rpcrdma_conn_upcall
, xprt
, RDMA_PS_TCP
,
318 dprintk("RPC: %s: rdma_create_id() failed %i\n",
323 ia
->ri_async_rc
= -ETIMEDOUT
;
324 rc
= rdma_resolve_addr(id
, NULL
, addr
, RDMA_RESOLVE_TIMEOUT
);
326 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
330 wait_for_completion_interruptible_timeout(&ia
->ri_done
,
331 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT
) + 1);
334 * Until xprtrdma supports DEVICE_REMOVAL, the provider must
335 * be pinned while there are active NFS/RDMA mounts to prevent
336 * hangs and crashes at umount time.
338 if (!ia
->ri_async_rc
&& !try_module_get(id
->device
->owner
)) {
339 dprintk("RPC: %s: Failed to get device module\n",
341 ia
->ri_async_rc
= -ENODEV
;
343 rc
= ia
->ri_async_rc
;
347 ia
->ri_async_rc
= -ETIMEDOUT
;
348 rc
= rdma_resolve_route(id
, RDMA_RESOLVE_TIMEOUT
);
350 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
354 wait_for_completion_interruptible_timeout(&ia
->ri_done
,
355 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT
) + 1);
356 rc
= ia
->ri_async_rc
;
362 module_put(id
->device
->owner
);
369 * Exported functions.
373 * Open and initialize an Interface Adapter.
374 * o initializes fields of struct rpcrdma_ia, including
375 * interface and provider attributes and protection zone.
378 rpcrdma_ia_open(struct rpcrdma_xprt
*xprt
, struct sockaddr
*addr
, int memreg
)
380 struct rpcrdma_ia
*ia
= &xprt
->rx_ia
;
383 ia
->ri_id
= rpcrdma_create_id(xprt
, ia
, addr
);
384 if (IS_ERR(ia
->ri_id
)) {
385 rc
= PTR_ERR(ia
->ri_id
);
388 ia
->ri_device
= ia
->ri_id
->device
;
390 ia
->ri_pd
= ib_alloc_pd(ia
->ri_device
);
391 if (IS_ERR(ia
->ri_pd
)) {
392 rc
= PTR_ERR(ia
->ri_pd
);
393 pr_err("rpcrdma: ib_alloc_pd() returned %d\n", rc
);
399 if (frwr_is_supported(ia
)) {
400 ia
->ri_ops
= &rpcrdma_frwr_memreg_ops
;
404 case RPCRDMA_MTHCAFMR
:
405 if (fmr_is_supported(ia
)) {
406 ia
->ri_ops
= &rpcrdma_fmr_memreg_ops
;
411 pr_err("rpcrdma: Unsupported memory registration mode: %d\n",
420 ib_dealloc_pd(ia
->ri_pd
);
423 rpcrdma_destroy_id(ia
->ri_id
);
430 * Clean up/close an IA.
431 * o if event handles and PD have been initialized, free them.
435 rpcrdma_ia_close(struct rpcrdma_ia
*ia
)
437 dprintk("RPC: %s: entering\n", __func__
);
438 if (ia
->ri_id
!= NULL
&& !IS_ERR(ia
->ri_id
)) {
440 rdma_destroy_qp(ia
->ri_id
);
441 rpcrdma_destroy_id(ia
->ri_id
);
445 /* If the pd is still busy, xprtrdma missed freeing a resource */
446 if (ia
->ri_pd
&& !IS_ERR(ia
->ri_pd
))
447 ib_dealloc_pd(ia
->ri_pd
);
451 * Create unconnected endpoint.
454 rpcrdma_ep_create(struct rpcrdma_ep
*ep
, struct rpcrdma_ia
*ia
,
455 struct rpcrdma_create_data_internal
*cdata
)
457 struct ib_cq
*sendcq
, *recvcq
;
458 unsigned int max_qp_wr
;
461 if (ia
->ri_device
->attrs
.max_sge
< RPCRDMA_MAX_IOVS
) {
462 dprintk("RPC: %s: insufficient sge's available\n",
467 if (ia
->ri_device
->attrs
.max_qp_wr
<= RPCRDMA_BACKWARD_WRS
) {
468 dprintk("RPC: %s: insufficient wqe's available\n",
472 max_qp_wr
= ia
->ri_device
->attrs
.max_qp_wr
- RPCRDMA_BACKWARD_WRS
- 1;
474 /* check provider's send/recv wr limits */
475 if (cdata
->max_requests
> max_qp_wr
)
476 cdata
->max_requests
= max_qp_wr
;
478 ep
->rep_attr
.event_handler
= rpcrdma_qp_async_error_upcall
;
479 ep
->rep_attr
.qp_context
= ep
;
480 ep
->rep_attr
.srq
= NULL
;
481 ep
->rep_attr
.cap
.max_send_wr
= cdata
->max_requests
;
482 ep
->rep_attr
.cap
.max_send_wr
+= RPCRDMA_BACKWARD_WRS
;
483 ep
->rep_attr
.cap
.max_send_wr
+= 1; /* drain cqe */
484 rc
= ia
->ri_ops
->ro_open(ia
, ep
, cdata
);
487 ep
->rep_attr
.cap
.max_recv_wr
= cdata
->max_requests
;
488 ep
->rep_attr
.cap
.max_recv_wr
+= RPCRDMA_BACKWARD_WRS
;
489 ep
->rep_attr
.cap
.max_recv_wr
+= 1; /* drain cqe */
490 ep
->rep_attr
.cap
.max_send_sge
= RPCRDMA_MAX_IOVS
;
491 ep
->rep_attr
.cap
.max_recv_sge
= 1;
492 ep
->rep_attr
.cap
.max_inline_data
= 0;
493 ep
->rep_attr
.sq_sig_type
= IB_SIGNAL_REQ_WR
;
494 ep
->rep_attr
.qp_type
= IB_QPT_RC
;
495 ep
->rep_attr
.port_num
= ~0;
497 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
498 "iovs: send %d recv %d\n",
500 ep
->rep_attr
.cap
.max_send_wr
,
501 ep
->rep_attr
.cap
.max_recv_wr
,
502 ep
->rep_attr
.cap
.max_send_sge
,
503 ep
->rep_attr
.cap
.max_recv_sge
);
505 /* set trigger for requesting send completion */
506 ep
->rep_cqinit
= ep
->rep_attr
.cap
.max_send_wr
/2 - 1;
507 if (ep
->rep_cqinit
<= 2)
508 ep
->rep_cqinit
= 0; /* always signal? */
510 init_waitqueue_head(&ep
->rep_connect_wait
);
511 INIT_DELAYED_WORK(&ep
->rep_connect_worker
, rpcrdma_connect_worker
);
513 sendcq
= ib_alloc_cq(ia
->ri_device
, NULL
,
514 ep
->rep_attr
.cap
.max_send_wr
+ 1,
516 if (IS_ERR(sendcq
)) {
517 rc
= PTR_ERR(sendcq
);
518 dprintk("RPC: %s: failed to create send CQ: %i\n",
523 recvcq
= ib_alloc_cq(ia
->ri_device
, NULL
,
524 ep
->rep_attr
.cap
.max_recv_wr
+ 1,
526 if (IS_ERR(recvcq
)) {
527 rc
= PTR_ERR(recvcq
);
528 dprintk("RPC: %s: failed to create recv CQ: %i\n",
533 ep
->rep_attr
.send_cq
= sendcq
;
534 ep
->rep_attr
.recv_cq
= recvcq
;
536 /* Initialize cma parameters */
537 memset(&ep
->rep_remote_cma
, 0, sizeof(ep
->rep_remote_cma
));
539 /* RPC/RDMA does not use private data */
540 ep
->rep_remote_cma
.private_data
= NULL
;
541 ep
->rep_remote_cma
.private_data_len
= 0;
543 /* Client offers RDMA Read but does not initiate */
544 ep
->rep_remote_cma
.initiator_depth
= 0;
545 if (ia
->ri_device
->attrs
.max_qp_rd_atom
> 32) /* arbitrary but <= 255 */
546 ep
->rep_remote_cma
.responder_resources
= 32;
548 ep
->rep_remote_cma
.responder_resources
=
549 ia
->ri_device
->attrs
.max_qp_rd_atom
;
551 /* Limit transport retries so client can detect server
552 * GID changes quickly. RPC layer handles re-establishing
553 * transport connection and retransmission.
555 ep
->rep_remote_cma
.retry_count
= 6;
557 /* RPC-over-RDMA handles its own flow control. In addition,
558 * make all RNR NAKs visible so we know that RPC-over-RDMA
559 * flow control is working correctly (no NAKs should be seen).
561 ep
->rep_remote_cma
.flow_control
= 0;
562 ep
->rep_remote_cma
.rnr_retry_count
= 0;
575 * Disconnect and destroy endpoint. After this, the only
576 * valid operations on the ep are to free it (if dynamically
577 * allocated) or re-create it.
580 rpcrdma_ep_destroy(struct rpcrdma_ep
*ep
, struct rpcrdma_ia
*ia
)
582 dprintk("RPC: %s: entering, connected is %d\n",
583 __func__
, ep
->rep_connected
);
585 cancel_delayed_work_sync(&ep
->rep_connect_worker
);
588 rpcrdma_ep_disconnect(ep
, ia
);
589 rdma_destroy_qp(ia
->ri_id
);
590 ia
->ri_id
->qp
= NULL
;
593 ib_free_cq(ep
->rep_attr
.recv_cq
);
594 ib_free_cq(ep
->rep_attr
.send_cq
);
598 * Connect unconnected endpoint.
601 rpcrdma_ep_connect(struct rpcrdma_ep
*ep
, struct rpcrdma_ia
*ia
)
603 struct rdma_cm_id
*id
, *old
;
607 if (ep
->rep_connected
!= 0) {
608 struct rpcrdma_xprt
*xprt
;
610 dprintk("RPC: %s: reconnecting...\n", __func__
);
612 rpcrdma_ep_disconnect(ep
, ia
);
614 xprt
= container_of(ia
, struct rpcrdma_xprt
, rx_ia
);
615 id
= rpcrdma_create_id(xprt
, ia
,
616 (struct sockaddr
*)&xprt
->rx_data
.addr
);
621 /* TEMP TEMP TEMP - fail if new device:
622 * Deregister/remarshal *all* requests!
623 * Close and recreate adapter, pd, etc!
624 * Re-determine all attributes still sane!
625 * More stuff I haven't thought of!
628 if (ia
->ri_device
!= id
->device
) {
629 printk("RPC: %s: can't reconnect on "
630 "different device!\n", __func__
);
631 rpcrdma_destroy_id(id
);
636 rc
= rdma_create_qp(id
, ia
->ri_pd
, &ep
->rep_attr
);
638 dprintk("RPC: %s: rdma_create_qp failed %i\n",
640 rpcrdma_destroy_id(id
);
648 rdma_destroy_qp(old
);
649 rpcrdma_destroy_id(old
);
651 dprintk("RPC: %s: connecting...\n", __func__
);
652 rc
= rdma_create_qp(ia
->ri_id
, ia
->ri_pd
, &ep
->rep_attr
);
654 dprintk("RPC: %s: rdma_create_qp failed %i\n",
656 /* do not update ep->rep_connected */
661 ep
->rep_connected
= 0;
663 rc
= rdma_connect(ia
->ri_id
, &ep
->rep_remote_cma
);
665 dprintk("RPC: %s: rdma_connect() failed with %i\n",
670 wait_event_interruptible(ep
->rep_connect_wait
, ep
->rep_connected
!= 0);
673 * Check state. A non-peer reject indicates no listener
674 * (ECONNREFUSED), which may be a transient state. All
675 * others indicate a transport condition which has already
676 * undergone a best-effort.
678 if (ep
->rep_connected
== -ECONNREFUSED
&&
679 ++retry_count
<= RDMA_CONNECT_RETRY_MAX
) {
680 dprintk("RPC: %s: non-peer_reject, retry\n", __func__
);
683 if (ep
->rep_connected
<= 0) {
684 /* Sometimes, the only way to reliably connect to remote
685 * CMs is to use same nonzero values for ORD and IRD. */
686 if (retry_count
++ <= RDMA_CONNECT_RETRY_MAX
+ 1 &&
687 (ep
->rep_remote_cma
.responder_resources
== 0 ||
688 ep
->rep_remote_cma
.initiator_depth
!=
689 ep
->rep_remote_cma
.responder_resources
)) {
690 if (ep
->rep_remote_cma
.responder_resources
== 0)
691 ep
->rep_remote_cma
.responder_resources
= 1;
692 ep
->rep_remote_cma
.initiator_depth
=
693 ep
->rep_remote_cma
.responder_resources
;
696 rc
= ep
->rep_connected
;
698 struct rpcrdma_xprt
*r_xprt
;
701 dprintk("RPC: %s: connected\n", __func__
);
703 r_xprt
= container_of(ia
, struct rpcrdma_xprt
, rx_ia
);
704 extras
= r_xprt
->rx_buf
.rb_bc_srv_max_requests
;
707 rc
= rpcrdma_ep_post_extra_recv(r_xprt
, extras
);
709 pr_warn("%s: rpcrdma_ep_post_extra_recv: %i\n",
718 ep
->rep_connected
= rc
;
723 * rpcrdma_ep_disconnect
725 * This is separate from destroy to facilitate the ability
726 * to reconnect without recreating the endpoint.
728 * This call is not reentrant, and must not be made in parallel
729 * on the same endpoint.
732 rpcrdma_ep_disconnect(struct rpcrdma_ep
*ep
, struct rpcrdma_ia
*ia
)
736 rc
= rdma_disconnect(ia
->ri_id
);
738 /* returns without wait if not connected */
739 wait_event_interruptible(ep
->rep_connect_wait
,
740 ep
->rep_connected
!= 1);
741 dprintk("RPC: %s: after wait, %sconnected\n", __func__
,
742 (ep
->rep_connected
== 1) ? "still " : "dis");
744 dprintk("RPC: %s: rdma_disconnect %i\n", __func__
, rc
);
745 ep
->rep_connected
= rc
;
748 ib_drain_qp(ia
->ri_id
->qp
);
752 rpcrdma_mr_recovery_worker(struct work_struct
*work
)
754 struct rpcrdma_buffer
*buf
= container_of(work
, struct rpcrdma_buffer
,
755 rb_recovery_worker
.work
);
756 struct rpcrdma_mw
*mw
;
758 spin_lock(&buf
->rb_recovery_lock
);
759 while (!list_empty(&buf
->rb_stale_mrs
)) {
760 mw
= list_first_entry(&buf
->rb_stale_mrs
,
761 struct rpcrdma_mw
, mw_list
);
762 list_del_init(&mw
->mw_list
);
763 spin_unlock(&buf
->rb_recovery_lock
);
765 dprintk("RPC: %s: recovering MR %p\n", __func__
, mw
);
766 mw
->mw_xprt
->rx_ia
.ri_ops
->ro_recover_mr(mw
);
768 spin_lock(&buf
->rb_recovery_lock
);
770 spin_unlock(&buf
->rb_recovery_lock
);
774 rpcrdma_defer_mr_recovery(struct rpcrdma_mw
*mw
)
776 struct rpcrdma_xprt
*r_xprt
= mw
->mw_xprt
;
777 struct rpcrdma_buffer
*buf
= &r_xprt
->rx_buf
;
779 spin_lock(&buf
->rb_recovery_lock
);
780 list_add(&mw
->mw_list
, &buf
->rb_stale_mrs
);
781 spin_unlock(&buf
->rb_recovery_lock
);
783 schedule_delayed_work(&buf
->rb_recovery_worker
, 0);
787 rpcrdma_create_mrs(struct rpcrdma_xprt
*r_xprt
)
789 struct rpcrdma_buffer
*buf
= &r_xprt
->rx_buf
;
790 struct rpcrdma_ia
*ia
= &r_xprt
->rx_ia
;
795 for (count
= 0; count
< 32; count
++) {
796 struct rpcrdma_mw
*mw
;
799 mw
= kzalloc(sizeof(*mw
), GFP_KERNEL
);
803 rc
= ia
->ri_ops
->ro_init_mr(ia
, mw
);
809 mw
->mw_xprt
= r_xprt
;
811 list_add(&mw
->mw_list
, &free
);
812 list_add(&mw
->mw_all
, &all
);
815 spin_lock(&buf
->rb_mwlock
);
816 list_splice(&free
, &buf
->rb_mws
);
817 list_splice(&all
, &buf
->rb_all
);
818 r_xprt
->rx_stats
.mrs_allocated
+= count
;
819 spin_unlock(&buf
->rb_mwlock
);
821 dprintk("RPC: %s: created %u MRs\n", __func__
, count
);
825 rpcrdma_mr_refresh_worker(struct work_struct
*work
)
827 struct rpcrdma_buffer
*buf
= container_of(work
, struct rpcrdma_buffer
,
828 rb_refresh_worker
.work
);
829 struct rpcrdma_xprt
*r_xprt
= container_of(buf
, struct rpcrdma_xprt
,
832 rpcrdma_create_mrs(r_xprt
);
836 rpcrdma_create_req(struct rpcrdma_xprt
*r_xprt
)
838 struct rpcrdma_buffer
*buffer
= &r_xprt
->rx_buf
;
839 struct rpcrdma_req
*req
;
841 req
= kzalloc(sizeof(*req
), GFP_KERNEL
);
843 return ERR_PTR(-ENOMEM
);
845 INIT_LIST_HEAD(&req
->rl_free
);
846 spin_lock(&buffer
->rb_reqslock
);
847 list_add(&req
->rl_all
, &buffer
->rb_allreqs
);
848 spin_unlock(&buffer
->rb_reqslock
);
849 req
->rl_cqe
.done
= rpcrdma_wc_send
;
850 req
->rl_buffer
= &r_xprt
->rx_buf
;
851 INIT_LIST_HEAD(&req
->rl_registered
);
856 rpcrdma_create_rep(struct rpcrdma_xprt
*r_xprt
)
858 struct rpcrdma_create_data_internal
*cdata
= &r_xprt
->rx_data
;
859 struct rpcrdma_ia
*ia
= &r_xprt
->rx_ia
;
860 struct rpcrdma_rep
*rep
;
864 rep
= kzalloc(sizeof(*rep
), GFP_KERNEL
);
868 rep
->rr_rdmabuf
= rpcrdma_alloc_regbuf(ia
, cdata
->inline_rsize
,
870 if (IS_ERR(rep
->rr_rdmabuf
)) {
871 rc
= PTR_ERR(rep
->rr_rdmabuf
);
875 rep
->rr_device
= ia
->ri_device
;
876 rep
->rr_cqe
.done
= rpcrdma_receive_wc
;
877 rep
->rr_rxprt
= r_xprt
;
878 INIT_WORK(&rep
->rr_work
, rpcrdma_receive_worker
);
888 rpcrdma_buffer_create(struct rpcrdma_xprt
*r_xprt
)
890 struct rpcrdma_buffer
*buf
= &r_xprt
->rx_buf
;
893 buf
->rb_max_requests
= r_xprt
->rx_data
.max_requests
;
894 buf
->rb_bc_srv_max_requests
= 0;
895 atomic_set(&buf
->rb_credits
, 1);
896 spin_lock_init(&buf
->rb_mwlock
);
897 spin_lock_init(&buf
->rb_lock
);
898 spin_lock_init(&buf
->rb_recovery_lock
);
899 INIT_LIST_HEAD(&buf
->rb_mws
);
900 INIT_LIST_HEAD(&buf
->rb_all
);
901 INIT_LIST_HEAD(&buf
->rb_stale_mrs
);
902 INIT_DELAYED_WORK(&buf
->rb_refresh_worker
,
903 rpcrdma_mr_refresh_worker
);
904 INIT_DELAYED_WORK(&buf
->rb_recovery_worker
,
905 rpcrdma_mr_recovery_worker
);
907 rpcrdma_create_mrs(r_xprt
);
909 INIT_LIST_HEAD(&buf
->rb_send_bufs
);
910 INIT_LIST_HEAD(&buf
->rb_allreqs
);
911 spin_lock_init(&buf
->rb_reqslock
);
912 for (i
= 0; i
< buf
->rb_max_requests
; i
++) {
913 struct rpcrdma_req
*req
;
915 req
= rpcrdma_create_req(r_xprt
);
917 dprintk("RPC: %s: request buffer %d alloc"
918 " failed\n", __func__
, i
);
922 req
->rl_backchannel
= false;
923 list_add(&req
->rl_free
, &buf
->rb_send_bufs
);
926 INIT_LIST_HEAD(&buf
->rb_recv_bufs
);
927 for (i
= 0; i
< buf
->rb_max_requests
+ RPCRDMA_MAX_BC_REQUESTS
; i
++) {
928 struct rpcrdma_rep
*rep
;
930 rep
= rpcrdma_create_rep(r_xprt
);
932 dprintk("RPC: %s: reply buffer %d alloc failed\n",
937 list_add(&rep
->rr_list
, &buf
->rb_recv_bufs
);
942 rpcrdma_buffer_destroy(buf
);
946 static struct rpcrdma_req
*
947 rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer
*buf
)
949 struct rpcrdma_req
*req
;
951 req
= list_first_entry(&buf
->rb_send_bufs
,
952 struct rpcrdma_req
, rl_free
);
953 list_del(&req
->rl_free
);
957 static struct rpcrdma_rep
*
958 rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer
*buf
)
960 struct rpcrdma_rep
*rep
;
962 rep
= list_first_entry(&buf
->rb_recv_bufs
,
963 struct rpcrdma_rep
, rr_list
);
964 list_del(&rep
->rr_list
);
969 rpcrdma_destroy_rep(struct rpcrdma_ia
*ia
, struct rpcrdma_rep
*rep
)
971 rpcrdma_free_regbuf(ia
, rep
->rr_rdmabuf
);
976 rpcrdma_destroy_req(struct rpcrdma_ia
*ia
, struct rpcrdma_req
*req
)
978 rpcrdma_free_regbuf(ia
, req
->rl_sendbuf
);
979 rpcrdma_free_regbuf(ia
, req
->rl_rdmabuf
);
984 rpcrdma_destroy_mrs(struct rpcrdma_buffer
*buf
)
986 struct rpcrdma_xprt
*r_xprt
= container_of(buf
, struct rpcrdma_xprt
,
988 struct rpcrdma_ia
*ia
= rdmab_to_ia(buf
);
989 struct rpcrdma_mw
*mw
;
993 spin_lock(&buf
->rb_mwlock
);
994 while (!list_empty(&buf
->rb_all
)) {
995 mw
= list_entry(buf
->rb_all
.next
, struct rpcrdma_mw
, mw_all
);
996 list_del(&mw
->mw_all
);
998 spin_unlock(&buf
->rb_mwlock
);
999 ia
->ri_ops
->ro_release_mr(mw
);
1001 spin_lock(&buf
->rb_mwlock
);
1003 spin_unlock(&buf
->rb_mwlock
);
1004 r_xprt
->rx_stats
.mrs_allocated
= 0;
1006 dprintk("RPC: %s: released %u MRs\n", __func__
, count
);
1010 rpcrdma_buffer_destroy(struct rpcrdma_buffer
*buf
)
1012 struct rpcrdma_ia
*ia
= rdmab_to_ia(buf
);
1014 cancel_delayed_work_sync(&buf
->rb_recovery_worker
);
1016 while (!list_empty(&buf
->rb_recv_bufs
)) {
1017 struct rpcrdma_rep
*rep
;
1019 rep
= rpcrdma_buffer_get_rep_locked(buf
);
1020 rpcrdma_destroy_rep(ia
, rep
);
1022 buf
->rb_send_count
= 0;
1024 spin_lock(&buf
->rb_reqslock
);
1025 while (!list_empty(&buf
->rb_allreqs
)) {
1026 struct rpcrdma_req
*req
;
1028 req
= list_first_entry(&buf
->rb_allreqs
,
1029 struct rpcrdma_req
, rl_all
);
1030 list_del(&req
->rl_all
);
1032 spin_unlock(&buf
->rb_reqslock
);
1033 rpcrdma_destroy_req(ia
, req
);
1034 spin_lock(&buf
->rb_reqslock
);
1036 spin_unlock(&buf
->rb_reqslock
);
1037 buf
->rb_recv_count
= 0;
1039 rpcrdma_destroy_mrs(buf
);
1043 rpcrdma_get_mw(struct rpcrdma_xprt
*r_xprt
)
1045 struct rpcrdma_buffer
*buf
= &r_xprt
->rx_buf
;
1046 struct rpcrdma_mw
*mw
= NULL
;
1048 spin_lock(&buf
->rb_mwlock
);
1049 if (!list_empty(&buf
->rb_mws
)) {
1050 mw
= list_first_entry(&buf
->rb_mws
,
1051 struct rpcrdma_mw
, mw_list
);
1052 list_del_init(&mw
->mw_list
);
1054 spin_unlock(&buf
->rb_mwlock
);
1061 dprintk("RPC: %s: no MWs available\n", __func__
);
1062 schedule_delayed_work(&buf
->rb_refresh_worker
, 0);
1064 /* Allow the reply handler and refresh worker to run */
1071 rpcrdma_put_mw(struct rpcrdma_xprt
*r_xprt
, struct rpcrdma_mw
*mw
)
1073 struct rpcrdma_buffer
*buf
= &r_xprt
->rx_buf
;
1075 spin_lock(&buf
->rb_mwlock
);
1076 list_add_tail(&mw
->mw_list
, &buf
->rb_mws
);
1077 spin_unlock(&buf
->rb_mwlock
);
1080 static struct rpcrdma_rep
*
1081 rpcrdma_buffer_get_rep(struct rpcrdma_buffer
*buffers
)
1083 /* If an RPC previously completed without a reply (say, a
1084 * credential problem or a soft timeout occurs) then hold off
1085 * on supplying more Receive buffers until the number of new
1086 * pending RPCs catches up to the number of posted Receives.
1088 if (unlikely(buffers
->rb_send_count
< buffers
->rb_recv_count
))
1091 if (unlikely(list_empty(&buffers
->rb_recv_bufs
)))
1093 buffers
->rb_recv_count
++;
1094 return rpcrdma_buffer_get_rep_locked(buffers
);
1098 * Get a set of request/reply buffers.
1100 * Reply buffer (if available) is attached to send buffer upon return.
1102 struct rpcrdma_req
*
1103 rpcrdma_buffer_get(struct rpcrdma_buffer
*buffers
)
1105 struct rpcrdma_req
*req
;
1107 spin_lock(&buffers
->rb_lock
);
1108 if (list_empty(&buffers
->rb_send_bufs
))
1110 buffers
->rb_send_count
++;
1111 req
= rpcrdma_buffer_get_req_locked(buffers
);
1112 req
->rl_reply
= rpcrdma_buffer_get_rep(buffers
);
1113 spin_unlock(&buffers
->rb_lock
);
1117 spin_unlock(&buffers
->rb_lock
);
1118 pr_warn("RPC: %s: out of request buffers\n", __func__
);
1123 * Put request/reply buffers back into pool.
1124 * Pre-decrement counter/array index.
1127 rpcrdma_buffer_put(struct rpcrdma_req
*req
)
1129 struct rpcrdma_buffer
*buffers
= req
->rl_buffer
;
1130 struct rpcrdma_rep
*rep
= req
->rl_reply
;
1133 req
->rl_reply
= NULL
;
1135 spin_lock(&buffers
->rb_lock
);
1136 buffers
->rb_send_count
--;
1137 list_add_tail(&req
->rl_free
, &buffers
->rb_send_bufs
);
1139 buffers
->rb_recv_count
--;
1140 list_add_tail(&rep
->rr_list
, &buffers
->rb_recv_bufs
);
1142 spin_unlock(&buffers
->rb_lock
);
1146 * Recover reply buffers from pool.
1147 * This happens when recovering from disconnect.
1150 rpcrdma_recv_buffer_get(struct rpcrdma_req
*req
)
1152 struct rpcrdma_buffer
*buffers
= req
->rl_buffer
;
1154 spin_lock(&buffers
->rb_lock
);
1155 req
->rl_reply
= rpcrdma_buffer_get_rep(buffers
);
1156 spin_unlock(&buffers
->rb_lock
);
1160 * Put reply buffers back into pool when not attached to
1161 * request. This happens in error conditions.
1164 rpcrdma_recv_buffer_put(struct rpcrdma_rep
*rep
)
1166 struct rpcrdma_buffer
*buffers
= &rep
->rr_rxprt
->rx_buf
;
1168 spin_lock(&buffers
->rb_lock
);
1169 buffers
->rb_recv_count
--;
1170 list_add_tail(&rep
->rr_list
, &buffers
->rb_recv_bufs
);
1171 spin_unlock(&buffers
->rb_lock
);
1175 * Wrappers for internal-use kmalloc memory registration, used by buffer code.
1179 * rpcrdma_alloc_regbuf - kmalloc and register memory for SEND/RECV buffers
1180 * @ia: controlling rpcrdma_ia
1181 * @size: size of buffer to be allocated, in bytes
1184 * Returns pointer to private header of an area of internally
1185 * registered memory, or an ERR_PTR. The registered buffer follows
1186 * the end of the private header.
1188 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
1189 * receiving the payload of RDMA RECV operations. regbufs are not
1190 * used for RDMA READ/WRITE operations, thus are registered only for
1193 struct rpcrdma_regbuf
*
1194 rpcrdma_alloc_regbuf(struct rpcrdma_ia
*ia
, size_t size
, gfp_t flags
)
1196 struct rpcrdma_regbuf
*rb
;
1199 rb
= kmalloc(sizeof(*rb
) + size
, flags
);
1204 iov
->addr
= ib_dma_map_single(ia
->ri_device
,
1205 (void *)rb
->rg_base
, size
,
1207 if (ib_dma_mapping_error(ia
->ri_device
, iov
->addr
))
1211 iov
->lkey
= ia
->ri_pd
->local_dma_lkey
;
1213 rb
->rg_owner
= NULL
;
1219 return ERR_PTR(-ENOMEM
);
1223 * rpcrdma_free_regbuf - deregister and free registered buffer
1224 * @ia: controlling rpcrdma_ia
1225 * @rb: regbuf to be deregistered and freed
1228 rpcrdma_free_regbuf(struct rpcrdma_ia
*ia
, struct rpcrdma_regbuf
*rb
)
1236 ib_dma_unmap_single(ia
->ri_device
,
1237 iov
->addr
, iov
->length
, DMA_BIDIRECTIONAL
);
1242 * Prepost any receive buffer, then post send.
1244 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1247 rpcrdma_ep_post(struct rpcrdma_ia
*ia
,
1248 struct rpcrdma_ep
*ep
,
1249 struct rpcrdma_req
*req
)
1251 struct ib_device
*device
= ia
->ri_device
;
1252 struct ib_send_wr send_wr
, *send_wr_fail
;
1253 struct rpcrdma_rep
*rep
= req
->rl_reply
;
1254 struct ib_sge
*iov
= req
->rl_send_iov
;
1258 rc
= rpcrdma_ep_post_recv(ia
, ep
, rep
);
1261 req
->rl_reply
= NULL
;
1264 send_wr
.next
= NULL
;
1265 send_wr
.wr_cqe
= &req
->rl_cqe
;
1266 send_wr
.sg_list
= iov
;
1267 send_wr
.num_sge
= req
->rl_niovs
;
1268 send_wr
.opcode
= IB_WR_SEND
;
1270 for (i
= 0; i
< send_wr
.num_sge
; i
++)
1271 ib_dma_sync_single_for_device(device
, iov
[i
].addr
,
1272 iov
[i
].length
, DMA_TO_DEVICE
);
1273 dprintk("RPC: %s: posting %d s/g entries\n",
1274 __func__
, send_wr
.num_sge
);
1276 if (DECR_CQCOUNT(ep
) > 0)
1277 send_wr
.send_flags
= 0;
1278 else { /* Provider must take a send completion every now and then */
1280 send_wr
.send_flags
= IB_SEND_SIGNALED
;
1283 rc
= ib_post_send(ia
->ri_id
->qp
, &send_wr
, &send_wr_fail
);
1285 goto out_postsend_err
;
1289 pr_err("rpcrdma: RDMA Send ib_post_send returned %i\n", rc
);
1294 * (Re)post a receive buffer.
1297 rpcrdma_ep_post_recv(struct rpcrdma_ia
*ia
,
1298 struct rpcrdma_ep
*ep
,
1299 struct rpcrdma_rep
*rep
)
1301 struct ib_recv_wr recv_wr
, *recv_wr_fail
;
1304 recv_wr
.next
= NULL
;
1305 recv_wr
.wr_cqe
= &rep
->rr_cqe
;
1306 recv_wr
.sg_list
= &rep
->rr_rdmabuf
->rg_iov
;
1307 recv_wr
.num_sge
= 1;
1309 ib_dma_sync_single_for_cpu(ia
->ri_device
,
1310 rdmab_addr(rep
->rr_rdmabuf
),
1311 rdmab_length(rep
->rr_rdmabuf
),
1314 rc
= ib_post_recv(ia
->ri_id
->qp
, &recv_wr
, &recv_wr_fail
);
1320 pr_err("rpcrdma: ib_post_recv returned %i\n", rc
);
1325 * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests
1326 * @r_xprt: transport associated with these backchannel resources
1327 * @min_reqs: minimum number of incoming requests expected
1329 * Returns zero if all requested buffers were posted, or a negative errno.
1332 rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt
*r_xprt
, unsigned int count
)
1334 struct rpcrdma_buffer
*buffers
= &r_xprt
->rx_buf
;
1335 struct rpcrdma_ia
*ia
= &r_xprt
->rx_ia
;
1336 struct rpcrdma_ep
*ep
= &r_xprt
->rx_ep
;
1337 struct rpcrdma_rep
*rep
;
1341 spin_lock(&buffers
->rb_lock
);
1342 if (list_empty(&buffers
->rb_recv_bufs
))
1344 rep
= rpcrdma_buffer_get_rep_locked(buffers
);
1345 spin_unlock(&buffers
->rb_lock
);
1347 rc
= rpcrdma_ep_post_recv(ia
, ep
, rep
);
1355 spin_unlock(&buffers
->rb_lock
);
1356 pr_warn("%s: no extra receive buffers\n", __func__
);
1360 rpcrdma_recv_buffer_put(rep
);