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/pci.h> /* for Tavor hack below */
52 #include <linux/slab.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)
170 while ((rc = ib_poll_cq(cq, 1, &wc)) == 1)
171 rpcrdma_sendcq_process_wc(&wc);
176 * Handle send, fast_reg_mr, and local_inv completions.
178 * Send events are typically suppressed and thus do not result
179 * in an upcall. Occasionally one is signaled, however. This
180 * prevents the provider's completion queue from wrapping and
181 * losing a completion.
184 rpcrdma_sendcq_upcall(struct ib_cq *cq, void *cq_context)
188 rc = rpcrdma_sendcq_poll(cq);
190 dprintk("RPC: %s: ib_poll_cq failed: %i\n",
195 rc = ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
197 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
202 rpcrdma_sendcq_poll(cq);
206 rpcrdma_recvcq_process_wc(struct ib_wc *wc)
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 rpcrdma_schedule_tasklet(rep);
241 rpcrdma_recvcq_poll(struct ib_cq *cq)
246 while ((rc = ib_poll_cq(cq, 1, &wc)) == 1)
247 rpcrdma_recvcq_process_wc(&wc);
252 * Handle receive completions.
254 * It is reentrant but processes single events in order to maintain
255 * ordering of receives to keep server credits.
257 * It is the responsibility of the scheduled tasklet to return
258 * recv buffers to the pool. NOTE: this affects synchronization of
259 * connection shutdown. That is, the structures required for
260 * the completion of the reply handler must remain intact until
261 * all memory has been reclaimed.
264 rpcrdma_recvcq_upcall(struct ib_cq *cq, void *cq_context)
268 rc = rpcrdma_recvcq_poll(cq);
270 dprintk("RPC: %s: ib_poll_cq failed: %i\n",
275 rc = ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
277 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
282 rpcrdma_recvcq_poll(cq);
286 static const char * const conn[] = {
303 rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
305 struct rpcrdma_xprt *xprt = id->context;
306 struct rpcrdma_ia *ia = &xprt->rx_ia;
307 struct rpcrdma_ep *ep = &xprt->rx_ep;
309 struct sockaddr_in *addr = (struct sockaddr_in *) &ep->rep_remote_addr;
311 struct ib_qp_attr attr;
312 struct ib_qp_init_attr iattr;
315 switch (event->event) {
316 case RDMA_CM_EVENT_ADDR_RESOLVED:
317 case RDMA_CM_EVENT_ROUTE_RESOLVED:
319 complete(&ia->ri_done);
321 case RDMA_CM_EVENT_ADDR_ERROR:
322 ia->ri_async_rc = -EHOSTUNREACH;
323 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
325 complete(&ia->ri_done);
327 case RDMA_CM_EVENT_ROUTE_ERROR:
328 ia->ri_async_rc = -ENETUNREACH;
329 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
331 complete(&ia->ri_done);
333 case RDMA_CM_EVENT_ESTABLISHED:
335 ib_query_qp(ia->ri_id->qp, &attr,
336 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_MAX_DEST_RD_ATOMIC,
338 dprintk("RPC: %s: %d responder resources"
340 __func__, attr.max_dest_rd_atomic, attr.max_rd_atomic);
342 case RDMA_CM_EVENT_CONNECT_ERROR:
343 connstate = -ENOTCONN;
345 case RDMA_CM_EVENT_UNREACHABLE:
346 connstate = -ENETDOWN;
348 case RDMA_CM_EVENT_REJECTED:
349 connstate = -ECONNREFUSED;
351 case RDMA_CM_EVENT_DISCONNECTED:
352 connstate = -ECONNABORTED;
354 case RDMA_CM_EVENT_DEVICE_REMOVAL:
357 dprintk("RPC: %s: %s: %pI4:%u (ep 0x%p event 0x%x)\n",
359 (event->event <= 11) ? conn[event->event] :
360 "unknown connection error",
361 &addr->sin_addr.s_addr,
362 ntohs(addr->sin_port),
364 atomic_set(&rpcx_to_rdmax(ep->rep_xprt)->rx_buf.rb_credits, 1);
365 dprintk("RPC: %s: %sconnected\n",
366 __func__, connstate > 0 ? "" : "dis");
367 ep->rep_connected = connstate;
369 wake_up_all(&ep->rep_connect_wait);
372 dprintk("RPC: %s: unexpected CM event %d\n",
373 __func__, event->event);
378 if (connstate == 1) {
379 int ird = attr.max_dest_rd_atomic;
380 int tird = ep->rep_remote_cma.responder_resources;
381 printk(KERN_INFO "rpcrdma: connection to %pI4:%u "
382 "on %s, memreg %d slots %d ird %d%s\n",
383 &addr->sin_addr.s_addr,
384 ntohs(addr->sin_port),
385 ia->ri_id->device->name,
386 ia->ri_memreg_strategy,
387 xprt->rx_buf.rb_max_requests,
388 ird, ird < 4 && ird < tird / 2 ? " (low!)" : "");
389 } else if (connstate < 0) {
390 printk(KERN_INFO "rpcrdma: connection to %pI4:%u closed (%d)\n",
391 &addr->sin_addr.s_addr,
392 ntohs(addr->sin_port),
400 static struct rdma_cm_id *
401 rpcrdma_create_id(struct rpcrdma_xprt *xprt,
402 struct rpcrdma_ia *ia, struct sockaddr *addr)
404 struct rdma_cm_id *id;
407 init_completion(&ia->ri_done);
409 id = rdma_create_id(rpcrdma_conn_upcall, xprt, RDMA_PS_TCP, IB_QPT_RC);
412 dprintk("RPC: %s: rdma_create_id() failed %i\n",
417 ia->ri_async_rc = -ETIMEDOUT;
418 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
420 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
424 wait_for_completion_interruptible_timeout(&ia->ri_done,
425 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
426 rc = ia->ri_async_rc;
430 ia->ri_async_rc = -ETIMEDOUT;
431 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
433 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
437 wait_for_completion_interruptible_timeout(&ia->ri_done,
438 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
439 rc = ia->ri_async_rc;
451 * Drain any cq, prior to teardown.
454 rpcrdma_clean_cq(struct ib_cq *cq)
459 while (1 == ib_poll_cq(cq, 1, &wc))
463 dprintk("RPC: %s: flushed %d events (last 0x%x)\n",
464 __func__, count, wc.opcode);
468 * Exported functions.
472 * Open and initialize an Interface Adapter.
473 * o initializes fields of struct rpcrdma_ia, including
474 * interface and provider attributes and protection zone.
477 rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr, int memreg)
480 struct ib_device_attr devattr;
481 struct rpcrdma_ia *ia = &xprt->rx_ia;
483 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
484 if (IS_ERR(ia->ri_id)) {
485 rc = PTR_ERR(ia->ri_id);
489 ia->ri_pd = ib_alloc_pd(ia->ri_id->device);
490 if (IS_ERR(ia->ri_pd)) {
491 rc = PTR_ERR(ia->ri_pd);
492 dprintk("RPC: %s: ib_alloc_pd() failed %i\n",
498 * Query the device to determine if the requested memory
499 * registration strategy is supported. If it isn't, set the
500 * strategy to a globally supported model.
502 rc = ib_query_device(ia->ri_id->device, &devattr);
504 dprintk("RPC: %s: ib_query_device failed %d\n",
509 if (devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY) {
510 ia->ri_have_dma_lkey = 1;
511 ia->ri_dma_lkey = ia->ri_id->device->local_dma_lkey;
514 if (memreg == RPCRDMA_FRMR) {
515 /* Requires both frmr reg and local dma lkey */
516 if ((devattr.device_cap_flags &
517 (IB_DEVICE_MEM_MGT_EXTENSIONS|IB_DEVICE_LOCAL_DMA_LKEY)) !=
518 (IB_DEVICE_MEM_MGT_EXTENSIONS|IB_DEVICE_LOCAL_DMA_LKEY)) {
519 dprintk("RPC: %s: FRMR registration "
520 "not supported by HCA\n", __func__);
521 memreg = RPCRDMA_MTHCAFMR;
523 /* Mind the ia limit on FRMR page list depth */
524 ia->ri_max_frmr_depth = min_t(unsigned int,
525 RPCRDMA_MAX_DATA_SEGS,
526 devattr.max_fast_reg_page_list_len);
529 if (memreg == RPCRDMA_MTHCAFMR) {
530 if (!ia->ri_id->device->alloc_fmr) {
531 dprintk("RPC: %s: MTHCAFMR registration "
532 "not supported by HCA\n", __func__);
533 #if RPCRDMA_PERSISTENT_REGISTRATION
534 memreg = RPCRDMA_ALLPHYSICAL;
543 * Optionally obtain an underlying physical identity mapping in
544 * order to do a memory window-based bind. This base registration
545 * is protected from remote access - that is enabled only by binding
546 * for the specific bytes targeted during each RPC operation, and
547 * revoked after the corresponding completion similar to a storage
553 #if RPCRDMA_PERSISTENT_REGISTRATION
554 case RPCRDMA_ALLPHYSICAL:
555 mem_priv = IB_ACCESS_LOCAL_WRITE |
556 IB_ACCESS_REMOTE_WRITE |
557 IB_ACCESS_REMOTE_READ;
560 case RPCRDMA_MTHCAFMR:
561 if (ia->ri_have_dma_lkey)
563 mem_priv = IB_ACCESS_LOCAL_WRITE;
564 #if RPCRDMA_PERSISTENT_REGISTRATION
567 ia->ri_bind_mem = ib_get_dma_mr(ia->ri_pd, mem_priv);
568 if (IS_ERR(ia->ri_bind_mem)) {
569 printk(KERN_ALERT "%s: ib_get_dma_mr for "
570 "phys register failed with %lX\n",
571 __func__, PTR_ERR(ia->ri_bind_mem));
577 printk(KERN_ERR "RPC: Unsupported memory "
578 "registration mode: %d\n", memreg);
582 dprintk("RPC: %s: memory registration strategy is %d\n",
585 /* Else will do memory reg/dereg for each chunk */
586 ia->ri_memreg_strategy = memreg;
590 rdma_destroy_id(ia->ri_id);
597 * Clean up/close an IA.
598 * o if event handles and PD have been initialized, free them.
602 rpcrdma_ia_close(struct rpcrdma_ia *ia)
606 dprintk("RPC: %s: entering\n", __func__);
607 if (ia->ri_bind_mem != NULL) {
608 rc = ib_dereg_mr(ia->ri_bind_mem);
609 dprintk("RPC: %s: ib_dereg_mr returned %i\n",
612 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
614 rdma_destroy_qp(ia->ri_id);
615 rdma_destroy_id(ia->ri_id);
618 if (ia->ri_pd != NULL && !IS_ERR(ia->ri_pd)) {
619 rc = ib_dealloc_pd(ia->ri_pd);
620 dprintk("RPC: %s: ib_dealloc_pd returned %i\n",
626 * Create unconnected endpoint.
629 rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
630 struct rpcrdma_create_data_internal *cdata)
632 struct ib_device_attr devattr;
633 struct ib_cq *sendcq, *recvcq;
636 rc = ib_query_device(ia->ri_id->device, &devattr);
638 dprintk("RPC: %s: ib_query_device failed %d\n",
643 /* check provider's send/recv wr limits */
644 if (cdata->max_requests > devattr.max_qp_wr)
645 cdata->max_requests = devattr.max_qp_wr;
647 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
648 ep->rep_attr.qp_context = ep;
649 /* send_cq and recv_cq initialized below */
650 ep->rep_attr.srq = NULL;
651 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
652 switch (ia->ri_memreg_strategy) {
656 /* Add room for frmr register and invalidate WRs.
657 * 1. FRMR reg WR for head
658 * 2. FRMR invalidate WR for head
659 * 3. N FRMR reg WRs for pagelist
660 * 4. N FRMR invalidate WRs for pagelist
661 * 5. FRMR reg WR for tail
662 * 6. FRMR invalidate WR for tail
663 * 7. The RDMA_SEND WR
666 /* Calculate N if the device max FRMR depth is smaller than
667 * RPCRDMA_MAX_DATA_SEGS.
669 if (ia->ri_max_frmr_depth < RPCRDMA_MAX_DATA_SEGS) {
670 int delta = RPCRDMA_MAX_DATA_SEGS -
671 ia->ri_max_frmr_depth;
674 depth += 2; /* FRMR reg + invalidate */
675 delta -= ia->ri_max_frmr_depth;
679 ep->rep_attr.cap.max_send_wr *= depth;
680 if (ep->rep_attr.cap.max_send_wr > devattr.max_qp_wr) {
681 cdata->max_requests = devattr.max_qp_wr / depth;
682 if (!cdata->max_requests)
684 ep->rep_attr.cap.max_send_wr = cdata->max_requests *
692 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
693 ep->rep_attr.cap.max_send_sge = (cdata->padding ? 4 : 2);
694 ep->rep_attr.cap.max_recv_sge = 1;
695 ep->rep_attr.cap.max_inline_data = 0;
696 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
697 ep->rep_attr.qp_type = IB_QPT_RC;
698 ep->rep_attr.port_num = ~0;
700 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
701 "iovs: send %d recv %d\n",
703 ep->rep_attr.cap.max_send_wr,
704 ep->rep_attr.cap.max_recv_wr,
705 ep->rep_attr.cap.max_send_sge,
706 ep->rep_attr.cap.max_recv_sge);
708 /* set trigger for requesting send completion */
709 ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
710 if (ep->rep_cqinit <= 2)
714 init_waitqueue_head(&ep->rep_connect_wait);
715 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
717 sendcq = ib_create_cq(ia->ri_id->device, rpcrdma_sendcq_upcall,
718 rpcrdma_cq_async_error_upcall, NULL,
719 ep->rep_attr.cap.max_send_wr + 1, 0);
720 if (IS_ERR(sendcq)) {
721 rc = PTR_ERR(sendcq);
722 dprintk("RPC: %s: failed to create send CQ: %i\n",
727 rc = ib_req_notify_cq(sendcq, IB_CQ_NEXT_COMP);
729 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
734 recvcq = ib_create_cq(ia->ri_id->device, rpcrdma_recvcq_upcall,
735 rpcrdma_cq_async_error_upcall, NULL,
736 ep->rep_attr.cap.max_recv_wr + 1, 0);
737 if (IS_ERR(recvcq)) {
738 rc = PTR_ERR(recvcq);
739 dprintk("RPC: %s: failed to create recv CQ: %i\n",
744 rc = ib_req_notify_cq(recvcq, IB_CQ_NEXT_COMP);
746 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
748 ib_destroy_cq(recvcq);
752 ep->rep_attr.send_cq = sendcq;
753 ep->rep_attr.recv_cq = recvcq;
755 /* Initialize cma parameters */
757 /* RPC/RDMA does not use private data */
758 ep->rep_remote_cma.private_data = NULL;
759 ep->rep_remote_cma.private_data_len = 0;
761 /* Client offers RDMA Read but does not initiate */
762 ep->rep_remote_cma.initiator_depth = 0;
763 if (devattr.max_qp_rd_atom > 32) /* arbitrary but <= 255 */
764 ep->rep_remote_cma.responder_resources = 32;
766 ep->rep_remote_cma.responder_resources = devattr.max_qp_rd_atom;
768 ep->rep_remote_cma.retry_count = 7;
769 ep->rep_remote_cma.flow_control = 0;
770 ep->rep_remote_cma.rnr_retry_count = 0;
775 err = ib_destroy_cq(sendcq);
777 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
786 * Disconnect and destroy endpoint. After this, the only
787 * valid operations on the ep are to free it (if dynamically
788 * allocated) or re-create it.
791 rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
795 dprintk("RPC: %s: entering, connected is %d\n",
796 __func__, ep->rep_connected);
798 cancel_delayed_work_sync(&ep->rep_connect_worker);
801 rc = rpcrdma_ep_disconnect(ep, ia);
803 dprintk("RPC: %s: rpcrdma_ep_disconnect"
804 " returned %i\n", __func__, rc);
805 rdma_destroy_qp(ia->ri_id);
806 ia->ri_id->qp = NULL;
809 /* padding - could be done in rpcrdma_buffer_destroy... */
810 if (ep->rep_pad_mr) {
811 rpcrdma_deregister_internal(ia, ep->rep_pad_mr, &ep->rep_pad);
812 ep->rep_pad_mr = NULL;
815 rpcrdma_clean_cq(ep->rep_attr.recv_cq);
816 rc = ib_destroy_cq(ep->rep_attr.recv_cq);
818 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
821 rpcrdma_clean_cq(ep->rep_attr.send_cq);
822 rc = ib_destroy_cq(ep->rep_attr.send_cq);
824 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
829 * Connect unconnected endpoint.
832 rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
834 struct rdma_cm_id *id;
838 if (ep->rep_connected != 0) {
839 struct rpcrdma_xprt *xprt;
841 rc = rpcrdma_ep_disconnect(ep, ia);
842 if (rc && rc != -ENOTCONN)
843 dprintk("RPC: %s: rpcrdma_ep_disconnect"
844 " status %i\n", __func__, rc);
846 rpcrdma_clean_cq(ep->rep_attr.recv_cq);
847 rpcrdma_clean_cq(ep->rep_attr.send_cq);
849 xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
850 id = rpcrdma_create_id(xprt, ia,
851 (struct sockaddr *)&xprt->rx_data.addr);
856 /* TEMP TEMP TEMP - fail if new device:
857 * Deregister/remarshal *all* requests!
858 * Close and recreate adapter, pd, etc!
859 * Re-determine all attributes still sane!
860 * More stuff I haven't thought of!
863 if (ia->ri_id->device != id->device) {
864 printk("RPC: %s: can't reconnect on "
865 "different device!\n", __func__);
871 rdma_destroy_qp(ia->ri_id);
872 rdma_destroy_id(ia->ri_id);
876 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
878 dprintk("RPC: %s: rdma_create_qp failed %i\n",
883 /* XXX Tavor device performs badly with 2K MTU! */
884 if (strnicmp(ia->ri_id->device->dma_device->bus->name, "pci", 3) == 0) {
885 struct pci_dev *pcid = to_pci_dev(ia->ri_id->device->dma_device);
886 if (pcid->device == PCI_DEVICE_ID_MELLANOX_TAVOR &&
887 (pcid->vendor == PCI_VENDOR_ID_MELLANOX ||
888 pcid->vendor == PCI_VENDOR_ID_TOPSPIN)) {
889 struct ib_qp_attr attr = {
890 .path_mtu = IB_MTU_1024
892 rc = ib_modify_qp(ia->ri_id->qp, &attr, IB_QP_PATH_MTU);
896 ep->rep_connected = 0;
898 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
900 dprintk("RPC: %s: rdma_connect() failed with %i\n",
905 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
908 * Check state. A non-peer reject indicates no listener
909 * (ECONNREFUSED), which may be a transient state. All
910 * others indicate a transport condition which has already
911 * undergone a best-effort.
913 if (ep->rep_connected == -ECONNREFUSED &&
914 ++retry_count <= RDMA_CONNECT_RETRY_MAX) {
915 dprintk("RPC: %s: non-peer_reject, retry\n", __func__);
918 if (ep->rep_connected <= 0) {
919 /* Sometimes, the only way to reliably connect to remote
920 * CMs is to use same nonzero values for ORD and IRD. */
921 if (retry_count++ <= RDMA_CONNECT_RETRY_MAX + 1 &&
922 (ep->rep_remote_cma.responder_resources == 0 ||
923 ep->rep_remote_cma.initiator_depth !=
924 ep->rep_remote_cma.responder_resources)) {
925 if (ep->rep_remote_cma.responder_resources == 0)
926 ep->rep_remote_cma.responder_resources = 1;
927 ep->rep_remote_cma.initiator_depth =
928 ep->rep_remote_cma.responder_resources;
931 rc = ep->rep_connected;
933 dprintk("RPC: %s: connected\n", __func__);
938 ep->rep_connected = rc;
943 * rpcrdma_ep_disconnect
945 * This is separate from destroy to facilitate the ability
946 * to reconnect without recreating the endpoint.
948 * This call is not reentrant, and must not be made in parallel
949 * on the same endpoint.
952 rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
956 rpcrdma_clean_cq(ep->rep_attr.recv_cq);
957 rpcrdma_clean_cq(ep->rep_attr.send_cq);
958 rc = rdma_disconnect(ia->ri_id);
960 /* returns without wait if not connected */
961 wait_event_interruptible(ep->rep_connect_wait,
962 ep->rep_connected != 1);
963 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
964 (ep->rep_connected == 1) ? "still " : "dis");
966 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
967 ep->rep_connected = rc;
973 * Initialize buffer memory
976 rpcrdma_buffer_create(struct rpcrdma_buffer *buf, struct rpcrdma_ep *ep,
977 struct rpcrdma_ia *ia, struct rpcrdma_create_data_internal *cdata)
982 struct rpcrdma_mw *r;
984 buf->rb_max_requests = cdata->max_requests;
985 spin_lock_init(&buf->rb_lock);
986 atomic_set(&buf->rb_credits, 1);
989 * 1. arrays for send and recv pointers
990 * 2. arrays of struct rpcrdma_req to fill in pointers
991 * 3. array of struct rpcrdma_rep for replies
993 * 5. mw's, fmr's or frmr's, if any
994 * Send/recv buffers in req/rep need to be registered
997 len = buf->rb_max_requests *
998 (sizeof(struct rpcrdma_req *) + sizeof(struct rpcrdma_rep *));
999 len += cdata->padding;
1000 switch (ia->ri_memreg_strategy) {
1002 len += buf->rb_max_requests * RPCRDMA_MAX_SEGS *
1003 sizeof(struct rpcrdma_mw);
1005 case RPCRDMA_MTHCAFMR:
1006 /* TBD we are perhaps overallocating here */
1007 len += (buf->rb_max_requests + 1) * RPCRDMA_MAX_SEGS *
1008 sizeof(struct rpcrdma_mw);
1014 /* allocate 1, 4 and 5 in one shot */
1015 p = kzalloc(len, GFP_KERNEL);
1017 dprintk("RPC: %s: req_t/rep_t/pad kzalloc(%zd) failed\n",
1022 buf->rb_pool = p; /* for freeing it later */
1024 buf->rb_send_bufs = (struct rpcrdma_req **) p;
1025 p = (char *) &buf->rb_send_bufs[buf->rb_max_requests];
1026 buf->rb_recv_bufs = (struct rpcrdma_rep **) p;
1027 p = (char *) &buf->rb_recv_bufs[buf->rb_max_requests];
1030 * Register the zeroed pad buffer, if any.
1032 if (cdata->padding) {
1033 rc = rpcrdma_register_internal(ia, p, cdata->padding,
1034 &ep->rep_pad_mr, &ep->rep_pad);
1038 p += cdata->padding;
1040 INIT_LIST_HEAD(&buf->rb_mws);
1041 r = (struct rpcrdma_mw *)p;
1042 switch (ia->ri_memreg_strategy) {
1044 for (i = buf->rb_max_requests * RPCRDMA_MAX_SEGS; i; i--) {
1045 r->r.frmr.fr_mr = ib_alloc_fast_reg_mr(ia->ri_pd,
1046 ia->ri_max_frmr_depth);
1047 if (IS_ERR(r->r.frmr.fr_mr)) {
1048 rc = PTR_ERR(r->r.frmr.fr_mr);
1049 dprintk("RPC: %s: ib_alloc_fast_reg_mr"
1050 " failed %i\n", __func__, rc);
1053 r->r.frmr.fr_pgl = ib_alloc_fast_reg_page_list(
1055 ia->ri_max_frmr_depth);
1056 if (IS_ERR(r->r.frmr.fr_pgl)) {
1057 rc = PTR_ERR(r->r.frmr.fr_pgl);
1059 "ib_alloc_fast_reg_page_list "
1060 "failed %i\n", __func__, rc);
1062 ib_dereg_mr(r->r.frmr.fr_mr);
1065 list_add(&r->mw_list, &buf->rb_mws);
1069 case RPCRDMA_MTHCAFMR:
1070 /* TBD we are perhaps overallocating here */
1071 for (i = (buf->rb_max_requests+1) * RPCRDMA_MAX_SEGS; i; i--) {
1072 static struct ib_fmr_attr fa =
1073 { RPCRDMA_MAX_DATA_SEGS, 1, PAGE_SHIFT };
1074 r->r.fmr = ib_alloc_fmr(ia->ri_pd,
1075 IB_ACCESS_REMOTE_WRITE | IB_ACCESS_REMOTE_READ,
1077 if (IS_ERR(r->r.fmr)) {
1078 rc = PTR_ERR(r->r.fmr);
1079 dprintk("RPC: %s: ib_alloc_fmr"
1080 " failed %i\n", __func__, rc);
1083 list_add(&r->mw_list, &buf->rb_mws);
1092 * Allocate/init the request/reply buffers. Doing this
1093 * using kmalloc for now -- one for each buf.
1095 for (i = 0; i < buf->rb_max_requests; i++) {
1096 struct rpcrdma_req *req;
1097 struct rpcrdma_rep *rep;
1099 len = cdata->inline_wsize + sizeof(struct rpcrdma_req);
1100 /* RPC layer requests *double* size + 1K RPC_SLACK_SPACE! */
1101 /* Typical ~2400b, so rounding up saves work later */
1104 req = kmalloc(len, GFP_KERNEL);
1106 dprintk("RPC: %s: request buffer %d alloc"
1107 " failed\n", __func__, i);
1111 memset(req, 0, sizeof(struct rpcrdma_req));
1112 buf->rb_send_bufs[i] = req;
1113 buf->rb_send_bufs[i]->rl_buffer = buf;
1115 rc = rpcrdma_register_internal(ia, req->rl_base,
1116 len - offsetof(struct rpcrdma_req, rl_base),
1117 &buf->rb_send_bufs[i]->rl_handle,
1118 &buf->rb_send_bufs[i]->rl_iov);
1122 buf->rb_send_bufs[i]->rl_size = len-sizeof(struct rpcrdma_req);
1124 len = cdata->inline_rsize + sizeof(struct rpcrdma_rep);
1125 rep = kmalloc(len, GFP_KERNEL);
1127 dprintk("RPC: %s: reply buffer %d alloc failed\n",
1132 memset(rep, 0, sizeof(struct rpcrdma_rep));
1133 buf->rb_recv_bufs[i] = rep;
1134 buf->rb_recv_bufs[i]->rr_buffer = buf;
1136 rc = rpcrdma_register_internal(ia, rep->rr_base,
1137 len - offsetof(struct rpcrdma_rep, rr_base),
1138 &buf->rb_recv_bufs[i]->rr_handle,
1139 &buf->rb_recv_bufs[i]->rr_iov);
1144 dprintk("RPC: %s: max_requests %d\n",
1145 __func__, buf->rb_max_requests);
1149 rpcrdma_buffer_destroy(buf);
1154 * Unregister and destroy buffer memory. Need to deal with
1155 * partial initialization, so it's callable from failed create.
1156 * Must be called before destroying endpoint, as registrations
1160 rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1163 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
1164 struct rpcrdma_mw *r;
1166 /* clean up in reverse order from create
1167 * 1. recv mr memory (mr free, then kfree)
1168 * 2. send mr memory (mr free, then kfree)
1169 * 3. padding (if any) [moved to rpcrdma_ep_destroy]
1172 dprintk("RPC: %s: entering\n", __func__);
1174 for (i = 0; i < buf->rb_max_requests; i++) {
1175 if (buf->rb_recv_bufs && buf->rb_recv_bufs[i]) {
1176 rpcrdma_deregister_internal(ia,
1177 buf->rb_recv_bufs[i]->rr_handle,
1178 &buf->rb_recv_bufs[i]->rr_iov);
1179 kfree(buf->rb_recv_bufs[i]);
1181 if (buf->rb_send_bufs && buf->rb_send_bufs[i]) {
1182 rpcrdma_deregister_internal(ia,
1183 buf->rb_send_bufs[i]->rl_handle,
1184 &buf->rb_send_bufs[i]->rl_iov);
1185 kfree(buf->rb_send_bufs[i]);
1189 while (!list_empty(&buf->rb_mws)) {
1190 r = list_entry(buf->rb_mws.next,
1191 struct rpcrdma_mw, mw_list);
1192 list_del(&r->mw_list);
1193 switch (ia->ri_memreg_strategy) {
1195 rc = ib_dereg_mr(r->r.frmr.fr_mr);
1201 ib_free_fast_reg_page_list(r->r.frmr.fr_pgl);
1203 case RPCRDMA_MTHCAFMR:
1204 rc = ib_dealloc_fmr(r->r.fmr);
1216 kfree(buf->rb_pool);
1220 * Get a set of request/reply buffers.
1222 * Reply buffer (if needed) is attached to send buffer upon return.
1224 * rb_send_index and rb_recv_index MUST always be pointing to the
1225 * *next* available buffer (non-NULL). They are incremented after
1226 * removing buffers, and decremented *before* returning them.
1228 struct rpcrdma_req *
1229 rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1231 struct rpcrdma_req *req;
1232 unsigned long flags;
1234 struct rpcrdma_mw *r;
1236 spin_lock_irqsave(&buffers->rb_lock, flags);
1237 if (buffers->rb_send_index == buffers->rb_max_requests) {
1238 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1239 dprintk("RPC: %s: out of request buffers\n", __func__);
1240 return ((struct rpcrdma_req *)NULL);
1243 req = buffers->rb_send_bufs[buffers->rb_send_index];
1244 if (buffers->rb_send_index < buffers->rb_recv_index) {
1245 dprintk("RPC: %s: %d extra receives outstanding (ok)\n",
1247 buffers->rb_recv_index - buffers->rb_send_index);
1248 req->rl_reply = NULL;
1250 req->rl_reply = buffers->rb_recv_bufs[buffers->rb_recv_index];
1251 buffers->rb_recv_bufs[buffers->rb_recv_index++] = NULL;
1253 buffers->rb_send_bufs[buffers->rb_send_index++] = NULL;
1254 if (!list_empty(&buffers->rb_mws)) {
1255 i = RPCRDMA_MAX_SEGS - 1;
1257 r = list_entry(buffers->rb_mws.next,
1258 struct rpcrdma_mw, mw_list);
1259 list_del(&r->mw_list);
1260 req->rl_segments[i].mr_chunk.rl_mw = r;
1263 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1268 * Put request/reply buffers back into pool.
1269 * Pre-decrement counter/array index.
1272 rpcrdma_buffer_put(struct rpcrdma_req *req)
1274 struct rpcrdma_buffer *buffers = req->rl_buffer;
1275 struct rpcrdma_ia *ia = rdmab_to_ia(buffers);
1277 unsigned long flags;
1279 BUG_ON(req->rl_nchunks != 0);
1280 spin_lock_irqsave(&buffers->rb_lock, flags);
1281 buffers->rb_send_bufs[--buffers->rb_send_index] = req;
1283 if (req->rl_reply) {
1284 buffers->rb_recv_bufs[--buffers->rb_recv_index] = req->rl_reply;
1285 req->rl_reply->rr_func = NULL;
1286 req->rl_reply = NULL;
1288 switch (ia->ri_memreg_strategy) {
1290 case RPCRDMA_MTHCAFMR:
1292 * Cycle mw's back in reverse order, and "spin" them.
1293 * This delays and scrambles reuse as much as possible.
1297 struct rpcrdma_mw **mw;
1298 mw = &req->rl_segments[i].mr_chunk.rl_mw;
1299 list_add_tail(&(*mw)->mw_list, &buffers->rb_mws);
1301 } while (++i < RPCRDMA_MAX_SEGS);
1302 list_add_tail(&req->rl_segments[0].mr_chunk.rl_mw->mw_list,
1304 req->rl_segments[0].mr_chunk.rl_mw = NULL;
1309 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1313 * Recover reply buffers from pool.
1314 * This happens when recovering from error conditions.
1315 * Post-increment counter/array index.
1318 rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1320 struct rpcrdma_buffer *buffers = req->rl_buffer;
1321 unsigned long flags;
1323 if (req->rl_iov.length == 0) /* special case xprt_rdma_allocate() */
1324 buffers = ((struct rpcrdma_req *) buffers)->rl_buffer;
1325 spin_lock_irqsave(&buffers->rb_lock, flags);
1326 if (buffers->rb_recv_index < buffers->rb_max_requests) {
1327 req->rl_reply = buffers->rb_recv_bufs[buffers->rb_recv_index];
1328 buffers->rb_recv_bufs[buffers->rb_recv_index++] = NULL;
1330 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1334 * Put reply buffers back into pool when not attached to
1335 * request. This happens in error conditions.
1338 rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1340 struct rpcrdma_buffer *buffers = rep->rr_buffer;
1341 unsigned long flags;
1343 rep->rr_func = NULL;
1344 spin_lock_irqsave(&buffers->rb_lock, flags);
1345 buffers->rb_recv_bufs[--buffers->rb_recv_index] = rep;
1346 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1350 * Wrappers for internal-use kmalloc memory registration, used by buffer code.
1354 rpcrdma_register_internal(struct rpcrdma_ia *ia, void *va, int len,
1355 struct ib_mr **mrp, struct ib_sge *iov)
1357 struct ib_phys_buf ipb;
1362 * All memory passed here was kmalloc'ed, therefore phys-contiguous.
1364 iov->addr = ib_dma_map_single(ia->ri_id->device,
1365 va, len, DMA_BIDIRECTIONAL);
1368 if (ia->ri_have_dma_lkey) {
1370 iov->lkey = ia->ri_dma_lkey;
1372 } else if (ia->ri_bind_mem != NULL) {
1374 iov->lkey = ia->ri_bind_mem->lkey;
1378 ipb.addr = iov->addr;
1379 ipb.size = iov->length;
1380 mr = ib_reg_phys_mr(ia->ri_pd, &ipb, 1,
1381 IB_ACCESS_LOCAL_WRITE, &iov->addr);
1383 dprintk("RPC: %s: phys convert: 0x%llx "
1384 "registered 0x%llx length %d\n",
1385 __func__, (unsigned long long)ipb.addr,
1386 (unsigned long long)iov->addr, len);
1391 dprintk("RPC: %s: failed with %i\n", __func__, rc);
1394 iov->lkey = mr->lkey;
1402 rpcrdma_deregister_internal(struct rpcrdma_ia *ia,
1403 struct ib_mr *mr, struct ib_sge *iov)
1407 ib_dma_unmap_single(ia->ri_id->device,
1408 iov->addr, iov->length, DMA_BIDIRECTIONAL);
1413 rc = ib_dereg_mr(mr);
1415 dprintk("RPC: %s: ib_dereg_mr failed %i\n", __func__, rc);
1420 * Wrappers for chunk registration, shared by read/write chunk code.
1424 rpcrdma_map_one(struct rpcrdma_ia *ia, struct rpcrdma_mr_seg *seg, int writing)
1426 seg->mr_dir = writing ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1427 seg->mr_dmalen = seg->mr_len;
1429 seg->mr_dma = ib_dma_map_page(ia->ri_id->device,
1430 seg->mr_page, offset_in_page(seg->mr_offset),
1431 seg->mr_dmalen, seg->mr_dir);
1433 seg->mr_dma = ib_dma_map_single(ia->ri_id->device,
1435 seg->mr_dmalen, seg->mr_dir);
1436 if (ib_dma_mapping_error(ia->ri_id->device, seg->mr_dma)) {
1437 dprintk("RPC: %s: mr_dma %llx mr_offset %p mr_dma_len %zu\n",
1439 (unsigned long long)seg->mr_dma,
1440 seg->mr_offset, seg->mr_dmalen);
1445 rpcrdma_unmap_one(struct rpcrdma_ia *ia, struct rpcrdma_mr_seg *seg)
1448 ib_dma_unmap_page(ia->ri_id->device,
1449 seg->mr_dma, seg->mr_dmalen, seg->mr_dir);
1451 ib_dma_unmap_single(ia->ri_id->device,
1452 seg->mr_dma, seg->mr_dmalen, seg->mr_dir);
1456 rpcrdma_register_frmr_external(struct rpcrdma_mr_seg *seg,
1457 int *nsegs, int writing, struct rpcrdma_ia *ia,
1458 struct rpcrdma_xprt *r_xprt)
1460 struct rpcrdma_mr_seg *seg1 = seg;
1461 struct ib_send_wr invalidate_wr, frmr_wr, *bad_wr, *post_wr;
1470 pageoff = offset_in_page(seg1->mr_offset);
1471 seg1->mr_offset -= pageoff; /* start of page */
1472 seg1->mr_len += pageoff;
1474 if (*nsegs > ia->ri_max_frmr_depth)
1475 *nsegs = ia->ri_max_frmr_depth;
1476 for (page_no = i = 0; i < *nsegs;) {
1477 rpcrdma_map_one(ia, seg, writing);
1479 for (seg_len = seg->mr_len; seg_len > 0; seg_len -= PAGE_SIZE) {
1480 seg1->mr_chunk.rl_mw->r.frmr.fr_pgl->
1481 page_list[page_no++] = pa;
1487 /* Check for holes */
1488 if ((i < *nsegs && offset_in_page(seg->mr_offset)) ||
1489 offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len))
1492 dprintk("RPC: %s: Using frmr %p to map %d segments\n",
1493 __func__, seg1->mr_chunk.rl_mw, i);
1495 if (unlikely(seg1->mr_chunk.rl_mw->r.frmr.state == FRMR_IS_VALID)) {
1496 dprintk("RPC: %s: frmr %x left valid, posting invalidate.\n",
1498 seg1->mr_chunk.rl_mw->r.frmr.fr_mr->rkey);
1499 /* Invalidate before using. */
1500 memset(&invalidate_wr, 0, sizeof invalidate_wr);
1501 invalidate_wr.wr_id = (unsigned long)(void *)seg1->mr_chunk.rl_mw;
1502 invalidate_wr.next = &frmr_wr;
1503 invalidate_wr.opcode = IB_WR_LOCAL_INV;
1504 invalidate_wr.send_flags = IB_SEND_SIGNALED;
1505 invalidate_wr.ex.invalidate_rkey =
1506 seg1->mr_chunk.rl_mw->r.frmr.fr_mr->rkey;
1507 DECR_CQCOUNT(&r_xprt->rx_ep);
1508 post_wr = &invalidate_wr;
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 /* Prepare FRMR WR */
1517 memset(&frmr_wr, 0, sizeof frmr_wr);
1518 frmr_wr.wr_id = (unsigned long)(void *)seg1->mr_chunk.rl_mw;
1519 frmr_wr.opcode = IB_WR_FAST_REG_MR;
1520 frmr_wr.send_flags = IB_SEND_SIGNALED;
1521 frmr_wr.wr.fast_reg.iova_start = seg1->mr_dma;
1522 frmr_wr.wr.fast_reg.page_list = seg1->mr_chunk.rl_mw->r.frmr.fr_pgl;
1523 frmr_wr.wr.fast_reg.page_list_len = page_no;
1524 frmr_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
1525 frmr_wr.wr.fast_reg.length = page_no << PAGE_SHIFT;
1526 BUG_ON(frmr_wr.wr.fast_reg.length < len);
1527 frmr_wr.wr.fast_reg.access_flags = (writing ?
1528 IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
1529 IB_ACCESS_REMOTE_READ);
1530 frmr_wr.wr.fast_reg.rkey = seg1->mr_chunk.rl_mw->r.frmr.fr_mr->rkey;
1531 DECR_CQCOUNT(&r_xprt->rx_ep);
1533 rc = ib_post_send(ia->ri_id->qp, post_wr, &bad_wr);
1536 dprintk("RPC: %s: failed ib_post_send for register,"
1537 " status %i\n", __func__, rc);
1539 rpcrdma_unmap_one(ia, --seg);
1541 seg1->mr_rkey = seg1->mr_chunk.rl_mw->r.frmr.fr_mr->rkey;
1542 seg1->mr_base = seg1->mr_dma + pageoff;
1551 rpcrdma_deregister_frmr_external(struct rpcrdma_mr_seg *seg,
1552 struct rpcrdma_ia *ia, struct rpcrdma_xprt *r_xprt)
1554 struct rpcrdma_mr_seg *seg1 = seg;
1555 struct ib_send_wr invalidate_wr, *bad_wr;
1558 while (seg1->mr_nsegs--)
1559 rpcrdma_unmap_one(ia, seg++);
1561 memset(&invalidate_wr, 0, sizeof invalidate_wr);
1562 invalidate_wr.wr_id = (unsigned long)(void *)seg1->mr_chunk.rl_mw;
1563 invalidate_wr.opcode = IB_WR_LOCAL_INV;
1564 invalidate_wr.send_flags = IB_SEND_SIGNALED;
1565 invalidate_wr.ex.invalidate_rkey = seg1->mr_chunk.rl_mw->r.frmr.fr_mr->rkey;
1566 DECR_CQCOUNT(&r_xprt->rx_ep);
1568 rc = ib_post_send(ia->ri_id->qp, &invalidate_wr, &bad_wr);
1570 dprintk("RPC: %s: failed ib_post_send for invalidate,"
1571 " status %i\n", __func__, rc);
1576 rpcrdma_register_fmr_external(struct rpcrdma_mr_seg *seg,
1577 int *nsegs, int writing, struct rpcrdma_ia *ia)
1579 struct rpcrdma_mr_seg *seg1 = seg;
1580 u64 physaddrs[RPCRDMA_MAX_DATA_SEGS];
1581 int len, pageoff, i, rc;
1583 pageoff = offset_in_page(seg1->mr_offset);
1584 seg1->mr_offset -= pageoff; /* start of page */
1585 seg1->mr_len += pageoff;
1587 if (*nsegs > RPCRDMA_MAX_DATA_SEGS)
1588 *nsegs = RPCRDMA_MAX_DATA_SEGS;
1589 for (i = 0; i < *nsegs;) {
1590 rpcrdma_map_one(ia, seg, writing);
1591 physaddrs[i] = seg->mr_dma;
1595 /* Check for holes */
1596 if ((i < *nsegs && offset_in_page(seg->mr_offset)) ||
1597 offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len))
1600 rc = ib_map_phys_fmr(seg1->mr_chunk.rl_mw->r.fmr,
1601 physaddrs, i, seg1->mr_dma);
1603 dprintk("RPC: %s: failed ib_map_phys_fmr "
1604 "%u@0x%llx+%i (%d)... status %i\n", __func__,
1605 len, (unsigned long long)seg1->mr_dma,
1608 rpcrdma_unmap_one(ia, --seg);
1610 seg1->mr_rkey = seg1->mr_chunk.rl_mw->r.fmr->rkey;
1611 seg1->mr_base = seg1->mr_dma + pageoff;
1620 rpcrdma_deregister_fmr_external(struct rpcrdma_mr_seg *seg,
1621 struct rpcrdma_ia *ia)
1623 struct rpcrdma_mr_seg *seg1 = seg;
1627 list_add(&seg1->mr_chunk.rl_mw->r.fmr->list, &l);
1628 rc = ib_unmap_fmr(&l);
1629 while (seg1->mr_nsegs--)
1630 rpcrdma_unmap_one(ia, seg++);
1632 dprintk("RPC: %s: failed ib_unmap_fmr,"
1633 " status %i\n", __func__, rc);
1638 rpcrdma_register_external(struct rpcrdma_mr_seg *seg,
1639 int nsegs, int writing, struct rpcrdma_xprt *r_xprt)
1641 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
1644 switch (ia->ri_memreg_strategy) {
1646 #if RPCRDMA_PERSISTENT_REGISTRATION
1647 case RPCRDMA_ALLPHYSICAL:
1648 rpcrdma_map_one(ia, seg, writing);
1649 seg->mr_rkey = ia->ri_bind_mem->rkey;
1650 seg->mr_base = seg->mr_dma;
1656 /* Registration using frmr registration */
1658 rc = rpcrdma_register_frmr_external(seg, &nsegs, writing, ia, r_xprt);
1661 /* Registration using fmr memory registration */
1662 case RPCRDMA_MTHCAFMR:
1663 rc = rpcrdma_register_fmr_external(seg, &nsegs, writing, ia);
1676 rpcrdma_deregister_external(struct rpcrdma_mr_seg *seg,
1677 struct rpcrdma_xprt *r_xprt)
1679 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
1680 int nsegs = seg->mr_nsegs, rc;
1682 switch (ia->ri_memreg_strategy) {
1684 #if RPCRDMA_PERSISTENT_REGISTRATION
1685 case RPCRDMA_ALLPHYSICAL:
1687 rpcrdma_unmap_one(ia, seg);
1693 rc = rpcrdma_deregister_frmr_external(seg, ia, r_xprt);
1696 case RPCRDMA_MTHCAFMR:
1697 rc = rpcrdma_deregister_fmr_external(seg, ia);
1707 * Prepost any receive buffer, then post send.
1709 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1712 rpcrdma_ep_post(struct rpcrdma_ia *ia,
1713 struct rpcrdma_ep *ep,
1714 struct rpcrdma_req *req)
1716 struct ib_send_wr send_wr, *send_wr_fail;
1717 struct rpcrdma_rep *rep = req->rl_reply;
1721 rc = rpcrdma_ep_post_recv(ia, ep, rep);
1724 req->rl_reply = NULL;
1727 send_wr.next = NULL;
1728 send_wr.wr_id = 0ULL; /* no send cookie */
1729 send_wr.sg_list = req->rl_send_iov;
1730 send_wr.num_sge = req->rl_niovs;
1731 send_wr.opcode = IB_WR_SEND;
1732 if (send_wr.num_sge == 4) /* no need to sync any pad (constant) */
1733 ib_dma_sync_single_for_device(ia->ri_id->device,
1734 req->rl_send_iov[3].addr, req->rl_send_iov[3].length,
1736 ib_dma_sync_single_for_device(ia->ri_id->device,
1737 req->rl_send_iov[1].addr, req->rl_send_iov[1].length,
1739 ib_dma_sync_single_for_device(ia->ri_id->device,
1740 req->rl_send_iov[0].addr, req->rl_send_iov[0].length,
1743 if (DECR_CQCOUNT(ep) > 0)
1744 send_wr.send_flags = 0;
1745 else { /* Provider must take a send completion every now and then */
1747 send_wr.send_flags = IB_SEND_SIGNALED;
1750 rc = ib_post_send(ia->ri_id->qp, &send_wr, &send_wr_fail);
1752 dprintk("RPC: %s: ib_post_send returned %i\n", __func__,
1759 * (Re)post a receive buffer.
1762 rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
1763 struct rpcrdma_ep *ep,
1764 struct rpcrdma_rep *rep)
1766 struct ib_recv_wr recv_wr, *recv_wr_fail;
1769 recv_wr.next = NULL;
1770 recv_wr.wr_id = (u64) (unsigned long) rep;
1771 recv_wr.sg_list = &rep->rr_iov;
1772 recv_wr.num_sge = 1;
1774 ib_dma_sync_single_for_cpu(ia->ri_id->device,
1775 rep->rr_iov.addr, rep->rr_iov.length, DMA_BIDIRECTIONAL);
1777 rc = ib_post_recv(ia->ri_id->qp, &recv_wr, &recv_wr_fail);
1780 dprintk("RPC: %s: ib_post_recv returned %i\n", __func__,