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 <asm/bitops.h>
55 #include <linux/module.h> /* try_module_get()/module_put() */
57 #include "xprt_rdma.h"
63 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
64 # define RPCDBG_FACILITY RPCDBG_TRANS
71 static struct workqueue_struct *rpcrdma_receive_wq;
74 rpcrdma_alloc_wq(void)
76 struct workqueue_struct *recv_wq;
78 recv_wq = alloc_workqueue("xprtrdma_receive",
79 WQ_MEM_RECLAIM | WQ_UNBOUND | WQ_HIGHPRI,
84 rpcrdma_receive_wq = recv_wq;
89 rpcrdma_destroy_wq(void)
91 struct workqueue_struct *wq;
93 if (rpcrdma_receive_wq) {
94 wq = rpcrdma_receive_wq;
95 rpcrdma_receive_wq = NULL;
96 destroy_workqueue(wq);
101 rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context)
103 struct rpcrdma_ep *ep = context;
105 pr_err("RPC: %s: %s on device %s ep %p\n",
106 __func__, ib_event_msg(event->event),
107 event->device->name, context);
108 if (ep->rep_connected == 1) {
109 ep->rep_connected = -EIO;
110 rpcrdma_conn_func(ep);
111 wake_up_all(&ep->rep_connect_wait);
116 rpcrdma_cq_async_error_upcall(struct ib_event *event, void *context)
118 struct rpcrdma_ep *ep = context;
120 pr_err("RPC: %s: %s on device %s ep %p\n",
121 __func__, ib_event_msg(event->event),
122 event->device->name, context);
123 if (ep->rep_connected == 1) {
124 ep->rep_connected = -EIO;
125 rpcrdma_conn_func(ep);
126 wake_up_all(&ep->rep_connect_wait);
131 rpcrdma_sendcq_process_wc(struct ib_wc *wc)
133 /* WARNING: Only wr_id and status are reliable at this point */
134 if (wc->wr_id == RPCRDMA_IGNORE_COMPLETION) {
135 if (wc->status != IB_WC_SUCCESS &&
136 wc->status != IB_WC_WR_FLUSH_ERR)
137 pr_err("RPC: %s: SEND: %s\n",
138 __func__, ib_wc_status_msg(wc->status));
140 struct rpcrdma_mw *r;
142 r = (struct rpcrdma_mw *)(unsigned long)wc->wr_id;
143 r->mw_sendcompletion(wc);
147 /* The common case is a single send completion is waiting. By
148 * passing two WC entries to ib_poll_cq, a return code of 1
149 * means there is exactly one WC waiting and no more. We don't
150 * have to invoke ib_poll_cq again to know that the CQ has been
154 rpcrdma_sendcq_poll(struct ib_cq *cq)
156 struct ib_wc *pos, wcs[2];
162 rc = ib_poll_cq(cq, ARRAY_SIZE(wcs), pos);
168 rpcrdma_sendcq_process_wc(pos++);
169 } while (rc == ARRAY_SIZE(wcs));
173 /* Handle provider send completion upcalls.
176 rpcrdma_sendcq_upcall(struct ib_cq *cq, void *cq_context)
179 rpcrdma_sendcq_poll(cq);
180 } while (ib_req_notify_cq(cq, IB_CQ_NEXT_COMP |
181 IB_CQ_REPORT_MISSED_EVENTS) > 0);
185 rpcrdma_receive_worker(struct work_struct *work)
187 struct rpcrdma_rep *rep =
188 container_of(work, struct rpcrdma_rep, rr_work);
190 rpcrdma_reply_handler(rep);
194 rpcrdma_recvcq_process_wc(struct ib_wc *wc)
196 struct rpcrdma_rep *rep =
197 (struct rpcrdma_rep *)(unsigned long)wc->wr_id;
199 /* WARNING: Only wr_id and status are reliable at this point */
200 if (wc->status != IB_WC_SUCCESS)
203 /* status == SUCCESS means all fields in wc are trustworthy */
204 if (wc->opcode != IB_WC_RECV)
207 dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
208 __func__, rep, wc->byte_len);
210 rep->rr_len = wc->byte_len;
211 ib_dma_sync_single_for_cpu(rep->rr_device,
212 rdmab_addr(rep->rr_rdmabuf),
213 rep->rr_len, DMA_FROM_DEVICE);
214 prefetch(rdmab_to_msg(rep->rr_rdmabuf));
217 queue_work(rpcrdma_receive_wq, &rep->rr_work);
221 if (wc->status != IB_WC_WR_FLUSH_ERR)
222 pr_err("RPC: %s: rep %p: %s\n",
223 __func__, rep, ib_wc_status_msg(wc->status));
224 rep->rr_len = RPCRDMA_BAD_LEN;
228 /* The wc array is on stack: automatic memory is always CPU-local.
230 * struct ib_wc is 64 bytes, making the poll array potentially
231 * large. But this is at the bottom of the call chain. Further
232 * substantial work is done in another thread.
235 rpcrdma_recvcq_poll(struct ib_cq *cq)
237 struct ib_wc *pos, wcs[4];
243 rc = ib_poll_cq(cq, ARRAY_SIZE(wcs), pos);
249 rpcrdma_recvcq_process_wc(pos++);
250 } while (rc == ARRAY_SIZE(wcs));
253 /* Handle provider receive completion upcalls.
256 rpcrdma_recvcq_upcall(struct ib_cq *cq, void *cq_context)
259 rpcrdma_recvcq_poll(cq);
260 } while (ib_req_notify_cq(cq, IB_CQ_NEXT_COMP |
261 IB_CQ_REPORT_MISSED_EVENTS) > 0);
265 rpcrdma_flush_cqs(struct rpcrdma_ep *ep)
269 while (ib_poll_cq(ep->rep_attr.recv_cq, 1, &wc) > 0)
270 rpcrdma_recvcq_process_wc(&wc);
271 while (ib_poll_cq(ep->rep_attr.send_cq, 1, &wc) > 0)
272 rpcrdma_sendcq_process_wc(&wc);
276 rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
278 struct rpcrdma_xprt *xprt = id->context;
279 struct rpcrdma_ia *ia = &xprt->rx_ia;
280 struct rpcrdma_ep *ep = &xprt->rx_ep;
281 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
282 struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr;
284 struct ib_qp_attr *attr = &ia->ri_qp_attr;
285 struct ib_qp_init_attr *iattr = &ia->ri_qp_init_attr;
288 switch (event->event) {
289 case RDMA_CM_EVENT_ADDR_RESOLVED:
290 case RDMA_CM_EVENT_ROUTE_RESOLVED:
292 complete(&ia->ri_done);
294 case RDMA_CM_EVENT_ADDR_ERROR:
295 ia->ri_async_rc = -EHOSTUNREACH;
296 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
298 complete(&ia->ri_done);
300 case RDMA_CM_EVENT_ROUTE_ERROR:
301 ia->ri_async_rc = -ENETUNREACH;
302 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
304 complete(&ia->ri_done);
306 case RDMA_CM_EVENT_ESTABLISHED:
308 ib_query_qp(ia->ri_id->qp, attr,
309 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_MAX_DEST_RD_ATOMIC,
311 dprintk("RPC: %s: %d responder resources"
313 __func__, attr->max_dest_rd_atomic,
314 attr->max_rd_atomic);
316 case RDMA_CM_EVENT_CONNECT_ERROR:
317 connstate = -ENOTCONN;
319 case RDMA_CM_EVENT_UNREACHABLE:
320 connstate = -ENETDOWN;
322 case RDMA_CM_EVENT_REJECTED:
323 connstate = -ECONNREFUSED;
325 case RDMA_CM_EVENT_DISCONNECTED:
326 connstate = -ECONNABORTED;
328 case RDMA_CM_EVENT_DEVICE_REMOVAL:
331 dprintk("RPC: %s: %sconnected\n",
332 __func__, connstate > 0 ? "" : "dis");
333 ep->rep_connected = connstate;
334 rpcrdma_conn_func(ep);
335 wake_up_all(&ep->rep_connect_wait);
338 dprintk("RPC: %s: %pIS:%u (ep 0x%p): %s\n",
339 __func__, sap, rpc_get_port(sap), ep,
340 rdma_event_msg(event->event));
344 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
345 if (connstate == 1) {
346 int ird = attr->max_dest_rd_atomic;
347 int tird = ep->rep_remote_cma.responder_resources;
349 pr_info("rpcrdma: connection to %pIS:%u on %s, memreg '%s', %d credits, %d responders%s\n",
350 sap, rpc_get_port(sap),
352 ia->ri_ops->ro_displayname,
353 xprt->rx_buf.rb_max_requests,
354 ird, ird < 4 && ird < tird / 2 ? " (low!)" : "");
355 } else if (connstate < 0) {
356 pr_info("rpcrdma: connection to %pIS:%u closed (%d)\n",
357 sap, rpc_get_port(sap), connstate);
364 static void rpcrdma_destroy_id(struct rdma_cm_id *id)
367 module_put(id->device->owner);
372 static struct rdma_cm_id *
373 rpcrdma_create_id(struct rpcrdma_xprt *xprt,
374 struct rpcrdma_ia *ia, struct sockaddr *addr)
376 struct rdma_cm_id *id;
379 init_completion(&ia->ri_done);
381 id = rdma_create_id(&init_net, rpcrdma_conn_upcall, xprt, RDMA_PS_TCP,
385 dprintk("RPC: %s: rdma_create_id() failed %i\n",
390 ia->ri_async_rc = -ETIMEDOUT;
391 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
393 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
397 wait_for_completion_interruptible_timeout(&ia->ri_done,
398 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
401 * Until xprtrdma supports DEVICE_REMOVAL, the provider must
402 * be pinned while there are active NFS/RDMA mounts to prevent
403 * hangs and crashes at umount time.
405 if (!ia->ri_async_rc && !try_module_get(id->device->owner)) {
406 dprintk("RPC: %s: Failed to get device module\n",
408 ia->ri_async_rc = -ENODEV;
410 rc = ia->ri_async_rc;
414 ia->ri_async_rc = -ETIMEDOUT;
415 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
417 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
421 wait_for_completion_interruptible_timeout(&ia->ri_done,
422 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
423 rc = ia->ri_async_rc;
429 module_put(id->device->owner);
436 * Drain any cq, prior to teardown.
439 rpcrdma_clean_cq(struct ib_cq *cq)
444 while (1 == ib_poll_cq(cq, 1, &wc))
448 dprintk("RPC: %s: flushed %d events (last 0x%x)\n",
449 __func__, count, wc.opcode);
453 * Exported functions.
457 * Open and initialize an Interface Adapter.
458 * o initializes fields of struct rpcrdma_ia, including
459 * interface and provider attributes and protection zone.
462 rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr, int memreg)
464 struct rpcrdma_ia *ia = &xprt->rx_ia;
467 ia->ri_dma_mr = NULL;
469 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
470 if (IS_ERR(ia->ri_id)) {
471 rc = PTR_ERR(ia->ri_id);
474 ia->ri_device = ia->ri_id->device;
476 ia->ri_pd = ib_alloc_pd(ia->ri_device);
477 if (IS_ERR(ia->ri_pd)) {
478 rc = PTR_ERR(ia->ri_pd);
479 dprintk("RPC: %s: ib_alloc_pd() failed %i\n",
484 if (memreg == RPCRDMA_FRMR) {
485 if (!(ia->ri_device->attrs.device_cap_flags &
486 IB_DEVICE_MEM_MGT_EXTENSIONS) ||
487 (ia->ri_device->attrs.max_fast_reg_page_list_len == 0)) {
488 dprintk("RPC: %s: FRMR registration "
489 "not supported by HCA\n", __func__);
490 memreg = RPCRDMA_MTHCAFMR;
493 if (memreg == RPCRDMA_MTHCAFMR) {
494 if (!ia->ri_device->alloc_fmr) {
495 dprintk("RPC: %s: MTHCAFMR registration "
496 "not supported by HCA\n", __func__);
504 ia->ri_ops = &rpcrdma_frwr_memreg_ops;
506 case RPCRDMA_ALLPHYSICAL:
507 ia->ri_ops = &rpcrdma_physical_memreg_ops;
509 case RPCRDMA_MTHCAFMR:
510 ia->ri_ops = &rpcrdma_fmr_memreg_ops;
513 printk(KERN_ERR "RPC: Unsupported memory "
514 "registration mode: %d\n", memreg);
518 dprintk("RPC: %s: memory registration strategy is '%s'\n",
519 __func__, ia->ri_ops->ro_displayname);
521 rwlock_init(&ia->ri_qplock);
525 ib_dealloc_pd(ia->ri_pd);
528 rpcrdma_destroy_id(ia->ri_id);
535 * Clean up/close an IA.
536 * o if event handles and PD have been initialized, free them.
540 rpcrdma_ia_close(struct rpcrdma_ia *ia)
542 dprintk("RPC: %s: entering\n", __func__);
543 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
545 rdma_destroy_qp(ia->ri_id);
546 rpcrdma_destroy_id(ia->ri_id);
550 /* If the pd is still busy, xprtrdma missed freeing a resource */
551 if (ia->ri_pd && !IS_ERR(ia->ri_pd))
552 ib_dealloc_pd(ia->ri_pd);
556 * Create unconnected endpoint.
559 rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
560 struct rpcrdma_create_data_internal *cdata)
562 struct ib_cq *sendcq, *recvcq;
563 struct ib_cq_init_attr cq_attr = {};
564 unsigned int max_qp_wr;
567 if (ia->ri_device->attrs.max_sge < RPCRDMA_MAX_IOVS) {
568 dprintk("RPC: %s: insufficient sge's available\n",
573 if (ia->ri_device->attrs.max_qp_wr <= RPCRDMA_BACKWARD_WRS) {
574 dprintk("RPC: %s: insufficient wqe's available\n",
578 max_qp_wr = ia->ri_device->attrs.max_qp_wr - RPCRDMA_BACKWARD_WRS;
580 /* check provider's send/recv wr limits */
581 if (cdata->max_requests > max_qp_wr)
582 cdata->max_requests = max_qp_wr;
584 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
585 ep->rep_attr.qp_context = ep;
586 ep->rep_attr.srq = NULL;
587 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
588 ep->rep_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS;
589 rc = ia->ri_ops->ro_open(ia, ep, cdata);
592 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
593 ep->rep_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS;
594 ep->rep_attr.cap.max_send_sge = RPCRDMA_MAX_IOVS;
595 ep->rep_attr.cap.max_recv_sge = 1;
596 ep->rep_attr.cap.max_inline_data = 0;
597 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
598 ep->rep_attr.qp_type = IB_QPT_RC;
599 ep->rep_attr.port_num = ~0;
601 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
602 "iovs: send %d recv %d\n",
604 ep->rep_attr.cap.max_send_wr,
605 ep->rep_attr.cap.max_recv_wr,
606 ep->rep_attr.cap.max_send_sge,
607 ep->rep_attr.cap.max_recv_sge);
609 /* set trigger for requesting send completion */
610 ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
611 if (ep->rep_cqinit <= 2)
612 ep->rep_cqinit = 0; /* always signal? */
614 init_waitqueue_head(&ep->rep_connect_wait);
615 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
617 cq_attr.cqe = ep->rep_attr.cap.max_send_wr + 1;
618 sendcq = ib_create_cq(ia->ri_device, rpcrdma_sendcq_upcall,
619 rpcrdma_cq_async_error_upcall, NULL, &cq_attr);
620 if (IS_ERR(sendcq)) {
621 rc = PTR_ERR(sendcq);
622 dprintk("RPC: %s: failed to create send CQ: %i\n",
627 rc = ib_req_notify_cq(sendcq, IB_CQ_NEXT_COMP);
629 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
634 cq_attr.cqe = ep->rep_attr.cap.max_recv_wr + 1;
635 recvcq = ib_create_cq(ia->ri_device, rpcrdma_recvcq_upcall,
636 rpcrdma_cq_async_error_upcall, NULL, &cq_attr);
637 if (IS_ERR(recvcq)) {
638 rc = PTR_ERR(recvcq);
639 dprintk("RPC: %s: failed to create recv CQ: %i\n",
644 rc = ib_req_notify_cq(recvcq, IB_CQ_NEXT_COMP);
646 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
648 ib_destroy_cq(recvcq);
652 ep->rep_attr.send_cq = sendcq;
653 ep->rep_attr.recv_cq = recvcq;
655 /* Initialize cma parameters */
657 /* RPC/RDMA does not use private data */
658 ep->rep_remote_cma.private_data = NULL;
659 ep->rep_remote_cma.private_data_len = 0;
661 /* Client offers RDMA Read but does not initiate */
662 ep->rep_remote_cma.initiator_depth = 0;
663 if (ia->ri_device->attrs.max_qp_rd_atom > 32) /* arbitrary but <= 255 */
664 ep->rep_remote_cma.responder_resources = 32;
666 ep->rep_remote_cma.responder_resources =
667 ia->ri_device->attrs.max_qp_rd_atom;
669 ep->rep_remote_cma.retry_count = 7;
670 ep->rep_remote_cma.flow_control = 0;
671 ep->rep_remote_cma.rnr_retry_count = 0;
676 err = ib_destroy_cq(sendcq);
678 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
682 ib_dereg_mr(ia->ri_dma_mr);
689 * Disconnect and destroy endpoint. After this, the only
690 * valid operations on the ep are to free it (if dynamically
691 * allocated) or re-create it.
694 rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
698 dprintk("RPC: %s: entering, connected is %d\n",
699 __func__, ep->rep_connected);
701 cancel_delayed_work_sync(&ep->rep_connect_worker);
704 rpcrdma_ep_disconnect(ep, ia);
706 rpcrdma_clean_cq(ep->rep_attr.recv_cq);
707 rpcrdma_clean_cq(ep->rep_attr.send_cq);
710 rdma_destroy_qp(ia->ri_id);
711 ia->ri_id->qp = NULL;
714 rc = ib_destroy_cq(ep->rep_attr.recv_cq);
716 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
719 rc = ib_destroy_cq(ep->rep_attr.send_cq);
721 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
725 rc = ib_dereg_mr(ia->ri_dma_mr);
726 dprintk("RPC: %s: ib_dereg_mr returned %i\n",
732 * Connect unconnected endpoint.
735 rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
737 struct rdma_cm_id *id, *old;
741 if (ep->rep_connected != 0) {
742 struct rpcrdma_xprt *xprt;
744 dprintk("RPC: %s: reconnecting...\n", __func__);
746 rpcrdma_ep_disconnect(ep, ia);
747 rpcrdma_flush_cqs(ep);
749 xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
750 id = rpcrdma_create_id(xprt, ia,
751 (struct sockaddr *)&xprt->rx_data.addr);
756 /* TEMP TEMP TEMP - fail if new device:
757 * Deregister/remarshal *all* requests!
758 * Close and recreate adapter, pd, etc!
759 * Re-determine all attributes still sane!
760 * More stuff I haven't thought of!
763 if (ia->ri_device != id->device) {
764 printk("RPC: %s: can't reconnect on "
765 "different device!\n", __func__);
766 rpcrdma_destroy_id(id);
771 rc = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
773 dprintk("RPC: %s: rdma_create_qp failed %i\n",
775 rpcrdma_destroy_id(id);
780 write_lock(&ia->ri_qplock);
783 write_unlock(&ia->ri_qplock);
785 rdma_destroy_qp(old);
786 rpcrdma_destroy_id(old);
788 dprintk("RPC: %s: connecting...\n", __func__);
789 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
791 dprintk("RPC: %s: rdma_create_qp failed %i\n",
793 /* do not update ep->rep_connected */
798 ep->rep_connected = 0;
800 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
802 dprintk("RPC: %s: rdma_connect() failed with %i\n",
807 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
810 * Check state. A non-peer reject indicates no listener
811 * (ECONNREFUSED), which may be a transient state. All
812 * others indicate a transport condition which has already
813 * undergone a best-effort.
815 if (ep->rep_connected == -ECONNREFUSED &&
816 ++retry_count <= RDMA_CONNECT_RETRY_MAX) {
817 dprintk("RPC: %s: non-peer_reject, retry\n", __func__);
820 if (ep->rep_connected <= 0) {
821 /* Sometimes, the only way to reliably connect to remote
822 * CMs is to use same nonzero values for ORD and IRD. */
823 if (retry_count++ <= RDMA_CONNECT_RETRY_MAX + 1 &&
824 (ep->rep_remote_cma.responder_resources == 0 ||
825 ep->rep_remote_cma.initiator_depth !=
826 ep->rep_remote_cma.responder_resources)) {
827 if (ep->rep_remote_cma.responder_resources == 0)
828 ep->rep_remote_cma.responder_resources = 1;
829 ep->rep_remote_cma.initiator_depth =
830 ep->rep_remote_cma.responder_resources;
833 rc = ep->rep_connected;
835 struct rpcrdma_xprt *r_xprt;
838 dprintk("RPC: %s: connected\n", __func__);
840 r_xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
841 extras = r_xprt->rx_buf.rb_bc_srv_max_requests;
844 rc = rpcrdma_ep_post_extra_recv(r_xprt, extras);
846 pr_warn("%s: rpcrdma_ep_post_extra_recv: %i\n",
855 ep->rep_connected = rc;
860 * rpcrdma_ep_disconnect
862 * This is separate from destroy to facilitate the ability
863 * to reconnect without recreating the endpoint.
865 * This call is not reentrant, and must not be made in parallel
866 * on the same endpoint.
869 rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
873 rpcrdma_flush_cqs(ep);
874 rc = rdma_disconnect(ia->ri_id);
876 /* returns without wait if not connected */
877 wait_event_interruptible(ep->rep_connect_wait,
878 ep->rep_connected != 1);
879 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
880 (ep->rep_connected == 1) ? "still " : "dis");
882 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
883 ep->rep_connected = rc;
888 rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
890 struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
891 struct rpcrdma_req *req;
893 req = kzalloc(sizeof(*req), GFP_KERNEL);
895 return ERR_PTR(-ENOMEM);
897 INIT_LIST_HEAD(&req->rl_free);
898 spin_lock(&buffer->rb_reqslock);
899 list_add(&req->rl_all, &buffer->rb_allreqs);
900 spin_unlock(&buffer->rb_reqslock);
901 req->rl_buffer = &r_xprt->rx_buf;
906 rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
908 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
909 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
910 struct rpcrdma_rep *rep;
914 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
918 rep->rr_rdmabuf = rpcrdma_alloc_regbuf(ia, cdata->inline_rsize,
920 if (IS_ERR(rep->rr_rdmabuf)) {
921 rc = PTR_ERR(rep->rr_rdmabuf);
925 rep->rr_device = ia->ri_device;
926 rep->rr_rxprt = r_xprt;
927 INIT_WORK(&rep->rr_work, rpcrdma_receive_worker);
937 rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
939 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
940 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
943 buf->rb_max_requests = r_xprt->rx_data.max_requests;
944 buf->rb_bc_srv_max_requests = 0;
945 spin_lock_init(&buf->rb_lock);
947 rc = ia->ri_ops->ro_init(r_xprt);
951 INIT_LIST_HEAD(&buf->rb_send_bufs);
952 INIT_LIST_HEAD(&buf->rb_allreqs);
953 spin_lock_init(&buf->rb_reqslock);
954 for (i = 0; i < buf->rb_max_requests; i++) {
955 struct rpcrdma_req *req;
957 req = rpcrdma_create_req(r_xprt);
959 dprintk("RPC: %s: request buffer %d alloc"
960 " failed\n", __func__, i);
964 req->rl_backchannel = false;
965 list_add(&req->rl_free, &buf->rb_send_bufs);
968 INIT_LIST_HEAD(&buf->rb_recv_bufs);
969 for (i = 0; i < buf->rb_max_requests + 2; i++) {
970 struct rpcrdma_rep *rep;
972 rep = rpcrdma_create_rep(r_xprt);
974 dprintk("RPC: %s: reply buffer %d alloc failed\n",
979 list_add(&rep->rr_list, &buf->rb_recv_bufs);
984 rpcrdma_buffer_destroy(buf);
988 static struct rpcrdma_req *
989 rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf)
991 struct rpcrdma_req *req;
993 req = list_first_entry(&buf->rb_send_bufs,
994 struct rpcrdma_req, rl_free);
995 list_del(&req->rl_free);
999 static struct rpcrdma_rep *
1000 rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf)
1002 struct rpcrdma_rep *rep;
1004 rep = list_first_entry(&buf->rb_recv_bufs,
1005 struct rpcrdma_rep, rr_list);
1006 list_del(&rep->rr_list);
1011 rpcrdma_destroy_rep(struct rpcrdma_ia *ia, struct rpcrdma_rep *rep)
1013 rpcrdma_free_regbuf(ia, rep->rr_rdmabuf);
1018 rpcrdma_destroy_req(struct rpcrdma_ia *ia, struct rpcrdma_req *req)
1020 rpcrdma_free_regbuf(ia, req->rl_sendbuf);
1021 rpcrdma_free_regbuf(ia, req->rl_rdmabuf);
1026 rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1028 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
1030 while (!list_empty(&buf->rb_recv_bufs)) {
1031 struct rpcrdma_rep *rep;
1033 rep = rpcrdma_buffer_get_rep_locked(buf);
1034 rpcrdma_destroy_rep(ia, rep);
1037 spin_lock(&buf->rb_reqslock);
1038 while (!list_empty(&buf->rb_allreqs)) {
1039 struct rpcrdma_req *req;
1041 req = list_first_entry(&buf->rb_allreqs,
1042 struct rpcrdma_req, rl_all);
1043 list_del(&req->rl_all);
1045 spin_unlock(&buf->rb_reqslock);
1046 rpcrdma_destroy_req(ia, req);
1047 spin_lock(&buf->rb_reqslock);
1049 spin_unlock(&buf->rb_reqslock);
1051 ia->ri_ops->ro_destroy(buf);
1055 rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt)
1057 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1058 struct rpcrdma_mw *mw = NULL;
1060 spin_lock(&buf->rb_mwlock);
1061 if (!list_empty(&buf->rb_mws)) {
1062 mw = list_first_entry(&buf->rb_mws,
1063 struct rpcrdma_mw, mw_list);
1064 list_del_init(&mw->mw_list);
1066 spin_unlock(&buf->rb_mwlock);
1069 pr_err("RPC: %s: no MWs available\n", __func__);
1074 rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw)
1076 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1078 spin_lock(&buf->rb_mwlock);
1079 list_add_tail(&mw->mw_list, &buf->rb_mws);
1080 spin_unlock(&buf->rb_mwlock);
1084 * Get a set of request/reply buffers.
1086 * Reply buffer (if available) is attached to send buffer upon return.
1088 struct rpcrdma_req *
1089 rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1091 struct rpcrdma_req *req;
1093 spin_lock(&buffers->rb_lock);
1094 if (list_empty(&buffers->rb_send_bufs))
1096 req = rpcrdma_buffer_get_req_locked(buffers);
1097 if (list_empty(&buffers->rb_recv_bufs))
1099 req->rl_reply = rpcrdma_buffer_get_rep_locked(buffers);
1100 spin_unlock(&buffers->rb_lock);
1104 spin_unlock(&buffers->rb_lock);
1105 pr_warn("RPC: %s: out of request buffers\n", __func__);
1108 spin_unlock(&buffers->rb_lock);
1109 pr_warn("RPC: %s: out of reply buffers\n", __func__);
1110 req->rl_reply = NULL;
1115 * Put request/reply buffers back into pool.
1116 * Pre-decrement counter/array index.
1119 rpcrdma_buffer_put(struct rpcrdma_req *req)
1121 struct rpcrdma_buffer *buffers = req->rl_buffer;
1122 struct rpcrdma_rep *rep = req->rl_reply;
1125 req->rl_reply = NULL;
1127 spin_lock(&buffers->rb_lock);
1128 list_add_tail(&req->rl_free, &buffers->rb_send_bufs);
1130 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
1131 spin_unlock(&buffers->rb_lock);
1135 * Recover reply buffers from pool.
1136 * This happens when recovering from disconnect.
1139 rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1141 struct rpcrdma_buffer *buffers = req->rl_buffer;
1143 spin_lock(&buffers->rb_lock);
1144 if (!list_empty(&buffers->rb_recv_bufs))
1145 req->rl_reply = rpcrdma_buffer_get_rep_locked(buffers);
1146 spin_unlock(&buffers->rb_lock);
1150 * Put reply buffers back into pool when not attached to
1151 * request. This happens in error conditions.
1154 rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1156 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
1158 spin_lock(&buffers->rb_lock);
1159 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
1160 spin_unlock(&buffers->rb_lock);
1164 * Wrappers for internal-use kmalloc memory registration, used by buffer code.
1168 rpcrdma_mapping_error(struct rpcrdma_mr_seg *seg)
1170 dprintk("RPC: map_one: offset %p iova %llx len %zu\n",
1172 (unsigned long long)seg->mr_dma, seg->mr_dmalen);
1176 * rpcrdma_alloc_regbuf - kmalloc and register memory for SEND/RECV buffers
1177 * @ia: controlling rpcrdma_ia
1178 * @size: size of buffer to be allocated, in bytes
1181 * Returns pointer to private header of an area of internally
1182 * registered memory, or an ERR_PTR. The registered buffer follows
1183 * the end of the private header.
1185 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
1186 * receiving the payload of RDMA RECV operations. regbufs are not
1187 * used for RDMA READ/WRITE operations, thus are registered only for
1190 struct rpcrdma_regbuf *
1191 rpcrdma_alloc_regbuf(struct rpcrdma_ia *ia, size_t size, gfp_t flags)
1193 struct rpcrdma_regbuf *rb;
1196 rb = kmalloc(sizeof(*rb) + size, flags);
1201 iov->addr = ib_dma_map_single(ia->ri_device,
1202 (void *)rb->rg_base, size,
1204 if (ib_dma_mapping_error(ia->ri_device, iov->addr))
1208 iov->lkey = ia->ri_pd->local_dma_lkey;
1210 rb->rg_owner = NULL;
1216 return ERR_PTR(-ENOMEM);
1220 * rpcrdma_free_regbuf - deregister and free registered buffer
1221 * @ia: controlling rpcrdma_ia
1222 * @rb: regbuf to be deregistered and freed
1225 rpcrdma_free_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1233 ib_dma_unmap_single(ia->ri_device,
1234 iov->addr, iov->length, DMA_BIDIRECTIONAL);
1239 * Prepost any receive buffer, then post send.
1241 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1244 rpcrdma_ep_post(struct rpcrdma_ia *ia,
1245 struct rpcrdma_ep *ep,
1246 struct rpcrdma_req *req)
1248 struct ib_device *device = ia->ri_device;
1249 struct ib_send_wr send_wr, *send_wr_fail;
1250 struct rpcrdma_rep *rep = req->rl_reply;
1251 struct ib_sge *iov = req->rl_send_iov;
1255 rc = rpcrdma_ep_post_recv(ia, ep, rep);
1258 req->rl_reply = NULL;
1261 send_wr.next = NULL;
1262 send_wr.wr_id = RPCRDMA_IGNORE_COMPLETION;
1263 send_wr.sg_list = iov;
1264 send_wr.num_sge = req->rl_niovs;
1265 send_wr.opcode = IB_WR_SEND;
1267 for (i = 0; i < send_wr.num_sge; i++)
1268 ib_dma_sync_single_for_device(device, iov[i].addr,
1269 iov[i].length, DMA_TO_DEVICE);
1270 dprintk("RPC: %s: posting %d s/g entries\n",
1271 __func__, send_wr.num_sge);
1273 if (DECR_CQCOUNT(ep) > 0)
1274 send_wr.send_flags = 0;
1275 else { /* Provider must take a send completion every now and then */
1277 send_wr.send_flags = IB_SEND_SIGNALED;
1280 rc = ib_post_send(ia->ri_id->qp, &send_wr, &send_wr_fail);
1282 dprintk("RPC: %s: ib_post_send returned %i\n", __func__,
1289 * (Re)post a receive buffer.
1292 rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
1293 struct rpcrdma_ep *ep,
1294 struct rpcrdma_rep *rep)
1296 struct ib_recv_wr recv_wr, *recv_wr_fail;
1299 recv_wr.next = NULL;
1300 recv_wr.wr_id = (u64) (unsigned long) rep;
1301 recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
1302 recv_wr.num_sge = 1;
1304 ib_dma_sync_single_for_cpu(ia->ri_device,
1305 rdmab_addr(rep->rr_rdmabuf),
1306 rdmab_length(rep->rr_rdmabuf),
1309 rc = ib_post_recv(ia->ri_id->qp, &recv_wr, &recv_wr_fail);
1312 dprintk("RPC: %s: ib_post_recv returned %i\n", __func__,
1318 * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests
1319 * @r_xprt: transport associated with these backchannel resources
1320 * @min_reqs: minimum number of incoming requests expected
1322 * Returns zero if all requested buffers were posted, or a negative errno.
1325 rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt *r_xprt, unsigned int count)
1327 struct rpcrdma_buffer *buffers = &r_xprt->rx_buf;
1328 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
1329 struct rpcrdma_ep *ep = &r_xprt->rx_ep;
1330 struct rpcrdma_rep *rep;
1334 spin_lock(&buffers->rb_lock);
1335 if (list_empty(&buffers->rb_recv_bufs))
1337 rep = rpcrdma_buffer_get_rep_locked(buffers);
1338 spin_unlock(&buffers->rb_lock);
1340 rc = rpcrdma_ep_post_recv(ia, ep, rep);
1348 spin_unlock(&buffers->rb_lock);
1349 pr_warn("%s: no extra receive buffers\n", __func__);
1353 rpcrdma_recv_buffer_put(rep);
1357 /* How many chunk list items fit within our inline buffers?
1360 rpcrdma_max_segments(struct rpcrdma_xprt *r_xprt)
1362 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
1363 int bytes, segments;
1365 bytes = min_t(unsigned int, cdata->inline_wsize, cdata->inline_rsize);
1366 bytes -= RPCRDMA_HDRLEN_MIN;
1367 if (bytes < sizeof(struct rpcrdma_segment) * 2) {
1368 pr_warn("RPC: %s: inline threshold too small\n",
1373 segments = 1 << (fls(bytes / sizeof(struct rpcrdma_segment)) - 1);
1374 dprintk("RPC: %s: max chunk list size = %d segments\n",
1375 __func__, segments);