net/mlx5: add option to allocate memory from system
[dpdk.git] / drivers / net / mlx5 / mlx5.c
index 0786945..b2a8460 100644 (file)
 /* Flow memory reclaim mode. */
 #define MLX5_RECLAIM_MEM "reclaim_mem_mode"
 
+/* The default memory allocator used in PMD. */
+#define MLX5_SYS_MEM_EN "sys_mem_en"
+
 static const char *MZ_MLX5_PMD_SHARED_DATA = "mlx5_pmd_shared_data";
 
 /* Shared memory between primary and secondary processes. */
@@ -608,6 +611,111 @@ mlx5_flow_ipool_destroy(struct mlx5_dev_ctx_shared *sh)
                mlx5_ipool_destroy(sh->ipool[i]);
 }
 
+/*
+ * Check if dynamic flex parser for eCPRI already exists.
+ *
+ * @param dev
+ *   Pointer to Ethernet device structure.
+ *
+ * @return
+ *   true on exists, false on not.
+ */
+bool
+mlx5_flex_parser_ecpri_exist(struct rte_eth_dev *dev)
+{
+       struct mlx5_priv *priv = dev->data->dev_private;
+       struct mlx5_flex_parser_profiles *prf =
+                               &priv->sh->fp[MLX5_FLEX_PARSER_ECPRI_0];
+
+       return !!prf->obj;
+}
+
+/*
+ * Allocation of a flex parser for eCPRI. Once created, this parser related
+ * resources will be held until the device is closed.
+ *
+ * @param dev
+ *   Pointer to Ethernet device structure.
+ *
+ * @return
+ *   0 on success, a negative errno value otherwise and rte_errno is set.
+ */
+int
+mlx5_flex_parser_ecpri_alloc(struct rte_eth_dev *dev)
+{
+       struct mlx5_priv *priv = dev->data->dev_private;
+       struct mlx5_flex_parser_profiles *prf =
+                               &priv->sh->fp[MLX5_FLEX_PARSER_ECPRI_0];
+       struct mlx5_devx_graph_node_attr node = {
+               .modify_field_select = 0,
+       };
+       uint32_t ids[8];
+       int ret;
+
+       if (!priv->config.hca_attr.parse_graph_flex_node) {
+               DRV_LOG(ERR, "Dynamic flex parser is not supported "
+                       "for device %s.", priv->dev_data->name);
+               return -ENOTSUP;
+       }
+       node.header_length_mode = MLX5_GRAPH_NODE_LEN_FIXED;
+       /* 8 bytes now: 4B common header + 4B message body header. */
+       node.header_length_base_value = 0x8;
+       /* After MAC layer: Ether / VLAN. */
+       node.in[0].arc_parse_graph_node = MLX5_GRAPH_ARC_NODE_MAC;
+       /* Type of compared condition should be 0xAEFE in the L2 layer. */
+       node.in[0].compare_condition_value = RTE_ETHER_TYPE_ECPRI;
+       /* Sample #0: type in common header. */
+       node.sample[0].flow_match_sample_en = 1;
+       /* Fixed offset. */
+       node.sample[0].flow_match_sample_offset_mode = 0x0;
+       /* Only the 2nd byte will be used. */
+       node.sample[0].flow_match_sample_field_base_offset = 0x0;
+       /* Sample #1: message payload. */
+       node.sample[1].flow_match_sample_en = 1;
+       /* Fixed offset. */
+       node.sample[1].flow_match_sample_offset_mode = 0x0;
+       /*
+        * Only the first two bytes will be used right now, and its offset will
+        * start after the common header that with the length of a DW(u32).
+        */
+       node.sample[1].flow_match_sample_field_base_offset = sizeof(uint32_t);
+       prf->obj = mlx5_devx_cmd_create_flex_parser(priv->sh->ctx, &node);
+       if (!prf->obj) {
+               DRV_LOG(ERR, "Failed to create flex parser node object.");
+               return (rte_errno == 0) ? -ENODEV : -rte_errno;
+       }
+       prf->num = 2;
+       ret = mlx5_devx_cmd_query_parse_samples(prf->obj, ids, prf->num);
+       if (ret) {
+               DRV_LOG(ERR, "Failed to query sample IDs.");
+               return (rte_errno == 0) ? -ENODEV : -rte_errno;
+       }
+       prf->offset[0] = 0x0;
+       prf->offset[1] = sizeof(uint32_t);
+       prf->ids[0] = ids[0];
+       prf->ids[1] = ids[1];
+       return 0;
+}
+
+/*
+ * Destroy the flex parser node, including the parser itself, input / output
+ * arcs and DW samples. Resources could be reused then.
+ *
+ * @param dev
+ *   Pointer to Ethernet device structure.
+ */
+static void
+mlx5_flex_parser_ecpri_release(struct rte_eth_dev *dev)
+{
+       struct mlx5_priv *priv = dev->data->dev_private;
+       struct mlx5_flex_parser_profiles *prf =
+                               &priv->sh->fp[MLX5_FLEX_PARSER_ECPRI_0];
+
+       if (prf->obj)
+               mlx5_devx_cmd_destroy(prf->obj);
+       prf->obj = NULL;
+}
+
 /**
  * Allocate shared device context. If there is multiport device the
  * master and representors will share this context, if there is single
@@ -709,6 +817,12 @@ mlx5_alloc_shared_dev_ctx(const struct mlx5_dev_spawn_data *spawn,
                        err = ENOMEM;
                        goto error;
                }
+               sh->tx_uar = mlx5_glue->devx_alloc_uar(sh->ctx, 0);
+               if (!sh->tx_uar) {
+                       DRV_LOG(ERR, "Failed to allocate DevX UAR.");
+                       err = ENOMEM;
+                       goto error;
+               }
        }
        sh->flow_id_pool = mlx5_flow_id_pool_alloc
                                        ((1 << HAIRPIN_FLOW_ID_BITS) - 1);
@@ -761,12 +875,17 @@ exit:
        pthread_mutex_unlock(&mlx5_dev_ctx_list_mutex);
        return sh;
 error:
+       pthread_mutex_destroy(&sh->txpp.mutex);
        pthread_mutex_unlock(&mlx5_dev_ctx_list_mutex);
        MLX5_ASSERT(sh);
        if (sh->cnt_id_tbl) {
                mlx5_l3t_destroy(sh->cnt_id_tbl);
                sh->cnt_id_tbl = NULL;
        }
+       if (sh->tx_uar) {
+               mlx5_glue->devx_free_uar(sh->tx_uar);
+               sh->tx_uar = NULL;
+       }
        if (sh->tis)
                claim_zero(mlx5_devx_cmd_destroy(sh->tis));
        if (sh->td)
@@ -832,6 +951,10 @@ mlx5_free_shared_dev_ctx(struct mlx5_dev_ctx_shared *sh)
                mlx5_l3t_destroy(sh->cnt_id_tbl);
                sh->cnt_id_tbl = NULL;
        }
+       if (sh->tx_uar) {
+               mlx5_glue->devx_free_uar(sh->tx_uar);
+               sh->tx_uar = NULL;
+       }
        if (sh->pd)
                claim_zero(mlx5_glue->dealloc_pd(sh->pd));
        if (sh->tis)
@@ -842,6 +965,7 @@ mlx5_free_shared_dev_ctx(struct mlx5_dev_ctx_shared *sh)
                claim_zero(mlx5_glue->close_device(sh->ctx));
        if (sh->flow_id_pool)
                mlx5_flow_id_pool_release(sh->flow_id_pool);
+       pthread_mutex_destroy(&sh->txpp.mutex);
        rte_free(sh);
 exit:
        pthread_mutex_unlock(&mlx5_dev_ctx_list_mutex);
@@ -1175,6 +1299,8 @@ mlx5_dev_close(struct rte_eth_dev *dev)
        rte_wmb();
        /* Disable datapath on secondary process. */
        mlx5_mp_req_stop_rxtx(dev);
+       /* Free the eCPRI flex parser resource. */
+       mlx5_flex_parser_ecpri_release(dev);
        if (priv->rxqs != NULL) {
                /* XXX race condition if mlx5_rx_burst() is still running. */
                usleep(1000);
@@ -1410,6 +1536,8 @@ mlx5_args_check(const char *key, const char *val, void *opaque)
                        return -rte_errno;
                }
                config->reclaim_mode = tmp;
+       } else if (strcmp(MLX5_SYS_MEM_EN, key) == 0) {
+               config->sys_mem_en = !!tmp;
        } else {
                DRV_LOG(WARNING, "%s: unknown parameter", key);
                rte_errno = EINVAL;
@@ -1468,6 +1596,7 @@ mlx5_args(struct mlx5_dev_config *config, struct rte_devargs *devargs)
                MLX5_CLASS_ARG_NAME,
                MLX5_HP_BUF_SIZE,
                MLX5_RECLAIM_MEM,
+               MLX5_SYS_MEM_EN,
                NULL,
        };
        struct rte_kvargs *kvlist;