Whamcloud - gitweb
LU-4423 lnet: don't use iovec instead of kvec
[fs/lustre-release.git] / lnet / klnds / o2iblnd / o2iblnd_cb.c
index 0dd9cdf..8196b1b 100644 (file)
@@ -27,7 +27,7 @@
  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2012, 2014, Intel Corporation.
+ * Copyright (c) 2012, 2015, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -132,7 +132,6 @@ kiblnd_get_idle_tx(lnet_ni_t *ni, lnet_nid_t target)
         LASSERT (tx->tx_conn == NULL);
         LASSERT (tx->tx_lntmsg[0] == NULL);
         LASSERT (tx->tx_lntmsg[1] == NULL);
-        LASSERT (tx->tx_u.pmr == NULL);
         LASSERT (tx->tx_nfrags == 0);
 
         return tx;
@@ -159,7 +158,7 @@ kiblnd_post_rx (kib_rx_t *rx, int credit)
        kib_conn_t         *conn = rx->rx_conn;
        kib_net_t          *net = conn->ibc_peer->ibp_ni->ni_data;
        struct ib_recv_wr  *bad_wrq = NULL;
-       struct ib_mr       *mr;
+       struct ib_mr       *mr = conn->ibc_hdev->ibh_mrs;
        int                 rc;
 
        LASSERT (net != NULL);
@@ -167,9 +166,7 @@ kiblnd_post_rx (kib_rx_t *rx, int credit)
        LASSERT (credit == IBLND_POSTRX_NO_CREDIT ||
                 credit == IBLND_POSTRX_PEER_CREDIT ||
                 credit == IBLND_POSTRX_RSRVD_CREDIT);
-
-       mr = kiblnd_find_dma_mr(conn->ibc_hdev, rx->rx_msgaddr, IBLND_MSG_SIZE);
-       LASSERT (mr != NULL);
+       LASSERT(mr != NULL);
 
         rx->rx_sge.lkey   = mr->lkey;
         rx->rx_sge.addr   = rx->rx_msgaddr;
@@ -332,19 +329,19 @@ kiblnd_handle_rx (kib_rx_t *rx)
                spin_lock(&conn->ibc_lock);
 
                if (conn->ibc_credits + credits >
-                   IBLND_MSG_QUEUE_SIZE(conn->ibc_version)) {
+                   conn->ibc_queue_depth) {
                        rc2 = conn->ibc_credits;
                        spin_unlock(&conn->ibc_lock);
 
-                        CERROR("Bad credits from %s: %d + %d > %d\n",
-                               libcfs_nid2str(conn->ibc_peer->ibp_nid),
-                               rc2, credits,
-                               IBLND_MSG_QUEUE_SIZE(conn->ibc_version));
+                       CERROR("Bad credits from %s: %d + %d > %d\n",
+                              libcfs_nid2str(conn->ibc_peer->ibp_nid),
+                              rc2, credits,
+                              conn->ibc_queue_depth);
 
-                        kiblnd_close_conn(conn, -EPROTO);
-                        kiblnd_post_rx(rx, IBLND_POSTRX_NO_CREDIT);
-                        return;
-                }
+                       kiblnd_close_conn(conn, -EPROTO);
+                       kiblnd_post_rx(rx, IBLND_POSTRX_NO_CREDIT);
+                       return;
+               }
 
                 conn->ibc_credits += credits;
 
@@ -592,57 +589,21 @@ kiblnd_fmr_map_tx(kib_net_t *net, kib_tx_t *tx, kib_rdma_desc_t *rd, int nob)
        cpt = tx->tx_pool->tpo_pool.po_owner->ps_cpt;
 
        fps = net->ibn_fmr_ps[cpt];
-       rc = kiblnd_fmr_pool_map(fps, pages, npages, 0, &tx->tx_u.fmr);
+       rc = kiblnd_fmr_pool_map(fps, pages, npages, 0, &tx->fmr);
         if (rc != 0) {
                 CERROR ("Can't map %d pages: %d\n", npages, rc);
                 return rc;
         }
 
