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"
106a48faeeSMaor Gottlieb 
116a48faeeSMaor Gottlieb static bool mlx5_dr_is_fw_table(u32 flags)
126a48faeeSMaor Gottlieb {
136a48faeeSMaor Gottlieb 	if (flags & MLX5_FLOW_TABLE_TERMINATION)
146a48faeeSMaor Gottlieb 		return true;
156a48faeeSMaor Gottlieb 
166a48faeeSMaor Gottlieb 	return false;
176a48faeeSMaor Gottlieb }
186a48faeeSMaor Gottlieb 
196a48faeeSMaor Gottlieb static int mlx5_cmd_dr_update_root_ft(struct mlx5_flow_root_namespace *ns,
206a48faeeSMaor Gottlieb 				      struct mlx5_flow_table *ft,
216a48faeeSMaor Gottlieb 				      u32 underlay_qpn,
226a48faeeSMaor Gottlieb 				      bool disconnect)
236a48faeeSMaor Gottlieb {
246a48faeeSMaor Gottlieb 	return mlx5_fs_cmd_get_fw_cmds()->update_root_ft(ns, ft, underlay_qpn,
256a48faeeSMaor Gottlieb 							 disconnect);
266a48faeeSMaor Gottlieb }
276a48faeeSMaor Gottlieb 
286a48faeeSMaor Gottlieb static int set_miss_action(struct mlx5_flow_root_namespace *ns,
296a48faeeSMaor Gottlieb 			   struct mlx5_flow_table *ft,
306a48faeeSMaor Gottlieb 			   struct mlx5_flow_table *next_ft)
316a48faeeSMaor Gottlieb {
326a48faeeSMaor Gottlieb 	struct mlx5dr_action *old_miss_action;
336a48faeeSMaor Gottlieb 	struct mlx5dr_action *action = NULL;
346a48faeeSMaor Gottlieb 	struct mlx5dr_table *next_tbl;
356a48faeeSMaor Gottlieb 	int err;
366a48faeeSMaor Gottlieb 
376a48faeeSMaor Gottlieb 	next_tbl = next_ft ? next_ft->fs_dr_table.dr_table : NULL;
386a48faeeSMaor Gottlieb 	if (next_tbl) {
396a48faeeSMaor Gottlieb 		action = mlx5dr_action_create_dest_table(next_tbl);
406a48faeeSMaor Gottlieb 		if (!action)
416a48faeeSMaor Gottlieb 			return -EINVAL;
426a48faeeSMaor Gottlieb 	}
436a48faeeSMaor Gottlieb 	old_miss_action = ft->fs_dr_table.miss_action;
446a48faeeSMaor Gottlieb 	err = mlx5dr_table_set_miss_action(ft->fs_dr_table.dr_table, action);
456a48faeeSMaor Gottlieb 	if (err && action) {
466a48faeeSMaor Gottlieb 		err = mlx5dr_action_destroy(action);
476a48faeeSMaor Gottlieb 		if (err) {
486a48faeeSMaor Gottlieb 			action = NULL;
496a48faeeSMaor Gottlieb 			mlx5_core_err(ns->dev, "Failed to destroy action (%d)\n",
506a48faeeSMaor Gottlieb 				      err);
516a48faeeSMaor Gottlieb 		}
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,
6604745afbSPaul Blakey 					 unsigned int size,
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 
736a48faeeSMaor Gottlieb 	if (mlx5_dr_is_fw_table(ft->flags))
746a48faeeSMaor Gottlieb 		return mlx5_fs_cmd_get_fw_cmds()->create_flow_table(ns, ft,
7504745afbSPaul Blakey 								    size,
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 
8313a7e459SErez Shitrit 	tbl = mlx5dr_table_create(ns->fs_dr_domain.dr_domain, ft->level, flags);
846a48faeeSMaor Gottlieb 	if (!tbl) {
856a48faeeSMaor Gottlieb 		mlx5_core_err(ns->dev, "Failed creating dr flow_table\n");
866a48faeeSMaor Gottlieb 		return -EINVAL;
876a48faeeSMaor Gottlieb 	}
886a48faeeSMaor Gottlieb 
896a48faeeSMaor Gottlieb 	ft->fs_dr_table.dr_table = tbl;
906a48faeeSMaor Gottlieb 	ft->id = mlx5dr_table_get_id(tbl);
916a48faeeSMaor Gottlieb 
926a48faeeSMaor Gottlieb 	if (next_ft) {
936a48faeeSMaor Gottlieb 		err = set_miss_action(ns, ft, next_ft);
946a48faeeSMaor Gottlieb 		if (err) {
956a48faeeSMaor Gottlieb 			mlx5dr_table_destroy(tbl);
966a48faeeSMaor Gottlieb 			ft->fs_dr_table.dr_table = NULL;
976a48faeeSMaor Gottlieb 			return err;
986a48faeeSMaor Gottlieb 		}
996a48faeeSMaor Gottlieb 	}
1006a48faeeSMaor Gottlieb 
1019e117998SPaul Blakey 	ft->max_fte = INT_MAX;
10204745afbSPaul Blakey 
1036a48faeeSMaor Gottlieb 	return 0;
1046a48faeeSMaor Gottlieb }
1056a48faeeSMaor Gottlieb 
1066a48faeeSMaor Gottlieb static int mlx5_cmd_dr_destroy_flow_table(struct mlx5_flow_root_namespace *ns,
1076a48faeeSMaor Gottlieb 					  struct mlx5_flow_table *ft)
1086a48faeeSMaor Gottlieb {
1096a48faeeSMaor Gottlieb 	struct mlx5dr_action *action = ft->fs_dr_table.miss_action;
1106a48faeeSMaor Gottlieb 	int err;
1116a48faeeSMaor Gottlieb 
1126a48faeeSMaor Gottlieb 	if (mlx5_dr_is_fw_table(ft->flags))
1136a48faeeSMaor Gottlieb 		return mlx5_fs_cmd_get_fw_cmds()->destroy_flow_table(ns, ft);
1146a48faeeSMaor Gottlieb 
1156a48faeeSMaor Gottlieb 	err = mlx5dr_table_destroy(ft->fs_dr_table.dr_table);
1166a48faeeSMaor Gottlieb 	if (err) {
1176a48faeeSMaor Gottlieb 		mlx5_core_err(ns->dev, "Failed to destroy flow_table (%d)\n",
1186a48faeeSMaor Gottlieb 			      err);
1196a48faeeSMaor Gottlieb 		return err;
1206a48faeeSMaor Gottlieb 	}
1216a48faeeSMaor Gottlieb 	if (action) {
1226a48faeeSMaor Gottlieb 		err = mlx5dr_action_destroy(action);
1236a48faeeSMaor Gottlieb 		if (err) {
1246a48faeeSMaor Gottlieb 			mlx5_core_err(ns->dev, "Failed to destroy action(%d)\n",
1256a48faeeSMaor Gottlieb 				      err);
1266a48faeeSMaor Gottlieb 			return err;
1276a48faeeSMaor Gottlieb 		}
1286a48faeeSMaor Gottlieb 	}
1296a48faeeSMaor Gottlieb 
1306a48faeeSMaor Gottlieb 	return err;
1316a48faeeSMaor Gottlieb }
1326a48faeeSMaor Gottlieb 
1336a48faeeSMaor Gottlieb static int mlx5_cmd_dr_modify_flow_table(struct mlx5_flow_root_namespace *ns,
1346a48faeeSMaor Gottlieb 					 struct mlx5_flow_table *ft,
1356a48faeeSMaor Gottlieb 					 struct mlx5_flow_table *next_ft)
1366a48faeeSMaor Gottlieb {
137a01a43faSYevgeny Kliteynik 	if (mlx5_dr_is_fw_table(ft->flags))
138a01a43faSYevgeny Kliteynik 		return mlx5_fs_cmd_get_fw_cmds()->modify_flow_table(ns, ft, next_ft);
139a01a43faSYevgeny Kliteynik 
1406a48faeeSMaor Gottlieb 	return set_miss_action(ns, ft, next_ft);
1416a48faeeSMaor Gottlieb }
1426a48faeeSMaor Gottlieb 
1436a48faeeSMaor Gottlieb static int mlx5_cmd_dr_create_flow_group(struct mlx5_flow_root_namespace *ns,
1446a48faeeSMaor Gottlieb 					 struct mlx5_flow_table *ft,
1456a48faeeSMaor Gottlieb 					 u32 *in,
1466a48faeeSMaor Gottlieb 					 struct mlx5_flow_group *fg)
1476a48faeeSMaor Gottlieb {
1486a48faeeSMaor Gottlieb 	struct mlx5dr_matcher *matcher;
149f6409299SHamdan Igbaria 	u32 priority = MLX5_GET(create_flow_group_in, in,
1506a48faeeSMaor Gottlieb 				start_flow_index);
1516a48faeeSMaor Gottlieb 	u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
1526a48faeeSMaor Gottlieb 					    in,
1536a48faeeSMaor Gottlieb 					    match_criteria_enable);
1546a48faeeSMaor Gottlieb 	struct mlx5dr_match_parameters mask;
1556a48faeeSMaor Gottlieb 
1566a48faeeSMaor Gottlieb 	if (mlx5_dr_is_fw_table(ft->flags))
1576a48faeeSMaor Gottlieb 		return mlx5_fs_cmd_get_fw_cmds()->create_flow_group(ns, ft, in,
1586a48faeeSMaor Gottlieb 								    fg);
1596a48faeeSMaor Gottlieb 
1606a48faeeSMaor Gottlieb 	mask.match_buf = MLX5_ADDR_OF(create_flow_group_in,
1616a48faeeSMaor Gottlieb 				      in, match_criteria);
1626a48faeeSMaor Gottlieb 	mask.match_sz = sizeof(fg->mask.match_criteria);
1636a48faeeSMaor Gottlieb 
1646a48faeeSMaor Gottlieb 	matcher = mlx5dr_matcher_create(ft->fs_dr_table.dr_table,
1656a48faeeSMaor Gottlieb 					priority,
1666a48faeeSMaor Gottlieb 					match_criteria_enable,
1676a48faeeSMaor Gottlieb 					&mask);
1686a48faeeSMaor Gottlieb 	if (!matcher) {
1696a48faeeSMaor Gottlieb 		mlx5_core_err(ns->dev, "Failed creating matcher\n");
1706a48faeeSMaor Gottlieb 		return -EINVAL;
1716a48faeeSMaor Gottlieb 	}
1726a48faeeSMaor Gottlieb 
1736a48faeeSMaor Gottlieb 	fg->fs_dr_matcher.dr_matcher = matcher;
1746a48faeeSMaor Gottlieb 	return 0;
1756a48faeeSMaor Gottlieb }
1766a48faeeSMaor Gottlieb 
1776a48faeeSMaor Gottlieb static int mlx5_cmd_dr_destroy_flow_group(struct mlx5_flow_root_namespace *ns,
1786a48faeeSMaor Gottlieb 					  struct mlx5_flow_table *ft,
1796a48faeeSMaor Gottlieb 					  struct mlx5_flow_group *fg)
1806a48faeeSMaor Gottlieb {
1816a48faeeSMaor Gottlieb 	if (mlx5_dr_is_fw_table(ft->flags))
1826a48faeeSMaor Gottlieb 		return mlx5_fs_cmd_get_fw_cmds()->destroy_flow_group(ns, ft, fg);
1836a48faeeSMaor Gottlieb 
1846a48faeeSMaor Gottlieb 	return mlx5dr_matcher_destroy(fg->fs_dr_matcher.dr_matcher);
1856a48faeeSMaor Gottlieb }
1866a48faeeSMaor Gottlieb 
1876a48faeeSMaor Gottlieb static struct mlx5dr_action *create_vport_action(struct mlx5dr_domain *domain,
1886a48faeeSMaor Gottlieb 						 struct mlx5_flow_rule *dst)
1896a48faeeSMaor Gottlieb {
1906a48faeeSMaor Gottlieb 	struct mlx5_flow_destination *dest_attr = &dst->dest_attr;
1916a48faeeSMaor Gottlieb 
1926a48faeeSMaor Gottlieb 	return mlx5dr_action_create_dest_vport(domain, dest_attr->vport.num,
1936a48faeeSMaor Gottlieb 					       dest_attr->vport.flags &
1946a48faeeSMaor Gottlieb 					       MLX5_FLOW_DEST_VPORT_VHCA_ID,
1956a48faeeSMaor Gottlieb 					       dest_attr->vport.vhca_id);
1966a48faeeSMaor Gottlieb }
1976a48faeeSMaor Gottlieb 
198e3a0f40bSYevgeny Kliteynik static struct mlx5dr_action *create_uplink_action(struct mlx5dr_domain *domain,
199e3a0f40bSYevgeny Kliteynik 						  struct mlx5_flow_rule *dst)
200e3a0f40bSYevgeny Kliteynik {
201e3a0f40bSYevgeny Kliteynik 	struct mlx5_flow_destination *dest_attr = &dst->dest_attr;
202e3a0f40bSYevgeny Kliteynik 
203e3a0f40bSYevgeny Kliteynik 	return mlx5dr_action_create_dest_vport(domain, MLX5_VPORT_UPLINK, 1,
204e3a0f40bSYevgeny Kliteynik 					       dest_attr->vport.vhca_id);
205e3a0f40bSYevgeny Kliteynik }
206e3a0f40bSYevgeny Kliteynik 
207aec292eeSAlex Vesker static struct mlx5dr_action *create_ft_action(struct mlx5dr_domain *domain,
2086a48faeeSMaor Gottlieb 					      struct mlx5_flow_rule *dst)
2096a48faeeSMaor Gottlieb {
2106a48faeeSMaor Gottlieb 	struct mlx5_flow_table *dest_ft = dst->dest_attr.ft;
2116a48faeeSMaor Gottlieb 
2126a48faeeSMaor Gottlieb 	if (mlx5_dr_is_fw_table(dest_ft->flags))
213aec292eeSAlex Vesker 		return mlx5dr_action_create_dest_flow_fw_table(domain, dest_ft);
2146a48faeeSMaor Gottlieb 	return mlx5dr_action_create_dest_table(dest_ft->fs_dr_table.dr_table);
2156a48faeeSMaor Gottlieb }
2166a48faeeSMaor Gottlieb 
2176a48faeeSMaor Gottlieb static struct mlx5dr_action *create_action_push_vlan(struct mlx5dr_domain *domain,
2186a48faeeSMaor Gottlieb 						     struct mlx5_fs_vlan *vlan)
2196a48faeeSMaor Gottlieb {
2206a48faeeSMaor Gottlieb 	u16 n_ethtype = vlan->ethtype;
2216a48faeeSMaor Gottlieb 	u8  prio = vlan->prio;
2226a48faeeSMaor Gottlieb 	u16 vid = vlan->vid;
2236a48faeeSMaor Gottlieb 	u32 vlan_hdr;
2246a48faeeSMaor Gottlieb 
2256a48faeeSMaor Gottlieb 	vlan_hdr = (u32)n_ethtype << 16 | (u32)(prio) << 12 |  (u32)vid;
2266a48faeeSMaor Gottlieb 	return mlx5dr_action_create_push_vlan(domain, htonl(vlan_hdr));
2276a48faeeSMaor Gottlieb }
2286a48faeeSMaor Gottlieb 
2297ee3f6d2SAlex Vesker static bool contain_vport_reformat_action(struct mlx5_flow_rule *dst)
2307ee3f6d2SAlex Vesker {
231e3a0f40bSYevgeny Kliteynik 	return (dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_VPORT ||
232e3a0f40bSYevgeny Kliteynik 		dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_UPLINK) &&
2337ee3f6d2SAlex Vesker 		dst->dest_attr.vport.flags & MLX5_FLOW_DEST_VPORT_REFORMAT_ID;
2347ee3f6d2SAlex Vesker }
2357ee3f6d2SAlex Vesker 
2360aec12d9SYevgeny Kliteynik /* We want to support a rule with 32 destinations, which means we need to
2370aec12d9SYevgeny Kliteynik  * account for 32 destinations plus usually a counter plus one more action
2380aec12d9SYevgeny Kliteynik  * for a multi-destination flow table.
2390aec12d9SYevgeny Kliteynik  */
2400aec12d9SYevgeny Kliteynik #define MLX5_FLOW_CONTEXT_ACTION_MAX  34
2416a48faeeSMaor Gottlieb static int mlx5_cmd_dr_create_fte(struct mlx5_flow_root_namespace *ns,
2426a48faeeSMaor Gottlieb 				  struct mlx5_flow_table *ft,
2436a48faeeSMaor Gottlieb 				  struct mlx5_flow_group *group,
2446a48faeeSMaor Gottlieb 				  struct fs_fte *fte)
2456a48faeeSMaor Gottlieb {
2466a48faeeSMaor Gottlieb 	struct mlx5dr_domain *domain = ns->fs_dr_domain.dr_domain;
2477ee3f6d2SAlex Vesker 	struct mlx5dr_action_dest *term_actions;
2486a48faeeSMaor Gottlieb 	struct mlx5dr_match_parameters params;
2496a48faeeSMaor Gottlieb 	struct mlx5_core_dev *dev = ns->dev;
2506a48faeeSMaor Gottlieb 	struct mlx5dr_action **fs_dr_actions;
2516a48faeeSMaor Gottlieb 	struct mlx5dr_action *tmp_action;
2526a48faeeSMaor Gottlieb 	struct mlx5dr_action **actions;
2536a48faeeSMaor Gottlieb 	bool delay_encap_set = false;
2546a48faeeSMaor Gottlieb 	struct mlx5dr_rule *rule;
2556a48faeeSMaor Gottlieb 	struct mlx5_flow_rule *dst;
2566a48faeeSMaor Gottlieb 	int fs_dr_num_actions = 0;
2577ee3f6d2SAlex Vesker 	int num_term_actions = 0;
2586a48faeeSMaor Gottlieb 	int num_actions = 0;
2596a48faeeSMaor Gottlieb 	size_t match_sz;
2606a48faeeSMaor Gottlieb 	int err = 0;
2616a48faeeSMaor Gottlieb 	int i;
2626a48faeeSMaor Gottlieb 
2636a48faeeSMaor Gottlieb 	if (mlx5_dr_is_fw_table(ft->flags))
2646a48faeeSMaor Gottlieb 		return mlx5_fs_cmd_get_fw_cmds()->create_fte(ns, ft, group, fte);
2656a48faeeSMaor Gottlieb 
2666a48faeeSMaor Gottlieb 	actions = kcalloc(MLX5_FLOW_CONTEXT_ACTION_MAX, sizeof(*actions),
2676a48faeeSMaor Gottlieb 			  GFP_KERNEL);
2687ee3f6d2SAlex Vesker 	if (!actions) {
2697ee3f6d2SAlex Vesker 		err = -ENOMEM;
2707ee3f6d2SAlex Vesker 		goto out_err;
2717ee3f6d2SAlex Vesker 	}
2726a48faeeSMaor Gottlieb 
2736a48faeeSMaor Gottlieb 	fs_dr_actions = kcalloc(MLX5_FLOW_CONTEXT_ACTION_MAX,
2746a48faeeSMaor Gottlieb 				sizeof(*fs_dr_actions), GFP_KERNEL);
2756a48faeeSMaor Gottlieb 	if (!fs_dr_actions) {
2767ee3f6d2SAlex Vesker 		err = -ENOMEM;
2777ee3f6d2SAlex Vesker 		goto free_actions_alloc;
2787ee3f6d2SAlex Vesker 	}
2797ee3f6d2SAlex Vesker 
2807ee3f6d2SAlex Vesker 	term_actions = kcalloc(MLX5_FLOW_CONTEXT_ACTION_MAX,
2817ee3f6d2SAlex Vesker 			       sizeof(*term_actions), GFP_KERNEL);
2827ee3f6d2SAlex Vesker 	if (!term_actions) {
2837ee3f6d2SAlex Vesker 		err = -ENOMEM;
2847ee3f6d2SAlex Vesker 		goto free_fs_dr_actions_alloc;
2856a48faeeSMaor Gottlieb 	}
2866a48faeeSMaor Gottlieb 
2876a48faeeSMaor Gottlieb 	match_sz = sizeof(fte->val);
2886a48faeeSMaor Gottlieb 
2897ee3f6d2SAlex Vesker 	/* Drop reformat action bit if destination vport set with reformat */
2907ee3f6d2SAlex Vesker 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
2917ee3f6d2SAlex Vesker 		list_for_each_entry(dst, &fte->node.children, node.list) {
2927ee3f6d2SAlex Vesker 			if (!contain_vport_reformat_action(dst))
2937ee3f6d2SAlex Vesker 				continue;
2947ee3f6d2SAlex Vesker 
2957ee3f6d2SAlex Vesker 			fte->action.action &= ~MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
2967ee3f6d2SAlex Vesker 			break;
2977ee3f6d2SAlex Vesker 		}
2987ee3f6d2SAlex Vesker 	}
2997ee3f6d2SAlex Vesker 
3006a48faeeSMaor Gottlieb 	/* The order of the actions are must to be keep, only the following
3016a48faeeSMaor Gottlieb 	 * order is supported by SW steering:
302b2064909SAlex Vesker 	 * TX: modify header -> push vlan -> encap
3036a48faeeSMaor Gottlieb 	 * RX: decap -> pop vlan -> modify header
3046a48faeeSMaor Gottlieb 	 */
3056a48faeeSMaor Gottlieb 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_DECAP) {
3066a48faeeSMaor Gottlieb 		enum mlx5dr_action_reformat_type decap_type =
3076a48faeeSMaor Gottlieb 			DR_ACTION_REFORMAT_TYP_TNL_L2_TO_L2;
3086a48faeeSMaor Gottlieb 
3096a48faeeSMaor Gottlieb 		tmp_action = mlx5dr_action_create_packet_reformat(domain,
3103f3f05abSYevgeny Kliteynik 								  decap_type,
3113f3f05abSYevgeny Kliteynik 								  0, 0, 0,
3126a48faeeSMaor Gottlieb 								  NULL);
3136a48faeeSMaor Gottlieb 		if (!tmp_action) {
3146a48faeeSMaor Gottlieb 			err = -ENOMEM;
3156a48faeeSMaor Gottlieb 			goto free_actions;
3166a48faeeSMaor Gottlieb 		}
3176a48faeeSMaor Gottlieb 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
3186a48faeeSMaor Gottlieb 		actions[num_actions++] = tmp_action;
3196a48faeeSMaor Gottlieb 	}
3206a48faeeSMaor Gottlieb 
3216a48faeeSMaor Gottlieb 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT) {
3226a48faeeSMaor Gottlieb 		bool is_decap = fte->action.pkt_reformat->reformat_type ==
3236a48faeeSMaor Gottlieb 			MLX5_REFORMAT_TYPE_L3_TUNNEL_TO_L2;
3246a48faeeSMaor Gottlieb 
3256a48faeeSMaor Gottlieb 		if (is_decap)
3266a48faeeSMaor Gottlieb 			actions[num_actions++] =
3276a48faeeSMaor Gottlieb 				fte->action.pkt_reformat->action.dr_action;
3286a48faeeSMaor Gottlieb 		else
3296a48faeeSMaor Gottlieb 			delay_encap_set = true;
3306a48faeeSMaor Gottlieb 	}
3316a48faeeSMaor Gottlieb 
3326a48faeeSMaor Gottlieb 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP) {
3336a48faeeSMaor Gottlieb 		tmp_action =
3346a48faeeSMaor Gottlieb 			mlx5dr_action_create_pop_vlan();
3356a48faeeSMaor Gottlieb 		if (!tmp_action) {
3366a48faeeSMaor Gottlieb 			err = -ENOMEM;
3376a48faeeSMaor Gottlieb 			goto free_actions;
3386a48faeeSMaor Gottlieb 		}
3396a48faeeSMaor Gottlieb 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
3406a48faeeSMaor Gottlieb 		actions[num_actions++] = tmp_action;
3416a48faeeSMaor Gottlieb 	}
3426a48faeeSMaor Gottlieb 
3436a48faeeSMaor Gottlieb 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP_2) {
3446a48faeeSMaor Gottlieb 		tmp_action =
3456a48faeeSMaor Gottlieb 			mlx5dr_action_create_pop_vlan();
3466a48faeeSMaor Gottlieb 		if (!tmp_action) {
3476a48faeeSMaor Gottlieb 			err = -ENOMEM;
3486a48faeeSMaor Gottlieb 			goto free_actions;
3496a48faeeSMaor Gottlieb 		}
3506a48faeeSMaor Gottlieb 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
3516a48faeeSMaor Gottlieb 		actions[num_actions++] = tmp_action;
3526a48faeeSMaor Gottlieb 	}
3536a48faeeSMaor Gottlieb 
3546a48faeeSMaor Gottlieb 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
3556a48faeeSMaor Gottlieb 		actions[num_actions++] =
3566a48faeeSMaor Gottlieb 			fte->action.modify_hdr->action.dr_action;
3576a48faeeSMaor Gottlieb 
358b2064909SAlex Vesker 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH) {
359b2064909SAlex Vesker 		tmp_action = create_action_push_vlan(domain, &fte->action.vlan[0]);
360b2064909SAlex Vesker 		if (!tmp_action) {
361b2064909SAlex Vesker 			err = -ENOMEM;
362b2064909SAlex Vesker 			goto free_actions;
363b2064909SAlex Vesker 		}
364b2064909SAlex Vesker 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
365b2064909SAlex Vesker 		actions[num_actions++] = tmp_action;
366b2064909SAlex Vesker 	}
367b2064909SAlex Vesker 
368b2064909SAlex Vesker 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2) {
369b2064909SAlex Vesker 		tmp_action = create_action_push_vlan(domain, &fte->action.vlan[1]);
370b2064909SAlex Vesker 		if (!tmp_action) {
371b2064909SAlex Vesker 			err = -ENOMEM;
372b2064909SAlex Vesker 			goto free_actions;
373b2064909SAlex Vesker 		}
374b2064909SAlex Vesker 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
375b2064909SAlex Vesker 		actions[num_actions++] = tmp_action;
376b2064909SAlex Vesker 	}
377b2064909SAlex Vesker 
3786a48faeeSMaor Gottlieb 	if (delay_encap_set)
3796a48faeeSMaor Gottlieb 		actions[num_actions++] =
3806a48faeeSMaor Gottlieb 			fte->action.pkt_reformat->action.dr_action;
3816a48faeeSMaor Gottlieb 
3826a48faeeSMaor Gottlieb 	/* The order of the actions below is not important */
3836a48faeeSMaor Gottlieb 
3846a48faeeSMaor Gottlieb 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
3856a48faeeSMaor Gottlieb 		tmp_action = mlx5dr_action_create_drop();
3866a48faeeSMaor Gottlieb 		if (!tmp_action) {
3876a48faeeSMaor Gottlieb 			err = -ENOMEM;
3886a48faeeSMaor Gottlieb 			goto free_actions;
3896a48faeeSMaor Gottlieb 		}
3906a48faeeSMaor Gottlieb 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
3917ee3f6d2SAlex Vesker 		term_actions[num_term_actions++].dest = tmp_action;
3926a48faeeSMaor Gottlieb 	}
3936a48faeeSMaor Gottlieb 
3946a48faeeSMaor Gottlieb 	if (fte->flow_context.flow_tag) {
3956a48faeeSMaor Gottlieb 		tmp_action =
3966a48faeeSMaor Gottlieb 			mlx5dr_action_create_tag(fte->flow_context.flow_tag);
3976a48faeeSMaor Gottlieb 		if (!tmp_action) {
3986a48faeeSMaor Gottlieb 			err = -ENOMEM;
3996a48faeeSMaor Gottlieb 			goto free_actions;
4006a48faeeSMaor Gottlieb 		}
4016a48faeeSMaor Gottlieb 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
4026a48faeeSMaor Gottlieb 		actions[num_actions++] = tmp_action;
4036a48faeeSMaor Gottlieb 	}
4046a48faeeSMaor Gottlieb 
4056a48faeeSMaor Gottlieb 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
4066a48faeeSMaor Gottlieb 		list_for_each_entry(dst, &fte->node.children, node.list) {
4076a48faeeSMaor Gottlieb 			enum mlx5_flow_destination_type type = dst->dest_attr.type;
4081ab6dc35SYevgeny Kliteynik 			u32 id;
4096a48faeeSMaor Gottlieb 
4100aec12d9SYevgeny Kliteynik 			if (fs_dr_num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX ||
4110aec12d9SYevgeny Kliteynik 			    num_term_actions == MLX5_FLOW_CONTEXT_ACTION_MAX) {
4120aec12d9SYevgeny Kliteynik 				err = -EOPNOTSUPP;
4136a48faeeSMaor Gottlieb 				goto free_actions;
4146a48faeeSMaor Gottlieb 			}
4156a48faeeSMaor Gottlieb 
416b850a821SErez Shitrit 			if (type == MLX5_FLOW_DESTINATION_TYPE_COUNTER)
417b850a821SErez Shitrit 				continue;
4186a48faeeSMaor Gottlieb 
419b850a821SErez Shitrit 			switch (type) {
4206a48faeeSMaor Gottlieb 			case MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE:
421aec292eeSAlex Vesker 				tmp_action = create_ft_action(domain, dst);
4226a48faeeSMaor Gottlieb 				if (!tmp_action) {
4236a48faeeSMaor Gottlieb 					err = -ENOMEM;
4246a48faeeSMaor Gottlieb 					goto free_actions;
4256a48faeeSMaor Gottlieb 				}
4266a48faeeSMaor Gottlieb 				fs_dr_actions[fs_dr_num_actions++] = tmp_action;
4277ee3f6d2SAlex Vesker 				term_actions[num_term_actions++].dest = tmp_action;
4286a48faeeSMaor Gottlieb 				break;
429e3a0f40bSYevgeny Kliteynik 			case MLX5_FLOW_DESTINATION_TYPE_UPLINK:
4306a48faeeSMaor Gottlieb 			case MLX5_FLOW_DESTINATION_TYPE_VPORT:
431e3a0f40bSYevgeny Kliteynik 				tmp_action = type == MLX5_FLOW_DESTINATION_TYPE_VPORT ?
432e3a0f40bSYevgeny Kliteynik 					     create_vport_action(domain, dst) :
433e3a0f40bSYevgeny Kliteynik 					     create_uplink_action(domain, dst);
4346a48faeeSMaor Gottlieb 				if (!tmp_action) {
4356a48faeeSMaor Gottlieb 					err = -ENOMEM;
4366a48faeeSMaor Gottlieb 					goto free_actions;
4376a48faeeSMaor Gottlieb 				}
4386a48faeeSMaor Gottlieb 				fs_dr_actions[fs_dr_num_actions++] = tmp_action;
4397ee3f6d2SAlex Vesker 				term_actions[num_term_actions].dest = tmp_action;
4407ee3f6d2SAlex Vesker 
4417ee3f6d2SAlex Vesker 				if (dst->dest_attr.vport.flags &
4427ee3f6d2SAlex Vesker 				    MLX5_FLOW_DEST_VPORT_REFORMAT_ID)
4437ee3f6d2SAlex Vesker 					term_actions[num_term_actions].reformat =
4447ee3f6d2SAlex Vesker 						dst->dest_attr.vport.pkt_reformat->action.dr_action;
4457ee3f6d2SAlex Vesker 
4467ee3f6d2SAlex Vesker 				num_term_actions++;
4476a48faeeSMaor Gottlieb 				break;
448de346f40SAlex Vesker 			case MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM:
4491ab6dc35SYevgeny Kliteynik 				id = dst->dest_attr.ft_num;
450de346f40SAlex Vesker 				tmp_action = mlx5dr_action_create_dest_table_num(domain,
4511ab6dc35SYevgeny Kliteynik 										 id);
4521ab6dc35SYevgeny Kliteynik 				if (!tmp_action) {
4531ab6dc35SYevgeny Kliteynik 					err = -ENOMEM;
4541ab6dc35SYevgeny Kliteynik 					goto free_actions;
4551ab6dc35SYevgeny Kliteynik 				}
4561ab6dc35SYevgeny Kliteynik 				fs_dr_actions[fs_dr_num_actions++] = tmp_action;
4571ab6dc35SYevgeny Kliteynik 				term_actions[num_term_actions++].dest = tmp_action;
4581ab6dc35SYevgeny Kliteynik 				break;
4591ab6dc35SYevgeny Kliteynik 			case MLX5_FLOW_DESTINATION_TYPE_FLOW_SAMPLER:
4601ab6dc35SYevgeny Kliteynik 				id = dst->dest_attr.sampler_id;
4611ab6dc35SYevgeny Kliteynik 				tmp_action = mlx5dr_action_create_flow_sampler(domain,
4621ab6dc35SYevgeny Kliteynik 									       id);
463de346f40SAlex Vesker 				if (!tmp_action) {
464de346f40SAlex Vesker 					err = -ENOMEM;
465de346f40SAlex Vesker 					goto free_actions;
466de346f40SAlex Vesker 				}
467de346f40SAlex Vesker 				fs_dr_actions[fs_dr_num_actions++] = tmp_action;
468de346f40SAlex Vesker 				term_actions[num_term_actions++].dest = tmp_action;
469de346f40SAlex Vesker 				break;
4706a48faeeSMaor Gottlieb 			default:
4716a48faeeSMaor Gottlieb 				err = -EOPNOTSUPP;
4726a48faeeSMaor Gottlieb 				goto free_actions;
4736a48faeeSMaor Gottlieb 			}
4746a48faeeSMaor Gottlieb 		}
4756a48faeeSMaor Gottlieb 	}
4766a48faeeSMaor Gottlieb 
477b850a821SErez Shitrit 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
478b850a821SErez Shitrit 		list_for_each_entry(dst, &fte->node.children, node.list) {
479b850a821SErez Shitrit 			u32 id;
480b850a821SErez Shitrit 
481b850a821SErez Shitrit 			if (dst->dest_attr.type !=
482b850a821SErez Shitrit 			    MLX5_FLOW_DESTINATION_TYPE_COUNTER)
483b850a821SErez Shitrit 				continue;
484b850a821SErez Shitrit 
4850aec12d9SYevgeny Kliteynik 			if (num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX ||
4860aec12d9SYevgeny Kliteynik 			    fs_dr_num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX) {
4870aec12d9SYevgeny Kliteynik 				err = -EOPNOTSUPP;
488b850a821SErez Shitrit 				goto free_actions;
489b850a821SErez Shitrit 			}
490b850a821SErez Shitrit 
491b850a821SErez Shitrit 			id = dst->dest_attr.counter_id;
492b850a821SErez Shitrit 			tmp_action =
493b850a821SErez Shitrit 				mlx5dr_action_create_flow_counter(id);
494b850a821SErez Shitrit 			if (!tmp_action) {
495b850a821SErez Shitrit 				err = -ENOMEM;
496b850a821SErez Shitrit 				goto free_actions;
497b850a821SErez Shitrit 			}
498b850a821SErez Shitrit 
499b850a821SErez Shitrit 			fs_dr_actions[fs_dr_num_actions++] = tmp_action;
500b850a821SErez Shitrit 			actions[num_actions++] = tmp_action;
501b850a821SErez Shitrit 		}
502b850a821SErez Shitrit 	}
503b850a821SErez Shitrit 
5046a48faeeSMaor Gottlieb 	params.match_sz = match_sz;
5056a48faeeSMaor Gottlieb 	params.match_buf = (u64 *)fte->val;
5067ee3f6d2SAlex Vesker 	if (num_term_actions == 1) {
5070aec12d9SYevgeny Kliteynik 		if (term_actions->reformat) {
5080aec12d9SYevgeny Kliteynik 			if (num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX) {
5090aec12d9SYevgeny Kliteynik 				err = -EOPNOTSUPP;
5100aec12d9SYevgeny Kliteynik 				goto free_actions;
5110aec12d9SYevgeny Kliteynik 			}
5127ee3f6d2SAlex Vesker 			actions[num_actions++] = term_actions->reformat;
5130aec12d9SYevgeny Kliteynik 		}
5146a48faeeSMaor Gottlieb 
5150aec12d9SYevgeny Kliteynik 		if (num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX) {
5160aec12d9SYevgeny Kliteynik 			err = -EOPNOTSUPP;
5170aec12d9SYevgeny Kliteynik 			goto free_actions;
5180aec12d9SYevgeny Kliteynik 		}
5197ee3f6d2SAlex Vesker 		actions[num_actions++] = term_actions->dest;
5207ee3f6d2SAlex Vesker 	} else if (num_term_actions > 1) {
52163b85f49SYevgeny Kliteynik 		bool ignore_flow_level =
52263b85f49SYevgeny Kliteynik 			!!(fte->action.flags & FLOW_ACT_IGNORE_FLOW_LEVEL);
52363b85f49SYevgeny Kliteynik 
5240aec12d9SYevgeny Kliteynik 		if (num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX ||
5250aec12d9SYevgeny Kliteynik 		    fs_dr_num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX) {
5260aec12d9SYevgeny Kliteynik 			err = -EOPNOTSUPP;
5270aec12d9SYevgeny Kliteynik 			goto free_actions;
5280aec12d9SYevgeny Kliteynik 		}
5297ee3f6d2SAlex Vesker 		tmp_action = mlx5dr_action_create_mult_dest_tbl(domain,
5307ee3f6d2SAlex Vesker 								term_actions,
53163b85f49SYevgeny Kliteynik 								num_term_actions,
53263b85f49SYevgeny Kliteynik 								ignore_flow_level);
5337ee3f6d2SAlex Vesker 		if (!tmp_action) {
5347ee3f6d2SAlex Vesker 			err = -EOPNOTSUPP;
5357ee3f6d2SAlex Vesker 			goto free_actions;
5367ee3f6d2SAlex Vesker 		}
5377ee3f6d2SAlex Vesker 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
5387ee3f6d2SAlex Vesker 		actions[num_actions++] = tmp_action;
5397ee3f6d2SAlex Vesker 	}
5406a48faeeSMaor Gottlieb 
5416a48faeeSMaor Gottlieb 	rule = mlx5dr_rule_create(group->fs_dr_matcher.dr_matcher,
5426a48faeeSMaor Gottlieb 				  &params,
5436a48faeeSMaor Gottlieb 				  num_actions,
54401723919SHamdan Igbaria 				  actions,
54501723919SHamdan Igbaria 				  fte->flow_context.flow_source);
5466a48faeeSMaor Gottlieb 	if (!rule) {
5476a48faeeSMaor Gottlieb 		err = -EINVAL;
5486a48faeeSMaor Gottlieb 		goto free_actions;
5496a48faeeSMaor Gottlieb 	}
5506a48faeeSMaor Gottlieb 
5517ee3f6d2SAlex Vesker 	kfree(term_actions);
5526a48faeeSMaor Gottlieb 	kfree(actions);
5537ee3f6d2SAlex Vesker 
5546a48faeeSMaor Gottlieb 	fte->fs_dr_rule.dr_rule = rule;
5556a48faeeSMaor Gottlieb 	fte->fs_dr_rule.num_actions = fs_dr_num_actions;
5566a48faeeSMaor Gottlieb 	fte->fs_dr_rule.dr_actions = fs_dr_actions;
5576a48faeeSMaor Gottlieb 
5586a48faeeSMaor Gottlieb 	return 0;
5596a48faeeSMaor Gottlieb 
5606a48faeeSMaor Gottlieb free_actions:
5617ee3f6d2SAlex Vesker 	/* Free in reverse order to handle action dependencies */
5627ee3f6d2SAlex Vesker 	for (i = fs_dr_num_actions - 1; i >= 0; i--)
5636a48faeeSMaor Gottlieb 		if (!IS_ERR_OR_NULL(fs_dr_actions[i]))
5646a48faeeSMaor Gottlieb 			mlx5dr_action_destroy(fs_dr_actions[i]);
5656a48faeeSMaor Gottlieb 
5667ee3f6d2SAlex Vesker 	kfree(term_actions);
5677ee3f6d2SAlex Vesker free_fs_dr_actions_alloc:
5686a48faeeSMaor Gottlieb 	kfree(fs_dr_actions);
5697ee3f6d2SAlex Vesker free_actions_alloc:
5707ee3f6d2SAlex Vesker 	kfree(actions);
5717ee3f6d2SAlex Vesker out_err:
5727ee3f6d2SAlex Vesker 	mlx5_core_err(dev, "Failed to create dr rule err(%d)\n", err);
5736a48faeeSMaor Gottlieb 	return err;
5746a48faeeSMaor Gottlieb }
5756a48faeeSMaor Gottlieb 
5766a48faeeSMaor Gottlieb static int mlx5_cmd_dr_packet_reformat_alloc(struct mlx5_flow_root_namespace *ns,
5773f3f05abSYevgeny Kliteynik 					     struct mlx5_pkt_reformat_params *params,
5786a48faeeSMaor Gottlieb 					     enum mlx5_flow_namespace_type namespace,
5796a48faeeSMaor Gottlieb 					     struct mlx5_pkt_reformat *pkt_reformat)
5806a48faeeSMaor Gottlieb {
5816a48faeeSMaor Gottlieb 	struct mlx5dr_domain *dr_domain = ns->fs_dr_domain.dr_domain;
5826a48faeeSMaor Gottlieb 	struct mlx5dr_action *action;
5836a48faeeSMaor Gottlieb 	int dr_reformat;
5846a48faeeSMaor Gottlieb 
5853f3f05abSYevgeny Kliteynik 	switch (params->type) {
5866a48faeeSMaor Gottlieb 	case MLX5_REFORMAT_TYPE_L2_TO_VXLAN:
5876a48faeeSMaor Gottlieb 	case MLX5_REFORMAT_TYPE_L2_TO_NVGRE:
5886a48faeeSMaor Gottlieb 	case MLX5_REFORMAT_TYPE_L2_TO_L2_TUNNEL:
5896a48faeeSMaor Gottlieb 		dr_reformat = DR_ACTION_REFORMAT_TYP_L2_TO_TNL_L2;
5906a48faeeSMaor Gottlieb 		break;
5916a48faeeSMaor Gottlieb 	case MLX5_REFORMAT_TYPE_L3_TUNNEL_TO_L2:
5926a48faeeSMaor Gottlieb 		dr_reformat = DR_ACTION_REFORMAT_TYP_TNL_L3_TO_L2;
5936a48faeeSMaor Gottlieb 		break;
5946a48faeeSMaor Gottlieb 	case MLX5_REFORMAT_TYPE_L2_TO_L3_TUNNEL:
5956a48faeeSMaor Gottlieb 		dr_reformat = DR_ACTION_REFORMAT_TYP_L2_TO_TNL_L3;
5966a48faeeSMaor Gottlieb 		break;
5977ea9b398SYevgeny Kliteynik 	case MLX5_REFORMAT_TYPE_INSERT_HDR:
5987ea9b398SYevgeny Kliteynik 		dr_reformat = DR_ACTION_REFORMAT_TYP_INSERT_HDR;
5997ea9b398SYevgeny Kliteynik 		break;
6000139145fSYevgeny Kliteynik 	case MLX5_REFORMAT_TYPE_REMOVE_HDR:
6010139145fSYevgeny Kliteynik 		dr_reformat = DR_ACTION_REFORMAT_TYP_REMOVE_HDR;
6020139145fSYevgeny Kliteynik 		break;
6036a48faeeSMaor Gottlieb 	default:
6046a48faeeSMaor Gottlieb 		mlx5_core_err(ns->dev, "Packet-reformat not supported(%d)\n",
6053f3f05abSYevgeny Kliteynik 			      params->type);
6066a48faeeSMaor Gottlieb 		return -EOPNOTSUPP;
6076a48faeeSMaor Gottlieb 	}
6086a48faeeSMaor Gottlieb 
6096a48faeeSMaor Gottlieb 	action = mlx5dr_action_create_packet_reformat(dr_domain,
6106a48faeeSMaor Gottlieb 						      dr_reformat,
6113f3f05abSYevgeny Kliteynik 						      params->param_0,
6123f3f05abSYevgeny Kliteynik 						      params->param_1,
6133f3f05abSYevgeny Kliteynik 						      params->size,
6143f3f05abSYevgeny Kliteynik 						      params->data);
6156a48faeeSMaor Gottlieb 	if (!action) {
6166a48faeeSMaor Gottlieb 		mlx5_core_err(ns->dev, "Failed allocating packet-reformat action\n");
6176a48faeeSMaor Gottlieb 		return -EINVAL;
6186a48faeeSMaor Gottlieb 	}
6196a48faeeSMaor Gottlieb 
6206a48faeeSMaor Gottlieb 	pkt_reformat->action.dr_action = action;
6216a48faeeSMaor Gottlieb 
6226a48faeeSMaor Gottlieb 	return 0;
6236a48faeeSMaor Gottlieb }
6246a48faeeSMaor Gottlieb 
6256a48faeeSMaor Gottlieb static void mlx5_cmd_dr_packet_reformat_dealloc(struct mlx5_flow_root_namespace *ns,
6266a48faeeSMaor Gottlieb 						struct mlx5_pkt_reformat *pkt_reformat)
6276a48faeeSMaor Gottlieb {
6286a48faeeSMaor Gottlieb 	mlx5dr_action_destroy(pkt_reformat->action.dr_action);
6296a48faeeSMaor Gottlieb }
6306a48faeeSMaor Gottlieb 
6316a48faeeSMaor Gottlieb static int mlx5_cmd_dr_modify_header_alloc(struct mlx5_flow_root_namespace *ns,
6326a48faeeSMaor Gottlieb 					   u8 namespace, u8 num_actions,
6336a48faeeSMaor Gottlieb 					   void *modify_actions,
6346a48faeeSMaor Gottlieb 					   struct mlx5_modify_hdr *modify_hdr)
6356a48faeeSMaor Gottlieb {
6366a48faeeSMaor Gottlieb 	struct mlx5dr_domain *dr_domain = ns->fs_dr_domain.dr_domain;
6376a48faeeSMaor Gottlieb 	struct mlx5dr_action *action;
6386a48faeeSMaor Gottlieb 	size_t actions_sz;
6396a48faeeSMaor Gottlieb 
640d65dbedfSHuy Nguyen 	actions_sz = MLX5_UN_SZ_BYTES(set_add_copy_action_in_auto) *
6416a48faeeSMaor Gottlieb 		num_actions;
6426a48faeeSMaor Gottlieb 	action = mlx5dr_action_create_modify_header(dr_domain, 0,
6436a48faeeSMaor Gottlieb 						    actions_sz,
6446a48faeeSMaor Gottlieb 						    modify_actions);
6456a48faeeSMaor Gottlieb 	if (!action) {
6466a48faeeSMaor Gottlieb 		mlx5_core_err(ns->dev, "Failed allocating modify-header action\n");
6476a48faeeSMaor Gottlieb 		return -EINVAL;
6486a48faeeSMaor Gottlieb 	}
6496a48faeeSMaor Gottlieb 
6506a48faeeSMaor Gottlieb 	modify_hdr->action.dr_action = action;
6516a48faeeSMaor Gottlieb 
6526a48faeeSMaor Gottlieb 	return 0;
6536a48faeeSMaor Gottlieb }
6546a48faeeSMaor Gottlieb 
6556a48faeeSMaor Gottlieb static void mlx5_cmd_dr_modify_header_dealloc(struct mlx5_flow_root_namespace *ns,
6566a48faeeSMaor Gottlieb 					      struct mlx5_modify_hdr *modify_hdr)
6576a48faeeSMaor Gottlieb {
6586a48faeeSMaor Gottlieb 	mlx5dr_action_destroy(modify_hdr->action.dr_action);
6596a48faeeSMaor Gottlieb }
6606a48faeeSMaor Gottlieb 
661e7e2519eSMaor Gottlieb static int
662e7e2519eSMaor Gottlieb mlx5_cmd_dr_destroy_match_definer(struct mlx5_flow_root_namespace *ns,
663e7e2519eSMaor Gottlieb 				  int definer_id)
664e7e2519eSMaor Gottlieb {
665e7e2519eSMaor Gottlieb 	return -EOPNOTSUPP;
666e7e2519eSMaor Gottlieb }
667e7e2519eSMaor Gottlieb 
668e7e2519eSMaor Gottlieb static int mlx5_cmd_dr_create_match_definer(struct mlx5_flow_root_namespace *ns,
669e7e2519eSMaor Gottlieb 					    u16 format_id, u32 *match_mask)
670e7e2519eSMaor Gottlieb {
671e7e2519eSMaor Gottlieb 	return -EOPNOTSUPP;
672e7e2519eSMaor Gottlieb }
673e7e2519eSMaor Gottlieb 
6746a48faeeSMaor Gottlieb static int mlx5_cmd_dr_delete_fte(struct mlx5_flow_root_namespace *ns,
6756a48faeeSMaor Gottlieb 				  struct mlx5_flow_table *ft,
6766a48faeeSMaor Gottlieb 				  struct fs_fte *fte)
6776a48faeeSMaor Gottlieb {
6786a48faeeSMaor Gottlieb 	struct mlx5_fs_dr_rule *rule = &fte->fs_dr_rule;
6796a48faeeSMaor Gottlieb 	int err;
6806a48faeeSMaor Gottlieb 	int i;
6816a48faeeSMaor Gottlieb 
6826a48faeeSMaor Gottlieb 	if (mlx5_dr_is_fw_table(ft->flags))
6836a48faeeSMaor Gottlieb 		return mlx5_fs_cmd_get_fw_cmds()->delete_fte(ns, ft, fte);
6846a48faeeSMaor Gottlieb 
6856a48faeeSMaor Gottlieb 	err = mlx5dr_rule_destroy(rule->dr_rule);
6866a48faeeSMaor Gottlieb 	if (err)
6876a48faeeSMaor Gottlieb 		return err;
6886a48faeeSMaor Gottlieb 
6897ee3f6d2SAlex Vesker 	/* Free in reverse order to handle action dependencies */
6907ee3f6d2SAlex Vesker 	for (i = rule->num_actions - 1; i >= 0; i--)
6916a48faeeSMaor Gottlieb 		if (!IS_ERR_OR_NULL(rule->dr_actions[i]))
6926a48faeeSMaor Gottlieb 			mlx5dr_action_destroy(rule->dr_actions[i]);
6936a48faeeSMaor Gottlieb 
6946a48faeeSMaor Gottlieb 	kfree(rule->dr_actions);
6956a48faeeSMaor Gottlieb 	return 0;
6966a48faeeSMaor Gottlieb }
6976a48faeeSMaor Gottlieb 
698a2ebfbb7SYevgeny Kliteynik static int mlx5_cmd_dr_update_fte(struct mlx5_flow_root_namespace *ns,
699a2ebfbb7SYevgeny Kliteynik 				  struct mlx5_flow_table *ft,
700a2ebfbb7SYevgeny Kliteynik 				  struct mlx5_flow_group *group,
701a2ebfbb7SYevgeny Kliteynik 				  int modify_mask,
702a2ebfbb7SYevgeny Kliteynik 				  struct fs_fte *fte)
703a2ebfbb7SYevgeny Kliteynik {
704a2ebfbb7SYevgeny Kliteynik 	struct fs_fte fte_tmp = {};
705a2ebfbb7SYevgeny Kliteynik 	int ret;
706a2ebfbb7SYevgeny Kliteynik 
707a2ebfbb7SYevgeny Kliteynik 	if (mlx5_dr_is_fw_table(ft->flags))
708a2ebfbb7SYevgeny Kliteynik 		return mlx5_fs_cmd_get_fw_cmds()->update_fte(ns, ft, group, modify_mask, fte);
709a2ebfbb7SYevgeny Kliteynik 
710a2ebfbb7SYevgeny Kliteynik 	/* Backup current dr rule details */
711a2ebfbb7SYevgeny Kliteynik 	fte_tmp.fs_dr_rule = fte->fs_dr_rule;
712a2ebfbb7SYevgeny Kliteynik 	memset(&fte->fs_dr_rule, 0, sizeof(struct mlx5_fs_dr_rule));
713a2ebfbb7SYevgeny Kliteynik 
714a2ebfbb7SYevgeny Kliteynik 	/* First add the new updated rule, then delete the old rule */
715a2ebfbb7SYevgeny Kliteynik 	ret = mlx5_cmd_dr_create_fte(ns, ft, group, fte);
716a2ebfbb7SYevgeny Kliteynik 	if (ret)
717a2ebfbb7SYevgeny Kliteynik 		goto restore_fte;
718a2ebfbb7SYevgeny Kliteynik 
719a2ebfbb7SYevgeny Kliteynik 	ret = mlx5_cmd_dr_delete_fte(ns, ft, &fte_tmp);
720a2ebfbb7SYevgeny Kliteynik 	WARN_ONCE(ret, "dr update fte duplicate rule deletion failed\n");
721a2ebfbb7SYevgeny Kliteynik 	return ret;
722a2ebfbb7SYevgeny Kliteynik 
723a2ebfbb7SYevgeny Kliteynik restore_fte:
724a2ebfbb7SYevgeny Kliteynik 	fte->fs_dr_rule = fte_tmp.fs_dr_rule;
725a2ebfbb7SYevgeny Kliteynik 	return ret;
726a2ebfbb7SYevgeny Kliteynik }
727a2ebfbb7SYevgeny Kliteynik 
7286a48faeeSMaor Gottlieb static int mlx5_cmd_dr_set_peer(struct mlx5_flow_root_namespace *ns,
7296a48faeeSMaor Gottlieb 				struct mlx5_flow_root_namespace *peer_ns)
7306a48faeeSMaor Gottlieb {
7316a48faeeSMaor Gottlieb 	struct mlx5dr_domain *peer_domain = NULL;
7326a48faeeSMaor Gottlieb 
7336a48faeeSMaor Gottlieb 	if (peer_ns)
7346a48faeeSMaor Gottlieb 		peer_domain = peer_ns->fs_dr_domain.dr_domain;
7356a48faeeSMaor Gottlieb 	mlx5dr_domain_set_peer(ns->fs_dr_domain.dr_domain,
7366a48faeeSMaor Gottlieb 			       peer_domain);
7376a48faeeSMaor Gottlieb 	return 0;
7386a48faeeSMaor Gottlieb }
7396a48faeeSMaor Gottlieb 
7406a48faeeSMaor Gottlieb static int mlx5_cmd_dr_create_ns(struct mlx5_flow_root_namespace *ns)
7416a48faeeSMaor Gottlieb {
7426a48faeeSMaor Gottlieb 	ns->fs_dr_domain.dr_domain =
7436a48faeeSMaor Gottlieb 		mlx5dr_domain_create(ns->dev,
7446a48faeeSMaor Gottlieb 				     MLX5DR_DOMAIN_TYPE_FDB);
7456a48faeeSMaor Gottlieb 	if (!ns->fs_dr_domain.dr_domain) {
7466a48faeeSMaor Gottlieb 		mlx5_core_err(ns->dev, "Failed to create dr flow namespace\n");
7476a48faeeSMaor Gottlieb 		return -EOPNOTSUPP;
7486a48faeeSMaor Gottlieb 	}
7496a48faeeSMaor Gottlieb 	return 0;
7506a48faeeSMaor Gottlieb }
7516a48faeeSMaor Gottlieb 
7526a48faeeSMaor Gottlieb static int mlx5_cmd_dr_destroy_ns(struct mlx5_flow_root_namespace *ns)
7536a48faeeSMaor Gottlieb {
7546a48faeeSMaor Gottlieb 	return mlx5dr_domain_destroy(ns->fs_dr_domain.dr_domain);
7556a48faeeSMaor Gottlieb }
7566a48faeeSMaor Gottlieb 
7578348b71cSDima Chumak static u32 mlx5_cmd_dr_get_capabilities(struct mlx5_flow_root_namespace *ns,
7588348b71cSDima Chumak 					enum fs_flow_table_type ft_type)
7598348b71cSDima Chumak {
7608348b71cSDima Chumak 	if (ft_type != FS_FT_FDB ||
761*6862c787SYevgeny Kliteynik 	    MLX5_CAP_GEN(ns->dev, steering_format_version) == MLX5_STEERING_FORMAT_CONNECTX_5)
7628348b71cSDima Chumak 		return 0;
7638348b71cSDima Chumak 
7648348b71cSDima Chumak 	return MLX5_FLOW_STEERING_CAP_VLAN_PUSH_ON_RX | MLX5_FLOW_STEERING_CAP_VLAN_POP_ON_TX;
7658348b71cSDima Chumak }
7668348b71cSDima Chumak 
7676a48faeeSMaor Gottlieb bool mlx5_fs_dr_is_supported(struct mlx5_core_dev *dev)
7686a48faeeSMaor Gottlieb {
7696a48faeeSMaor Gottlieb 	return mlx5dr_is_supported(dev);
7706a48faeeSMaor Gottlieb }
7716a48faeeSMaor Gottlieb 
7726a48faeeSMaor Gottlieb static const struct mlx5_flow_cmds mlx5_flow_cmds_dr = {
7736a48faeeSMaor Gottlieb 	.create_flow_table = mlx5_cmd_dr_create_flow_table,
7746a48faeeSMaor Gottlieb 	.destroy_flow_table = mlx5_cmd_dr_destroy_flow_table,
7756a48faeeSMaor Gottlieb 	.modify_flow_table = mlx5_cmd_dr_modify_flow_table,
7766a48faeeSMaor Gottlieb 	.create_flow_group = mlx5_cmd_dr_create_flow_group,
7776a48faeeSMaor Gottlieb 	.destroy_flow_group = mlx5_cmd_dr_destroy_flow_group,
7786a48faeeSMaor Gottlieb 	.create_fte = mlx5_cmd_dr_create_fte,
7796a48faeeSMaor Gottlieb 	.update_fte = mlx5_cmd_dr_update_fte,
7806a48faeeSMaor Gottlieb 	.delete_fte = mlx5_cmd_dr_delete_fte,
7816a48faeeSMaor Gottlieb 	.update_root_ft = mlx5_cmd_dr_update_root_ft,
7826a48faeeSMaor Gottlieb 	.packet_reformat_alloc = mlx5_cmd_dr_packet_reformat_alloc,
7836a48faeeSMaor Gottlieb 	.packet_reformat_dealloc = mlx5_cmd_dr_packet_reformat_dealloc,
7846a48faeeSMaor Gottlieb 	.modify_header_alloc = mlx5_cmd_dr_modify_header_alloc,
7856a48faeeSMaor Gottlieb 	.modify_header_dealloc = mlx5_cmd_dr_modify_header_dealloc,
786e7e2519eSMaor Gottlieb 	.create_match_definer = mlx5_cmd_dr_create_match_definer,
787e7e2519eSMaor Gottlieb 	.destroy_match_definer = mlx5_cmd_dr_destroy_match_definer,
7886a48faeeSMaor Gottlieb 	.set_peer = mlx5_cmd_dr_set_peer,
7896a48faeeSMaor Gottlieb 	.create_ns = mlx5_cmd_dr_create_ns,
7906a48faeeSMaor Gottlieb 	.destroy_ns = mlx5_cmd_dr_destroy_ns,
7918348b71cSDima Chumak 	.get_capabilities = mlx5_cmd_dr_get_capabilities,
7926a48faeeSMaor Gottlieb };
7936a48faeeSMaor Gottlieb 
7946a48faeeSMaor Gottlieb const struct mlx5_flow_cmds *mlx5_fs_cmd_get_dr_cmds(void)
7956a48faeeSMaor Gottlieb {
7966a48faeeSMaor Gottlieb 		return &mlx5_flow_cmds_dr;
7976a48faeeSMaor Gottlieb }
798