Whamcloud - gitweb
LU-6245 libcfs: remove sysctl module handling
[fs/lustre-release.git] / lnet / klnds / o2iblnd / o2iblnd.c
index 293bb0c..b6396d2 100644 (file)
@@ -337,8 +337,8 @@ kiblnd_create_peer(lnet_ni_t *ni, kib_peer_t **peerp, lnet_nid_t nid)
        peer->ibp_nid = nid;
        peer->ibp_error = 0;
        peer->ibp_last_alive = 0;
-       peer->ibp_max_frags = IBLND_CFG_RDMA_FRAGS;
-       peer->ibp_queue_depth = *kiblnd_tunables.kib_peertxcredits;
+       peer->ibp_max_frags = kiblnd_cfg_rdma_frags(peer->ibp_ni);
+       peer->ibp_queue_depth = ni->ni_peertxcredits;
        atomic_set(&peer->ibp_refcount, 1);     /* 1 ref for caller */
 
        INIT_LIST_HEAD(&peer->ibp_list);        /* not in the peer table yet */
@@ -675,6 +675,7 @@ kiblnd_get_completion_vector(kib_conn_t *conn, int cpt)
        int             vectors;
        int             off;
        int             i;
+       lnet_nid_t      ibp_nid;
 
        vectors = conn->ibc_cmid->device->num_comp_vectors;
        if (vectors <= 1)
@@ -683,7 +684,8 @@ kiblnd_get_completion_vector(kib_conn_t *conn, int cpt)
        mask = cfs_cpt_cpumask(lnet_cpt_table(), cpt);
 
        /* hash NID to CPU id in this partition... */