-        /* If rd is not tx_rd, it's going to get sent to a peer, who will need
-         * the rkey */
-        rd->rd_key = (rd != tx->tx_rd) ? tx->tx_u.fmr.fmr_pfmr->fmr->rkey :
-                                         tx->tx_u.fmr.fmr_pfmr->fmr->lkey;
-        rd->rd_frags[0].rf_addr &= ~hdev->ibh_page_mask;
-        rd->rd_frags[0].rf_nob   = nob;
-        rd->rd_nfrags = 1;
-
-        return 0;
-}
-
-static int
-kiblnd_pmr_map_tx(kib_net_t *net, kib_tx_t *tx, kib_rdma_desc_t *rd, int nob)
-{
-       kib_hca_dev_t           *hdev;
-       kib_pmr_poolset_t       *pps;
-       __u64                   iova;
-       int                     cpt;
-       int                     rc;
-
-       LASSERT(tx->tx_pool != NULL);
-       LASSERT(tx->tx_pool->tpo_pool.po_owner != NULL);
-
-       hdev = tx->tx_pool->tpo_hdev;
-
-       iova = rd->rd_frags[0].rf_addr & ~hdev->ibh_page_mask;
-
-       cpt = tx->tx_pool->tpo_pool.po_owner->ps_cpt;
-
-       pps = net->ibn_pmr_ps[cpt];
-       rc = kiblnd_pmr_pool_map(pps, hdev, rd, &iova, &tx->tx_u.pmr);
-        if (rc != 0) {
-                CERROR("Failed to create MR by phybuf: %d\n", rc);
-                return rc;
-        }
-
-        /* If rd is not tx_rd, it's going to get sent to a peer, who will need
-         * the rkey */
-        rd->rd_key = (rd != tx->tx_rd) ? tx->tx_u.pmr->pmr_mr->rkey :
-                                         tx->tx_u.pmr->pmr_mr->lkey;
-        rd->rd_nfrags = 1;
-        rd->rd_frags[0].rf_addr = iova;
-        rd->rd_frags[0].rf_nob  = nob;
+       /* If rd is not tx_rd, it's going to get sent to a peer, who will need
+        * the rkey */
+       rd->rd_key = (rd != tx->tx_rd) ? tx->fmr.fmr_pfmr->fmr->rkey :
+                                        tx->fmr.fmr_pfmr->fmr->lkey;
+       rd->rd_frags[0].rf_addr &= ~hdev->ibh_page_mask;
+       rd->rd_frags[0].rf_nob   = nob;
+       rd->rd_nfrags = 1;
 
-        return 0;
+       return 0;
 }
 
 static void
@@ -652,13 +613,9 @@ kiblnd_unmap_tx(lnet_ni_t *ni, kib_tx_t *tx)
 
        LASSERT(net != NULL);
 
-       if (net->ibn_fmr_ps != NULL && tx->tx_u.fmr.fmr_pfmr != NULL) {
-               kiblnd_fmr_pool_unmap(&tx->tx_u.fmr, tx->tx_status);
-               tx->tx_u.fmr.fmr_pfmr = NULL;
-
-       } else if (net->ibn_pmr_ps != NULL && tx->tx_u.pmr != NULL) {
-               kiblnd_pmr_pool_unmap(tx->tx_u.pmr);
-               tx->tx_u.pmr = NULL;
+       if (net->ibn_fmr_ps != NULL && tx->fmr.fmr_pfmr != NULL) {
+               kiblnd_fmr_pool_unmap(&tx->fmr, tx->tx_status);
+               tx->fmr.fmr_pfmr = NULL;
        }
 
         if (tx->tx_nfrags != 0) {
@@ -693,18 +650,17 @@ kiblnd_map_tx(lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd, int nfrags)
                 nob += rd->rd_frags[i].rf_nob;
         }
 
-        /* looking for pre-mapping MR */
-        mr = kiblnd_find_rd_dma_mr(hdev, rd);
-        if (mr != NULL) {
-                /* found pre-mapping MR */
-                rd->rd_key = (rd != tx->tx_rd) ? mr->rkey : mr->lkey;
-                return 0;
-        }
+       mr = kiblnd_find_rd_dma_mr(hdev, rd,
+                                  (tx->tx_conn != NULL) ?
+                                  tx->tx_conn->ibc_max_frags : -1);
+       if (mr != NULL) {
+               /* found pre-mapping MR */
+               rd->rd_key = (rd != tx->tx_rd) ? mr->rkey : mr->lkey;
+               return 0;
+       }
 
        if (net->ibn_fmr_ps != NULL)
                return kiblnd_fmr_map_tx(net, tx, rd, nob);
-       else if (net->ibn_pmr_ps != NULL)
-               return kiblnd_pmr_map_tx(net, tx, rd, nob);
 
        return -EINVAL;
 }
