common/mlx5: share MR mempool registration
[dpdk.git] / drivers / common / mlx5 / mlx5_common_mr.c
index 1beaead..53a3e85 100644 (file)
 #include <rte_rwlock.h>
 
 #include "mlx5_glue.h"
+#include "mlx5_common.h"
 #include "mlx5_common_mp.h"
 #include "mlx5_common_mr.h"
+#include "mlx5_common_os.h"
 #include "mlx5_common_log.h"
 #include "mlx5_malloc.h"
 
@@ -47,6 +49,20 @@ struct mlx5_mempool_reg {
        unsigned int mrs_n;
 };
 
+void
+mlx5_mprq_buf_free_cb(void *addr __rte_unused, void *opaque)
+{
+       struct mlx5_mprq_buf *buf = opaque;
+
+       if (__atomic_load_n(&buf->refcnt, __ATOMIC_RELAXED) == 1) {
+               rte_mempool_put(buf->mp, buf);
+       } else if (unlikely(__atomic_sub_fetch(&buf->refcnt, 1,
+                                              __ATOMIC_RELAXED) == 0)) {
+               __atomic_store_n(&buf->refcnt, 1, __ATOMIC_RELAXED);
+               rte_mempool_put(buf->mp, buf);
+       }
+}
+
 /**
  * Expand B-tree table to a given size. Can't be called with holding
  * memory_hotplug_lock or share_cache.rwlock due to rte_realloc().
@@ -199,7 +215,7 @@ mr_btree_insert(struct mlx5_mr_btree *bt, struct mr_cache_entry *entry)
  * @return
  *   0 on success, a negative errno value otherwise and rte_errno is set.
  */
