xref: /dpdk/drivers/net/mlx5/mlx5_flow.c (revision 25a2a0dc3de31ca0a6fbc9371cf3dd85dfd74b07)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016 6WIND S.A.
3  * Copyright 2016 Mellanox Technologies, Ltd
4  */
5 
6 #include <stdalign.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <stdbool.h>
10 #include <sys/queue.h>
11 
12 #include <rte_common.h>
13 #include <rte_ether.h>
14 #include <ethdev_driver.h>
15 #include <rte_eal_paging.h>
16 #include <rte_flow.h>
17 #include <rte_cycles.h>
18 #include <rte_flow_driver.h>
19 #include <rte_malloc.h>
20 #include <rte_ip.h>
21 
22 #include <mlx5_glue.h>
23 #include <mlx5_devx_cmds.h>
24 #include <mlx5_prm.h>
25 #include <mlx5_malloc.h>
26 
27 #include "mlx5_defs.h"
28 #include "mlx5.h"
29 #include "mlx5_flow.h"
30 #include "mlx5_flow_os.h"
31 #include "mlx5_rx.h"
32 #include "mlx5_tx.h"
33 #include "mlx5_common_os.h"
34 #include "rte_pmd_mlx5.h"
35 
36 /*
37  * Shared array for quick translation between port_id and vport mask/values
38  * used for HWS rules.
39  */
40 struct flow_hw_port_info mlx5_flow_hw_port_infos[RTE_MAX_ETHPORTS];
41 
42 struct tunnel_default_miss_ctx {
43 	uint16_t *queue;
44 	__extension__
45 	union {
46 		struct rte_flow_action_rss action_rss;
47 		struct rte_flow_action_queue miss_queue;
48 		struct rte_flow_action_jump miss_jump;
49 		uint8_t raw[0];
50 	};
51 };
52 
53 void
54 mlx5_indirect_list_handles_release(struct rte_eth_dev *dev)
55 {
56 	struct mlx5_priv *priv = dev->data->dev_private;
57 #ifdef HAVE_MLX5_HWS_SUPPORT
58 	struct rte_flow_error error;
59 #endif
60 
61 	while (!LIST_EMPTY(&priv->indirect_list_head)) {
62 		struct mlx5_indirect_list *e =
63 			LIST_FIRST(&priv->indirect_list_head);
64 
65 		LIST_REMOVE(e, entry);
66 		switch (e->type) {
67 #ifdef HAVE_MLX5_HWS_SUPPORT
68 		case MLX5_INDIRECT_ACTION_LIST_TYPE_MIRROR:
69 			mlx5_hw_mirror_destroy(dev, (struct mlx5_mirror *)e);
70 		break;
71 		case MLX5_INDIRECT_ACTION_LIST_TYPE_LEGACY:
72 			mlx5_destroy_legacy_indirect(dev, e);
73 			break;
74 		case MLX5_INDIRECT_ACTION_LIST_TYPE_REFORMAT:
75 			mlx5_reformat_action_destroy(dev,
76 				(struct rte_flow_action_list_handle *)e, &error);
77 			break;
78 #endif
79 		default:
80 			DRV_LOG(ERR, "invalid indirect list type");
81 			MLX5_ASSERT(false);
82 			break;
83 		}
84 	}
85 }
86 
87 static int
88 flow_tunnel_add_default_miss(struct rte_eth_dev *dev,
89 			     struct rte_flow *flow,
90 			     const struct rte_flow_attr *attr,
91 			     const struct rte_flow_action *app_actions,
92 			     uint32_t flow_idx,
93 			     const struct mlx5_flow_tunnel *tunnel,
94 			     struct tunnel_default_miss_ctx *ctx,
95 			     struct rte_flow_error *error);
96 static struct mlx5_flow_tunnel *
97 mlx5_find_tunnel_id(struct rte_eth_dev *dev, uint32_t id);
98 static void
99 mlx5_flow_tunnel_free(struct rte_eth_dev *dev, struct mlx5_flow_tunnel *tunnel);
100 static uint32_t
101 tunnel_flow_group_to_flow_table(struct rte_eth_dev *dev,
102 				const struct mlx5_flow_tunnel *tunnel,
103 				uint32_t group, uint32_t *table,
104 				struct rte_flow_error *error);
105 
106 /** Device flow drivers. */
107 extern const struct mlx5_flow_driver_ops mlx5_flow_verbs_drv_ops;
108 
109 const struct mlx5_flow_driver_ops mlx5_flow_null_drv_ops;
110 
111 const struct mlx5_flow_driver_ops *flow_drv_ops[] = {
112 	[MLX5_FLOW_TYPE_MIN] = &mlx5_flow_null_drv_ops,
113 #if defined(HAVE_IBV_FLOW_DV_SUPPORT) || !defined(HAVE_INFINIBAND_VERBS_H)
114 	[MLX5_FLOW_TYPE_DV] = &mlx5_flow_dv_drv_ops,
115 #endif
116 #ifdef HAVE_MLX5_HWS_SUPPORT
117 	[MLX5_FLOW_TYPE_HW] = &mlx5_flow_hw_drv_ops,
118 #endif
119 	[MLX5_FLOW_TYPE_VERBS] = &mlx5_flow_verbs_drv_ops,
120 	[MLX5_FLOW_TYPE_MAX] = &mlx5_flow_null_drv_ops
121 };
122 
123 /** Helper macro to build input graph for mlx5_flow_expand_rss(). */
124 #define MLX5_FLOW_EXPAND_RSS_NEXT(...) \
125 	(const int []){ \
126 		__VA_ARGS__, 0, \
127 	}
128 
129 /** Node object of input graph for mlx5_flow_expand_rss(). */
130 struct mlx5_flow_expand_node {
131 	const int *const next;
132 	/**<
133 	 * List of next node indexes. Index 0 is interpreted as a terminator.
134 	 */
135 	const enum rte_flow_item_type type;
136 	/**< Pattern item type of current node. */
137 	uint64_t rss_types;
138 	/**<
139 	 * RSS types bit-field associated with this node
140 	 * (see RTE_ETH_RSS_* definitions).
141 	 */
142 	uint64_t node_flags;
143 	/**<
144 	 *  Bit-fields that define how the node is used in the expansion.
145 	 * (see MLX5_EXPANSION_NODE_* definitions).
146 	 */
147 };
148 
149 /** Keep same format with mlx5_flow_expand_rss to share the buffer for expansion. */
150 struct mlx5_flow_expand_sqn {
151 	uint32_t entries; /** Number of entries */
152 	struct {
153 		struct rte_flow_item *pattern; /**< Expanded pattern array. */
154 		uint32_t priority; /**< Priority offset for each expansion. */
155 	} entry[];
156 };
157 
158 /* Optional expand field. The expansion alg will not go deeper. */
159 #define MLX5_EXPANSION_NODE_OPTIONAL (UINT64_C(1) << 0)
160 
161 /* The node is not added implicitly as expansion to the flow pattern.
162  * If the node type does not match the flow pattern item type, the
163  * expansion alg will go deeper to its next items.
164  * In the current implementation, the list of next nodes indexes can
165  * have up to one node with this flag set and it has to be the last
166  * node index (before the list terminator).
167  */
168 #define MLX5_EXPANSION_NODE_EXPLICIT (UINT64_C(1) << 1)
169 
170 /** Object returned by mlx5_flow_expand_rss(). */
171 struct mlx5_flow_expand_rss {
172 	uint32_t entries;
173 	/**< Number of entries @p patterns and @p priorities. */
174 	struct {
175 		struct rte_flow_item *pattern; /**< Expanded pattern array. */
176 		uint32_t priority; /**< Priority offset for each expansion. */
177 	} entry[];
178 };
179 
180 static void
181 mlx5_dbg__print_pattern(const struct rte_flow_item *item);
182 
183 static const struct mlx5_flow_expand_node *
184 mlx5_flow_expand_rss_adjust_node(const struct rte_flow_item *pattern,
185 		unsigned int item_idx,
186 		const struct mlx5_flow_expand_node graph[],
187 		const struct mlx5_flow_expand_node *node);
188 
189 static __rte_always_inline int
190 mlx5_need_cache_flow(const struct mlx5_priv *priv,
191 		     const struct rte_flow_attr *attr)
192 {
193 	return priv->isolated && priv->sh->config.dv_flow_en == 1 &&
194 		(attr ? !attr->group : true) &&
195 		priv->mode_info.mode == RTE_PMD_MLX5_FLOW_ENGINE_MODE_STANDBY &&
196 		(!priv->sh->config.dv_esw_en || !priv->sh->config.fdb_def_rule);
197 }
198 
199 static bool
200 mlx5_flow_is_rss_expandable_item(const struct rte_flow_item *item)
201 {
202 	switch (item->type) {
203 	case RTE_FLOW_ITEM_TYPE_ETH:
204 	case RTE_FLOW_ITEM_TYPE_VLAN:
205 	case RTE_FLOW_ITEM_TYPE_IPV4:
206 	case RTE_FLOW_ITEM_TYPE_IPV6:
207 	case RTE_FLOW_ITEM_TYPE_UDP:
208 	case RTE_FLOW_ITEM_TYPE_TCP:
209 	case RTE_FLOW_ITEM_TYPE_ESP:
210 	case RTE_FLOW_ITEM_TYPE_ICMP:
211 	case RTE_FLOW_ITEM_TYPE_ICMP6:
212 	case RTE_FLOW_ITEM_TYPE_VXLAN:
213 	case RTE_FLOW_ITEM_TYPE_NVGRE:
214 	case RTE_FLOW_ITEM_TYPE_GRE:
215 	case RTE_FLOW_ITEM_TYPE_GENEVE:
216 	case RTE_FLOW_ITEM_TYPE_MPLS:
217 	case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
218 	case RTE_FLOW_ITEM_TYPE_GRE_KEY:
219 	case RTE_FLOW_ITEM_TYPE_IPV6_FRAG_EXT:
220 	case RTE_FLOW_ITEM_TYPE_GTP:
221 		return true;
222 	default:
223 		break;
224 	}
225 	return false;
226 }
227 
228 /**
229  * Network Service Header (NSH) and its next protocol values
230  * are described in RFC-8393.
231  */
232 static enum rte_flow_item_type
233 mlx5_nsh_proto_to_item_type(uint8_t proto_spec, uint8_t proto_mask)
234 {
235 	enum rte_flow_item_type type;
236 
237 	switch (proto_mask & proto_spec) {
238 	case 0:
239 		type = RTE_FLOW_ITEM_TYPE_VOID;
240 		break;
241 	case RTE_VXLAN_GPE_TYPE_IPV4:
242 		type = RTE_FLOW_ITEM_TYPE_IPV4;
243 		break;
244 	case RTE_VXLAN_GPE_TYPE_IPV6:
245 		type = RTE_VXLAN_GPE_TYPE_IPV6;
246 		break;
247 	case RTE_VXLAN_GPE_TYPE_ETH:
248 		type = RTE_FLOW_ITEM_TYPE_ETH;
249 		break;
250 	default:
251 		type = RTE_FLOW_ITEM_TYPE_END;
252 	}
253 	return type;
254 }
255 
256 static enum rte_flow_item_type
257 mlx5_inet_proto_to_item_type(uint8_t proto_spec, uint8_t proto_mask)
258 {
259 	enum rte_flow_item_type type;
260 
261 	switch (proto_mask & proto_spec) {
262 	case 0:
263 		type = RTE_FLOW_ITEM_TYPE_VOID;
264 		break;
265 	case IPPROTO_UDP:
266 		type = RTE_FLOW_ITEM_TYPE_UDP;
267 		break;
268 	case IPPROTO_TCP:
269 		type = RTE_FLOW_ITEM_TYPE_TCP;
270 		break;
271 	case IPPROTO_IPIP:
272 		type = RTE_FLOW_ITEM_TYPE_IPV4;
273 		break;
274 	case IPPROTO_IPV6:
275 		type = RTE_FLOW_ITEM_TYPE_IPV6;
276 		break;
277 	case IPPROTO_ESP:
278 		type = RTE_FLOW_ITEM_TYPE_ESP;
279 		break;
280 	default:
281 		type = RTE_FLOW_ITEM_TYPE_END;
282 	}
283 	return type;
284 }
285 
286 static enum rte_flow_item_type
287 mlx5_ethertype_to_item_type(rte_be16_t type_spec,
288 			    rte_be16_t type_mask, bool is_tunnel)
289 {
290 	enum rte_flow_item_type type;
291 
292 	switch (rte_be_to_cpu_16(type_spec & type_mask)) {
293 	case 0:
294 		type = RTE_FLOW_ITEM_TYPE_VOID;
295 		break;
296 	case RTE_ETHER_TYPE_TEB:
297 		type = is_tunnel ?
298 		       RTE_FLOW_ITEM_TYPE_ETH : RTE_FLOW_ITEM_TYPE_END;
299 		break;
300 	case RTE_ETHER_TYPE_VLAN:
301 		type = !is_tunnel ?
302 		       RTE_FLOW_ITEM_TYPE_VLAN : RTE_FLOW_ITEM_TYPE_END;
303 		break;
304 	case RTE_ETHER_TYPE_IPV4:
305 		type = RTE_FLOW_ITEM_TYPE_IPV4;
306 		break;
307 	case RTE_ETHER_TYPE_IPV6:
308 		type = RTE_FLOW_ITEM_TYPE_IPV6;
309 		break;
310 	default:
311 		type = RTE_FLOW_ITEM_TYPE_END;
312 	}
313 	return type;
314 }
315 
316 static enum rte_flow_item_type
317 mlx5_flow_expand_rss_item_complete(const struct rte_flow_item *item)
318 {
319 #define MLX5_XSET_ITEM_MASK_SPEC(type, fld)                              \
320 	do {                                                             \
321 		const void *m = item->mask;                              \
322 		const void *s = item->spec;                              \
323 		mask = m ?                                               \
324 			((const struct rte_flow_item_##type *)m)->fld :  \
325 			rte_flow_item_##type##_mask.fld;                 \
326 		spec = ((const struct rte_flow_item_##type *)s)->fld;    \
327 	} while (0)
328 
329 	enum rte_flow_item_type ret;
330 	uint16_t spec, mask;
331 
332 	if (item == NULL || item->spec == NULL)
333 		return RTE_FLOW_ITEM_TYPE_VOID;
334 	switch (item->type) {
335 	case RTE_FLOW_ITEM_TYPE_ETH:
336 		MLX5_XSET_ITEM_MASK_SPEC(eth, hdr.ether_type);
337 		if (!mask)
338 			return RTE_FLOW_ITEM_TYPE_VOID;
339 		ret = mlx5_ethertype_to_item_type(spec, mask, false);
340 		break;
341 	case RTE_FLOW_ITEM_TYPE_VLAN:
342 		MLX5_XSET_ITEM_MASK_SPEC(vlan, hdr.eth_proto);
343 		if (!mask)
344 			return RTE_FLOW_ITEM_TYPE_VOID;
345 		ret = mlx5_ethertype_to_item_type(spec, mask, false);
346 		break;
347 	case RTE_FLOW_ITEM_TYPE_IPV4:
348 		MLX5_XSET_ITEM_MASK_SPEC(ipv4, hdr.next_proto_id);
349 		if (!mask)
350 			return RTE_FLOW_ITEM_TYPE_VOID;
351 		ret = mlx5_inet_proto_to_item_type(spec, mask);
352 		break;
353 	case RTE_FLOW_ITEM_TYPE_IPV6:
354 		MLX5_XSET_ITEM_MASK_SPEC(ipv6, hdr.proto);
355 		if (!mask)
356 			return RTE_FLOW_ITEM_TYPE_VOID;
357 		ret = mlx5_inet_proto_to_item_type(spec, mask);
358 		break;
359 	case RTE_FLOW_ITEM_TYPE_GENEVE:
360 		MLX5_XSET_ITEM_MASK_SPEC(geneve, protocol);
361 		ret = mlx5_ethertype_to_item_type(spec, mask, true);
362 		break;
363 	case RTE_FLOW_ITEM_TYPE_GRE:
364 		MLX5_XSET_ITEM_MASK_SPEC(gre, protocol);
365 		ret = mlx5_ethertype_to_item_type(spec, mask, true);
366 		break;
367 	case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
368 		MLX5_XSET_ITEM_MASK_SPEC(vxlan_gpe, hdr.proto);
369 		ret = mlx5_nsh_proto_to_item_type(spec, mask);
370 		break;
371 	default:
372 		ret = RTE_FLOW_ITEM_TYPE_VOID;
373 		break;
374 	}
375 	return ret;
376 #undef MLX5_XSET_ITEM_MASK_SPEC
377 }
378 
379 static const int *
380 mlx5_flow_expand_rss_skip_explicit(const struct mlx5_flow_expand_node graph[],
381 		const int *next_node)
382 {
383 	const struct mlx5_flow_expand_node *node = NULL;
384 	const int *next = next_node;
385 
386 	while (next && *next) {
387 		/*
388 		 * Skip the nodes with the MLX5_EXPANSION_NODE_EXPLICIT
389 		 * flag set, because they were not found in the flow pattern.
390 		 */
391 		node = &graph[*next];
392 		if (!(node->node_flags & MLX5_EXPANSION_NODE_EXPLICIT))
393 			break;
394 		next = node->next;
395 	}
396 	return next;
397 }
398 
399 #define MLX5_RSS_EXP_ELT_N 32
400 
401 /**
402  * Expand RSS flows into several possible flows according to the RSS hash
403  * fields requested and the driver capabilities.
404  *
405  * @param[out] buf
406  *   Buffer to store the result expansion.
407  * @param[in] size
408  *   Buffer size in bytes. If 0, @p buf can be NULL.
409  * @param[in] pattern
410  *   User flow pattern.
411  * @param[in] types
412  *   RSS types to expand (see RTE_ETH_RSS_* definitions).
413  * @param[in] graph
414  *   Input graph to expand @p pattern according to @p types.
415  * @param[in] graph_root_index
416  *   Index of root node in @p graph, typically 0.
417  *
418  * @return
419  *   A positive value representing the size of @p buf in bytes regardless of
420  *   @p size on success, a negative errno value otherwise and rte_errno is
421  *   set, the following errors are defined:
422  *
423  *   -E2BIG: graph-depth @p graph is too deep.
424  *   -EINVAL: @p size has not enough space for expanded pattern.
425  */
426 static int
427 mlx5_flow_expand_rss(struct mlx5_flow_expand_rss *buf, size_t size,
428 		     const struct rte_flow_item *pattern, uint64_t types,
429 		     const struct mlx5_flow_expand_node graph[],
430 		     int graph_root_index)
431 {
432 	const struct rte_flow_item *item;
433 	const struct mlx5_flow_expand_node *node = &graph[graph_root_index];
434 	const int *next_node;
435 	const int *stack[MLX5_RSS_EXP_ELT_N];
436 	int stack_pos = 0;
437 	struct rte_flow_item flow_items[MLX5_RSS_EXP_ELT_N];
438 	unsigned int i, item_idx, last_expand_item_idx = 0;
439 	size_t lsize;
440 	size_t user_pattern_size = 0;
441 	void *addr = NULL;
442 	const struct mlx5_flow_expand_node *next = NULL;
443 	struct rte_flow_item missed_item;
444 	int missed = 0;
445 	int elt = 0;
446 	const struct rte_flow_item *last_expand_item = NULL;
447 
448 	memset(&missed_item, 0, sizeof(missed_item));
449 	lsize = offsetof(struct mlx5_flow_expand_rss, entry) +
450 		MLX5_RSS_EXP_ELT_N * sizeof(buf->entry[0]);
451 	if (lsize > size)
452 		return -EINVAL;
453 	buf->entry[0].priority = 0;
454 	buf->entry[0].pattern = (void *)&buf->entry[MLX5_RSS_EXP_ELT_N];
455 	buf->entries = 0;
456 	addr = buf->entry[0].pattern;
457 	for (item = pattern, item_idx = 0;
458 			item->type != RTE_FLOW_ITEM_TYPE_END;
459 			item++, item_idx++) {
460 		if (!mlx5_flow_is_rss_expandable_item(item)) {
461 			user_pattern_size += sizeof(*item);
462 			continue;
463 		}
464 		last_expand_item = item;
465 		last_expand_item_idx = item_idx;
466 		i = 0;
467 		while (node->next && node->next[i]) {
468 			next = &graph[node->next[i]];
469 			if (next->type == item->type)
470 				break;
471 			if (next->node_flags & MLX5_EXPANSION_NODE_EXPLICIT) {
472 				node = next;
473 				i = 0;
474 			} else {
475 				++i;
476 			}
477 		}
478 		if (next)
479 			node = next;
480 		user_pattern_size += sizeof(*item);
481 	}
482 	user_pattern_size += sizeof(*item); /* Handle END item. */
483 	lsize += user_pattern_size;
484 	if (lsize > size)
485 		return -EINVAL;
486 	/* Copy the user pattern in the first entry of the buffer. */
487 	rte_memcpy(addr, pattern, user_pattern_size);
488 	addr = (void *)(((uintptr_t)addr) + user_pattern_size);
489 	buf->entries = 1;
490 	/* Start expanding. */
491 	memset(flow_items, 0, sizeof(flow_items));
492 	user_pattern_size -= sizeof(*item);
493 	/*
494 	 * Check if the last valid item has spec set, need complete pattern,
495 	 * and the pattern can be used for expansion.
496 	 */
497 	missed_item.type = mlx5_flow_expand_rss_item_complete(last_expand_item);
498 	if (missed_item.type == RTE_FLOW_ITEM_TYPE_END) {
499 		/* Item type END indicates expansion is not required. */
500 		return lsize;
501 	}
502 	if (missed_item.type != RTE_FLOW_ITEM_TYPE_VOID) {
503 		next = NULL;
504 		missed = 1;
505 		i = 0;
506 		while (node->next && node->next[i]) {
507 			next = &graph[node->next[i]];
508 			if (next->type == missed_item.type) {
509 				flow_items[0].type = missed_item.type;
510 				flow_items[1].type = RTE_FLOW_ITEM_TYPE_END;
511 				break;
512 			}
513 			if (next->node_flags & MLX5_EXPANSION_NODE_EXPLICIT) {
514 				node = next;
515 				i = 0;
516 			} else {
517 				++i;
518 			}
519 			next = NULL;
520 		}
521 	}
522 	if (next && missed) {
523 		elt = 2; /* missed item + item end. */
524 		node = next;
525 		lsize += elt * sizeof(*item) + user_pattern_size;
526 		if (lsize > size)
527 			return -EINVAL;
528 		if (node->rss_types & types) {
529 			buf->entry[buf->entries].priority = 1;
530 			buf->entry[buf->entries].pattern = addr;
531 			buf->entries++;
532 			rte_memcpy(addr, buf->entry[0].pattern,
533 				   user_pattern_size);
534 			addr = (void *)(((uintptr_t)addr) + user_pattern_size);
535 			rte_memcpy(addr, flow_items, elt * sizeof(*item));
536 			addr = (void *)(((uintptr_t)addr) +
537 					elt * sizeof(*item));
538 		}
539 	} else if (last_expand_item != NULL) {
540 		node = mlx5_flow_expand_rss_adjust_node(pattern,
541 				last_expand_item_idx, graph, node);
542 	}
543 	memset(flow_items, 0, sizeof(flow_items));
544 	next_node = mlx5_flow_expand_rss_skip_explicit(graph,
545 			node->next);
546 	stack[stack_pos] = next_node;
547 	node = next_node ? &graph[*next_node] : NULL;
548 	while (node) {
549 		flow_items[stack_pos].type = node->type;
550 		if (node->rss_types & types) {
551 			size_t n;
552 			/*
553 			 * compute the number of items to copy from the
554 			 * expansion and copy it.
555 			 * When the stack_pos is 0, there are 1 element in it,
556 			 * plus the addition END item.
557 			 */
558 			elt = stack_pos + 2;
559 			flow_items[stack_pos + 1].type = RTE_FLOW_ITEM_TYPE_END;
560 			lsize += elt * sizeof(*item) + user_pattern_size;
561 			if (lsize > size)
562 				return -EINVAL;
563 			n = elt * sizeof(*item);
564 			MLX5_ASSERT((buf->entries) < MLX5_RSS_EXP_ELT_N);
565 			buf->entry[buf->entries].priority =
566 				stack_pos + 1 + missed;
567 			buf->entry[buf->entries].pattern = addr;
568 			buf->entries++;
569 			rte_memcpy(addr, buf->entry[0].pattern,
570 				   user_pattern_size);
571 			addr = (void *)(((uintptr_t)addr) +
572 					user_pattern_size);
573 			rte_memcpy(addr, &missed_item,
574 				   missed * sizeof(*item));
575 			addr = (void *)(((uintptr_t)addr) +
576 				missed * sizeof(*item));
577 			rte_memcpy(addr, flow_items, n);
578 			addr = (void *)(((uintptr_t)addr) + n);
579 		}
580 		/* Go deeper. */
581 		if (!(node->node_flags & MLX5_EXPANSION_NODE_OPTIONAL) &&
582 				node->next) {
583 			next_node = mlx5_flow_expand_rss_skip_explicit(graph,
584 					node->next);
585 			if (stack_pos++ == MLX5_RSS_EXP_ELT_N) {
586 				rte_errno = E2BIG;
587 				return -rte_errno;
588 			}
589 			stack[stack_pos] = next_node;
590 		} else if (*(next_node + 1)) {
591 			/* Follow up with the next possibility. */
592 			next_node = mlx5_flow_expand_rss_skip_explicit(graph,
593 					++next_node);
594 		} else if (!stack_pos) {
595 			/*
596 			 * Completing the traverse over the different paths.
597 			 * The next_node is advanced to the terminator.
598 			 */
599 			++next_node;
600 		} else {
601 			/* Move to the next path. */
602 			while (stack_pos) {
603 				next_node = stack[--stack_pos];
604 				next_node++;
605 				if (*next_node)
606 					break;
607 			}
608 			next_node = mlx5_flow_expand_rss_skip_explicit(graph,
609 					next_node);
610 			stack[stack_pos] = next_node;
611 		}
612 		node = next_node && *next_node ? &graph[*next_node] : NULL;
613 	};
614 	return lsize;
615 }
616 
617 /**
618  * Expand SQN flows into several possible flows according to the Tx queue
619  * number
620  *
621  * @param[in] buf
622  *   Buffer to store the result expansion.
623  * @param[in] size
624  *   Buffer size in bytes. If 0, @p buf can be NULL.
625  * @param[in] pattern
626  *   User flow pattern.
627  * @param[in] sq_specs
628  *   Buffer to store sq spec.
629  *
630  * @return
631  *   0 for success and negative value for failure
632  *
633  */
634 static int
635 mlx5_flow_expand_sqn(struct mlx5_flow_expand_sqn *buf, size_t size,
636 		     const struct rte_flow_item *pattern,
637 		     struct mlx5_rte_flow_item_sq *sq_specs)
638 {
639 	const struct rte_flow_item *item;
640 	bool port_representor = false;
641 	size_t user_pattern_size = 0;
642 	struct rte_eth_dev *dev;
643 	struct mlx5_priv *priv;
644 	void *addr = NULL;
645 	uint16_t port_id;
646 	size_t lsize;
647 	int elt = 2;
648 	uint16_t i;
649 
650 	buf->entries = 0;
651 	for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
652 		if (item->type == RTE_FLOW_ITEM_TYPE_PORT_REPRESENTOR) {
653 			const struct rte_flow_item_ethdev *pid_v = item->spec;
654 
655 			if (!pid_v)
656 				return 0;
657 			port_id = pid_v->port_id;
658 			port_representor = true;
659 		}
660 		user_pattern_size += sizeof(*item);
661 	}
662 	if (!port_representor)
663 		return 0;
664 	dev = &rte_eth_devices[port_id];
665 	priv = dev->data->dev_private;
666 	buf->entry[0].pattern = (void *)&buf->entry[priv->txqs_n];
667 	lsize = offsetof(struct mlx5_flow_expand_sqn, entry) +
668 		sizeof(buf->entry[0]) * priv->txqs_n;
669 	if (lsize + (user_pattern_size + sizeof(struct rte_flow_item) * elt) * priv->txqs_n > size)
670 		return -EINVAL;
671 	addr = buf->entry[0].pattern;
672 	for (i = 0; i != priv->txqs_n; ++i) {
673 		struct rte_flow_item pattern_add[] = {
674 			{
675 				.type = (enum rte_flow_item_type)
676 					MLX5_RTE_FLOW_ITEM_TYPE_SQ,
677 				.spec = &sq_specs[i],
678 			},
679 			{
680 				.type = RTE_FLOW_ITEM_TYPE_END,
681 			},
682 		};
683 		struct mlx5_txq_ctrl *txq = mlx5_txq_get(dev, i);
684 
685 		if (txq == NULL)
686 			return -EINVAL;
687 		buf->entry[i].pattern = addr;
688 		sq_specs[i].queue = mlx5_txq_get_sqn(txq);
689 		mlx5_txq_release(dev, i);
690 		rte_memcpy(addr, pattern, user_pattern_size);
691 		addr = (void *)(((uintptr_t)addr) + user_pattern_size);
692 		rte_memcpy(addr, pattern_add, sizeof(struct rte_flow_item) * elt);
693 		addr = (void *)(((uintptr_t)addr) + sizeof(struct rte_flow_item) * elt);
694 		buf->entries++;
695 	}
696 	return 0;
697 }
698 
699 enum mlx5_expansion {
700 	MLX5_EXPANSION_ROOT,
701 	MLX5_EXPANSION_ROOT_OUTER,
702 	MLX5_EXPANSION_OUTER_ETH,
703 	MLX5_EXPANSION_OUTER_VLAN,
704 	MLX5_EXPANSION_OUTER_IPV4,
705 	MLX5_EXPANSION_OUTER_IPV4_UDP,
706 	MLX5_EXPANSION_OUTER_IPV4_TCP,
707 	MLX5_EXPANSION_OUTER_IPV4_ESP,
708 	MLX5_EXPANSION_OUTER_IPV4_ICMP,
709 	MLX5_EXPANSION_OUTER_IPV6,
710 	MLX5_EXPANSION_OUTER_IPV6_UDP,
711 	MLX5_EXPANSION_OUTER_IPV6_TCP,
712 	MLX5_EXPANSION_OUTER_IPV6_ESP,
713 	MLX5_EXPANSION_OUTER_IPV6_ICMP6,
714 	MLX5_EXPANSION_VXLAN,
715 	MLX5_EXPANSION_STD_VXLAN,
716 	MLX5_EXPANSION_L3_VXLAN,
717 	MLX5_EXPANSION_VXLAN_GPE,
718 	MLX5_EXPANSION_GRE,
719 	MLX5_EXPANSION_NVGRE,
720 	MLX5_EXPANSION_GRE_KEY,
721 	MLX5_EXPANSION_MPLS,
722 	MLX5_EXPANSION_ETH,
723 	MLX5_EXPANSION_VLAN,
724 	MLX5_EXPANSION_IPV4,
725 	MLX5_EXPANSION_IPV4_UDP,
726 	MLX5_EXPANSION_IPV4_TCP,
727 	MLX5_EXPANSION_IPV4_ESP,
728 	MLX5_EXPANSION_IPV4_ICMP,
729 	MLX5_EXPANSION_IPV6,
730 	MLX5_EXPANSION_IPV6_UDP,
731 	MLX5_EXPANSION_IPV6_TCP,
732 	MLX5_EXPANSION_IPV6_ESP,
733 	MLX5_EXPANSION_IPV6_ICMP6,
734 	MLX5_EXPANSION_IPV6_FRAG_EXT,
735 	MLX5_EXPANSION_GTP,
736 	MLX5_EXPANSION_GENEVE,
737 };
738 
739 /** Supported expansion of items. */
740 static const struct mlx5_flow_expand_node mlx5_support_expansion[] = {
741 	[MLX5_EXPANSION_ROOT] = {
742 		.next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH,
743 						  MLX5_EXPANSION_IPV4,
744 						  MLX5_EXPANSION_IPV6),
745 		.type = RTE_FLOW_ITEM_TYPE_END,
746 	},
747 	[MLX5_EXPANSION_ROOT_OUTER] = {
748 		.next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_ETH,
749 						  MLX5_EXPANSION_OUTER_IPV4,
750 						  MLX5_EXPANSION_OUTER_IPV6),
751 		.type = RTE_FLOW_ITEM_TYPE_END,
752 	},
753 	[MLX5_EXPANSION_OUTER_ETH] = {
754 		.next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_VLAN),
755 		.type = RTE_FLOW_ITEM_TYPE_ETH,
756 		.rss_types = 0,
757 	},
758 	[MLX5_EXPANSION_OUTER_VLAN] = {
759 		.next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_IPV4,
760 						  MLX5_EXPANSION_OUTER_IPV6),
761 		.type = RTE_FLOW_ITEM_TYPE_VLAN,
762 		.node_flags = MLX5_EXPANSION_NODE_EXPLICIT,
763 	},
764 	[MLX5_EXPANSION_OUTER_IPV4] = {
765 		.next = MLX5_FLOW_EXPAND_RSS_NEXT
766 			(MLX5_EXPANSION_OUTER_IPV4_UDP,
767 			 MLX5_EXPANSION_OUTER_IPV4_TCP,
768 			 MLX5_EXPANSION_OUTER_IPV4_ESP,
769 			 MLX5_EXPANSION_OUTER_IPV4_ICMP,
770 			 MLX5_EXPANSION_GRE,
771 			 MLX5_EXPANSION_NVGRE,
772 			 MLX5_EXPANSION_IPV4,
773 			 MLX5_EXPANSION_IPV6),
774 		.type = RTE_FLOW_ITEM_TYPE_IPV4,
775 		.rss_types = RTE_ETH_RSS_IPV4 | RTE_ETH_RSS_FRAG_IPV4 |
776 			RTE_ETH_RSS_NONFRAG_IPV4_OTHER,
777 	},
778 	[MLX5_EXPANSION_OUTER_IPV4_UDP] = {
779 		.next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_VXLAN,
780 						  MLX5_EXPANSION_VXLAN_GPE,
781 						  MLX5_EXPANSION_MPLS,
782 						  MLX5_EXPANSION_GENEVE,
783 						  MLX5_EXPANSION_GTP),
784 		.type = RTE_FLOW_ITEM_TYPE_UDP,
785 		.rss_types = RTE_ETH_RSS_NONFRAG_IPV4_UDP,
786 	},
787 	[MLX5_EXPANSION_OUTER_IPV4_TCP] = {
788 		.type = RTE_FLOW_ITEM_TYPE_TCP,
789 		.rss_types = RTE_ETH_RSS_NONFRAG_IPV4_TCP,
790 	},
791 	[MLX5_EXPANSION_OUTER_IPV4_ESP] = {
792 		.type = RTE_FLOW_ITEM_TYPE_ESP,
793 		.rss_types = RTE_ETH_RSS_ESP,
794 	},
795 	[MLX5_EXPANSION_OUTER_IPV4_ICMP] = {
796 		.type = RTE_FLOW_ITEM_TYPE_ICMP,
797 	},
798 	[MLX5_EXPANSION_OUTER_IPV6] = {
799 		.next = MLX5_FLOW_EXPAND_RSS_NEXT
800 			(MLX5_EXPANSION_OUTER_IPV6_UDP,
801 			 MLX5_EXPANSION_OUTER_IPV6_TCP,
802 			 MLX5_EXPANSION_OUTER_IPV6_ESP,
803 			 MLX5_EXPANSION_OUTER_IPV6_ICMP6,
804 			 MLX5_EXPANSION_IPV4,
805 			 MLX5_EXPANSION_IPV6,
806 			 MLX5_EXPANSION_GRE,
807 			 MLX5_EXPANSION_NVGRE),
808 		.type = RTE_FLOW_ITEM_TYPE_IPV6,
809 		.rss_types = RTE_ETH_RSS_IPV6 | RTE_ETH_RSS_FRAG_IPV6 |
810 			RTE_ETH_RSS_NONFRAG_IPV6_OTHER,
811 	},
812 	[MLX5_EXPANSION_OUTER_IPV6_UDP] = {
813 		.next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_VXLAN,
814 						  MLX5_EXPANSION_VXLAN_GPE,
815 						  MLX5_EXPANSION_MPLS,
816 						  MLX5_EXPANSION_GENEVE,
817 						  MLX5_EXPANSION_GTP),
818 		.type = RTE_FLOW_ITEM_TYPE_UDP,
819 		.rss_types = RTE_ETH_RSS_NONFRAG_IPV6_UDP,
820 	},
821 	[MLX5_EXPANSION_OUTER_IPV6_TCP] = {
822 		.type = RTE_FLOW_ITEM_TYPE_TCP,
823 		.rss_types = RTE_ETH_RSS_NONFRAG_IPV6_TCP,
824 	},
825 	[MLX5_EXPANSION_OUTER_IPV6_ESP] = {
826 		.type = RTE_FLOW_ITEM_TYPE_ESP,
827 		.rss_types = RTE_ETH_RSS_ESP,
828 	},
829 	[MLX5_EXPANSION_OUTER_IPV6_ICMP6] = {
830 		.type = RTE_FLOW_ITEM_TYPE_ICMP6,
831 	},
832 	[MLX5_EXPANSION_VXLAN] = {
833 		.next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH,
834 						  MLX5_EXPANSION_IPV4,
835 						  MLX5_EXPANSION_IPV6),
836 		.type = RTE_FLOW_ITEM_TYPE_VXLAN,
837 	},
838 	[MLX5_EXPANSION_STD_VXLAN] = {
839 			.next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH),
840 					.type = RTE_FLOW_ITEM_TYPE_VXLAN,
841 	},
842 	[MLX5_EXPANSION_L3_VXLAN] = {
843 			.next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4,
844 					MLX5_EXPANSION_IPV6),
845 					.type = RTE_FLOW_ITEM_TYPE_VXLAN,
846 	},
847 	[MLX5_EXPANSION_VXLAN_GPE] = {
848 		.next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH,
849 						  MLX5_EXPANSION_IPV4,
850 						  MLX5_EXPANSION_IPV6),
851 		.type = RTE_FLOW_ITEM_TYPE_VXLAN_GPE,
852 	},
853 	[MLX5_EXPANSION_GRE] = {
854 		.next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH,
855 						  MLX5_EXPANSION_IPV4,
856 						  MLX5_EXPANSION_IPV6,
857 						  MLX5_EXPANSION_GRE_KEY,
858 						  MLX5_EXPANSION_MPLS),
859 		.type = RTE_FLOW_ITEM_TYPE_GRE,
860 	},
861 	[MLX5_EXPANSION_GRE_KEY] = {
862 		.next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4,
863 						  MLX5_EXPANSION_IPV6,
864 						  MLX5_EXPANSION_MPLS),
865 		.type = RTE_FLOW_ITEM_TYPE_GRE_KEY,
866 		.node_flags = MLX5_EXPANSION_NODE_OPTIONAL,
867 	},
868 	[MLX5_EXPANSION_NVGRE] = {
869 		.next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH),
870 		.type = RTE_FLOW_ITEM_TYPE_NVGRE,
871 	},
872 	[MLX5_EXPANSION_MPLS] = {
873 		.next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4,
874 						  MLX5_EXPANSION_IPV6,
875 						  MLX5_EXPANSION_ETH),
876 		.type = RTE_FLOW_ITEM_TYPE_MPLS,
877 		.node_flags = MLX5_EXPANSION_NODE_OPTIONAL,
878 	},
879 	[MLX5_EXPANSION_ETH] = {
880 		.next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_VLAN),
881 		.type = RTE_FLOW_ITEM_TYPE_ETH,
882 	},
883 	[MLX5_EXPANSION_VLAN] = {
884 		.next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4,
885 						  MLX5_EXPANSION_IPV6),
886 		.type = RTE_FLOW_ITEM_TYPE_VLAN,
887 		.node_flags = MLX5_EXPANSION_NODE_EXPLICIT,
888 	},
889 	[MLX5_EXPANSION_IPV4] = {
890 		.next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4_UDP,
891 						  MLX5_EXPANSION_IPV4_TCP,
892 						  MLX5_EXPANSION_IPV4_ESP,
893 						  MLX5_EXPANSION_IPV4_ICMP),
894 		.type = RTE_FLOW_ITEM_TYPE_IPV4,
895 		.rss_types = RTE_ETH_RSS_IPV4 | RTE_ETH_RSS_FRAG_IPV4 |
896 			RTE_ETH_RSS_NONFRAG_IPV4_OTHER,
897 	},
898 	[MLX5_EXPANSION_IPV4_UDP] = {
899 		.type = RTE_FLOW_ITEM_TYPE_UDP,
900 		.rss_types = RTE_ETH_RSS_NONFRAG_IPV4_UDP,
901 	},
902 	[MLX5_EXPANSION_IPV4_TCP] = {
903 		.type = RTE_FLOW_ITEM_TYPE_TCP,
904 		.rss_types = RTE_ETH_RSS_NONFRAG_IPV4_TCP,
905 	},
906 	[MLX5_EXPANSION_IPV4_ESP] = {
907 		.type = RTE_FLOW_ITEM_TYPE_ESP,
908 		.rss_types = RTE_ETH_RSS_ESP,
909 	},
910 	[MLX5_EXPANSION_IPV4_ICMP] = {
911 		.type = RTE_FLOW_ITEM_TYPE_ICMP,
912 	},
913 	[MLX5_EXPANSION_IPV6] = {
914 		.next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV6_UDP,
915 						  MLX5_EXPANSION_IPV6_TCP,
916 						  MLX5_EXPANSION_IPV6_ESP,
917 						  MLX5_EXPANSION_IPV6_ICMP6,
918 						  MLX5_EXPANSION_IPV6_FRAG_EXT),
919 		.type = RTE_FLOW_ITEM_TYPE_IPV6,
920 		.rss_types = RTE_ETH_RSS_IPV6 | RTE_ETH_RSS_FRAG_IPV6 |
921 			RTE_ETH_RSS_NONFRAG_IPV6_OTHER,
922 	},
923 	[MLX5_EXPANSION_IPV6_UDP] = {
924 		.type = RTE_FLOW_ITEM_TYPE_UDP,
925 		.rss_types = RTE_ETH_RSS_NONFRAG_IPV6_UDP,
926 	},
927 	[MLX5_EXPANSION_IPV6_TCP] = {
928 		.type = RTE_FLOW_ITEM_TYPE_TCP,
929 		.rss_types = RTE_ETH_RSS_NONFRAG_IPV6_TCP,
930 	},
931 	[MLX5_EXPANSION_IPV6_ESP] = {
932 		.type = RTE_FLOW_ITEM_TYPE_ESP,
933 		.rss_types = RTE_ETH_RSS_ESP,
934 	},
935 	[MLX5_EXPANSION_IPV6_FRAG_EXT] = {
936 		.type = RTE_FLOW_ITEM_TYPE_IPV6_FRAG_EXT,
937 	},
938 	[MLX5_EXPANSION_IPV6_ICMP6] = {
939 		.type = RTE_FLOW_ITEM_TYPE_ICMP6,
940 	},
941 	[MLX5_EXPANSION_GTP] = {
942 		.next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4,
943 						  MLX5_EXPANSION_IPV6),
944 		.type = RTE_FLOW_ITEM_TYPE_GTP,
945 	},
946 	[MLX5_EXPANSION_GENEVE] = {
947 		.next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH,
948 						  MLX5_EXPANSION_IPV4,
949 						  MLX5_EXPANSION_IPV6),
950 		.type = RTE_FLOW_ITEM_TYPE_GENEVE,
951 	},
952 };
953 
954 static struct rte_flow_action_handle *
955 mlx5_action_handle_create(struct rte_eth_dev *dev,
956 			  const struct rte_flow_indir_action_conf *conf,
957 			  const struct rte_flow_action *action,
958 			  struct rte_flow_error *error);
959 static int mlx5_action_handle_destroy
960 				(struct rte_eth_dev *dev,
961 				 struct rte_flow_action_handle *handle,
962 				 struct rte_flow_error *error);
963 static int mlx5_action_handle_update
964 				(struct rte_eth_dev *dev,
965 				 struct rte_flow_action_handle *handle,
966 				 const void *update,
967 				 struct rte_flow_error *error);
968 static int mlx5_action_handle_query
969 				(struct rte_eth_dev *dev,
970 				 const struct rte_flow_action_handle *handle,
971 				 void *data,
972 				 struct rte_flow_error *error);
973 static int
974 mlx5_flow_tunnel_decap_set(struct rte_eth_dev *dev,
975 		    struct rte_flow_tunnel *app_tunnel,
976 		    struct rte_flow_action **actions,
977 		    uint32_t *num_of_actions,
978 		    struct rte_flow_error *error);
979 static int
980 mlx5_flow_tunnel_match(struct rte_eth_dev *dev,
981 		       struct rte_flow_tunnel *app_tunnel,
982 		       struct rte_flow_item **items,
983 		       uint32_t *num_of_items,
984 		       struct rte_flow_error *error);
985 static int
986 mlx5_flow_tunnel_item_release(struct rte_eth_dev *dev,
987 			      struct rte_flow_item *pmd_items,
988 			      uint32_t num_items, struct rte_flow_error *err);
989 static int
990 mlx5_flow_tunnel_action_release(struct rte_eth_dev *dev,
991 				struct rte_flow_action *pmd_actions,
992 				uint32_t num_actions,
993 				struct rte_flow_error *err);
994 static int
995 mlx5_flow_tunnel_get_restore_info(struct rte_eth_dev *dev,
996 				  struct rte_mbuf *m,
997 				  struct rte_flow_restore_info *info,
998 				  struct rte_flow_error *err);
999 static struct rte_flow_item_flex_handle *
1000 mlx5_flow_flex_item_create(struct rte_eth_dev *dev,
1001 			   const struct rte_flow_item_flex_conf *conf,
1002 			   struct rte_flow_error *error);
1003 static int
1004 mlx5_flow_flex_item_release(struct rte_eth_dev *dev,
1005 			    const struct rte_flow_item_flex_handle *handle,
1006 			    struct rte_flow_error *error);
1007 static int
1008 mlx5_flow_info_get(struct rte_eth_dev *dev,
1009 		   struct rte_flow_port_info *port_info,
1010 		   struct rte_flow_queue_info *queue_info,
1011 		   struct rte_flow_error *error);
1012 static int
1013 mlx5_flow_port_configure(struct rte_eth_dev *dev,
1014 			 const struct rte_flow_port_attr *port_attr,
1015 			 uint16_t nb_queue,
1016 			 const struct rte_flow_queue_attr *queue_attr[],
1017 			 struct rte_flow_error *err);
1018 
1019 static struct rte_flow_pattern_template *
1020 mlx5_flow_pattern_template_create(struct rte_eth_dev *dev,
1021 		const struct rte_flow_pattern_template_attr *attr,
1022 		const struct rte_flow_item items[],
1023 		struct rte_flow_error *error);
1024 
1025 static int
1026 mlx5_flow_pattern_template_destroy(struct rte_eth_dev *dev,
1027 				   struct rte_flow_pattern_template *template,
1028 				   struct rte_flow_error *error);
1029 static struct rte_flow_actions_template *
1030 mlx5_flow_actions_template_create(struct rte_eth_dev *dev,
1031 			const struct rte_flow_actions_template_attr *attr,
1032 			const struct rte_flow_action actions[],
1033 			const struct rte_flow_action masks[],
1034 			struct rte_flow_error *error);
1035 static int
1036 mlx5_flow_actions_template_destroy(struct rte_eth_dev *dev,
1037 				   struct rte_flow_actions_template *template,
1038 				   struct rte_flow_error *error);
1039 
1040 static struct rte_flow_template_table *
1041 mlx5_flow_table_create(struct rte_eth_dev *dev,
1042 		       const struct rte_flow_template_table_attr *attr,
1043 		       struct rte_flow_pattern_template *item_templates[],
1044 		       uint8_t nb_item_templates,
1045 		       struct rte_flow_actions_template *action_templates[],
1046 		       uint8_t nb_action_templates,
1047 		       struct rte_flow_error *error);
1048 static int
1049 mlx5_flow_table_destroy(struct rte_eth_dev *dev,
1050 			struct rte_flow_template_table *table,
1051 			struct rte_flow_error *error);
1052 static int
1053 mlx5_flow_group_set_miss_actions(struct rte_eth_dev *dev,
1054 				 uint32_t group_id,
1055 				 const struct rte_flow_group_attr *attr,
1056 				 const struct rte_flow_action actions[],
1057 				 struct rte_flow_error *error);
1058 
1059 static int
1060 mlx5_action_handle_query_update(struct rte_eth_dev *dev,
1061 				struct rte_flow_action_handle *handle,
1062 				const void *update, void *query,
1063 				enum rte_flow_query_update_mode qu_mode,
1064 				struct rte_flow_error *error);
1065 
1066 static struct rte_flow_action_list_handle *
1067 mlx5_action_list_handle_create(struct rte_eth_dev *dev,
1068 			       const struct rte_flow_indir_action_conf *conf,
1069 			       const struct rte_flow_action *actions,
1070 			       struct rte_flow_error *error);
1071 
1072 static int
1073 mlx5_action_list_handle_destroy(struct rte_eth_dev *dev,
1074 				struct rte_flow_action_list_handle *handle,
1075 				struct rte_flow_error *error);
1076 
1077 static int
1078 mlx5_flow_action_list_handle_query_update(struct rte_eth_dev *dev,
1079 					  const
1080 					  struct rte_flow_action_list_handle *handle,
1081 					  const void **update, void **query,
1082 					  enum rte_flow_query_update_mode mode,
1083 					  struct rte_flow_error *error);
1084 
1085 static int
1086 mlx5_flow_calc_table_hash(struct rte_eth_dev *dev,
1087 			  const struct rte_flow_template_table *table,
1088 			  const struct rte_flow_item pattern[],
1089 			  uint8_t pattern_template_index,
1090 			  uint32_t *hash, struct rte_flow_error *error);
1091 static int
1092 mlx5_flow_calc_encap_hash(struct rte_eth_dev *dev,
1093 			  const struct rte_flow_item pattern[],
1094 			  enum rte_flow_encap_hash_field dest_field,
1095 			  uint8_t *hash,
1096 			  struct rte_flow_error *error);
1097 
1098 static int
1099 mlx5_template_table_resize(struct rte_eth_dev *dev,
1100 			   struct rte_flow_template_table *table,
1101 			   uint32_t nb_rules, struct rte_flow_error *error);
1102 static int
1103 mlx5_flow_async_update_resized(struct rte_eth_dev *dev, uint32_t queue,
1104 			       const struct rte_flow_op_attr *attr,
1105 			       struct rte_flow *rule, void *user_data,
1106 			       struct rte_flow_error *error);
1107 static int
1108 mlx5_table_resize_complete(struct rte_eth_dev *dev,
1109 			   struct rte_flow_template_table *table,
1110 			   struct rte_flow_error *error);
1111 
1112 static const struct rte_flow_ops mlx5_flow_ops = {
1113 	.validate = mlx5_flow_validate,
1114 	.create = mlx5_flow_create,
1115 	.destroy = mlx5_flow_destroy,
1116 	.flush = mlx5_flow_flush,
1117 	.isolate = mlx5_flow_isolate,
1118 	.query = mlx5_flow_query,
1119 	.dev_dump = mlx5_flow_dev_dump,
1120 	.get_q_aged_flows = mlx5_flow_get_q_aged_flows,
1121 	.get_aged_flows = mlx5_flow_get_aged_flows,
1122 	.action_handle_create = mlx5_action_handle_create,
1123 	.action_handle_destroy = mlx5_action_handle_destroy,
1124 	.action_handle_update = mlx5_action_handle_update,
1125 	.action_handle_query = mlx5_action_handle_query,
1126 	.action_handle_query_update = mlx5_action_handle_query_update,
1127 	.action_list_handle_create = mlx5_action_list_handle_create,
1128 	.action_list_handle_destroy = mlx5_action_list_handle_destroy,
1129 	.tunnel_decap_set = mlx5_flow_tunnel_decap_set,
1130 	.tunnel_match = mlx5_flow_tunnel_match,
1131 	.tunnel_action_decap_release = mlx5_flow_tunnel_action_release,
1132 	.tunnel_item_release = mlx5_flow_tunnel_item_release,
1133 	.get_restore_info = mlx5_flow_tunnel_get_restore_info,
1134 	.flex_item_create = mlx5_flow_flex_item_create,
1135 	.flex_item_release = mlx5_flow_flex_item_release,
1136 	.info_get = mlx5_flow_info_get,
1137 	.pick_transfer_proxy = mlx5_flow_pick_transfer_proxy,
1138 	.configure = mlx5_flow_port_configure,
1139 	.pattern_template_create = mlx5_flow_pattern_template_create,
1140 	.pattern_template_destroy = mlx5_flow_pattern_template_destroy,
1141 	.actions_template_create = mlx5_flow_actions_template_create,
1142 	.actions_template_destroy = mlx5_flow_actions_template_destroy,
1143 	.template_table_create = mlx5_flow_table_create,
1144 	.template_table_destroy = mlx5_flow_table_destroy,
1145 	.group_set_miss_actions = mlx5_flow_group_set_miss_actions,
1146 	.action_list_handle_query_update =
1147 		mlx5_flow_action_list_handle_query_update,
1148 	.flow_calc_table_hash = mlx5_flow_calc_table_hash,
1149 	.flow_calc_encap_hash = mlx5_flow_calc_encap_hash,
1150 	.flow_template_table_resize = mlx5_template_table_resize,
1151 	.flow_update_resized = mlx5_flow_async_update_resized,
1152 	.flow_template_table_resize_complete = mlx5_table_resize_complete,
1153 };
1154 
1155 /* Tunnel information. */
1156 struct mlx5_flow_tunnel_info {
1157 	uint64_t tunnel; /**< Tunnel bit (see MLX5_FLOW_*). */
1158 	uint32_t ptype; /**< Tunnel Ptype (see RTE_PTYPE_*). */
1159 };
1160 
1161 static struct mlx5_flow_tunnel_info tunnels_info[] = {
1162 	{
1163 		.tunnel = MLX5_FLOW_LAYER_VXLAN,
1164 		.ptype = RTE_PTYPE_TUNNEL_VXLAN | RTE_PTYPE_L4_UDP,
1165 	},
1166 	{
1167 		.tunnel = MLX5_FLOW_LAYER_GENEVE,
1168 		.ptype = RTE_PTYPE_TUNNEL_GENEVE | RTE_PTYPE_L4_UDP,
1169 	},
1170 	{
1171 		.tunnel = MLX5_FLOW_LAYER_VXLAN_GPE,
1172 		.ptype = RTE_PTYPE_TUNNEL_VXLAN_GPE | RTE_PTYPE_L4_UDP,
1173 	},
1174 	{
1175 		.tunnel = MLX5_FLOW_LAYER_GRE,
1176 		.ptype = RTE_PTYPE_TUNNEL_GRE,
1177 	},
1178 	{
1179 		.tunnel = MLX5_FLOW_LAYER_MPLS | MLX5_FLOW_LAYER_OUTER_L4_UDP,
1180 		.ptype = RTE_PTYPE_TUNNEL_MPLS_IN_UDP | RTE_PTYPE_L4_UDP,
1181 	},
1182 	{
1183 		.tunnel = MLX5_FLOW_LAYER_MPLS,
1184 		.ptype = RTE_PTYPE_TUNNEL_MPLS_IN_GRE,
1185 	},
1186 	{
1187 		.tunnel = MLX5_FLOW_LAYER_NVGRE,
1188 		.ptype = RTE_PTYPE_TUNNEL_NVGRE,
1189 	},
1190 	{
1191 		.tunnel = MLX5_FLOW_LAYER_IPIP,
1192 		.ptype = RTE_PTYPE_TUNNEL_IP,
1193 	},
1194 	{
1195 		.tunnel = MLX5_FLOW_LAYER_IPV6_ENCAP,
1196 		.ptype = RTE_PTYPE_TUNNEL_IP,
1197 	},
1198 	{
1199 		.tunnel = MLX5_FLOW_LAYER_GTP,
1200 		.ptype = RTE_PTYPE_TUNNEL_GTPU,
1201 	},
1202 };
1203 
1204 
1205 
1206 /**
1207  * Translate tag ID to register.
1208  *
1209  * @param[in] dev
1210  *   Pointer to the Ethernet device structure.
1211  * @param[in] feature
1212  *   The feature that request the register.
1213  * @param[in] id
1214  *   The request register ID.
1215  * @param[out] error
1216  *   Error description in case of any.
1217  *
1218  * @return
1219  *   The request register on success, a negative errno
1220  *   value otherwise and rte_errno is set.
1221  */
1222 int
1223 mlx5_flow_get_reg_id(struct rte_eth_dev *dev,
1224 		     enum mlx5_feature_name feature,
1225 		     uint32_t id,
1226 		     struct rte_flow_error *error)
1227 {
1228 	struct mlx5_priv *priv = dev->data->dev_private;
1229 	struct mlx5_sh_config *config = &priv->sh->config;
1230 	struct mlx5_dev_registers *reg = &priv->sh->registers;
1231 	enum modify_reg start_reg;
1232 	bool skip_mtr_reg = false;
1233 
1234 	switch (feature) {
1235 	case MLX5_HAIRPIN_RX:
1236 		return REG_B;
1237 	case MLX5_HAIRPIN_TX:
1238 		return REG_A;
1239 	case MLX5_METADATA_RX:
1240 		switch (config->dv_xmeta_en) {
1241 		case MLX5_XMETA_MODE_LEGACY:
1242 			return REG_B;
1243 		case MLX5_XMETA_MODE_META16:
1244 			return REG_C_0;
1245 		case MLX5_XMETA_MODE_META32:
1246 			return REG_C_1;
1247 		case MLX5_XMETA_MODE_META32_HWS:
1248 			return REG_C_1;
1249 		}
1250 		break;
1251 	case MLX5_METADATA_TX:
1252 		if (config->dv_flow_en == 2 && config->dv_xmeta_en == MLX5_XMETA_MODE_META32_HWS) {
1253 			return REG_C_1;
1254 		} else {
1255 			return REG_A;
1256 		}
1257 	case MLX5_METADATA_FDB:
1258 		switch (config->dv_xmeta_en) {
1259 		case MLX5_XMETA_MODE_LEGACY:
1260 			return REG_NON;
1261 		case MLX5_XMETA_MODE_META16:
1262 			return REG_C_0;
1263 		case MLX5_XMETA_MODE_META32:
1264 			return REG_C_1;
1265 		case MLX5_XMETA_MODE_META32_HWS:
1266 			return REG_C_1;
1267 		}
1268 		break;
1269 	case MLX5_FLOW_MARK:
1270 		switch (config->dv_xmeta_en) {
1271 		case MLX5_XMETA_MODE_LEGACY:
1272 		case MLX5_XMETA_MODE_META32_HWS:
1273 			return REG_NON;
1274 		case MLX5_XMETA_MODE_META16:
1275 			return REG_C_1;
1276 		case MLX5_XMETA_MODE_META32:
1277 			return REG_C_0;
1278 		}
1279 		break;
1280 	case MLX5_MTR_ID:
1281 		/*
1282 		 * If meter color and meter id share one register, flow match
1283 		 * should use the meter color register for match.
1284 		 */
1285 		if (priv->mtr_reg_share)
1286 			return reg->aso_reg;
1287 		else
1288 			return reg->aso_reg != REG_C_2 ? REG_C_2 :
1289 			       REG_C_3;
1290 	case MLX5_MTR_COLOR:
1291 	case MLX5_ASO_FLOW_HIT:
1292 	case MLX5_ASO_CONNTRACK:
1293 	case MLX5_SAMPLE_ID:
1294 		/* All features use the same REG_C. */
1295 		MLX5_ASSERT(reg->aso_reg != REG_NON);
1296 		return reg->aso_reg;
1297 	case MLX5_COPY_MARK:
1298 		/*
1299 		 * Metadata COPY_MARK register using is in meter suffix sub
1300 		 * flow while with meter. It's safe to share the same register.
1301 		 */
1302 		return reg->aso_reg != REG_C_2 ? REG_C_2 : REG_C_3;
1303 	case MLX5_APP_TAG:
1304 		/*
1305 		 * If meter is enable, it will engage the register for color
1306 		 * match and flow match. If meter color match is not using the
1307 		 * REG_C_2, need to skip the REG_C_x be used by meter color
1308 		 * match.
1309 		 * If meter is disable, free to use all available registers.
1310 		 */
1311 		start_reg = reg->aso_reg != REG_C_2 ? REG_C_2 :
1312 			    (priv->mtr_reg_share ? REG_C_3 : REG_C_4);
1313 		skip_mtr_reg = !!(priv->mtr_en && start_reg == REG_C_2);
1314 		if (id > (uint32_t)(REG_C_7 - start_reg))
1315 			return rte_flow_error_set(error, EINVAL,
1316 						  RTE_FLOW_ERROR_TYPE_ITEM,
1317 						  NULL, "invalid tag id");
1318 		if (priv->sh->flow_mreg_c[id + start_reg - REG_C_0] == REG_NON)
1319 			return rte_flow_error_set(error, ENOTSUP,
1320 						  RTE_FLOW_ERROR_TYPE_ITEM,
1321 						  NULL, "unsupported tag id");
1322 		/*
1323 		 * This case means meter is using the REG_C_x great than 2.
1324 		 * Take care not to conflict with meter color REG_C_x.
1325 		 * If the available index REG_C_y >= REG_C_x, skip the
1326 		 * color register.
1327 		 */
1328 		if (skip_mtr_reg && priv->sh->flow_mreg_c
1329 		    [id + start_reg - REG_C_0] >= reg->aso_reg) {
1330 			if (id >= (uint32_t)(REG_C_7 - start_reg))
1331 				return rte_flow_error_set(error, EINVAL,
1332 						       RTE_FLOW_ERROR_TYPE_ITEM,
1333 							NULL, "invalid tag id");
1334 			if (priv->sh->flow_mreg_c
1335 			    [id + 1 + start_reg - REG_C_0] != REG_NON)
1336 				return priv->sh->flow_mreg_c
1337 					       [id + 1 + start_reg - REG_C_0];
1338 			return rte_flow_error_set(error, ENOTSUP,
1339 						  RTE_FLOW_ERROR_TYPE_ITEM,
1340 						  NULL, "unsupported tag id");
1341 		}
1342 		return priv->sh->flow_mreg_c[id + start_reg - REG_C_0];
1343 	}
1344 	MLX5_ASSERT(false);
1345 	return rte_flow_error_set(error, EINVAL,
1346 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1347 				  NULL, "invalid feature name");
1348 }
1349 
1350 /**
1351  * Check extensive flow metadata register support.
1352  *
1353  * @param dev
1354  *   Pointer to rte_eth_dev structure.
1355  *
1356  * @return
1357  *   True if device supports extensive flow metadata register, otherwise false.
1358  */
1359 bool
1360 mlx5_flow_ext_mreg_supported(struct rte_eth_dev *dev)
1361 {
1362 	struct mlx5_priv *priv = dev->data->dev_private;
1363 
1364 	/*
1365 	 * Having available reg_c can be regarded inclusively as supporting
1366 	 * extensive flow metadata register, which could mean,
1367 	 * - metadata register copy action by modify header.
1368 	 * - 16 modify header actions is supported.
1369 	 * - reg_c's are preserved across different domain (FDB and NIC) on
1370 	 *   packet loopback by flow lookup miss.
1371 	 */
1372 	return priv->sh->flow_mreg_c[2] != REG_NON;
1373 }
1374 
1375 /**
1376  * Get the lowest priority.
1377  *
1378  * @param[in] dev
1379  *   Pointer to the Ethernet device structure.
1380  * @param[in] attributes
1381  *   Pointer to device flow rule attributes.
1382  *
1383  * @return
1384  *   The value of lowest priority of flow.
1385  */
1386 uint32_t
1387 mlx5_get_lowest_priority(struct rte_eth_dev *dev,
1388 			  const struct rte_flow_attr *attr)
1389 {
1390 	struct mlx5_priv *priv = dev->data->dev_private;
1391 
1392 	if (!attr->group && !(attr->transfer && priv->fdb_def_rule))
1393 		return priv->sh->flow_max_priority - 2;
1394 	return MLX5_NON_ROOT_FLOW_MAX_PRIO - 1;
1395 }
1396 
1397 /**
1398  * Calculate matcher priority of the flow.
1399  *
1400  * @param[in] dev
1401  *   Pointer to the Ethernet device structure.
1402  * @param[in] attr
1403  *   Pointer to device flow rule attributes.
1404  * @param[in] subpriority
1405  *   The priority based on the items.
1406  * @param[in] external
1407  *   Flow is user flow.
1408  * @return
1409  *   The matcher priority of the flow.
1410  */
1411 uint16_t
1412 mlx5_get_matcher_priority(struct rte_eth_dev *dev,
1413 			  const struct rte_flow_attr *attr,
1414 			  uint32_t subpriority, bool external)
1415 {
1416 	uint16_t priority = (uint16_t)attr->priority;
1417 	struct mlx5_priv *priv = dev->data->dev_private;
1418 
1419 	/* NIC root rules */
1420 	if (!attr->group && !attr->transfer) {
1421 		if (attr->priority == MLX5_FLOW_LOWEST_PRIO_INDICATOR)
1422 			priority = priv->sh->flow_max_priority - 1;
1423 		return mlx5_os_flow_adjust_priority(dev, priority, subpriority);
1424 	/* FDB root rules */
1425 	} else if (attr->transfer && (!external || !priv->fdb_def_rule) &&
1426 		   attr->group == 0 &&
1427 		   attr->priority == MLX5_FLOW_LOWEST_PRIO_INDICATOR) {
1428 		return (priv->sh->flow_max_priority - 1) * 3;
1429 	}
1430 	if (attr->priority == MLX5_FLOW_LOWEST_PRIO_INDICATOR)
1431 		priority = MLX5_NON_ROOT_FLOW_MAX_PRIO;
1432 	return priority * 3 + subpriority;
1433 }
1434 
1435 /**
1436  * Verify the @p item specifications (spec, last, mask) are compatible with the
1437  * NIC capabilities.
1438  *
1439  * @param[in] item
1440  *   Item specification.
1441  * @param[in] mask
1442  *   @p item->mask or flow default bit-masks.
1443  * @param[in] nic_mask
1444  *   Bit-masks covering supported fields by the NIC to compare with user mask.
1445  * @param[in] size
1446  *   Bit-masks size in bytes.
1447  * @param[in] range_accepted
1448  *   True if range of values is accepted for specific fields, false otherwise.
1449  * @param[out] error
1450  *   Pointer to error structure.
1451  *
1452  * @return
1453  *   0 on success, a negative errno value otherwise and rte_errno is set.
1454  */
1455 int
1456 mlx5_flow_item_acceptable(const struct rte_eth_dev *dev,
1457 			  const struct rte_flow_item *item,
1458 			  const uint8_t *mask,
1459 			  const uint8_t *nic_mask,
1460 			  unsigned int size,
1461 			  bool range_accepted,
1462 			  struct rte_flow_error *error)
1463 {
1464 	unsigned int i;
1465 
1466 	MLX5_ASSERT(nic_mask);
1467 	for (i = 0; i < size; ++i)
1468 		if ((nic_mask[i] | mask[i]) != nic_mask[i])
1469 			return rte_flow_error_set(error, ENOTSUP,
1470 						  RTE_FLOW_ERROR_TYPE_ITEM,
1471 						  item,
1472 						  "mask enables non supported"
1473 						  " bits");
1474 	if (mlx5_hws_active(dev))
1475 		return 0;
1476 	if (!item->spec && (item->mask || item->last))
1477 		return rte_flow_error_set(error, EINVAL,
1478 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
1479 					  "mask/last without a spec is not"
1480 					  " supported");
1481 	if (item->spec && item->last && !range_accepted) {
1482 		uint8_t spec[size];
1483 		uint8_t last[size];
1484 		unsigned int i;
1485 		int ret;
1486 
1487 		for (i = 0; i < size; ++i) {
1488 			spec[i] = ((const uint8_t *)item->spec)[i] & mask[i];
1489 			last[i] = ((const uint8_t *)item->last)[i] & mask[i];
1490 		}
1491 		ret = memcmp(spec, last, size);
1492 		if (ret != 0)
1493 			return rte_flow_error_set(error, EINVAL,
1494 						  RTE_FLOW_ERROR_TYPE_ITEM,
1495 						  item,
1496 						  "range is not valid");
1497 	}
1498 	return 0;
1499 }
1500 
1501 /**
1502  * Adjust the hash fields according to the @p flow information.
1503  *
1504  * @param[in] dev_flow.
1505  *   Pointer to the mlx5_flow.
1506  * @param[in] tunnel
1507  *   1 when the hash field is for a tunnel item.
1508  * @param[in] layer_types
1509  *   RTE_ETH_RSS_* types.
1510  * @param[in] hash_fields
1511  *   Item hash fields.
1512  *
1513  * @return
1514  *   The hash fields that should be used.
1515  */
1516 uint64_t
1517 mlx5_flow_hashfields_adjust(struct mlx5_flow_rss_desc *rss_desc,
1518 			    int tunnel __rte_unused, uint64_t layer_types,
1519 			    uint64_t hash_fields)
1520 {
1521 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
1522 	int rss_request_inner = rss_desc->level >= 2;
1523 
1524 	/* Check RSS hash level for tunnel. */
1525 	if (tunnel && rss_request_inner)
1526 		hash_fields |= IBV_RX_HASH_INNER;
1527 	else if (tunnel || rss_request_inner)
1528 		return 0;
1529 #endif
1530 	/* Check if requested layer matches RSS hash fields. */
1531 	if (!(rss_desc->types & layer_types))
1532 		return 0;
1533 	return hash_fields;
1534 }
1535 
1536 /**
1537  * Lookup and set the ptype in the data Rx part.  A single Ptype can be used,
1538  * if several tunnel rules are used on this queue, the tunnel ptype will be
1539  * cleared.
1540  *
1541  * @param rxq_ctrl
1542  *   Rx queue to update.
1543  */
1544 static void
1545 flow_rxq_tunnel_ptype_update(struct mlx5_rxq_ctrl *rxq_ctrl)
1546 {
1547 	unsigned int i;
1548 	uint32_t tunnel_ptype = 0;
1549 
1550 	/* Look up for the ptype to use. */
1551 	for (i = 0; i != MLX5_FLOW_TUNNEL; ++i) {
1552 		if (!rxq_ctrl->flow_tunnels_n[i])
1553 			continue;
1554 		if (!tunnel_ptype) {
1555 			tunnel_ptype = tunnels_info[i].ptype;
1556 		} else {
1557 			tunnel_ptype = 0;
1558 			break;
1559 		}
1560 	}
1561 	rxq_ctrl->rxq.tunnel = tunnel_ptype;
1562 }
1563 
1564 /**
1565  * Set the Rx queue flags (Mark/Flag and Tunnel Ptypes) according to the device
1566  * flow.
1567  *
1568  * @param[in] dev
1569  *   Pointer to the Ethernet device structure.
1570  * @param[in] dev_handle
1571  *   Pointer to device flow handle structure.
1572  */
1573 void
1574 flow_drv_rxq_flags_set(struct rte_eth_dev *dev,
1575 		       struct mlx5_flow_handle *dev_handle)
1576 {
1577 	struct mlx5_priv *priv = dev->data->dev_private;
1578 	const int tunnel = !!(dev_handle->layers & MLX5_FLOW_LAYER_TUNNEL);
1579 	struct mlx5_ind_table_obj *ind_tbl = NULL;
1580 	unsigned int i;
1581 
1582 	if (dev_handle->fate_action == MLX5_FLOW_FATE_QUEUE) {
1583 		struct mlx5_hrxq *hrxq;
1584 
1585 		hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ],
1586 			      dev_handle->rix_hrxq);
1587 		if (hrxq)
1588 			ind_tbl = hrxq->ind_table;
1589 	} else if (dev_handle->fate_action == MLX5_FLOW_FATE_SHARED_RSS) {
1590 		struct mlx5_shared_action_rss *shared_rss;
1591 
1592 		shared_rss = mlx5_ipool_get
1593 			(priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS],
1594 			 dev_handle->rix_srss);
1595 		if (shared_rss)
1596 			ind_tbl = shared_rss->ind_tbl;
1597 	}
1598 	if (!ind_tbl)
1599 		return;
1600 	for (i = 0; i != ind_tbl->queues_n; ++i) {
1601 		int idx = ind_tbl->queues[i];
1602 		struct mlx5_rxq_ctrl *rxq_ctrl;
1603 
1604 		if (mlx5_is_external_rxq(dev, idx))
1605 			continue;
1606 		rxq_ctrl = mlx5_rxq_ctrl_get(dev, idx);
1607 		MLX5_ASSERT(rxq_ctrl != NULL);
1608 		if (rxq_ctrl == NULL)
1609 			continue;
1610 		/*
1611 		 * To support metadata register copy on Tx loopback,
1612 		 * this must be always enabled (metadata may arive
1613 		 * from other port - not from local flows only.
1614 		 */
1615 		if (tunnel) {
1616 			unsigned int j;
1617 
1618 			/* Increase the counter matching the flow. */
1619 			for (j = 0; j != MLX5_FLOW_TUNNEL; ++j) {
1620 				if ((tunnels_info[j].tunnel &
1621 				     dev_handle->layers) ==
1622 				    tunnels_info[j].tunnel) {
1623 					rxq_ctrl->flow_tunnels_n[j]++;
1624 					break;
1625 				}
1626 			}
1627 			flow_rxq_tunnel_ptype_update(rxq_ctrl);
1628 		}
1629 	}
1630 }
1631 
1632 static void
1633 flow_rxq_mark_flag_set(struct rte_eth_dev *dev)
1634 {
1635 	struct mlx5_priv *priv = dev->data->dev_private;
1636 	struct mlx5_rxq_ctrl *rxq_ctrl;
1637 	uint16_t port_id;
1638 
1639 	if (priv->sh->shared_mark_enabled)
1640 		return;
1641 	if (priv->master || priv->representor) {
1642 		MLX5_ETH_FOREACH_DEV(port_id, dev->device) {
1643 			struct mlx5_priv *opriv =
1644 				rte_eth_devices[port_id].data->dev_private;
1645 
1646 			if (!opriv ||
1647 			    opriv->sh != priv->sh ||
1648 			    opriv->domain_id != priv->domain_id ||
1649 			    opriv->mark_enabled)
1650 				continue;
1651 			LIST_FOREACH(rxq_ctrl, &opriv->rxqsctrl, next) {
1652 				rxq_ctrl->rxq.mark = 1;
1653 			}
1654 			opriv->mark_enabled = 1;
1655 		}
1656 	} else {
1657 		LIST_FOREACH(rxq_ctrl, &priv->rxqsctrl, next) {
1658 			rxq_ctrl->rxq.mark = 1;
1659 		}
1660 		priv->mark_enabled = 1;
1661 	}
1662 	priv->sh->shared_mark_enabled = 1;
1663 }
1664 
1665 /**
1666  * Set the Rx queue flags (Mark/Flag and Tunnel Ptypes) for a flow
1667  *
1668  * @param[in] dev
1669  *   Pointer to the Ethernet device structure.
1670  * @param[in] flow
1671  *   Pointer to flow structure.
1672  */
1673 static void
1674 flow_rxq_flags_set(struct rte_eth_dev *dev, struct rte_flow *flow)
1675 {
1676 	struct mlx5_priv *priv = dev->data->dev_private;
1677 	uint32_t handle_idx;
1678 	struct mlx5_flow_handle *dev_handle;
1679 	struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
1680 
1681 	MLX5_ASSERT(wks);
1682 	if (wks->mark)
1683 		flow_rxq_mark_flag_set(dev);
1684 	SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
1685 		       handle_idx, dev_handle, next)
1686 		flow_drv_rxq_flags_set(dev, dev_handle);
1687 }
1688 
1689 /**
1690  * Clear the Rx queue flags (Mark/Flag and Tunnel Ptype) associated with the
1691  * device flow if no other flow uses it with the same kind of request.
1692  *
1693  * @param dev
1694  *   Pointer to Ethernet device.
1695  * @param[in] dev_handle
1696  *   Pointer to the device flow handle structure.
1697  */
1698 static void
1699 flow_drv_rxq_flags_trim(struct rte_eth_dev *dev,
1700 			struct mlx5_flow_handle *dev_handle)
1701 {
1702 	struct mlx5_priv *priv = dev->data->dev_private;
1703 	const int tunnel = !!(dev_handle->layers & MLX5_FLOW_LAYER_TUNNEL);
1704 	struct mlx5_ind_table_obj *ind_tbl = NULL;
1705 	unsigned int i;
1706 
1707 	if (dev_handle->fate_action == MLX5_FLOW_FATE_QUEUE) {
1708 		struct mlx5_hrxq *hrxq;
1709 
1710 		hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ],
1711 			      dev_handle->rix_hrxq);
1712 		if (hrxq)
1713 			ind_tbl = hrxq->ind_table;
1714 	} else if (dev_handle->fate_action == MLX5_FLOW_FATE_SHARED_RSS) {
1715 		struct mlx5_shared_action_rss *shared_rss;
1716 
1717 		shared_rss = mlx5_ipool_get
1718 			(priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS],
1719 			 dev_handle->rix_srss);
1720 		if (shared_rss)
1721 			ind_tbl = shared_rss->ind_tbl;
1722 	}
1723 	if (!ind_tbl)
1724 		return;
1725 	MLX5_ASSERT(dev->data->dev_started);
1726 	for (i = 0; i != ind_tbl->queues_n; ++i) {
1727 		int idx = ind_tbl->queues[i];
1728 		struct mlx5_rxq_ctrl *rxq_ctrl;
1729 
1730 		if (mlx5_is_external_rxq(dev, idx))
1731 			continue;
1732 		rxq_ctrl = mlx5_rxq_ctrl_get(dev, idx);
1733 		MLX5_ASSERT(rxq_ctrl != NULL);
1734 		if (rxq_ctrl == NULL)
1735 			continue;
1736 		if (tunnel) {
1737 			unsigned int j;
1738 
1739 			/* Decrease the counter matching the flow. */
1740 			for (j = 0; j != MLX5_FLOW_TUNNEL; ++j) {
1741 				if ((tunnels_info[j].tunnel &
1742 				     dev_handle->layers) ==
1743 				    tunnels_info[j].tunnel) {
1744 					rxq_ctrl->flow_tunnels_n[j]--;
1745 					break;
1746 				}
1747 			}
1748 			flow_rxq_tunnel_ptype_update(rxq_ctrl);
1749 		}
1750 	}
1751 }
1752 
1753 /**
1754  * Clear the Rx queue flags (Mark/Flag and Tunnel Ptype) associated with the
1755  * @p flow if no other flow uses it with the same kind of request.
1756  *
1757  * @param dev
1758  *   Pointer to Ethernet device.
1759  * @param[in] flow
1760  *   Pointer to the flow.
1761  */
1762 static void
1763 flow_rxq_flags_trim(struct rte_eth_dev *dev, struct rte_flow *flow)
1764 {
1765 	struct mlx5_priv *priv = dev->data->dev_private;
1766 	uint32_t handle_idx;
1767 	struct mlx5_flow_handle *dev_handle;
1768 
1769 	SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
1770 		       handle_idx, dev_handle, next)
1771 		flow_drv_rxq_flags_trim(dev, dev_handle);
1772 }
1773 
1774 /**
1775  * Clear the Mark/Flag and Tunnel ptype information in all Rx queues.
1776  *
1777  * @param dev
1778  *   Pointer to Ethernet device.
1779  */
1780 static void
1781 flow_rxq_flags_clear(struct rte_eth_dev *dev)
1782 {
1783 	struct mlx5_priv *priv = dev->data->dev_private;
1784 	unsigned int i;
1785 
1786 	for (i = 0; i != priv->rxqs_n; ++i) {
1787 		struct mlx5_rxq_priv *rxq = mlx5_rxq_get(dev, i);
1788 		unsigned int j;
1789 
1790 		if (rxq == NULL || rxq->ctrl == NULL)
1791 			continue;
1792 		rxq->ctrl->rxq.mark = 0;
1793 		for (j = 0; j != MLX5_FLOW_TUNNEL; ++j)
1794 			rxq->ctrl->flow_tunnels_n[j] = 0;
1795 		rxq->ctrl->rxq.tunnel = 0;
1796 	}
1797 	priv->mark_enabled = 0;
1798 	priv->sh->shared_mark_enabled = 0;
1799 }
1800 
1801 static uint64_t mlx5_restore_info_dynflag;
1802 
1803 int
1804 mlx5_flow_rx_metadata_negotiate(struct rte_eth_dev *dev, uint64_t *features)
1805 {
1806 	struct mlx5_priv *priv = dev->data->dev_private;
1807 	uint64_t supported = 0;
1808 
1809 	if (!is_tunnel_offload_active(dev)) {
1810 		supported |= RTE_ETH_RX_METADATA_USER_FLAG;
1811 		supported |= RTE_ETH_RX_METADATA_USER_MARK;
1812 		if ((*features & RTE_ETH_RX_METADATA_TUNNEL_ID) != 0) {
1813 			DRV_LOG(DEBUG,
1814 				"tunnel offload was not activated, consider setting dv_xmeta_en=%d",
1815 				MLX5_XMETA_MODE_MISS_INFO);
1816 		}
1817 	} else {
1818 		supported |= RTE_ETH_RX_METADATA_TUNNEL_ID;
1819 		if ((*features & RTE_ETH_RX_METADATA_TUNNEL_ID) != 0 &&
1820 				mlx5_restore_info_dynflag == 0)
1821 			mlx5_restore_info_dynflag = rte_flow_restore_info_dynflag();
1822 	}
1823 
1824 	if (((*features & supported) & RTE_ETH_RX_METADATA_TUNNEL_ID) != 0)
1825 		priv->tunnel_enabled = 1;
1826 	else
1827 		priv->tunnel_enabled = 0;
1828 
1829 	*features &= supported;
1830 	return 0;
1831 }
1832 
1833 /**
1834  * Set the Rx queue dynamic metadata (mask and offset) for a flow
1835  *
1836  * @param[in] dev
1837  *   Pointer to the Ethernet device structure.
1838  */
1839 void
1840 mlx5_flow_rxq_dynf_set(struct rte_eth_dev *dev)
1841 {
1842 	struct mlx5_priv *priv = dev->data->dev_private;
1843 	uint64_t mark_flag = RTE_MBUF_F_RX_FDIR_ID;
1844 	unsigned int i;
1845 
1846 	if (priv->tunnel_enabled)
1847 		mark_flag |= mlx5_restore_info_dynflag;
1848 
1849 	for (i = 0; i != priv->rxqs_n; ++i) {
1850 		struct mlx5_rxq_priv *rxq = mlx5_rxq_get(dev, i);
1851 		struct mlx5_rxq_data *data;
1852 
1853 		if (rxq == NULL || rxq->ctrl == NULL)
1854 			continue;
1855 		data = &rxq->ctrl->rxq;
1856 		if (!data->shared || !rxq->ctrl->started) {
1857 			if (!rte_flow_dynf_metadata_avail()) {
1858 				data->dynf_meta = 0;
1859 				data->flow_meta_mask = 0;
1860 				data->flow_meta_offset = -1;
1861 				data->flow_meta_port_mask = 0;
1862 			} else {
1863 				data->dynf_meta = 1;
1864 				data->flow_meta_mask = rte_flow_dynf_metadata_mask;
1865 				data->flow_meta_offset = rte_flow_dynf_metadata_offs;
1866 				data->flow_meta_port_mask = priv->sh->dv_meta_mask;
1867 			}
1868 			data->mark_flag = mark_flag;
1869 		}
1870 	}
1871 }
1872 
1873 /*
1874  * return a pointer to the desired action in the list of actions.
1875  *
1876  * @param[in] actions
1877  *   The list of actions to search the action in.
1878  * @param[in] action
1879  *   The action to find.
1880  *
1881  * @return
1882  *   Pointer to the action in the list, if found. NULL otherwise.
1883  */
1884 const struct rte_flow_action *
1885 mlx5_flow_find_action(const struct rte_flow_action *actions,
1886 		      enum rte_flow_action_type action)
1887 {
1888 	if (actions == NULL)
1889 		return NULL;
1890 	for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++)
1891 		if (actions->type == action)
1892 			return actions;
1893 	return NULL;
1894 }
1895 
1896 /*
1897  * Validate the flag action.
1898  *
1899  * @param[in] action_flags
1900  *   Bit-fields that holds the actions detected until now.
1901  * @param[in] attr
1902  *   Attributes of flow that includes this action.
1903  * @param[out] error
1904  *   Pointer to error structure.
1905  *
1906  * @return
1907  *   0 on success, a negative errno value otherwise and rte_errno is set.
1908  */
1909 int
1910 mlx5_flow_validate_action_flag(uint64_t action_flags,
1911 			       const struct rte_flow_attr *attr,
1912 			       struct rte_flow_error *error)
1913 {
1914 	if (action_flags & MLX5_FLOW_ACTION_MARK)
1915 		return rte_flow_error_set(error, EINVAL,
1916 					  RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1917 					  "can't mark and flag in same flow");
1918 	if (action_flags & MLX5_FLOW_ACTION_FLAG)
1919 		return rte_flow_error_set(error, EINVAL,
1920 					  RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1921 					  "can't have 2 flag"
1922 					  " actions in same flow");
1923 	if (attr->egress)
1924 		return rte_flow_error_set(error, ENOTSUP,
1925 					  RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1926 					  "flag action not supported for "
1927 					  "egress");
1928 	return 0;
1929 }
1930 
1931 /*
1932  * Validate the mark action.
1933  *
1934  * @param[in] action
1935  *   Pointer to the queue action.
1936  * @param[in] action_flags
1937  *   Bit-fields that holds the actions detected until now.
1938  * @param[in] attr
1939  *   Attributes of flow that includes this action.
1940  * @param[out] error
1941  *   Pointer to error structure.
1942  *
1943  * @return
1944  *   0 on success, a negative errno value otherwise and rte_errno is set.
1945  */
1946 int
1947 mlx5_flow_validate_action_mark(struct rte_eth_dev *dev,
1948 			       const struct rte_flow_action *action,
1949 			       uint64_t action_flags,
1950 			       const struct rte_flow_attr *attr,
1951 			       struct rte_flow_error *error)
1952 {
1953 	const struct rte_flow_action_mark *mark = action->conf;
1954 
1955 	if (!mark)
1956 		return rte_flow_error_set(error, EINVAL,
1957 					  RTE_FLOW_ERROR_TYPE_ACTION,
1958 					  action,
1959 					  "configuration cannot be null");
1960 	if (mark->id >= MLX5_FLOW_MARK_MAX)
1961 		return rte_flow_error_set(error, EINVAL,
1962 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1963 					  &mark->id,
1964 					  "mark id must in 0 <= id < "
1965 					  RTE_STR(MLX5_FLOW_MARK_MAX));
1966 	if (action_flags & MLX5_FLOW_ACTION_FLAG)
1967 		return rte_flow_error_set(error, EINVAL,
1968 					  RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1969 					  "can't flag and mark in same flow");
1970 	if (action_flags & MLX5_FLOW_ACTION_MARK)
1971 		return rte_flow_error_set(error, EINVAL,
1972 					  RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1973 					  "can't have 2 mark actions in same"
1974 					  " flow");
1975 	if (attr->egress)
1976 		return rte_flow_error_set(error, ENOTSUP,
1977 					  RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1978 					  "mark action not supported for "
1979 					  "egress");
1980 	if (attr->transfer && mlx5_hws_active(dev))
1981 		return rte_flow_error_set(error, ENOTSUP,
1982 					  RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1983 					  "non-template mark action not supported for transfer");
1984 	return 0;
1985 }
1986 
1987 /*
1988  * Validate the drop action.
1989  *
1990  * @param[in] dev
1991  *   Pointer to the Ethernet device structure.
1992  * @param[in] is_root
1993  *   True if flow is validated for root table. False otherwise.
1994  * @param[in] attr
1995  *   Attributes of flow that includes this action.
1996  * @param[out] error
1997  *   Pointer to error structure.
1998  *
1999  * @return
2000  *   0 on success, a negative errno value otherwise and rte_errno is set.
2001  */
2002 int
2003 mlx5_flow_validate_action_drop(struct rte_eth_dev *dev,
2004 			       bool is_root,
2005 			       const struct rte_flow_attr *attr,
2006 			       struct rte_flow_error *error)
2007 {
2008 	struct mlx5_priv *priv = dev->data->dev_private;
2009 
2010 	if (priv->sh->config.dv_flow_en == 0 && attr->egress)
2011 		return rte_flow_error_set(error, ENOTSUP,
2012 					  RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
2013 					  "drop action not supported for "
2014 					  "egress");
2015 	if (priv->sh->config.dv_flow_en == 1 && is_root && (attr->egress || attr->transfer) &&
2016 	    !priv->sh->dr_root_drop_action_en) {
2017 		return rte_flow_error_set(error, ENOTSUP,
2018 					  RTE_FLOW_ERROR_TYPE_ATTR, NULL,
2019 					  "drop action not supported for "
2020 					  "egress and transfer on group 0");
2021 	}
2022 	return 0;
2023 }
2024 
2025 /*
2026  * Check if a queue specified in the queue action is valid.
2027  *
2028  * @param[in] dev
2029  *   Pointer to the Ethernet device structure.
2030  * @param[in] action
2031  *   Pointer to the queue action.
2032  * @param[out] error
2033  *   Pointer to error structure.
2034  *
2035  * @return
2036  *   0 on success, a negative errno value otherwise and rte_errno is set.
2037  */
2038 int
2039 mlx5_flow_validate_target_queue(struct rte_eth_dev *dev,
2040 				const struct rte_flow_action *action,
2041 				struct rte_flow_error *error)
2042 {
2043 	const struct rte_flow_action_queue *queue = action->conf;
2044 	struct mlx5_priv *priv = dev->data->dev_private;
2045 
2046 	if (mlx5_is_external_rxq(dev, queue->index))
2047 		return 0;
2048 	if (!priv->rxqs_n)
2049 		return rte_flow_error_set(error, EINVAL,
2050 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2051 					  NULL, "No Rx queues configured");
2052 	if (queue->index >= priv->rxqs_n)
2053 		return rte_flow_error_set(error, EINVAL,
2054 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2055 					  &queue->index,
2056 					  "queue index out of range");
2057 	if (mlx5_rxq_get(dev, queue->index) == NULL)
2058 		return rte_flow_error_set(error, EINVAL,
2059 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2060 					  &queue->index,
2061 					  "queue is not configured");
2062 	return 0;
2063 }
2064 
2065 /*
2066  * Validate the queue action.
2067  *
2068  * @param[in] action
2069  *   Pointer to the queue action.
2070  * @param[in] action_flags
2071  *   Bit-fields that holds the actions detected until now.
2072  * @param[in] dev
2073  *   Pointer to the Ethernet device structure.
2074  * @param[in] attr
2075  *   Attributes of flow that includes this action.
2076  * @param[out] error
2077  *   Pointer to error structure.
2078  *
2079  * @return
2080  *   0 on success, a negative errno value otherwise and rte_errno is set.
2081  */
2082 int
2083 mlx5_flow_validate_action_queue(const struct rte_flow_action *action,
2084 				uint64_t action_flags,
2085 				struct rte_eth_dev *dev,
2086 				const struct rte_flow_attr *attr,
2087 				struct rte_flow_error *error)
2088 {
2089 	const struct rte_flow_action_queue *queue = action->conf;
2090 
2091 	if (!queue)
2092 		return rte_flow_error_set(error, EINVAL,
2093 					  RTE_FLOW_ERROR_TYPE_ACTION, action,
2094 					  "no QUEUE action configuration");
2095 	if (action_flags & MLX5_FLOW_FATE_ACTIONS)
2096 		return rte_flow_error_set(error, EINVAL,
2097 					  RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2098 					  "can't have 2 fate actions in"
2099 					  " same flow");
2100 	if (attr->egress)
2101 		return rte_flow_error_set(error, ENOTSUP,
2102 					  RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
2103 					  "queue action not supported for egress.");
2104 	return mlx5_flow_validate_target_queue(dev, action, error);
2105 }
2106 
2107 /**
2108  * Validate queue numbers for device RSS.
2109  *
2110  * @param[in] dev
2111  *   Configured device.
2112  * @param[in] queues
2113  *   Array of queue numbers.
2114  * @param[in] queues_n
2115  *   Size of the @p queues array.
2116  * @param[out] error
2117  *   On error, filled with a textual error description.
2118  * @param[out] queue_idx
2119  *   On error, filled with an offending queue index in @p queues array.
2120  *
2121  * @return
2122  *   0 on success, a negative errno code on error.
2123  */
2124 static int
2125 mlx5_validate_rss_queues(struct rte_eth_dev *dev,
2126 			 const uint16_t *queues, uint32_t queues_n,
2127 			 const char **error, uint32_t *queue_idx)
2128 {
2129 	const struct mlx5_priv *priv = dev->data->dev_private;
2130 	bool is_hairpin = false;
2131 	bool is_ext_rss = false;
2132 	uint32_t i;
2133 
2134 	for (i = 0; i != queues_n; ++i) {
2135 		struct mlx5_rxq_ctrl *rxq_ctrl;
2136 
2137 		if (mlx5_is_external_rxq(dev, queues[0])) {
2138 			is_ext_rss = true;
2139 			continue;
2140 		}
2141 		if (is_ext_rss) {
2142 			*error = "Combining external and regular RSS queues is not supported";
2143 			*queue_idx = i;
2144 			return -ENOTSUP;
2145 		}
2146 		if (queues[i] >= priv->rxqs_n) {
2147 			*error = "queue index out of range";
2148 			*queue_idx = i;
2149 			return -EINVAL;
2150 		}
2151 		rxq_ctrl = mlx5_rxq_ctrl_get(dev, queues[i]);
2152 		if (rxq_ctrl == NULL) {
2153 			*error =  "queue is not configured";
2154 			*queue_idx = i;
2155 			return -EINVAL;
2156 		}
2157 		if (i == 0 && rxq_ctrl->is_hairpin)
2158 			is_hairpin = true;
2159 		if (is_hairpin != rxq_ctrl->is_hairpin) {
2160 			*error = "combining hairpin and regular RSS queues is not supported";
2161 			*queue_idx = i;
2162 			return -ENOTSUP;
2163 		}
2164 	}
2165 	return 0;
2166 }
2167 
2168 /*
2169  * Validate the rss action.
2170  *
2171  * @param[in] dev
2172  *   Pointer to the Ethernet device structure.
2173  * @param[in] action
2174  *   Pointer to the queue action.
2175  * @param[out] error
2176  *   Pointer to error structure.
2177  *
2178  * @return
2179  *   0 on success, a negative errno value otherwise and rte_errno is set.
2180  */
2181 int
2182 mlx5_validate_action_rss(struct rte_eth_dev *dev,
2183 			 const struct rte_flow_action *action,
2184 			 struct rte_flow_error *error)
2185 {
2186 	struct mlx5_priv *priv = dev->data->dev_private;
2187 	const struct rte_flow_action_rss *rss = action->conf;
2188 	int ret;
2189 	const char *message;
2190 	uint32_t queue_idx;
2191 
2192 	if (!rss)
2193 		return rte_flow_error_set
2194 			(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
2195 			 action, "no RSS action configuration");
2196 	if (rss->func == RTE_ETH_HASH_FUNCTION_SYMMETRIC_TOEPLITZ) {
2197 		DRV_LOG(WARNING, "port %u symmetric RSS supported with SORT",
2198 			dev->data->port_id);
2199 	} else if (rss->func != RTE_ETH_HASH_FUNCTION_DEFAULT &&
2200 		   rss->func != RTE_ETH_HASH_FUNCTION_TOEPLITZ)
2201 		return rte_flow_error_set(error, ENOTSUP,
2202 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2203 					  &rss->func,
2204 					  "RSS hash function not supported");
2205 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
2206 	if (rss->level > 2)
2207 #else
2208 	if (rss->level > 1)
2209 #endif
2210 		return rte_flow_error_set(error, ENOTSUP,
2211 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2212 					  &rss->level,
2213 					  "tunnel RSS is not supported");
2214 	/* allow RSS key_len 0 in case of NULL (default) RSS key. */
2215 	if (rss->key_len == 0 && rss->key != NULL)
2216 		return rte_flow_error_set(error, ENOTSUP,
2217 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2218 					  &rss->key_len,
2219 					  "RSS hash key length 0");
2220 	if (rss->key_len > 0 && rss->key_len < MLX5_RSS_HASH_KEY_LEN)
2221 		return rte_flow_error_set(error, ENOTSUP,
2222 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2223 					  &rss->key_len,
2224 					  "RSS hash key too small");
2225 	if (rss->key_len > MLX5_RSS_HASH_KEY_LEN)
2226 		return rte_flow_error_set(error, ENOTSUP,
2227 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2228 					  &rss->key_len,
2229 					  "RSS hash key too large");
2230 	if (rss->queue_num > priv->sh->dev_cap.ind_table_max_size)
2231 		return rte_flow_error_set(error, ENOTSUP,
2232 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2233 					  &rss->queue_num,
2234 					  "number of queues too large");
2235 	if (rss->types & MLX5_RSS_HF_MASK)
2236 		return rte_flow_error_set(error, ENOTSUP,
2237 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2238 					  &rss->types,
2239 					  "some RSS protocols are not"
2240 					  " supported");
2241 	if ((rss->types & (RTE_ETH_RSS_L3_SRC_ONLY | RTE_ETH_RSS_L3_DST_ONLY)) &&
2242 	    !(rss->types & RTE_ETH_RSS_IP))
2243 		return rte_flow_error_set(error, EINVAL,
2244 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
2245 					  "L3 partial RSS requested but L3 RSS"
2246 					  " type not specified");
2247 	if ((rss->types & (RTE_ETH_RSS_L4_SRC_ONLY | RTE_ETH_RSS_L4_DST_ONLY)) &&
2248 	    !(rss->types & (RTE_ETH_RSS_UDP | RTE_ETH_RSS_TCP)))
2249 		return rte_flow_error_set(error, EINVAL,
2250 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
2251 					  "L4 partial RSS requested but L4 RSS"
2252 					  " type not specified");
2253 	if (!priv->rxqs_n && priv->ext_rxqs == NULL)
2254 		return rte_flow_error_set(error, EINVAL,
2255 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2256 					  NULL, "No Rx queues configured");
2257 	if (!rss->queue_num)
2258 		return rte_flow_error_set(error, EINVAL,
2259 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2260 					  NULL, "No queues configured");
2261 	ret = mlx5_validate_rss_queues(dev, rss->queue, rss->queue_num,
2262 				       &message, &queue_idx);
2263 	if (ret != 0) {
2264 		return rte_flow_error_set(error, -ret,
2265 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2266 					  &rss->queue[queue_idx], message);
2267 	}
2268 	return 0;
2269 }
2270 
2271 /*
2272  * Validate the rss action.
2273  *
2274  * @param[in] action
2275  *   Pointer to the queue action.
2276  * @param[in] action_flags
2277  *   Bit-fields that holds the actions detected until now.
2278  * @param[in] dev
2279  *   Pointer to the Ethernet device structure.
2280  * @param[in] attr
2281  *   Attributes of flow that includes this action.
2282  * @param[in] item_flags
2283  *   Items that were detected.
2284  * @param[out] error
2285  *   Pointer to error structure.
2286  *
2287  * @return
2288  *   0 on success, a negative errno value otherwise and rte_errno is set.
2289  */
2290 int
2291 mlx5_flow_validate_action_rss(const struct rte_flow_action *action,
2292 			      uint64_t action_flags,
2293 			      struct rte_eth_dev *dev,
2294 			      const struct rte_flow_attr *attr,
2295 			      uint64_t item_flags,
2296 			      struct rte_flow_error *error)
2297 {
2298 	const struct rte_flow_action_rss *rss = action->conf;
2299 	int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
2300 	int ret;
2301 
2302 	if (action_flags & MLX5_FLOW_FATE_ACTIONS)
2303 		return rte_flow_error_set(error, EINVAL,
2304 					  RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2305 					  "can't have 2 fate actions"
2306 					  " in same flow");
2307 	ret = mlx5_validate_action_rss(dev, action, error);
2308 	if (ret)
2309 		return ret;
2310 	if (attr->egress)
2311 		return rte_flow_error_set(error, ENOTSUP,
2312 					  RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
2313 					  "rss action not supported for "
2314 					  "egress");
2315 	if (rss->level > 1 && !tunnel)
2316 		return rte_flow_error_set(error, EINVAL,
2317 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
2318 					  "inner RSS is not supported for "
2319 					  "non-tunnel flows");
2320 	if ((item_flags & MLX5_FLOW_LAYER_ECPRI) &&
2321 	    !(item_flags & MLX5_FLOW_LAYER_INNER_L4_UDP)) {
2322 		return rte_flow_error_set(error, EINVAL,
2323 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
2324 					  "RSS on eCPRI is not supported now");
2325 	}
2326 	if ((item_flags & MLX5_FLOW_LAYER_MPLS) &&
2327 	    !(item_flags &
2328 	      (MLX5_FLOW_LAYER_INNER_L2 | MLX5_FLOW_LAYER_INNER_L3)) &&
2329 	    rss->level > 1)
2330 		return rte_flow_error_set(error, EINVAL,
2331 					  RTE_FLOW_ERROR_TYPE_ITEM, NULL,
2332 					  "MPLS inner RSS needs to specify inner L2/L3 items after MPLS in pattern");
2333 	return 0;
2334 }
2335 
2336 /*
2337  * Validate the default miss action.
2338  *
2339  * @param[in] action_flags
2340  *   Bit-fields that holds the actions detected until now.
2341  * @param[out] error
2342  *   Pointer to error structure.
2343  *
2344  * @return
2345  *   0 on success, a negative errno value otherwise and rte_errno is set.
2346  */
2347 int
2348 mlx5_flow_validate_action_default_miss(uint64_t action_flags,
2349 				const struct rte_flow_attr *attr,
2350 				struct rte_flow_error *error)
2351 {
2352 	if (action_flags & MLX5_FLOW_FATE_ACTIONS)
2353 		return rte_flow_error_set(error, EINVAL,
2354 					  RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2355 					  "can't have 2 fate actions in"
2356 					  " same flow");
2357 	if (attr->egress)
2358 		return rte_flow_error_set(error, ENOTSUP,
2359 					  RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
2360 					  "default miss action not supported "
2361 					  "for egress");
2362 	if (attr->group)
2363 		return rte_flow_error_set(error, ENOTSUP,
2364 					  RTE_FLOW_ERROR_TYPE_ATTR_GROUP, NULL,
2365 					  "only group 0 is supported");
2366 	if (attr->transfer)
2367 		return rte_flow_error_set(error, ENOTSUP,
2368 					  RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
2369 					  NULL, "transfer is not supported");
2370 	return 0;
2371 }
2372 
2373 /*
2374  * Validate the count action.
2375  *
2376  * @param[in] dev
2377  *   Pointer to the Ethernet device structure.
2378  * @param[in] attr
2379  *   Attributes of flow that includes this action.
2380  * @param[out] error
2381  *   Pointer to error structure.
2382  *
2383  * @return
2384  *   0 on success, a negative errno value otherwise and rte_errno is set.
2385  */
2386 int
2387 mlx5_flow_validate_action_count(struct rte_eth_dev *dev __rte_unused,
2388 				const struct rte_flow_attr *attr,
2389 				struct rte_flow_error *error)
2390 {
2391 	if (attr->egress)
2392 		return rte_flow_error_set(error, ENOTSUP,
2393 					  RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
2394 					  "count action not supported for "
2395 					  "egress");
2396 	return 0;
2397 }
2398 
2399 /*
2400  * Validate the ASO CT action.
2401  *
2402  * @param[in] dev
2403  *   Pointer to the Ethernet device structure.
2404  * @param[in] conntrack
2405  *   Pointer to the CT action profile.
2406  * @param[out] error
2407  *   Pointer to error structure.
2408  *
2409  * @return
2410  *   0 on success, a negative errno value otherwise and rte_errno is set.
2411  */
2412 int
2413 mlx5_validate_action_ct(struct rte_eth_dev *dev,
2414 			const struct rte_flow_action_conntrack *conntrack,
2415 			struct rte_flow_error *error)
2416 {
2417 	RTE_SET_USED(dev);
2418 
2419 	if (conntrack->state > RTE_FLOW_CONNTRACK_STATE_TIME_WAIT)
2420 		return rte_flow_error_set(error, EINVAL,
2421 					  RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2422 					  "Invalid CT state");
2423 	if (conntrack->last_index > RTE_FLOW_CONNTRACK_FLAG_RST)
2424 		return rte_flow_error_set(error, EINVAL,
2425 					  RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2426 					  "Invalid last TCP packet flag");
2427 	return 0;
2428 }
2429 
2430 /**
2431  * Validate the level value for modify field action.
2432  *
2433  * @param[in] data
2434  *   Pointer to the rte_flow_field_data structure either src or dst.
2435  * @param[out] error
2436  *   Pointer to error structure.
2437  *
2438  * @return
2439  *   0 on success, a negative errno value otherwise and rte_errno is set.
2440  */
2441 int
2442 flow_validate_modify_field_level(const struct rte_flow_field_data *data,
2443 				 struct rte_flow_error *error)
2444 {
2445 	if (data->level == 0 || data->field == RTE_FLOW_FIELD_FLEX_ITEM)
2446 		return 0;
2447 	if (data->field != RTE_FLOW_FIELD_TAG &&
2448 	    data->field != (enum rte_flow_field_id)MLX5_RTE_FLOW_FIELD_META_REG) {
2449 		if (data->level > 1)
2450 			return rte_flow_error_set(error, ENOTSUP,
2451 						  RTE_FLOW_ERROR_TYPE_ACTION,
2452 						  NULL,
2453 						  "inner header fields modification is not supported");
2454 		return 0;
2455 	}
2456 	if (data->tag_index != 0)
2457 		return rte_flow_error_set(error, EINVAL,
2458 					  RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2459 					  "tag array can be provided using 'level' or 'tag_index' fields, not both");
2460 	/*
2461 	 * The tag array for RTE_FLOW_FIELD_TAG type is provided using
2462 	 * 'tag_index' field. In old API, it was provided using 'level' field
2463 	 * and it is still supported for backwards compatibility.
2464 	 */
2465 	DRV_LOG(DEBUG, "tag array provided in 'level' field instead of 'tag_index' field.");
2466 	return 0;
2467 }
2468 
2469 /**
2470  * Validate ICMP6 item.
2471  *
2472  * @param[in] item
2473  *   Item specification.
2474  * @param[in] item_flags
2475  *   Bit-fields that holds the items detected until now.
2476  * @param[in] ext_vlan_sup
2477  *   Whether extended VLAN features are supported or not.
2478  * @param[out] error
2479  *   Pointer to error structure.
2480  *
2481  * @return
2482  *   0 on success, a negative errno value otherwise and rte_errno is set.
2483  */
2484 int
2485 mlx5_flow_validate_item_icmp6(const struct rte_eth_dev *dev,
2486 			      const struct rte_flow_item *item,
2487 			      uint64_t item_flags,
2488 			      uint8_t target_protocol,
2489 			      struct rte_flow_error *error)
2490 {
2491 	const struct rte_flow_item_icmp6 *mask = item->mask;
2492 	const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
2493 	const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
2494 				      MLX5_FLOW_LAYER_OUTER_L3_IPV6;
2495 	const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
2496 				      MLX5_FLOW_LAYER_OUTER_L4;
2497 	int ret;
2498 
2499 	if (target_protocol != 0xFF && target_protocol != IPPROTO_ICMPV6)
2500 		return rte_flow_error_set(error, EINVAL,
2501 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
2502 					  "protocol filtering not compatible"
2503 					  " with ICMP6 layer");
2504 	if (!mlx5_hws_active(dev)) {
2505 		if (!(item_flags & l3m))
2506 			return rte_flow_error_set(error, EINVAL,
2507 						  RTE_FLOW_ERROR_TYPE_ITEM,
2508 						  item, "IPv6 is mandatory to filter on ICMP6");
2509 	}
2510 	if (item_flags & l4m)
2511 		return rte_flow_error_set(error, EINVAL,
2512 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
2513 					  "multiple L4 layers not supported");
2514 	if (!mask)
2515 		mask = &rte_flow_item_icmp6_mask;
2516 	ret = mlx5_flow_item_acceptable
2517 		(dev, item, (const uint8_t *)mask,
2518 		 (const uint8_t *)&rte_flow_item_icmp6_mask,
2519 		 sizeof(struct rte_flow_item_icmp6),
2520 		 MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2521 	if (ret < 0)
2522 		return ret;
2523 	return 0;
2524 }
2525 
2526 /**
2527  * Validate ICMP6 echo request/reply item.
2528  *
2529  * @param[in] item
2530  *   Item specification.
2531  * @param[in] item_flags
2532  *   Bit-fields that holds the items detected until now.
2533  * @param[in] ext_vlan_sup
2534  *   Whether extended VLAN features are supported or not.
2535  * @param[out] error
2536  *   Pointer to error structure.
2537  *
2538  * @return
2539  *   0 on success, a negative errno value otherwise and rte_errno is set.
2540  */
2541 int
2542 mlx5_flow_validate_item_icmp6_echo(const struct rte_eth_dev *dev,
2543 				   const struct rte_flow_item *item,
2544 				   uint64_t item_flags,
2545 				   uint8_t target_protocol,
2546 				   struct rte_flow_error *error)
2547 {
2548 	const struct rte_flow_item_icmp6_echo *mask = item->mask;
2549 	const struct rte_flow_item_icmp6_echo nic_mask = {
2550 		.hdr.base.type = 0xff,
2551 		.hdr.base.code = 0xff,
2552 		.hdr.identifier = RTE_BE16(0xffff),
2553 		.hdr.sequence = RTE_BE16(0xffff),
2554 	};
2555 	const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
2556 	const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
2557 				      MLX5_FLOW_LAYER_OUTER_L3_IPV6;
2558 	const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
2559 				      MLX5_FLOW_LAYER_OUTER_L4;
2560 	int ret;
2561 
2562 	if (target_protocol != 0xFF && target_protocol != IPPROTO_ICMPV6)
2563 		return rte_flow_error_set(error, EINVAL,
2564 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
2565 					  "protocol filtering not compatible"
2566 					  " with ICMP6 layer");
2567 	if (!mlx5_hws_active(dev)) {
2568 		if (!(item_flags & l3m))
2569 			return rte_flow_error_set(error, EINVAL,
2570 						  RTE_FLOW_ERROR_TYPE_ITEM,
2571 						  item, "IPv6 is mandatory to filter on ICMP6");
2572 	}
2573 	if (item_flags & l4m)
2574 		return rte_flow_error_set(error, EINVAL,
2575 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
2576 					  "multiple L4 layers not supported");
2577 	if (!mask)
2578 		mask = &nic_mask;
2579 	ret = mlx5_flow_item_acceptable
2580 		(dev, item, (const uint8_t *)mask,
2581 		 (const uint8_t *)&nic_mask,
2582 		 sizeof(struct rte_flow_item_icmp6_echo),
2583 		 MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2584 	if (ret < 0)
2585 		return ret;
2586 	return 0;
2587 }
2588 
2589 /**
2590  * Validate ICMP item.
2591  *
2592  * @param[in] item
2593  *   Item specification.
2594  * @param[in] item_flags
2595  *   Bit-fields that holds the items detected until now.
2596  * @param[out] error
2597  *   Pointer to error structure.
2598  *
2599  * @return
2600  *   0 on success, a negative errno value otherwise and rte_errno is set.
2601  */
2602 int
2603 mlx5_flow_validate_item_icmp(const struct rte_eth_dev *dev,
2604 			     const struct rte_flow_item *item,
2605 			     uint64_t item_flags,
2606 			     uint8_t target_protocol,
2607 			     struct rte_flow_error *error)
2608 {
2609 	const struct rte_flow_item_icmp *mask = item->mask;
2610 	const struct rte_flow_item_icmp nic_mask = {
2611 		.hdr.icmp_type = 0xff,
2612 		.hdr.icmp_code = 0xff,
2613 		.hdr.icmp_ident = RTE_BE16(0xffff),
2614 		.hdr.icmp_seq_nb = RTE_BE16(0xffff),
2615 	};
2616 	const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
2617 	const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
2618 				      MLX5_FLOW_LAYER_OUTER_L3_IPV4;
2619 	const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
2620 				      MLX5_FLOW_LAYER_OUTER_L4;
2621 	int ret;
2622 
2623 	if (target_protocol != 0xFF && target_protocol != IPPROTO_ICMP)
2624 		return rte_flow_error_set(error, EINVAL,
2625 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
2626 					  "protocol filtering not compatible"
2627 					  " with ICMP layer");
2628 	if (!mlx5_hws_active(dev)) {
2629 		if (!(item_flags & l3m))
2630 			return rte_flow_error_set(error, EINVAL,
2631 						  RTE_FLOW_ERROR_TYPE_ITEM,
2632 						  item, "IPv4 is mandatory to filter on ICMP");
2633 	}
2634 	if (item_flags & l4m)
2635 		return rte_flow_error_set(error, EINVAL,
2636 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
2637 					  "multiple L4 layers not supported");
2638 	if (!mask)
2639 		mask = &nic_mask;
2640 	ret = mlx5_flow_item_acceptable
2641 		(dev, item, (const uint8_t *)mask,
2642 		 (const uint8_t *)&nic_mask,
2643 		 sizeof(struct rte_flow_item_icmp),
2644 		 MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2645 	if (ret < 0)
2646 		return ret;
2647 	return 0;
2648 }
2649 
2650 /**
2651  * Validate Ethernet item.
2652  *
2653  * @param[in] item
2654  *   Item specification.
2655  * @param[in] item_flags
2656  *   Bit-fields that holds the items detected until now.
2657  * @param[out] error
2658  *   Pointer to error structure.
2659  *
2660  * @return
2661  *   0 on success, a negative errno value otherwise and rte_errno is set.
2662  */
2663 int
2664 mlx5_flow_validate_item_eth(const struct rte_eth_dev *dev,
2665 			    const struct rte_flow_item *item,
2666 			    uint64_t item_flags, bool ext_vlan_sup,
2667 			    struct rte_flow_error *error)
2668 {
2669 	const struct rte_flow_item_eth *mask = item->mask;
2670 	const struct rte_flow_item_eth nic_mask = {
2671 		.hdr.dst_addr.addr_bytes = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2672 		.hdr.src_addr.addr_bytes = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2673 		.hdr.ether_type = RTE_BE16(0xffff),
2674 		.has_vlan = ext_vlan_sup ? 1 : 0,
2675 	};
2676 	int ret;
2677 	int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
2678 	const uint64_t ethm = tunnel ? MLX5_FLOW_LAYER_INNER_L2	:
2679 				       MLX5_FLOW_LAYER_OUTER_L2;
2680 
2681 	if (item_flags & ethm)
2682 		return rte_flow_error_set(error, ENOTSUP,
2683 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
2684 					  "multiple L2 layers not supported");
2685 	if ((!tunnel && (item_flags & MLX5_FLOW_LAYER_OUTER_L3)) ||
2686 	    (tunnel && (item_flags & MLX5_FLOW_LAYER_INNER_L3)))
2687 		return rte_flow_error_set(error, EINVAL,
2688 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
2689 					  "L2 layer should not follow "
2690 					  "L3 layers");
2691 	if ((!tunnel && (item_flags & MLX5_FLOW_LAYER_OUTER_VLAN)) ||
2692 	    (tunnel && (item_flags & MLX5_FLOW_LAYER_INNER_VLAN)))
2693 		return rte_flow_error_set(error, EINVAL,
2694 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
2695 					  "L2 layer should not follow VLAN");
2696 	if (item_flags & MLX5_FLOW_LAYER_GTP)
2697 		return rte_flow_error_set(error, EINVAL,
2698 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
2699 					  "L2 layer should not follow GTP");
2700 	if (!mask)
2701 		mask = &rte_flow_item_eth_mask;
2702 	ret = mlx5_flow_item_acceptable(dev, item, (const uint8_t *)mask,
2703 					(const uint8_t *)&nic_mask,
2704 					sizeof(struct rte_flow_item_eth),
2705 					MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2706 	return ret;
2707 }
2708 
2709 /**
2710  * Validate VLAN item.
2711  *
2712  * @param[in] item
2713  *   Item specification.
2714  * @param[in] item_flags
2715  *   Bit-fields that holds the items detected until now.
2716  * @param[in] dev
2717  *   Ethernet device flow is being created on.
2718  * @param[out] error
2719  *   Pointer to error structure.
2720  *
2721  * @return
2722  *   0 on success, a negative errno value otherwise and rte_errno is set.
2723  */
2724 int
2725 mlx5_flow_validate_item_vlan(const struct rte_flow_item *item,
2726 			     uint64_t item_flags,
2727 			     struct rte_eth_dev *dev,
2728 			     struct rte_flow_error *error)
2729 {
2730 	const struct rte_flow_item_vlan *spec = item->spec;
2731 	const struct rte_flow_item_vlan *mask = item->mask;
2732 	const struct rte_flow_item_vlan nic_mask = {
2733 		.hdr.vlan_tci = RTE_BE16(UINT16_MAX),
2734 		.hdr.eth_proto = RTE_BE16(UINT16_MAX),
2735 	};
2736 	uint16_t vlan_tag = 0;
2737 	const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
2738 	int ret;
2739 	const uint64_t l34m = tunnel ? (MLX5_FLOW_LAYER_INNER_L3 |
2740 					MLX5_FLOW_LAYER_INNER_L4) :
2741 				       (MLX5_FLOW_LAYER_OUTER_L3 |
2742 					MLX5_FLOW_LAYER_OUTER_L4);
2743 	const uint64_t vlanm = tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
2744 					MLX5_FLOW_LAYER_OUTER_VLAN;
2745 
2746 	if (item_flags & vlanm)
2747 		return rte_flow_error_set(error, EINVAL,
2748 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
2749 					  "multiple VLAN layers not supported");
2750 	else if ((item_flags & l34m) != 0)
2751 		return rte_flow_error_set(error, EINVAL,
2752 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
2753 					  "VLAN cannot follow L3/L4 layer");
2754 	if (!mask)
2755 		mask = &rte_flow_item_vlan_mask;
2756 	ret = mlx5_flow_item_acceptable(dev, item, (const uint8_t *)mask,
2757 					(const uint8_t *)&nic_mask,
2758 					sizeof(struct rte_flow_item_vlan),
2759 					MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2760 	if (ret)
2761 		return ret;
2762 	if (!tunnel && mask->hdr.vlan_tci != RTE_BE16(0x0fff)) {
2763 		struct mlx5_priv *priv = dev->data->dev_private;
2764 
2765 		if (priv->vmwa_context) {
2766 			/*
2767 			 * Non-NULL context means we have a virtual machine
2768 			 * and SR-IOV enabled, we have to create VLAN interface
2769 			 * to make hypervisor to setup E-Switch vport
2770 			 * context correctly. We avoid creating the multiple
2771 			 * VLAN interfaces, so we cannot support VLAN tag mask.
2772 			 */
2773 			return rte_flow_error_set(error, EINVAL,
2774 						  RTE_FLOW_ERROR_TYPE_ITEM,
2775 						  item,
2776 						  "VLAN tag mask is not"
2777 						  " supported in virtual"
2778 						  " environment");
2779 		}
2780 	}
2781 	if (spec) {
2782 		vlan_tag = spec->hdr.vlan_tci;
2783 		vlan_tag &= mask->hdr.vlan_tci;
2784 	}
2785 	/*
2786 	 * From verbs perspective an empty VLAN is equivalent
2787 	 * to a packet without VLAN layer.
2788 	 */
2789 	if (!vlan_tag)
2790 		return rte_flow_error_set(error, EINVAL,
2791 					  RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
2792 					  item->spec,
2793 					  "VLAN cannot be empty");
2794 	return 0;
2795 }
2796 
2797 /**
2798  * Validate IPV4 item.
2799  *
2800  * @param[in] item
2801  *   Item specification.
2802  * @param[in] item_flags
2803  *   Bit-fields that holds the items detected until now.
2804  * @param[in] last_item
2805  *   Previous validated item in the pattern items.
2806  * @param[in] ether_type
2807  *   Type in the ethernet layer header (including dot1q).
2808  * @param[in] acc_mask
2809  *   Acceptable mask, if NULL default internal default mask
2810  *   will be used to check whether item fields are supported.
2811  * @param[in] range_accepted
2812  *   True if range of values is accepted for specific fields, false otherwise.
2813  * @param[out] error
2814  *   Pointer to error structure.
2815  *
2816  * @return
2817  *   0 on success, a negative errno value otherwise and rte_errno is set.
2818  */
2819 int
2820 mlx5_flow_validate_item_ipv4(const struct rte_eth_dev *dev,
2821 			     const struct rte_flow_item *item,
2822 			     uint64_t item_flags,
2823 			     uint64_t last_item,
2824 			     uint16_t ether_type,
2825 			     const struct rte_flow_item_ipv4 *acc_mask,
2826 			     bool range_accepted,
2827 			     struct rte_flow_error *error)
2828 {
2829 	const struct rte_flow_item_ipv4 *mask = item->mask;
2830 	const struct rte_flow_item_ipv4 *spec = item->spec;
2831 	const struct rte_flow_item_ipv4 nic_mask = {
2832 		.hdr = {
2833 			.src_addr = RTE_BE32(0xffffffff),
2834 			.dst_addr = RTE_BE32(0xffffffff),
2835 			.type_of_service = 0xff,
2836 			.next_proto_id = 0xff,
2837 		},
2838 	};
2839 	const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
2840 	const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
2841 				      MLX5_FLOW_LAYER_OUTER_L3;
2842 	const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
2843 				      MLX5_FLOW_LAYER_OUTER_L4;
2844 	int ret;
2845 	uint8_t next_proto = 0xFF;
2846 	const uint64_t l2_vlan = (MLX5_FLOW_LAYER_L2 |
2847 				  MLX5_FLOW_LAYER_OUTER_VLAN |
2848 				  MLX5_FLOW_LAYER_INNER_VLAN);
2849 
2850 	if ((last_item & l2_vlan) && ether_type &&
2851 	    ether_type != RTE_ETHER_TYPE_IPV4)
2852 		return rte_flow_error_set(error, EINVAL,
2853 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
2854 					  "IPv4 cannot follow L2/VLAN layer "
2855 					  "which ether type is not IPv4");
2856 	if (item_flags & MLX5_FLOW_LAYER_IPIP) {
2857 		if (mask && spec)
2858 			next_proto = mask->hdr.next_proto_id &
2859 				     spec->hdr.next_proto_id;
2860 		if (next_proto == IPPROTO_IPIP || next_proto == IPPROTO_IPV6)
2861 			return rte_flow_error_set(error, EINVAL,
2862 						  RTE_FLOW_ERROR_TYPE_ITEM,
2863 						  item,
2864 						  "multiple tunnel "
2865 						  "not supported");
2866 	}
2867 	if (item_flags & MLX5_FLOW_LAYER_IPV6_ENCAP)
2868 		return rte_flow_error_set(error, EINVAL,
2869 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
2870 					  "wrong tunnel type - IPv6 specified "
2871 					  "but IPv4 item provided");
2872 	if (item_flags & l3m)
2873 		return rte_flow_error_set(error, ENOTSUP,
2874 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
2875 					  "multiple L3 layers not supported");
2876 	else if (item_flags & l4m)
2877 		return rte_flow_error_set(error, EINVAL,
2878 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
2879 					  "L3 cannot follow an L4 layer.");
2880 	else if ((item_flags & MLX5_FLOW_LAYER_NVGRE) &&
2881 		  !(item_flags & MLX5_FLOW_LAYER_INNER_L2))
2882 		return rte_flow_error_set(error, EINVAL,
2883 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
2884 					  "L3 cannot follow an NVGRE layer.");
2885 	if (!mask)
2886 		mask = &rte_flow_item_ipv4_mask;
2887 	else if (mask->hdr.next_proto_id != 0 &&
2888 		 mask->hdr.next_proto_id != 0xff)
2889 		return rte_flow_error_set(error, EINVAL,
2890 					  RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
2891 					  "partial mask is not supported"
2892 					  " for protocol");
2893 	ret = mlx5_flow_item_acceptable(dev, item, (const uint8_t *)mask,
2894 					acc_mask ? (const uint8_t *)acc_mask
2895 						 : (const uint8_t *)&nic_mask,
2896 					sizeof(struct rte_flow_item_ipv4),
2897 					range_accepted, error);
2898 	if (ret < 0)
2899 		return ret;
2900 	return 0;
2901 }
2902 
2903 /**
2904  * Validate IPV6 item.
2905  *
2906  * @param[in] item
2907  *   Item specification.
2908  * @param[in] item_flags
2909  *   Bit-fields that holds the items detected until now.
2910  * @param[in] last_item
2911  *   Previous validated item in the pattern items.
2912  * @param[in] ether_type
2913  *   Type in the ethernet layer header (including dot1q).
2914  * @param[in] acc_mask
2915  *   Acceptable mask, if NULL default internal default mask
2916  *   will be used to check whether item fields are supported.
2917  * @param[out] error
2918  *   Pointer to error structure.
2919  *
2920  * @return
2921  *   0 on success, a negative errno value otherwise and rte_errno is set.
2922  */
2923 int
2924 mlx5_flow_validate_item_ipv6(const struct rte_eth_dev *dev,
2925 			     const struct rte_flow_item *item,
2926 			     uint64_t item_flags,
2927 			     uint64_t last_item,
2928 			     uint16_t ether_type,
2929 			     const struct rte_flow_item_ipv6 *acc_mask,
2930 			     struct rte_flow_error *error)
2931 {
2932 	const struct rte_flow_item_ipv6 *mask = item->mask;
2933 	const struct rte_flow_item_ipv6 *spec = item->spec;
2934 	const struct rte_flow_item_ipv6 nic_mask = {
2935 		.hdr = {
2936 			.src_addr = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2937 				      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2938 			.dst_addr = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2939 				      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2940 			.vtc_flow = RTE_BE32(0xffffffff),
2941 			.proto = 0xff,
2942 		},
2943 	};
2944 	const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
2945 	const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
2946 				      MLX5_FLOW_LAYER_OUTER_L3;
2947 	const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
2948 				      MLX5_FLOW_LAYER_OUTER_L4;
2949 	int ret;
2950 	uint8_t next_proto = 0xFF;
2951 	const uint64_t l2_vlan = (MLX5_FLOW_LAYER_L2 |
2952 				  MLX5_FLOW_LAYER_OUTER_VLAN |
2953 				  MLX5_FLOW_LAYER_INNER_VLAN);
2954 
2955 	if ((last_item & l2_vlan) && ether_type &&
2956 	    ether_type != RTE_ETHER_TYPE_IPV6)
2957 		return rte_flow_error_set(error, EINVAL,
2958 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
2959 					  "IPv6 cannot follow L2/VLAN layer "
2960 					  "which ether type is not IPv6");
2961 	if (mask && mask->hdr.proto == UINT8_MAX && spec)
2962 		next_proto = spec->hdr.proto;
2963 	if (item_flags & MLX5_FLOW_LAYER_IPIP) {
2964 		if (next_proto == IPPROTO_IPIP || next_proto == IPPROTO_IPV6)
2965 			return rte_flow_error_set(error, EINVAL,
2966 						  RTE_FLOW_ERROR_TYPE_ITEM,
2967 						  item,
2968 						  "multiple tunnel "
2969 						  "not supported");
2970 	}
2971 	if (next_proto == IPPROTO_HOPOPTS  ||
2972 	    next_proto == IPPROTO_ROUTING  ||
2973 	    next_proto == IPPROTO_FRAGMENT ||
2974 	    next_proto == IPPROTO_AH	   ||
2975 	    next_proto == IPPROTO_DSTOPTS  ||
2976 	    (!mlx5_hws_active(dev) && next_proto == IPPROTO_ESP))
2977 		return rte_flow_error_set(error, EINVAL,
2978 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
2979 					  "IPv6 proto (next header) should "
2980 					  "not be set as extension header");
2981 	if (item_flags & MLX5_FLOW_LAYER_IPIP)
2982 		return rte_flow_error_set(error, EINVAL,
2983 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
2984 					  "wrong tunnel type - IPv4 specified "
2985 					  "but IPv6 item provided");
2986 	if (item_flags & l3m)
2987 		return rte_flow_error_set(error, ENOTSUP,
2988 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
2989 					  "multiple L3 layers not supported");
2990 	else if (item_flags & l4m)
2991 		return rte_flow_error_set(error, EINVAL,
2992 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
2993 					  "L3 cannot follow an L4 layer.");
2994 	else if ((item_flags & MLX5_FLOW_LAYER_NVGRE) &&
2995 		  !(item_flags & MLX5_FLOW_LAYER_INNER_L2))
2996 		return rte_flow_error_set(error, EINVAL,
2997 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
2998 					  "L3 cannot follow an NVGRE layer.");
2999 	if (!mask)
3000 		mask = &rte_flow_item_ipv6_mask;
3001 	ret = mlx5_flow_item_acceptable(dev, item, (const uint8_t *)mask,
3002 					acc_mask ? (const uint8_t *)acc_mask
3003 						 : (const uint8_t *)&nic_mask,
3004 					sizeof(struct rte_flow_item_ipv6),
3005 					MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
3006 	if (ret < 0)
3007 		return ret;
3008 	return 0;
3009 }
3010 
3011 /**
3012  * Validate UDP item.
3013  *
3014  * @param[in] item
3015  *   Item specification.
3016  * @param[in] item_flags
3017  *   Bit-fields that holds the items detected until now.
3018  * @param[in] target_protocol
3019  *   The next protocol in the previous item.
3020  * @param[in] flow_mask
3021  *   mlx5 flow-specific (DV, verbs, etc.) supported header fields mask.
3022  * @param[out] error
3023  *   Pointer to error structure.
3024  *
3025  * @return
3026  *   0 on success, a negative errno value otherwise and rte_errno is set.
3027  */
3028 int
3029 mlx5_flow_validate_item_udp(const struct rte_eth_dev *dev,
3030 			    const struct rte_flow_item *item,
3031 			    uint64_t item_flags,
3032 			    uint8_t target_protocol,
3033 			    struct rte_flow_error *error)
3034 {
3035 	const struct rte_flow_item_udp *mask = item->mask;
3036 	const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
3037 	const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
3038 				      MLX5_FLOW_LAYER_OUTER_L3;
3039 	const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
3040 				      MLX5_FLOW_LAYER_OUTER_L4;
3041 	int ret;
3042 
3043 	if (!mlx5_hws_active(dev)) {
3044 		if (target_protocol != 0xff && target_protocol != IPPROTO_UDP)
3045 			return rte_flow_error_set(error, EINVAL,
3046 						  RTE_FLOW_ERROR_TYPE_ITEM,
3047 						  item, "protocol filtering not compatible with UDP layer");
3048 		if (!(item_flags & l3m))
3049 			return rte_flow_error_set(error, EINVAL,
3050 						  RTE_FLOW_ERROR_TYPE_ITEM,
3051 						  item,
3052 						  "L3 is mandatory to filter on L4");
3053 	}
3054 	if (item_flags & l4m)
3055 		return rte_flow_error_set(error, EINVAL,
3056 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3057 					  "multiple L4 layers not supported");
3058 	if (!mask)
3059 		mask = &rte_flow_item_udp_mask;
3060 	ret = mlx5_flow_item_acceptable
3061 		(dev, item, (const uint8_t *)mask,
3062 		 (const uint8_t *)&rte_flow_item_udp_mask,
3063 		 sizeof(struct rte_flow_item_udp), MLX5_ITEM_RANGE_NOT_ACCEPTED,
3064 		 error);
3065 	if (ret < 0)
3066 		return ret;
3067 	return 0;
3068 }
3069 
3070 /**
3071  * Validate TCP item.
3072  *
3073  * @param[in] item
3074  *   Item specification.
3075  * @param[in] item_flags
3076  *   Bit-fields that holds the items detected until now.
3077  * @param[in] target_protocol
3078  *   The next protocol in the previous item.
3079  * @param[out] error
3080  *   Pointer to error structure.
3081  *
3082  * @return
3083  *   0 on success, a negative errno value otherwise and rte_errno is set.
3084  */
3085 int
3086 mlx5_flow_validate_item_tcp(const struct rte_eth_dev *dev,
3087 			    const struct rte_flow_item *item,
3088 			    uint64_t item_flags,
3089 			    uint8_t target_protocol,
3090 			    const struct rte_flow_item_tcp *flow_mask,
3091 			    struct rte_flow_error *error)
3092 {
3093 	const struct rte_flow_item_tcp *mask = item->mask;
3094 	const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
3095 	const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
3096 				      MLX5_FLOW_LAYER_OUTER_L3;
3097 	const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
3098 				      MLX5_FLOW_LAYER_OUTER_L4;
3099 	int ret;
3100 
3101 	MLX5_ASSERT(flow_mask);
3102 	if (!mlx5_hws_active(dev)) {
3103 		if (target_protocol != 0xff && target_protocol != IPPROTO_TCP)
3104 			return rte_flow_error_set(error, EINVAL,
3105 						  RTE_FLOW_ERROR_TYPE_ITEM,
3106 						  item, "protocol filtering not compatible with TCP layer");
3107 		if (!(item_flags & l3m))
3108 			return rte_flow_error_set(error, EINVAL,
3109 						  RTE_FLOW_ERROR_TYPE_ITEM,
3110 						  item, "L3 is mandatory to filter on L4");
3111 	}
3112 	if (item_flags & l4m)
3113 		return rte_flow_error_set(error, EINVAL,
3114 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3115 					  "multiple L4 layers not supported");
3116 	if (!mask)
3117 		mask = &rte_flow_item_tcp_mask;
3118 	ret = mlx5_flow_item_acceptable
3119 		(dev, item, (const uint8_t *)mask,
3120 		 (const uint8_t *)flow_mask,
3121 		 sizeof(struct rte_flow_item_tcp), MLX5_ITEM_RANGE_NOT_ACCEPTED,
3122 		 error);
3123 	if (ret < 0)
3124 		return ret;
3125 	return 0;
3126 }
3127 
3128 /**
3129  * Validate VXLAN item.
3130  *
3131  * @param[in] dev
3132  *   Pointer to the Ethernet device structure.
3133  * @param[in] udp_dport
3134  *   UDP destination port
3135  * @param[in] item
3136  *   Item specification.
3137  * @param[in] item_flags
3138  *   Bit-fields that holds the items detected until now.
3139  * @param root
3140  *   Whether action is on root table.
3141  * @param[out] error
3142  *   Pointer to error structure.
3143  *
3144  * @return
3145  *   0 on success, a negative errno value otherwise and rte_errno is set.
3146  */
3147 int
3148 mlx5_flow_validate_item_vxlan(struct rte_eth_dev *dev,
3149 			      uint16_t udp_dport,
3150 			      const struct rte_flow_item *item,
3151 			      uint64_t item_flags,
3152 			      bool root,
3153 			      struct rte_flow_error *error)
3154 {
3155 	const struct rte_flow_item_vxlan *spec = item->spec;
3156 	const struct rte_flow_item_vxlan *mask = item->mask;
3157 	int ret;
3158 	struct mlx5_priv *priv = dev->data->dev_private;
3159 	union vni {
3160 		uint32_t vlan_id;
3161 		uint8_t vni[4];
3162 	} id = { .vlan_id = 0, };
3163 	const struct rte_flow_item_vxlan nic_mask = {
3164 		.hdr.vni =  { 0xff, 0xff, 0xff },
3165 		.hdr.rsvd1 = 0xff,
3166 	};
3167 	const struct rte_flow_item_vxlan *valid_mask;
3168 
3169 	if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
3170 		return rte_flow_error_set(error, ENOTSUP,
3171 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3172 					  "multiple tunnel layers not"
3173 					  " supported");
3174 	/* HWS can match entire VXLAN, VXLAN-GBP and VXLAN-GPE headers */
3175 	if (mlx5_hws_active(dev))
3176 		return 0;
3177 	valid_mask = &rte_flow_item_vxlan_mask;
3178 	/*
3179 	 * Verify only UDPv4 is present as defined in
3180 	 * https://tools.ietf.org/html/rfc7348
3181 	 */
3182 	if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
3183 		return rte_flow_error_set(error, EINVAL,
3184 					  RTE_FLOW_ERROR_TYPE_ITEM,
3185 					  item, "no outer UDP layer found");
3186 	if (!(item_flags & MLX5_FLOW_LAYER_OUTER))
3187 		return rte_flow_error_set(error, ENOTSUP,
3188 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3189 					  "VXLAN tunnel must be fully defined");
3190 	if (!mask)
3191 		mask = &rte_flow_item_vxlan_mask;
3192 
3193 	if (priv->sh->steering_format_version !=
3194 	    MLX5_STEERING_LOGIC_FORMAT_CONNECTX_5 ||
3195 	    !udp_dport || udp_dport == MLX5_UDP_PORT_VXLAN) {
3196 		/* non-root table */
3197 		if (!root && priv->sh->misc5_cap)
3198 			valid_mask = &nic_mask;
3199 		/* Group zero in NIC domain */
3200 		if (!root && priv->sh->tunnel_header_0_1)
3201 			valid_mask = &nic_mask;
3202 	}
3203 	ret = mlx5_flow_item_acceptable
3204 		(dev, item, (const uint8_t *)mask,
3205 		 (const uint8_t *)valid_mask,
3206 		 sizeof(struct rte_flow_item_vxlan),
3207 		 MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
3208 	if (ret < 0)
3209 		return ret;
3210 	if (spec) {
3211 		memcpy(&id.vni[1], spec->hdr.vni, 3);
3212 		memcpy(&id.vni[1], mask->hdr.vni, 3);
3213 	}
3214 	return 0;
3215 }
3216 
3217 /**
3218  * Validate VXLAN_GPE item.
3219  *
3220  * @param[in] item
3221  *   Item specification.
3222  * @param[in] item_flags
3223  *   Bit-fields that holds the items detected until now.
3224  * @param[in] priv
3225  *   Pointer to the private data structure.
3226  * @param[in] target_protocol
3227  *   The next protocol in the previous item.
3228  * @param[out] error
3229  *   Pointer to error structure.
3230  *
3231  * @return
3232  *   0 on success, a negative errno value otherwise and rte_errno is set.
3233  */
3234 int
3235 mlx5_flow_validate_item_vxlan_gpe(const struct rte_flow_item *item,
3236 				  uint64_t item_flags,
3237 				  struct rte_eth_dev *dev,
3238 				  struct rte_flow_error *error)
3239 {
3240 	struct mlx5_priv *priv = dev->data->dev_private;
3241 	const struct rte_flow_item_vxlan_gpe *spec = item->spec;
3242 	const struct rte_flow_item_vxlan_gpe *mask = item->mask;
3243 	int ret;
3244 	union vni {
3245 		uint32_t vlan_id;
3246 		uint8_t vni[4];
3247 	} id = { .vlan_id = 0, };
3248 
3249 	struct rte_flow_item_vxlan_gpe nic_mask = {
3250 		.vni =  { 0xff, 0xff, 0xff },
3251 		.protocol = 0xff,
3252 		.flags = 0xff,
3253 	};
3254 
3255 	if (!priv->sh->config.l3_vxlan_en)
3256 		return rte_flow_error_set(error, ENOTSUP,
3257 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3258 					  "L3 VXLAN is not enabled by device"
3259 					  " parameter and/or not configured in"
3260 					  " firmware");
3261 	if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
3262 		return rte_flow_error_set(error, ENOTSUP,
3263 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3264 					  "multiple tunnel layers not"
3265 					  " supported");
3266 	/*
3267 	 * Verify only UDPv4 is present as defined in
3268 	 * https://tools.ietf.org/html/rfc7348
3269 	 */
3270 	if (!mlx5_hws_active(dev)) {
3271 		if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
3272 			return rte_flow_error_set(error, EINVAL,
3273 						  RTE_FLOW_ERROR_TYPE_ITEM,
3274 						  item, "no outer UDP layer found");
3275 	}
3276 	if (!mask)
3277 		mask = &rte_flow_item_vxlan_gpe_mask;
3278 	if (mlx5_hws_active(dev) ||
3279 	    (priv->sh->misc5_cap && priv->sh->tunnel_header_0_1)) {
3280 		nic_mask.rsvd0[0] = 0xff;
3281 		nic_mask.rsvd0[1] = 0xff;
3282 		nic_mask.rsvd1 = 0xff;
3283 	}
3284 	ret = mlx5_flow_item_acceptable
3285 		(dev, item, (const uint8_t *)mask,
3286 		 (const uint8_t *)&nic_mask,
3287 		 sizeof(struct rte_flow_item_vxlan_gpe),
3288 		 MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
3289 	if (ret < 0)
3290 		return ret;
3291 	if (spec) {
3292 		memcpy(&id.vni[1], spec->hdr.vni, 3);
3293 		memcpy(&id.vni[1], mask->hdr.vni, 3);
3294 	}
3295 	if (!(item_flags & MLX5_FLOW_LAYER_OUTER))
3296 		return rte_flow_error_set(error, ENOTSUP,
3297 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3298 					  "VXLAN-GPE tunnel must be fully"
3299 					  " defined");
3300 	return 0;
3301 }
3302 /**
3303  * Validate GRE Key item.
3304  *
3305  * @param[in] item
3306  *   Item specification.
3307  * @param[in] item_flags
3308  *   Bit flags to mark detected items.
3309  * @param[in] gre_item
3310  *   Pointer to gre_item
3311  * @param[out] error
3312  *   Pointer to error structure.
3313  *
3314  * @return
3315  *   0 on success, a negative errno value otherwise and rte_errno is set.
3316  */
3317 int
3318 mlx5_flow_validate_item_gre_key(const struct rte_eth_dev *dev,
3319 				const struct rte_flow_item *item,
3320 				uint64_t item_flags,
3321 				const struct rte_flow_item *gre_item,
3322 				struct rte_flow_error *error)
3323 {
3324 	const rte_be32_t *mask = item->mask;
3325 	int ret = 0;
3326 	rte_be32_t gre_key_default_mask = RTE_BE32(UINT32_MAX);
3327 	const struct rte_flow_item_gre *gre_spec;
3328 	const struct rte_flow_item_gre *gre_mask;
3329 
3330 	if (item_flags & MLX5_FLOW_LAYER_GRE_KEY)
3331 		return rte_flow_error_set(error, ENOTSUP,
3332 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3333 					  "Multiple GRE key not support");
3334 	if (!(item_flags & MLX5_FLOW_LAYER_GRE))
3335 		return rte_flow_error_set(error, ENOTSUP,
3336 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3337 					  "No preceding GRE header");
3338 	if (item_flags & MLX5_FLOW_LAYER_INNER)
3339 		return rte_flow_error_set(error, ENOTSUP,
3340 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3341 					  "GRE key following a wrong item");
3342 	gre_mask = gre_item->mask;
3343 	if (!gre_mask)
3344 		gre_mask = &rte_flow_item_gre_mask;
3345 	gre_spec = gre_item->spec;
3346 	if (gre_spec && (gre_mask->c_rsvd0_ver & RTE_BE16(0x2000)) &&
3347 			 !(gre_spec->c_rsvd0_ver & RTE_BE16(0x2000)))
3348 		return rte_flow_error_set(error, EINVAL,
3349 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3350 					  "Key bit must be on");
3351 
3352 	if (!mask)
3353 		mask = &gre_key_default_mask;
3354 	ret = mlx5_flow_item_acceptable
3355 		(dev, item, (const uint8_t *)mask,
3356 		 (const uint8_t *)&gre_key_default_mask,
3357 		 sizeof(rte_be32_t), MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
3358 	return ret;
3359 }
3360 
3361 /**
3362  * Validate GRE optional item.
3363  *
3364  * @param[in] dev
3365  *   Pointer to the Ethernet device structure.
3366  * @param[in] item
3367  *   Item specification.
3368  * @param[in] item_flags
3369  *   Bit flags to mark detected items.
3370  * @param[in] attr
3371  *   Flow rule attributes.
3372  * @param[in] gre_item
3373  *   Pointer to gre_item
3374  * @param[out] error
3375  *   Pointer to error structure.
3376  *
3377  * @return
3378  *   0 on success, a negative errno value otherwise and rte_errno is set.
3379  */
3380 int
3381 mlx5_flow_validate_item_gre_option(struct rte_eth_dev *dev,
3382 				   const struct rte_flow_item *item,
3383 				   uint64_t item_flags,
3384 				   const struct rte_flow_attr *attr,
3385 				   const struct rte_flow_item *gre_item,
3386 				   struct rte_flow_error *error)
3387 {
3388 	const struct rte_flow_item_gre *gre_spec = gre_item->spec;
3389 	const struct rte_flow_item_gre *gre_mask = gre_item->mask;
3390 	const struct rte_flow_item_gre_opt *spec = item->spec;
3391 	const struct rte_flow_item_gre_opt *mask = item->mask;
3392 	struct mlx5_priv *priv = dev->data->dev_private;
3393 	int ret = 0;
3394 	struct rte_flow_item_gre_opt nic_mask = {
3395 		.checksum_rsvd = {
3396 			.checksum = RTE_BE16(UINT16_MAX),
3397 			.reserved1 = 0x0,
3398 		},
3399 		.key = {
3400 			.key = RTE_BE32(UINT32_MAX),
3401 		},
3402 		.sequence = {
3403 			.sequence = RTE_BE32(UINT32_MAX),
3404 		},
3405 	};
3406 
3407 	if (!(item_flags & MLX5_FLOW_LAYER_GRE))
3408 		return rte_flow_error_set(error, ENOTSUP,
3409 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3410 					  "No preceding GRE header");
3411 	if (item_flags & MLX5_FLOW_LAYER_INNER)
3412 		return rte_flow_error_set(error, ENOTSUP,
3413 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3414 					  "GRE option following a wrong item");
3415 	if ((!spec && !mlx5_hws_active(dev)) || !mask)
3416 		return rte_flow_error_set(error, EINVAL,
3417 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3418 					  "At least one field gre_option(checksum/key/sequence) must be specified");
3419 	if (!gre_mask)
3420 		gre_mask = &rte_flow_item_gre_mask;
3421 	if (mask->checksum_rsvd.checksum)
3422 		if (gre_spec && (gre_mask->c_rsvd0_ver & RTE_BE16(0x8000)) &&
3423 				 !(gre_spec->c_rsvd0_ver & RTE_BE16(0x8000)))
3424 			return rte_flow_error_set(error, EINVAL,
3425 						  RTE_FLOW_ERROR_TYPE_ITEM,
3426 						  item,
3427 						  "Checksum bit must be on");
3428 	if (mask->key.key)
3429 		if (gre_spec && (gre_mask->c_rsvd0_ver & RTE_BE16(0x2000)) &&
3430 				 !(gre_spec->c_rsvd0_ver & RTE_BE16(0x2000)))
3431 			return rte_flow_error_set(error, EINVAL,
3432 						  RTE_FLOW_ERROR_TYPE_ITEM,
3433 						  item, "Key bit must be on");
3434 	if (mask->sequence.sequence)
3435 		if (gre_spec && (gre_mask->c_rsvd0_ver & RTE_BE16(0x1000)) &&
3436 				 !(gre_spec->c_rsvd0_ver & RTE_BE16(0x1000)))
3437 			return rte_flow_error_set(error, EINVAL,
3438 						  RTE_FLOW_ERROR_TYPE_ITEM,
3439 						  item,
3440 						  "Sequence bit must be on");
3441 	if (!mlx5_hws_active(dev)) {
3442 		if (mask->checksum_rsvd.checksum || mask->sequence.sequence) {
3443 			if (priv->sh->steering_format_version ==
3444 			    MLX5_STEERING_LOGIC_FORMAT_CONNECTX_5 ||
3445 			    ((attr->group ||
3446 			      (attr->transfer && priv->fdb_def_rule)) &&
3447 			     !priv->sh->misc5_cap) ||
3448 			    (!(priv->sh->tunnel_header_0_1 &&
3449 			       priv->sh->tunnel_header_2_3) &&
3450 			     !attr->group &&
3451 			     (!attr->transfer || !priv->fdb_def_rule)))
3452 				return rte_flow_error_set
3453 					(error, EINVAL,	RTE_FLOW_ERROR_TYPE_ITEM,
3454 					 item, "Checksum/Sequence not supported");
3455 		}
3456 	}
3457 	ret = mlx5_flow_item_acceptable
3458 		(dev, item, (const uint8_t *)mask,
3459 		 (const uint8_t *)&nic_mask,
3460 		 sizeof(struct rte_flow_item_gre_opt),
3461 		 MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
3462 	return ret;
3463 }
3464 
3465 /**
3466  * Validate GRE item.
3467  *
3468  * @param[in] item
3469  *   Item specification.
3470  * @param[in] item_flags
3471  *   Bit flags to mark detected items.
3472  * @param[in] target_protocol
3473  *   The next protocol in the previous item.
3474  * @param[out] error
3475  *   Pointer to error structure.
3476  *
3477  * @return
3478  *   0 on success, a negative errno value otherwise and rte_errno is set.
3479  */
3480 int
3481 mlx5_flow_validate_item_gre(const struct rte_eth_dev *dev,
3482 			    const struct rte_flow_item *item,
3483 			    uint64_t item_flags,
3484 			    uint8_t target_protocol,
3485 			    struct rte_flow_error *error)
3486 {
3487 	const struct rte_flow_item_gre *spec __rte_unused = item->spec;
3488 	const struct rte_flow_item_gre *mask = item->mask;
3489 	int ret;
3490 	const struct rte_flow_item_gre nic_mask = {
3491 		.c_rsvd0_ver = RTE_BE16(0xB000),
3492 		.protocol = RTE_BE16(UINT16_MAX),
3493 	};
3494 
3495 	if (target_protocol != 0xff && target_protocol != IPPROTO_GRE)
3496 		return rte_flow_error_set(error, EINVAL,
3497 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3498 					  "protocol filtering not compatible"
3499 					  " with this GRE layer");
3500 	if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
3501 		return rte_flow_error_set(error, ENOTSUP,
3502 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3503 					  "multiple tunnel layers not"
3504 					  " supported");
3505 	if (!mlx5_hws_active(dev)) {
3506 		if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L3))
3507 			return rte_flow_error_set(error, ENOTSUP,
3508 						  RTE_FLOW_ERROR_TYPE_ITEM,
3509 						  item, "L3 Layer is missing");
3510 	}
3511 	if (!mask)
3512 		mask = &rte_flow_item_gre_mask;
3513 	ret = mlx5_flow_item_acceptable
3514 		(dev, item, (const uint8_t *)mask,
3515 		 (const uint8_t *)&nic_mask,
3516 		 sizeof(struct rte_flow_item_gre), MLX5_ITEM_RANGE_NOT_ACCEPTED,
3517 		 error);
3518 	if (ret < 0)
3519 		return ret;
3520 #ifndef HAVE_MLX5DV_DR
3521 #ifndef HAVE_IBV_DEVICE_MPLS_SUPPORT
3522 	if (spec && (spec->protocol & mask->protocol))
3523 		return rte_flow_error_set(error, ENOTSUP,
3524 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3525 					  "without MPLS support the"
3526 					  " specification cannot be used for"
3527 					  " filtering");
3528 #endif
3529 #endif
3530 	return 0;
3531 }
3532 
3533 /**
3534  * Validate Geneve item.
3535  *
3536  * @param[in] item
3537  *   Item specification.
3538  * @param[in] itemFlags
3539  *   Bit-fields that holds the items detected until now.
3540  * @param[in] enPriv
3541  *   Pointer to the private data structure.
3542  * @param[out] error
3543  *   Pointer to error structure.
3544  *
3545  * @return
3546  *   0 on success, a negative errno value otherwise and rte_errno is set.
3547  */
3548 
3549 int
3550 mlx5_flow_validate_item_geneve(const struct rte_flow_item *item,
3551 			       uint64_t item_flags,
3552 			       struct rte_eth_dev *dev,
3553 			       struct rte_flow_error *error)
3554 {
3555 	struct mlx5_priv *priv = dev->data->dev_private;
3556 	const struct rte_flow_item_geneve *spec = item->spec;
3557 	const struct rte_flow_item_geneve *mask = item->mask;
3558 	int ret;
3559 	uint16_t gbhdr;
3560 	uint8_t opt_len = priv->sh->cdev->config.hca_attr.geneve_max_opt_len ?
3561 			  MLX5_GENEVE_OPT_LEN_1 : MLX5_GENEVE_OPT_LEN_0;
3562 	const struct rte_flow_item_geneve nic_mask = {
3563 		.ver_opt_len_o_c_rsvd0 = RTE_BE16(0x3f80),
3564 		.vni =  { 0xff, 0xff, 0xff },
3565 		.protocol = RTE_BE16(UINT16_MAX),
3566 	};
3567 
3568 	if (!priv->sh->cdev->config.hca_attr.tunnel_stateless_geneve_rx)
3569 		return rte_flow_error_set(error, ENOTSUP,
3570 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3571 					  "L3 Geneve is not enabled by device"
3572 					  " parameter and/or not configured in"
3573 					  " firmware");
3574 	if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
3575 		return rte_flow_error_set(error, ENOTSUP,
3576 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3577 					  "multiple tunnel layers not"
3578 					  " supported");
3579 	/*
3580 	 * Verify only UDPv4 is present as defined in
3581 	 * https://tools.ietf.org/html/rfc7348
3582 	 */
3583 	if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
3584 		return rte_flow_error_set(error, EINVAL,
3585 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3586 					  "no outer UDP layer found");
3587 	if (!mask)
3588 		mask = &rte_flow_item_geneve_mask;
3589 	ret = mlx5_flow_item_acceptable
3590 				  (dev, item, (const uint8_t *)mask,
3591 				   (const uint8_t *)&nic_mask,
3592 				   sizeof(struct rte_flow_item_geneve),
3593 				   MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
3594 	if (ret)
3595 		return ret;
3596 	if (spec) {
3597 		gbhdr = rte_be_to_cpu_16(spec->ver_opt_len_o_c_rsvd0);
3598 		if (MLX5_GENEVE_VER_VAL(gbhdr) ||
3599 		     MLX5_GENEVE_CRITO_VAL(gbhdr) ||
3600 		     MLX5_GENEVE_RSVD_VAL(gbhdr) || spec->rsvd1)
3601 			return rte_flow_error_set(error, ENOTSUP,
3602 						  RTE_FLOW_ERROR_TYPE_ITEM,
3603 						  item,
3604 						  "Geneve protocol unsupported"
3605 						  " fields are being used");
3606 		if (MLX5_GENEVE_OPTLEN_VAL(gbhdr) > opt_len)
3607 			return rte_flow_error_set
3608 					(error, ENOTSUP,
3609 					 RTE_FLOW_ERROR_TYPE_ITEM,
3610 					 item,
3611 					 "Unsupported Geneve options length");
3612 	}
3613 	if (!(item_flags & MLX5_FLOW_LAYER_OUTER))
3614 		return rte_flow_error_set
3615 				    (error, ENOTSUP,
3616 				     RTE_FLOW_ERROR_TYPE_ITEM, item,
3617 				     "Geneve tunnel must be fully defined");
3618 	return 0;
3619 }
3620 
3621 /**
3622  * Validate Geneve TLV option item.
3623  *
3624  * @param[in] item
3625  *   Item specification.
3626  * @param[in] last_item
3627  *   Previous validated item in the pattern items.
3628  * @param[in] geneve_item
3629  *   Previous GENEVE item specification.
3630  * @param[in] dev
3631  *   Pointer to the rte_eth_dev structure.
3632  * @param[out] error
3633  *   Pointer to error structure.
3634  *
3635  * @return
3636  *   0 on success, a negative errno value otherwise and rte_errno is set.
3637  */
3638 int
3639 mlx5_flow_validate_item_geneve_opt(const struct rte_flow_item *item,
3640 				   uint64_t last_item,
3641 				   const struct rte_flow_item *geneve_item,
3642 				   struct rte_eth_dev *dev,
3643 				   struct rte_flow_error *error)
3644 {
3645 	struct mlx5_priv *priv = dev->data->dev_private;
3646 	struct mlx5_dev_ctx_shared *sh = priv->sh;
3647 	struct mlx5_geneve_tlv_option_resource *geneve_opt_resource;
3648 	struct mlx5_hca_attr *hca_attr = &sh->cdev->config.hca_attr;
3649 	uint8_t data_max_supported =
3650 			hca_attr->max_geneve_tlv_option_data_len * 4;
3651 	const struct rte_flow_item_geneve *geneve_spec;
3652 	const struct rte_flow_item_geneve *geneve_mask;
3653 	const struct rte_flow_item_geneve_opt *spec = item->spec;
3654 	const struct rte_flow_item_geneve_opt *mask = item->mask;
3655 	unsigned int i;
3656 	unsigned int data_len;
3657 	uint8_t tlv_option_len;
3658 	uint16_t optlen_m, optlen_v;
3659 	const struct rte_flow_item_geneve_opt full_mask = {
3660 		.option_class = RTE_BE16(0xffff),
3661 		.option_type = 0xff,
3662 		.option_len = 0x1f,
3663 	};
3664 
3665 	if (!mask)
3666 		mask = &rte_flow_item_geneve_opt_mask;
3667 	if (!spec)
3668 		return rte_flow_error_set
3669 			(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, item,
3670 			"Geneve TLV opt class/type/length must be specified");
3671 	if ((uint32_t)spec->option_len > MLX5_GENEVE_OPTLEN_MASK)
3672 		return rte_flow_error_set
3673 			(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, item,
3674 			"Geneve TLV opt length exceeds the limit (31)");
3675 	/* Check if class type and length masks are full. */
3676 	if (full_mask.option_class != mask->option_class ||
3677 	    full_mask.option_type != mask->option_type ||
3678 	    full_mask.option_len != (mask->option_len & full_mask.option_len))
3679 		return rte_flow_error_set
3680 			(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, item,
3681 			"Geneve TLV opt class/type/length masks must be full");
3682 	/* Check if length is supported */
3683 	if ((uint32_t)spec->option_len >
3684 			hca_attr->max_geneve_tlv_option_data_len)
3685 		return rte_flow_error_set
3686 			(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, item,
3687 			"Geneve TLV opt length not supported");
3688 	if (hca_attr->max_geneve_tlv_options > 1)
3689 		DRV_LOG(DEBUG,
3690 			"max_geneve_tlv_options supports more than 1 option");
3691 	/* Check GENEVE item preceding. */
3692 	if (!geneve_item || !(last_item & MLX5_FLOW_LAYER_GENEVE))
3693 		return rte_flow_error_set
3694 			(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, item,
3695 			"Geneve opt item must be preceded with Geneve item");
3696 	geneve_spec = geneve_item->spec;
3697 	geneve_mask = geneve_item->mask ? geneve_item->mask :
3698 					  &rte_flow_item_geneve_mask;
3699 	/* Check if GENEVE TLV option size doesn't exceed option length */
3700 	if (geneve_spec && (geneve_mask->ver_opt_len_o_c_rsvd0 ||
3701 			    geneve_spec->ver_opt_len_o_c_rsvd0)) {
3702 		tlv_option_len = spec->option_len & mask->option_len;
3703 		optlen_v = rte_be_to_cpu_16(geneve_spec->ver_opt_len_o_c_rsvd0);
3704 		optlen_v = MLX5_GENEVE_OPTLEN_VAL(optlen_v);
3705 		optlen_m = rte_be_to_cpu_16(geneve_mask->ver_opt_len_o_c_rsvd0);
3706 		optlen_m = MLX5_GENEVE_OPTLEN_VAL(optlen_m);
3707 		if ((optlen_v & optlen_m) <= tlv_option_len)
3708 			return rte_flow_error_set
3709 				(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, item,
3710 				 "GENEVE TLV option length exceeds optlen");
3711 	}
3712 	/* Check if length is 0 or data is 0. */
3713 	if (spec->data == NULL || spec->option_len == 0)
3714 		return rte_flow_error_set
3715 			(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, item,
3716 			"Geneve TLV opt with zero data/length not supported");
3717 	/* Check not all data & mask are 0. */
3718 	data_len = spec->option_len * 4;
3719 	if (mask->data == NULL) {
3720 		for (i = 0; i < data_len; i++)
3721 			if (spec->data[i])
3722 				break;
3723 		if (i == data_len)
3724 			return rte_flow_error_set(error, ENOTSUP,
3725 				RTE_FLOW_ERROR_TYPE_ITEM, item,
3726 				"Can't match on Geneve option data 0");
3727 	} else {
3728 		for (i = 0; i < data_len; i++)
3729 			if (spec->data[i] & mask->data[i])
3730 				break;
3731 		if (i == data_len)
3732 			return rte_flow_error_set(error, ENOTSUP,
3733 				RTE_FLOW_ERROR_TYPE_ITEM, item,
3734 				"Can't match on Geneve option data and mask 0");
3735 		/* Check data mask supported. */
3736 		for (i = data_max_supported; i < data_len ; i++)
3737 			if (mask->data[i])
3738 				return rte_flow_error_set(error, ENOTSUP,
3739 					RTE_FLOW_ERROR_TYPE_ITEM, item,
3740 					"Data mask is of unsupported size");
3741 	}
3742 	/* Check GENEVE option is supported in NIC. */
3743 	if (!hca_attr->geneve_tlv_opt)
3744 		return rte_flow_error_set
3745 			(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, item,
3746 			"Geneve TLV opt not supported");
3747 	/* Check if we already have geneve option with different type/class. */
3748 	rte_spinlock_lock(&sh->geneve_tlv_opt_sl);
3749 	geneve_opt_resource = sh->geneve_tlv_option_resource;
3750 	if (geneve_opt_resource != NULL)
3751 		if (geneve_opt_resource->option_class != spec->option_class ||
3752 		    geneve_opt_resource->option_type != spec->option_type ||
3753 		    geneve_opt_resource->length != spec->option_len) {
3754 			rte_spinlock_unlock(&sh->geneve_tlv_opt_sl);
3755 			return rte_flow_error_set(error, ENOTSUP,
3756 				RTE_FLOW_ERROR_TYPE_ITEM, item,
3757 				"Only one Geneve TLV option supported");
3758 		}
3759 	rte_spinlock_unlock(&sh->geneve_tlv_opt_sl);
3760 	return 0;
3761 }
3762 
3763 /**
3764  * Validate MPLS item.
3765  *
3766  * @param[in] dev
3767  *   Pointer to the rte_eth_dev structure.
3768  * @param[in] item
3769  *   Item specification.
3770  * @param[in] item_flags
3771  *   Bit-fields that holds the items detected until now.
3772  * @param[in] prev_layer
3773  *   The protocol layer indicated in previous item.
3774  * @param[out] error
3775  *   Pointer to error structure.
3776  *
3777  * @return
3778  *   0 on success, a negative errno value otherwise and rte_errno is set.
3779  */
3780 int
3781 mlx5_flow_validate_item_mpls(struct rte_eth_dev *dev __rte_unused,
3782 			     const struct rte_flow_item *item __rte_unused,
3783 			     uint64_t item_flags __rte_unused,
3784 			     uint64_t prev_layer __rte_unused,
3785 			     struct rte_flow_error *error)
3786 {
3787 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
3788 	const struct rte_flow_item_mpls hws_nic_mask = {
3789 		.label_tc_s = {0xff, 0xff, 0xff},
3790 		.ttl = 0xff
3791 	};
3792 	const struct rte_flow_item_mpls *nic_mask = !mlx5_hws_active(dev) ?
3793 		&rte_flow_item_mpls_mask : &hws_nic_mask;
3794 	const struct rte_flow_item_mpls *mask = item->mask;
3795 	struct mlx5_priv *priv = dev->data->dev_private;
3796 	int ret;
3797 
3798 	if (!mlx5_hws_active(dev)) {
3799 		/* MPLS has HW support in HWS */
3800 		if (!priv->sh->dev_cap.mpls_en)
3801 			return rte_flow_error_set(error, ENOTSUP,
3802 						  RTE_FLOW_ERROR_TYPE_ITEM,
3803 						  item, "MPLS not supported or disabled in firmware configuration.");
3804 		/* MPLS over UDP, GRE is allowed */
3805 		if (!(prev_layer & (MLX5_FLOW_LAYER_OUTER_L4_UDP |
3806 				    MLX5_FLOW_LAYER_GRE |
3807 				    MLX5_FLOW_LAYER_GRE_KEY)))
3808 			return rte_flow_error_set(error, EINVAL,
3809 						  RTE_FLOW_ERROR_TYPE_ITEM,
3810 						  item, "protocol filtering not compatible with MPLS layer");
3811 		/* Multi-tunnel isn't allowed but MPLS over GRE is an exception. */
3812 		if ((item_flags & MLX5_FLOW_LAYER_TUNNEL) &&
3813 		    !(item_flags & MLX5_FLOW_LAYER_GRE))
3814 			return rte_flow_error_set(error, ENOTSUP,
3815 						  RTE_FLOW_ERROR_TYPE_ITEM, item,
3816 						  "multiple tunnel layers not supported");
3817 	} else {
3818 		/* Multi-tunnel isn't allowed but MPLS over GRE is an exception. */
3819 		if ((item_flags & MLX5_FLOW_LAYER_TUNNEL) &&
3820 		    !(item_flags & MLX5_FLOW_LAYER_MPLS))
3821 			return rte_flow_error_set(error, ENOTSUP,
3822 						  RTE_FLOW_ERROR_TYPE_ITEM, item,
3823 						  "multiple tunnel layers not supported");
3824 	}
3825 	if (!mask)
3826 		mask = nic_mask;
3827 	ret = mlx5_flow_item_acceptable
3828 		(dev, item, (const uint8_t *)mask,
3829 		 (const uint8_t *)nic_mask,
3830 		 sizeof(struct rte_flow_item_mpls),
3831 		 MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
3832 	if (ret < 0)
3833 		return ret;
3834 	return 0;
3835 #else
3836 	return rte_flow_error_set(error, ENOTSUP,
3837 				  RTE_FLOW_ERROR_TYPE_ITEM, item,
3838 				  "MPLS is not supported by Verbs, please"
3839 				  " update.");
3840 #endif
3841 }
3842 
3843 /**
3844  * Validate NVGRE item.
3845  *
3846  * @param[in] item
3847  *   Item specification.
3848  * @param[in] item_flags
3849  *   Bit flags to mark detected items.
3850  * @param[in] target_protocol
3851  *   The next protocol in the previous item.
3852  * @param[out] error
3853  *   Pointer to error structure.
3854  *
3855  * @return
3856  *   0 on success, a negative errno value otherwise and rte_errno is set.
3857  */
3858 int
3859 mlx5_flow_validate_item_nvgre(const struct rte_eth_dev *dev,
3860 			      const struct rte_flow_item *item,
3861 			      uint64_t item_flags,
3862 			      uint8_t target_protocol,
3863 			      struct rte_flow_error *error)
3864 {
3865 	const struct rte_flow_item_nvgre *mask = item->mask;
3866 	int ret;
3867 
3868 	if (target_protocol != 0xff && target_protocol != IPPROTO_GRE)
3869 		return rte_flow_error_set(error, EINVAL,
3870 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3871 					  "protocol filtering not compatible"
3872 					  " with this GRE layer");
3873 	if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
3874 		return rte_flow_error_set(error, ENOTSUP,
3875 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3876 					  "multiple tunnel layers not"
3877 					  " supported");
3878 	if (!mlx5_hws_active(dev)) {
3879 		if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L3))
3880 			return rte_flow_error_set(error, ENOTSUP,
3881 						  RTE_FLOW_ERROR_TYPE_ITEM,
3882 						  item, "L3 Layer is missing");
3883 	}
3884 	if (!mask)
3885 		mask = &rte_flow_item_nvgre_mask;
3886 	ret = mlx5_flow_item_acceptable
3887 		(dev, item, (const uint8_t *)mask,
3888 		 (const uint8_t *)&rte_flow_item_nvgre_mask,
3889 		 sizeof(struct rte_flow_item_nvgre),
3890 		 MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
3891 	if (ret < 0)
3892 		return ret;
3893 	return 0;
3894 }
3895 
3896 /**
3897  * Validate eCPRI item.
3898  *
3899  * @param[in] item
3900  *   Item specification.
3901  * @param[in] item_flags
3902  *   Bit-fields that holds the items detected until now.
3903  * @param[in] last_item
3904  *   Previous validated item in the pattern items.
3905  * @param[in] ether_type
3906  *   Type in the ethernet layer header (including dot1q).
3907  * @param[in] acc_mask
3908  *   Acceptable mask, if NULL default internal default mask
3909  *   will be used to check whether item fields are supported.
3910  * @param[out] error
3911  *   Pointer to error structure.
3912  *
3913  * @return
3914  *   0 on success, a negative errno value otherwise and rte_errno is set.
3915  */
3916 int
3917 mlx5_flow_validate_item_ecpri(const struct rte_eth_dev *dev,
3918 			      const struct rte_flow_item *item,
3919 			      uint64_t item_flags,
3920 			      uint64_t last_item,
3921 			      uint16_t ether_type,
3922 			      const struct rte_flow_item_ecpri *acc_mask,
3923 			      struct rte_flow_error *error)
3924 {
3925 	const struct rte_flow_item_ecpri *mask = item->mask;
3926 	const struct rte_flow_item_ecpri nic_mask = {
3927 		.hdr = {
3928 			.common = {
3929 				.u32 =
3930 				RTE_BE32(((const struct rte_ecpri_common_hdr) {
3931 					.type = 0xFF,
3932 					}).u32),
3933 			},
3934 			.dummy[0] = 0xFFFFFFFF,
3935 		},
3936 	};
3937 	const uint64_t outer_l2_vlan = (MLX5_FLOW_LAYER_OUTER_L2 |
3938 					MLX5_FLOW_LAYER_OUTER_VLAN);
3939 	struct rte_flow_item_ecpri mask_lo;
3940 
3941 	if (!(last_item & outer_l2_vlan) &&
3942 	    last_item != MLX5_FLOW_LAYER_OUTER_L4_UDP)
3943 		return rte_flow_error_set(error, EINVAL,
3944 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3945 					  "eCPRI can only follow L2/VLAN layer or UDP layer");
3946 	if ((last_item & outer_l2_vlan) && ether_type &&
3947 	    ether_type != RTE_ETHER_TYPE_ECPRI)
3948 		return rte_flow_error_set(error, EINVAL,
3949 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3950 					  "eCPRI cannot follow L2/VLAN layer which ether type is not 0xAEFE");
3951 	if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
3952 		return rte_flow_error_set(error, EINVAL,
3953 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3954 					  "eCPRI with tunnel is not supported right now");
3955 	if (item_flags & MLX5_FLOW_LAYER_OUTER_L3)
3956 		return rte_flow_error_set(error, ENOTSUP,
3957 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3958 					  "multiple L3 layers not supported");
3959 	else if (item_flags & MLX5_FLOW_LAYER_OUTER_L4_TCP)
3960 		return rte_flow_error_set(error, EINVAL,
3961 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3962 					  "eCPRI cannot coexist with a TCP layer");
3963 	/* In specification, eCPRI could be over UDP layer. */
3964 	else if (item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP)
3965 		return rte_flow_error_set(error, EINVAL,
3966 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
3967 					  "eCPRI over UDP layer is not yet supported right now");
3968 	/* Mask for type field in common header could be zero. */
3969 	if (!mask)
3970 		mask = &rte_flow_item_ecpri_mask;
3971 	mask_lo.hdr.common.u32 = rte_be_to_cpu_32(mask->hdr.common.u32);
3972 	/* Input mask is in big-endian format. */
3973 	if (mask_lo.hdr.common.type != 0 && mask_lo.hdr.common.type != 0xff)
3974 		return rte_flow_error_set(error, EINVAL,
3975 					  RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
3976 					  "partial mask is not supported for protocol");
3977 	else if (mask_lo.hdr.common.type == 0 && mask->hdr.dummy[0] != 0)
3978 		return rte_flow_error_set(error, EINVAL,
3979 					  RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
3980 					  "message header mask must be after a type mask");
3981 	return mlx5_flow_item_acceptable(dev, item, (const uint8_t *)mask,
3982 					 acc_mask ? (const uint8_t *)acc_mask
3983 						  : (const uint8_t *)&nic_mask,
3984 					 sizeof(struct rte_flow_item_ecpri),
3985 					 MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
3986 }
3987 
3988 /**
3989  * Validate the NSH item.
3990  *
3991  * @param[in] dev
3992  *   Pointer to Ethernet device on which flow rule is being created on.
3993  * @param[out] error
3994  *   Pointer to error structure.
3995  *
3996  * @return
3997  *   0 on success, a negative errno value otherwise and rte_errno is set.
3998  */
3999 int
4000 mlx5_flow_validate_item_nsh(struct rte_eth_dev *dev,
4001 			    const struct rte_flow_item *item,
4002 			    struct rte_flow_error *error)
4003 {
4004 	struct mlx5_priv *priv = dev->data->dev_private;
4005 
4006 	if (item->mask) {
4007 		return rte_flow_error_set(error, ENOTSUP,
4008 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
4009 					  "NSH fields matching is not supported");
4010 	}
4011 
4012 	if (!priv->sh->config.dv_flow_en) {
4013 		return rte_flow_error_set(error, ENOTSUP,
4014 					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4015 					  NULL, "NSH support requires DV flow interface");
4016 	}
4017 
4018 	if (!priv->sh->cdev->config.hca_attr.tunnel_stateless_vxlan_gpe_nsh) {
4019 		return rte_flow_error_set(error, ENOTSUP,
4020 					  RTE_FLOW_ERROR_TYPE_ITEM, item,
4021 					  "Current FW does not support matching on NSH");
4022 	}
4023 
4024 	return 0;
4025 }
4026 
4027 static int
4028 flow_null_validate(struct rte_eth_dev *dev __rte_unused,
4029 		   const struct rte_flow_attr *attr __rte_unused,
4030 		   const struct rte_flow_item items[] __rte_unused,
4031 		   const struct rte_flow_action actions[] __rte_unused,
4032 		   bool external __rte_unused,
4033 		   int hairpin __rte_unused,
4034 		   struct rte_flow_error *error)
4035 {
4036 	return rte_flow_error_set(error, ENOTSUP,
4037 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL, NULL);
4038 }
4039 
4040 static struct mlx5_flow *
4041 flow_null_prepare(struct rte_eth_dev *dev __rte_unused,
4042 		  const struct rte_flow_attr *attr __rte_unused,
4043 		  const struct rte_flow_item items[] __rte_unused,
4044 		  const struct rte_flow_action actions[] __rte_unused,
4045 		  struct rte_flow_error *error)
4046 {
4047 	rte_flow_error_set(error, ENOTSUP,
4048 			   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL, NULL);
4049 	return NULL;
4050 }
4051 
4052 static int
4053 flow_null_translate(struct rte_eth_dev *dev __rte_unused,
4054 		    struct mlx5_flow *dev_flow __rte_unused,
4055 		    const struct rte_flow_attr *attr __rte_unused,
4056 		    const struct rte_flow_item items[] __rte_unused,
4057 		    const struct rte_flow_action actions[] __rte_unused,
4058 		    struct rte_flow_error *error)
4059 {
4060 	return rte_flow_error_set(error, ENOTSUP,
4061 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL, NULL);
4062 }
4063 
4064 static int
4065 flow_null_apply(struct rte_eth_dev *dev __rte_unused,
4066 		struct rte_flow *flow __rte_unused,
4067 		struct rte_flow_error *error)
4068 {
4069 	return rte_flow_error_set(error, ENOTSUP,
4070 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL, NULL);
4071 }
4072 
4073 static void
4074 flow_null_remove(struct rte_eth_dev *dev __rte_unused,
4075 		 struct rte_flow *flow __rte_unused)
4076 {
4077 }
4078 
4079 static void
4080 flow_null_destroy(struct rte_eth_dev *dev __rte_unused,
4081 		  struct rte_flow *flow __rte_unused)
4082 {
4083 }
4084 
4085 static int
4086 flow_null_query(struct rte_eth_dev *dev __rte_unused,
4087 		struct rte_flow *flow __rte_unused,
4088 		const struct rte_flow_action *actions __rte_unused,
4089 		void *data __rte_unused,
4090 		struct rte_flow_error *error)
4091 {
4092 	return rte_flow_error_set(error, ENOTSUP,
4093 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL, NULL);
4094 }
4095 
4096 static int
4097 flow_null_sync_domain(struct rte_eth_dev *dev __rte_unused,
4098 		      uint32_t domains __rte_unused,
4099 		      uint32_t flags __rte_unused)
4100 {
4101 	return 0;
4102 }
4103 
4104 int
4105 flow_null_get_aged_flows(struct rte_eth_dev *dev,
4106 		    void **context __rte_unused,
4107 		    uint32_t nb_contexts __rte_unused,
4108 		    struct rte_flow_error *error __rte_unused)
4109 {
4110 	DRV_LOG(ERR, "port %u get aged flows is not supported.",
4111 		dev->data->port_id);
4112 	return -ENOTSUP;
4113 }
4114 
4115 uint32_t
4116 flow_null_counter_allocate(struct rte_eth_dev *dev)
4117 {
4118 	DRV_LOG(ERR, "port %u counter allocate is not supported.",
4119 		dev->data->port_id);
4120 	return 0;
4121 }
4122 
4123 void
4124 flow_null_counter_free(struct rte_eth_dev *dev,
4125 			uint32_t counter __rte_unused)
4126 {
4127 	DRV_LOG(ERR, "port %u counter free is not supported.",
4128 		 dev->data->port_id);
4129 }
4130 
4131 int
4132 flow_null_counter_query(struct rte_eth_dev *dev,
4133 			uint32_t counter __rte_unused,
4134 			bool clear __rte_unused,
4135 			uint64_t *pkts __rte_unused,
4136 			uint64_t *bytes __rte_unused,
4137 			void **action __rte_unused)
4138 {
4139 	DRV_LOG(ERR, "port %u counter query is not supported.",
4140 		 dev->data->port_id);
4141 	return -ENOTSUP;
4142 }
4143 
4144 /* Void driver to protect from null pointer reference. */
4145 const struct mlx5_flow_driver_ops mlx5_flow_null_drv_ops = {
4146 	.validate = flow_null_validate,
4147 	.prepare = flow_null_prepare,
4148 	.translate = flow_null_translate,
4149 	.apply = flow_null_apply,
4150 	.remove = flow_null_remove,
4151 	.destroy = flow_null_destroy,
4152 	.query = flow_null_query,
4153 	.sync_domain = flow_null_sync_domain,
4154 	.get_aged_flows = flow_null_get_aged_flows,
4155 	.counter_alloc = flow_null_counter_allocate,
4156 	.counter_free = flow_null_counter_free,
4157 	.counter_query = flow_null_counter_query
4158 };
4159 
4160 /**
4161  * Select flow driver type according to flow attributes and device
4162  * configuration.
4163  *
4164  * @param[in] dev
4165  *   Pointer to the dev structure.
4166  * @param[in] attr
4167  *   Pointer to the flow attributes.
4168  *
4169  * @return
4170  *   flow driver type, MLX5_FLOW_TYPE_MAX otherwise.
4171  */
4172 static enum mlx5_flow_drv_type
4173 flow_get_drv_type(struct rte_eth_dev *dev, const struct rte_flow_attr *attr)
4174 {
4175 	struct mlx5_priv *priv = dev->data->dev_private;
4176 	/* The OS can determine first a specific flow type (DV, VERBS) */
4177 	enum mlx5_flow_drv_type type = mlx5_flow_os_get_type();
4178 
4179 	if (type != MLX5_FLOW_TYPE_MAX)
4180 		return type;
4181 	/*
4182 	 * Currently when dv_flow_en == 2, only HW steering engine is
4183 	 * supported. New engines can also be chosen here if ready.
4184 	 */
4185 	if (priv->sh->config.dv_flow_en == 2)
4186 		return MLX5_FLOW_TYPE_HW;
4187 	if (!attr)
4188 		return MLX5_FLOW_TYPE_MIN;
4189 	/* If no OS specific type - continue with DV/VERBS selection */
4190 	if (attr->transfer && priv->sh->config.dv_esw_en)
4191 		type = MLX5_FLOW_TYPE_DV;
4192 	if (!attr->transfer)
4193 		type = priv->sh->config.dv_flow_en ? MLX5_FLOW_TYPE_DV :
4194 						     MLX5_FLOW_TYPE_VERBS;
4195 	return type;
4196 }
4197 
4198 #define flow_get_drv_ops(type) flow_drv_ops[type]
4199 
4200 /**
4201  * Flow driver validation API. This abstracts calling driver specific functions.
4202  * The type of flow driver is determined according to flow attributes.
4203  *
4204  * @param[in] dev
4205  *   Pointer to the dev structure.
4206  * @param[in] attr
4207  *   Pointer to the flow attributes.
4208  * @param[in] items
4209  *   Pointer to the list of items.
4210  * @param[in] actions
4211  *   Pointer to the list of actions.
4212  * @param[in] external
4213  *   This flow rule is created by request external to PMD.
4214  * @param[in] hairpin
4215  *   Number of hairpin TX actions, 0 means classic flow.
4216  * @param[out] error
4217  *   Pointer to the error structure.
4218  *
4219  * @return
4220  *   0 on success, a negative errno value otherwise and rte_errno is set.
4221  */
4222 static inline int
4223 flow_drv_validate(struct rte_eth_dev *dev,
4224 		  const struct rte_flow_attr *attr,
4225 		  const struct rte_flow_item items[],
4226 		  const struct rte_flow_action actions[],
4227 		  bool external, int hairpin, struct rte_flow_error *error)
4228 {
4229 	const struct mlx5_flow_driver_ops *fops;
4230 	enum mlx5_flow_drv_type type = flow_get_drv_type(dev, attr);
4231 
4232 	fops = flow_get_drv_ops(type);
4233 	return fops->validate(dev, attr, items, actions, external,
4234 			      hairpin, error);
4235 }
4236 
4237 /**
4238  * Flow driver preparation API. This abstracts calling driver specific
4239  * functions. Parent flow (rte_flow) should have driver type (drv_type). It
4240  * calculates the size of memory required for device flow, allocates the memory,
4241  * initializes the device flow and returns the pointer.
4242  *
4243  * @note
4244  *   This function initializes device flow structure such as dv or verbs in
4245  *   struct mlx5_flow. However, it is caller's responsibility to initialize the
4246  *   rest. For example, adding returning device flow to flow->dev_flow list and
4247  *   setting backward reference to the flow should be done out of this function.
4248  *   layers field is not filled either.
4249  *
4250  * @param[in] dev
4251  *   Pointer to the dev structure.
4252  * @param[in] attr
4253  *   Pointer to the flow attributes.
4254  * @param[in] items
4255  *   Pointer to the list of items.
4256  * @param[in] actions
4257  *   Pointer to the list of actions.
4258  * @param[in] flow_idx
4259  *   This memory pool index to the flow.
4260  * @param[out] error
4261  *   Pointer to the error structure.
4262  *
4263  * @return
4264  *   Pointer to device flow on success, otherwise NULL and rte_errno is set.
4265  */
4266 static inline struct mlx5_flow *
4267 flow_drv_prepare(struct rte_eth_dev *dev,
4268 		 const struct rte_flow *flow,
4269 		 const struct rte_flow_attr *attr,
4270 		 const struct rte_flow_item items[],
4271 		 const struct rte_flow_action actions[],
4272 		 uint32_t flow_idx,
4273 		 struct rte_flow_error *error)
4274 {
4275 	const struct mlx5_flow_driver_ops *fops;
4276 	enum mlx5_flow_drv_type type = flow->drv_type;
4277 	struct mlx5_flow *mlx5_flow = NULL;
4278 
4279 	MLX5_ASSERT(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
4280 	fops = flow_get_drv_ops(type);
4281 	mlx5_flow = fops->prepare(dev, attr, items, actions, error);
4282 	if (mlx5_flow)
4283 		mlx5_flow->flow_idx = flow_idx;
4284 	return mlx5_flow;
4285 }
4286 
4287 /**
4288  * Flow driver translation API. This abstracts calling driver specific
4289  * functions. Parent flow (rte_flow) should have driver type (drv_type). It
4290  * translates a generic flow into a driver flow. flow_drv_prepare() must
4291  * precede.
4292  *
4293  * @note
4294  *   dev_flow->layers could be filled as a result of parsing during translation
4295  *   if needed by flow_drv_apply(). dev_flow->flow->actions can also be filled
4296  *   if necessary. As a flow can have multiple dev_flows by RSS flow expansion,
4297  *   flow->actions could be overwritten even though all the expanded dev_flows
4298  *   have the same actions.
4299  *
4300  * @param[in] dev
4301  *   Pointer to the rte dev structure.
4302  * @param[in, out] dev_flow
4303  *   Pointer to the mlx5 flow.
4304  * @param[in] attr
4305  *   Pointer to the flow attributes.
4306  * @param[in] items
4307  *   Pointer to the list of items.
4308  * @param[in] actions
4309  *   Pointer to the list of actions.
4310  * @param[out] error
4311  *   Pointer to the error structure.
4312  *
4313  * @return
4314  *   0 on success, a negative errno value otherwise and rte_errno is set.
4315  */
4316 static inline int
4317 flow_drv_translate(struct rte_eth_dev *dev, struct mlx5_flow *dev_flow,
4318 		   const struct rte_flow_attr *attr,
4319 		   const struct rte_flow_item items[],
4320 		   const struct rte_flow_action actions[],
4321 		   struct rte_flow_error *error)
4322 {
4323 	const struct mlx5_flow_driver_ops *fops;
4324 	enum mlx5_flow_drv_type type = dev_flow->flow->drv_type;
4325 
4326 	MLX5_ASSERT(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
4327 	fops = flow_get_drv_ops(type);
4328 	return fops->translate(dev, dev_flow, attr, items, actions, error);
4329 }
4330 
4331 /**
4332  * Flow driver apply API. This abstracts calling driver specific functions.
4333  * Parent flow (rte_flow) should have driver type (drv_type). It applies
4334  * translated driver flows on to device. flow_drv_translate() must precede.
4335  *
4336  * @param[in] dev
4337  *   Pointer to Ethernet device structure.
4338  * @param[in, out] flow
4339  *   Pointer to flow structure.
4340  * @param[out] error
4341  *   Pointer to error structure.
4342  *
4343  * @return
4344  *   0 on success, a negative errno value otherwise and rte_errno is set.
4345  */
4346 static inline int
4347 flow_drv_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
4348 	       struct rte_flow_error *error)
4349 {
4350 	const struct mlx5_flow_driver_ops *fops;
4351 	enum mlx5_flow_drv_type type = flow->drv_type;
4352 
4353 	MLX5_ASSERT(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
4354 	fops = flow_get_drv_ops(type);
4355 	return fops->apply(dev, flow, error);
4356 }
4357 
4358 /**
4359  * Flow driver destroy API. This abstracts calling driver specific functions.
4360  * Parent flow (rte_flow) should have driver type (drv_type). It removes a flow
4361  * on device and releases resources of the flow.
4362  *
4363  * @param[in] dev
4364  *   Pointer to Ethernet device.
4365  * @param[in, out] flow
4366  *   Pointer to flow structure.
4367  */
4368 static inline void
4369 flow_drv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
4370 {
4371 	const struct mlx5_flow_driver_ops *fops;
4372 	enum mlx5_flow_drv_type type = flow->drv_type;
4373 
4374 	MLX5_ASSERT(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
4375 	fops = flow_get_drv_ops(type);
4376 	fops->destroy(dev, flow);
4377 }
4378 
4379 /**
4380  * Flow driver find RSS policy tbl API. This abstracts calling driver
4381  * specific functions. Parent flow (rte_flow) should have driver
4382  * type (drv_type). It will find the RSS policy table that has the rss_desc.
4383  *
4384  * @param[in] dev
4385  *   Pointer to Ethernet device.
4386  * @param[in, out] flow
4387  *   Pointer to flow structure.
4388  * @param[in] policy
4389  *   Pointer to meter policy table.
4390  * @param[in] rss_desc
4391  *   Pointer to rss_desc
4392  */
4393 static struct mlx5_flow_meter_sub_policy *
4394 flow_drv_meter_sub_policy_rss_prepare(struct rte_eth_dev *dev,
4395 		struct rte_flow *flow,
4396 		struct mlx5_flow_meter_policy *policy,
4397 		struct mlx5_flow_rss_desc *rss_desc[MLX5_MTR_RTE_COLORS])
4398 {
4399 	const struct mlx5_flow_driver_ops *fops;
4400 	enum mlx5_flow_drv_type type = flow->drv_type;
4401 
4402 	MLX5_ASSERT(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
4403 	fops = flow_get_drv_ops(type);
4404 	return fops->meter_sub_policy_rss_prepare(dev, policy, rss_desc);
4405 }
4406 
4407 /**
4408  * Flow driver color tag rule API. This abstracts calling driver
4409  * specific functions. Parent flow (rte_flow) should have driver
4410  * type (drv_type). It will create the color tag rules in hierarchy meter.
4411  *
4412  * @param[in] dev
4413  *   Pointer to Ethernet device.
4414  * @param[in, out] flow
4415  *   Pointer to flow structure.
4416  * @param[in] fm
4417  *   Pointer to flow meter structure.
4418  * @param[in] src_port
4419  *   The src port this extra rule should use.
4420  * @param[in] item
4421  *   The src port id match item.
4422  * @param[out] error
4423  *   Pointer to error structure.
4424  */
4425 static int
4426 flow_drv_mtr_hierarchy_rule_create(struct rte_eth_dev *dev,
4427 		struct rte_flow *flow,
4428 		struct mlx5_flow_meter_info *fm,
4429 		int32_t src_port,
4430 		const struct rte_flow_item *item,
4431 		struct rte_flow_error *error)
4432 {
4433 	const struct mlx5_flow_driver_ops *fops;
4434 	enum mlx5_flow_drv_type type = flow->drv_type;
4435 
4436 	MLX5_ASSERT(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
4437 	fops = flow_get_drv_ops(type);
4438 	return fops->meter_hierarchy_rule_create(dev, fm,
4439 						src_port, item, error);
4440 }
4441 
4442 /**
4443  * Get RSS action from the action list.
4444  *
4445  * @param[in] dev
4446  *   Pointer to Ethernet device.
4447  * @param[in] actions
4448  *   Pointer to the list of actions.
4449  * @param[in] flow
4450  *   Parent flow structure pointer.
4451  *
4452  * @return
4453  *   Pointer to the RSS action if exist, else return NULL.
4454  */
4455 static const struct rte_flow_action_rss*
4456 flow_get_rss_action(struct rte_eth_dev *dev,
4457 		    const struct rte_flow_action actions[])
4458 {
4459 	struct mlx5_priv *priv = dev->data->dev_private;
4460 	const struct rte_flow_action_rss *rss = NULL;
4461 	struct mlx5_meter_policy_action_container *acg;
4462 	struct mlx5_meter_policy_action_container *acy;
4463 
4464 	for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
4465 		switch (actions->type) {
4466 		case RTE_FLOW_ACTION_TYPE_RSS:
4467 			rss = actions->conf;
4468 			break;
4469 		case RTE_FLOW_ACTION_TYPE_SAMPLE:
4470 		{
4471 			const struct rte_flow_action_sample *sample =
4472 								actions->conf;
4473 			const struct rte_flow_action *act = sample->actions;
4474 			for (; act->type != RTE_FLOW_ACTION_TYPE_END; act++)
4475 				if (act->type == RTE_FLOW_ACTION_TYPE_RSS)
4476 					rss = act->conf;
4477 			break;
4478 		}
4479 		case RTE_FLOW_ACTION_TYPE_METER:
4480 		{
4481 			uint32_t mtr_idx;
4482 			struct mlx5_flow_meter_info *fm;
4483 			struct mlx5_flow_meter_policy *policy;
4484 			const struct rte_flow_action_meter *mtr = actions->conf;
4485 
4486 			fm = mlx5_flow_meter_find(priv, mtr->mtr_id, &mtr_idx);
4487 			if (fm && !fm->def_policy) {
4488 				policy = mlx5_flow_meter_policy_find(dev,
4489 						fm->policy_id, NULL);
4490 				MLX5_ASSERT(policy);
4491 				if (policy->is_hierarchy) {
4492 					policy =
4493 				mlx5_flow_meter_hierarchy_get_final_policy(dev,
4494 									policy);
4495 					if (!policy)
4496 						return NULL;
4497 				}
4498 				if (policy->is_rss) {
4499 					acg =
4500 					&policy->act_cnt[RTE_COLOR_GREEN];
4501 					acy =
4502 					&policy->act_cnt[RTE_COLOR_YELLOW];
4503 					if (acg->fate_action ==
4504 					    MLX5_FLOW_FATE_SHARED_RSS)
4505 						rss = acg->rss->conf;
4506 					else if (acy->fate_action ==
4507 						 MLX5_FLOW_FATE_SHARED_RSS)
4508 						rss = acy->rss->conf;
4509 				}
4510 			}
4511 			break;
4512 		}
4513 		default:
4514 			break;
4515 		}
4516 	}
4517 	return rss;
4518 }
4519 
4520 /**
4521  * Get ASO age action by index.
4522  *
4523  * @param[in] dev
4524  *   Pointer to the Ethernet device structure.
4525  * @param[in] age_idx
4526  *   Index to the ASO age action.
4527  *
4528  * @return
4529  *   The specified ASO age action.
4530  */
4531 struct mlx5_aso_age_action*
4532 flow_aso_age_get_by_idx(struct rte_eth_dev *dev, uint32_t age_idx)
4533 {
4534 	uint16_t pool_idx = age_idx & UINT16_MAX;
4535 	uint16_t offset = (age_idx >> 16) & UINT16_MAX;
4536 	struct mlx5_priv *priv = dev->data->dev_private;
4537 	struct mlx5_aso_age_mng *mng = priv->sh->aso_age_mng;
4538 	struct mlx5_aso_age_pool *pool;
4539 
4540 	rte_rwlock_read_lock(&mng->resize_rwl);
4541 	pool = mng->pools[pool_idx];
4542 	rte_rwlock_read_unlock(&mng->resize_rwl);
4543 	return &pool->actions[offset - 1];
4544 }
4545 
4546 /* maps indirect action to translated direct in some actions array */
4547 struct mlx5_translated_action_handle {
4548 	struct rte_flow_action_handle *action; /**< Indirect action handle. */
4549 	int index; /**< Index in related array of rte_flow_action. */
4550 };
4551 
4552 /**
4553  * Translates actions of type RTE_FLOW_ACTION_TYPE_INDIRECT to related
4554  * direct action if translation possible.
4555  * This functionality used to run same execution path for both direct and
4556  * indirect actions on flow create. All necessary preparations for indirect
4557  * action handling should be performed on *handle* actions list returned
4558  * from this call.
4559  *
4560  * @param[in] dev
4561  *   Pointer to Ethernet device.
4562  * @param[in] actions
4563  *   List of actions to translate.
4564  * @param[out] handle
4565  *   List to store translated indirect action object handles.
4566  * @param[in, out] indir_n
4567  *   Size of *handle* array. On return should be updated with number of
4568  *   indirect actions retrieved from the *actions* list.
4569  * @param[out] translated_actions
4570  *   List of actions where all indirect actions were translated to direct
4571  *   if possible. NULL if no translation took place.
4572  * @param[out] error
4573  *   Pointer to the error structure.
4574  *
4575  * @return
4576  *   0 on success, a negative errno value otherwise and rte_errno is set.
4577  */
4578 static int
4579 flow_action_handles_translate(struct rte_eth_dev *dev,
4580 			      const struct rte_flow_action actions[],
4581 			      struct mlx5_translated_action_handle *handle,
4582 			      int *indir_n,
4583 			      struct rte_flow_action **translated_actions,
4584 			      struct rte_flow_error *error)
4585 {
4586 	struct mlx5_priv *priv = dev->data->dev_private;
4587 	struct rte_flow_action *translated = NULL;
4588 	size_t actions_size;
4589 	int n;
4590 	int copied_n = 0;
4591 	struct mlx5_translated_action_handle *handle_end = NULL;
4592 
4593 	for (n = 0; actions[n].type != RTE_FLOW_ACTION_TYPE_END; n++) {
4594 		if (actions[n].type != RTE_FLOW_ACTION_TYPE_INDIRECT)
4595 			continue;
4596 		if (copied_n == *indir_n) {
4597 			return rte_flow_error_set
4598 				(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION_NUM,
4599 				 NULL, "too many shared actions");
4600 		}
4601 		rte_memcpy(&handle[copied_n].action, &actions[n].conf,
4602 			   sizeof(actions[n].conf));
4603 		handle[copied_n].index = n;
4604 		copied_n++;
4605 	}
4606 	n++;
4607 	*indir_n = copied_n;
4608 	if (!copied_n)
4609 		return 0;
4610 	actions_size = sizeof(struct rte_flow_action) * n;
4611 	translated = mlx5_malloc(MLX5_MEM_ZERO, actions_size, 0, SOCKET_ID_ANY);
4612 	if (!translated) {
4613 		rte_errno = ENOMEM;
4614 		return -ENOMEM;
4615 	}
4616 	memcpy(translated, actions, actions_size);
4617 	for (handle_end = handle + copied_n; handle < handle_end; handle++) {
4618 		struct mlx5_shared_action_rss *shared_rss;
4619 		uint32_t act_idx = (uint32_t)(uintptr_t)handle->action;
4620 		uint32_t type = act_idx >> MLX5_INDIRECT_ACTION_TYPE_OFFSET;
4621 		uint32_t idx = act_idx &
4622 			       ((1u << MLX5_INDIRECT_ACTION_TYPE_OFFSET) - 1);
4623 
4624 		switch (type) {
4625 		case MLX5_INDIRECT_ACTION_TYPE_RSS:
4626 			shared_rss = mlx5_ipool_get
4627 			  (priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS], idx);
4628 			translated[handle->index].type =
4629 				RTE_FLOW_ACTION_TYPE_RSS;
4630 			translated[handle->index].conf =
4631 				&shared_rss->origin;
4632 			break;
4633 		case MLX5_INDIRECT_ACTION_TYPE_COUNT:
4634 			translated[handle->index].type =
4635 						(enum rte_flow_action_type)
4636 						MLX5_RTE_FLOW_ACTION_TYPE_COUNT;
4637 			translated[handle->index].conf = (void *)(uintptr_t)idx;
4638 			break;
4639 		case MLX5_INDIRECT_ACTION_TYPE_METER_MARK:
4640 			translated[handle->index].type =
4641 						(enum rte_flow_action_type)
4642 						MLX5_RTE_FLOW_ACTION_TYPE_METER_MARK;
4643 			translated[handle->index].conf = (void *)(uintptr_t)idx;
4644 			break;
4645 		case MLX5_INDIRECT_ACTION_TYPE_AGE:
4646 			if (priv->sh->flow_hit_aso_en) {
4647 				translated[handle->index].type =
4648 					(enum rte_flow_action_type)
4649 					MLX5_RTE_FLOW_ACTION_TYPE_AGE;
4650 				translated[handle->index].conf =
4651 							 (void *)(uintptr_t)idx;
4652 				break;
4653 			}
4654 			/* Fall-through */
4655 		case MLX5_INDIRECT_ACTION_TYPE_CT:
4656 			if (priv->sh->ct_aso_en) {
4657 				translated[handle->index].type =
4658 					RTE_FLOW_ACTION_TYPE_CONNTRACK;
4659 				translated[handle->index].conf =
4660 							 (void *)(uintptr_t)idx;
4661 				break;
4662 			}
4663 			/* Fall-through */
4664 		default:
4665 			mlx5_free(translated);
4666 			return rte_flow_error_set
4667 				(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
4668 				 NULL, "invalid indirect action type");
4669 		}
4670 	}
4671 	*translated_actions = translated;
4672 	return 0;
4673 }
4674 
4675 /**
4676  * Get Shared RSS action from the action list.
4677  *
4678  * @param[in] dev
4679  *   Pointer to Ethernet device.
4680  * @param[in] shared
4681  *   Pointer to the list of actions.
4682  * @param[in] shared_n
4683  *   Actions list length.
4684  *
4685  * @return
4686  *   The MLX5 RSS action ID if exists, otherwise return 0.
4687  */
4688 static uint32_t
4689 flow_get_shared_rss_action(struct rte_eth_dev *dev,
4690 			   struct mlx5_translated_action_handle *handle,
4691 			   int shared_n)
4692 {
4693 	struct mlx5_translated_action_handle *handle_end;
4694 	struct mlx5_priv *priv = dev->data->dev_private;
4695 	struct mlx5_shared_action_rss *shared_rss;
4696 
4697 
4698 	for (handle_end = handle + shared_n; handle < handle_end; handle++) {
4699 		uint32_t act_idx = (uint32_t)(uintptr_t)handle->action;
4700 		uint32_t type = act_idx >> MLX5_INDIRECT_ACTION_TYPE_OFFSET;
4701 		uint32_t idx = act_idx &
4702 			       ((1u << MLX5_INDIRECT_ACTION_TYPE_OFFSET) - 1);
4703 		switch (type) {
4704 		case MLX5_INDIRECT_ACTION_TYPE_RSS:
4705 			shared_rss = mlx5_ipool_get
4706 				(priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS],
4707 									   idx);
4708 			rte_atomic_fetch_add_explicit(&shared_rss->refcnt, 1,
4709 					   rte_memory_order_relaxed);
4710 			return idx;
4711 		default:
4712 			break;
4713 		}
4714 	}
4715 	return 0;
4716 }
4717 
4718 static unsigned int
4719 find_graph_root(uint32_t rss_level)
4720 {
4721 	return rss_level < 2 ? MLX5_EXPANSION_ROOT :
4722 			       MLX5_EXPANSION_ROOT_OUTER;
4723 }
4724 
4725 /**
4726  *  Get layer flags from the prefix flow.
4727  *
4728  *  Some flows may be split to several subflows, the prefix subflow gets the
4729  *  match items and the suffix sub flow gets the actions.
4730  *  Some actions need the user defined match item flags to get the detail for
4731  *  the action.
4732  *  This function helps the suffix flow to get the item layer flags from prefix
4733  *  subflow.
4734  *
4735  * @param[in] dev_flow
4736  *   Pointer the created prefix subflow.
4737  *
4738  * @return
4739  *   The layers get from prefix subflow.
4740  */
4741 static inline uint64_t
4742 flow_get_prefix_layer_flags(struct mlx5_flow *dev_flow)
4743 {
4744 	uint64_t layers = 0;
4745 
4746 	/*
4747 	 * Layers bits could be localization, but usually the compiler will
4748 	 * help to do the optimization work for source code.
4749 	 * If no decap actions, use the layers directly.
4750 	 */
4751 	if (!(dev_flow->act_flags & MLX5_FLOW_ACTION_DECAP))
4752 		return dev_flow->handle->layers;
4753 	/* Convert L3 layers with decap action. */
4754 	if (dev_flow->handle->layers & MLX5_FLOW_LAYER_INNER_L3_IPV4)
4755 		layers |= MLX5_FLOW_LAYER_OUTER_L3_IPV4;
4756 	else if (dev_flow->handle->layers & MLX5_FLOW_LAYER_INNER_L3_IPV6)
4757 		layers |= MLX5_FLOW_LAYER_OUTER_L3_IPV6;
4758 	/* Convert L4 layers with decap action.  */
4759 	if (dev_flow->handle->layers & MLX5_FLOW_LAYER_INNER_L4_TCP)
4760 		layers |= MLX5_FLOW_LAYER_OUTER_L4_TCP;
4761 	else if (dev_flow->handle->layers & MLX5_FLOW_LAYER_INNER_L4_UDP)
4762 		layers |= MLX5_FLOW_LAYER_OUTER_L4_UDP;
4763 	return layers;
4764 }
4765 
4766 /**
4767  * Get metadata split action information.
4768  *
4769  * @param[in] actions
4770  *   Pointer to the list of actions.
4771  * @param[out] qrss
4772  *   Pointer to the return pointer.
4773  * @param[out] qrss_type
4774  *   Pointer to the action type to return. RTE_FLOW_ACTION_TYPE_END is returned
4775  *   if no QUEUE/RSS is found.
4776  * @param[out] encap_idx
4777  *   Pointer to the index of the encap action if exists, otherwise the last
4778  *   action index.
4779  *
4780  * @return
4781  *   Total number of actions.
4782  */
4783 static int
4784 flow_parse_metadata_split_actions_info(const struct rte_flow_action actions[],
4785 				       const struct rte_flow_action **qrss,
4786 				       int *encap_idx)
4787 {
4788 	const struct rte_flow_action_raw_encap *raw_encap;
4789 	int actions_n = 0;
4790 	int raw_decap_idx = -1;
4791 
4792 	*encap_idx = -1;
4793 	for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
4794 		switch (actions->type) {
4795 		case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
4796 		case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
4797 			*encap_idx = actions_n;
4798 			break;
4799 		case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
4800 			raw_decap_idx = actions_n;
4801 			break;
4802 		case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
4803 			raw_encap = actions->conf;
4804 			if (raw_encap->size > MLX5_ENCAPSULATION_DECISION_SIZE)
4805 				*encap_idx = raw_decap_idx != -1 ?
4806 						      raw_decap_idx : actions_n;
4807 			break;
4808 		case RTE_FLOW_ACTION_TYPE_QUEUE:
4809 		case RTE_FLOW_ACTION_TYPE_RSS:
4810 			*qrss = actions;
4811 			break;
4812 		default:
4813 			break;
4814 		}
4815 		actions_n++;
4816 	}
4817 	if (*encap_idx == -1)
4818 		*encap_idx = actions_n;
4819 	/* Count RTE_FLOW_ACTION_TYPE_END. */
4820 	return actions_n + 1;
4821 }
4822 
4823 /**
4824  * Check if the action will change packet.
4825  *
4826  * @param dev
4827  *   Pointer to Ethernet device.
4828  * @param[in] type
4829  *   action type.
4830  *
4831  * @return
4832  *   true if action will change packet, false otherwise.
4833  */
4834 static bool flow_check_modify_action_type(struct rte_eth_dev *dev,
4835 					  enum rte_flow_action_type type)
4836 {
4837 	struct mlx5_priv *priv = dev->data->dev_private;
4838 
4839 	switch (type) {
4840 	case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
4841 	case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
4842 	case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
4843 	case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
4844 	case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
4845 	case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
4846 	case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
4847 	case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
4848 	case RTE_FLOW_ACTION_TYPE_DEC_TTL:
4849 	case RTE_FLOW_ACTION_TYPE_SET_TTL:
4850 	case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
4851 	case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
4852 	case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
4853 	case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
4854 	case RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP:
4855 	case RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP:
4856 	case RTE_FLOW_ACTION_TYPE_SET_META:
4857 	case RTE_FLOW_ACTION_TYPE_SET_TAG:
4858 	case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
4859 	case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
4860 	case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
4861 	case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
4862 	case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
4863 	case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
4864 	case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
4865 	case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
4866 	case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
4867 	case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
4868 	case RTE_FLOW_ACTION_TYPE_MODIFY_FIELD:
4869 		return true;
4870 	case RTE_FLOW_ACTION_TYPE_FLAG:
4871 	case RTE_FLOW_ACTION_TYPE_MARK:
4872 		if (priv->sh->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
4873 		    priv->sh->config.dv_xmeta_en != MLX5_XMETA_MODE_META32_HWS)
4874 			return true;
4875 		else
4876 			return false;
4877 	default:
4878 		return false;
4879 	}
4880 }
4881 
4882 /**
4883  * Check meter action from the action list.
4884  *
4885  * @param dev
4886  *   Pointer to Ethernet device.
4887  * @param[in] actions
4888  *   Pointer to the list of actions.
4889  * @param[out] has_mtr
4890  *   Pointer to the meter exist flag.
4891  * @param[out] has_modify
4892  *   Pointer to the flag showing there's packet change action.
4893  * @param[out] meter_id
4894  *   Pointer to the meter id.
4895  *
4896  * @return
4897  *   Total number of actions.
4898  */
4899 static int
4900 flow_check_meter_action(struct rte_eth_dev *dev,
4901 			const struct rte_flow_action actions[],
4902 			bool *has_mtr, bool *has_modify, uint32_t *meter_id)
4903 {
4904 	const struct rte_flow_action_meter *mtr = NULL;
4905 	int actions_n = 0;
4906 
4907 	MLX5_ASSERT(has_mtr);
4908 	*has_mtr = false;
4909 	for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
4910 		switch (actions->type) {
4911 		case RTE_FLOW_ACTION_TYPE_METER:
4912 			mtr = actions->conf;
4913 			*meter_id = mtr->mtr_id;
4914 			*has_mtr = true;
4915 			break;
4916 		default:
4917 			break;
4918 		}
4919 		if (!*has_mtr)
4920 			*has_modify |= flow_check_modify_action_type(dev,
4921 								actions->type);
4922 		actions_n++;
4923 	}
4924 	/* Count RTE_FLOW_ACTION_TYPE_END. */
4925 	return actions_n + 1;
4926 }
4927 
4928 /**
4929  * Check if the flow should be split due to hairpin.
4930  * The reason for the split is that in current HW we can't
4931  * support encap and push-vlan on Rx, so if a flow contains
4932  * these actions we move it to Tx.
4933  *
4934  * @param dev
4935  *   Pointer to Ethernet device.
4936  * @param[in] attr
4937  *   Flow rule attributes.
4938  * @param[in] actions
4939  *   Associated actions (list terminated by the END action).
4940  *
4941  * @return
4942  *   > 0 the number of actions and the flow should be split,
4943  *   0 when no split required.
4944  */
4945 static int
4946 flow_check_hairpin_split(struct rte_eth_dev *dev,
4947 			 const struct rte_flow_attr *attr,
4948 			 const struct rte_flow_action actions[])
4949 {
4950 	int queue_action = 0;
4951 	int action_n = 0;
4952 	int split = 0;
4953 	int push_vlan = 0;
4954 	const struct rte_flow_action_queue *queue;
4955 	const struct rte_flow_action_rss *rss;
4956 	const struct rte_flow_action_raw_encap *raw_encap;
4957 	const struct rte_eth_hairpin_conf *conf;
4958 
4959 	if (!attr->ingress)
4960 		return 0;
4961 	for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
4962 		if (actions->type == RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN)
4963 			push_vlan = 1;
4964 		switch (actions->type) {
4965 		case RTE_FLOW_ACTION_TYPE_QUEUE:
4966 			queue = actions->conf;
4967 			if (queue == NULL)
4968 				return 0;
4969 			conf = mlx5_rxq_get_hairpin_conf(dev, queue->index);
4970 			if (conf == NULL || conf->tx_explicit != 0)
4971 				return 0;
4972 			queue_action = 1;
4973 			action_n++;
4974 			break;
4975 		case RTE_FLOW_ACTION_TYPE_RSS:
4976 			rss = actions->conf;
4977 			if (rss == NULL || rss->queue_num == 0)
4978 				return 0;
4979 			conf = mlx5_rxq_get_hairpin_conf(dev, rss->queue[0]);
4980 			if (conf == NULL || conf->tx_explicit != 0)
4981 				return 0;
4982 			queue_action = 1;
4983 			action_n++;
4984 			break;
4985 		case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
4986 		case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
4987 		case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
4988 		case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
4989 			split++;
4990 			action_n++;
4991 			break;
4992 		case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
4993 			if (push_vlan)
4994 				split++;
4995 			action_n++;
4996 			break;
4997 		case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
4998 			raw_encap = actions->conf;
4999 			if (raw_encap->size > MLX5_ENCAPSULATION_DECISION_SIZE)
5000 				split++;
5001 			action_n++;
5002 			break;
5003 		default:
5004 			action_n++;
5005 			break;
5006 		}
5007 	}
5008 	if (split && queue_action)
5009 		return action_n;
5010 	return 0;
5011 }
5012 
5013 int
5014 flow_dv_mreg_match_cb(void *tool_ctx __rte_unused,
5015 		      struct mlx5_list_entry *entry, void *cb_ctx)
5016 {
5017 	struct mlx5_flow_cb_ctx *ctx = cb_ctx;
5018 	struct mlx5_flow_mreg_copy_resource *mcp_res =
5019 			       container_of(entry, typeof(*mcp_res), hlist_ent);
5020 
5021 	return mcp_res->mark_id != *(uint32_t *)(ctx->data);
5022 }
5023 
5024 struct mlx5_list_entry *
5025 flow_dv_mreg_create_cb(void *tool_ctx, void *cb_ctx)
5026 {
5027 	struct rte_eth_dev *dev = tool_ctx;
5028 	struct mlx5_priv *priv = dev->data->dev_private;
5029 	struct mlx5_flow_cb_ctx *ctx = cb_ctx;
5030 	struct mlx5_flow_mreg_copy_resource *mcp_res;
5031 	struct rte_flow_error *error = ctx->error;
5032 	uint32_t idx = 0;
5033 	int ret;
5034 	uint32_t mark_id = *(uint32_t *)(ctx->data);
5035 	struct rte_flow_attr attr = {
5036 		.group = MLX5_FLOW_MREG_CP_TABLE_GROUP,
5037 		.ingress = 1,
5038 	};
5039 	struct mlx5_rte_flow_item_tag tag_spec = {
5040 		.data = mark_id,
5041 	};
5042 	struct rte_flow_item items[] = {
5043 		[1] = { .type = RTE_FLOW_ITEM_TYPE_END, },
5044 	};
5045 	struct rte_flow_action_mark ftag = {
5046 		.id = mark_id,
5047 	};
5048 	struct mlx5_flow_action_copy_mreg cp_mreg = {
5049 		.dst = REG_B,
5050 		.src = REG_NON,
5051 	};
5052 	struct rte_flow_action_jump jump = {
5053 		.group = MLX5_FLOW_MREG_ACT_TABLE_GROUP,
5054 	};
5055 	struct rte_flow_action actions[] = {
5056 		[3] = { .type = RTE_FLOW_ACTION_TYPE_END, },
5057 	};
5058 
5059 	/* Fill the register fields in the flow. */
5060 	ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
5061 	if (ret < 0)
5062 		return NULL;
5063 	tag_spec.id = ret;
5064 	ret = mlx5_flow_get_reg_id(dev, MLX5_METADATA_RX, 0, error);
5065 	if (ret < 0)
5066 		return NULL;
5067 	cp_mreg.src = ret;
5068 	/* Provide the full width of FLAG specific value. */
5069 	if (mark_id == (priv->sh->dv_regc0_mask & MLX5_FLOW_MARK_DEFAULT))
5070 		tag_spec.data = MLX5_FLOW_MARK_DEFAULT;
5071 	/* Build a new flow. */
5072 	if (mark_id != MLX5_DEFAULT_COPY_ID) {
5073 		items[0] = (struct rte_flow_item){
5074 			.type = (enum rte_flow_item_type)
5075 				MLX5_RTE_FLOW_ITEM_TYPE_TAG,
5076 			.spec = &tag_spec,
5077 		};
5078 		items[1] = (struct rte_flow_item){
5079 			.type = RTE_FLOW_ITEM_TYPE_END,
5080 		};
5081 		actions[0] = (struct rte_flow_action){
5082 			.type = (enum rte_flow_action_type)
5083 				MLX5_RTE_FLOW_ACTION_TYPE_MARK,
5084 			.conf = &ftag,
5085 		};
5086 		actions[1] = (struct rte_flow_action){
5087 			.type = (enum rte_flow_action_type)
5088 				MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG,
5089 			.conf = &cp_mreg,
5090 		};
5091 		actions[2] = (struct rte_flow_action){
5092 			.type = RTE_FLOW_ACTION_TYPE_JUMP,
5093 			.conf = &jump,
5094 		};
5095 		actions[3] = (struct rte_flow_action){
5096 			.type = RTE_FLOW_ACTION_TYPE_END,
5097 		};
5098 	} else {
5099 		/* Default rule, wildcard match. */
5100 		attr.priority = MLX5_FLOW_LOWEST_PRIO_INDICATOR;
5101 		items[0] = (struct rte_flow_item){
5102 			.type = RTE_FLOW_ITEM_TYPE_END,
5103 		};
5104 		actions[0] = (struct rte_flow_action){
5105 			.type = (enum rte_flow_action_type)
5106 				MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG,
5107 			.conf = &cp_mreg,
5108 		};
5109 		actions[1] = (struct rte_flow_action){
5110 			.type = RTE_FLOW_ACTION_TYPE_JUMP,
5111 			.conf = &jump,
5112 		};
5113 		actions[2] = (struct rte_flow_action){
5114 			.type = RTE_FLOW_ACTION_TYPE_END,
5115 		};
5116 	}
5117 	/* Build a new entry. */
5118 	mcp_res = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_MCP], &idx);
5119 	if (!mcp_res) {
5120 		rte_errno = ENOMEM;
5121 		return NULL;
5122 	}
5123 	mcp_res->idx = idx;
5124 	mcp_res->mark_id = mark_id;
5125 	/*
5126 	 * The copy Flows are not included in any list. There
5127 	 * ones are referenced from other Flows and can not
5128 	 * be applied, removed, deleted in arbitrary order
5129 	 * by list traversing.
5130 	 */
5131 	mcp_res->rix_flow = mlx5_flow_list_create(dev, MLX5_FLOW_TYPE_MCP,
5132 					&attr, items, actions, false, error);
5133 	if (!mcp_res->rix_flow) {
5134 		mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MCP], idx);
5135 		return NULL;
5136 	}
5137 	return &mcp_res->hlist_ent;
5138 }
5139 
5140 struct mlx5_list_entry *
5141 flow_dv_mreg_clone_cb(void *tool_ctx, struct mlx5_list_entry *oentry,
5142 		      void *cb_ctx __rte_unused)
5143 {
5144 	struct rte_eth_dev *dev = tool_ctx;
5145 	struct mlx5_priv *priv = dev->data->dev_private;
5146 	struct mlx5_flow_mreg_copy_resource *mcp_res;
5147 	uint32_t idx = 0;
5148 
5149 	mcp_res = mlx5_ipool_malloc(priv->sh->ipool[MLX5_IPOOL_MCP], &idx);
5150 	if (!mcp_res) {
5151 		rte_errno = ENOMEM;
5152 		return NULL;
5153 	}
5154 	memcpy(mcp_res, oentry, sizeof(*mcp_res));
5155 	mcp_res->idx = idx;
5156 	return &mcp_res->hlist_ent;
5157 }
5158 
5159 void
5160 flow_dv_mreg_clone_free_cb(void *tool_ctx, struct mlx5_list_entry *entry)
5161 {
5162 	struct mlx5_flow_mreg_copy_resource *mcp_res =
5163 			       container_of(entry, typeof(*mcp_res), hlist_ent);
5164 	struct rte_eth_dev *dev = tool_ctx;
5165 	struct mlx5_priv *priv = dev->data->dev_private;
5166 
5167 	mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MCP], mcp_res->idx);
5168 }
5169 
5170 /**
5171  * Add a flow of copying flow metadata registers in RX_CP_TBL.
5172  *
5173  * As mark_id is unique, if there's already a registered flow for the mark_id,
5174  * return by increasing the reference counter of the resource. Otherwise, create
5175  * the resource (mcp_res) and flow.
5176  *
5177  * Flow looks like,
5178  *   - If ingress port is ANY and reg_c[1] is mark_id,
5179  *     flow_tag := mark_id, reg_b := reg_c[0] and jump to RX_ACT_TBL.
5180  *
5181  * For default flow (zero mark_id), flow is like,
5182  *   - If ingress port is ANY,
5183  *     reg_b := reg_c[0] and jump to RX_ACT_TBL.
5184  *
5185  * @param dev
5186  *   Pointer to Ethernet device.
5187  * @param mark_id
5188  *   ID of MARK action, zero means default flow for META.
5189  * @param[out] error
5190  *   Perform verbose error reporting if not NULL.
5191  *
5192  * @return
5193  *   Associated resource on success, NULL otherwise and rte_errno is set.
5194  */
5195 static struct mlx5_flow_mreg_copy_resource *
5196 flow_mreg_add_copy_action(struct rte_eth_dev *dev, uint32_t mark_id,
5197 			  struct rte_flow_error *error)
5198 {
5199 	struct mlx5_priv *priv = dev->data->dev_private;
5200 	struct mlx5_list_entry *entry;
5201 	struct mlx5_flow_cb_ctx ctx = {
5202 		.dev = dev,
5203 		.error = error,
5204 		.data = &mark_id,
5205 	};
5206 
5207 	/* Check if already registered. */
5208 	MLX5_ASSERT(priv->sh->mreg_cp_tbl);
5209 	entry = mlx5_hlist_register(priv->sh->mreg_cp_tbl, mark_id, &ctx);
5210 	if (!entry)
5211 		return NULL;
5212 	return container_of(entry, struct mlx5_flow_mreg_copy_resource,
5213 			    hlist_ent);
5214 }
5215 
5216 void
5217 flow_dv_mreg_remove_cb(void *tool_ctx, struct mlx5_list_entry *entry)
5218 {
5219 	struct mlx5_flow_mreg_copy_resource *mcp_res =
5220 			       container_of(entry, typeof(*mcp_res), hlist_ent);
5221 	struct rte_eth_dev *dev = tool_ctx;
5222 	struct mlx5_priv *priv = dev->data->dev_private;
5223 
5224 	MLX5_ASSERT(mcp_res->rix_flow);
5225 	mlx5_flow_list_destroy(dev, MLX5_FLOW_TYPE_MCP, mcp_res->rix_flow);
5226 	mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MCP], mcp_res->idx);
5227 }
5228 
5229 /**
5230  * Release flow in RX_CP_TBL.
5231  *
5232  * @param dev
5233  *   Pointer to Ethernet device.
5234  * @flow
5235  *   Parent flow for wich copying is provided.
5236  */
5237 static void
5238 flow_mreg_del_copy_action(struct rte_eth_dev *dev,
5239 			  struct rte_flow *flow)
5240 {
5241 	struct mlx5_flow_mreg_copy_resource *mcp_res;
5242 	struct mlx5_priv *priv = dev->data->dev_private;
5243 
5244 	if (!flow->rix_mreg_copy)
5245 		return;
5246 	mcp_res = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_MCP],
5247 				 flow->rix_mreg_copy);
5248 	if (!mcp_res || !priv->sh->mreg_cp_tbl)
5249 		return;
5250 	MLX5_ASSERT(mcp_res->rix_flow);
5251 	mlx5_hlist_unregister(priv->sh->mreg_cp_tbl, &mcp_res->hlist_ent);
5252 	flow->rix_mreg_copy = 0;
5253 }
5254 
5255 /**
5256  * Remove the default copy action from RX_CP_TBL.
5257  *
5258  * This functions is called in the mlx5_dev_start(). No thread safe
5259  * is guaranteed.
5260  *
5261  * @param dev
5262  *   Pointer to Ethernet device.
5263  */
5264 static void
5265 flow_mreg_del_default_copy_action(struct rte_eth_dev *dev)
5266 {
5267 	struct mlx5_list_entry *entry;
5268 	struct mlx5_priv *priv = dev->data->dev_private;
5269 	struct mlx5_flow_cb_ctx ctx;
5270 	uint32_t mark_id;
5271 
5272 	/* Check if default flow is registered. */
5273 	if (!priv->sh->mreg_cp_tbl)
5274 		return;
5275 	mark_id = MLX5_DEFAULT_COPY_ID;
5276 	ctx.data = &mark_id;
5277 	entry = mlx5_hlist_lookup(priv->sh->mreg_cp_tbl, mark_id, &ctx);
5278 	if (!entry)
5279 		return;
5280 	mlx5_hlist_unregister(priv->sh->mreg_cp_tbl, entry);
5281 }
5282 
5283 /**
5284  * Add the default copy action in RX_CP_TBL.
5285  *
5286  * This functions is called in the mlx5_dev_start(). No thread safe
5287  * is guaranteed.
5288  *
5289  * @param dev
5290  *   Pointer to Ethernet device.
5291  * @param[out] error
5292  *   Perform verbose error reporting if not NULL.
5293  *
5294  * @return
5295  *   0 for success, negative value otherwise and rte_errno is set.
5296  */
5297 static int
5298 flow_mreg_add_default_copy_action(struct rte_eth_dev *dev,
5299 				  struct rte_flow_error *error)
5300 {
5301 	struct mlx5_priv *priv = dev->data->dev_private;
5302 	struct mlx5_flow_mreg_copy_resource *mcp_res;
5303 	struct mlx5_flow_cb_ctx ctx;
5304 	uint32_t mark_id;
5305 
5306 	/* Check whether extensive metadata feature is engaged. */
5307 	if (!priv->sh->config.dv_flow_en ||
5308 	    priv->sh->config.dv_xmeta_en == MLX5_XMETA_MODE_LEGACY ||
5309 	    !mlx5_flow_ext_mreg_supported(dev) ||
5310 	    !priv->sh->dv_regc0_mask)
5311 		return 0;
5312 	/*
5313 	 * Add default mreg copy flow may be called multiple time, but
5314 	 * only be called once in stop. Avoid register it twice.
5315 	 */
5316 	mark_id = MLX5_DEFAULT_COPY_ID;
5317 	ctx.data = &mark_id;
5318 	if (mlx5_hlist_lookup(priv->sh->mreg_cp_tbl, mark_id, &ctx))
5319 		return 0;
5320 	mcp_res = flow_mreg_add_copy_action(dev, mark_id, error);
5321 	if (!mcp_res)
5322 		return -rte_errno;
5323 	return 0;
5324 }
5325 
5326 /**
5327  * Add a flow of copying flow metadata registers in RX_CP_TBL.
5328  *
5329  * All the flow having Q/RSS action should be split by
5330  * flow_mreg_split_qrss_prep() to pass by RX_CP_TBL. A flow in the RX_CP_TBL
5331  * performs the following,
5332  *   - CQE->flow_tag := reg_c[1] (MARK)
5333  *   - CQE->flow_table_metadata (reg_b) := reg_c[0] (META)
5334  * As CQE's flow_tag is not a register, it can't be simply copied from reg_c[1]
5335  * but there should be a flow per each MARK ID set by MARK action.
5336  *
5337  * For the aforementioned reason, if there's a MARK action in flow's action
5338  * list, a corresponding flow should be added to the RX_CP_TBL in order to copy
5339  * the MARK ID to CQE's flow_tag like,
5340  *   - If reg_c[1] is mark_id,
5341  *     flow_tag := mark_id, reg_b := reg_c[0] and jump to RX_ACT_TBL.
5342  *
5343  * For SET_META action which stores value in reg_c[0], as the destination is
5344  * also a flow metadata register (reg_b), adding a default flow is enough. Zero
5345  * MARK ID means the default flow. The default flow looks like,
5346  *   - For all flow, reg_b := reg_c[0] and jump to RX_ACT_TBL.
5347  *
5348  * @param dev
5349  *   Pointer to Ethernet device.
5350  * @param flow
5351  *   Pointer to flow structure.
5352  * @param[in] actions
5353  *   Pointer to the list of actions.
5354  * @param[out] error
5355  *   Perform verbose error reporting if not NULL.
5356  *
5357  * @return
5358  *   0 on success, negative value otherwise and rte_errno is set.
5359  */
5360 static int
5361 flow_mreg_update_copy_table(struct rte_eth_dev *dev,
5362 			    struct rte_flow *flow,
5363 			    const struct rte_flow_action *actions,
5364 			    struct rte_flow_error *error)
5365 {
5366 	struct mlx5_priv *priv = dev->data->dev_private;
5367 	struct mlx5_sh_config *config = &priv->sh->config;
5368 	struct mlx5_flow_mreg_copy_resource *mcp_res;
5369 	const struct rte_flow_action_mark *mark;
5370 
5371 	/* Check whether extensive metadata feature is engaged. */
5372 	if (!config->dv_flow_en ||
5373 	    config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY ||
5374 	    !mlx5_flow_ext_mreg_supported(dev) ||
5375 	    !priv->sh->dv_regc0_mask)
5376 		return 0;
5377 	/* Find MARK action. */
5378 	for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
5379 		switch (actions->type) {
5380 		case RTE_FLOW_ACTION_TYPE_FLAG:
5381 			mcp_res = flow_mreg_add_copy_action
5382 				(dev, MLX5_FLOW_MARK_DEFAULT, error);
5383 			if (!mcp_res)
5384 				return -rte_errno;
5385 			flow->rix_mreg_copy = mcp_res->idx;
5386 			return 0;
5387 		case RTE_FLOW_ACTION_TYPE_MARK:
5388 			mark = (const struct rte_flow_action_mark *)
5389 				actions->conf;
5390 			mcp_res =
5391 				flow_mreg_add_copy_action(dev, mark->id, error);
5392 			if (!mcp_res)
5393 				return -rte_errno;
5394 			flow->rix_mreg_copy = mcp_res->idx;
5395 			return 0;
5396 		default:
5397 			break;
5398 		}
5399 	}
5400 	return 0;
5401 }
5402 
5403 #define MLX5_MAX_SPLIT_ACTIONS 24
5404 #define MLX5_MAX_SPLIT_ITEMS 24
5405 
5406 /**
5407  * Split the hairpin flow.
5408  * Since HW can't support encap and push-vlan on Rx, we move these
5409  * actions to Tx.
5410  * If the count action is after the encap then we also
5411  * move the count action. in this case the count will also measure
5412  * the outer bytes.
5413  *
5414  * @param dev
5415  *   Pointer to Ethernet device.
5416  * @param[in] actions
5417  *   Associated actions (list terminated by the END action).
5418  * @param[out] actions_rx
5419  *   Rx flow actions.
5420  * @param[out] actions_tx
5421  *   Tx flow actions..
5422  * @param[out] pattern_tx
5423  *   The pattern items for the Tx flow.
5424  * @param[out] flow_id
5425  *   The flow ID connected to this flow.
5426  *
5427  * @return
5428  *   0 on success.
5429  */
5430 static int
5431 flow_hairpin_split(struct rte_eth_dev *dev,
5432 		   const struct rte_flow_action actions[],
5433 		   struct rte_flow_action actions_rx[],
5434 		   struct rte_flow_action actions_tx[],
5435 		   struct rte_flow_item pattern_tx[],
5436 		   uint32_t flow_id)
5437 {
5438 	const struct rte_flow_action_raw_encap *raw_encap;
5439 	const struct rte_flow_action_raw_decap *raw_decap;
5440 	struct mlx5_rte_flow_action_set_tag *set_tag;
5441 	struct rte_flow_action *tag_action;
5442 	struct mlx5_rte_flow_item_tag *tag_item;
5443 	struct rte_flow_item *item;
5444 	char *addr;
5445 	int push_vlan = 0;
5446 	int encap = 0;
5447 
5448 	for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
5449 		if (actions->type == RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN)
5450 			push_vlan = 1;
5451 		switch (actions->type) {
5452 		case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
5453 		case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
5454 		case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
5455 		case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
5456 			rte_memcpy(actions_tx, actions,
5457 			       sizeof(struct rte_flow_action));
5458 			actions_tx++;
5459 			break;
5460 		case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
5461 			if (push_vlan) {
5462 				rte_memcpy(actions_tx, actions,
5463 					   sizeof(struct rte_flow_action));
5464 				actions_tx++;
5465 			} else {
5466 				rte_memcpy(actions_rx, actions,
5467 					   sizeof(struct rte_flow_action));
5468 				actions_rx++;
5469 			}
5470 			break;
5471 		case RTE_FLOW_ACTION_TYPE_COUNT:
5472 		case RTE_FLOW_ACTION_TYPE_AGE:
5473 			if (encap) {
5474 				rte_memcpy(actions_tx, actions,
5475 					   sizeof(struct rte_flow_action));
5476 				actions_tx++;
5477 			} else {
5478 				rte_memcpy(actions_rx, actions,
5479 					   sizeof(struct rte_flow_action));
5480 				actions_rx++;
5481 			}
5482 			break;
5483 		case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
5484 			raw_encap = actions->conf;
5485 			if (raw_encap->size > MLX5_ENCAPSULATION_DECISION_SIZE) {
5486 				memcpy(actions_tx, actions,
5487 				       sizeof(struct rte_flow_action));
5488 				actions_tx++;
5489 				encap = 1;
5490 			} else {
5491 				rte_memcpy(actions_rx, actions,
5492 					   sizeof(struct rte_flow_action));
5493 				actions_rx++;
5494 			}
5495 			break;
5496 		case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
5497 			raw_decap = actions->conf;
5498 			if (raw_decap->size < MLX5_ENCAPSULATION_DECISION_SIZE) {
5499 				memcpy(actions_tx, actions,
5500 				       sizeof(struct rte_flow_action));
5501 				actions_tx++;
5502 			} else {
5503 				rte_memcpy(actions_rx, actions,
5504 					   sizeof(struct rte_flow_action));
5505 				actions_rx++;
5506 			}
5507 			break;
5508 		default:
5509 			rte_memcpy(actions_rx, actions,
5510 				   sizeof(struct rte_flow_action));
5511 			actions_rx++;
5512 			break;
5513 		}
5514 	}
5515 	/* Add set meta action and end action for the Rx flow. */
5516 	tag_action = actions_rx;
5517 	tag_action->type = (enum rte_flow_action_type)
5518 			   MLX5_RTE_FLOW_ACTION_TYPE_TAG;
5519 	actions_rx++;
5520 	rte_memcpy(actions_rx, actions, sizeof(struct rte_flow_action));
5521 	actions_rx++;
5522 	set_tag = (void *)actions_rx;
5523 	*set_tag = (struct mlx5_rte_flow_action_set_tag) {
5524 		.id = mlx5_flow_get_reg_id(dev, MLX5_HAIRPIN_RX, 0, NULL),
5525 		.data = flow_id,
5526 	};
5527 	MLX5_ASSERT(set_tag->id > REG_NON);
5528 	tag_action->conf = set_tag;
5529 	/* Create Tx item list. */
5530 	rte_memcpy(actions_tx, actions, sizeof(struct rte_flow_action));
5531 	addr = (void *)&pattern_tx[2];
5532 	item = pattern_tx;
5533 	item->type = (enum rte_flow_item_type)
5534 		     MLX5_RTE_FLOW_ITEM_TYPE_TAG;
5535 	tag_item = (void *)addr;
5536 	tag_item->data = flow_id;
5537 	tag_item->id = mlx5_flow_get_reg_id(dev, MLX5_HAIRPIN_TX, 0, NULL);
5538 	MLX5_ASSERT(set_tag->id > REG_NON);
5539 	item->spec = tag_item;
5540 	addr += sizeof(struct mlx5_rte_flow_item_tag);
5541 	tag_item = (void *)addr;
5542 	tag_item->data = UINT32_MAX;
5543 	tag_item->id = UINT16_MAX;
5544 	item->mask = tag_item;
5545 	item->last = NULL;
5546 	item++;
5547 	item->type = RTE_FLOW_ITEM_TYPE_END;
5548 	return 0;
5549 }
5550 
5551 /**
5552  * The last stage of splitting chain, just creates the subflow
5553  * without any modification.
5554  *
5555  * @param[in] dev
5556  *   Pointer to Ethernet device.
5557  * @param[in] flow
5558  *   Parent flow structure pointer.
5559  * @param[in, out] sub_flow
5560  *   Pointer to return the created subflow, may be NULL.
5561  * @param[in] attr
5562  *   Flow rule attributes.
5563  * @param[in] items
5564  *   Pattern specification (list terminated by the END pattern item).
5565  * @param[in] actions
5566  *   Associated actions (list terminated by the END action).
5567  * @param[in] flow_split_info
5568  *   Pointer to flow split info structure.
5569  * @param[out] error
5570  *   Perform verbose error reporting if not NULL.
5571  * @return
5572  *   0 on success, negative value otherwise
5573  */
5574 static int
5575 flow_create_split_inner(struct rte_eth_dev *dev,
5576 			struct rte_flow *flow,
5577 			struct mlx5_flow **sub_flow,
5578 			const struct rte_flow_attr *attr,
5579 			const struct rte_flow_item items[],
5580 			const struct rte_flow_action actions[],
5581 			struct mlx5_flow_split_info *flow_split_info,
5582 			struct rte_flow_error *error)
5583 {
5584 	struct mlx5_flow *dev_flow;
5585 	struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
5586 
5587 	dev_flow = flow_drv_prepare(dev, flow, attr, items, actions,
5588 				    flow_split_info->flow_idx, error);
5589 	if (!dev_flow)
5590 		return -rte_errno;
5591 	dev_flow->flow = flow;
5592 	dev_flow->external = flow_split_info->external;
5593 	dev_flow->skip_scale = flow_split_info->skip_scale;
5594 	/* Subflow object was created, we must include one in the list. */
5595 	SILIST_INSERT(&flow->dev_handles, dev_flow->handle_idx,
5596 		      dev_flow->handle, next);
5597 	/*
5598 	 * If dev_flow is as one of the suffix flow, some actions in suffix
5599 	 * flow may need some user defined item layer flags, and pass the
5600 	 * Metadata rxq mark flag to suffix flow as well.
5601 	 */
5602 	if (flow_split_info->prefix_layers)
5603 		dev_flow->handle->layers = flow_split_info->prefix_layers;
5604 	if (flow_split_info->prefix_mark) {
5605 		MLX5_ASSERT(wks);
5606 		wks->mark = 1;
5607 	}
5608 	if (sub_flow)
5609 		*sub_flow = dev_flow;
5610 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
5611 	dev_flow->dv.table_id = flow_split_info->table_id;
5612 #endif
5613 	return flow_drv_translate(dev, dev_flow, attr, items, actions, error);
5614 }
5615 
5616 /**
5617  * Get the sub policy of a meter.
5618  *
5619  * @param[in] dev
5620  *   Pointer to Ethernet device.
5621  * @param[in] flow
5622  *   Parent flow structure pointer.
5623  * @param wks
5624  *   Pointer to thread flow work space.
5625  * @param[in] attr
5626  *   Flow rule attributes.
5627  * @param[in] items
5628  *   Pattern specification (list terminated by the END pattern item).
5629  * @param[out] error
5630  *   Perform verbose error reporting if not NULL.
5631  *
5632  * @return
5633  *   Pointer to the meter sub policy, NULL otherwise and rte_errno is set.
5634  */
5635 static struct mlx5_flow_meter_sub_policy *
5636 get_meter_sub_policy(struct rte_eth_dev *dev,
5637 		     struct rte_flow *flow,
5638 		     struct mlx5_flow_workspace *wks,
5639 		     const struct rte_flow_attr *attr,
5640 		     const struct rte_flow_item items[],
5641 		     struct rte_flow_error *error)
5642 {
5643 	struct mlx5_flow_meter_policy *policy;
5644 	struct mlx5_flow_meter_policy *final_policy;
5645 	struct mlx5_flow_meter_sub_policy *sub_policy = NULL;
5646 
5647 	policy = wks->policy;
5648 	final_policy = policy->is_hierarchy ? wks->final_policy : policy;
5649 	if (final_policy->is_rss || final_policy->is_queue) {
5650 		struct mlx5_flow_rss_desc rss_desc_v[MLX5_MTR_RTE_COLORS];
5651 		struct mlx5_flow_rss_desc *rss_desc[MLX5_MTR_RTE_COLORS] = {0};
5652 		uint32_t i;
5653 
5654 		/*
5655 		 * This is a tmp dev_flow,
5656 		 * no need to register any matcher for it in translate.
5657 		 */
5658 		wks->skip_matcher_reg = 1;
5659 		for (i = 0; i < MLX5_MTR_RTE_COLORS; i++) {
5660 			struct mlx5_flow dev_flow = {0};
5661 			struct mlx5_flow_handle dev_handle = { {0} };
5662 			uint8_t fate = final_policy->act_cnt[i].fate_action;
5663 
5664 			if (fate == MLX5_FLOW_FATE_SHARED_RSS) {
5665 				const struct rte_flow_action_rss *rss_act =
5666 					final_policy->act_cnt[i].rss->conf;
5667 				struct rte_flow_action rss_actions[2] = {
5668 					[0] = {
5669 					.type = RTE_FLOW_ACTION_TYPE_RSS,
5670 					.conf = rss_act,
5671 					},
5672 					[1] = {
5673 					.type = RTE_FLOW_ACTION_TYPE_END,
5674 					.conf = NULL,
5675 					}
5676 				};
5677 
5678 				dev_flow.handle = &dev_handle;
5679 				dev_flow.ingress = attr->ingress;
5680 				dev_flow.flow = flow;
5681 				dev_flow.external = 0;
5682 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
5683 				dev_flow.dv.transfer = attr->transfer;
5684 #endif
5685 				/**
5686 				 * Translate RSS action to get rss hash fields.
5687 				 */
5688 				if (flow_drv_translate(dev, &dev_flow, attr,
5689 						items, rss_actions, error))
5690 					goto exit;
5691 				rss_desc_v[i] = wks->rss_desc;
5692 				rss_desc_v[i].symmetric_hash_function =
5693 						dev_flow.symmetric_hash_function;
5694 				rss_desc_v[i].key_len = MLX5_RSS_HASH_KEY_LEN;
5695 				rss_desc_v[i].hash_fields =
5696 						dev_flow.hash_fields;
5697 				rss_desc_v[i].queue_num =
5698 						rss_desc_v[i].hash_fields ?
5699 						rss_desc_v[i].queue_num : 1;
5700 				rss_desc_v[i].tunnel =
5701 						!!(dev_flow.handle->layers &
5702 						   MLX5_FLOW_LAYER_TUNNEL);
5703 				/* Use the RSS queues in the containers. */
5704 				rss_desc_v[i].queue =
5705 					(uint16_t *)(uintptr_t)rss_act->queue;
5706 				rss_desc[i] = &rss_desc_v[i];
5707 			} else if (fate == MLX5_FLOW_FATE_QUEUE) {
5708 				/* This is queue action. */
5709 				rss_desc_v[i] = wks->rss_desc;
5710 				rss_desc_v[i].key_len = 0;
5711 				rss_desc_v[i].hash_fields = 0;
5712 				rss_desc_v[i].queue =
5713 					&final_policy->act_cnt[i].queue;
5714 				rss_desc_v[i].queue_num = 1;
5715 				rss_desc[i] = &rss_desc_v[i];
5716 			} else {
5717 				rss_desc[i] = NULL;
5718 			}
5719 		}
5720 		sub_policy = flow_drv_meter_sub_policy_rss_prepare(dev,
5721 						flow, policy, rss_desc);
5722 	} else {
5723 		enum mlx5_meter_domain mtr_domain =
5724 			attr->transfer ? MLX5_MTR_DOMAIN_TRANSFER :
5725 				(attr->egress ? MLX5_MTR_DOMAIN_EGRESS :
5726 						MLX5_MTR_DOMAIN_INGRESS);
5727 		sub_policy = policy->sub_policys[mtr_domain][0];
5728 	}
5729 	if (!sub_policy)
5730 		rte_flow_error_set(error, EINVAL,
5731 				   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5732 				   "Failed to get meter sub-policy.");
5733 exit:
5734 	return sub_policy;
5735 }
5736 
5737 /**
5738  * Split the meter flow.
5739  *
5740  * As meter flow will split to three sub flow, other than meter
5741  * action, the other actions make sense to only meter accepts
5742  * the packet. If it need to be dropped, no other additional
5743  * actions should be take.
5744  *
5745  * One kind of special action which decapsulates the L3 tunnel
5746  * header will be in the prefix sub flow, as not to take the
5747  * L3 tunnel header into account.
5748  *
5749  * @param[in] dev
5750  *   Pointer to Ethernet device.
5751  * @param[in] flow
5752  *   Parent flow structure pointer.
5753  * @param wks
5754  *   Pointer to thread flow work space.
5755  * @param[in] attr
5756  *   Flow rule attributes.
5757  * @param[in] items
5758  *   Pattern specification (list terminated by the END pattern item).
5759  * @param[out] sfx_items
5760  *   Suffix flow match items (list terminated by the END pattern item).
5761  * @param[in] actions
5762  *   Associated actions (list terminated by the END action).
5763  * @param[out] actions_sfx
5764  *   Suffix flow actions.
5765  * @param[out] actions_pre
5766  *   Prefix flow actions.
5767  * @param[out] mtr_flow_id
5768  *   Pointer to meter flow id.
5769  * @param[out] error
5770  *   Perform verbose error reporting if not NULL.
5771  *
5772  * @return
5773  *   0 on success, a negative errno value otherwise and rte_errno is set.
5774  */
5775 static int
5776 flow_meter_split_prep(struct rte_eth_dev *dev,
5777 		      struct rte_flow *flow,
5778 		      struct mlx5_flow_workspace *wks,
5779 		      const struct rte_flow_attr *attr,
5780 		      const struct rte_flow_item items[],
5781 		      struct rte_flow_item sfx_items[],
5782 		      const struct rte_flow_action actions[],
5783 		      struct rte_flow_action actions_sfx[],
5784 		      struct rte_flow_action actions_pre[],
5785 		      uint32_t *mtr_flow_id,
5786 		      struct rte_flow_error *error)
5787 {
5788 	struct mlx5_priv *priv = dev->data->dev_private;
5789 	struct mlx5_flow_meter_info *fm = wks->fm;
5790 	struct rte_flow_action *tag_action = NULL;
5791 	struct rte_flow_item *tag_item;
5792 	struct mlx5_rte_flow_action_set_tag *set_tag;
5793 	const struct rte_flow_action_raw_encap *raw_encap;
5794 	const struct rte_flow_action_raw_decap *raw_decap;
5795 	struct mlx5_rte_flow_item_tag *tag_item_spec;
5796 	struct mlx5_rte_flow_item_tag *tag_item_mask;
5797 	uint32_t tag_id = 0;
5798 	bool vlan_actions;
5799 	struct rte_flow_item *orig_sfx_items = sfx_items;
5800 	const struct rte_flow_item *orig_items = items;
5801 	struct rte_flow_action *hw_mtr_action;
5802 	struct rte_flow_action *action_pre_head = NULL;
5803 	uint16_t flow_src_port = priv->representor_id;
5804 	bool mtr_first;
5805 	uint8_t mtr_id_offset = priv->mtr_reg_share ? MLX5_MTR_COLOR_BITS : 0;
5806 	uint8_t mtr_reg_bits = priv->mtr_reg_share ?
5807 				MLX5_MTR_IDLE_BITS_IN_COLOR_REG : MLX5_REG_BITS;
5808 	uint32_t flow_id = 0;
5809 	uint32_t flow_id_reversed = 0;
5810 	uint8_t flow_id_bits = 0;
5811 	bool after_meter = false;
5812 	int shift;
5813 
5814 	/* Prepare the suffix subflow items. */
5815 	tag_item = sfx_items++;
5816 	tag_item->type = (enum rte_flow_item_type)MLX5_RTE_FLOW_ITEM_TYPE_TAG;
5817 	for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
5818 		int item_type = items->type;
5819 
5820 		switch (item_type) {
5821 		case RTE_FLOW_ITEM_TYPE_PORT_ID:
5822 		case RTE_FLOW_ITEM_TYPE_REPRESENTED_PORT:
5823 		case RTE_FLOW_ITEM_TYPE_PORT_REPRESENTOR:
5824 			if (mlx5_flow_get_item_vport_id(dev, items, &flow_src_port, NULL, error))
5825 				return -rte_errno;
5826 			if (!fm->def_policy && wks->policy->hierarchy_match_port &&
5827 			    flow_src_port != priv->representor_id) {
5828 				if (flow_drv_mtr_hierarchy_rule_create(dev,
5829 								flow, fm,
5830 								flow_src_port,
5831 								items,
5832 								error))
5833 					return -rte_errno;
5834 			}
5835 			memcpy(sfx_items, items, sizeof(*sfx_items));
5836 			sfx_items++;
5837 			break;
5838 		case RTE_FLOW_ITEM_TYPE_VLAN:
5839 			/*
5840 			 * Copy VLAN items in case VLAN actions are performed.
5841 			 * If there are no VLAN actions, these items will be VOID.
5842 			 */
5843 			memcpy(sfx_items, items, sizeof(*sfx_items));
5844 			sfx_items->type = (enum rte_flow_item_type)MLX5_RTE_FLOW_ITEM_TYPE_VLAN;
5845 			sfx_items++;
5846 			break;
5847 		default:
5848 			break;
5849 		}
5850 	}
5851 	sfx_items->type = RTE_FLOW_ITEM_TYPE_END;
5852 	sfx_items++;
5853 	mtr_first = priv->sh->meter_aso_en &&
5854 		(attr->egress || (attr->transfer && flow_src_port != UINT16_MAX));
5855 	/* For ASO meter, meter must be before tag in TX direction. */
5856 	if (mtr_first) {
5857 		action_pre_head = actions_pre++;
5858 		/* Leave space for tag action. */
5859 		tag_action = actions_pre++;
5860 	}
5861 	/* Prepare the actions for prefix and suffix flow. */
5862 	vlan_actions = false;
5863 	for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
5864 		struct rte_flow_action *action_cur = NULL;
5865 
5866 		switch (actions->type) {
5867 		case RTE_FLOW_ACTION_TYPE_METER:
5868 			if (mtr_first) {
5869 				action_cur = action_pre_head;
5870 			} else {
5871 				/* Leave space for tag action. */
5872 				tag_action = actions_pre++;
5873 				action_cur = actions_pre++;
5874 			}
5875 			after_meter = true;
5876 			break;
5877 		case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
5878 		case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
5879 			action_cur = actions_pre++;
5880 			break;
5881 		case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
5882 			raw_encap = actions->conf;
5883 			if (raw_encap->size < MLX5_ENCAPSULATION_DECISION_SIZE)
5884 				action_cur = actions_pre++;
5885 			break;
5886 		case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
5887 			raw_decap = actions->conf;
5888 			if (raw_decap->size > MLX5_ENCAPSULATION_DECISION_SIZE)
5889 				action_cur = actions_pre++;
5890 			break;
5891 		case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
5892 		case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
5893 			vlan_actions = true;
5894 			break;
5895 		case RTE_FLOW_ACTION_TYPE_COUNT:
5896 			if (fm->def_policy)
5897 				action_cur = after_meter ?
5898 						actions_sfx++ : actions_pre++;
5899 			break;
5900 		default:
5901 			break;
5902 		}
5903 		if (!action_cur)
5904 			action_cur = (fm->def_policy) ?
5905 					actions_sfx++ : actions_pre++;
5906 		memcpy(action_cur, actions, sizeof(struct rte_flow_action));
5907 	}
5908 	/* If there are no VLAN actions, convert VLAN items to VOID in suffix flow items. */
5909 	if (!vlan_actions) {
5910 		struct rte_flow_item *it = orig_sfx_items;
5911 
5912 		for (; it->type != RTE_FLOW_ITEM_TYPE_END; it++)
5913 			if (it->type == (enum rte_flow_item_type)MLX5_RTE_FLOW_ITEM_TYPE_VLAN)
5914 				it->type = RTE_FLOW_ITEM_TYPE_VOID;
5915 	}
5916 	/* Add end action to the actions. */
5917 	actions_sfx->type = RTE_FLOW_ACTION_TYPE_END;
5918 	if (priv->sh->meter_aso_en) {
5919 		/**
5920 		 * For ASO meter, need to add an extra jump action explicitly,
5921 		 * to jump from meter to policer table.
5922 		 */
5923 		struct mlx5_flow_meter_sub_policy *sub_policy;
5924 		struct mlx5_flow_tbl_data_entry *tbl_data;
5925 
5926 		if (!fm->def_policy) {
5927 			sub_policy = get_meter_sub_policy(dev, flow, wks,
5928 							  attr, orig_items,
5929 							  error);
5930 			if (!sub_policy)
5931 				return -rte_errno;
5932 		} else {
5933 			enum mlx5_meter_domain mtr_domain =
5934 			attr->transfer ? MLX5_MTR_DOMAIN_TRANSFER :
5935 				(attr->egress ? MLX5_MTR_DOMAIN_EGRESS :
5936 						MLX5_MTR_DOMAIN_INGRESS);
5937 
5938 			sub_policy =
5939 			&priv->sh->mtrmng->def_policy[mtr_domain]->sub_policy;
5940 		}
5941 		tbl_data = container_of(sub_policy->tbl_rsc,
5942 					struct mlx5_flow_tbl_data_entry, tbl);
5943 		hw_mtr_action = actions_pre++;
5944 		hw_mtr_action->type = (enum rte_flow_action_type)
5945 				      MLX5_RTE_FLOW_ACTION_TYPE_JUMP;
5946 		hw_mtr_action->conf = tbl_data->jump.action;
5947 	}
5948 	actions_pre->type = RTE_FLOW_ACTION_TYPE_END;
5949 	actions_pre++;
5950 	if (!tag_action)
5951 		return rte_flow_error_set(error, ENOMEM,
5952 					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5953 					  NULL, "No tag action space.");
5954 	if (!mtr_flow_id) {
5955 		tag_action->type = RTE_FLOW_ACTION_TYPE_VOID;
5956 		goto exit;
5957 	}
5958 	/* Only default-policy Meter creates mtr flow id. */
5959 	if (fm->def_policy) {
5960 		mlx5_ipool_malloc(fm->flow_ipool, &tag_id);
5961 		if (!tag_id)
5962 			return rte_flow_error_set(error, ENOMEM,
5963 					RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5964 					"Failed to allocate meter flow id.");
5965 		flow_id = tag_id - 1;
5966 		flow_id_bits = (!flow_id) ? 1 :
5967 				(MLX5_REG_BITS - rte_clz32(flow_id));
5968 		if ((flow_id_bits + priv->sh->mtrmng->max_mtr_bits) >
5969 		    mtr_reg_bits) {
5970 			mlx5_ipool_free(fm->flow_ipool, tag_id);
5971 			return rte_flow_error_set(error, EINVAL,
5972 					RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5973 					"Meter flow id exceeds max limit.");
5974 		}
5975 		if (flow_id_bits > priv->sh->mtrmng->max_mtr_flow_bits)
5976 			priv->sh->mtrmng->max_mtr_flow_bits = flow_id_bits;
5977 	}
5978 	/* Build tag actions and items for meter_id/meter flow_id. */
5979 	set_tag = (struct mlx5_rte_flow_action_set_tag *)actions_pre;
5980 	tag_item_spec = (struct mlx5_rte_flow_item_tag *)sfx_items;
5981 	tag_item_mask = tag_item_spec + 1;
5982 	/* Both flow_id and meter_id share the same register. */
5983 	*set_tag = (struct mlx5_rte_flow_action_set_tag) {
5984 		.id = (enum modify_reg)mlx5_flow_get_reg_id(dev, MLX5_MTR_ID,
5985 							    0, error),
5986 		.offset = mtr_id_offset,
5987 		.length = mtr_reg_bits,
5988 		.data = flow->meter,
5989 	};
5990 	/*
5991 	 * The color Reg bits used by flow_id are growing from
5992 	 * msb to lsb, so must do bit reverse for flow_id val in RegC.
5993 	 */
5994 	for (shift = 0; shift < flow_id_bits; shift++)
5995 		flow_id_reversed = (flow_id_reversed << 1) |
5996 				((flow_id >> shift) & 0x1);
5997 	set_tag->data |=
5998 		flow_id_reversed << (mtr_reg_bits - flow_id_bits);
5999 	tag_item_spec->id = set_tag->id;
6000 	tag_item_spec->data = set_tag->data << mtr_id_offset;
6001 	tag_item_mask->data = UINT32_MAX << mtr_id_offset;
6002 	tag_action->type = (enum rte_flow_action_type)
6003 				MLX5_RTE_FLOW_ACTION_TYPE_TAG;
6004 	tag_action->conf = set_tag;
6005 	tag_item->spec = tag_item_spec;
6006 	tag_item->last = NULL;
6007 	tag_item->mask = tag_item_mask;
6008 exit:
6009 	if (mtr_flow_id)
6010 		*mtr_flow_id = tag_id;
6011 	return 0;
6012 }
6013 
6014 /**
6015  * Split action list having QUEUE/RSS for metadata register copy.
6016  *
6017  * Once Q/RSS action is detected in user's action list, the flow action
6018  * should be split in order to copy metadata registers, which will happen in
6019  * RX_CP_TBL like,
6020  *   - CQE->flow_tag := reg_c[1] (MARK)
6021  *   - CQE->flow_table_metadata (reg_b) := reg_c[0] (META)
6022  * The Q/RSS action will be performed on RX_ACT_TBL after passing by RX_CP_TBL.
6023  * This is because the last action of each flow must be a terminal action
6024  * (QUEUE, RSS or DROP).
6025  *
6026  * Flow ID must be allocated to identify actions in the RX_ACT_TBL and it is
6027  * stored and kept in the mlx5_flow structure per each sub_flow.
6028  *
6029  * The Q/RSS action is replaced with,
6030  *   - SET_TAG, setting the allocated flow ID to reg_c[2].
6031  * And the following JUMP action is added at the end,
6032  *   - JUMP, to RX_CP_TBL.
6033  *
6034  * A flow to perform remained Q/RSS action will be created in RX_ACT_TBL by
6035  * flow_create_split_metadata() routine. The flow will look like,
6036  *   - If flow ID matches (reg_c[2]), perform Q/RSS.
6037  *
6038  * @param dev
6039  *   Pointer to Ethernet device.
6040  * @param[out] split_actions
6041  *   Pointer to store split actions to jump to CP_TBL.
6042  * @param[in] actions
6043  *   Pointer to the list of original flow actions.
6044  * @param[in] qrss
6045  *   Pointer to the Q/RSS action.
6046  * @param[in] actions_n
6047  *   Number of original actions.
6048  * @param[in] mtr_sfx
6049  *   Check if it is in meter suffix table.
6050  * @param[out] error
6051  *   Perform verbose error reporting if not NULL.
6052  *
6053  * @return
6054  *   non-zero unique flow_id on success, otherwise 0 and
6055  *   error/rte_error are set.
6056  */
6057 static uint32_t
6058 flow_mreg_split_qrss_prep(struct rte_eth_dev *dev,
6059 			  struct rte_flow_action *split_actions,
6060 			  const struct rte_flow_action *actions,
6061 			  const struct rte_flow_action *qrss,
6062 			  int actions_n, int mtr_sfx,
6063 			  struct rte_flow_error *error)
6064 {
6065 	struct mlx5_priv *priv = dev->data->dev_private;
6066 	struct mlx5_rte_flow_action_set_tag *set_tag;
6067 	struct rte_flow_action_jump *jump;
6068 	const int qrss_idx = qrss - actions;
6069 	uint32_t flow_id = 0;
6070 	int ret = 0;
6071 
6072 	/*
6073 	 * Given actions will be split
6074 	 * - Replace QUEUE/RSS action with SET_TAG to set flow ID.
6075 	 * - Add jump to mreg CP_TBL.
6076 	 * As a result, there will be one more action.
6077 	 */
6078 	memcpy(split_actions, actions, sizeof(*split_actions) * actions_n);
6079 	/* Count MLX5_RTE_FLOW_ACTION_TYPE_TAG. */
6080 	++actions_n;
6081 	set_tag = (void *)(split_actions + actions_n);
6082 	/*
6083 	 * If we are not the meter suffix flow, add the tag action.
6084 	 * Since meter suffix flow already has the tag added.
6085 	 */
6086 	if (!mtr_sfx) {
6087 		/*
6088 		 * Allocate the new subflow ID. This one is unique within
6089 		 * device and not shared with representors. Otherwise,
6090 		 * we would have to resolve multi-thread access synch
6091 		 * issue. Each flow on the shared device is appended
6092 		 * with source vport identifier, so the resulting
6093 		 * flows will be unique in the shared (by master and
6094 		 * representors) domain even if they have coinciding
6095 		 * IDs.
6096 		 */
6097 		mlx5_ipool_malloc(priv->sh->ipool
6098 				  [MLX5_IPOOL_RSS_EXPANTION_FLOW_ID], &flow_id);
6099 		if (!flow_id)
6100 			return rte_flow_error_set(error, ENOMEM,
6101 						  RTE_FLOW_ERROR_TYPE_ACTION,
6102 						  NULL, "can't allocate id "
6103 						  "for split Q/RSS subflow");
6104 		/* Internal SET_TAG action to set flow ID. */
6105 		*set_tag = (struct mlx5_rte_flow_action_set_tag){
6106 			.data = flow_id,
6107 		};
6108 		ret = mlx5_flow_get_reg_id(dev, MLX5_COPY_MARK, 0, error);
6109 		if (ret < 0)
6110 			return ret;
6111 		set_tag->id = ret;
6112 		/* Construct new actions array. */
6113 		/* Replace QUEUE/RSS action. */
6114 		split_actions[qrss_idx] = (struct rte_flow_action){
6115 			.type = (enum rte_flow_action_type)
6116 				MLX5_RTE_FLOW_ACTION_TYPE_TAG,
6117 			.conf = set_tag,
6118 		};
6119 	} else {
6120 		/*
6121 		 * If we are the suffix flow of meter, tag already exist.
6122 		 * Set the QUEUE/RSS action to void.
6123 		 */
6124 		split_actions[qrss_idx].type = RTE_FLOW_ACTION_TYPE_VOID;
6125 	}
6126 	/* JUMP action to jump to mreg copy table (CP_TBL). */
6127 	jump = (void *)(set_tag + 1);
6128 	*jump = (struct rte_flow_action_jump){
6129 		.group = MLX5_FLOW_MREG_CP_TABLE_GROUP,
6130 	};
6131 	split_actions[actions_n - 2] = (struct rte_flow_action){
6132 		.type = RTE_FLOW_ACTION_TYPE_JUMP,
6133 		.conf = jump,
6134 	};
6135 	split_actions[actions_n - 1] = (struct rte_flow_action){
6136 		.type = RTE_FLOW_ACTION_TYPE_END,
6137 	};
6138 	return flow_id;
6139 }
6140 
6141 /**
6142  * Extend the given action list for Tx metadata copy.
6143  *
6144  * Copy the given action list to the ext_actions and add flow metadata register
6145  * copy action in order to copy reg_a set by WQE to reg_c[0].
6146  *
6147  * @param[out] ext_actions
6148  *   Pointer to the extended action list.
6149  * @param[in] actions
6150  *   Pointer to the list of actions.
6151  * @param[in] actions_n
6152  *   Number of actions in the list.
6153  * @param[out] error
6154  *   Perform verbose error reporting if not NULL.
6155  * @param[in] encap_idx
6156  *   The encap action index.
6157  *
6158  * @return
6159  *   0 on success, negative value otherwise
6160  */
6161 static int
6162 flow_mreg_tx_copy_prep(struct rte_eth_dev *dev,
6163 		       struct rte_flow_action *ext_actions,
6164 		       const struct rte_flow_action *actions,
6165 		       int actions_n, struct rte_flow_error *error,
6166 		       int encap_idx)
6167 {
6168 	struct mlx5_flow_action_copy_mreg *cp_mreg =
6169 		(struct mlx5_flow_action_copy_mreg *)
6170 			(ext_actions + actions_n + 1);
6171 	int ret;
6172 
6173 	ret = mlx5_flow_get_reg_id(dev, MLX5_METADATA_RX, 0, error);
6174 	if (ret < 0)
6175 		return ret;
6176 	cp_mreg->dst = ret;
6177 	ret = mlx5_flow_get_reg_id(dev, MLX5_METADATA_TX, 0, error);
6178 	if (ret < 0)
6179 		return ret;
6180 	cp_mreg->src = ret;
6181 	if (encap_idx != 0)
6182 		memcpy(ext_actions, actions, sizeof(*ext_actions) * encap_idx);
6183 	if (encap_idx == actions_n - 1) {
6184 		ext_actions[actions_n - 1] = (struct rte_flow_action){
6185 			.type = (enum rte_flow_action_type)
6186 				MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG,
6187 			.conf = cp_mreg,
6188 		};
6189 		ext_actions[actions_n] = (struct rte_flow_action){
6190 			.type = RTE_FLOW_ACTION_TYPE_END,
6191 		};
6192 	} else {
6193 		ext_actions[encap_idx] = (struct rte_flow_action){
6194 			.type = (enum rte_flow_action_type)
6195 				MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG,
6196 			.conf = cp_mreg,
6197 		};
6198 		memcpy(ext_actions + encap_idx + 1, actions + encap_idx,
6199 				sizeof(*ext_actions) * (actions_n - encap_idx));
6200 	}
6201 	return 0;
6202 }
6203 
6204 /**
6205  * Check the match action from the action list.
6206  *
6207  * @param[in] actions
6208  *   Pointer to the list of actions.
6209  * @param[in] attr
6210  *   Flow rule attributes.
6211  * @param[in] action
6212  *   The action to be check if exist.
6213  * @param[out] match_action_pos
6214  *   Pointer to the position of the matched action if exists, otherwise is -1.
6215  * @param[out] qrss_action_pos
6216  *   Pointer to the position of the Queue/RSS action if exists, otherwise is -1.
6217  * @param[out] modify_after_mirror
6218  *   Pointer to the flag of modify action after FDB mirroring.
6219  *
6220  * @return
6221  *   > 0 the total number of actions.
6222  *   0 if not found match action in action list.
6223  */
6224 static int
6225 flow_check_match_action(const struct rte_flow_action actions[],
6226 			const struct rte_flow_attr *attr,
6227 			enum rte_flow_action_type action,
6228 			int *match_action_pos, int *qrss_action_pos,
6229 			int *modify_after_mirror)
6230 {
6231 	const struct rte_flow_action_sample *sample;
6232 	const struct rte_flow_action_raw_decap *decap;
6233 	const struct rte_flow_action *action_cur = NULL;
6234 	int actions_n = 0;
6235 	uint32_t ratio = 0;
6236 	int sub_type = 0;
6237 	int flag = 0;
6238 	int fdb_mirror = 0;
6239 
6240 	*match_action_pos = -1;
6241 	*qrss_action_pos = -1;
6242 	for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
6243 		if (actions->type == action) {
6244 			flag = 1;
6245 			*match_action_pos = actions_n;
6246 		}
6247 		switch (actions->type) {
6248 		case RTE_FLOW_ACTION_TYPE_QUEUE:
6249 		case RTE_FLOW_ACTION_TYPE_RSS:
6250 			*qrss_action_pos = actions_n;
6251 			break;
6252 		case RTE_FLOW_ACTION_TYPE_SAMPLE:
6253 			sample = actions->conf;
6254 			ratio = sample->ratio;
6255 			sub_type = ((const struct rte_flow_action *)
6256 					(sample->actions))->type;
6257 			if (ratio == 1 && attr->transfer &&
6258 			    sub_type != RTE_FLOW_ACTION_TYPE_END)
6259 				fdb_mirror = 1;
6260 			break;
6261 		case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
6262 		case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
6263 		case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
6264 		case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
6265 		case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
6266 		case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
6267 		case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
6268 		case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
6269 		case RTE_FLOW_ACTION_TYPE_DEC_TTL:
6270 		case RTE_FLOW_ACTION_TYPE_SET_TTL:
6271 		case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
6272 		case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
6273 		case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
6274 		case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
6275 		case RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP:
6276 		case RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP:
6277 		case RTE_FLOW_ACTION_TYPE_FLAG:
6278 		case RTE_FLOW_ACTION_TYPE_MARK:
6279 		case RTE_FLOW_ACTION_TYPE_SET_META:
6280 		case RTE_FLOW_ACTION_TYPE_SET_TAG:
6281 		case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
6282 		case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
6283 		case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
6284 		case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
6285 		case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
6286 		case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
6287 		case RTE_FLOW_ACTION_TYPE_MODIFY_FIELD:
6288 		case RTE_FLOW_ACTION_TYPE_METER:
6289 			if (fdb_mirror)
6290 				*modify_after_mirror = 1;
6291 			break;
6292 		case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
6293 			decap = actions->conf;
6294 			action_cur = actions;
6295 			while ((++action_cur)->type == RTE_FLOW_ACTION_TYPE_VOID)
6296 				;
6297 			if (action_cur->type == RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
6298 				const struct rte_flow_action_raw_encap *encap =
6299 								action_cur->conf;
6300 				if (decap->size <=
6301 					MLX5_ENCAPSULATION_DECISION_SIZE &&
6302 				    encap->size >
6303 					MLX5_ENCAPSULATION_DECISION_SIZE)
6304 					/* L3 encap. */
6305 					break;
6306 			}
6307 			if (fdb_mirror)
6308 				*modify_after_mirror = 1;
6309 			break;
6310 		default:
6311 			break;
6312 		}
6313 		actions_n++;
6314 	}
6315 	if (flag && fdb_mirror && !*modify_after_mirror) {
6316 		/* FDB mirroring uses the destination array to implement
6317 		 * instead of FLOW_SAMPLER object.
6318 		 */
6319 		if (sub_type != RTE_FLOW_ACTION_TYPE_END)
6320 			flag = 0;
6321 	}
6322 	/* Count RTE_FLOW_ACTION_TYPE_END. */
6323 	return flag ? actions_n + 1 : 0;
6324 }
6325 
6326 #define SAMPLE_SUFFIX_ITEM 3
6327 
6328 /**
6329  * Split the sample flow.
6330  *
6331  * As sample flow will split to two sub flow, sample flow with
6332  * sample action, the other actions will move to new suffix flow.
6333  *
6334  * Also add unique tag id with tag action in the sample flow,
6335  * the same tag id will be as match in the suffix flow.
6336  *
6337  * @param dev
6338  *   Pointer to Ethernet device.
6339  * @param[in] add_tag
6340  *   Add extra tag action flag.
6341  * @param[out] sfx_items
6342  *   Suffix flow match items (list terminated by the END pattern item).
6343  * @param[in] actions
6344  *   Associated actions (list terminated by the END action).
6345  * @param[out] actions_sfx
6346  *   Suffix flow actions.
6347  * @param[out] actions_pre
6348  *   Prefix flow actions.
6349  * @param[in] actions_n
6350  *  The total number of actions.
6351  * @param[in] sample_action_pos
6352  *   The sample action position.
6353  * @param[in] qrss_action_pos
6354  *   The Queue/RSS action position.
6355  * @param[in] jump_table
6356  *   Add extra jump action flag.
6357  * @param[out] error
6358  *   Perform verbose error reporting if not NULL.
6359  *
6360  * @return
6361  *   0 on success, or unique flow_id, a negative errno value
6362  *   otherwise and rte_errno is set.
6363  */
6364 static int
6365 flow_sample_split_prep(struct rte_eth_dev *dev,
6366 		       int add_tag,
6367 		       const struct rte_flow_item items[],
6368 		       struct rte_flow_item sfx_items[],
6369 		       const struct rte_flow_action actions[],
6370 		       struct rte_flow_action actions_sfx[],
6371 		       struct rte_flow_action actions_pre[],
6372 		       int actions_n,
6373 		       int sample_action_pos,
6374 		       int qrss_action_pos,
6375 		       int jump_table,
6376 		       struct rte_flow_error *error)
6377 {
6378 	struct mlx5_priv *priv = dev->data->dev_private;
6379 	struct mlx5_rte_flow_action_set_tag *set_tag;
6380 	struct mlx5_rte_flow_item_tag *tag_spec;
6381 	struct mlx5_rte_flow_item_tag *tag_mask;
6382 	struct rte_flow_action_jump *jump_action;
6383 	uint32_t tag_id = 0;
6384 	int append_index = 0;
6385 	int set_tag_idx = -1;
6386 	int index;
6387 	int ret;
6388 
6389 	if (sample_action_pos < 0)
6390 		return rte_flow_error_set(error, EINVAL,
6391 					  RTE_FLOW_ERROR_TYPE_ACTION,
6392 					  NULL, "invalid position of sample "
6393 					  "action in list");
6394 	/* Prepare the actions for prefix and suffix flow. */
6395 	if (add_tag) {
6396 		/* Update the new added tag action index preceding
6397 		 * the PUSH_VLAN or ENCAP action.
6398 		 */
6399 		const struct rte_flow_action_raw_encap *raw_encap;
6400 		const struct rte_flow_action *action = actions;
6401 		int encap_idx;
6402 		int action_idx = 0;
6403 		int raw_decap_idx = -1;
6404 		int push_vlan_idx = -1;
6405 		for (; action->type != RTE_FLOW_ACTION_TYPE_END; action++) {
6406 			switch (action->type) {
6407 			case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
6408 				raw_decap_idx = action_idx;
6409 				break;
6410 			case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
6411 				raw_encap = action->conf;
6412 				if (raw_encap->size >
6413 					MLX5_ENCAPSULATION_DECISION_SIZE) {
6414 					encap_idx = raw_decap_idx != -1 ?
6415 						    raw_decap_idx : action_idx;
6416 					if (encap_idx < sample_action_pos &&
6417 					    push_vlan_idx == -1)
6418 						set_tag_idx = encap_idx;
6419 				}
6420 				break;
6421 			case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
6422 			case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
6423 				encap_idx = action_idx;
6424 				if (encap_idx < sample_action_pos &&
6425 				    push_vlan_idx == -1)
6426 					set_tag_idx = encap_idx;
6427 				break;
6428 			case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
6429 			case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
6430 				if (action_idx < sample_action_pos &&
6431 				    push_vlan_idx == -1) {
6432 					set_tag_idx = action_idx;
6433 					push_vlan_idx = action_idx;
6434 				}
6435 				break;
6436 			default:
6437 				break;
6438 			}
6439 			action_idx++;
6440 		}
6441 	}
6442 	/* Prepare the actions for prefix and suffix flow. */
6443 	if (qrss_action_pos >= 0 && qrss_action_pos < sample_action_pos) {
6444 		index = qrss_action_pos;
6445 		/* Put the preceding the Queue/RSS action into prefix flow. */
6446 		if (index != 0)
6447 			memcpy(actions_pre, actions,
6448 			       sizeof(struct rte_flow_action) * index);
6449 		/* Put others preceding the sample action into prefix flow. */
6450 		if (sample_action_pos > index + 1)
6451 			memcpy(actions_pre + index, actions + index + 1,
6452 			       sizeof(struct rte_flow_action) *
6453 			       (sample_action_pos - index - 1));
6454 		index = sample_action_pos - 1;
6455 		/* Put Queue/RSS action into Suffix flow. */
6456 		memcpy(actions_sfx, actions + qrss_action_pos,
6457 		       sizeof(struct rte_flow_action));
6458 		actions_sfx++;
6459 	} else if (add_tag && set_tag_idx >= 0) {
6460 		if (set_tag_idx > 0)
6461 			memcpy(actions_pre, actions,
6462 			       sizeof(struct rte_flow_action) * set_tag_idx);
6463 		memcpy(actions_pre + set_tag_idx + 1, actions + set_tag_idx,
6464 		       sizeof(struct rte_flow_action) *
6465 		       (sample_action_pos - set_tag_idx));
6466 		index = sample_action_pos;
6467 	} else {
6468 		index = sample_action_pos;
6469 		if (index != 0)
6470 			memcpy(actions_pre, actions,
6471 			       sizeof(struct rte_flow_action) * index);
6472 	}
6473 	/* For CX5, add an extra tag action for NIC-RX and E-Switch ingress.
6474 	 * For CX6DX and above, metadata registers Cx preserve their value,
6475 	 * add an extra tag action for NIC-RX and E-Switch Domain.
6476 	 */
6477 	if (add_tag) {
6478 		/* Prepare the prefix tag action. */
6479 		append_index++;
6480 		set_tag = (void *)(actions_pre + actions_n + append_index);
6481 		/* Trust VF/SF on CX5 not supported meter so that the reserved
6482 		 * metadata regC is REG_NON, back to use application tag
6483 		 * index 0.
6484 		 */
6485 		if (unlikely(priv->sh->registers.aso_reg == REG_NON))
6486 			ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, 0, error);
6487 		else
6488 			ret = mlx5_flow_get_reg_id(dev, MLX5_SAMPLE_ID, 0, error);
6489 		if (ret < 0)
6490 			return ret;
6491 		mlx5_ipool_malloc(priv->sh->ipool
6492 				  [MLX5_IPOOL_RSS_EXPANTION_FLOW_ID], &tag_id);
6493 		*set_tag = (struct mlx5_rte_flow_action_set_tag) {
6494 			.id = ret,
6495 			.data = tag_id,
6496 		};
6497 		/* Prepare the suffix subflow items. */
6498 		tag_spec = (void *)(sfx_items + SAMPLE_SUFFIX_ITEM);
6499 		tag_spec->data = tag_id;
6500 		tag_spec->id = set_tag->id;
6501 		tag_mask = tag_spec + 1;
6502 		tag_mask->data = UINT32_MAX;
6503 		for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
6504 			if (items->type == RTE_FLOW_ITEM_TYPE_PORT_ID ||
6505 			    items->type == RTE_FLOW_ITEM_TYPE_PORT_REPRESENTOR ||
6506 			    items->type == RTE_FLOW_ITEM_TYPE_REPRESENTED_PORT) {
6507 				memcpy(sfx_items, items, sizeof(*sfx_items));
6508 				sfx_items++;
6509 				break;
6510 			}
6511 		}
6512 		sfx_items[0] = (struct rte_flow_item){
6513 			.type = (enum rte_flow_item_type)
6514 				MLX5_RTE_FLOW_ITEM_TYPE_TAG,
6515 			.spec = tag_spec,
6516 			.last = NULL,
6517 			.mask = tag_mask,
6518 		};
6519 		sfx_items[1] = (struct rte_flow_item){
6520 			.type = (enum rte_flow_item_type)
6521 				RTE_FLOW_ITEM_TYPE_END,
6522 		};
6523 		/* Prepare the tag action in prefix subflow. */
6524 		set_tag_idx = (set_tag_idx == -1) ? index : set_tag_idx;
6525 		actions_pre[set_tag_idx] =
6526 			(struct rte_flow_action){
6527 			.type = (enum rte_flow_action_type)
6528 				MLX5_RTE_FLOW_ACTION_TYPE_TAG,
6529 			.conf = set_tag,
6530 		};
6531 		/* Update next sample position due to add one tag action */
6532 		index += 1;
6533 	}
6534 	/* Copy the sample action into prefix flow. */
6535 	memcpy(actions_pre + index, actions + sample_action_pos,
6536 	       sizeof(struct rte_flow_action));
6537 	index += 1;
6538 	/* For the modify action after the sample action in E-Switch mirroring,
6539 	 * Add the extra jump action in prefix subflow and jump into the next
6540 	 * table, then do the modify action in the new table.
6541 	 */
6542 	if (jump_table) {
6543 		/* Prepare the prefix jump action. */
6544 		append_index++;
6545 		jump_action = (void *)(actions_pre + actions_n + append_index);
6546 		jump_action->group = jump_table;
6547 		actions_pre[index++] =
6548 			(struct rte_flow_action){
6549 			.type = (enum rte_flow_action_type)
6550 				RTE_FLOW_ACTION_TYPE_JUMP,
6551 			.conf = jump_action,
6552 		};
6553 	}
6554 	actions_pre[index] = (struct rte_flow_action){
6555 		.type = (enum rte_flow_action_type)
6556 			RTE_FLOW_ACTION_TYPE_END,
6557 	};
6558 	/* Put the actions after sample into Suffix flow. */
6559 	memcpy(actions_sfx, actions + sample_action_pos + 1,
6560 	       sizeof(struct rte_flow_action) *
6561 	       (actions_n - sample_action_pos - 1));
6562 	return tag_id;
6563 }
6564 
6565 /**
6566  * The splitting for metadata feature.
6567  *
6568  * - Q/RSS action on NIC Rx should be split in order to pass by
6569  *   the mreg copy table (RX_CP_TBL) and then it jumps to the
6570  *   action table (RX_ACT_TBL) which has the split Q/RSS action.
6571  *
6572  * - All the actions on NIC Tx should have a mreg copy action to
6573  *   copy reg_a from WQE to reg_c[0].
6574  *
6575  * @param dev
6576  *   Pointer to Ethernet device.
6577  * @param[in] flow
6578  *   Parent flow structure pointer.
6579  * @param[in] attr
6580  *   Flow rule attributes.
6581  * @param[in] items
6582  *   Pattern specification (list terminated by the END pattern item).
6583  * @param[in] actions
6584  *   Associated actions (list terminated by the END action).
6585  * @param[in] flow_split_info
6586  *   Pointer to flow split info structure.
6587  * @param[out] error
6588  *   Perform verbose error reporting if not NULL.
6589  * @return
6590  *   0 on success, negative value otherwise
6591  */
6592 static int
6593 flow_create_split_metadata(struct rte_eth_dev *dev,
6594 			   struct rte_flow *flow,
6595 			   const struct rte_flow_attr *attr,
6596 			   const struct rte_flow_item items[],
6597 			   const struct rte_flow_action actions[],
6598 			   struct mlx5_flow_split_info *flow_split_info,
6599 			   struct rte_flow_error *error)
6600 {
6601 	struct mlx5_priv *priv = dev->data->dev_private;
6602 	struct mlx5_sh_config *config = &priv->sh->config;
6603 	const struct rte_flow_action *qrss = NULL;
6604 	struct rte_flow_action *ext_actions = NULL;
6605 	struct mlx5_flow *dev_flow = NULL;
6606 	uint32_t qrss_id = 0;
6607 	int mtr_sfx = 0;
6608 	size_t act_size;
6609 	int actions_n;
6610 	int encap_idx;
6611 	int ret;
6612 
6613 	/* Check whether extensive metadata feature is engaged. */
6614 	if (!config->dv_flow_en ||
6615 	    config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY ||
6616 	    !mlx5_flow_ext_mreg_supported(dev))
6617 		return flow_create_split_inner(dev, flow, NULL, attr, items,
6618 					       actions, flow_split_info, error);
6619 	actions_n = flow_parse_metadata_split_actions_info(actions, &qrss,
6620 							   &encap_idx);
6621 	if (qrss) {
6622 		/* Exclude hairpin flows from splitting. */
6623 		if (qrss->type == RTE_FLOW_ACTION_TYPE_QUEUE) {
6624 			const struct rte_flow_action_queue *queue;
6625 
6626 			queue = qrss->conf;
6627 			if (mlx5_rxq_is_hairpin(dev, queue->index))
6628 				qrss = NULL;
6629 		} else if (qrss->type == RTE_FLOW_ACTION_TYPE_RSS) {
6630 			const struct rte_flow_action_rss *rss;
6631 
6632 			rss = qrss->conf;
6633 			if (mlx5_rxq_is_hairpin(dev, rss->queue[0]))
6634 				qrss = NULL;
6635 		}
6636 	}
6637 	if (qrss) {
6638 		/* Check if it is in meter suffix table. */
6639 		mtr_sfx = attr->group ==
6640 			  ((attr->transfer && priv->fdb_def_rule) ?
6641 			  (MLX5_FLOW_TABLE_LEVEL_METER - 1) :
6642 			  MLX5_FLOW_TABLE_LEVEL_METER);
6643 		/*
6644 		 * Q/RSS action on NIC Rx should be split in order to pass by
6645 		 * the mreg copy table (RX_CP_TBL) and then it jumps to the
6646 		 * action table (RX_ACT_TBL) which has the split Q/RSS action.
6647 		 */
6648 		act_size = sizeof(struct rte_flow_action) * (actions_n + 1) +
6649 			   sizeof(struct rte_flow_action_set_tag) +
6650 			   sizeof(struct rte_flow_action_jump);
6651 		ext_actions = mlx5_malloc(MLX5_MEM_ZERO, act_size, 0,
6652 					  SOCKET_ID_ANY);
6653 		if (!ext_actions)
6654 			return rte_flow_error_set(error, ENOMEM,
6655 						  RTE_FLOW_ERROR_TYPE_ACTION,
6656 						  NULL, "no memory to split "
6657 						  "metadata flow");
6658 		/*
6659 		 * Create the new actions list with removed Q/RSS action
6660 		 * and appended set tag and jump to register copy table
6661 		 * (RX_CP_TBL). We should preallocate unique tag ID here
6662 		 * in advance, because it is needed for set tag action.
6663 		 */
6664 		qrss_id = flow_mreg_split_qrss_prep(dev, ext_actions, actions,
6665 						    qrss, actions_n,
6666 						    mtr_sfx, error);
6667 		if (!mtr_sfx && !qrss_id) {
6668 			ret = -rte_errno;
6669 			goto exit;
6670 		}
6671 	} else if (attr->egress) {
6672 		/*
6673 		 * All the actions on NIC Tx should have a metadata register
6674 		 * copy action to copy reg_a from WQE to reg_c[meta]
6675 		 */
6676 		act_size = sizeof(struct rte_flow_action) * (actions_n + 1) +
6677 			   sizeof(struct mlx5_flow_action_copy_mreg);
6678 		ext_actions = mlx5_malloc(MLX5_MEM_ZERO, act_size, 0,
6679 					  SOCKET_ID_ANY);
6680 		if (!ext_actions)
6681 			return rte_flow_error_set(error, ENOMEM,
6682 						  RTE_FLOW_ERROR_TYPE_ACTION,
6683 						  NULL, "no memory to split "
6684 						  "metadata flow");
6685 		/* Create the action list appended with copy register. */
6686 		ret = flow_mreg_tx_copy_prep(dev, ext_actions, actions,
6687 					     actions_n, error, encap_idx);
6688 		if (ret < 0)
6689 			goto exit;
6690 	}
6691 	/* Add the unmodified original or prefix subflow. */
6692 	ret = flow_create_split_inner(dev, flow, &dev_flow, attr,
6693 				      items, ext_actions ? ext_actions :
6694 				      actions, flow_split_info, error);
6695 	if (ret < 0)
6696 		goto exit;
6697 	MLX5_ASSERT(dev_flow);
6698 	if (qrss) {
6699 		const struct rte_flow_attr q_attr = {
6700 			.group = MLX5_FLOW_MREG_ACT_TABLE_GROUP,
6701 			.ingress = 1,
6702 		};
6703 		/* Internal PMD action to set register. */
6704 		struct mlx5_rte_flow_item_tag q_tag_spec = {
6705 			.data = qrss_id,
6706 			.id = REG_NON,
6707 		};
6708 		struct rte_flow_item q_items[] = {
6709 			{
6710 				.type = (enum rte_flow_item_type)
6711 					MLX5_RTE_FLOW_ITEM_TYPE_TAG,
6712 				.spec = &q_tag_spec,
6713 				.last = NULL,
6714 				.mask = NULL,
6715 			},
6716 			{
6717 				.type = RTE_FLOW_ITEM_TYPE_END,
6718 			},
6719 		};
6720 		struct rte_flow_action q_actions[] = {
6721 			{
6722 				.type = qrss->type,
6723 				.conf = qrss->conf,
6724 			},
6725 			{
6726 				.type = RTE_FLOW_ACTION_TYPE_END,
6727 			},
6728 		};
6729 		uint64_t layers = flow_get_prefix_layer_flags(dev_flow);
6730 
6731 		/*
6732 		 * Configure the tag item only if there is no meter subflow.
6733 		 * Since tag is already marked in the meter suffix subflow
6734 		 * we can just use the meter suffix items as is.
6735 		 */
6736 		if (qrss_id) {
6737 			/* Not meter subflow. */
6738 			MLX5_ASSERT(!mtr_sfx);
6739 			/*
6740 			 * Put unique id in prefix flow due to it is destroyed
6741 			 * after suffix flow and id will be freed after there
6742 			 * is no actual flows with this id and identifier
6743 			 * reallocation becomes possible (for example, for
6744 			 * other flows in other threads).
6745 			 */
6746 			dev_flow->handle->split_flow_id = qrss_id;
6747 			ret = mlx5_flow_get_reg_id(dev, MLX5_COPY_MARK, 0,
6748 						   error);
6749 			if (ret < 0)
6750 				goto exit;
6751 			q_tag_spec.id = ret;
6752 		}
6753 		dev_flow = NULL;
6754 		/* Add suffix subflow to execute Q/RSS. */
6755 		flow_split_info->prefix_layers = layers;
6756 		flow_split_info->prefix_mark = 0;
6757 		flow_split_info->table_id = 0;
6758 		ret = flow_create_split_inner(dev, flow, &dev_flow,
6759 					      &q_attr, mtr_sfx ? items :
6760 					      q_items, q_actions,
6761 					      flow_split_info, error);
6762 		if (ret < 0)
6763 			goto exit;
6764 		/* qrss ID should be freed if failed. */
6765 		qrss_id = 0;
6766 		MLX5_ASSERT(dev_flow);
6767 	}
6768 
6769 exit:
6770 	/*
6771 	 * We do not destroy the partially created sub_flows in case of error.
6772 	 * These ones are included into parent flow list and will be destroyed
6773 	 * by flow_drv_destroy.
6774 	 */
6775 	mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_RSS_EXPANTION_FLOW_ID],
6776 			qrss_id);
6777 	mlx5_free(ext_actions);
6778 	return ret;
6779 }
6780 
6781 /**
6782  * Create meter internal drop flow with the original pattern.
6783  *
6784  * @param dev
6785  *   Pointer to Ethernet device.
6786  * @param[in] flow
6787  *   Parent flow structure pointer.
6788  * @param[in] attr
6789  *   Flow rule attributes.
6790  * @param[in] items
6791  *   Pattern specification (list terminated by the END pattern item).
6792  * @param[in] flow_split_info
6793  *   Pointer to flow split info structure.
6794  * @param[in] fm
6795  *   Pointer to flow meter structure.
6796  * @param[out] error
6797  *   Perform verbose error reporting if not NULL.
6798  * @return
6799  *   0 on success, negative value otherwise
6800  */
6801 static uint32_t
6802 flow_meter_create_drop_flow_with_org_pattern(struct rte_eth_dev *dev,
6803 			struct rte_flow *flow,
6804 			const struct rte_flow_attr *attr,
6805 			const struct rte_flow_item items[],
6806 			struct mlx5_flow_split_info *flow_split_info,
6807 			struct mlx5_flow_meter_info *fm,
6808 			struct rte_flow_error *error)
6809 {
6810 	struct mlx5_flow *dev_flow = NULL;
6811 	struct rte_flow_attr drop_attr = *attr;
6812 	struct rte_flow_action drop_actions[3];
6813 	struct mlx5_flow_split_info drop_split_info = *flow_split_info;
6814 
6815 	MLX5_ASSERT(fm->drop_cnt);
6816 	drop_actions[0].type =
6817 		(enum rte_flow_action_type)MLX5_RTE_FLOW_ACTION_TYPE_COUNT;
6818 	drop_actions[0].conf = (void *)(uintptr_t)fm->drop_cnt;
6819 	drop_actions[1].type = RTE_FLOW_ACTION_TYPE_DROP;
6820 	drop_actions[1].conf = NULL;
6821 	drop_actions[2].type = RTE_FLOW_ACTION_TYPE_END;
6822 	drop_actions[2].conf = NULL;
6823 	drop_split_info.external = false;
6824 	drop_split_info.skip_scale |= 1 << MLX5_SCALE_FLOW_GROUP_BIT;
6825 	drop_split_info.table_id = MLX5_MTR_TABLE_ID_DROP;
6826 	drop_attr.group = MLX5_FLOW_TABLE_LEVEL_METER;
6827 	return flow_create_split_inner(dev, flow, &dev_flow,
6828 				&drop_attr, items, drop_actions,
6829 				&drop_split_info, error);
6830 }
6831 
6832 static int
6833 flow_count_vlan_items(const struct rte_flow_item items[])
6834 {
6835 	int items_n = 0;
6836 
6837 	for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
6838 		if (items->type == RTE_FLOW_ITEM_TYPE_VLAN ||
6839 		    items->type == (enum rte_flow_item_type)MLX5_RTE_FLOW_ITEM_TYPE_VLAN)
6840 			items_n++;
6841 	}
6842 	return items_n;
6843 }
6844 
6845 /**
6846  * The splitting for meter feature.
6847  *
6848  * - The meter flow will be split to two flows as prefix and
6849  *   suffix flow. The packets make sense only it pass the prefix
6850  *   meter action.
6851  *
6852  * - Reg_C_5 is used for the packet to match betweend prefix and
6853  *   suffix flow.
6854  *
6855  * @param dev
6856  *   Pointer to Ethernet device.
6857  * @param[in] flow
6858  *   Parent flow structure pointer.
6859  * @param[in] attr
6860  *   Flow rule attributes.
6861  * @param[in] items
6862  *   Pattern specification (list terminated by the END pattern item).
6863  * @param[in] actions
6864  *   Associated actions (list terminated by the END action).
6865  * @param[in] flow_split_info
6866  *   Pointer to flow split info structure.
6867  * @param[out] error
6868  *   Perform verbose error reporting if not NULL.
6869  * @return
6870  *   0 on success, negative value otherwise
6871  */
6872 static int
6873 flow_create_split_meter(struct rte_eth_dev *dev,
6874 			struct rte_flow *flow,
6875 			const struct rte_flow_attr *attr,
6876 			const struct rte_flow_item items[],
6877 			const struct rte_flow_action actions[],
6878 			struct mlx5_flow_split_info *flow_split_info,
6879 			struct rte_flow_error *error)
6880 {
6881 	struct mlx5_priv *priv = dev->data->dev_private;
6882 	struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
6883 	struct rte_flow_action *sfx_actions = NULL;
6884 	struct rte_flow_action *pre_actions = NULL;
6885 	struct rte_flow_item *sfx_items = NULL;
6886 	struct mlx5_flow *dev_flow = NULL;
6887 	struct rte_flow_attr sfx_attr = *attr;
6888 	struct mlx5_flow_meter_info *fm = NULL;
6889 	uint8_t skip_scale_restore;
6890 	bool has_mtr = false;
6891 	bool has_modify = false;
6892 	bool set_mtr_reg = true;
6893 	bool is_mtr_hierarchy = false;
6894 	uint32_t meter_id = 0;
6895 	uint32_t mtr_idx = 0;
6896 	uint32_t mtr_flow_id = 0;
6897 	size_t act_size;
6898 	size_t item_size;
6899 	int actions_n = 0;
6900 	int vlan_items_n = 0;
6901 	int ret = 0;
6902 
6903 	if (priv->mtr_en)
6904 		actions_n = flow_check_meter_action(dev, actions, &has_mtr,
6905 						    &has_modify, &meter_id);
6906 	if (has_mtr) {
6907 		if (flow->meter) {
6908 			fm = flow_dv_meter_find_by_idx(priv, flow->meter);
6909 			if (!fm)
6910 				return rte_flow_error_set(error, EINVAL,
6911 						RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6912 						NULL, "Meter not found.");
6913 		} else {
6914 			fm = mlx5_flow_meter_find(priv, meter_id, &mtr_idx);
6915 			if (!fm)
6916 				return rte_flow_error_set(error, EINVAL,
6917 						RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6918 						NULL, "Meter not found.");
6919 			ret = mlx5_flow_meter_attach(priv, fm,
6920 						     &sfx_attr, error);
6921 			if (ret)
6922 				return -rte_errno;
6923 			flow->meter = mtr_idx;
6924 		}
6925 		MLX5_ASSERT(wks);
6926 		wks->fm = fm;
6927 		if (!fm->def_policy) {
6928 			wks->policy = mlx5_flow_meter_policy_find(dev,
6929 								  fm->policy_id,
6930 								  NULL);
6931 			MLX5_ASSERT(wks->policy);
6932 			if (wks->policy->mark)
6933 				wks->mark = 1;
6934 			if (wks->policy->is_hierarchy) {
6935 				wks->final_policy =
6936 				mlx5_flow_meter_hierarchy_get_final_policy(dev,
6937 								wks->policy);
6938 				if (!wks->final_policy)
6939 					return rte_flow_error_set(error,
6940 					EINVAL,
6941 					RTE_FLOW_ERROR_TYPE_ACTION, NULL,
6942 				"Failed to find terminal policy of hierarchy.");
6943 				is_mtr_hierarchy = true;
6944 			}
6945 		}
6946 		/*
6947 		 * If it isn't default-policy Meter, and
6948 		 * 1. Not meter hierarchy and there's no action in flow to change
6949 		 *    packet (modify/encap/decap etc.), OR
6950 		 * 2. No drop count needed for this meter.
6951 		 * Then no need to use regC to save meter id anymore.
6952 		 */
6953 		if (!fm->def_policy && ((!has_modify && !is_mtr_hierarchy) || !fm->drop_cnt))
6954 			set_mtr_reg = false;
6955 		/* Prefix actions: meter, decap, encap, tag, jump, end, cnt. */
6956 #define METER_PREFIX_ACTION 7
6957 		act_size = (sizeof(struct rte_flow_action) *
6958 			    (actions_n + METER_PREFIX_ACTION)) +
6959 			   sizeof(struct mlx5_rte_flow_action_set_tag);
6960 		/* Flow can have multiple VLAN items. Account for them in suffix items. */
6961 		vlan_items_n = flow_count_vlan_items(items);
6962 		/* Suffix items: tag, [vlans], port id, end. */
6963 #define METER_SUFFIX_ITEM 3
6964 		item_size = sizeof(struct rte_flow_item) * (METER_SUFFIX_ITEM + vlan_items_n) +
6965 			    sizeof(struct mlx5_rte_flow_item_tag) * 2;
6966 		sfx_actions = mlx5_malloc(MLX5_MEM_ZERO, (act_size + item_size),
6967 					  0, SOCKET_ID_ANY);
6968 		if (!sfx_actions)
6969 			return rte_flow_error_set(error, ENOMEM,
6970 						  RTE_FLOW_ERROR_TYPE_ACTION,
6971 						  NULL, "no memory to split "
6972 						  "meter flow");
6973 		sfx_items = (struct rte_flow_item *)((char *)sfx_actions +
6974 			     act_size);
6975 		/* There's no suffix flow for meter of non-default policy. */
6976 		if (!fm->def_policy)
6977 			pre_actions = sfx_actions + 1;
6978 		else
6979 			pre_actions = sfx_actions + actions_n;
6980 		ret = flow_meter_split_prep(dev, flow, wks, &sfx_attr,
6981 					    items, sfx_items, actions,
6982 					    sfx_actions, pre_actions,
6983 					    (set_mtr_reg ? &mtr_flow_id : NULL),
6984 					    error);
6985 		if (ret) {
6986 			ret = -rte_errno;
6987 			goto exit;
6988 		}
6989 		/* Add the prefix subflow. */
6990 		skip_scale_restore = flow_split_info->skip_scale;
6991 		flow_split_info->skip_scale |=
6992 			1 << MLX5_SCALE_JUMP_FLOW_GROUP_BIT;
6993 		ret = flow_create_split_inner(dev, flow, &dev_flow,
6994 					      attr, items, pre_actions,
6995 					      flow_split_info, error);
6996 		flow_split_info->skip_scale = skip_scale_restore;
6997 		if (ret) {
6998 			if (mtr_flow_id)
6999 				mlx5_ipool_free(fm->flow_ipool, mtr_flow_id);
7000 			ret = -rte_errno;
7001 			goto exit;
7002 		}
7003 		if (mtr_flow_id) {
7004 			dev_flow->handle->split_flow_id = mtr_flow_id;
7005 			dev_flow->handle->is_meter_flow_id = 1;
7006 		}
7007 		if (!fm->def_policy) {
7008 			if (!set_mtr_reg && fm->drop_cnt)
7009 				ret =
7010 			flow_meter_create_drop_flow_with_org_pattern(dev, flow,
7011 							&sfx_attr, items,
7012 							flow_split_info,
7013 							fm, error);
7014 			goto exit;
7015 		}
7016 		/* Setting the sfx group atrr. */
7017 		sfx_attr.group = sfx_attr.transfer ?
7018 				(MLX5_FLOW_TABLE_LEVEL_METER - 1) :
7019 				 MLX5_FLOW_TABLE_LEVEL_METER;
7020 		flow_split_info->prefix_layers =
7021 				flow_get_prefix_layer_flags(dev_flow);
7022 		flow_split_info->prefix_mark |= wks->mark;
7023 		flow_split_info->table_id = MLX5_MTR_TABLE_ID_SUFFIX;
7024 	}
7025 	/* Add the prefix subflow. */
7026 	ret = flow_create_split_metadata(dev, flow,
7027 					 &sfx_attr, sfx_items ?
7028 					 sfx_items : items,
7029 					 sfx_actions ? sfx_actions : actions,
7030 					 flow_split_info, error);
7031 exit:
7032 	if (sfx_actions)
7033 		mlx5_free(sfx_actions);
7034 	return ret;
7035 }
7036 
7037 /**
7038  * The splitting for sample feature.
7039  *
7040  * Once Sample action is detected in the action list, the flow actions should
7041  * be split into prefix sub flow and suffix sub flow.
7042  *
7043  * The original items remain in the prefix sub flow, all actions preceding the
7044  * sample action and the sample action itself will be copied to the prefix
7045  * sub flow, the actions following the sample action will be copied to the
7046  * suffix sub flow, Queue action always be located in the suffix sub flow.
7047  *
7048  * In order to make the packet from prefix sub flow matches with suffix sub
7049  * flow, an extra tag action be added into prefix sub flow, and the suffix sub
7050  * flow uses tag item with the unique flow id.
7051  *
7052  * @param dev
7053  *   Pointer to Ethernet device.
7054  * @param[in] flow
7055  *   Parent flow structure pointer.
7056  * @param[in] attr
7057  *   Flow rule attributes.
7058  * @param[in] items
7059  *   Pattern specification (list terminated by the END pattern item).
7060  * @param[in] actions
7061  *   Associated actions (list terminated by the END action).
7062  * @param[in] flow_split_info
7063  *   Pointer to flow split info structure.
7064  * @param[out] error
7065  *   Perform verbose error reporting if not NULL.
7066  * @return
7067  *   0 on success, negative value otherwise
7068  */
7069 static int
7070 flow_create_split_sample(struct rte_eth_dev *dev,
7071 			 struct rte_flow *flow,
7072 			 const struct rte_flow_attr *attr,
7073 			 const struct rte_flow_item items[],
7074 			 const struct rte_flow_action actions[],
7075 			 struct mlx5_flow_split_info *flow_split_info,
7076 			 struct rte_flow_error *error)
7077 {
7078 	struct mlx5_priv *priv = dev->data->dev_private;
7079 	struct rte_flow_action *sfx_actions = NULL;
7080 	struct rte_flow_action *pre_actions = NULL;
7081 	struct rte_flow_item *sfx_items = NULL;
7082 	struct mlx5_flow *dev_flow = NULL;
7083 	struct rte_flow_attr sfx_attr = *attr;
7084 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
7085 	struct mlx5_flow_dv_sample_resource *sample_res;
7086 	struct mlx5_flow_tbl_data_entry *sfx_tbl_data;
7087 	struct mlx5_flow_tbl_resource *sfx_tbl;
7088 	struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
7089 #endif
7090 	size_t act_size;
7091 	size_t item_size;
7092 	uint32_t fdb_tx = 0;
7093 	int32_t tag_id = 0;
7094 	int actions_n = 0;
7095 	int sample_action_pos;
7096 	int qrss_action_pos;
7097 	int add_tag = 0;
7098 	int modify_after_mirror = 0;
7099 	uint16_t jump_table = 0;
7100 	const uint32_t next_ft_step = 1;
7101 	int ret = 0;
7102 	struct mlx5_priv *item_port_priv = NULL;
7103 	const struct rte_flow_item *item;
7104 
7105 	if (priv->sampler_en)
7106 		actions_n = flow_check_match_action(actions, attr,
7107 					RTE_FLOW_ACTION_TYPE_SAMPLE,
7108 					&sample_action_pos, &qrss_action_pos,
7109 					&modify_after_mirror);
7110 	if (actions_n) {
7111 		/* The prefix actions must includes sample, tag, end. */
7112 		act_size = sizeof(struct rte_flow_action) * (actions_n * 2 + 1)
7113 			   + sizeof(struct mlx5_rte_flow_action_set_tag);
7114 		item_size = sizeof(struct rte_flow_item) * SAMPLE_SUFFIX_ITEM +
7115 			    sizeof(struct mlx5_rte_flow_item_tag) * 2;
7116 		sfx_actions = mlx5_malloc(MLX5_MEM_ZERO, (act_size +
7117 					  item_size), 0, SOCKET_ID_ANY);
7118 		if (!sfx_actions)
7119 			return rte_flow_error_set(error, ENOMEM,
7120 						  RTE_FLOW_ERROR_TYPE_ACTION,
7121 						  NULL, "no memory to split "
7122 						  "sample flow");
7123 		for (item = items; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
7124 			if (item->type == RTE_FLOW_ITEM_TYPE_PORT_ID) {
7125 				const struct rte_flow_item_port_id *spec;
7126 
7127 				spec = (const struct rte_flow_item_port_id *)item->spec;
7128 				if (spec)
7129 					item_port_priv =
7130 						mlx5_port_to_eswitch_info(spec->id, true);
7131 				break;
7132 			} else if (item->type == RTE_FLOW_ITEM_TYPE_REPRESENTED_PORT) {
7133 				const struct rte_flow_item_ethdev *spec;
7134 
7135 				spec = (const struct rte_flow_item_ethdev *)item->spec;
7136 				if (spec)
7137 					item_port_priv =
7138 						mlx5_port_to_eswitch_info(spec->port_id, true);
7139 				break;
7140 			} else if (item->type == RTE_FLOW_ITEM_TYPE_PORT_REPRESENTOR) {
7141 				const struct rte_flow_item_ethdev *spec;
7142 
7143 				spec = (const struct rte_flow_item_ethdev *)item->spec;
7144 				if (spec)
7145 					item_port_priv =
7146 						mlx5_port_to_eswitch_info(spec->port_id, true);
7147 				break;
7148 			}
7149 		}
7150 		/* The representor_id is UINT16_MAX for uplink. */
7151 		fdb_tx = (attr->transfer &&
7152 			  flow_source_vport_representor(priv, item_port_priv));
7153 		/*
7154 		 * When reg_c_preserve is set, metadata registers Cx preserve
7155 		 * their value even through packet duplication.
7156 		 */
7157 		add_tag = (!fdb_tx ||
7158 			   priv->sh->cdev->config.hca_attr.reg_c_preserve);
7159 		if (add_tag)
7160 			sfx_items = (struct rte_flow_item *)((char *)sfx_actions
7161 					+ act_size);
7162 		if (modify_after_mirror)
7163 			jump_table = attr->group * MLX5_FLOW_TABLE_FACTOR +
7164 				     next_ft_step;
7165 		pre_actions = sfx_actions + actions_n;
7166 		tag_id = flow_sample_split_prep(dev, add_tag, items, sfx_items,
7167 						actions, sfx_actions,
7168 						pre_actions, actions_n,
7169 						sample_action_pos,
7170 						qrss_action_pos, jump_table,
7171 						error);
7172 		if (tag_id < 0 || (add_tag && !tag_id)) {
7173 			ret = -rte_errno;
7174 			goto exit;
7175 		}
7176 		if (modify_after_mirror)
7177 			flow_split_info->skip_scale =
7178 					1 << MLX5_SCALE_JUMP_FLOW_GROUP_BIT;
7179 		/* Add the prefix subflow. */
7180 		ret = flow_create_split_inner(dev, flow, &dev_flow, attr,
7181 					      items, pre_actions,
7182 					      flow_split_info, error);
7183 		if (ret) {
7184 			ret = -rte_errno;
7185 			goto exit;
7186 		}
7187 		dev_flow->handle->split_flow_id = tag_id;
7188 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
7189 		if (!modify_after_mirror) {
7190 			/* Set the sfx group attr. */
7191 			sample_res = (struct mlx5_flow_dv_sample_resource *)
7192 						dev_flow->dv.sample_res;
7193 			sfx_tbl = (struct mlx5_flow_tbl_resource *)
7194 						sample_res->normal_path_tbl;
7195 			sfx_tbl_data = container_of(sfx_tbl,
7196 						struct mlx5_flow_tbl_data_entry,
7197 						tbl);
7198 			sfx_attr.group = sfx_attr.transfer ?
7199 			(sfx_tbl_data->level - 1) : sfx_tbl_data->level;
7200 		} else {
7201 			MLX5_ASSERT(attr->transfer);
7202 			sfx_attr.group = jump_table;
7203 		}
7204 		flow_split_info->prefix_layers =
7205 				flow_get_prefix_layer_flags(dev_flow);
7206 		MLX5_ASSERT(wks);
7207 		flow_split_info->prefix_mark |= wks->mark;
7208 		/* Suffix group level already be scaled with factor, set
7209 		 * MLX5_SCALE_FLOW_GROUP_BIT of skip_scale to 1 to avoid scale
7210 		 * again in translation.
7211 		 */
7212 		flow_split_info->skip_scale = 1 << MLX5_SCALE_FLOW_GROUP_BIT;
7213 #endif
7214 	}
7215 	/* Add the suffix subflow. */
7216 	ret = flow_create_split_meter(dev, flow, &sfx_attr,
7217 				      sfx_items ? sfx_items : items,
7218 				      sfx_actions ? sfx_actions : actions,
7219 				      flow_split_info, error);
7220 exit:
7221 	if (sfx_actions)
7222 		mlx5_free(sfx_actions);
7223 	return ret;
7224 }
7225 
7226 /**
7227  * Split the flow to subflow set. The splitters might be linked
7228  * in the chain, like this:
7229  * flow_create_split_outer() calls:
7230  *   flow_create_split_meter() calls:
7231  *     flow_create_split_metadata(meter_subflow_0) calls:
7232  *       flow_create_split_inner(metadata_subflow_0)
7233  *       flow_create_split_inner(metadata_subflow_1)
7234  *       flow_create_split_inner(metadata_subflow_2)
7235  *     flow_create_split_metadata(meter_subflow_1) calls:
7236  *       flow_create_split_inner(metadata_subflow_0)
7237  *       flow_create_split_inner(metadata_subflow_1)
7238  *       flow_create_split_inner(metadata_subflow_2)
7239  *
7240  * This provide flexible way to add new levels of flow splitting.
7241  * The all of successfully created subflows are included to the
7242  * parent flow dev_flow list.
7243  *
7244  * @param dev
7245  *   Pointer to Ethernet device.
7246  * @param[in] flow
7247  *   Parent flow structure pointer.
7248  * @param[in] attr
7249  *   Flow rule attributes.
7250  * @param[in] items
7251  *   Pattern specification (list terminated by the END pattern item).
7252  * @param[in] actions
7253  *   Associated actions (list terminated by the END action).
7254  * @param[in] flow_split_info
7255  *   Pointer to flow split info structure.
7256  * @param[out] error
7257  *   Perform verbose error reporting if not NULL.
7258  * @return
7259  *   0 on success, negative value otherwise
7260  */
7261 static int
7262 flow_create_split_outer(struct rte_eth_dev *dev,
7263 			struct rte_flow *flow,
7264 			const struct rte_flow_attr *attr,
7265 			const struct rte_flow_item items[],
7266 			const struct rte_flow_action actions[],
7267 			struct mlx5_flow_split_info *flow_split_info,
7268 			struct rte_flow_error *error)
7269 {
7270 	int ret;
7271 
7272 	ret = flow_create_split_sample(dev, flow, attr, items,
7273 				       actions, flow_split_info, error);
7274 	MLX5_ASSERT(ret <= 0);
7275 	return ret;
7276 }
7277 
7278 static inline struct mlx5_flow_tunnel *
7279 flow_tunnel_from_rule(const struct mlx5_flow *flow)
7280 {
7281 	struct mlx5_flow_tunnel *tunnel;
7282 
7283 #pragma GCC diagnostic push
7284 #pragma GCC diagnostic ignored "-Wcast-qual"
7285 	tunnel = (typeof(tunnel))flow->tunnel;
7286 #pragma GCC diagnostic pop
7287 
7288 	return tunnel;
7289 }
7290 
7291 /**
7292  * Create a flow and add it to @p list.
7293  *
7294  * @param dev
7295  *   Pointer to Ethernet device.
7296  * @param list
7297  *   Pointer to a TAILQ flow list. If this parameter NULL,
7298  *   no list insertion occurred, flow is just created,
7299  *   this is caller's responsibility to track the
7300  *   created flow.
7301  * @param[in] attr
7302  *   Flow rule attributes.
7303  * @param[in] items
7304  *   Pattern specification (list terminated by the END pattern item).
7305  * @param[in] actions
7306  *   Associated actions (list terminated by the END action).
7307  * @param[in] external
7308  *   This flow rule is created by request external to PMD.
7309  * @param[out] error
7310  *   Perform verbose error reporting if not NULL.
7311  *
7312  * @return
7313  *   A flow index on success, 0 otherwise and rte_errno is set.
7314  */
7315 uintptr_t
7316 flow_legacy_list_create(struct rte_eth_dev *dev, enum mlx5_flow_type type,
7317 		    const struct rte_flow_attr *attr,
7318 		    const struct rte_flow_item items[],
7319 		    const struct rte_flow_action original_actions[],
7320 		    bool external, struct rte_flow_error *error)
7321 {
7322 	struct mlx5_priv *priv = dev->data->dev_private;
7323 	struct rte_flow *flow = NULL;
7324 	struct mlx5_flow *dev_flow;
7325 	const struct rte_flow_action_rss *rss = NULL;
7326 	struct mlx5_translated_action_handle
7327 		indir_actions[MLX5_MAX_INDIRECT_ACTIONS];
7328 	int indir_actions_n = MLX5_MAX_INDIRECT_ACTIONS;
7329 	union {
7330 		struct mlx5_flow_expand_rss buf;
7331 		uint8_t buffer[8192];
7332 	} expand_buffer;
7333 	union {
7334 		struct rte_flow_action actions[MLX5_MAX_SPLIT_ACTIONS];
7335 		uint8_t buffer[2048];
7336 	} actions_rx;
7337 	union {
7338 		struct rte_flow_action actions[MLX5_MAX_SPLIT_ACTIONS];
7339 		uint8_t buffer[2048];
7340 	} actions_hairpin_tx;
7341 	union {
7342 		struct rte_flow_item items[MLX5_MAX_SPLIT_ITEMS];
7343 		uint8_t buffer[2048];
7344 	} items_tx;
7345 	struct mlx5_rte_flow_item_sq sq_specs[RTE_MAX_QUEUES_PER_PORT];
7346 	struct mlx5_flow_expand_rss *buf = &expand_buffer.buf;
7347 	struct mlx5_flow_rss_desc *rss_desc;
7348 	const struct rte_flow_action *p_actions_rx;
7349 	uint32_t i;
7350 	uint32_t idx = 0;
7351 	int hairpin_flow;
7352 	struct rte_flow_attr attr_tx = { .priority = 0 };
7353 	const struct rte_flow_action *actions;
7354 	struct rte_flow_action *translated_actions = NULL;
7355 	struct mlx5_flow_tunnel *tunnel;
7356 	struct tunnel_default_miss_ctx default_miss_ctx = { 0, };
7357 	struct mlx5_flow_workspace *wks = mlx5_flow_push_thread_workspace();
7358 	struct mlx5_flow_split_info flow_split_info = {
7359 		.external = !!external,
7360 		.skip_scale = 0,
7361 		.flow_idx = 0,
7362 		.prefix_mark = 0,
7363 		.prefix_layers = 0,
7364 		.table_id = 0
7365 	};
7366 	int ret;
7367 	struct mlx5_shared_action_rss *shared_rss_action;
7368 
7369 	if (!wks)
7370 		return rte_flow_error_set(error, ENOMEM,
7371 					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7372 					  NULL,
7373 					  "failed to push flow workspace");
7374 	memset(indir_actions, 0, sizeof(indir_actions));
7375 	rss_desc = &wks->rss_desc;
7376 	ret = flow_action_handles_translate(dev, original_actions,
7377 					    indir_actions,
7378 					    &indir_actions_n,
7379 					    &translated_actions, error);
7380 	if (ret < 0) {
7381 		MLX5_ASSERT(translated_actions == NULL);
7382 		return 0;
7383 	}
7384 	actions = translated_actions ? translated_actions : original_actions;
7385 	p_actions_rx = actions;
7386 	hairpin_flow = flow_check_hairpin_split(dev, attr, actions);
7387 	ret = flow_drv_validate(dev, attr, items, p_actions_rx,
7388 				external, hairpin_flow, error);
7389 	if (ret < 0)
7390 		goto error_before_hairpin_split;
7391 	flow = mlx5_ipool_zmalloc(priv->flows[type], &idx);
7392 	if (!flow) {
7393 		rte_errno = ENOMEM;
7394 		goto error_before_hairpin_split;
7395 	}
7396 	if (hairpin_flow > 0) {
7397 		if (hairpin_flow > MLX5_MAX_SPLIT_ACTIONS) {
7398 			rte_errno = EINVAL;
7399 			goto error_before_hairpin_split;
7400 		}
7401 		flow_hairpin_split(dev, actions, actions_rx.actions,
7402 				   actions_hairpin_tx.actions, items_tx.items,
7403 				   idx);
7404 		p_actions_rx = actions_rx.actions;
7405 	}
7406 	flow_split_info.flow_idx = idx;
7407 	flow->drv_type = flow_get_drv_type(dev, attr);
7408 	MLX5_ASSERT(flow->drv_type > MLX5_FLOW_TYPE_MIN &&
7409 		    flow->drv_type < MLX5_FLOW_TYPE_MAX);
7410 	memset(rss_desc, 0, offsetof(struct mlx5_flow_rss_desc, queue));
7411 	/* RSS Action only works on NIC RX domain */
7412 	if (attr->ingress)
7413 		rss = flow_get_rss_action(dev, p_actions_rx);
7414 	if (rss) {
7415 		MLX5_ASSERT(rss->queue_num <= RTE_ETH_RSS_RETA_SIZE_512);
7416 		rss_desc->symmetric_hash_function = MLX5_RSS_IS_SYMM(rss->func);
7417 		/*
7418 		 * The following information is required by
7419 		 * mlx5_flow_hashfields_adjust() in advance.
7420 		 */
7421 		rss_desc->level = rss->level;
7422 		/* RSS type 0 indicates default RSS type (RTE_ETH_RSS_IP). */
7423 		rss_desc->types = !rss->types ? RTE_ETH_RSS_IP : rss->types;
7424 	}
7425 	flow->dev_handles = 0;
7426 	if (rss && rss->types) {
7427 		unsigned int graph_root;
7428 
7429 		graph_root = find_graph_root(rss->level);
7430 		ret = mlx5_flow_expand_rss(buf, sizeof(expand_buffer.buffer),
7431 					   items, rss->types,
7432 					   mlx5_support_expansion, graph_root);
7433 		MLX5_ASSERT(ret > 0 &&
7434 		       (unsigned int)ret < sizeof(expand_buffer.buffer));
7435 		if (rte_log_can_log(mlx5_logtype, RTE_LOG_DEBUG)) {
7436 			for (i = 0; i < buf->entries; ++i)
7437 				mlx5_dbg__print_pattern(buf->entry[i].pattern);
7438 		}
7439 	} else {
7440 		ret = mlx5_flow_expand_sqn((struct mlx5_flow_expand_sqn *)buf,
7441 					   sizeof(expand_buffer.buffer),
7442 					   items, sq_specs);
7443 		if (ret) {
7444 			rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
7445 					   NULL, "not enough memory for rte_flow");
7446 			goto error;
7447 		}
7448 		if (buf->entries == 0) {
7449 			buf->entries = 1;
7450 			buf->entry[0].pattern = (void *)(uintptr_t)items;
7451 		}
7452 	}
7453 	rss_desc->shared_rss = flow_get_shared_rss_action(dev, indir_actions,
7454 						      indir_actions_n);
7455 	for (i = 0; i < buf->entries; ++i) {
7456 		/* Initialize flow split data. */
7457 		flow_split_info.prefix_layers = 0;
7458 		flow_split_info.prefix_mark = 0;
7459 		flow_split_info.skip_scale = 0;
7460 		/*
7461 		 * The splitter may create multiple dev_flows,
7462 		 * depending on configuration. In the simplest
7463 		 * case it just creates unmodified original flow.
7464 		 */
7465 		ret = flow_create_split_outer(dev, flow, attr,
7466 					      buf->entry[i].pattern,
7467 					      p_actions_rx, &flow_split_info,
7468 					      error);
7469 		if (ret < 0)
7470 			goto error;
7471 		if (is_flow_tunnel_steer_rule(wks->flows[0].tof_type)) {
7472 			ret = flow_tunnel_add_default_miss(dev, flow, attr,
7473 							   p_actions_rx,
7474 							   idx,
7475 							   wks->flows[0].tunnel,
7476 							   &default_miss_ctx,
7477 							   error);
7478 			if (ret < 0) {
7479 				mlx5_free(default_miss_ctx.queue);
7480 				goto error;
7481 			}
7482 		}
7483 	}
7484 	/* Create the tx flow. */
7485 	if (hairpin_flow) {
7486 		attr_tx.group = MLX5_HAIRPIN_TX_TABLE;
7487 		attr_tx.ingress = 0;
7488 		attr_tx.egress = 1;
7489 		dev_flow = flow_drv_prepare(dev, flow, &attr_tx, items_tx.items,
7490 					 actions_hairpin_tx.actions,
7491 					 idx, error);
7492 		if (!dev_flow)
7493 			goto error;
7494 		dev_flow->flow = flow;
7495 		dev_flow->external = 0;
7496 		SILIST_INSERT(&flow->dev_handles, dev_flow->handle_idx,
7497 			      dev_flow->handle, next);
7498 		ret = flow_drv_translate(dev, dev_flow, &attr_tx,
7499 					 items_tx.items,
7500 					 actions_hairpin_tx.actions, error);
7501 		if (ret < 0)
7502 			goto error;
7503 	}
7504 	/*
7505 	 * Update the metadata register copy table. If extensive
7506 	 * metadata feature is enabled and registers are supported
7507 	 * we might create the extra rte_flow for each unique
7508 	 * MARK/FLAG action ID.
7509 	 *
7510 	 * The table is updated for ingress and transfer flows only, because
7511 	 * the egress Flows belong to the different device and
7512 	 * copy table should be updated in peer NIC Rx domain.
7513 	 */
7514 	if ((attr->ingress || attr->transfer) &&
7515 	    (external || attr->group != MLX5_FLOW_MREG_CP_TABLE_GROUP)) {
7516 		ret = flow_mreg_update_copy_table(dev, flow, actions, error);
7517 		if (ret)
7518 			goto error;
7519 	}
7520 	/*
7521 	 * If the flow is external (from application) OR device is started,
7522 	 * OR mreg discover, then apply immediately.
7523 	 */
7524 	if (external || dev->data->dev_started ||
7525 	    (attr->group == MLX5_FLOW_MREG_CP_TABLE_GROUP &&
7526 	     attr->priority == MLX5_FLOW_LOWEST_PRIO_INDICATOR)) {
7527 		ret = flow_drv_apply(dev, flow, error);
7528 		if (ret < 0)
7529 			goto error;
7530 	}
7531 	flow->type = type;
7532 	flow_rxq_flags_set(dev, flow);
7533 	rte_free(translated_actions);
7534 	tunnel = flow_tunnel_from_rule(wks->flows);
7535 	if (tunnel) {
7536 		flow->tunnel = 1;
7537 		flow->tunnel_id = tunnel->tunnel_id;
7538 		rte_atomic_fetch_add_explicit(&tunnel->refctn, 1, rte_memory_order_relaxed);
7539 		mlx5_free(default_miss_ctx.queue);
7540 	}
7541 	mlx5_flow_pop_thread_workspace();
7542 	return idx;
7543 error:
7544 	MLX5_ASSERT(flow);
7545 	ret = rte_errno; /* Save rte_errno before cleanup. */
7546 	flow_mreg_del_copy_action(dev, flow);
7547 	flow_drv_destroy(dev, flow);
7548 
7549 	if (rss_desc->shared_rss) {
7550 		shared_rss_action = (struct mlx5_shared_action_rss *)
7551 			mlx5_ipool_get
7552 			(priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS],
7553 			rss_desc->shared_rss);
7554 		if (shared_rss_action)
7555 			rte_atomic_fetch_sub_explicit(&(shared_rss_action)->refcnt, 1,
7556 						      rte_memory_order_relaxed);
7557 	}
7558 	mlx5_ipool_free(priv->flows[type], idx);
7559 	rte_errno = ret; /* Restore rte_errno. */
7560 	ret = rte_errno;
7561 	rte_errno = ret;
7562 error_before_hairpin_split:
7563 	mlx5_flow_pop_thread_workspace();
7564 	rte_free(translated_actions);
7565 	return 0;
7566 }
7567 
7568 /**
7569  * Create a dedicated flow rule on e-switch table 0 (root table), to direct all
7570  * incoming packets to table 1.
7571  *
7572  * Other flow rules, requested for group n, will be created in
7573  * e-switch table n+1.
7574  * Jump action to e-switch group n will be created to group n+1.
7575  *
7576  * Used when working in switchdev mode, to utilise advantages of table 1
7577  * and above.
7578  *
7579  * @param dev
7580  *   Pointer to Ethernet device.
7581  *
7582  * @return
7583  *   Pointer to flow on success, NULL otherwise and rte_errno is set.
7584  */
7585 struct rte_flow *
7586 mlx5_flow_create_esw_table_zero_flow(struct rte_eth_dev *dev)
7587 {
7588 	const struct rte_flow_attr attr = {
7589 		.group = 0,
7590 		.priority = 0,
7591 		.ingress = 0,
7592 		.egress = 0,
7593 		.transfer = 1,
7594 	};
7595 	const struct rte_flow_item pattern = {
7596 		.type = RTE_FLOW_ITEM_TYPE_END,
7597 	};
7598 	struct rte_flow_action_jump jump = {
7599 		.group = 1,
7600 	};
7601 	const struct rte_flow_action actions[] = {
7602 		{
7603 			.type = RTE_FLOW_ACTION_TYPE_JUMP,
7604 			.conf = &jump,
7605 		},
7606 		{
7607 			.type = RTE_FLOW_ACTION_TYPE_END,
7608 		},
7609 	};
7610 	struct rte_flow_error error;
7611 
7612 	return (void *)(uintptr_t)mlx5_flow_list_create(dev, MLX5_FLOW_TYPE_CTL,
7613 						   &attr, &pattern,
7614 						   actions, false, &error);
7615 }
7616 
7617 /**
7618  * Create a dedicated flow rule on e-switch table 1, matches ESW manager
7619  * and sq number, directs all packets to peer vport.
7620  *
7621  * @param dev
7622  *   Pointer to Ethernet device.
7623  * @param sq_num
7624  *   SQ number.
7625  *
7626  * @return
7627  *   Flow ID on success, 0 otherwise and rte_errno is set.
7628  */
7629 uint32_t
7630 mlx5_flow_create_devx_sq_miss_flow(struct rte_eth_dev *dev, uint32_t sq_num)
7631 {
7632 	struct rte_flow_attr attr = {
7633 		.group = 0,
7634 		.priority = MLX5_FLOW_LOWEST_PRIO_INDICATOR,
7635 		.ingress = 0,
7636 		.egress = 0,
7637 		.transfer = 1,
7638 	};
7639 	struct rte_flow_item_port_id port_spec = {
7640 		.id = MLX5_PORT_ESW_MGR,
7641 	};
7642 	struct mlx5_rte_flow_item_sq sq_spec = {
7643 		.queue = sq_num,
7644 	};
7645 	struct rte_flow_item pattern[] = {
7646 		{
7647 			.type = RTE_FLOW_ITEM_TYPE_PORT_ID,
7648 			.spec = &port_spec,
7649 		},
7650 		{
7651 			.type = (enum rte_flow_item_type)
7652 				MLX5_RTE_FLOW_ITEM_TYPE_SQ,
7653 			.spec = &sq_spec,
7654 		},
7655 		{
7656 			.type = RTE_FLOW_ITEM_TYPE_END,
7657 		},
7658 	};
7659 	struct rte_flow_action_jump jump = {
7660 		.group = 1,
7661 	};
7662 	struct rte_flow_action_port_id port = {
7663 		.id = dev->data->port_id,
7664 	};
7665 	struct rte_flow_action actions[] = {
7666 		{
7667 			.type = RTE_FLOW_ACTION_TYPE_JUMP,
7668 			.conf = &jump,
7669 		},
7670 		{
7671 			.type = RTE_FLOW_ACTION_TYPE_END,
7672 		},
7673 	};
7674 	struct rte_flow_error error;
7675 
7676 	/*
7677 	 * Creates group 0, highest priority jump flow.
7678 	 * Matches txq to bypass kernel packets.
7679 	 */
7680 	if (mlx5_flow_list_create(dev, MLX5_FLOW_TYPE_CTL, &attr, pattern, actions,
7681 			     false, &error) == 0)
7682 		return 0;
7683 	/* Create group 1, lowest priority redirect flow for txq. */
7684 	attr.group = 1;
7685 	actions[0].conf = &port;
7686 	actions[0].type = RTE_FLOW_ACTION_TYPE_PORT_ID;
7687 	return mlx5_flow_list_create(dev, MLX5_FLOW_TYPE_CTL, &attr, pattern,
7688 				actions, false, &error);
7689 }
7690 
7691 /**
7692  * Validate a flow supported by the NIC.
7693  *
7694  * @see rte_flow_validate()
7695  * @see rte_flow_ops
7696  */
7697 int
7698 mlx5_flow_validate(struct rte_eth_dev *dev,
7699 		   const struct rte_flow_attr *attr,
7700 		   const struct rte_flow_item items[],
7701 		   const struct rte_flow_action original_actions[],
7702 		   struct rte_flow_error *error)
7703 {
7704 	int hairpin_flow;
7705 	struct mlx5_translated_action_handle
7706 		indir_actions[MLX5_MAX_INDIRECT_ACTIONS];
7707 	int indir_actions_n = MLX5_MAX_INDIRECT_ACTIONS;
7708 	const struct rte_flow_action *actions;
7709 	struct rte_flow_action *translated_actions = NULL;
7710 	int ret = flow_action_handles_translate(dev, original_actions,
7711 						indir_actions,
7712 						&indir_actions_n,
7713 						&translated_actions, error);
7714 
7715 	if (ret)
7716 		return ret;
7717 	actions = translated_actions ? translated_actions : original_actions;
7718 	hairpin_flow = flow_check_hairpin_split(dev, attr, actions);
7719 	ret = flow_drv_validate(dev, attr, items, actions,
7720 				true, hairpin_flow, error);
7721 	rte_free(translated_actions);
7722 	return ret;
7723 }
7724 
7725 static int
7726 mlx5_flow_cache_flow_info(struct rte_eth_dev *dev,
7727 			  const struct rte_flow_attr *attr,
7728 			  const uint32_t orig_prio,
7729 			  const struct rte_flow_item *items,
7730 			  const struct rte_flow_action *actions,
7731 			  uint32_t flow_idx)
7732 {
7733 	struct mlx5_priv *priv = dev->data->dev_private;
7734 	struct rte_pmd_mlx5_flow_engine_mode_info *mode_info = &priv->mode_info;
7735 	struct mlx5_dv_flow_info *flow_info, *tmp_info;
7736 	struct rte_flow_error error;
7737 	int len, ret;
7738 
7739 	flow_info = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*flow_info), 0, SOCKET_ID_ANY);
7740 	if (!flow_info) {
7741 		DRV_LOG(ERR, "No enough memory for flow_info caching.");
7742 		return -1;
7743 	}
7744 	flow_info->orig_prio = orig_prio;
7745 	flow_info->attr = *attr;
7746 	/* Standby mode rule awlays saves it in low priority entry. */
7747 	flow_info->flow_idx_low_prio = flow_idx;
7748 
7749 	/* Store matching items. */
7750 	ret = rte_flow_conv(RTE_FLOW_CONV_OP_PATTERN, NULL, 0, items, &error);
7751 	if (ret <= 0) {
7752 		DRV_LOG(ERR, "Can't get items length.");
7753 		goto end;
7754 	}
7755 	len = RTE_ALIGN(ret, 16);
7756 	flow_info->items = mlx5_malloc(MLX5_MEM_ZERO, len, 0, SOCKET_ID_ANY);
7757 	if (!flow_info->items) {
7758 		DRV_LOG(ERR, "No enough memory for items caching.");
7759 		goto end;
7760 	}
7761 	ret = rte_flow_conv(RTE_FLOW_CONV_OP_PATTERN, flow_info->items, ret, items, &error);
7762 	if (ret <= 0) {
7763 		DRV_LOG(ERR, "Can't duplicate items.");
7764 		goto end;
7765 	}
7766 
7767 	/* Store flow actions. */
7768 	ret = rte_flow_conv(RTE_FLOW_CONV_OP_ACTIONS, NULL, 0, actions, &error);
7769 	if (ret <= 0) {
7770 		DRV_LOG(ERR, "Can't get actions length.");
7771 		goto end;
7772 	}
7773 	len = RTE_ALIGN(ret, 16);
7774 	flow_info->actions = mlx5_malloc(MLX5_MEM_ZERO, len, 0, SOCKET_ID_ANY);
7775 	if (!flow_info->actions) {
7776 		DRV_LOG(ERR, "No enough memory for actions caching.");
7777 		goto end;
7778 	}
7779 	ret = rte_flow_conv(RTE_FLOW_CONV_OP_ACTIONS, flow_info->actions, ret, actions, &error);
7780 	if (ret <= 0) {
7781 		DRV_LOG(ERR, "Can't duplicate actions.");
7782 		goto end;
7783 	}
7784 
7785 	/* Insert to the list end. */
7786 	if (LIST_EMPTY(&mode_info->hot_upgrade)) {
7787 		LIST_INSERT_HEAD(&mode_info->hot_upgrade, flow_info,  next);
7788 	} else {
7789 		tmp_info = LIST_FIRST(&mode_info->hot_upgrade);
7790 		while (LIST_NEXT(tmp_info, next))
7791 			tmp_info = LIST_NEXT(tmp_info, next);
7792 		LIST_INSERT_AFTER(tmp_info, flow_info, next);
7793 	}
7794 	return 0;
7795 end:
7796 	if (flow_info->items)
7797 		mlx5_free(flow_info->items);
7798 	if (flow_info->actions)
7799 		mlx5_free(flow_info->actions);
7800 	mlx5_free(flow_info);
7801 	return -1;
7802 }
7803 
7804 static int
7805 mlx5_flow_cache_flow_toggle(struct rte_eth_dev *dev, bool orig_prio)
7806 {
7807 	struct mlx5_priv *priv = dev->data->dev_private;
7808 	struct rte_pmd_mlx5_flow_engine_mode_info *mode_info = &priv->mode_info;
7809 	struct mlx5_dv_flow_info *flow_info;
7810 	struct rte_flow_attr attr;
7811 	struct rte_flow_error error;
7812 	struct rte_flow *high, *low;
7813 
7814 	flow_info = LIST_FIRST(&mode_info->hot_upgrade);
7815 	while (flow_info) {
7816 		/* DUP flow may have the same priority. */
7817 		if (flow_info->orig_prio != flow_info->attr.priority) {
7818 			attr = flow_info->attr;
7819 			if (orig_prio)
7820 				attr.priority = flow_info->orig_prio;
7821 			flow_info->flow_idx_high_prio = mlx5_flow_list_create(dev,
7822 					MLX5_FLOW_TYPE_GEN, &attr,
7823 					flow_info->items, flow_info->actions,
7824 					true, &error);
7825 			if (!flow_info->flow_idx_high_prio) {
7826 				DRV_LOG(ERR, "Priority toggle failed internally.");
7827 				goto err;
7828 			}
7829 		}
7830 		flow_info = LIST_NEXT(flow_info, next);
7831 	}
7832 	/* Delete the low priority rules and swap the flow handle. */
7833 	flow_info = LIST_FIRST(&mode_info->hot_upgrade);
7834 	while (flow_info) {
7835 		MLX5_ASSERT(flow_info->flow_idx_low_prio);
7836 		if (flow_info->orig_prio != flow_info->attr.priority) {
7837 			high = mlx5_ipool_get(priv->flows[MLX5_FLOW_TYPE_GEN],
7838 					flow_info->flow_idx_high_prio);
7839 			low = mlx5_ipool_get(priv->flows[MLX5_FLOW_TYPE_GEN],
7840 					flow_info->flow_idx_low_prio);
7841 			if (high && low) {
7842 				RTE_SWAP(*low, *high);
7843 				mlx5_flow_list_destroy(dev, MLX5_FLOW_TYPE_GEN,
7844 						  flow_info->flow_idx_low_prio);
7845 				flow_info->flow_idx_high_prio = 0;
7846 			}
7847 		}
7848 		flow_info = LIST_NEXT(flow_info, next);
7849 	}
7850 	return 0;
7851 err:
7852 	/* Destroy preceding successful high priority rules. */
7853 	flow_info = LIST_FIRST(&mode_info->hot_upgrade);
7854 	while (flow_info) {
7855 		if (flow_info->orig_prio != flow_info->attr.priority) {
7856 			if (flow_info->flow_idx_high_prio)
7857 				mlx5_flow_list_destroy(dev, MLX5_FLOW_TYPE_GEN,
7858 						  flow_info->flow_idx_high_prio);
7859 			else
7860 				break;
7861 			flow_info->flow_idx_high_prio = 0;
7862 		}
7863 		flow_info = LIST_NEXT(flow_info, next);
7864 	}
7865 	return -1;
7866 }
7867 
7868 /**
7869  * Set the mode of the flow engine of a process to active or standby during live migration.
7870  *
7871  * @param[in] mode
7872  *   MLX5 flow engine mode, @see `enum rte_pmd_mlx5_flow_engine_mode`.
7873  * @param[in] flags
7874  *   Flow engine mode specific flags.
7875  *
7876  * @return
7877  *   Negative value on error, positive on success.
7878  */
7879 int
7880 rte_pmd_mlx5_flow_engine_set_mode(enum rte_pmd_mlx5_flow_engine_mode mode, uint32_t flags)
7881 {
7882 	struct mlx5_priv *priv;
7883 	struct rte_pmd_mlx5_flow_engine_mode_info *mode_info;
7884 	struct mlx5_dv_flow_info *flow_info, *tmp_info;
7885 	uint16_t port, port_id;
7886 	uint16_t toggle_num = 0;
7887 	struct rte_eth_dev *dev;
7888 	enum rte_pmd_mlx5_flow_engine_mode orig_mode;
7889 	uint32_t orig_flags;
7890 	bool need_toggle = false;
7891 
7892 	/* Check if flags combinations are supported. */
7893 	if (flags && flags != RTE_PMD_MLX5_FLOW_ENGINE_FLAG_STANDBY_DUP_INGRESS) {
7894 		DRV_LOG(ERR, "Doesn't support such flags %u", flags);
7895 		return -1;
7896 	}
7897 	MLX5_ETH_FOREACH_DEV(port, NULL) {
7898 		dev = &rte_eth_devices[port];
7899 		priv = dev->data->dev_private;
7900 		mode_info = &priv->mode_info;
7901 		/* No mode change. Assume all devices hold the same mode. */
7902 		if (mode_info->mode == mode) {
7903 			DRV_LOG(INFO, "Process flow engine has been in mode %u", mode);
7904 			if (mode_info->mode_flag != flags && !LIST_EMPTY(&mode_info->hot_upgrade)) {
7905 				DRV_LOG(ERR, "Port %u has rule cache with different flag %u\n",
7906 						port, mode_info->mode_flag);
7907 				orig_mode = mode_info->mode;
7908 				orig_flags = mode_info->mode_flag;
7909 				goto err;
7910 			}
7911 			mode_info->mode_flag = flags;
7912 			toggle_num++;
7913 			continue;
7914 		}
7915 		/* Active -> standby. */
7916 		if (mode == RTE_PMD_MLX5_FLOW_ENGINE_MODE_STANDBY) {
7917 			if (!LIST_EMPTY(&mode_info->hot_upgrade)) {
7918 				DRV_LOG(ERR, "Cached rule existed");
7919 				orig_mode = mode_info->mode;
7920 				orig_flags = mode_info->mode_flag;
7921 				goto err;
7922 			}
7923 			mode_info->mode_flag = flags;
7924 			mode_info->mode = mode;
7925 			toggle_num++;
7926 		/* Standby -> active. */
7927 		} else if (mode == RTE_PMD_MLX5_FLOW_ENGINE_MODE_ACTIVE) {
7928 			if (LIST_EMPTY(&mode_info->hot_upgrade)) {
7929 				DRV_LOG(INFO, "No cached rule existed");
7930 			} else {
7931 				if (mlx5_flow_cache_flow_toggle(dev, true)) {
7932 					orig_mode = mode_info->mode;
7933 					orig_flags = mode_info->mode_flag;
7934 					need_toggle = true;
7935 					goto err;
7936 				}
7937 			}
7938 			toggle_num++;
7939 		}
7940 	}
7941 	if (mode == RTE_PMD_MLX5_FLOW_ENGINE_MODE_ACTIVE) {
7942 		/* Clear cache flow rules. */
7943 		MLX5_ETH_FOREACH_DEV(port, NULL) {
7944 			priv = rte_eth_devices[port].data->dev_private;
7945 			mode_info = &priv->mode_info;
7946 			flow_info = LIST_FIRST(&mode_info->hot_upgrade);
7947 			while (flow_info) {
7948 				tmp_info = LIST_NEXT(flow_info, next);
7949 				LIST_REMOVE(flow_info, next);
7950 				mlx5_free(flow_info->actions);
7951 				mlx5_free(flow_info->items);
7952 				mlx5_free(flow_info);
7953 				flow_info = tmp_info;
7954 			}
7955 			MLX5_ASSERT(LIST_EMPTY(&mode_info->hot_upgrade));
7956 		}
7957 	}
7958 	return toggle_num;
7959 err:
7960 	/* Rollback all preceding successful ports. */
7961 	MLX5_ETH_FOREACH_DEV(port_id, NULL) {
7962 		if (port_id == port)
7963 			break;
7964 		priv = rte_eth_devices[port_id].data->dev_private;
7965 		mode_info = &priv->mode_info;
7966 		if (need_toggle && !LIST_EMPTY(&mode_info->hot_upgrade) &&
7967 		    mlx5_flow_cache_flow_toggle(dev, false))
7968 			return -EPERM;
7969 		mode_info->mode = orig_mode;
7970 		mode_info->mode_flag = orig_flags;
7971 	}
7972 	return -EINVAL;
7973 }
7974 /**
7975  * Create a flow.
7976  *
7977  * @see rte_flow_create()
7978  * @see rte_flow_ops
7979  */
7980 struct rte_flow *
7981 mlx5_flow_create(struct rte_eth_dev *dev,
7982 		 const struct rte_flow_attr *attr,
7983 		 const struct rte_flow_item items[],
7984 		 const struct rte_flow_action actions[],
7985 		 struct rte_flow_error *error)
7986 {
7987 	struct mlx5_priv *priv = dev->data->dev_private;
7988 	struct rte_flow_attr *new_attr = (void *)(uintptr_t)attr;
7989 	uint32_t prio = attr->priority;
7990 	uintptr_t flow_idx;
7991 
7992 	/*
7993 	 * If the device is not started yet, it is not allowed to created a
7994 	 * flow from application. PMD default flows and traffic control flows
7995 	 * are not affected.
7996 	 */
7997 	if (unlikely(!dev->data->dev_started)) {
7998 		DRV_LOG(DEBUG, "port %u is not started when "
7999 			"inserting a flow", dev->data->port_id);
8000 		rte_flow_error_set(error, ENODEV,
8001 				   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8002 				   NULL,
8003 				   "port not started");
8004 		return NULL;
8005 	}
8006 	if (unlikely(mlx5_need_cache_flow(priv, attr))) {
8007 		if (attr->transfer ||
8008 				(attr->ingress && !(priv->mode_info.mode_flag &
8009 				RTE_PMD_MLX5_FLOW_ENGINE_FLAG_STANDBY_DUP_INGRESS)))
8010 			new_attr->priority += 1;
8011 	}
8012 	flow_idx = mlx5_flow_list_create(dev, MLX5_FLOW_TYPE_GEN, attr, items, actions,
8013 		true, error);
8014 	if (!flow_idx)
8015 		return NULL;
8016 	if (unlikely(mlx5_need_cache_flow(priv, attr))) {
8017 		if (mlx5_flow_cache_flow_info(dev, attr, prio, items, actions, flow_idx)) {
8018 			mlx5_flow_list_destroy(dev, MLX5_FLOW_TYPE_GEN, flow_idx);
8019 			flow_idx = 0;
8020 		}
8021 	}
8022 	return (void *)(uintptr_t)flow_idx;
8023 }
8024 
8025 uintptr_t
8026 mlx5_flow_list_create(struct rte_eth_dev *dev, enum mlx5_flow_type type,
8027 		      const struct rte_flow_attr *attr,
8028 		      const struct rte_flow_item items[],
8029 		      const struct rte_flow_action actions[],
8030 		      bool external, struct rte_flow_error *error)
8031 {
8032 	const struct mlx5_flow_driver_ops *fops;
8033 	enum mlx5_flow_drv_type drv_type = flow_get_drv_type(dev, attr);
8034 
8035 	fops = flow_get_drv_ops(drv_type);
8036 	return fops->list_create(dev, type, attr, items, actions, external,
8037 		error);
8038 }
8039 
8040 /**
8041  * Destroy a flow in a list.
8042  *
8043  * @param dev
8044  *   Pointer to Ethernet device.
8045  * @param[in] flow_idx
8046  *   Index of flow to destroy.
8047  */
8048 void
8049 flow_legacy_list_destroy(struct rte_eth_dev *dev, enum mlx5_flow_type type,
8050 		     uintptr_t flow_idx)
8051 {
8052 	struct mlx5_priv *priv = dev->data->dev_private;
8053 	struct rte_flow *flow = mlx5_ipool_get(priv->flows[type], (uint32_t)flow_idx);
8054 
8055 	if (!flow)
8056 		return;
8057 	MLX5_ASSERT((type >= MLX5_FLOW_TYPE_CTL) && (type < MLX5_FLOW_TYPE_MAXI));
8058 	MLX5_ASSERT(flow->type == type);
8059 	/*
8060 	 * Update RX queue flags only if port is started, otherwise it is
8061 	 * already clean.
8062 	 */
8063 	if (dev->data->dev_started)
8064 		flow_rxq_flags_trim(dev, flow);
8065 	flow_drv_destroy(dev, flow);
8066 	if (flow->tunnel) {
8067 		struct mlx5_flow_tunnel *tunnel;
8068 
8069 		tunnel = mlx5_find_tunnel_id(dev, flow->tunnel_id);
8070 		RTE_VERIFY(tunnel);
8071 		if (!(rte_atomic_fetch_sub_explicit(&tunnel->refctn, 1,
8072 				rte_memory_order_relaxed) - 1))
8073 			mlx5_flow_tunnel_free(dev, tunnel);
8074 	}
8075 	flow_mreg_del_copy_action(dev, flow);
8076 	mlx5_ipool_free(priv->flows[type], flow_idx);
8077 }
8078 
8079 void
8080 mlx5_flow_list_destroy(struct rte_eth_dev *dev, enum mlx5_flow_type type,
8081 		       uintptr_t flow_idx)
8082 {
8083 	const struct mlx5_flow_driver_ops *fops;
8084 	struct rte_flow_attr attr = { .transfer = 0 };
8085 	enum mlx5_flow_drv_type drv_type = flow_get_drv_type(dev, &attr);
8086 
8087 	fops = flow_get_drv_ops(drv_type);
8088 	fops->list_destroy(dev, type, flow_idx);
8089 }
8090 
8091 /**
8092  * Destroy all flows.
8093  *
8094  * @param dev
8095  *   Pointer to Ethernet device.
8096  * @param type
8097  *   Flow type to be flushed.
8098  * @param active
8099  *   If flushing is called actively.
8100  */
8101 void
8102 mlx5_flow_list_flush(struct rte_eth_dev *dev, enum mlx5_flow_type type,
8103 		     bool active)
8104 {
8105 	struct mlx5_priv *priv = dev->data->dev_private;
8106 	uint32_t num_flushed = 0, fidx = 1;
8107 	struct rte_flow *flow;
8108 	struct rte_pmd_mlx5_flow_engine_mode_info *mode_info = &priv->mode_info;
8109 	struct mlx5_dv_flow_info *flow_info;
8110 
8111 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
8112 	if (priv->sh->config.dv_flow_en == 2 &&
8113 	    type == MLX5_FLOW_TYPE_GEN) {
8114 		flow_hw_q_flow_flush(dev, NULL);
8115 	}
8116 #endif
8117 	MLX5_IPOOL_FOREACH(priv->flows[type], fidx, flow) {
8118 		if (priv->sh->config.dv_flow_en == 2) {
8119 			mlx5_flow_list_destroy(dev, type, (uintptr_t)flow);
8120 		} else {
8121 			mlx5_flow_list_destroy(dev, type, fidx);
8122 		}
8123 		if (unlikely(mlx5_need_cache_flow(priv, NULL) && type == MLX5_FLOW_TYPE_GEN)) {
8124 			flow_info = LIST_FIRST(&mode_info->hot_upgrade);
8125 			while (flow_info) {
8126 				/* Romove the cache flow info. */
8127 				if (flow_info->flow_idx_low_prio == (uint32_t)(uintptr_t)fidx) {
8128 					MLX5_ASSERT(!flow_info->flow_idx_high_prio);
8129 					LIST_REMOVE(flow_info, next);
8130 					mlx5_free(flow_info->items);
8131 					mlx5_free(flow_info->actions);
8132 					mlx5_free(flow_info);
8133 					break;
8134 				}
8135 				flow_info = LIST_NEXT(flow_info, next);
8136 			}
8137 		}
8138 		num_flushed++;
8139 	}
8140 	if (active) {
8141 		DRV_LOG(INFO, "port %u: %u flows flushed before stopping",
8142 			dev->data->port_id, num_flushed);
8143 	}
8144 }
8145 
8146 /**
8147  * Stop all default actions for flows.
8148  *
8149  * @param dev
8150  *   Pointer to Ethernet device.
8151  */
8152 void
8153 mlx5_flow_stop_default(struct rte_eth_dev *dev)
8154 {
8155 	flow_mreg_del_default_copy_action(dev);
8156 	flow_rxq_flags_clear(dev);
8157 }
8158 
8159 /**
8160  * Set rxq flag.
8161  *
8162  * @param[in] dev
8163  *   Pointer to the rte_eth_dev structure.
8164  * @param[in] enable
8165  *   Flag to enable or not.
8166  */
8167 void
8168 flow_hw_rxq_flag_set(struct rte_eth_dev *dev, bool enable)
8169 {
8170 	struct mlx5_priv *priv = dev->data->dev_private;
8171 	unsigned int i;
8172 
8173 	if ((!priv->mark_enabled && !enable) ||
8174 	    (priv->mark_enabled && enable))
8175 		return;
8176 	for (i = 0; i < priv->rxqs_n; ++i) {
8177 		struct mlx5_rxq_ctrl *rxq_ctrl = mlx5_rxq_ctrl_get(dev, i);
8178 
8179 		/* With RXQ start/stop feature, RXQ might be stopped. */
8180 		if (!rxq_ctrl)
8181 			continue;
8182 		rxq_ctrl->rxq.mark = enable;
8183 	}
8184 	priv->mark_enabled = enable;
8185 }
8186 
8187 /**
8188  * Start all default actions for flows.
8189  *
8190  * @param dev
8191  *   Pointer to Ethernet device.
8192  * @return
8193  *   0 on success, a negative errno value otherwise and rte_errno is set.
8194  */
8195 int
8196 mlx5_flow_start_default(struct rte_eth_dev *dev)
8197 {
8198 	struct rte_flow_error error;
8199 
8200 	/* Make sure default copy action (reg_c[0] -> reg_b) is created. */
8201 	return flow_mreg_add_default_copy_action(dev, &error);
8202 }
8203 
8204 /**
8205  * Release key of thread specific flow workspace data.
8206  */
8207 void
8208 flow_release_workspace(void *data)
8209 {
8210 	struct mlx5_flow_workspace *wks = data;
8211 	struct mlx5_flow_workspace *next;
8212 
8213 	while (wks) {
8214 		next = wks->next;
8215 		free(wks);
8216 		wks = next;
8217 	}
8218 }
8219 
8220 /**
8221  * Get thread specific current flow workspace.
8222  *
8223  * @return pointer to thread specific flow workspace data, NULL on error.
8224  */
8225 struct mlx5_flow_workspace*
8226 mlx5_flow_get_thread_workspace(void)
8227 {
8228 	struct mlx5_flow_workspace *data;
8229 
8230 	data = mlx5_flow_os_get_specific_workspace();
8231 	MLX5_ASSERT(data && data->inuse);
8232 	if (!data || !data->inuse)
8233 		DRV_LOG(ERR, "flow workspace not initialized.");
8234 	return data;
8235 }
8236 
8237 /**
8238  * Allocate and init new flow workspace.
8239  *
8240  * @return pointer to flow workspace data, NULL on error.
8241  */
8242 static struct mlx5_flow_workspace*
8243 flow_alloc_thread_workspace(void)
8244 {
8245 	size_t data_size = RTE_ALIGN(sizeof(struct mlx5_flow_workspace), sizeof(long));
8246 	size_t rss_queue_array_size = sizeof(uint16_t) * RTE_ETH_RSS_RETA_SIZE_512;
8247 	struct mlx5_flow_workspace *data = calloc(1, data_size +
8248 						     rss_queue_array_size);
8249 
8250 	if (!data) {
8251 		DRV_LOG(ERR, "Failed to allocate flow workspace memory.");
8252 		return NULL;
8253 	}
8254 	data->rss_desc.queue = RTE_PTR_ADD(data, data_size);
8255 	return data;
8256 }
8257 
8258 /**
8259  * Get new thread specific flow workspace.
8260  *
8261  * If current workspace inuse, create new one and set as current.
8262  *
8263  * @return pointer to thread specific flow workspace data, NULL on error.
8264  */
8265 struct mlx5_flow_workspace*
8266 mlx5_flow_push_thread_workspace(void)
8267 {
8268 	struct mlx5_flow_workspace *curr;
8269 	struct mlx5_flow_workspace *data;
8270 
8271 	curr = mlx5_flow_os_get_specific_workspace();
8272 	if (!curr) {
8273 		data = flow_alloc_thread_workspace();
8274 		if (!data)
8275 			return NULL;
8276 		mlx5_flow_os_workspace_gc_add(data);
8277 	} else if (!curr->inuse) {
8278 		data = curr;
8279 	} else if (curr->next) {
8280 		data = curr->next;
8281 	} else {
8282 		data = flow_alloc_thread_workspace();
8283 		if (!data)
8284 			return NULL;
8285 		curr->next = data;
8286 		data->prev = curr;
8287 	}
8288 	data->inuse = 1;
8289 	data->flow_idx = 0;
8290 	/* Set as current workspace */
8291 	if (mlx5_flow_os_set_specific_workspace(data))
8292 		DRV_LOG(ERR, "Failed to set flow workspace to thread.");
8293 	return data;
8294 }
8295 
8296 /**
8297  * Close current thread specific flow workspace.
8298  *
8299  * If previous workspace available, set it as current.
8300  *
8301  * @return pointer to thread specific flow workspace data, NULL on error.
8302  */
8303 void
8304 mlx5_flow_pop_thread_workspace(void)
8305 {
8306 	struct mlx5_flow_workspace *data = mlx5_flow_get_thread_workspace();
8307 
8308 	if (!data)
8309 		return;
8310 	if (!data->inuse) {
8311 		DRV_LOG(ERR, "Failed to close unused flow workspace.");
8312 		return;
8313 	}
8314 	data->inuse = 0;
8315 	if (!data->prev)
8316 		return;
8317 	if (mlx5_flow_os_set_specific_workspace(data->prev))
8318 		DRV_LOG(ERR, "Failed to set flow workspace to thread.");
8319 }
8320 
8321 /**
8322  * Verify the flow list is empty
8323  *
8324  * @param dev
8325  *  Pointer to Ethernet device.
8326  *
8327  * @return the number of flows not released.
8328  */
8329 int
8330 mlx5_flow_verify(struct rte_eth_dev *dev __rte_unused)
8331 {
8332 	struct mlx5_priv *priv = dev->data->dev_private;
8333 	struct rte_flow *flow;
8334 	uint32_t idx = 0;
8335 	int ret = 0, i;
8336 
8337 	for (i = 0; i < MLX5_FLOW_TYPE_MAXI; i++) {
8338 		MLX5_IPOOL_FOREACH(priv->flows[i], idx, flow) {
8339 			DRV_LOG(DEBUG, "port %u flow %p still referenced",
8340 				dev->data->port_id, (void *)flow);
8341 			ret++;
8342 		}
8343 	}
8344 	return ret;
8345 }
8346 
8347 /**
8348  * Enable default hairpin egress flow.
8349  *
8350  * @param dev
8351  *   Pointer to Ethernet device.
8352  * @param sq_num
8353  *   The SQ hw number.
8354  *
8355  * @return
8356  *   0 on success, a negative errno value otherwise and rte_errno is set.
8357  */
8358 int
8359 mlx5_ctrl_flow_source_queue(struct rte_eth_dev *dev,
8360 			    uint32_t sq_num)
8361 {
8362 	const struct rte_flow_attr attr = {
8363 		.egress = 1,
8364 		.priority = 0,
8365 	};
8366 	struct mlx5_rte_flow_item_sq queue_spec = {
8367 		.queue = sq_num,
8368 	};
8369 	struct mlx5_rte_flow_item_sq queue_mask = {
8370 		.queue = UINT32_MAX,
8371 	};
8372 	struct rte_flow_item items[] = {
8373 		{
8374 			.type = (enum rte_flow_item_type)
8375 				MLX5_RTE_FLOW_ITEM_TYPE_SQ,
8376 			.spec = &queue_spec,
8377 			.last = NULL,
8378 			.mask = &queue_mask,
8379 		},
8380 		{
8381 			.type = RTE_FLOW_ITEM_TYPE_END,
8382 		},
8383 	};
8384 	struct rte_flow_action_jump jump = {
8385 		.group = MLX5_HAIRPIN_TX_TABLE,
8386 	};
8387 	struct rte_flow_action actions[2];
8388 	uint32_t flow_idx;
8389 	struct rte_flow_error error;
8390 
8391 	actions[0].type = RTE_FLOW_ACTION_TYPE_JUMP;
8392 	actions[0].conf = &jump;
8393 	actions[1].type = RTE_FLOW_ACTION_TYPE_END;
8394 	flow_idx = mlx5_flow_list_create(dev, MLX5_FLOW_TYPE_CTL,
8395 				    &attr, items, actions, false, &error);
8396 	if (!flow_idx) {
8397 		DRV_LOG(DEBUG,
8398 			"Failed to create ctrl flow: rte_errno(%d),"
8399 			" type(%d), message(%s)",
8400 			rte_errno, error.type,
8401 			error.message ? error.message : " (no stated reason)");
8402 		return -rte_errno;
8403 	}
8404 	return 0;
8405 }
8406 
8407 /**
8408  * Enable a control flow configured from the control plane.
8409  *
8410  * @param dev
8411  *   Pointer to Ethernet device.
8412  * @param eth_spec
8413  *   An Ethernet flow spec to apply.
8414  * @param eth_mask
8415  *   An Ethernet flow mask to apply.
8416  * @param vlan_spec
8417  *   A VLAN flow spec to apply.
8418  * @param vlan_mask
8419  *   A VLAN flow mask to apply.
8420  *
8421  * @return
8422  *   0 on success, a negative errno value otherwise and rte_errno is set.
8423  */
8424 int
8425 mlx5_ctrl_flow_vlan(struct rte_eth_dev *dev,
8426 		    struct rte_flow_item_eth *eth_spec,
8427 		    struct rte_flow_item_eth *eth_mask,
8428 		    struct rte_flow_item_vlan *vlan_spec,
8429 		    struct rte_flow_item_vlan *vlan_mask)
8430 {
8431 	struct mlx5_priv *priv = dev->data->dev_private;
8432 	const struct rte_flow_attr attr = {
8433 		.ingress = 1,
8434 		.priority = MLX5_FLOW_LOWEST_PRIO_INDICATOR,
8435 	};
8436 	struct rte_flow_item items[] = {
8437 		{
8438 			.type = RTE_FLOW_ITEM_TYPE_ETH,
8439 			.spec = eth_spec,
8440 			.last = NULL,
8441 			.mask = eth_mask,
8442 		},
8443 		{
8444 			.type = (vlan_spec) ? RTE_FLOW_ITEM_TYPE_VLAN :
8445 					      RTE_FLOW_ITEM_TYPE_END,
8446 			.spec = vlan_spec,
8447 			.last = NULL,
8448 			.mask = vlan_mask,
8449 		},
8450 		{
8451 			.type = RTE_FLOW_ITEM_TYPE_END,
8452 		},
8453 	};
8454 	uint16_t queue[priv->reta_idx_n];
8455 	struct rte_flow_action_rss action_rss = {
8456 		.func = RTE_ETH_HASH_FUNCTION_DEFAULT,
8457 		.level = 0,
8458 		.types = priv->rss_conf.rss_hf,
8459 		.key_len = priv->rss_conf.rss_key_len,
8460 		.queue_num = priv->reta_idx_n,
8461 		.key = priv->rss_conf.rss_key,
8462 		.queue = queue,
8463 	};
8464 	struct rte_flow_action actions[] = {
8465 		{
8466 			.type = RTE_FLOW_ACTION_TYPE_RSS,
8467 			.conf = &action_rss,
8468 		},
8469 		{
8470 			.type = RTE_FLOW_ACTION_TYPE_END,
8471 		},
8472 	};
8473 	uint32_t flow_idx;
8474 	struct rte_flow_error error;
8475 	unsigned int i;
8476 
8477 	if (!priv->reta_idx_n || !priv->rxqs_n) {
8478 		return 0;
8479 	}
8480 	if (!(dev->data->dev_conf.rxmode.mq_mode & RTE_ETH_MQ_RX_RSS_FLAG))
8481 		action_rss.types = 0;
8482 	for (i = 0; i != priv->reta_idx_n; ++i)
8483 		queue[i] = (*priv->reta_idx)[i];
8484 	flow_idx = mlx5_flow_list_create(dev, MLX5_FLOW_TYPE_CTL,
8485 				    &attr, items, actions, false, &error);
8486 	if (!flow_idx)
8487 		return -rte_errno;
8488 	return 0;
8489 }
8490 
8491 /**
8492  * Enable a flow control configured from the control plane.
8493  *
8494  * @param dev
8495  *   Pointer to Ethernet device.
8496  * @param eth_spec
8497  *   An Ethernet flow spec to apply.
8498  * @param eth_mask
8499  *   An Ethernet flow mask to apply.
8500  *
8501  * @return
8502  *   0 on success, a negative errno value otherwise and rte_errno is set.
8503  */
8504 int
8505 mlx5_ctrl_flow(struct rte_eth_dev *dev,
8506 	       struct rte_flow_item_eth *eth_spec,
8507 	       struct rte_flow_item_eth *eth_mask)
8508 {
8509 	return mlx5_ctrl_flow_vlan(dev, eth_spec, eth_mask, NULL, NULL);
8510 }
8511 
8512 /**
8513  * Create default miss flow rule matching lacp traffic
8514  *
8515  * @param dev
8516  *   Pointer to Ethernet device.
8517  * @param eth_spec
8518  *   An Ethernet flow spec to apply.
8519  *
8520  * @return
8521  *   0 on success, a negative errno value otherwise and rte_errno is set.
8522  */
8523 int
8524 mlx5_flow_lacp_miss(struct rte_eth_dev *dev)
8525 {
8526 	/*
8527 	 * The LACP matching is done by only using ether type since using
8528 	 * a multicast dst mac causes kernel to give low priority to this flow.
8529 	 */
8530 	static const struct rte_flow_item_eth lacp_spec = {
8531 		.hdr.ether_type = RTE_BE16(0x8809),
8532 	};
8533 	static const struct rte_flow_item_eth lacp_mask = {
8534 		.hdr.ether_type = 0xffff,
8535 	};
8536 	const struct rte_flow_attr attr = {
8537 		.ingress = 1,
8538 	};
8539 	struct rte_flow_item items[] = {
8540 		{
8541 			.type = RTE_FLOW_ITEM_TYPE_ETH,
8542 			.spec = &lacp_spec,
8543 			.mask = &lacp_mask,
8544 		},
8545 		{
8546 			.type = RTE_FLOW_ITEM_TYPE_END,
8547 		},
8548 	};
8549 	struct rte_flow_action actions[] = {
8550 		{
8551 			.type = (enum rte_flow_action_type)
8552 				MLX5_RTE_FLOW_ACTION_TYPE_DEFAULT_MISS,
8553 		},
8554 		{
8555 			.type = RTE_FLOW_ACTION_TYPE_END,
8556 		},
8557 	};
8558 	struct rte_flow_error error;
8559 	uint32_t flow_idx = mlx5_flow_list_create(dev, MLX5_FLOW_TYPE_CTL,
8560 					&attr, items, actions,
8561 					false, &error);
8562 
8563 	if (!flow_idx)
8564 		return -rte_errno;
8565 	return 0;
8566 }
8567 
8568 /**
8569  * Destroy a flow.
8570  *
8571  * @see rte_flow_destroy()
8572  * @see rte_flow_ops
8573  */
8574 int
8575 mlx5_flow_destroy(struct rte_eth_dev *dev,
8576 		  struct rte_flow *flow,
8577 		  struct rte_flow_error *error __rte_unused)
8578 {
8579 	struct mlx5_priv *priv = dev->data->dev_private;
8580 	struct rte_pmd_mlx5_flow_engine_mode_info *mode_info = &priv->mode_info;
8581 	struct mlx5_dv_flow_info *flow_info;
8582 
8583 	mlx5_flow_list_destroy(dev, MLX5_FLOW_TYPE_GEN,
8584 				(uintptr_t)(void *)flow);
8585 	if (unlikely(mlx5_need_cache_flow(priv, NULL))) {
8586 		flow_info = LIST_FIRST(&mode_info->hot_upgrade);
8587 		while (flow_info) {
8588 			/* Romove the cache flow info. */
8589 			if (flow_info->flow_idx_low_prio == (uint32_t)(uintptr_t)flow) {
8590 				MLX5_ASSERT(!flow_info->flow_idx_high_prio);
8591 				LIST_REMOVE(flow_info, next);
8592 				mlx5_free(flow_info->items);
8593 				mlx5_free(flow_info->actions);
8594 				mlx5_free(flow_info);
8595 				break;
8596 			}
8597 			flow_info = LIST_NEXT(flow_info, next);
8598 		}
8599 	}
8600 	return 0;
8601 }
8602 
8603 /**
8604  * Destroy all flows.
8605  *
8606  * @see rte_flow_flush()
8607  * @see rte_flow_ops
8608  */
8609 int
8610 mlx5_flow_flush(struct rte_eth_dev *dev,
8611 		struct rte_flow_error *error __rte_unused)
8612 {
8613 	mlx5_flow_list_flush(dev, MLX5_FLOW_TYPE_GEN, false);
8614 	return 0;
8615 }
8616 
8617 /**
8618  * Isolated mode.
8619  *
8620  * @see rte_flow_isolate()
8621  * @see rte_flow_ops
8622  */
8623 int
8624 mlx5_flow_isolate(struct rte_eth_dev *dev,
8625 		  int enable,
8626 		  struct rte_flow_error *error)
8627 {
8628 	struct mlx5_priv *priv = dev->data->dev_private;
8629 
8630 	if (dev->data->dev_started) {
8631 		rte_flow_error_set(error, EBUSY,
8632 				   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8633 				   NULL,
8634 				   "port must be stopped first");
8635 		return -rte_errno;
8636 	}
8637 	if (!enable && !priv->sh->config.repr_matching)
8638 		return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
8639 					  "isolated mode cannot be disabled when "
8640 					  "representor matching is disabled");
8641 	priv->isolated = !!enable;
8642 	if (enable)
8643 		dev->dev_ops = &mlx5_dev_ops_isolate;
8644 	else
8645 		dev->dev_ops = &mlx5_dev_ops;
8646 
8647 	dev->rx_descriptor_status = mlx5_rx_descriptor_status;
8648 	dev->tx_descriptor_status = mlx5_tx_descriptor_status;
8649 
8650 	return 0;
8651 }
8652 
8653 /**
8654  * Query a flow.
8655  *
8656  * @see rte_flow_query()
8657  * @see rte_flow_ops
8658  */
8659 static int
8660 flow_drv_query(struct rte_eth_dev *dev,
8661 	       struct rte_flow *eflow,
8662 	       const struct rte_flow_action *actions,
8663 	       void *data,
8664 	       struct rte_flow_error *error)
8665 {
8666 	struct mlx5_priv *priv = dev->data->dev_private;
8667 	const struct mlx5_flow_driver_ops *fops;
8668 	struct rte_flow *flow = NULL;
8669 	enum mlx5_flow_drv_type ftype = MLX5_FLOW_TYPE_MIN;
8670 
8671 	if (priv->sh->config.dv_flow_en == 2) {
8672 #ifdef HAVE_MLX5_HWS_SUPPORT
8673 		flow = eflow;
8674 		ftype = MLX5_FLOW_TYPE_HW;
8675 #endif
8676 	} else {
8677 		flow = (struct rte_flow *)mlx5_ipool_get(priv->flows[MLX5_FLOW_TYPE_GEN],
8678 				(uintptr_t)(void *)eflow);
8679 	}
8680 	if (!flow) {
8681 		return rte_flow_error_set(error, ENOENT,
8682 			  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8683 			  NULL,
8684 			  "invalid flow handle");
8685 	}
8686 	if (ftype == MLX5_FLOW_TYPE_MIN)
8687 		ftype = flow->drv_type;
8688 	MLX5_ASSERT(ftype > MLX5_FLOW_TYPE_MIN && ftype < MLX5_FLOW_TYPE_MAX);
8689 	fops = flow_get_drv_ops(ftype);
8690 
8691 	return fops->query(dev, flow, actions, data, error);
8692 }
8693 
8694 /**
8695  * Query a flow.
8696  *
8697  * @see rte_flow_query()
8698  * @see rte_flow_ops
8699  */
8700 int
8701 mlx5_flow_query(struct rte_eth_dev *dev,
8702 		struct rte_flow *flow,
8703 		const struct rte_flow_action *actions,
8704 		void *data,
8705 		struct rte_flow_error *error)
8706 {
8707 	int ret;
8708 
8709 	ret = flow_drv_query(dev, flow, actions, data,
8710 			     error);
8711 	if (ret < 0)
8712 		return ret;
8713 	return 0;
8714 }
8715 
8716 /**
8717  * Get rte_flow callbacks.
8718  *
8719  * @param dev
8720  *   Pointer to Ethernet device structure.
8721  * @param ops
8722  *   Pointer to operation-specific structure.
8723  *
8724  * @return 0
8725  */
8726 int
8727 mlx5_flow_ops_get(struct rte_eth_dev *dev __rte_unused,
8728 		  const struct rte_flow_ops **ops)
8729 {
8730 	*ops = &mlx5_flow_ops;
8731 	return 0;
8732 }
8733 
8734 /**
8735  * Validate meter policy actions.
8736  * Dispatcher for action type specific validation.
8737  *
8738  * @param[in] dev
8739  *   Pointer to the Ethernet device structure.
8740  * @param[in] action
8741  *   The meter policy action object to validate.
8742  * @param[in] attr
8743  *   Attributes of flow to determine steering domain.
8744  * @param[out] is_rss
8745  *   Is RSS or not.
8746  * @param[out] domain_bitmap
8747  *   Domain bitmap.
8748  * @param[out] is_def_policy
8749  *   Is default policy or not.
8750  * @param[out] error
8751  *   Perform verbose error reporting if not NULL. Initialized in case of
8752  *   error only.
8753  *
8754  * @return
8755  *   0 on success, otherwise negative errno value.
8756  */
8757 int
8758 mlx5_flow_validate_mtr_acts(struct rte_eth_dev *dev,
8759 			const struct rte_flow_action *actions[RTE_COLORS],
8760 			struct rte_flow_attr *attr,
8761 			bool *is_rss,
8762 			uint8_t *domain_bitmap,
8763 			uint8_t *policy_mode,
8764 			struct rte_mtr_error *error)
8765 {
8766 	const struct mlx5_flow_driver_ops *fops;
8767 
8768 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
8769 	return fops->validate_mtr_acts(dev, actions, attr, is_rss,
8770 				       domain_bitmap, policy_mode, error);
8771 }
8772 
8773 /**
8774  * Destroy the meter table set.
8775  *
8776  * @param[in] dev
8777  *   Pointer to Ethernet device.
8778  * @param[in] mtr_policy
8779  *   Meter policy struct.
8780  */
8781 void
8782 mlx5_flow_destroy_mtr_acts(struct rte_eth_dev *dev,
8783 		      struct mlx5_flow_meter_policy *mtr_policy)
8784 {
8785 	const struct mlx5_flow_driver_ops *fops;
8786 
8787 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
8788 	fops->destroy_mtr_acts(dev, mtr_policy);
8789 }
8790 
8791 /**
8792  * Create policy action, lock free,
8793  * (mutex should be acquired by caller).
8794  * Dispatcher for action type specific call.
8795  *
8796  * @param[in] dev
8797  *   Pointer to the Ethernet device structure.
8798  * @param[in] mtr_policy
8799  *   Meter policy struct.
8800  * @param[in] action
8801  *   Action specification used to create meter actions.
8802  * @param[in] attr
8803  *   Flow rule attributes.
8804  * @param[out] error
8805  *   Perform verbose error reporting if not NULL. Initialized in case of
8806  *   error only.
8807  *
8808  * @return
8809  *   0 on success, otherwise negative errno value.
8810  */
8811 int
8812 mlx5_flow_create_mtr_acts(struct rte_eth_dev *dev,
8813 		      struct mlx5_flow_meter_policy *mtr_policy,
8814 		      const struct rte_flow_action *actions[RTE_COLORS],
8815 		      struct rte_flow_attr *attr,
8816 		      struct rte_mtr_error *error)
8817 {
8818 	const struct mlx5_flow_driver_ops *fops;
8819 
8820 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
8821 	return fops->create_mtr_acts(dev, mtr_policy, actions, attr, error);
8822 }
8823 
8824 /**
8825  * Create policy rules, lock free,
8826  * (mutex should be acquired by caller).
8827  * Dispatcher for action type specific call.
8828  *
8829  * @param[in] dev
8830  *   Pointer to the Ethernet device structure.
8831  * @param[in] mtr_policy
8832  *   Meter policy struct.
8833  *
8834  * @return
8835  *   0 on success, -1 otherwise.
8836  */
8837 int
8838 mlx5_flow_create_policy_rules(struct rte_eth_dev *dev,
8839 			     struct mlx5_flow_meter_policy *mtr_policy)
8840 {
8841 	const struct mlx5_flow_driver_ops *fops;
8842 
8843 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
8844 	return fops->create_policy_rules(dev, mtr_policy);
8845 }
8846 
8847 /**
8848  * Destroy policy rules, lock free,
8849  * (mutex should be acquired by caller).
8850  * Dispatcher for action type specific call.
8851  *
8852  * @param[in] dev
8853  *   Pointer to the Ethernet device structure.
8854  * @param[in] mtr_policy
8855  *   Meter policy struct.
8856  */
8857 void
8858 mlx5_flow_destroy_policy_rules(struct rte_eth_dev *dev,
8859 			     struct mlx5_flow_meter_policy *mtr_policy)
8860 {
8861 	const struct mlx5_flow_driver_ops *fops;
8862 
8863 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
8864 	fops->destroy_policy_rules(dev, mtr_policy);
8865 }
8866 
8867 /**
8868  * Destroy the default policy table set.
8869  *
8870  * @param[in] dev
8871  *   Pointer to Ethernet device.
8872  */
8873 void
8874 mlx5_flow_destroy_def_policy(struct rte_eth_dev *dev)
8875 {
8876 	const struct mlx5_flow_driver_ops *fops;
8877 
8878 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
8879 	fops->destroy_def_policy(dev);
8880 }
8881 
8882 /**
8883  * Destroy the default policy table set.
8884  *
8885  * @param[in] dev
8886  *   Pointer to Ethernet device.
8887  *
8888  * @return
8889  *   0 on success, -1 otherwise.
8890  */
8891 int
8892 mlx5_flow_create_def_policy(struct rte_eth_dev *dev)
8893 {
8894 	const struct mlx5_flow_driver_ops *fops;
8895 
8896 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
8897 	return fops->create_def_policy(dev);
8898 }
8899 
8900 /**
8901  * Create the needed meter and suffix tables.
8902  *
8903  * @param[in] dev
8904  *   Pointer to Ethernet device.
8905  *
8906  * @return
8907  *   0 on success, -1 otherwise.
8908  */
8909 int
8910 mlx5_flow_create_mtr_tbls(struct rte_eth_dev *dev,
8911 			struct mlx5_flow_meter_info *fm,
8912 			uint32_t mtr_idx,
8913 			uint8_t domain_bitmap)
8914 {
8915 	const struct mlx5_flow_driver_ops *fops;
8916 
8917 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
8918 	return fops->create_mtr_tbls(dev, fm, mtr_idx, domain_bitmap);
8919 }
8920 
8921 /**
8922  * Destroy the meter table set.
8923  *
8924  * @param[in] dev
8925  *   Pointer to Ethernet device.
8926  * @param[in] tbl
8927  *   Pointer to the meter table set.
8928  */
8929 void
8930 mlx5_flow_destroy_mtr_tbls(struct rte_eth_dev *dev,
8931 			   struct mlx5_flow_meter_info *fm)
8932 {
8933 	const struct mlx5_flow_driver_ops *fops;
8934 
8935 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
8936 	fops->destroy_mtr_tbls(dev, fm);
8937 }
8938 
8939 /**
8940  * Destroy the global meter drop table.
8941  *
8942  * @param[in] dev
8943  *   Pointer to Ethernet device.
8944  */
8945 void
8946 mlx5_flow_destroy_mtr_drop_tbls(struct rte_eth_dev *dev)
8947 {
8948 	const struct mlx5_flow_driver_ops *fops;
8949 
8950 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
8951 	fops->destroy_mtr_drop_tbls(dev);
8952 }
8953 
8954 /**
8955  * Destroy the sub policy table with RX queue.
8956  *
8957  * @param[in] dev
8958  *   Pointer to Ethernet device.
8959  * @param[in] mtr_policy
8960  *   Pointer to meter policy table.
8961  */
8962 void
8963 mlx5_flow_destroy_sub_policy_with_rxq(struct rte_eth_dev *dev,
8964 		struct mlx5_flow_meter_policy *mtr_policy)
8965 {
8966 	const struct mlx5_flow_driver_ops *fops;
8967 
8968 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
8969 	fops->destroy_sub_policy_with_rxq(dev, mtr_policy);
8970 }
8971 
8972 /**
8973  * Allocate the needed aso flow meter id.
8974  *
8975  * @param[in] dev
8976  *   Pointer to Ethernet device.
8977  *
8978  * @return
8979  *   Index to aso flow meter on success, NULL otherwise.
8980  */
8981 uint32_t
8982 mlx5_flow_mtr_alloc(struct rte_eth_dev *dev)
8983 {
8984 	const struct mlx5_flow_driver_ops *fops;
8985 
8986 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
8987 	return fops->create_meter(dev);
8988 }
8989 
8990 /**
8991  * Free the aso flow meter id.
8992  *
8993  * @param[in] dev
8994  *   Pointer to Ethernet device.
8995  * @param[in] mtr_idx
8996  *  Index to aso flow meter to be free.
8997  *
8998  * @return
8999  *   0 on success.
9000  */
9001 void
9002 mlx5_flow_mtr_free(struct rte_eth_dev *dev, uint32_t mtr_idx)
9003 {
9004 	const struct mlx5_flow_driver_ops *fops;
9005 
9006 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
9007 	fops->free_meter(dev, mtr_idx);
9008 }
9009 
9010 /**
9011  * Allocate a counter.
9012  *
9013  * @param[in] dev
9014  *   Pointer to Ethernet device structure.
9015  *
9016  * @return
9017  *   Index to allocated counter  on success, 0 otherwise.
9018  */
9019 uint32_t
9020 mlx5_counter_alloc(struct rte_eth_dev *dev)
9021 {
9022 	struct rte_flow_attr attr = { .transfer = 0 };
9023 
9024 	return flow_get_drv_ops(flow_get_drv_type(dev, &attr))->counter_alloc
9025 		(dev);
9026 }
9027 
9028 /**
9029  * Free a counter.
9030  *
9031  * @param[in] dev
9032  *   Pointer to Ethernet device structure.
9033  * @param[in] cnt
9034  *   Index to counter to be free.
9035  */
9036 void
9037 mlx5_counter_free(struct rte_eth_dev *dev, uint32_t cnt)
9038 {
9039 	struct rte_flow_attr attr = { .transfer = 0 };
9040 
9041 	flow_get_drv_ops(flow_get_drv_type(dev, &attr))->counter_free(dev, cnt);
9042 }
9043 
9044 /**
9045  * Query counter statistics.
9046  *
9047  * @param[in] dev
9048  *   Pointer to Ethernet device structure.
9049  * @param[in] cnt
9050  *   Index to counter to query.
9051  * @param[in] clear
9052  *   Set to clear counter statistics.
9053  * @param[out] pkts
9054  *   The counter hits packets number to save.
9055  * @param[out] bytes
9056  *   The counter hits bytes number to save.
9057  *
9058  * @return
9059  *   0 on success, a negative errno value otherwise.
9060  */
9061 int
9062 mlx5_counter_query(struct rte_eth_dev *dev, uint32_t cnt,
9063 		   bool clear, uint64_t *pkts, uint64_t *bytes, void **action)
9064 {
9065 	struct rte_flow_attr attr = { .transfer = 0 };
9066 
9067 	return flow_get_drv_ops(flow_get_drv_type(dev, &attr))->counter_query
9068 		(dev, cnt, clear, pkts, bytes, action);
9069 }
9070 
9071 /**
9072  * Get information about HWS pre-configurable resources.
9073  *
9074  * @param[in] dev
9075  *   Pointer to the rte_eth_dev structure.
9076  * @param[out] port_info
9077  *   Pointer to port information.
9078  * @param[out] queue_info
9079  *   Pointer to queue information.
9080  * @param[out] error
9081  *   Pointer to error structure.
9082  *
9083  * @return
9084  *   0 on success, a negative errno value otherwise and rte_errno is set.
9085  */
9086 static int
9087 mlx5_flow_info_get(struct rte_eth_dev *dev,
9088 		   struct rte_flow_port_info *port_info,
9089 		   struct rte_flow_queue_info *queue_info,
9090 		   struct rte_flow_error *error)
9091 {
9092 	const struct mlx5_flow_driver_ops *fops;
9093 	struct rte_flow_attr attr = {0};
9094 
9095 	if (flow_get_drv_type(dev, &attr) != MLX5_FLOW_TYPE_HW)
9096 		return rte_flow_error_set(error, ENOTSUP,
9097 				RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
9098 				NULL,
9099 				"info get with incorrect steering mode");
9100 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_HW);
9101 	return fops->info_get(dev, port_info, queue_info, error);
9102 }
9103 
9104 /**
9105  * Configure port HWS resources.
9106  *
9107  * @param[in] dev
9108  *   Pointer to the rte_eth_dev structure.
9109  * @param[in] port_attr
9110  *   Port configuration attributes.
9111  * @param[in] nb_queue
9112  *   Number of queue.
9113  * @param[in] queue_attr
9114  *   Array that holds attributes for each flow queue.
9115  * @param[out] error
9116  *   Pointer to error structure.
9117  *
9118  * @return
9119  *   0 on success, a negative errno value otherwise and rte_errno is set.
9120  */
9121 static int
9122 mlx5_flow_port_configure(struct rte_eth_dev *dev,
9123 			 const struct rte_flow_port_attr *port_attr,
9124 			 uint16_t nb_queue,
9125 			 const struct rte_flow_queue_attr *queue_attr[],
9126 			 struct rte_flow_error *error)
9127 {
9128 	const struct mlx5_flow_driver_ops *fops;
9129 	struct rte_flow_attr attr = {0};
9130 
9131 	if (flow_get_drv_type(dev, &attr) != MLX5_FLOW_TYPE_HW)
9132 		return rte_flow_error_set(error, ENOTSUP,
9133 				RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
9134 				NULL,
9135 				"port configure with incorrect steering mode");
9136 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_HW);
9137 	return fops->configure(dev, port_attr, nb_queue, queue_attr, error);
9138 }
9139 
9140 /**
9141  * Validate item template.
9142  *
9143  * @param[in] dev
9144  *   Pointer to the rte_eth_dev structure.
9145  * @param[in] attr
9146  *   Pointer to the item template attributes.
9147  * @param[in] items
9148  *   The template item pattern.
9149  * @param[out] error
9150  *   Pointer to error structure.
9151  *
9152  * @return
9153  *   0 on success, a negative errno value otherwise and rte_errno is set.
9154  */
9155 int
9156 mlx5_flow_pattern_validate(struct rte_eth_dev *dev,
9157 		const struct rte_flow_pattern_template_attr *attr,
9158 		const struct rte_flow_item items[],
9159 		struct rte_flow_error *error)
9160 {
9161 	const struct mlx5_flow_driver_ops *fops;
9162 	struct rte_flow_attr fattr = {0};
9163 	uint64_t item_flags = 0;
9164 
9165 	if (flow_get_drv_type(dev, &fattr) != MLX5_FLOW_TYPE_HW) {
9166 		rte_flow_error_set(error, ENOTSUP,
9167 			RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
9168 			"pattern validate with incorrect steering mode");
9169 		return -ENOTSUP;
9170 	}
9171 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_HW);
9172 	return fops->pattern_validate(dev, attr, items, &item_flags, error);
9173 }
9174 
9175 /**
9176  * Create flow item template.
9177  *
9178  * @param[in] dev
9179  *   Pointer to the rte_eth_dev structure.
9180  * @param[in] attr
9181  *   Pointer to the item template attributes.
9182  * @param[in] items
9183  *   The template item pattern.
9184  * @param[out] error
9185  *   Pointer to error structure.
9186  *
9187  * @return
9188  *   0 on success, a negative errno value otherwise and rte_errno is set.
9189  */
9190 static struct rte_flow_pattern_template *
9191 mlx5_flow_pattern_template_create(struct rte_eth_dev *dev,
9192 		const struct rte_flow_pattern_template_attr *attr,
9193 		const struct rte_flow_item items[],
9194 		struct rte_flow_error *error)
9195 {
9196 	const struct mlx5_flow_driver_ops *fops;
9197 	struct rte_flow_attr fattr = {0};
9198 
9199 	if (flow_get_drv_type(dev, &fattr) != MLX5_FLOW_TYPE_HW) {
9200 		rte_flow_error_set(error, ENOTSUP,
9201 				RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
9202 				NULL,
9203 				"pattern create with incorrect steering mode");
9204 		return NULL;
9205 	}
9206 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_HW);
9207 	return fops->pattern_template_create(dev, attr, items, error);
9208 }
9209 
9210 /**
9211  * Destroy flow item template.
9212  *
9213  * @param[in] dev
9214  *   Pointer to the rte_eth_dev structure.
9215  * @param[in] template
9216  *   Pointer to the item template to be destroyed.
9217  * @param[out] error
9218  *   Pointer to error structure.
9219  *
9220  * @return
9221  *   0 on success, a negative errno value otherwise and rte_errno is set.
9222  */
9223 static int
9224 mlx5_flow_pattern_template_destroy(struct rte_eth_dev *dev,
9225 				   struct rte_flow_pattern_template *template,
9226 				   struct rte_flow_error *error)
9227 {
9228 	const struct mlx5_flow_driver_ops *fops;
9229 	struct rte_flow_attr attr = {0};
9230 
9231 	if (flow_get_drv_type(dev, &attr) != MLX5_FLOW_TYPE_HW)
9232 		return rte_flow_error_set(error, ENOTSUP,
9233 				RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
9234 				NULL,
9235 				"pattern destroy with incorrect steering mode");
9236 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_HW);
9237 	return fops->pattern_template_destroy(dev, template, error);
9238 }
9239 
9240 /**
9241  * Validate flow actions template.
9242  *
9243  * @param[in] dev
9244  *   Pointer to the rte_eth_dev structure.
9245  * @param[in] attr
9246  *   Pointer to the action template attributes.
9247  * @param[in] actions
9248  *   Associated actions (list terminated by the END action).
9249  * @param[in] masks
9250  *   List of actions that marks which of the action's member is constant.
9251  * @param[out] error
9252  *   Pointer to error structure.
9253  *
9254  * @return
9255  *   0 on success, a negative errno value otherwise and rte_errno is set.
9256  */
9257 int
9258 mlx5_flow_actions_validate(struct rte_eth_dev *dev,
9259 			const struct rte_flow_actions_template_attr *attr,
9260 			const struct rte_flow_action actions[],
9261 			const struct rte_flow_action masks[],
9262 			struct rte_flow_error *error)
9263 {
9264 	const struct mlx5_flow_driver_ops *fops;
9265 	struct rte_flow_attr fattr = {0};
9266 
9267 	if (flow_get_drv_type(dev, &fattr) != MLX5_FLOW_TYPE_HW) {
9268 		rte_flow_error_set(error, ENOTSUP,
9269 			RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
9270 			"actions validate with incorrect steering mode");
9271 		return -ENOTSUP;
9272 	}
9273 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_HW);
9274 	return fops->actions_validate(dev, attr, actions, masks, error);
9275 }
9276 
9277 /**
9278  * Create flow item template.
9279  *
9280  * @param[in] dev
9281  *   Pointer to the rte_eth_dev structure.
9282  * @param[in] attr
9283  *   Pointer to the action template attributes.
9284  * @param[in] actions
9285  *   Associated actions (list terminated by the END action).
9286  * @param[in] masks
9287  *   List of actions that marks which of the action's member is constant.
9288  * @param[out] error
9289  *   Pointer to error structure.
9290  *
9291  * @return
9292  *   0 on success, a negative errno value otherwise and rte_errno is set.
9293  */
9294 static struct rte_flow_actions_template *
9295 mlx5_flow_actions_template_create(struct rte_eth_dev *dev,
9296 			const struct rte_flow_actions_template_attr *attr,
9297 			const struct rte_flow_action actions[],
9298 			const struct rte_flow_action masks[],
9299 			struct rte_flow_error *error)
9300 {
9301 	const struct mlx5_flow_driver_ops *fops;
9302 	struct rte_flow_attr fattr = {0};
9303 
9304 	if (flow_get_drv_type(dev, &fattr) != MLX5_FLOW_TYPE_HW) {
9305 		rte_flow_error_set(error, ENOTSUP,
9306 				RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
9307 				NULL,
9308 				"action create with incorrect steering mode");
9309 		return NULL;
9310 	}
9311 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_HW);
9312 	return fops->actions_template_create(dev, attr, actions, masks, error);
9313 }
9314 
9315 /**
9316  * Destroy flow action template.
9317  *
9318  * @param[in] dev
9319  *   Pointer to the rte_eth_dev structure.
9320  * @param[in] template
9321  *   Pointer to the action template to be destroyed.
9322  * @param[out] error
9323  *   Pointer to error structure.
9324  *
9325  * @return
9326  *   0 on success, a negative errno value otherwise and rte_errno is set.
9327  */
9328 static int
9329 mlx5_flow_actions_template_destroy(struct rte_eth_dev *dev,
9330 				   struct rte_flow_actions_template *template,
9331 				   struct rte_flow_error *error)
9332 {
9333 	const struct mlx5_flow_driver_ops *fops;
9334 	struct rte_flow_attr attr = {0};
9335 
9336 	if (flow_get_drv_type(dev, &attr) != MLX5_FLOW_TYPE_HW)
9337 		return rte_flow_error_set(error, ENOTSUP,
9338 				RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
9339 				NULL,
9340 				"action destroy with incorrect steering mode");
9341 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_HW);
9342 	return fops->actions_template_destroy(dev, template, error);
9343 }
9344 
9345 /**
9346  * Create flow table.
9347  *
9348  * @param[in] dev
9349  *   Pointer to the rte_eth_dev structure.
9350  * @param[in] attr
9351  *   Pointer to the table attributes.
9352  * @param[in] item_templates
9353  *   Item template array to be binded to the table.
9354  * @param[in] nb_item_templates
9355  *   Number of item template.
9356  * @param[in] action_templates
9357  *   Action template array to be binded to the table.
9358  * @param[in] nb_action_templates
9359  *   Number of action template.
9360  * @param[out] error
9361  *   Pointer to error structure.
9362  *
9363  * @return
9364  *    Table on success, NULL otherwise and rte_errno is set.
9365  */
9366 static struct rte_flow_template_table *
9367 mlx5_flow_table_create(struct rte_eth_dev *dev,
9368 		       const struct rte_flow_template_table_attr *attr,
9369 		       struct rte_flow_pattern_template *item_templates[],
9370 		       uint8_t nb_item_templates,
9371 		       struct rte_flow_actions_template *action_templates[],
9372 		       uint8_t nb_action_templates,
9373 		       struct rte_flow_error *error)
9374 {
9375 	const struct mlx5_flow_driver_ops *fops;
9376 	struct rte_flow_attr fattr = {0};
9377 
9378 	if (flow_get_drv_type(dev, &fattr) != MLX5_FLOW_TYPE_HW) {
9379 		rte_flow_error_set(error, ENOTSUP,
9380 				RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
9381 				NULL,
9382 				"table create with incorrect steering mode");
9383 		return NULL;
9384 	}
9385 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_HW);
9386 	return fops->template_table_create(dev,
9387 					   attr,
9388 					   item_templates,
9389 					   nb_item_templates,
9390 					   action_templates,
9391 					   nb_action_templates,
9392 					   error);
9393 }
9394 
9395 /**
9396  * PMD destroy flow table.
9397  *
9398  * @param[in] dev
9399  *   Pointer to the rte_eth_dev structure.
9400  * @param[in] table
9401  *   Pointer to the table to be destroyed.
9402  * @param[out] error
9403  *   Pointer to error structure.
9404  *
9405  * @return
9406  *   0 on success, a negative errno value otherwise and rte_errno is set.
9407  */
9408 static int
9409 mlx5_flow_table_destroy(struct rte_eth_dev *dev,
9410 			struct rte_flow_template_table *table,
9411 			struct rte_flow_error *error)
9412 {
9413 	const struct mlx5_flow_driver_ops *fops;
9414 	struct rte_flow_attr attr = {0};
9415 
9416 	if (flow_get_drv_type(dev, &attr) != MLX5_FLOW_TYPE_HW)
9417 		return rte_flow_error_set(error, ENOTSUP,
9418 				RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
9419 				NULL,
9420 				"table destroy with incorrect steering mode");
9421 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_HW);
9422 	return fops->template_table_destroy(dev, table, error);
9423 }
9424 
9425 /**
9426  * PMD group set miss actions.
9427  *
9428  * @param[in] dev
9429  *   Pointer to the rte_eth_dev structure.
9430  * @param[in] attr
9431  *   Pointer to group attributes
9432  * @param[in] actions
9433  *   Array of actions
9434  * @param[out] error
9435  *   Pointer to error structure.
9436  *
9437  * @return
9438  *   0 on success, a negative errno value otherwise and rte_errno is set.
9439  */
9440 static int
9441 mlx5_flow_group_set_miss_actions(struct rte_eth_dev *dev,
9442 				 uint32_t group_id,
9443 				 const struct rte_flow_group_attr *attr,
9444 				 const struct rte_flow_action actions[],
9445 				 struct rte_flow_error *error)
9446 {
9447 	const struct mlx5_flow_driver_ops *fops;
9448 	struct rte_flow_attr fattr = {0};
9449 
9450 	if (flow_get_drv_type(dev, &fattr) != MLX5_FLOW_TYPE_HW)
9451 		return rte_flow_error_set(error, ENOTSUP,
9452 				RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
9453 				NULL,
9454 				"group set miss actions with incorrect steering mode");
9455 	fops = flow_get_drv_ops(MLX5_FLOW_TYPE_HW);
9456 	return fops->group_set_miss_actions(dev, group_id, attr, actions, error);
9457 }
9458 
9459 /**
9460  * Allocate a new memory for the counter values wrapped by all the needed
9461  * management.
9462  *
9463  * @param[in] sh
9464  *   Pointer to mlx5_dev_ctx_shared object.
9465  *
9466  * @return
9467  *   0 on success, a negative errno value otherwise.
9468  */
9469 static int
9470 mlx5_flow_create_counter_stat_mem_mng(struct mlx5_dev_ctx_shared *sh)
9471 {
9472 	struct mlx5_counter_stats_mem_mng *mem_mng;
9473 	volatile struct flow_counter_stats *raw_data;
9474 	int raws_n = MLX5_CNT_MR_ALLOC_BULK + MLX5_MAX_PENDING_QUERIES;
9475 	int size = (sizeof(struct flow_counter_stats) *
9476 			MLX5_COUNTERS_PER_POOL +
9477 			sizeof(struct mlx5_counter_stats_raw)) * raws_n +
9478 			sizeof(struct mlx5_counter_stats_mem_mng);
9479 	size_t pgsize = rte_mem_page_size();
9480 	uint8_t *mem;
9481 	int ret;
9482 	int i;
9483 
9484 	if (pgsize == (size_t)-1) {
9485 		DRV_LOG(ERR, "Failed to get mem page size");
9486 		rte_errno = ENOMEM;
9487 		return -ENOMEM;
9488 	}
9489 	mem = mlx5_malloc(MLX5_MEM_ZERO, size, pgsize, SOCKET_ID_ANY);
9490 	if (!mem) {
9491 		rte_errno = ENOMEM;
9492 		return -ENOMEM;
9493 	}
9494 	mem_mng = (struct mlx5_counter_stats_mem_mng *)(mem + size) - 1;
9495 	size = sizeof(*raw_data) * MLX5_COUNTERS_PER_POOL * raws_n;
9496 	ret = mlx5_os_wrapped_mkey_create(sh->cdev->ctx, sh->cdev->pd,
9497 					  sh->cdev->pdn, mem, size,
9498 					  &mem_mng->wm);
9499 	if (ret) {
9500 		rte_errno = errno;
9501 		mlx5_free(mem);
9502 		return -rte_errno;
9503 	}
9504 	mem_mng->raws = (struct mlx5_counter_stats_raw *)(mem + size);
9505 	raw_data = (volatile struct flow_counter_stats *)mem;
9506 	for (i = 0; i < raws_n; ++i) {
9507 		mem_mng->raws[i].mem_mng = mem_mng;
9508 		mem_mng->raws[i].data = raw_data + i * MLX5_COUNTERS_PER_POOL;
9509 	}
9510 	for (i = 0; i < MLX5_MAX_PENDING_QUERIES; ++i)
9511 		LIST_INSERT_HEAD(&sh->sws_cmng.free_stat_raws,
9512 				 mem_mng->raws + MLX5_CNT_MR_ALLOC_BULK + i,
9513 				 next);
9514 	LIST_INSERT_HEAD(&sh->sws_cmng.mem_mngs, mem_mng, next);
9515 	sh->sws_cmng.mem_mng = mem_mng;
9516 	return 0;
9517 }
9518 
9519 /**
9520  * Set the statistic memory to the new counter pool.
9521  *
9522  * @param[in] sh
9523  *   Pointer to mlx5_dev_ctx_shared object.
9524  * @param[in] pool
9525  *   Pointer to the pool to set the statistic memory.
9526  *
9527  * @return
9528  *   0 on success, a negative errno value otherwise.
9529  */
9530 static int
9531 mlx5_flow_set_counter_stat_mem(struct mlx5_dev_ctx_shared *sh,
9532 			       struct mlx5_flow_counter_pool *pool)
9533 {
9534 	struct mlx5_flow_counter_mng *cmng = &sh->sws_cmng;
9535 	/* Resize statistic memory once used out. */
9536 	if (!(pool->index % MLX5_CNT_MR_ALLOC_BULK) &&
9537 	    mlx5_flow_create_counter_stat_mem_mng(sh)) {
9538 		DRV_LOG(ERR, "Cannot resize counter stat mem.");
9539 		return -1;
9540 	}
9541 	rte_spinlock_lock(&pool->sl);
9542 	pool->raw = cmng->mem_mng->raws + pool->index % MLX5_CNT_MR_ALLOC_BULK;
9543 	rte_spinlock_unlock(&pool->sl);
9544 	pool->raw_hw = NULL;
9545 	return 0;
9546 }
9547 
9548 #define MLX5_POOL_QUERY_FREQ_US 1000000
9549 
9550 /**
9551  * Set the periodic procedure for triggering asynchronous batch queries for all
9552  * the counter pools.
9553  *
9554  * @param[in] sh
9555  *   Pointer to mlx5_dev_ctx_shared object.
9556  */
9557 void
9558 mlx5_set_query_alarm(struct mlx5_dev_ctx_shared *sh)
9559 {
9560 	uint32_t pools_n, us;
9561 
9562 	pools_n = rte_atomic_load_explicit(&sh->sws_cmng.n_valid, rte_memory_order_relaxed);
9563 	us = MLX5_POOL_QUERY_FREQ_US / pools_n;
9564 	DRV_LOG(DEBUG, "Set alarm for %u pools each %u us", pools_n, us);
9565 	if (rte_eal_alarm_set(us, mlx5_flow_query_alarm, sh)) {
9566 		sh->sws_cmng.query_thread_on = 0;
9567 		DRV_LOG(ERR, "Cannot reinitialize query alarm");
9568 	} else {
9569 		sh->sws_cmng.query_thread_on = 1;
9570 	}
9571 }
9572 
9573 /**
9574  * The periodic procedure for triggering asynchronous batch queries for all the
9575  * counter pools. This function is probably called by the host thread.
9576  *
9577  * @param[in] arg
9578  *   The parameter for the alarm process.
9579  */
9580 void
9581 mlx5_flow_query_alarm(void *arg)
9582 {
9583 	struct mlx5_dev_ctx_shared *sh = arg;
9584 	struct mlx5_flow_counter_mng *cmng = &sh->sws_cmng;
9585 	uint16_t pool_index = cmng->pool_index;
9586 	struct mlx5_flow_counter_pool *pool;
9587 	uint16_t n_valid;
9588 	int ret;
9589 
9590 	if (cmng->pending_queries >= MLX5_MAX_PENDING_QUERIES)
9591 		goto set_alarm;
9592 	rte_spinlock_lock(&cmng->pool_update_sl);
9593 	pool = cmng->pools[pool_index];
9594 	n_valid = cmng->n_valid;
9595 	rte_spinlock_unlock(&cmng->pool_update_sl);
9596 	/* Set the statistic memory to the new created pool. */
9597 	if ((!pool->raw && mlx5_flow_set_counter_stat_mem(sh, pool)))
9598 		goto set_alarm;
9599 	if (pool->raw_hw)
9600 		/* There is a pool query in progress. */
9601 		goto set_alarm;
9602 	pool->raw_hw = LIST_FIRST(&cmng->free_stat_raws);
9603 	if (!pool->raw_hw)
9604 		/* No free counter statistics raw memory. */
9605 		goto set_alarm;
9606 	/*
9607 	 * Identify the counters released between query trigger and query
9608 	 * handle more efficiently. The counter released in this gap period
9609 	 * should wait for a new round of query as the new arrived packets
9610 	 * will not be taken into account.
9611 	 */
9612 	pool->query_gen++;
9613 	ret = mlx5_devx_cmd_flow_counter_query(pool->min_dcs, 0,
9614 					       MLX5_COUNTERS_PER_POOL,
9615 					       NULL, NULL,
9616 					       pool->raw_hw->mem_mng->wm.lkey,
9617 					       (void *)(uintptr_t)
9618 					       pool->raw_hw->data,
9619 					       sh->devx_comp,
9620 					       (uint64_t)(uintptr_t)pool);
9621 	if (ret) {
9622 		DRV_LOG(ERR, "Failed to trigger asynchronous query for dcs ID"
9623 			" %d", pool->min_dcs->id);
9624 		pool->raw_hw = NULL;
9625 		goto set_alarm;
9626 	}
9627 	LIST_REMOVE(pool->raw_hw, next);
9628 	cmng->pending_queries++;
9629 	pool_index++;
9630 	if (pool_index >= n_valid)
9631 		pool_index = 0;
9632 set_alarm:
9633 	cmng->pool_index = pool_index;
9634 	mlx5_set_query_alarm(sh);
9635 }
9636 
9637 /**
9638  * Check and callback event for new aged flow in the counter pool
9639  *
9640  * @param[in] sh
9641  *   Pointer to mlx5_dev_ctx_shared object.
9642  * @param[in] pool
9643  *   Pointer to Current counter pool.
9644  */
9645 static void
9646 mlx5_flow_aging_check(struct mlx5_dev_ctx_shared *sh,
9647 		   struct mlx5_flow_counter_pool *pool)
9648 {
9649 	struct mlx5_priv *priv;
9650 	struct mlx5_flow_counter *cnt;
9651 	struct mlx5_age_info *age_info;
9652 	struct mlx5_age_param *age_param;
9653 	struct mlx5_counter_stats_raw *cur = pool->raw_hw;
9654 	struct mlx5_counter_stats_raw *prev = pool->raw;
9655 	const uint64_t curr_time = MLX5_CURR_TIME_SEC;
9656 	const uint32_t time_delta = curr_time - pool->time_of_last_age_check;
9657 	uint16_t expected = AGE_CANDIDATE;
9658 	uint32_t i;
9659 
9660 	pool->time_of_last_age_check = curr_time;
9661 	for (i = 0; i < MLX5_COUNTERS_PER_POOL; ++i) {
9662 		cnt = MLX5_POOL_GET_CNT(pool, i);
9663 		age_param = MLX5_CNT_TO_AGE(cnt);
9664 		if (rte_atomic_load_explicit(&age_param->state,
9665 				    rte_memory_order_relaxed) != AGE_CANDIDATE)
9666 			continue;
9667 		if (cur->data[i].hits != prev->data[i].hits) {
9668 			rte_atomic_store_explicit(&age_param->sec_since_last_hit, 0,
9669 					 rte_memory_order_relaxed);
9670 			continue;
9671 		}
9672 		if (rte_atomic_fetch_add_explicit(&age_param->sec_since_last_hit,
9673 				       time_delta,
9674 				       rte_memory_order_relaxed) + time_delta <= age_param->timeout)
9675 			continue;
9676 		/**
9677 		 * Hold the lock first, or if between the
9678 		 * state AGE_TMOUT and tailq operation the
9679 		 * release happened, the release procedure
9680 		 * may delete a non-existent tailq node.
9681 		 */
9682 		priv = rte_eth_devices[age_param->port_id].data->dev_private;
9683 		age_info = GET_PORT_AGE_INFO(priv);
9684 		rte_spinlock_lock(&age_info->aged_sl);
9685 		if (rte_atomic_compare_exchange_strong_explicit(&age_param->state, &expected,
9686 						AGE_TMOUT,
9687 						rte_memory_order_relaxed,
9688 						rte_memory_order_relaxed)) {
9689 			TAILQ_INSERT_TAIL(&age_info->aged_counters, cnt, next);
9690 			MLX5_AGE_SET(age_info, MLX5_AGE_EVENT_NEW);
9691 		}
9692 		rte_spinlock_unlock(&age_info->aged_sl);
9693 	}
9694 	mlx5_age_event_prepare(sh);
9695 }
9696 
9697 /**
9698  * Handler for the HW respond about ready values from an asynchronous batch
9699  * query. This function is probably called by the host thread.
9700  *
9701  * @param[in] sh
9702  *   The pointer to the shared device context.
9703  * @param[in] async_id
9704  *   The Devx async ID.
9705  * @param[in] status
9706  *   The status of the completion.
9707  */
9708 void
9709 mlx5_flow_async_pool_query_handle(struct mlx5_dev_ctx_shared *sh,
9710 				  uint64_t async_id, int status)
9711 {
9712 	struct mlx5_flow_counter_pool *pool =
9713 		(struct mlx5_flow_counter_pool *)(uintptr_t)async_id;
9714 	struct mlx5_counter_stats_raw *raw_to_free;
9715 	uint8_t query_gen = pool->query_gen ^ 1;
9716 	struct mlx5_flow_counter_mng *cmng = &sh->sws_cmng;
9717 	enum mlx5_counter_type cnt_type =
9718 		pool->is_aged ? MLX5_COUNTER_TYPE_AGE :
9719 				MLX5_COUNTER_TYPE_ORIGIN;
9720 
9721 	if (unlikely(status)) {
9722 		raw_to_free = pool->raw_hw;
9723 	} else {
9724 		raw_to_free = pool->raw;
9725 		if (pool->is_aged)
9726 			mlx5_flow_aging_check(sh, pool);
9727 		rte_spinlock_lock(&pool->sl);
9728 		pool->raw = pool->raw_hw;
9729 		rte_spinlock_unlock(&pool->sl);
9730 		/* Be sure the new raw counters data is updated in memory. */
9731 		rte_io_wmb();
9732 		if (!TAILQ_EMPTY(&pool->counters[query_gen])) {
9733 			rte_spinlock_lock(&cmng->csl[cnt_type]);
9734 			TAILQ_CONCAT(&cmng->counters[cnt_type],
9735 				     &pool->counters[query_gen], next);
9736 			rte_spinlock_unlock(&cmng->csl[cnt_type]);
9737 		}
9738 	}
9739 	LIST_INSERT_HEAD(&sh->sws_cmng.free_stat_raws, raw_to_free, next);
9740 	pool->raw_hw = NULL;
9741 	sh->sws_cmng.pending_queries--;
9742 }
9743 
9744 static int
9745 flow_group_to_table(uint32_t port_id, uint32_t group, uint32_t *table,
9746 		    const struct flow_grp_info *grp_info,
9747 		    struct rte_flow_error *error)
9748 {
9749 	if (grp_info->transfer && grp_info->external &&
9750 	    grp_info->fdb_def_rule) {
9751 		if (group == UINT32_MAX)
9752 			return rte_flow_error_set
9753 						(error, EINVAL,
9754 						 RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
9755 						 NULL,
9756 						 "group index not supported");
9757 		*table = group + 1;
9758 	} else {
9759 		*table = group;
9760 	}
9761 	DRV_LOG(DEBUG, "port %u group=%#x table=%#x", port_id, group, *table);
9762 	return 0;
9763 }
9764 
9765 /**
9766  * Translate the rte_flow group index to HW table value.
9767  *
9768  * If tunnel offload is disabled, all group ids converted to flow table
9769  * id using the standard method.
9770  * If tunnel offload is enabled, group id can be converted using the
9771  * standard or tunnel conversion method. Group conversion method
9772  * selection depends on flags in `grp_info` parameter:
9773  * - Internal (grp_info.external == 0) groups conversion uses the
9774  *   standard method.
9775  * - Group ids in JUMP action converted with the tunnel conversion.
9776  * - Group id in rule attribute conversion depends on a rule type and
9777  *   group id value:
9778  *   ** non zero group attributes converted with the tunnel method
9779  *   ** zero group attribute in non-tunnel rule is converted using the
9780  *      standard method - there's only one root table
9781  *   ** zero group attribute in steer tunnel rule is converted with the
9782  *      standard method - single root table
9783  *   ** zero group attribute in match tunnel rule is a special OvS
9784  *      case: that value is used for portability reasons. That group
9785  *      id is converted with the tunnel conversion method.
9786  *
9787  * @param[in] dev
9788  *   Port device
9789  * @param[in] tunnel
9790  *   PMD tunnel offload object
9791  * @param[in] group
9792  *   rte_flow group index value.
9793  * @param[out] table
9794  *   HW table value.
9795  * @param[in] grp_info
9796  *   flags used for conversion
9797  * @param[out] error
9798  *   Pointer to error structure.
9799  *
9800  * @return
9801  *   0 on success, a negative errno value otherwise and rte_errno is set.
9802  */
9803 int
9804 mlx5_flow_group_to_table(struct rte_eth_dev *dev,
9805 			 const struct mlx5_flow_tunnel *tunnel,
9806 			 uint32_t group, uint32_t *table,
9807 			 const struct flow_grp_info *grp_info,
9808 			 struct rte_flow_error *error)
9809 {
9810 	int ret;
9811 	bool standard_translation;
9812 
9813 	if (!grp_info->skip_scale && grp_info->external &&
9814 	    group < MLX5_MAX_TABLES_EXTERNAL)
9815 		group *= MLX5_FLOW_TABLE_FACTOR;
9816 	if (is_tunnel_offload_active(dev)) {
9817 		standard_translation = !grp_info->external ||
9818 					grp_info->std_tbl_fix;
9819 	} else {
9820 		standard_translation = true;
9821 	}
9822 	DRV_LOG(DEBUG,
9823 		"port %u group=%u transfer=%d external=%d fdb_def_rule=%d translate=%s",
9824 		dev->data->port_id, group, grp_info->transfer,
9825 		grp_info->external, grp_info->fdb_def_rule,
9826 		standard_translation ? "STANDARD" : "TUNNEL");
9827 	if (standard_translation)
9828 		ret = flow_group_to_table(dev->data->port_id, group, table,
9829 					  grp_info, error);
9830 	else
9831 		ret = tunnel_flow_group_to_flow_table(dev, tunnel, group,
9832 						      table, error);
9833 
9834 	return ret;
9835 }
9836 
9837 /**
9838  * Discover availability of metadata reg_c's.
9839  *
9840  * Iteratively use test flows to check availability.
9841  *
9842  * @param[in] dev
9843  *   Pointer to the Ethernet device structure.
9844  *
9845  * @return
9846  *   0 on success, a negative errno value otherwise and rte_errno is set.
9847  */
9848 int
9849 mlx5_flow_discover_mreg_c(struct rte_eth_dev *dev)
9850 {
9851 	struct mlx5_priv *priv = dev->data->dev_private;
9852 	enum modify_reg idx;
9853 	int n = 0;
9854 
9855 	/* reg_c[0] and reg_c[1] are reserved. */
9856 	priv->sh->flow_mreg_c[n++] = REG_C_0;
9857 	priv->sh->flow_mreg_c[n++] = REG_C_1;
9858 	/* Discover availability of other reg_c's. */
9859 	for (idx = REG_C_2; idx <= REG_C_7; ++idx) {
9860 		struct rte_flow_attr attr = {
9861 			.group = MLX5_FLOW_MREG_CP_TABLE_GROUP,
9862 			.priority = MLX5_FLOW_LOWEST_PRIO_INDICATOR,
9863 			.ingress = 1,
9864 		};
9865 		struct rte_flow_item items[] = {
9866 			[0] = {
9867 				.type = RTE_FLOW_ITEM_TYPE_END,
9868 			},
9869 		};
9870 		struct rte_flow_action actions[] = {
9871 			[0] = {
9872 				.type = (enum rte_flow_action_type)
9873 					MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG,
9874 				.conf = &(struct mlx5_flow_action_copy_mreg){
9875 					.src = REG_C_1,
9876 					.dst = idx,
9877 				},
9878 			},
9879 			[1] = {
9880 				.type = RTE_FLOW_ACTION_TYPE_JUMP,
9881 				.conf = &(struct rte_flow_action_jump){
9882 					.group = MLX5_FLOW_MREG_ACT_TABLE_GROUP,
9883 				},
9884 			},
9885 			[2] = {
9886 				.type = RTE_FLOW_ACTION_TYPE_END,
9887 			},
9888 		};
9889 		uint32_t flow_idx;
9890 		struct rte_flow *flow;
9891 		struct rte_flow_error error;
9892 
9893 		if (!priv->sh->config.dv_flow_en)
9894 			break;
9895 		/* Create internal flow, validation skips copy action. */
9896 		flow_idx = mlx5_flow_list_create(dev, MLX5_FLOW_TYPE_GEN, &attr,
9897 					items, actions, false, &error);
9898 		flow = mlx5_ipool_get(priv->flows[MLX5_FLOW_TYPE_GEN],
9899 				      flow_idx);
9900 		if (!flow)
9901 			continue;
9902 		priv->sh->flow_mreg_c[n++] = idx;
9903 		mlx5_flow_list_destroy(dev, MLX5_FLOW_TYPE_GEN, flow_idx);
9904 	}
9905 	for (; n < MLX5_MREG_C_NUM; ++n)
9906 		priv->sh->flow_mreg_c[n] = REG_NON;
9907 	priv->sh->metadata_regc_check_flag = 1;
9908 	return 0;
9909 }
9910 
9911 int
9912 save_dump_file(const uint8_t *data, uint32_t size,
9913 	uint32_t type, uint64_t id, void *arg, FILE *file)
9914 {
9915 	char line[BUF_SIZE];
9916 	uint32_t out = 0;
9917 	uint32_t k;
9918 	uint32_t actions_num;
9919 	struct rte_flow_query_count *count;
9920 
9921 	memset(line, 0, BUF_SIZE);
9922 	switch (type) {
9923 	case DR_DUMP_REC_TYPE_PMD_MODIFY_HDR:
9924 		actions_num = *(uint32_t *)(arg);
9925 		out += snprintf(line + out, BUF_SIZE - out, "%d,0x%" PRIx64 ",%d,",
9926 				type, id, actions_num);
9927 		break;
9928 	case DR_DUMP_REC_TYPE_PMD_PKT_REFORMAT:
9929 		out += snprintf(line + out, BUF_SIZE - out, "%d,0x%" PRIx64 ",",
9930 				type, id);
9931 		break;
9932 	case DR_DUMP_REC_TYPE_PMD_COUNTER:
9933 		count = (struct rte_flow_query_count *)arg;
9934 		fprintf(file,
9935 			"%d,0x%" PRIx64 ",%" PRIu64 ",%" PRIu64 "\n",
9936 			type, id, count->hits, count->bytes);
9937 		return 0;
9938 	default:
9939 		return -1;
9940 	}
9941 
9942 	for (k = 0; k < size; k++) {
9943 		/* Make sure we do not overrun the line buffer length. */
9944 		if (out >= BUF_SIZE - 4) {
9945 			line[out] = '\0';
9946 			break;
9947 		}
9948 		out += snprintf(line + out, BUF_SIZE - out, "%02x",
9949 				(data[k]) & 0xff);
9950 	}
9951 	fprintf(file, "%s\n", line);
9952 	return 0;
9953 }
9954 
9955 int
9956 mlx5_flow_query_counter(struct rte_eth_dev *dev, struct rte_flow *flow,
9957 	struct rte_flow_query_count *count, struct rte_flow_error *error)
9958 {
9959 	struct rte_flow_action action[2];
9960 	enum mlx5_flow_drv_type ftype;
9961 	const struct mlx5_flow_driver_ops *fops;
9962 
9963 	if (!flow) {
9964 		return rte_flow_error_set(error, ENOENT,
9965 				RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
9966 				NULL,
9967 				"invalid flow handle");
9968 	}
9969 	action[0].type = RTE_FLOW_ACTION_TYPE_COUNT;
9970 	action[1].type = RTE_FLOW_ACTION_TYPE_END;
9971 	if (flow->counter) {
9972 		memset(count, 0, sizeof(struct rte_flow_query_count));
9973 		ftype = (enum mlx5_flow_drv_type)(flow->drv_type);
9974 		MLX5_ASSERT(ftype > MLX5_FLOW_TYPE_MIN &&
9975 						ftype < MLX5_FLOW_TYPE_MAX);
9976 		fops = flow_get_drv_ops(ftype);
9977 		return fops->query(dev, flow, action, count, error);
9978 	}
9979 	return -1;
9980 }
9981 
9982 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
9983 /**
9984  * Dump flow ipool data to file
9985  *
9986  * @param[in] dev
9987  *   The pointer to Ethernet device.
9988  * @param[in] file
9989  *   A pointer to a file for output.
9990  * @param[out] error
9991  *   Perform verbose error reporting if not NULL. PMDs initialize this
9992  *   structure in case of error only.
9993  * @return
9994  *   0 on success, a negative value otherwise.
9995  */
9996 int
9997 mlx5_flow_dev_dump_ipool(struct rte_eth_dev *dev,
9998 	struct rte_flow *flow, FILE *file,
9999 	struct rte_flow_error *error)
10000 {
10001 	struct mlx5_priv *priv = dev->data->dev_private;
10002 	struct mlx5_flow_dv_modify_hdr_resource  *modify_hdr;
10003 	struct mlx5_flow_dv_encap_decap_resource *encap_decap;
10004 	uint32_t handle_idx;
10005 	struct mlx5_flow_handle *dh;
10006 	struct rte_flow_query_count count;
10007 	uint32_t actions_num;
10008 	const uint8_t *data;
10009 	size_t size;
10010 	uint64_t id;
10011 	uint32_t type;
10012 	void *action = NULL;
10013 
10014 	if (!flow) {
10015 		return rte_flow_error_set(error, ENOENT,
10016 				RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
10017 				NULL,
10018 				"invalid flow handle");
10019 	}
10020 	handle_idx = flow->dev_handles;
10021 	/* query counter */
10022 	if (flow->counter &&
10023 	(!mlx5_counter_query(dev, flow->counter, false,
10024 	&count.hits, &count.bytes, &action)) && action) {
10025 		id = (uint64_t)(uintptr_t)action;
10026 		type = DR_DUMP_REC_TYPE_PMD_COUNTER;
10027 		save_dump_file(NULL, 0, type,
10028 			id, (void *)&count, file);
10029 	}
10030 
10031 	while (handle_idx) {
10032 		dh = mlx5_ipool_get(priv->sh->ipool
10033 				[MLX5_IPOOL_MLX5_FLOW], handle_idx);
10034 		if (!dh)
10035 			continue;
10036 		handle_idx = dh->next.next;
10037 
10038 		/* Get modify_hdr and encap_decap buf from ipools. */
10039 		encap_decap = NULL;
10040 		modify_hdr = dh->dvh.modify_hdr;
10041 
10042 		if (dh->dvh.rix_encap_decap) {
10043 			encap_decap = mlx5_ipool_get(priv->sh->ipool
10044 						[MLX5_IPOOL_DECAP_ENCAP],
10045 						dh->dvh.rix_encap_decap);
10046 		}
10047 		if (modify_hdr) {
10048 			data = (const uint8_t *)modify_hdr->actions;
10049 			size = (size_t)(modify_hdr->actions_num) * 8;
10050 			id = (uint64_t)(uintptr_t)modify_hdr->action;
10051 			actions_num = modify_hdr->actions_num;
10052 			type = DR_DUMP_REC_TYPE_PMD_MODIFY_HDR;
10053 			save_dump_file(data, size, type, id,
10054 						(void *)(&actions_num), file);
10055 		}
10056 		if (encap_decap) {
10057 			data = encap_decap->buf;
10058 			size = encap_decap->size;
10059 			id = (uint64_t)(uintptr_t)encap_decap->action;
10060 			type = DR_DUMP_REC_TYPE_PMD_PKT_REFORMAT;
10061 			save_dump_file(data, size, type,
10062 						id, NULL, file);
10063 		}
10064 	}
10065 	return 0;
10066 }
10067 
10068 /**
10069  * Dump all flow's encap_decap/modify_hdr/counter data to file
10070  *
10071  * @param[in] dev
10072  *   The pointer to Ethernet device.
10073  * @param[in] file
10074  *   A pointer to a file for output.
10075  * @param[out] error
10076  *   Perform verbose error reporting if not NULL. PMDs initialize this
10077  *   structure in case of error only.
10078  * @return
10079  *   0 on success, a negative value otherwise.
10080  */
10081 static int
10082 mlx5_flow_dev_dump_sh_all(struct rte_eth_dev *dev,
10083 	FILE *file, struct rte_flow_error *error __rte_unused)
10084 {
10085 	struct mlx5_priv *priv = dev->data->dev_private;
10086 	struct mlx5_dev_ctx_shared *sh = priv->sh;
10087 	struct mlx5_hlist *h;
10088 	struct mlx5_flow_dv_modify_hdr_resource  *modify_hdr;
10089 	struct mlx5_flow_dv_encap_decap_resource *encap_decap;
10090 	struct rte_flow_query_count count;
10091 	uint32_t actions_num;
10092 	const uint8_t *data;
10093 	size_t size;
10094 	uint64_t id;
10095 	uint32_t type;
10096 	uint32_t i;
10097 	uint32_t j;
10098 	struct mlx5_list_inconst *l_inconst;
10099 	struct mlx5_list_entry *e;
10100 	int lcore_index;
10101 	struct mlx5_flow_counter_mng *cmng = &priv->sh->sws_cmng;
10102 	uint32_t max;
10103 	void *action;
10104 
10105 	/* encap_decap hlist is lcore_share, get global core cache. */
10106 	i = MLX5_LIST_GLOBAL;
10107 	h = sh->encaps_decaps;
10108 	if (h) {
10109 		for (j = 0; j <= h->mask; j++) {
10110 			l_inconst = &h->buckets[j].l;
10111 			if (!l_inconst || !l_inconst->cache[i])
10112 				continue;
10113 
10114 			e = LIST_FIRST(&l_inconst->cache[i]->h);
10115 			while (e) {
10116 				encap_decap =
10117 				(struct mlx5_flow_dv_encap_decap_resource *)e;
10118 				data = encap_decap->buf;
10119 				size = encap_decap->size;
10120 				id = (uint64_t)(uintptr_t)encap_decap->action;
10121 				type = DR_DUMP_REC_TYPE_PMD_PKT_REFORMAT;
10122 				save_dump_file(data, size, type,
10123 					id, NULL, file);
10124 				e = LIST_NEXT(e, next);
10125 			}
10126 		}
10127 	}
10128 
10129 	/* get modify_hdr */
10130 	h = sh->modify_cmds;
10131 	if (h) {
10132 		lcore_index = rte_lcore_index(rte_lcore_id());
10133 		if (unlikely(lcore_index == -1)) {
10134 			lcore_index = MLX5_LIST_NLCORE;
10135 			rte_spinlock_lock(&h->l_const.lcore_lock);
10136 		}
10137 		i = lcore_index;
10138 
10139 		if (lcore_index == MLX5_LIST_NLCORE) {
10140 			for (i = 0; i <= (uint32_t)lcore_index; i++) {
10141 				for (j = 0; j <= h->mask; j++) {
10142 					l_inconst = &h->buckets[j].l;
10143 					if (!l_inconst || !l_inconst->cache[i])
10144 						continue;
10145 
10146 					e = LIST_FIRST(&l_inconst->cache[i]->h);
10147 					while (e) {
10148 						modify_hdr =
10149 						(struct mlx5_flow_dv_modify_hdr_resource *)e;
10150 						data = (const uint8_t *)modify_hdr->actions;
10151 						size = (size_t)(modify_hdr->actions_num) * 8;
10152 						actions_num = modify_hdr->actions_num;
10153 						id = (uint64_t)(uintptr_t)modify_hdr->action;
10154 						type = DR_DUMP_REC_TYPE_PMD_MODIFY_HDR;
10155 						save_dump_file(data, size, type, id,
10156 								(void *)(&actions_num), file);
10157 						e = LIST_NEXT(e, next);
10158 					}
10159 				}
10160 			}
10161 		} else {
10162 			for (j = 0; j <= h->mask; j++) {
10163 				l_inconst = &h->buckets[j].l;
10164 				if (!l_inconst || !l_inconst->cache[i])
10165 					continue;
10166 
10167 				e = LIST_FIRST(&l_inconst->cache[i]->h);
10168 				while (e) {
10169 					modify_hdr =
10170 					(struct mlx5_flow_dv_modify_hdr_resource *)e;
10171 					data = (const uint8_t *)modify_hdr->actions;
10172 					size = (size_t)(modify_hdr->actions_num) * 8;
10173 					actions_num = modify_hdr->actions_num;
10174 					id = (uint64_t)(uintptr_t)modify_hdr->action;
10175 					type = DR_DUMP_REC_TYPE_PMD_MODIFY_HDR;
10176 					save_dump_file(data, size, type, id,
10177 							(void *)(&actions_num), file);
10178 					e = LIST_NEXT(e, next);
10179 				}
10180 			}
10181 		}
10182 
10183 		if (unlikely(lcore_index == MLX5_LIST_NLCORE))
10184 			rte_spinlock_unlock(&h->l_const.lcore_lock);
10185 	}
10186 
10187 	/* get counter */
10188 	MLX5_ASSERT(cmng->n_valid <= MLX5_COUNTER_POOLS_MAX_NUM);
10189 	max = MLX5_COUNTERS_PER_POOL * cmng->n_valid;
10190 	for (j = 1; j <= max; j++) {
10191 		action = NULL;
10192 		if ((!mlx5_counter_query(dev, j, false, &count.hits,
10193 		&count.bytes, &action)) && action) {
10194 			id = (uint64_t)(uintptr_t)action;
10195 			type = DR_DUMP_REC_TYPE_PMD_COUNTER;
10196 			save_dump_file(NULL, 0, type,
10197 					id, (void *)&count, file);
10198 		}
10199 	}
10200 	return 0;
10201 }
10202 #endif
10203 
10204 /**
10205  * Dump flow raw hw data to file
10206  *
10207  * @param[in] dev
10208  *    The pointer to Ethernet device.
10209  * @param[in] file
10210  *   A pointer to a file for output.
10211  * @param[out] error
10212  *   Perform verbose error reporting if not NULL. PMDs initialize this
10213  *   structure in case of error only.
10214  * @return
10215  *   0 on success, a negative value otherwise.
10216  */
10217 int
10218 mlx5_flow_dev_dump(struct rte_eth_dev *dev, struct rte_flow *flow_idx,
10219 		   FILE *file,
10220 		   struct rte_flow_error *error __rte_unused)
10221 {
10222 	struct mlx5_priv *priv = dev->data->dev_private;
10223 	struct mlx5_dev_ctx_shared *sh = priv->sh;
10224 	uint32_t handle_idx;
10225 	int ret;
10226 	struct mlx5_flow_handle *dh;
10227 	struct rte_flow *flow;
10228 
10229 	if (!sh->config.dv_flow_en) {
10230 		if (fputs("device dv flow disabled\n", file) <= 0)
10231 			return -errno;
10232 		return -ENOTSUP;
10233 	}
10234 
10235 	/* dump all */
10236 	if (!flow_idx) {
10237 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
10238 		if (mlx5_flow_dev_dump_sh_all(dev, file, error))
10239 			return -EINVAL;
10240 
10241 		if (sh->config.dv_flow_en == 2)
10242 			return mlx5dr_debug_dump(priv->dr_ctx, file);
10243 #endif
10244 		return mlx5_devx_cmd_flow_dump(sh->fdb_domain,
10245 					       sh->rx_domain,
10246 					       sh->tx_domain, file);
10247 	}
10248 	/* dump one */
10249 	flow = mlx5_ipool_get(priv->flows[MLX5_FLOW_TYPE_GEN],
10250 			(uintptr_t)(void *)flow_idx);
10251 	if (!flow)
10252 		return -EINVAL;
10253 
10254 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
10255 	mlx5_flow_dev_dump_ipool(dev, flow, file, error);
10256 #endif
10257 	handle_idx = flow->dev_handles;
10258 	while (handle_idx) {
10259 		dh = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
10260 				handle_idx);
10261 		if (!dh)
10262 			return -ENOENT;
10263 		if (dh->drv_flow) {
10264 			if (sh->config.dv_flow_en == 2)
10265 				return -ENOTSUP;
10266 
10267 			ret = mlx5_devx_cmd_flow_single_dump(dh->drv_flow,
10268 							     file);
10269 			if (ret)
10270 				return -ENOENT;
10271 		}
10272 		handle_idx = dh->next.next;
10273 	}
10274 	return 0;
10275 }
10276 
10277 /**
10278  * Get aged-out flows.
10279  *
10280  * @param[in] dev
10281  *   Pointer to the Ethernet device structure.
10282  * @param[in] context
10283  *   The address of an array of pointers to the aged-out flows contexts.
10284  * @param[in] nb_countexts
10285  *   The length of context array pointers.
10286  * @param[out] error
10287  *   Perform verbose error reporting if not NULL. Initialized in case of
10288  *   error only.
10289  *
10290  * @return
10291  *   how many contexts get in success, otherwise negative errno value.
10292  *   if nb_contexts is 0, return the amount of all aged contexts.
10293  *   if nb_contexts is not 0 , return the amount of aged flows reported
10294  *   in the context array.
10295  */
10296 int
10297 mlx5_flow_get_aged_flows(struct rte_eth_dev *dev, void **contexts,
10298 			uint32_t nb_contexts, struct rte_flow_error *error)
10299 {
10300 	struct rte_flow_attr attr = { .transfer = 0 };
10301 
10302 	return flow_get_drv_ops(flow_get_drv_type(dev, &attr))->get_aged_flows
10303 		(dev, contexts, nb_contexts, error);
10304 }
10305 
10306 /**
10307  * Get aged-out flows per HWS queue.
10308  *
10309  * @param[in] dev
10310  *   Pointer to the Ethernet device structure.
10311  * @param[in] queue_id
10312  *   Flow queue to query.
10313  * @param[in] context
10314  *   The address of an array of pointers to the aged-out flows contexts.
10315  * @param[in] nb_countexts
10316  *   The length of context array pointers.
10317  * @param[out] error
10318  *   Perform verbose error reporting if not NULL. Initialized in case of
10319  *   error only.
10320  *
10321  * @return
10322  *   how many contexts get in success, otherwise negative errno value.
10323  *   if nb_contexts is 0, return the amount of all aged contexts.
10324  *   if nb_contexts is not 0 , return the amount of aged flows reported
10325  *   in the context array.
10326  */
10327 int
10328 mlx5_flow_get_q_aged_flows(struct rte_eth_dev *dev, uint32_t queue_id,
10329 			   void **contexts, uint32_t nb_contexts,
10330 			   struct rte_flow_error *error)
10331 {
10332 	const struct mlx5_flow_driver_ops *fops;
10333 	struct rte_flow_attr attr = { 0 };
10334 
10335 	if (flow_get_drv_type(dev, &attr) == MLX5_FLOW_TYPE_HW) {
10336 		fops = flow_get_drv_ops(MLX5_FLOW_TYPE_HW);
10337 		return fops->get_q_aged_flows(dev, queue_id, contexts,
10338 					      nb_contexts, error);
10339 	}
10340 	DRV_LOG(ERR, "port %u queue %u get aged flows is not supported.",
10341 		dev->data->port_id, queue_id);
10342 	return rte_flow_error_set(error, ENOTSUP,
10343 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
10344 				  "get Q aged flows with incorrect steering mode");
10345 }
10346 
10347 /* Wrapper for driver action_validate op callback */
10348 static int
10349 flow_drv_action_validate(struct rte_eth_dev *dev,
10350 			 const struct rte_flow_indir_action_conf *conf,
10351 			 const struct rte_flow_action *action,
10352 			 const struct mlx5_flow_driver_ops *fops,
10353 			 struct rte_flow_error *error)
10354 {
10355 	static const char err_msg[] = "indirect action validation unsupported";
10356 
10357 	if (!fops->action_validate) {
10358 		DRV_LOG(ERR, "port %u %s.", dev->data->port_id, err_msg);
10359 		rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
10360 				   NULL, err_msg);
10361 		return -rte_errno;
10362 	}
10363 	return fops->action_validate(dev, conf, action, error);
10364 }
10365 
10366 /**
10367  * Destroys the shared action by handle.
10368  *
10369  * @param dev
10370  *   Pointer to Ethernet device structure.
10371  * @param[in] handle
10372  *   Handle for the indirect action object to be destroyed.
10373  * @param[out] error
10374  *   Perform verbose error reporting if not NULL. PMDs initialize this
10375  *   structure in case of error only.
10376  *
10377  * @return
10378  *   0 on success, a negative errno value otherwise and rte_errno is set.
10379  *
10380  * @note: wrapper for driver action_create op callback.
10381  */
10382 static int
10383 mlx5_action_handle_destroy(struct rte_eth_dev *dev,
10384 			   struct rte_flow_action_handle *handle,
10385 			   struct rte_flow_error *error)
10386 {
10387 	static const char err_msg[] = "indirect action destruction unsupported";
10388 	struct rte_flow_attr attr = { .transfer = 0 };
10389 	const struct mlx5_flow_driver_ops *fops =
10390 			flow_get_drv_ops(flow_get_drv_type(dev, &attr));
10391 
10392 	if (!fops->action_destroy) {
10393 		DRV_LOG(ERR, "port %u %s.", dev->data->port_id, err_msg);
10394 		rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
10395 				   NULL, err_msg);
10396 		return -rte_errno;
10397 	}
10398 	return fops->action_destroy(dev, handle, error);
10399 }
10400 
10401 /* Wrapper for driver action_destroy op callback */
10402 static int
10403 flow_drv_action_update(struct rte_eth_dev *dev,
10404 		       struct rte_flow_action_handle *handle,
10405 		       const void *update,
10406 		       const struct mlx5_flow_driver_ops *fops,
10407 		       struct rte_flow_error *error)
10408 {
10409 	static const char err_msg[] = "indirect action update unsupported";
10410 
10411 	if (!fops->action_update) {
10412 		DRV_LOG(ERR, "port %u %s.", dev->data->port_id, err_msg);
10413 		rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
10414 				   NULL, err_msg);
10415 		return -rte_errno;
10416 	}
10417 	return fops->action_update(dev, handle, update, error);
10418 }
10419 
10420 /* Wrapper for driver action_destroy op callback */
10421 static int
10422 flow_drv_action_query(struct rte_eth_dev *dev,
10423 		      const struct rte_flow_action_handle *handle,
10424 		      void *data,
10425 		      const struct mlx5_flow_driver_ops *fops,
10426 		      struct rte_flow_error *error)
10427 {
10428 	static const char err_msg[] = "indirect action query unsupported";
10429 
10430 	if (!fops->action_query) {
10431 		DRV_LOG(ERR, "port %u %s.", dev->data->port_id, err_msg);
10432 		rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
10433 				   NULL, err_msg);
10434 		return -rte_errno;
10435 	}
10436 	return fops->action_query(dev, handle, data, error);
10437 }
10438 
10439 /**
10440  * Create indirect action for reuse in multiple flow rules.
10441  *
10442  * @param dev
10443  *   Pointer to Ethernet device structure.
10444  * @param conf
10445  *   Pointer to indirect action object configuration.
10446  * @param[in] action
10447  *   Action configuration for indirect action object creation.
10448  * @param[out] error
10449  *   Perform verbose error reporting if not NULL. PMDs initialize this
10450  *   structure in case of error only.
10451  * @return
10452  *   A valid handle in case of success, NULL otherwise and rte_errno is set.
10453  */
10454 static struct rte_flow_action_handle *
10455 mlx5_action_handle_create(struct rte_eth_dev *dev,
10456 			  const struct rte_flow_indir_action_conf *conf,
10457 			  const struct rte_flow_action *action,
10458 			  struct rte_flow_error *error)
10459 {
10460 	static const char err_msg[] = "indirect action creation unsupported";
10461 	struct rte_flow_attr attr = { .transfer = 0 };
10462 	const struct mlx5_flow_driver_ops *fops =
10463 			flow_get_drv_ops(flow_get_drv_type(dev, &attr));
10464 
10465 	if (flow_drv_action_validate(dev, conf, action, fops, error))
10466 		return NULL;
10467 	if (!fops->action_create) {
10468 		DRV_LOG(ERR, "port %u %s.", dev->data->port_id, err_msg);
10469 		rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
10470 				   NULL, err_msg);
10471 		return NULL;
10472 	}
10473 	return fops->action_create(dev, conf, action, error);
10474 }
10475 
10476 /**
10477  * Updates inplace the indirect action configuration pointed by *handle*
10478  * with the configuration provided as *update* argument.
10479  * The update of the indirect action configuration effects all flow rules
10480  * reusing the action via handle.
10481  *
10482  * @param dev
10483  *   Pointer to Ethernet device structure.
10484  * @param[in] handle
10485  *   Handle for the indirect action to be updated.
10486  * @param[in] update
10487  *   Action specification used to modify the action pointed by handle.
10488  *   *update* could be of same type with the action pointed by the *handle*
10489  *   handle argument, or some other structures like a wrapper, depending on
10490  *   the indirect action type.
10491  * @param[out] error
10492  *   Perform verbose error reporting if not NULL. PMDs initialize this
10493  *   structure in case of error only.
10494  *
10495  * @return
10496  *   0 on success, a negative errno value otherwise and rte_errno is set.
10497  */
10498 static int
10499 mlx5_action_handle_update(struct rte_eth_dev *dev,
10500 		struct rte_flow_action_handle *handle,
10501 		const void *update,
10502 		struct rte_flow_error *error)
10503 {
10504 	struct rte_flow_attr attr = { .transfer = 0 };
10505 	const struct mlx5_flow_driver_ops *fops =
10506 			flow_get_drv_ops(flow_get_drv_type(dev, &attr));
10507 	int ret;
10508 	uint32_t act_idx = (uint32_t)(uintptr_t)handle;
10509 	uint32_t type = act_idx >> MLX5_INDIRECT_ACTION_TYPE_OFFSET;
10510 
10511 	switch (type) {
10512 	case MLX5_INDIRECT_ACTION_TYPE_CT:
10513 	case MLX5_INDIRECT_ACTION_TYPE_METER_MARK:
10514 		ret = 0;
10515 		break;
10516 	default:
10517 		ret = flow_drv_action_validate(dev, NULL,
10518 				(const struct rte_flow_action *)update,
10519 				fops, error);
10520 	}
10521 	if (ret)
10522 		return ret;
10523 	return flow_drv_action_update(dev, handle, update, fops,
10524 				      error);
10525 }
10526 
10527 /**
10528  * Query the indirect action by handle.
10529  *
10530  * This function allows retrieving action-specific data such as counters.
10531  * Data is gathered by special action which may be present/referenced in
10532  * more than one flow rule definition.
10533  *
10534  * see @RTE_FLOW_ACTION_TYPE_COUNT
10535  *
10536  * @param dev
10537  *   Pointer to Ethernet device structure.
10538  * @param[in] handle
10539  *   Handle for the indirect action to query.
10540  * @param[in, out] data
10541  *   Pointer to storage for the associated query data type.
10542  * @param[out] error
10543  *   Perform verbose error reporting if not NULL. PMDs initialize this
10544  *   structure in case of error only.
10545  *
10546  * @return
10547  *   0 on success, a negative errno value otherwise and rte_errno is set.
10548  */
10549 static int
10550 mlx5_action_handle_query(struct rte_eth_dev *dev,
10551 			 const struct rte_flow_action_handle *handle,
10552 			 void *data,
10553 			 struct rte_flow_error *error)
10554 {
10555 	struct rte_flow_attr attr = { .transfer = 0 };
10556 	const struct mlx5_flow_driver_ops *fops =
10557 			flow_get_drv_ops(flow_get_drv_type(dev, &attr));
10558 
10559 	return flow_drv_action_query(dev, handle, data, fops, error);
10560 }
10561 
10562 static int
10563 mlx5_action_handle_query_update(struct rte_eth_dev *dev,
10564 				struct rte_flow_action_handle *handle,
10565 				const void *update, void *query,
10566 				enum rte_flow_query_update_mode qu_mode,
10567 				struct rte_flow_error *error)
10568 {
10569 	struct rte_flow_attr attr = { .transfer = 0 };
10570 	enum mlx5_flow_drv_type drv_type = flow_get_drv_type(dev, &attr);
10571 	const struct mlx5_flow_driver_ops *fops;
10572 
10573 	if (drv_type == MLX5_FLOW_TYPE_MIN || drv_type == MLX5_FLOW_TYPE_MAX)
10574 		return rte_flow_error_set(error, ENOTSUP,
10575 					  RTE_FLOW_ERROR_TYPE_ACTION,
10576 					  NULL, "invalid driver type");
10577 	fops = flow_get_drv_ops(drv_type);
10578 	if (!fops || !fops->action_query_update)
10579 		return rte_flow_error_set(error, ENOTSUP,
10580 					  RTE_FLOW_ERROR_TYPE_ACTION,
10581 					  NULL, "no query_update handler");
10582 	return fops->action_query_update(dev, handle, update,
10583 					 query, qu_mode, error);
10584 }
10585 
10586 
10587 #define MLX5_DRV_FOPS_OR_ERR(dev, fops, drv_cb, ret)                           \
10588 {                                                                              \
10589 	struct rte_flow_attr attr = { .transfer = 0 };                         \
10590 	enum mlx5_flow_drv_type drv_type = flow_get_drv_type((dev), &attr);    \
10591 	if (drv_type == MLX5_FLOW_TYPE_MIN ||                                  \
10592 	    drv_type == MLX5_FLOW_TYPE_MAX) {                                  \
10593 		rte_flow_error_set(error, ENOTSUP,                             \
10594 				   RTE_FLOW_ERROR_TYPE_ACTION,                 \
10595 				   NULL, "invalid driver type");               \
10596 		return ret;                                                    \
10597 	}                                                                      \
10598 	(fops) = flow_get_drv_ops(drv_type);                                   \
10599 	if (!(fops) || !(fops)->drv_cb) {                                      \
10600 		rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION, \
10601 				   NULL, "no action_list handler");            \
10602 		return ret;                                                    \
10603 	}                                                                      \
10604 }
10605 
10606 static struct rte_flow_action_list_handle *
10607 mlx5_action_list_handle_create(struct rte_eth_dev *dev,
10608 			       const struct rte_flow_indir_action_conf *conf,
10609 			       const struct rte_flow_action *actions,
10610 			       struct rte_flow_error *error)
10611 {
10612 	const struct mlx5_flow_driver_ops *fops;
10613 
10614 	MLX5_DRV_FOPS_OR_ERR(dev, fops, action_list_handle_create, NULL);
10615 	return fops->action_list_handle_create(dev, conf, actions, error);
10616 }
10617 
10618 static int
10619 mlx5_action_list_handle_destroy(struct rte_eth_dev *dev,
10620 				struct rte_flow_action_list_handle *handle,
10621 				struct rte_flow_error *error)
10622 {
10623 	const struct mlx5_flow_driver_ops *fops;
10624 
10625 	MLX5_DRV_FOPS_OR_ERR(dev, fops, action_list_handle_destroy, ENOTSUP);
10626 	return fops->action_list_handle_destroy(dev, handle, error);
10627 }
10628 
10629 static int
10630 mlx5_flow_action_list_handle_query_update(struct rte_eth_dev *dev,
10631 					  const
10632 					  struct rte_flow_action_list_handle *handle,
10633 					  const void **update, void **query,
10634 					  enum rte_flow_query_update_mode mode,
10635 					  struct rte_flow_error *error)
10636 {
10637 	const struct mlx5_flow_driver_ops *fops;
10638 
10639 	MLX5_DRV_FOPS_OR_ERR(dev, fops,
10640 			     action_list_handle_query_update, ENOTSUP);
10641 	return fops->action_list_handle_query_update(dev, handle, update, query,
10642 						     mode, error);
10643 }
10644 static int
10645 mlx5_flow_calc_table_hash(struct rte_eth_dev *dev,
10646 			  const struct rte_flow_template_table *table,
10647 			  const struct rte_flow_item pattern[],
10648 			  uint8_t pattern_template_index,
10649 			  uint32_t *hash, struct rte_flow_error *error)
10650 {
10651 	struct rte_flow_attr attr = { .transfer = 0 };
10652 	enum mlx5_flow_drv_type drv_type = flow_get_drv_type(dev, &attr);
10653 	const struct mlx5_flow_driver_ops *fops;
10654 
10655 	if (drv_type == MLX5_FLOW_TYPE_MIN || drv_type == MLX5_FLOW_TYPE_MAX)
10656 		return rte_flow_error_set(error, ENOTSUP,
10657 					  RTE_FLOW_ERROR_TYPE_ACTION,
10658 					  NULL, "invalid driver type");
10659 	fops = flow_get_drv_ops(drv_type);
10660 	if (!fops || !fops->action_query_update)
10661 		return rte_flow_error_set(error, ENOTSUP,
10662 					  RTE_FLOW_ERROR_TYPE_ACTION,
10663 					  NULL, "no query_update handler");
10664 	return fops->flow_calc_table_hash(dev, table, pattern, pattern_template_index,
10665 					  hash, error);
10666 }
10667 
10668 static int
10669 mlx5_flow_calc_encap_hash(struct rte_eth_dev *dev,
10670 			  const struct rte_flow_item pattern[],
10671 			  enum rte_flow_encap_hash_field dest_field,
10672 			  uint8_t *hash,
10673 			  struct rte_flow_error *error)
10674 {
10675 	enum mlx5_flow_drv_type drv_type = flow_get_drv_type(dev, NULL);
10676 	const struct mlx5_flow_driver_ops *fops;
10677 
10678 	if (drv_type == MLX5_FLOW_TYPE_MIN || drv_type == MLX5_FLOW_TYPE_MAX)
10679 		return rte_flow_error_set(error, ENOTSUP,
10680 					  RTE_FLOW_ERROR_TYPE_ACTION,
10681 					  NULL, "invalid driver type");
10682 	fops = flow_get_drv_ops(drv_type);
10683 	if (!fops || !fops->flow_calc_encap_hash)
10684 		return rte_flow_error_set(error, ENOTSUP,
10685 					  RTE_FLOW_ERROR_TYPE_ACTION,
10686 					  NULL, "no calc encap hash handler");
10687 	return fops->flow_calc_encap_hash(dev, pattern, dest_field, hash, error);
10688 }
10689 
10690 static int
10691 mlx5_template_table_resize(struct rte_eth_dev *dev,
10692 			   struct rte_flow_template_table *table,
10693 			   uint32_t nb_rules, struct rte_flow_error *error)
10694 {
10695 	const struct mlx5_flow_driver_ops *fops;
10696 
10697 	MLX5_DRV_FOPS_OR_ERR(dev, fops, table_resize, ENOTSUP);
10698 	return fops->table_resize(dev, table, nb_rules, error);
10699 }
10700 
10701 static int
10702 mlx5_table_resize_complete(struct rte_eth_dev *dev,
10703 			   struct rte_flow_template_table *table,
10704 			   struct rte_flow_error *error)
10705 {
10706 	const struct mlx5_flow_driver_ops *fops;
10707 
10708 	MLX5_DRV_FOPS_OR_ERR(dev, fops, table_resize_complete, ENOTSUP);
10709 	return fops->table_resize_complete(dev, table, error);
10710 }
10711 
10712 static int
10713 mlx5_flow_async_update_resized(struct rte_eth_dev *dev, uint32_t queue,
10714 			       const struct rte_flow_op_attr *op_attr,
10715 			       struct rte_flow *rule, void *user_data,
10716 			       struct rte_flow_error *error)
10717 {
10718 	const struct mlx5_flow_driver_ops *fops;
10719 
10720 	MLX5_DRV_FOPS_OR_ERR(dev, fops, flow_update_resized, ENOTSUP);
10721 	return fops->flow_update_resized(dev, queue, op_attr, rule, user_data, error);
10722 }
10723 
10724 /**
10725  * Destroy all indirect actions (shared RSS).
10726  *
10727  * @param dev
10728  *   Pointer to Ethernet device.
10729  *
10730  * @return
10731  *   0 on success, a negative errno value otherwise and rte_errno is set.
10732  */
10733 int
10734 mlx5_action_handle_flush(struct rte_eth_dev *dev)
10735 {
10736 	struct rte_flow_error error;
10737 	struct mlx5_priv *priv = dev->data->dev_private;
10738 	struct mlx5_shared_action_rss *shared_rss;
10739 	int ret = 0;
10740 	uint32_t idx;
10741 
10742 	ILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS],
10743 		      priv->rss_shared_actions, idx, shared_rss, next) {
10744 		ret |= mlx5_action_handle_destroy(dev,
10745 		       (struct rte_flow_action_handle *)(uintptr_t)idx, &error);
10746 	}
10747 	return ret;
10748 }
10749 
10750 /**
10751  * Validate existing indirect actions against current device configuration
10752  * and attach them to device resources.
10753  *
10754  * @param dev
10755  *   Pointer to Ethernet device.
10756  *
10757  * @return
10758  *   0 on success, a negative errno value otherwise and rte_errno is set.
10759  */
10760 int
10761 mlx5_action_handle_attach(struct rte_eth_dev *dev)
10762 {
10763 	struct mlx5_priv *priv = dev->data->dev_private;
10764 	int ret = 0;
10765 	struct mlx5_ind_table_obj *ind_tbl, *ind_tbl_last;
10766 
10767 	LIST_FOREACH(ind_tbl, &priv->standalone_ind_tbls, next) {
10768 		const char *message;
10769 		uint32_t queue_idx;
10770 
10771 		ret = mlx5_validate_rss_queues(dev, ind_tbl->queues,
10772 					       ind_tbl->queues_n,
10773 					       &message, &queue_idx);
10774 		if (ret != 0) {
10775 			DRV_LOG(ERR, "Port %u cannot use queue %u in RSS: %s",
10776 				dev->data->port_id, ind_tbl->queues[queue_idx],
10777 				message);
10778 			break;
10779 		}
10780 	}
10781 	if (ret != 0)
10782 		return ret;
10783 	LIST_FOREACH(ind_tbl, &priv->standalone_ind_tbls, next) {
10784 		ret = mlx5_ind_table_obj_attach(dev, ind_tbl);
10785 		if (ret != 0) {
10786 			DRV_LOG(ERR, "Port %u could not attach "
10787 				"indirection table obj %p",
10788 				dev->data->port_id, (void *)ind_tbl);
10789 			goto error;
10790 		}
10791 	}
10792 
10793 	return 0;
10794 error:
10795 	ind_tbl_last = ind_tbl;
10796 	LIST_FOREACH(ind_tbl, &priv->standalone_ind_tbls, next) {
10797 		if (ind_tbl == ind_tbl_last)
10798 			break;
10799 		if (mlx5_ind_table_obj_detach(dev, ind_tbl) != 0)
10800 			DRV_LOG(CRIT, "Port %u could not detach "
10801 				"indirection table obj %p on rollback",
10802 				dev->data->port_id, (void *)ind_tbl);
10803 	}
10804 	return ret;
10805 }
10806 
10807 /**
10808  * Detach indirect actions of the device from its resources.
10809  *
10810  * @param dev
10811  *   Pointer to Ethernet device.
10812  *
10813  * @return
10814  *   0 on success, a negative errno value otherwise and rte_errno is set.
10815  */
10816 int
10817 mlx5_action_handle_detach(struct rte_eth_dev *dev)
10818 {
10819 	struct mlx5_priv *priv = dev->data->dev_private;
10820 	int ret = 0;
10821 	struct mlx5_ind_table_obj *ind_tbl, *ind_tbl_last;
10822 
10823 	LIST_FOREACH(ind_tbl, &priv->standalone_ind_tbls, next) {
10824 		ret = mlx5_ind_table_obj_detach(dev, ind_tbl);
10825 		if (ret != 0) {
10826 			DRV_LOG(ERR, "Port %u could not detach "
10827 				"indirection table obj %p",
10828 				dev->data->port_id, (void *)ind_tbl);
10829 			goto error;
10830 		}
10831 	}
10832 	return 0;
10833 error:
10834 	ind_tbl_last = ind_tbl;
10835 	LIST_FOREACH(ind_tbl, &priv->standalone_ind_tbls, next) {
10836 		if (ind_tbl == ind_tbl_last)
10837 			break;
10838 		if (mlx5_ind_table_obj_attach(dev, ind_tbl) != 0)
10839 			DRV_LOG(CRIT, "Port %u could not attach "
10840 				"indirection table obj %p on rollback",
10841 				dev->data->port_id, (void *)ind_tbl);
10842 	}
10843 	return ret;
10844 }
10845 
10846 #ifndef HAVE_MLX5DV_DR
10847 #define MLX5_DOMAIN_SYNC_FLOW ((1 << 0) | (1 << 1))
10848 #else
10849 #define MLX5_DOMAIN_SYNC_FLOW \
10850 	(MLX5DV_DR_DOMAIN_SYNC_FLAGS_SW | MLX5DV_DR_DOMAIN_SYNC_FLAGS_HW)
10851 #endif
10852 
10853 int rte_pmd_mlx5_sync_flow(uint16_t port_id, uint32_t domains)
10854 {
10855 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
10856 	const struct mlx5_flow_driver_ops *fops;
10857 	int ret;
10858 	struct rte_flow_attr attr = { .transfer = 0 };
10859 
10860 	fops = flow_get_drv_ops(flow_get_drv_type(dev, &attr));
10861 	ret = fops->sync_domain(dev, domains, MLX5_DOMAIN_SYNC_FLOW);
10862 	if (ret > 0)
10863 		ret = -ret;
10864 	return ret;
10865 }
10866 
10867 const struct mlx5_flow_tunnel *
10868 mlx5_get_tof(const struct rte_flow_item *item,
10869 	     const struct rte_flow_action *action,
10870 	     enum mlx5_tof_rule_type *rule_type)
10871 {
10872 	for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
10873 		if (item->type == (typeof(item->type))
10874 				  MLX5_RTE_FLOW_ITEM_TYPE_TUNNEL) {
10875 			*rule_type = MLX5_TUNNEL_OFFLOAD_MATCH_RULE;
10876 			return flow_items_to_tunnel(item);
10877 		}
10878 	}
10879 	for (; action->conf != RTE_FLOW_ACTION_TYPE_END; action++) {
10880 		if (action->type == (typeof(action->type))
10881 				    MLX5_RTE_FLOW_ACTION_TYPE_TUNNEL_SET) {
10882 			*rule_type = MLX5_TUNNEL_OFFLOAD_SET_RULE;
10883 			return flow_actions_to_tunnel(action);
10884 		}
10885 	}
10886 	return NULL;
10887 }
10888 
10889 /**
10890  * tunnel offload functionality is defined for DV environment only
10891  */
10892 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
10893 __extension__
10894 union tunnel_offload_mark {
10895 	uint32_t val;
10896 	struct {
10897 		uint32_t app_reserve:8;
10898 		uint32_t table_id:15;
10899 		uint32_t transfer:1;
10900 		uint32_t _unused_:8;
10901 	};
10902 };
10903 
10904 static bool
10905 mlx5_access_tunnel_offload_db
10906 	(struct rte_eth_dev *dev,
10907 	 bool (*match)(struct rte_eth_dev *,
10908 		       struct mlx5_flow_tunnel *, const void *),
10909 	 void (*hit)(struct rte_eth_dev *, struct mlx5_flow_tunnel *, void *),
10910 	 void (*miss)(struct rte_eth_dev *, void *),
10911 	 void *ctx, bool lock_op);
10912 
10913 static int
10914 flow_tunnel_add_default_miss(struct rte_eth_dev *dev,
10915 			     struct rte_flow *flow,
10916 			     const struct rte_flow_attr *attr,
10917 			     const struct rte_flow_action *app_actions,
10918 			     uint32_t flow_idx,
10919 			     const struct mlx5_flow_tunnel *tunnel,
10920 			     struct tunnel_default_miss_ctx *ctx,
10921 			     struct rte_flow_error *error)
10922 {
10923 	struct mlx5_priv *priv = dev->data->dev_private;
10924 	struct mlx5_flow *dev_flow;
10925 	struct rte_flow_attr miss_attr = *attr;
10926 	const struct rte_flow_item miss_items[2] = {
10927 		{
10928 			.type = RTE_FLOW_ITEM_TYPE_ETH,
10929 			.spec = NULL,
10930 			.last = NULL,
10931 			.mask = NULL
10932 		},
10933 		{
10934 			.type = RTE_FLOW_ITEM_TYPE_END,
10935 			.spec = NULL,
10936 			.last = NULL,
10937 			.mask = NULL
10938 		}
10939 	};
10940 	union tunnel_offload_mark mark_id;
10941 	struct rte_flow_action_mark miss_mark;
10942 	struct rte_flow_action miss_actions[3] = {
10943 		[0] = { .type = RTE_FLOW_ACTION_TYPE_MARK, .conf = &miss_mark },
10944 		[2] = { .type = RTE_FLOW_ACTION_TYPE_END,  .conf = NULL }
10945 	};
10946 	const struct rte_flow_action_jump *jump_data;
10947 	uint32_t i, flow_table = 0; /* prevent compilation warning */
10948 	struct flow_grp_info grp_info = {
10949 		.external = 1,
10950 		.transfer = attr->transfer,
10951 		.fdb_def_rule = !!priv->fdb_def_rule,
10952 		.std_tbl_fix = 0,
10953 	};
10954 	int ret;
10955 
10956 	if (!attr->transfer) {
10957 		uint32_t q_size;
10958 
10959 		miss_actions[1].type = RTE_FLOW_ACTION_TYPE_RSS;
10960 		q_size = priv->reta_idx_n * sizeof(ctx->queue[0]);
10961 		ctx->queue = mlx5_malloc(MLX5_MEM_SYS | MLX5_MEM_ZERO, q_size,
10962 					 0, SOCKET_ID_ANY);
10963 		if (!ctx->queue)
10964 			return rte_flow_error_set
10965 				(error, ENOMEM,
10966 				RTE_FLOW_ERROR_TYPE_ACTION_CONF,
10967 				NULL, "invalid default miss RSS");
10968 		ctx->action_rss.func = RTE_ETH_HASH_FUNCTION_DEFAULT,
10969 		ctx->action_rss.level = 0,
10970 		ctx->action_rss.types = priv->rss_conf.rss_hf,
10971 		ctx->action_rss.key_len = priv->rss_conf.rss_key_len,
10972 		ctx->action_rss.queue_num = priv->reta_idx_n,
10973 		ctx->action_rss.key = priv->rss_conf.rss_key,
10974 		ctx->action_rss.queue = ctx->queue;
10975 		if (!priv->reta_idx_n || !priv->rxqs_n)
10976 			return rte_flow_error_set
10977 				(error, EINVAL,
10978 				RTE_FLOW_ERROR_TYPE_ACTION_CONF,
10979 				NULL, "invalid port configuration");
10980 		if (!(dev->data->dev_conf.rxmode.mq_mode & RTE_ETH_MQ_RX_RSS_FLAG))
10981 			ctx->action_rss.types = 0;
10982 		for (i = 0; i != priv->reta_idx_n; ++i)
10983 			ctx->queue[i] = (*priv->reta_idx)[i];
10984 	} else {
10985 		miss_actions[1].type = RTE_FLOW_ACTION_TYPE_JUMP;
10986 		ctx->miss_jump.group = MLX5_TNL_MISS_FDB_JUMP_GRP;
10987 	}
10988 	miss_actions[1].conf = (typeof(miss_actions[1].conf))ctx->raw;
10989 	for (; app_actions->type != RTE_FLOW_ACTION_TYPE_JUMP; app_actions++);
10990 	jump_data = app_actions->conf;
10991 	miss_attr.priority = MLX5_TNL_MISS_RULE_PRIORITY;
10992 	miss_attr.group = jump_data->group;
10993 	ret = mlx5_flow_group_to_table(dev, tunnel, jump_data->group,
10994 				       &flow_table, &grp_info, error);
10995 	if (ret)
10996 		return rte_flow_error_set(error, EINVAL,
10997 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF,
10998 					  NULL, "invalid tunnel id");
10999 	mark_id.app_reserve = 0;
11000 	mark_id.table_id = tunnel_flow_tbl_to_id(flow_table);
11001 	mark_id.transfer = !!attr->transfer;
11002 	mark_id._unused_ = 0;
11003 	miss_mark.id = mark_id.val;
11004 	dev_flow = flow_drv_prepare(dev, flow, &miss_attr,
11005 				    miss_items, miss_actions, flow_idx, error);
11006 	if (!dev_flow)
11007 		return -rte_errno;
11008 	dev_flow->flow = flow;
11009 	dev_flow->external = true;
11010 	dev_flow->tunnel = tunnel;
11011 	dev_flow->tof_type = MLX5_TUNNEL_OFFLOAD_MISS_RULE;
11012 	/* Subflow object was created, we must include one in the list. */
11013 	SILIST_INSERT(&flow->dev_handles, dev_flow->handle_idx,
11014 		      dev_flow->handle, next);
11015 	DRV_LOG(DEBUG,
11016 		"port %u tunnel type=%d id=%u miss rule priority=%u group=%u",
11017 		dev->data->port_id, tunnel->app_tunnel.type,
11018 		tunnel->tunnel_id, miss_attr.priority, miss_attr.group);
11019 	ret = flow_drv_translate(dev, dev_flow, &miss_attr, miss_items,
11020 				  miss_actions, error);
11021 	if (!ret)
11022 		ret = flow_mreg_update_copy_table(dev, flow, miss_actions,
11023 						  error);
11024 
11025 	return ret;
11026 }
11027 
11028 static const struct mlx5_flow_tbl_data_entry  *
11029 tunnel_mark_decode(struct rte_eth_dev *dev, uint32_t mark)
11030 {
11031 	struct mlx5_priv *priv = dev->data->dev_private;
11032 	struct mlx5_dev_ctx_shared *sh = priv->sh;
11033 	struct mlx5_list_entry *he;
11034 	union tunnel_offload_mark mbits = { .val = mark };
11035 	union mlx5_flow_tbl_key table_key = {
11036 		{
11037 			.level = tunnel_id_to_flow_tbl(mbits.table_id),
11038 			.id = 0,
11039 			.reserved = 0,
11040 			.dummy = 0,
11041 			.is_fdb = !!mbits.transfer,
11042 			.is_egress = 0,
11043 		}
11044 	};
11045 	struct mlx5_flow_cb_ctx ctx = {
11046 		.data = &table_key.v64,
11047 	};
11048 
11049 	he = mlx5_hlist_lookup(sh->flow_tbls, table_key.v64, &ctx);
11050 	return he ?
11051 	       container_of(he, struct mlx5_flow_tbl_data_entry, entry) : NULL;
11052 }
11053 
11054 static void
11055 mlx5_flow_tunnel_grp2tbl_remove_cb(void *tool_ctx,
11056 				   struct mlx5_list_entry *entry)
11057 {
11058 	struct mlx5_dev_ctx_shared *sh = tool_ctx;
11059 	struct tunnel_tbl_entry *tte = container_of(entry, typeof(*tte), hash);
11060 
11061 	mlx5_ipool_free(sh->ipool[MLX5_IPOOL_TNL_TBL_ID],
11062 			tunnel_flow_tbl_to_id(tte->flow_table));
11063 	mlx5_free(tte);
11064 }
11065 
11066 static int
11067 mlx5_flow_tunnel_grp2tbl_match_cb(void *tool_ctx __rte_unused,
11068 				  struct mlx5_list_entry *entry, void *cb_ctx)
11069 {
11070 	struct mlx5_flow_cb_ctx *ctx = cb_ctx;
11071 	union tunnel_tbl_key tbl = {
11072 		.val = *(uint64_t *)(ctx->data),
11073 	};
11074 	struct tunnel_tbl_entry *tte = container_of(entry, typeof(*tte), hash);
11075 
11076 	return tbl.tunnel_id != tte->tunnel_id || tbl.group != tte->group;
11077 }
11078 
11079 static struct mlx5_list_entry *
11080 mlx5_flow_tunnel_grp2tbl_create_cb(void *tool_ctx, void *cb_ctx)
11081 {
11082 	struct mlx5_dev_ctx_shared *sh = tool_ctx;
11083 	struct mlx5_flow_cb_ctx *ctx = cb_ctx;
11084 	struct tunnel_tbl_entry *tte;
11085 	union tunnel_tbl_key tbl = {
11086 		.val = *(uint64_t *)(ctx->data),
11087 	};
11088 
11089 	tte = mlx5_malloc(MLX5_MEM_SYS | MLX5_MEM_ZERO,
11090 			  sizeof(*tte), 0,
11091 			  SOCKET_ID_ANY);
11092 	if (!tte)
11093 		goto err;
11094 	mlx5_ipool_malloc(sh->ipool[MLX5_IPOOL_TNL_TBL_ID],
11095 			  &tte->flow_table);
11096 	if (tte->flow_table >= MLX5_MAX_TABLES) {
11097 		DRV_LOG(ERR, "Tunnel TBL ID %d exceed max limit.",
11098 			tte->flow_table);
11099 		mlx5_ipool_free(sh->ipool[MLX5_IPOOL_TNL_TBL_ID],
11100 				tte->flow_table);
11101 		goto err;
11102 	} else if (!tte->flow_table) {
11103 		goto err;
11104 	}
11105 	tte->flow_table = tunnel_id_to_flow_tbl(tte->flow_table);
11106 	tte->tunnel_id = tbl.tunnel_id;
11107 	tte->group = tbl.group;
11108 	return &tte->hash;
11109 err:
11110 	if (tte)
11111 		mlx5_free(tte);
11112 	return NULL;
11113 }
11114 
11115 static struct mlx5_list_entry *
11116 mlx5_flow_tunnel_grp2tbl_clone_cb(void *tool_ctx __rte_unused,
11117 				  struct mlx5_list_entry *oentry,
11118 				  void *cb_ctx __rte_unused)
11119 {
11120 	struct tunnel_tbl_entry *tte = mlx5_malloc(MLX5_MEM_SYS, sizeof(*tte),
11121 						   0, SOCKET_ID_ANY);
11122 
11123 	if (!tte)
11124 		return NULL;
11125 	memcpy(tte, oentry, sizeof(*tte));
11126 	return &tte->hash;
11127 }
11128 
11129 static void
11130 mlx5_flow_tunnel_grp2tbl_clone_free_cb(void *tool_ctx __rte_unused,
11131 				       struct mlx5_list_entry *entry)
11132 {
11133 	struct tunnel_tbl_entry *tte = container_of(entry, typeof(*tte), hash);
11134 
11135 	mlx5_free(tte);
11136 }
11137 
11138 static uint32_t
11139 tunnel_flow_group_to_flow_table(struct rte_eth_dev *dev,
11140 				const struct mlx5_flow_tunnel *tunnel,
11141 				uint32_t group, uint32_t *table,
11142 				struct rte_flow_error *error)
11143 {
11144 	struct mlx5_list_entry *he;
11145 	struct tunnel_tbl_entry *tte;
11146 	union tunnel_tbl_key key = {
11147 		.tunnel_id = tunnel ? tunnel->tunnel_id : 0,
11148 		.group = group
11149 	};
11150 	struct mlx5_flow_tunnel_hub *thub = mlx5_tunnel_hub(dev);
11151 	struct mlx5_hlist *group_hash;
11152 	struct mlx5_flow_cb_ctx ctx = {
11153 		.data = &key.val,
11154 	};
11155 
11156 	group_hash = tunnel ? tunnel->groups : thub->groups;
11157 	he = mlx5_hlist_register(group_hash, key.val, &ctx);
11158 	if (!he)
11159 		return rte_flow_error_set(error, EINVAL,
11160 					  RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
11161 					  NULL,
11162 					  "tunnel group index not supported");
11163 	tte = container_of(he, typeof(*tte), hash);
11164 	*table = tte->flow_table;
11165 	DRV_LOG(DEBUG, "port %u tunnel %u group=%#x table=%#x",
11166 		dev->data->port_id, key.tunnel_id, group, *table);
11167 	return 0;
11168 }
11169 
11170 static void
11171 mlx5_flow_tunnel_free(struct rte_eth_dev *dev,
11172 		      struct mlx5_flow_tunnel *tunnel)
11173 {
11174 	struct mlx5_priv *priv = dev->data->dev_private;
11175 	struct mlx5_indexed_pool *ipool;
11176 
11177 	DRV_LOG(DEBUG, "port %u release pmd tunnel id=0x%x",
11178 		dev->data->port_id, tunnel->tunnel_id);
11179 	LIST_REMOVE(tunnel, chain);
11180 	mlx5_hlist_destroy(tunnel->groups);
11181 	ipool = priv->sh->ipool[MLX5_IPOOL_TUNNEL_ID];
11182 	mlx5_ipool_free(ipool, tunnel->tunnel_id);
11183 }
11184 
11185 static bool
11186 mlx5_access_tunnel_offload_db
11187 	(struct rte_eth_dev *dev,
11188 	 bool (*match)(struct rte_eth_dev *,
11189 		       struct mlx5_flow_tunnel *, const void *),
11190 	 void (*hit)(struct rte_eth_dev *, struct mlx5_flow_tunnel *, void *),
11191 	 void (*miss)(struct rte_eth_dev *, void *),
11192 	 void *ctx, bool lock_op)
11193 {
11194 	bool verdict = false;
11195 	struct mlx5_flow_tunnel_hub *thub = mlx5_tunnel_hub(dev);
11196 	struct mlx5_flow_tunnel *tunnel;
11197 
11198 	rte_spinlock_lock(&thub->sl);
11199 	LIST_FOREACH(tunnel, &thub->tunnels, chain) {
11200 		verdict = match(dev, tunnel, (const void *)ctx);
11201 		if (verdict)
11202 			break;
11203 	}
11204 	if (!lock_op)
11205 		rte_spinlock_unlock(&thub->sl);
11206 	if (verdict && hit)
11207 		hit(dev, tunnel, ctx);
11208 	if (!verdict && miss)
11209 		miss(dev, ctx);
11210 	if (lock_op)
11211 		rte_spinlock_unlock(&thub->sl);
11212 
11213 	return verdict;
11214 }
11215 
11216 struct tunnel_db_find_tunnel_id_ctx {
11217 	uint32_t tunnel_id;
11218 	struct mlx5_flow_tunnel *tunnel;
11219 };
11220 
11221 static bool
11222 find_tunnel_id_match(struct rte_eth_dev *dev,
11223 		     struct mlx5_flow_tunnel *tunnel, const void *x)
11224 {
11225 	const struct tunnel_db_find_tunnel_id_ctx *ctx = x;
11226 
11227 	RTE_SET_USED(dev);
11228 	return tunnel->tunnel_id == ctx->tunnel_id;
11229 }
11230 
11231 static void
11232 find_tunnel_id_hit(struct rte_eth_dev *dev,
11233 		   struct mlx5_flow_tunnel *tunnel, void *x)
11234 {
11235 	struct tunnel_db_find_tunnel_id_ctx *ctx = x;
11236 	RTE_SET_USED(dev);
11237 	ctx->tunnel = tunnel;
11238 }
11239 
11240 static struct mlx5_flow_tunnel *
11241 mlx5_find_tunnel_id(struct rte_eth_dev *dev, uint32_t id)
11242 {
11243 	struct tunnel_db_find_tunnel_id_ctx ctx = {
11244 		.tunnel_id = id,
11245 	};
11246 
11247 	mlx5_access_tunnel_offload_db(dev, find_tunnel_id_match,
11248 				      find_tunnel_id_hit, NULL, &ctx, true);
11249 
11250 	return ctx.tunnel;
11251 }
11252 
11253 static struct mlx5_flow_tunnel *
11254 mlx5_flow_tunnel_allocate(struct rte_eth_dev *dev,
11255 			  const struct rte_flow_tunnel *app_tunnel)
11256 {
11257 	struct mlx5_priv *priv = dev->data->dev_private;
11258 	struct mlx5_indexed_pool *ipool;
11259 	struct mlx5_flow_tunnel *tunnel;
11260 	uint32_t id;
11261 
11262 	ipool = priv->sh->ipool[MLX5_IPOOL_TUNNEL_ID];
11263 	tunnel = mlx5_ipool_zmalloc(ipool, &id);
11264 	if (!tunnel)
11265 		return NULL;
11266 	if (id >= MLX5_MAX_TUNNELS) {
11267 		mlx5_ipool_free(ipool, id);
11268 		DRV_LOG(ERR, "Tunnel ID %d exceed max limit.", id);
11269 		return NULL;
11270 	}
11271 	tunnel->groups = mlx5_hlist_create("tunnel groups", 64, false, true,
11272 					   priv->sh,
11273 					   mlx5_flow_tunnel_grp2tbl_create_cb,
11274 					   mlx5_flow_tunnel_grp2tbl_match_cb,
11275 					   mlx5_flow_tunnel_grp2tbl_remove_cb,
11276 					   mlx5_flow_tunnel_grp2tbl_clone_cb,
11277 					mlx5_flow_tunnel_grp2tbl_clone_free_cb);
11278 	if (!tunnel->groups) {
11279 		mlx5_ipool_free(ipool, id);
11280 		return NULL;
11281 	}
11282 	/* initiate new PMD tunnel */
11283 	memcpy(&tunnel->app_tunnel, app_tunnel, sizeof(*app_tunnel));
11284 	tunnel->tunnel_id = id;
11285 	tunnel->action.type = (typeof(tunnel->action.type))
11286 			      MLX5_RTE_FLOW_ACTION_TYPE_TUNNEL_SET;
11287 	tunnel->action.conf = tunnel;
11288 	tunnel->item.type = (typeof(tunnel->item.type))
11289 			    MLX5_RTE_FLOW_ITEM_TYPE_TUNNEL;
11290 	tunnel->item.spec = tunnel;
11291 	tunnel->item.last = NULL;
11292 	tunnel->item.mask = NULL;
11293 
11294 	DRV_LOG(DEBUG, "port %u new pmd tunnel id=0x%x",
11295 		dev->data->port_id, tunnel->tunnel_id);
11296 
11297 	return tunnel;
11298 }
11299 
11300 struct tunnel_db_get_tunnel_ctx {
11301 	const struct rte_flow_tunnel *app_tunnel;
11302 	struct mlx5_flow_tunnel *tunnel;
11303 };
11304 
11305 static bool get_tunnel_match(struct rte_eth_dev *dev,
11306 			     struct mlx5_flow_tunnel *tunnel, const void *x)
11307 {
11308 	const struct tunnel_db_get_tunnel_ctx *ctx = x;
11309 
11310 	RTE_SET_USED(dev);
11311 	return !memcmp(ctx->app_tunnel, &tunnel->app_tunnel,
11312 		       sizeof(*ctx->app_tunnel));
11313 }
11314 
11315 static void get_tunnel_hit(struct rte_eth_dev *dev,
11316 			   struct mlx5_flow_tunnel *tunnel, void *x)
11317 {
11318 	/* called under tunnel spinlock protection */
11319 	struct tunnel_db_get_tunnel_ctx *ctx = x;
11320 
11321 	RTE_SET_USED(dev);
11322 	tunnel->refctn++;
11323 	ctx->tunnel = tunnel;
11324 }
11325 
11326 static void get_tunnel_miss(struct rte_eth_dev *dev, void *x)
11327 {
11328 	/* called under tunnel spinlock protection */
11329 	struct mlx5_flow_tunnel_hub *thub = mlx5_tunnel_hub(dev);
11330 	struct tunnel_db_get_tunnel_ctx *ctx = x;
11331 
11332 	rte_spinlock_unlock(&thub->sl);
11333 	ctx->tunnel = mlx5_flow_tunnel_allocate(dev, ctx->app_tunnel);
11334 	rte_spinlock_lock(&thub->sl);
11335 	if (ctx->tunnel) {
11336 		ctx->tunnel->refctn = 1;
11337 		LIST_INSERT_HEAD(&thub->tunnels, ctx->tunnel, chain);
11338 	}
11339 }
11340 
11341 
11342 static int
11343 mlx5_get_flow_tunnel(struct rte_eth_dev *dev,
11344 		     const struct rte_flow_tunnel *app_tunnel,
11345 		     struct mlx5_flow_tunnel **tunnel)
11346 {
11347 	struct tunnel_db_get_tunnel_ctx ctx = {
11348 		.app_tunnel = app_tunnel,
11349 	};
11350 
11351 	mlx5_access_tunnel_offload_db(dev, get_tunnel_match, get_tunnel_hit,
11352 				      get_tunnel_miss, &ctx, true);
11353 	*tunnel = ctx.tunnel;
11354 	return ctx.tunnel ? 0 : -ENOMEM;
11355 }
11356 
11357 void mlx5_release_tunnel_hub(struct mlx5_dev_ctx_shared *sh, uint16_t port_id)
11358 {
11359 	struct mlx5_flow_tunnel_hub *thub = sh->tunnel_hub;
11360 
11361 	if (!thub)
11362 		return;
11363 	if (!LIST_EMPTY(&thub->tunnels))
11364 		DRV_LOG(WARNING, "port %u tunnels present", port_id);
11365 	mlx5_hlist_destroy(thub->groups);
11366 	mlx5_free(thub);
11367 }
11368 
11369 int mlx5_alloc_tunnel_hub(struct mlx5_dev_ctx_shared *sh)
11370 {
11371 	int err;
11372 	struct mlx5_flow_tunnel_hub *thub;
11373 
11374 	thub = mlx5_malloc(MLX5_MEM_SYS | MLX5_MEM_ZERO, sizeof(*thub),
11375 			   0, SOCKET_ID_ANY);
11376 	if (!thub)
11377 		return -ENOMEM;
11378 	LIST_INIT(&thub->tunnels);
11379 	rte_spinlock_init(&thub->sl);
11380 	thub->groups = mlx5_hlist_create("flow groups", 64,
11381 					 false, true, sh,
11382 					 mlx5_flow_tunnel_grp2tbl_create_cb,
11383 					 mlx5_flow_tunnel_grp2tbl_match_cb,
11384 					 mlx5_flow_tunnel_grp2tbl_remove_cb,
11385 					 mlx5_flow_tunnel_grp2tbl_clone_cb,
11386 					mlx5_flow_tunnel_grp2tbl_clone_free_cb);
11387 	if (!thub->groups) {
11388 		err = -rte_errno;
11389 		goto err;
11390 	}
11391 	sh->tunnel_hub = thub;
11392 
11393 	return 0;
11394 
11395 err:
11396 	if (thub->groups)
11397 		mlx5_hlist_destroy(thub->groups);
11398 	if (thub)
11399 		mlx5_free(thub);
11400 	return err;
11401 }
11402 
11403 static inline int
11404 mlx5_flow_tunnel_validate(struct rte_eth_dev *dev,
11405 			  struct rte_flow_tunnel *tunnel,
11406 			  struct rte_flow_error *error)
11407 {
11408 	struct mlx5_priv *priv = dev->data->dev_private;
11409 
11410 	if (!priv->sh->config.dv_flow_en)
11411 		return rte_flow_error_set(error, ENOTSUP,
11412 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
11413 					  "flow DV interface is off");
11414 	if (!is_tunnel_offload_active(dev))
11415 		return rte_flow_error_set(error, ENOTSUP,
11416 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
11417 					  "tunnel offload was not activated, consider setting dv_xmeta_en=3");
11418 	if (!tunnel)
11419 		return rte_flow_error_set(error, EINVAL,
11420 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
11421 					  "no application tunnel");
11422 	switch (tunnel->type) {
11423 	default:
11424 		return rte_flow_error_set(error, EINVAL,
11425 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
11426 					  "unsupported tunnel type");
11427 	case RTE_FLOW_ITEM_TYPE_VXLAN:
11428 	case RTE_FLOW_ITEM_TYPE_GRE:
11429 	case RTE_FLOW_ITEM_TYPE_NVGRE:
11430 	case RTE_FLOW_ITEM_TYPE_GENEVE:
11431 		break;
11432 	}
11433 	return 0;
11434 }
11435 
11436 static int
11437 mlx5_flow_tunnel_decap_set(struct rte_eth_dev *dev,
11438 		    struct rte_flow_tunnel *app_tunnel,
11439 		    struct rte_flow_action **actions,
11440 		    uint32_t *num_of_actions,
11441 		    struct rte_flow_error *error)
11442 {
11443 	struct mlx5_flow_tunnel *tunnel;
11444 	int ret = mlx5_flow_tunnel_validate(dev, app_tunnel, error);
11445 
11446 	if (ret)
11447 		return ret;
11448 	ret = mlx5_get_flow_tunnel(dev, app_tunnel, &tunnel);
11449 	if (ret < 0) {
11450 		return rte_flow_error_set(error, ret,
11451 					  RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
11452 					  "failed to initialize pmd tunnel");
11453 	}
11454 	*actions = &tunnel->action;
11455 	*num_of_actions = 1;
11456 	return 0;
11457 }
11458 
11459 static int
11460 mlx5_flow_tunnel_match(struct rte_eth_dev *dev,
11461 		       struct rte_flow_tunnel *app_tunnel,
11462 		       struct rte_flow_item **items,
11463 		       uint32_t *num_of_items,
11464 		       struct rte_flow_error *error)
11465 {
11466 	struct mlx5_flow_tunnel *tunnel;
11467 	int ret = mlx5_flow_tunnel_validate(dev, app_tunnel, error);
11468 
11469 	if (ret)
11470 		return ret;
11471 	ret = mlx5_get_flow_tunnel(dev, app_tunnel, &tunnel);
11472 	if (ret < 0) {
11473 		return rte_flow_error_set(error, ret,
11474 					  RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
11475 					  "failed to initialize pmd tunnel");
11476 	}
11477 	*items = &tunnel->item;
11478 	*num_of_items = 1;
11479 	return 0;
11480 }
11481 
11482 struct tunnel_db_element_release_ctx {
11483 	struct rte_flow_item *items;
11484 	struct rte_flow_action *actions;
11485 	uint32_t num_elements;
11486 	struct rte_flow_error *error;
11487 	int ret;
11488 };
11489 
11490 static bool
11491 tunnel_element_release_match(struct rte_eth_dev *dev,
11492 			     struct mlx5_flow_tunnel *tunnel, const void *x)
11493 {
11494 	const struct tunnel_db_element_release_ctx *ctx = x;
11495 
11496 	RTE_SET_USED(dev);
11497 	if (ctx->num_elements != 1)
11498 		return false;
11499 	else if (ctx->items)
11500 		return ctx->items == &tunnel->item;
11501 	else if (ctx->actions)
11502 		return ctx->actions == &tunnel->action;
11503 
11504 	return false;
11505 }
11506 
11507 static void
11508 tunnel_element_release_hit(struct rte_eth_dev *dev,
11509 			   struct mlx5_flow_tunnel *tunnel, void *x)
11510 {
11511 	struct tunnel_db_element_release_ctx *ctx = x;
11512 	ctx->ret = 0;
11513 	if (!(rte_atomic_fetch_sub_explicit(&tunnel->refctn, 1, rte_memory_order_relaxed) - 1))
11514 		mlx5_flow_tunnel_free(dev, tunnel);
11515 }
11516 
11517 static void
11518 tunnel_element_release_miss(struct rte_eth_dev *dev, void *x)
11519 {
11520 	struct tunnel_db_element_release_ctx *ctx = x;
11521 	RTE_SET_USED(dev);
11522 	ctx->ret = rte_flow_error_set(ctx->error, EINVAL,
11523 				      RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
11524 				      "invalid argument");
11525 }
11526 
11527 static int
11528 mlx5_flow_tunnel_item_release(struct rte_eth_dev *dev,
11529 		       struct rte_flow_item *pmd_items,
11530 		       uint32_t num_items, struct rte_flow_error *err)
11531 {
11532 	struct tunnel_db_element_release_ctx ctx = {
11533 		.items = pmd_items,
11534 		.actions = NULL,
11535 		.num_elements = num_items,
11536 		.error = err,
11537 	};
11538 
11539 	mlx5_access_tunnel_offload_db(dev, tunnel_element_release_match,
11540 				      tunnel_element_release_hit,
11541 				      tunnel_element_release_miss, &ctx, false);
11542 
11543 	return ctx.ret;
11544 }
11545 
11546 static int
11547 mlx5_flow_tunnel_action_release(struct rte_eth_dev *dev,
11548 			 struct rte_flow_action *pmd_actions,
11549 			 uint32_t num_actions, struct rte_flow_error *err)
11550 {
11551 	struct tunnel_db_element_release_ctx ctx = {
11552 		.items = NULL,
11553 		.actions = pmd_actions,
11554 		.num_elements = num_actions,
11555 		.error = err,
11556 	};
11557 
11558 	mlx5_access_tunnel_offload_db(dev, tunnel_element_release_match,
11559 				      tunnel_element_release_hit,
11560 				      tunnel_element_release_miss, &ctx, false);
11561 
11562 	return ctx.ret;
11563 }
11564 
11565 static int
11566 mlx5_flow_tunnel_get_restore_info(struct rte_eth_dev *dev,
11567 				  struct rte_mbuf *m,
11568 				  struct rte_flow_restore_info *info,
11569 				  struct rte_flow_error *err)
11570 {
11571 	uint64_t ol_flags = m->ol_flags;
11572 	const struct mlx5_flow_tbl_data_entry *tble;
11573 	const uint64_t mask = RTE_MBUF_F_RX_FDIR | RTE_MBUF_F_RX_FDIR_ID;
11574 	struct mlx5_priv *priv = dev->data->dev_private;
11575 
11576 	if (priv->tunnel_enabled == 0)
11577 		goto err;
11578 	if ((ol_flags & mask) != mask)
11579 		goto err;
11580 	tble = tunnel_mark_decode(dev, m->hash.fdir.hi);
11581 	if (!tble) {
11582 		DRV_LOG(DEBUG, "port %u invalid miss tunnel mark %#x",
11583 			dev->data->port_id, m->hash.fdir.hi);
11584 		goto err;
11585 	}
11586 	MLX5_ASSERT(tble->tunnel);
11587 	memcpy(&info->tunnel, &tble->tunnel->app_tunnel, sizeof(info->tunnel));
11588 	info->group_id = tble->group_id;
11589 	info->flags = RTE_FLOW_RESTORE_INFO_TUNNEL |
11590 		      RTE_FLOW_RESTORE_INFO_GROUP_ID |
11591 		      RTE_FLOW_RESTORE_INFO_ENCAPSULATED;
11592 
11593 	return 0;
11594 
11595 err:
11596 	return rte_flow_error_set(err, EINVAL,
11597 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
11598 				  "failed to get restore info");
11599 }
11600 
11601 #else /* HAVE_IBV_FLOW_DV_SUPPORT */
11602 static int
11603 mlx5_flow_tunnel_decap_set(__rte_unused struct rte_eth_dev *dev,
11604 			   __rte_unused struct rte_flow_tunnel *app_tunnel,
11605 			   __rte_unused struct rte_flow_action **actions,
11606 			   __rte_unused uint32_t *num_of_actions,
11607 			   __rte_unused struct rte_flow_error *error)
11608 {
11609 	return -ENOTSUP;
11610 }
11611 
11612 static int
11613 mlx5_flow_tunnel_match(__rte_unused struct rte_eth_dev *dev,
11614 		       __rte_unused struct rte_flow_tunnel *app_tunnel,
11615 		       __rte_unused struct rte_flow_item **items,
11616 		       __rte_unused uint32_t *num_of_items,
11617 		       __rte_unused struct rte_flow_error *error)
11618 {
11619 	return -ENOTSUP;
11620 }
11621 
11622 static int
11623 mlx5_flow_tunnel_item_release(__rte_unused struct rte_eth_dev *dev,
11624 			      __rte_unused struct rte_flow_item *pmd_items,
11625 			      __rte_unused uint32_t num_items,
11626 			      __rte_unused struct rte_flow_error *err)
11627 {
11628 	return -ENOTSUP;
11629 }
11630 
11631 static int
11632 mlx5_flow_tunnel_action_release(__rte_unused struct rte_eth_dev *dev,
11633 				__rte_unused struct rte_flow_action *pmd_action,
11634 				__rte_unused uint32_t num_actions,
11635 				__rte_unused struct rte_flow_error *err)
11636 {
11637 	return -ENOTSUP;
11638 }
11639 
11640 static int
11641 mlx5_flow_tunnel_get_restore_info(__rte_unused struct rte_eth_dev *dev,
11642 				  __rte_unused struct rte_mbuf *m,
11643 				  __rte_unused struct rte_flow_restore_info *i,
11644 				  __rte_unused struct rte_flow_error *err)
11645 {
11646 	return -ENOTSUP;
11647 }
11648 
11649 static int
11650 flow_tunnel_add_default_miss(__rte_unused struct rte_eth_dev *dev,
11651 			     __rte_unused struct rte_flow *flow,
11652 			     __rte_unused const struct rte_flow_attr *attr,
11653 			     __rte_unused const struct rte_flow_action *actions,
11654 			     __rte_unused uint32_t flow_idx,
11655 			     __rte_unused const struct mlx5_flow_tunnel *tunnel,
11656 			     __rte_unused struct tunnel_default_miss_ctx *ctx,
11657 			     __rte_unused struct rte_flow_error *error)
11658 {
11659 	return -ENOTSUP;
11660 }
11661 
11662 static struct mlx5_flow_tunnel *
11663 mlx5_find_tunnel_id(__rte_unused struct rte_eth_dev *dev,
11664 		    __rte_unused uint32_t id)
11665 {
11666 	return NULL;
11667 }
11668 
11669 static void
11670 mlx5_flow_tunnel_free(__rte_unused struct rte_eth_dev *dev,
11671 		      __rte_unused struct mlx5_flow_tunnel *tunnel)
11672 {
11673 }
11674 
11675 static uint32_t
11676 tunnel_flow_group_to_flow_table(__rte_unused struct rte_eth_dev *dev,
11677 				__rte_unused const struct mlx5_flow_tunnel *t,
11678 				__rte_unused uint32_t group,
11679 				__rte_unused uint32_t *table,
11680 				struct rte_flow_error *error)
11681 {
11682 	return rte_flow_error_set(error, ENOTSUP,
11683 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
11684 				  "tunnel offload requires DV support");
11685 }
11686 
11687 void
11688 mlx5_release_tunnel_hub(__rte_unused struct mlx5_dev_ctx_shared *sh,
11689 			__rte_unused  uint16_t port_id)
11690 {
11691 }
11692 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */
11693 
11694 /* Flex flow item API */
11695 static struct rte_flow_item_flex_handle *
11696 mlx5_flow_flex_item_create(struct rte_eth_dev *dev,
11697 			   const struct rte_flow_item_flex_conf *conf,
11698 			   struct rte_flow_error *error)
11699 {
11700 	static const char err_msg[] = "flex item creation unsupported";
11701 	struct mlx5_priv *priv = dev->data->dev_private;
11702 	struct rte_flow_attr attr = { .transfer = 0 };
11703 	const struct mlx5_flow_driver_ops *fops =
11704 			flow_get_drv_ops(flow_get_drv_type(dev, &attr));
11705 
11706 	if (!priv->pci_dev) {
11707 		rte_flow_error_set(error, ENOTSUP,
11708 				   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
11709 				   "create flex item on PF only");
11710 		return NULL;
11711 	}
11712 	switch (priv->pci_dev->id.device_id) {
11713 	case PCI_DEVICE_ID_MELLANOX_BLUEFIELD2:
11714 	case PCI_DEVICE_ID_MELLANOX_BLUEFIELD3:
11715 		break;
11716 	default:
11717 		rte_flow_error_set(error, ENOTSUP,
11718 				   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
11719 				   "flex item available on BlueField ports only");
11720 		return NULL;
11721 	}
11722 	if (!fops->item_create) {
11723 		DRV_LOG(ERR, "port %u %s.", dev->data->port_id, err_msg);
11724 		rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
11725 				   NULL, err_msg);
11726 		return NULL;
11727 	}
11728 	return fops->item_create(dev, conf, error);
11729 }
11730 
11731 static int
11732 mlx5_flow_flex_item_release(struct rte_eth_dev *dev,
11733 			    const struct rte_flow_item_flex_handle *handle,
11734 			    struct rte_flow_error *error)
11735 {
11736 	static const char err_msg[] = "flex item release unsupported";
11737 	struct rte_flow_attr attr = { .transfer = 0 };
11738 	const struct mlx5_flow_driver_ops *fops =
11739 			flow_get_drv_ops(flow_get_drv_type(dev, &attr));
11740 
11741 	if (!fops->item_release) {
11742 		DRV_LOG(ERR, "port %u %s.", dev->data->port_id, err_msg);
11743 		rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
11744 				   NULL, err_msg);
11745 		return -rte_errno;
11746 	}
11747 	return fops->item_release(dev, handle, error);
11748 }
11749 
11750 static void
11751 mlx5_dbg__print_pattern(const struct rte_flow_item *item)
11752 {
11753 	int ret;
11754 	struct rte_flow_error error;
11755 
11756 	for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
11757 		char *item_name;
11758 		ret = rte_flow_conv(RTE_FLOW_CONV_OP_ITEM_NAME_PTR, &item_name,
11759 				    sizeof(item_name),
11760 				    (void *)(uintptr_t)item->type, &error);
11761 		if (ret > 0)
11762 			printf("%s ", item_name);
11763 		else
11764 			printf("%d\n", (int)item->type);
11765 	}
11766 	printf("END\n");
11767 }
11768 
11769 static int
11770 mlx5_flow_is_std_vxlan_port(const struct rte_flow_item *udp_item)
11771 {
11772 	const struct rte_flow_item_udp *spec = udp_item->spec;
11773 	const struct rte_flow_item_udp *mask = udp_item->mask;
11774 	uint16_t udp_dport = 0;
11775 
11776 	if (spec != NULL) {
11777 		if (!mask)
11778 			mask = &rte_flow_item_udp_mask;
11779 		udp_dport = rte_be_to_cpu_16(spec->hdr.dst_port &
11780 				mask->hdr.dst_port);
11781 	}
11782 	return (!udp_dport || udp_dport == MLX5_UDP_PORT_VXLAN);
11783 }
11784 
11785 static const struct mlx5_flow_expand_node *
11786 mlx5_flow_expand_rss_adjust_node(const struct rte_flow_item *pattern,
11787 		unsigned int item_idx,
11788 		const struct mlx5_flow_expand_node graph[],
11789 		const struct mlx5_flow_expand_node *node)
11790 {
11791 	const struct rte_flow_item *item = pattern + item_idx, *prev_item;
11792 
11793 	if (item->type == RTE_FLOW_ITEM_TYPE_VXLAN &&
11794 			node != NULL &&
11795 			node->type == RTE_FLOW_ITEM_TYPE_VXLAN) {
11796 		/*
11797 		 * The expansion node is VXLAN and it is also the last
11798 		 * expandable item in the pattern, so need to continue
11799 		 * expansion of the inner tunnel.
11800 		 */
11801 		MLX5_ASSERT(item_idx > 0);
11802 		prev_item = pattern + item_idx - 1;
11803 		MLX5_ASSERT(prev_item->type == RTE_FLOW_ITEM_TYPE_UDP);
11804 		if (mlx5_flow_is_std_vxlan_port(prev_item))
11805 			return &graph[MLX5_EXPANSION_STD_VXLAN];
11806 		return &graph[MLX5_EXPANSION_L3_VXLAN];
11807 	}
11808 	return node;
11809 }
11810 
11811 /* Map of Verbs to Flow priority with 8 Verbs priorities. */
11812 static const uint32_t priority_map_3[][MLX5_PRIORITY_MAP_MAX] = {
11813 	{ 0, 1, 2 }, { 2, 3, 4 }, { 5, 6, 7 },
11814 };
11815 
11816 /* Map of Verbs to Flow priority with 16 Verbs priorities. */
11817 static const uint32_t priority_map_5[][MLX5_PRIORITY_MAP_MAX] = {
11818 	{ 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 },
11819 	{ 9, 10, 11 }, { 12, 13, 14 },
11820 };
11821 
11822 /**
11823  * Discover the number of available flow priorities.
11824  *
11825  * @param dev
11826  *   Ethernet device.
11827  *
11828  * @return
11829  *   On success, number of available flow priorities.
11830  *   On failure, a negative errno-style code and rte_errno is set.
11831  */
11832 int
11833 mlx5_flow_discover_priorities(struct rte_eth_dev *dev)
11834 {
11835 	static const uint16_t vprio[] = {8, 16};
11836 	const struct mlx5_priv *priv = dev->data->dev_private;
11837 	const struct mlx5_flow_driver_ops *fops;
11838 	enum mlx5_flow_drv_type type;
11839 	int ret;
11840 
11841 	type = mlx5_flow_os_get_type();
11842 	if (type == MLX5_FLOW_TYPE_MAX) {
11843 		type = MLX5_FLOW_TYPE_VERBS;
11844 		if (priv->sh->cdev->config.devx && priv->sh->config.dv_flow_en)
11845 			type = MLX5_FLOW_TYPE_DV;
11846 	}
11847 	fops = flow_get_drv_ops(type);
11848 	if (fops->discover_priorities == NULL) {
11849 		DRV_LOG(ERR, "Priority discovery not supported");
11850 		rte_errno = ENOTSUP;
11851 		return -rte_errno;
11852 	}
11853 	ret = fops->discover_priorities(dev, vprio, RTE_DIM(vprio));
11854 	if (ret < 0)
11855 		return ret;
11856 	switch (ret) {
11857 	case 8:
11858 		ret = RTE_DIM(priority_map_3);
11859 		break;
11860 	case 16:
11861 		ret = RTE_DIM(priority_map_5);
11862 		break;
11863 	default:
11864 		rte_errno = ENOTSUP;
11865 		DRV_LOG(ERR,
11866 			"port %u maximum priority: %d expected 8/16",
11867 			dev->data->port_id, ret);
11868 		return -rte_errno;
11869 	}
11870 	DRV_LOG(INFO, "port %u supported flow priorities:"
11871 		" 0-%d for ingress or egress root table,"
11872 		" 0-%d for non-root table or transfer root table.",
11873 		dev->data->port_id, ret - 2,
11874 		MLX5_NON_ROOT_FLOW_MAX_PRIO - 1);
11875 	return ret;
11876 }
11877 
11878 /**
11879  * Adjust flow priority based on the highest layer and the request priority.
11880  *
11881  * @param[in] dev
11882  *   Pointer to the Ethernet device structure.
11883  * @param[in] priority
11884  *   The rule base priority.
11885  * @param[in] subpriority
11886  *   The priority based on the items.
11887  *
11888  * @return
11889  *   The new priority.
11890  */
11891 uint32_t
11892 mlx5_flow_adjust_priority(struct rte_eth_dev *dev, int32_t priority,
11893 			  uint32_t subpriority)
11894 {
11895 	uint32_t res = 0;
11896 	struct mlx5_priv *priv = dev->data->dev_private;
11897 
11898 	switch (priv->sh->flow_max_priority) {
11899 	case RTE_DIM(priority_map_3):
11900 		res = priority_map_3[priority][subpriority];
11901 		break;
11902 	case RTE_DIM(priority_map_5):
11903 		res = priority_map_5[priority][subpriority];
11904 		break;
11905 	}
11906 	return  res;
11907 }
11908 
11909 /**
11910  * Get the priority for sending traffic to kernel table.
11911  *
11912  * @param[in] dev
11913  *   Pointer to the Ethernet device structure.
11914  *
11915  * @return
11916  *   On success: the value of priority for sending traffic to kernel table
11917  *   On failure: -1
11918  */
11919 uint32_t
11920 mlx5_get_send_to_kernel_priority(struct rte_eth_dev *dev)
11921 {
11922 	struct mlx5_priv *priv = dev->data->dev_private;
11923 	uint32_t res;
11924 
11925 	switch (priv->sh->flow_max_priority) {
11926 	case RTE_DIM(priority_map_5):
11927 		res = 15;
11928 		break;
11929 	case RTE_DIM(priority_map_3):
11930 		res = 7;
11931 		break;
11932 	default:
11933 		DRV_LOG(ERR,
11934 			"port %u maximum priority: %d expected 8/16",
11935 			dev->data->port_id, priv->sh->flow_max_priority);
11936 		res = (uint32_t)-1;
11937 	}
11938 	return res;
11939 }
11940 
11941 /**
11942  * Get the E-Switch Manager vport id.
11943  *
11944  * @param[in] dev
11945  *   Pointer to the Ethernet device structure.
11946  *
11947  * @return
11948  *   The vport id.
11949  */
11950 int16_t mlx5_flow_get_esw_manager_vport_id(struct rte_eth_dev *dev)
11951 {
11952 	struct mlx5_priv *priv = dev->data->dev_private;
11953 	struct mlx5_common_device *cdev = priv->sh->cdev;
11954 
11955 	/* New FW exposes E-Switch Manager vport ID, can use it directly. */
11956 	if (cdev->config.hca_attr.esw_mgr_vport_id_valid)
11957 		return (int16_t)cdev->config.hca_attr.esw_mgr_vport_id;
11958 
11959 	if (priv->pci_dev == NULL)
11960 		return 0;
11961 	switch (priv->pci_dev->id.device_id) {
11962 	case PCI_DEVICE_ID_MELLANOX_BLUEFIELD:
11963 	case PCI_DEVICE_ID_MELLANOX_BLUEFIELD2:
11964 	case PCI_DEVICE_ID_MELLANOX_BLUEFIELD3:
11965 	/*
11966 	 * In old FW which doesn't expose the E-Switch Manager vport ID in the capability,
11967 	 * only the BF embedded CPUs control the E-Switch Manager port. Hence,
11968 	 * ECPF vport ID is selected and not the host port (0) in any BF case.
11969 	 */
11970 		return (int16_t)MLX5_ECPF_VPORT_ID;
11971 	default:
11972 		return MLX5_PF_VPORT_ID;
11973 	}
11974 }
11975 
11976 /**
11977  * Parse item to get the vport id.
11978  *
11979  * @param[in] dev
11980  *   Pointer to the Ethernet device structure.
11981  * @param[in] item
11982  *   The src port id match item.
11983  * @param[out] vport_id
11984  *   Pointer to put the vport id.
11985  * @param[out] all_ports
11986  *   Indicate if the item matches all ports.
11987  * @param[out] error
11988  *   Pointer to error structure.
11989  *
11990  * @return
11991  *   0 on success, a negative errno value otherwise and rte_errno is set.
11992  */
11993 int mlx5_flow_get_item_vport_id(struct rte_eth_dev *dev,
11994 				const struct rte_flow_item *item,
11995 				uint16_t *vport_id,
11996 				bool *all_ports,
11997 				struct rte_flow_error *error)
11998 {
11999 	struct mlx5_priv *port_priv;
12000 	const struct rte_flow_item_port_id *pid_v = NULL;
12001 	const struct rte_flow_item_ethdev *dev_v = NULL;
12002 	uint32_t esw_mgr_port;
12003 	uint32_t src_port;
12004 
12005 	if (all_ports)
12006 		*all_ports = false;
12007 	switch (item->type) {
12008 	case RTE_FLOW_ITEM_TYPE_PORT_ID:
12009 		pid_v = item->spec;
12010 		if (!pid_v)
12011 			return 0;
12012 		src_port = pid_v->id;
12013 		esw_mgr_port = MLX5_PORT_ESW_MGR;
12014 		break;
12015 	case RTE_FLOW_ITEM_TYPE_REPRESENTED_PORT:
12016 		dev_v = item->spec;
12017 		if (!dev_v) {
12018 			if (all_ports)
12019 				*all_ports = true;
12020 			return 0;
12021 		}
12022 		src_port = dev_v->port_id;
12023 		esw_mgr_port = MLX5_REPRESENTED_PORT_ESW_MGR;
12024 		break;
12025 	case RTE_FLOW_ITEM_TYPE_PORT_REPRESENTOR:
12026 		src_port = MLX5_REPRESENTED_PORT_ESW_MGR;
12027 		esw_mgr_port = MLX5_REPRESENTED_PORT_ESW_MGR;
12028 		break;
12029 	default:
12030 		return rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
12031 					  NULL, "Incorrect item type.");
12032 	}
12033 	if (src_port == esw_mgr_port) {
12034 		*vport_id = mlx5_flow_get_esw_manager_vport_id(dev);
12035 	} else {
12036 		port_priv = mlx5_port_to_eswitch_info(src_port, false);
12037 		if (!port_priv)
12038 			return rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
12039 						  NULL, "Failed to get port info.");
12040 		*vport_id = port_priv->representor_id;
12041 	}
12042 
12043 	return 0;
12044 }
12045 
12046 int
12047 mlx5_flow_pick_transfer_proxy(struct rte_eth_dev *dev,
12048 			      uint16_t *proxy_port_id,
12049 			      struct rte_flow_error *error)
12050 {
12051 	const struct mlx5_priv *priv = dev->data->dev_private;
12052 	uint16_t port_id;
12053 
12054 	if (!priv->sh->config.dv_esw_en)
12055 		return rte_flow_error_set(error, EINVAL,
12056 					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
12057 					  NULL,
12058 					  "unable to provide a proxy port"
12059 					  " without E-Switch configured");
12060 	if (!priv->master && !priv->representor)
12061 		return rte_flow_error_set(error, EINVAL,
12062 					  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
12063 					  NULL,
12064 					  "unable to provide a proxy port"
12065 					  " for port which is not a master"
12066 					  " or a representor port");
12067 	if (priv->master) {
12068 		*proxy_port_id = dev->data->port_id;
12069 		return 0;
12070 	}
12071 	MLX5_ETH_FOREACH_DEV(port_id, dev->device) {
12072 		const struct rte_eth_dev *port_dev = &rte_eth_devices[port_id];
12073 		const struct mlx5_priv *port_priv = port_dev->data->dev_private;
12074 
12075 		if (port_priv->master &&
12076 		    port_priv->domain_id == priv->domain_id) {
12077 			*proxy_port_id = port_id;
12078 			return 0;
12079 		}
12080 	}
12081 	return rte_flow_error_set(error, ENODEV,
12082 				  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
12083 				  NULL, "unable to find a proxy port");
12084 }
12085 
12086 /**
12087  * Discover IPv6 traffic class ID support in rdma-core and firmware.
12088  *
12089  * @param dev
12090  *   Ethernet device.
12091  *
12092  * @return
12093  *   0, rdma-core is good to work with firmware.
12094  *   -EOPNOTSUPP, rdma-core could not work with new IPv6 TC ID.
12095  */
12096 int
12097 mlx5_flow_discover_ipv6_tc_support(struct rte_eth_dev *dev)
12098 {
12099 	struct rte_flow_action_set_dscp set_dscp;
12100 	struct rte_flow_attr attr;
12101 	struct rte_flow_action actions[2];
12102 	struct rte_flow_item items[3];
12103 	struct rte_flow_error error;
12104 	uint32_t flow_idx;
12105 
12106 	memset(&attr, 0, sizeof(attr));
12107 	memset(actions, 0, sizeof(actions));
12108 	memset(items, 0, sizeof(items));
12109 	attr.group = 1;
12110 	attr.egress = 1;
12111 	items[0].type = RTE_FLOW_ITEM_TYPE_ETH;
12112 	items[1].type = RTE_FLOW_ITEM_TYPE_IPV6;
12113 	items[2].type = RTE_FLOW_ITEM_TYPE_END;
12114 	/* Random value */
12115 	set_dscp.dscp = 9;
12116 	actions[0].type = RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP;
12117 	actions[0].conf = &set_dscp;
12118 	actions[1].type = RTE_FLOW_ACTION_TYPE_END;
12119 
12120 	flow_idx = mlx5_flow_list_create(dev, MLX5_FLOW_TYPE_GEN, &attr, items,
12121 					 actions, true, &error);
12122 	if (!flow_idx)
12123 		return -EOPNOTSUPP;
12124 
12125 	mlx5_flow_list_destroy(dev, MLX5_FLOW_TYPE_GEN, flow_idx);
12126 	return 0;
12127 }
12128 
12129 void *
12130 rte_pmd_mlx5_create_geneve_tlv_parser(uint16_t port_id,
12131 				      const struct rte_pmd_mlx5_geneve_tlv tlv_list[],
12132 				      uint8_t nb_options)
12133 {
12134 #ifdef HAVE_MLX5_HWS_SUPPORT
12135 	return mlx5_geneve_tlv_parser_create(port_id, tlv_list, nb_options);
12136 #else
12137 	(void)port_id;
12138 	(void)tlv_list;
12139 	(void)nb_options;
12140 	DRV_LOG(ERR, "%s is not supported.", __func__);
12141 	rte_errno = ENOTSUP;
12142 	return NULL;
12143 #endif
12144 }
12145 
12146 int
12147 rte_pmd_mlx5_destroy_geneve_tlv_parser(void *handle)
12148 {
12149 #ifdef HAVE_MLX5_HWS_SUPPORT
12150 	return mlx5_geneve_tlv_parser_destroy(handle);
12151 #else
12152 	(void)handle;
12153 	DRV_LOG(ERR, "%s is not supported.", __func__);
12154 	rte_errno = ENOTSUP;
12155 	return -rte_errno;
12156 #endif
12157 }
12158