@@ -712,7 +668,7 @@ kiblnd_map_tx(lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd, int nfrags)
 
 static int
 kiblnd_setup_rd_iov(lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
-                    unsigned int niov, struct iovec *iov, int offset, int nob)
+                   unsigned int niov, struct kvec *iov, int offset, int nob)
 {
         kib_net_t          *net = ni->ni_data;
         struct page        *page;
@@ -814,16 +770,16 @@ __must_hold(&conn->ibc_lock)
         int                done;
         struct ib_send_wr *bad_wrq;
 
-        LASSERT (tx->tx_queued);
-        /* We rely on this for QP sizing */
-        LASSERT (tx->tx_nwrq > 0);
-        LASSERT (tx->tx_nwrq <= 1 + IBLND_RDMA_FRAGS(ver));
+       LASSERT(tx->tx_queued);
+       /* We rely on this for QP sizing */
+       LASSERT(tx->tx_nwrq > 0);
+       LASSERT(tx->tx_nwrq <= 1 + conn->ibc_max_frags);
 
-        LASSERT (credit == 0 || credit == 1);
-        LASSERT (conn->ibc_outstanding_credits >= 0);
-        LASSERT (conn->ibc_outstanding_credits <= IBLND_MSG_QUEUE_SIZE(ver));
-        LASSERT (conn->ibc_credits >= 0);
-        LASSERT (conn->ibc_credits <= IBLND_MSG_QUEUE_SIZE(ver));
+       LASSERT(credit == 0 || credit == 1);
+       LASSERT(conn->ibc_outstanding_credits >= 0);
+       LASSERT(conn->ibc_outstanding_credits <= conn->ibc_queue_depth);
+       LASSERT(conn->ibc_credits >= 0);
+       LASSERT(conn->ibc_credits <= conn->ibc_queue_depth);
 
         if (conn->ibc_nsends_posted == IBLND_CONCURRENT_SENDS(ver)) {
                 /* tx completions outstanding... */
@@ -1070,17 +1026,15 @@ kiblnd_init_tx_msg (lnet_ni_t *ni, kib_tx_t *tx, int type, int body_nob)
         struct ib_sge     *sge = &tx->tx_sge[tx->tx_nwrq];
         struct ib_send_wr *wrq = &tx->tx_wrq[tx->tx_nwrq];
         int                nob = offsetof (kib_msg_t, ibm_u) + body_nob;
-        struct ib_mr      *mr;
+       struct ib_mr      *mr = hdev->ibh_mrs;
 
-        LASSERT (tx->tx_nwrq >= 0);
-        LASSERT (tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
-        LASSERT (nob <= IBLND_MSG_SIZE);
+       LASSERT(tx->tx_nwrq >= 0);
+       LASSERT(tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
+       LASSERT(nob <= IBLND_MSG_SIZE);
+       LASSERT(mr != NULL);
 
         kiblnd_init_msg(tx->tx_msg, type, body_nob);
 
-        mr = kiblnd_find_dma_mr(hdev, tx->tx_msgaddr, nob);
-        LASSERT (mr != NULL);
-
         sge->lkey   = mr->lkey;
         sge->addr   = tx->tx_msgaddr;
         sge->length = nob;
@@ -1130,16 +1084,16 @@ kiblnd_init_rdma(kib_conn_t *conn, kib_tx_t *tx, int type,
                         break;
                 }
 
-                if (tx->tx_nwrq == IBLND_RDMA_FRAGS(conn->ibc_version)) {
-                        CERROR("RDMA too fragmented for %s (%d): "
-                               "%d/%d src %d/%d dst frags\n",
-                               libcfs_nid2str(conn->ibc_peer->ibp_nid),
-                               IBLND_RDMA_FRAGS(conn->ibc_version),
-                               srcidx, srcrd->rd_nfrags,
-                               dstidx, dstrd->rd_nfrags);
-                        rc = -EMSGSIZE;
-                        break;
-                }
+               if (tx->tx_nwrq >= conn->ibc_max_frags) {
+                       CERROR("RDMA has too many fragments for peer %s (%d), "
+                              "src idx/frags: %d/%d dst idx/frags: %d/%d\n",
+                              libcfs_nid2str(conn->ibc_peer->ibp_nid),
+                              conn->ibc_max_frags,
+                              srcidx, srcrd->rd_nfrags,
+                              dstidx, dstrd->rd_nfrags);
+                       rc = -EMSGSIZE;
+                       break;
+               }
 
                 wrknob = MIN(MIN(kiblnd_rd_frag_size(srcrd, srcidx),
                                  kiblnd_rd_frag_size(dstrd, dstidx)), resid);
@@ -1412,17 +1366,17 @@ kiblnd_launch_tx (lnet_ni_t *ni, kib_tx_t *tx, lnet_nid_t nid)
 
        write_unlock_irqrestore(g_lock, flags);
 
-        /* Allocate a peer ready to add to the peer table and retry */
-        rc = kiblnd_create_peer(ni, &peer, nid);
-        if (rc != 0) {
-                CERROR("Can't create peer %s\n", libcfs_nid2str(nid));
-                if (tx != NULL) {
-                        tx->tx_status = -EHOSTUNREACH;
-                        tx->tx_waiting = 0;
-                        kiblnd_tx_done(ni, tx);
-                }
-                return;
-        }
+       /* Allocate a peer ready to add to the peer table and retry */
+       rc = kiblnd_create_peer(ni, &peer, nid);
+       if (rc != 0) {
+               CERROR("Can't create peer %s\n", libcfs_nid2str(nid));
+               if (tx != NULL) {
+                       tx->tx_status = -EHOSTUNREACH;
+                       tx->tx_waiting = 0;
+                       kiblnd_tx_done(ni, tx);
+               }
+               return;
+       }
 
        write_lock_irqsave(g_lock, flags);
 
@@ -1479,7 +1433,7 @@ kiblnd_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg)
        int               target_is_router = lntmsg->msg_target_is_router;
        int               routing = lntmsg->msg_routing;
        unsigned int      payload_niov = lntmsg->msg_niov;
-       struct iovec     *payload_iov = lntmsg->msg_iov;
+       struct kvec      *payload_iov = lntmsg->msg_iov;
        lnet_kiov_t      *payload_kiov = lntmsg->msg_kiov;
        unsigned int      payload_offset = lntmsg->msg_offset;
        unsigned int      payload_nob = lntmsg->msg_len;
@@ -1645,7 +1599,7 @@ kiblnd_reply (lnet_ni_t *ni, kib_rx_t *rx, lnet_msg_t *lntmsg)
 {
         lnet_process_id_t target = lntmsg->msg_target;
         unsigned int      niov = lntmsg->msg_niov;
-        struct iovec     *iov = lntmsg->msg_iov;
+       struct kvec      *iov = lntmsg->msg_iov;
         lnet_kiov_t      *kiov = lntmsg->msg_kiov;
         unsigned int      offset = lntmsg->msg_offset;
         unsigned int      nob = lntmsg->msg_len;
@@ -1703,9 +1657,9 @@ kiblnd_reply (lnet_ni_t *ni, kib_rx_t *rx, lnet_msg_t *lntmsg)
 }
 
 int
-kiblnd_recv (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, int delayed,
-             unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
-             unsigned int offset, unsigned int mlen, unsigned int rlen)
+kiblnd_recv(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, int delayed,
+           unsigned int niov, struct kvec *iov, lnet_kiov_t *kiov,
+           unsigned int offset, unsigned int mlen, unsigned int rlen)
 {
         kib_rx_t    *rx = private;
         kib_msg_t   *rxmsg = rx->rx_msg;
@@ -2274,7 +2228,7 @@ kiblnd_passive_connect(struct rdma_cm_id *cmid, void *priv, int priv_nob)
         if (ni == NULL ||                         /* no matching net */
             ni->ni_nid != reqmsg->ibm_dstnid ||   /* right NET, wrong NID! */
             net->ibn_dev != ibdev) {              /* wrong device */
-               CERROR("Can't accept %s on %s (%s:%d:%pI4h): "
+               CERROR("Can't accept conn from %s on %s (%s:%d:%pI4h): "
                        "bad dst nid %s\n", libcfs_nid2str(nid),
                        ni == NULL ? "NA" : libcfs_nid2str(ni->ni_nid),
                        ibdev->ibd_ifname, ibdev->ibd_nnets,
@@ -2301,32 +2255,46 @@ kiblnd_passive_connect(struct rdma_cm_id *cmid, void *priv, int priv_nob)
                 goto failed;
         }
 
-        if (reqmsg->ibm_u.connparams.ibcp_queue_depth !=
-            IBLND_MSG_QUEUE_SIZE(version)) {
-                CERROR("Can't accept %s: incompatible queue depth %d (%d wanted)\n",
-                       libcfs_nid2str(nid), reqmsg->ibm_u.connparams.ibcp_queue_depth,
-                       IBLND_MSG_QUEUE_SIZE(version));
-
-                if (version == IBLND_MSG_VERSION)
-                        rej.ibr_why = IBLND_REJECT_MSG_QUEUE_SIZE;
-
-                goto failed;
-        }
-
-        if (reqmsg->ibm_u.connparams.ibcp_max_frags !=
-            IBLND_RDMA_FRAGS(version)) {
-                CERROR("Can't accept %s(version %x): "
-                       "incompatible max_frags %d (%d wanted)\n",
-                       libcfs_nid2str(nid), version,
-                       reqmsg->ibm_u.connparams.ibcp_max_frags,
-                       IBLND_RDMA_FRAGS(version));
+       if (reqmsg->ibm_u.connparams.ibcp_queue_depth >
+           IBLND_MSG_QUEUE_SIZE(version)) {
+               CERROR("Can't accept conn from %s, queue depth too large: "
+                      " %d (<=%d wanted)\n",
+                      libcfs_nid2str(nid),
+                      reqmsg->ibm_u.connparams.ibcp_queue_depth,
+                      IBLND_MSG_QUEUE_SIZE(version));
 
-                if (version == IBLND_MSG_VERSION)
-                        rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
+               if (version == IBLND_MSG_VERSION)
+                       rej.ibr_why = IBLND_REJECT_MSG_QUEUE_SIZE;
 
-                goto failed;
+               goto failed;
+       }
 
-        }
+       if (reqmsg->ibm_u.connparams.ibcp_max_frags >
+           IBLND_RDMA_FRAGS(version)) {
+               CWARN("Can't accept conn from %s (version %x): "
+                     "max_frags %d too large (%d wanted)\n",
+                      libcfs_nid2str(nid), version,
+                      reqmsg->ibm_u.connparams.ibcp_max_frags,
+                      IBLND_RDMA_FRAGS(version));
+
+               if (version >= IBLND_MSG_VERSION)
+                       rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
+
+               goto failed;
+       } else if (reqmsg->ibm_u.connparams.ibcp_max_frags <
+                  IBLND_RDMA_FRAGS(version) && net->ibn_fmr_ps == NULL) {
+               CWARN("Can't accept conn from %s (version %x): "
+                     "max_frags %d incompatible without FMR pool "
+                     "(%d wanted)\n",
+                     libcfs_nid2str(nid), version,
+                     reqmsg->ibm_u.connparams.ibcp_max_frags,
+                     IBLND_RDMA_FRAGS(version));
+
+               if (version >= IBLND_MSG_VERSION)
+                       rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
+
+               goto failed;
+       }
 
         if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
                 CERROR("Can't accept %s: message size %d too big (%d max)\n",
@@ -2336,13 +2304,17 @@ kiblnd_passive_connect(struct rdma_cm_id *cmid, void *priv, int priv_nob)
                 goto failed;
         }
 
-        /* assume 'nid' is a new peer; create  */
-        rc = kiblnd_create_peer(ni, &peer, nid);
-        if (rc != 0) {
-                CERROR("Can't create peer for %s\n", libcfs_nid2str(nid));
-                rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
-                goto failed;
-        }
+       /* assume 'nid' is a new peer; create  */
+       rc = kiblnd_create_peer(ni, &peer, nid);
+       if (rc != 0) {
+               CERROR("Can't create peer for %s\n", libcfs_nid2str(nid));
+               rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
+               goto failed;
+       }
+
+       /* We have validated the peer's parameters so use those */
+       peer->ibp_max_frags = reqmsg->ibm_u.connparams.ibcp_max_frags;
+       peer->ibp_queue_depth = reqmsg->ibm_u.connparams.ibcp_queue_depth;
 
        write_lock_irqsave(g_lock, flags);
 
@@ -2382,6 +2354,12 @@ kiblnd_passive_connect(struct rdma_cm_id *cmid, void *priv, int priv_nob)
                 peer2->ibp_accepting++;
                 kiblnd_peer_addref(peer2);
 
+               /* Race with kiblnd_launch_tx (active connect) to create peer
+                * so copy validated parameters since we now know what the
+                * peer's limits are */
+               peer2->ibp_max_frags = peer->ibp_max_frags;
+               peer2->ibp_queue_depth = peer->ibp_queue_depth;
+
                write_unlock_irqrestore(g_lock, flags);
                 kiblnd_peer_decref(peer);
                 peer = peer2;
@@ -2404,7 +2382,7 @@ kiblnd_passive_connect(struct rdma_cm_id *cmid, void *priv, int priv_nob)
                write_unlock_irqrestore(g_lock, flags);
         }
 
-        conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_PASSIVE_WAIT, version);
+       conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_PASSIVE_WAIT, version);
         if (conn == NULL) {
                 kiblnd_peer_connect_failed(peer, 0, -ENOMEM);
                 kiblnd_peer_decref(peer);
@@ -2414,21 +2392,20 @@ kiblnd_passive_connect(struct rdma_cm_id *cmid, void *priv, int priv_nob)
 
         /* conn now "owns" cmid, so I return success from here on to ensure the
          * CM callback doesn't destroy cmid. */
-
-        conn->ibc_incarnation      = reqmsg->ibm_srcstamp;
-        conn->ibc_credits          = IBLND_MSG_QUEUE_SIZE(version);
-        conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE(version);
-        LASSERT (conn->ibc_credits + conn->ibc_reserved_credits + IBLND_OOB_MSGS(version)
-                 <= IBLND_RX_MSGS(version));
+       conn->ibc_incarnation      = reqmsg->ibm_srcstamp;
+       conn->ibc_credits          = conn->ibc_queue_depth;
+       conn->ibc_reserved_credits = conn->ibc_queue_depth;
+       LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
+               IBLND_OOB_MSGS(version) <= IBLND_RX_MSGS(conn));
 
         ackmsg = &conn->ibc_connvars->cv_msg;
         memset(ackmsg, 0, sizeof(*ackmsg));
 
         kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
                         sizeof(ackmsg->ibm_u.connparams));
-        ackmsg->ibm_u.connparams.ibcp_queue_depth  = IBLND_MSG_QUEUE_SIZE(version);
-        ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
-        ackmsg->ibm_u.connparams.ibcp_max_frags    = IBLND_RDMA_FRAGS(version);
+       ackmsg->ibm_u.connparams.ibcp_queue_depth  = conn->ibc_queue_depth;
+       ackmsg->ibm_u.connparams.ibcp_max_frags    = conn->ibc_max_frags;
+       ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
 
         kiblnd_pack_msg(ni, ackmsg, version, 0, nid, reqmsg->ibm_srcstamp);
 
@@ -2501,10 +2478,9 @@ kiblnd_reconnect (kib_conn_t *conn, int version,
                } else {
                        retry_now = 1;
                }
-                peer->ibp_connecting++;
-
-                peer->ibp_version     = version;
-                peer->ibp_incarnation = incarnation;
+               peer->ibp_connecting++;
+               peer->ibp_version     = version;
+               peer->ibp_incarnation = incarnation;
         }
 
        write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
@@ -2517,6 +2493,38 @@ kiblnd_reconnect (kib_conn_t *conn, int version,
                 reason = "Unknown";
                 break;
 
+       case IBLND_REJECT_RDMA_FRAGS:
+               if (!cp)
+                       goto failed;
+
+               if (conn->ibc_max_frags <= cp->ibcp_max_frags) {
+                       CNETERR("Unsupported max frags, peer supports %d\n",
+                               cp->ibcp_max_frags);
+                       goto failed;
+               } else if (*kiblnd_tunables.kib_map_on_demand == 0) {
+                       CNETERR("map_on_demand must be enabled to support "
+                               "map_on_demand peers\n");
+                       goto failed;
+               }
+
+               peer->ibp_max_frags = cp->ibcp_max_frags;
+               reason = "rdma fragments";
+               break;
+
+       case IBLND_REJECT_MSG_QUEUE_SIZE:
+               if (!cp)
+                       goto failed;
+
+               if (conn->ibc_queue_depth <= cp->ibcp_queue_depth) {
+                       CNETERR("Unsupported queue depth, peer supports %d\n",
+                               cp->ibcp_queue_depth);
+                       goto failed;
+               }
+
+               peer->ibp_queue_depth = cp->ibcp_queue_depth;
+               reason = "queue depth";
+               break;
+
         case IBLND_REJECT_CONN_STALE:
                 reason = "stale";
                 break;
@@ -2526,15 +2534,22 @@ kiblnd_reconnect (kib_conn_t *conn, int version,
                 break;
         }
 
-        CNETERR("%s: retrying (%s), %x, %x, "
-                "queue_dep: %d, max_frag: %d, msg_size: %d\n",
-                libcfs_nid2str(peer->ibp_nid),
-                reason, IBLND_MSG_VERSION, version,
-                cp != NULL? cp->ibcp_queue_depth :IBLND_MSG_QUEUE_SIZE(version),
-                cp != NULL? cp->ibcp_max_frags   : IBLND_RDMA_FRAGS(version),
-                cp != NULL? cp->ibcp_max_msg_size: IBLND_MSG_SIZE);
+       CNETERR("%s: retrying (%s), %x, %x, "
+               "queue_depth: %d, max_frags: %d, msg_size: %d\n",
+               libcfs_nid2str(peer->ibp_nid),
+               reason, IBLND_MSG_VERSION, version,
+               conn->ibc_queue_depth, conn->ibc_max_frags,
+               cp != NULL ? cp->ibcp_max_msg_size : IBLND_MSG_SIZE);
 
         kiblnd_connect_peer(peer);
+       return;
+
+ failed:
+       write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
+       peer->ibp_connecting--;
+       write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
+
+       return;
 }
 
 static void
@@ -2628,26 +2643,12 @@ kiblnd_rejected (kib_conn_t *conn, int reason, void *priv, int priv_nob)
                         case IBLND_REJECT_CONN_RACE:
                         case IBLND_REJECT_CONN_STALE:
                         case IBLND_REJECT_CONN_UNCOMPAT:
+                       case IBLND_REJECT_MSG_QUEUE_SIZE:
+                       case IBLND_REJECT_RDMA_FRAGS:
                                 kiblnd_reconnect(conn, rej->ibr_version,
                                                  incarnation, rej->ibr_why, cp);
                                 break;
 
-                        case IBLND_REJECT_MSG_QUEUE_SIZE:
-                                CERROR("%s rejected: incompatible message queue depth %d, %d\n",
-                                      libcfs_nid2str(peer->ibp_nid),
-                                      cp != NULL ? cp->ibcp_queue_depth :
-                                      IBLND_MSG_QUEUE_SIZE(rej->ibr_version),
-                                      IBLND_MSG_QUEUE_SIZE(conn->ibc_version));
-                                break;
-
-                        case IBLND_REJECT_RDMA_FRAGS:
-                                CERROR("%s rejected: incompatible # of RDMA fragments %d, %d\n",
-                                      libcfs_nid2str(peer->ibp_nid),
-                                      cp != NULL ? cp->ibcp_max_frags :
-                                      IBLND_RDMA_FRAGS(rej->ibr_version),
-                                      IBLND_RDMA_FRAGS(conn->ibc_version));
-                                break;
-
                         case IBLND_REJECT_NO_RESOURCES:
                                 CERROR("%s rejected: o2iblnd no resources\n",
                                        libcfs_nid2str(peer->ibp_nid));
@@ -2710,25 +2711,25 @@ kiblnd_check_connreply (kib_conn_t *conn, void *priv, int priv_nob)
                 goto failed;
         }
 