-int
+static int
 mlx5_mr_btree_init(struct mlx5_mr_btree *bt, int n, int socket)
 {
        if (bt == NULL) {
@@ -271,6 +287,34 @@ mlx5_mr_btree_dump(struct mlx5_mr_btree *bt __rte_unused)
 #endif
 }
 
+/**
+ * Initialize per-queue MR control descriptor.
+ *
+ * @param mr_ctrl
+ *   Pointer to MR control structure.
+ * @param dev_gen_ptr
+ *   Pointer to generation number of global cache.
+ * @param socket
+ *   NUMA socket on which memory must be allocated.
+ *
+ * @return
+ *   0 on success, a negative errno value otherwise and rte_errno is set.
+ */
+int
+mlx5_mr_ctrl_init(struct mlx5_mr_ctrl *mr_ctrl, uint32_t *dev_gen_ptr,
+                 int socket)
+{
+       if (mr_ctrl == NULL) {
+               rte_errno = EINVAL;
+               return -rte_errno;
+       }
+       /* Save pointer of global generation number to check memory event. */
+       mr_ctrl->dev_gen_ptr = dev_gen_ptr;
+       /* Initialize B-tree and allocate memory for bottom-half cache table. */
+       return mlx5_mr_btree_init(&mr_ctrl->cache_bh, MLX5_MR_BTREE_CACHE_N,
+                                 socket);
+}
+
 /**
  * Find virtually contiguous memory chunk in a given MR.
  *
@@ -427,7 +471,7 @@ mlx5_mr_lookup_list(struct mlx5_mr_share_cache *share_cache,
  * @return
  *   Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
  */
-uint32_t
+static uint32_t
 mlx5_mr_lookup_cache(struct mlx5_mr_share_cache *share_cache,
                     struct mr_cache_entry *entry, uintptr_t addr)
 {
@@ -548,6 +592,8 @@ mr_find_contig_memsegs_cb(const struct rte_memseg_list *msl,
  *
  * @param pd
  *   Pointer to pd of a device (net, regex, vdpa,...).
+ * @param mp_id
+ *   Multi-process identifier, may be NULL for the primary process.
  * @param share_cache
  *   Pointer to a global shared MR cache.
  * @param[out] entry
@@ -570,6 +616,10 @@ mlx5_mr_create_secondary(void *pd __rte_unused,
 {
        int ret;
 
+       if (mp_id == NULL) {
+               rte_errno = EINVAL;
+               return UINT32_MAX;
+       }
        DRV_LOG(DEBUG, "port %u requesting MR creation for address (%p)",
              mp_id->port_id, (void *)addr);
        ret = mlx5_mp_req_mr_create(mp_id, addr);
@@ -839,6 +889,8 @@ err_nolock:
  *
  * @param pd
  *   Pointer to pd handle of a device (net, regex, vdpa,...).
+ * @param mp_id
+ *   Multi-process identifier, may be NULL for the primary process.
  * @param share_cache
  *   Pointer to a global shared MR cache.
  * @param[out] entry
@@ -846,6 +898,8 @@ err_nolock:
  *   created. If failed to create one, this will not be updated.
  * @param addr
  *   Target virtual address to register.
+ * @param mr_ext_memseg_en
+ *   Configurable flag about external memory segment enable or not.
  *
  * @return
  *   Searched LKey on success, UINT32_MAX on failure and rte_errno is set.
@@ -879,6 +933,8 @@ mlx5_mr_create(void *pd, struct mlx5_mp_id *mp_id,
  *
  * @param pd
  *   Pointer to pd of a device (net, regex, vdpa,...).
+ * @param mp_id
+ *   Multi-process identifier, may be NULL for the primary process.
  * @param share_cache
  *   Pointer to a global shared MR cache.
  * @param mr_ctrl
@@ -888,6 +944,8 @@ mlx5_mr_create(void *pd, struct mlx5_mp_id *mp_id,
  *   created. If failed to create one, this is not written.
  * @param addr
  *   Search key.
+ * @param mr_ext_memseg_en
+ *   Configurable flag about external memory segment enable or not.
  *
  * @return
  *   Searched LKey on success, UINT32_MAX on no match.
@@ -943,20 +1001,25 @@ mr_lookup_caches(void *pd, struct mlx5_mp_id *mp_id,
  *
  * @param pd
  *   Pointer to pd of a device (net, regex, vdpa,...).
+ * @param mp_id
+ *   Multi-process identifier, may be NULL for the primary process.
  * @param share_cache
  *   Pointer to a global shared MR cache.
  * @param mr_ctrl
  *   Pointer to per-queue MR control structure.
  * @param addr
  *   Search key.
+ * @param mr_ext_memseg_en
+ *   Configurable flag about external memory segment enable or not.
  *
  * @return
  *   Searched LKey on success, UINT32_MAX on no match.
  */
-uint32_t mlx5_mr_addr2mr_bh(void *pd, struct mlx5_mp_id *mp_id,
-                           struct mlx5_mr_share_cache *share_cache,
-                           struct mlx5_mr_ctrl *mr_ctrl,
-                           uintptr_t addr, unsigned int mr_ext_memseg_en)
+static uint32_t
+mlx5_mr_addr2mr_bh(void *pd, struct mlx5_mp_id *mp_id,
+                  struct mlx5_mr_share_cache *share_cache,
+                  struct mlx5_mr_ctrl *mr_ctrl, uintptr_t addr,
+                  unsigned int mr_ext_memseg_en)
 {
        uint32_t lkey;
        uint16_t bh_idx = 0;
@@ -987,7 +1050,7 @@ uint32_t mlx5_mr_addr2mr_bh(void *pd, struct mlx5_mp_id *mp_id,
 }
 
 /**
- * Release all the created MRs and resources on global MR cache of a device.
+ * Release all the created MRs and resources on global MR cache of a device
  * list.
  *
  * @param share_cache
@@ -1016,6 +1079,31 @@ mlx5_mr_release_cache(struct mlx5_mr_share_cache *share_cache)
        mlx5_mr_garbage_collect(share_cache);
 }
 
+/**
+ * Initialize global MR cache of a device.
+ *
+ * @param share_cache
+ *   Pointer to a global shared MR cache.
+ * @param socket
+ *   NUMA socket on which memory must be allocated.
+ *
+ * @return
+ *   0 on success, a negative errno value otherwise and rte_errno is set.
+ */
+int
+mlx5_mr_create_cache(struct mlx5_mr_share_cache *share_cache, int socket)
+{
+       /* Set the reg_mr and dereg_mr callback functions */
+       mlx5_os_set_reg_mr_cb(&share_cache->reg_mr_cb,
+                             &share_cache->dereg_mr_cb);
+       rte_rwlock_init(&share_cache->rwlock);
+       rte_rwlock_init(&share_cache->mprwlock);
+       share_cache->mp_cb_registered = 0;
+       /* Initialize B-tree and allocate memory for global MR cache table. */
+       return mlx5_mr_btree_init(&share_cache->cache,
+                                 MLX5_MR_BTREE_CACHE_N * 2, socket);
+}
+
 /**
  * Flush all of the local cache entries.
  *
@@ -1180,8 +1268,8 @@ mlx5_free_mr_by_addr(struct mlx5_mr_share_cache *share_cache,
 /**
  * Dump all the created MRs and the global cache entries.
  *
- * @param sh
- *   Pointer to Ethernet device shared context.
+ * @param share_cache
+ *   Pointer to a global shared MR cache.
  */
 void
 mlx5_mr_dump_cache(struct mlx5_mr_share_cache *share_cache __rte_unused)
@@ -1516,8 +1604,7 @@ mlx5_mr_mempool_register_primary(struct mlx5_mr_share_cache *share_cache,
        mpr = mlx5_mempool_reg_lookup(share_cache, mp);
        if (mpr == NULL) {
                mlx5_mempool_reg_attach(new_mpr);
-               LIST_INSERT_HEAD(&share_cache->mempool_reg_list,
-                                new_mpr, next);
+               LIST_INSERT_HEAD(&share_cache->mempool_reg_list, new_mpr, next);
                ret = 0;
        }
        rte_rwlock_write_unlock(&share_cache->rwlock);
@@ -1771,3 +1858,86 @@ mlx5_mr_mempool2mr_bh(struct mlx5_mr_share_cache *share_cache,
        mr_ctrl->head = (mr_ctrl->head + 1) % MLX5_MR_CACHE_N;
        return lkey;
 }
+
+/**
+ * Bottom-half of LKey search on. If supported, lookup for the address from
+ * the mempool. Otherwise, search in old mechanism caches.
+ *
+ * @param cdev
+ *   Pointer to mlx5 device.
+ * @param mp_id
+ *   Multi-process identifier, may be NULL for the primary process.
+ * @param mr_ctrl
+ *   Pointer to per-queue MR control structure.
+ * @param mb
+ *   Pointer to mbuf.
+ *
+ * @return
+ *   Searched LKey on success, UINT32_MAX on no match.
+ */
+static uint32_t
+mlx5_mr_mb2mr_bh(struct mlx5_common_device *cdev, struct mlx5_mp_id *mp_id,
+                struct mlx5_mr_ctrl *mr_ctrl, struct rte_mbuf *mb)
+{
+       uint32_t lkey;
+       uintptr_t addr = (uintptr_t)mb->buf_addr;
+
+       if (cdev->config.mr_mempool_reg_en) {
+               struct rte_mempool *mp = NULL;
+               struct mlx5_mprq_buf *buf;
+
+               if (!RTE_MBUF_HAS_EXTBUF(mb)) {
+                       mp = mlx5_mb2mp(mb);
+               } else if (mb->shinfo->free_cb == mlx5_mprq_buf_free_cb) {
+                       /* Recover MPRQ mempool. */
+                       buf = mb->shinfo->fcb_opaque;
+                       mp = buf->mp;
+               }
+               if (mp != NULL) {
+                       lkey = mlx5_mr_mempool2mr_bh(&cdev->mr_scache,
+                                                    mr_ctrl, mp, addr);
+                       /*
+                        * Lookup can only fail on invalid input, e.g. "addr"
+                        * is not from "mp" or "mp" has MEMPOOL_F_NON_IO set.
+                        */
+                       if (lkey != UINT32_MAX)
+                               return lkey;
+               }
+               /* Fallback for generic mechanism in corner cases. */
+       }
+       return mlx5_mr_addr2mr_bh(cdev->pd, mp_id, &cdev->mr_scache, mr_ctrl,
+                                 addr, cdev->config.mr_ext_memseg_en);
+}
+
+/**
+ * Query LKey from a packet buffer.
+ *
+ * @param cdev
+ *   Pointer to the mlx5 device structure.
+ * @param mp_id
+ *   Multi-process identifier, may be NULL for the primary process.
+ * @param mr_ctrl
+ *   Pointer to per-queue MR control structure.
+ * @param mbuf
+ *   Pointer to mbuf.
+ *
+ * @return
+ *   Searched LKey on success, UINT32_MAX on no match.
+ */
+uint32_t
+mlx5_mr_mb2mr(struct mlx5_common_device *cdev, struct mlx5_mp_id *mp_id,
+             struct mlx5_mr_ctrl *mr_ctrl, struct rte_mbuf *mbuf)
+{
+       uint32_t lkey;
+
+       /* Check generation bit to see if there's any change on existing MRs. */
+       if (unlikely(*mr_ctrl->dev_gen_ptr != mr_ctrl->cur_gen))
+               mlx5_mr_flush_local_cache(mr_ctrl);
+       /* Linear search on MR cache array. */
+       lkey = mlx5_mr_lookup_lkey(mr_ctrl->cache, &mr_ctrl->mru,
+                                  MLX5_MR_CACHE_N, (uintptr_t)mbuf->buf_addr);
+       if (likely(lkey != UINT32_MAX))
+               return lkey;
+       /* Take slower bottom-half on miss. */
+       return mlx5_mr_mb2mr_bh(cdev, mp_id, mr_ctrl, mbuf);
+}