net/mlx5: reorganize flow API structure
[dpdk.git] / drivers / net / mlx5 / mlx5.c
index 293d316..cc13e44 100644 (file)
@@ -38,6 +38,7 @@
 #include <mlx5_glue.h>
 #include <mlx5_devx_cmds.h>
 #include <mlx5_common.h>
+#include <mlx5_common_mp.h>
 
 #include "mlx5_defs.h"
 #include "mlx5.h"
@@ -197,6 +198,120 @@ struct mlx5_dev_spawn_data {
 static LIST_HEAD(, mlx5_ibv_shared) mlx5_ibv_list = LIST_HEAD_INITIALIZER();
 static pthread_mutex_t mlx5_ibv_list_mutex = PTHREAD_MUTEX_INITIALIZER;
 
+static struct mlx5_indexed_pool_config mlx5_ipool_cfg[] = {
+#ifdef HAVE_IBV_FLOW_DV_SUPPORT
+       {
+               .size = sizeof(struct mlx5_flow_dv_encap_decap_resource),
+               .trunk_size = 64,
+               .grow_trunk = 3,
+               .grow_shift = 2,
+               .need_lock = 0,
+               .release_mem_en = 1,
+               .malloc = rte_malloc_socket,
+               .free = rte_free,
+               .type = "mlx5_encap_decap_ipool",
+       },
+       {
+               .size = sizeof(struct mlx5_flow_dv_push_vlan_action_resource),
+               .trunk_size = 64,
+               .grow_trunk = 3,
+               .grow_shift = 2,
+               .need_lock = 0,
+               .release_mem_en = 1,
+               .malloc = rte_malloc_socket,
+               .free = rte_free,
+               .type = "mlx5_push_vlan_ipool",
+       },
+       {
+               .size = sizeof(struct mlx5_flow_dv_tag_resource),
+               .trunk_size = 64,
+               .grow_trunk = 3,
+               .grow_shift = 2,
+               .need_lock = 0,
+               .release_mem_en = 1,
+               .malloc = rte_malloc_socket,
+               .free = rte_free,
+               .type = "mlx5_tag_ipool",
+       },
+       {
+               .size = sizeof(struct mlx5_flow_dv_port_id_action_resource),
+               .trunk_size = 64,
+               .grow_trunk = 3,
+               .grow_shift = 2,
+               .need_lock = 0,
+               .release_mem_en = 1,
+               .malloc = rte_malloc_socket,
+               .free = rte_free,
+               .type = "mlx5_port_id_ipool",
+       },
+       {
+               .size = sizeof(struct mlx5_flow_tbl_data_entry),
+               .trunk_size = 64,
+               .grow_trunk = 3,
+               .grow_shift = 2,
+               .need_lock = 0,
+               .release_mem_en = 1,
+               .malloc = rte_malloc_socket,
+               .free = rte_free,
+               .type = "mlx5_jump_ipool",
+       },
+#endif
+       {
+               .size = sizeof(struct mlx5_flow_meter),
+               .trunk_size = 64,
+               .grow_trunk = 3,
+               .grow_shift = 2,
+               .need_lock = 0,
+               .release_mem_en = 1,
+               .malloc = rte_malloc_socket,
+               .free = rte_free,
+               .type = "mlx5_meter_ipool",
+       },
+       {
+               .size = sizeof(struct mlx5_flow_mreg_copy_resource),
+               .trunk_size = 64,
+               .grow_trunk = 3,
+               .grow_shift = 2,
+               .need_lock = 0,
+               .release_mem_en = 1,
+               .malloc = rte_malloc_socket,
+               .free = rte_free,
+               .type = "mlx5_mcp_ipool",
+       },
+       {
+               .size = (sizeof(struct mlx5_hrxq) + MLX5_RSS_HASH_KEY_LEN),
+               .trunk_size = 64,
+               .grow_trunk = 3,
+               .grow_shift = 2,
+               .need_lock = 0,
+               .release_mem_en = 1,
+               .malloc = rte_malloc_socket,
+               .free = rte_free,
+               .type = "mlx5_hrxq_ipool",
+       },
+       {
+               .size = sizeof(struct mlx5_flow_handle),
+               .trunk_size = 64,
+               .grow_trunk = 3,
+               .grow_shift = 2,
+               .need_lock = 0,
+               .release_mem_en = 1,
+               .malloc = rte_malloc_socket,
+               .free = rte_free,
+               .type = "mlx5_flow_handle_ipool",
+       },
+       {
+               .size = sizeof(struct rte_flow),
+               .trunk_size = 4096,
+               .need_lock = 1,
+               .release_mem_en = 1,
+               .malloc = rte_malloc_socket,
+               .free = rte_free,
+               .type = "rte_flow_ipool",
+       },
+};
+
+
 #define MLX5_FLOW_MIN_ID_POOL_SIZE 512
 #define MLX5_ID_GENERATION_ARRAY_FACTOR 16
 
@@ -415,6 +530,49 @@ mlx5_flow_counters_mng_close(struct mlx5_ibv_shared *sh)
        memset(&sh->cmng, 0, sizeof(sh->cmng));
 }
 
