net/mlx5: Refactor mlx5_add_flow_rule
[cascardo/linux.git] / drivers / net / ethernet / mellanox / mlx5 / core / eswitch_offloads.c
index ed8ad98..1842dfb 100644 (file)
@@ -43,37 +43,35 @@ mlx5_eswitch_add_send_to_vport_rule(struct mlx5_eswitch *esw, int vport, u32 sqn
 {
        struct mlx5_flow_destination dest;
        struct mlx5_flow_rule *flow_rule;
-       int match_header = MLX5_MATCH_MISC_PARAMETERS;
-       u32 *match_v, *match_c;
+       struct mlx5_flow_spec *spec;
        void *misc;
 
-       match_v = kzalloc(MLX5_ST_SZ_BYTES(fte_match_param), GFP_KERNEL);
-       match_c = kzalloc(MLX5_ST_SZ_BYTES(fte_match_param), GFP_KERNEL);
-       if (!match_v || !match_c) {
+       spec = mlx5_vzalloc(sizeof(*spec));
+       if (!spec) {
                esw_warn(esw->dev, "FDB: Failed to alloc match parameters\n");
                flow_rule = ERR_PTR(-ENOMEM);
                goto out;
        }
 
-       misc = MLX5_ADDR_OF(fte_match_param, match_v, misc_parameters);
+       misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
        MLX5_SET(fte_match_set_misc, misc, source_sqn, sqn);
        MLX5_SET(fte_match_set_misc, misc, source_port, 0x0); /* source vport is 0 */
 
-       misc = MLX5_ADDR_OF(fte_match_param, match_c, misc_parameters);
+       misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
        MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_sqn);
        MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
 
+       spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
        dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
        dest.vport_num = vport;
 
-       flow_rule = mlx5_add_flow_rule(esw->fdb_table.fdb, match_header, match_c,
-                                      match_v, MLX5_FLOW_CONTEXT_ACTION_FWD_DEST,
+       flow_rule = mlx5_add_flow_rule(esw->fdb_table.fdb, spec,
+                                      MLX5_FLOW_CONTEXT_ACTION_FWD_DEST,
                                       0, &dest);
        if (IS_ERR(flow_rule))
                esw_warn(esw->dev, "FDB: Failed to add send to vport rule err %ld\n", PTR_ERR(flow_rule));
 out:
-       kfree(match_v);
-       kfree(match_c);
+       kvfree(spec);
        return flow_rule;
 }
 
@@ -138,12 +136,11 @@ static int esw_add_fdb_miss_rule(struct mlx5_eswitch *esw)
 {
        struct mlx5_flow_destination dest;
        struct mlx5_flow_rule *flow_rule = NULL;
-       u32 *match_v, *match_c;
+       struct mlx5_flow_spec *spec;
        int err = 0;
 
-       match_v = kzalloc(MLX5_ST_SZ_BYTES(fte_match_param), GFP_KERNEL);
-       match_c = kzalloc(MLX5_ST_SZ_BYTES(fte_match_param), GFP_KERNEL);
-       if (!match_v || !match_c) {
+       spec = mlx5_vzalloc(sizeof(*spec));
+       if (!spec) {
                esw_warn(esw->dev, "FDB: Failed to alloc match parameters\n");
                err = -ENOMEM;
                goto out;
@@ -152,8 +149,9 @@ static int esw_add_fdb_miss_rule(struct mlx5_eswitch *esw)
        dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
        dest.vport_num = 0;
 
-       flow_rule = mlx5_add_flow_rule(esw->fdb_table.fdb, 0, match_c, match_v,
-                                      MLX5_FLOW_CONTEXT_ACTION_FWD_DEST, 0, &dest);
+       flow_rule = mlx5_add_flow_rule(esw->fdb_table.fdb, spec,
+                                      MLX5_FLOW_CONTEXT_ACTION_FWD_DEST,
+                                      0, &dest);
        if (IS_ERR(flow_rule)) {
                err = PTR_ERR(flow_rule);
                esw_warn(esw->dev,  "FDB: Failed to add miss flow rule err %d\n", err);
@@ -162,8 +160,7 @@ static int esw_add_fdb_miss_rule(struct mlx5_eswitch *esw)
 
        esw->fdb_table.offloads.miss_rule = flow_rule;
 out:
-       kfree(match_v);
-       kfree(match_c);
+       kvfree(spec);
        return err;
 }
 
@@ -351,29 +348,28 @@ mlx5_eswitch_create_vport_rx_rule(struct mlx5_eswitch *esw, int vport, u32 tirn)
 {
        struct mlx5_flow_destination dest;
        struct mlx5_flow_rule *flow_rule;
-       int match_header = MLX5_MATCH_MISC_PARAMETERS;
-       u32 *match_v, *match_c;
+       struct mlx5_flow_spec *spec;
        void *misc;
 
-       match_v = kzalloc(MLX5_ST_SZ_BYTES(fte_match_param), GFP_KERNEL);
-       match_c = kzalloc(MLX5_ST_SZ_BYTES(fte_match_param), GFP_KERNEL);
-       if (!match_v || !match_c) {
+       spec = mlx5_vzalloc(sizeof(*spec));
+       if (!spec) {
                esw_warn(esw->dev, "Failed to alloc match parameters\n");
                flow_rule = ERR_PTR(-ENOMEM);
                goto out;
        }
 
-       misc = MLX5_ADDR_OF(fte_match_param, match_v, misc_parameters);
+       misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
        MLX5_SET(fte_match_set_misc, misc, source_port, vport);
 
-       misc = MLX5_ADDR_OF(fte_match_param, match_c, misc_parameters);
+       misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
        MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
 
+       spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
        dest.type = MLX5_FLOW_DESTINATION_TYPE_TIR;
        dest.tir_num = tirn;
 
-       flow_rule = mlx5_add_flow_rule(esw->offloads.ft_offloads, match_header, match_c,
-                                      match_v, MLX5_FLOW_CONTEXT_ACTION_FWD_DEST,
+       flow_rule = mlx5_add_flow_rule(esw->offloads.ft_offloads, spec,
+                                      MLX5_FLOW_CONTEXT_ACTION_FWD_DEST,
                                       0, &dest);
        if (IS_ERR(flow_rule)) {
                esw_warn(esw->dev, "fs offloads: Failed to add vport rx rule err %ld\n", PTR_ERR(flow_rule));
@@ -381,8 +377,7 @@ mlx5_eswitch_create_vport_rx_rule(struct mlx5_eswitch *esw, int vport, u32 tirn)
        }
 
 out:
-       kfree(match_v);
-       kfree(match_c);
+       kvfree(spec);
        return flow_rule;
 }