From: Roi Dayan <roid@nvidia.com>
Date: Tue, 22 Feb 2022 16:30:35 +0200
Subject: net/mlx5e: TC, Allocate post meter ft per rule
Patch-mainline: v6.0-rc1
Git-commit: e5b1db27410e5d625a49b57db133f0551e01dd56
References: jsc#PED-1549

To support a TC police action notexceed counter and supporting
actions other than drop/pipe there is a need to create separate ft
and rules per rule and not to use a common one created on eswitch init.

Signed-off-by: Roi Dayan <roid@nvidia.com>
Reviewed-by: Jianbo Liu <jianbol@nvidia.com>
Reviewed-by: Oz Shlomo <ozsh@nvidia.com>
Signed-off-by: Saeed Mahameed <saeedm@nvidia.com>
Acked-by: Thomas Bogendoerfer <tbogendoerfer@suse.de>
---
 drivers/net/ethernet/mellanox/mlx5/core/en/tc/meter.c |   19 ++-----------
 drivers/net/ethernet/mellanox/mlx5/core/en/tc/meter.h |    6 ++--
 drivers/net/ethernet/mellanox/mlx5/core/en_tc.c       |   26 ++++++++++++++++--
 3 files changed, 31 insertions(+), 20 deletions(-)

--- a/drivers/net/ethernet/mellanox/mlx5/core/en/tc/meter.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en/tc/meter.c
@@ -6,7 +6,6 @@
 #include "en/tc/post_act.h"
 #include "meter.h"
 #include "en/tc_priv.h"
-#include "post_meter.h"
 
 #define MLX5_START_COLOR_SHIFT 28
 #define MLX5_METER_MODE_SHIFT 24
@@ -47,8 +46,6 @@ struct mlx5e_flow_meters {
 
 	struct mlx5_core_dev *mdev;
 	struct mlx5e_post_act *post_act;
-
-	struct mlx5e_post_meter_priv *post_meter;
 };
 
 static void
@@ -390,10 +387,10 @@ mlx5e_tc_meter_put(struct mlx5e_flow_met
 	mutex_unlock(&flow_meters->sync_lock);
 }
 
-struct mlx5_flow_table *
-mlx5e_tc_meter_get_post_meter_ft(struct mlx5e_flow_meters *flow_meters)
+enum mlx5_flow_namespace_type
+mlx5e_tc_meter_get_namespace(struct mlx5e_flow_meters *flow_meters)
 {
-	return mlx5e_post_meter_get_ft(flow_meters->post_meter);
+	return flow_meters->ns_type;
 }
 
 struct mlx5e_flow_meters *
@@ -432,12 +429,6 @@ mlx5e_flow_meters_init(struct mlx5e_priv
 		goto err_sq;
 	}
 
-	flow_meters->post_meter = mlx5e_post_meter_init(priv, ns_type, post_act);
-	if (IS_ERR(flow_meters->post_meter)) {
-		err = PTR_ERR(flow_meters->post_meter);
-		goto err_post_meter;
-	}
-
 	mutex_init(&flow_meters->sync_lock);
 	INIT_LIST_HEAD(&flow_meters->partial_list);
 	INIT_LIST_HEAD(&flow_meters->full_list);
@@ -451,8 +442,6 @@ mlx5e_flow_meters_init(struct mlx5e_priv
 
 	return flow_meters;
 
-err_post_meter:
-	mlx5_aso_destroy(flow_meters->aso);
 err_sq:
 	mlx5_core_dealloc_pd(mdev, flow_meters->pdn);
 err_out:
@@ -466,9 +455,7 @@ mlx5e_flow_meters_cleanup(struct mlx5e_f
 	if (IS_ERR_OR_NULL(flow_meters))
 		return;
 
-	mlx5e_post_meter_cleanup(flow_meters->post_meter);
 	mlx5_aso_destroy(flow_meters->aso);
 	mlx5_core_dealloc_pd(flow_meters->mdev, flow_meters->pdn);
-
 	kfree(flow_meters);
 }
--- a/drivers/net/ethernet/mellanox/mlx5/core/en/tc/meter.h
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en/tc/meter.h
@@ -4,6 +4,7 @@
 #ifndef __MLX5_EN_FLOW_METER_H__
 #define __MLX5_EN_FLOW_METER_H__
 
+struct mlx5e_post_meter_priv;
 struct mlx5e_flow_meter_aso_obj;
 struct mlx5e_flow_meters;
 struct mlx5_flow_attr;
@@ -35,6 +36,7 @@ struct mlx5e_flow_meter_handle {
 struct mlx5e_meter_attr {
 	struct mlx5e_flow_meter_params params;
 	struct mlx5e_flow_meter_handle *meter;
+	struct mlx5e_post_meter_priv *post_meter;
 };
 
 int
@@ -47,8 +49,8 @@ mlx5e_tc_meter_get(struct mlx5_core_dev
 void
 mlx5e_tc_meter_put(struct mlx5e_flow_meter_handle *meter);
 
-struct mlx5_flow_table *
-mlx5e_tc_meter_get_post_meter_ft(struct mlx5e_flow_meters *flow_meters);
+enum mlx5_flow_namespace_type
+mlx5e_tc_meter_get_namespace(struct mlx5e_flow_meters *flow_meters);
 
 struct mlx5e_flow_meters *
 mlx5e_flow_meters_init(struct mlx5e_priv *priv,
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_tc.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_tc.c
@@ -356,6 +356,9 @@ static int
 mlx5e_tc_add_flow_meter(struct mlx5e_priv *priv,
 			struct mlx5_flow_attr *attr)
 {
+	struct mlx5e_post_act *post_act = get_post_action(priv);
+	struct mlx5e_post_meter_priv *post_meter;
+	enum mlx5_flow_namespace_type ns_type;
 	struct mlx5e_flow_meter_handle *meter;
 
 	meter = mlx5e_tc_meter_get(priv->mdev, &attr->meter_attr.params);
@@ -364,11 +367,30 @@ mlx5e_tc_add_flow_meter(struct mlx5e_pri
 		return PTR_ERR(meter);
 	}
 
+	ns_type = mlx5e_tc_meter_get_namespace(meter->flow_meters);
+	post_meter = mlx5e_post_meter_init(priv, ns_type, post_act);
+	if (IS_ERR(post_meter)) {
+		mlx5_core_err(priv->mdev, "Failed to init post meter\n");
+		goto err_meter_init;
+	}
+
 	attr->meter_attr.meter = meter;
-	attr->dest_ft = mlx5e_tc_meter_get_post_meter_ft(meter->flow_meters);
+	attr->meter_attr.post_meter = post_meter;
+	attr->dest_ft = mlx5e_post_meter_get_ft(post_meter);
 	attr->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
 
 	return 0;
+
+err_meter_init:
+	mlx5e_tc_meter_put(meter);
+	return PTR_ERR(post_meter);
+}
+
+static void
+mlx5e_tc_del_flow_meter(struct mlx5_flow_attr *attr)
+{
+	mlx5e_post_meter_cleanup(attr->meter_attr.post_meter);
+	mlx5e_tc_meter_put(attr->meter_attr.meter);
 }
 
 struct mlx5_flow_handle *
@@ -428,7 +450,7 @@ mlx5e_tc_rule_unoffload(struct mlx5e_pri
 	mlx5_eswitch_del_offloaded_rule(esw, rule, attr);
 
 	if (attr->meter_attr.meter)
-		mlx5e_tc_meter_put(attr->meter_attr.meter);
+		mlx5e_tc_del_flow_meter(attr);
 }
 
 int