-        if (msg->ibm_u.connparams.ibcp_queue_depth !=
-            IBLND_MSG_QUEUE_SIZE(ver)) {
-                CERROR("%s has incompatible queue depth %d(%d wanted)\n",
-                       libcfs_nid2str(peer->ibp_nid),
-                       msg->ibm_u.connparams.ibcp_queue_depth,
-                       IBLND_MSG_QUEUE_SIZE(ver));
-                rc = -EPROTO;
-                goto failed;
-        }
+       if (msg->ibm_u.connparams.ibcp_queue_depth >
+           conn->ibc_queue_depth) {
+               CERROR("%s has incompatible queue depth %d (<=%d wanted)\n",
+                      libcfs_nid2str(peer->ibp_nid),
+                      msg->ibm_u.connparams.ibcp_queue_depth,
+                      conn->ibc_queue_depth);
+               rc = -EPROTO;
+               goto failed;
+       }
 
-        if (msg->ibm_u.connparams.ibcp_max_frags !=
-            IBLND_RDMA_FRAGS(ver)) {
-                CERROR("%s has incompatible max_frags %d (%d wanted)\n",
-                       libcfs_nid2str(peer->ibp_nid),
-                       msg->ibm_u.connparams.ibcp_max_frags,
-                       IBLND_RDMA_FRAGS(ver));
-                rc = -EPROTO;
-                goto failed;
-        }
+       if (msg->ibm_u.connparams.ibcp_max_frags >
+           conn->ibc_max_frags) {
+               CERROR("%s has incompatible max_frags %d (<=%d wanted)\n",
+                      libcfs_nid2str(peer->ibp_nid),
+                      msg->ibm_u.connparams.ibcp_max_frags,
+                      conn->ibc_max_frags);
+               rc = -EPROTO;
+               goto failed;
+       }
 
         if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
                 CERROR("%s max message size %d too big (%d max)\n",
@@ -2755,11 +2756,13 @@ kiblnd_check_connreply (kib_conn_t *conn, void *priv, int priv_nob)
                 goto failed;
         }
 
