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