+/**
+ * Initialize the flow resources' indexed mempool.
+ *
+ * @param[in] sh
+ *   Pointer to mlx5_ibv_shared object.
+ * @param[in] sh
+ *   Pointer to user dev config.
+ */
+static void
+mlx5_flow_ipool_create(struct mlx5_ibv_shared *sh,
+                      const struct mlx5_dev_config *config __rte_unused)
+{
+       uint8_t i;
+
+#ifdef HAVE_IBV_FLOW_DV_SUPPORT
+       /*
+        * While DV is supported, user chooses the verbs mode,
+        * the mlx5 flow handle size is different with the
+        * MLX5_FLOW_HANDLE_VERBS_SIZE.
+        */
+       if (!config->dv_flow_en)
+               mlx5_ipool_cfg[MLX5_IPOOL_MLX5_FLOW].size =
+                                       MLX5_FLOW_HANDLE_VERBS_SIZE;
+#endif
+       for (i = 0; i < MLX5_IPOOL_MAX; ++i)
+               sh->ipool[i] = mlx5_ipool_create(&mlx5_ipool_cfg[i]);
+}
+
+/**
+ * Release the flow resources' indexed mempool.
+ *
+ * @param[in] sh
+ *   Pointer to mlx5_ibv_shared object.
+ */
+static void
+mlx5_flow_ipool_destroy(struct mlx5_ibv_shared *sh)
+{
+       uint8_t i;
+
+       for (i = 0; i < MLX5_IPOOL_MAX; ++i)
+               mlx5_ipool_destroy(sh->ipool[i]);
+}
+
 /**
  * Extract pdn of PD object using DV API.
  *
@@ -606,7 +764,8 @@ mlx5_alloc_shared_ibctx(const struct mlx5_dev_spawn_data *spawn,
                        goto error;
                }
        }
-       sh->flow_id_pool = mlx5_flow_id_pool_alloc(UINT32_MAX);
+       sh->flow_id_pool = mlx5_flow_id_pool_alloc
+                                       ((1 << HAIRPIN_FLOW_ID_BITS) - 1);
        if (!sh->flow_id_pool) {
                DRV_LOG(ERR, "can't create flow id pool");
                err = ENOMEM;
@@ -622,7 +781,7 @@ mlx5_alloc_shared_ibctx(const struct mlx5_dev_spawn_data *spawn,
         * At this point the device is not added to the memory
         * event list yet, context is just being created.
         */
