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_buffer *buffer = &rep->rr_rxprt->rx_buf;
143 __be32 *p = rep->rr_rdmabuf->rg_base;
146 credits = be32_to_cpup(p + 2);
148 credits = 1; /* don't deadlock */
149 else if (credits > buffer->rb_max_requests)
150 credits = buffer->rb_max_requests;
152 atomic_set(&buffer->rb_credits, credits);
156 * rpcrdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
157 * @cq: completion queue (ignored)
162 rpcrdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
164 struct ib_cqe *cqe = wc->wr_cqe;
165 struct rpcrdma_rep *rep = container_of(cqe, struct rpcrdma_rep,
168 /* WARNING: Only wr_id and status are reliable at this point */
169 if (wc->status != IB_WC_SUCCESS)
172 /* status == SUCCESS means all fields in wc are trustworthy */
173 dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
174 __func__, rep, wc->byte_len);
176 rpcrdma_set_xdrlen(&rep->rr_hdrbuf, wc->byte_len);
177 rep->rr_wc_flags = wc->wc_flags;
178 rep->rr_inv_rkey = wc->ex.invalidate_rkey;
180 ib_dma_sync_single_for_cpu(rdmab_device(rep->rr_rdmabuf),
181 rdmab_addr(rep->rr_rdmabuf),
182 wc->byte_len, DMA_FROM_DEVICE);
184 if (wc->byte_len >= RPCRDMA_HDRLEN_ERR)
185 rpcrdma_update_granted_credits(rep);
188 queue_work(rpcrdma_receive_wq, &rep->rr_work);
192 if (wc->status != IB_WC_WR_FLUSH_ERR)
193 pr_err("rpcrdma: Recv: %s (%u/0x%x)\n",
194 ib_wc_status_msg(wc->status),
195 wc->status, wc->vendor_err);
196 rpcrdma_set_xdrlen(&rep->rr_hdrbuf, 0);
201 rpcrdma_update_connect_private(struct rpcrdma_xprt *r_xprt,
202 struct rdma_conn_param *param)
204 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
205 const struct rpcrdma_connect_private *pmsg = param->private_data;
206 unsigned int rsize, wsize;
208 /* Default settings for RPC-over-RDMA Version One */
209 r_xprt->rx_ia.ri_reminv_expected = false;
210 r_xprt->rx_ia.ri_implicit_roundup = xprt_rdma_pad_optimize;
211 rsize = RPCRDMA_V1_DEF_INLINE_SIZE;
212 wsize = RPCRDMA_V1_DEF_INLINE_SIZE;
215 pmsg->cp_magic == rpcrdma_cmp_magic &&
216 pmsg->cp_version == RPCRDMA_CMP_VERSION) {
217 r_xprt->rx_ia.ri_reminv_expected = true;
218 r_xprt->rx_ia.ri_implicit_roundup = true;
219 rsize = rpcrdma_decode_buffer_size(pmsg->cp_send_size);
220 wsize = rpcrdma_decode_buffer_size(pmsg->cp_recv_size);
223 if (rsize < cdata->inline_rsize)
224 cdata->inline_rsize = rsize;
225 if (wsize < cdata->inline_wsize)
226 cdata->inline_wsize = wsize;
227 dprintk("RPC: %s: max send %u, max recv %u\n",
228 __func__, cdata->inline_wsize, cdata->inline_rsize);
229 rpcrdma_set_max_header_sizes(r_xprt);
233 rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
235 struct rpcrdma_xprt *xprt = id->context;
236 struct rpcrdma_ia *ia = &xprt->rx_ia;
237 struct rpcrdma_ep *ep = &xprt->rx_ep;
238 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
239 struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr;
243 switch (event->event) {
244 case RDMA_CM_EVENT_ADDR_RESOLVED:
245 case RDMA_CM_EVENT_ROUTE_RESOLVED:
247 complete(&ia->ri_done);
249 case RDMA_CM_EVENT_ADDR_ERROR:
250 ia->ri_async_rc = -EHOSTUNREACH;
251 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
253 complete(&ia->ri_done);
255 case RDMA_CM_EVENT_ROUTE_ERROR:
256 ia->ri_async_rc = -ENETUNREACH;
257 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
259 complete(&ia->ri_done);
261 case RDMA_CM_EVENT_DEVICE_REMOVAL:
262 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
263 pr_info("rpcrdma: removing device %s for %pIS:%u\n",
265 sap, rpc_get_port(sap));
267 init_completion(&ia->ri_remove_done);
268 set_bit(RPCRDMA_IAF_REMOVING, &ia->ri_flags);
269 ep->rep_connected = -ENODEV;
270 xprt_force_disconnect(&xprt->rx_xprt);
271 wait_for_completion(&ia->ri_remove_done);
274 ia->ri_device = NULL;
275 /* Return 1 to ensure the core destroys the id. */
277 case RDMA_CM_EVENT_ESTABLISHED:
279 rpcrdma_update_connect_private(xprt, &event->param.conn);
281 case RDMA_CM_EVENT_CONNECT_ERROR:
282 connstate = -ENOTCONN;
284 case RDMA_CM_EVENT_UNREACHABLE:
285 connstate = -ENETDOWN;
287 case RDMA_CM_EVENT_REJECTED:
288 dprintk("rpcrdma: connection to %pIS:%u rejected: %s\n",
289 sap, rpc_get_port(sap),
290 rdma_reject_msg(id, event->status));
291 connstate = -ECONNREFUSED;
292 if (event->status == IB_CM_REJ_STALE_CONN)
295 case RDMA_CM_EVENT_DISCONNECTED:
296 connstate = -ECONNABORTED;
298 atomic_set(&xprt->rx_buf.rb_credits, 1);
299 ep->rep_connected = connstate;
300 rpcrdma_conn_func(ep);
301 wake_up_all(&ep->rep_connect_wait);
304 dprintk("RPC: %s: %pIS:%u on %s/%s (ep 0x%p): %s\n",
305 __func__, sap, rpc_get_port(sap),
306 ia->ri_device->name, ia->ri_ops->ro_displayname,
307 ep, rdma_event_msg(event->event));
314 static struct rdma_cm_id *
315 rpcrdma_create_id(struct rpcrdma_xprt *xprt,
316 struct rpcrdma_ia *ia, struct sockaddr *addr)
318 unsigned long wtimeout = msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1;
319 struct rdma_cm_id *id;
322 init_completion(&ia->ri_done);
324 id = rdma_create_id(&init_net, rpcrdma_conn_upcall, xprt, RDMA_PS_TCP,
328 dprintk("RPC: %s: rdma_create_id() failed %i\n",
333 ia->ri_async_rc = -ETIMEDOUT;
334 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
336 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
340 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
342 dprintk("RPC: %s: wait() exited: %i\n",
347 rc = ia->ri_async_rc;
351 ia->ri_async_rc = -ETIMEDOUT;
352 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
354 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
358 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
360 dprintk("RPC: %s: wait() exited: %i\n",
364 rc = ia->ri_async_rc;
376 * Exported functions.
380 * rpcrdma_ia_open - Open and initialize an Interface Adapter.
381 * @xprt: controlling transport
382 * @addr: IP address of remote peer
384 * Returns 0 on success, negative errno if an appropriate
385 * Interface Adapter could not be found and opened.
388 rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr)
390 struct rpcrdma_ia *ia = &xprt->rx_ia;
393 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
394 if (IS_ERR(ia->ri_id)) {
395 rc = PTR_ERR(ia->ri_id);
398 ia->ri_device = ia->ri_id->device;
400 ia->ri_pd = ib_alloc_pd(ia->ri_device, 0);
401 if (IS_ERR(ia->ri_pd)) {
402 rc = PTR_ERR(ia->ri_pd);
403 pr_err("rpcrdma: ib_alloc_pd() returned %d\n", rc);
407 switch (xprt_rdma_memreg_strategy) {
409 if (frwr_is_supported(ia)) {
410 ia->ri_ops = &rpcrdma_frwr_memreg_ops;
414 case RPCRDMA_MTHCAFMR:
415 if (fmr_is_supported(ia)) {
416 ia->ri_ops = &rpcrdma_fmr_memreg_ops;
421 pr_err("rpcrdma: Device %s does not support memreg mode %d\n",
422 ia->ri_device->name, xprt_rdma_memreg_strategy);
430 rpcrdma_ia_close(ia);
435 * rpcrdma_ia_remove - Handle device driver unload
436 * @ia: interface adapter being removed
438 * Divest transport H/W resources associated with this adapter,
439 * but allow it to be restored later.
442 rpcrdma_ia_remove(struct rpcrdma_ia *ia)
444 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
446 struct rpcrdma_ep *ep = &r_xprt->rx_ep;
447 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
448 struct rpcrdma_req *req;
449 struct rpcrdma_rep *rep;
451 cancel_delayed_work_sync(&buf->rb_refresh_worker);
453 /* This is similar to rpcrdma_ep_destroy, but:
454 * - Don't cancel the connect worker.
455 * - Don't call rpcrdma_ep_disconnect, which waits
456 * for another conn upcall, which will deadlock.
457 * - rdma_disconnect is unneeded, the underlying
458 * connection is already gone.
461 ib_drain_qp(ia->ri_id->qp);
462 rdma_destroy_qp(ia->ri_id);
463 ia->ri_id->qp = NULL;
465 ib_free_cq(ep->rep_attr.recv_cq);
466 ep->rep_attr.recv_cq = NULL;
467 ib_free_cq(ep->rep_attr.send_cq);
468 ep->rep_attr.send_cq = NULL;
470 /* The ULP is responsible for ensuring all DMA
471 * mappings and MRs are gone.
473 list_for_each_entry(rep, &buf->rb_recv_bufs, rr_list)
474 rpcrdma_dma_unmap_regbuf(rep->rr_rdmabuf);
475 list_for_each_entry(req, &buf->rb_allreqs, rl_all) {
476 rpcrdma_dma_unmap_regbuf(req->rl_rdmabuf);
477 rpcrdma_dma_unmap_regbuf(req->rl_sendbuf);
478 rpcrdma_dma_unmap_regbuf(req->rl_recvbuf);
480 rpcrdma_destroy_mrs(buf);
481 ib_dealloc_pd(ia->ri_pd);
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;
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);
656 if (ia->ri_id && ia->ri_id->qp) {
657 rpcrdma_ep_disconnect(ep, ia);
658 rdma_destroy_qp(ia->ri_id);
659 ia->ri_id->qp = NULL;
662 if (ep->rep_attr.recv_cq)
663 ib_free_cq(ep->rep_attr.recv_cq);
664 if (ep->rep_attr.send_cq)
665 ib_free_cq(ep->rep_attr.send_cq);
668 /* Re-establish a connection after a device removal event.
669 * Unlike a normal reconnection, a fresh PD and a new set
670 * of MRs and buffers is needed.
673 rpcrdma_ep_recreate_xprt(struct rpcrdma_xprt *r_xprt,
674 struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
676 struct sockaddr *sap = (struct sockaddr *)&r_xprt->rx_data.addr;
679 pr_info("%s: r_xprt = %p\n", __func__, r_xprt);
682 if (rpcrdma_ia_open(r_xprt, sap))
686 err = rpcrdma_ep_create(ep, ia, &r_xprt->rx_data);
688 pr_err("rpcrdma: rpcrdma_ep_create returned %d\n", err);
693 err = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
695 pr_err("rpcrdma: rdma_create_qp returned %d\n", err);
699 rpcrdma_create_mrs(r_xprt);
703 rpcrdma_ep_destroy(ep, ia);
705 rpcrdma_ia_close(ia);
711 rpcrdma_ep_reconnect(struct rpcrdma_xprt *r_xprt, struct rpcrdma_ep *ep,
712 struct rpcrdma_ia *ia)
714 struct sockaddr *sap = (struct sockaddr *)&r_xprt->rx_data.addr;
715 struct rdma_cm_id *id, *old;
718 dprintk("RPC: %s: reconnecting...\n", __func__);
720 rpcrdma_ep_disconnect(ep, ia);
723 id = rpcrdma_create_id(r_xprt, ia, sap);
727 /* As long as the new ID points to the same device as the
728 * old ID, we can reuse the transport's existing PD and all
729 * previously allocated MRs. Also, the same device means
730 * the transport's previous DMA mappings are still valid.
732 * This is a sanity check only. There should be no way these
733 * point to two different devices here.
737 if (ia->ri_device != id->device) {
738 pr_err("rpcrdma: can't reconnect on different device!\n");
742 err = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
744 dprintk("RPC: %s: rdma_create_qp returned %d\n",
749 /* Atomically replace the transport's ID and QP. */
753 rdma_destroy_qp(old);
756 rdma_destroy_id(old);
762 * Connect unconnected endpoint.
765 rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
767 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
773 switch (ep->rep_connected) {
775 dprintk("RPC: %s: connecting...\n", __func__);
776 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
778 dprintk("RPC: %s: rdma_create_qp failed %i\n",
785 rc = rpcrdma_ep_recreate_xprt(r_xprt, ep, ia);
790 rc = rpcrdma_ep_reconnect(r_xprt, ep, ia);
795 ep->rep_connected = 0;
797 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
799 dprintk("RPC: %s: rdma_connect() failed with %i\n",
804 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
805 if (ep->rep_connected <= 0) {
806 if (ep->rep_connected == -EAGAIN)
808 rc = ep->rep_connected;
812 dprintk("RPC: %s: connected\n", __func__);
813 extras = r_xprt->rx_buf.rb_bc_srv_max_requests;
815 rpcrdma_ep_post_extra_recv(r_xprt, extras);
819 ep->rep_connected = rc;
826 * rpcrdma_ep_disconnect
828 * This is separate from destroy to facilitate the ability
829 * to reconnect without recreating the endpoint.
831 * This call is not reentrant, and must not be made in parallel
832 * on the same endpoint.
835 rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
839 rc = rdma_disconnect(ia->ri_id);
841 /* returns without wait if not connected */
842 wait_event_interruptible(ep->rep_connect_wait,
843 ep->rep_connected != 1);
844 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
845 (ep->rep_connected == 1) ? "still " : "dis");
847 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
848 ep->rep_connected = rc;
851 ib_drain_qp(ia->ri_id->qp);
855 rpcrdma_mr_recovery_worker(struct work_struct *work)
857 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
858 rb_recovery_worker.work);
859 struct rpcrdma_mw *mw;
861 spin_lock(&buf->rb_recovery_lock);
862 while (!list_empty(&buf->rb_stale_mrs)) {
863 mw = rpcrdma_pop_mw(&buf->rb_stale_mrs);
864 spin_unlock(&buf->rb_recovery_lock);
866 dprintk("RPC: %s: recovering MR %p\n", __func__, mw);
867 mw->mw_xprt->rx_ia.ri_ops->ro_recover_mr(mw);
869 spin_lock(&buf->rb_recovery_lock);
871 spin_unlock(&buf->rb_recovery_lock);
875 rpcrdma_defer_mr_recovery(struct rpcrdma_mw *mw)
877 struct rpcrdma_xprt *r_xprt = mw->mw_xprt;
878 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
880 spin_lock(&buf->rb_recovery_lock);
881 rpcrdma_push_mw(mw, &buf->rb_stale_mrs);
882 spin_unlock(&buf->rb_recovery_lock);
884 schedule_delayed_work(&buf->rb_recovery_worker, 0);
888 rpcrdma_create_mrs(struct rpcrdma_xprt *r_xprt)
890 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
891 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
896 for (count = 0; count < 32; count++) {
897 struct rpcrdma_mw *mw;
900 mw = kzalloc(sizeof(*mw), GFP_KERNEL);
904 rc = ia->ri_ops->ro_init_mr(ia, mw);
910 mw->mw_xprt = r_xprt;
912 list_add(&mw->mw_list, &free);
913 list_add(&mw->mw_all, &all);
916 spin_lock(&buf->rb_mwlock);
917 list_splice(&free, &buf->rb_mws);
918 list_splice(&all, &buf->rb_all);
919 r_xprt->rx_stats.mrs_allocated += count;
920 spin_unlock(&buf->rb_mwlock);
922 dprintk("RPC: %s: created %u MRs\n", __func__, count);
926 rpcrdma_mr_refresh_worker(struct work_struct *work)
928 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
929 rb_refresh_worker.work);
930 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
933 rpcrdma_create_mrs(r_xprt);
937 rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
939 struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
940 struct rpcrdma_req *req;
942 req = kzalloc(sizeof(*req), GFP_KERNEL);
944 return ERR_PTR(-ENOMEM);
946 spin_lock(&buffer->rb_reqslock);
947 list_add(&req->rl_all, &buffer->rb_allreqs);
948 spin_unlock(&buffer->rb_reqslock);
949 req->rl_cqe.done = rpcrdma_wc_send;
950 req->rl_buffer = &r_xprt->rx_buf;
951 INIT_LIST_HEAD(&req->rl_registered);
952 req->rl_send_wr.next = NULL;
953 req->rl_send_wr.wr_cqe = &req->rl_cqe;
954 req->rl_send_wr.sg_list = req->rl_send_sge;
955 req->rl_send_wr.opcode = IB_WR_SEND;
960 * rpcrdma_create_rep - Allocate an rpcrdma_rep object
961 * @r_xprt: controlling transport
963 * Returns 0 on success or a negative errno on failure.
966 rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
968 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
969 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
970 struct rpcrdma_rep *rep;
974 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
978 rep->rr_rdmabuf = rpcrdma_alloc_regbuf(cdata->inline_rsize,
979 DMA_FROM_DEVICE, GFP_KERNEL);
980 if (IS_ERR(rep->rr_rdmabuf)) {
981 rc = PTR_ERR(rep->rr_rdmabuf);
984 xdr_buf_init(&rep->rr_hdrbuf, rep->rr_rdmabuf->rg_base,
985 rdmab_length(rep->rr_rdmabuf));
987 rep->rr_cqe.done = rpcrdma_wc_receive;
988 rep->rr_rxprt = r_xprt;
989 INIT_WORK(&rep->rr_work, rpcrdma_reply_handler);
990 rep->rr_recv_wr.next = NULL;
991 rep->rr_recv_wr.wr_cqe = &rep->rr_cqe;
992 rep->rr_recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
993 rep->rr_recv_wr.num_sge = 1;
995 spin_lock(&buf->rb_lock);
996 list_add(&rep->rr_list, &buf->rb_recv_bufs);
997 spin_unlock(&buf->rb_lock);
1003 dprintk("RPC: %s: reply buffer %d alloc failed\n",
1009 rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
1011 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1014 buf->rb_max_requests = r_xprt->rx_data.max_requests;
1015 buf->rb_bc_srv_max_requests = 0;
1016 atomic_set(&buf->rb_credits, 1);
1017 spin_lock_init(&buf->rb_mwlock);
1018 spin_lock_init(&buf->rb_lock);
1019 spin_lock_init(&buf->rb_recovery_lock);
1020 INIT_LIST_HEAD(&buf->rb_mws);
1021 INIT_LIST_HEAD(&buf->rb_all);
1022 INIT_LIST_HEAD(&buf->rb_stale_mrs);
1023 INIT_DELAYED_WORK(&buf->rb_refresh_worker,
1024 rpcrdma_mr_refresh_worker);
1025 INIT_DELAYED_WORK(&buf->rb_recovery_worker,
1026 rpcrdma_mr_recovery_worker);
1028 rpcrdma_create_mrs(r_xprt);
1030 INIT_LIST_HEAD(&buf->rb_send_bufs);
1031 INIT_LIST_HEAD(&buf->rb_allreqs);
1032 spin_lock_init(&buf->rb_reqslock);
1033 for (i = 0; i < buf->rb_max_requests; i++) {
1034 struct rpcrdma_req *req;
1036 req = rpcrdma_create_req(r_xprt);
1038 dprintk("RPC: %s: request buffer %d alloc"
1039 " failed\n", __func__, i);
1043 req->rl_backchannel = false;
1044 list_add(&req->rl_list, &buf->rb_send_bufs);
1047 INIT_LIST_HEAD(&buf->rb_recv_bufs);
1048 for (i = 0; i <= buf->rb_max_requests; i++) {
1049 rc = rpcrdma_create_rep(r_xprt);
1056 rpcrdma_buffer_destroy(buf);
1060 static struct rpcrdma_req *
1061 rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf)
1063 struct rpcrdma_req *req;
1065 req = list_first_entry(&buf->rb_send_bufs,
1066 struct rpcrdma_req, rl_list);
1067 list_del_init(&req->rl_list);
1071 static struct rpcrdma_rep *
1072 rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf)
1074 struct rpcrdma_rep *rep;
1076 rep = list_first_entry(&buf->rb_recv_bufs,
1077 struct rpcrdma_rep, rr_list);
1078 list_del(&rep->rr_list);
1083 rpcrdma_destroy_rep(struct rpcrdma_rep *rep)
1085 rpcrdma_free_regbuf(rep->rr_rdmabuf);
1090 rpcrdma_destroy_req(struct rpcrdma_req *req)
1092 rpcrdma_free_regbuf(req->rl_recvbuf);
1093 rpcrdma_free_regbuf(req->rl_sendbuf);
1094 rpcrdma_free_regbuf(req->rl_rdmabuf);
1099 rpcrdma_destroy_mrs(struct rpcrdma_buffer *buf)
1101 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
1103 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
1104 struct rpcrdma_mw *mw;
1108 spin_lock(&buf->rb_mwlock);
1109 while (!list_empty(&buf->rb_all)) {
1110 mw = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
1111 list_del(&mw->mw_all);
1113 spin_unlock(&buf->rb_mwlock);
1114 ia->ri_ops->ro_release_mr(mw);
1116 spin_lock(&buf->rb_mwlock);
1118 spin_unlock(&buf->rb_mwlock);
1119 r_xprt->rx_stats.mrs_allocated = 0;
1121 dprintk("RPC: %s: released %u MRs\n", __func__, count);
1125 rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1127 cancel_delayed_work_sync(&buf->rb_recovery_worker);
1128 cancel_delayed_work_sync(&buf->rb_refresh_worker);
1130 while (!list_empty(&buf->rb_recv_bufs)) {
1131 struct rpcrdma_rep *rep;
1133 rep = rpcrdma_buffer_get_rep_locked(buf);
1134 rpcrdma_destroy_rep(rep);
1136 buf->rb_send_count = 0;
1138 spin_lock(&buf->rb_reqslock);
1139 while (!list_empty(&buf->rb_allreqs)) {
1140 struct rpcrdma_req *req;
1142 req = list_first_entry(&buf->rb_allreqs,
1143 struct rpcrdma_req, rl_all);
1144 list_del(&req->rl_all);
1146 spin_unlock(&buf->rb_reqslock);
1147 rpcrdma_destroy_req(req);
1148 spin_lock(&buf->rb_reqslock);
1150 spin_unlock(&buf->rb_reqslock);
1151 buf->rb_recv_count = 0;
1153 rpcrdma_destroy_mrs(buf);
1157 rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt)
1159 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1160 struct rpcrdma_mw *mw = NULL;
1162 spin_lock(&buf->rb_mwlock);
1163 if (!list_empty(&buf->rb_mws))
1164 mw = rpcrdma_pop_mw(&buf->rb_mws);
1165 spin_unlock(&buf->rb_mwlock);
1173 dprintk("RPC: %s: no MWs available\n", __func__);
1174 if (r_xprt->rx_ep.rep_connected != -ENODEV)
1175 schedule_delayed_work(&buf->rb_refresh_worker, 0);
1177 /* Allow the reply handler and refresh worker to run */
1184 rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw)
1186 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1188 spin_lock(&buf->rb_mwlock);
1189 rpcrdma_push_mw(mw, &buf->rb_mws);
1190 spin_unlock(&buf->rb_mwlock);
1193 static struct rpcrdma_rep *
1194 rpcrdma_buffer_get_rep(struct rpcrdma_buffer *buffers)
1196 /* If an RPC previously completed without a reply (say, a
1197 * credential problem or a soft timeout occurs) then hold off
1198 * on supplying more Receive buffers until the number of new
1199 * pending RPCs catches up to the number of posted Receives.
1201 if (unlikely(buffers->rb_send_count < buffers->rb_recv_count))
1204 if (unlikely(list_empty(&buffers->rb_recv_bufs)))
1206 buffers->rb_recv_count++;
1207 return rpcrdma_buffer_get_rep_locked(buffers);
1211 * Get a set of request/reply buffers.
1213 * Reply buffer (if available) is attached to send buffer upon return.
1215 struct rpcrdma_req *
1216 rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1218 struct rpcrdma_req *req;
1220 spin_lock(&buffers->rb_lock);
1221 if (list_empty(&buffers->rb_send_bufs))
1223 buffers->rb_send_count++;
1224 req = rpcrdma_buffer_get_req_locked(buffers);
1225 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
1226 spin_unlock(&buffers->rb_lock);
1230 spin_unlock(&buffers->rb_lock);
1231 pr_warn("RPC: %s: out of request buffers\n", __func__);
1236 * Put request/reply buffers back into pool.
1237 * Pre-decrement counter/array index.
1240 rpcrdma_buffer_put(struct rpcrdma_req *req)
1242 struct rpcrdma_buffer *buffers = req->rl_buffer;
1243 struct rpcrdma_rep *rep = req->rl_reply;
1245 req->rl_send_wr.num_sge = 0;
1246 req->rl_reply = NULL;
1248 spin_lock(&buffers->rb_lock);
1249 buffers->rb_send_count--;
1250 list_add_tail(&req->rl_list, &buffers->rb_send_bufs);
1252 buffers->rb_recv_count--;
1253 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
1255 spin_unlock(&buffers->rb_lock);
1259 * Recover reply buffers from pool.
1260 * This happens when recovering from disconnect.
1263 rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1265 struct rpcrdma_buffer *buffers = req->rl_buffer;
1267 spin_lock(&buffers->rb_lock);
1268 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
1269 spin_unlock(&buffers->rb_lock);
1273 * Put reply buffers back into pool when not attached to
1274 * request. This happens in error conditions.
1277 rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1279 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
1281 spin_lock(&buffers->rb_lock);
1282 buffers->rb_recv_count--;
1283 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
1284 spin_unlock(&buffers->rb_lock);
1288 * rpcrdma_alloc_regbuf - allocate and DMA-map memory for SEND/RECV buffers
1289 * @size: size of buffer to be allocated, in bytes
1290 * @direction: direction of data movement
1293 * Returns an ERR_PTR, or a pointer to a regbuf, a buffer that
1294 * can be persistently DMA-mapped for I/O.
1296 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
1297 * receiving the payload of RDMA RECV operations. During Long Calls
1298 * or Replies they may be registered externally via ro_map.
1300 struct rpcrdma_regbuf *
1301 rpcrdma_alloc_regbuf(size_t size, enum dma_data_direction direction,
1304 struct rpcrdma_regbuf *rb;
1306 rb = kmalloc(sizeof(*rb) + size, flags);
1308 return ERR_PTR(-ENOMEM);
1310 rb->rg_device = NULL;
1311 rb->rg_direction = direction;
1312 rb->rg_iov.length = size;
1318 * __rpcrdma_map_regbuf - DMA-map a regbuf
1319 * @ia: controlling rpcrdma_ia
1320 * @rb: regbuf to be mapped
1323 __rpcrdma_dma_map_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1325 struct ib_device *device = ia->ri_device;
1327 if (rb->rg_direction == DMA_NONE)
1330 rb->rg_iov.addr = ib_dma_map_single(device,
1331 (void *)rb->rg_base,
1334 if (ib_dma_mapping_error(device, rdmab_addr(rb)))
1337 rb->rg_device = device;
1338 rb->rg_iov.lkey = ia->ri_pd->local_dma_lkey;
1343 rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb)
1348 if (!rpcrdma_regbuf_is_mapped(rb))
1351 ib_dma_unmap_single(rb->rg_device, rdmab_addr(rb),
1352 rdmab_length(rb), rb->rg_direction);
1353 rb->rg_device = NULL;
1357 * rpcrdma_free_regbuf - deregister and free registered buffer
1358 * @rb: regbuf to be deregistered and freed
1361 rpcrdma_free_regbuf(struct rpcrdma_regbuf *rb)
1363 rpcrdma_dma_unmap_regbuf(rb);
1368 * Prepost any receive buffer, then post send.
1370 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1373 rpcrdma_ep_post(struct rpcrdma_ia *ia,
1374 struct rpcrdma_ep *ep,
1375 struct rpcrdma_req *req)
1377 struct ib_send_wr *send_wr = &req->rl_send_wr;
1378 struct ib_send_wr *send_wr_fail;
1381 if (req->rl_reply) {
1382 rc = rpcrdma_ep_post_recv(ia, req->rl_reply);
1385 req->rl_reply = NULL;
1388 dprintk("RPC: %s: posting %d s/g entries\n",
1389 __func__, send_wr->num_sge);
1391 rpcrdma_set_signaled(ep, send_wr);
1392 rc = ib_post_send(ia->ri_id->qp, send_wr, &send_wr_fail);
1394 goto out_postsend_err;
1398 pr_err("rpcrdma: RDMA Send ib_post_send returned %i\n", rc);
1403 rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
1404 struct rpcrdma_rep *rep)
1406 struct ib_recv_wr *recv_wr_fail;
1409 if (!rpcrdma_dma_map_regbuf(ia, rep->rr_rdmabuf))
1411 rc = ib_post_recv(ia->ri_id->qp, &rep->rr_recv_wr, &recv_wr_fail);
1417 pr_err("rpcrdma: failed to DMA map the Receive buffer\n");
1421 pr_err("rpcrdma: ib_post_recv returned %i\n", rc);
1426 * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests
1427 * @r_xprt: transport associated with these backchannel resources
1428 * @min_reqs: minimum number of incoming requests expected
1430 * Returns zero if all requested buffers were posted, or a negative errno.
1433 rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt *r_xprt, unsigned int count)
1435 struct rpcrdma_buffer *buffers = &r_xprt->rx_buf;
1436 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
1437 struct rpcrdma_rep *rep;
1441 spin_lock(&buffers->rb_lock);
1442 if (list_empty(&buffers->rb_recv_bufs))
1444 rep = rpcrdma_buffer_get_rep_locked(buffers);
1445 spin_unlock(&buffers->rb_lock);
1447 rc = rpcrdma_ep_post_recv(ia, rep);
1455 spin_unlock(&buffers->rb_lock);
1456 pr_warn("%s: no extra receive buffers\n", __func__);
1460 rpcrdma_recv_buffer_put(rep);