-        conn->ibc_incarnation      = msg->ibm_srcstamp;
-        conn->ibc_credits          =
-        conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE(ver);
-        LASSERT (conn->ibc_credits + conn->ibc_reserved_credits + IBLND_OOB_MSGS(ver)
-                 <= IBLND_RX_MSGS(ver));
+       conn->ibc_incarnation      = msg->ibm_srcstamp;
+       conn->ibc_credits          = msg->ibm_u.connparams.ibcp_queue_depth;
+       conn->ibc_reserved_credits = msg->ibm_u.connparams.ibcp_queue_depth;
+       conn->ibc_queue_depth      = msg->ibm_u.connparams.ibcp_queue_depth;
+       conn->ibc_max_frags        = msg->ibm_u.connparams.ibcp_max_frags;
+       LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
+               IBLND_OOB_MSGS(ver) <= IBLND_RX_MSGS(conn));
 
         kiblnd_connreq_done(conn, 0);
         return;
@@ -2795,7 +2798,8 @@ kiblnd_active_connect (struct rdma_cm_id *cmid)
 
        read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
 
-        conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_ACTIVE_CONNECT, version);
+       conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_ACTIVE_CONNECT,
+                                 version);
         if (conn == NULL) {
                 kiblnd_peer_connect_failed(peer, 1, -ENOMEM);
                 kiblnd_peer_decref(peer); /* lose cmid's ref */
@@ -2808,11 +2812,11 @@ kiblnd_active_connect (struct rdma_cm_id *cmid)
 
         msg = &conn->ibc_connvars->cv_msg;
 
-        memset(msg, 0, sizeof(*msg));
-        kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
-        msg->ibm_u.connparams.ibcp_queue_depth  = IBLND_MSG_QUEUE_SIZE(version);
-        msg->ibm_u.connparams.ibcp_max_frags    = IBLND_RDMA_FRAGS(version);
-        msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
+       memset(msg, 0, sizeof(*msg));
+       kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
+       msg->ibm_u.connparams.ibcp_queue_depth  = conn->ibc_queue_depth;
+       msg->ibm_u.connparams.ibcp_max_frags    = conn->ibc_max_frags;
+       msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
 
         kiblnd_pack_msg(peer->ibp_ni, msg, version,
                         0, peer->ibp_nid, incarnation);