16a48faeeSMaor Gottlieb // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
26a48faeeSMaor Gottlieb /* Copyright (c) 2019 Mellanox Technologies */
36a48faeeSMaor Gottlieb 
4e3a0f40bSYevgeny Kliteynik #include <linux/mlx5/vport.h>
56a48faeeSMaor Gottlieb #include "mlx5_core.h"
66a48faeeSMaor Gottlieb #include "fs_core.h"
76a48faeeSMaor Gottlieb #include "fs_cmd.h"
86a48faeeSMaor Gottlieb #include "mlx5dr.h"
96a48faeeSMaor Gottlieb #include "fs_dr.h"
10*0a8c20e2SYevgeny Kliteynik #include "dr_types.h"
116a48faeeSMaor Gottlieb 
12*0a8c20e2SYevgeny Kliteynik static bool dr_is_fw_term_table(struct mlx5_flow_table *ft)
136a48faeeSMaor Gottlieb {
14*0a8c20e2SYevgeny Kliteynik 	if (ft->flags & MLX5_FLOW_TABLE_TERMINATION)
156a48faeeSMaor Gottlieb 		return true;
166a48faeeSMaor Gottlieb 
176a48faeeSMaor Gottlieb 	return false;
186a48faeeSMaor Gottlieb }
196a48faeeSMaor Gottlieb 
206a48faeeSMaor Gottlieb static int mlx5_cmd_dr_update_root_ft(struct mlx5_flow_root_namespace *ns,
216a48faeeSMaor Gottlieb 				      struct mlx5_flow_table *ft,
226a48faeeSMaor Gottlieb 				      u32 underlay_qpn,
236a48faeeSMaor Gottlieb 				      bool disconnect)
246a48faeeSMaor Gottlieb {
256a48faeeSMaor Gottlieb 	return mlx5_fs_cmd_get_fw_cmds()->update_root_ft(ns, ft, underlay_qpn,
266a48faeeSMaor Gottlieb 							 disconnect);
276a48faeeSMaor Gottlieb }
286a48faeeSMaor Gottlieb 
296a48faeeSMaor Gottlieb static int set_miss_action(struct mlx5_flow_root_namespace *ns,
306a48faeeSMaor Gottlieb 			   struct mlx5_flow_table *ft,
316a48faeeSMaor Gottlieb 			   struct mlx5_flow_table *next_ft)
326a48faeeSMaor Gottlieb {
336a48faeeSMaor Gottlieb 	struct mlx5dr_action *old_miss_action;
346a48faeeSMaor Gottlieb 	struct mlx5dr_action *action = NULL;
356a48faeeSMaor Gottlieb 	struct mlx5dr_table *next_tbl;
366a48faeeSMaor Gottlieb 	int err;
376a48faeeSMaor Gottlieb 
386a48faeeSMaor Gottlieb 	next_tbl = next_ft ? next_ft->fs_dr_table.dr_table : NULL;
396a48faeeSMaor Gottlieb 	if (next_tbl) {
406a48faeeSMaor Gottlieb 		action = mlx5dr_action_create_dest_table(next_tbl);
416a48faeeSMaor Gottlieb 		if (!action)
426a48faeeSMaor Gottlieb 			return -EINVAL;
436a48faeeSMaor Gottlieb 	}
446a48faeeSMaor Gottlieb 	old_miss_action = ft->fs_dr_table.miss_action;
456a48faeeSMaor Gottlieb 	err = mlx5dr_table_set_miss_action(ft->fs_dr_table.dr_table, action);
466a48faeeSMaor Gottlieb 	if (err && action) {
476a48faeeSMaor Gottlieb 		err = mlx5dr_action_destroy(action);
4880b2bd73SLeon Romanovsky 		if (err)
4980b2bd73SLeon Romanovsky 			mlx5_core_err(ns->dev,
5080b2bd73SLeon Romanovsky 				      "Failed to destroy action (%d)\n", err);
516a48faeeSMaor Gottlieb 		action = NULL;
526a48faeeSMaor Gottlieb 	}
536a48faeeSMaor Gottlieb 	ft->fs_dr_table.miss_action = action;
546a48faeeSMaor Gottlieb 	if (old_miss_action) {
556a48faeeSMaor Gottlieb 		err = mlx5dr_action_destroy(old_miss_action);
566a48faeeSMaor Gottlieb 		if (err)
576a48faeeSMaor Gottlieb 			mlx5_core_err(ns->dev, "Failed to destroy action (%d)\n",
586a48faeeSMaor Gottlieb 				      err);
596a48faeeSMaor Gottlieb 	}
606a48faeeSMaor Gottlieb 
616a48faeeSMaor Gottlieb 	return err;
626a48faeeSMaor Gottlieb }
636a48faeeSMaor Gottlieb 
646a48faeeSMaor Gottlieb static int mlx5_cmd_dr_create_flow_table(struct mlx5_flow_root_namespace *ns,
656a48faeeSMaor Gottlieb 					 struct mlx5_flow_table *ft,
66b0bb369eSMark Bloch 					 struct mlx5_flow_table_attr *ft_attr,
676a48faeeSMaor Gottlieb 					 struct mlx5_flow_table *next_ft)
686a48faeeSMaor Gottlieb {
696a48faeeSMaor Gottlieb 	struct mlx5dr_table *tbl;
7013a7e459SErez Shitrit 	u32 flags;
716a48faeeSMaor Gottlieb 	int err;
726a48faeeSMaor Gottlieb 
73*0a8c20e2SYevgeny Kliteynik 	if (dr_is_fw_term_table(ft))
746a48faeeSMaor Gottlieb 		return mlx5_fs_cmd_get_fw_cmds()->create_flow_table(ns, ft,
75b0bb369eSMark Bloch 								    ft_attr,
766a48faeeSMaor Gottlieb 								    next_ft);
7713a7e459SErez Shitrit 	flags = ft->flags;
7813a7e459SErez Shitrit 	/* turn off encap/decap if not supported for sw-str by fw */
7913a7e459SErez Shitrit 	if (!MLX5_CAP_FLOWTABLE(ns->dev, sw_owner_reformat_supported))
8013a7e459SErez Shitrit 		flags = ft->flags & ~(MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT |
8113a7e459SErez Shitrit 				      MLX5_FLOW_TABLE_TUNNEL_EN_DECAP);
826a48faeeSMaor Gottlieb 
83b0bb369eSMark Bloch 	tbl = mlx5dr_table_create(ns->fs_dr_domain.dr_domain, ft->level, flags,
84b0bb369eSMark Bloch 				  ft_attr->uid);
856a48faeeSMaor Gottlieb 	if (!tbl) {
866a48faeeSMaor Gottlieb 		mlx5_core_err(ns->dev, "Failed creating dr flow_table\n");
876a48faeeSMaor Gottlieb 		return -EINVAL;
886a48faeeSMaor Gottlieb 	}
896a48faeeSMaor Gottlieb 
906a48faeeSMaor Gottlieb 	ft->fs_dr_table.dr_table = tbl;
916a48faeeSMaor Gottlieb 	ft->id = mlx5dr_table_get_id(tbl);
926a48faeeSMaor Gottlieb 
936a48faeeSMaor Gottlieb 	if (next_ft) {
946a48faeeSMaor Gottlieb 		err = set_miss_action(ns, ft, next_ft);
956a48faeeSMaor Gottlieb 		if (err) {
966a48faeeSMaor Gottlieb 			mlx5dr_table_destroy(tbl);
976a48faeeSMaor Gottlieb 			ft->fs_dr_table.dr_table = NULL;
986a48faeeSMaor Gottlieb 			return err;
996a48faeeSMaor Gottlieb 		}
1006a48faeeSMaor Gottlieb 	}
1016a48faeeSMaor Gottlieb 
1029e117998SPaul Blakey 	ft->max_fte = INT_MAX;
10304745afbSPaul Blakey 
1046a48faeeSMaor Gottlieb 	return 0;
1056a48faeeSMaor Gottlieb }
1066a48faeeSMaor Gottlieb 
1076a48faeeSMaor Gottlieb static int mlx5_cmd_dr_destroy_flow_table(struct mlx5_flow_root_namespace *ns,
1086a48faeeSMaor Gottlieb 					  struct mlx5_flow_table *ft)
1096a48faeeSMaor Gottlieb {
1106a48faeeSMaor Gottlieb 	struct mlx5dr_action *action = ft->fs_dr_table.miss_action;
1116a48faeeSMaor Gottlieb 	int err;
1126a48faeeSMaor Gottlieb 
113*0a8c20e2SYevgeny Kliteynik 	if (dr_is_fw_term_table(ft))
1146a48faeeSMaor Gottlieb 		return mlx5_fs_cmd_get_fw_cmds()->destroy_flow_table(ns, ft);
1156a48faeeSMaor Gottlieb 
1166a48faeeSMaor Gottlieb 	err = mlx5dr_table_destroy(ft->fs_dr_table.dr_table);
1176a48faeeSMaor Gottlieb 	if (err) {
1186a48faeeSMaor Gottlieb 		mlx5_core_err(ns->dev, "Failed to destroy flow_table (%d)\n",
1196a48faeeSMaor Gottlieb 			      err);
1206a48faeeSMaor Gottlieb 		return err;
1216a48faeeSMaor Gottlieb 	}
1226a48faeeSMaor Gottlieb 	if (action) {
1236a48faeeSMaor Gottlieb 		err = mlx5dr_action_destroy(action);
1246a48faeeSMaor Gottlieb 		if (err) {
1256a48faeeSMaor Gottlieb 			mlx5_core_err(ns->dev, "Failed to destroy action(%d)\n",
1266a48faeeSMaor Gottlieb 				      err);
1276a48faeeSMaor Gottlieb 			return err;
1286a48faeeSMaor Gottlieb 		}
1296a48faeeSMaor Gottlieb 	}
1306a48faeeSMaor Gottlieb 
1316a48faeeSMaor Gottlieb 	return err;
1326a48faeeSMaor Gottlieb }
1336a48faeeSMaor Gottlieb 
1346a48faeeSMaor Gottlieb static int mlx5_cmd_dr_modify_flow_table(struct mlx5_flow_root_namespace *ns,
1356a48faeeSMaor Gottlieb 					 struct mlx5_flow_table *ft,
1366a48faeeSMaor Gottlieb 					 struct mlx5_flow_table *next_ft)
1376a48faeeSMaor Gottlieb {
138*0a8c20e2SYevgeny Kliteynik 	if (dr_is_fw_term_table(ft))
139a01a43faSYevgeny Kliteynik 		return mlx5_fs_cmd_get_fw_cmds()->modify_flow_table(ns, ft, next_ft);
140a01a43faSYevgeny Kliteynik 
1416a48faeeSMaor Gottlieb 	return set_miss_action(ns, ft, next_ft);
1426a48faeeSMaor Gottlieb }
1436a48faeeSMaor Gottlieb 
1446a48faeeSMaor Gottlieb static int mlx5_cmd_dr_create_flow_group(struct mlx5_flow_root_namespace *ns,
1456a48faeeSMaor Gottlieb 					 struct mlx5_flow_table *ft,
1466a48faeeSMaor Gottlieb 					 u32 *in,
1476a48faeeSMaor Gottlieb 					 struct mlx5_flow_group *fg)
1486a48faeeSMaor Gottlieb {
1496a48faeeSMaor Gottlieb 	struct mlx5dr_matcher *matcher;
150f6409299SHamdan Igbaria 	u32 priority = MLX5_GET(create_flow_group_in, in,
1516a48faeeSMaor Gottlieb 				start_flow_index);
1526a48faeeSMaor Gottlieb 	u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
1536a48faeeSMaor Gottlieb 					    in,
1546a48faeeSMaor Gottlieb 					    match_criteria_enable);
1556a48faeeSMaor Gottlieb 	struct mlx5dr_match_parameters mask;
1566a48faeeSMaor Gottlieb 
157*0a8c20e2SYevgeny Kliteynik 	if (dr_is_fw_term_table(ft))
1586a48faeeSMaor Gottlieb 		return mlx5_fs_cmd_get_fw_cmds()->create_flow_group(ns, ft, in,
1596a48faeeSMaor Gottlieb 								    fg);
1606a48faeeSMaor Gottlieb 
1616a48faeeSMaor Gottlieb 	mask.match_buf = MLX5_ADDR_OF(create_flow_group_in,
1626a48faeeSMaor Gottlieb 				      in, match_criteria);
1636a48faeeSMaor Gottlieb 	mask.match_sz = sizeof(fg->mask.match_criteria);
1646a48faeeSMaor Gottlieb 
1656a48faeeSMaor Gottlieb 	matcher = mlx5dr_matcher_create(ft->fs_dr_table.dr_table,
1666a48faeeSMaor Gottlieb 					priority,
1676a48faeeSMaor Gottlieb 					match_criteria_enable,
1686a48faeeSMaor Gottlieb 					&mask);
1696a48faeeSMaor Gottlieb 	if (!matcher) {
1706a48faeeSMaor Gottlieb 		mlx5_core_err(ns->dev, "Failed creating matcher\n");
1716a48faeeSMaor Gottlieb 		return -EINVAL;
1726a48faeeSMaor Gottlieb 	}
1736a48faeeSMaor Gottlieb 
1746a48faeeSMaor Gottlieb 	fg->fs_dr_matcher.dr_matcher = matcher;
1756a48faeeSMaor Gottlieb 	return 0;
1766a48faeeSMaor Gottlieb }
1776a48faeeSMaor Gottlieb 
1786a48faeeSMaor Gottlieb static int mlx5_cmd_dr_destroy_flow_group(struct mlx5_flow_root_namespace *ns,
1796a48faeeSMaor Gottlieb 					  struct mlx5_flow_table *ft,
1806a48faeeSMaor Gottlieb 					  struct mlx5_flow_group *fg)
1816a48faeeSMaor Gottlieb {
182*0a8c20e2SYevgeny Kliteynik 	if (dr_is_fw_term_table(ft))
1836a48faeeSMaor Gottlieb 		return mlx5_fs_cmd_get_fw_cmds()->destroy_flow_group(ns, ft, fg);
1846a48faeeSMaor Gottlieb 
1856a48faeeSMaor Gottlieb 	return mlx5dr_matcher_destroy(fg->fs_dr_matcher.dr_matcher);
1866a48faeeSMaor Gottlieb }
1876a48faeeSMaor Gottlieb 
1886a48faeeSMaor Gottlieb static struct mlx5dr_action *create_vport_action(struct mlx5dr_domain *domain,
1896a48faeeSMaor Gottlieb 						 struct mlx5_flow_rule *dst)
1906a48faeeSMaor Gottlieb {
1916a48faeeSMaor Gottlieb 	struct mlx5_flow_destination *dest_attr = &dst->dest_attr;
1926a48faeeSMaor Gottlieb 
1936a48faeeSMaor Gottlieb 	return mlx5dr_action_create_dest_vport(domain, dest_attr->vport.num,
1946a48faeeSMaor Gottlieb 					       dest_attr->vport.flags &
1956a48faeeSMaor Gottlieb 					       MLX5_FLOW_DEST_VPORT_VHCA_ID,
1966a48faeeSMaor Gottlieb 					       dest_attr->vport.vhca_id);
1976a48faeeSMaor Gottlieb }
1986a48faeeSMaor Gottlieb 
199e3a0f40bSYevgeny Kliteynik static struct mlx5dr_action *create_uplink_action(struct mlx5dr_domain *domain,
200e3a0f40bSYevgeny Kliteynik 						  struct mlx5_flow_rule *dst)
201e3a0f40bSYevgeny Kliteynik {
202e3a0f40bSYevgeny Kliteynik 	struct mlx5_flow_destination *dest_attr = &dst->dest_attr;
203e3a0f40bSYevgeny Kliteynik 
204e3a0f40bSYevgeny Kliteynik 	return mlx5dr_action_create_dest_vport(domain, MLX5_VPORT_UPLINK, 1,
205e3a0f40bSYevgeny Kliteynik 					       dest_attr->vport.vhca_id);
206e3a0f40bSYevgeny Kliteynik }
207e3a0f40bSYevgeny Kliteynik 
208aec292eeSAlex Vesker static struct mlx5dr_action *create_ft_action(struct mlx5dr_domain *domain,
2096a48faeeSMaor Gottlieb 					      struct mlx5_flow_rule *dst)
2106a48faeeSMaor Gottlieb {
2116a48faeeSMaor Gottlieb 	struct mlx5_flow_table *dest_ft = dst->dest_attr.ft;
2126a48faeeSMaor Gottlieb 
213*0a8c20e2SYevgeny Kliteynik 	if (mlx5dr_is_fw_table(dest_ft))
214aec292eeSAlex Vesker 		return mlx5dr_action_create_dest_flow_fw_table(domain, dest_ft);
2156a48faeeSMaor Gottlieb 	return mlx5dr_action_create_dest_table(dest_ft->fs_dr_table.dr_table);
2166a48faeeSMaor Gottlieb }
2176a48faeeSMaor Gottlieb 
2186a48faeeSMaor Gottlieb static struct mlx5dr_action *create_action_push_vlan(struct mlx5dr_domain *domain,
2196a48faeeSMaor Gottlieb 						     struct mlx5_fs_vlan *vlan)
2206a48faeeSMaor Gottlieb {
2216a48faeeSMaor Gottlieb 	u16 n_ethtype = vlan->ethtype;
2226a48faeeSMaor Gottlieb 	u8  prio = vlan->prio;
2236a48faeeSMaor Gottlieb 	u16 vid = vlan->vid;
2246a48faeeSMaor Gottlieb 	u32 vlan_hdr;
2256a48faeeSMaor Gottlieb 
2266a48faeeSMaor Gottlieb 	vlan_hdr = (u32)n_ethtype << 16 | (u32)(prio) << 12 |  (u32)vid;
2276a48faeeSMaor Gottlieb 	return mlx5dr_action_create_push_vlan(domain, htonl(vlan_hdr));
2286a48faeeSMaor Gottlieb }
2296a48faeeSMaor Gottlieb 
2307ee3f6d2SAlex Vesker static bool contain_vport_reformat_action(struct mlx5_flow_rule *dst)
2317ee3f6d2SAlex Vesker {
232e3a0f40bSYevgeny Kliteynik 	return (dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_VPORT ||
233e3a0f40bSYevgeny Kliteynik 		dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_UPLINK) &&
2347ee3f6d2SAlex Vesker 		dst->dest_attr.vport.flags & MLX5_FLOW_DEST_VPORT_REFORMAT_ID;
2357ee3f6d2SAlex Vesker }
2367ee3f6d2SAlex Vesker 
2370aec12d9SYevgeny Kliteynik /* We want to support a rule with 32 destinations, which means we need to
2380aec12d9SYevgeny Kliteynik  * account for 32 destinations plus usually a counter plus one more action
2390aec12d9SYevgeny Kliteynik  * for a multi-destination flow table.
2400aec12d9SYevgeny Kliteynik  */
2410aec12d9SYevgeny Kliteynik #define MLX5_FLOW_CONTEXT_ACTION_MAX  34
2426a48faeeSMaor Gottlieb static int mlx5_cmd_dr_create_fte(struct mlx5_flow_root_namespace *ns,
2436a48faeeSMaor Gottlieb 				  struct mlx5_flow_table *ft,
2446a48faeeSMaor Gottlieb 				  struct mlx5_flow_group *group,
2456a48faeeSMaor Gottlieb 				  struct fs_fte *fte)
2466a48faeeSMaor Gottlieb {
2476a48faeeSMaor Gottlieb 	struct mlx5dr_domain *domain = ns->fs_dr_domain.dr_domain;
2487ee3f6d2SAlex Vesker 	struct mlx5dr_action_dest *term_actions;
2496a48faeeSMaor Gottlieb 	struct mlx5dr_match_parameters params;
2506a48faeeSMaor Gottlieb 	struct mlx5_core_dev *dev = ns->dev;
2516a48faeeSMaor Gottlieb 	struct mlx5dr_action **fs_dr_actions;
2526a48faeeSMaor Gottlieb 	struct mlx5dr_action *tmp_action;
2536a48faeeSMaor Gottlieb 	struct mlx5dr_action **actions;
2546a48faeeSMaor Gottlieb 	bool delay_encap_set = false;
2556a48faeeSMaor Gottlieb 	struct mlx5dr_rule *rule;
2566a48faeeSMaor Gottlieb 	struct mlx5_flow_rule *dst;
2576a48faeeSMaor Gottlieb 	int fs_dr_num_actions = 0;
2587ee3f6d2SAlex Vesker 	int num_term_actions = 0;
2596a48faeeSMaor Gottlieb 	int num_actions = 0;
2606a48faeeSMaor Gottlieb 	size_t match_sz;
2616a48faeeSMaor Gottlieb 	int err = 0;
2626a48faeeSMaor Gottlieb 	int i;
2636a48faeeSMaor Gottlieb 
264*0a8c20e2SYevgeny Kliteynik 	if (dr_is_fw_term_table(ft))
2656a48faeeSMaor Gottlieb 		return mlx5_fs_cmd_get_fw_cmds()->create_fte(ns, ft, group, fte);
2666a48faeeSMaor Gottlieb 
2676a48faeeSMaor Gottlieb 	actions = kcalloc(MLX5_FLOW_CONTEXT_ACTION_MAX, sizeof(*actions),
2686a48faeeSMaor Gottlieb 			  GFP_KERNEL);
2697ee3f6d2SAlex Vesker 	if (!actions) {
2707ee3f6d2SAlex Vesker 		err = -ENOMEM;
2717ee3f6d2SAlex Vesker 		goto out_err;
2727ee3f6d2SAlex Vesker 	}
2736a48faeeSMaor Gottlieb 
2746a48faeeSMaor Gottlieb 	fs_dr_actions = kcalloc(MLX5_FLOW_CONTEXT_ACTION_MAX,
2756a48faeeSMaor Gottlieb 				sizeof(*fs_dr_actions), GFP_KERNEL);
2766a48faeeSMaor Gottlieb 	if (!fs_dr_actions) {
2777ee3f6d2SAlex Vesker 		err = -ENOMEM;
2787ee3f6d2SAlex Vesker 		goto free_actions_alloc;
2797ee3f6d2SAlex Vesker 	}
2807ee3f6d2SAlex Vesker 
2817ee3f6d2SAlex Vesker 	term_actions = kcalloc(MLX5_FLOW_CONTEXT_ACTION_MAX,
2827ee3f6d2SAlex Vesker 			       sizeof(*term_actions), GFP_KERNEL);
2837ee3f6d2SAlex Vesker 	if (!term_actions) {
2847ee3f6d2SAlex Vesker 		err = -ENOMEM;
2857ee3f6d2SAlex Vesker 		goto free_fs_dr_actions_alloc;
2866a48faeeSMaor Gottlieb 	}
2876a48faeeSMaor Gottlieb 
2886a48faeeSMaor Gottlieb 	match_sz = sizeof(fte->val);
2896a48faeeSMaor Gottlieb 
2907ee3f6d2SAlex Vesker 	/* Drop reformat action bit if destination vport set with reformat */
2917ee3f6d2SAlex Vesker 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
2927ee3f6d2SAlex Vesker 		list_for_each_entry(dst, &fte->node.children, node.list) {
2937ee3f6d2SAlex Vesker 			if (!contain_vport_reformat_action(dst))
2947ee3f6d2SAlex Vesker 				continue;
2957ee3f6d2SAlex Vesker 
2967ee3f6d2SAlex Vesker 			fte->action.action &= ~MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
2977ee3f6d2SAlex Vesker 			break;
2987ee3f6d2SAlex Vesker 		}
2997ee3f6d2SAlex Vesker 	}
3007ee3f6d2SAlex Vesker 
3016a48faeeSMaor Gottlieb 	/* The order of the actions are must to be keep, only the following
3026a48faeeSMaor Gottlieb 	 * order is supported by SW steering:
303b2064909SAlex Vesker 	 * TX: modify header -> push vlan -> encap
3046a48faeeSMaor Gottlieb 	 * RX: decap -> pop vlan -> modify header
3056a48faeeSMaor Gottlieb 	 */
3066a48faeeSMaor Gottlieb 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_DECAP) {
3076a48faeeSMaor Gottlieb 		enum mlx5dr_action_reformat_type decap_type =
3086a48faeeSMaor Gottlieb 			DR_ACTION_REFORMAT_TYP_TNL_L2_TO_L2;
3096a48faeeSMaor Gottlieb 
3106a48faeeSMaor Gottlieb 		tmp_action = mlx5dr_action_create_packet_reformat(domain,
3113f3f05abSYevgeny Kliteynik 								  decap_type,
3123f3f05abSYevgeny Kliteynik 								  0, 0, 0,
3136a48faeeSMaor Gottlieb 								  NULL);
3146a48faeeSMaor Gottlieb 		if (!tmp_action) {
3156a48faeeSMaor Gottlieb 			err = -ENOMEM;
3166a48faeeSMaor Gottlieb 			goto free_actions;
3176a48faeeSMaor Gottlieb 		}
3186a48faeeSMaor Gottlieb 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
3196a48faeeSMaor Gottlieb 		actions[num_actions++] = tmp_action;
3206a48faeeSMaor Gottlieb 	}
3216a48faeeSMaor Gottlieb 
3226a48faeeSMaor Gottlieb 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT) {
3236a48faeeSMaor Gottlieb 		bool is_decap = fte->action.pkt_reformat->reformat_type ==
3246a48faeeSMaor Gottlieb 			MLX5_REFORMAT_TYPE_L3_TUNNEL_TO_L2;
3256a48faeeSMaor Gottlieb 
3266a48faeeSMaor Gottlieb 		if (is_decap)
3276a48faeeSMaor Gottlieb 			actions[num_actions++] =
3286a48faeeSMaor Gottlieb 				fte->action.pkt_reformat->action.dr_action;
3296a48faeeSMaor Gottlieb 		else
3306a48faeeSMaor Gottlieb 			delay_encap_set = true;
3316a48faeeSMaor Gottlieb 	}
3326a48faeeSMaor Gottlieb 
3336a48faeeSMaor Gottlieb 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP) {
3346a48faeeSMaor Gottlieb 		tmp_action =
3356a48faeeSMaor Gottlieb 			mlx5dr_action_create_pop_vlan();
3366a48faeeSMaor Gottlieb 		if (!tmp_action) {
3376a48faeeSMaor Gottlieb 			err = -ENOMEM;
3386a48faeeSMaor Gottlieb 			goto free_actions;
3396a48faeeSMaor Gottlieb 		}
3406a48faeeSMaor Gottlieb 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
3416a48faeeSMaor Gottlieb 		actions[num_actions++] = tmp_action;
3426a48faeeSMaor Gottlieb 	}
3436a48faeeSMaor Gottlieb 
3446a48faeeSMaor Gottlieb 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP_2) {
3456a48faeeSMaor Gottlieb 		tmp_action =
3466a48faeeSMaor Gottlieb 			mlx5dr_action_create_pop_vlan();
3476a48faeeSMaor Gottlieb 		if (!tmp_action) {
3486a48faeeSMaor Gottlieb 			err = -ENOMEM;
3496a48faeeSMaor Gottlieb 			goto free_actions;
3506a48faeeSMaor Gottlieb 		}
3516a48faeeSMaor Gottlieb 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
3526a48faeeSMaor Gottlieb 		actions[num_actions++] = tmp_action;
3536a48faeeSMaor Gottlieb 	}
3546a48faeeSMaor Gottlieb 
3556a48faeeSMaor Gottlieb 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
3566a48faeeSMaor Gottlieb 		actions[num_actions++] =
3576a48faeeSMaor Gottlieb 			fte->action.modify_hdr->action.dr_action;
3586a48faeeSMaor Gottlieb 
359b2064909SAlex Vesker 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH) {
360b2064909SAlex Vesker 		tmp_action = create_action_push_vlan(domain, &fte->action.vlan[0]);
361b2064909SAlex Vesker 		if (!tmp_action) {
362b2064909SAlex Vesker 			err = -ENOMEM;
363b2064909SAlex Vesker 			goto free_actions;
364b2064909SAlex Vesker 		}
365b2064909SAlex Vesker 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
366b2064909SAlex Vesker 		actions[num_actions++] = tmp_action;
367b2064909SAlex Vesker 	}
368b2064909SAlex Vesker 
369b2064909SAlex Vesker 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2) {
370b2064909SAlex Vesker 		tmp_action = create_action_push_vlan(domain, &fte->action.vlan[1]);
371b2064909SAlex Vesker 		if (!tmp_action) {
372b2064909SAlex Vesker 			err = -ENOMEM;
373b2064909SAlex Vesker 			goto free_actions;
374b2064909SAlex Vesker 		}
375b2064909SAlex Vesker 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
376b2064909SAlex Vesker 		actions[num_actions++] = tmp_action;
377b2064909SAlex Vesker 	}
378b2064909SAlex Vesker 
3796a48faeeSMaor Gottlieb 	if (delay_encap_set)
3806a48faeeSMaor Gottlieb 		actions[num_actions++] =
3816a48faeeSMaor Gottlieb 			fte->action.pkt_reformat->action.dr_action;
3826a48faeeSMaor Gottlieb 
3836a48faeeSMaor Gottlieb 	/* The order of the actions below is not important */
3846a48faeeSMaor Gottlieb 
3856a48faeeSMaor Gottlieb 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
3866a48faeeSMaor Gottlieb 		tmp_action = mlx5dr_action_create_drop();
3876a48faeeSMaor Gottlieb 		if (!tmp_action) {
3886a48faeeSMaor Gottlieb 			err = -ENOMEM;
3896a48faeeSMaor Gottlieb 			goto free_actions;
3906a48faeeSMaor Gottlieb 		}
3916a48faeeSMaor Gottlieb 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
3927ee3f6d2SAlex Vesker 		term_actions[num_term_actions++].dest = tmp_action;
3936a48faeeSMaor Gottlieb 	}
3946a48faeeSMaor Gottlieb 
3956a48faeeSMaor Gottlieb 	if (fte->flow_context.flow_tag) {
3966a48faeeSMaor Gottlieb 		tmp_action =
3976a48faeeSMaor Gottlieb 			mlx5dr_action_create_tag(fte->flow_context.flow_tag);
3986a48faeeSMaor Gottlieb 		if (!tmp_action) {
3996a48faeeSMaor Gottlieb 			err = -ENOMEM;
4006a48faeeSMaor Gottlieb 			goto free_actions;
4016a48faeeSMaor Gottlieb 		}
4026a48faeeSMaor Gottlieb 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
4036a48faeeSMaor Gottlieb 		actions[num_actions++] = tmp_action;
4046a48faeeSMaor Gottlieb 	}
4056a48faeeSMaor Gottlieb 
4066a48faeeSMaor Gottlieb 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
4076a48faeeSMaor Gottlieb 		list_for_each_entry(dst, &fte->node.children, node.list) {
4086a48faeeSMaor Gottlieb 			enum mlx5_flow_destination_type type = dst->dest_attr.type;
4091ab6dc35SYevgeny Kliteynik 			u32 id;
4106a48faeeSMaor Gottlieb 
4110aec12d9SYevgeny Kliteynik 			if (fs_dr_num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX ||
4120aec12d9SYevgeny Kliteynik 			    num_term_actions == MLX5_FLOW_CONTEXT_ACTION_MAX) {
4130aec12d9SYevgeny Kliteynik 				err = -EOPNOTSUPP;
4146a48faeeSMaor Gottlieb 				goto free_actions;
4156a48faeeSMaor Gottlieb 			}
4166a48faeeSMaor Gottlieb 
417b850a821SErez Shitrit 			if (type == MLX5_FLOW_DESTINATION_TYPE_COUNTER)
418b850a821SErez Shitrit 				continue;
4196a48faeeSMaor Gottlieb 
420b850a821SErez Shitrit 			switch (type) {
4216a48faeeSMaor Gottlieb 			case MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE:
422aec292eeSAlex Vesker 				tmp_action = create_ft_action(domain, dst);
4236a48faeeSMaor Gottlieb 				if (!tmp_action) {
4246a48faeeSMaor Gottlieb 					err = -ENOMEM;
4256a48faeeSMaor Gottlieb 					goto free_actions;
4266a48faeeSMaor Gottlieb 				}
4276a48faeeSMaor Gottlieb 				fs_dr_actions[fs_dr_num_actions++] = tmp_action;
4287ee3f6d2SAlex Vesker 				term_actions[num_term_actions++].dest = tmp_action;
4296a48faeeSMaor Gottlieb 				break;
430e3a0f40bSYevgeny Kliteynik 			case MLX5_FLOW_DESTINATION_TYPE_UPLINK:
4316a48faeeSMaor Gottlieb 			case MLX5_FLOW_DESTINATION_TYPE_VPORT:
432e3a0f40bSYevgeny Kliteynik 				tmp_action = type == MLX5_FLOW_DESTINATION_TYPE_VPORT ?
433e3a0f40bSYevgeny Kliteynik 					     create_vport_action(domain, dst) :
434e3a0f40bSYevgeny Kliteynik 					     create_uplink_action(domain, dst);
4356a48faeeSMaor Gottlieb 				if (!tmp_action) {
4366a48faeeSMaor Gottlieb 					err = -ENOMEM;
4376a48faeeSMaor Gottlieb 					goto free_actions;
4386a48faeeSMaor Gottlieb 				}
4396a48faeeSMaor Gottlieb 				fs_dr_actions[fs_dr_num_actions++] = tmp_action;
4407ee3f6d2SAlex Vesker 				term_actions[num_term_actions].dest = tmp_action;
4417ee3f6d2SAlex Vesker 
4427ee3f6d2SAlex Vesker 				if (dst->dest_attr.vport.flags &
4437ee3f6d2SAlex Vesker 				    MLX5_FLOW_DEST_VPORT_REFORMAT_ID)
4447ee3f6d2SAlex Vesker 					term_actions[num_term_actions].reformat =
4457ee3f6d2SAlex Vesker 						dst->dest_attr.vport.pkt_reformat->action.dr_action;
4467ee3f6d2SAlex Vesker 
4477ee3f6d2SAlex Vesker 				num_term_actions++;
4486a48faeeSMaor Gottlieb 				break;
449de346f40SAlex Vesker 			case MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM:
4501ab6dc35SYevgeny Kliteynik 				id = dst->dest_attr.ft_num;
451de346f40SAlex Vesker 				tmp_action = mlx5dr_action_create_dest_table_num(domain,
4521ab6dc35SYevgeny Kliteynik 										 id);
4531ab6dc35SYevgeny Kliteynik 				if (!tmp_action) {
4541ab6dc35SYevgeny Kliteynik 					err = -ENOMEM;
4551ab6dc35SYevgeny Kliteynik 					goto free_actions;
4561ab6dc35SYevgeny Kliteynik 				}
4571ab6dc35SYevgeny Kliteynik 				fs_dr_actions[fs_dr_num_actions++] = tmp_action;
4581ab6dc35SYevgeny Kliteynik 				term_actions[num_term_actions++].dest = tmp_action;
4591ab6dc35SYevgeny Kliteynik 				break;
4601ab6dc35SYevgeny Kliteynik 			case MLX5_FLOW_DESTINATION_TYPE_FLOW_SAMPLER:
4611ab6dc35SYevgeny Kliteynik 				id = dst->dest_attr.sampler_id;
4621ab6dc35SYevgeny Kliteynik 				tmp_action = mlx5dr_action_create_flow_sampler(domain,
4631ab6dc35SYevgeny Kliteynik 									       id);
464de346f40SAlex Vesker 				if (!tmp_action) {
465de346f40SAlex Vesker 					err = -ENOMEM;
466de346f40SAlex Vesker 					goto free_actions;
467de346f40SAlex Vesker 				}
468de346f40SAlex Vesker 				fs_dr_actions[fs_dr_num_actions++] = tmp_action;
469de346f40SAlex Vesker 				term_actions[num_term_actions++].dest = tmp_action;
470de346f40SAlex Vesker 				break;
4716a48faeeSMaor Gottlieb 			default:
4726a48faeeSMaor Gottlieb 				err = -EOPNOTSUPP;
4736a48faeeSMaor Gottlieb 				goto free_actions;
4746a48faeeSMaor Gottlieb 			}
4756a48faeeSMaor Gottlieb 		}
4766a48faeeSMaor Gottlieb 	}
4776a48faeeSMaor Gottlieb 
478b850a821SErez Shitrit 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
479b850a821SErez Shitrit 		list_for_each_entry(dst, &fte->node.children, node.list) {
480b850a821SErez Shitrit 			u32 id;
481b850a821SErez Shitrit 
482b850a821SErez Shitrit 			if (dst->dest_attr.type !=
483b850a821SErez Shitrit 			    MLX5_FLOW_DESTINATION_TYPE_COUNTER)
484b850a821SErez Shitrit 				continue;
485b850a821SErez Shitrit 
4860aec12d9SYevgeny Kliteynik 			if (num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX ||
4870aec12d9SYevgeny Kliteynik 			    fs_dr_num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX) {
4880aec12d9SYevgeny Kliteynik 				err = -EOPNOTSUPP;
489b850a821SErez Shitrit 				goto free_actions;
490b850a821SErez Shitrit 			}
491b850a821SErez Shitrit 
492b850a821SErez Shitrit 			id = dst->dest_attr.counter_id;
493b850a821SErez Shitrit 			tmp_action =
494b850a821SErez Shitrit 				mlx5dr_action_create_flow_counter(id);
495b850a821SErez Shitrit 			if (!tmp_action) {
496b850a821SErez Shitrit 				err = -ENOMEM;
497b850a821SErez Shitrit 				goto free_actions;
498b850a821SErez Shitrit 			}
499b850a821SErez Shitrit 
500b850a821SErez Shitrit 			fs_dr_actions[fs_dr_num_actions++] = tmp_action;
501b850a821SErez Shitrit 			actions[num_actions++] = tmp_action;
502b850a821SErez Shitrit 		}
503b850a821SErez Shitrit 	}
504b850a821SErez Shitrit 
5058920d92bSYevgeny Kliteynik 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_EXECUTE_ASO) {
5068920d92bSYevgeny Kliteynik 		if (fte->action.exe_aso.type != MLX5_EXE_ASO_FLOW_METER) {
5078920d92bSYevgeny Kliteynik 			err = -EOPNOTSUPP;
5088920d92bSYevgeny Kliteynik 			goto free_actions;
5098920d92bSYevgeny Kliteynik 		}
5108920d92bSYevgeny Kliteynik 
5118920d92bSYevgeny Kliteynik 		tmp_action =
5128920d92bSYevgeny Kliteynik 			mlx5dr_action_create_aso(domain,
5138920d92bSYevgeny Kliteynik 						 fte->action.exe_aso.object_id,
5148920d92bSYevgeny Kliteynik 						 fte->action.exe_aso.return_reg_id,
5158920d92bSYevgeny Kliteynik 						 fte->action.exe_aso.type,
5168920d92bSYevgeny Kliteynik 						 fte->action.exe_aso.flow_meter.init_color,
5178920d92bSYevgeny Kliteynik 						 fte->action.exe_aso.flow_meter.meter_idx);
5188920d92bSYevgeny Kliteynik 		if (!tmp_action) {
5198920d92bSYevgeny Kliteynik 			err = -ENOMEM;
5208920d92bSYevgeny Kliteynik 			goto free_actions;
5218920d92bSYevgeny Kliteynik 		}
5228920d92bSYevgeny Kliteynik 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
5238920d92bSYevgeny Kliteynik 		actions[num_actions++] = tmp_action;
5248920d92bSYevgeny Kliteynik 	}
5258920d92bSYevgeny Kliteynik 
5266a48faeeSMaor Gottlieb 	params.match_sz = match_sz;
5276a48faeeSMaor Gottlieb 	params.match_buf = (u64 *)fte->val;
5287ee3f6d2SAlex Vesker 	if (num_term_actions == 1) {
5290aec12d9SYevgeny Kliteynik 		if (term_actions->reformat) {
5300aec12d9SYevgeny Kliteynik 			if (num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX) {
5310aec12d9SYevgeny Kliteynik 				err = -EOPNOTSUPP;
5320aec12d9SYevgeny Kliteynik 				goto free_actions;
5330aec12d9SYevgeny Kliteynik 			}
5347ee3f6d2SAlex Vesker 			actions[num_actions++] = term_actions->reformat;
5350aec12d9SYevgeny Kliteynik 		}
5366a48faeeSMaor Gottlieb 
5370aec12d9SYevgeny Kliteynik 		if (num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX) {
5380aec12d9SYevgeny Kliteynik 			err = -EOPNOTSUPP;
5390aec12d9SYevgeny Kliteynik 			goto free_actions;
5400aec12d9SYevgeny Kliteynik 		}
5417ee3f6d2SAlex Vesker 		actions[num_actions++] = term_actions->dest;
5427ee3f6d2SAlex Vesker 	} else if (num_term_actions > 1) {
54363b85f49SYevgeny Kliteynik 		bool ignore_flow_level =
54463b85f49SYevgeny Kliteynik 			!!(fte->action.flags & FLOW_ACT_IGNORE_FLOW_LEVEL);
5452c5fc6cdSMaor Dickman 		u32 flow_source = fte->flow_context.flow_source;
54663b85f49SYevgeny Kliteynik 
5470aec12d9SYevgeny Kliteynik 		if (num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX ||
5480aec12d9SYevgeny Kliteynik 		    fs_dr_num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX) {
5490aec12d9SYevgeny Kliteynik 			err = -EOPNOTSUPP;
5500aec12d9SYevgeny Kliteynik 			goto free_actions;
5510aec12d9SYevgeny Kliteynik 		}
5527ee3f6d2SAlex Vesker 		tmp_action = mlx5dr_action_create_mult_dest_tbl(domain,
5537ee3f6d2SAlex Vesker 								term_actions,
55463b85f49SYevgeny Kliteynik 								num_term_actions,
5552c5fc6cdSMaor Dickman 								ignore_flow_level,
5562c5fc6cdSMaor Dickman 								flow_source);
5577ee3f6d2SAlex Vesker 		if (!tmp_action) {
5587ee3f6d2SAlex Vesker 			err = -EOPNOTSUPP;
5597ee3f6d2SAlex Vesker 			goto free_actions;
5607ee3f6d2SAlex Vesker 		}
5617ee3f6d2SAlex Vesker 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
5627ee3f6d2SAlex Vesker 		actions[num_actions++] = tmp_action;
5637ee3f6d2SAlex Vesker 	}
5646a48faeeSMaor Gottlieb 
5656a48faeeSMaor Gottlieb 	rule = mlx5dr_rule_create(group->fs_dr_matcher.dr_matcher,
5666a48faeeSMaor Gottlieb 				  &params,
5676a48faeeSMaor Gottlieb 				  num_actions,
56801723919SHamdan Igbaria 				  actions,
56901723919SHamdan Igbaria 				  fte->flow_context.flow_source);
5706a48faeeSMaor Gottlieb 	if (!rule) {
5716a48faeeSMaor Gottlieb 		err = -EINVAL;
5726a48faeeSMaor Gottlieb 		goto free_actions;
5736a48faeeSMaor Gottlieb 	}
5746a48faeeSMaor Gottlieb 
5757ee3f6d2SAlex Vesker 	kfree(term_actions);
5766a48faeeSMaor Gottlieb 	kfree(actions);
5777ee3f6d2SAlex Vesker 
5786a48faeeSMaor Gottlieb 	fte->fs_dr_rule.dr_rule = rule;
5796a48faeeSMaor Gottlieb 	fte->fs_dr_rule.num_actions = fs_dr_num_actions;
5806a48faeeSMaor Gottlieb 	fte->fs_dr_rule.dr_actions = fs_dr_actions;
5816a48faeeSMaor Gottlieb 
5826a48faeeSMaor Gottlieb 	return 0;
5836a48faeeSMaor Gottlieb 
5846a48faeeSMaor Gottlieb free_actions:
5857ee3f6d2SAlex Vesker 	/* Free in reverse order to handle action dependencies */
5867ee3f6d2SAlex Vesker 	for (i = fs_dr_num_actions - 1; i >= 0; i--)
5876a48faeeSMaor Gottlieb 		if (!IS_ERR_OR_NULL(fs_dr_actions[i]))
5886a48faeeSMaor Gottlieb 			mlx5dr_action_destroy(fs_dr_actions[i]);
5896a48faeeSMaor Gottlieb 
5907ee3f6d2SAlex Vesker 	kfree(term_actions);
5917ee3f6d2SAlex Vesker free_fs_dr_actions_alloc:
5926a48faeeSMaor Gottlieb 	kfree(fs_dr_actions);
5937ee3f6d2SAlex Vesker free_actions_alloc:
5947ee3f6d2SAlex Vesker 	kfree(actions);
5957ee3f6d2SAlex Vesker out_err:
5967ee3f6d2SAlex Vesker 	mlx5_core_err(dev, "Failed to create dr rule err(%d)\n", err);
5976a48faeeSMaor Gottlieb 	return err;
5986a48faeeSMaor Gottlieb }
5996a48faeeSMaor Gottlieb 
6006a48faeeSMaor Gottlieb static int mlx5_cmd_dr_packet_reformat_alloc(struct mlx5_flow_root_namespace *ns,
6013f3f05abSYevgeny Kliteynik 					     struct mlx5_pkt_reformat_params *params,
6026a48faeeSMaor Gottlieb 					     enum mlx5_flow_namespace_type namespace,
6036a48faeeSMaor Gottlieb 					     struct mlx5_pkt_reformat *pkt_reformat)
6046a48faeeSMaor Gottlieb {
6056a48faeeSMaor Gottlieb 	struct mlx5dr_domain *dr_domain = ns->fs_dr_domain.dr_domain;
6066a48faeeSMaor Gottlieb 	struct mlx5dr_action *action;
6076a48faeeSMaor Gottlieb 	int dr_reformat;
6086a48faeeSMaor Gottlieb 
6093f3f05abSYevgeny Kliteynik 	switch (params->type) {
6106a48faeeSMaor Gottlieb 	case MLX5_REFORMAT_TYPE_L2_TO_VXLAN:
6116a48faeeSMaor Gottlieb 	case MLX5_REFORMAT_TYPE_L2_TO_NVGRE:
6126a48faeeSMaor Gottlieb 	case MLX5_REFORMAT_TYPE_L2_TO_L2_TUNNEL:
6136a48faeeSMaor Gottlieb 		dr_reformat = DR_ACTION_REFORMAT_TYP_L2_TO_TNL_L2;
6146a48faeeSMaor Gottlieb 		break;
6156a48faeeSMaor Gottlieb 	case MLX5_REFORMAT_TYPE_L3_TUNNEL_TO_L2:
6166a48faeeSMaor Gottlieb 		dr_reformat = DR_ACTION_REFORMAT_TYP_TNL_L3_TO_L2;
6176a48faeeSMaor Gottlieb 		break;
6186a48faeeSMaor Gottlieb 	case MLX5_REFORMAT_TYPE_L2_TO_L3_TUNNEL:
6196a48faeeSMaor Gottlieb 		dr_reformat = DR_ACTION_REFORMAT_TYP_L2_TO_TNL_L3;
6206a48faeeSMaor Gottlieb 		break;
6217ea9b398SYevgeny Kliteynik 	case MLX5_REFORMAT_TYPE_INSERT_HDR:
6227ea9b398SYevgeny Kliteynik 		dr_reformat = DR_ACTION_REFORMAT_TYP_INSERT_HDR;
6237ea9b398SYevgeny Kliteynik 		break;
6240139145fSYevgeny Kliteynik 	case MLX5_REFORMAT_TYPE_REMOVE_HDR:
6250139145fSYevgeny Kliteynik 		dr_reformat = DR_ACTION_REFORMAT_TYP_REMOVE_HDR;
6260139145fSYevgeny Kliteynik 		break;
6276a48faeeSMaor Gottlieb 	default:
6286a48faeeSMaor Gottlieb 		mlx5_core_err(ns->dev, "Packet-reformat not supported(%d)\n",
6293f3f05abSYevgeny Kliteynik 			      params->type);
6306a48faeeSMaor Gottlieb 		return -EOPNOTSUPP;
6316a48faeeSMaor Gottlieb 	}
6326a48faeeSMaor Gottlieb 
6336a48faeeSMaor Gottlieb 	action = mlx5dr_action_create_packet_reformat(dr_domain,
6346a48faeeSMaor Gottlieb 						      dr_reformat,
6353f3f05abSYevgeny Kliteynik 						      params->param_0,
6363f3f05abSYevgeny Kliteynik 						      params->param_1,
6373f3f05abSYevgeny Kliteynik 						      params->size,
6383f3f05abSYevgeny Kliteynik 						      params->data);
6396a48faeeSMaor Gottlieb 	if (!action) {
6406a48faeeSMaor Gottlieb 		mlx5_core_err(ns->dev, "Failed allocating packet-reformat action\n");
6416a48faeeSMaor Gottlieb 		return -EINVAL;
6426a48faeeSMaor Gottlieb 	}
6436a48faeeSMaor Gottlieb 
6446a48faeeSMaor Gottlieb 	pkt_reformat->action.dr_action = action;
6456a48faeeSMaor Gottlieb 
6466a48faeeSMaor Gottlieb 	return 0;
6476a48faeeSMaor Gottlieb }
6486a48faeeSMaor Gottlieb 
6496a48faeeSMaor Gottlieb static void mlx5_cmd_dr_packet_reformat_dealloc(struct mlx5_flow_root_namespace *ns,
6506a48faeeSMaor Gottlieb 						struct mlx5_pkt_reformat *pkt_reformat)
6516a48faeeSMaor Gottlieb {
6526a48faeeSMaor Gottlieb 	mlx5dr_action_destroy(pkt_reformat->action.dr_action);
6536a48faeeSMaor Gottlieb }
6546a48faeeSMaor Gottlieb 
6556a48faeeSMaor Gottlieb static int mlx5_cmd_dr_modify_header_alloc(struct mlx5_flow_root_namespace *ns,
6566a48faeeSMaor Gottlieb 					   u8 namespace, u8 num_actions,
6576a48faeeSMaor Gottlieb 					   void *modify_actions,
6586a48faeeSMaor Gottlieb 					   struct mlx5_modify_hdr *modify_hdr)
6596a48faeeSMaor Gottlieb {
6606a48faeeSMaor Gottlieb 	struct mlx5dr_domain *dr_domain = ns->fs_dr_domain.dr_domain;
6616a48faeeSMaor Gottlieb 	struct mlx5dr_action *action;
6626a48faeeSMaor Gottlieb 	size_t actions_sz;
6636a48faeeSMaor Gottlieb 
664d65dbedfSHuy Nguyen 	actions_sz = MLX5_UN_SZ_BYTES(set_add_copy_action_in_auto) *
6656a48faeeSMaor Gottlieb 		num_actions;
6666a48faeeSMaor Gottlieb 	action = mlx5dr_action_create_modify_header(dr_domain, 0,
6676a48faeeSMaor Gottlieb 						    actions_sz,
6686a48faeeSMaor Gottlieb 						    modify_actions);
6696a48faeeSMaor Gottlieb 	if (!action) {
6706a48faeeSMaor Gottlieb 		mlx5_core_err(ns->dev, "Failed allocating modify-header action\n");
6716a48faeeSMaor Gottlieb 		return -EINVAL;
6726a48faeeSMaor Gottlieb 	}
6736a48faeeSMaor Gottlieb 
6746a48faeeSMaor Gottlieb 	modify_hdr->action.dr_action = action;
6756a48faeeSMaor Gottlieb 
6766a48faeeSMaor Gottlieb 	return 0;
6776a48faeeSMaor Gottlieb }
6786a48faeeSMaor Gottlieb 
6796a48faeeSMaor Gottlieb static void mlx5_cmd_dr_modify_header_dealloc(struct mlx5_flow_root_namespace *ns,
6806a48faeeSMaor Gottlieb 					      struct mlx5_modify_hdr *modify_hdr)
6816a48faeeSMaor Gottlieb {
6826a48faeeSMaor Gottlieb 	mlx5dr_action_destroy(modify_hdr->action.dr_action);
6836a48faeeSMaor Gottlieb }
6846a48faeeSMaor Gottlieb 
685e7e2519eSMaor Gottlieb static int
686e7e2519eSMaor Gottlieb mlx5_cmd_dr_destroy_match_definer(struct mlx5_flow_root_namespace *ns,
687e7e2519eSMaor Gottlieb 				  int definer_id)
688e7e2519eSMaor Gottlieb {
689e7e2519eSMaor Gottlieb 	return -EOPNOTSUPP;
690e7e2519eSMaor Gottlieb }
691e7e2519eSMaor Gottlieb 
692e7e2519eSMaor Gottlieb static int mlx5_cmd_dr_create_match_definer(struct mlx5_flow_root_namespace *ns,
693e7e2519eSMaor Gottlieb 					    u16 format_id, u32 *match_mask)
694e7e2519eSMaor Gottlieb {
695e7e2519eSMaor Gottlieb 	return -EOPNOTSUPP;
696e7e2519eSMaor Gottlieb }
697e7e2519eSMaor Gottlieb 
6986a48faeeSMaor Gottlieb static int mlx5_cmd_dr_delete_fte(struct mlx5_flow_root_namespace *ns,
6996a48faeeSMaor Gottlieb 				  struct mlx5_flow_table *ft,
7006a48faeeSMaor Gottlieb 				  struct fs_fte *fte)
7016a48faeeSMaor Gottlieb {
7026a48faeeSMaor Gottlieb 	struct mlx5_fs_dr_rule *rule = &fte->fs_dr_rule;
7036a48faeeSMaor Gottlieb 	int err;
7046a48faeeSMaor Gottlieb 	int i;
7056a48faeeSMaor Gottlieb 
706*0a8c20e2SYevgeny Kliteynik 	if (dr_is_fw_term_table(ft))
7076a48faeeSMaor Gottlieb 		return mlx5_fs_cmd_get_fw_cmds()->delete_fte(ns, ft, fte);
7086a48faeeSMaor Gottlieb 
7096a48faeeSMaor Gottlieb 	err = mlx5dr_rule_destroy(rule->dr_rule);
7106a48faeeSMaor Gottlieb 	if (err)
7116a48faeeSMaor Gottlieb 		return err;
7126a48faeeSMaor Gottlieb 
7137ee3f6d2SAlex Vesker 	/* Free in reverse order to handle action dependencies */
7147ee3f6d2SAlex Vesker 	for (i = rule->num_actions - 1; i >= 0; i--)
7156a48faeeSMaor Gottlieb 		if (!IS_ERR_OR_NULL(rule->dr_actions[i]))
7166a48faeeSMaor Gottlieb 			mlx5dr_action_destroy(rule->dr_actions[i]);
7176a48faeeSMaor Gottlieb 
7186a48faeeSMaor Gottlieb 	kfree(rule->dr_actions);
7196a48faeeSMaor Gottlieb 	return 0;
7206a48faeeSMaor Gottlieb }
7216a48faeeSMaor Gottlieb 
722a2ebfbb7SYevgeny Kliteynik static int mlx5_cmd_dr_update_fte(struct mlx5_flow_root_namespace *ns,
723a2ebfbb7SYevgeny Kliteynik 				  struct mlx5_flow_table *ft,
724a2ebfbb7SYevgeny Kliteynik 				  struct mlx5_flow_group *group,
725a2ebfbb7SYevgeny Kliteynik 				  int modify_mask,
726a2ebfbb7SYevgeny Kliteynik 				  struct fs_fte *fte)
727a2ebfbb7SYevgeny Kliteynik {
728a2ebfbb7SYevgeny Kliteynik 	struct fs_fte fte_tmp = {};
729a2ebfbb7SYevgeny Kliteynik 	int ret;
730a2ebfbb7SYevgeny Kliteynik 
731*0a8c20e2SYevgeny Kliteynik 	if (dr_is_fw_term_table(ft))
732a2ebfbb7SYevgeny Kliteynik 		return mlx5_fs_cmd_get_fw_cmds()->update_fte(ns, ft, group, modify_mask, fte);
733a2ebfbb7SYevgeny Kliteynik 
734a2ebfbb7SYevgeny Kliteynik 	/* Backup current dr rule details */
735a2ebfbb7SYevgeny Kliteynik 	fte_tmp.fs_dr_rule = fte->fs_dr_rule;
736a2ebfbb7SYevgeny Kliteynik 	memset(&fte->fs_dr_rule, 0, sizeof(struct mlx5_fs_dr_rule));
737a2ebfbb7SYevgeny Kliteynik 
738a2ebfbb7SYevgeny Kliteynik 	/* First add the new updated rule, then delete the old rule */
739a2ebfbb7SYevgeny Kliteynik 	ret = mlx5_cmd_dr_create_fte(ns, ft, group, fte);
740a2ebfbb7SYevgeny Kliteynik 	if (ret)
741a2ebfbb7SYevgeny Kliteynik 		goto restore_fte;
742a2ebfbb7SYevgeny Kliteynik 
743a2ebfbb7SYevgeny Kliteynik 	ret = mlx5_cmd_dr_delete_fte(ns, ft, &fte_tmp);
744a2ebfbb7SYevgeny Kliteynik 	WARN_ONCE(ret, "dr update fte duplicate rule deletion failed\n");
745a2ebfbb7SYevgeny Kliteynik 	return ret;
746a2ebfbb7SYevgeny Kliteynik 
747a2ebfbb7SYevgeny Kliteynik restore_fte:
748a2ebfbb7SYevgeny Kliteynik 	fte->fs_dr_rule = fte_tmp.fs_dr_rule;
749a2ebfbb7SYevgeny Kliteynik 	return ret;
750a2ebfbb7SYevgeny Kliteynik }
751a2ebfbb7SYevgeny Kliteynik 
7526a48faeeSMaor Gottlieb static int mlx5_cmd_dr_set_peer(struct mlx5_flow_root_namespace *ns,
7536a48faeeSMaor Gottlieb 				struct mlx5_flow_root_namespace *peer_ns)
7546a48faeeSMaor Gottlieb {
7556a48faeeSMaor Gottlieb 	struct mlx5dr_domain *peer_domain = NULL;
7566a48faeeSMaor Gottlieb 
7576a48faeeSMaor Gottlieb 	if (peer_ns)
7586a48faeeSMaor Gottlieb 		peer_domain = peer_ns->fs_dr_domain.dr_domain;
7596a48faeeSMaor Gottlieb 	mlx5dr_domain_set_peer(ns->fs_dr_domain.dr_domain,
7606a48faeeSMaor Gottlieb 			       peer_domain);
7616a48faeeSMaor Gottlieb 	return 0;
7626a48faeeSMaor Gottlieb }
7636a48faeeSMaor Gottlieb 
7646a48faeeSMaor Gottlieb static int mlx5_cmd_dr_create_ns(struct mlx5_flow_root_namespace *ns)
7656a48faeeSMaor Gottlieb {
7666a48faeeSMaor Gottlieb 	ns->fs_dr_domain.dr_domain =
7676a48faeeSMaor Gottlieb 		mlx5dr_domain_create(ns->dev,
7686a48faeeSMaor Gottlieb 				     MLX5DR_DOMAIN_TYPE_FDB);
7696a48faeeSMaor Gottlieb 	if (!ns->fs_dr_domain.dr_domain) {
7706a48faeeSMaor Gottlieb 		mlx5_core_err(ns->dev, "Failed to create dr flow namespace\n");
7716a48faeeSMaor Gottlieb 		return -EOPNOTSUPP;
7726a48faeeSMaor Gottlieb 	}
7736a48faeeSMaor Gottlieb 	return 0;
7746a48faeeSMaor Gottlieb }
7756a48faeeSMaor Gottlieb 
7766a48faeeSMaor Gottlieb static int mlx5_cmd_dr_destroy_ns(struct mlx5_flow_root_namespace *ns)
7776a48faeeSMaor Gottlieb {
7786a48faeeSMaor Gottlieb 	return mlx5dr_domain_destroy(ns->fs_dr_domain.dr_domain);
7796a48faeeSMaor Gottlieb }
7806a48faeeSMaor Gottlieb 
7818348b71cSDima Chumak static u32 mlx5_cmd_dr_get_capabilities(struct mlx5_flow_root_namespace *ns,
7828348b71cSDima Chumak 					enum fs_flow_table_type ft_type)
7838348b71cSDima Chumak {
7848348b71cSDima Chumak 	if (ft_type != FS_FT_FDB ||
7856862c787SYevgeny Kliteynik 	    MLX5_CAP_GEN(ns->dev, steering_format_version) == MLX5_STEERING_FORMAT_CONNECTX_5)
7868348b71cSDima Chumak 		return 0;
7878348b71cSDima Chumak 
7888348b71cSDima Chumak 	return MLX5_FLOW_STEERING_CAP_VLAN_PUSH_ON_RX | MLX5_FLOW_STEERING_CAP_VLAN_POP_ON_TX;
7898348b71cSDima Chumak }
7908348b71cSDima Chumak 
7916a48faeeSMaor Gottlieb bool mlx5_fs_dr_is_supported(struct mlx5_core_dev *dev)
7926a48faeeSMaor Gottlieb {
7936a48faeeSMaor Gottlieb 	return mlx5dr_is_supported(dev);
7946a48faeeSMaor Gottlieb }
7956a48faeeSMaor Gottlieb 
7966a48faeeSMaor Gottlieb static const struct mlx5_flow_cmds mlx5_flow_cmds_dr = {
7976a48faeeSMaor Gottlieb 	.create_flow_table = mlx5_cmd_dr_create_flow_table,
7986a48faeeSMaor Gottlieb 	.destroy_flow_table = mlx5_cmd_dr_destroy_flow_table,
7996a48faeeSMaor Gottlieb 	.modify_flow_table = mlx5_cmd_dr_modify_flow_table,
8006a48faeeSMaor Gottlieb 	.create_flow_group = mlx5_cmd_dr_create_flow_group,
8016a48faeeSMaor Gottlieb 	.destroy_flow_group = mlx5_cmd_dr_destroy_flow_group,
8026a48faeeSMaor Gottlieb 	.create_fte = mlx5_cmd_dr_create_fte,
8036a48faeeSMaor Gottlieb 	.update_fte = mlx5_cmd_dr_update_fte,
8046a48faeeSMaor Gottlieb 	.delete_fte = mlx5_cmd_dr_delete_fte,
8056a48faeeSMaor Gottlieb 	.update_root_ft = mlx5_cmd_dr_update_root_ft,
8066a48faeeSMaor Gottlieb 	.packet_reformat_alloc = mlx5_cmd_dr_packet_reformat_alloc,
8076a48faeeSMaor Gottlieb 	.packet_reformat_dealloc = mlx5_cmd_dr_packet_reformat_dealloc,
8086a48faeeSMaor Gottlieb 	.modify_header_alloc = mlx5_cmd_dr_modify_header_alloc,
8096a48faeeSMaor Gottlieb 	.modify_header_dealloc = mlx5_cmd_dr_modify_header_dealloc,
810e7e2519eSMaor Gottlieb 	.create_match_definer = mlx5_cmd_dr_create_match_definer,
811e7e2519eSMaor Gottlieb 	.destroy_match_definer = mlx5_cmd_dr_destroy_match_definer,
8126a48faeeSMaor Gottlieb 	.set_peer = mlx5_cmd_dr_set_peer,
8136a48faeeSMaor Gottlieb 	.create_ns = mlx5_cmd_dr_create_ns,
8146a48faeeSMaor Gottlieb 	.destroy_ns = mlx5_cmd_dr_destroy_ns,
8158348b71cSDima Chumak 	.get_capabilities = mlx5_cmd_dr_get_capabilities,
8166a48faeeSMaor Gottlieb };
8176a48faeeSMaor Gottlieb 
8186a48faeeSMaor Gottlieb const struct mlx5_flow_cmds *mlx5_fs_cmd_get_dr_cmds(void)
8196a48faeeSMaor Gottlieb {
8206a48faeeSMaor Gottlieb 		return &mlx5_flow_cmds_dr;
8216a48faeeSMaor Gottlieb }
822