16a48faeeSMaor Gottlieb // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
26a48faeeSMaor Gottlieb /* Copyright (c) 2019 Mellanox Technologies */
36a48faeeSMaor Gottlieb 
46a48faeeSMaor Gottlieb #include "mlx5_core.h"
56a48faeeSMaor Gottlieb #include "fs_core.h"
66a48faeeSMaor Gottlieb #include "fs_cmd.h"
76a48faeeSMaor Gottlieb #include "mlx5dr.h"
86a48faeeSMaor Gottlieb #include "fs_dr.h"
96a48faeeSMaor Gottlieb 
106a48faeeSMaor Gottlieb static bool mlx5_dr_is_fw_table(u32 flags)
116a48faeeSMaor Gottlieb {
126a48faeeSMaor Gottlieb 	if (flags & MLX5_FLOW_TABLE_TERMINATION)
136a48faeeSMaor Gottlieb 		return true;
146a48faeeSMaor Gottlieb 
156a48faeeSMaor Gottlieb 	return false;
166a48faeeSMaor Gottlieb }
176a48faeeSMaor Gottlieb 
186a48faeeSMaor Gottlieb static int mlx5_cmd_dr_update_root_ft(struct mlx5_flow_root_namespace *ns,
196a48faeeSMaor Gottlieb 				      struct mlx5_flow_table *ft,
206a48faeeSMaor Gottlieb 				      u32 underlay_qpn,
216a48faeeSMaor Gottlieb 				      bool disconnect)
226a48faeeSMaor Gottlieb {
236a48faeeSMaor Gottlieb 	return mlx5_fs_cmd_get_fw_cmds()->update_root_ft(ns, ft, underlay_qpn,
246a48faeeSMaor Gottlieb 							 disconnect);
256a48faeeSMaor Gottlieb }
266a48faeeSMaor Gottlieb 
276a48faeeSMaor Gottlieb static int set_miss_action(struct mlx5_flow_root_namespace *ns,
286a48faeeSMaor Gottlieb 			   struct mlx5_flow_table *ft,
296a48faeeSMaor Gottlieb 			   struct mlx5_flow_table *next_ft)
306a48faeeSMaor Gottlieb {
316a48faeeSMaor Gottlieb 	struct mlx5dr_action *old_miss_action;
326a48faeeSMaor Gottlieb 	struct mlx5dr_action *action = NULL;
336a48faeeSMaor Gottlieb 	struct mlx5dr_table *next_tbl;
346a48faeeSMaor Gottlieb 	int err;
356a48faeeSMaor Gottlieb 
366a48faeeSMaor Gottlieb 	next_tbl = next_ft ? next_ft->fs_dr_table.dr_table : NULL;
376a48faeeSMaor Gottlieb 	if (next_tbl) {
386a48faeeSMaor Gottlieb 		action = mlx5dr_action_create_dest_table(next_tbl);
396a48faeeSMaor Gottlieb 		if (!action)
406a48faeeSMaor Gottlieb 			return -EINVAL;
416a48faeeSMaor Gottlieb 	}
426a48faeeSMaor Gottlieb 	old_miss_action = ft->fs_dr_table.miss_action;
436a48faeeSMaor Gottlieb 	err = mlx5dr_table_set_miss_action(ft->fs_dr_table.dr_table, action);
446a48faeeSMaor Gottlieb 	if (err && action) {
456a48faeeSMaor Gottlieb 		err = mlx5dr_action_destroy(action);
466a48faeeSMaor Gottlieb 		if (err) {
476a48faeeSMaor Gottlieb 			action = NULL;
486a48faeeSMaor Gottlieb 			mlx5_core_err(ns->dev, "Failed to destroy action (%d)\n",
496a48faeeSMaor Gottlieb 				      err);
506a48faeeSMaor Gottlieb 		}
516a48faeeSMaor Gottlieb 	}
526a48faeeSMaor Gottlieb 	ft->fs_dr_table.miss_action = action;
536a48faeeSMaor Gottlieb 	if (old_miss_action) {
546a48faeeSMaor Gottlieb 		err = mlx5dr_action_destroy(old_miss_action);
556a48faeeSMaor Gottlieb 		if (err)
566a48faeeSMaor Gottlieb 			mlx5_core_err(ns->dev, "Failed to destroy action (%d)\n",
576a48faeeSMaor Gottlieb 				      err);
586a48faeeSMaor Gottlieb 	}
596a48faeeSMaor Gottlieb 
606a48faeeSMaor Gottlieb 	return err;
616a48faeeSMaor Gottlieb }
626a48faeeSMaor Gottlieb 
636a48faeeSMaor Gottlieb static int mlx5_cmd_dr_create_flow_table(struct mlx5_flow_root_namespace *ns,
646a48faeeSMaor Gottlieb 					 struct mlx5_flow_table *ft,
6504745afbSPaul Blakey 					 unsigned int size,
666a48faeeSMaor Gottlieb 					 struct mlx5_flow_table *next_ft)
676a48faeeSMaor Gottlieb {
686a48faeeSMaor Gottlieb 	struct mlx5dr_table *tbl;
6913a7e459SErez Shitrit 	u32 flags;
706a48faeeSMaor Gottlieb 	int err;
716a48faeeSMaor Gottlieb 
726a48faeeSMaor Gottlieb 	if (mlx5_dr_is_fw_table(ft->flags))
736a48faeeSMaor Gottlieb 		return mlx5_fs_cmd_get_fw_cmds()->create_flow_table(ns, ft,
7404745afbSPaul Blakey 								    size,
756a48faeeSMaor Gottlieb 								    next_ft);
7613a7e459SErez Shitrit 	flags = ft->flags;
7713a7e459SErez Shitrit 	/* turn off encap/decap if not supported for sw-str by fw */
7813a7e459SErez Shitrit 	if (!MLX5_CAP_FLOWTABLE(ns->dev, sw_owner_reformat_supported))
7913a7e459SErez Shitrit 		flags = ft->flags & ~(MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT |
8013a7e459SErez Shitrit 				      MLX5_FLOW_TABLE_TUNNEL_EN_DECAP);
816a48faeeSMaor Gottlieb 
8213a7e459SErez Shitrit 	tbl = mlx5dr_table_create(ns->fs_dr_domain.dr_domain, ft->level, flags);
836a48faeeSMaor Gottlieb 	if (!tbl) {
846a48faeeSMaor Gottlieb 		mlx5_core_err(ns->dev, "Failed creating dr flow_table\n");
856a48faeeSMaor Gottlieb 		return -EINVAL;
866a48faeeSMaor Gottlieb 	}
876a48faeeSMaor Gottlieb 
886a48faeeSMaor Gottlieb 	ft->fs_dr_table.dr_table = tbl;
896a48faeeSMaor Gottlieb 	ft->id = mlx5dr_table_get_id(tbl);
906a48faeeSMaor Gottlieb 
916a48faeeSMaor Gottlieb 	if (next_ft) {
926a48faeeSMaor Gottlieb 		err = set_miss_action(ns, ft, next_ft);
936a48faeeSMaor Gottlieb 		if (err) {
946a48faeeSMaor Gottlieb 			mlx5dr_table_destroy(tbl);
956a48faeeSMaor Gottlieb 			ft->fs_dr_table.dr_table = NULL;
966a48faeeSMaor Gottlieb 			return err;
976a48faeeSMaor Gottlieb 		}
986a48faeeSMaor Gottlieb 	}
996a48faeeSMaor Gottlieb 
1009e117998SPaul Blakey 	ft->max_fte = INT_MAX;
10104745afbSPaul Blakey 
1026a48faeeSMaor Gottlieb 	return 0;
1036a48faeeSMaor Gottlieb }
1046a48faeeSMaor Gottlieb 
1056a48faeeSMaor Gottlieb static int mlx5_cmd_dr_destroy_flow_table(struct mlx5_flow_root_namespace *ns,
1066a48faeeSMaor Gottlieb 					  struct mlx5_flow_table *ft)
1076a48faeeSMaor Gottlieb {
1086a48faeeSMaor Gottlieb 	struct mlx5dr_action *action = ft->fs_dr_table.miss_action;
1096a48faeeSMaor Gottlieb 	int err;
1106a48faeeSMaor Gottlieb 
1116a48faeeSMaor Gottlieb 	if (mlx5_dr_is_fw_table(ft->flags))
1126a48faeeSMaor Gottlieb 		return mlx5_fs_cmd_get_fw_cmds()->destroy_flow_table(ns, ft);
1136a48faeeSMaor Gottlieb 
1146a48faeeSMaor Gottlieb 	err = mlx5dr_table_destroy(ft->fs_dr_table.dr_table);
1156a48faeeSMaor Gottlieb 	if (err) {
1166a48faeeSMaor Gottlieb 		mlx5_core_err(ns->dev, "Failed to destroy flow_table (%d)\n",
1176a48faeeSMaor Gottlieb 			      err);
1186a48faeeSMaor Gottlieb 		return err;
1196a48faeeSMaor Gottlieb 	}
1206a48faeeSMaor Gottlieb 	if (action) {
1216a48faeeSMaor Gottlieb 		err = mlx5dr_action_destroy(action);
1226a48faeeSMaor Gottlieb 		if (err) {
1236a48faeeSMaor Gottlieb 			mlx5_core_err(ns->dev, "Failed to destroy action(%d)\n",
1246a48faeeSMaor Gottlieb 				      err);
1256a48faeeSMaor Gottlieb 			return err;
1266a48faeeSMaor Gottlieb 		}
1276a48faeeSMaor Gottlieb 	}
1286a48faeeSMaor Gottlieb 
1296a48faeeSMaor Gottlieb 	return err;
1306a48faeeSMaor Gottlieb }
1316a48faeeSMaor Gottlieb 
1326a48faeeSMaor Gottlieb static int mlx5_cmd_dr_modify_flow_table(struct mlx5_flow_root_namespace *ns,
1336a48faeeSMaor Gottlieb 					 struct mlx5_flow_table *ft,
1346a48faeeSMaor Gottlieb 					 struct mlx5_flow_table *next_ft)
1356a48faeeSMaor Gottlieb {
136a01a43faSYevgeny Kliteynik 	if (mlx5_dr_is_fw_table(ft->flags))
137a01a43faSYevgeny Kliteynik 		return mlx5_fs_cmd_get_fw_cmds()->modify_flow_table(ns, ft, next_ft);
138a01a43faSYevgeny Kliteynik 
1396a48faeeSMaor Gottlieb 	return set_miss_action(ns, ft, next_ft);
1406a48faeeSMaor Gottlieb }
1416a48faeeSMaor Gottlieb 
1426a48faeeSMaor Gottlieb static int mlx5_cmd_dr_create_flow_group(struct mlx5_flow_root_namespace *ns,
1436a48faeeSMaor Gottlieb 					 struct mlx5_flow_table *ft,
1446a48faeeSMaor Gottlieb 					 u32 *in,
1456a48faeeSMaor Gottlieb 					 struct mlx5_flow_group *fg)
1466a48faeeSMaor Gottlieb {
1476a48faeeSMaor Gottlieb 	struct mlx5dr_matcher *matcher;
148f6409299SHamdan Igbaria 	u32 priority = MLX5_GET(create_flow_group_in, in,
1496a48faeeSMaor Gottlieb 				start_flow_index);
1506a48faeeSMaor Gottlieb 	u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
1516a48faeeSMaor Gottlieb 					    in,
1526a48faeeSMaor Gottlieb 					    match_criteria_enable);
1536a48faeeSMaor Gottlieb 	struct mlx5dr_match_parameters mask;
1546a48faeeSMaor Gottlieb 
1556a48faeeSMaor Gottlieb 	if (mlx5_dr_is_fw_table(ft->flags))
1566a48faeeSMaor Gottlieb 		return mlx5_fs_cmd_get_fw_cmds()->create_flow_group(ns, ft, in,
1576a48faeeSMaor Gottlieb 								    fg);
1586a48faeeSMaor Gottlieb 
1596a48faeeSMaor Gottlieb 	mask.match_buf = MLX5_ADDR_OF(create_flow_group_in,
1606a48faeeSMaor Gottlieb 				      in, match_criteria);
1616a48faeeSMaor Gottlieb 	mask.match_sz = sizeof(fg->mask.match_criteria);
1626a48faeeSMaor Gottlieb 
1636a48faeeSMaor Gottlieb 	matcher = mlx5dr_matcher_create(ft->fs_dr_table.dr_table,
1646a48faeeSMaor Gottlieb 					priority,
1656a48faeeSMaor Gottlieb 					match_criteria_enable,
1666a48faeeSMaor Gottlieb 					&mask);
1676a48faeeSMaor Gottlieb 	if (!matcher) {
1686a48faeeSMaor Gottlieb 		mlx5_core_err(ns->dev, "Failed creating matcher\n");
1696a48faeeSMaor Gottlieb 		return -EINVAL;
1706a48faeeSMaor Gottlieb 	}
1716a48faeeSMaor Gottlieb 
1726a48faeeSMaor Gottlieb 	fg->fs_dr_matcher.dr_matcher = matcher;
1736a48faeeSMaor Gottlieb 	return 0;
1746a48faeeSMaor Gottlieb }
1756a48faeeSMaor Gottlieb 
1766a48faeeSMaor Gottlieb static int mlx5_cmd_dr_destroy_flow_group(struct mlx5_flow_root_namespace *ns,
1776a48faeeSMaor Gottlieb 					  struct mlx5_flow_table *ft,
1786a48faeeSMaor Gottlieb 					  struct mlx5_flow_group *fg)
1796a48faeeSMaor Gottlieb {
1806a48faeeSMaor Gottlieb 	if (mlx5_dr_is_fw_table(ft->flags))
1816a48faeeSMaor Gottlieb 		return mlx5_fs_cmd_get_fw_cmds()->destroy_flow_group(ns, ft, fg);
1826a48faeeSMaor Gottlieb 
1836a48faeeSMaor Gottlieb 	return mlx5dr_matcher_destroy(fg->fs_dr_matcher.dr_matcher);
1846a48faeeSMaor Gottlieb }
1856a48faeeSMaor Gottlieb 
1866a48faeeSMaor Gottlieb static struct mlx5dr_action *create_vport_action(struct mlx5dr_domain *domain,
1876a48faeeSMaor Gottlieb 						 struct mlx5_flow_rule *dst)
1886a48faeeSMaor Gottlieb {
1896a48faeeSMaor Gottlieb 	struct mlx5_flow_destination *dest_attr = &dst->dest_attr;
1906a48faeeSMaor Gottlieb 
1916a48faeeSMaor Gottlieb 	return mlx5dr_action_create_dest_vport(domain, dest_attr->vport.num,
1926a48faeeSMaor Gottlieb 					       dest_attr->vport.flags &
1936a48faeeSMaor Gottlieb 					       MLX5_FLOW_DEST_VPORT_VHCA_ID,
1946a48faeeSMaor Gottlieb 					       dest_attr->vport.vhca_id);
1956a48faeeSMaor Gottlieb }
1966a48faeeSMaor Gottlieb 
197aec292eeSAlex Vesker static struct mlx5dr_action *create_ft_action(struct mlx5dr_domain *domain,
1986a48faeeSMaor Gottlieb 					      struct mlx5_flow_rule *dst)
1996a48faeeSMaor Gottlieb {
2006a48faeeSMaor Gottlieb 	struct mlx5_flow_table *dest_ft = dst->dest_attr.ft;
2016a48faeeSMaor Gottlieb 
2026a48faeeSMaor Gottlieb 	if (mlx5_dr_is_fw_table(dest_ft->flags))
203aec292eeSAlex Vesker 		return mlx5dr_action_create_dest_flow_fw_table(domain, dest_ft);
2046a48faeeSMaor Gottlieb 	return mlx5dr_action_create_dest_table(dest_ft->fs_dr_table.dr_table);
2056a48faeeSMaor Gottlieb }
2066a48faeeSMaor Gottlieb 
2076a48faeeSMaor Gottlieb static struct mlx5dr_action *create_action_push_vlan(struct mlx5dr_domain *domain,
2086a48faeeSMaor Gottlieb 						     struct mlx5_fs_vlan *vlan)
2096a48faeeSMaor Gottlieb {
2106a48faeeSMaor Gottlieb 	u16 n_ethtype = vlan->ethtype;
2116a48faeeSMaor Gottlieb 	u8  prio = vlan->prio;
2126a48faeeSMaor Gottlieb 	u16 vid = vlan->vid;
2136a48faeeSMaor Gottlieb 	u32 vlan_hdr;
2146a48faeeSMaor Gottlieb 
2156a48faeeSMaor Gottlieb 	vlan_hdr = (u32)n_ethtype << 16 | (u32)(prio) << 12 |  (u32)vid;
2166a48faeeSMaor Gottlieb 	return mlx5dr_action_create_push_vlan(domain, htonl(vlan_hdr));
2176a48faeeSMaor Gottlieb }
2186a48faeeSMaor Gottlieb 
2197ee3f6d2SAlex Vesker static bool contain_vport_reformat_action(struct mlx5_flow_rule *dst)
2207ee3f6d2SAlex Vesker {
2217ee3f6d2SAlex Vesker 	return dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_VPORT &&
2227ee3f6d2SAlex Vesker 		dst->dest_attr.vport.flags & MLX5_FLOW_DEST_VPORT_REFORMAT_ID;
2237ee3f6d2SAlex Vesker }
2247ee3f6d2SAlex Vesker 
2251ffd4989SYevgeny Kliteynik #define MLX5_FLOW_CONTEXT_ACTION_MAX  32
2266a48faeeSMaor Gottlieb static int mlx5_cmd_dr_create_fte(struct mlx5_flow_root_namespace *ns,
2276a48faeeSMaor Gottlieb 				  struct mlx5_flow_table *ft,
2286a48faeeSMaor Gottlieb 				  struct mlx5_flow_group *group,
2296a48faeeSMaor Gottlieb 				  struct fs_fte *fte)
2306a48faeeSMaor Gottlieb {
2316a48faeeSMaor Gottlieb 	struct mlx5dr_domain *domain = ns->fs_dr_domain.dr_domain;
2327ee3f6d2SAlex Vesker 	struct mlx5dr_action_dest *term_actions;
2336a48faeeSMaor Gottlieb 	struct mlx5dr_match_parameters params;
2346a48faeeSMaor Gottlieb 	struct mlx5_core_dev *dev = ns->dev;
2356a48faeeSMaor Gottlieb 	struct mlx5dr_action **fs_dr_actions;
2366a48faeeSMaor Gottlieb 	struct mlx5dr_action *tmp_action;
2376a48faeeSMaor Gottlieb 	struct mlx5dr_action **actions;
2386a48faeeSMaor Gottlieb 	bool delay_encap_set = false;
2396a48faeeSMaor Gottlieb 	struct mlx5dr_rule *rule;
2406a48faeeSMaor Gottlieb 	struct mlx5_flow_rule *dst;
2416a48faeeSMaor Gottlieb 	int fs_dr_num_actions = 0;
2427ee3f6d2SAlex Vesker 	int num_term_actions = 0;
2436a48faeeSMaor Gottlieb 	int num_actions = 0;
2446a48faeeSMaor Gottlieb 	size_t match_sz;
2456a48faeeSMaor Gottlieb 	int err = 0;
2466a48faeeSMaor Gottlieb 	int i;
2476a48faeeSMaor Gottlieb 
2486a48faeeSMaor Gottlieb 	if (mlx5_dr_is_fw_table(ft->flags))
2496a48faeeSMaor Gottlieb 		return mlx5_fs_cmd_get_fw_cmds()->create_fte(ns, ft, group, fte);
2506a48faeeSMaor Gottlieb 
2516a48faeeSMaor Gottlieb 	actions = kcalloc(MLX5_FLOW_CONTEXT_ACTION_MAX, sizeof(*actions),
2526a48faeeSMaor Gottlieb 			  GFP_KERNEL);
2537ee3f6d2SAlex Vesker 	if (!actions) {
2547ee3f6d2SAlex Vesker 		err = -ENOMEM;
2557ee3f6d2SAlex Vesker 		goto out_err;
2567ee3f6d2SAlex Vesker 	}
2576a48faeeSMaor Gottlieb 
2586a48faeeSMaor Gottlieb 	fs_dr_actions = kcalloc(MLX5_FLOW_CONTEXT_ACTION_MAX,
2596a48faeeSMaor Gottlieb 				sizeof(*fs_dr_actions), GFP_KERNEL);
2606a48faeeSMaor Gottlieb 	if (!fs_dr_actions) {
2617ee3f6d2SAlex Vesker 		err = -ENOMEM;
2627ee3f6d2SAlex Vesker 		goto free_actions_alloc;
2637ee3f6d2SAlex Vesker 	}
2647ee3f6d2SAlex Vesker 
2657ee3f6d2SAlex Vesker 	term_actions = kcalloc(MLX5_FLOW_CONTEXT_ACTION_MAX,
2667ee3f6d2SAlex Vesker 			       sizeof(*term_actions), GFP_KERNEL);
2677ee3f6d2SAlex Vesker 	if (!term_actions) {
2687ee3f6d2SAlex Vesker 		err = -ENOMEM;
2697ee3f6d2SAlex Vesker 		goto free_fs_dr_actions_alloc;
2706a48faeeSMaor Gottlieb 	}
2716a48faeeSMaor Gottlieb 
2726a48faeeSMaor Gottlieb 	match_sz = sizeof(fte->val);
2736a48faeeSMaor Gottlieb 
2747ee3f6d2SAlex Vesker 	/* Drop reformat action bit if destination vport set with reformat */
2757ee3f6d2SAlex Vesker 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
2767ee3f6d2SAlex Vesker 		list_for_each_entry(dst, &fte->node.children, node.list) {
2777ee3f6d2SAlex Vesker 			if (!contain_vport_reformat_action(dst))
2787ee3f6d2SAlex Vesker 				continue;
2797ee3f6d2SAlex Vesker 
2807ee3f6d2SAlex Vesker 			fte->action.action &= ~MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
2817ee3f6d2SAlex Vesker 			break;
2827ee3f6d2SAlex Vesker 		}
2837ee3f6d2SAlex Vesker 	}
2847ee3f6d2SAlex Vesker 
2856a48faeeSMaor Gottlieb 	/* The order of the actions are must to be keep, only the following
2866a48faeeSMaor Gottlieb 	 * order is supported by SW steering:
287b2064909SAlex Vesker 	 * TX: modify header -> push vlan -> encap
2886a48faeeSMaor Gottlieb 	 * RX: decap -> pop vlan -> modify header
2896a48faeeSMaor Gottlieb 	 */
2906a48faeeSMaor Gottlieb 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_DECAP) {
2916a48faeeSMaor Gottlieb 		enum mlx5dr_action_reformat_type decap_type =
2926a48faeeSMaor Gottlieb 			DR_ACTION_REFORMAT_TYP_TNL_L2_TO_L2;
2936a48faeeSMaor Gottlieb 
2946a48faeeSMaor Gottlieb 		tmp_action = mlx5dr_action_create_packet_reformat(domain,
2953f3f05abSYevgeny Kliteynik 								  decap_type,
2963f3f05abSYevgeny Kliteynik 								  0, 0, 0,
2976a48faeeSMaor Gottlieb 								  NULL);
2986a48faeeSMaor Gottlieb 		if (!tmp_action) {
2996a48faeeSMaor Gottlieb 			err = -ENOMEM;
3006a48faeeSMaor Gottlieb 			goto free_actions;
3016a48faeeSMaor Gottlieb 		}
3026a48faeeSMaor Gottlieb 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
3036a48faeeSMaor Gottlieb 		actions[num_actions++] = tmp_action;
3046a48faeeSMaor Gottlieb 	}
3056a48faeeSMaor Gottlieb 
3066a48faeeSMaor Gottlieb 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT) {
3076a48faeeSMaor Gottlieb 		bool is_decap = fte->action.pkt_reformat->reformat_type ==
3086a48faeeSMaor Gottlieb 			MLX5_REFORMAT_TYPE_L3_TUNNEL_TO_L2;
3096a48faeeSMaor Gottlieb 
3106a48faeeSMaor Gottlieb 		if (is_decap)
3116a48faeeSMaor Gottlieb 			actions[num_actions++] =
3126a48faeeSMaor Gottlieb 				fte->action.pkt_reformat->action.dr_action;
3136a48faeeSMaor Gottlieb 		else
3146a48faeeSMaor Gottlieb 			delay_encap_set = true;
3156a48faeeSMaor Gottlieb 	}
3166a48faeeSMaor Gottlieb 
3176a48faeeSMaor Gottlieb 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP) {
3186a48faeeSMaor Gottlieb 		tmp_action =
3196a48faeeSMaor Gottlieb 			mlx5dr_action_create_pop_vlan();
3206a48faeeSMaor Gottlieb 		if (!tmp_action) {
3216a48faeeSMaor Gottlieb 			err = -ENOMEM;
3226a48faeeSMaor Gottlieb 			goto free_actions;
3236a48faeeSMaor Gottlieb 		}
3246a48faeeSMaor Gottlieb 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
3256a48faeeSMaor Gottlieb 		actions[num_actions++] = tmp_action;
3266a48faeeSMaor Gottlieb 	}
3276a48faeeSMaor Gottlieb 
3286a48faeeSMaor Gottlieb 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP_2) {
3296a48faeeSMaor Gottlieb 		tmp_action =
3306a48faeeSMaor Gottlieb 			mlx5dr_action_create_pop_vlan();
3316a48faeeSMaor Gottlieb 		if (!tmp_action) {
3326a48faeeSMaor Gottlieb 			err = -ENOMEM;
3336a48faeeSMaor Gottlieb 			goto free_actions;
3346a48faeeSMaor Gottlieb 		}
3356a48faeeSMaor Gottlieb 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
3366a48faeeSMaor Gottlieb 		actions[num_actions++] = tmp_action;
3376a48faeeSMaor Gottlieb 	}
3386a48faeeSMaor Gottlieb 
3396a48faeeSMaor Gottlieb 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
3406a48faeeSMaor Gottlieb 		actions[num_actions++] =
3416a48faeeSMaor Gottlieb 			fte->action.modify_hdr->action.dr_action;
3426a48faeeSMaor Gottlieb 
343b2064909SAlex Vesker 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH) {
344b2064909SAlex Vesker 		tmp_action = create_action_push_vlan(domain, &fte->action.vlan[0]);
345b2064909SAlex Vesker 		if (!tmp_action) {
346b2064909SAlex Vesker 			err = -ENOMEM;
347b2064909SAlex Vesker 			goto free_actions;
348b2064909SAlex Vesker 		}
349b2064909SAlex Vesker 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
350b2064909SAlex Vesker 		actions[num_actions++] = tmp_action;
351b2064909SAlex Vesker 	}
352b2064909SAlex Vesker 
353b2064909SAlex Vesker 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2) {
354b2064909SAlex Vesker 		tmp_action = create_action_push_vlan(domain, &fte->action.vlan[1]);
355b2064909SAlex Vesker 		if (!tmp_action) {
356b2064909SAlex Vesker 			err = -ENOMEM;
357b2064909SAlex Vesker 			goto free_actions;
358b2064909SAlex Vesker 		}
359b2064909SAlex Vesker 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
360b2064909SAlex Vesker 		actions[num_actions++] = tmp_action;
361b2064909SAlex Vesker 	}
362b2064909SAlex Vesker 
3636a48faeeSMaor Gottlieb 	if (delay_encap_set)
3646a48faeeSMaor Gottlieb 		actions[num_actions++] =
3656a48faeeSMaor Gottlieb 			fte->action.pkt_reformat->action.dr_action;
3666a48faeeSMaor Gottlieb 
3676a48faeeSMaor Gottlieb 	/* The order of the actions below is not important */
3686a48faeeSMaor Gottlieb 
3696a48faeeSMaor Gottlieb 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
3706a48faeeSMaor Gottlieb 		tmp_action = mlx5dr_action_create_drop();
3716a48faeeSMaor Gottlieb 		if (!tmp_action) {
3726a48faeeSMaor Gottlieb 			err = -ENOMEM;
3736a48faeeSMaor Gottlieb 			goto free_actions;
3746a48faeeSMaor Gottlieb 		}
3756a48faeeSMaor Gottlieb 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
3767ee3f6d2SAlex Vesker 		term_actions[num_term_actions++].dest = tmp_action;
3776a48faeeSMaor Gottlieb 	}
3786a48faeeSMaor Gottlieb 
3796a48faeeSMaor Gottlieb 	if (fte->flow_context.flow_tag) {
3806a48faeeSMaor Gottlieb 		tmp_action =
3816a48faeeSMaor Gottlieb 			mlx5dr_action_create_tag(fte->flow_context.flow_tag);
3826a48faeeSMaor Gottlieb 		if (!tmp_action) {
3836a48faeeSMaor Gottlieb 			err = -ENOMEM;
3846a48faeeSMaor Gottlieb 			goto free_actions;
3856a48faeeSMaor Gottlieb 		}
3866a48faeeSMaor Gottlieb 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
3876a48faeeSMaor Gottlieb 		actions[num_actions++] = tmp_action;
3886a48faeeSMaor Gottlieb 	}
3896a48faeeSMaor Gottlieb 
3906a48faeeSMaor Gottlieb 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
3916a48faeeSMaor Gottlieb 		list_for_each_entry(dst, &fte->node.children, node.list) {
3926a48faeeSMaor Gottlieb 			enum mlx5_flow_destination_type type = dst->dest_attr.type;
3931ab6dc35SYevgeny Kliteynik 			u32 id;
3946a48faeeSMaor Gottlieb 
3957ee3f6d2SAlex Vesker 			if (num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX ||
3967ee3f6d2SAlex Vesker 			    num_term_actions >= MLX5_FLOW_CONTEXT_ACTION_MAX) {
3976a48faeeSMaor Gottlieb 				err = -ENOSPC;
3986a48faeeSMaor Gottlieb 				goto free_actions;
3996a48faeeSMaor Gottlieb 			}
4006a48faeeSMaor Gottlieb 
401b850a821SErez Shitrit 			if (type == MLX5_FLOW_DESTINATION_TYPE_COUNTER)
402b850a821SErez Shitrit 				continue;
4036a48faeeSMaor Gottlieb 
404b850a821SErez Shitrit 			switch (type) {
4056a48faeeSMaor Gottlieb 			case MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE:
406aec292eeSAlex Vesker 				tmp_action = create_ft_action(domain, dst);
4076a48faeeSMaor Gottlieb 				if (!tmp_action) {
4086a48faeeSMaor Gottlieb 					err = -ENOMEM;
4096a48faeeSMaor Gottlieb 					goto free_actions;
4106a48faeeSMaor Gottlieb 				}
4116a48faeeSMaor Gottlieb 				fs_dr_actions[fs_dr_num_actions++] = tmp_action;
4127ee3f6d2SAlex Vesker 				term_actions[num_term_actions++].dest = tmp_action;
4136a48faeeSMaor Gottlieb 				break;
4146a48faeeSMaor Gottlieb 			case MLX5_FLOW_DESTINATION_TYPE_VPORT:
4156a48faeeSMaor Gottlieb 				tmp_action = create_vport_action(domain, dst);
4166a48faeeSMaor Gottlieb 				if (!tmp_action) {
4176a48faeeSMaor Gottlieb 					err = -ENOMEM;
4186a48faeeSMaor Gottlieb 					goto free_actions;
4196a48faeeSMaor Gottlieb 				}
4206a48faeeSMaor Gottlieb 				fs_dr_actions[fs_dr_num_actions++] = tmp_action;
4217ee3f6d2SAlex Vesker 				term_actions[num_term_actions].dest = tmp_action;
4227ee3f6d2SAlex Vesker 
4237ee3f6d2SAlex Vesker 				if (dst->dest_attr.vport.flags &
4247ee3f6d2SAlex Vesker 				    MLX5_FLOW_DEST_VPORT_REFORMAT_ID)
4257ee3f6d2SAlex Vesker 					term_actions[num_term_actions].reformat =
4267ee3f6d2SAlex Vesker 						dst->dest_attr.vport.pkt_reformat->action.dr_action;
4277ee3f6d2SAlex Vesker 
4287ee3f6d2SAlex Vesker 				num_term_actions++;
4296a48faeeSMaor Gottlieb 				break;
430de346f40SAlex Vesker 			case MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM:
4311ab6dc35SYevgeny Kliteynik 				id = dst->dest_attr.ft_num;
432de346f40SAlex Vesker 				tmp_action = mlx5dr_action_create_dest_table_num(domain,
4331ab6dc35SYevgeny Kliteynik 										 id);
4341ab6dc35SYevgeny Kliteynik 				if (!tmp_action) {
4351ab6dc35SYevgeny Kliteynik 					err = -ENOMEM;
4361ab6dc35SYevgeny Kliteynik 					goto free_actions;
4371ab6dc35SYevgeny Kliteynik 				}
4381ab6dc35SYevgeny Kliteynik 				fs_dr_actions[fs_dr_num_actions++] = tmp_action;
4391ab6dc35SYevgeny Kliteynik 				term_actions[num_term_actions++].dest = tmp_action;
4401ab6dc35SYevgeny Kliteynik 				break;
4411ab6dc35SYevgeny Kliteynik 			case MLX5_FLOW_DESTINATION_TYPE_FLOW_SAMPLER:
4421ab6dc35SYevgeny Kliteynik 				id = dst->dest_attr.sampler_id;
4431ab6dc35SYevgeny Kliteynik 				tmp_action = mlx5dr_action_create_flow_sampler(domain,
4441ab6dc35SYevgeny Kliteynik 									       id);
445de346f40SAlex Vesker 				if (!tmp_action) {
446de346f40SAlex Vesker 					err = -ENOMEM;
447de346f40SAlex Vesker 					goto free_actions;
448de346f40SAlex Vesker 				}
449de346f40SAlex Vesker 				fs_dr_actions[fs_dr_num_actions++] = tmp_action;
450de346f40SAlex Vesker 				term_actions[num_term_actions++].dest = tmp_action;
451de346f40SAlex Vesker 				break;
4526a48faeeSMaor Gottlieb 			default:
4536a48faeeSMaor Gottlieb 				err = -EOPNOTSUPP;
4546a48faeeSMaor Gottlieb 				goto free_actions;
4556a48faeeSMaor Gottlieb 			}
4566a48faeeSMaor Gottlieb 		}
4576a48faeeSMaor Gottlieb 	}
4586a48faeeSMaor Gottlieb 
459b850a821SErez Shitrit 	if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
460b850a821SErez Shitrit 		list_for_each_entry(dst, &fte->node.children, node.list) {
461b850a821SErez Shitrit 			u32 id;
462b850a821SErez Shitrit 
463b850a821SErez Shitrit 			if (dst->dest_attr.type !=
464b850a821SErez Shitrit 			    MLX5_FLOW_DESTINATION_TYPE_COUNTER)
465b850a821SErez Shitrit 				continue;
466b850a821SErez Shitrit 
467b850a821SErez Shitrit 			if (num_actions == MLX5_FLOW_CONTEXT_ACTION_MAX) {
468b850a821SErez Shitrit 				err = -ENOSPC;
469b850a821SErez Shitrit 				goto free_actions;
470b850a821SErez Shitrit 			}
471b850a821SErez Shitrit 
472b850a821SErez Shitrit 			id = dst->dest_attr.counter_id;
473b850a821SErez Shitrit 			tmp_action =
474b850a821SErez Shitrit 				mlx5dr_action_create_flow_counter(id);
475b850a821SErez Shitrit 			if (!tmp_action) {
476b850a821SErez Shitrit 				err = -ENOMEM;
477b850a821SErez Shitrit 				goto free_actions;
478b850a821SErez Shitrit 			}
479b850a821SErez Shitrit 
480b850a821SErez Shitrit 			fs_dr_actions[fs_dr_num_actions++] = tmp_action;
481b850a821SErez Shitrit 			actions[num_actions++] = tmp_action;
482b850a821SErez Shitrit 		}
483b850a821SErez Shitrit 	}
484b850a821SErez Shitrit 
4856a48faeeSMaor Gottlieb 	params.match_sz = match_sz;
4866a48faeeSMaor Gottlieb 	params.match_buf = (u64 *)fte->val;
4877ee3f6d2SAlex Vesker 	if (num_term_actions == 1) {
4887ee3f6d2SAlex Vesker 		if (term_actions->reformat)
4897ee3f6d2SAlex Vesker 			actions[num_actions++] = term_actions->reformat;
4906a48faeeSMaor Gottlieb 
4917ee3f6d2SAlex Vesker 		actions[num_actions++] = term_actions->dest;
4927ee3f6d2SAlex Vesker 	} else if (num_term_actions > 1) {
49363b85f49SYevgeny Kliteynik 		bool ignore_flow_level =
49463b85f49SYevgeny Kliteynik 			!!(fte->action.flags & FLOW_ACT_IGNORE_FLOW_LEVEL);
49563b85f49SYevgeny Kliteynik 
4967ee3f6d2SAlex Vesker 		tmp_action = mlx5dr_action_create_mult_dest_tbl(domain,
4977ee3f6d2SAlex Vesker 								term_actions,
49863b85f49SYevgeny Kliteynik 								num_term_actions,
49963b85f49SYevgeny Kliteynik 								ignore_flow_level);
5007ee3f6d2SAlex Vesker 		if (!tmp_action) {
5017ee3f6d2SAlex Vesker 			err = -EOPNOTSUPP;
5027ee3f6d2SAlex Vesker 			goto free_actions;
5037ee3f6d2SAlex Vesker 		}
5047ee3f6d2SAlex Vesker 		fs_dr_actions[fs_dr_num_actions++] = tmp_action;
5057ee3f6d2SAlex Vesker 		actions[num_actions++] = tmp_action;
5067ee3f6d2SAlex Vesker 	}
5076a48faeeSMaor Gottlieb 
5086a48faeeSMaor Gottlieb 	rule = mlx5dr_rule_create(group->fs_dr_matcher.dr_matcher,
5096a48faeeSMaor Gottlieb 				  &params,
5106a48faeeSMaor Gottlieb 				  num_actions,
51101723919SHamdan Igbaria 				  actions,
51201723919SHamdan Igbaria 				  fte->flow_context.flow_source);
5136a48faeeSMaor Gottlieb 	if (!rule) {
5146a48faeeSMaor Gottlieb 		err = -EINVAL;
5156a48faeeSMaor Gottlieb 		goto free_actions;
5166a48faeeSMaor Gottlieb 	}
5176a48faeeSMaor Gottlieb 
5187ee3f6d2SAlex Vesker 	kfree(term_actions);
5196a48faeeSMaor Gottlieb 	kfree(actions);
5207ee3f6d2SAlex Vesker 
5216a48faeeSMaor Gottlieb 	fte->fs_dr_rule.dr_rule = rule;
5226a48faeeSMaor Gottlieb 	fte->fs_dr_rule.num_actions = fs_dr_num_actions;
5236a48faeeSMaor Gottlieb 	fte->fs_dr_rule.dr_actions = fs_dr_actions;
5246a48faeeSMaor Gottlieb 
5256a48faeeSMaor Gottlieb 	return 0;
5266a48faeeSMaor Gottlieb 
5276a48faeeSMaor Gottlieb free_actions:
5287ee3f6d2SAlex Vesker 	/* Free in reverse order to handle action dependencies */
5297ee3f6d2SAlex Vesker 	for (i = fs_dr_num_actions - 1; i >= 0; i--)
5306a48faeeSMaor Gottlieb 		if (!IS_ERR_OR_NULL(fs_dr_actions[i]))
5316a48faeeSMaor Gottlieb 			mlx5dr_action_destroy(fs_dr_actions[i]);
5326a48faeeSMaor Gottlieb 
5337ee3f6d2SAlex Vesker 	kfree(term_actions);
5347ee3f6d2SAlex Vesker free_fs_dr_actions_alloc:
5356a48faeeSMaor Gottlieb 	kfree(fs_dr_actions);
5367ee3f6d2SAlex Vesker free_actions_alloc:
5377ee3f6d2SAlex Vesker 	kfree(actions);
5387ee3f6d2SAlex Vesker out_err:
5397ee3f6d2SAlex Vesker 	mlx5_core_err(dev, "Failed to create dr rule err(%d)\n", err);
5406a48faeeSMaor Gottlieb 	return err;
5416a48faeeSMaor Gottlieb }
5426a48faeeSMaor Gottlieb 
5436a48faeeSMaor Gottlieb static int mlx5_cmd_dr_packet_reformat_alloc(struct mlx5_flow_root_namespace *ns,
5443f3f05abSYevgeny Kliteynik 					     struct mlx5_pkt_reformat_params *params,
5456a48faeeSMaor Gottlieb 					     enum mlx5_flow_namespace_type namespace,
5466a48faeeSMaor Gottlieb 					     struct mlx5_pkt_reformat *pkt_reformat)
5476a48faeeSMaor Gottlieb {
5486a48faeeSMaor Gottlieb 	struct mlx5dr_domain *dr_domain = ns->fs_dr_domain.dr_domain;
5496a48faeeSMaor Gottlieb 	struct mlx5dr_action *action;
5506a48faeeSMaor Gottlieb 	int dr_reformat;
5516a48faeeSMaor Gottlieb 
5523f3f05abSYevgeny Kliteynik 	switch (params->type) {
5536a48faeeSMaor Gottlieb 	case MLX5_REFORMAT_TYPE_L2_TO_VXLAN:
5546a48faeeSMaor Gottlieb 	case MLX5_REFORMAT_TYPE_L2_TO_NVGRE:
5556a48faeeSMaor Gottlieb 	case MLX5_REFORMAT_TYPE_L2_TO_L2_TUNNEL:
5566a48faeeSMaor Gottlieb 		dr_reformat = DR_ACTION_REFORMAT_TYP_L2_TO_TNL_L2;
5576a48faeeSMaor Gottlieb 		break;
5586a48faeeSMaor Gottlieb 	case MLX5_REFORMAT_TYPE_L3_TUNNEL_TO_L2:
5596a48faeeSMaor Gottlieb 		dr_reformat = DR_ACTION_REFORMAT_TYP_TNL_L3_TO_L2;
5606a48faeeSMaor Gottlieb 		break;
5616a48faeeSMaor Gottlieb 	case MLX5_REFORMAT_TYPE_L2_TO_L3_TUNNEL:
5626a48faeeSMaor Gottlieb 		dr_reformat = DR_ACTION_REFORMAT_TYP_L2_TO_TNL_L3;
5636a48faeeSMaor Gottlieb 		break;
5647ea9b398SYevgeny Kliteynik 	case MLX5_REFORMAT_TYPE_INSERT_HDR:
5657ea9b398SYevgeny Kliteynik 		dr_reformat = DR_ACTION_REFORMAT_TYP_INSERT_HDR;
5667ea9b398SYevgeny Kliteynik 		break;
5670139145fSYevgeny Kliteynik 	case MLX5_REFORMAT_TYPE_REMOVE_HDR:
5680139145fSYevgeny Kliteynik 		dr_reformat = DR_ACTION_REFORMAT_TYP_REMOVE_HDR;
5690139145fSYevgeny Kliteynik 		break;
5706a48faeeSMaor Gottlieb 	default:
5716a48faeeSMaor Gottlieb 		mlx5_core_err(ns->dev, "Packet-reformat not supported(%d)\n",
5723f3f05abSYevgeny Kliteynik 			      params->type);
5736a48faeeSMaor Gottlieb 		return -EOPNOTSUPP;
5746a48faeeSMaor Gottlieb 	}
5756a48faeeSMaor Gottlieb 
5766a48faeeSMaor Gottlieb 	action = mlx5dr_action_create_packet_reformat(dr_domain,
5776a48faeeSMaor Gottlieb 						      dr_reformat,
5783f3f05abSYevgeny Kliteynik 						      params->param_0,
5793f3f05abSYevgeny Kliteynik 						      params->param_1,
5803f3f05abSYevgeny Kliteynik 						      params->size,
5813f3f05abSYevgeny Kliteynik 						      params->data);
5826a48faeeSMaor Gottlieb 	if (!action) {
5836a48faeeSMaor Gottlieb 		mlx5_core_err(ns->dev, "Failed allocating packet-reformat action\n");
5846a48faeeSMaor Gottlieb 		return -EINVAL;
5856a48faeeSMaor Gottlieb 	}
5866a48faeeSMaor Gottlieb 
5876a48faeeSMaor Gottlieb 	pkt_reformat->action.dr_action = action;
5886a48faeeSMaor Gottlieb 
5896a48faeeSMaor Gottlieb 	return 0;
5906a48faeeSMaor Gottlieb }
5916a48faeeSMaor Gottlieb 
5926a48faeeSMaor Gottlieb static void mlx5_cmd_dr_packet_reformat_dealloc(struct mlx5_flow_root_namespace *ns,
5936a48faeeSMaor Gottlieb 						struct mlx5_pkt_reformat *pkt_reformat)
5946a48faeeSMaor Gottlieb {
5956a48faeeSMaor Gottlieb 	mlx5dr_action_destroy(pkt_reformat->action.dr_action);
5966a48faeeSMaor Gottlieb }
5976a48faeeSMaor Gottlieb 
5986a48faeeSMaor Gottlieb static int mlx5_cmd_dr_modify_header_alloc(struct mlx5_flow_root_namespace *ns,
5996a48faeeSMaor Gottlieb 					   u8 namespace, u8 num_actions,
6006a48faeeSMaor Gottlieb 					   void *modify_actions,
6016a48faeeSMaor Gottlieb 					   struct mlx5_modify_hdr *modify_hdr)
6026a48faeeSMaor Gottlieb {
6036a48faeeSMaor Gottlieb 	struct mlx5dr_domain *dr_domain = ns->fs_dr_domain.dr_domain;
6046a48faeeSMaor Gottlieb 	struct mlx5dr_action *action;
6056a48faeeSMaor Gottlieb 	size_t actions_sz;
6066a48faeeSMaor Gottlieb 
607d65dbedfSHuy Nguyen 	actions_sz = MLX5_UN_SZ_BYTES(set_add_copy_action_in_auto) *
6086a48faeeSMaor Gottlieb 		num_actions;
6096a48faeeSMaor Gottlieb 	action = mlx5dr_action_create_modify_header(dr_domain, 0,
6106a48faeeSMaor Gottlieb 						    actions_sz,
6116a48faeeSMaor Gottlieb 						    modify_actions);
6126a48faeeSMaor Gottlieb 	if (!action) {
6136a48faeeSMaor Gottlieb 		mlx5_core_err(ns->dev, "Failed allocating modify-header action\n");
6146a48faeeSMaor Gottlieb 		return -EINVAL;
6156a48faeeSMaor Gottlieb 	}
6166a48faeeSMaor Gottlieb 
6176a48faeeSMaor Gottlieb 	modify_hdr->action.dr_action = action;
6186a48faeeSMaor Gottlieb 
6196a48faeeSMaor Gottlieb 	return 0;
6206a48faeeSMaor Gottlieb }
6216a48faeeSMaor Gottlieb 
6226a48faeeSMaor Gottlieb static void mlx5_cmd_dr_modify_header_dealloc(struct mlx5_flow_root_namespace *ns,
6236a48faeeSMaor Gottlieb 					      struct mlx5_modify_hdr *modify_hdr)
6246a48faeeSMaor Gottlieb {
6256a48faeeSMaor Gottlieb 	mlx5dr_action_destroy(modify_hdr->action.dr_action);
6266a48faeeSMaor Gottlieb }
6276a48faeeSMaor Gottlieb 
628*e7e2519eSMaor Gottlieb static int
629*e7e2519eSMaor Gottlieb mlx5_cmd_dr_destroy_match_definer(struct mlx5_flow_root_namespace *ns,
630*e7e2519eSMaor Gottlieb 				  int definer_id)
631*e7e2519eSMaor Gottlieb {
632*e7e2519eSMaor Gottlieb 	return -EOPNOTSUPP;
633*e7e2519eSMaor Gottlieb }
634*e7e2519eSMaor Gottlieb 
635*e7e2519eSMaor Gottlieb static int mlx5_cmd_dr_create_match_definer(struct mlx5_flow_root_namespace *ns,
636*e7e2519eSMaor Gottlieb 					    u16 format_id, u32 *match_mask)
637*e7e2519eSMaor Gottlieb {
638*e7e2519eSMaor Gottlieb 	return -EOPNOTSUPP;
639*e7e2519eSMaor Gottlieb }
640*e7e2519eSMaor Gottlieb 
6416a48faeeSMaor Gottlieb static int mlx5_cmd_dr_delete_fte(struct mlx5_flow_root_namespace *ns,
6426a48faeeSMaor Gottlieb 				  struct mlx5_flow_table *ft,
6436a48faeeSMaor Gottlieb 				  struct fs_fte *fte)
6446a48faeeSMaor Gottlieb {
6456a48faeeSMaor Gottlieb 	struct mlx5_fs_dr_rule *rule = &fte->fs_dr_rule;
6466a48faeeSMaor Gottlieb 	int err;
6476a48faeeSMaor Gottlieb 	int i;
6486a48faeeSMaor Gottlieb 
6496a48faeeSMaor Gottlieb 	if (mlx5_dr_is_fw_table(ft->flags))
6506a48faeeSMaor Gottlieb 		return mlx5_fs_cmd_get_fw_cmds()->delete_fte(ns, ft, fte);
6516a48faeeSMaor Gottlieb 
6526a48faeeSMaor Gottlieb 	err = mlx5dr_rule_destroy(rule->dr_rule);
6536a48faeeSMaor Gottlieb 	if (err)
6546a48faeeSMaor Gottlieb 		return err;
6556a48faeeSMaor Gottlieb 
6567ee3f6d2SAlex Vesker 	/* Free in reverse order to handle action dependencies */
6577ee3f6d2SAlex Vesker 	for (i = rule->num_actions - 1; i >= 0; i--)
6586a48faeeSMaor Gottlieb 		if (!IS_ERR_OR_NULL(rule->dr_actions[i]))
6596a48faeeSMaor Gottlieb 			mlx5dr_action_destroy(rule->dr_actions[i]);
6606a48faeeSMaor Gottlieb 
6616a48faeeSMaor Gottlieb 	kfree(rule->dr_actions);
6626a48faeeSMaor Gottlieb 	return 0;
6636a48faeeSMaor Gottlieb }
6646a48faeeSMaor Gottlieb 
665a2ebfbb7SYevgeny Kliteynik static int mlx5_cmd_dr_update_fte(struct mlx5_flow_root_namespace *ns,
666a2ebfbb7SYevgeny Kliteynik 				  struct mlx5_flow_table *ft,
667a2ebfbb7SYevgeny Kliteynik 				  struct mlx5_flow_group *group,
668a2ebfbb7SYevgeny Kliteynik 				  int modify_mask,
669a2ebfbb7SYevgeny Kliteynik 				  struct fs_fte *fte)
670a2ebfbb7SYevgeny Kliteynik {
671a2ebfbb7SYevgeny Kliteynik 	struct fs_fte fte_tmp = {};
672a2ebfbb7SYevgeny Kliteynik 	int ret;
673a2ebfbb7SYevgeny Kliteynik 
674a2ebfbb7SYevgeny Kliteynik 	if (mlx5_dr_is_fw_table(ft->flags))
675a2ebfbb7SYevgeny Kliteynik 		return mlx5_fs_cmd_get_fw_cmds()->update_fte(ns, ft, group, modify_mask, fte);
676a2ebfbb7SYevgeny Kliteynik 
677a2ebfbb7SYevgeny Kliteynik 	/* Backup current dr rule details */
678a2ebfbb7SYevgeny Kliteynik 	fte_tmp.fs_dr_rule = fte->fs_dr_rule;
679a2ebfbb7SYevgeny Kliteynik 	memset(&fte->fs_dr_rule, 0, sizeof(struct mlx5_fs_dr_rule));
680a2ebfbb7SYevgeny Kliteynik 
681a2ebfbb7SYevgeny Kliteynik 	/* First add the new updated rule, then delete the old rule */
682a2ebfbb7SYevgeny Kliteynik 	ret = mlx5_cmd_dr_create_fte(ns, ft, group, fte);
683a2ebfbb7SYevgeny Kliteynik 	if (ret)
684a2ebfbb7SYevgeny Kliteynik 		goto restore_fte;
685a2ebfbb7SYevgeny Kliteynik 
686a2ebfbb7SYevgeny Kliteynik 	ret = mlx5_cmd_dr_delete_fte(ns, ft, &fte_tmp);
687a2ebfbb7SYevgeny Kliteynik 	WARN_ONCE(ret, "dr update fte duplicate rule deletion failed\n");
688a2ebfbb7SYevgeny Kliteynik 	return ret;
689a2ebfbb7SYevgeny Kliteynik 
690a2ebfbb7SYevgeny Kliteynik restore_fte:
691a2ebfbb7SYevgeny Kliteynik 	fte->fs_dr_rule = fte_tmp.fs_dr_rule;
692a2ebfbb7SYevgeny Kliteynik 	return ret;
693a2ebfbb7SYevgeny Kliteynik }
694a2ebfbb7SYevgeny Kliteynik 
6956a48faeeSMaor Gottlieb static int mlx5_cmd_dr_set_peer(struct mlx5_flow_root_namespace *ns,
6966a48faeeSMaor Gottlieb 				struct mlx5_flow_root_namespace *peer_ns)
6976a48faeeSMaor Gottlieb {
6986a48faeeSMaor Gottlieb 	struct mlx5dr_domain *peer_domain = NULL;
6996a48faeeSMaor Gottlieb 
7006a48faeeSMaor Gottlieb 	if (peer_ns)
7016a48faeeSMaor Gottlieb 		peer_domain = peer_ns->fs_dr_domain.dr_domain;
7026a48faeeSMaor Gottlieb 	mlx5dr_domain_set_peer(ns->fs_dr_domain.dr_domain,
7036a48faeeSMaor Gottlieb 			       peer_domain);
7046a48faeeSMaor Gottlieb 	return 0;
7056a48faeeSMaor Gottlieb }
7066a48faeeSMaor Gottlieb 
7076a48faeeSMaor Gottlieb static int mlx5_cmd_dr_create_ns(struct mlx5_flow_root_namespace *ns)
7086a48faeeSMaor Gottlieb {
7096a48faeeSMaor Gottlieb 	ns->fs_dr_domain.dr_domain =
7106a48faeeSMaor Gottlieb 		mlx5dr_domain_create(ns->dev,
7116a48faeeSMaor Gottlieb 				     MLX5DR_DOMAIN_TYPE_FDB);
7126a48faeeSMaor Gottlieb 	if (!ns->fs_dr_domain.dr_domain) {
7136a48faeeSMaor Gottlieb 		mlx5_core_err(ns->dev, "Failed to create dr flow namespace\n");
7146a48faeeSMaor Gottlieb 		return -EOPNOTSUPP;
7156a48faeeSMaor Gottlieb 	}
7166a48faeeSMaor Gottlieb 	return 0;
7176a48faeeSMaor Gottlieb }
7186a48faeeSMaor Gottlieb 
7196a48faeeSMaor Gottlieb static int mlx5_cmd_dr_destroy_ns(struct mlx5_flow_root_namespace *ns)
7206a48faeeSMaor Gottlieb {
7216a48faeeSMaor Gottlieb 	return mlx5dr_domain_destroy(ns->fs_dr_domain.dr_domain);
7226a48faeeSMaor Gottlieb }
7236a48faeeSMaor Gottlieb 
7246a48faeeSMaor Gottlieb bool mlx5_fs_dr_is_supported(struct mlx5_core_dev *dev)
7256a48faeeSMaor Gottlieb {
7266a48faeeSMaor Gottlieb 	return mlx5dr_is_supported(dev);
7276a48faeeSMaor Gottlieb }
7286a48faeeSMaor Gottlieb 
7296a48faeeSMaor Gottlieb static const struct mlx5_flow_cmds mlx5_flow_cmds_dr = {
7306a48faeeSMaor Gottlieb 	.create_flow_table = mlx5_cmd_dr_create_flow_table,
7316a48faeeSMaor Gottlieb 	.destroy_flow_table = mlx5_cmd_dr_destroy_flow_table,
7326a48faeeSMaor Gottlieb 	.modify_flow_table = mlx5_cmd_dr_modify_flow_table,
7336a48faeeSMaor Gottlieb 	.create_flow_group = mlx5_cmd_dr_create_flow_group,
7346a48faeeSMaor Gottlieb 	.destroy_flow_group = mlx5_cmd_dr_destroy_flow_group,
7356a48faeeSMaor Gottlieb 	.create_fte = mlx5_cmd_dr_create_fte,
7366a48faeeSMaor Gottlieb 	.update_fte = mlx5_cmd_dr_update_fte,
7376a48faeeSMaor Gottlieb 	.delete_fte = mlx5_cmd_dr_delete_fte,
7386a48faeeSMaor Gottlieb 	.update_root_ft = mlx5_cmd_dr_update_root_ft,
7396a48faeeSMaor Gottlieb 	.packet_reformat_alloc = mlx5_cmd_dr_packet_reformat_alloc,
7406a48faeeSMaor Gottlieb 	.packet_reformat_dealloc = mlx5_cmd_dr_packet_reformat_dealloc,
7416a48faeeSMaor Gottlieb 	.modify_header_alloc = mlx5_cmd_dr_modify_header_alloc,
7426a48faeeSMaor Gottlieb 	.modify_header_dealloc = mlx5_cmd_dr_modify_header_dealloc,
743*e7e2519eSMaor Gottlieb 	.create_match_definer = mlx5_cmd_dr_create_match_definer,
744*e7e2519eSMaor Gottlieb 	.destroy_match_definer = mlx5_cmd_dr_destroy_match_definer,
7456a48faeeSMaor Gottlieb 	.set_peer = mlx5_cmd_dr_set_peer,
7466a48faeeSMaor Gottlieb 	.create_ns = mlx5_cmd_dr_create_ns,
7476a48faeeSMaor Gottlieb 	.destroy_ns = mlx5_cmd_dr_destroy_ns,
7486a48faeeSMaor Gottlieb };
7496a48faeeSMaor Gottlieb 
7506a48faeeSMaor Gottlieb const struct mlx5_flow_cmds *mlx5_fs_cmd_get_dr_cmds(void)
7516a48faeeSMaor Gottlieb {
7526a48faeeSMaor Gottlieb 		return &mlx5_flow_cmds_dr;
7536a48faeeSMaor Gottlieb }
754