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 <asm/bitops.h>
54 #include "xprt_rdma.h"
61 # define RPCDBG_FACILITY RPCDBG_TRANS
64 static void rpcrdma_reset_frmrs(struct rpcrdma_ia *);
71 * handle replies in tasklet context, using a single, global list
72 * rdma tasklet function -- just turn around and call the func
73 * for all replies on the list
76 static DEFINE_SPINLOCK(rpcrdma_tk_lock_g);
77 static LIST_HEAD(rpcrdma_tasklets_g);
80 rpcrdma_run_tasklet(unsigned long data)
82 struct rpcrdma_rep *rep;
83 void (*func)(struct rpcrdma_rep *);
87 spin_lock_irqsave(&rpcrdma_tk_lock_g, flags);
88 while (!list_empty(&rpcrdma_tasklets_g)) {
89 rep = list_entry(rpcrdma_tasklets_g.next,
90 struct rpcrdma_rep, rr_list);
91 list_del(&rep->rr_list);
94 spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
99 rpcrdma_recv_buffer_put(rep);
101 spin_lock_irqsave(&rpcrdma_tk_lock_g, flags);
103 spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
106 static DECLARE_TASKLET(rpcrdma_tasklet_g, rpcrdma_run_tasklet, 0UL);
109 rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context)
111 struct rpcrdma_ep *ep = context;
113 dprintk("RPC: %s: QP error %X on device %s ep %p\n",
114 __func__, event->event, event->device->name, context);
115 if (ep->rep_connected == 1) {
116 ep->rep_connected = -EIO;
118 wake_up_all(&ep->rep_connect_wait);
123 rpcrdma_cq_async_error_upcall(struct ib_event *event, void *context)
125 struct rpcrdma_ep *ep = context;
127 dprintk("RPC: %s: CQ error %X on device %s ep %p\n",
128 __func__, event->event, event->device->name, context);
129 if (ep->rep_connected == 1) {
130 ep->rep_connected = -EIO;
132 wake_up_all(&ep->rep_connect_wait);
137 rpcrdma_sendcq_process_wc(struct ib_wc *wc)
139 struct rpcrdma_mw *frmr = (struct rpcrdma_mw *)(unsigned long)wc->wr_id;
141 dprintk("RPC: %s: frmr %p status %X opcode %d\n",
142 __func__, frmr, wc->status, wc->opcode);
144 if (wc->wr_id == 0ULL)
146 if (wc->status != IB_WC_SUCCESS)
147 frmr->r.frmr.fr_state = FRMR_IS_STALE;
151 rpcrdma_sendcq_poll(struct ib_cq *cq, struct rpcrdma_ep *ep)
154 int budget, count, rc;
156 budget = RPCRDMA_WC_BUDGET / RPCRDMA_POLLSIZE;
158 wcs = ep->rep_send_wcs;
160 rc = ib_poll_cq(cq, RPCRDMA_POLLSIZE, wcs);
166 rpcrdma_sendcq_process_wc(wcs++);
167 } while (rc == RPCRDMA_POLLSIZE && --budget);
172 * Handle send, fast_reg_mr, and local_inv completions.
174 * Send events are typically suppressed and thus do not result
175 * in an upcall. Occasionally one is signaled, however. This
176 * prevents the provider's completion queue from wrapping and
177 * losing a completion.
180 rpcrdma_sendcq_upcall(struct ib_cq *cq, void *cq_context)
182 struct rpcrdma_ep *ep = (struct rpcrdma_ep *)cq_context;
185 rc = rpcrdma_sendcq_poll(cq, ep);
187 dprintk("RPC: %s: ib_poll_cq failed: %i\n",
192 rc = ib_req_notify_cq(cq,
193 IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS);
197 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
202 rpcrdma_sendcq_poll(cq, ep);
206 rpcrdma_recvcq_process_wc(struct ib_wc *wc, struct list_head *sched_list)
208 struct rpcrdma_rep *rep =
209 (struct rpcrdma_rep *)(unsigned long)wc->wr_id;
211 dprintk("RPC: %s: rep %p status %X opcode %X length %u\n",
212 __func__, rep, wc->status, wc->opcode, wc->byte_len);
214 if (wc->status != IB_WC_SUCCESS) {
218 if (wc->opcode != IB_WC_RECV)
221 rep->rr_len = wc->byte_len;
222 ib_dma_sync_single_for_cpu(rdmab_to_ia(rep->rr_buffer)->ri_id->device,
223 rep->rr_iov.addr, rep->rr_len, DMA_FROM_DEVICE);
225 if (rep->rr_len >= 16) {
226 struct rpcrdma_msg *p = (struct rpcrdma_msg *)rep->rr_base;
227 unsigned int credits = ntohl(p->rm_credit);
230 credits = 1; /* don't deadlock */
231 else if (credits > rep->rr_buffer->rb_max_requests)
232 credits = rep->rr_buffer->rb_max_requests;
233 atomic_set(&rep->rr_buffer->rb_credits, credits);
237 list_add_tail(&rep->rr_list, sched_list);
241 rpcrdma_recvcq_poll(struct ib_cq *cq, struct rpcrdma_ep *ep)
243 struct list_head sched_list;
245 int budget, count, rc;
248 INIT_LIST_HEAD(&sched_list);
249 budget = RPCRDMA_WC_BUDGET / RPCRDMA_POLLSIZE;
251 wcs = ep->rep_recv_wcs;
253 rc = ib_poll_cq(cq, RPCRDMA_POLLSIZE, wcs);
259 rpcrdma_recvcq_process_wc(wcs++, &sched_list);
260 } while (rc == RPCRDMA_POLLSIZE && --budget);
264 spin_lock_irqsave(&rpcrdma_tk_lock_g, flags);
265 list_splice_tail(&sched_list, &rpcrdma_tasklets_g);
266 spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
267 tasklet_schedule(&rpcrdma_tasklet_g);
272 * Handle receive completions.
274 * It is reentrant but processes single events in order to maintain
275 * ordering of receives to keep server credits.
277 * It is the responsibility of the scheduled tasklet to return
278 * recv buffers to the pool. NOTE: this affects synchronization of
279 * connection shutdown. That is, the structures required for
280 * the completion of the reply handler must remain intact until
281 * all memory has been reclaimed.
284 rpcrdma_recvcq_upcall(struct ib_cq *cq, void *cq_context)
286 struct rpcrdma_ep *ep = (struct rpcrdma_ep *)cq_context;
289 rc = rpcrdma_recvcq_poll(cq, ep);
291 dprintk("RPC: %s: ib_poll_cq failed: %i\n",
296 rc = ib_req_notify_cq(cq,
297 IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS);
301 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
306 rpcrdma_recvcq_poll(cq, ep);
310 rpcrdma_flush_cqs(struct rpcrdma_ep *ep)
312 rpcrdma_recvcq_upcall(ep->rep_attr.recv_cq, ep);
313 rpcrdma_sendcq_upcall(ep->rep_attr.send_cq, ep);
317 static const char * const conn[] = {
336 #define CONNECTION_MSG(status) \
337 ((status) < ARRAY_SIZE(conn) ? \
338 conn[(status)] : "unrecognized connection error")
342 rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
344 struct rpcrdma_xprt *xprt = id->context;
345 struct rpcrdma_ia *ia = &xprt->rx_ia;
346 struct rpcrdma_ep *ep = &xprt->rx_ep;
348 struct sockaddr_in *addr = (struct sockaddr_in *) &ep->rep_remote_addr;
350 struct ib_qp_attr attr;
351 struct ib_qp_init_attr iattr;
354 switch (event->event) {
355 case RDMA_CM_EVENT_ADDR_RESOLVED:
356 case RDMA_CM_EVENT_ROUTE_RESOLVED:
358 complete(&ia->ri_done);
360 case RDMA_CM_EVENT_ADDR_ERROR:
361 ia->ri_async_rc = -EHOSTUNREACH;
362 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
364 complete(&ia->ri_done);
366 case RDMA_CM_EVENT_ROUTE_ERROR:
367 ia->ri_async_rc = -ENETUNREACH;
368 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
370 complete(&ia->ri_done);
372 case RDMA_CM_EVENT_ESTABLISHED:
374 ib_query_qp(ia->ri_id->qp, &attr,
375 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_MAX_DEST_RD_ATOMIC,
377 dprintk("RPC: %s: %d responder resources"
379 __func__, attr.max_dest_rd_atomic, attr.max_rd_atomic);
381 case RDMA_CM_EVENT_CONNECT_ERROR:
382 connstate = -ENOTCONN;
384 case RDMA_CM_EVENT_UNREACHABLE:
385 connstate = -ENETDOWN;
387 case RDMA_CM_EVENT_REJECTED:
388 connstate = -ECONNREFUSED;
390 case RDMA_CM_EVENT_DISCONNECTED:
391 connstate = -ECONNABORTED;
393 case RDMA_CM_EVENT_DEVICE_REMOVAL:
396 atomic_set(&rpcx_to_rdmax(ep->rep_xprt)->rx_buf.rb_credits, 1);
397 dprintk("RPC: %s: %sconnected\n",
398 __func__, connstate > 0 ? "" : "dis");
399 ep->rep_connected = connstate;
401 wake_up_all(&ep->rep_connect_wait);
404 dprintk("RPC: %s: %pI4:%u (ep 0x%p): %s\n",
405 __func__, &addr->sin_addr.s_addr,
406 ntohs(addr->sin_port), ep,
407 CONNECTION_MSG(event->event));
412 if (connstate == 1) {
413 int ird = attr.max_dest_rd_atomic;
414 int tird = ep->rep_remote_cma.responder_resources;
415 printk(KERN_INFO "rpcrdma: connection to %pI4:%u "
416 "on %s, memreg %d slots %d ird %d%s\n",
417 &addr->sin_addr.s_addr,
418 ntohs(addr->sin_port),
419 ia->ri_id->device->name,
420 ia->ri_memreg_strategy,
421 xprt->rx_buf.rb_max_requests,
422 ird, ird < 4 && ird < tird / 2 ? " (low!)" : "");
423 } else if (connstate < 0) {
424 printk(KERN_INFO "rpcrdma: connection to %pI4:%u closed (%d)\n",
425 &addr->sin_addr.s_addr,
426 ntohs(addr->sin_port),
434 static struct rdma_cm_id *
435 rpcrdma_create_id(struct rpcrdma_xprt *xprt,
436 struct rpcrdma_ia *ia, struct sockaddr *addr)
438 struct rdma_cm_id *id;
441 init_completion(&ia->ri_done);
443 id = rdma_create_id(rpcrdma_conn_upcall, xprt, RDMA_PS_TCP, IB_QPT_RC);
446 dprintk("RPC: %s: rdma_create_id() failed %i\n",
451 ia->ri_async_rc = -ETIMEDOUT;
452 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
454 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
458 wait_for_completion_interruptible_timeout(&ia->ri_done,
459 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
460 rc = ia->ri_async_rc;
464 ia->ri_async_rc = -ETIMEDOUT;
465 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
467 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
471 wait_for_completion_interruptible_timeout(&ia->ri_done,
472 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
473 rc = ia->ri_async_rc;
485 * Drain any cq, prior to teardown.
488 rpcrdma_clean_cq(struct ib_cq *cq)
493 while (1 == ib_poll_cq(cq, 1, &wc))
497 dprintk("RPC: %s: flushed %d events (last 0x%x)\n",
498 __func__, count, wc.opcode);
502 * Exported functions.
506 * Open and initialize an Interface Adapter.
507 * o initializes fields of struct rpcrdma_ia, including
508 * interface and provider attributes and protection zone.
511 rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr, int memreg)
514 struct ib_device_attr devattr;
515 struct rpcrdma_ia *ia = &xprt->rx_ia;
517 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
518 if (IS_ERR(ia->ri_id)) {
519 rc = PTR_ERR(ia->ri_id);
523 ia->ri_pd = ib_alloc_pd(ia->ri_id->device);
524 if (IS_ERR(ia->ri_pd)) {
525 rc = PTR_ERR(ia->ri_pd);
526 dprintk("RPC: %s: ib_alloc_pd() failed %i\n",
532 * Query the device to determine if the requested memory
533 * registration strategy is supported. If it isn't, set the
534 * strategy to a globally supported model.
536 rc = ib_query_device(ia->ri_id->device, &devattr);
538 dprintk("RPC: %s: ib_query_device failed %d\n",
543 if (devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY) {
544 ia->ri_have_dma_lkey = 1;
545 ia->ri_dma_lkey = ia->ri_id->device->local_dma_lkey;
548 if (memreg == RPCRDMA_FRMR) {
549 /* Requires both frmr reg and local dma lkey */
550 if ((devattr.device_cap_flags &
551 (IB_DEVICE_MEM_MGT_EXTENSIONS|IB_DEVICE_LOCAL_DMA_LKEY)) !=
552 (IB_DEVICE_MEM_MGT_EXTENSIONS|IB_DEVICE_LOCAL_DMA_LKEY)) {
553 dprintk("RPC: %s: FRMR registration "
554 "not supported by HCA\n", __func__);
555 memreg = RPCRDMA_MTHCAFMR;
557 /* Mind the ia limit on FRMR page list depth */
558 ia->ri_max_frmr_depth = min_t(unsigned int,
559 RPCRDMA_MAX_DATA_SEGS,
560 devattr.max_fast_reg_page_list_len);
563 if (memreg == RPCRDMA_MTHCAFMR) {
564 if (!ia->ri_id->device->alloc_fmr) {
565 dprintk("RPC: %s: MTHCAFMR registration "
566 "not supported by HCA\n", __func__);
567 memreg = RPCRDMA_ALLPHYSICAL;
572 * Optionally obtain an underlying physical identity mapping in
573 * order to do a memory window-based bind. This base registration
574 * is protected from remote access - that is enabled only by binding
575 * for the specific bytes targeted during each RPC operation, and
576 * revoked after the corresponding completion similar to a storage
582 case RPCRDMA_ALLPHYSICAL:
583 mem_priv = IB_ACCESS_LOCAL_WRITE |
584 IB_ACCESS_REMOTE_WRITE |
585 IB_ACCESS_REMOTE_READ;
587 case RPCRDMA_MTHCAFMR:
588 if (ia->ri_have_dma_lkey)
590 mem_priv = IB_ACCESS_LOCAL_WRITE;
592 ia->ri_bind_mem = ib_get_dma_mr(ia->ri_pd, mem_priv);
593 if (IS_ERR(ia->ri_bind_mem)) {
594 printk(KERN_ALERT "%s: ib_get_dma_mr for "
595 "phys register failed with %lX\n",
596 __func__, PTR_ERR(ia->ri_bind_mem));
602 printk(KERN_ERR "RPC: Unsupported memory "
603 "registration mode: %d\n", memreg);
607 dprintk("RPC: %s: memory registration strategy is %d\n",
610 /* Else will do memory reg/dereg for each chunk */
611 ia->ri_memreg_strategy = memreg;
613 rwlock_init(&ia->ri_qplock);
616 rdma_destroy_id(ia->ri_id);
623 * Clean up/close an IA.
624 * o if event handles and PD have been initialized, free them.
628 rpcrdma_ia_close(struct rpcrdma_ia *ia)
632 dprintk("RPC: %s: entering\n", __func__);
633 if (ia->ri_bind_mem != NULL) {
634 rc = ib_dereg_mr(ia->ri_bind_mem);
635 dprintk("RPC: %s: ib_dereg_mr returned %i\n",
638 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
640 rdma_destroy_qp(ia->ri_id);
641 rdma_destroy_id(ia->ri_id);
644 if (ia->ri_pd != NULL && !IS_ERR(ia->ri_pd)) {
645 rc = ib_dealloc_pd(ia->ri_pd);
646 dprintk("RPC: %s: ib_dealloc_pd returned %i\n",
652 * Create unconnected endpoint.
655 rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
656 struct rpcrdma_create_data_internal *cdata)
658 struct ib_device_attr devattr;
659 struct ib_cq *sendcq, *recvcq;
662 rc = ib_query_device(ia->ri_id->device, &devattr);
664 dprintk("RPC: %s: ib_query_device failed %d\n",
669 /* check provider's send/recv wr limits */
670 if (cdata->max_requests > devattr.max_qp_wr)
671 cdata->max_requests = devattr.max_qp_wr;
673 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
674 ep->rep_attr.qp_context = ep;
675 /* send_cq and recv_cq initialized below */
676 ep->rep_attr.srq = NULL;
677 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
678 switch (ia->ri_memreg_strategy) {
682 /* Add room for frmr register and invalidate WRs.
683 * 1. FRMR reg WR for head
684 * 2. FRMR invalidate WR for head
685 * 3. N FRMR reg WRs for pagelist
686 * 4. N FRMR invalidate WRs for pagelist
687 * 5. FRMR reg WR for tail
688 * 6. FRMR invalidate WR for tail
689 * 7. The RDMA_SEND WR
692 /* Calculate N if the device max FRMR depth is smaller than
693 * RPCRDMA_MAX_DATA_SEGS.
695 if (ia->ri_max_frmr_depth < RPCRDMA_MAX_DATA_SEGS) {
696 int delta = RPCRDMA_MAX_DATA_SEGS -
697 ia->ri_max_frmr_depth;
700 depth += 2; /* FRMR reg + invalidate */
701 delta -= ia->ri_max_frmr_depth;
705 ep->rep_attr.cap.max_send_wr *= depth;
706 if (ep->rep_attr.cap.max_send_wr > devattr.max_qp_wr) {
707 cdata->max_requests = devattr.max_qp_wr / depth;
708 if (!cdata->max_requests)
710 ep->rep_attr.cap.max_send_wr = cdata->max_requests *
718 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
719 ep->rep_attr.cap.max_send_sge = (cdata->padding ? 4 : 2);
720 ep->rep_attr.cap.max_recv_sge = 1;
721 ep->rep_attr.cap.max_inline_data = 0;
722 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
723 ep->rep_attr.qp_type = IB_QPT_RC;
724 ep->rep_attr.port_num = ~0;
726 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
727 "iovs: send %d recv %d\n",
729 ep->rep_attr.cap.max_send_wr,
730 ep->rep_attr.cap.max_recv_wr,
731 ep->rep_attr.cap.max_send_sge,
732 ep->rep_attr.cap.max_recv_sge);
734 /* set trigger for requesting send completion */
735 ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
736 if (ep->rep_cqinit <= 2)
740 init_waitqueue_head(&ep->rep_connect_wait);
741 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
743 sendcq = ib_create_cq(ia->ri_id->device, rpcrdma_sendcq_upcall,
744 rpcrdma_cq_async_error_upcall, ep,
745 ep->rep_attr.cap.max_send_wr + 1, 0);
746 if (IS_ERR(sendcq)) {
747 rc = PTR_ERR(sendcq);
748 dprintk("RPC: %s: failed to create send CQ: %i\n",
753 rc = ib_req_notify_cq(sendcq, IB_CQ_NEXT_COMP);
755 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
760 recvcq = ib_create_cq(ia->ri_id->device, rpcrdma_recvcq_upcall,
761 rpcrdma_cq_async_error_upcall, ep,
762 ep->rep_attr.cap.max_recv_wr + 1, 0);
763 if (IS_ERR(recvcq)) {
764 rc = PTR_ERR(recvcq);
765 dprintk("RPC: %s: failed to create recv CQ: %i\n",
770 rc = ib_req_notify_cq(recvcq, IB_CQ_NEXT_COMP);
772 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
774 ib_destroy_cq(recvcq);
778 ep->rep_attr.send_cq = sendcq;
779 ep->rep_attr.recv_cq = recvcq;
781 /* Initialize cma parameters */
783 /* RPC/RDMA does not use private data */
784 ep->rep_remote_cma.private_data = NULL;
785 ep->rep_remote_cma.private_data_len = 0;
787 /* Client offers RDMA Read but does not initiate */
788 ep->rep_remote_cma.initiator_depth = 0;
789 if (devattr.max_qp_rd_atom > 32) /* arbitrary but <= 255 */
790 ep->rep_remote_cma.responder_resources = 32;
792 ep->rep_remote_cma.responder_resources = devattr.max_qp_rd_atom;
794 ep->rep_remote_cma.retry_count = 7;
795 ep->rep_remote_cma.flow_control = 0;
796 ep->rep_remote_cma.rnr_retry_count = 0;
801 err = ib_destroy_cq(sendcq);
803 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
812 * Disconnect and destroy endpoint. After this, the only
813 * valid operations on the ep are to free it (if dynamically
814 * allocated) or re-create it.
817 rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
821 dprintk("RPC: %s: entering, connected is %d\n",
822 __func__, ep->rep_connected);
824 cancel_delayed_work_sync(&ep->rep_connect_worker);
827 rpcrdma_ep_disconnect(ep, ia);
828 rdma_destroy_qp(ia->ri_id);
829 ia->ri_id->qp = NULL;
832 /* padding - could be done in rpcrdma_buffer_destroy... */
833 if (ep->rep_pad_mr) {
834 rpcrdma_deregister_internal(ia, ep->rep_pad_mr, &ep->rep_pad);
835 ep->rep_pad_mr = NULL;
838 rpcrdma_clean_cq(ep->rep_attr.recv_cq);
839 rc = ib_destroy_cq(ep->rep_attr.recv_cq);
841 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
844 rpcrdma_clean_cq(ep->rep_attr.send_cq);
845 rc = ib_destroy_cq(ep->rep_attr.send_cq);
847 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
852 * Connect unconnected endpoint.
855 rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
857 struct rdma_cm_id *id, *old;
861 if (ep->rep_connected != 0) {
862 struct rpcrdma_xprt *xprt;
864 dprintk("RPC: %s: reconnecting...\n", __func__);
866 rpcrdma_ep_disconnect(ep, ia);
867 rpcrdma_flush_cqs(ep);
869 if (ia->ri_memreg_strategy == RPCRDMA_FRMR)
870 rpcrdma_reset_frmrs(ia);
872 xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
873 id = rpcrdma_create_id(xprt, ia,
874 (struct sockaddr *)&xprt->rx_data.addr);
879 /* TEMP TEMP TEMP - fail if new device:
880 * Deregister/remarshal *all* requests!
881 * Close and recreate adapter, pd, etc!
882 * Re-determine all attributes still sane!
883 * More stuff I haven't thought of!
886 if (ia->ri_id->device != id->device) {
887 printk("RPC: %s: can't reconnect on "
888 "different device!\n", __func__);
894 rc = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
896 dprintk("RPC: %s: rdma_create_qp failed %i\n",
903 write_lock(&ia->ri_qplock);
906 write_unlock(&ia->ri_qplock);
908 rdma_destroy_qp(old);
909 rdma_destroy_id(old);
911 dprintk("RPC: %s: connecting...\n", __func__);
912 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
914 dprintk("RPC: %s: rdma_create_qp failed %i\n",
916 /* do not update ep->rep_connected */
921 ep->rep_connected = 0;
923 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
925 dprintk("RPC: %s: rdma_connect() failed with %i\n",
930 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
933 * Check state. A non-peer reject indicates no listener
934 * (ECONNREFUSED), which may be a transient state. All
935 * others indicate a transport condition which has already
936 * undergone a best-effort.
938 if (ep->rep_connected == -ECONNREFUSED &&
939 ++retry_count <= RDMA_CONNECT_RETRY_MAX) {
940 dprintk("RPC: %s: non-peer_reject, retry\n", __func__);
943 if (ep->rep_connected <= 0) {
944 /* Sometimes, the only way to reliably connect to remote
945 * CMs is to use same nonzero values for ORD and IRD. */
946 if (retry_count++ <= RDMA_CONNECT_RETRY_MAX + 1 &&
947 (ep->rep_remote_cma.responder_resources == 0 ||
948 ep->rep_remote_cma.initiator_depth !=
949 ep->rep_remote_cma.responder_resources)) {
950 if (ep->rep_remote_cma.responder_resources == 0)
951 ep->rep_remote_cma.responder_resources = 1;
952 ep->rep_remote_cma.initiator_depth =
953 ep->rep_remote_cma.responder_resources;
956 rc = ep->rep_connected;
958 dprintk("RPC: %s: connected\n", __func__);
963 ep->rep_connected = rc;
968 * rpcrdma_ep_disconnect
970 * This is separate from destroy to facilitate the ability
971 * to reconnect without recreating the endpoint.
973 * This call is not reentrant, and must not be made in parallel
974 * on the same endpoint.
977 rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
981 rpcrdma_flush_cqs(ep);
982 rc = rdma_disconnect(ia->ri_id);
984 /* returns without wait if not connected */
985 wait_event_interruptible(ep->rep_connect_wait,
986 ep->rep_connected != 1);
987 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
988 (ep->rep_connected == 1) ? "still " : "dis");
990 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
991 ep->rep_connected = rc;
996 rpcrdma_init_fmrs(struct rpcrdma_ia *ia, struct rpcrdma_buffer *buf)
998 int mr_access_flags = IB_ACCESS_REMOTE_WRITE | IB_ACCESS_REMOTE_READ;
999 struct ib_fmr_attr fmr_attr = {
1000 .max_pages = RPCRDMA_MAX_DATA_SEGS,
1002 .page_shift = PAGE_SHIFT
1004 struct rpcrdma_mw *r;
1007 i = (buf->rb_max_requests + 1) * RPCRDMA_MAX_SEGS;
1008 dprintk("RPC: %s: initalizing %d FMRs\n", __func__, i);
1011 r = kzalloc(sizeof(*r), GFP_KERNEL);
1015 r->r.fmr = ib_alloc_fmr(ia->ri_pd, mr_access_flags, &fmr_attr);
1016 if (IS_ERR(r->r.fmr)) {
1017 rc = PTR_ERR(r->r.fmr);
1018 dprintk("RPC: %s: ib_alloc_fmr failed %i\n",
1023 list_add(&r->mw_list, &buf->rb_mws);
1024 list_add(&r->mw_all, &buf->rb_all);
1034 rpcrdma_init_frmrs(struct rpcrdma_ia *ia, struct rpcrdma_buffer *buf)
1036 struct rpcrdma_frmr *f;
1037 struct rpcrdma_mw *r;
1040 i = (buf->rb_max_requests + 1) * RPCRDMA_MAX_SEGS;
1041 dprintk("RPC: %s: initalizing %d FRMRs\n", __func__, i);
1044 r = kzalloc(sizeof(*r), GFP_KERNEL);
1049 f->fr_mr = ib_alloc_fast_reg_mr(ia->ri_pd,
1050 ia->ri_max_frmr_depth);
1051 if (IS_ERR(f->fr_mr)) {
1052 rc = PTR_ERR(f->fr_mr);
1053 dprintk("RPC: %s: ib_alloc_fast_reg_mr "
1054 "failed %i\n", __func__, rc);
1058 f->fr_pgl = ib_alloc_fast_reg_page_list(ia->ri_id->device,
1059 ia->ri_max_frmr_depth);
1060 if (IS_ERR(f->fr_pgl)) {
1061 rc = PTR_ERR(f->fr_pgl);
1062 dprintk("RPC: %s: ib_alloc_fast_reg_page_list "
1063 "failed %i\n", __func__, rc);
1065 ib_dereg_mr(f->fr_mr);
1069 list_add(&r->mw_list, &buf->rb_mws);
1070 list_add(&r->mw_all, &buf->rb_all);
1081 rpcrdma_buffer_create(struct rpcrdma_buffer *buf, struct rpcrdma_ep *ep,
1082 struct rpcrdma_ia *ia, struct rpcrdma_create_data_internal *cdata)
1085 size_t len, rlen, wlen;
1088 buf->rb_max_requests = cdata->max_requests;
1089 spin_lock_init(&buf->rb_lock);
1090 atomic_set(&buf->rb_credits, 1);
1092 /* Need to allocate:
1093 * 1. arrays for send and recv pointers
1094 * 2. arrays of struct rpcrdma_req to fill in pointers
1095 * 3. array of struct rpcrdma_rep for replies
1096 * 4. padding, if any
1097 * Send/recv buffers in req/rep need to be registered
1099 len = buf->rb_max_requests *
1100 (sizeof(struct rpcrdma_req *) + sizeof(struct rpcrdma_rep *));
1101 len += cdata->padding;
1103 p = kzalloc(len, GFP_KERNEL);
1105 dprintk("RPC: %s: req_t/rep_t/pad kzalloc(%zd) failed\n",
1110 buf->rb_pool = p; /* for freeing it later */
1112 buf->rb_send_bufs = (struct rpcrdma_req **) p;
1113 p = (char *) &buf->rb_send_bufs[buf->rb_max_requests];
1114 buf->rb_recv_bufs = (struct rpcrdma_rep **) p;
1115 p = (char *) &buf->rb_recv_bufs[buf->rb_max_requests];
1118 * Register the zeroed pad buffer, if any.
1120 if (cdata->padding) {
1121 rc = rpcrdma_register_internal(ia, p, cdata->padding,
1122 &ep->rep_pad_mr, &ep->rep_pad);
1126 p += cdata->padding;
1128 INIT_LIST_HEAD(&buf->rb_mws);
1129 INIT_LIST_HEAD(&buf->rb_all);
1130 switch (ia->ri_memreg_strategy) {
1132 rc = rpcrdma_init_frmrs(ia, buf);
1136 case RPCRDMA_MTHCAFMR:
1137 rc = rpcrdma_init_fmrs(ia, buf);
1146 * Allocate/init the request/reply buffers. Doing this
1147 * using kmalloc for now -- one for each buf.
1149 wlen = 1 << fls(cdata->inline_wsize + sizeof(struct rpcrdma_req));
1150 rlen = 1 << fls(cdata->inline_rsize + sizeof(struct rpcrdma_rep));
1151 dprintk("RPC: %s: wlen = %zu, rlen = %zu\n",
1152 __func__, wlen, rlen);
1154 for (i = 0; i < buf->rb_max_requests; i++) {
1155 struct rpcrdma_req *req;
1156 struct rpcrdma_rep *rep;
1158 req = kmalloc(wlen, GFP_KERNEL);
1160 dprintk("RPC: %s: request buffer %d alloc"
1161 " failed\n", __func__, i);
1165 memset(req, 0, sizeof(struct rpcrdma_req));
1166 buf->rb_send_bufs[i] = req;
1167 buf->rb_send_bufs[i]->rl_buffer = buf;
1169 rc = rpcrdma_register_internal(ia, req->rl_base,
1170 wlen - offsetof(struct rpcrdma_req, rl_base),
1171 &buf->rb_send_bufs[i]->rl_handle,
1172 &buf->rb_send_bufs[i]->rl_iov);
1176 buf->rb_send_bufs[i]->rl_size = wlen -
1177 sizeof(struct rpcrdma_req);
1179 rep = kmalloc(rlen, GFP_KERNEL);
1181 dprintk("RPC: %s: reply buffer %d alloc failed\n",
1186 memset(rep, 0, sizeof(struct rpcrdma_rep));
1187 buf->rb_recv_bufs[i] = rep;
1188 buf->rb_recv_bufs[i]->rr_buffer = buf;
1190 rc = rpcrdma_register_internal(ia, rep->rr_base,
1191 rlen - offsetof(struct rpcrdma_rep, rr_base),
1192 &buf->rb_recv_bufs[i]->rr_handle,
1193 &buf->rb_recv_bufs[i]->rr_iov);
1198 dprintk("RPC: %s: max_requests %d\n",
1199 __func__, buf->rb_max_requests);
1203 rpcrdma_buffer_destroy(buf);
1208 rpcrdma_destroy_fmrs(struct rpcrdma_buffer *buf)
1210 struct rpcrdma_mw *r;
1213 while (!list_empty(&buf->rb_all)) {
1214 r = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
1215 list_del(&r->mw_all);
1216 list_del(&r->mw_list);
1218 rc = ib_dealloc_fmr(r->r.fmr);
1220 dprintk("RPC: %s: ib_dealloc_fmr failed %i\n",
1228 rpcrdma_destroy_frmrs(struct rpcrdma_buffer *buf)
1230 struct rpcrdma_mw *r;
1233 while (!list_empty(&buf->rb_all)) {
1234 r = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
1235 list_del(&r->mw_all);
1236 list_del(&r->mw_list);
1238 rc = ib_dereg_mr(r->r.frmr.fr_mr);
1240 dprintk("RPC: %s: ib_dereg_mr failed %i\n",
1242 ib_free_fast_reg_page_list(r->r.frmr.fr_pgl);
1249 rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1251 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
1254 /* clean up in reverse order from create
1255 * 1. recv mr memory (mr free, then kfree)
1256 * 2. send mr memory (mr free, then kfree)
1259 dprintk("RPC: %s: entering\n", __func__);
1261 for (i = 0; i < buf->rb_max_requests; i++) {
1262 if (buf->rb_recv_bufs && buf->rb_recv_bufs[i]) {
1263 rpcrdma_deregister_internal(ia,
1264 buf->rb_recv_bufs[i]->rr_handle,
1265 &buf->rb_recv_bufs[i]->rr_iov);
1266 kfree(buf->rb_recv_bufs[i]);
1268 if (buf->rb_send_bufs && buf->rb_send_bufs[i]) {
1269 rpcrdma_deregister_internal(ia,
1270 buf->rb_send_bufs[i]->rl_handle,
1271 &buf->rb_send_bufs[i]->rl_iov);
1272 kfree(buf->rb_send_bufs[i]);
1276 switch (ia->ri_memreg_strategy) {
1278 rpcrdma_destroy_frmrs(buf);
1280 case RPCRDMA_MTHCAFMR:
1281 rpcrdma_destroy_fmrs(buf);
1287 kfree(buf->rb_pool);
1290 /* After a disconnect, a flushed FAST_REG_MR can leave an FRMR in
1291 * an unusable state. Find FRMRs in this state and dereg / reg
1292 * each. FRMRs that are VALID and attached to an rpcrdma_req are
1295 * This gives all in-use FRMRs a fresh rkey and leaves them INVALID.
1297 * This is invoked only in the transport connect worker in order
1298 * to serialize with rpcrdma_register_frmr_external().
1301 rpcrdma_reset_frmrs(struct rpcrdma_ia *ia)
1303 struct rpcrdma_xprt *r_xprt =
1304 container_of(ia, struct rpcrdma_xprt, rx_ia);
1305 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1306 struct list_head *pos;
1307 struct rpcrdma_mw *r;
1310 list_for_each(pos, &buf->rb_all) {
1311 r = list_entry(pos, struct rpcrdma_mw, mw_all);
1313 if (r->r.frmr.fr_state == FRMR_IS_INVALID)
1316 rc = ib_dereg_mr(r->r.frmr.fr_mr);
1318 dprintk("RPC: %s: ib_dereg_mr failed %i\n",
1320 ib_free_fast_reg_page_list(r->r.frmr.fr_pgl);
1322 r->r.frmr.fr_mr = ib_alloc_fast_reg_mr(ia->ri_pd,
1323 ia->ri_max_frmr_depth);
1324 if (IS_ERR(r->r.frmr.fr_mr)) {
1325 rc = PTR_ERR(r->r.frmr.fr_mr);
1326 dprintk("RPC: %s: ib_alloc_fast_reg_mr"
1327 " failed %i\n", __func__, rc);
1330 r->r.frmr.fr_pgl = ib_alloc_fast_reg_page_list(
1332 ia->ri_max_frmr_depth);
1333 if (IS_ERR(r->r.frmr.fr_pgl)) {
1334 rc = PTR_ERR(r->r.frmr.fr_pgl);
1336 "ib_alloc_fast_reg_page_list "
1337 "failed %i\n", __func__, rc);
1339 ib_dereg_mr(r->r.frmr.fr_mr);
1342 r->r.frmr.fr_state = FRMR_IS_INVALID;
1346 /* "*mw" can be NULL when rpcrdma_buffer_get_mrs() fails, leaving
1347 * some req segments uninitialized.
1350 rpcrdma_buffer_put_mr(struct rpcrdma_mw **mw, struct rpcrdma_buffer *buf)
1353 list_add_tail(&(*mw)->mw_list, &buf->rb_mws);
1358 /* Cycle mw's back in reverse order, and "spin" them.
1359 * This delays and scrambles reuse as much as possible.
1362 rpcrdma_buffer_put_mrs(struct rpcrdma_req *req, struct rpcrdma_buffer *buf)
1364 struct rpcrdma_mr_seg *seg = req->rl_segments;
1365 struct rpcrdma_mr_seg *seg1 = seg;
1368 for (i = 1, seg++; i < RPCRDMA_MAX_SEGS; seg++, i++)
1369 rpcrdma_buffer_put_mr(&seg->mr_chunk.rl_mw, buf);
1370 rpcrdma_buffer_put_mr(&seg1->mr_chunk.rl_mw, buf);
1374 rpcrdma_buffer_put_sendbuf(struct rpcrdma_req *req, struct rpcrdma_buffer *buf)
1376 buf->rb_send_bufs[--buf->rb_send_index] = req;
1378 if (req->rl_reply) {
1379 buf->rb_recv_bufs[--buf->rb_recv_index] = req->rl_reply;
1380 req->rl_reply->rr_func = NULL;
1381 req->rl_reply = NULL;
1385 /* rpcrdma_unmap_one() was already done by rpcrdma_deregister_frmr_external().
1386 * Redo only the ib_post_send().
1389 rpcrdma_retry_local_inv(struct rpcrdma_mw *r, struct rpcrdma_ia *ia)
1391 struct rpcrdma_xprt *r_xprt =
1392 container_of(ia, struct rpcrdma_xprt, rx_ia);
1393 struct ib_send_wr invalidate_wr, *bad_wr;
1396 dprintk("RPC: %s: FRMR %p is stale\n", __func__, r);
1398 /* When this FRMR is re-inserted into rb_mws, it is no longer stale */
1399 r->r.frmr.fr_state = FRMR_IS_INVALID;
1401 memset(&invalidate_wr, 0, sizeof(invalidate_wr));
1402 invalidate_wr.wr_id = (unsigned long)(void *)r;
1403 invalidate_wr.opcode = IB_WR_LOCAL_INV;
1404 invalidate_wr.ex.invalidate_rkey = r->r.frmr.fr_mr->rkey;
1405 DECR_CQCOUNT(&r_xprt->rx_ep);
1407 dprintk("RPC: %s: frmr %p invalidating rkey %08x\n",
1408 __func__, r, r->r.frmr.fr_mr->rkey);
1410 read_lock(&ia->ri_qplock);
1411 rc = ib_post_send(ia->ri_id->qp, &invalidate_wr, &bad_wr);
1412 read_unlock(&ia->ri_qplock);
1414 /* Force rpcrdma_buffer_get() to retry */
1415 r->r.frmr.fr_state = FRMR_IS_STALE;
1416 dprintk("RPC: %s: ib_post_send failed, %i\n",
1422 rpcrdma_retry_flushed_linv(struct list_head *stale,
1423 struct rpcrdma_buffer *buf)
1425 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
1426 struct list_head *pos;
1427 struct rpcrdma_mw *r;
1428 unsigned long flags;
1430 list_for_each(pos, stale) {
1431 r = list_entry(pos, struct rpcrdma_mw, mw_list);
1432 rpcrdma_retry_local_inv(r, ia);
1435 spin_lock_irqsave(&buf->rb_lock, flags);
1436 list_splice_tail(stale, &buf->rb_mws);
1437 spin_unlock_irqrestore(&buf->rb_lock, flags);
1440 static struct rpcrdma_req *
1441 rpcrdma_buffer_get_frmrs(struct rpcrdma_req *req, struct rpcrdma_buffer *buf,
1442 struct list_head *stale)
1444 struct rpcrdma_mw *r;
1447 i = RPCRDMA_MAX_SEGS - 1;
1448 while (!list_empty(&buf->rb_mws)) {
1449 r = list_entry(buf->rb_mws.next,
1450 struct rpcrdma_mw, mw_list);
1451 list_del(&r->mw_list);
1452 if (r->r.frmr.fr_state == FRMR_IS_STALE) {
1453 list_add(&r->mw_list, stale);
1456 req->rl_segments[i].mr_chunk.rl_mw = r;
1457 if (unlikely(i-- == 0))
1458 return req; /* Success */
1461 /* Not enough entries on rb_mws for this req */
1462 rpcrdma_buffer_put_sendbuf(req, buf);
1463 rpcrdma_buffer_put_mrs(req, buf);
1467 static struct rpcrdma_req *
1468 rpcrdma_buffer_get_fmrs(struct rpcrdma_req *req, struct rpcrdma_buffer *buf)
1470 struct rpcrdma_mw *r;
1473 i = RPCRDMA_MAX_SEGS - 1;
1474 while (!list_empty(&buf->rb_mws)) {
1475 r = list_entry(buf->rb_mws.next,
1476 struct rpcrdma_mw, mw_list);
1477 list_del(&r->mw_list);
1478 req->rl_segments[i].mr_chunk.rl_mw = r;
1479 if (unlikely(i-- == 0))
1480 return req; /* Success */
1483 /* Not enough entries on rb_mws for this req */
1484 rpcrdma_buffer_put_sendbuf(req, buf);
1485 rpcrdma_buffer_put_mrs(req, buf);
1490 * Get a set of request/reply buffers.
1492 * Reply buffer (if needed) is attached to send buffer upon return.
1494 * rb_send_index and rb_recv_index MUST always be pointing to the
1495 * *next* available buffer (non-NULL). They are incremented after
1496 * removing buffers, and decremented *before* returning them.
1498 struct rpcrdma_req *
1499 rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1501 struct rpcrdma_ia *ia = rdmab_to_ia(buffers);
1502 struct list_head stale;
1503 struct rpcrdma_req *req;
1504 unsigned long flags;
1506 spin_lock_irqsave(&buffers->rb_lock, flags);
1507 if (buffers->rb_send_index == buffers->rb_max_requests) {
1508 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1509 dprintk("RPC: %s: out of request buffers\n", __func__);
1510 return ((struct rpcrdma_req *)NULL);
1513 req = buffers->rb_send_bufs[buffers->rb_send_index];
1514 if (buffers->rb_send_index < buffers->rb_recv_index) {
1515 dprintk("RPC: %s: %d extra receives outstanding (ok)\n",
1517 buffers->rb_recv_index - buffers->rb_send_index);
1518 req->rl_reply = NULL;
1520 req->rl_reply = buffers->rb_recv_bufs[buffers->rb_recv_index];
1521 buffers->rb_recv_bufs[buffers->rb_recv_index++] = NULL;
1523 buffers->rb_send_bufs[buffers->rb_send_index++] = NULL;
1525 INIT_LIST_HEAD(&stale);
1526 switch (ia->ri_memreg_strategy) {
1528 req = rpcrdma_buffer_get_frmrs(req, buffers, &stale);
1530 case RPCRDMA_MTHCAFMR:
1531 req = rpcrdma_buffer_get_fmrs(req, buffers);
1536 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1537 if (!list_empty(&stale))
1538 rpcrdma_retry_flushed_linv(&stale, buffers);
1543 * Put request/reply buffers back into pool.
1544 * Pre-decrement counter/array index.
1547 rpcrdma_buffer_put(struct rpcrdma_req *req)
1549 struct rpcrdma_buffer *buffers = req->rl_buffer;
1550 struct rpcrdma_ia *ia = rdmab_to_ia(buffers);
1551 unsigned long flags;
1553 spin_lock_irqsave(&buffers->rb_lock, flags);
1554 rpcrdma_buffer_put_sendbuf(req, buffers);
1555 switch (ia->ri_memreg_strategy) {
1557 case RPCRDMA_MTHCAFMR:
1558 rpcrdma_buffer_put_mrs(req, buffers);
1563 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1567 * Recover reply buffers from pool.
1568 * This happens when recovering from error conditions.
1569 * Post-increment counter/array index.
1572 rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1574 struct rpcrdma_buffer *buffers = req->rl_buffer;
1575 unsigned long flags;
1577 if (req->rl_iov.length == 0) /* special case xprt_rdma_allocate() */
1578 buffers = ((struct rpcrdma_req *) buffers)->rl_buffer;
1579 spin_lock_irqsave(&buffers->rb_lock, flags);
1580 if (buffers->rb_recv_index < buffers->rb_max_requests) {
1581 req->rl_reply = buffers->rb_recv_bufs[buffers->rb_recv_index];
1582 buffers->rb_recv_bufs[buffers->rb_recv_index++] = NULL;
1584 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1588 * Put reply buffers back into pool when not attached to
1589 * request. This happens in error conditions.
1592 rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1594 struct rpcrdma_buffer *buffers = rep->rr_buffer;
1595 unsigned long flags;
1597 rep->rr_func = NULL;
1598 spin_lock_irqsave(&buffers->rb_lock, flags);
1599 buffers->rb_recv_bufs[--buffers->rb_recv_index] = rep;
1600 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1604 * Wrappers for internal-use kmalloc memory registration, used by buffer code.
1608 rpcrdma_register_internal(struct rpcrdma_ia *ia, void *va, int len,
1609 struct ib_mr **mrp, struct ib_sge *iov)
1611 struct ib_phys_buf ipb;
1616 * All memory passed here was kmalloc'ed, therefore phys-contiguous.
1618 iov->addr = ib_dma_map_single(ia->ri_id->device,
1619 va, len, DMA_BIDIRECTIONAL);
1620 if (ib_dma_mapping_error(ia->ri_id->device, iov->addr))
1625 if (ia->ri_have_dma_lkey) {
1627 iov->lkey = ia->ri_dma_lkey;
1629 } else if (ia->ri_bind_mem != NULL) {
1631 iov->lkey = ia->ri_bind_mem->lkey;
1635 ipb.addr = iov->addr;
1636 ipb.size = iov->length;
1637 mr = ib_reg_phys_mr(ia->ri_pd, &ipb, 1,
1638 IB_ACCESS_LOCAL_WRITE, &iov->addr);
1640 dprintk("RPC: %s: phys convert: 0x%llx "
1641 "registered 0x%llx length %d\n",
1642 __func__, (unsigned long long)ipb.addr,
1643 (unsigned long long)iov->addr, len);
1648 dprintk("RPC: %s: failed with %i\n", __func__, rc);
1651 iov->lkey = mr->lkey;
1659 rpcrdma_deregister_internal(struct rpcrdma_ia *ia,
1660 struct ib_mr *mr, struct ib_sge *iov)
1664 ib_dma_unmap_single(ia->ri_id->device,
1665 iov->addr, iov->length, DMA_BIDIRECTIONAL);
1670 rc = ib_dereg_mr(mr);
1672 dprintk("RPC: %s: ib_dereg_mr failed %i\n", __func__, rc);
1677 * Wrappers for chunk registration, shared by read/write chunk code.
1681 rpcrdma_map_one(struct rpcrdma_ia *ia, struct rpcrdma_mr_seg *seg, int writing)
1683 seg->mr_dir = writing ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1684 seg->mr_dmalen = seg->mr_len;
1686 seg->mr_dma = ib_dma_map_page(ia->ri_id->device,
1687 seg->mr_page, offset_in_page(seg->mr_offset),
1688 seg->mr_dmalen, seg->mr_dir);
1690 seg->mr_dma = ib_dma_map_single(ia->ri_id->device,
1692 seg->mr_dmalen, seg->mr_dir);
1693 if (ib_dma_mapping_error(ia->ri_id->device, seg->mr_dma)) {
1694 dprintk("RPC: %s: mr_dma %llx mr_offset %p mr_dma_len %zu\n",
1696 (unsigned long long)seg->mr_dma,
1697 seg->mr_offset, seg->mr_dmalen);
1702 rpcrdma_unmap_one(struct rpcrdma_ia *ia, struct rpcrdma_mr_seg *seg)
1705 ib_dma_unmap_page(ia->ri_id->device,
1706 seg->mr_dma, seg->mr_dmalen, seg->mr_dir);
1708 ib_dma_unmap_single(ia->ri_id->device,
1709 seg->mr_dma, seg->mr_dmalen, seg->mr_dir);
1713 rpcrdma_register_frmr_external(struct rpcrdma_mr_seg *seg,
1714 int *nsegs, int writing, struct rpcrdma_ia *ia,
1715 struct rpcrdma_xprt *r_xprt)
1717 struct rpcrdma_mr_seg *seg1 = seg;
1718 struct rpcrdma_mw *mw = seg1->mr_chunk.rl_mw;
1719 struct rpcrdma_frmr *frmr = &mw->r.frmr;
1720 struct ib_mr *mr = frmr->fr_mr;
1721 struct ib_send_wr fastreg_wr, *bad_wr;
1729 pageoff = offset_in_page(seg1->mr_offset);
1730 seg1->mr_offset -= pageoff; /* start of page */
1731 seg1->mr_len += pageoff;
1733 if (*nsegs > ia->ri_max_frmr_depth)
1734 *nsegs = ia->ri_max_frmr_depth;
1735 for (page_no = i = 0; i < *nsegs;) {
1736 rpcrdma_map_one(ia, seg, writing);
1738 for (seg_len = seg->mr_len; seg_len > 0; seg_len -= PAGE_SIZE) {
1739 frmr->fr_pgl->page_list[page_no++] = pa;
1745 /* Check for holes */
1746 if ((i < *nsegs && offset_in_page(seg->mr_offset)) ||
1747 offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len))
1750 dprintk("RPC: %s: Using frmr %p to map %d segments\n",
1753 frmr->fr_state = FRMR_IS_VALID;
1755 memset(&fastreg_wr, 0, sizeof(fastreg_wr));
1756 fastreg_wr.wr_id = (unsigned long)(void *)mw;
1757 fastreg_wr.opcode = IB_WR_FAST_REG_MR;
1758 fastreg_wr.wr.fast_reg.iova_start = seg1->mr_dma;
1759 fastreg_wr.wr.fast_reg.page_list = frmr->fr_pgl;
1760 fastreg_wr.wr.fast_reg.page_list_len = page_no;
1761 fastreg_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
1762 fastreg_wr.wr.fast_reg.length = page_no << PAGE_SHIFT;
1763 if (fastreg_wr.wr.fast_reg.length < len) {
1769 key = (u8)(mr->rkey & 0x000000FF);
1770 ib_update_fast_reg_key(mr, ++key);
1772 fastreg_wr.wr.fast_reg.access_flags = (writing ?
1773 IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
1774 IB_ACCESS_REMOTE_READ);
1775 fastreg_wr.wr.fast_reg.rkey = mr->rkey;
1776 DECR_CQCOUNT(&r_xprt->rx_ep);
1778 rc = ib_post_send(ia->ri_id->qp, &fastreg_wr, &bad_wr);
1780 dprintk("RPC: %s: failed ib_post_send for register,"
1781 " status %i\n", __func__, rc);
1782 ib_update_fast_reg_key(mr, --key);
1785 seg1->mr_rkey = mr->rkey;
1786 seg1->mr_base = seg1->mr_dma + pageoff;
1793 frmr->fr_state = FRMR_IS_INVALID;
1795 rpcrdma_unmap_one(ia, --seg);
1800 rpcrdma_deregister_frmr_external(struct rpcrdma_mr_seg *seg,
1801 struct rpcrdma_ia *ia, struct rpcrdma_xprt *r_xprt)
1803 struct rpcrdma_mr_seg *seg1 = seg;
1804 struct ib_send_wr invalidate_wr, *bad_wr;
1807 seg1->mr_chunk.rl_mw->r.frmr.fr_state = FRMR_IS_INVALID;
1809 memset(&invalidate_wr, 0, sizeof invalidate_wr);
1810 invalidate_wr.wr_id = (unsigned long)(void *)seg1->mr_chunk.rl_mw;
1811 invalidate_wr.opcode = IB_WR_LOCAL_INV;
1812 invalidate_wr.ex.invalidate_rkey = seg1->mr_chunk.rl_mw->r.frmr.fr_mr->rkey;
1813 DECR_CQCOUNT(&r_xprt->rx_ep);
1815 read_lock(&ia->ri_qplock);
1816 while (seg1->mr_nsegs--)
1817 rpcrdma_unmap_one(ia, seg++);
1818 rc = ib_post_send(ia->ri_id->qp, &invalidate_wr, &bad_wr);
1819 read_unlock(&ia->ri_qplock);
1821 /* Force rpcrdma_buffer_get() to retry */
1822 seg1->mr_chunk.rl_mw->r.frmr.fr_state = FRMR_IS_STALE;
1823 dprintk("RPC: %s: failed ib_post_send for invalidate,"
1824 " status %i\n", __func__, rc);
1830 rpcrdma_register_fmr_external(struct rpcrdma_mr_seg *seg,
1831 int *nsegs, int writing, struct rpcrdma_ia *ia)
1833 struct rpcrdma_mr_seg *seg1 = seg;
1834 u64 physaddrs[RPCRDMA_MAX_DATA_SEGS];
1835 int len, pageoff, i, rc;
1837 pageoff = offset_in_page(seg1->mr_offset);
1838 seg1->mr_offset -= pageoff; /* start of page */
1839 seg1->mr_len += pageoff;
1841 if (*nsegs > RPCRDMA_MAX_DATA_SEGS)
1842 *nsegs = RPCRDMA_MAX_DATA_SEGS;
1843 for (i = 0; i < *nsegs;) {
1844 rpcrdma_map_one(ia, seg, writing);
1845 physaddrs[i] = seg->mr_dma;
1849 /* Check for holes */
1850 if ((i < *nsegs && offset_in_page(seg->mr_offset)) ||
1851 offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len))
1854 rc = ib_map_phys_fmr(seg1->mr_chunk.rl_mw->r.fmr,
1855 physaddrs, i, seg1->mr_dma);
1857 dprintk("RPC: %s: failed ib_map_phys_fmr "
1858 "%u@0x%llx+%i (%d)... status %i\n", __func__,
1859 len, (unsigned long long)seg1->mr_dma,
1862 rpcrdma_unmap_one(ia, --seg);
1864 seg1->mr_rkey = seg1->mr_chunk.rl_mw->r.fmr->rkey;
1865 seg1->mr_base = seg1->mr_dma + pageoff;
1874 rpcrdma_deregister_fmr_external(struct rpcrdma_mr_seg *seg,
1875 struct rpcrdma_ia *ia)
1877 struct rpcrdma_mr_seg *seg1 = seg;
1881 list_add(&seg1->mr_chunk.rl_mw->r.fmr->list, &l);
1882 rc = ib_unmap_fmr(&l);
1883 read_lock(&ia->ri_qplock);
1884 while (seg1->mr_nsegs--)
1885 rpcrdma_unmap_one(ia, seg++);
1886 read_unlock(&ia->ri_qplock);
1888 dprintk("RPC: %s: failed ib_unmap_fmr,"
1889 " status %i\n", __func__, rc);
1894 rpcrdma_register_external(struct rpcrdma_mr_seg *seg,
1895 int nsegs, int writing, struct rpcrdma_xprt *r_xprt)
1897 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
1900 switch (ia->ri_memreg_strategy) {
1902 case RPCRDMA_ALLPHYSICAL:
1903 rpcrdma_map_one(ia, seg, writing);
1904 seg->mr_rkey = ia->ri_bind_mem->rkey;
1905 seg->mr_base = seg->mr_dma;
1910 /* Registration using frmr registration */
1912 rc = rpcrdma_register_frmr_external(seg, &nsegs, writing, ia, r_xprt);
1915 /* Registration using fmr memory registration */
1916 case RPCRDMA_MTHCAFMR:
1917 rc = rpcrdma_register_fmr_external(seg, &nsegs, writing, ia);
1930 rpcrdma_deregister_external(struct rpcrdma_mr_seg *seg,
1931 struct rpcrdma_xprt *r_xprt)
1933 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
1934 int nsegs = seg->mr_nsegs, rc;
1936 switch (ia->ri_memreg_strategy) {
1938 case RPCRDMA_ALLPHYSICAL:
1939 read_lock(&ia->ri_qplock);
1940 rpcrdma_unmap_one(ia, seg);
1941 read_unlock(&ia->ri_qplock);
1945 rc = rpcrdma_deregister_frmr_external(seg, ia, r_xprt);
1948 case RPCRDMA_MTHCAFMR:
1949 rc = rpcrdma_deregister_fmr_external(seg, ia);
1959 * Prepost any receive buffer, then post send.
1961 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1964 rpcrdma_ep_post(struct rpcrdma_ia *ia,
1965 struct rpcrdma_ep *ep,
1966 struct rpcrdma_req *req)
1968 struct ib_send_wr send_wr, *send_wr_fail;
1969 struct rpcrdma_rep *rep = req->rl_reply;
1973 rc = rpcrdma_ep_post_recv(ia, ep, rep);
1976 req->rl_reply = NULL;
1979 send_wr.next = NULL;
1980 send_wr.wr_id = 0ULL; /* no send cookie */
1981 send_wr.sg_list = req->rl_send_iov;
1982 send_wr.num_sge = req->rl_niovs;
1983 send_wr.opcode = IB_WR_SEND;
1984 if (send_wr.num_sge == 4) /* no need to sync any pad (constant) */
1985 ib_dma_sync_single_for_device(ia->ri_id->device,
1986 req->rl_send_iov[3].addr, req->rl_send_iov[3].length,
1988 ib_dma_sync_single_for_device(ia->ri_id->device,
1989 req->rl_send_iov[1].addr, req->rl_send_iov[1].length,
1991 ib_dma_sync_single_for_device(ia->ri_id->device,
1992 req->rl_send_iov[0].addr, req->rl_send_iov[0].length,
1995 if (DECR_CQCOUNT(ep) > 0)
1996 send_wr.send_flags = 0;
1997 else { /* Provider must take a send completion every now and then */
1999 send_wr.send_flags = IB_SEND_SIGNALED;
2002 rc = ib_post_send(ia->ri_id->qp, &send_wr, &send_wr_fail);
2004 dprintk("RPC: %s: ib_post_send returned %i\n", __func__,
2011 * (Re)post a receive buffer.
2014 rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
2015 struct rpcrdma_ep *ep,
2016 struct rpcrdma_rep *rep)
2018 struct ib_recv_wr recv_wr, *recv_wr_fail;
2021 recv_wr.next = NULL;
2022 recv_wr.wr_id = (u64) (unsigned long) rep;
2023 recv_wr.sg_list = &rep->rr_iov;
2024 recv_wr.num_sge = 1;
2026 ib_dma_sync_single_for_cpu(ia->ri_id->device,
2027 rep->rr_iov.addr, rep->rr_iov.length, DMA_BIDIRECTIONAL);
2029 rc = ib_post_recv(ia->ri_id->qp, &recv_wr, &recv_wr_fail);
2032 dprintk("RPC: %s: ib_post_recv returned %i\n", __func__,
2037 /* Physical mapping means one Read/Write list entry per-page.
2038 * All list entries must fit within an inline buffer
2040 * NB: The server must return a Write list for NFS READ,
2041 * which has the same constraint. Factor in the inline
2045 rpcrdma_physical_max_payload(struct rpcrdma_xprt *r_xprt)
2047 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
2048 unsigned int inline_size, pages;
2050 inline_size = min_t(unsigned int,
2051 cdata->inline_wsize, cdata->inline_rsize);
2052 inline_size -= RPCRDMA_HDRLEN_MIN;
2053 pages = inline_size / sizeof(struct rpcrdma_segment);
2054 return pages << PAGE_SHIFT;
2058 rpcrdma_mr_max_payload(struct rpcrdma_xprt *r_xprt)
2060 return RPCRDMA_MAX_DATA_SEGS << PAGE_SHIFT;
2064 rpcrdma_max_payload(struct rpcrdma_xprt *r_xprt)
2068 switch (r_xprt->rx_ia.ri_memreg_strategy) {
2069 case RPCRDMA_ALLPHYSICAL:
2070 result = rpcrdma_physical_max_payload(r_xprt);
2073 result = rpcrdma_mr_max_payload(r_xprt);