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("rpcrdma: %s on device %s ep %p\n",
107 ib_event_msg(event
->event
), 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
);
132 /* Perform basic sanity checking to avoid using garbage
133 * to update the credit grant value.
136 rpcrdma_update_granted_credits(struct rpcrdma_rep
*rep
)
138 struct rpcrdma_msg
*rmsgp
= rdmab_to_msg(rep
->rr_rdmabuf
);
139 struct rpcrdma_buffer
*buffer
= &rep
->rr_rxprt
->rx_buf
;
142 if (rep
->rr_len
< RPCRDMA_HDRLEN_ERR
)
145 credits
= be32_to_cpu(rmsgp
->rm_credit
);
147 credits
= 1; /* don't deadlock */
148 else if (credits
> buffer
->rb_max_requests
)
149 credits
= buffer
->rb_max_requests
;
151 atomic_set(&buffer
->rb_credits
, credits
);
155 * rpcrdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
156 * @cq: completion queue (ignored)
161 rpcrdma_wc_receive(struct ib_cq
*cq
, struct ib_wc
*wc
)
163 struct ib_cqe
*cqe
= wc
->wr_cqe
;
164 struct rpcrdma_rep
*rep
= container_of(cqe
, struct rpcrdma_rep
,
167 /* WARNING: Only wr_id and status are reliable at this point */
168 if (wc
->status
!= IB_WC_SUCCESS
)
171 /* status == SUCCESS means all fields in wc are trustworthy */
172 if (wc
->opcode
!= IB_WC_RECV
)
175 dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
176 __func__
, rep
, wc
->byte_len
);
178 rep
->rr_len
= wc
->byte_len
;
179 rep
->rr_wc_flags
= wc
->wc_flags
;
180 rep
->rr_inv_rkey
= wc
->ex
.invalidate_rkey
;
182 ib_dma_sync_single_for_cpu(rep
->rr_device
,
183 rdmab_addr(rep
->rr_rdmabuf
),
184 rep
->rr_len
, DMA_FROM_DEVICE
);
186 rpcrdma_update_granted_credits(rep
);
189 queue_work(rpcrdma_receive_wq
, &rep
->rr_work
);
193 if (wc
->status
!= IB_WC_WR_FLUSH_ERR
)
194 pr_err("rpcrdma: Recv: %s (%u/0x%x)\n",
195 ib_wc_status_msg(wc
->status
),
196 wc
->status
, wc
->vendor_err
);
197 rep
->rr_len
= RPCRDMA_BAD_LEN
;
202 rpcrdma_update_connect_private(struct rpcrdma_xprt
*r_xprt
,
203 struct rdma_conn_param
*param
)
205 struct rpcrdma_create_data_internal
*cdata
= &r_xprt
->rx_data
;
206 const struct rpcrdma_connect_private
*pmsg
= param
->private_data
;
207 unsigned int rsize
, wsize
;
209 /* Default settings for RPC-over-RDMA Version One */
210 r_xprt
->rx_ia
.ri_reminv_expected
= false;
211 r_xprt
->rx_ia
.ri_implicit_roundup
= xprt_rdma_pad_optimize
;
212 rsize
= RPCRDMA_V1_DEF_INLINE_SIZE
;
213 wsize
= RPCRDMA_V1_DEF_INLINE_SIZE
;
216 pmsg
->cp_magic
== rpcrdma_cmp_magic
&&
217 pmsg
->cp_version
== RPCRDMA_CMP_VERSION
) {
218 r_xprt
->rx_ia
.ri_reminv_expected
= true;
219 r_xprt
->rx_ia
.ri_implicit_roundup
= true;
220 rsize
= rpcrdma_decode_buffer_size(pmsg
->cp_send_size
);
221 wsize
= rpcrdma_decode_buffer_size(pmsg
->cp_recv_size
);
224 if (rsize
< cdata
->inline_rsize
)
225 cdata
->inline_rsize
= rsize
;
226 if (wsize
< cdata
->inline_wsize
)
227 cdata
->inline_wsize
= wsize
;
228 dprintk("RPC: %s: max send %u, max recv %u\n",
229 __func__
, cdata
->inline_wsize
, cdata
->inline_rsize
);
230 rpcrdma_set_max_header_sizes(r_xprt
);
234 rpcrdma_conn_upcall(struct rdma_cm_id
*id
, struct rdma_cm_event
*event
)
236 struct rpcrdma_xprt
*xprt
= id
->context
;
237 struct rpcrdma_ia
*ia
= &xprt
->rx_ia
;
238 struct rpcrdma_ep
*ep
= &xprt
->rx_ep
;
239 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
240 struct sockaddr
*sap
= (struct sockaddr
*)&ep
->rep_remote_addr
;
242 struct ib_qp_attr
*attr
= &ia
->ri_qp_attr
;
243 struct ib_qp_init_attr
*iattr
= &ia
->ri_qp_init_attr
;
246 switch (event
->event
) {
247 case RDMA_CM_EVENT_ADDR_RESOLVED
:
248 case RDMA_CM_EVENT_ROUTE_RESOLVED
:
250 complete(&ia
->ri_done
);
252 case RDMA_CM_EVENT_ADDR_ERROR
:
253 ia
->ri_async_rc
= -EHOSTUNREACH
;
254 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
256 complete(&ia
->ri_done
);
258 case RDMA_CM_EVENT_ROUTE_ERROR
:
259 ia
->ri_async_rc
= -ENETUNREACH
;
260 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
262 complete(&ia
->ri_done
);
264 case RDMA_CM_EVENT_ESTABLISHED
:
266 ib_query_qp(ia
->ri_id
->qp
, attr
,
267 IB_QP_MAX_QP_RD_ATOMIC
| IB_QP_MAX_DEST_RD_ATOMIC
,
269 dprintk("RPC: %s: %d responder resources"
271 __func__
, attr
->max_dest_rd_atomic
,
272 attr
->max_rd_atomic
);
273 rpcrdma_update_connect_private(xprt
, &event
->param
.conn
);
275 case RDMA_CM_EVENT_CONNECT_ERROR
:
276 connstate
= -ENOTCONN
;
278 case RDMA_CM_EVENT_UNREACHABLE
:
279 connstate
= -ENETDOWN
;
281 case RDMA_CM_EVENT_REJECTED
:
282 connstate
= -ECONNREFUSED
;
284 case RDMA_CM_EVENT_DISCONNECTED
:
285 connstate
= -ECONNABORTED
;
287 case RDMA_CM_EVENT_DEVICE_REMOVAL
:
290 dprintk("RPC: %s: %sconnected\n",
291 __func__
, connstate
> 0 ? "" : "dis");
292 atomic_set(&xprt
->rx_buf
.rb_credits
, 1);
293 ep
->rep_connected
= connstate
;
294 rpcrdma_conn_func(ep
);
295 wake_up_all(&ep
->rep_connect_wait
);
298 dprintk("RPC: %s: %pIS:%u (ep 0x%p): %s\n",
299 __func__
, sap
, rpc_get_port(sap
), ep
,
300 rdma_event_msg(event
->event
));
304 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
305 if (connstate
== 1) {
306 int ird
= attr
->max_dest_rd_atomic
;
307 int tird
= ep
->rep_remote_cma
.responder_resources
;
309 pr_info("rpcrdma: connection to %pIS:%u on %s, memreg '%s', %d credits, %d responders%s\n",
310 sap
, rpc_get_port(sap
),
312 ia
->ri_ops
->ro_displayname
,
313 xprt
->rx_buf
.rb_max_requests
,
314 ird
, ird
< 4 && ird
< tird
/ 2 ? " (low!)" : "");
315 } else if (connstate
< 0) {
316 pr_info("rpcrdma: connection to %pIS:%u closed (%d)\n",
317 sap
, rpc_get_port(sap
), connstate
);
324 static void rpcrdma_destroy_id(struct rdma_cm_id
*id
)
327 module_put(id
->device
->owner
);
332 static struct rdma_cm_id
*
333 rpcrdma_create_id(struct rpcrdma_xprt
*xprt
,
334 struct rpcrdma_ia
*ia
, struct sockaddr
*addr
)
336 unsigned long wtimeout
= msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT
) + 1;
337 struct rdma_cm_id
*id
;
340 init_completion(&ia
->ri_done
);
342 id
= rdma_create_id(&init_net
, rpcrdma_conn_upcall
, xprt
, RDMA_PS_TCP
,
346 dprintk("RPC: %s: rdma_create_id() failed %i\n",
351 ia
->ri_async_rc
= -ETIMEDOUT
;
352 rc
= rdma_resolve_addr(id
, NULL
, addr
, RDMA_RESOLVE_TIMEOUT
);
354 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
358 rc
= wait_for_completion_interruptible_timeout(&ia
->ri_done
, wtimeout
);
360 dprintk("RPC: %s: wait() exited: %i\n",
366 * Until xprtrdma supports DEVICE_REMOVAL, the provider must
367 * be pinned while there are active NFS/RDMA mounts to prevent
368 * hangs and crashes at umount time.
370 if (!ia
->ri_async_rc
&& !try_module_get(id
->device
->owner
)) {
371 dprintk("RPC: %s: Failed to get device module\n",
373 ia
->ri_async_rc
= -ENODEV
;
375 rc
= ia
->ri_async_rc
;
379 ia
->ri_async_rc
= -ETIMEDOUT
;
380 rc
= rdma_resolve_route(id
, RDMA_RESOLVE_TIMEOUT
);
382 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
386 rc
= wait_for_completion_interruptible_timeout(&ia
->ri_done
, wtimeout
);
388 dprintk("RPC: %s: wait() exited: %i\n",
392 rc
= ia
->ri_async_rc
;
398 module_put(id
->device
->owner
);
405 * Exported functions.
409 * Open and initialize an Interface Adapter.
410 * o initializes fields of struct rpcrdma_ia, including
411 * interface and provider attributes and protection zone.
414 rpcrdma_ia_open(struct rpcrdma_xprt
*xprt
, struct sockaddr
*addr
, int memreg
)
416 struct rpcrdma_ia
*ia
= &xprt
->rx_ia
;
419 ia
->ri_id
= rpcrdma_create_id(xprt
, ia
, addr
);
420 if (IS_ERR(ia
->ri_id
)) {
421 rc
= PTR_ERR(ia
->ri_id
);
424 ia
->ri_device
= ia
->ri_id
->device
;
426 ia
->ri_pd
= ib_alloc_pd(ia
->ri_device
, 0);
427 if (IS_ERR(ia
->ri_pd
)) {
428 rc
= PTR_ERR(ia
->ri_pd
);
429 pr_err("rpcrdma: ib_alloc_pd() returned %d\n", rc
);
435 if (frwr_is_supported(ia
)) {
436 ia
->ri_ops
= &rpcrdma_frwr_memreg_ops
;
440 case RPCRDMA_MTHCAFMR
:
441 if (fmr_is_supported(ia
)) {
442 ia
->ri_ops
= &rpcrdma_fmr_memreg_ops
;
447 pr_err("rpcrdma: Unsupported memory registration mode: %d\n",
456 ib_dealloc_pd(ia
->ri_pd
);
459 rpcrdma_destroy_id(ia
->ri_id
);
466 * Clean up/close an IA.
467 * o if event handles and PD have been initialized, free them.
471 rpcrdma_ia_close(struct rpcrdma_ia
*ia
)
473 dprintk("RPC: %s: entering\n", __func__
);
474 if (ia
->ri_id
!= NULL
&& !IS_ERR(ia
->ri_id
)) {
476 rdma_destroy_qp(ia
->ri_id
);
477 rpcrdma_destroy_id(ia
->ri_id
);
481 /* If the pd is still busy, xprtrdma missed freeing a resource */
482 if (ia
->ri_pd
&& !IS_ERR(ia
->ri_pd
))
483 ib_dealloc_pd(ia
->ri_pd
);
487 * Create unconnected endpoint.
490 rpcrdma_ep_create(struct rpcrdma_ep
*ep
, struct rpcrdma_ia
*ia
,
491 struct rpcrdma_create_data_internal
*cdata
)
493 struct rpcrdma_connect_private
*pmsg
= &ep
->rep_cm_private
;
494 struct ib_cq
*sendcq
, *recvcq
;
495 unsigned int max_qp_wr
;
498 if (ia
->ri_device
->attrs
.max_sge
< RPCRDMA_MAX_SEND_SGES
) {
499 dprintk("RPC: %s: insufficient sge's available\n",
504 if (ia
->ri_device
->attrs
.max_qp_wr
<= RPCRDMA_BACKWARD_WRS
) {
505 dprintk("RPC: %s: insufficient wqe's available\n",
509 max_qp_wr
= ia
->ri_device
->attrs
.max_qp_wr
- RPCRDMA_BACKWARD_WRS
- 1;
511 /* check provider's send/recv wr limits */
512 if (cdata
->max_requests
> max_qp_wr
)
513 cdata
->max_requests
= max_qp_wr
;
515 ep
->rep_attr
.event_handler
= rpcrdma_qp_async_error_upcall
;
516 ep
->rep_attr
.qp_context
= ep
;
517 ep
->rep_attr
.srq
= NULL
;
518 ep
->rep_attr
.cap
.max_send_wr
= cdata
->max_requests
;
519 ep
->rep_attr
.cap
.max_send_wr
+= RPCRDMA_BACKWARD_WRS
;
520 ep
->rep_attr
.cap
.max_send_wr
+= 1; /* drain cqe */
521 rc
= ia
->ri_ops
->ro_open(ia
, ep
, cdata
);
524 ep
->rep_attr
.cap
.max_recv_wr
= cdata
->max_requests
;
525 ep
->rep_attr
.cap
.max_recv_wr
+= RPCRDMA_BACKWARD_WRS
;
526 ep
->rep_attr
.cap
.max_recv_wr
+= 1; /* drain cqe */
527 ep
->rep_attr
.cap
.max_send_sge
= RPCRDMA_MAX_SEND_SGES
;
528 ep
->rep_attr
.cap
.max_recv_sge
= 1;
529 ep
->rep_attr
.cap
.max_inline_data
= 0;
530 ep
->rep_attr
.sq_sig_type
= IB_SIGNAL_REQ_WR
;
531 ep
->rep_attr
.qp_type
= IB_QPT_RC
;
532 ep
->rep_attr
.port_num
= ~0;
534 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
535 "iovs: send %d recv %d\n",
537 ep
->rep_attr
.cap
.max_send_wr
,
538 ep
->rep_attr
.cap
.max_recv_wr
,
539 ep
->rep_attr
.cap
.max_send_sge
,
540 ep
->rep_attr
.cap
.max_recv_sge
);
542 /* set trigger for requesting send completion */
543 ep
->rep_cqinit
= ep
->rep_attr
.cap
.max_send_wr
/2 - 1;
544 if (ep
->rep_cqinit
<= 2)
545 ep
->rep_cqinit
= 0; /* always signal? */
546 rpcrdma_init_cqcount(ep
, 0);
547 init_waitqueue_head(&ep
->rep_connect_wait
);
548 INIT_DELAYED_WORK(&ep
->rep_connect_worker
, rpcrdma_connect_worker
);
550 sendcq
= ib_alloc_cq(ia
->ri_device
, NULL
,
551 ep
->rep_attr
.cap
.max_send_wr
+ 1,
553 if (IS_ERR(sendcq
)) {
554 rc
= PTR_ERR(sendcq
);
555 dprintk("RPC: %s: failed to create send CQ: %i\n",
560 recvcq
= ib_alloc_cq(ia
->ri_device
, NULL
,
561 ep
->rep_attr
.cap
.max_recv_wr
+ 1,
563 if (IS_ERR(recvcq
)) {
564 rc
= PTR_ERR(recvcq
);
565 dprintk("RPC: %s: failed to create recv CQ: %i\n",
570 ep
->rep_attr
.send_cq
= sendcq
;
571 ep
->rep_attr
.recv_cq
= recvcq
;
573 /* Initialize cma parameters */
574 memset(&ep
->rep_remote_cma
, 0, sizeof(ep
->rep_remote_cma
));
576 /* Prepare RDMA-CM private message */
577 pmsg
->cp_magic
= rpcrdma_cmp_magic
;
578 pmsg
->cp_version
= RPCRDMA_CMP_VERSION
;
579 pmsg
->cp_flags
|= ia
->ri_ops
->ro_send_w_inv_ok
;
580 pmsg
->cp_send_size
= rpcrdma_encode_buffer_size(cdata
->inline_wsize
);
581 pmsg
->cp_recv_size
= rpcrdma_encode_buffer_size(cdata
->inline_rsize
);
582 ep
->rep_remote_cma
.private_data
= pmsg
;
583 ep
->rep_remote_cma
.private_data_len
= sizeof(*pmsg
);
585 /* Client offers RDMA Read but does not initiate */
586 ep
->rep_remote_cma
.initiator_depth
= 0;
587 if (ia
->ri_device
->attrs
.max_qp_rd_atom
> 32) /* arbitrary but <= 255 */
588 ep
->rep_remote_cma
.responder_resources
= 32;
590 ep
->rep_remote_cma
.responder_resources
=
591 ia
->ri_device
->attrs
.max_qp_rd_atom
;
593 /* Limit transport retries so client can detect server
594 * GID changes quickly. RPC layer handles re-establishing
595 * transport connection and retransmission.
597 ep
->rep_remote_cma
.retry_count
= 6;
599 /* RPC-over-RDMA handles its own flow control. In addition,
600 * make all RNR NAKs visible so we know that RPC-over-RDMA
601 * flow control is working correctly (no NAKs should be seen).
603 ep
->rep_remote_cma
.flow_control
= 0;
604 ep
->rep_remote_cma
.rnr_retry_count
= 0;
617 * Disconnect and destroy endpoint. After this, the only
618 * valid operations on the ep are to free it (if dynamically
619 * allocated) or re-create it.
622 rpcrdma_ep_destroy(struct rpcrdma_ep
*ep
, struct rpcrdma_ia
*ia
)
624 dprintk("RPC: %s: entering, connected is %d\n",
625 __func__
, ep
->rep_connected
);
627 cancel_delayed_work_sync(&ep
->rep_connect_worker
);
630 rpcrdma_ep_disconnect(ep
, ia
);
631 rdma_destroy_qp(ia
->ri_id
);
632 ia
->ri_id
->qp
= NULL
;
635 ib_free_cq(ep
->rep_attr
.recv_cq
);
636 ib_free_cq(ep
->rep_attr
.send_cq
);
640 * Connect unconnected endpoint.
643 rpcrdma_ep_connect(struct rpcrdma_ep
*ep
, struct rpcrdma_ia
*ia
)
645 struct rdma_cm_id
*id
, *old
;
649 if (ep
->rep_connected
!= 0) {
650 struct rpcrdma_xprt
*xprt
;
652 dprintk("RPC: %s: reconnecting...\n", __func__
);
654 rpcrdma_ep_disconnect(ep
, ia
);
656 xprt
= container_of(ia
, struct rpcrdma_xprt
, rx_ia
);
657 id
= rpcrdma_create_id(xprt
, ia
,
658 (struct sockaddr
*)&xprt
->rx_data
.addr
);
663 /* TEMP TEMP TEMP - fail if new device:
664 * Deregister/remarshal *all* requests!
665 * Close and recreate adapter, pd, etc!
666 * Re-determine all attributes still sane!
667 * More stuff I haven't thought of!
670 if (ia
->ri_device
!= id
->device
) {
671 printk("RPC: %s: can't reconnect on "
672 "different device!\n", __func__
);
673 rpcrdma_destroy_id(id
);
678 rc
= rdma_create_qp(id
, ia
->ri_pd
, &ep
->rep_attr
);
680 dprintk("RPC: %s: rdma_create_qp failed %i\n",
682 rpcrdma_destroy_id(id
);
690 rdma_destroy_qp(old
);
691 rpcrdma_destroy_id(old
);
693 dprintk("RPC: %s: connecting...\n", __func__
);
694 rc
= rdma_create_qp(ia
->ri_id
, ia
->ri_pd
, &ep
->rep_attr
);
696 dprintk("RPC: %s: rdma_create_qp failed %i\n",
698 /* do not update ep->rep_connected */
703 ep
->rep_connected
= 0;
705 rc
= rdma_connect(ia
->ri_id
, &ep
->rep_remote_cma
);
707 dprintk("RPC: %s: rdma_connect() failed with %i\n",
712 wait_event_interruptible(ep
->rep_connect_wait
, ep
->rep_connected
!= 0);
715 * Check state. A non-peer reject indicates no listener
716 * (ECONNREFUSED), which may be a transient state. All
717 * others indicate a transport condition which has already
718 * undergone a best-effort.
720 if (ep
->rep_connected
== -ECONNREFUSED
&&
721 ++retry_count
<= RDMA_CONNECT_RETRY_MAX
) {
722 dprintk("RPC: %s: non-peer_reject, retry\n", __func__
);
725 if (ep
->rep_connected
<= 0) {
726 /* Sometimes, the only way to reliably connect to remote
727 * CMs is to use same nonzero values for ORD and IRD. */
728 if (retry_count
++ <= RDMA_CONNECT_RETRY_MAX
+ 1 &&
729 (ep
->rep_remote_cma
.responder_resources
== 0 ||
730 ep
->rep_remote_cma
.initiator_depth
!=
731 ep
->rep_remote_cma
.responder_resources
)) {
732 if (ep
->rep_remote_cma
.responder_resources
== 0)
733 ep
->rep_remote_cma
.responder_resources
= 1;
734 ep
->rep_remote_cma
.initiator_depth
=
735 ep
->rep_remote_cma
.responder_resources
;
738 rc
= ep
->rep_connected
;
740 struct rpcrdma_xprt
*r_xprt
;
743 dprintk("RPC: %s: connected\n", __func__
);
745 r_xprt
= container_of(ia
, struct rpcrdma_xprt
, rx_ia
);
746 extras
= r_xprt
->rx_buf
.rb_bc_srv_max_requests
;
749 rc
= rpcrdma_ep_post_extra_recv(r_xprt
, extras
);
751 pr_warn("%s: rpcrdma_ep_post_extra_recv: %i\n",
760 ep
->rep_connected
= rc
;
765 * rpcrdma_ep_disconnect
767 * This is separate from destroy to facilitate the ability
768 * to reconnect without recreating the endpoint.
770 * This call is not reentrant, and must not be made in parallel
771 * on the same endpoint.
774 rpcrdma_ep_disconnect(struct rpcrdma_ep
*ep
, struct rpcrdma_ia
*ia
)
778 rc
= rdma_disconnect(ia
->ri_id
);
780 /* returns without wait if not connected */
781 wait_event_interruptible(ep
->rep_connect_wait
,
782 ep
->rep_connected
!= 1);
783 dprintk("RPC: %s: after wait, %sconnected\n", __func__
,
784 (ep
->rep_connected
== 1) ? "still " : "dis");
786 dprintk("RPC: %s: rdma_disconnect %i\n", __func__
, rc
);
787 ep
->rep_connected
= rc
;
790 ib_drain_qp(ia
->ri_id
->qp
);
794 rpcrdma_mr_recovery_worker(struct work_struct
*work
)
796 struct rpcrdma_buffer
*buf
= container_of(work
, struct rpcrdma_buffer
,
797 rb_recovery_worker
.work
);
798 struct rpcrdma_mw
*mw
;
800 spin_lock(&buf
->rb_recovery_lock
);
801 while (!list_empty(&buf
->rb_stale_mrs
)) {
802 mw
= list_first_entry(&buf
->rb_stale_mrs
,
803 struct rpcrdma_mw
, mw_list
);
804 list_del_init(&mw
->mw_list
);
805 spin_unlock(&buf
->rb_recovery_lock
);
807 dprintk("RPC: %s: recovering MR %p\n", __func__
, mw
);
808 mw
->mw_xprt
->rx_ia
.ri_ops
->ro_recover_mr(mw
);
810 spin_lock(&buf
->rb_recovery_lock
);
812 spin_unlock(&buf
->rb_recovery_lock
);
816 rpcrdma_defer_mr_recovery(struct rpcrdma_mw
*mw
)
818 struct rpcrdma_xprt
*r_xprt
= mw
->mw_xprt
;
819 struct rpcrdma_buffer
*buf
= &r_xprt
->rx_buf
;
821 spin_lock(&buf
->rb_recovery_lock
);
822 list_add(&mw
->mw_list
, &buf
->rb_stale_mrs
);
823 spin_unlock(&buf
->rb_recovery_lock
);
825 schedule_delayed_work(&buf
->rb_recovery_worker
, 0);
829 rpcrdma_create_mrs(struct rpcrdma_xprt
*r_xprt
)
831 struct rpcrdma_buffer
*buf
= &r_xprt
->rx_buf
;
832 struct rpcrdma_ia
*ia
= &r_xprt
->rx_ia
;
837 for (count
= 0; count
< 32; count
++) {
838 struct rpcrdma_mw
*mw
;
841 mw
= kzalloc(sizeof(*mw
), GFP_KERNEL
);
845 rc
= ia
->ri_ops
->ro_init_mr(ia
, mw
);
851 mw
->mw_xprt
= r_xprt
;
853 list_add(&mw
->mw_list
, &free
);
854 list_add(&mw
->mw_all
, &all
);
857 spin_lock(&buf
->rb_mwlock
);
858 list_splice(&free
, &buf
->rb_mws
);
859 list_splice(&all
, &buf
->rb_all
);
860 r_xprt
->rx_stats
.mrs_allocated
+= count
;
861 spin_unlock(&buf
->rb_mwlock
);
863 dprintk("RPC: %s: created %u MRs\n", __func__
, count
);
867 rpcrdma_mr_refresh_worker(struct work_struct
*work
)
869 struct rpcrdma_buffer
*buf
= container_of(work
, struct rpcrdma_buffer
,
870 rb_refresh_worker
.work
);
871 struct rpcrdma_xprt
*r_xprt
= container_of(buf
, struct rpcrdma_xprt
,
874 rpcrdma_create_mrs(r_xprt
);
878 rpcrdma_create_req(struct rpcrdma_xprt
*r_xprt
)
880 struct rpcrdma_buffer
*buffer
= &r_xprt
->rx_buf
;
881 struct rpcrdma_req
*req
;
883 req
= kzalloc(sizeof(*req
), GFP_KERNEL
);
885 return ERR_PTR(-ENOMEM
);
887 INIT_LIST_HEAD(&req
->rl_free
);
888 spin_lock(&buffer
->rb_reqslock
);
889 list_add(&req
->rl_all
, &buffer
->rb_allreqs
);
890 spin_unlock(&buffer
->rb_reqslock
);
891 req
->rl_cqe
.done
= rpcrdma_wc_send
;
892 req
->rl_buffer
= &r_xprt
->rx_buf
;
893 INIT_LIST_HEAD(&req
->rl_registered
);
894 req
->rl_send_wr
.next
= NULL
;
895 req
->rl_send_wr
.wr_cqe
= &req
->rl_cqe
;
896 req
->rl_send_wr
.sg_list
= req
->rl_send_sge
;
897 req
->rl_send_wr
.opcode
= IB_WR_SEND
;
902 rpcrdma_create_rep(struct rpcrdma_xprt
*r_xprt
)
904 struct rpcrdma_create_data_internal
*cdata
= &r_xprt
->rx_data
;
905 struct rpcrdma_ia
*ia
= &r_xprt
->rx_ia
;
906 struct rpcrdma_rep
*rep
;
910 rep
= kzalloc(sizeof(*rep
), GFP_KERNEL
);
914 rep
->rr_rdmabuf
= rpcrdma_alloc_regbuf(cdata
->inline_rsize
,
915 DMA_FROM_DEVICE
, GFP_KERNEL
);
916 if (IS_ERR(rep
->rr_rdmabuf
)) {
917 rc
= PTR_ERR(rep
->rr_rdmabuf
);
921 rep
->rr_device
= ia
->ri_device
;
922 rep
->rr_cqe
.done
= rpcrdma_wc_receive
;
923 rep
->rr_rxprt
= r_xprt
;
924 INIT_WORK(&rep
->rr_work
, rpcrdma_reply_handler
);
925 rep
->rr_recv_wr
.next
= NULL
;
926 rep
->rr_recv_wr
.wr_cqe
= &rep
->rr_cqe
;
927 rep
->rr_recv_wr
.sg_list
= &rep
->rr_rdmabuf
->rg_iov
;
928 rep
->rr_recv_wr
.num_sge
= 1;
938 rpcrdma_buffer_create(struct rpcrdma_xprt
*r_xprt
)
940 struct rpcrdma_buffer
*buf
= &r_xprt
->rx_buf
;
943 buf
->rb_max_requests
= r_xprt
->rx_data
.max_requests
;
944 buf
->rb_bc_srv_max_requests
= 0;
945 atomic_set(&buf
->rb_credits
, 1);
946 spin_lock_init(&buf
->rb_mwlock
);
947 spin_lock_init(&buf
->rb_lock
);
948 spin_lock_init(&buf
->rb_recovery_lock
);
949 INIT_LIST_HEAD(&buf
->rb_mws
);
950 INIT_LIST_HEAD(&buf
->rb_all
);
951 INIT_LIST_HEAD(&buf
->rb_stale_mrs
);
952 INIT_DELAYED_WORK(&buf
->rb_refresh_worker
,
953 rpcrdma_mr_refresh_worker
);
954 INIT_DELAYED_WORK(&buf
->rb_recovery_worker
,
955 rpcrdma_mr_recovery_worker
);
957 rpcrdma_create_mrs(r_xprt
);
959 INIT_LIST_HEAD(&buf
->rb_send_bufs
);
960 INIT_LIST_HEAD(&buf
->rb_allreqs
);
961 spin_lock_init(&buf
->rb_reqslock
);
962 for (i
= 0; i
< buf
->rb_max_requests
; i
++) {
963 struct rpcrdma_req
*req
;
965 req
= rpcrdma_create_req(r_xprt
);
967 dprintk("RPC: %s: request buffer %d alloc"
968 " failed\n", __func__
, i
);
972 req
->rl_backchannel
= false;
973 list_add(&req
->rl_free
, &buf
->rb_send_bufs
);
976 INIT_LIST_HEAD(&buf
->rb_recv_bufs
);
977 for (i
= 0; i
< buf
->rb_max_requests
+ RPCRDMA_MAX_BC_REQUESTS
; i
++) {
978 struct rpcrdma_rep
*rep
;
980 rep
= rpcrdma_create_rep(r_xprt
);
982 dprintk("RPC: %s: reply buffer %d alloc failed\n",
987 list_add(&rep
->rr_list
, &buf
->rb_recv_bufs
);
992 rpcrdma_buffer_destroy(buf
);
996 static struct rpcrdma_req
*
997 rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer
*buf
)
999 struct rpcrdma_req
*req
;
1001 req
= list_first_entry(&buf
->rb_send_bufs
,
1002 struct rpcrdma_req
, rl_free
);
1003 list_del(&req
->rl_free
);
1007 static struct rpcrdma_rep
*
1008 rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer
*buf
)
1010 struct rpcrdma_rep
*rep
;
1012 rep
= list_first_entry(&buf
->rb_recv_bufs
,
1013 struct rpcrdma_rep
, rr_list
);
1014 list_del(&rep
->rr_list
);
1019 rpcrdma_destroy_rep(struct rpcrdma_rep
*rep
)
1021 rpcrdma_free_regbuf(rep
->rr_rdmabuf
);
1026 rpcrdma_destroy_req(struct rpcrdma_req
*req
)
1028 rpcrdma_free_regbuf(req
->rl_recvbuf
);
1029 rpcrdma_free_regbuf(req
->rl_sendbuf
);
1030 rpcrdma_free_regbuf(req
->rl_rdmabuf
);
1035 rpcrdma_destroy_mrs(struct rpcrdma_buffer
*buf
)
1037 struct rpcrdma_xprt
*r_xprt
= container_of(buf
, struct rpcrdma_xprt
,
1039 struct rpcrdma_ia
*ia
= rdmab_to_ia(buf
);
1040 struct rpcrdma_mw
*mw
;
1044 spin_lock(&buf
->rb_mwlock
);
1045 while (!list_empty(&buf
->rb_all
)) {
1046 mw
= list_entry(buf
->rb_all
.next
, struct rpcrdma_mw
, mw_all
);
1047 list_del(&mw
->mw_all
);
1049 spin_unlock(&buf
->rb_mwlock
);
1050 ia
->ri_ops
->ro_release_mr(mw
);
1052 spin_lock(&buf
->rb_mwlock
);
1054 spin_unlock(&buf
->rb_mwlock
);
1055 r_xprt
->rx_stats
.mrs_allocated
= 0;
1057 dprintk("RPC: %s: released %u MRs\n", __func__
, count
);
1061 rpcrdma_buffer_destroy(struct rpcrdma_buffer
*buf
)
1063 cancel_delayed_work_sync(&buf
->rb_recovery_worker
);
1065 while (!list_empty(&buf
->rb_recv_bufs
)) {
1066 struct rpcrdma_rep
*rep
;
1068 rep
= rpcrdma_buffer_get_rep_locked(buf
);
1069 rpcrdma_destroy_rep(rep
);
1071 buf
->rb_send_count
= 0;
1073 spin_lock(&buf
->rb_reqslock
);
1074 while (!list_empty(&buf
->rb_allreqs
)) {
1075 struct rpcrdma_req
*req
;
1077 req
= list_first_entry(&buf
->rb_allreqs
,
1078 struct rpcrdma_req
, rl_all
);
1079 list_del(&req
->rl_all
);
1081 spin_unlock(&buf
->rb_reqslock
);
1082 rpcrdma_destroy_req(req
);
1083 spin_lock(&buf
->rb_reqslock
);
1085 spin_unlock(&buf
->rb_reqslock
);
1086 buf
->rb_recv_count
= 0;
1088 rpcrdma_destroy_mrs(buf
);
1092 rpcrdma_get_mw(struct rpcrdma_xprt
*r_xprt
)
1094 struct rpcrdma_buffer
*buf
= &r_xprt
->rx_buf
;
1095 struct rpcrdma_mw
*mw
= NULL
;
1097 spin_lock(&buf
->rb_mwlock
);
1098 if (!list_empty(&buf
->rb_mws
)) {
1099 mw
= list_first_entry(&buf
->rb_mws
,
1100 struct rpcrdma_mw
, mw_list
);
1101 list_del_init(&mw
->mw_list
);
1103 spin_unlock(&buf
->rb_mwlock
);
1110 dprintk("RPC: %s: no MWs available\n", __func__
);
1111 schedule_delayed_work(&buf
->rb_refresh_worker
, 0);
1113 /* Allow the reply handler and refresh worker to run */
1120 rpcrdma_put_mw(struct rpcrdma_xprt
*r_xprt
, struct rpcrdma_mw
*mw
)
1122 struct rpcrdma_buffer
*buf
= &r_xprt
->rx_buf
;
1124 spin_lock(&buf
->rb_mwlock
);
1125 list_add_tail(&mw
->mw_list
, &buf
->rb_mws
);
1126 spin_unlock(&buf
->rb_mwlock
);
1129 static struct rpcrdma_rep
*
1130 rpcrdma_buffer_get_rep(struct rpcrdma_buffer
*buffers
)
1132 /* If an RPC previously completed without a reply (say, a
1133 * credential problem or a soft timeout occurs) then hold off
1134 * on supplying more Receive buffers until the number of new
1135 * pending RPCs catches up to the number of posted Receives.
1137 if (unlikely(buffers
->rb_send_count
< buffers
->rb_recv_count
))
1140 if (unlikely(list_empty(&buffers
->rb_recv_bufs
)))
1142 buffers
->rb_recv_count
++;
1143 return rpcrdma_buffer_get_rep_locked(buffers
);
1147 * Get a set of request/reply buffers.
1149 * Reply buffer (if available) is attached to send buffer upon return.
1151 struct rpcrdma_req
*
1152 rpcrdma_buffer_get(struct rpcrdma_buffer
*buffers
)
1154 struct rpcrdma_req
*req
;
1156 spin_lock(&buffers
->rb_lock
);
1157 if (list_empty(&buffers
->rb_send_bufs
))
1159 buffers
->rb_send_count
++;
1160 req
= rpcrdma_buffer_get_req_locked(buffers
);
1161 req
->rl_reply
= rpcrdma_buffer_get_rep(buffers
);
1162 spin_unlock(&buffers
->rb_lock
);
1166 spin_unlock(&buffers
->rb_lock
);
1167 pr_warn("RPC: %s: out of request buffers\n", __func__
);
1172 * Put request/reply buffers back into pool.
1173 * Pre-decrement counter/array index.
1176 rpcrdma_buffer_put(struct rpcrdma_req
*req
)
1178 struct rpcrdma_buffer
*buffers
= req
->rl_buffer
;
1179 struct rpcrdma_rep
*rep
= req
->rl_reply
;
1181 req
->rl_send_wr
.num_sge
= 0;
1182 req
->rl_reply
= NULL
;
1184 spin_lock(&buffers
->rb_lock
);
1185 buffers
->rb_send_count
--;
1186 list_add_tail(&req
->rl_free
, &buffers
->rb_send_bufs
);
1188 buffers
->rb_recv_count
--;
1189 list_add_tail(&rep
->rr_list
, &buffers
->rb_recv_bufs
);
1191 spin_unlock(&buffers
->rb_lock
);
1195 * Recover reply buffers from pool.
1196 * This happens when recovering from disconnect.
1199 rpcrdma_recv_buffer_get(struct rpcrdma_req
*req
)
1201 struct rpcrdma_buffer
*buffers
= req
->rl_buffer
;
1203 spin_lock(&buffers
->rb_lock
);
1204 req
->rl_reply
= rpcrdma_buffer_get_rep(buffers
);
1205 spin_unlock(&buffers
->rb_lock
);
1209 * Put reply buffers back into pool when not attached to
1210 * request. This happens in error conditions.
1213 rpcrdma_recv_buffer_put(struct rpcrdma_rep
*rep
)
1215 struct rpcrdma_buffer
*buffers
= &rep
->rr_rxprt
->rx_buf
;
1217 spin_lock(&buffers
->rb_lock
);
1218 buffers
->rb_recv_count
--;
1219 list_add_tail(&rep
->rr_list
, &buffers
->rb_recv_bufs
);
1220 spin_unlock(&buffers
->rb_lock
);
1224 * rpcrdma_alloc_regbuf - allocate and DMA-map memory for SEND/RECV buffers
1225 * @size: size of buffer to be allocated, in bytes
1226 * @direction: direction of data movement
1229 * Returns an ERR_PTR, or a pointer to a regbuf, a buffer that
1230 * can be persistently DMA-mapped for I/O.
1232 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
1233 * receiving the payload of RDMA RECV operations. During Long Calls
1234 * or Replies they may be registered externally via ro_map.
1236 struct rpcrdma_regbuf
*
1237 rpcrdma_alloc_regbuf(size_t size
, enum dma_data_direction direction
,
1240 struct rpcrdma_regbuf
*rb
;
1242 rb
= kmalloc(sizeof(*rb
) + size
, flags
);
1244 return ERR_PTR(-ENOMEM
);
1246 rb
->rg_device
= NULL
;
1247 rb
->rg_direction
= direction
;
1248 rb
->rg_iov
.length
= size
;
1254 * __rpcrdma_map_regbuf - DMA-map a regbuf
1255 * @ia: controlling rpcrdma_ia
1256 * @rb: regbuf to be mapped
1259 __rpcrdma_dma_map_regbuf(struct rpcrdma_ia
*ia
, struct rpcrdma_regbuf
*rb
)
1261 if (rb
->rg_direction
== DMA_NONE
)
1264 rb
->rg_iov
.addr
= ib_dma_map_single(ia
->ri_device
,
1265 (void *)rb
->rg_base
,
1268 if (ib_dma_mapping_error(ia
->ri_device
, rdmab_addr(rb
)))
1271 rb
->rg_device
= ia
->ri_device
;
1272 rb
->rg_iov
.lkey
= ia
->ri_pd
->local_dma_lkey
;
1277 rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf
*rb
)
1279 if (!rpcrdma_regbuf_is_mapped(rb
))
1282 ib_dma_unmap_single(rb
->rg_device
, rdmab_addr(rb
),
1283 rdmab_length(rb
), rb
->rg_direction
);
1284 rb
->rg_device
= NULL
;
1288 * rpcrdma_free_regbuf - deregister and free registered buffer
1289 * @rb: regbuf to be deregistered and freed
1292 rpcrdma_free_regbuf(struct rpcrdma_regbuf
*rb
)
1297 rpcrdma_dma_unmap_regbuf(rb
);
1302 * Prepost any receive buffer, then post send.
1304 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1307 rpcrdma_ep_post(struct rpcrdma_ia
*ia
,
1308 struct rpcrdma_ep
*ep
,
1309 struct rpcrdma_req
*req
)
1311 struct ib_send_wr
*send_wr
= &req
->rl_send_wr
;
1312 struct ib_send_wr
*send_wr_fail
;
1315 if (req
->rl_reply
) {
1316 rc
= rpcrdma_ep_post_recv(ia
, req
->rl_reply
);
1319 req
->rl_reply
= NULL
;
1322 dprintk("RPC: %s: posting %d s/g entries\n",
1323 __func__
, send_wr
->num_sge
);
1325 rpcrdma_set_signaled(ep
, send_wr
);
1326 rc
= ib_post_send(ia
->ri_id
->qp
, send_wr
, &send_wr_fail
);
1328 goto out_postsend_err
;
1332 pr_err("rpcrdma: RDMA Send ib_post_send returned %i\n", rc
);
1337 rpcrdma_ep_post_recv(struct rpcrdma_ia
*ia
,
1338 struct rpcrdma_rep
*rep
)
1340 struct ib_recv_wr
*recv_wr_fail
;
1343 if (!rpcrdma_dma_map_regbuf(ia
, rep
->rr_rdmabuf
))
1345 rc
= ib_post_recv(ia
->ri_id
->qp
, &rep
->rr_recv_wr
, &recv_wr_fail
);
1351 pr_err("rpcrdma: failed to DMA map the Receive buffer\n");
1355 pr_err("rpcrdma: ib_post_recv returned %i\n", rc
);
1360 * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests
1361 * @r_xprt: transport associated with these backchannel resources
1362 * @min_reqs: minimum number of incoming requests expected
1364 * Returns zero if all requested buffers were posted, or a negative errno.
1367 rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt
*r_xprt
, unsigned int count
)
1369 struct rpcrdma_buffer
*buffers
= &r_xprt
->rx_buf
;
1370 struct rpcrdma_ia
*ia
= &r_xprt
->rx_ia
;
1371 struct rpcrdma_rep
*rep
;
1375 spin_lock(&buffers
->rb_lock
);
1376 if (list_empty(&buffers
->rb_recv_bufs
))
1378 rep
= rpcrdma_buffer_get_rep_locked(buffers
);
1379 spin_unlock(&buffers
->rb_lock
);
1381 rc
= rpcrdma_ep_post_recv(ia
, rep
);
1389 spin_unlock(&buffers
->rb_lock
);
1390 pr_warn("%s: no extra receive buffers\n", __func__
);
1394 rpcrdma_recv_buffer_put(rep
);