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>
57 #include <rdma/ib_cm.h>
59 #include "xprt_rdma.h"
65 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
66 # define RPCDBG_FACILITY RPCDBG_TRANS
72 static void rpcrdma_create_mrs(struct rpcrdma_xprt *r_xprt);
73 static void rpcrdma_destroy_mrs(struct rpcrdma_buffer *buf);
74 static void rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb);
76 static struct workqueue_struct *rpcrdma_receive_wq __read_mostly;
79 rpcrdma_alloc_wq(void)
81 struct workqueue_struct *recv_wq;
83 recv_wq = alloc_workqueue("xprtrdma_receive",
84 WQ_MEM_RECLAIM | WQ_UNBOUND | WQ_HIGHPRI,
89 rpcrdma_receive_wq = recv_wq;
94 rpcrdma_destroy_wq(void)
96 struct workqueue_struct *wq;
98 if (rpcrdma_receive_wq) {
99 wq = rpcrdma_receive_wq;
100 rpcrdma_receive_wq = NULL;
101 destroy_workqueue(wq);
106 rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context)
108 struct rpcrdma_ep *ep = context;
110 pr_err("rpcrdma: %s on device %s ep %p\n",
111 ib_event_msg(event->event), event->device->name, context);
113 if (ep->rep_connected == 1) {
114 ep->rep_connected = -EIO;
115 rpcrdma_conn_func(ep);
116 wake_up_all(&ep->rep_connect_wait);
121 * rpcrdma_wc_send - Invoked by RDMA provider for each polled Send WC
122 * @cq: completion queue (ignored)
127 rpcrdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
129 /* WARNING: Only wr_cqe and status are reliable at this point */
130 if (wc->status != IB_WC_SUCCESS && wc->status != IB_WC_WR_FLUSH_ERR)
131 pr_err("rpcrdma: Send: %s (%u/0x%x)\n",
132 ib_wc_status_msg(wc->status),
133 wc->status, wc->vendor_err);
136 /* Perform basic sanity checking to avoid using garbage
137 * to update the credit grant value.
140 rpcrdma_update_granted_credits(struct rpcrdma_rep *rep)
142 struct rpcrdma_msg *rmsgp = rdmab_to_msg(rep->rr_rdmabuf);
143 struct rpcrdma_buffer *buffer = &rep->rr_rxprt->rx_buf;
146 if (rep->rr_len < RPCRDMA_HDRLEN_ERR)
149 credits = be32_to_cpu(rmsgp->rm_credit);
151 credits = 1; /* don't deadlock */
152 else if (credits > buffer->rb_max_requests)
153 credits = buffer->rb_max_requests;
155 atomic_set(&buffer->rb_credits, credits);
159 * rpcrdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
160 * @cq: completion queue (ignored)
165 rpcrdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
167 struct ib_cqe *cqe = wc->wr_cqe;
168 struct rpcrdma_rep *rep = container_of(cqe, struct rpcrdma_rep,
171 /* WARNING: Only wr_id and status are reliable at this point */
172 if (wc->status != IB_WC_SUCCESS)
175 /* status == SUCCESS means all fields in wc are trustworthy */
176 dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
177 __func__, rep, wc->byte_len);
179 rep->rr_len = wc->byte_len;
180 rpcrdma_set_xdrlen(&rep->rr_hdrbuf, wc->byte_len);
181 rep->rr_wc_flags = wc->wc_flags;
182 rep->rr_inv_rkey = wc->ex.invalidate_rkey;
184 ib_dma_sync_single_for_cpu(rdmab_device(rep->rr_rdmabuf),
185 rdmab_addr(rep->rr_rdmabuf),
186 rep->rr_len, DMA_FROM_DEVICE);
188 rpcrdma_update_granted_credits(rep);
191 queue_work(rpcrdma_receive_wq, &rep->rr_work);
195 if (wc->status != IB_WC_WR_FLUSH_ERR)
196 pr_err("rpcrdma: Recv: %s (%u/0x%x)\n",
197 ib_wc_status_msg(wc->status),
198 wc->status, wc->vendor_err);
199 rep->rr_len = RPCRDMA_BAD_LEN;
204 rpcrdma_update_connect_private(struct rpcrdma_xprt *r_xprt,
205 struct rdma_conn_param *param)
207 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
208 const struct rpcrdma_connect_private *pmsg = param->private_data;
209 unsigned int rsize, wsize;
211 /* Default settings for RPC-over-RDMA Version One */
212 r_xprt->rx_ia.ri_reminv_expected = false;
213 r_xprt->rx_ia.ri_implicit_roundup = xprt_rdma_pad_optimize;
214 rsize = RPCRDMA_V1_DEF_INLINE_SIZE;
215 wsize = RPCRDMA_V1_DEF_INLINE_SIZE;
218 pmsg->cp_magic == rpcrdma_cmp_magic &&
219 pmsg->cp_version == RPCRDMA_CMP_VERSION) {
220 r_xprt->rx_ia.ri_reminv_expected = true;
221 r_xprt->rx_ia.ri_implicit_roundup = true;
222 rsize = rpcrdma_decode_buffer_size(pmsg->cp_send_size);
223 wsize = rpcrdma_decode_buffer_size(pmsg->cp_recv_size);
226 if (rsize < cdata->inline_rsize)
227 cdata->inline_rsize = rsize;
228 if (wsize < cdata->inline_wsize)
229 cdata->inline_wsize = wsize;
230 dprintk("RPC: %s: max send %u, max recv %u\n",
231 __func__, cdata->inline_wsize, cdata->inline_rsize);
232 rpcrdma_set_max_header_sizes(r_xprt);
236 rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
238 struct rpcrdma_xprt *xprt = id->context;
239 struct rpcrdma_ia *ia = &xprt->rx_ia;
240 struct rpcrdma_ep *ep = &xprt->rx_ep;
241 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
242 struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr;
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_DEVICE_REMOVAL:
265 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
266 pr_info("rpcrdma: removing device %s for %pIS:%u\n",
268 sap, rpc_get_port(sap));
270 set_bit(RPCRDMA_IAF_REMOVING, &ia->ri_flags);
271 ep->rep_connected = -ENODEV;
272 xprt_force_disconnect(&xprt->rx_xprt);
273 wait_for_completion(&ia->ri_remove_done);
277 ia->ri_device = NULL;
278 /* Return 1 to ensure the core destroys the id. */
280 case RDMA_CM_EVENT_ESTABLISHED:
282 rpcrdma_update_connect_private(xprt, &event->param.conn);
284 case RDMA_CM_EVENT_CONNECT_ERROR:
285 connstate = -ENOTCONN;
287 case RDMA_CM_EVENT_UNREACHABLE:
288 connstate = -ENETDOWN;
290 case RDMA_CM_EVENT_REJECTED:
291 dprintk("rpcrdma: connection to %pIS:%u rejected: %s\n",
292 sap, rpc_get_port(sap),
293 rdma_reject_msg(id, event->status));
294 connstate = -ECONNREFUSED;
295 if (event->status == IB_CM_REJ_STALE_CONN)
298 case RDMA_CM_EVENT_DISCONNECTED:
299 connstate = -ECONNABORTED;
301 atomic_set(&xprt->rx_buf.rb_credits, 1);
302 ep->rep_connected = connstate;
303 rpcrdma_conn_func(ep);
304 wake_up_all(&ep->rep_connect_wait);
307 dprintk("RPC: %s: %pIS:%u on %s/%s (ep 0x%p): %s\n",
308 __func__, sap, rpc_get_port(sap),
309 ia->ri_device->name, ia->ri_ops->ro_displayname,
310 ep, rdma_event_msg(event->event));
317 static struct rdma_cm_id *
318 rpcrdma_create_id(struct rpcrdma_xprt *xprt,
319 struct rpcrdma_ia *ia, struct sockaddr *addr)
321 unsigned long wtimeout = msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1;
322 struct rdma_cm_id *id;
325 init_completion(&ia->ri_done);
326 init_completion(&ia->ri_remove_done);
328 id = rdma_create_id(&init_net, rpcrdma_conn_upcall, xprt, RDMA_PS_TCP,
332 dprintk("RPC: %s: rdma_create_id() failed %i\n",
337 ia->ri_async_rc = -ETIMEDOUT;
338 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
340 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
344 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
346 dprintk("RPC: %s: wait() exited: %i\n",
351 rc = ia->ri_async_rc;
355 ia->ri_async_rc = -ETIMEDOUT;
356 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
358 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
362 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
364 dprintk("RPC: %s: wait() exited: %i\n",
368 rc = ia->ri_async_rc;
380 * Exported functions.
384 * rpcrdma_ia_open - Open and initialize an Interface Adapter.
385 * @xprt: controlling transport
386 * @addr: IP address of remote peer
388 * Returns 0 on success, negative errno if an appropriate
389 * Interface Adapter could not be found and opened.
392 rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr)
394 struct rpcrdma_ia *ia = &xprt->rx_ia;
397 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
398 if (IS_ERR(ia->ri_id)) {
399 rc = PTR_ERR(ia->ri_id);
402 ia->ri_device = ia->ri_id->device;
404 ia->ri_pd = ib_alloc_pd(ia->ri_device, 0);
405 if (IS_ERR(ia->ri_pd)) {
406 rc = PTR_ERR(ia->ri_pd);
407 pr_err("rpcrdma: ib_alloc_pd() returned %d\n", rc);
411 switch (xprt_rdma_memreg_strategy) {
413 if (frwr_is_supported(ia)) {
414 ia->ri_ops = &rpcrdma_frwr_memreg_ops;
418 case RPCRDMA_MTHCAFMR:
419 if (fmr_is_supported(ia)) {
420 ia->ri_ops = &rpcrdma_fmr_memreg_ops;
425 pr_err("rpcrdma: Device %s does not support memreg mode %d\n",
426 ia->ri_device->name, xprt_rdma_memreg_strategy);
434 rpcrdma_ia_close(ia);
439 * rpcrdma_ia_remove - Handle device driver unload
440 * @ia: interface adapter being removed
442 * Divest transport H/W resources associated with this adapter,
443 * but allow it to be restored later.
446 rpcrdma_ia_remove(struct rpcrdma_ia *ia)
448 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
450 struct rpcrdma_ep *ep = &r_xprt->rx_ep;
451 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
452 struct rpcrdma_req *req;
453 struct rpcrdma_rep *rep;
455 cancel_delayed_work_sync(&buf->rb_refresh_worker);
457 /* This is similar to rpcrdma_ep_destroy, but:
458 * - Don't cancel the connect worker.
459 * - Don't call rpcrdma_ep_disconnect, which waits
460 * for another conn upcall, which will deadlock.
461 * - rdma_disconnect is unneeded, the underlying
462 * connection is already gone.
465 ib_drain_qp(ia->ri_id->qp);
466 rdma_destroy_qp(ia->ri_id);
467 ia->ri_id->qp = NULL;
469 ib_free_cq(ep->rep_attr.recv_cq);
470 ib_free_cq(ep->rep_attr.send_cq);
472 /* The ULP is responsible for ensuring all DMA
473 * mappings and MRs are gone.
475 list_for_each_entry(rep, &buf->rb_recv_bufs, rr_list)
476 rpcrdma_dma_unmap_regbuf(rep->rr_rdmabuf);
477 list_for_each_entry(req, &buf->rb_allreqs, rl_all) {
478 rpcrdma_dma_unmap_regbuf(req->rl_rdmabuf);
479 rpcrdma_dma_unmap_regbuf(req->rl_sendbuf);
480 rpcrdma_dma_unmap_regbuf(req->rl_recvbuf);
482 rpcrdma_destroy_mrs(buf);
484 /* Allow waiters to continue */
485 complete(&ia->ri_remove_done);
489 * rpcrdma_ia_close - Clean up/close an IA.
490 * @ia: interface adapter to close
494 rpcrdma_ia_close(struct rpcrdma_ia *ia)
496 dprintk("RPC: %s: entering\n", __func__);
497 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
499 rdma_destroy_qp(ia->ri_id);
500 rdma_destroy_id(ia->ri_id);
503 ia->ri_device = NULL;
505 /* If the pd is still busy, xprtrdma missed freeing a resource */
506 if (ia->ri_pd && !IS_ERR(ia->ri_pd))
507 ib_dealloc_pd(ia->ri_pd);
512 * Create unconnected endpoint.
515 rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
516 struct rpcrdma_create_data_internal *cdata)
518 struct rpcrdma_connect_private *pmsg = &ep->rep_cm_private;
519 unsigned int max_qp_wr, max_sge;
520 struct ib_cq *sendcq, *recvcq;
523 max_sge = min_t(unsigned int, ia->ri_device->attrs.max_sge,
524 RPCRDMA_MAX_SEND_SGES);
525 if (max_sge < RPCRDMA_MIN_SEND_SGES) {
526 pr_warn("rpcrdma: HCA provides only %d send SGEs\n", max_sge);
529 ia->ri_max_send_sges = max_sge - RPCRDMA_MIN_SEND_SGES;
531 if (ia->ri_device->attrs.max_qp_wr <= RPCRDMA_BACKWARD_WRS) {
532 dprintk("RPC: %s: insufficient wqe's available\n",
536 max_qp_wr = ia->ri_device->attrs.max_qp_wr - RPCRDMA_BACKWARD_WRS - 1;
538 /* check provider's send/recv wr limits */
539 if (cdata->max_requests > max_qp_wr)
540 cdata->max_requests = max_qp_wr;
542 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
543 ep->rep_attr.qp_context = ep;
544 ep->rep_attr.srq = NULL;
545 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
546 ep->rep_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS;
547 ep->rep_attr.cap.max_send_wr += 1; /* drain cqe */
548 rc = ia->ri_ops->ro_open(ia, ep, cdata);
551 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
552 ep->rep_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS;
553 ep->rep_attr.cap.max_recv_wr += 1; /* drain cqe */
554 ep->rep_attr.cap.max_send_sge = max_sge;
555 ep->rep_attr.cap.max_recv_sge = 1;
556 ep->rep_attr.cap.max_inline_data = 0;
557 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
558 ep->rep_attr.qp_type = IB_QPT_RC;
559 ep->rep_attr.port_num = ~0;
561 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
562 "iovs: send %d recv %d\n",
564 ep->rep_attr.cap.max_send_wr,
565 ep->rep_attr.cap.max_recv_wr,
566 ep->rep_attr.cap.max_send_sge,
567 ep->rep_attr.cap.max_recv_sge);
569 /* set trigger for requesting send completion */
570 ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
571 if (ep->rep_cqinit <= 2)
572 ep->rep_cqinit = 0; /* always signal? */
573 rpcrdma_init_cqcount(ep, 0);
574 init_waitqueue_head(&ep->rep_connect_wait);
575 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
577 sendcq = ib_alloc_cq(ia->ri_device, NULL,
578 ep->rep_attr.cap.max_send_wr + 1,
580 if (IS_ERR(sendcq)) {
581 rc = PTR_ERR(sendcq);
582 dprintk("RPC: %s: failed to create send CQ: %i\n",
587 recvcq = ib_alloc_cq(ia->ri_device, NULL,
588 ep->rep_attr.cap.max_recv_wr + 1,
590 if (IS_ERR(recvcq)) {
591 rc = PTR_ERR(recvcq);
592 dprintk("RPC: %s: failed to create recv CQ: %i\n",
597 ep->rep_attr.send_cq = sendcq;
598 ep->rep_attr.recv_cq = recvcq;
600 /* Initialize cma parameters */
601 memset(&ep->rep_remote_cma, 0, sizeof(ep->rep_remote_cma));
603 /* Prepare RDMA-CM private message */
604 pmsg->cp_magic = rpcrdma_cmp_magic;
605 pmsg->cp_version = RPCRDMA_CMP_VERSION;
606 pmsg->cp_flags |= ia->ri_ops->ro_send_w_inv_ok;
607 pmsg->cp_send_size = rpcrdma_encode_buffer_size(cdata->inline_wsize);
608 pmsg->cp_recv_size = rpcrdma_encode_buffer_size(cdata->inline_rsize);
609 ep->rep_remote_cma.private_data = pmsg;
610 ep->rep_remote_cma.private_data_len = sizeof(*pmsg);
612 /* Client offers RDMA Read but does not initiate */
613 ep->rep_remote_cma.initiator_depth = 0;
614 if (ia->ri_device->attrs.max_qp_rd_atom > 32) /* arbitrary but <= 255 */
615 ep->rep_remote_cma.responder_resources = 32;
617 ep->rep_remote_cma.responder_resources =
618 ia->ri_device->attrs.max_qp_rd_atom;
620 /* Limit transport retries so client can detect server
621 * GID changes quickly. RPC layer handles re-establishing
622 * transport connection and retransmission.
624 ep->rep_remote_cma.retry_count = 6;
626 /* RPC-over-RDMA handles its own flow control. In addition,
627 * make all RNR NAKs visible so we know that RPC-over-RDMA
628 * flow control is working correctly (no NAKs should be seen).
630 ep->rep_remote_cma.flow_control = 0;
631 ep->rep_remote_cma.rnr_retry_count = 0;
644 * Disconnect and destroy endpoint. After this, the only
645 * valid operations on the ep are to free it (if dynamically
646 * allocated) or re-create it.
649 rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
651 dprintk("RPC: %s: entering, connected is %d\n",
652 __func__, ep->rep_connected);
654 cancel_delayed_work_sync(&ep->rep_connect_worker);
657 rpcrdma_ep_disconnect(ep, ia);
658 rdma_destroy_qp(ia->ri_id);
659 ia->ri_id->qp = NULL;
662 ib_free_cq(ep->rep_attr.recv_cq);
663 ib_free_cq(ep->rep_attr.send_cq);
666 /* Re-establish a connection after a device removal event.
667 * Unlike a normal reconnection, a fresh PD and a new set
668 * of MRs and buffers is needed.
671 rpcrdma_ep_recreate_xprt(struct rpcrdma_xprt *r_xprt,
672 struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
674 struct sockaddr *sap = (struct sockaddr *)&r_xprt->rx_data.addr;
677 pr_info("%s: r_xprt = %p\n", __func__, r_xprt);
680 if (rpcrdma_ia_open(r_xprt, sap))
684 err = rpcrdma_ep_create(ep, ia, &r_xprt->rx_data);
686 pr_err("rpcrdma: rpcrdma_ep_create returned %d\n", err);
691 err = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
693 pr_err("rpcrdma: rdma_create_qp returned %d\n", err);
697 rpcrdma_create_mrs(r_xprt);
701 rpcrdma_ep_destroy(ep, ia);
703 rpcrdma_ia_close(ia);
709 rpcrdma_ep_reconnect(struct rpcrdma_xprt *r_xprt, struct rpcrdma_ep *ep,
710 struct rpcrdma_ia *ia)
712 struct sockaddr *sap = (struct sockaddr *)&r_xprt->rx_data.addr;
713 struct rdma_cm_id *id, *old;
716 dprintk("RPC: %s: reconnecting...\n", __func__);
718 rpcrdma_ep_disconnect(ep, ia);
721 id = rpcrdma_create_id(r_xprt, ia, sap);
725 /* As long as the new ID points to the same device as the
726 * old ID, we can reuse the transport's existing PD and all
727 * previously allocated MRs. Also, the same device means
728 * the transport's previous DMA mappings are still valid.
730 * This is a sanity check only. There should be no way these
731 * point to two different devices here.
735 if (ia->ri_device != id->device) {
736 pr_err("rpcrdma: can't reconnect on different device!\n");
740 err = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
742 dprintk("RPC: %s: rdma_create_qp returned %d\n",
747 /* Atomically replace the transport's ID and QP. */
751 rdma_destroy_qp(old);
754 rdma_destroy_id(old);
760 * Connect unconnected endpoint.
763 rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
765 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
771 switch (ep->rep_connected) {
773 dprintk("RPC: %s: connecting...\n", __func__);
774 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
776 dprintk("RPC: %s: rdma_create_qp failed %i\n",
783 rc = rpcrdma_ep_recreate_xprt(r_xprt, ep, ia);
788 rc = rpcrdma_ep_reconnect(r_xprt, ep, ia);
793 ep->rep_connected = 0;
795 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
797 dprintk("RPC: %s: rdma_connect() failed with %i\n",
802 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
803 if (ep->rep_connected <= 0) {
804 if (ep->rep_connected == -EAGAIN)
806 rc = ep->rep_connected;
810 dprintk("RPC: %s: connected\n", __func__);
811 extras = r_xprt->rx_buf.rb_bc_srv_max_requests;
813 rpcrdma_ep_post_extra_recv(r_xprt, extras);
817 ep->rep_connected = rc;
824 * rpcrdma_ep_disconnect
826 * This is separate from destroy to facilitate the ability
827 * to reconnect without recreating the endpoint.
829 * This call is not reentrant, and must not be made in parallel
830 * on the same endpoint.
833 rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
837 rc = rdma_disconnect(ia->ri_id);
839 /* returns without wait if not connected */
840 wait_event_interruptible(ep->rep_connect_wait,
841 ep->rep_connected != 1);
842 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
843 (ep->rep_connected == 1) ? "still " : "dis");
845 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
846 ep->rep_connected = rc;
849 ib_drain_qp(ia->ri_id->qp);
853 rpcrdma_mr_recovery_worker(struct work_struct *work)
855 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
856 rb_recovery_worker.work);
857 struct rpcrdma_mw *mw;
859 spin_lock(&buf->rb_recovery_lock);
860 while (!list_empty(&buf->rb_stale_mrs)) {
861 mw = rpcrdma_pop_mw(&buf->rb_stale_mrs);
862 spin_unlock(&buf->rb_recovery_lock);
864 dprintk("RPC: %s: recovering MR %p\n", __func__, mw);
865 mw->mw_xprt->rx_ia.ri_ops->ro_recover_mr(mw);
867 spin_lock(&buf->rb_recovery_lock);
869 spin_unlock(&buf->rb_recovery_lock);
873 rpcrdma_defer_mr_recovery(struct rpcrdma_mw *mw)
875 struct rpcrdma_xprt *r_xprt = mw->mw_xprt;
876 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
878 spin_lock(&buf->rb_recovery_lock);
879 rpcrdma_push_mw(mw, &buf->rb_stale_mrs);
880 spin_unlock(&buf->rb_recovery_lock);
882 schedule_delayed_work(&buf->rb_recovery_worker, 0);
886 rpcrdma_create_mrs(struct rpcrdma_xprt *r_xprt)
888 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
889 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
894 for (count = 0; count < 32; count++) {
895 struct rpcrdma_mw *mw;
898 mw = kzalloc(sizeof(*mw), GFP_KERNEL);
902 rc = ia->ri_ops->ro_init_mr(ia, mw);
908 mw->mw_xprt = r_xprt;
910 list_add(&mw->mw_list, &free);
911 list_add(&mw->mw_all, &all);
914 spin_lock(&buf->rb_mwlock);
915 list_splice(&free, &buf->rb_mws);
916 list_splice(&all, &buf->rb_all);
917 r_xprt->rx_stats.mrs_allocated += count;
918 spin_unlock(&buf->rb_mwlock);
920 dprintk("RPC: %s: created %u MRs\n", __func__, count);
924 rpcrdma_mr_refresh_worker(struct work_struct *work)
926 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
927 rb_refresh_worker.work);
928 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
931 rpcrdma_create_mrs(r_xprt);
935 rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
937 struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
938 struct rpcrdma_req *req;
940 req = kzalloc(sizeof(*req), GFP_KERNEL);
942 return ERR_PTR(-ENOMEM);
944 spin_lock(&buffer->rb_reqslock);
945 list_add(&req->rl_all, &buffer->rb_allreqs);
946 spin_unlock(&buffer->rb_reqslock);
947 req->rl_cqe.done = rpcrdma_wc_send;
948 req->rl_buffer = &r_xprt->rx_buf;
949 INIT_LIST_HEAD(&req->rl_registered);
950 req->rl_send_wr.next = NULL;
951 req->rl_send_wr.wr_cqe = &req->rl_cqe;
952 req->rl_send_wr.sg_list = req->rl_send_sge;
953 req->rl_send_wr.opcode = IB_WR_SEND;
958 rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
960 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
961 struct rpcrdma_rep *rep;
965 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
969 rep->rr_rdmabuf = rpcrdma_alloc_regbuf(cdata->inline_rsize,
970 DMA_FROM_DEVICE, GFP_KERNEL);
971 if (IS_ERR(rep->rr_rdmabuf)) {
972 rc = PTR_ERR(rep->rr_rdmabuf);
975 xdr_buf_init(&rep->rr_hdrbuf, rep->rr_rdmabuf->rg_base,
976 rdmab_length(rep->rr_rdmabuf));
978 rep->rr_cqe.done = rpcrdma_wc_receive;
979 rep->rr_rxprt = r_xprt;
980 INIT_WORK(&rep->rr_work, rpcrdma_reply_handler);
981 rep->rr_recv_wr.next = NULL;
982 rep->rr_recv_wr.wr_cqe = &rep->rr_cqe;
983 rep->rr_recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
984 rep->rr_recv_wr.num_sge = 1;
994 rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
996 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
999 buf->rb_max_requests = r_xprt->rx_data.max_requests;
1000 buf->rb_bc_srv_max_requests = 0;
1001 atomic_set(&buf->rb_credits, 1);
1002 spin_lock_init(&buf->rb_mwlock);
1003 spin_lock_init(&buf->rb_lock);
1004 spin_lock_init(&buf->rb_recovery_lock);
1005 INIT_LIST_HEAD(&buf->rb_mws);
1006 INIT_LIST_HEAD(&buf->rb_all);
1007 INIT_LIST_HEAD(&buf->rb_pending);
1008 INIT_LIST_HEAD(&buf->rb_stale_mrs);
1009 INIT_DELAYED_WORK(&buf->rb_refresh_worker,
1010 rpcrdma_mr_refresh_worker);
1011 INIT_DELAYED_WORK(&buf->rb_recovery_worker,
1012 rpcrdma_mr_recovery_worker);
1014 rpcrdma_create_mrs(r_xprt);
1016 INIT_LIST_HEAD(&buf->rb_send_bufs);
1017 INIT_LIST_HEAD(&buf->rb_allreqs);
1018 spin_lock_init(&buf->rb_reqslock);
1019 for (i = 0; i < buf->rb_max_requests; i++) {
1020 struct rpcrdma_req *req;
1022 req = rpcrdma_create_req(r_xprt);
1024 dprintk("RPC: %s: request buffer %d alloc"
1025 " failed\n", __func__, i);
1029 req->rl_backchannel = false;
1030 list_add(&req->rl_list, &buf->rb_send_bufs);
1033 INIT_LIST_HEAD(&buf->rb_recv_bufs);
1034 for (i = 0; i < buf->rb_max_requests + RPCRDMA_MAX_BC_REQUESTS; i++) {
1035 struct rpcrdma_rep *rep;
1037 rep = rpcrdma_create_rep(r_xprt);
1039 dprintk("RPC: %s: reply buffer %d alloc failed\n",
1044 list_add(&rep->rr_list, &buf->rb_recv_bufs);
1049 rpcrdma_buffer_destroy(buf);
1053 static struct rpcrdma_req *
1054 rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf)
1056 struct rpcrdma_req *req;
1058 req = list_first_entry(&buf->rb_send_bufs,
1059 struct rpcrdma_req, rl_list);
1060 list_del_init(&req->rl_list);
1064 static struct rpcrdma_rep *
1065 rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf)
1067 struct rpcrdma_rep *rep;
1069 rep = list_first_entry(&buf->rb_recv_bufs,
1070 struct rpcrdma_rep, rr_list);
1071 list_del(&rep->rr_list);
1076 rpcrdma_destroy_rep(struct rpcrdma_rep *rep)
1078 rpcrdma_free_regbuf(rep->rr_rdmabuf);
1083 rpcrdma_destroy_req(struct rpcrdma_req *req)
1085 rpcrdma_free_regbuf(req->rl_recvbuf);
1086 rpcrdma_free_regbuf(req->rl_sendbuf);
1087 rpcrdma_free_regbuf(req->rl_rdmabuf);
1092 rpcrdma_destroy_mrs(struct rpcrdma_buffer *buf)
1094 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
1096 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
1097 struct rpcrdma_mw *mw;
1101 spin_lock(&buf->rb_mwlock);
1102 while (!list_empty(&buf->rb_all)) {
1103 mw = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
1104 list_del(&mw->mw_all);
1106 spin_unlock(&buf->rb_mwlock);
1107 ia->ri_ops->ro_release_mr(mw);
1109 spin_lock(&buf->rb_mwlock);
1111 spin_unlock(&buf->rb_mwlock);
1112 r_xprt->rx_stats.mrs_allocated = 0;
1114 dprintk("RPC: %s: released %u MRs\n", __func__, count);
1118 rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1120 cancel_delayed_work_sync(&buf->rb_recovery_worker);
1121 cancel_delayed_work_sync(&buf->rb_refresh_worker);
1123 while (!list_empty(&buf->rb_recv_bufs)) {
1124 struct rpcrdma_rep *rep;
1126 rep = rpcrdma_buffer_get_rep_locked(buf);
1127 rpcrdma_destroy_rep(rep);
1129 buf->rb_send_count = 0;
1131 spin_lock(&buf->rb_reqslock);
1132 while (!list_empty(&buf->rb_allreqs)) {
1133 struct rpcrdma_req *req;
1135 req = list_first_entry(&buf->rb_allreqs,
1136 struct rpcrdma_req, rl_all);
1137 list_del(&req->rl_all);
1139 spin_unlock(&buf->rb_reqslock);
1140 rpcrdma_destroy_req(req);
1141 spin_lock(&buf->rb_reqslock);
1143 spin_unlock(&buf->rb_reqslock);
1144 buf->rb_recv_count = 0;
1146 rpcrdma_destroy_mrs(buf);
1150 rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt)
1152 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1153 struct rpcrdma_mw *mw = NULL;
1155 spin_lock(&buf->rb_mwlock);
1156 if (!list_empty(&buf->rb_mws))
1157 mw = rpcrdma_pop_mw(&buf->rb_mws);
1158 spin_unlock(&buf->rb_mwlock);
1166 dprintk("RPC: %s: no MWs available\n", __func__);
1167 if (r_xprt->rx_ep.rep_connected != -ENODEV)
1168 schedule_delayed_work(&buf->rb_refresh_worker, 0);
1170 /* Allow the reply handler and refresh worker to run */
1177 rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw)
1179 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1181 spin_lock(&buf->rb_mwlock);
1182 rpcrdma_push_mw(mw, &buf->rb_mws);
1183 spin_unlock(&buf->rb_mwlock);
1186 static struct rpcrdma_rep *
1187 rpcrdma_buffer_get_rep(struct rpcrdma_buffer *buffers)
1189 /* If an RPC previously completed without a reply (say, a
1190 * credential problem or a soft timeout occurs) then hold off
1191 * on supplying more Receive buffers until the number of new
1192 * pending RPCs catches up to the number of posted Receives.
1194 if (unlikely(buffers->rb_send_count < buffers->rb_recv_count))
1197 if (unlikely(list_empty(&buffers->rb_recv_bufs)))
1199 buffers->rb_recv_count++;
1200 return rpcrdma_buffer_get_rep_locked(buffers);
1204 * Get a set of request/reply buffers.
1206 * Reply buffer (if available) is attached to send buffer upon return.
1208 struct rpcrdma_req *
1209 rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1211 struct rpcrdma_req *req;
1213 spin_lock(&buffers->rb_lock);
1214 if (list_empty(&buffers->rb_send_bufs))
1216 buffers->rb_send_count++;
1217 req = rpcrdma_buffer_get_req_locked(buffers);
1218 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
1219 spin_unlock(&buffers->rb_lock);
1223 spin_unlock(&buffers->rb_lock);
1224 pr_warn("RPC: %s: out of request buffers\n", __func__);
1229 * Put request/reply buffers back into pool.
1230 * Pre-decrement counter/array index.
1233 rpcrdma_buffer_put(struct rpcrdma_req *req)
1235 struct rpcrdma_buffer *buffers = req->rl_buffer;
1236 struct rpcrdma_rep *rep = req->rl_reply;
1238 req->rl_send_wr.num_sge = 0;
1239 req->rl_reply = NULL;
1241 spin_lock(&buffers->rb_lock);
1242 buffers->rb_send_count--;
1243 list_add_tail(&req->rl_list, &buffers->rb_send_bufs);
1245 buffers->rb_recv_count--;
1246 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
1248 spin_unlock(&buffers->rb_lock);
1252 * Recover reply buffers from pool.
1253 * This happens when recovering from disconnect.
1256 rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1258 struct rpcrdma_buffer *buffers = req->rl_buffer;
1260 spin_lock(&buffers->rb_lock);
1261 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
1262 spin_unlock(&buffers->rb_lock);
1266 * Put reply buffers back into pool when not attached to
1267 * request. This happens in error conditions.
1270 rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1272 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
1274 spin_lock(&buffers->rb_lock);
1275 buffers->rb_recv_count--;
1276 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
1277 spin_unlock(&buffers->rb_lock);
1281 * rpcrdma_alloc_regbuf - allocate and DMA-map memory for SEND/RECV buffers
1282 * @size: size of buffer to be allocated, in bytes
1283 * @direction: direction of data movement
1286 * Returns an ERR_PTR, or a pointer to a regbuf, a buffer that
1287 * can be persistently DMA-mapped for I/O.
1289 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
1290 * receiving the payload of RDMA RECV operations. During Long Calls
1291 * or Replies they may be registered externally via ro_map.
1293 struct rpcrdma_regbuf *
1294 rpcrdma_alloc_regbuf(size_t size, enum dma_data_direction direction,
1297 struct rpcrdma_regbuf *rb;
1299 rb = kmalloc(sizeof(*rb) + size, flags);
1301 return ERR_PTR(-ENOMEM);
1303 rb->rg_device = NULL;
1304 rb->rg_direction = direction;
1305 rb->rg_iov.length = size;
1311 * __rpcrdma_map_regbuf - DMA-map a regbuf
1312 * @ia: controlling rpcrdma_ia
1313 * @rb: regbuf to be mapped
1316 __rpcrdma_dma_map_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1318 struct ib_device *device = ia->ri_device;
1320 if (rb->rg_direction == DMA_NONE)
1323 rb->rg_iov.addr = ib_dma_map_single(device,
1324 (void *)rb->rg_base,
1327 if (ib_dma_mapping_error(device, rdmab_addr(rb)))
1330 rb->rg_device = device;
1331 rb->rg_iov.lkey = ia->ri_pd->local_dma_lkey;
1336 rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb)
1338 if (!rpcrdma_regbuf_is_mapped(rb))
1341 ib_dma_unmap_single(rb->rg_device, rdmab_addr(rb),
1342 rdmab_length(rb), rb->rg_direction);
1343 rb->rg_device = NULL;
1347 * rpcrdma_free_regbuf - deregister and free registered buffer
1348 * @rb: regbuf to be deregistered and freed
1351 rpcrdma_free_regbuf(struct rpcrdma_regbuf *rb)
1356 rpcrdma_dma_unmap_regbuf(rb);
1361 * Prepost any receive buffer, then post send.
1363 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1366 rpcrdma_ep_post(struct rpcrdma_ia *ia,
1367 struct rpcrdma_ep *ep,
1368 struct rpcrdma_req *req)
1370 struct ib_send_wr *send_wr = &req->rl_send_wr;
1371 struct ib_send_wr *send_wr_fail;
1374 if (req->rl_reply) {
1375 rc = rpcrdma_ep_post_recv(ia, req->rl_reply);
1378 req->rl_reply = NULL;
1381 dprintk("RPC: %s: posting %d s/g entries\n",
1382 __func__, send_wr->num_sge);
1384 rpcrdma_set_signaled(ep, send_wr);
1385 rc = ib_post_send(ia->ri_id->qp, send_wr, &send_wr_fail);
1387 goto out_postsend_err;
1391 pr_err("rpcrdma: RDMA Send ib_post_send returned %i\n", rc);
1396 rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
1397 struct rpcrdma_rep *rep)
1399 struct ib_recv_wr *recv_wr_fail;
1402 if (!rpcrdma_dma_map_regbuf(ia, rep->rr_rdmabuf))
1404 rc = ib_post_recv(ia->ri_id->qp, &rep->rr_recv_wr, &recv_wr_fail);
1410 pr_err("rpcrdma: failed to DMA map the Receive buffer\n");
1414 pr_err("rpcrdma: ib_post_recv returned %i\n", rc);
1419 * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests
1420 * @r_xprt: transport associated with these backchannel resources
1421 * @min_reqs: minimum number of incoming requests expected
1423 * Returns zero if all requested buffers were posted, or a negative errno.
1426 rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt *r_xprt, unsigned int count)
1428 struct rpcrdma_buffer *buffers = &r_xprt->rx_buf;
1429 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
1430 struct rpcrdma_rep *rep;
1434 spin_lock(&buffers->rb_lock);
1435 if (list_empty(&buffers->rb_recv_bufs))
1437 rep = rpcrdma_buffer_get_rep_locked(buffers);
1438 spin_unlock(&buffers->rb_lock);
1440 rc = rpcrdma_ep_post_recv(ia, rep);
1448 spin_unlock(&buffers->rb_lock);
1449 pr_warn("%s: no extra receive buffers\n", __func__);
1453 rpcrdma_recv_buffer_put(rep);