-       err = mlx5_mr_btree_init(&sh->mr.cache,
+       err = mlx5_mr_btree_init(&sh->share_cache.cache,
                                 MLX5_MR_BTREE_CACHE_N * 2,
                                 spawn->pci_dev->device.numa_node);
        if (err) {
@@ -630,6 +789,7 @@ mlx5_alloc_shared_ibctx(const struct mlx5_dev_spawn_data *spawn,
                goto error;
        }
        mlx5_flow_counters_mng_init(sh);
+       mlx5_flow_ipool_create(sh, config);
        /* Add device to memory callback list. */
        rte_rwlock_write_lock(&mlx5_shared_data->mem_event_rwlock);
        LIST_INSERT_HEAD(&mlx5_shared_data->mem_event_cb_list,
@@ -694,7 +854,7 @@ mlx5_free_shared_ibctx(struct mlx5_ibv_shared *sh)
        LIST_REMOVE(sh, mem_event_cb);
        rte_rwlock_write_unlock(&mlx5_shared_data->mem_event_rwlock);
        /* Release created Memory Regions. */
-       mlx5_mr_release(sh);
+       mlx5_mr_release_cache(&sh->share_cache);
        /* Remove context from the global device list. */
        LIST_REMOVE(sh, next);
        /*
@@ -702,6 +862,7 @@ mlx5_free_shared_ibctx(struct mlx5_ibv_shared *sh)
         *  Only primary process handles async device events.
         **/
        mlx5_flow_counters_mng_close(sh);
+       mlx5_flow_ipool_destroy(sh);
        MLX5_ASSERT(!sh->intr_cnt);
        if (sh->intr_cnt)
                mlx5_intr_callback_unregister
@@ -1300,16 +1461,6 @@ mlx5_dev_close(struct rte_eth_dev *dev)
                close(priv->nl_socket_rdma);
        if (priv->vmwa_context)
                mlx5_vlan_vmwa_exit(priv->vmwa_context);
-       if (priv->sh) {
-               /*
-                * Free the shared context in last turn, because the cleanup
-                * routines above may use some shared fields, like
-                * mlx5_nl_mac_addr_flush() uses ibdev_path for retrieveing
-                * ifindex if Netlink fails.
-                */
-               mlx5_free_shared_ibctx(priv->sh);
-               priv->sh = NULL;
-       }
        ret = mlx5_hrxq_verify(dev);
        if (ret)
                DRV_LOG(WARNING, "port %u some hash Rx queue still remain",
@@ -1338,6 +1489,16 @@ mlx5_dev_close(struct rte_eth_dev *dev)
        if (ret)
                DRV_LOG(WARNING, "port %u some flows still remain",
                        dev->data->port_id);
+       if (priv->sh) {
+               /*
+                * Free the shared context in last turn, because the cleanup
+                * routines above may use some shared fields, like
+                * mlx5_nl_mac_addr_flush() uses ibdev_path for retrieveing
+                * ifindex if Netlink fails.
+                */
+               mlx5_free_shared_ibctx(priv->sh);
+               priv->sh = NULL;
+       }
        if (priv->domain_id != RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
                unsigned int c = 0;
                uint16_t port_id;
@@ -1722,7 +1883,8 @@ mlx5_init_once(void)
                rte_rwlock_init(&sd->mem_event_rwlock);
                rte_mem_event_callback_register("MLX5_MEM_EVENT_CB",
                                                mlx5_mr_mem_event_cb, NULL);
-               ret = mlx5_mp_init_primary();
+               ret = mlx5_mp_init_primary(MLX5_MP_NAME,
+                                          mlx5_mp_primary_handle);
                if (ret)
                        goto out;
                sd->init_done = true;
@@ -1730,7 +1892,8 @@ mlx5_init_once(void)
        case RTE_PROC_SECONDARY:
                if (ld->init_done)
                        break;
-               ret = mlx5_mp_init_secondary();
+               ret = mlx5_mp_init_secondary(MLX5_MP_NAME,
+                                            mlx5_mp_secondary_handle);
                if (ret)
                        goto out;
                ++sd->secondary_cnt;
@@ -2205,6 +2368,8 @@ mlx5_dev_spawn(struct rte_device *dpdk_dev,
        }
        DRV_LOG(DEBUG, "naming Ethernet device \"%s\"", name);
        if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
+               struct mlx5_mp_id mp_id;
+
                eth_dev = rte_eth_dev_attach_secondary(name);
                if (eth_dev == NULL) {
                        DRV_LOG(ERR, "can not attach rte ethdev");
@@ -2216,8 +2381,10 @@ mlx5_dev_spawn(struct rte_device *dpdk_dev,
                err = mlx5_proc_priv_init(eth_dev);
                if (err)
                        return NULL;
+               mp_id.port_id = eth_dev->data->port_id;
+               strlcpy(mp_id.name, MLX5_MP_NAME, RTE_MP_MAX_NAME_LEN);
                /* Receive command fd from primary process */
-               err = mlx5_mp_req_verbs_cmd_fd(eth_dev);
+               err = mlx5_mp_req_verbs_cmd_fd(&mp_id);
                if (err < 0)
                        return NULL;
                /* Remap UAR for Tx queues. */
@@ -2379,6 +2546,8 @@ mlx5_dev_spawn(struct rte_device *dpdk_dev,
        priv->ibv_port = spawn->ibv_port;
        priv->pci_dev = spawn->pci_dev;
        priv->mtu = RTE_ETHER_MTU;
+       priv->mp_id.port_id = port_id;
+       strlcpy(priv->mp_id.name, MLX5_MP_NAME, RTE_MP_MAX_NAME_LEN);
 #ifndef RTE_ARCH_64
        /* Initialize UAR access locks for 32bit implementations. */
        rte_spinlock_init(&priv->uar_lock_cq);
@@ -2726,8 +2895,8 @@ mlx5_dev_spawn(struct rte_device *dpdk_dev,
                                      mlx5_ifindex(eth_dev),
                                      eth_dev->data->mac_addrs,
                                      MLX5_MAX_MAC_ADDRESSES);
-       TAILQ_INIT(&priv->flows);
-       TAILQ_INIT(&priv->ctrl_flows);
+       priv->flows = 0;
+       priv->ctrl_flows = 0;
        TAILQ_INIT(&priv->flow_meters);
        TAILQ_INIT(&priv->flow_meter_profiles);
        /* Hint libmlx5 to use PMD allocator for data plane resources */