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
69 * handle replies in tasklet context, using a single, global list
70 * rdma tasklet function -- just turn around and call the func
71 * for all replies on the list
74 static DEFINE_SPINLOCK(rpcrdma_tk_lock_g);
75 static LIST_HEAD(rpcrdma_tasklets_g);
78 rpcrdma_run_tasklet(unsigned long data)
80 struct rpcrdma_rep *rep;
81 void (*func)(struct rpcrdma_rep *);
85 spin_lock_irqsave(&rpcrdma_tk_lock_g, flags);
86 while (!list_empty(&rpcrdma_tasklets_g)) {
87 rep = list_entry(rpcrdma_tasklets_g.next,
88 struct rpcrdma_rep, rr_list);
89 list_del(&rep->rr_list);
92 spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
97 rpcrdma_recv_buffer_put(rep);
99 spin_lock_irqsave(&rpcrdma_tk_lock_g, flags);
101 spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
104 static DECLARE_TASKLET(rpcrdma_tasklet_g, rpcrdma_run_tasklet, 0UL);
107 rpcrdma_schedule_tasklet(struct rpcrdma_rep *rep)
111 spin_lock_irqsave(&rpcrdma_tk_lock_g, flags);
112 list_add_tail(&rep->rr_list, &rpcrdma_tasklets_g);
113 spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
114 tasklet_schedule(&rpcrdma_tasklet_g);
118 rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context)
120 struct rpcrdma_ep *ep = context;
122 dprintk("RPC: %s: QP error %X on device %s ep %p\n",
123 __func__, event->event, event->device->name, context);
124 if (ep->rep_connected == 1) {
125 ep->rep_connected = -EIO;
127 wake_up_all(&ep->rep_connect_wait);
132 rpcrdma_cq_async_error_upcall(struct ib_event *event, void *context)
134 struct rpcrdma_ep *ep = context;
136 dprintk("RPC: %s: CQ error %X on device %s ep %p\n",
137 __func__, event->event, event->device->name, context);
138 if (ep->rep_connected == 1) {
139 ep->rep_connected = -EIO;
141 wake_up_all(&ep->rep_connect_wait);
146 rpcrdma_sendcq_process_wc(struct ib_wc *wc)
148 struct rpcrdma_mw *frmr = (struct rpcrdma_mw *)(unsigned long)wc->wr_id;
150 dprintk("RPC: %s: frmr %p status %X opcode %d\n",
151 __func__, frmr, wc->status, wc->opcode);
153 if (wc->wr_id == 0ULL)
155 if (wc->status != IB_WC_SUCCESS)
158 if (wc->opcode == IB_WC_FAST_REG_MR)
159 frmr->r.frmr.state = FRMR_IS_VALID;
160 else if (wc->opcode == IB_WC_LOCAL_INV)
161 frmr->r.frmr.state = FRMR_IS_INVALID;
165 rpcrdma_sendcq_poll(struct ib_cq *cq, struct rpcrdma_ep *ep)
168 int budget, count, rc;
170 budget = RPCRDMA_WC_BUDGET / RPCRDMA_POLLSIZE;
172 wcs = ep->rep_send_wcs;
174 rc = ib_poll_cq(cq, RPCRDMA_POLLSIZE, wcs);
180 rpcrdma_sendcq_process_wc(wcs++);
181 } while (rc == RPCRDMA_POLLSIZE && --budget);
185 /* Handle provider send completion upcalls.
188 rpcrdma_sendcq_upcall(struct ib_cq *cq, void *cq_context)
190 struct rpcrdma_ep *ep = (struct rpcrdma_ep *)cq_context;
193 rpcrdma_sendcq_poll(cq, ep);
194 } while (ib_req_notify_cq(cq, IB_CQ_NEXT_COMP |
195 IB_CQ_REPORT_MISSED_EVENTS) > 0);
199 rpcrdma_recvcq_process_wc(struct ib_wc *wc)
201 struct rpcrdma_rep *rep =
202 (struct rpcrdma_rep *)(unsigned long)wc->wr_id;
204 dprintk("RPC: %s: rep %p status %X opcode %X length %u\n",
205 __func__, rep, wc->status, wc->opcode, wc->byte_len);
207 if (wc->status != IB_WC_SUCCESS) {
211 if (wc->opcode != IB_WC_RECV)
214 rep->rr_len = wc->byte_len;
215 ib_dma_sync_single_for_cpu(rdmab_to_ia(rep->rr_buffer)->ri_id->device,
216 rep->rr_iov.addr, rep->rr_len, DMA_FROM_DEVICE);
218 if (rep->rr_len >= 16) {
219 struct rpcrdma_msg *p = (struct rpcrdma_msg *)rep->rr_base;
220 unsigned int credits = ntohl(p->rm_credit);
223 credits = 1; /* don't deadlock */
224 else if (credits > rep->rr_buffer->rb_max_requests)
225 credits = rep->rr_buffer->rb_max_requests;
226 atomic_set(&rep->rr_buffer->rb_credits, credits);
230 rpcrdma_schedule_tasklet(rep);
234 rpcrdma_recvcq_poll(struct ib_cq *cq, struct rpcrdma_ep *ep)
237 int budget, count, rc;
239 budget = RPCRDMA_WC_BUDGET / RPCRDMA_POLLSIZE;
241 wcs = ep->rep_recv_wcs;
243 rc = ib_poll_cq(cq, RPCRDMA_POLLSIZE, wcs);
249 rpcrdma_recvcq_process_wc(wcs++);
250 } while (rc == RPCRDMA_POLLSIZE && --budget);
254 /* Handle provider receive completion upcalls.
257 rpcrdma_recvcq_upcall(struct ib_cq *cq, void *cq_context)
259 struct rpcrdma_ep *ep = (struct rpcrdma_ep *)cq_context;
262 rpcrdma_recvcq_poll(cq, ep);
263 } while (ib_req_notify_cq(cq, IB_CQ_NEXT_COMP |
264 IB_CQ_REPORT_MISSED_EVENTS) > 0);
268 static const char * const conn[] = {
285 rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
287 struct rpcrdma_xprt *xprt = id->context;
288 struct rpcrdma_ia *ia = &xprt->rx_ia;
289 struct rpcrdma_ep *ep = &xprt->rx_ep;
291 struct sockaddr_in *addr = (struct sockaddr_in *) &ep->rep_remote_addr;
293 struct ib_qp_attr attr;
294 struct ib_qp_init_attr iattr;
297 switch (event->event) {
298 case RDMA_CM_EVENT_ADDR_RESOLVED:
299 case RDMA_CM_EVENT_ROUTE_RESOLVED:
301 complete(&ia->ri_done);
303 case RDMA_CM_EVENT_ADDR_ERROR:
304 ia->ri_async_rc = -EHOSTUNREACH;
305 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
307 complete(&ia->ri_done);
309 case RDMA_CM_EVENT_ROUTE_ERROR:
310 ia->ri_async_rc = -ENETUNREACH;
311 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
313 complete(&ia->ri_done);
315 case RDMA_CM_EVENT_ESTABLISHED:
317 ib_query_qp(ia->ri_id->qp, &attr,
318 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_MAX_DEST_RD_ATOMIC,
320 dprintk("RPC: %s: %d responder resources"
322 __func__, attr.max_dest_rd_atomic, attr.max_rd_atomic);
324 case RDMA_CM_EVENT_CONNECT_ERROR:
325 connstate = -ENOTCONN;
327 case RDMA_CM_EVENT_UNREACHABLE:
328 connstate = -ENETDOWN;
330 case RDMA_CM_EVENT_REJECTED:
331 connstate = -ECONNREFUSED;
333 case RDMA_CM_EVENT_DISCONNECTED:
334 connstate = -ECONNABORTED;
336 case RDMA_CM_EVENT_DEVICE_REMOVAL:
339 dprintk("RPC: %s: %s: %pI4:%u (ep 0x%p event 0x%x)\n",
341 (event->event <= 11) ? conn[event->event] :
342 "unknown connection error",
343 &addr->sin_addr.s_addr,
344 ntohs(addr->sin_port),
346 atomic_set(&rpcx_to_rdmax(ep->rep_xprt)->rx_buf.rb_credits, 1);
347 dprintk("RPC: %s: %sconnected\n",
348 __func__, connstate > 0 ? "" : "dis");
349 ep->rep_connected = connstate;
351 wake_up_all(&ep->rep_connect_wait);
354 dprintk("RPC: %s: unexpected CM event %d\n",
355 __func__, event->event);
360 if (connstate == 1) {
361 int ird = attr.max_dest_rd_atomic;
362 int tird = ep->rep_remote_cma.responder_resources;
363 printk(KERN_INFO "rpcrdma: connection to %pI4:%u "
364 "on %s, memreg %d slots %d ird %d%s\n",
365 &addr->sin_addr.s_addr,
366 ntohs(addr->sin_port),
367 ia->ri_id->device->name,
368 ia->ri_memreg_strategy,
369 xprt->rx_buf.rb_max_requests,
370 ird, ird < 4 && ird < tird / 2 ? " (low!)" : "");
371 } else if (connstate < 0) {
372 printk(KERN_INFO "rpcrdma: connection to %pI4:%u closed (%d)\n",
373 &addr->sin_addr.s_addr,
374 ntohs(addr->sin_port),
382 static struct rdma_cm_id *
383 rpcrdma_create_id(struct rpcrdma_xprt *xprt,
384 struct rpcrdma_ia *ia, struct sockaddr *addr)
386 struct rdma_cm_id *id;
389 init_completion(&ia->ri_done);
391 id = rdma_create_id(rpcrdma_conn_upcall, xprt, RDMA_PS_TCP, IB_QPT_RC);
394 dprintk("RPC: %s: rdma_create_id() failed %i\n",
399 ia->ri_async_rc = -ETIMEDOUT;
400 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
402 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
406 wait_for_completion_interruptible_timeout(&ia->ri_done,
407 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
408 rc = ia->ri_async_rc;
412 ia->ri_async_rc = -ETIMEDOUT;
413 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
415 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
419 wait_for_completion_interruptible_timeout(&ia->ri_done,
420 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
421 rc = ia->ri_async_rc;
433 * Drain any cq, prior to teardown.
436 rpcrdma_clean_cq(struct ib_cq *cq)
441 while (1 == ib_poll_cq(cq, 1, &wc))
445 dprintk("RPC: %s: flushed %d events (last 0x%x)\n",
446 __func__, count, wc.opcode);
450 * Exported functions.
454 * Open and initialize an Interface Adapter.
455 * o initializes fields of struct rpcrdma_ia, including
456 * interface and provider attributes and protection zone.
459 rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr, int memreg)
462 struct ib_device_attr devattr;
463 struct rpcrdma_ia *ia = &xprt->rx_ia;
465 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
466 if (IS_ERR(ia->ri_id)) {
467 rc = PTR_ERR(ia->ri_id);
471 ia->ri_pd = ib_alloc_pd(ia->ri_id->device);
472 if (IS_ERR(ia->ri_pd)) {
473 rc = PTR_ERR(ia->ri_pd);
474 dprintk("RPC: %s: ib_alloc_pd() failed %i\n",
480 * Query the device to determine if the requested memory
481 * registration strategy is supported. If it isn't, set the
482 * strategy to a globally supported model.
484 rc = ib_query_device(ia->ri_id->device, &devattr);
486 dprintk("RPC: %s: ib_query_device failed %d\n",
491 if (devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY) {
492 ia->ri_have_dma_lkey = 1;
493 ia->ri_dma_lkey = ia->ri_id->device->local_dma_lkey;
496 if (memreg == RPCRDMA_FRMR) {
497 /* Requires both frmr reg and local dma lkey */
498 if (((devattr.device_cap_flags &
499 (IB_DEVICE_MEM_MGT_EXTENSIONS|IB_DEVICE_LOCAL_DMA_LKEY)) !=
500 (IB_DEVICE_MEM_MGT_EXTENSIONS|IB_DEVICE_LOCAL_DMA_LKEY)) ||
501 (devattr.max_fast_reg_page_list_len == 0)) {
502 dprintk("RPC: %s: FRMR registration "
503 "not supported by HCA\n", __func__);
504 memreg = RPCRDMA_MTHCAFMR;
506 /* Mind the ia limit on FRMR page list depth */
507 ia->ri_max_frmr_depth = min_t(unsigned int,
508 RPCRDMA_MAX_DATA_SEGS,
509 devattr.max_fast_reg_page_list_len);
512 if (memreg == RPCRDMA_MTHCAFMR) {
513 if (!ia->ri_id->device->alloc_fmr) {
514 dprintk("RPC: %s: MTHCAFMR registration "
515 "not supported by HCA\n", __func__);
516 #if RPCRDMA_PERSISTENT_REGISTRATION
517 memreg = RPCRDMA_ALLPHYSICAL;
526 * Optionally obtain an underlying physical identity mapping in
527 * order to do a memory window-based bind. This base registration
528 * is protected from remote access - that is enabled only by binding
529 * for the specific bytes targeted during each RPC operation, and
530 * revoked after the corresponding completion similar to a storage
536 #if RPCRDMA_PERSISTENT_REGISTRATION
537 case RPCRDMA_ALLPHYSICAL:
538 mem_priv = IB_ACCESS_LOCAL_WRITE |
539 IB_ACCESS_REMOTE_WRITE |
540 IB_ACCESS_REMOTE_READ;
543 case RPCRDMA_MTHCAFMR:
544 if (ia->ri_have_dma_lkey)
546 mem_priv = IB_ACCESS_LOCAL_WRITE;
547 #if RPCRDMA_PERSISTENT_REGISTRATION
550 ia->ri_bind_mem = ib_get_dma_mr(ia->ri_pd, mem_priv);
551 if (IS_ERR(ia->ri_bind_mem)) {
552 printk(KERN_ALERT "%s: ib_get_dma_mr for "
553 "phys register failed with %lX\n",
554 __func__, PTR_ERR(ia->ri_bind_mem));
560 printk(KERN_ERR "RPC: Unsupported memory "
561 "registration mode: %d\n", memreg);
565 dprintk("RPC: %s: memory registration strategy is %d\n",
568 /* Else will do memory reg/dereg for each chunk */
569 ia->ri_memreg_strategy = memreg;
574 ib_dealloc_pd(ia->ri_pd);
577 rdma_destroy_id(ia->ri_id);
584 * Clean up/close an IA.
585 * o if event handles and PD have been initialized, free them.
589 rpcrdma_ia_close(struct rpcrdma_ia *ia)
593 dprintk("RPC: %s: entering\n", __func__);
594 if (ia->ri_bind_mem != NULL) {
595 rc = ib_dereg_mr(ia->ri_bind_mem);
596 dprintk("RPC: %s: ib_dereg_mr returned %i\n",
599 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
601 rdma_destroy_qp(ia->ri_id);
602 rdma_destroy_id(ia->ri_id);
605 if (ia->ri_pd != NULL && !IS_ERR(ia->ri_pd)) {
606 rc = ib_dealloc_pd(ia->ri_pd);
607 dprintk("RPC: %s: ib_dealloc_pd returned %i\n",
613 * Create unconnected endpoint.
616 rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
617 struct rpcrdma_create_data_internal *cdata)
619 struct ib_device_attr devattr;
620 struct ib_cq *sendcq, *recvcq;
623 rc = ib_query_device(ia->ri_id->device, &devattr);
625 dprintk("RPC: %s: ib_query_device failed %d\n",
630 /* check provider's send/recv wr limits */
631 if (cdata->max_requests > devattr.max_qp_wr)
632 cdata->max_requests = devattr.max_qp_wr;
634 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
635 ep->rep_attr.qp_context = ep;
636 /* send_cq and recv_cq initialized below */
637 ep->rep_attr.srq = NULL;
638 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
639 switch (ia->ri_memreg_strategy) {
643 /* Add room for frmr register and invalidate WRs.
644 * 1. FRMR reg WR for head
645 * 2. FRMR invalidate WR for head
646 * 3. N FRMR reg WRs for pagelist
647 * 4. N FRMR invalidate WRs for pagelist
648 * 5. FRMR reg WR for tail
649 * 6. FRMR invalidate WR for tail
650 * 7. The RDMA_SEND WR
653 /* Calculate N if the device max FRMR depth is smaller than
654 * RPCRDMA_MAX_DATA_SEGS.
656 if (ia->ri_max_frmr_depth < RPCRDMA_MAX_DATA_SEGS) {
657 int delta = RPCRDMA_MAX_DATA_SEGS -
658 ia->ri_max_frmr_depth;
661 depth += 2; /* FRMR reg + invalidate */
662 delta -= ia->ri_max_frmr_depth;
666 ep->rep_attr.cap.max_send_wr *= depth;
667 if (ep->rep_attr.cap.max_send_wr > devattr.max_qp_wr) {
668 cdata->max_requests = devattr.max_qp_wr / depth;
669 if (!cdata->max_requests)
671 ep->rep_attr.cap.max_send_wr = cdata->max_requests *
679 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
680 ep->rep_attr.cap.max_send_sge = (cdata->padding ? 4 : 2);
681 ep->rep_attr.cap.max_recv_sge = 1;
682 ep->rep_attr.cap.max_inline_data = 0;
683 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
684 ep->rep_attr.qp_type = IB_QPT_RC;
685 ep->rep_attr.port_num = ~0;
687 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
688 "iovs: send %d recv %d\n",
690 ep->rep_attr.cap.max_send_wr,
691 ep->rep_attr.cap.max_recv_wr,
692 ep->rep_attr.cap.max_send_sge,
693 ep->rep_attr.cap.max_recv_sge);
695 /* set trigger for requesting send completion */
696 ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
697 if (ep->rep_cqinit <= 2)
701 init_waitqueue_head(&ep->rep_connect_wait);
702 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
704 sendcq = ib_create_cq(ia->ri_id->device, rpcrdma_sendcq_upcall,
705 rpcrdma_cq_async_error_upcall, ep,
706 ep->rep_attr.cap.max_send_wr + 1, 0);
707 if (IS_ERR(sendcq)) {
708 rc = PTR_ERR(sendcq);
709 dprintk("RPC: %s: failed to create send CQ: %i\n",
714 rc = ib_req_notify_cq(sendcq, IB_CQ_NEXT_COMP);
716 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
721 recvcq = ib_create_cq(ia->ri_id->device, rpcrdma_recvcq_upcall,
722 rpcrdma_cq_async_error_upcall, ep,
723 ep->rep_attr.cap.max_recv_wr + 1, 0);
724 if (IS_ERR(recvcq)) {
725 rc = PTR_ERR(recvcq);
726 dprintk("RPC: %s: failed to create recv CQ: %i\n",
731 rc = ib_req_notify_cq(recvcq, IB_CQ_NEXT_COMP);
733 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
735 ib_destroy_cq(recvcq);
739 ep->rep_attr.send_cq = sendcq;
740 ep->rep_attr.recv_cq = recvcq;
742 /* Initialize cma parameters */
744 /* RPC/RDMA does not use private data */
745 ep->rep_remote_cma.private_data = NULL;
746 ep->rep_remote_cma.private_data_len = 0;
748 /* Client offers RDMA Read but does not initiate */
749 ep->rep_remote_cma.initiator_depth = 0;
750 if (devattr.max_qp_rd_atom > 32) /* arbitrary but <= 255 */
751 ep->rep_remote_cma.responder_resources = 32;
753 ep->rep_remote_cma.responder_resources = devattr.max_qp_rd_atom;
755 ep->rep_remote_cma.retry_count = 7;
756 ep->rep_remote_cma.flow_control = 0;
757 ep->rep_remote_cma.rnr_retry_count = 0;
762 err = ib_destroy_cq(sendcq);
764 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
773 * Disconnect and destroy endpoint. After this, the only
774 * valid operations on the ep are to free it (if dynamically
775 * allocated) or re-create it.
778 rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
782 dprintk("RPC: %s: entering, connected is %d\n",
783 __func__, ep->rep_connected);
785 cancel_delayed_work_sync(&ep->rep_connect_worker);
788 rc = rpcrdma_ep_disconnect(ep, ia);
790 dprintk("RPC: %s: rpcrdma_ep_disconnect"
791 " returned %i\n", __func__, rc);
792 rdma_destroy_qp(ia->ri_id);
793 ia->ri_id->qp = NULL;
796 /* padding - could be done in rpcrdma_buffer_destroy... */
797 if (ep->rep_pad_mr) {
798 rpcrdma_deregister_internal(ia, ep->rep_pad_mr, &ep->rep_pad);
799 ep->rep_pad_mr = NULL;
802 rpcrdma_clean_cq(ep->rep_attr.recv_cq);
803 rc = ib_destroy_cq(ep->rep_attr.recv_cq);
805 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
808 rpcrdma_clean_cq(ep->rep_attr.send_cq);
809 rc = ib_destroy_cq(ep->rep_attr.send_cq);
811 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
816 * Connect unconnected endpoint.
819 rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
821 struct rdma_cm_id *id;
825 if (ep->rep_connected != 0) {
826 struct rpcrdma_xprt *xprt;
828 dprintk("RPC: %s: reconnecting...\n", __func__);
829 rc = rpcrdma_ep_disconnect(ep, ia);
830 if (rc && rc != -ENOTCONN)
831 dprintk("RPC: %s: rpcrdma_ep_disconnect"
832 " status %i\n", __func__, rc);
834 rpcrdma_clean_cq(ep->rep_attr.recv_cq);
835 rpcrdma_clean_cq(ep->rep_attr.send_cq);
837 xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
838 id = rpcrdma_create_id(xprt, ia,
839 (struct sockaddr *)&xprt->rx_data.addr);
844 /* TEMP TEMP TEMP - fail if new device:
845 * Deregister/remarshal *all* requests!
846 * Close and recreate adapter, pd, etc!
847 * Re-determine all attributes still sane!
848 * More stuff I haven't thought of!
851 if (ia->ri_id->device != id->device) {
852 printk("RPC: %s: can't reconnect on "
853 "different device!\n", __func__);
859 rc = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
861 dprintk("RPC: %s: rdma_create_qp failed %i\n",
867 rdma_destroy_qp(ia->ri_id);
868 rdma_destroy_id(ia->ri_id);
871 dprintk("RPC: %s: connecting...\n", __func__);
872 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
874 dprintk("RPC: %s: rdma_create_qp failed %i\n",
876 /* do not update ep->rep_connected */
881 ep->rep_connected = 0;
883 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
885 dprintk("RPC: %s: rdma_connect() failed with %i\n",
890 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
893 * Check state. A non-peer reject indicates no listener
894 * (ECONNREFUSED), which may be a transient state. All
895 * others indicate a transport condition which has already
896 * undergone a best-effort.
898 if (ep->rep_connected == -ECONNREFUSED &&
899 ++retry_count <= RDMA_CONNECT_RETRY_MAX) {
900 dprintk("RPC: %s: non-peer_reject, retry\n", __func__);
903 if (ep->rep_connected <= 0) {
904 /* Sometimes, the only way to reliably connect to remote
905 * CMs is to use same nonzero values for ORD and IRD. */
906 if (retry_count++ <= RDMA_CONNECT_RETRY_MAX + 1 &&
907 (ep->rep_remote_cma.responder_resources == 0 ||
908 ep->rep_remote_cma.initiator_depth !=
909 ep->rep_remote_cma.responder_resources)) {
910 if (ep->rep_remote_cma.responder_resources == 0)
911 ep->rep_remote_cma.responder_resources = 1;
912 ep->rep_remote_cma.initiator_depth =
913 ep->rep_remote_cma.responder_resources;
916 rc = ep->rep_connected;
918 dprintk("RPC: %s: connected\n", __func__);
923 ep->rep_connected = rc;
928 * rpcrdma_ep_disconnect
930 * This is separate from destroy to facilitate the ability
931 * to reconnect without recreating the endpoint.
933 * This call is not reentrant, and must not be made in parallel
934 * on the same endpoint.
937 rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
941 rpcrdma_clean_cq(ep->rep_attr.recv_cq);
942 rpcrdma_clean_cq(ep->rep_attr.send_cq);
943 rc = rdma_disconnect(ia->ri_id);
945 /* returns without wait if not connected */
946 wait_event_interruptible(ep->rep_connect_wait,
947 ep->rep_connected != 1);
948 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
949 (ep->rep_connected == 1) ? "still " : "dis");
951 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
952 ep->rep_connected = rc;
958 * Initialize buffer memory
961 rpcrdma_buffer_create(struct rpcrdma_buffer *buf, struct rpcrdma_ep *ep,
962 struct rpcrdma_ia *ia, struct rpcrdma_create_data_internal *cdata)
965 size_t len, rlen, wlen;
967 struct rpcrdma_mw *r;
969 buf->rb_max_requests = cdata->max_requests;
970 spin_lock_init(&buf->rb_lock);
971 atomic_set(&buf->rb_credits, 1);
974 * 1. arrays for send and recv pointers
975 * 2. arrays of struct rpcrdma_req to fill in pointers
976 * 3. array of struct rpcrdma_rep for replies
978 * 5. mw's, fmr's or frmr's, if any
979 * Send/recv buffers in req/rep need to be registered
982 len = buf->rb_max_requests *
983 (sizeof(struct rpcrdma_req *) + sizeof(struct rpcrdma_rep *));
984 len += cdata->padding;
985 switch (ia->ri_memreg_strategy) {
987 len += buf->rb_max_requests * RPCRDMA_MAX_SEGS *
988 sizeof(struct rpcrdma_mw);
990 case RPCRDMA_MTHCAFMR:
991 /* TBD we are perhaps overallocating here */
992 len += (buf->rb_max_requests + 1) * RPCRDMA_MAX_SEGS *
993 sizeof(struct rpcrdma_mw);
999 /* allocate 1, 4 and 5 in one shot */
1000 p = kzalloc(len, GFP_KERNEL);
1002 dprintk("RPC: %s: req_t/rep_t/pad kzalloc(%zd) failed\n",
1007 buf->rb_pool = p; /* for freeing it later */
1009 buf->rb_send_bufs = (struct rpcrdma_req **) p;
1010 p = (char *) &buf->rb_send_bufs[buf->rb_max_requests];
1011 buf->rb_recv_bufs = (struct rpcrdma_rep **) p;
1012 p = (char *) &buf->rb_recv_bufs[buf->rb_max_requests];
1015 * Register the zeroed pad buffer, if any.
1017 if (cdata->padding) {
1018 rc = rpcrdma_register_internal(ia, p, cdata->padding,
1019 &ep->rep_pad_mr, &ep->rep_pad);
1023 p += cdata->padding;
1025 INIT_LIST_HEAD(&buf->rb_mws);
1026 r = (struct rpcrdma_mw *)p;
1027 switch (ia->ri_memreg_strategy) {
1029 for (i = buf->rb_max_requests * RPCRDMA_MAX_SEGS; i; i--) {
1030 r->r.frmr.fr_mr = ib_alloc_fast_reg_mr(ia->ri_pd,
1031 ia->ri_max_frmr_depth);
1032 if (IS_ERR(r->r.frmr.fr_mr)) {
1033 rc = PTR_ERR(r->r.frmr.fr_mr);
1034 dprintk("RPC: %s: ib_alloc_fast_reg_mr"
1035 " failed %i\n", __func__, rc);
1038 r->r.frmr.fr_pgl = ib_alloc_fast_reg_page_list(
1040 ia->ri_max_frmr_depth);
1041 if (IS_ERR(r->r.frmr.fr_pgl)) {
1042 rc = PTR_ERR(r->r.frmr.fr_pgl);
1044 "ib_alloc_fast_reg_page_list "
1045 "failed %i\n", __func__, rc);
1047 ib_dereg_mr(r->r.frmr.fr_mr);
1050 list_add(&r->mw_list, &buf->rb_mws);
1054 case RPCRDMA_MTHCAFMR:
1055 /* TBD we are perhaps overallocating here */
1056 for (i = (buf->rb_max_requests+1) * RPCRDMA_MAX_SEGS; i; i--) {
1057 static struct ib_fmr_attr fa =
1058 { RPCRDMA_MAX_DATA_SEGS, 1, PAGE_SHIFT };
1059 r->r.fmr = ib_alloc_fmr(ia->ri_pd,
1060 IB_ACCESS_REMOTE_WRITE | IB_ACCESS_REMOTE_READ,
1062 if (IS_ERR(r->r.fmr)) {
1063 rc = PTR_ERR(r->r.fmr);
1064 dprintk("RPC: %s: ib_alloc_fmr"
1065 " failed %i\n", __func__, rc);
1068 list_add(&r->mw_list, &buf->rb_mws);
1077 * Allocate/init the request/reply buffers. Doing this
1078 * using kmalloc for now -- one for each buf.
1080 wlen = 1 << fls(cdata->inline_wsize + sizeof(struct rpcrdma_req));
1081 rlen = 1 << fls(cdata->inline_rsize + sizeof(struct rpcrdma_rep));
1082 dprintk("RPC: %s: wlen = %zu, rlen = %zu\n",
1083 __func__, wlen, rlen);
1085 for (i = 0; i < buf->rb_max_requests; i++) {
1086 struct rpcrdma_req *req;
1087 struct rpcrdma_rep *rep;
1089 req = kmalloc(wlen, GFP_KERNEL);
1091 dprintk("RPC: %s: request buffer %d alloc"
1092 " failed\n", __func__, i);
1096 memset(req, 0, sizeof(struct rpcrdma_req));
1097 buf->rb_send_bufs[i] = req;
1098 buf->rb_send_bufs[i]->rl_buffer = buf;
1100 rc = rpcrdma_register_internal(ia, req->rl_base,
1101 wlen - offsetof(struct rpcrdma_req, rl_base),
1102 &buf->rb_send_bufs[i]->rl_handle,
1103 &buf->rb_send_bufs[i]->rl_iov);
1107 buf->rb_send_bufs[i]->rl_size = wlen -
1108 sizeof(struct rpcrdma_req);
1110 rep = kmalloc(rlen, GFP_KERNEL);
1112 dprintk("RPC: %s: reply buffer %d alloc failed\n",
1117 memset(rep, 0, sizeof(struct rpcrdma_rep));
1118 buf->rb_recv_bufs[i] = rep;
1119 buf->rb_recv_bufs[i]->rr_buffer = buf;
1121 rc = rpcrdma_register_internal(ia, rep->rr_base,
1122 rlen - offsetof(struct rpcrdma_rep, rr_base),
1123 &buf->rb_recv_bufs[i]->rr_handle,
1124 &buf->rb_recv_bufs[i]->rr_iov);
1129 dprintk("RPC: %s: max_requests %d\n",
1130 __func__, buf->rb_max_requests);
1134 rpcrdma_buffer_destroy(buf);
1139 * Unregister and destroy buffer memory. Need to deal with
1140 * partial initialization, so it's callable from failed create.
1141 * Must be called before destroying endpoint, as registrations
1145 rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1148 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
1149 struct rpcrdma_mw *r;
1151 /* clean up in reverse order from create
1152 * 1. recv mr memory (mr free, then kfree)
1153 * 2. send mr memory (mr free, then kfree)
1154 * 3. padding (if any) [moved to rpcrdma_ep_destroy]
1157 dprintk("RPC: %s: entering\n", __func__);
1159 for (i = 0; i < buf->rb_max_requests; i++) {
1160 if (buf->rb_recv_bufs && buf->rb_recv_bufs[i]) {
1161 rpcrdma_deregister_internal(ia,
1162 buf->rb_recv_bufs[i]->rr_handle,
1163 &buf->rb_recv_bufs[i]->rr_iov);
1164 kfree(buf->rb_recv_bufs[i]);
1166 if (buf->rb_send_bufs && buf->rb_send_bufs[i]) {
1167 rpcrdma_deregister_internal(ia,
1168 buf->rb_send_bufs[i]->rl_handle,
1169 &buf->rb_send_bufs[i]->rl_iov);
1170 kfree(buf->rb_send_bufs[i]);
1174 while (!list_empty(&buf->rb_mws)) {
1175 r = list_entry(buf->rb_mws.next,
1176 struct rpcrdma_mw, mw_list);
1177 list_del(&r->mw_list);
1178 switch (ia->ri_memreg_strategy) {
1180 rc = ib_dereg_mr(r->r.frmr.fr_mr);
1186 ib_free_fast_reg_page_list(r->r.frmr.fr_pgl);
1188 case RPCRDMA_MTHCAFMR:
1189 rc = ib_dealloc_fmr(r->r.fmr);
1201 kfree(buf->rb_pool);
1205 * Get a set of request/reply buffers.
1207 * Reply buffer (if needed) is attached to send buffer upon return.
1209 * rb_send_index and rb_recv_index MUST always be pointing to the
1210 * *next* available buffer (non-NULL). They are incremented after
1211 * removing buffers, and decremented *before* returning them.
1213 struct rpcrdma_req *
1214 rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1216 struct rpcrdma_req *req;
1217 unsigned long flags;
1219 struct rpcrdma_mw *r;
1221 spin_lock_irqsave(&buffers->rb_lock, flags);
1222 if (buffers->rb_send_index == buffers->rb_max_requests) {
1223 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1224 dprintk("RPC: %s: out of request buffers\n", __func__);
1225 return ((struct rpcrdma_req *)NULL);
1228 req = buffers->rb_send_bufs[buffers->rb_send_index];
1229 if (buffers->rb_send_index < buffers->rb_recv_index) {
1230 dprintk("RPC: %s: %d extra receives outstanding (ok)\n",
1232 buffers->rb_recv_index - buffers->rb_send_index);
1233 req->rl_reply = NULL;
1235 req->rl_reply = buffers->rb_recv_bufs[buffers->rb_recv_index];
1236 buffers->rb_recv_bufs[buffers->rb_recv_index++] = NULL;
1238 buffers->rb_send_bufs[buffers->rb_send_index++] = NULL;
1239 if (!list_empty(&buffers->rb_mws)) {
1240 i = RPCRDMA_MAX_SEGS - 1;
1242 r = list_entry(buffers->rb_mws.next,
1243 struct rpcrdma_mw, mw_list);
1244 list_del(&r->mw_list);
1245 req->rl_segments[i].mr_chunk.rl_mw = r;
1248 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1253 * Put request/reply buffers back into pool.
1254 * Pre-decrement counter/array index.
1257 rpcrdma_buffer_put(struct rpcrdma_req *req)
1259 struct rpcrdma_buffer *buffers = req->rl_buffer;
1260 struct rpcrdma_ia *ia = rdmab_to_ia(buffers);
1262 unsigned long flags;
1264 spin_lock_irqsave(&buffers->rb_lock, flags);
1265 buffers->rb_send_bufs[--buffers->rb_send_index] = req;
1267 if (req->rl_reply) {
1268 buffers->rb_recv_bufs[--buffers->rb_recv_index] = req->rl_reply;
1269 req->rl_reply->rr_func = NULL;
1270 req->rl_reply = NULL;
1272 switch (ia->ri_memreg_strategy) {
1274 case RPCRDMA_MTHCAFMR:
1276 * Cycle mw's back in reverse order, and "spin" them.
1277 * This delays and scrambles reuse as much as possible.
1281 struct rpcrdma_mw **mw;
1282 mw = &req->rl_segments[i].mr_chunk.rl_mw;
1283 list_add_tail(&(*mw)->mw_list, &buffers->rb_mws);
1285 } while (++i < RPCRDMA_MAX_SEGS);
1286 list_add_tail(&req->rl_segments[0].mr_chunk.rl_mw->mw_list,
1288 req->rl_segments[0].mr_chunk.rl_mw = NULL;
1293 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1297 * Recover reply buffers from pool.
1298 * This happens when recovering from error conditions.
1299 * Post-increment counter/array index.
1302 rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1304 struct rpcrdma_buffer *buffers = req->rl_buffer;
1305 unsigned long flags;
1307 if (req->rl_iov.length == 0) /* special case xprt_rdma_allocate() */
1308 buffers = ((struct rpcrdma_req *) buffers)->rl_buffer;
1309 spin_lock_irqsave(&buffers->rb_lock, flags);
1310 if (buffers->rb_recv_index < buffers->rb_max_requests) {
1311 req->rl_reply = buffers->rb_recv_bufs[buffers->rb_recv_index];
1312 buffers->rb_recv_bufs[buffers->rb_recv_index++] = NULL;
1314 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1318 * Put reply buffers back into pool when not attached to
1319 * request. This happens in error conditions.
1322 rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1324 struct rpcrdma_buffer *buffers = rep->rr_buffer;
1325 unsigned long flags;
1327 rep->rr_func = NULL;
1328 spin_lock_irqsave(&buffers->rb_lock, flags);
1329 buffers->rb_recv_bufs[--buffers->rb_recv_index] = rep;
1330 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1334 * Wrappers for internal-use kmalloc memory registration, used by buffer code.
1338 rpcrdma_register_internal(struct rpcrdma_ia *ia, void *va, int len,
1339 struct ib_mr **mrp, struct ib_sge *iov)
1341 struct ib_phys_buf ipb;
1346 * All memory passed here was kmalloc'ed, therefore phys-contiguous.
1348 iov->addr = ib_dma_map_single(ia->ri_id->device,
1349 va, len, DMA_BIDIRECTIONAL);
1352 if (ia->ri_have_dma_lkey) {
1354 iov->lkey = ia->ri_dma_lkey;
1356 } else if (ia->ri_bind_mem != NULL) {
1358 iov->lkey = ia->ri_bind_mem->lkey;
1362 ipb.addr = iov->addr;
1363 ipb.size = iov->length;
1364 mr = ib_reg_phys_mr(ia->ri_pd, &ipb, 1,
1365 IB_ACCESS_LOCAL_WRITE, &iov->addr);
1367 dprintk("RPC: %s: phys convert: 0x%llx "
1368 "registered 0x%llx length %d\n",
1369 __func__, (unsigned long long)ipb.addr,
1370 (unsigned long long)iov->addr, len);
1375 dprintk("RPC: %s: failed with %i\n", __func__, rc);
1378 iov->lkey = mr->lkey;
1386 rpcrdma_deregister_internal(struct rpcrdma_ia *ia,
1387 struct ib_mr *mr, struct ib_sge *iov)
1391 ib_dma_unmap_single(ia->ri_id->device,
1392 iov->addr, iov->length, DMA_BIDIRECTIONAL);
1397 rc = ib_dereg_mr(mr);
1399 dprintk("RPC: %s: ib_dereg_mr failed %i\n", __func__, rc);
1404 * Wrappers for chunk registration, shared by read/write chunk code.
1408 rpcrdma_map_one(struct rpcrdma_ia *ia, struct rpcrdma_mr_seg *seg, int writing)
1410 seg->mr_dir = writing ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1411 seg->mr_dmalen = seg->mr_len;
1413 seg->mr_dma = ib_dma_map_page(ia->ri_id->device,
1414 seg->mr_page, offset_in_page(seg->mr_offset),
1415 seg->mr_dmalen, seg->mr_dir);
1417 seg->mr_dma = ib_dma_map_single(ia->ri_id->device,
1419 seg->mr_dmalen, seg->mr_dir);
1420 if (ib_dma_mapping_error(ia->ri_id->device, seg->mr_dma)) {
1421 dprintk("RPC: %s: mr_dma %llx mr_offset %p mr_dma_len %zu\n",
1423 (unsigned long long)seg->mr_dma,
1424 seg->mr_offset, seg->mr_dmalen);
1429 rpcrdma_unmap_one(struct rpcrdma_ia *ia, struct rpcrdma_mr_seg *seg)
1432 ib_dma_unmap_page(ia->ri_id->device,
1433 seg->mr_dma, seg->mr_dmalen, seg->mr_dir);
1435 ib_dma_unmap_single(ia->ri_id->device,
1436 seg->mr_dma, seg->mr_dmalen, seg->mr_dir);
1440 rpcrdma_register_frmr_external(struct rpcrdma_mr_seg *seg,
1441 int *nsegs, int writing, struct rpcrdma_ia *ia,
1442 struct rpcrdma_xprt *r_xprt)
1444 struct rpcrdma_mr_seg *seg1 = seg;
1445 struct ib_send_wr invalidate_wr, frmr_wr, *bad_wr, *post_wr;
1454 pageoff = offset_in_page(seg1->mr_offset);
1455 seg1->mr_offset -= pageoff; /* start of page */
1456 seg1->mr_len += pageoff;
1458 if (*nsegs > ia->ri_max_frmr_depth)
1459 *nsegs = ia->ri_max_frmr_depth;
1460 for (page_no = i = 0; i < *nsegs;) {
1461 rpcrdma_map_one(ia, seg, writing);
1463 for (seg_len = seg->mr_len; seg_len > 0; seg_len -= PAGE_SIZE) {
1464 seg1->mr_chunk.rl_mw->r.frmr.fr_pgl->
1465 page_list[page_no++] = pa;
1471 /* Check for holes */
1472 if ((i < *nsegs && offset_in_page(seg->mr_offset)) ||
1473 offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len))
1476 dprintk("RPC: %s: Using frmr %p to map %d segments\n",
1477 __func__, seg1->mr_chunk.rl_mw, i);
1479 if (unlikely(seg1->mr_chunk.rl_mw->r.frmr.state == FRMR_IS_VALID)) {
1480 dprintk("RPC: %s: frmr %x left valid, posting invalidate.\n",
1482 seg1->mr_chunk.rl_mw->r.frmr.fr_mr->rkey);
1483 /* Invalidate before using. */
1484 memset(&invalidate_wr, 0, sizeof invalidate_wr);
1485 invalidate_wr.wr_id = (unsigned long)(void *)seg1->mr_chunk.rl_mw;
1486 invalidate_wr.next = &frmr_wr;
1487 invalidate_wr.opcode = IB_WR_LOCAL_INV;
1488 invalidate_wr.send_flags = IB_SEND_SIGNALED;
1489 invalidate_wr.ex.invalidate_rkey =
1490 seg1->mr_chunk.rl_mw->r.frmr.fr_mr->rkey;
1491 DECR_CQCOUNT(&r_xprt->rx_ep);
1492 post_wr = &invalidate_wr;
1496 /* Prepare FRMR WR */
1497 memset(&frmr_wr, 0, sizeof frmr_wr);
1498 frmr_wr.wr_id = (unsigned long)(void *)seg1->mr_chunk.rl_mw;
1499 frmr_wr.opcode = IB_WR_FAST_REG_MR;
1500 frmr_wr.send_flags = IB_SEND_SIGNALED;
1501 frmr_wr.wr.fast_reg.iova_start = seg1->mr_dma;
1502 frmr_wr.wr.fast_reg.page_list = seg1->mr_chunk.rl_mw->r.frmr.fr_pgl;
1503 frmr_wr.wr.fast_reg.page_list_len = page_no;
1504 frmr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
1505 frmr_wr.wr.fast_reg.length = page_no << PAGE_SHIFT;
1506 if (frmr_wr.wr.fast_reg.length < len) {
1507 while (seg1->mr_nsegs--)
1508 rpcrdma_unmap_one(ia, seg++);
1513 key = (u8)(seg1->mr_chunk.rl_mw->r.frmr.fr_mr->rkey & 0x000000FF);
1514 ib_update_fast_reg_key(seg1->mr_chunk.rl_mw->r.frmr.fr_mr, ++key);
1516 frmr_wr.wr.fast_reg.access_flags = (writing ?
1517 IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
1518 IB_ACCESS_REMOTE_READ);
1519 frmr_wr.wr.fast_reg.rkey = seg1->mr_chunk.rl_mw->r.frmr.fr_mr->rkey;
1520 DECR_CQCOUNT(&r_xprt->rx_ep);
1522 rc = ib_post_send(ia->ri_id->qp, post_wr, &bad_wr);
1525 dprintk("RPC: %s: failed ib_post_send for register,"
1526 " status %i\n", __func__, rc);
1528 rpcrdma_unmap_one(ia, --seg);
1530 seg1->mr_rkey = seg1->mr_chunk.rl_mw->r.frmr.fr_mr->rkey;
1531 seg1->mr_base = seg1->mr_dma + pageoff;
1540 rpcrdma_deregister_frmr_external(struct rpcrdma_mr_seg *seg,
1541 struct rpcrdma_ia *ia, struct rpcrdma_xprt *r_xprt)
1543 struct rpcrdma_mr_seg *seg1 = seg;
1544 struct ib_send_wr invalidate_wr, *bad_wr;
1547 while (seg1->mr_nsegs--)
1548 rpcrdma_unmap_one(ia, seg++);
1550 memset(&invalidate_wr, 0, sizeof invalidate_wr);
1551 invalidate_wr.wr_id = (unsigned long)(void *)seg1->mr_chunk.rl_mw;
1552 invalidate_wr.opcode = IB_WR_LOCAL_INV;
1553 invalidate_wr.send_flags = IB_SEND_SIGNALED;
1554 invalidate_wr.ex.invalidate_rkey = seg1->mr_chunk.rl_mw->r.frmr.fr_mr->rkey;
1555 DECR_CQCOUNT(&r_xprt->rx_ep);
1557 rc = ib_post_send(ia->ri_id->qp, &invalidate_wr, &bad_wr);
1559 dprintk("RPC: %s: failed ib_post_send for invalidate,"
1560 " status %i\n", __func__, rc);
1565 rpcrdma_register_fmr_external(struct rpcrdma_mr_seg *seg,
1566 int *nsegs, int writing, struct rpcrdma_ia *ia)
1568 struct rpcrdma_mr_seg *seg1 = seg;
1569 u64 physaddrs[RPCRDMA_MAX_DATA_SEGS];
1570 int len, pageoff, i, rc;
1572 pageoff = offset_in_page(seg1->mr_offset);
1573 seg1->mr_offset -= pageoff; /* start of page */
1574 seg1->mr_len += pageoff;
1576 if (*nsegs > RPCRDMA_MAX_DATA_SEGS)
1577 *nsegs = RPCRDMA_MAX_DATA_SEGS;
1578 for (i = 0; i < *nsegs;) {
1579 rpcrdma_map_one(ia, seg, writing);
1580 physaddrs[i] = seg->mr_dma;
1584 /* Check for holes */
1585 if ((i < *nsegs && offset_in_page(seg->mr_offset)) ||
1586 offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len))
1589 rc = ib_map_phys_fmr(seg1->mr_chunk.rl_mw->r.fmr,
1590 physaddrs, i, seg1->mr_dma);
1592 dprintk("RPC: %s: failed ib_map_phys_fmr "
1593 "%u@0x%llx+%i (%d)... status %i\n", __func__,
1594 len, (unsigned long long)seg1->mr_dma,
1597 rpcrdma_unmap_one(ia, --seg);
1599 seg1->mr_rkey = seg1->mr_chunk.rl_mw->r.fmr->rkey;
1600 seg1->mr_base = seg1->mr_dma + pageoff;
1609 rpcrdma_deregister_fmr_external(struct rpcrdma_mr_seg *seg,
1610 struct rpcrdma_ia *ia)
1612 struct rpcrdma_mr_seg *seg1 = seg;
1616 list_add(&seg1->mr_chunk.rl_mw->r.fmr->list, &l);
1617 rc = ib_unmap_fmr(&l);
1618 while (seg1->mr_nsegs--)
1619 rpcrdma_unmap_one(ia, seg++);
1621 dprintk("RPC: %s: failed ib_unmap_fmr,"
1622 " status %i\n", __func__, rc);
1627 rpcrdma_register_external(struct rpcrdma_mr_seg *seg,
1628 int nsegs, int writing, struct rpcrdma_xprt *r_xprt)
1630 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
1633 switch (ia->ri_memreg_strategy) {
1635 #if RPCRDMA_PERSISTENT_REGISTRATION
1636 case RPCRDMA_ALLPHYSICAL:
1637 rpcrdma_map_one(ia, seg, writing);
1638 seg->mr_rkey = ia->ri_bind_mem->rkey;
1639 seg->mr_base = seg->mr_dma;
1645 /* Registration using frmr registration */
1647 rc = rpcrdma_register_frmr_external(seg, &nsegs, writing, ia, r_xprt);
1650 /* Registration using fmr memory registration */
1651 case RPCRDMA_MTHCAFMR:
1652 rc = rpcrdma_register_fmr_external(seg, &nsegs, writing, ia);
1665 rpcrdma_deregister_external(struct rpcrdma_mr_seg *seg,
1666 struct rpcrdma_xprt *r_xprt)
1668 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
1669 int nsegs = seg->mr_nsegs, rc;
1671 switch (ia->ri_memreg_strategy) {
1673 #if RPCRDMA_PERSISTENT_REGISTRATION
1674 case RPCRDMA_ALLPHYSICAL:
1675 rpcrdma_unmap_one(ia, seg);
1680 rc = rpcrdma_deregister_frmr_external(seg, ia, r_xprt);
1683 case RPCRDMA_MTHCAFMR:
1684 rc = rpcrdma_deregister_fmr_external(seg, ia);
1694 * Prepost any receive buffer, then post send.
1696 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1699 rpcrdma_ep_post(struct rpcrdma_ia *ia,
1700 struct rpcrdma_ep *ep,
1701 struct rpcrdma_req *req)
1703 struct ib_send_wr send_wr, *send_wr_fail;
1704 struct rpcrdma_rep *rep = req->rl_reply;
1708 rc = rpcrdma_ep_post_recv(ia, ep, rep);
1711 req->rl_reply = NULL;
1714 send_wr.next = NULL;
1715 send_wr.wr_id = 0ULL; /* no send cookie */
1716 send_wr.sg_list = req->rl_send_iov;
1717 send_wr.num_sge = req->rl_niovs;
1718 send_wr.opcode = IB_WR_SEND;
1719 if (send_wr.num_sge == 4) /* no need to sync any pad (constant) */
1720 ib_dma_sync_single_for_device(ia->ri_id->device,
1721 req->rl_send_iov[3].addr, req->rl_send_iov[3].length,
1723 ib_dma_sync_single_for_device(ia->ri_id->device,
1724 req->rl_send_iov[1].addr, req->rl_send_iov[1].length,
1726 ib_dma_sync_single_for_device(ia->ri_id->device,
1727 req->rl_send_iov[0].addr, req->rl_send_iov[0].length,
1730 if (DECR_CQCOUNT(ep) > 0)
1731 send_wr.send_flags = 0;
1732 else { /* Provider must take a send completion every now and then */
1734 send_wr.send_flags = IB_SEND_SIGNALED;
1737 rc = ib_post_send(ia->ri_id->qp, &send_wr, &send_wr_fail);
1739 dprintk("RPC: %s: ib_post_send returned %i\n", __func__,
1746 * (Re)post a receive buffer.
1749 rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
1750 struct rpcrdma_ep *ep,
1751 struct rpcrdma_rep *rep)
1753 struct ib_recv_wr recv_wr, *recv_wr_fail;
1756 recv_wr.next = NULL;
1757 recv_wr.wr_id = (u64) (unsigned long) rep;
1758 recv_wr.sg_list = &rep->rr_iov;
1759 recv_wr.num_sge = 1;
1761 ib_dma_sync_single_for_cpu(ia->ri_id->device,
1762 rep->rr_iov.addr, rep->rr_iov.length, DMA_BIDIRECTIONAL);
1764 rc = ib_post_recv(ia->ri_id->qp, &recv_wr, &recv_wr_fail);
1767 dprintk("RPC: %s: ib_post_recv returned %i\n", __func__,