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() */
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_destroy_mrs(struct rpcrdma_buffer *buf);
73 static void rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb);
75 static struct workqueue_struct *rpcrdma_receive_wq;
78 rpcrdma_alloc_wq(void)
80 struct workqueue_struct *recv_wq;
82 recv_wq = alloc_workqueue("xprtrdma_receive",
83 WQ_MEM_RECLAIM | WQ_UNBOUND | WQ_HIGHPRI,
88 rpcrdma_receive_wq = recv_wq;
93 rpcrdma_destroy_wq(void)
95 struct workqueue_struct *wq;
97 if (rpcrdma_receive_wq) {
98 wq = rpcrdma_receive_wq;
99 rpcrdma_receive_wq = NULL;
100 destroy_workqueue(wq);
105 rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context)
107 struct rpcrdma_ep *ep = context;
109 pr_err("rpcrdma: %s on device %s ep %p\n",
110 ib_event_msg(event->event), event->device->name, context);
112 if (ep->rep_connected == 1) {
113 ep->rep_connected = -EIO;
114 rpcrdma_conn_func(ep);
115 wake_up_all(&ep->rep_connect_wait);
120 * rpcrdma_wc_send - Invoked by RDMA provider for each polled Send WC
121 * @cq: completion queue (ignored)
126 rpcrdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
128 /* WARNING: Only wr_cqe and status are reliable at this point */
129 if (wc->status != IB_WC_SUCCESS && wc->status != IB_WC_WR_FLUSH_ERR)
130 pr_err("rpcrdma: Send: %s (%u/0x%x)\n",
131 ib_wc_status_msg(wc->status),
132 wc->status, wc->vendor_err);
135 /* Perform basic sanity checking to avoid using garbage
136 * to update the credit grant value.
139 rpcrdma_update_granted_credits(struct rpcrdma_rep *rep)
141 struct rpcrdma_msg *rmsgp = rdmab_to_msg(rep->rr_rdmabuf);
142 struct rpcrdma_buffer *buffer = &rep->rr_rxprt->rx_buf;
145 if (rep->rr_len < RPCRDMA_HDRLEN_ERR)
148 credits = be32_to_cpu(rmsgp->rm_credit);
150 credits = 1; /* don't deadlock */
151 else if (credits > buffer->rb_max_requests)
152 credits = buffer->rb_max_requests;
154 atomic_set(&buffer->rb_credits, credits);
158 * rpcrdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
159 * @cq: completion queue (ignored)
164 rpcrdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
166 struct ib_cqe *cqe = wc->wr_cqe;
167 struct rpcrdma_rep *rep = container_of(cqe, struct rpcrdma_rep,
170 /* WARNING: Only wr_id and status are reliable at this point */
171 if (wc->status != IB_WC_SUCCESS)
174 /* status == SUCCESS means all fields in wc are trustworthy */
175 if (wc->opcode != IB_WC_RECV)
178 dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
179 __func__, rep, wc->byte_len);
181 rep->rr_len = wc->byte_len;
182 rep->rr_wc_flags = wc->wc_flags;
183 rep->rr_inv_rkey = wc->ex.invalidate_rkey;
185 ib_dma_sync_single_for_cpu(rdmab_device(rep->rr_rdmabuf),
186 rdmab_addr(rep->rr_rdmabuf),
187 rep->rr_len, DMA_FROM_DEVICE);
189 rpcrdma_update_granted_credits(rep);
192 queue_work(rpcrdma_receive_wq, &rep->rr_work);
196 if (wc->status != IB_WC_WR_FLUSH_ERR)
197 pr_err("rpcrdma: Recv: %s (%u/0x%x)\n",
198 ib_wc_status_msg(wc->status),
199 wc->status, wc->vendor_err);
200 rep->rr_len = RPCRDMA_BAD_LEN;
205 rpcrdma_update_connect_private(struct rpcrdma_xprt *r_xprt,
206 struct rdma_conn_param *param)
208 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
209 const struct rpcrdma_connect_private *pmsg = param->private_data;
210 unsigned int rsize, wsize;
212 /* Default settings for RPC-over-RDMA Version One */
213 r_xprt->rx_ia.ri_reminv_expected = false;
214 r_xprt->rx_ia.ri_implicit_roundup = xprt_rdma_pad_optimize;
215 rsize = RPCRDMA_V1_DEF_INLINE_SIZE;
216 wsize = RPCRDMA_V1_DEF_INLINE_SIZE;
219 pmsg->cp_magic == rpcrdma_cmp_magic &&
220 pmsg->cp_version == RPCRDMA_CMP_VERSION) {
221 r_xprt->rx_ia.ri_reminv_expected = true;
222 r_xprt->rx_ia.ri_implicit_roundup = true;
223 rsize = rpcrdma_decode_buffer_size(pmsg->cp_send_size);
224 wsize = rpcrdma_decode_buffer_size(pmsg->cp_recv_size);
227 if (rsize < cdata->inline_rsize)
228 cdata->inline_rsize = rsize;
229 if (wsize < cdata->inline_wsize)
230 cdata->inline_wsize = wsize;
231 dprintk("RPC: %s: max send %u, max recv %u\n",
232 __func__, cdata->inline_wsize, cdata->inline_rsize);
233 rpcrdma_set_max_header_sizes(r_xprt);
237 rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
239 struct rpcrdma_xprt *xprt = id->context;
240 struct rpcrdma_ia *ia = &xprt->rx_ia;
241 struct rpcrdma_ep *ep = &xprt->rx_ep;
242 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
243 struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr;
245 struct ib_qp_attr *attr = &ia->ri_qp_attr;
246 struct ib_qp_init_attr *iattr = &ia->ri_qp_init_attr;
249 switch (event->event) {
250 case RDMA_CM_EVENT_ADDR_RESOLVED:
251 case RDMA_CM_EVENT_ROUTE_RESOLVED:
253 complete(&ia->ri_done);
255 case RDMA_CM_EVENT_ADDR_ERROR:
256 ia->ri_async_rc = -EHOSTUNREACH;
257 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
259 complete(&ia->ri_done);
261 case RDMA_CM_EVENT_ROUTE_ERROR:
262 ia->ri_async_rc = -ENETUNREACH;
263 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
265 complete(&ia->ri_done);
267 case RDMA_CM_EVENT_DEVICE_REMOVAL:
268 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
269 pr_info("rpcrdma: removing device for %pIS:%u\n",
270 sap, rpc_get_port(sap));
272 set_bit(RPCRDMA_IAF_REMOVING, &ia->ri_flags);
273 ep->rep_connected = -ENODEV;
274 xprt_force_disconnect(&xprt->rx_xprt);
275 wait_for_completion(&ia->ri_remove_done);
279 ia->ri_device = NULL;
280 /* Return 1 to ensure the core destroys the id. */
282 case RDMA_CM_EVENT_ESTABLISHED:
284 ib_query_qp(ia->ri_id->qp, attr,
285 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_MAX_DEST_RD_ATOMIC,
287 dprintk("RPC: %s: %d responder resources"
289 __func__, attr->max_dest_rd_atomic,
290 attr->max_rd_atomic);
291 rpcrdma_update_connect_private(xprt, &event->param.conn);
293 case RDMA_CM_EVENT_CONNECT_ERROR:
294 connstate = -ENOTCONN;
296 case RDMA_CM_EVENT_UNREACHABLE:
297 connstate = -ENETDOWN;
299 case RDMA_CM_EVENT_REJECTED:
300 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
301 pr_info("rpcrdma: connection to %pIS:%u on %s rejected: %s\n",
302 sap, rpc_get_port(sap), ia->ri_device->name,
303 rdma_reject_msg(id, event->status));
305 connstate = -ECONNREFUSED;
306 if (event->status == IB_CM_REJ_STALE_CONN)
309 case RDMA_CM_EVENT_DISCONNECTED:
310 connstate = -ECONNABORTED;
312 dprintk("RPC: %s: %sconnected\n",
313 __func__, connstate > 0 ? "" : "dis");
314 atomic_set(&xprt->rx_buf.rb_credits, 1);
315 ep->rep_connected = connstate;
316 rpcrdma_conn_func(ep);
317 wake_up_all(&ep->rep_connect_wait);
320 dprintk("RPC: %s: %pIS:%u (ep 0x%p): %s\n",
321 __func__, sap, rpc_get_port(sap), ep,
322 rdma_event_msg(event->event));
326 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
327 if (connstate == 1) {
328 int ird = attr->max_dest_rd_atomic;
329 int tird = ep->rep_remote_cma.responder_resources;
331 pr_info("rpcrdma: connection to %pIS:%u on %s, memreg '%s', %d credits, %d responders%s\n",
332 sap, rpc_get_port(sap),
334 ia->ri_ops->ro_displayname,
335 xprt->rx_buf.rb_max_requests,
336 ird, ird < 4 && ird < tird / 2 ? " (low!)" : "");
337 } else if (connstate < 0) {
338 pr_info("rpcrdma: connection to %pIS:%u closed (%d)\n",
339 sap, rpc_get_port(sap), connstate);
346 static void rpcrdma_destroy_id(struct rdma_cm_id *id)
349 module_put(id->device->owner);
354 static struct rdma_cm_id *
355 rpcrdma_create_id(struct rpcrdma_xprt *xprt,
356 struct rpcrdma_ia *ia, struct sockaddr *addr)
358 unsigned long wtimeout = msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1;
359 struct rdma_cm_id *id;
362 init_completion(&ia->ri_done);
363 init_completion(&ia->ri_remove_done);
365 id = rdma_create_id(&init_net, rpcrdma_conn_upcall, xprt, RDMA_PS_TCP,
369 dprintk("RPC: %s: rdma_create_id() failed %i\n",
374 ia->ri_async_rc = -ETIMEDOUT;
375 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
377 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
381 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
383 dprintk("RPC: %s: wait() exited: %i\n",
389 * Until xprtrdma supports DEVICE_REMOVAL, the provider must
390 * be pinned while there are active NFS/RDMA mounts to prevent
391 * hangs and crashes at umount time.
393 if (!ia->ri_async_rc && !try_module_get(id->device->owner)) {
394 dprintk("RPC: %s: Failed to get device module\n",
396 ia->ri_async_rc = -ENODEV;
398 rc = ia->ri_async_rc;
402 ia->ri_async_rc = -ETIMEDOUT;
403 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
405 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
409 rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout);
411 dprintk("RPC: %s: wait() exited: %i\n",
415 rc = ia->ri_async_rc;
421 module_put(id->device->owner);
428 * Exported functions.
432 * rpcrdma_ia_open - Open and initialize an Interface Adapter.
433 * @xprt: controlling transport
434 * @addr: IP address of remote peer
436 * Returns 0 on success, negative errno if an appropriate
437 * Interface Adapter could not be found and opened.
440 rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr)
442 struct rpcrdma_ia *ia = &xprt->rx_ia;
445 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
446 if (IS_ERR(ia->ri_id)) {
447 rc = PTR_ERR(ia->ri_id);
450 ia->ri_device = ia->ri_id->device;
452 ia->ri_pd = ib_alloc_pd(ia->ri_device, 0);
453 if (IS_ERR(ia->ri_pd)) {
454 rc = PTR_ERR(ia->ri_pd);
455 pr_err("rpcrdma: ib_alloc_pd() returned %d\n", rc);
459 switch (xprt_rdma_memreg_strategy) {
461 if (frwr_is_supported(ia)) {
462 ia->ri_ops = &rpcrdma_frwr_memreg_ops;
466 case RPCRDMA_MTHCAFMR:
467 if (fmr_is_supported(ia)) {
468 ia->ri_ops = &rpcrdma_fmr_memreg_ops;
473 pr_err("rpcrdma: Device %s does not support memreg mode %d\n",
474 ia->ri_device->name, xprt_rdma_memreg_strategy);
482 rpcrdma_ia_close(ia);
487 * rpcrdma_ia_remove - Handle device driver unload
488 * @ia: interface adapter being removed
490 * Divest transport H/W resources associated with this adapter,
491 * but allow it to be restored later.
494 rpcrdma_ia_remove(struct rpcrdma_ia *ia)
496 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
498 struct rpcrdma_ep *ep = &r_xprt->rx_ep;
499 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
500 struct rpcrdma_req *req;
501 struct rpcrdma_rep *rep;
503 cancel_delayed_work_sync(&buf->rb_refresh_worker);
505 /* This is similar to rpcrdma_ep_destroy, but:
506 * - Don't cancel the connect worker.
507 * - Don't call rpcrdma_ep_disconnect, which waits
508 * for another conn upcall, which will deadlock.
509 * - rdma_disconnect is unneeded, the underlying
510 * connection is already gone.
513 ib_drain_qp(ia->ri_id->qp);
514 rdma_destroy_qp(ia->ri_id);
515 ia->ri_id->qp = NULL;
517 ib_free_cq(ep->rep_attr.recv_cq);
518 ib_free_cq(ep->rep_attr.send_cq);
520 /* The ULP is responsible for ensuring all DMA
521 * mappings and MRs are gone.
523 list_for_each_entry(rep, &buf->rb_recv_bufs, rr_list)
524 rpcrdma_dma_unmap_regbuf(rep->rr_rdmabuf);
525 list_for_each_entry(req, &buf->rb_allreqs, rl_all) {
526 rpcrdma_dma_unmap_regbuf(req->rl_rdmabuf);
527 rpcrdma_dma_unmap_regbuf(req->rl_sendbuf);
528 rpcrdma_dma_unmap_regbuf(req->rl_recvbuf);
530 rpcrdma_destroy_mrs(buf);
532 /* Allow waiters to continue */
533 complete(&ia->ri_remove_done);
537 * rpcrdma_ia_close - Clean up/close an IA.
538 * @ia: interface adapter to close
542 rpcrdma_ia_close(struct rpcrdma_ia *ia)
544 dprintk("RPC: %s: entering\n", __func__);
545 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
547 rdma_destroy_qp(ia->ri_id);
548 rpcrdma_destroy_id(ia->ri_id);
551 ia->ri_device = NULL;
553 /* If the pd is still busy, xprtrdma missed freeing a resource */
554 if (ia->ri_pd && !IS_ERR(ia->ri_pd))
555 ib_dealloc_pd(ia->ri_pd);
560 * Create unconnected endpoint.
563 rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
564 struct rpcrdma_create_data_internal *cdata)
566 struct rpcrdma_connect_private *pmsg = &ep->rep_cm_private;
567 unsigned int max_qp_wr, max_sge;
568 struct ib_cq *sendcq, *recvcq;
571 max_sge = min_t(unsigned int, ia->ri_device->attrs.max_sge,
572 RPCRDMA_MAX_SEND_SGES);
573 if (max_sge < RPCRDMA_MIN_SEND_SGES) {
574 pr_warn("rpcrdma: HCA provides only %d send SGEs\n", max_sge);
577 ia->ri_max_send_sges = max_sge - RPCRDMA_MIN_SEND_SGES;
579 if (ia->ri_device->attrs.max_qp_wr <= RPCRDMA_BACKWARD_WRS) {
580 dprintk("RPC: %s: insufficient wqe's available\n",
584 max_qp_wr = ia->ri_device->attrs.max_qp_wr - RPCRDMA_BACKWARD_WRS - 1;
586 /* check provider's send/recv wr limits */
587 if (cdata->max_requests > max_qp_wr)
588 cdata->max_requests = max_qp_wr;
590 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
591 ep->rep_attr.qp_context = ep;
592 ep->rep_attr.srq = NULL;
593 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
594 ep->rep_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS;
595 ep->rep_attr.cap.max_send_wr += 1; /* drain cqe */
596 rc = ia->ri_ops->ro_open(ia, ep, cdata);
599 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
600 ep->rep_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS;
601 ep->rep_attr.cap.max_recv_wr += 1; /* drain cqe */
602 ep->rep_attr.cap.max_send_sge = max_sge;
603 ep->rep_attr.cap.max_recv_sge = 1;
604 ep->rep_attr.cap.max_inline_data = 0;
605 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
606 ep->rep_attr.qp_type = IB_QPT_RC;
607 ep->rep_attr.port_num = ~0;
609 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
610 "iovs: send %d recv %d\n",
612 ep->rep_attr.cap.max_send_wr,
613 ep->rep_attr.cap.max_recv_wr,
614 ep->rep_attr.cap.max_send_sge,
615 ep->rep_attr.cap.max_recv_sge);
617 /* set trigger for requesting send completion */
618 ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
619 if (ep->rep_cqinit <= 2)
620 ep->rep_cqinit = 0; /* always signal? */
621 rpcrdma_init_cqcount(ep, 0);
622 init_waitqueue_head(&ep->rep_connect_wait);
623 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
625 sendcq = ib_alloc_cq(ia->ri_device, NULL,
626 ep->rep_attr.cap.max_send_wr + 1,
628 if (IS_ERR(sendcq)) {
629 rc = PTR_ERR(sendcq);
630 dprintk("RPC: %s: failed to create send CQ: %i\n",
635 recvcq = ib_alloc_cq(ia->ri_device, NULL,
636 ep->rep_attr.cap.max_recv_wr + 1,
638 if (IS_ERR(recvcq)) {
639 rc = PTR_ERR(recvcq);
640 dprintk("RPC: %s: failed to create recv CQ: %i\n",
645 ep->rep_attr.send_cq = sendcq;
646 ep->rep_attr.recv_cq = recvcq;
648 /* Initialize cma parameters */
649 memset(&ep->rep_remote_cma, 0, sizeof(ep->rep_remote_cma));
651 /* Prepare RDMA-CM private message */
652 pmsg->cp_magic = rpcrdma_cmp_magic;
653 pmsg->cp_version = RPCRDMA_CMP_VERSION;
654 pmsg->cp_flags |= ia->ri_ops->ro_send_w_inv_ok;
655 pmsg->cp_send_size = rpcrdma_encode_buffer_size(cdata->inline_wsize);
656 pmsg->cp_recv_size = rpcrdma_encode_buffer_size(cdata->inline_rsize);
657 ep->rep_remote_cma.private_data = pmsg;
658 ep->rep_remote_cma.private_data_len = sizeof(*pmsg);
660 /* Client offers RDMA Read but does not initiate */
661 ep->rep_remote_cma.initiator_depth = 0;
662 if (ia->ri_device->attrs.max_qp_rd_atom > 32) /* arbitrary but <= 255 */
663 ep->rep_remote_cma.responder_resources = 32;
665 ep->rep_remote_cma.responder_resources =
666 ia->ri_device->attrs.max_qp_rd_atom;
668 /* Limit transport retries so client can detect server
669 * GID changes quickly. RPC layer handles re-establishing
670 * transport connection and retransmission.
672 ep->rep_remote_cma.retry_count = 6;
674 /* RPC-over-RDMA handles its own flow control. In addition,
675 * make all RNR NAKs visible so we know that RPC-over-RDMA
676 * flow control is working correctly (no NAKs should be seen).
678 ep->rep_remote_cma.flow_control = 0;
679 ep->rep_remote_cma.rnr_retry_count = 0;
692 * Disconnect and destroy endpoint. After this, the only
693 * valid operations on the ep are to free it (if dynamically
694 * allocated) or re-create it.
697 rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
699 dprintk("RPC: %s: entering, connected is %d\n",
700 __func__, ep->rep_connected);
702 cancel_delayed_work_sync(&ep->rep_connect_worker);
705 rpcrdma_ep_disconnect(ep, ia);
706 rdma_destroy_qp(ia->ri_id);
707 ia->ri_id->qp = NULL;
710 ib_free_cq(ep->rep_attr.recv_cq);
711 ib_free_cq(ep->rep_attr.send_cq);
715 rpcrdma_ep_reconnect(struct rpcrdma_xprt *r_xprt, struct rpcrdma_ep *ep,
716 struct rpcrdma_ia *ia)
718 struct sockaddr *sap = (struct sockaddr *)&r_xprt->rx_data.addr;
719 struct rdma_cm_id *id, *old;
722 dprintk("RPC: %s: reconnecting...\n", __func__);
724 rpcrdma_ep_disconnect(ep, ia);
727 id = rpcrdma_create_id(r_xprt, ia, sap);
731 /* As long as the new ID points to the same device as the
732 * old ID, we can reuse the transport's existing PD and all
733 * previously allocated MRs. Also, the same device means
734 * the transport's previous DMA mappings are still valid.
736 * This is a sanity check only. There should be no way these
737 * point to two different devices here.
741 if (ia->ri_device != id->device) {
742 pr_err("rpcrdma: can't reconnect on different device!\n");
746 err = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
748 dprintk("RPC: %s: rdma_create_qp returned %d\n",
753 /* Atomically replace the transport's ID and QP. */
757 rdma_destroy_qp(old);
760 rpcrdma_destroy_id(old);
766 * Connect unconnected endpoint.
769 rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
771 struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt,
777 switch (ep->rep_connected) {
779 dprintk("RPC: %s: connecting...\n", __func__);
780 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
782 dprintk("RPC: %s: rdma_create_qp failed %i\n",
789 rc = rpcrdma_ep_reconnect(r_xprt, ep, ia);
794 ep->rep_connected = 0;
796 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
798 dprintk("RPC: %s: rdma_connect() failed with %i\n",
803 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
804 if (ep->rep_connected <= 0) {
805 if (ep->rep_connected == -EAGAIN)
807 rc = ep->rep_connected;
811 dprintk("RPC: %s: connected\n", __func__);
812 extras = r_xprt->rx_buf.rb_bc_srv_max_requests;
814 rpcrdma_ep_post_extra_recv(r_xprt, extras);
818 ep->rep_connected = rc;
825 * rpcrdma_ep_disconnect
827 * This is separate from destroy to facilitate the ability
828 * to reconnect without recreating the endpoint.
830 * This call is not reentrant, and must not be made in parallel
831 * on the same endpoint.
834 rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
838 rc = rdma_disconnect(ia->ri_id);
840 /* returns without wait if not connected */
841 wait_event_interruptible(ep->rep_connect_wait,
842 ep->rep_connected != 1);
843 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
844 (ep->rep_connected == 1) ? "still " : "dis");
846 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
847 ep->rep_connected = rc;
850 ib_drain_qp(ia->ri_id->qp);
854 rpcrdma_mr_recovery_worker(struct work_struct *work)
856 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
857 rb_recovery_worker.work);
858 struct rpcrdma_mw *mw;
860 spin_lock(&buf->rb_recovery_lock);
861 while (!list_empty(&buf->rb_stale_mrs)) {
862 mw = rpcrdma_pop_mw(&buf->rb_stale_mrs);
863 spin_unlock(&buf->rb_recovery_lock);
865 dprintk("RPC: %s: recovering MR %p\n", __func__, mw);
866 mw->mw_xprt->rx_ia.ri_ops->ro_recover_mr(mw);
868 spin_lock(&buf->rb_recovery_lock);
870 spin_unlock(&buf->rb_recovery_lock);
874 rpcrdma_defer_mr_recovery(struct rpcrdma_mw *mw)
876 struct rpcrdma_xprt *r_xprt = mw->mw_xprt;
877 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
879 spin_lock(&buf->rb_recovery_lock);
880 rpcrdma_push_mw(mw, &buf->rb_stale_mrs);
881 spin_unlock(&buf->rb_recovery_lock);
883 schedule_delayed_work(&buf->rb_recovery_worker, 0);
887 rpcrdma_create_mrs(struct rpcrdma_xprt *r_xprt)
889 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
890 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
895 for (count = 0; count < 32; count++) {
896 struct rpcrdma_mw *mw;
899 mw = kzalloc(sizeof(*mw), GFP_KERNEL);
903 rc = ia->ri_ops->ro_init_mr(ia, mw);
909 mw->mw_xprt = r_xprt;
911 list_add(&mw->mw_list, &free);
912 list_add(&mw->mw_all, &all);
915 spin_lock(&buf->rb_mwlock);
916 list_splice(&free, &buf->rb_mws);
917 list_splice(&all, &buf->rb_all);
918 r_xprt->rx_stats.mrs_allocated += count;
919 spin_unlock(&buf->rb_mwlock);
921 dprintk("RPC: %s: created %u MRs\n", __func__, count);
925 rpcrdma_mr_refresh_worker(struct work_struct *work)
927 struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer,
928 rb_refresh_worker.work);
929 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
932 rpcrdma_create_mrs(r_xprt);
936 rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
938 struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
939 struct rpcrdma_req *req;
941 req = kzalloc(sizeof(*req), GFP_KERNEL);
943 return ERR_PTR(-ENOMEM);
945 INIT_LIST_HEAD(&req->rl_free);
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(struct rpcrdma_xprt *r_xprt)
962 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
963 struct rpcrdma_rep *rep;
967 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
971 rep->rr_rdmabuf = rpcrdma_alloc_regbuf(cdata->inline_rsize,
972 DMA_FROM_DEVICE, GFP_KERNEL);
973 if (IS_ERR(rep->rr_rdmabuf)) {
974 rc = PTR_ERR(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_stale_mrs);
1008 INIT_DELAYED_WORK(&buf->rb_refresh_worker,
1009 rpcrdma_mr_refresh_worker);
1010 INIT_DELAYED_WORK(&buf->rb_recovery_worker,
1011 rpcrdma_mr_recovery_worker);
1013 rpcrdma_create_mrs(r_xprt);
1015 INIT_LIST_HEAD(&buf->rb_send_bufs);
1016 INIT_LIST_HEAD(&buf->rb_allreqs);
1017 spin_lock_init(&buf->rb_reqslock);
1018 for (i = 0; i < buf->rb_max_requests; i++) {
1019 struct rpcrdma_req *req;
1021 req = rpcrdma_create_req(r_xprt);
1023 dprintk("RPC: %s: request buffer %d alloc"
1024 " failed\n", __func__, i);
1028 req->rl_backchannel = false;
1029 list_add(&req->rl_free, &buf->rb_send_bufs);
1032 INIT_LIST_HEAD(&buf->rb_recv_bufs);
1033 for (i = 0; i < buf->rb_max_requests + RPCRDMA_MAX_BC_REQUESTS; i++) {
1034 struct rpcrdma_rep *rep;
1036 rep = rpcrdma_create_rep(r_xprt);
1038 dprintk("RPC: %s: reply buffer %d alloc failed\n",
1043 list_add(&rep->rr_list, &buf->rb_recv_bufs);
1048 rpcrdma_buffer_destroy(buf);
1052 static struct rpcrdma_req *
1053 rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf)
1055 struct rpcrdma_req *req;
1057 req = list_first_entry(&buf->rb_send_bufs,
1058 struct rpcrdma_req, rl_free);
1059 list_del(&req->rl_free);
1063 static struct rpcrdma_rep *
1064 rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf)
1066 struct rpcrdma_rep *rep;
1068 rep = list_first_entry(&buf->rb_recv_bufs,
1069 struct rpcrdma_rep, rr_list);
1070 list_del(&rep->rr_list);
1075 rpcrdma_destroy_rep(struct rpcrdma_rep *rep)
1077 rpcrdma_free_regbuf(rep->rr_rdmabuf);
1082 rpcrdma_destroy_req(struct rpcrdma_req *req)
1084 rpcrdma_free_regbuf(req->rl_recvbuf);
1085 rpcrdma_free_regbuf(req->rl_sendbuf);
1086 rpcrdma_free_regbuf(req->rl_rdmabuf);
1091 rpcrdma_destroy_mrs(struct rpcrdma_buffer *buf)
1093 struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt,
1095 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
1096 struct rpcrdma_mw *mw;
1100 spin_lock(&buf->rb_mwlock);
1101 while (!list_empty(&buf->rb_all)) {
1102 mw = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
1103 list_del(&mw->mw_all);
1105 spin_unlock(&buf->rb_mwlock);
1106 ia->ri_ops->ro_release_mr(mw);
1108 spin_lock(&buf->rb_mwlock);
1110 spin_unlock(&buf->rb_mwlock);
1111 r_xprt->rx_stats.mrs_allocated = 0;
1113 dprintk("RPC: %s: released %u MRs\n", __func__, count);
1117 rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1119 cancel_delayed_work_sync(&buf->rb_recovery_worker);
1120 cancel_delayed_work_sync(&buf->rb_refresh_worker);
1122 while (!list_empty(&buf->rb_recv_bufs)) {
1123 struct rpcrdma_rep *rep;
1125 rep = rpcrdma_buffer_get_rep_locked(buf);
1126 rpcrdma_destroy_rep(rep);
1128 buf->rb_send_count = 0;
1130 spin_lock(&buf->rb_reqslock);
1131 while (!list_empty(&buf->rb_allreqs)) {
1132 struct rpcrdma_req *req;
1134 req = list_first_entry(&buf->rb_allreqs,
1135 struct rpcrdma_req, rl_all);
1136 list_del(&req->rl_all);
1138 spin_unlock(&buf->rb_reqslock);
1139 rpcrdma_destroy_req(req);
1140 spin_lock(&buf->rb_reqslock);
1142 spin_unlock(&buf->rb_reqslock);
1143 buf->rb_recv_count = 0;
1145 rpcrdma_destroy_mrs(buf);
1149 rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt)
1151 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1152 struct rpcrdma_mw *mw = NULL;
1154 spin_lock(&buf->rb_mwlock);
1155 if (!list_empty(&buf->rb_mws))
1156 mw = rpcrdma_pop_mw(&buf->rb_mws);
1157 spin_unlock(&buf->rb_mwlock);
1164 dprintk("RPC: %s: no MWs available\n", __func__);
1165 if (r_xprt->rx_ep.rep_connected != -ENODEV)
1166 schedule_delayed_work(&buf->rb_refresh_worker, 0);
1168 /* Allow the reply handler and refresh worker to run */
1175 rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw)
1177 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1179 spin_lock(&buf->rb_mwlock);
1180 rpcrdma_push_mw(mw, &buf->rb_mws);
1181 spin_unlock(&buf->rb_mwlock);
1184 static struct rpcrdma_rep *
1185 rpcrdma_buffer_get_rep(struct rpcrdma_buffer *buffers)
1187 /* If an RPC previously completed without a reply (say, a
1188 * credential problem or a soft timeout occurs) then hold off
1189 * on supplying more Receive buffers until the number of new
1190 * pending RPCs catches up to the number of posted Receives.
1192 if (unlikely(buffers->rb_send_count < buffers->rb_recv_count))
1195 if (unlikely(list_empty(&buffers->rb_recv_bufs)))
1197 buffers->rb_recv_count++;
1198 return rpcrdma_buffer_get_rep_locked(buffers);
1202 * Get a set of request/reply buffers.
1204 * Reply buffer (if available) is attached to send buffer upon return.
1206 struct rpcrdma_req *
1207 rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1209 struct rpcrdma_req *req;
1211 spin_lock(&buffers->rb_lock);
1212 if (list_empty(&buffers->rb_send_bufs))
1214 buffers->rb_send_count++;
1215 req = rpcrdma_buffer_get_req_locked(buffers);
1216 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
1217 spin_unlock(&buffers->rb_lock);
1221 spin_unlock(&buffers->rb_lock);
1222 pr_warn("RPC: %s: out of request buffers\n", __func__);
1227 * Put request/reply buffers back into pool.
1228 * Pre-decrement counter/array index.
1231 rpcrdma_buffer_put(struct rpcrdma_req *req)
1233 struct rpcrdma_buffer *buffers = req->rl_buffer;
1234 struct rpcrdma_rep *rep = req->rl_reply;
1236 req->rl_send_wr.num_sge = 0;
1237 req->rl_reply = NULL;
1239 spin_lock(&buffers->rb_lock);
1240 buffers->rb_send_count--;
1241 list_add_tail(&req->rl_free, &buffers->rb_send_bufs);
1243 buffers->rb_recv_count--;
1244 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
1246 spin_unlock(&buffers->rb_lock);
1250 * Recover reply buffers from pool.
1251 * This happens when recovering from disconnect.
1254 rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1256 struct rpcrdma_buffer *buffers = req->rl_buffer;
1258 spin_lock(&buffers->rb_lock);
1259 req->rl_reply = rpcrdma_buffer_get_rep(buffers);
1260 spin_unlock(&buffers->rb_lock);
1264 * Put reply buffers back into pool when not attached to
1265 * request. This happens in error conditions.
1268 rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1270 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
1272 spin_lock(&buffers->rb_lock);
1273 buffers->rb_recv_count--;
1274 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
1275 spin_unlock(&buffers->rb_lock);
1279 * rpcrdma_alloc_regbuf - allocate and DMA-map memory for SEND/RECV buffers
1280 * @size: size of buffer to be allocated, in bytes
1281 * @direction: direction of data movement
1284 * Returns an ERR_PTR, or a pointer to a regbuf, a buffer that
1285 * can be persistently DMA-mapped for I/O.
1287 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
1288 * receiving the payload of RDMA RECV operations. During Long Calls
1289 * or Replies they may be registered externally via ro_map.
1291 struct rpcrdma_regbuf *
1292 rpcrdma_alloc_regbuf(size_t size, enum dma_data_direction direction,
1295 struct rpcrdma_regbuf *rb;
1297 rb = kmalloc(sizeof(*rb) + size, flags);
1299 return ERR_PTR(-ENOMEM);
1301 rb->rg_device = NULL;
1302 rb->rg_direction = direction;
1303 rb->rg_iov.length = size;
1309 * __rpcrdma_map_regbuf - DMA-map a regbuf
1310 * @ia: controlling rpcrdma_ia
1311 * @rb: regbuf to be mapped
1314 __rpcrdma_dma_map_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1316 struct ib_device *device = ia->ri_device;
1318 if (rb->rg_direction == DMA_NONE)
1321 rb->rg_iov.addr = ib_dma_map_single(device,
1322 (void *)rb->rg_base,
1325 if (ib_dma_mapping_error(device, rdmab_addr(rb)))
1328 rb->rg_device = device;
1329 rb->rg_iov.lkey = ia->ri_pd->local_dma_lkey;
1334 rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb)
1336 if (!rpcrdma_regbuf_is_mapped(rb))
1339 ib_dma_unmap_single(rb->rg_device, rdmab_addr(rb),
1340 rdmab_length(rb), rb->rg_direction);
1341 rb->rg_device = NULL;
1345 * rpcrdma_free_regbuf - deregister and free registered buffer
1346 * @rb: regbuf to be deregistered and freed
1349 rpcrdma_free_regbuf(struct rpcrdma_regbuf *rb)
1354 rpcrdma_dma_unmap_regbuf(rb);
1359 * Prepost any receive buffer, then post send.
1361 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1364 rpcrdma_ep_post(struct rpcrdma_ia *ia,
1365 struct rpcrdma_ep *ep,
1366 struct rpcrdma_req *req)
1368 struct ib_send_wr *send_wr = &req->rl_send_wr;
1369 struct ib_send_wr *send_wr_fail;
1372 if (req->rl_reply) {
1373 rc = rpcrdma_ep_post_recv(ia, req->rl_reply);
1376 req->rl_reply = NULL;
1379 dprintk("RPC: %s: posting %d s/g entries\n",
1380 __func__, send_wr->num_sge);
1382 rpcrdma_set_signaled(ep, send_wr);
1383 rc = ib_post_send(ia->ri_id->qp, send_wr, &send_wr_fail);
1385 goto out_postsend_err;
1389 pr_err("rpcrdma: RDMA Send ib_post_send returned %i\n", rc);
1394 rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
1395 struct rpcrdma_rep *rep)
1397 struct ib_recv_wr *recv_wr_fail;
1400 if (!rpcrdma_dma_map_regbuf(ia, rep->rr_rdmabuf))
1402 rc = ib_post_recv(ia->ri_id->qp, &rep->rr_recv_wr, &recv_wr_fail);
1408 pr_err("rpcrdma: failed to DMA map the Receive buffer\n");
1412 pr_err("rpcrdma: ib_post_recv returned %i\n", rc);
1417 * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests
1418 * @r_xprt: transport associated with these backchannel resources
1419 * @min_reqs: minimum number of incoming requests expected
1421 * Returns zero if all requested buffers were posted, or a negative errno.
1424 rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt *r_xprt, unsigned int count)
1426 struct rpcrdma_buffer *buffers = &r_xprt->rx_buf;
1427 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
1428 struct rpcrdma_rep *rep;
1432 spin_lock(&buffers->rb_lock);
1433 if (list_empty(&buffers->rb_recv_bufs))
1435 rep = rpcrdma_buffer_get_rep_locked(buffers);
1436 spin_unlock(&buffers->rb_lock);
1438 rc = rpcrdma_ep_post_recv(ia, rep);
1446 spin_unlock(&buffers->rb_lock);
1447 pr_warn("%s: no extra receive buffers\n", __func__);
1451 rpcrdma_recv_buffer_put(rep);