-       off = conn->ibc_peer->ibp_nid % cpumask_weight(mask);
+       ibp_nid = conn->ibc_peer->ibp_nid;
+       off = do_div(ibp_nid, cpumask_weight(mask));
        for_each_cpu(i, mask) {
                if (off-- == 0)
                        return i % vectors;
@@ -848,24 +850,34 @@ kiblnd_create_conn(kib_peer_t *peer, struct rdma_cm_id *cmid,
 
        conn->ibc_sched = sched;
 
-        rc = rdma_create_qp(cmid, conn->ibc_hdev->ibh_pd, init_qp_attr);
-        if (rc != 0) {
-                CERROR("Can't create QP: %d, send_wr: %d, recv_wr: %d\n",
-                       rc, init_qp_attr->cap.max_send_wr,
-                       init_qp_attr->cap.max_recv_wr);
-                goto failed_2;
-        }
+       do {
+               rc = rdma_create_qp(cmid, conn->ibc_hdev->ibh_pd, init_qp_attr);
+               if (!rc || init_qp_attr->cap.max_send_wr < 16)
+                       break;
 
-        LIBCFS_FREE(init_qp_attr, sizeof(*init_qp_attr));
+               init_qp_attr->cap.max_send_wr -= init_qp_attr->cap.max_send_wr / 4;
+       } while (rc);
+
+       if (rc) {
+               CERROR("Can't create QP: %d, send_wr: %d, recv_wr: %d\n",
+                       rc, init_qp_attr->cap.max_send_wr,
+                       init_qp_attr->cap.max_recv_wr);
+               goto failed_2;
+       }
 
-        /* 1 ref for caller and each rxmsg */
+       if (init_qp_attr->cap.max_send_wr != IBLND_SEND_WRS(conn))
+               CDEBUG(D_NET, "original send wr %d, created with %d\n",
+                       IBLND_SEND_WRS(conn), init_qp_attr->cap.max_send_wr);
+
+       LIBCFS_FREE(init_qp_attr, sizeof(*init_qp_attr));
+
+       /* 1 ref for caller and each rxmsg */
        atomic_set(&conn->ibc_refcount, 1 + IBLND_RX_MSGS(conn));
        conn->ibc_nrx = IBLND_RX_MSGS(conn);
 
-        /* post receives */
+       /* post receives */
        for (i = 0; i < IBLND_RX_MSGS(conn); i++) {
-               rc = kiblnd_post_rx(&conn->ibc_rxs[i],
-                                   IBLND_POSTRX_NO_CREDIT);
+               rc = kiblnd_post_rx(&conn->ibc_rxs[i], IBLND_POSTRX_NO_CREDIT);
                if (rc != 0) {
                        CERROR("Can't post rxmsg: %d\n", rc);
 
@@ -1306,6 +1318,7 @@ kiblnd_current_hdev(kib_dev_t *dev)
                if (i++ % 50 == 0)
                        CDEBUG(D_NET, "%s: Wait for failover\n",
                               dev->ibd_ifname);
+               set_current_state(TASK_INTERRUPTIBLE);
                schedule_timeout(cfs_time_seconds(1) / 100);
 
                read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
@@ -1373,100 +1386,248 @@ kiblnd_map_tx_pool(kib_tx_pool_t *tpo)
 }
 
 struct ib_mr *
-kiblnd_find_rd_dma_mr(kib_hca_dev_t *hdev, kib_rdma_desc_t *rd,
+kiblnd_find_rd_dma_mr(struct lnet_ni *ni, kib_rdma_desc_t *rd,
                      int negotiated_nfrags)
 {
-       __u16   nfrags = (negotiated_nfrags != -1) ?
-         negotiated_nfrags : *kiblnd_tunables.kib_map_on_demand;
+       kib_net_t     *net   = ni->ni_data;
+       kib_hca_dev_t *hdev  = net->ibn_dev->ibd_hdev;
+       struct lnet_ioctl_config_o2iblnd_tunables *tunables;
+       int     mod;
+       __u16   nfrags;
+
+       tunables = &ni->ni_lnd_tunables->lt_tun_u.lt_o2ib;
+       mod = tunables->lnd_map_on_demand;
+       nfrags = (negotiated_nfrags != -1) ? negotiated_nfrags : mod;
 
        LASSERT(hdev->ibh_mrs != NULL);
 
-       if (*kiblnd_tunables.kib_map_on_demand > 0 &&
-           nfrags <= rd->rd_nfrags)
+       if (mod > 0 && nfrags <= rd->rd_nfrags)
                return NULL;
 
        return hdev->ibh_mrs;
 }
 
 static void
-kiblnd_destroy_fmr_pool(kib_fmr_pool_t *pool)
+kiblnd_destroy_fmr_pool(kib_fmr_pool_t *fpo)
 {
-        LASSERT (pool->fpo_map_count == 0);
+       LASSERT(fpo->fpo_map_count == 0);
 
-        if (pool->fpo_fmr_pool != NULL)
-                ib_destroy_fmr_pool(pool->fpo_fmr_pool);
+       if (fpo->fpo_is_fmr) {
+               if (fpo->fmr.fpo_fmr_pool)
+                       ib_destroy_fmr_pool(fpo->fmr.fpo_fmr_pool);
+       } else {
+               struct kib_fast_reg_descriptor *frd, *tmp;
+               int i = 0;
+
+               list_for_each_entry_safe(frd, tmp, &fpo->fast_reg.fpo_pool_list,
+                                        frd_list) {
+                       list_del(&frd->frd_list);
+                       ib_free_fast_reg_page_list(frd->frd_frpl);
+                       ib_dereg_mr(frd->frd_mr);
+                       LIBCFS_FREE(frd, sizeof(*frd));
+                       i++;
+               }
+               if (i < fpo->fast_reg.fpo_pool_size)
+                       CERROR("FastReg pool still has %d regions registered\n",
+                               fpo->fast_reg.fpo_pool_size - i);
+       }
 
-        if (pool->fpo_hdev != NULL)
-                kiblnd_hdev_decref(pool->fpo_hdev);
+       if (fpo->fpo_hdev)
+               kiblnd_hdev_decref(fpo->fpo_hdev);
 
-        LIBCFS_FREE(pool, sizeof(kib_fmr_pool_t));
+       LIBCFS_FREE(fpo, sizeof(*fpo));
 }
 
 static void
 kiblnd_destroy_fmr_pool_list(struct list_head *head)
 {
-       kib_fmr_pool_t *pool;
+       kib_fmr_pool_t *fpo, *tmp;
 
-       while (!list_empty(head)) {
-               pool = list_entry(head->next, kib_fmr_pool_t, fpo_list);
-               list_del(&pool->fpo_list);
-               kiblnd_destroy_fmr_pool(pool);
+       list_for_each_entry_safe(fpo, tmp, head, fpo_list) {
+               list_del(&fpo->fpo_list);
+               kiblnd_destroy_fmr_pool(fpo);
        }
 }
 
-static int kiblnd_fmr_pool_size(int ncpts)
+static int
+kiblnd_fmr_pool_size(struct lnet_ioctl_config_o2iblnd_tunables *tunables,
+                    int ncpts)
 {
-       int size = *kiblnd_tunables.kib_fmr_pool_size / ncpts;
+       int size = tunables->lnd_fmr_pool_size / ncpts;
 
        return max(IBLND_FMR_POOL, size);
 }
 
-static int kiblnd_fmr_flush_trigger(int ncpts)
+static int
+kiblnd_fmr_flush_trigger(struct lnet_ioctl_config_o2iblnd_tunables *tunables,
+                        int ncpts)
 {
-       int size = *kiblnd_tunables.kib_fmr_flush_trigger / ncpts;
+       int size = tunables->lnd_fmr_flush_trigger / ncpts;
 
        return max(IBLND_FMR_POOL_FLUSH, size);
 }
 
-static int
-kiblnd_create_fmr_pool(kib_fmr_poolset_t *fps, kib_fmr_pool_t **pp_fpo)
+static int kiblnd_alloc_fmr_pool(kib_fmr_poolset_t *fps, kib_fmr_pool_t *fpo)
 {
-        /* FMR pool for RDMA */
-        kib_dev_t               *dev = fps->fps_net->ibn_dev;
-        kib_fmr_pool_t          *fpo;
-        struct ib_fmr_pool_param param = {
-                .max_pages_per_fmr = LNET_MAX_PAYLOAD/PAGE_SIZE,
-                .page_shift        = PAGE_SHIFT,
-                .access            = (IB_ACCESS_LOCAL_WRITE |
-                                      IB_ACCESS_REMOTE_WRITE),
+       struct ib_fmr_pool_param param = {
+               .max_pages_per_fmr = LNET_MAX_PAYLOAD/PAGE_SIZE,
+               .page_shift        = PAGE_SHIFT,
+               .access            = (IB_ACCESS_LOCAL_WRITE |
+                                     IB_ACCESS_REMOTE_WRITE),
                .pool_size         = fps->fps_pool_size,
                .dirty_watermark   = fps->fps_flush_trigger,
                .flush_function    = NULL,
                .flush_arg         = NULL,
-               .cache             = !!*kiblnd_tunables.kib_fmr_cache};
+               .cache             = !!fps->fps_cache };
+       int rc = 0;
+
+       fpo->fmr.fpo_fmr_pool = ib_create_fmr_pool(fpo->fpo_hdev->ibh_pd,
+                                                  &param);
+       if (IS_ERR(fpo->fmr.fpo_fmr_pool)) {
+               rc = PTR_ERR(fpo->fmr.fpo_fmr_pool);
+               if (rc != -ENOSYS)
+                       CERROR("Failed to create FMR pool: %d\n", rc);
+               else
+                       CERROR("FMRs are not supported\n");
+       }
+
+       return rc;
+}
+
+static int kiblnd_alloc_freg_pool(kib_fmr_poolset_t *fps, kib_fmr_pool_t *fpo)
+{
+       struct kib_fast_reg_descriptor *frd, *tmp;
+       int i, rc;
+
+       INIT_LIST_HEAD(&fpo->fast_reg.fpo_pool_list);
+       fpo->fast_reg.fpo_pool_size = 0;
+       for (i = 0; i < fps->fps_pool_size; i++) {
+               LIBCFS_CPT_ALLOC(frd, lnet_cpt_table(), fps->fps_cpt,
+                                sizeof(*frd));
+               if (!frd) {
+                       CERROR("Failed to allocate a new fast_reg descriptor\n");
+                       rc = -ENOMEM;
+                       goto out;
+               }
+               frd->frd_mr = NULL;
+
+               frd->frd_frpl = ib_alloc_fast_reg_page_list(fpo->fpo_hdev->ibh_ibdev,
+                                                           LNET_MAX_PAYLOAD/PAGE_SIZE);
+               if (IS_ERR(frd->frd_frpl)) {
+                       rc = PTR_ERR(frd->frd_frpl);
+                       CERROR("Failed to allocate ib_fast_reg_page_list: %d\n",
+                               rc);
+                       frd->frd_frpl = NULL;
+                       goto out_middle;
+               }
+
+#ifdef HAVE_IB_ALLOC_FAST_REG_MR
+               frd->frd_mr = ib_alloc_fast_reg_mr(fpo->fpo_hdev->ibh_pd,
+                                                  LNET_MAX_PAYLOAD/PAGE_SIZE);
+#else
+               frd->frd_mr = ib_alloc_mr(fpo->fpo_hdev->ibh_pd,
+                                         IB_MR_TYPE_MEM_REG,
+                                         LNET_MAX_PAYLOAD/PAGE_SIZE);
+#endif
+               if (IS_ERR(frd->frd_mr)) {
+                       rc = PTR_ERR(frd->frd_mr);
+                       CERROR("Failed to allocate ib_fast_reg_mr: %d\n", rc);
+                       frd->frd_mr = NULL;
+                       goto out_middle;
+               }
+
+               frd->frd_valid = true;
+
+               list_add_tail(&frd->frd_list, &fpo->fast_reg.fpo_pool_list);
+               fpo->fast_reg.fpo_pool_size++;
+       }
+
+       return 0;
+
+out_middle:
+       if (frd->frd_mr)
+               ib_dereg_mr(frd->frd_mr);
+       if (frd->frd_frpl)
+               ib_free_fast_reg_page_list(frd->frd_frpl);
+       LIBCFS_FREE(frd, sizeof(*frd));
+
+out:
+       list_for_each_entry_safe(frd, tmp, &fpo->fast_reg.fpo_pool_list,
+                                frd_list) {
+               list_del(&frd->frd_list);
+               ib_free_fast_reg_page_list(frd->frd_frpl);
+               ib_dereg_mr(frd->frd_mr);
+               LIBCFS_FREE(frd, sizeof(*frd));
+       }
+
+       return rc;
+}
+
+static int
+kiblnd_create_fmr_pool(kib_fmr_poolset_t *fps, kib_fmr_pool_t **pp_fpo)
+{
+       struct ib_device_attr *dev_attr;
+       kib_dev_t *dev = fps->fps_net->ibn_dev;
+       kib_fmr_pool_t *fpo;
        int rc;
 
-       LIBCFS_CPT_ALLOC(fpo, lnet_cpt_table(), fps->fps_cpt, sizeof(*fpo));
-       if (fpo == NULL)
+       dev_attr = kmalloc(sizeof(*dev_attr), GFP_KERNEL);
+       if (!dev_attr)
                return -ENOMEM;
 
+       LIBCFS_CPT_ALLOC(fpo, lnet_cpt_table(), fps->fps_cpt, sizeof(*fpo));
+       if (!fpo) {
+               rc = -ENOMEM;
+               goto out_dev_attr;
+       }
+
        fpo->fpo_hdev = kiblnd_current_hdev(dev);
 
-       fpo->fpo_fmr_pool = ib_create_fmr_pool(fpo->fpo_hdev->ibh_pd, &param);
-       if (IS_ERR(fpo->fpo_fmr_pool)) {
-               rc = PTR_ERR(fpo->fpo_fmr_pool);
-               CERROR("Failed to create FMR pool: %d\n", rc);
+       rc = ib_query_device(fpo->fpo_hdev->ibh_ibdev, dev_attr);
+       if (rc) {
+               CERROR("Query device failed for %s: %d\n",
+                       fpo->fpo_hdev->ibh_ibdev->name, rc);
+               goto out_dev_attr;
+       }
 
-                kiblnd_hdev_decref(fpo->fpo_hdev);
-                LIBCFS_FREE(fpo, sizeof(kib_fmr_pool_t));
-                return rc;
-        }
+       /* Check for FMR or FastReg support */
+       fpo->fpo_is_fmr = 0;
+       if (fpo->fpo_hdev->ibh_ibdev->alloc_fmr &&
+           fpo->fpo_hdev->ibh_ibdev->dealloc_fmr &&
+           fpo->fpo_hdev->ibh_ibdev->map_phys_fmr &&
+           fpo->fpo_hdev->ibh_ibdev->unmap_fmr) {
+               LCONSOLE_INFO("Using FMR for registration\n");
+               fpo->fpo_is_fmr = 1;
+       } else if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
+               LCONSOLE_INFO("Using FastReg for registration\n");
+       } else {
+               rc = -ENOSYS;
+               LCONSOLE_ERROR_MSG(rc, "IB device does not support FMRs nor FastRegs, can't register memory\n");
+               goto out_dev_attr;
+       }
 
-        fpo->fpo_deadline = cfs_time_shift(IBLND_POOL_DEADLINE);
-        fpo->fpo_owner    = fps;
-        *pp_fpo = fpo;
+       if (fpo->fpo_is_fmr)
+               rc = kiblnd_alloc_fmr_pool(fps, fpo);
+       else
+               rc = kiblnd_alloc_freg_pool(fps, fpo);
+       if (rc)
+               goto out_fpo;
 
-        return 0;
+       kfree(dev_attr);
+       fpo->fpo_deadline = cfs_time_shift(IBLND_POOL_DEADLINE);
+       fpo->fpo_owner    = fps;
+       *pp_fpo = fpo;
+
+       return 0;
+
+out_fpo:
+       kiblnd_hdev_decref(fpo->fpo_hdev);
+       LIBCFS_FREE(fpo, sizeof(*fpo));
+
+out_dev_attr:
+       kfree(dev_attr);
+
+       return rc;
 }
 
 static void
@@ -1501,8 +1662,9 @@ kiblnd_fini_fmr_poolset(kib_fmr_poolset_t *fps)
 }
 
 static int
-kiblnd_init_fmr_poolset(kib_fmr_poolset_t *fps, int cpt, kib_net_t *net,
-                       int pool_size, int flush_trigger)
+kiblnd_init_fmr_poolset(kib_fmr_poolset_t *fps, int cpt, int ncpts,
+                       kib_net_t *net,
+                       struct lnet_ioctl_config_o2iblnd_tunables *tunables)
 {
        kib_fmr_pool_t *fpo;
        int             rc;
@@ -1511,8 +1673,11 @@ kiblnd_init_fmr_poolset(kib_fmr_poolset_t *fps, int cpt, kib_net_t *net,
 
        fps->fps_net = net;
        fps->fps_cpt = cpt;
-       fps->fps_pool_size = pool_size;
-       fps->fps_flush_trigger = flush_trigger;
+
+       fps->fps_pool_size = kiblnd_fmr_pool_size(tunables, ncpts);
+       fps->fps_flush_trigger = kiblnd_fmr_flush_trigger(tunables, ncpts);
+       fps->fps_cache = tunables->lnd_fmr_cache;
+
        spin_lock_init(&fps->fps_lock);
        INIT_LIST_HEAD(&fps->fps_pool_list);
        INIT_LIST_HEAD(&fps->fps_failed_pool_list);
@@ -1539,21 +1704,38 @@ kiblnd_fmr_pool_unmap(kib_fmr_t *fmr, int status)
 {
        struct list_head   zombies = LIST_HEAD_INIT(zombies);
        kib_fmr_pool_t    *fpo = fmr->fmr_pool;
-       kib_fmr_poolset_t *fps = fpo->fpo_owner;
+       kib_fmr_poolset_t *fps;
        cfs_time_t         now = cfs_time_current();
        kib_fmr_pool_t    *tmp;
        int                rc;
 
-       rc = ib_fmr_pool_unmap(fmr->fmr_pfmr);
-       LASSERT(rc == 0);
+       if (!fpo)
+               return;
 
-       if (status != 0) {
-               rc = ib_flush_fmr_pool(fpo->fpo_fmr_pool);
-               LASSERT(rc == 0);
-       }
+       fps = fpo->fpo_owner;
+       if (fpo->fpo_is_fmr) {
+               if (fmr->fmr_pfmr) {
+                       rc = ib_fmr_pool_unmap(fmr->fmr_pfmr);
+                       LASSERT(!rc);
+                       fmr->fmr_pfmr = NULL;
+               }
 
+               if (status) {
+                       rc = ib_flush_fmr_pool(fpo->fmr.fpo_fmr_pool);
+                       LASSERT(!rc);
+               }
+       } else {
+               struct kib_fast_reg_descriptor *frd = fmr->fmr_frd;
+
+               if (frd) {
+                       frd->frd_valid = false;
+                       spin_lock(&fps->fps_lock);
+                       list_add_tail(&frd->frd_list, &fpo->fast_reg.fpo_pool_list);
+                       spin_unlock(&fps->fps_lock);
+                       fmr->fmr_frd = NULL;
+               }
+       }
        fmr->fmr_pool = NULL;
-       fmr->fmr_pfmr = NULL;
 
        spin_lock(&fps->fps_lock);
        fpo->fpo_map_count--;   /* decref the pool */
@@ -1576,12 +1758,11 @@ kiblnd_fmr_pool_unmap(kib_fmr_t *fmr, int status)
 
 int
 kiblnd_fmr_pool_map(kib_fmr_poolset_t *fps, __u64 *pages, int npages,
-                    __u64 iov, kib_fmr_t *fmr)
+                   __u32 nob, __u64 iov, bool is_rx, kib_fmr_t *fmr)
 {
-        struct ib_pool_fmr *pfmr;
-        kib_fmr_pool_t     *fpo;
-        __u64               version;
-        int                 rc;
+       kib_fmr_pool_t *fpo;
+       __u64 version;
+       int rc;
 
 again:
        spin_lock(&fps->fps_lock);
@@ -1589,21 +1770,88 @@ again:
        list_for_each_entry(fpo, &fps->fps_pool_list, fpo_list) {
                fpo->fpo_deadline = cfs_time_shift(IBLND_POOL_DEADLINE);
                fpo->fpo_map_count++;
-               spin_unlock(&fps->fps_lock);
 
-                pfmr = ib_fmr_pool_map_phys(fpo->fpo_fmr_pool,
-                                            pages, npages, iov);
-                if (likely(!IS_ERR(pfmr))) {
-                        fmr->fmr_pool = fpo;
-                        fmr->fmr_pfmr = pfmr;
-                        return 0;
-                }
+               if (fpo->fpo_is_fmr) {
+                       struct ib_pool_fmr *pfmr;
+
+                       spin_unlock(&fps->fps_lock);
+                       pfmr = ib_fmr_pool_map_phys(fpo->fmr.fpo_fmr_pool,
+                                                   pages, npages, iov);
+                       if (likely(!IS_ERR(pfmr))) {
+                               fmr->fmr_key  = is_rx ? pfmr->fmr->rkey
+                                                     : pfmr->fmr->lkey;
+                               fmr->fmr_frd  = NULL;
+                               fmr->fmr_pfmr = pfmr;
+                               fmr->fmr_pool = fpo;
+                               return 0;
+                       }
+                       rc = PTR_ERR(pfmr);
+               } else {
+                       if (!list_empty(&fpo->fast_reg.fpo_pool_list)) {
+                               struct ib_send_wr *wr;
+                               struct kib_fast_reg_descriptor *frd;
+                               struct ib_fast_reg_page_list *frpl;
+                               struct ib_mr *mr;
+
+                               frd = list_first_entry(&fpo->fast_reg.fpo_pool_list,
+                                                       struct kib_fast_reg_descriptor,
+                                                       frd_list);
+                               list_del(&frd->frd_list);
+                               spin_unlock(&fps->fps_lock);
+
+                               frpl = frd->frd_frpl;
+                               mr   = frd->frd_mr;
+
+                               if (!frd->frd_valid) {
+                                       struct ib_send_wr *inv_wr;
+                                       __u32 key = is_rx ? mr->rkey : mr->lkey;
+
+                                       inv_wr = &frd->frd_inv_wr;
+                                       memset(inv_wr, 0, sizeof(*inv_wr));
+                                       inv_wr->opcode = IB_WR_LOCAL_INV;
+                                       inv_wr->wr_id = IBLND_WID_MR;
+                                       inv_wr->ex.invalidate_rkey = key;
+
+                                       /* Bump the key */
+                                       key = ib_inc_rkey(key);
+                                       ib_update_fast_reg_key(mr, key);
+                               }
+
+                               LASSERT(npages <= frpl->max_page_list_len);
+                               memcpy(frpl->page_list, pages,
+                                       sizeof(*pages) * npages);
+
+                               /* Prepare FastReg WR */
+                               wr = &frd->frd_fastreg_wr;
+                               memset(wr, 0, sizeof(*wr));
+                               wr->opcode = IB_WR_FAST_REG_MR;
+                               wr->wr_id = IBLND_WID_MR;
+                               wr->wr.fast_reg.iova_start = iov;
+                               wr->wr.fast_reg.page_list  = frpl;
+                               wr->wr.fast_reg.page_list_len = npages;
+                               wr->wr.fast_reg.page_shift = PAGE_SHIFT;
+                               wr->wr.fast_reg.length = nob;
+                               wr->wr.fast_reg.rkey = is_rx ? mr->rkey
+                                                            : mr->lkey;
+                               wr->wr.fast_reg.access_flags =
+                                               (IB_ACCESS_LOCAL_WRITE |
+                                                IB_ACCESS_REMOTE_WRITE);
+
+                               fmr->fmr_key  = is_rx ? mr->rkey : mr->lkey;
+                               fmr->fmr_frd  = frd;
+                               fmr->fmr_pfmr = NULL;
+                               fmr->fmr_pool = fpo;
+                               return 0;
+                       }
+                       spin_unlock(&fps->fps_lock);
+                       rc = -EBUSY;
+               }
 
                spin_lock(&fps->fps_lock);
                fpo->fpo_map_count--;
-               if (PTR_ERR(pfmr) != -EAGAIN) {
+               if (rc != -EAGAIN) {
                        spin_unlock(&fps->fps_lock);
-                       return PTR_ERR(pfmr);
+                       return rc;
                }
 
                /* EAGAIN and ... */
@@ -1793,6 +2041,9 @@ kiblnd_pool_alloc_node(kib_poolset_t *ps)
        struct list_head        *node;
        kib_pool_t              *pool;
        int                     rc;
+       unsigned int            interval = 1;
+       cfs_time_t              time_before;
+       unsigned int            trips = 0;
 
 again:
        spin_lock(&ps->ps_lock);
@@ -1817,10 +2068,17 @@ again:
        if (ps->ps_increasing) {
                /* another thread is allocating a new pool */
                spin_unlock(&ps->ps_lock);
+               trips++;
                 CDEBUG(D_NET, "Another thread is allocating new "
-                       "%s pool, waiting for her to complete\n",
-                       ps->ps_name);
-               schedule();
+                      "%s pool, waiting %d HZs for her to complete."
+                      "trips = %d\n",
+                      ps->ps_name, interval, trips);
+
+               set_current_state(TASK_INTERRUPTIBLE);
+               schedule_timeout(interval);
+               if (interval < cfs_time_seconds(1))
+                       interval *= 2;
+
                 goto again;
         }
 
@@ -1834,8 +2092,10 @@ again:
        spin_unlock(&ps->ps_lock);
 
        CDEBUG(D_NET, "%s pool exhausted, allocate new pool\n", ps->ps_name);
-
+       time_before = cfs_time_current();
        rc = ps->ps_pool_create(ps, ps->ps_pool_size, &pool);
+       CDEBUG(D_NET, "ps_pool_create took %lu HZ to complete",
+              cfs_time_current() - time_before);
 
        spin_lock(&ps->ps_lock);
        ps->ps_increasing = 0;
@@ -2033,15 +2293,18 @@ kiblnd_net_fini_pools(kib_net_t *net)
 }
 
 static int
-kiblnd_net_init_pools(kib_net_t *net, __u32 *cpts, int ncpts)
+kiblnd_net_init_pools(kib_net_t *net, lnet_ni_t *ni, __u32 *cpts, int ncpts)
 {
+       struct lnet_ioctl_config_o2iblnd_tunables *tunables;
        unsigned long   flags;
        int             cpt;
        int             rc;
        int             i;
 
+       tunables = &ni->ni_lnd_tunables->lt_tun_u.lt_o2ib;
+
        read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
-       if (*kiblnd_tunables.kib_map_on_demand == 0) {
+       if (tunables->lnd_map_on_demand == 0) {
                read_unlock_irqrestore(&kiblnd_data.kib_global_lock,
                                           flags);
                goto create_tx_pool;
@@ -2049,10 +2312,9 @@ kiblnd_net_init_pools(kib_net_t *net, __u32 *cpts, int ncpts)
 
        read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
 
-       if (*kiblnd_tunables.kib_fmr_pool_size <
-           *kiblnd_tunables.kib_ntx / 4) {
+       if (tunables->lnd_fmr_pool_size < *kiblnd_tunables.kib_ntx / 4) {
                CERROR("Can't set fmr pool size (%d) < ntx / 4(%d)\n",
-                      *kiblnd_tunables.kib_fmr_pool_size,
+                      tunables->lnd_fmr_pool_size,
                       *kiblnd_tunables.kib_ntx / 4);
                rc = -EINVAL;
                goto failed;
@@ -2075,9 +2337,8 @@ kiblnd_net_init_pools(kib_net_t *net, __u32 *cpts, int ncpts)
 
        for (i = 0; i < ncpts; i++) {
                cpt = (cpts == NULL) ? i : cpts[i];
-               rc = kiblnd_init_fmr_poolset(net->ibn_fmr_ps[cpt], cpt, net,
-                                            kiblnd_fmr_pool_size(ncpts),
-                                            kiblnd_fmr_flush_trigger(ncpts));
+               rc = kiblnd_init_fmr_poolset(net->ibn_fmr_ps[cpt], cpt, ncpts,
+                                            net, tunables);
                if (rc != 0) {
                        CERROR("Can't initialize FMR pool for CPT %d: %d\n",
                               cpt, rc);
@@ -2439,7 +2700,6 @@ kiblnd_create_dev(char *ifname)
         if (dev == NULL)
                 return NULL;
 
-        memset(dev, 0, sizeof(*dev));
         netdev = dev_get_by_name(&init_net, ifname);
         if (netdev == NULL) {
                 dev->ibd_can_failover = 0;
@@ -2830,15 +3090,10 @@ kiblnd_startup (lnet_ni_t *ni)
         if (net == NULL)
                 goto failed;
 
-       memset(net, 0, sizeof(*net));
-
        do_gettimeofday(&tv);
        net->ibn_incarnation = (((__u64)tv.tv_sec) * 1000000) + tv.tv_usec;
 
-        ni->ni_peertimeout    = *kiblnd_tunables.kib_peertimeout;
-        ni->ni_maxtxcredits   = *kiblnd_tunables.kib_credits;
-        ni->ni_peertxcredits  = *kiblnd_tunables.kib_peertxcredits;
-        ni->ni_peerrtrcredits = *kiblnd_tunables.kib_peerrtrcredits;
+       kiblnd_tunables_setup(ni);
 
         if (ni->ni_interfaces[0] != NULL) {
                 /* Use the IPoIB interface specified in 'networks=' */
@@ -2877,7 +3132,7 @@ kiblnd_startup (lnet_ni_t *ni)
        if (rc != 0)
                goto failed;
 
-       rc = kiblnd_net_init_pools(net, ni->ni_cpts, ni->ni_ncpts);
+       rc = kiblnd_net_init_pools(net, ni, ni->ni_cpts, ni->ni_ncpts);
         if (rc != 0) {
                 CERROR("Failed to initialize NI pools: %d\n", rc);
                 goto failed;
@@ -2915,7 +3170,6 @@ static lnd_t the_o2iblnd = {
 static void __exit ko2iblnd_exit(void)
 {
        lnet_unregister_lnd(&the_o2iblnd);
-       kiblnd_tunables_fini();
 }
 
 static int __init ko2iblnd_init(void)