net/mlx5: fix tunnel offload private items location
[dpdk.git] / drivers / net / mlx5 / mlx5_flow_dv.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2018 Mellanox Technologies, Ltd
3  */
4
5 #include <sys/queue.h>
6 #include <stdalign.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <unistd.h>
10
11 #include <rte_common.h>
12 #include <rte_ether.h>
13 #include <ethdev_driver.h>
14 #include <rte_flow.h>
15 #include <rte_flow_driver.h>
16 #include <rte_malloc.h>
17 #include <rte_cycles.h>
18 #include <rte_ip.h>
19 #include <rte_gre.h>
20 #include <rte_vxlan.h>
21 #include <rte_gtp.h>
22 #include <rte_eal_paging.h>
23 #include <rte_mpls.h>
24 #include <rte_mtr.h>
25 #include <rte_mtr_driver.h>
26
27 #include <mlx5_glue.h>
28 #include <mlx5_devx_cmds.h>
29 #include <mlx5_prm.h>
30 #include <mlx5_malloc.h>
31
32 #include "mlx5_defs.h"
33 #include "mlx5.h"
34 #include "mlx5_common_os.h"
35 #include "mlx5_flow.h"
36 #include "mlx5_flow_os.h"
37 #include "mlx5_rx.h"
38 #include "mlx5_tx.h"
39 #include "rte_pmd_mlx5.h"
40
41 #if defined(HAVE_IBV_FLOW_DV_SUPPORT) || !defined(HAVE_INFINIBAND_VERBS_H)
42
43 #ifndef HAVE_IBV_FLOW_DEVX_COUNTERS
44 #define MLX5DV_FLOW_ACTION_COUNTERS_DEVX 0
45 #endif
46
47 #ifndef HAVE_MLX5DV_DR_ESWITCH
48 #ifndef MLX5DV_FLOW_TABLE_TYPE_FDB
49 #define MLX5DV_FLOW_TABLE_TYPE_FDB 0
50 #endif
51 #endif
52
53 #ifndef HAVE_MLX5DV_DR
54 #define MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL 1
55 #endif
56
57 /* VLAN header definitions */
58 #define MLX5DV_FLOW_VLAN_PCP_SHIFT 13
59 #define MLX5DV_FLOW_VLAN_PCP_MASK (0x7 << MLX5DV_FLOW_VLAN_PCP_SHIFT)
60 #define MLX5DV_FLOW_VLAN_VID_MASK 0x0fff
61 #define MLX5DV_FLOW_VLAN_PCP_MASK_BE RTE_BE16(MLX5DV_FLOW_VLAN_PCP_MASK)
62 #define MLX5DV_FLOW_VLAN_VID_MASK_BE RTE_BE16(MLX5DV_FLOW_VLAN_VID_MASK)
63
64 union flow_dv_attr {
65         struct {
66                 uint32_t valid:1;
67                 uint32_t ipv4:1;
68                 uint32_t ipv6:1;
69                 uint32_t tcp:1;
70                 uint32_t udp:1;
71                 uint32_t reserved:27;
72         };
73         uint32_t attr;
74 };
75
76 static int
77 flow_dv_tbl_resource_release(struct mlx5_dev_ctx_shared *sh,
78                              struct mlx5_flow_tbl_resource *tbl);
79
80 static int
81 flow_dv_encap_decap_resource_release(struct rte_eth_dev *dev,
82                                      uint32_t encap_decap_idx);
83
84 static int
85 flow_dv_port_id_action_resource_release(struct rte_eth_dev *dev,
86                                         uint32_t port_id);
87 static void
88 flow_dv_shared_rss_action_release(struct rte_eth_dev *dev, uint32_t srss);
89
90 static int
91 flow_dv_jump_tbl_resource_release(struct rte_eth_dev *dev,
92                                   uint32_t rix_jump);
93
94 /**
95  * Initialize flow attributes structure according to flow items' types.
96  *
97  * flow_dv_validate() avoids multiple L3/L4 layers cases other than tunnel
98  * mode. For tunnel mode, the items to be modified are the outermost ones.
99  *
100  * @param[in] item
101  *   Pointer to item specification.
102  * @param[out] attr
103  *   Pointer to flow attributes structure.
104  * @param[in] dev_flow
105  *   Pointer to the sub flow.
106  * @param[in] tunnel_decap
107  *   Whether action is after tunnel decapsulation.
108  */
109 static void
110 flow_dv_attr_init(const struct rte_flow_item *item, union flow_dv_attr *attr,
111                   struct mlx5_flow *dev_flow, bool tunnel_decap)
112 {
113         uint64_t layers = dev_flow->handle->layers;
114
115         /*
116          * If layers is already initialized, it means this dev_flow is the
117          * suffix flow, the layers flags is set by the prefix flow. Need to
118          * use the layer flags from prefix flow as the suffix flow may not
119          * have the user defined items as the flow is split.
120          */
121         if (layers) {
122                 if (layers & MLX5_FLOW_LAYER_OUTER_L3_IPV4)
123                         attr->ipv4 = 1;
124                 else if (layers & MLX5_FLOW_LAYER_OUTER_L3_IPV6)
125                         attr->ipv6 = 1;
126                 if (layers & MLX5_FLOW_LAYER_OUTER_L4_TCP)
127                         attr->tcp = 1;
128                 else if (layers & MLX5_FLOW_LAYER_OUTER_L4_UDP)
129                         attr->udp = 1;
130                 attr->valid = 1;
131                 return;
132         }
133         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
134                 uint8_t next_protocol = 0xff;
135                 switch (item->type) {
136                 case RTE_FLOW_ITEM_TYPE_GRE:
137                 case RTE_FLOW_ITEM_TYPE_NVGRE:
138                 case RTE_FLOW_ITEM_TYPE_VXLAN:
139                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
140                 case RTE_FLOW_ITEM_TYPE_GENEVE:
141                 case RTE_FLOW_ITEM_TYPE_MPLS:
142                         if (tunnel_decap)
143                                 attr->attr = 0;
144                         break;
145                 case RTE_FLOW_ITEM_TYPE_IPV4:
146                         if (!attr->ipv6)
147                                 attr->ipv4 = 1;
148                         if (item->mask != NULL &&
149                             ((const struct rte_flow_item_ipv4 *)
150                             item->mask)->hdr.next_proto_id)
151                                 next_protocol =
152                                     ((const struct rte_flow_item_ipv4 *)
153                                       (item->spec))->hdr.next_proto_id &
154                                     ((const struct rte_flow_item_ipv4 *)
155                                       (item->mask))->hdr.next_proto_id;
156                         if ((next_protocol == IPPROTO_IPIP ||
157                             next_protocol == IPPROTO_IPV6) && tunnel_decap)
158                                 attr->attr = 0;
159                         break;
160                 case RTE_FLOW_ITEM_TYPE_IPV6:
161                         if (!attr->ipv4)
162                                 attr->ipv6 = 1;
163                         if (item->mask != NULL &&
164                             ((const struct rte_flow_item_ipv6 *)
165                             item->mask)->hdr.proto)
166                                 next_protocol =
167                                     ((const struct rte_flow_item_ipv6 *)
168                                       (item->spec))->hdr.proto &
169                                     ((const struct rte_flow_item_ipv6 *)
170                                       (item->mask))->hdr.proto;
171                         if ((next_protocol == IPPROTO_IPIP ||
172                             next_protocol == IPPROTO_IPV6) && tunnel_decap)
173                                 attr->attr = 0;
174                         break;
175                 case RTE_FLOW_ITEM_TYPE_UDP:
176                         if (!attr->tcp)
177                                 attr->udp = 1;
178                         break;
179                 case RTE_FLOW_ITEM_TYPE_TCP:
180                         if (!attr->udp)
181                                 attr->tcp = 1;
182                         break;
183                 default:
184                         break;
185                 }
186         }
187         attr->valid = 1;
188 }
189
190 /**
191  * Convert rte_mtr_color to mlx5 color.
192  *
193  * @param[in] rcol
194  *   rte_mtr_color.
195  *
196  * @return
197  *   mlx5 color.
198  */
199 static int
200 rte_col_2_mlx5_col(enum rte_color rcol)
201 {
202         switch (rcol) {
203         case RTE_COLOR_GREEN:
204                 return MLX5_FLOW_COLOR_GREEN;
205         case RTE_COLOR_YELLOW:
206                 return MLX5_FLOW_COLOR_YELLOW;
207         case RTE_COLOR_RED:
208                 return MLX5_FLOW_COLOR_RED;
209         default:
210                 break;
211         }
212         return MLX5_FLOW_COLOR_UNDEFINED;
213 }
214
215 struct field_modify_info {
216         uint32_t size; /* Size of field in protocol header, in bytes. */
217         uint32_t offset; /* Offset of field in protocol header, in bytes. */
218         enum mlx5_modification_field id;
219 };
220
221 struct field_modify_info modify_eth[] = {
222         {4,  0, MLX5_MODI_OUT_DMAC_47_16},
223         {2,  4, MLX5_MODI_OUT_DMAC_15_0},
224         {4,  6, MLX5_MODI_OUT_SMAC_47_16},
225         {2, 10, MLX5_MODI_OUT_SMAC_15_0},
226         {0, 0, 0},
227 };
228
229 struct field_modify_info modify_vlan_out_first_vid[] = {
230         /* Size in bits !!! */
231         {12, 0, MLX5_MODI_OUT_FIRST_VID},
232         {0, 0, 0},
233 };
234
235 struct field_modify_info modify_ipv4[] = {
236         {1,  1, MLX5_MODI_OUT_IP_DSCP},
237         {1,  8, MLX5_MODI_OUT_IPV4_TTL},
238         {4, 12, MLX5_MODI_OUT_SIPV4},
239         {4, 16, MLX5_MODI_OUT_DIPV4},
240         {0, 0, 0},
241 };
242
243 struct field_modify_info modify_ipv6[] = {
244         {1,  0, MLX5_MODI_OUT_IP_DSCP},
245         {1,  7, MLX5_MODI_OUT_IPV6_HOPLIMIT},
246         {4,  8, MLX5_MODI_OUT_SIPV6_127_96},
247         {4, 12, MLX5_MODI_OUT_SIPV6_95_64},
248         {4, 16, MLX5_MODI_OUT_SIPV6_63_32},
249         {4, 20, MLX5_MODI_OUT_SIPV6_31_0},
250         {4, 24, MLX5_MODI_OUT_DIPV6_127_96},
251         {4, 28, MLX5_MODI_OUT_DIPV6_95_64},
252         {4, 32, MLX5_MODI_OUT_DIPV6_63_32},
253         {4, 36, MLX5_MODI_OUT_DIPV6_31_0},
254         {0, 0, 0},
255 };
256
257 struct field_modify_info modify_udp[] = {
258         {2, 0, MLX5_MODI_OUT_UDP_SPORT},
259         {2, 2, MLX5_MODI_OUT_UDP_DPORT},
260         {0, 0, 0},
261 };
262
263 struct field_modify_info modify_tcp[] = {
264         {2, 0, MLX5_MODI_OUT_TCP_SPORT},
265         {2, 2, MLX5_MODI_OUT_TCP_DPORT},
266         {4, 4, MLX5_MODI_OUT_TCP_SEQ_NUM},
267         {4, 8, MLX5_MODI_OUT_TCP_ACK_NUM},
268         {0, 0, 0},
269 };
270
271 static const struct rte_flow_item *
272 mlx5_flow_find_tunnel_item(const struct rte_flow_item *item)
273 {
274         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
275                 switch (item->type) {
276                 default:
277                         break;
278                 case RTE_FLOW_ITEM_TYPE_VXLAN:
279                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
280                 case RTE_FLOW_ITEM_TYPE_GRE:
281                 case RTE_FLOW_ITEM_TYPE_MPLS:
282                 case RTE_FLOW_ITEM_TYPE_NVGRE:
283                 case RTE_FLOW_ITEM_TYPE_GENEVE:
284                         return item;
285                 case RTE_FLOW_ITEM_TYPE_IPV4:
286                 case RTE_FLOW_ITEM_TYPE_IPV6:
287                         if (item[1].type == RTE_FLOW_ITEM_TYPE_IPV4 ||
288                             item[1].type == RTE_FLOW_ITEM_TYPE_IPV6)
289                                 return item;
290                         break;
291                 }
292         }
293         return NULL;
294 }
295
296 static void
297 mlx5_flow_tunnel_ip_check(const struct rte_flow_item *item __rte_unused,
298                           uint8_t next_protocol, uint64_t *item_flags,
299                           int *tunnel)
300 {
301         MLX5_ASSERT(item->type == RTE_FLOW_ITEM_TYPE_IPV4 ||
302                     item->type == RTE_FLOW_ITEM_TYPE_IPV6);
303         if (next_protocol == IPPROTO_IPIP) {
304                 *item_flags |= MLX5_FLOW_LAYER_IPIP;
305                 *tunnel = 1;
306         }
307         if (next_protocol == IPPROTO_IPV6) {
308                 *item_flags |= MLX5_FLOW_LAYER_IPV6_ENCAP;
309                 *tunnel = 1;
310         }
311 }
312
313 /* Update VLAN's VID/PCP based on input rte_flow_action.
314  *
315  * @param[in] action
316  *   Pointer to struct rte_flow_action.
317  * @param[out] vlan
318  *   Pointer to struct rte_vlan_hdr.
319  */
320 static void
321 mlx5_update_vlan_vid_pcp(const struct rte_flow_action *action,
322                          struct rte_vlan_hdr *vlan)
323 {
324         uint16_t vlan_tci;
325         if (action->type == RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP) {
326                 vlan_tci =
327                     ((const struct rte_flow_action_of_set_vlan_pcp *)
328                                                action->conf)->vlan_pcp;
329                 vlan_tci = vlan_tci << MLX5DV_FLOW_VLAN_PCP_SHIFT;
330                 vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_PCP_MASK;
331                 vlan->vlan_tci |= vlan_tci;
332         } else if (action->type == RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID) {
333                 vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_VID_MASK;
334                 vlan->vlan_tci |= rte_be_to_cpu_16
335                     (((const struct rte_flow_action_of_set_vlan_vid *)
336                                              action->conf)->vlan_vid);
337         }
338 }
339
340 /**
341  * Fetch 1, 2, 3 or 4 byte field from the byte array
342  * and return as unsigned integer in host-endian format.
343  *
344  * @param[in] data
345  *   Pointer to data array.
346  * @param[in] size
347  *   Size of field to extract.
348  *
349  * @return
350  *   converted field in host endian format.
351  */
352 static inline uint32_t
353 flow_dv_fetch_field(const uint8_t *data, uint32_t size)
354 {
355         uint32_t ret;
356
357         switch (size) {
358         case 1:
359                 ret = *data;
360                 break;
361         case 2:
362                 ret = rte_be_to_cpu_16(*(const unaligned_uint16_t *)data);
363                 break;
364         case 3:
365                 ret = rte_be_to_cpu_16(*(const unaligned_uint16_t *)data);
366                 ret = (ret << 8) | *(data + sizeof(uint16_t));
367                 break;
368         case 4:
369                 ret = rte_be_to_cpu_32(*(const unaligned_uint32_t *)data);
370                 break;
371         default:
372                 MLX5_ASSERT(false);
373                 ret = 0;
374                 break;
375         }
376         return ret;
377 }
378
379 /**
380  * Convert modify-header action to DV specification.
381  *
382  * Data length of each action is determined by provided field description
383  * and the item mask. Data bit offset and width of each action is determined
384  * by provided item mask.
385  *
386  * @param[in] item
387  *   Pointer to item specification.
388  * @param[in] field
389  *   Pointer to field modification information.
390  *     For MLX5_MODIFICATION_TYPE_SET specifies destination field.
391  *     For MLX5_MODIFICATION_TYPE_ADD specifies destination field.
392  *     For MLX5_MODIFICATION_TYPE_COPY specifies source field.
393  * @param[in] dcopy
394  *   Destination field info for MLX5_MODIFICATION_TYPE_COPY in @type.
395  *   Negative offset value sets the same offset as source offset.
396  *   size field is ignored, value is taken from source field.
397  * @param[in,out] resource
398  *   Pointer to the modify-header resource.
399  * @param[in] type
400  *   Type of modification.
401  * @param[out] error
402  *   Pointer to the error structure.
403  *
404  * @return
405  *   0 on success, a negative errno value otherwise and rte_errno is set.
406  */
407 static int
408 flow_dv_convert_modify_action(struct rte_flow_item *item,
409                               struct field_modify_info *field,
410                               struct field_modify_info *dcopy,
411                               struct mlx5_flow_dv_modify_hdr_resource *resource,
412                               uint32_t type, struct rte_flow_error *error)
413 {
414         uint32_t i = resource->actions_num;
415         struct mlx5_modification_cmd *actions = resource->actions;
416
417         /*
418          * The item and mask are provided in big-endian format.
419          * The fields should be presented as in big-endian format either.
420          * Mask must be always present, it defines the actual field width.
421          */
422         MLX5_ASSERT(item->mask);
423         MLX5_ASSERT(field->size);
424         do {
425                 unsigned int size_b;
426                 unsigned int off_b;
427                 uint32_t mask;
428                 uint32_t data;
429
430                 if (i >= MLX5_MAX_MODIFY_NUM)
431                         return rte_flow_error_set(error, EINVAL,
432                                  RTE_FLOW_ERROR_TYPE_ACTION, NULL,
433                                  "too many items to modify");
434                 /* Fetch variable byte size mask from the array. */
435                 mask = flow_dv_fetch_field((const uint8_t *)item->mask +
436                                            field->offset, field->size);
437                 if (!mask) {
438                         ++field;
439                         continue;
440                 }
441                 /* Deduce actual data width in bits from mask value. */
442                 off_b = rte_bsf32(mask);
443                 size_b = sizeof(uint32_t) * CHAR_BIT -
444                          off_b - __builtin_clz(mask);
445                 MLX5_ASSERT(size_b);
446                 size_b = size_b == sizeof(uint32_t) * CHAR_BIT ? 0 : size_b;
447                 actions[i] = (struct mlx5_modification_cmd) {
448                         .action_type = type,
449                         .field = field->id,
450                         .offset = off_b,
451                         .length = size_b,
452                 };
453                 /* Convert entire record to expected big-endian format. */
454                 actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
455                 if (type == MLX5_MODIFICATION_TYPE_COPY) {
456                         MLX5_ASSERT(dcopy);
457                         actions[i].dst_field = dcopy->id;
458                         actions[i].dst_offset =
459                                 (int)dcopy->offset < 0 ? off_b : dcopy->offset;
460                         /* Convert entire record to big-endian format. */
461                         actions[i].data1 = rte_cpu_to_be_32(actions[i].data1);
462                         ++dcopy;
463                 } else {
464                         MLX5_ASSERT(item->spec);
465                         data = flow_dv_fetch_field((const uint8_t *)item->spec +
466                                                    field->offset, field->size);
467                         /* Shift out the trailing masked bits from data. */
468                         data = (data & mask) >> off_b;
469                         actions[i].data1 = rte_cpu_to_be_32(data);
470                 }
471                 ++i;
472                 ++field;
473         } while (field->size);
474         if (resource->actions_num == i)
475                 return rte_flow_error_set(error, EINVAL,
476                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
477                                           "invalid modification flow item");
478         resource->actions_num = i;
479         return 0;
480 }
481
482 /**
483  * Convert modify-header set IPv4 address action to DV specification.
484  *
485  * @param[in,out] resource
486  *   Pointer to the modify-header resource.
487  * @param[in] action
488  *   Pointer to action specification.
489  * @param[out] error
490  *   Pointer to the error structure.
491  *
492  * @return
493  *   0 on success, a negative errno value otherwise and rte_errno is set.
494  */
495 static int
496 flow_dv_convert_action_modify_ipv4
497                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
498                          const struct rte_flow_action *action,
499                          struct rte_flow_error *error)
500 {
501         const struct rte_flow_action_set_ipv4 *conf =
502                 (const struct rte_flow_action_set_ipv4 *)(action->conf);
503         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV4 };
504         struct rte_flow_item_ipv4 ipv4;
505         struct rte_flow_item_ipv4 ipv4_mask;
506
507         memset(&ipv4, 0, sizeof(ipv4));
508         memset(&ipv4_mask, 0, sizeof(ipv4_mask));
509         if (action->type == RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC) {
510                 ipv4.hdr.src_addr = conf->ipv4_addr;
511                 ipv4_mask.hdr.src_addr = rte_flow_item_ipv4_mask.hdr.src_addr;
512         } else {
513                 ipv4.hdr.dst_addr = conf->ipv4_addr;
514                 ipv4_mask.hdr.dst_addr = rte_flow_item_ipv4_mask.hdr.dst_addr;
515         }
516         item.spec = &ipv4;
517         item.mask = &ipv4_mask;
518         return flow_dv_convert_modify_action(&item, modify_ipv4, NULL, resource,
519                                              MLX5_MODIFICATION_TYPE_SET, error);
520 }
521
522 /**
523  * Convert modify-header set IPv6 address action to DV specification.
524  *
525  * @param[in,out] resource
526  *   Pointer to the modify-header resource.
527  * @param[in] action
528  *   Pointer to action specification.
529  * @param[out] error
530  *   Pointer to the error structure.
531  *
532  * @return
533  *   0 on success, a negative errno value otherwise and rte_errno is set.
534  */
535 static int
536 flow_dv_convert_action_modify_ipv6
537                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
538                          const struct rte_flow_action *action,
539                          struct rte_flow_error *error)
540 {
541         const struct rte_flow_action_set_ipv6 *conf =
542                 (const struct rte_flow_action_set_ipv6 *)(action->conf);
543         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV6 };
544         struct rte_flow_item_ipv6 ipv6;
545         struct rte_flow_item_ipv6 ipv6_mask;
546
547         memset(&ipv6, 0, sizeof(ipv6));
548         memset(&ipv6_mask, 0, sizeof(ipv6_mask));
549         if (action->type == RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC) {
550                 memcpy(&ipv6.hdr.src_addr, &conf->ipv6_addr,
551                        sizeof(ipv6.hdr.src_addr));
552                 memcpy(&ipv6_mask.hdr.src_addr,
553                        &rte_flow_item_ipv6_mask.hdr.src_addr,
554                        sizeof(ipv6.hdr.src_addr));
555         } else {
556                 memcpy(&ipv6.hdr.dst_addr, &conf->ipv6_addr,
557                        sizeof(ipv6.hdr.dst_addr));
558                 memcpy(&ipv6_mask.hdr.dst_addr,
559                        &rte_flow_item_ipv6_mask.hdr.dst_addr,
560                        sizeof(ipv6.hdr.dst_addr));
561         }
562         item.spec = &ipv6;
563         item.mask = &ipv6_mask;
564         return flow_dv_convert_modify_action(&item, modify_ipv6, NULL, resource,
565                                              MLX5_MODIFICATION_TYPE_SET, error);
566 }
567
568 /**
569  * Convert modify-header set MAC address action to DV specification.
570  *
571  * @param[in,out] resource
572  *   Pointer to the modify-header resource.
573  * @param[in] action
574  *   Pointer to action specification.
575  * @param[out] error
576  *   Pointer to the error structure.
577  *
578  * @return
579  *   0 on success, a negative errno value otherwise and rte_errno is set.
580  */
581 static int
582 flow_dv_convert_action_modify_mac
583                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
584                          const struct rte_flow_action *action,
585                          struct rte_flow_error *error)
586 {
587         const struct rte_flow_action_set_mac *conf =
588                 (const struct rte_flow_action_set_mac *)(action->conf);
589         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_ETH };
590         struct rte_flow_item_eth eth;
591         struct rte_flow_item_eth eth_mask;
592
593         memset(&eth, 0, sizeof(eth));
594         memset(&eth_mask, 0, sizeof(eth_mask));
595         if (action->type == RTE_FLOW_ACTION_TYPE_SET_MAC_SRC) {
596                 memcpy(&eth.src.addr_bytes, &conf->mac_addr,
597                        sizeof(eth.src.addr_bytes));
598                 memcpy(&eth_mask.src.addr_bytes,
599                        &rte_flow_item_eth_mask.src.addr_bytes,
600                        sizeof(eth_mask.src.addr_bytes));
601         } else {
602                 memcpy(&eth.dst.addr_bytes, &conf->mac_addr,
603                        sizeof(eth.dst.addr_bytes));
604                 memcpy(&eth_mask.dst.addr_bytes,
605                        &rte_flow_item_eth_mask.dst.addr_bytes,
606                        sizeof(eth_mask.dst.addr_bytes));
607         }
608         item.spec = &eth;
609         item.mask = &eth_mask;
610         return flow_dv_convert_modify_action(&item, modify_eth, NULL, resource,
611                                              MLX5_MODIFICATION_TYPE_SET, error);
612 }
613
614 /**
615  * Convert modify-header set VLAN VID action to DV specification.
616  *
617  * @param[in,out] resource
618  *   Pointer to the modify-header resource.
619  * @param[in] action
620  *   Pointer to action specification.
621  * @param[out] error
622  *   Pointer to the error structure.
623  *
624  * @return
625  *   0 on success, a negative errno value otherwise and rte_errno is set.
626  */
627 static int
628 flow_dv_convert_action_modify_vlan_vid
629                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
630                          const struct rte_flow_action *action,
631                          struct rte_flow_error *error)
632 {
633         const struct rte_flow_action_of_set_vlan_vid *conf =
634                 (const struct rte_flow_action_of_set_vlan_vid *)(action->conf);
635         int i = resource->actions_num;
636         struct mlx5_modification_cmd *actions = resource->actions;
637         struct field_modify_info *field = modify_vlan_out_first_vid;
638
639         if (i >= MLX5_MAX_MODIFY_NUM)
640                 return rte_flow_error_set(error, EINVAL,
641                          RTE_FLOW_ERROR_TYPE_ACTION, NULL,
642                          "too many items to modify");
643         actions[i] = (struct mlx5_modification_cmd) {
644                 .action_type = MLX5_MODIFICATION_TYPE_SET,
645                 .field = field->id,
646                 .length = field->size,
647                 .offset = field->offset,
648         };
649         actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
650         actions[i].data1 = conf->vlan_vid;
651         actions[i].data1 = actions[i].data1 << 16;
652         resource->actions_num = ++i;
653         return 0;
654 }
655
656 /**
657  * Convert modify-header set TP action to DV specification.
658  *
659  * @param[in,out] resource
660  *   Pointer to the modify-header resource.
661  * @param[in] action
662  *   Pointer to action specification.
663  * @param[in] items
664  *   Pointer to rte_flow_item objects list.
665  * @param[in] attr
666  *   Pointer to flow attributes structure.
667  * @param[in] dev_flow
668  *   Pointer to the sub flow.
669  * @param[in] tunnel_decap
670  *   Whether action is after tunnel decapsulation.
671  * @param[out] error
672  *   Pointer to the error structure.
673  *
674  * @return
675  *   0 on success, a negative errno value otherwise and rte_errno is set.
676  */
677 static int
678 flow_dv_convert_action_modify_tp
679                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
680                          const struct rte_flow_action *action,
681                          const struct rte_flow_item *items,
682                          union flow_dv_attr *attr, struct mlx5_flow *dev_flow,
683                          bool tunnel_decap, struct rte_flow_error *error)
684 {
685         const struct rte_flow_action_set_tp *conf =
686                 (const struct rte_flow_action_set_tp *)(action->conf);
687         struct rte_flow_item item;
688         struct rte_flow_item_udp udp;
689         struct rte_flow_item_udp udp_mask;
690         struct rte_flow_item_tcp tcp;
691         struct rte_flow_item_tcp tcp_mask;
692         struct field_modify_info *field;
693
694         if (!attr->valid)
695                 flow_dv_attr_init(items, attr, dev_flow, tunnel_decap);
696         if (attr->udp) {
697                 memset(&udp, 0, sizeof(udp));
698                 memset(&udp_mask, 0, sizeof(udp_mask));
699                 if (action->type == RTE_FLOW_ACTION_TYPE_SET_TP_SRC) {
700                         udp.hdr.src_port = conf->port;
701                         udp_mask.hdr.src_port =
702                                         rte_flow_item_udp_mask.hdr.src_port;
703                 } else {
704                         udp.hdr.dst_port = conf->port;
705                         udp_mask.hdr.dst_port =
706                                         rte_flow_item_udp_mask.hdr.dst_port;
707                 }
708                 item.type = RTE_FLOW_ITEM_TYPE_UDP;
709                 item.spec = &udp;
710                 item.mask = &udp_mask;
711                 field = modify_udp;
712         } else {
713                 MLX5_ASSERT(attr->tcp);
714                 memset(&tcp, 0, sizeof(tcp));
715                 memset(&tcp_mask, 0, sizeof(tcp_mask));
716                 if (action->type == RTE_FLOW_ACTION_TYPE_SET_TP_SRC) {
717                         tcp.hdr.src_port = conf->port;
718                         tcp_mask.hdr.src_port =
719                                         rte_flow_item_tcp_mask.hdr.src_port;
720                 } else {
721                         tcp.hdr.dst_port = conf->port;
722                         tcp_mask.hdr.dst_port =
723                                         rte_flow_item_tcp_mask.hdr.dst_port;
724                 }
725                 item.type = RTE_FLOW_ITEM_TYPE_TCP;
726                 item.spec = &tcp;
727                 item.mask = &tcp_mask;
728                 field = modify_tcp;
729         }
730         return flow_dv_convert_modify_action(&item, field, NULL, resource,
731                                              MLX5_MODIFICATION_TYPE_SET, error);
732 }
733
734 /**
735  * Convert modify-header set TTL action to DV specification.
736  *
737  * @param[in,out] resource
738  *   Pointer to the modify-header resource.
739  * @param[in] action
740  *   Pointer to action specification.
741  * @param[in] items
742  *   Pointer to rte_flow_item objects list.
743  * @param[in] attr
744  *   Pointer to flow attributes structure.
745  * @param[in] dev_flow
746  *   Pointer to the sub flow.
747  * @param[in] tunnel_decap
748  *   Whether action is after tunnel decapsulation.
749  * @param[out] error
750  *   Pointer to the error structure.
751  *
752  * @return
753  *   0 on success, a negative errno value otherwise and rte_errno is set.
754  */
755 static int
756 flow_dv_convert_action_modify_ttl
757                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
758                          const struct rte_flow_action *action,
759                          const struct rte_flow_item *items,
760                          union flow_dv_attr *attr, struct mlx5_flow *dev_flow,
761                          bool tunnel_decap, struct rte_flow_error *error)
762 {
763         const struct rte_flow_action_set_ttl *conf =
764                 (const struct rte_flow_action_set_ttl *)(action->conf);
765         struct rte_flow_item item;
766         struct rte_flow_item_ipv4 ipv4;
767         struct rte_flow_item_ipv4 ipv4_mask;
768         struct rte_flow_item_ipv6 ipv6;
769         struct rte_flow_item_ipv6 ipv6_mask;
770         struct field_modify_info *field;
771
772         if (!attr->valid)
773                 flow_dv_attr_init(items, attr, dev_flow, tunnel_decap);
774         if (attr->ipv4) {
775                 memset(&ipv4, 0, sizeof(ipv4));
776                 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
777                 ipv4.hdr.time_to_live = conf->ttl_value;
778                 ipv4_mask.hdr.time_to_live = 0xFF;
779                 item.type = RTE_FLOW_ITEM_TYPE_IPV4;
780                 item.spec = &ipv4;
781                 item.mask = &ipv4_mask;
782                 field = modify_ipv4;
783         } else {
784                 MLX5_ASSERT(attr->ipv6);
785                 memset(&ipv6, 0, sizeof(ipv6));
786                 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
787                 ipv6.hdr.hop_limits = conf->ttl_value;
788                 ipv6_mask.hdr.hop_limits = 0xFF;
789                 item.type = RTE_FLOW_ITEM_TYPE_IPV6;
790                 item.spec = &ipv6;
791                 item.mask = &ipv6_mask;
792                 field = modify_ipv6;
793         }
794         return flow_dv_convert_modify_action(&item, field, NULL, resource,
795                                              MLX5_MODIFICATION_TYPE_SET, error);
796 }
797
798 /**
799  * Convert modify-header decrement TTL action to DV specification.
800  *
801  * @param[in,out] resource
802  *   Pointer to the modify-header resource.
803  * @param[in] action
804  *   Pointer to action specification.
805  * @param[in] items
806  *   Pointer to rte_flow_item objects list.
807  * @param[in] attr
808  *   Pointer to flow attributes structure.
809  * @param[in] dev_flow
810  *   Pointer to the sub flow.
811  * @param[in] tunnel_decap
812  *   Whether action is after tunnel decapsulation.
813  * @param[out] error
814  *   Pointer to the error structure.
815  *
816  * @return
817  *   0 on success, a negative errno value otherwise and rte_errno is set.
818  */
819 static int
820 flow_dv_convert_action_modify_dec_ttl
821                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
822                          const struct rte_flow_item *items,
823                          union flow_dv_attr *attr, struct mlx5_flow *dev_flow,
824                          bool tunnel_decap, struct rte_flow_error *error)
825 {
826         struct rte_flow_item item;
827         struct rte_flow_item_ipv4 ipv4;
828         struct rte_flow_item_ipv4 ipv4_mask;
829         struct rte_flow_item_ipv6 ipv6;
830         struct rte_flow_item_ipv6 ipv6_mask;
831         struct field_modify_info *field;
832
833         if (!attr->valid)
834                 flow_dv_attr_init(items, attr, dev_flow, tunnel_decap);
835         if (attr->ipv4) {
836                 memset(&ipv4, 0, sizeof(ipv4));
837                 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
838                 ipv4.hdr.time_to_live = 0xFF;
839                 ipv4_mask.hdr.time_to_live = 0xFF;
840                 item.type = RTE_FLOW_ITEM_TYPE_IPV4;
841                 item.spec = &ipv4;
842                 item.mask = &ipv4_mask;
843                 field = modify_ipv4;
844         } else {
845                 MLX5_ASSERT(attr->ipv6);
846                 memset(&ipv6, 0, sizeof(ipv6));
847                 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
848                 ipv6.hdr.hop_limits = 0xFF;
849                 ipv6_mask.hdr.hop_limits = 0xFF;
850                 item.type = RTE_FLOW_ITEM_TYPE_IPV6;
851                 item.spec = &ipv6;
852                 item.mask = &ipv6_mask;
853                 field = modify_ipv6;
854         }
855         return flow_dv_convert_modify_action(&item, field, NULL, resource,
856                                              MLX5_MODIFICATION_TYPE_ADD, error);
857 }
858
859 /**
860  * Convert modify-header increment/decrement TCP Sequence number
861  * to DV specification.
862  *
863  * @param[in,out] resource
864  *   Pointer to the modify-header resource.
865  * @param[in] action
866  *   Pointer to action specification.
867  * @param[out] error
868  *   Pointer to the error structure.
869  *
870  * @return
871  *   0 on success, a negative errno value otherwise and rte_errno is set.
872  */
873 static int
874 flow_dv_convert_action_modify_tcp_seq
875                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
876                          const struct rte_flow_action *action,
877                          struct rte_flow_error *error)
878 {
879         const rte_be32_t *conf = (const rte_be32_t *)(action->conf);
880         uint64_t value = rte_be_to_cpu_32(*conf);
881         struct rte_flow_item item;
882         struct rte_flow_item_tcp tcp;
883         struct rte_flow_item_tcp tcp_mask;
884
885         memset(&tcp, 0, sizeof(tcp));
886         memset(&tcp_mask, 0, sizeof(tcp_mask));
887         if (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ)
888                 /*
889                  * The HW has no decrement operation, only increment operation.
890                  * To simulate decrement X from Y using increment operation
891                  * we need to add UINT32_MAX X times to Y.
892                  * Each adding of UINT32_MAX decrements Y by 1.
893                  */
894                 value *= UINT32_MAX;
895         tcp.hdr.sent_seq = rte_cpu_to_be_32((uint32_t)value);
896         tcp_mask.hdr.sent_seq = RTE_BE32(UINT32_MAX);
897         item.type = RTE_FLOW_ITEM_TYPE_TCP;
898         item.spec = &tcp;
899         item.mask = &tcp_mask;
900         return flow_dv_convert_modify_action(&item, modify_tcp, NULL, resource,
901                                              MLX5_MODIFICATION_TYPE_ADD, error);
902 }
903
904 /**
905  * Convert modify-header increment/decrement TCP Acknowledgment number
906  * to DV specification.
907  *
908  * @param[in,out] resource
909  *   Pointer to the modify-header resource.
910  * @param[in] action
911  *   Pointer to action specification.
912  * @param[out] error
913  *   Pointer to the error structure.
914  *
915  * @return
916  *   0 on success, a negative errno value otherwise and rte_errno is set.
917  */
918 static int
919 flow_dv_convert_action_modify_tcp_ack
920                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
921                          const struct rte_flow_action *action,
922                          struct rte_flow_error *error)
923 {
924         const rte_be32_t *conf = (const rte_be32_t *)(action->conf);
925         uint64_t value = rte_be_to_cpu_32(*conf);
926         struct rte_flow_item item;
927         struct rte_flow_item_tcp tcp;
928         struct rte_flow_item_tcp tcp_mask;
929
930         memset(&tcp, 0, sizeof(tcp));
931         memset(&tcp_mask, 0, sizeof(tcp_mask));
932         if (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK)
933                 /*
934                  * The HW has no decrement operation, only increment operation.
935                  * To simulate decrement X from Y using increment operation
936                  * we need to add UINT32_MAX X times to Y.
937                  * Each adding of UINT32_MAX decrements Y by 1.
938                  */
939                 value *= UINT32_MAX;
940         tcp.hdr.recv_ack = rte_cpu_to_be_32((uint32_t)value);
941         tcp_mask.hdr.recv_ack = RTE_BE32(UINT32_MAX);
942         item.type = RTE_FLOW_ITEM_TYPE_TCP;
943         item.spec = &tcp;
944         item.mask = &tcp_mask;
945         return flow_dv_convert_modify_action(&item, modify_tcp, NULL, resource,
946                                              MLX5_MODIFICATION_TYPE_ADD, error);
947 }
948
949 static enum mlx5_modification_field reg_to_field[] = {
950         [REG_NON] = MLX5_MODI_OUT_NONE,
951         [REG_A] = MLX5_MODI_META_DATA_REG_A,
952         [REG_B] = MLX5_MODI_META_DATA_REG_B,
953         [REG_C_0] = MLX5_MODI_META_REG_C_0,
954         [REG_C_1] = MLX5_MODI_META_REG_C_1,
955         [REG_C_2] = MLX5_MODI_META_REG_C_2,
956         [REG_C_3] = MLX5_MODI_META_REG_C_3,
957         [REG_C_4] = MLX5_MODI_META_REG_C_4,
958         [REG_C_5] = MLX5_MODI_META_REG_C_5,
959         [REG_C_6] = MLX5_MODI_META_REG_C_6,
960         [REG_C_7] = MLX5_MODI_META_REG_C_7,
961 };
962
963 /**
964  * Convert register set to DV specification.
965  *
966  * @param[in,out] resource
967  *   Pointer to the modify-header resource.
968  * @param[in] action
969  *   Pointer to action specification.
970  * @param[out] error
971  *   Pointer to the error structure.
972  *
973  * @return
974  *   0 on success, a negative errno value otherwise and rte_errno is set.
975  */
976 static int
977 flow_dv_convert_action_set_reg
978                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
979                          const struct rte_flow_action *action,
980                          struct rte_flow_error *error)
981 {
982         const struct mlx5_rte_flow_action_set_tag *conf = action->conf;
983         struct mlx5_modification_cmd *actions = resource->actions;
984         uint32_t i = resource->actions_num;
985
986         if (i >= MLX5_MAX_MODIFY_NUM)
987                 return rte_flow_error_set(error, EINVAL,
988                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
989                                           "too many items to modify");
990         MLX5_ASSERT(conf->id != REG_NON);
991         MLX5_ASSERT(conf->id < (enum modify_reg)RTE_DIM(reg_to_field));
992         actions[i] = (struct mlx5_modification_cmd) {
993                 .action_type = MLX5_MODIFICATION_TYPE_SET,
994                 .field = reg_to_field[conf->id],
995                 .offset = conf->offset,
996                 .length = conf->length,
997         };
998         actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
999         actions[i].data1 = rte_cpu_to_be_32(conf->data);
1000         ++i;
1001         resource->actions_num = i;
1002         return 0;
1003 }
1004
1005 /**
1006  * Convert SET_TAG action to DV specification.
1007  *
1008  * @param[in] dev
1009  *   Pointer to the rte_eth_dev structure.
1010  * @param[in,out] resource
1011  *   Pointer to the modify-header resource.
1012  * @param[in] conf
1013  *   Pointer to action specification.
1014  * @param[out] error
1015  *   Pointer to the error structure.
1016  *
1017  * @return
1018  *   0 on success, a negative errno value otherwise and rte_errno is set.
1019  */
1020 static int
1021 flow_dv_convert_action_set_tag
1022                         (struct rte_eth_dev *dev,
1023                          struct mlx5_flow_dv_modify_hdr_resource *resource,
1024                          const struct rte_flow_action_set_tag *conf,
1025                          struct rte_flow_error *error)
1026 {
1027         rte_be32_t data = rte_cpu_to_be_32(conf->data);
1028         rte_be32_t mask = rte_cpu_to_be_32(conf->mask);
1029         struct rte_flow_item item = {
1030                 .spec = &data,
1031                 .mask = &mask,
1032         };
1033         struct field_modify_info reg_c_x[] = {
1034                 [1] = {0, 0, 0},
1035         };
1036         enum mlx5_modification_field reg_type;
1037         int ret;
1038
1039         ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, conf->index, error);
1040         if (ret < 0)
1041                 return ret;
1042         MLX5_ASSERT(ret != REG_NON);
1043         MLX5_ASSERT((unsigned int)ret < RTE_DIM(reg_to_field));
1044         reg_type = reg_to_field[ret];
1045         MLX5_ASSERT(reg_type > 0);
1046         reg_c_x[0] = (struct field_modify_info){4, 0, reg_type};
1047         return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
1048                                              MLX5_MODIFICATION_TYPE_SET, error);
1049 }
1050
1051 /**
1052  * Convert internal COPY_REG action to DV specification.
1053  *
1054  * @param[in] dev
1055  *   Pointer to the rte_eth_dev structure.
1056  * @param[in,out] res
1057  *   Pointer to the modify-header resource.
1058  * @param[in] action
1059  *   Pointer to action specification.
1060  * @param[out] error
1061  *   Pointer to the error structure.
1062  *
1063  * @return
1064  *   0 on success, a negative errno value otherwise and rte_errno is set.
1065  */
1066 static int
1067 flow_dv_convert_action_copy_mreg(struct rte_eth_dev *dev,
1068                                  struct mlx5_flow_dv_modify_hdr_resource *res,
1069                                  const struct rte_flow_action *action,
1070                                  struct rte_flow_error *error)
1071 {
1072         const struct mlx5_flow_action_copy_mreg *conf = action->conf;
1073         rte_be32_t mask = RTE_BE32(UINT32_MAX);
1074         struct rte_flow_item item = {
1075                 .spec = NULL,
1076                 .mask = &mask,
1077         };
1078         struct field_modify_info reg_src[] = {
1079                 {4, 0, reg_to_field[conf->src]},
1080                 {0, 0, 0},
1081         };
1082         struct field_modify_info reg_dst = {
1083                 .offset = 0,
1084                 .id = reg_to_field[conf->dst],
1085         };
1086         /* Adjust reg_c[0] usage according to reported mask. */
1087         if (conf->dst == REG_C_0 || conf->src == REG_C_0) {
1088                 struct mlx5_priv *priv = dev->data->dev_private;
1089                 uint32_t reg_c0 = priv->sh->dv_regc0_mask;
1090
1091                 MLX5_ASSERT(reg_c0);
1092                 MLX5_ASSERT(priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY);
1093                 if (conf->dst == REG_C_0) {
1094                         /* Copy to reg_c[0], within mask only. */
1095                         reg_dst.offset = rte_bsf32(reg_c0);
1096                         /*
1097                          * Mask is ignoring the enianness, because
1098                          * there is no conversion in datapath.
1099                          */
1100 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
1101                         /* Copy from destination lower bits to reg_c[0]. */
1102                         mask = reg_c0 >> reg_dst.offset;
1103 #else
1104                         /* Copy from destination upper bits to reg_c[0]. */
1105                         mask = reg_c0 << (sizeof(reg_c0) * CHAR_BIT -
1106                                           rte_fls_u32(reg_c0));
1107 #endif
1108                 } else {
1109                         mask = rte_cpu_to_be_32(reg_c0);
1110 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
1111                         /* Copy from reg_c[0] to destination lower bits. */
1112                         reg_dst.offset = 0;
1113 #else
1114                         /* Copy from reg_c[0] to destination upper bits. */
1115                         reg_dst.offset = sizeof(reg_c0) * CHAR_BIT -
1116                                          (rte_fls_u32(reg_c0) -
1117                                           rte_bsf32(reg_c0));
1118 #endif
1119                 }
1120         }
1121         return flow_dv_convert_modify_action(&item,
1122                                              reg_src, &reg_dst, res,
1123                                              MLX5_MODIFICATION_TYPE_COPY,
1124                                              error);
1125 }
1126
1127 /**
1128  * Convert MARK action to DV specification. This routine is used
1129  * in extensive metadata only and requires metadata register to be
1130  * handled. In legacy mode hardware tag resource is engaged.
1131  *
1132  * @param[in] dev
1133  *   Pointer to the rte_eth_dev structure.
1134  * @param[in] conf
1135  *   Pointer to MARK action specification.
1136  * @param[in,out] resource
1137  *   Pointer to the modify-header resource.
1138  * @param[out] error
1139  *   Pointer to the error structure.
1140  *
1141  * @return
1142  *   0 on success, a negative errno value otherwise and rte_errno is set.
1143  */
1144 static int
1145 flow_dv_convert_action_mark(struct rte_eth_dev *dev,
1146                             const struct rte_flow_action_mark *conf,
1147                             struct mlx5_flow_dv_modify_hdr_resource *resource,
1148                             struct rte_flow_error *error)
1149 {
1150         struct mlx5_priv *priv = dev->data->dev_private;
1151         rte_be32_t mask = rte_cpu_to_be_32(MLX5_FLOW_MARK_MASK &
1152                                            priv->sh->dv_mark_mask);
1153         rte_be32_t data = rte_cpu_to_be_32(conf->id) & mask;
1154         struct rte_flow_item item = {
1155                 .spec = &data,
1156                 .mask = &mask,
1157         };
1158         struct field_modify_info reg_c_x[] = {
1159                 [1] = {0, 0, 0},
1160         };
1161         int reg;
1162
1163         if (!mask)
1164                 return rte_flow_error_set(error, EINVAL,
1165                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1166                                           NULL, "zero mark action mask");
1167         reg = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
1168         if (reg < 0)
1169                 return reg;
1170         MLX5_ASSERT(reg > 0);
1171         if (reg == REG_C_0) {
1172                 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
1173                 uint32_t shl_c0 = rte_bsf32(msk_c0);
1174
1175                 data = rte_cpu_to_be_32(rte_cpu_to_be_32(data) << shl_c0);
1176                 mask = rte_cpu_to_be_32(mask) & msk_c0;
1177                 mask = rte_cpu_to_be_32(mask << shl_c0);
1178         }
1179         reg_c_x[0] = (struct field_modify_info){4, 0, reg_to_field[reg]};
1180         return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
1181                                              MLX5_MODIFICATION_TYPE_SET, error);
1182 }
1183
1184 /**
1185  * Get metadata register index for specified steering domain.
1186  *
1187  * @param[in] dev
1188  *   Pointer to the rte_eth_dev structure.
1189  * @param[in] attr
1190  *   Attributes of flow to determine steering domain.
1191  * @param[out] error
1192  *   Pointer to the error structure.
1193  *
1194  * @return
1195  *   positive index on success, a negative errno value otherwise
1196  *   and rte_errno is set.
1197  */
1198 static enum modify_reg
1199 flow_dv_get_metadata_reg(struct rte_eth_dev *dev,
1200                          const struct rte_flow_attr *attr,
1201                          struct rte_flow_error *error)
1202 {
1203         int reg =
1204                 mlx5_flow_get_reg_id(dev, attr->transfer ?
1205                                           MLX5_METADATA_FDB :
1206                                             attr->egress ?
1207                                             MLX5_METADATA_TX :
1208                                             MLX5_METADATA_RX, 0, error);
1209         if (reg < 0)
1210                 return rte_flow_error_set(error,
1211                                           ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
1212                                           NULL, "unavailable "
1213                                           "metadata register");
1214         return reg;
1215 }
1216
1217 /**
1218  * Convert SET_META action to DV specification.
1219  *
1220  * @param[in] dev
1221  *   Pointer to the rte_eth_dev structure.
1222  * @param[in,out] resource
1223  *   Pointer to the modify-header resource.
1224  * @param[in] attr
1225  *   Attributes of flow that includes this item.
1226  * @param[in] conf
1227  *   Pointer to action specification.
1228  * @param[out] error
1229  *   Pointer to the error structure.
1230  *
1231  * @return
1232  *   0 on success, a negative errno value otherwise and rte_errno is set.
1233  */
1234 static int
1235 flow_dv_convert_action_set_meta
1236                         (struct rte_eth_dev *dev,
1237                          struct mlx5_flow_dv_modify_hdr_resource *resource,
1238                          const struct rte_flow_attr *attr,
1239                          const struct rte_flow_action_set_meta *conf,
1240                          struct rte_flow_error *error)
1241 {
1242         uint32_t data = conf->data;
1243         uint32_t mask = conf->mask;
1244         struct rte_flow_item item = {
1245                 .spec = &data,
1246                 .mask = &mask,
1247         };
1248         struct field_modify_info reg_c_x[] = {
1249                 [1] = {0, 0, 0},
1250         };
1251         int reg = flow_dv_get_metadata_reg(dev, attr, error);
1252
1253         if (reg < 0)
1254                 return reg;
1255         MLX5_ASSERT(reg != REG_NON);
1256         /*
1257          * In datapath code there is no endianness
1258          * coversions for perfromance reasons, all
1259          * pattern conversions are done in rte_flow.
1260          */
1261         if (reg == REG_C_0) {
1262                 struct mlx5_priv *priv = dev->data->dev_private;
1263                 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
1264                 uint32_t shl_c0;
1265
1266                 MLX5_ASSERT(msk_c0);
1267 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
1268                 shl_c0 = rte_bsf32(msk_c0);
1269 #else
1270                 shl_c0 = sizeof(msk_c0) * CHAR_BIT - rte_fls_u32(msk_c0);
1271 #endif
1272                 mask <<= shl_c0;
1273                 data <<= shl_c0;
1274                 MLX5_ASSERT(!(~msk_c0 & rte_cpu_to_be_32(mask)));
1275         }
1276         reg_c_x[0] = (struct field_modify_info){4, 0, reg_to_field[reg]};
1277         /* The routine expects parameters in memory as big-endian ones. */
1278         return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
1279                                              MLX5_MODIFICATION_TYPE_SET, error);
1280 }
1281
1282 /**
1283  * Convert modify-header set IPv4 DSCP action to DV specification.
1284  *
1285  * @param[in,out] resource
1286  *   Pointer to the modify-header resource.
1287  * @param[in] action
1288  *   Pointer to action specification.
1289  * @param[out] error
1290  *   Pointer to the error structure.
1291  *
1292  * @return
1293  *   0 on success, a negative errno value otherwise and rte_errno is set.
1294  */
1295 static int
1296 flow_dv_convert_action_modify_ipv4_dscp
1297                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
1298                          const struct rte_flow_action *action,
1299                          struct rte_flow_error *error)
1300 {
1301         const struct rte_flow_action_set_dscp *conf =
1302                 (const struct rte_flow_action_set_dscp *)(action->conf);
1303         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV4 };
1304         struct rte_flow_item_ipv4 ipv4;
1305         struct rte_flow_item_ipv4 ipv4_mask;
1306
1307         memset(&ipv4, 0, sizeof(ipv4));
1308         memset(&ipv4_mask, 0, sizeof(ipv4_mask));
1309         ipv4.hdr.type_of_service = conf->dscp;
1310         ipv4_mask.hdr.type_of_service = RTE_IPV4_HDR_DSCP_MASK >> 2;
1311         item.spec = &ipv4;
1312         item.mask = &ipv4_mask;
1313         return flow_dv_convert_modify_action(&item, modify_ipv4, NULL, resource,
1314                                              MLX5_MODIFICATION_TYPE_SET, error);
1315 }
1316
1317 /**
1318  * Convert modify-header set IPv6 DSCP action to DV specification.
1319  *
1320  * @param[in,out] resource
1321  *   Pointer to the modify-header resource.
1322  * @param[in] action
1323  *   Pointer to action specification.
1324  * @param[out] error
1325  *   Pointer to the error structure.
1326  *
1327  * @return
1328  *   0 on success, a negative errno value otherwise and rte_errno is set.
1329  */
1330 static int
1331 flow_dv_convert_action_modify_ipv6_dscp
1332                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
1333                          const struct rte_flow_action *action,
1334                          struct rte_flow_error *error)
1335 {
1336         const struct rte_flow_action_set_dscp *conf =
1337                 (const struct rte_flow_action_set_dscp *)(action->conf);
1338         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV6 };
1339         struct rte_flow_item_ipv6 ipv6;
1340         struct rte_flow_item_ipv6 ipv6_mask;
1341
1342         memset(&ipv6, 0, sizeof(ipv6));
1343         memset(&ipv6_mask, 0, sizeof(ipv6_mask));
1344         /*
1345          * Even though the DSCP bits offset of IPv6 is not byte aligned,
1346          * rdma-core only accept the DSCP bits byte aligned start from
1347          * bit 0 to 5 as to be compatible with IPv4. No need to shift the
1348          * bits in IPv6 case as rdma-core requires byte aligned value.
1349          */
1350         ipv6.hdr.vtc_flow = conf->dscp;
1351         ipv6_mask.hdr.vtc_flow = RTE_IPV6_HDR_DSCP_MASK >> 22;
1352         item.spec = &ipv6;
1353         item.mask = &ipv6_mask;
1354         return flow_dv_convert_modify_action(&item, modify_ipv6, NULL, resource,
1355                                              MLX5_MODIFICATION_TYPE_SET, error);
1356 }
1357
1358 static int
1359 mlx5_flow_item_field_width(enum rte_flow_field_id field)
1360 {
1361         switch (field) {
1362         case RTE_FLOW_FIELD_START:
1363                 return 32;
1364         case RTE_FLOW_FIELD_MAC_DST:
1365         case RTE_FLOW_FIELD_MAC_SRC:
1366                 return 48;
1367         case RTE_FLOW_FIELD_VLAN_TYPE:
1368                 return 16;
1369         case RTE_FLOW_FIELD_VLAN_ID:
1370                 return 12;
1371         case RTE_FLOW_FIELD_MAC_TYPE:
1372                 return 16;
1373         case RTE_FLOW_FIELD_IPV4_DSCP:
1374                 return 6;
1375         case RTE_FLOW_FIELD_IPV4_TTL:
1376                 return 8;
1377         case RTE_FLOW_FIELD_IPV4_SRC:
1378         case RTE_FLOW_FIELD_IPV4_DST:
1379                 return 32;
1380         case RTE_FLOW_FIELD_IPV6_DSCP:
1381                 return 6;
1382         case RTE_FLOW_FIELD_IPV6_HOPLIMIT:
1383                 return 8;
1384         case RTE_FLOW_FIELD_IPV6_SRC:
1385         case RTE_FLOW_FIELD_IPV6_DST:
1386                 return 128;
1387         case RTE_FLOW_FIELD_TCP_PORT_SRC:
1388         case RTE_FLOW_FIELD_TCP_PORT_DST:
1389                 return 16;
1390         case RTE_FLOW_FIELD_TCP_SEQ_NUM:
1391         case RTE_FLOW_FIELD_TCP_ACK_NUM:
1392                 return 32;
1393         case RTE_FLOW_FIELD_TCP_FLAGS:
1394                 return 9;
1395         case RTE_FLOW_FIELD_UDP_PORT_SRC:
1396         case RTE_FLOW_FIELD_UDP_PORT_DST:
1397                 return 16;
1398         case RTE_FLOW_FIELD_VXLAN_VNI:
1399         case RTE_FLOW_FIELD_GENEVE_VNI:
1400                 return 24;
1401         case RTE_FLOW_FIELD_GTP_TEID:
1402         case RTE_FLOW_FIELD_TAG:
1403                 return 32;
1404         case RTE_FLOW_FIELD_MARK:
1405                 return 24;
1406         case RTE_FLOW_FIELD_META:
1407                 return 32;
1408         case RTE_FLOW_FIELD_POINTER:
1409         case RTE_FLOW_FIELD_VALUE:
1410                 return 64;
1411         default:
1412                 MLX5_ASSERT(false);
1413         }
1414         return 0;
1415 }
1416
1417 static void
1418 mlx5_flow_field_id_to_modify_info
1419                 (const struct rte_flow_action_modify_data *data,
1420                  struct field_modify_info *info,
1421                  uint32_t *mask, uint32_t *value,
1422                  uint32_t width, uint32_t dst_width,
1423                  struct rte_eth_dev *dev,
1424                  const struct rte_flow_attr *attr,
1425                  struct rte_flow_error *error)
1426 {
1427         uint32_t idx = 0;
1428         uint64_t val = 0;
1429         switch (data->field) {
1430         case RTE_FLOW_FIELD_START:
1431                 /* not supported yet */
1432                 MLX5_ASSERT(false);
1433                 break;
1434         case RTE_FLOW_FIELD_MAC_DST:
1435                 if (mask) {
1436                         if (data->offset < 32) {
1437                                 info[idx] = (struct field_modify_info){4, 0,
1438                                                 MLX5_MODI_OUT_DMAC_47_16};
1439                                 if (width < 32) {
1440                                         mask[idx] =
1441                                                 rte_cpu_to_be_32(0xffffffff >>
1442                                                                  (32 - width));
1443                                         width = 0;
1444                                 } else {
1445                                         mask[idx] = RTE_BE32(0xffffffff);
1446                                         width -= 32;
1447                                 }
1448                                 if (!width)
1449                                         break;
1450                                 ++idx;
1451                         }
1452                         info[idx] = (struct field_modify_info){2, 4 * idx,
1453                                                 MLX5_MODI_OUT_DMAC_15_0};
1454                         mask[idx] = rte_cpu_to_be_16(0xffff >> (16 - width));
1455                 } else {
1456                         if (data->offset < 32)
1457                                 info[idx++] = (struct field_modify_info){4, 0,
1458                                                 MLX5_MODI_OUT_DMAC_47_16};
1459                         info[idx] = (struct field_modify_info){2, 0,
1460                                                 MLX5_MODI_OUT_DMAC_15_0};
1461                 }
1462                 break;
1463         case RTE_FLOW_FIELD_MAC_SRC:
1464                 if (mask) {
1465                         if (data->offset < 32) {
1466                                 info[idx] = (struct field_modify_info){4, 0,
1467                                                 MLX5_MODI_OUT_SMAC_47_16};
1468                                 if (width < 32) {
1469                                         mask[idx] =
1470                                                 rte_cpu_to_be_32(0xffffffff >>
1471                                                                 (32 - width));
1472                                         width = 0;
1473                                 } else {
1474                                         mask[idx] = RTE_BE32(0xffffffff);
1475                                         width -= 32;
1476                                 }
1477                                 if (!width)
1478                                         break;
1479                                 ++idx;
1480                         }
1481                         info[idx] = (struct field_modify_info){2, 4 * idx,
1482                                                 MLX5_MODI_OUT_SMAC_15_0};
1483                         mask[idx] = rte_cpu_to_be_16(0xffff >> (16 - width));
1484                 } else {
1485                         if (data->offset < 32)
1486                                 info[idx++] = (struct field_modify_info){4, 0,
1487                                                 MLX5_MODI_OUT_SMAC_47_16};
1488                         info[idx] = (struct field_modify_info){2, 0,
1489                                                 MLX5_MODI_OUT_SMAC_15_0};
1490                 }
1491                 break;
1492         case RTE_FLOW_FIELD_VLAN_TYPE:
1493                 /* not supported yet */
1494                 break;
1495         case RTE_FLOW_FIELD_VLAN_ID:
1496                 info[idx] = (struct field_modify_info){2, 0,
1497                                         MLX5_MODI_OUT_FIRST_VID};
1498                 if (mask)
1499                         mask[idx] = rte_cpu_to_be_16(0x0fff >> (12 - width));
1500                 break;
1501         case RTE_FLOW_FIELD_MAC_TYPE:
1502                 info[idx] = (struct field_modify_info){2, 0,
1503                                         MLX5_MODI_OUT_ETHERTYPE};
1504                 if (mask)
1505                         mask[idx] = rte_cpu_to_be_16(0xffff >> (16 - width));
1506                 break;
1507         case RTE_FLOW_FIELD_IPV4_DSCP:
1508                 info[idx] = (struct field_modify_info){1, 0,
1509                                         MLX5_MODI_OUT_IP_DSCP};
1510                 if (mask)
1511                         mask[idx] = 0x3f >> (6 - width);
1512                 break;
1513         case RTE_FLOW_FIELD_IPV4_TTL:
1514                 info[idx] = (struct field_modify_info){1, 0,
1515                                         MLX5_MODI_OUT_IPV4_TTL};
1516                 if (mask)
1517                         mask[idx] = 0xff >> (8 - width);
1518                 break;
1519         case RTE_FLOW_FIELD_IPV4_SRC:
1520                 info[idx] = (struct field_modify_info){4, 0,
1521                                         MLX5_MODI_OUT_SIPV4};
1522                 if (mask)
1523                         mask[idx] = rte_cpu_to_be_32(0xffffffff >>
1524                                                      (32 - width));
1525                 break;
1526         case RTE_FLOW_FIELD_IPV4_DST:
1527                 info[idx] = (struct field_modify_info){4, 0,
1528                                         MLX5_MODI_OUT_DIPV4};
1529                 if (mask)
1530                         mask[idx] = rte_cpu_to_be_32(0xffffffff >>
1531                                                      (32 - width));
1532                 break;
1533         case RTE_FLOW_FIELD_IPV6_DSCP:
1534                 info[idx] = (struct field_modify_info){1, 0,
1535                                         MLX5_MODI_OUT_IP_DSCP};
1536                 if (mask)
1537                         mask[idx] = 0x3f >> (6 - width);
1538                 break;
1539         case RTE_FLOW_FIELD_IPV6_HOPLIMIT:
1540                 info[idx] = (struct field_modify_info){1, 0,
1541                                         MLX5_MODI_OUT_IPV6_HOPLIMIT};
1542                 if (mask)
1543                         mask[idx] = 0xff >> (8 - width);
1544                 break;
1545         case RTE_FLOW_FIELD_IPV6_SRC:
1546                 if (mask) {
1547                         if (data->offset < 32) {
1548                                 info[idx] = (struct field_modify_info){4,
1549                                                 4 * idx,
1550                                                 MLX5_MODI_OUT_SIPV6_31_0};
1551                                 if (width < 32) {
1552                                         mask[idx] =
1553                                                 rte_cpu_to_be_32(0xffffffff >>
1554                                                                  (32 - width));
1555                                         width = 0;
1556                                 } else {
1557                                         mask[idx] = RTE_BE32(0xffffffff);
1558                                         width -= 32;
1559                                 }
1560                                 if (!width)
1561                                         break;
1562                                 ++idx;
1563                         }
1564                         if (data->offset < 64) {
1565                                 info[idx] = (struct field_modify_info){4,
1566                                                 4 * idx,
1567                                                 MLX5_MODI_OUT_SIPV6_63_32};
1568                                 if (width < 32) {
1569                                         mask[idx] =
1570                                                 rte_cpu_to_be_32(0xffffffff >>
1571                                                                  (32 - width));
1572                                         width = 0;
1573                                 } else {
1574                                         mask[idx] = RTE_BE32(0xffffffff);
1575                                         width -= 32;
1576                                 }
1577                                 if (!width)
1578                                         break;
1579                                 ++idx;
1580                         }
1581                         if (data->offset < 96) {
1582                                 info[idx] = (struct field_modify_info){4,
1583                                                 4 * idx,
1584                                                 MLX5_MODI_OUT_SIPV6_95_64};
1585                                 if (width < 32) {
1586                                         mask[idx] =
1587                                                 rte_cpu_to_be_32(0xffffffff >>
1588                                                                  (32 - width));
1589                                         width = 0;
1590                                 } else {
1591                                         mask[idx] = RTE_BE32(0xffffffff);
1592                                         width -= 32;
1593                                 }
1594                                 if (!width)
1595                                         break;
1596                                 ++idx;
1597                         }
1598                         info[idx] = (struct field_modify_info){4, 4 * idx,
1599                                                 MLX5_MODI_OUT_SIPV6_127_96};
1600                         mask[idx] = rte_cpu_to_be_32(0xffffffff >>
1601                                                      (32 - width));
1602                 } else {
1603                         if (data->offset < 32)
1604                                 info[idx++] = (struct field_modify_info){4, 0,
1605                                                 MLX5_MODI_OUT_SIPV6_31_0};
1606                         if (data->offset < 64)
1607                                 info[idx++] = (struct field_modify_info){4, 0,
1608                                                 MLX5_MODI_OUT_SIPV6_63_32};
1609                         if (data->offset < 96)
1610                                 info[idx++] = (struct field_modify_info){4, 0,
1611                                                 MLX5_MODI_OUT_SIPV6_95_64};
1612                         if (data->offset < 128)
1613                                 info[idx++] = (struct field_modify_info){4, 0,
1614                                                 MLX5_MODI_OUT_SIPV6_127_96};
1615                 }
1616                 break;
1617         case RTE_FLOW_FIELD_IPV6_DST:
1618                 if (mask) {
1619                         if (data->offset < 32) {
1620                                 info[idx] = (struct field_modify_info){4,
1621                                                 4 * idx,
1622                                                 MLX5_MODI_OUT_DIPV6_31_0};
1623                                 if (width < 32) {
1624                                         mask[idx] =
1625                                                 rte_cpu_to_be_32(0xffffffff >>
1626                                                                  (32 - width));
1627                                         width = 0;
1628                                 } else {
1629                                         mask[idx] = RTE_BE32(0xffffffff);
1630                                         width -= 32;
1631                                 }
1632                                 if (!width)
1633                                         break;
1634                                 ++idx;
1635                         }
1636                         if (data->offset < 64) {
1637                                 info[idx] = (struct field_modify_info){4,
1638                                                 4 * idx,
1639                                                 MLX5_MODI_OUT_DIPV6_63_32};
1640                                 if (width < 32) {
1641                                         mask[idx] =
1642                                                 rte_cpu_to_be_32(0xffffffff >>
1643                                                                  (32 - width));
1644                                         width = 0;
1645                                 } else {
1646                                         mask[idx] = RTE_BE32(0xffffffff);
1647                                         width -= 32;
1648                                 }
1649                                 if (!width)
1650                                         break;
1651                                 ++idx;
1652                         }
1653                         if (data->offset < 96) {
1654                                 info[idx] = (struct field_modify_info){4,
1655                                                 4 * idx,
1656                                                 MLX5_MODI_OUT_DIPV6_95_64};
1657                                 if (width < 32) {
1658                                         mask[idx] =
1659                                                 rte_cpu_to_be_32(0xffffffff >>
1660                                                                  (32 - width));
1661                                         width = 0;
1662                                 } else {
1663                                         mask[idx] = RTE_BE32(0xffffffff);
1664                                         width -= 32;
1665                                 }
1666                                 if (!width)
1667                                         break;
1668                                 ++idx;
1669                         }
1670                         info[idx] = (struct field_modify_info){4, 4 * idx,
1671                                                 MLX5_MODI_OUT_DIPV6_127_96};
1672                         mask[idx] = rte_cpu_to_be_32(0xffffffff >>
1673                                                      (32 - width));
1674                 } else {
1675                         if (data->offset < 32)
1676                                 info[idx++] = (struct field_modify_info){4, 0,
1677                                                 MLX5_MODI_OUT_DIPV6_31_0};
1678                         if (data->offset < 64)
1679                                 info[idx++] = (struct field_modify_info){4, 0,
1680                                                 MLX5_MODI_OUT_DIPV6_63_32};
1681                         if (data->offset < 96)
1682                                 info[idx++] = (struct field_modify_info){4, 0,
1683                                                 MLX5_MODI_OUT_DIPV6_95_64};
1684                         if (data->offset < 128)
1685                                 info[idx++] = (struct field_modify_info){4, 0,
1686                                                 MLX5_MODI_OUT_DIPV6_127_96};
1687                 }
1688                 break;
1689         case RTE_FLOW_FIELD_TCP_PORT_SRC:
1690                 info[idx] = (struct field_modify_info){2, 0,
1691                                         MLX5_MODI_OUT_TCP_SPORT};
1692                 if (mask)
1693                         mask[idx] = rte_cpu_to_be_16(0xffff >> (16 - width));
1694                 break;
1695         case RTE_FLOW_FIELD_TCP_PORT_DST:
1696                 info[idx] = (struct field_modify_info){2, 0,
1697                                         MLX5_MODI_OUT_TCP_DPORT};
1698                 if (mask)
1699                         mask[idx] = rte_cpu_to_be_16(0xffff >> (16 - width));
1700                 break;
1701         case RTE_FLOW_FIELD_TCP_SEQ_NUM:
1702                 info[idx] = (struct field_modify_info){4, 0,
1703                                         MLX5_MODI_OUT_TCP_SEQ_NUM};
1704                 if (mask)
1705                         mask[idx] = rte_cpu_to_be_32(0xffffffff >>
1706                                                      (32 - width));
1707                 break;
1708         case RTE_FLOW_FIELD_TCP_ACK_NUM:
1709                 info[idx] = (struct field_modify_info){4, 0,
1710                                         MLX5_MODI_OUT_TCP_ACK_NUM};
1711                 if (mask)
1712                         mask[idx] = rte_cpu_to_be_32(0xffffffff >>
1713                                                      (32 - width));
1714                 break;
1715         case RTE_FLOW_FIELD_TCP_FLAGS:
1716                 info[idx] = (struct field_modify_info){2, 0,
1717                                         MLX5_MODI_OUT_TCP_FLAGS};
1718                 if (mask)
1719                         mask[idx] = rte_cpu_to_be_16(0x1ff >> (9 - width));
1720                 break;
1721         case RTE_FLOW_FIELD_UDP_PORT_SRC:
1722                 info[idx] = (struct field_modify_info){2, 0,
1723                                         MLX5_MODI_OUT_UDP_SPORT};
1724                 if (mask)
1725                         mask[idx] = rte_cpu_to_be_16(0xffff >> (16 - width));
1726                 break;
1727         case RTE_FLOW_FIELD_UDP_PORT_DST:
1728                 info[idx] = (struct field_modify_info){2, 0,
1729                                         MLX5_MODI_OUT_UDP_DPORT};
1730                 if (mask)
1731                         mask[idx] = rte_cpu_to_be_16(0xffff >> (16 - width));
1732                 break;
1733         case RTE_FLOW_FIELD_VXLAN_VNI:
1734                 /* not supported yet */
1735                 break;
1736         case RTE_FLOW_FIELD_GENEVE_VNI:
1737                 /* not supported yet*/
1738                 break;
1739         case RTE_FLOW_FIELD_GTP_TEID:
1740                 info[idx] = (struct field_modify_info){4, 0,
1741                                         MLX5_MODI_GTP_TEID};
1742                 if (mask)
1743                         mask[idx] = rte_cpu_to_be_32(0xffffffff >>
1744                                                      (32 - width));
1745                 break;
1746         case RTE_FLOW_FIELD_TAG:
1747                 {
1748                         int reg = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG,
1749                                                    data->level, error);
1750                         if (reg < 0)
1751                                 return;
1752                         MLX5_ASSERT(reg != REG_NON);
1753                         MLX5_ASSERT((unsigned int)reg < RTE_DIM(reg_to_field));
1754                         info[idx] = (struct field_modify_info){4, 0,
1755                                                 reg_to_field[reg]};
1756                         if (mask)
1757                                 mask[idx] =
1758                                         rte_cpu_to_be_32(0xffffffff >>
1759                                                          (32 - width));
1760                 }
1761                 break;
1762         case RTE_FLOW_FIELD_MARK:
1763                 {
1764                         int reg = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK,
1765                                                        0, error);
1766                         if (reg < 0)
1767                                 return;
1768                         MLX5_ASSERT(reg != REG_NON);
1769                         MLX5_ASSERT((unsigned int)reg < RTE_DIM(reg_to_field));
1770                         info[idx] = (struct field_modify_info){4, 0,
1771                                                 reg_to_field[reg]};
1772                         if (mask)
1773                                 mask[idx] =
1774                                         rte_cpu_to_be_32(0xffffffff >>
1775                                                          (32 - width));
1776                 }
1777                 break;
1778         case RTE_FLOW_FIELD_META:
1779                 {
1780                         int reg = flow_dv_get_metadata_reg(dev, attr, error);
1781                         if (reg < 0)
1782                                 return;
1783                         MLX5_ASSERT(reg != REG_NON);
1784                         MLX5_ASSERT((unsigned int)reg < RTE_DIM(reg_to_field));
1785                         info[idx] = (struct field_modify_info){4, 0,
1786                                                 reg_to_field[reg]};
1787                         if (mask)
1788                                 mask[idx] =
1789                                         rte_cpu_to_be_32(0xffffffff >>
1790                                                          (32 - width));
1791                 }
1792                 break;
1793         case RTE_FLOW_FIELD_POINTER:
1794         case RTE_FLOW_FIELD_VALUE:
1795                 if (data->field == RTE_FLOW_FIELD_POINTER)
1796                         memcpy(&val, (void *)(uintptr_t)data->value,
1797                                sizeof(uint64_t));
1798                 else
1799                         val = data->value;
1800                 for (idx = 0; idx < MLX5_ACT_MAX_MOD_FIELDS; idx++) {
1801                         if (mask[idx]) {
1802                                 if (dst_width > 16) {
1803                                         value[idx] = rte_cpu_to_be_32(val);
1804                                         val >>= 32;
1805                                 } else if (dst_width > 8) {
1806                                         value[idx] = rte_cpu_to_be_16(val);
1807                                         val >>= 16;
1808                                 } else {
1809                                         value[idx] = (uint8_t)val;
1810                                         val >>= 8;
1811                                 }
1812                                 if (!val)
1813                                         break;
1814                         }
1815                 }
1816                 break;
1817         default:
1818                 MLX5_ASSERT(false);
1819                 break;
1820         }
1821 }
1822
1823 /**
1824  * Convert modify_field action to DV specification.
1825  *
1826  * @param[in] dev
1827  *   Pointer to the rte_eth_dev structure.
1828  * @param[in,out] resource
1829  *   Pointer to the modify-header resource.
1830  * @param[in] action
1831  *   Pointer to action specification.
1832  * @param[in] attr
1833  *   Attributes of flow that includes this item.
1834  * @param[out] error
1835  *   Pointer to the error structure.
1836  *
1837  * @return
1838  *   0 on success, a negative errno value otherwise and rte_errno is set.
1839  */
1840 static int
1841 flow_dv_convert_action_modify_field
1842                         (struct rte_eth_dev *dev,
1843                          struct mlx5_flow_dv_modify_hdr_resource *resource,
1844                          const struct rte_flow_action *action,
1845                          const struct rte_flow_attr *attr,
1846                          struct rte_flow_error *error)
1847 {
1848         const struct rte_flow_action_modify_field *conf =
1849                 (const struct rte_flow_action_modify_field *)(action->conf);
1850         struct rte_flow_item item;
1851         struct field_modify_info field[MLX5_ACT_MAX_MOD_FIELDS] = {
1852                                                                 {0, 0, 0} };
1853         struct field_modify_info dcopy[MLX5_ACT_MAX_MOD_FIELDS] = {
1854                                                                 {0, 0, 0} };
1855         uint32_t mask[MLX5_ACT_MAX_MOD_FIELDS] = {0, 0, 0, 0, 0};
1856         uint32_t value[MLX5_ACT_MAX_MOD_FIELDS] = {0, 0, 0, 0, 0};
1857         uint32_t type;
1858         uint32_t dst_width = mlx5_flow_item_field_width(conf->dst.field);
1859
1860         if (conf->src.field == RTE_FLOW_FIELD_POINTER ||
1861                 conf->src.field == RTE_FLOW_FIELD_VALUE) {
1862                 type = MLX5_MODIFICATION_TYPE_SET;
1863                 /** For SET fill the destination field (field) first. */
1864                 mlx5_flow_field_id_to_modify_info(&conf->dst, field, mask,
1865                         value, conf->width, dst_width, dev, attr, error);
1866                 /** Then copy immediate value from source as per mask. */
1867                 mlx5_flow_field_id_to_modify_info(&conf->src, dcopy, mask,
1868                         value, conf->width, dst_width, dev, attr, error);
1869                 item.spec = &value;
1870         } else {
1871                 type = MLX5_MODIFICATION_TYPE_COPY;
1872                 /** For COPY fill the destination field (dcopy) without mask. */
1873                 mlx5_flow_field_id_to_modify_info(&conf->dst, dcopy, NULL,
1874                         value, conf->width, dst_width, dev, attr, error);
1875                 /** Then construct the source field (field) with mask. */
1876                 mlx5_flow_field_id_to_modify_info(&conf->src, field, mask,
1877                         value, conf->width, dst_width, dev, attr, error);
1878         }
1879         item.mask = &mask;
1880         return flow_dv_convert_modify_action(&item,
1881                         field, dcopy, resource, type, error);
1882 }
1883
1884 /**
1885  * Validate MARK item.
1886  *
1887  * @param[in] dev
1888  *   Pointer to the rte_eth_dev structure.
1889  * @param[in] item
1890  *   Item specification.
1891  * @param[in] attr
1892  *   Attributes of flow that includes this item.
1893  * @param[out] error
1894  *   Pointer to error structure.
1895  *
1896  * @return
1897  *   0 on success, a negative errno value otherwise and rte_errno is set.
1898  */
1899 static int
1900 flow_dv_validate_item_mark(struct rte_eth_dev *dev,
1901                            const struct rte_flow_item *item,
1902                            const struct rte_flow_attr *attr __rte_unused,
1903                            struct rte_flow_error *error)
1904 {
1905         struct mlx5_priv *priv = dev->data->dev_private;
1906         struct mlx5_dev_config *config = &priv->config;
1907         const struct rte_flow_item_mark *spec = item->spec;
1908         const struct rte_flow_item_mark *mask = item->mask;
1909         const struct rte_flow_item_mark nic_mask = {
1910                 .id = priv->sh->dv_mark_mask,
1911         };
1912         int ret;
1913
1914         if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
1915                 return rte_flow_error_set(error, ENOTSUP,
1916                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1917                                           "extended metadata feature"
1918                                           " isn't enabled");
1919         if (!mlx5_flow_ext_mreg_supported(dev))
1920                 return rte_flow_error_set(error, ENOTSUP,
1921                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1922                                           "extended metadata register"
1923                                           " isn't supported");
1924         if (!nic_mask.id)
1925                 return rte_flow_error_set(error, ENOTSUP,
1926                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1927                                           "extended metadata register"
1928                                           " isn't available");
1929         ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
1930         if (ret < 0)
1931                 return ret;
1932         if (!spec)
1933                 return rte_flow_error_set(error, EINVAL,
1934                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1935                                           item->spec,
1936                                           "data cannot be empty");
1937         if (spec->id >= (MLX5_FLOW_MARK_MAX & nic_mask.id))
1938                 return rte_flow_error_set(error, EINVAL,
1939                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1940                                           &spec->id,
1941                                           "mark id exceeds the limit");
1942         if (!mask)
1943                 mask = &nic_mask;
1944         if (!mask->id)
1945                 return rte_flow_error_set(error, EINVAL,
1946                                         RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
1947                                         "mask cannot be zero");
1948
1949         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1950                                         (const uint8_t *)&nic_mask,
1951                                         sizeof(struct rte_flow_item_mark),
1952                                         MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
1953         if (ret < 0)
1954                 return ret;
1955         return 0;
1956 }
1957
1958 /**
1959  * Validate META item.
1960  *
1961  * @param[in] dev
1962  *   Pointer to the rte_eth_dev structure.
1963  * @param[in] item
1964  *   Item specification.
1965  * @param[in] attr
1966  *   Attributes of flow that includes this item.
1967  * @param[out] error
1968  *   Pointer to error structure.
1969  *
1970  * @return
1971  *   0 on success, a negative errno value otherwise and rte_errno is set.
1972  */
1973 static int
1974 flow_dv_validate_item_meta(struct rte_eth_dev *dev __rte_unused,
1975                            const struct rte_flow_item *item,
1976                            const struct rte_flow_attr *attr,
1977                            struct rte_flow_error *error)
1978 {
1979         struct mlx5_priv *priv = dev->data->dev_private;
1980         struct mlx5_dev_config *config = &priv->config;
1981         const struct rte_flow_item_meta *spec = item->spec;
1982         const struct rte_flow_item_meta *mask = item->mask;
1983         struct rte_flow_item_meta nic_mask = {
1984                 .data = UINT32_MAX
1985         };
1986         int reg;
1987         int ret;
1988
1989         if (!spec)
1990                 return rte_flow_error_set(error, EINVAL,
1991                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1992                                           item->spec,
1993                                           "data cannot be empty");
1994         if (config->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
1995                 if (!mlx5_flow_ext_mreg_supported(dev))
1996                         return rte_flow_error_set(error, ENOTSUP,
1997                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1998                                           "extended metadata register"
1999                                           " isn't supported");
2000                 reg = flow_dv_get_metadata_reg(dev, attr, error);
2001                 if (reg < 0)
2002                         return reg;
2003                 if (reg == REG_NON)
2004                         return rte_flow_error_set(error, ENOTSUP,
2005                                         RTE_FLOW_ERROR_TYPE_ITEM, item,
2006                                         "unavalable extended metadata register");
2007                 if (reg == REG_B)
2008                         return rte_flow_error_set(error, ENOTSUP,
2009                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2010                                           "match on reg_b "
2011                                           "isn't supported");
2012                 if (reg != REG_A)
2013                         nic_mask.data = priv->sh->dv_meta_mask;
2014         } else {
2015                 if (attr->transfer)
2016                         return rte_flow_error_set(error, ENOTSUP,
2017                                         RTE_FLOW_ERROR_TYPE_ITEM, item,
2018                                         "extended metadata feature "
2019                                         "should be enabled when "
2020                                         "meta item is requested "
2021                                         "with e-switch mode ");
2022                 if (attr->ingress)
2023                         return rte_flow_error_set(error, ENOTSUP,
2024                                         RTE_FLOW_ERROR_TYPE_ITEM, item,
2025                                         "match on metadata for ingress "
2026                                         "is not supported in legacy "
2027                                         "metadata mode");
2028         }
2029         if (!mask)
2030                 mask = &rte_flow_item_meta_mask;
2031         if (!mask->data)
2032                 return rte_flow_error_set(error, EINVAL,
2033                                         RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
2034                                         "mask cannot be zero");
2035
2036         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
2037                                         (const uint8_t *)&nic_mask,
2038                                         sizeof(struct rte_flow_item_meta),
2039                                         MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2040         return ret;
2041 }
2042
2043 /**
2044  * Validate TAG item.
2045  *
2046  * @param[in] dev
2047  *   Pointer to the rte_eth_dev structure.
2048  * @param[in] item
2049  *   Item specification.
2050  * @param[in] attr
2051  *   Attributes of flow that includes this item.
2052  * @param[out] error
2053  *   Pointer to error structure.
2054  *
2055  * @return
2056  *   0 on success, a negative errno value otherwise and rte_errno is set.
2057  */
2058 static int
2059 flow_dv_validate_item_tag(struct rte_eth_dev *dev,
2060                           const struct rte_flow_item *item,
2061                           const struct rte_flow_attr *attr __rte_unused,
2062                           struct rte_flow_error *error)
2063 {
2064         const struct rte_flow_item_tag *spec = item->spec;
2065         const struct rte_flow_item_tag *mask = item->mask;
2066         const struct rte_flow_item_tag nic_mask = {
2067                 .data = RTE_BE32(UINT32_MAX),
2068                 .index = 0xff,
2069         };
2070         int ret;
2071
2072         if (!mlx5_flow_ext_mreg_supported(dev))
2073                 return rte_flow_error_set(error, ENOTSUP,
2074                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2075                                           "extensive metadata register"
2076                                           " isn't supported");
2077         if (!spec)
2078                 return rte_flow_error_set(error, EINVAL,
2079                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
2080                                           item->spec,
2081                                           "data cannot be empty");
2082         if (!mask)
2083                 mask = &rte_flow_item_tag_mask;
2084         if (!mask->data)
2085                 return rte_flow_error_set(error, EINVAL,
2086                                         RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
2087                                         "mask cannot be zero");
2088
2089         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
2090                                         (const uint8_t *)&nic_mask,
2091                                         sizeof(struct rte_flow_item_tag),
2092                                         MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2093         if (ret < 0)
2094                 return ret;
2095         if (mask->index != 0xff)
2096                 return rte_flow_error_set(error, EINVAL,
2097                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
2098                                           "partial mask for tag index"
2099                                           " is not supported");
2100         ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, spec->index, error);
2101         if (ret < 0)
2102                 return ret;
2103         MLX5_ASSERT(ret != REG_NON);
2104         return 0;
2105 }
2106
2107 /**
2108  * Validate vport item.
2109  *
2110  * @param[in] dev
2111  *   Pointer to the rte_eth_dev structure.
2112  * @param[in] item
2113  *   Item specification.
2114  * @param[in] attr
2115  *   Attributes of flow that includes this item.
2116  * @param[in] item_flags
2117  *   Bit-fields that holds the items detected until now.
2118  * @param[out] error
2119  *   Pointer to error structure.
2120  *
2121  * @return
2122  *   0 on success, a negative errno value otherwise and rte_errno is set.
2123  */
2124 static int
2125 flow_dv_validate_item_port_id(struct rte_eth_dev *dev,
2126                               const struct rte_flow_item *item,
2127                               const struct rte_flow_attr *attr,
2128                               uint64_t item_flags,
2129                               struct rte_flow_error *error)
2130 {
2131         const struct rte_flow_item_port_id *spec = item->spec;
2132         const struct rte_flow_item_port_id *mask = item->mask;
2133         const struct rte_flow_item_port_id switch_mask = {
2134                         .id = 0xffffffff,
2135         };
2136         struct mlx5_priv *esw_priv;
2137         struct mlx5_priv *dev_priv;
2138         int ret;
2139
2140         if (!attr->transfer)
2141                 return rte_flow_error_set(error, EINVAL,
2142                                           RTE_FLOW_ERROR_TYPE_ITEM,
2143                                           NULL,
2144                                           "match on port id is valid only"
2145                                           " when transfer flag is enabled");
2146         if (item_flags & MLX5_FLOW_ITEM_PORT_ID)
2147                 return rte_flow_error_set(error, ENOTSUP,
2148                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2149                                           "multiple source ports are not"
2150                                           " supported");
2151         if (!mask)
2152                 mask = &switch_mask;
2153         if (mask->id != 0xffffffff)
2154                 return rte_flow_error_set(error, ENOTSUP,
2155                                            RTE_FLOW_ERROR_TYPE_ITEM_MASK,
2156                                            mask,
2157                                            "no support for partial mask on"
2158                                            " \"id\" field");
2159         ret = mlx5_flow_item_acceptable
2160                                 (item, (const uint8_t *)mask,
2161                                  (const uint8_t *)&rte_flow_item_port_id_mask,
2162                                  sizeof(struct rte_flow_item_port_id),
2163                                  MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2164         if (ret)
2165                 return ret;
2166         if (!spec)
2167                 return 0;
2168         esw_priv = mlx5_port_to_eswitch_info(spec->id, false);
2169         if (!esw_priv)
2170                 return rte_flow_error_set(error, rte_errno,
2171                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC, spec,
2172                                           "failed to obtain E-Switch info for"
2173                                           " port");
2174         dev_priv = mlx5_dev_to_eswitch_info(dev);
2175         if (!dev_priv)
2176                 return rte_flow_error_set(error, rte_errno,
2177                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2178                                           NULL,
2179                                           "failed to obtain E-Switch info");
2180         if (esw_priv->domain_id != dev_priv->domain_id)
2181                 return rte_flow_error_set(error, EINVAL,
2182                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC, spec,
2183                                           "cannot match on a port from a"
2184                                           " different E-Switch");
2185         return 0;
2186 }
2187
2188 /**
2189  * Validate VLAN item.
2190  *
2191  * @param[in] item
2192  *   Item specification.
2193  * @param[in] item_flags
2194  *   Bit-fields that holds the items detected until now.
2195  * @param[in] dev
2196  *   Ethernet device flow is being created on.
2197  * @param[out] error
2198  *   Pointer to error structure.
2199  *
2200  * @return
2201  *   0 on success, a negative errno value otherwise and rte_errno is set.
2202  */
2203 static int
2204 flow_dv_validate_item_vlan(const struct rte_flow_item *item,
2205                            uint64_t item_flags,
2206                            struct rte_eth_dev *dev,
2207                            struct rte_flow_error *error)
2208 {
2209         const struct rte_flow_item_vlan *mask = item->mask;
2210         const struct rte_flow_item_vlan nic_mask = {
2211                 .tci = RTE_BE16(UINT16_MAX),
2212                 .inner_type = RTE_BE16(UINT16_MAX),
2213                 .has_more_vlan = 1,
2214         };
2215         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
2216         int ret;
2217         const uint64_t l34m = tunnel ? (MLX5_FLOW_LAYER_INNER_L3 |
2218                                         MLX5_FLOW_LAYER_INNER_L4) :
2219                                        (MLX5_FLOW_LAYER_OUTER_L3 |
2220                                         MLX5_FLOW_LAYER_OUTER_L4);
2221         const uint64_t vlanm = tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
2222                                         MLX5_FLOW_LAYER_OUTER_VLAN;
2223
2224         if (item_flags & vlanm)
2225                 return rte_flow_error_set(error, EINVAL,
2226                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2227                                           "multiple VLAN layers not supported");
2228         else if ((item_flags & l34m) != 0)
2229                 return rte_flow_error_set(error, EINVAL,
2230                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2231                                           "VLAN cannot follow L3/L4 layer");
2232         if (!mask)
2233                 mask = &rte_flow_item_vlan_mask;
2234         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
2235                                         (const uint8_t *)&nic_mask,
2236                                         sizeof(struct rte_flow_item_vlan),
2237                                         MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2238         if (ret)
2239                 return ret;
2240         if (!tunnel && mask->tci != RTE_BE16(0x0fff)) {
2241                 struct mlx5_priv *priv = dev->data->dev_private;
2242
2243                 if (priv->vmwa_context) {
2244                         /*
2245                          * Non-NULL context means we have a virtual machine
2246                          * and SR-IOV enabled, we have to create VLAN interface
2247                          * to make hypervisor to setup E-Switch vport
2248                          * context correctly. We avoid creating the multiple
2249                          * VLAN interfaces, so we cannot support VLAN tag mask.
2250                          */
2251                         return rte_flow_error_set(error, EINVAL,
2252                                                   RTE_FLOW_ERROR_TYPE_ITEM,
2253                                                   item,
2254                                                   "VLAN tag mask is not"
2255                                                   " supported in virtual"
2256                                                   " environment");
2257                 }
2258         }
2259         return 0;
2260 }
2261
2262 /*
2263  * GTP flags are contained in 1 byte of the format:
2264  * -------------------------------------------
2265  * | bit   | 0 - 2   | 3  | 4   | 5 | 6 | 7  |
2266  * |-----------------------------------------|
2267  * | value | Version | PT | Res | E | S | PN |
2268  * -------------------------------------------
2269  *
2270  * Matching is supported only for GTP flags E, S, PN.
2271  */
2272 #define MLX5_GTP_FLAGS_MASK     0x07
2273
2274 /**
2275  * Validate GTP item.
2276  *
2277  * @param[in] dev
2278  *   Pointer to the rte_eth_dev structure.
2279  * @param[in] item
2280  *   Item specification.
2281  * @param[in] item_flags
2282  *   Bit-fields that holds the items detected until now.
2283  * @param[out] error
2284  *   Pointer to error structure.
2285  *
2286  * @return
2287  *   0 on success, a negative errno value otherwise and rte_errno is set.
2288  */
2289 static int
2290 flow_dv_validate_item_gtp(struct rte_eth_dev *dev,
2291                           const struct rte_flow_item *item,
2292                           uint64_t item_flags,
2293                           struct rte_flow_error *error)
2294 {
2295         struct mlx5_priv *priv = dev->data->dev_private;
2296         const struct rte_flow_item_gtp *spec = item->spec;
2297         const struct rte_flow_item_gtp *mask = item->mask;
2298         const struct rte_flow_item_gtp nic_mask = {
2299                 .v_pt_rsv_flags = MLX5_GTP_FLAGS_MASK,
2300                 .msg_type = 0xff,
2301                 .teid = RTE_BE32(0xffffffff),
2302         };
2303
2304         if (!priv->config.hca_attr.tunnel_stateless_gtp)
2305                 return rte_flow_error_set(error, ENOTSUP,
2306                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2307                                           "GTP support is not enabled");
2308         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
2309                 return rte_flow_error_set(error, ENOTSUP,
2310                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2311                                           "multiple tunnel layers not"
2312                                           " supported");
2313         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
2314                 return rte_flow_error_set(error, EINVAL,
2315                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2316                                           "no outer UDP layer found");
2317         if (!mask)
2318                 mask = &rte_flow_item_gtp_mask;
2319         if (spec && spec->v_pt_rsv_flags & ~MLX5_GTP_FLAGS_MASK)
2320                 return rte_flow_error_set(error, ENOTSUP,
2321                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2322                                           "Match is supported for GTP"
2323                                           " flags only");
2324         return mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
2325                                          (const uint8_t *)&nic_mask,
2326                                          sizeof(struct rte_flow_item_gtp),
2327                                          MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2328 }
2329
2330 /**
2331  * Validate GTP PSC item.
2332  *
2333  * @param[in] item
2334  *   Item specification.
2335  * @param[in] last_item
2336  *   Previous validated item in the pattern items.
2337  * @param[in] gtp_item
2338  *   Previous GTP item specification.
2339  * @param[in] attr
2340  *   Pointer to flow attributes.
2341  * @param[out] error
2342  *   Pointer to error structure.
2343  *
2344  * @return
2345  *   0 on success, a negative errno value otherwise and rte_errno is set.
2346  */
2347 static int
2348 flow_dv_validate_item_gtp_psc(const struct rte_flow_item *item,
2349                               uint64_t last_item,
2350                               const struct rte_flow_item *gtp_item,
2351                               const struct rte_flow_attr *attr,
2352                               struct rte_flow_error *error)
2353 {
2354         const struct rte_flow_item_gtp *gtp_spec;
2355         const struct rte_flow_item_gtp *gtp_mask;
2356         const struct rte_flow_item_gtp_psc *spec;
2357         const struct rte_flow_item_gtp_psc *mask;
2358         const struct rte_flow_item_gtp_psc nic_mask = {
2359                 .pdu_type = 0xFF,
2360                 .qfi = 0xFF,
2361         };
2362
2363         if (!gtp_item || !(last_item & MLX5_FLOW_LAYER_GTP))
2364                 return rte_flow_error_set
2365                         (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, item,
2366                          "GTP PSC item must be preceded with GTP item");
2367         gtp_spec = gtp_item->spec;
2368         gtp_mask = gtp_item->mask ? gtp_item->mask : &rte_flow_item_gtp_mask;
2369         /* GTP spec and E flag is requested to match zero. */
2370         if (gtp_spec &&
2371                 (gtp_mask->v_pt_rsv_flags &
2372                 ~gtp_spec->v_pt_rsv_flags & MLX5_GTP_EXT_HEADER_FLAG))
2373                 return rte_flow_error_set
2374                         (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, item,
2375                          "GTP E flag must be 1 to match GTP PSC");
2376         /* Check the flow is not created in group zero. */
2377         if (!attr->transfer && !attr->group)
2378                 return rte_flow_error_set
2379                         (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2380                          "GTP PSC is not supported for group 0");
2381         /* GTP spec is here and E flag is requested to match zero. */
2382         if (!item->spec)
2383                 return 0;
2384         spec = item->spec;
2385         mask = item->mask ? item->mask : &rte_flow_item_gtp_psc_mask;
2386         if (spec->pdu_type > MLX5_GTP_EXT_MAX_PDU_TYPE)
2387                 return rte_flow_error_set
2388                         (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, item,
2389                          "PDU type should be smaller than 16");
2390         return mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
2391                                          (const uint8_t *)&nic_mask,
2392                                          sizeof(struct rte_flow_item_gtp_psc),
2393                                          MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2394 }
2395
2396 /**
2397  * Validate IPV4 item.
2398  * Use existing validation function mlx5_flow_validate_item_ipv4(), and
2399  * add specific validation of fragment_offset field,
2400  *
2401  * @param[in] item
2402  *   Item specification.
2403  * @param[in] item_flags
2404  *   Bit-fields that holds the items detected until now.
2405  * @param[out] error
2406  *   Pointer to error structure.
2407  *
2408  * @return
2409  *   0 on success, a negative errno value otherwise and rte_errno is set.
2410  */
2411 static int
2412 flow_dv_validate_item_ipv4(const struct rte_flow_item *item,
2413                            uint64_t item_flags,
2414                            uint64_t last_item,
2415                            uint16_t ether_type,
2416                            struct rte_flow_error *error)
2417 {
2418         int ret;
2419         const struct rte_flow_item_ipv4 *spec = item->spec;
2420         const struct rte_flow_item_ipv4 *last = item->last;
2421         const struct rte_flow_item_ipv4 *mask = item->mask;
2422         rte_be16_t fragment_offset_spec = 0;
2423         rte_be16_t fragment_offset_last = 0;
2424         const struct rte_flow_item_ipv4 nic_ipv4_mask = {
2425                 .hdr = {
2426                         .src_addr = RTE_BE32(0xffffffff),
2427                         .dst_addr = RTE_BE32(0xffffffff),
2428                         .type_of_service = 0xff,
2429                         .fragment_offset = RTE_BE16(0xffff),
2430                         .next_proto_id = 0xff,
2431                         .time_to_live = 0xff,
2432                 },
2433         };
2434
2435         ret = mlx5_flow_validate_item_ipv4(item, item_flags, last_item,
2436                                            ether_type, &nic_ipv4_mask,
2437                                            MLX5_ITEM_RANGE_ACCEPTED, error);
2438         if (ret < 0)
2439                 return ret;
2440         if (spec && mask)
2441                 fragment_offset_spec = spec->hdr.fragment_offset &
2442                                        mask->hdr.fragment_offset;
2443         if (!fragment_offset_spec)
2444                 return 0;
2445         /*
2446          * spec and mask are valid, enforce using full mask to make sure the
2447          * complete value is used correctly.
2448          */
2449         if ((mask->hdr.fragment_offset & RTE_BE16(MLX5_IPV4_FRAG_OFFSET_MASK))
2450                         != RTE_BE16(MLX5_IPV4_FRAG_OFFSET_MASK))
2451                 return rte_flow_error_set(error, EINVAL,
2452                                           RTE_FLOW_ERROR_TYPE_ITEM_MASK,
2453                                           item, "must use full mask for"
2454                                           " fragment_offset");
2455         /*
2456          * Match on fragment_offset 0x2000 means MF is 1 and frag-offset is 0,
2457          * indicating this is 1st fragment of fragmented packet.
2458          * This is not yet supported in MLX5, return appropriate error message.
2459          */
2460         if (fragment_offset_spec == RTE_BE16(RTE_IPV4_HDR_MF_FLAG))
2461                 return rte_flow_error_set(error, ENOTSUP,
2462                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2463                                           "match on first fragment not "
2464                                           "supported");
2465         if (fragment_offset_spec && !last)
2466                 return rte_flow_error_set(error, ENOTSUP,
2467                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2468                                           "specified value not supported");
2469         /* spec and last are valid, validate the specified range. */
2470         fragment_offset_last = last->hdr.fragment_offset &
2471                                mask->hdr.fragment_offset;
2472         /*
2473          * Match on fragment_offset spec 0x2001 and last 0x3fff
2474          * means MF is 1 and frag-offset is > 0.
2475          * This packet is fragment 2nd and onward, excluding last.
2476          * This is not yet supported in MLX5, return appropriate
2477          * error message.
2478          */
2479         if (fragment_offset_spec == RTE_BE16(RTE_IPV4_HDR_MF_FLAG + 1) &&
2480             fragment_offset_last == RTE_BE16(MLX5_IPV4_FRAG_OFFSET_MASK))
2481                 return rte_flow_error_set(error, ENOTSUP,
2482                                           RTE_FLOW_ERROR_TYPE_ITEM_LAST,
2483                                           last, "match on following "
2484                                           "fragments not supported");
2485         /*
2486          * Match on fragment_offset spec 0x0001 and last 0x1fff
2487          * means MF is 0 and frag-offset is > 0.
2488          * This packet is last fragment of fragmented packet.
2489          * This is not yet supported in MLX5, return appropriate
2490          * error message.
2491          */
2492         if (fragment_offset_spec == RTE_BE16(1) &&
2493             fragment_offset_last == RTE_BE16(RTE_IPV4_HDR_OFFSET_MASK))
2494                 return rte_flow_error_set(error, ENOTSUP,
2495                                           RTE_FLOW_ERROR_TYPE_ITEM_LAST,
2496                                           last, "match on last "
2497                                           "fragment not supported");
2498         /*
2499          * Match on fragment_offset spec 0x0001 and last 0x3fff
2500          * means MF and/or frag-offset is not 0.
2501          * This is a fragmented packet.
2502          * Other range values are invalid and rejected.
2503          */
2504         if (!(fragment_offset_spec == RTE_BE16(1) &&
2505               fragment_offset_last == RTE_BE16(MLX5_IPV4_FRAG_OFFSET_MASK)))
2506                 return rte_flow_error_set(error, ENOTSUP,
2507                                           RTE_FLOW_ERROR_TYPE_ITEM_LAST, last,
2508                                           "specified range not supported");
2509         return 0;
2510 }
2511
2512 /**
2513  * Validate IPV6 fragment extension item.
2514  *
2515  * @param[in] item
2516  *   Item specification.
2517  * @param[in] item_flags
2518  *   Bit-fields that holds the items detected until now.
2519  * @param[out] error
2520  *   Pointer to error structure.
2521  *
2522  * @return
2523  *   0 on success, a negative errno value otherwise and rte_errno is set.
2524  */
2525 static int
2526 flow_dv_validate_item_ipv6_frag_ext(const struct rte_flow_item *item,
2527                                     uint64_t item_flags,
2528                                     struct rte_flow_error *error)
2529 {
2530         const struct rte_flow_item_ipv6_frag_ext *spec = item->spec;
2531         const struct rte_flow_item_ipv6_frag_ext *last = item->last;
2532         const struct rte_flow_item_ipv6_frag_ext *mask = item->mask;
2533         rte_be16_t frag_data_spec = 0;
2534         rte_be16_t frag_data_last = 0;
2535         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
2536         const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
2537                                       MLX5_FLOW_LAYER_OUTER_L4;
2538         int ret = 0;
2539         struct rte_flow_item_ipv6_frag_ext nic_mask = {
2540                 .hdr = {
2541                         .next_header = 0xff,
2542                         .frag_data = RTE_BE16(0xffff),
2543                 },
2544         };
2545
2546         if (item_flags & l4m)
2547                 return rte_flow_error_set(error, EINVAL,
2548                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2549                                           "ipv6 fragment extension item cannot "
2550                                           "follow L4 item.");
2551         if ((tunnel && !(item_flags & MLX5_FLOW_LAYER_INNER_L3_IPV6)) ||
2552             (!tunnel && !(item_flags & MLX5_FLOW_LAYER_OUTER_L3_IPV6)))
2553                 return rte_flow_error_set(error, EINVAL,
2554                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2555                                           "ipv6 fragment extension item must "
2556                                           "follow ipv6 item");
2557         if (spec && mask)
2558                 frag_data_spec = spec->hdr.frag_data & mask->hdr.frag_data;
2559         if (!frag_data_spec)
2560                 return 0;
2561         /*
2562          * spec and mask are valid, enforce using full mask to make sure the
2563          * complete value is used correctly.
2564          */
2565         if ((mask->hdr.frag_data & RTE_BE16(RTE_IPV6_FRAG_USED_MASK)) !=
2566                                 RTE_BE16(RTE_IPV6_FRAG_USED_MASK))
2567                 return rte_flow_error_set(error, EINVAL,
2568                                           RTE_FLOW_ERROR_TYPE_ITEM_MASK,
2569                                           item, "must use full mask for"
2570                                           " frag_data");
2571         /*
2572          * Match on frag_data 0x00001 means M is 1 and frag-offset is 0.
2573          * This is 1st fragment of fragmented packet.
2574          */
2575         if (frag_data_spec == RTE_BE16(RTE_IPV6_EHDR_MF_MASK))
2576                 return rte_flow_error_set(error, ENOTSUP,
2577                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2578                                           "match on first fragment not "
2579                                           "supported");
2580         if (frag_data_spec && !last)
2581                 return rte_flow_error_set(error, EINVAL,
2582                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2583                                           "specified value not supported");
2584         ret = mlx5_flow_item_acceptable
2585                                 (item, (const uint8_t *)mask,
2586                                  (const uint8_t *)&nic_mask,
2587                                  sizeof(struct rte_flow_item_ipv6_frag_ext),
2588                                  MLX5_ITEM_RANGE_ACCEPTED, error);
2589         if (ret)
2590                 return ret;
2591         /* spec and last are valid, validate the specified range. */
2592         frag_data_last = last->hdr.frag_data & mask->hdr.frag_data;
2593         /*
2594          * Match on frag_data spec 0x0009 and last 0xfff9
2595          * means M is 1 and frag-offset is > 0.
2596          * This packet is fragment 2nd and onward, excluding last.
2597          * This is not yet supported in MLX5, return appropriate
2598          * error message.
2599          */
2600         if (frag_data_spec == RTE_BE16(RTE_IPV6_EHDR_FO_ALIGN |
2601                                        RTE_IPV6_EHDR_MF_MASK) &&
2602             frag_data_last == RTE_BE16(RTE_IPV6_FRAG_USED_MASK))
2603                 return rte_flow_error_set(error, ENOTSUP,
2604                                           RTE_FLOW_ERROR_TYPE_ITEM_LAST,
2605                                           last, "match on following "
2606                                           "fragments not supported");
2607         /*
2608          * Match on frag_data spec 0x0008 and last 0xfff8
2609          * means M is 0 and frag-offset is > 0.
2610          * This packet is last fragment of fragmented packet.
2611          * This is not yet supported in MLX5, return appropriate
2612          * error message.
2613          */
2614         if (frag_data_spec == RTE_BE16(RTE_IPV6_EHDR_FO_ALIGN) &&
2615             frag_data_last == RTE_BE16(RTE_IPV6_EHDR_FO_MASK))
2616                 return rte_flow_error_set(error, ENOTSUP,
2617                                           RTE_FLOW_ERROR_TYPE_ITEM_LAST,
2618                                           last, "match on last "
2619                                           "fragment not supported");
2620         /* Other range values are invalid and rejected. */
2621         return rte_flow_error_set(error, EINVAL,
2622                                   RTE_FLOW_ERROR_TYPE_ITEM_LAST, last,
2623                                   "specified range not supported");
2624 }
2625
2626 /*
2627  * Validate ASO CT item.
2628  *
2629  * @param[in] dev
2630  *   Pointer to the rte_eth_dev structure.
2631  * @param[in] item
2632  *   Item specification.
2633  * @param[in] item_flags
2634  *   Pointer to bit-fields that holds the items detected until now.
2635  * @param[out] error
2636  *   Pointer to error structure.
2637  *
2638  * @return
2639  *   0 on success, a negative errno value otherwise and rte_errno is set.
2640  */
2641 static int
2642 flow_dv_validate_item_aso_ct(struct rte_eth_dev *dev,
2643                              const struct rte_flow_item *item,
2644                              uint64_t *item_flags,
2645                              struct rte_flow_error *error)
2646 {
2647         const struct rte_flow_item_conntrack *spec = item->spec;
2648         const struct rte_flow_item_conntrack *mask = item->mask;
2649         RTE_SET_USED(dev);
2650         uint32_t flags;
2651
2652         if (*item_flags & MLX5_FLOW_LAYER_ASO_CT)
2653                 return rte_flow_error_set(error, EINVAL,
2654                                           RTE_FLOW_ERROR_TYPE_ITEM, NULL,
2655                                           "Only one CT is supported");
2656         if (!mask)
2657                 mask = &rte_flow_item_conntrack_mask;
2658         flags = spec->flags & mask->flags;
2659         if ((flags & RTE_FLOW_CONNTRACK_PKT_STATE_VALID) &&
2660             ((flags & RTE_FLOW_CONNTRACK_PKT_STATE_INVALID) ||
2661              (flags & RTE_FLOW_CONNTRACK_PKT_STATE_BAD) ||
2662              (flags & RTE_FLOW_CONNTRACK_PKT_STATE_DISABLED)))
2663                 return rte_flow_error_set(error, EINVAL,
2664                                           RTE_FLOW_ERROR_TYPE_ITEM, NULL,
2665                                           "Conflict status bits");
2666         /* State change also needs to be considered. */
2667         *item_flags |= MLX5_FLOW_LAYER_ASO_CT;
2668         return 0;
2669 }
2670
2671 /**
2672  * Validate the pop VLAN action.
2673  *
2674  * @param[in] dev
2675  *   Pointer to the rte_eth_dev structure.
2676  * @param[in] action_flags
2677  *   Holds the actions detected until now.
2678  * @param[in] action
2679  *   Pointer to the pop vlan action.
2680  * @param[in] item_flags
2681  *   The items found in this flow rule.
2682  * @param[in] attr
2683  *   Pointer to flow attributes.
2684  * @param[out] error
2685  *   Pointer to error structure.
2686  *
2687  * @return
2688  *   0 on success, a negative errno value otherwise and rte_errno is set.
2689  */
2690 static int
2691 flow_dv_validate_action_pop_vlan(struct rte_eth_dev *dev,
2692                                  uint64_t action_flags,
2693                                  const struct rte_flow_action *action,
2694                                  uint64_t item_flags,
2695                                  const struct rte_flow_attr *attr,
2696                                  struct rte_flow_error *error)
2697 {
2698         const struct mlx5_priv *priv = dev->data->dev_private;
2699
2700         (void)action;
2701         (void)attr;
2702         if (!priv->sh->pop_vlan_action)
2703                 return rte_flow_error_set(error, ENOTSUP,
2704                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2705                                           NULL,
2706                                           "pop vlan action is not supported");
2707         if (attr->egress)
2708                 return rte_flow_error_set(error, ENOTSUP,
2709                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
2710                                           NULL,
2711                                           "pop vlan action not supported for "
2712                                           "egress");
2713         if (action_flags & MLX5_FLOW_VLAN_ACTIONS)
2714                 return rte_flow_error_set(error, ENOTSUP,
2715                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2716                                           "no support for multiple VLAN "
2717                                           "actions");
2718         /* Pop VLAN with preceding Decap requires inner header with VLAN. */
2719         if ((action_flags & MLX5_FLOW_ACTION_DECAP) &&
2720             !(item_flags & MLX5_FLOW_LAYER_INNER_VLAN))
2721                 return rte_flow_error_set(error, ENOTSUP,
2722                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2723                                           NULL,
2724                                           "cannot pop vlan after decap without "
2725                                           "match on inner vlan in the flow");
2726         /* Pop VLAN without preceding Decap requires outer header with VLAN. */
2727         if (!(action_flags & MLX5_FLOW_ACTION_DECAP) &&
2728             !(item_flags & MLX5_FLOW_LAYER_OUTER_VLAN))
2729                 return rte_flow_error_set(error, ENOTSUP,
2730                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2731                                           NULL,
2732                                           "cannot pop vlan without a "
2733                                           "match on (outer) vlan in the flow");
2734         if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
2735                 return rte_flow_error_set(error, EINVAL,
2736                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2737                                           "wrong action order, port_id should "
2738                                           "be after pop VLAN action");
2739         if (!attr->transfer && priv->representor)
2740                 return rte_flow_error_set(error, ENOTSUP,
2741                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2742                                           "pop vlan action for VF representor "
2743                                           "not supported on NIC table");
2744         return 0;
2745 }
2746
2747 /**
2748  * Get VLAN default info from vlan match info.
2749  *
2750  * @param[in] items
2751  *   the list of item specifications.
2752  * @param[out] vlan
2753  *   pointer VLAN info to fill to.
2754  *
2755  * @return
2756  *   0 on success, a negative errno value otherwise and rte_errno is set.
2757  */
2758 static void
2759 flow_dev_get_vlan_info_from_items(const struct rte_flow_item *items,
2760                                   struct rte_vlan_hdr *vlan)
2761 {
2762         const struct rte_flow_item_vlan nic_mask = {
2763                 .tci = RTE_BE16(MLX5DV_FLOW_VLAN_PCP_MASK |
2764                                 MLX5DV_FLOW_VLAN_VID_MASK),
2765                 .inner_type = RTE_BE16(0xffff),
2766         };
2767
2768         if (items == NULL)
2769                 return;
2770         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
2771                 int type = items->type;
2772
2773                 if (type == RTE_FLOW_ITEM_TYPE_VLAN ||
2774                     type == MLX5_RTE_FLOW_ITEM_TYPE_VLAN)
2775                         break;
2776         }
2777         if (items->type != RTE_FLOW_ITEM_TYPE_END) {
2778                 const struct rte_flow_item_vlan *vlan_m = items->mask;
2779                 const struct rte_flow_item_vlan *vlan_v = items->spec;
2780
2781                 /* If VLAN item in pattern doesn't contain data, return here. */
2782                 if (!vlan_v)
2783                         return;
2784                 if (!vlan_m)
2785                         vlan_m = &nic_mask;
2786                 /* Only full match values are accepted */
2787                 if ((vlan_m->tci & MLX5DV_FLOW_VLAN_PCP_MASK_BE) ==
2788                      MLX5DV_FLOW_VLAN_PCP_MASK_BE) {
2789                         vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_PCP_MASK;
2790                         vlan->vlan_tci |=
2791                                 rte_be_to_cpu_16(vlan_v->tci &
2792                                                  MLX5DV_FLOW_VLAN_PCP_MASK_BE);
2793                 }
2794                 if ((vlan_m->tci & MLX5DV_FLOW_VLAN_VID_MASK_BE) ==
2795                      MLX5DV_FLOW_VLAN_VID_MASK_BE) {
2796                         vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_VID_MASK;
2797                         vlan->vlan_tci |=
2798                                 rte_be_to_cpu_16(vlan_v->tci &
2799                                                  MLX5DV_FLOW_VLAN_VID_MASK_BE);
2800                 }
2801                 if (vlan_m->inner_type == nic_mask.inner_type)
2802                         vlan->eth_proto = rte_be_to_cpu_16(vlan_v->inner_type &
2803                                                            vlan_m->inner_type);
2804         }
2805 }
2806
2807 /**
2808  * Validate the push VLAN action.
2809  *
2810  * @param[in] dev
2811  *   Pointer to the rte_eth_dev structure.
2812  * @param[in] action_flags
2813  *   Holds the actions detected until now.
2814  * @param[in] item_flags
2815  *   The items found in this flow rule.
2816  * @param[in] action
2817  *   Pointer to the action structure.
2818  * @param[in] attr
2819  *   Pointer to flow attributes
2820  * @param[out] error
2821  *   Pointer to error structure.
2822  *
2823  * @return
2824  *   0 on success, a negative errno value otherwise and rte_errno is set.
2825  */
2826 static int
2827 flow_dv_validate_action_push_vlan(struct rte_eth_dev *dev,
2828                                   uint64_t action_flags,
2829                                   const struct rte_flow_item_vlan *vlan_m,
2830                                   const struct rte_flow_action *action,
2831                                   const struct rte_flow_attr *attr,
2832                                   struct rte_flow_error *error)
2833 {
2834         const struct rte_flow_action_of_push_vlan *push_vlan = action->conf;
2835         const struct mlx5_priv *priv = dev->data->dev_private;
2836
2837         if (push_vlan->ethertype != RTE_BE16(RTE_ETHER_TYPE_VLAN) &&
2838             push_vlan->ethertype != RTE_BE16(RTE_ETHER_TYPE_QINQ))
2839                 return rte_flow_error_set(error, EINVAL,
2840                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2841                                           "invalid vlan ethertype");
2842         if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
2843                 return rte_flow_error_set(error, EINVAL,
2844                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2845                                           "wrong action order, port_id should "
2846                                           "be after push VLAN");
2847         if (!attr->transfer && priv->representor)
2848                 return rte_flow_error_set(error, ENOTSUP,
2849                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2850                                           "push vlan action for VF representor "
2851                                           "not supported on NIC table");
2852         if (vlan_m &&
2853             (vlan_m->tci & MLX5DV_FLOW_VLAN_PCP_MASK_BE) &&
2854             (vlan_m->tci & MLX5DV_FLOW_VLAN_PCP_MASK_BE) !=
2855                 MLX5DV_FLOW_VLAN_PCP_MASK_BE &&
2856             !(action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_PCP) &&
2857             !(mlx5_flow_find_action
2858                 (action + 1, RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP)))
2859                 return rte_flow_error_set(error, EINVAL,
2860                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2861                                           "not full match mask on VLAN PCP and "
2862                                           "there is no of_set_vlan_pcp action, "
2863                                           "push VLAN action cannot figure out "
2864                                           "PCP value");
2865         if (vlan_m &&
2866             (vlan_m->tci & MLX5DV_FLOW_VLAN_VID_MASK_BE) &&
2867             (vlan_m->tci & MLX5DV_FLOW_VLAN_VID_MASK_BE) !=
2868                 MLX5DV_FLOW_VLAN_VID_MASK_BE &&
2869             !(action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_VID) &&
2870             !(mlx5_flow_find_action
2871                 (action + 1, RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID)))
2872                 return rte_flow_error_set(error, EINVAL,
2873                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2874                                           "not full match mask on VLAN VID and "
2875                                           "there is no of_set_vlan_vid action, "
2876                                           "push VLAN action cannot figure out "
2877                                           "VID value");
2878         (void)attr;
2879         return 0;
2880 }
2881
2882 /**
2883  * Validate the set VLAN PCP.
2884  *
2885  * @param[in] action_flags
2886  *   Holds the actions detected until now.
2887  * @param[in] actions
2888  *   Pointer to the list of actions remaining in the flow rule.
2889  * @param[out] error
2890  *   Pointer to error structure.
2891  *
2892  * @return
2893  *   0 on success, a negative errno value otherwise and rte_errno is set.
2894  */
2895 static int
2896 flow_dv_validate_action_set_vlan_pcp(uint64_t action_flags,
2897                                      const struct rte_flow_action actions[],
2898                                      struct rte_flow_error *error)
2899 {
2900         const struct rte_flow_action *action = actions;
2901         const struct rte_flow_action_of_set_vlan_pcp *conf = action->conf;
2902
2903         if (conf->vlan_pcp > 7)
2904                 return rte_flow_error_set(error, EINVAL,
2905                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2906                                           "VLAN PCP value is too big");
2907         if (!(action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN))
2908                 return rte_flow_error_set(error, ENOTSUP,
2909                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2910                                           "set VLAN PCP action must follow "
2911                                           "the push VLAN action");
2912         if (action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_PCP)
2913                 return rte_flow_error_set(error, ENOTSUP,
2914                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2915                                           "Multiple VLAN PCP modification are "
2916                                           "not supported");
2917         if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
2918                 return rte_flow_error_set(error, EINVAL,
2919                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2920                                           "wrong action order, port_id should "
2921                                           "be after set VLAN PCP");
2922         return 0;
2923 }
2924
2925 /**
2926  * Validate the set VLAN VID.
2927  *
2928  * @param[in] item_flags
2929  *   Holds the items detected in this rule.
2930  * @param[in] action_flags
2931  *   Holds the actions detected until now.
2932  * @param[in] actions
2933  *   Pointer to the list of actions remaining in the flow rule.
2934  * @param[out] error
2935  *   Pointer to error structure.
2936  *
2937  * @return
2938  *   0 on success, a negative errno value otherwise and rte_errno is set.
2939  */
2940 static int
2941 flow_dv_validate_action_set_vlan_vid(uint64_t item_flags,
2942                                      uint64_t action_flags,
2943                                      const struct rte_flow_action actions[],
2944                                      struct rte_flow_error *error)
2945 {
2946         const struct rte_flow_action *action = actions;
2947         const struct rte_flow_action_of_set_vlan_vid *conf = action->conf;
2948
2949         if (rte_be_to_cpu_16(conf->vlan_vid) > 0xFFE)
2950                 return rte_flow_error_set(error, EINVAL,
2951                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2952                                           "VLAN VID value is too big");
2953         if (!(action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN) &&
2954             !(item_flags & MLX5_FLOW_LAYER_OUTER_VLAN))
2955                 return rte_flow_error_set(error, ENOTSUP,
2956                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2957                                           "set VLAN VID action must follow push"
2958                                           " VLAN action or match on VLAN item");
2959         if (action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_VID)
2960                 return rte_flow_error_set(error, ENOTSUP,
2961                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2962                                           "Multiple VLAN VID modifications are "
2963                                           "not supported");
2964         if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
2965                 return rte_flow_error_set(error, EINVAL,
2966                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2967                                           "wrong action order, port_id should "
2968                                           "be after set VLAN VID");
2969         return 0;
2970 }
2971
2972 /*
2973  * Validate the FLAG action.
2974  *
2975  * @param[in] dev
2976  *   Pointer to the rte_eth_dev structure.
2977  * @param[in] action_flags
2978  *   Holds the actions detected until now.
2979  * @param[in] attr
2980  *   Pointer to flow attributes
2981  * @param[out] error
2982  *   Pointer to error structure.
2983  *
2984  * @return
2985  *   0 on success, a negative errno value otherwise and rte_errno is set.
2986  */
2987 static int
2988 flow_dv_validate_action_flag(struct rte_eth_dev *dev,
2989                              uint64_t action_flags,
2990                              const struct rte_flow_attr *attr,
2991                              struct rte_flow_error *error)
2992 {
2993         struct mlx5_priv *priv = dev->data->dev_private;
2994         struct mlx5_dev_config *config = &priv->config;
2995         int ret;
2996
2997         /* Fall back if no extended metadata register support. */
2998         if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
2999                 return mlx5_flow_validate_action_flag(action_flags, attr,
3000                                                       error);
3001         /* Extensive metadata mode requires registers. */
3002         if (!mlx5_flow_ext_mreg_supported(dev))
3003                 return rte_flow_error_set(error, ENOTSUP,
3004                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3005                                           "no metadata registers "
3006                                           "to support flag action");
3007         if (!(priv->sh->dv_mark_mask & MLX5_FLOW_MARK_DEFAULT))
3008                 return rte_flow_error_set(error, ENOTSUP,
3009                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3010                                           "extended metadata register"
3011                                           " isn't available");
3012         ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
3013         if (ret < 0)
3014                 return ret;
3015         MLX5_ASSERT(ret > 0);
3016         if (action_flags & MLX5_FLOW_ACTION_MARK)
3017                 return rte_flow_error_set(error, EINVAL,
3018                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3019                                           "can't mark and flag in same flow");
3020         if (action_flags & MLX5_FLOW_ACTION_FLAG)
3021                 return rte_flow_error_set(error, EINVAL,
3022                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3023                                           "can't have 2 flag"
3024                                           " actions in same flow");
3025         return 0;
3026 }
3027
3028 /**
3029  * Validate MARK action.
3030  *
3031  * @param[in] dev
3032  *   Pointer to the rte_eth_dev structure.
3033  * @param[in] action
3034  *   Pointer to action.
3035  * @param[in] action_flags
3036  *   Holds the actions detected until now.
3037  * @param[in] attr
3038  *   Pointer to flow attributes
3039  * @param[out] error
3040  *   Pointer to error structure.
3041  *
3042  * @return
3043  *   0 on success, a negative errno value otherwise and rte_errno is set.
3044  */
3045 static int
3046 flow_dv_validate_action_mark(struct rte_eth_dev *dev,
3047                              const struct rte_flow_action *action,
3048                              uint64_t action_flags,
3049                              const struct rte_flow_attr *attr,
3050                              struct rte_flow_error *error)
3051 {
3052         struct mlx5_priv *priv = dev->data->dev_private;
3053         struct mlx5_dev_config *config = &priv->config;
3054         const struct rte_flow_action_mark *mark = action->conf;
3055         int ret;
3056
3057         if (is_tunnel_offload_active(dev))
3058                 return rte_flow_error_set(error, ENOTSUP,
3059                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3060                                           "no mark action "
3061                                           "if tunnel offload active");
3062         /* Fall back if no extended metadata register support. */
3063         if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
3064                 return mlx5_flow_validate_action_mark(action, action_flags,
3065                                                       attr, error);
3066         /* Extensive metadata mode requires registers. */
3067         if (!mlx5_flow_ext_mreg_supported(dev))
3068                 return rte_flow_error_set(error, ENOTSUP,
3069                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3070                                           "no metadata registers "
3071                                           "to support mark action");
3072         if (!priv->sh->dv_mark_mask)
3073                 return rte_flow_error_set(error, ENOTSUP,
3074                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3075                                           "extended metadata register"
3076                                           " isn't available");
3077         ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
3078         if (ret < 0)
3079                 return ret;
3080         MLX5_ASSERT(ret > 0);
3081         if (!mark)
3082                 return rte_flow_error_set(error, EINVAL,
3083                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3084                                           "configuration cannot be null");
3085         if (mark->id >= (MLX5_FLOW_MARK_MAX & priv->sh->dv_mark_mask))
3086                 return rte_flow_error_set(error, EINVAL,
3087                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
3088                                           &mark->id,
3089                                           "mark id exceeds the limit");
3090         if (action_flags & MLX5_FLOW_ACTION_FLAG)
3091                 return rte_flow_error_set(error, EINVAL,
3092                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3093                                           "can't flag and mark in same flow");
3094         if (action_flags & MLX5_FLOW_ACTION_MARK)
3095                 return rte_flow_error_set(error, EINVAL,
3096                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3097                                           "can't have 2 mark actions in same"
3098                                           " flow");
3099         return 0;
3100 }
3101
3102 /**
3103  * Validate SET_META action.
3104  *
3105  * @param[in] dev
3106  *   Pointer to the rte_eth_dev structure.
3107  * @param[in] action
3108  *   Pointer to the action structure.
3109  * @param[in] action_flags
3110  *   Holds the actions detected until now.
3111  * @param[in] attr
3112  *   Pointer to flow attributes
3113  * @param[out] error
3114  *   Pointer to error structure.
3115  *
3116  * @return
3117  *   0 on success, a negative errno value otherwise and rte_errno is set.
3118  */
3119 static int
3120 flow_dv_validate_action_set_meta(struct rte_eth_dev *dev,
3121                                  const struct rte_flow_action *action,
3122                                  uint64_t action_flags __rte_unused,
3123                                  const struct rte_flow_attr *attr,
3124                                  struct rte_flow_error *error)
3125 {
3126         const struct rte_flow_action_set_meta *conf;
3127         uint32_t nic_mask = UINT32_MAX;
3128         int reg;
3129
3130         if (!mlx5_flow_ext_mreg_supported(dev))
3131                 return rte_flow_error_set(error, ENOTSUP,
3132                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3133                                           "extended metadata register"
3134                                           " isn't supported");
3135         reg = flow_dv_get_metadata_reg(dev, attr, error);
3136         if (reg < 0)
3137                 return reg;
3138         if (reg == REG_NON)
3139                 return rte_flow_error_set(error, ENOTSUP,
3140                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3141                                           "unavalable extended metadata register");
3142         if (reg != REG_A && reg != REG_B) {
3143                 struct mlx5_priv *priv = dev->data->dev_private;
3144
3145                 nic_mask = priv->sh->dv_meta_mask;
3146         }
3147         if (!(action->conf))
3148                 return rte_flow_error_set(error, EINVAL,
3149                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3150                                           "configuration cannot be null");
3151         conf = (const struct rte_flow_action_set_meta *)action->conf;
3152         if (!conf->mask)
3153                 return rte_flow_error_set(error, EINVAL,
3154                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3155                                           "zero mask doesn't have any effect");
3156         if (conf->mask & ~nic_mask)
3157                 return rte_flow_error_set(error, EINVAL,
3158                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3159                                           "meta data must be within reg C0");
3160         return 0;
3161 }
3162
3163 /**
3164  * Validate SET_TAG action.
3165  *
3166  * @param[in] dev
3167  *   Pointer to the rte_eth_dev structure.
3168  * @param[in] action
3169  *   Pointer to the action structure.
3170  * @param[in] action_flags
3171  *   Holds the actions detected until now.
3172  * @param[in] attr
3173  *   Pointer to flow attributes
3174  * @param[out] error
3175  *   Pointer to error structure.
3176  *
3177  * @return
3178  *   0 on success, a negative errno value otherwise and rte_errno is set.
3179  */
3180 static int
3181 flow_dv_validate_action_set_tag(struct rte_eth_dev *dev,
3182                                 const struct rte_flow_action *action,
3183                                 uint64_t action_flags,
3184                                 const struct rte_flow_attr *attr,
3185                                 struct rte_flow_error *error)
3186 {
3187         const struct rte_flow_action_set_tag *conf;
3188         const uint64_t terminal_action_flags =
3189                 MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_QUEUE |
3190                 MLX5_FLOW_ACTION_RSS;
3191         int ret;
3192
3193         if (!mlx5_flow_ext_mreg_supported(dev))
3194                 return rte_flow_error_set(error, ENOTSUP,
3195                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3196                                           "extensive metadata register"
3197                                           " isn't supported");
3198         if (!(action->conf))
3199                 return rte_flow_error_set(error, EINVAL,
3200                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3201                                           "configuration cannot be null");
3202         conf = (const struct rte_flow_action_set_tag *)action->conf;
3203         if (!conf->mask)
3204                 return rte_flow_error_set(error, EINVAL,
3205                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3206                                           "zero mask doesn't have any effect");
3207         ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, conf->index, error);
3208         if (ret < 0)
3209                 return ret;
3210         if (!attr->transfer && attr->ingress &&
3211             (action_flags & terminal_action_flags))
3212                 return rte_flow_error_set(error, EINVAL,
3213                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3214                                           "set_tag has no effect"
3215                                           " with terminal actions");
3216         return 0;
3217 }
3218
3219 /**
3220  * Check if action counter is shared by either old or new mechanism.
3221  *
3222  * @param[in] action
3223  *   Pointer to the action structure.
3224  *
3225  * @return
3226  *   True when counter is shared, false otherwise.
3227  */
3228 static inline bool
3229 is_shared_action_count(const struct rte_flow_action *action)
3230 {
3231         const struct rte_flow_action_count *count =
3232                         (const struct rte_flow_action_count *)action->conf;
3233
3234         if ((int)action->type == MLX5_RTE_FLOW_ACTION_TYPE_COUNT)
3235                 return true;
3236         return !!(count && count->shared);
3237 }
3238
3239 /**
3240  * Validate count action.
3241  *
3242  * @param[in] dev
3243  *   Pointer to rte_eth_dev structure.
3244  * @param[in] shared
3245  *   Indicator if action is shared.
3246  * @param[in] action_flags
3247  *   Holds the actions detected until now.
3248  * @param[out] error
3249  *   Pointer to error structure.
3250  *
3251  * @return
3252  *   0 on success, a negative errno value otherwise and rte_errno is set.
3253  */
3254 static int
3255 flow_dv_validate_action_count(struct rte_eth_dev *dev, bool shared,
3256                               uint64_t action_flags,
3257                               struct rte_flow_error *error)
3258 {
3259         struct mlx5_priv *priv = dev->data->dev_private;
3260
3261         if (!priv->config.devx)
3262                 goto notsup_err;
3263         if (action_flags & MLX5_FLOW_ACTION_COUNT)
3264                 return rte_flow_error_set(error, EINVAL,
3265                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3266                                           "duplicate count actions set");
3267         if (shared && (action_flags & MLX5_FLOW_ACTION_AGE) &&
3268             !priv->sh->flow_hit_aso_en)
3269                 return rte_flow_error_set(error, EINVAL,
3270                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3271                                           "old age and shared count combination is not supported");
3272 #ifdef HAVE_IBV_FLOW_DEVX_COUNTERS
3273         return 0;
3274 #endif
3275 notsup_err:
3276         return rte_flow_error_set
3277                       (error, ENOTSUP,
3278                        RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3279                        NULL,
3280                        "count action not supported");
3281 }
3282
3283 /**
3284  * Validate the L2 encap action.
3285  *
3286  * @param[in] dev
3287  *   Pointer to the rte_eth_dev structure.
3288  * @param[in] action_flags
3289  *   Holds the actions detected until now.
3290  * @param[in] action
3291  *   Pointer to the action structure.
3292  * @param[in] attr
3293  *   Pointer to flow attributes.
3294  * @param[out] error
3295  *   Pointer to error structure.
3296  *
3297  * @return
3298  *   0 on success, a negative errno value otherwise and rte_errno is set.
3299  */
3300 static int
3301 flow_dv_validate_action_l2_encap(struct rte_eth_dev *dev,
3302                                  uint64_t action_flags,
3303                                  const struct rte_flow_action *action,
3304                                  const struct rte_flow_attr *attr,
3305                                  struct rte_flow_error *error)
3306 {
3307         const struct mlx5_priv *priv = dev->data->dev_private;
3308
3309         if (!(action->conf))
3310                 return rte_flow_error_set(error, EINVAL,
3311                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3312                                           "configuration cannot be null");
3313         if (action_flags & MLX5_FLOW_ACTION_ENCAP)
3314                 return rte_flow_error_set(error, EINVAL,
3315                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3316                                           "can only have a single encap action "
3317                                           "in a flow");
3318         if (!attr->transfer && priv->representor)
3319                 return rte_flow_error_set(error, ENOTSUP,
3320                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3321                                           "encap action for VF representor "
3322                                           "not supported on NIC table");
3323         return 0;
3324 }
3325
3326 /**
3327  * Validate a decap action.
3328  *
3329  * @param[in] dev
3330  *   Pointer to the rte_eth_dev structure.
3331  * @param[in] action_flags
3332  *   Holds the actions detected until now.
3333  * @param[in] action
3334  *   Pointer to the action structure.
3335  * @param[in] item_flags
3336  *   Holds the items detected.
3337  * @param[in] attr
3338  *   Pointer to flow attributes
3339  * @param[out] error
3340  *   Pointer to error structure.
3341  *
3342  * @return
3343  *   0 on success, a negative errno value otherwise and rte_errno is set.
3344  */
3345 static int
3346 flow_dv_validate_action_decap(struct rte_eth_dev *dev,
3347                               uint64_t action_flags,
3348                               const struct rte_flow_action *action,
3349                               const uint64_t item_flags,
3350                               const struct rte_flow_attr *attr,
3351                               struct rte_flow_error *error)
3352 {
3353         const struct mlx5_priv *priv = dev->data->dev_private;
3354
3355         if (priv->config.hca_attr.scatter_fcs_w_decap_disable &&
3356             !priv->config.decap_en)
3357                 return rte_flow_error_set(error, ENOTSUP,
3358                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3359                                           "decap is not enabled");
3360         if (action_flags & MLX5_FLOW_XCAP_ACTIONS)
3361                 return rte_flow_error_set(error, ENOTSUP,
3362                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3363                                           action_flags &
3364                                           MLX5_FLOW_ACTION_DECAP ? "can only "
3365                                           "have a single decap action" : "decap "
3366                                           "after encap is not supported");
3367         if (action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS)
3368                 return rte_flow_error_set(error, EINVAL,
3369                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3370                                           "can't have decap action after"
3371                                           " modify action");
3372         if (attr->egress)
3373                 return rte_flow_error_set(error, ENOTSUP,
3374                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
3375                                           NULL,
3376                                           "decap action not supported for "
3377                                           "egress");
3378         if (!attr->transfer && priv->representor)
3379                 return rte_flow_error_set(error, ENOTSUP,
3380                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3381                                           "decap action for VF representor "
3382                                           "not supported on NIC table");
3383         if (action->type == RTE_FLOW_ACTION_TYPE_VXLAN_DECAP &&
3384             !(item_flags & MLX5_FLOW_LAYER_VXLAN))
3385                 return rte_flow_error_set(error, ENOTSUP,
3386                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3387                                 "VXLAN item should be present for VXLAN decap");
3388         return 0;
3389 }
3390
3391 const struct rte_flow_action_raw_decap empty_decap = {.data = NULL, .size = 0,};
3392
3393 /**
3394  * Validate the raw encap and decap actions.
3395  *
3396  * @param[in] dev
3397  *   Pointer to the rte_eth_dev structure.
3398  * @param[in] decap
3399  *   Pointer to the decap action.
3400  * @param[in] encap
3401  *   Pointer to the encap action.
3402  * @param[in] attr
3403  *   Pointer to flow attributes
3404  * @param[in/out] action_flags
3405  *   Holds the actions detected until now.
3406  * @param[out] actions_n
3407  *   pointer to the number of actions counter.
3408  * @param[in] action
3409  *   Pointer to the action structure.
3410  * @param[in] item_flags
3411  *   Holds the items detected.
3412  * @param[out] error
3413  *   Pointer to error structure.
3414  *
3415  * @return
3416  *   0 on success, a negative errno value otherwise and rte_errno is set.
3417  */
3418 static int
3419 flow_dv_validate_action_raw_encap_decap
3420         (struct rte_eth_dev *dev,
3421          const struct rte_flow_action_raw_decap *decap,
3422          const struct rte_flow_action_raw_encap *encap,
3423          const struct rte_flow_attr *attr, uint64_t *action_flags,
3424          int *actions_n, const struct rte_flow_action *action,
3425          uint64_t item_flags, struct rte_flow_error *error)
3426 {
3427         const struct mlx5_priv *priv = dev->data->dev_private;
3428         int ret;
3429
3430         if (encap && (!encap->size || !encap->data))
3431                 return rte_flow_error_set(error, EINVAL,
3432                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3433                                           "raw encap data cannot be empty");
3434         if (decap && encap) {
3435                 if (decap->size <= MLX5_ENCAPSULATION_DECISION_SIZE &&
3436                     encap->size > MLX5_ENCAPSULATION_DECISION_SIZE)
3437                         /* L3 encap. */
3438                         decap = NULL;
3439                 else if (encap->size <=
3440                            MLX5_ENCAPSULATION_DECISION_SIZE &&
3441                            decap->size >
3442                            MLX5_ENCAPSULATION_DECISION_SIZE)
3443                         /* L3 decap. */
3444                         encap = NULL;
3445                 else if (encap->size >
3446                            MLX5_ENCAPSULATION_DECISION_SIZE &&
3447                            decap->size >
3448                            MLX5_ENCAPSULATION_DECISION_SIZE)
3449                         /* 2 L2 actions: encap and decap. */
3450                         ;
3451                 else
3452                         return rte_flow_error_set(error,
3453                                 ENOTSUP,
3454                                 RTE_FLOW_ERROR_TYPE_ACTION,
3455                                 NULL, "unsupported too small "
3456                                 "raw decap and too small raw "
3457                                 "encap combination");
3458         }
3459         if (decap) {
3460                 ret = flow_dv_validate_action_decap(dev, *action_flags, action,
3461                                                     item_flags, attr, error);
3462                 if (ret < 0)
3463                         return ret;
3464                 *action_flags |= MLX5_FLOW_ACTION_DECAP;
3465                 ++(*actions_n);
3466         }
3467         if (encap) {
3468                 if (encap->size <= MLX5_ENCAPSULATION_DECISION_SIZE)
3469                         return rte_flow_error_set(error, ENOTSUP,
3470                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3471                                                   NULL,
3472                                                   "small raw encap size");
3473                 if (*action_flags & MLX5_FLOW_ACTION_ENCAP)
3474                         return rte_flow_error_set(error, EINVAL,
3475                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3476                                                   NULL,
3477                                                   "more than one encap action");
3478                 if (!attr->transfer && priv->representor)
3479                         return rte_flow_error_set
3480                                         (error, ENOTSUP,
3481                                          RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3482                                          "encap action for VF representor "
3483                                          "not supported on NIC table");
3484                 *action_flags |= MLX5_FLOW_ACTION_ENCAP;
3485                 ++(*actions_n);
3486         }
3487         return 0;
3488 }
3489
3490 /*
3491  * Validate the ASO CT action.
3492  *
3493  * @param[in] dev
3494  *   Pointer to the rte_eth_dev structure.
3495  * @param[in] action_flags
3496  *   Holds the actions detected until now.
3497  * @param[in] item_flags
3498  *   The items found in this flow rule.
3499  * @param[in] attr
3500  *   Pointer to flow attributes.
3501  * @param[out] error
3502  *   Pointer to error structure.
3503  *
3504  * @return
3505  *   0 on success, a negative errno value otherwise and rte_errno is set.
3506  */
3507 static int
3508 flow_dv_validate_action_aso_ct(struct rte_eth_dev *dev,
3509                                uint64_t action_flags,
3510                                uint64_t item_flags,
3511                                const struct rte_flow_attr *attr,
3512                                struct rte_flow_error *error)
3513 {
3514         RTE_SET_USED(dev);
3515
3516         if (attr->group == 0 && !attr->transfer)
3517                 return rte_flow_error_set(error, ENOTSUP,
3518                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3519                                           NULL,
3520                                           "Only support non-root table");
3521         if (action_flags & MLX5_FLOW_FATE_ACTIONS)
3522                 return rte_flow_error_set(error, ENOTSUP,
3523                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3524                                           "CT cannot follow a fate action");
3525         if ((action_flags & MLX5_FLOW_ACTION_METER) ||
3526             (action_flags & MLX5_FLOW_ACTION_AGE))
3527                 return rte_flow_error_set(error, EINVAL,
3528                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3529                                           "Only one ASO action is supported");
3530         if (action_flags & MLX5_FLOW_ACTION_ENCAP)
3531                 return rte_flow_error_set(error, EINVAL,
3532                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3533                                           "Encap cannot exist before CT");
3534         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_TCP))
3535                 return rte_flow_error_set(error, EINVAL,
3536                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3537                                           "Not a outer TCP packet");
3538         return 0;
3539 }
3540
3541 /**
3542  * Match encap_decap resource.
3543  *
3544  * @param list
3545  *   Pointer to the hash list.
3546  * @param entry
3547  *   Pointer to exist resource entry object.
3548  * @param key
3549  *   Key of the new entry.
3550  * @param ctx_cb
3551  *   Pointer to new encap_decap resource.
3552  *
3553  * @return
3554  *   0 on matching, none-zero otherwise.
3555  */
3556 int
3557 flow_dv_encap_decap_match_cb(struct mlx5_hlist *list __rte_unused,
3558                              struct mlx5_hlist_entry *entry,
3559                              uint64_t key __rte_unused, void *cb_ctx)
3560 {
3561         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
3562         struct mlx5_flow_dv_encap_decap_resource *resource = ctx->data;
3563         struct mlx5_flow_dv_encap_decap_resource *cache_resource;
3564
3565         cache_resource = container_of(entry,
3566                                       struct mlx5_flow_dv_encap_decap_resource,
3567                                       entry);
3568         if (resource->reformat_type == cache_resource->reformat_type &&
3569             resource->ft_type == cache_resource->ft_type &&
3570             resource->flags == cache_resource->flags &&
3571             resource->size == cache_resource->size &&
3572             !memcmp((const void *)resource->buf,
3573                     (const void *)cache_resource->buf,
3574                     resource->size))
3575                 return 0;
3576         return -1;
3577 }
3578
3579 /**
3580  * Allocate encap_decap resource.
3581  *
3582  * @param list
3583  *   Pointer to the hash list.
3584  * @param entry
3585  *   Pointer to exist resource entry object.
3586  * @param ctx_cb
3587  *   Pointer to new encap_decap resource.
3588  *
3589  * @return
3590  *   0 on matching, none-zero otherwise.
3591  */
3592 struct mlx5_hlist_entry *
3593 flow_dv_encap_decap_create_cb(struct mlx5_hlist *list,
3594                               uint64_t key __rte_unused,
3595                               void *cb_ctx)
3596 {
3597         struct mlx5_dev_ctx_shared *sh = list->ctx;
3598         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
3599         struct mlx5dv_dr_domain *domain;
3600         struct mlx5_flow_dv_encap_decap_resource *resource = ctx->data;
3601         struct mlx5_flow_dv_encap_decap_resource *cache_resource;
3602         uint32_t idx;
3603         int ret;
3604
3605         if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
3606                 domain = sh->fdb_domain;
3607         else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_RX)
3608                 domain = sh->rx_domain;
3609         else
3610                 domain = sh->tx_domain;
3611         /* Register new encap/decap resource. */
3612         cache_resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_DECAP_ENCAP],
3613                                        &idx);
3614         if (!cache_resource) {
3615                 rte_flow_error_set(ctx->error, ENOMEM,
3616                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3617                                    "cannot allocate resource memory");
3618                 return NULL;
3619         }
3620         *cache_resource = *resource;
3621         cache_resource->idx = idx;
3622         ret = mlx5_flow_os_create_flow_action_packet_reformat
3623                                         (sh->ctx, domain, cache_resource,
3624                                          &cache_resource->action);
3625         if (ret) {
3626                 mlx5_ipool_free(sh->ipool[MLX5_IPOOL_DECAP_ENCAP], idx);
3627                 rte_flow_error_set(ctx->error, ENOMEM,
3628                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3629                                    NULL, "cannot create action");
3630                 return NULL;
3631         }
3632
3633         return &cache_resource->entry;
3634 }
3635
3636 /**
3637  * Find existing encap/decap resource or create and register a new one.
3638  *
3639  * @param[in, out] dev
3640  *   Pointer to rte_eth_dev structure.
3641  * @param[in, out] resource
3642  *   Pointer to encap/decap resource.
3643  * @parm[in, out] dev_flow
3644  *   Pointer to the dev_flow.
3645  * @param[out] error
3646  *   pointer to error structure.
3647  *
3648  * @return
3649  *   0 on success otherwise -errno and errno is set.
3650  */
3651 static int
3652 flow_dv_encap_decap_resource_register
3653                         (struct rte_eth_dev *dev,
3654                          struct mlx5_flow_dv_encap_decap_resource *resource,
3655                          struct mlx5_flow *dev_flow,
3656                          struct rte_flow_error *error)
3657 {
3658         struct mlx5_priv *priv = dev->data->dev_private;
3659         struct mlx5_dev_ctx_shared *sh = priv->sh;
3660         struct mlx5_hlist_entry *entry;
3661         union {
3662                 struct {
3663                         uint32_t ft_type:8;
3664                         uint32_t refmt_type:8;
3665                         /*
3666                          * Header reformat actions can be shared between
3667                          * non-root tables. One bit to indicate non-root
3668                          * table or not.
3669                          */
3670                         uint32_t is_root:1;
3671                         uint32_t reserve:15;
3672                 };
3673                 uint32_t v32;
3674         } encap_decap_key = {
3675                 {
3676                         .ft_type = resource->ft_type,
3677                         .refmt_type = resource->reformat_type,
3678                         .is_root = !!dev_flow->dv.group,
3679                         .reserve = 0,
3680                 }
3681         };
3682         struct mlx5_flow_cb_ctx ctx = {
3683                 .error = error,
3684                 .data = resource,
3685         };
3686         uint64_t key64;
3687
3688         resource->flags = dev_flow->dv.group ? 0 : 1;
3689         key64 =  __rte_raw_cksum(&encap_decap_key.v32,
3690                                  sizeof(encap_decap_key.v32), 0);
3691         if (resource->reformat_type !=
3692             MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TUNNEL_TO_L2 &&
3693             resource->size)
3694                 key64 = __rte_raw_cksum(resource->buf, resource->size, key64);
3695         entry = mlx5_hlist_register(sh->encaps_decaps, key64, &ctx);
3696         if (!entry)
3697                 return -rte_errno;
3698         resource = container_of(entry, typeof(*resource), entry);
3699         dev_flow->dv.encap_decap = resource;
3700         dev_flow->handle->dvh.rix_encap_decap = resource->idx;
3701         return 0;
3702 }
3703
3704 /**
3705  * Find existing table jump resource or create and register a new one.
3706  *
3707  * @param[in, out] dev
3708  *   Pointer to rte_eth_dev structure.
3709  * @param[in, out] tbl
3710  *   Pointer to flow table resource.
3711  * @parm[in, out] dev_flow
3712  *   Pointer to the dev_flow.
3713  * @param[out] error
3714  *   pointer to error structure.
3715  *
3716  * @return
3717  *   0 on success otherwise -errno and errno is set.
3718  */
3719 static int
3720 flow_dv_jump_tbl_resource_register
3721                         (struct rte_eth_dev *dev __rte_unused,
3722                          struct mlx5_flow_tbl_resource *tbl,
3723                          struct mlx5_flow *dev_flow,
3724                          struct rte_flow_error *error __rte_unused)
3725 {
3726         struct mlx5_flow_tbl_data_entry *tbl_data =
3727                 container_of(tbl, struct mlx5_flow_tbl_data_entry, tbl);
3728
3729         MLX5_ASSERT(tbl);
3730         MLX5_ASSERT(tbl_data->jump.action);
3731         dev_flow->handle->rix_jump = tbl_data->idx;
3732         dev_flow->dv.jump = &tbl_data->jump;
3733         return 0;
3734 }
3735
3736 int
3737 flow_dv_port_id_match_cb(struct mlx5_cache_list *list __rte_unused,
3738                          struct mlx5_cache_entry *entry, void *cb_ctx)
3739 {
3740         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
3741         struct mlx5_flow_dv_port_id_action_resource *ref = ctx->data;
3742         struct mlx5_flow_dv_port_id_action_resource *res =
3743                         container_of(entry, typeof(*res), entry);
3744
3745         return ref->port_id != res->port_id;
3746 }
3747
3748 struct mlx5_cache_entry *
3749 flow_dv_port_id_create_cb(struct mlx5_cache_list *list,
3750                           struct mlx5_cache_entry *entry __rte_unused,
3751                           void *cb_ctx)
3752 {
3753         struct mlx5_dev_ctx_shared *sh = list->ctx;
3754         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
3755         struct mlx5_flow_dv_port_id_action_resource *ref = ctx->data;
3756         struct mlx5_flow_dv_port_id_action_resource *cache;
3757         uint32_t idx;
3758         int ret;
3759
3760         /* Register new port id action resource. */
3761         cache = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_PORT_ID], &idx);
3762         if (!cache) {
3763                 rte_flow_error_set(ctx->error, ENOMEM,
3764                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3765                                    "cannot allocate port_id action cache memory");
3766                 return NULL;
3767         }
3768         *cache = *ref;
3769         ret = mlx5_flow_os_create_flow_action_dest_port(sh->fdb_domain,
3770                                                         ref->port_id,
3771                                                         &cache->action);
3772         if (ret) {
3773                 mlx5_ipool_free(sh->ipool[MLX5_IPOOL_PORT_ID], idx);
3774                 rte_flow_error_set(ctx->error, ENOMEM,
3775                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3776                                    "cannot create action");
3777                 return NULL;
3778         }
3779         cache->idx = idx;
3780         return &cache->entry;
3781 }
3782
3783 /**
3784  * Find existing table port ID resource or create and register a new one.
3785  *
3786  * @param[in, out] dev
3787  *   Pointer to rte_eth_dev structure.
3788  * @param[in, out] resource
3789  *   Pointer to port ID action resource.
3790  * @parm[in, out] dev_flow
3791  *   Pointer to the dev_flow.
3792  * @param[out] error
3793  *   pointer to error structure.
3794  *
3795  * @return
3796  *   0 on success otherwise -errno and errno is set.
3797  */
3798 static int
3799 flow_dv_port_id_action_resource_register
3800                         (struct rte_eth_dev *dev,
3801                          struct mlx5_flow_dv_port_id_action_resource *resource,
3802                          struct mlx5_flow *dev_flow,
3803                          struct rte_flow_error *error)
3804 {
3805         struct mlx5_priv *priv = dev->data->dev_private;
3806         struct mlx5_cache_entry *entry;
3807         struct mlx5_flow_dv_port_id_action_resource *cache;
3808         struct mlx5_flow_cb_ctx ctx = {
3809                 .error = error,
3810                 .data = resource,
3811         };
3812
3813         entry = mlx5_cache_register(&priv->sh->port_id_action_list, &ctx);
3814         if (!entry)
3815                 return -rte_errno;
3816         cache = container_of(entry, typeof(*cache), entry);
3817         dev_flow->dv.port_id_action = cache;
3818         dev_flow->handle->rix_port_id_action = cache->idx;
3819         return 0;
3820 }
3821
3822 int
3823 flow_dv_push_vlan_match_cb(struct mlx5_cache_list *list __rte_unused,
3824                          struct mlx5_cache_entry *entry, void *cb_ctx)
3825 {
3826         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
3827         struct mlx5_flow_dv_push_vlan_action_resource *ref = ctx->data;
3828         struct mlx5_flow_dv_push_vlan_action_resource *res =
3829                         container_of(entry, typeof(*res), entry);
3830
3831         return ref->vlan_tag != res->vlan_tag || ref->ft_type != res->ft_type;
3832 }
3833
3834 struct mlx5_cache_entry *
3835 flow_dv_push_vlan_create_cb(struct mlx5_cache_list *list,
3836                           struct mlx5_cache_entry *entry __rte_unused,
3837                           void *cb_ctx)
3838 {
3839         struct mlx5_dev_ctx_shared *sh = list->ctx;
3840         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
3841         struct mlx5_flow_dv_push_vlan_action_resource *ref = ctx->data;
3842         struct mlx5_flow_dv_push_vlan_action_resource *cache;
3843         struct mlx5dv_dr_domain *domain;
3844         uint32_t idx;
3845         int ret;
3846
3847         /* Register new port id action resource. */
3848         cache = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_PUSH_VLAN], &idx);
3849         if (!cache) {
3850                 rte_flow_error_set(ctx->error, ENOMEM,
3851                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3852                                    "cannot allocate push_vlan action cache memory");
3853                 return NULL;
3854         }
3855         *cache = *ref;
3856         if (ref->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
3857                 domain = sh->fdb_domain;
3858         else if (ref->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_RX)
3859                 domain = sh->rx_domain;
3860         else
3861                 domain = sh->tx_domain;
3862         ret = mlx5_flow_os_create_flow_action_push_vlan(domain, ref->vlan_tag,
3863                                                         &cache->action);
3864         if (ret) {
3865                 mlx5_ipool_free(sh->ipool[MLX5_IPOOL_PUSH_VLAN], idx);
3866                 rte_flow_error_set(ctx->error, ENOMEM,
3867                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3868                                    "cannot create push vlan action");
3869                 return NULL;
3870         }
3871         cache->idx = idx;
3872         return &cache->entry;
3873 }
3874
3875 /**
3876  * Find existing push vlan resource or create and register a new one.
3877  *
3878  * @param [in, out] dev
3879  *   Pointer to rte_eth_dev structure.
3880  * @param[in, out] resource
3881  *   Pointer to port ID action resource.
3882  * @parm[in, out] dev_flow
3883  *   Pointer to the dev_flow.
3884  * @param[out] error
3885  *   pointer to error structure.
3886  *
3887  * @return
3888  *   0 on success otherwise -errno and errno is set.
3889  */
3890 static int
3891 flow_dv_push_vlan_action_resource_register
3892                        (struct rte_eth_dev *dev,
3893                         struct mlx5_flow_dv_push_vlan_action_resource *resource,
3894                         struct mlx5_flow *dev_flow,
3895                         struct rte_flow_error *error)
3896 {
3897         struct mlx5_priv *priv = dev->data->dev_private;
3898         struct mlx5_flow_dv_push_vlan_action_resource *cache;
3899         struct mlx5_cache_entry *entry;
3900         struct mlx5_flow_cb_ctx ctx = {
3901                 .error = error,
3902                 .data = resource,
3903         };
3904
3905         entry = mlx5_cache_register(&priv->sh->push_vlan_action_list, &ctx);
3906         if (!entry)
3907                 return -rte_errno;
3908         cache = container_of(entry, typeof(*cache), entry);
3909
3910         dev_flow->handle->dvh.rix_push_vlan = cache->idx;
3911         dev_flow->dv.push_vlan_res = cache;
3912         return 0;
3913 }
3914
3915 /**
3916  * Get the size of specific rte_flow_item_type hdr size
3917  *
3918  * @param[in] item_type
3919  *   Tested rte_flow_item_type.
3920  *
3921  * @return
3922  *   sizeof struct item_type, 0 if void or irrelevant.
3923  */
3924 static size_t
3925 flow_dv_get_item_hdr_len(const enum rte_flow_item_type item_type)
3926 {
3927         size_t retval;
3928
3929         switch (item_type) {
3930         case RTE_FLOW_ITEM_TYPE_ETH:
3931                 retval = sizeof(struct rte_ether_hdr);
3932                 break;
3933         case RTE_FLOW_ITEM_TYPE_VLAN:
3934                 retval = sizeof(struct rte_vlan_hdr);
3935                 break;
3936         case RTE_FLOW_ITEM_TYPE_IPV4:
3937                 retval = sizeof(struct rte_ipv4_hdr);
3938                 break;
3939         case RTE_FLOW_ITEM_TYPE_IPV6:
3940                 retval = sizeof(struct rte_ipv6_hdr);
3941                 break;
3942         case RTE_FLOW_ITEM_TYPE_UDP:
3943                 retval = sizeof(struct rte_udp_hdr);
3944                 break;
3945         case RTE_FLOW_ITEM_TYPE_TCP:
3946                 retval = sizeof(struct rte_tcp_hdr);
3947                 break;
3948         case RTE_FLOW_ITEM_TYPE_VXLAN:
3949         case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
3950                 retval = sizeof(struct rte_vxlan_hdr);
3951                 break;
3952         case RTE_FLOW_ITEM_TYPE_GRE:
3953         case RTE_FLOW_ITEM_TYPE_NVGRE:
3954                 retval = sizeof(struct rte_gre_hdr);
3955                 break;
3956         case RTE_FLOW_ITEM_TYPE_MPLS:
3957                 retval = sizeof(struct rte_mpls_hdr);
3958                 break;
3959         case RTE_FLOW_ITEM_TYPE_VOID: /* Fall through. */
3960         default:
3961                 retval = 0;
3962                 break;
3963         }
3964         return retval;
3965 }
3966
3967 #define MLX5_ENCAP_IPV4_VERSION         0x40
3968 #define MLX5_ENCAP_IPV4_IHL_MIN         0x05
3969 #define MLX5_ENCAP_IPV4_TTL_DEF         0x40
3970 #define MLX5_ENCAP_IPV6_VTC_FLOW        0x60000000
3971 #define MLX5_ENCAP_IPV6_HOP_LIMIT       0xff
3972 #define MLX5_ENCAP_VXLAN_FLAGS          0x08000000
3973 #define MLX5_ENCAP_VXLAN_GPE_FLAGS      0x04
3974
3975 /**
3976  * Convert the encap action data from list of rte_flow_item to raw buffer
3977  *
3978  * @param[in] items
3979  *   Pointer to rte_flow_item objects list.
3980  * @param[out] buf
3981  *   Pointer to the output buffer.
3982  * @param[out] size
3983  *   Pointer to the output buffer size.
3984  * @param[out] error
3985  *   Pointer to the error structure.
3986  *
3987  * @return
3988  *   0 on success, a negative errno value otherwise and rte_errno is set.
3989  */
3990 static int
3991 flow_dv_convert_encap_data(const struct rte_flow_item *items, uint8_t *buf,
3992                            size_t *size, struct rte_flow_error *error)
3993 {
3994         struct rte_ether_hdr *eth = NULL;
3995         struct rte_vlan_hdr *vlan = NULL;
3996         struct rte_ipv4_hdr *ipv4 = NULL;
3997         struct rte_ipv6_hdr *ipv6 = NULL;
3998         struct rte_udp_hdr *udp = NULL;
3999         struct rte_vxlan_hdr *vxlan = NULL;
4000         struct rte_vxlan_gpe_hdr *vxlan_gpe = NULL;
4001         struct rte_gre_hdr *gre = NULL;
4002         size_t len;
4003         size_t temp_size = 0;
4004
4005         if (!items)
4006                 return rte_flow_error_set(error, EINVAL,
4007                                           RTE_FLOW_ERROR_TYPE_ACTION,
4008                                           NULL, "invalid empty data");
4009         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
4010                 len = flow_dv_get_item_hdr_len(items->type);
4011                 if (len + temp_size > MLX5_ENCAP_MAX_LEN)
4012                         return rte_flow_error_set(error, EINVAL,
4013                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4014                                                   (void *)items->type,
4015                                                   "items total size is too big"
4016                                                   " for encap action");
4017                 rte_memcpy((void *)&buf[temp_size], items->spec, len);
4018                 switch (items->type) {
4019                 case RTE_FLOW_ITEM_TYPE_ETH:
4020                         eth = (struct rte_ether_hdr *)&buf[temp_size];
4021                         break;
4022                 case RTE_FLOW_ITEM_TYPE_VLAN:
4023                         vlan = (struct rte_vlan_hdr *)&buf[temp_size];
4024                         if (!eth)
4025                                 return rte_flow_error_set(error, EINVAL,
4026                                                 RTE_FLOW_ERROR_TYPE_ACTION,
4027                                                 (void *)items->type,
4028                                                 "eth header not found");
4029                         if (!eth->ether_type)
4030                                 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_VLAN);
4031                         break;
4032                 case RTE_FLOW_ITEM_TYPE_IPV4:
4033                         ipv4 = (struct rte_ipv4_hdr *)&buf[temp_size];
4034                         if (!vlan && !eth)
4035                                 return rte_flow_error_set(error, EINVAL,
4036                                                 RTE_FLOW_ERROR_TYPE_ACTION,
4037                                                 (void *)items->type,
4038                                                 "neither eth nor vlan"
4039                                                 " header found");
4040                         if (vlan && !vlan->eth_proto)
4041                                 vlan->eth_proto = RTE_BE16(RTE_ETHER_TYPE_IPV4);
4042                         else if (eth && !eth->ether_type)
4043                                 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_IPV4);
4044                         if (!ipv4->version_ihl)
4045                                 ipv4->version_ihl = MLX5_ENCAP_IPV4_VERSION |
4046                                                     MLX5_ENCAP_IPV4_IHL_MIN;
4047                         if (!ipv4->time_to_live)
4048                                 ipv4->time_to_live = MLX5_ENCAP_IPV4_TTL_DEF;
4049                         break;
4050                 case RTE_FLOW_ITEM_TYPE_IPV6:
4051                         ipv6 = (struct rte_ipv6_hdr *)&buf[temp_size];
4052                         if (!vlan && !eth)
4053                                 return rte_flow_error_set(error, EINVAL,
4054                                                 RTE_FLOW_ERROR_TYPE_ACTION,
4055                                                 (void *)items->type,
4056                                                 "neither eth nor vlan"
4057                                                 " header found");
4058                         if (vlan && !vlan->eth_proto)
4059                                 vlan->eth_proto = RTE_BE16(RTE_ETHER_TYPE_IPV6);
4060                         else if (eth && !eth->ether_type)
4061                                 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_IPV6);
4062                         if (!ipv6->vtc_flow)
4063                                 ipv6->vtc_flow =
4064                                         RTE_BE32(MLX5_ENCAP_IPV6_VTC_FLOW);
4065                         if (!ipv6->hop_limits)
4066                                 ipv6->hop_limits = MLX5_ENCAP_IPV6_HOP_LIMIT;
4067                         break;
4068                 case RTE_FLOW_ITEM_TYPE_UDP:
4069                         udp = (struct rte_udp_hdr *)&buf[temp_size];
4070                         if (!ipv4 && !ipv6)
4071                                 return rte_flow_error_set(error, EINVAL,
4072                                                 RTE_FLOW_ERROR_TYPE_ACTION,
4073                                                 (void *)items->type,
4074                                                 "ip header not found");
4075                         if (ipv4 && !ipv4->next_proto_id)
4076                                 ipv4->next_proto_id = IPPROTO_UDP;
4077                         else if (ipv6 && !ipv6->proto)
4078                                 ipv6->proto = IPPROTO_UDP;
4079                         break;
4080                 case RTE_FLOW_ITEM_TYPE_VXLAN:
4081                         vxlan = (struct rte_vxlan_hdr *)&buf[temp_size];
4082                         if (!udp)
4083                                 return rte_flow_error_set(error, EINVAL,
4084                                                 RTE_FLOW_ERROR_TYPE_ACTION,
4085                                                 (void *)items->type,
4086                                                 "udp header not found");
4087                         if (!udp->dst_port)
4088                                 udp->dst_port = RTE_BE16(MLX5_UDP_PORT_VXLAN);
4089                         if (!vxlan->vx_flags)
4090                                 vxlan->vx_flags =
4091                                         RTE_BE32(MLX5_ENCAP_VXLAN_FLAGS);
4092                         break;
4093                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
4094                         vxlan_gpe = (struct rte_vxlan_gpe_hdr *)&buf[temp_size];
4095                         if (!udp)
4096                                 return rte_flow_error_set(error, EINVAL,
4097                                                 RTE_FLOW_ERROR_TYPE_ACTION,
4098                                                 (void *)items->type,
4099                                                 "udp header not found");
4100                         if (!vxlan_gpe->proto)
4101                                 return rte_flow_error_set(error, EINVAL,
4102                                                 RTE_FLOW_ERROR_TYPE_ACTION,
4103                                                 (void *)items->type,
4104                                                 "next protocol not found");
4105                         if (!udp->dst_port)
4106                                 udp->dst_port =
4107                                         RTE_BE16(MLX5_UDP_PORT_VXLAN_GPE);
4108                         if (!vxlan_gpe->vx_flags)
4109                                 vxlan_gpe->vx_flags =
4110                                                 MLX5_ENCAP_VXLAN_GPE_FLAGS;
4111                         break;
4112                 case RTE_FLOW_ITEM_TYPE_GRE:
4113                 case RTE_FLOW_ITEM_TYPE_NVGRE:
4114                         gre = (struct rte_gre_hdr *)&buf[temp_size];
4115                         if (!gre->proto)
4116                                 return rte_flow_error_set(error, EINVAL,
4117                                                 RTE_FLOW_ERROR_TYPE_ACTION,
4118                                                 (void *)items->type,
4119                                                 "next protocol not found");
4120                         if (!ipv4 && !ipv6)
4121                                 return rte_flow_error_set(error, EINVAL,
4122                                                 RTE_FLOW_ERROR_TYPE_ACTION,
4123                                                 (void *)items->type,
4124                                                 "ip header not found");
4125                         if (ipv4 && !ipv4->next_proto_id)
4126                                 ipv4->next_proto_id = IPPROTO_GRE;
4127                         else if (ipv6 && !ipv6->proto)
4128                                 ipv6->proto = IPPROTO_GRE;
4129                         break;
4130                 case RTE_FLOW_ITEM_TYPE_VOID:
4131                         break;
4132                 default:
4133                         return rte_flow_error_set(error, EINVAL,
4134                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4135                                                   (void *)items->type,
4136                                                   "unsupported item type");
4137                         break;
4138                 }
4139                 temp_size += len;
4140         }
4141         *size = temp_size;
4142         return 0;
4143 }
4144
4145 static int
4146 flow_dv_zero_encap_udp_csum(void *data, struct rte_flow_error *error)
4147 {
4148         struct rte_ether_hdr *eth = NULL;
4149         struct rte_vlan_hdr *vlan = NULL;
4150         struct rte_ipv6_hdr *ipv6 = NULL;
4151         struct rte_udp_hdr *udp = NULL;
4152         char *next_hdr;
4153         uint16_t proto;
4154
4155         eth = (struct rte_ether_hdr *)data;
4156         next_hdr = (char *)(eth + 1);
4157         proto = RTE_BE16(eth->ether_type);
4158
4159         /* VLAN skipping */
4160         while (proto == RTE_ETHER_TYPE_VLAN || proto == RTE_ETHER_TYPE_QINQ) {
4161                 vlan = (struct rte_vlan_hdr *)next_hdr;
4162                 proto = RTE_BE16(vlan->eth_proto);
4163                 next_hdr += sizeof(struct rte_vlan_hdr);
4164         }
4165
4166         /* HW calculates IPv4 csum. no need to proceed */
4167         if (proto == RTE_ETHER_TYPE_IPV4)
4168                 return 0;
4169
4170         /* non IPv4/IPv6 header. not supported */
4171         if (proto != RTE_ETHER_TYPE_IPV6) {
4172                 return rte_flow_error_set(error, ENOTSUP,
4173                                           RTE_FLOW_ERROR_TYPE_ACTION,
4174                                           NULL, "Cannot offload non IPv4/IPv6");
4175         }
4176
4177         ipv6 = (struct rte_ipv6_hdr *)next_hdr;
4178
4179         /* ignore non UDP */
4180         if (ipv6->proto != IPPROTO_UDP)
4181                 return 0;
4182
4183         udp = (struct rte_udp_hdr *)(ipv6 + 1);
4184         udp->dgram_cksum = 0;
4185
4186         return 0;
4187 }
4188
4189 /**
4190  * Convert L2 encap action to DV specification.
4191  *
4192  * @param[in] dev
4193  *   Pointer to rte_eth_dev structure.
4194  * @param[in] action
4195  *   Pointer to action structure.
4196  * @param[in, out] dev_flow
4197  *   Pointer to the mlx5_flow.
4198  * @param[in] transfer
4199  *   Mark if the flow is E-Switch flow.
4200  * @param[out] error
4201  *   Pointer to the error structure.
4202  *
4203  * @return
4204  *   0 on success, a negative errno value otherwise and rte_errno is set.
4205  */
4206 static int
4207 flow_dv_create_action_l2_encap(struct rte_eth_dev *dev,
4208                                const struct rte_flow_action *action,
4209                                struct mlx5_flow *dev_flow,
4210                                uint8_t transfer,
4211                                struct rte_flow_error *error)
4212 {
4213         const struct rte_flow_item *encap_data;
4214         const struct rte_flow_action_raw_encap *raw_encap_data;
4215         struct mlx5_flow_dv_encap_decap_resource res = {
4216                 .reformat_type =
4217                         MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L2_TUNNEL,
4218                 .ft_type = transfer ? MLX5DV_FLOW_TABLE_TYPE_FDB :
4219                                       MLX5DV_FLOW_TABLE_TYPE_NIC_TX,
4220         };
4221
4222         if (action->type == RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
4223                 raw_encap_data =
4224                         (const struct rte_flow_action_raw_encap *)action->conf;
4225                 res.size = raw_encap_data->size;
4226                 memcpy(res.buf, raw_encap_data->data, res.size);
4227         } else {
4228                 if (action->type == RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP)
4229                         encap_data =
4230                                 ((const struct rte_flow_action_vxlan_encap *)
4231                                                 action->conf)->definition;
4232                 else
4233                         encap_data =
4234                                 ((const struct rte_flow_action_nvgre_encap *)
4235                                                 action->conf)->definition;
4236                 if (flow_dv_convert_encap_data(encap_data, res.buf,
4237                                                &res.size, error))
4238                         return -rte_errno;
4239         }
4240         if (flow_dv_zero_encap_udp_csum(res.buf, error))
4241                 return -rte_errno;
4242         if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
4243                 return rte_flow_error_set(error, EINVAL,
4244                                           RTE_FLOW_ERROR_TYPE_ACTION,
4245                                           NULL, "can't create L2 encap action");
4246         return 0;
4247 }
4248
4249 /**
4250  * Convert L2 decap action to DV specification.
4251  *
4252  * @param[in] dev
4253  *   Pointer to rte_eth_dev structure.
4254  * @param[in, out] dev_flow
4255  *   Pointer to the mlx5_flow.
4256  * @param[in] transfer
4257  *   Mark if the flow is E-Switch flow.
4258  * @param[out] error
4259  *   Pointer to the error structure.
4260  *
4261  * @return
4262  *   0 on success, a negative errno value otherwise and rte_errno is set.
4263  */
4264 static int
4265 flow_dv_create_action_l2_decap(struct rte_eth_dev *dev,
4266                                struct mlx5_flow *dev_flow,
4267                                uint8_t transfer,
4268                                struct rte_flow_error *error)
4269 {
4270         struct mlx5_flow_dv_encap_decap_resource res = {
4271                 .size = 0,
4272                 .reformat_type =
4273                         MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TUNNEL_TO_L2,
4274                 .ft_type = transfer ? MLX5DV_FLOW_TABLE_TYPE_FDB :
4275                                       MLX5DV_FLOW_TABLE_TYPE_NIC_RX,
4276         };
4277
4278         if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
4279                 return rte_flow_error_set(error, EINVAL,
4280                                           RTE_FLOW_ERROR_TYPE_ACTION,
4281                                           NULL, "can't create L2 decap action");
4282         return 0;
4283 }
4284
4285 /**
4286  * Convert raw decap/encap (L3 tunnel) action to DV specification.
4287  *
4288  * @param[in] dev
4289  *   Pointer to rte_eth_dev structure.
4290  * @param[in] action
4291  *   Pointer to action structure.
4292  * @param[in, out] dev_flow
4293  *   Pointer to the mlx5_flow.
4294  * @param[in] attr
4295  *   Pointer to the flow attributes.
4296  * @param[out] error
4297  *   Pointer to the error structure.
4298  *
4299  * @return
4300  *   0 on success, a negative errno value otherwise and rte_errno is set.
4301  */
4302 static int
4303 flow_dv_create_action_raw_encap(struct rte_eth_dev *dev,
4304                                 const struct rte_flow_action *action,
4305                                 struct mlx5_flow *dev_flow,
4306                                 const struct rte_flow_attr *attr,
4307                                 struct rte_flow_error *error)
4308 {
4309         const struct rte_flow_action_raw_encap *encap_data;
4310         struct mlx5_flow_dv_encap_decap_resource res;
4311
4312         memset(&res, 0, sizeof(res));
4313         encap_data = (const struct rte_flow_action_raw_encap *)action->conf;
4314         res.size = encap_data->size;
4315         memcpy(res.buf, encap_data->data, res.size);
4316         res.reformat_type = res.size < MLX5_ENCAPSULATION_DECISION_SIZE ?
4317                 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L3_TUNNEL_TO_L2 :
4318                 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L3_TUNNEL;
4319         if (attr->transfer)
4320                 res.ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
4321         else
4322                 res.ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
4323                                              MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
4324         if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
4325                 return rte_flow_error_set(error, EINVAL,
4326                                           RTE_FLOW_ERROR_TYPE_ACTION,
4327                                           NULL, "can't create encap action");
4328         return 0;
4329 }
4330
4331 /**
4332  * Create action push VLAN.
4333  *
4334  * @param[in] dev
4335  *   Pointer to rte_eth_dev structure.
4336  * @param[in] attr
4337  *   Pointer to the flow attributes.
4338  * @param[in] vlan
4339  *   Pointer to the vlan to push to the Ethernet header.
4340  * @param[in, out] dev_flow
4341  *   Pointer to the mlx5_flow.
4342  * @param[out] error
4343  *   Pointer to the error structure.
4344  *
4345  * @return
4346  *   0 on success, a negative errno value otherwise and rte_errno is set.
4347  */
4348 static int
4349 flow_dv_create_action_push_vlan(struct rte_eth_dev *dev,
4350                                 const struct rte_flow_attr *attr,
4351                                 const struct rte_vlan_hdr *vlan,
4352                                 struct mlx5_flow *dev_flow,
4353                                 struct rte_flow_error *error)
4354 {
4355         struct mlx5_flow_dv_push_vlan_action_resource res;
4356
4357         memset(&res, 0, sizeof(res));
4358         res.vlan_tag =
4359                 rte_cpu_to_be_32(((uint32_t)vlan->eth_proto) << 16 |
4360                                  vlan->vlan_tci);
4361         if (attr->transfer)
4362                 res.ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
4363         else
4364                 res.ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
4365                                              MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
4366         return flow_dv_push_vlan_action_resource_register
4367                                             (dev, &res, dev_flow, error);
4368 }
4369
4370 /**
4371  * Validate the modify-header actions.
4372  *
4373  * @param[in] action_flags
4374  *   Holds the actions detected until now.
4375  * @param[in] action
4376  *   Pointer to the modify action.
4377  * @param[out] error
4378  *   Pointer to error structure.
4379  *
4380  * @return
4381  *   0 on success, a negative errno value otherwise and rte_errno is set.
4382  */
4383 static int
4384 flow_dv_validate_action_modify_hdr(const uint64_t action_flags,
4385                                    const struct rte_flow_action *action,
4386                                    struct rte_flow_error *error)
4387 {
4388         if (action->type != RTE_FLOW_ACTION_TYPE_DEC_TTL && !action->conf)
4389                 return rte_flow_error_set(error, EINVAL,
4390                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
4391                                           NULL, "action configuration not set");
4392         if (action_flags & MLX5_FLOW_ACTION_ENCAP)
4393                 return rte_flow_error_set(error, EINVAL,
4394                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
4395                                           "can't have encap action before"
4396                                           " modify action");
4397         return 0;
4398 }
4399
4400 /**
4401  * Validate the modify-header MAC address actions.
4402  *
4403  * @param[in] action_flags
4404  *   Holds the actions detected until now.
4405  * @param[in] action
4406  *   Pointer to the modify action.
4407  * @param[in] item_flags
4408  *   Holds the items detected.
4409  * @param[out] error
4410  *   Pointer to error structure.
4411  *
4412  * @return
4413  *   0 on success, a negative errno value otherwise and rte_errno is set.
4414  */
4415 static int
4416 flow_dv_validate_action_modify_mac(const uint64_t action_flags,
4417                                    const struct rte_flow_action *action,
4418                                    const uint64_t item_flags,
4419                                    struct rte_flow_error *error)
4420 {
4421         int ret = 0;
4422
4423         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
4424         if (!ret) {
4425                 if (!(item_flags & MLX5_FLOW_LAYER_L2))
4426                         return rte_flow_error_set(error, EINVAL,
4427                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4428                                                   NULL,
4429                                                   "no L2 item in pattern");
4430         }
4431         return ret;
4432 }
4433
4434 /**
4435  * Validate the modify-header IPv4 address actions.
4436  *
4437  * @param[in] action_flags
4438  *   Holds the actions detected until now.
4439  * @param[in] action
4440  *   Pointer to the modify action.
4441  * @param[in] item_flags
4442  *   Holds the items detected.
4443  * @param[out] error
4444  *   Pointer to error structure.
4445  *
4446  * @return
4447  *   0 on success, a negative errno value otherwise and rte_errno is set.
4448  */
4449 static int
4450 flow_dv_validate_action_modify_ipv4(const uint64_t action_flags,
4451                                     const struct rte_flow_action *action,
4452                                     const uint64_t item_flags,
4453                                     struct rte_flow_error *error)
4454 {
4455         int ret = 0;
4456         uint64_t layer;
4457
4458         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
4459         if (!ret) {
4460                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
4461                                  MLX5_FLOW_LAYER_INNER_L3_IPV4 :
4462                                  MLX5_FLOW_LAYER_OUTER_L3_IPV4;
4463                 if (!(item_flags & layer))
4464                         return rte_flow_error_set(error, EINVAL,
4465                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4466                                                   NULL,
4467                                                   "no ipv4 item in pattern");
4468         }
4469         return ret;
4470 }
4471
4472 /**
4473  * Validate the modify-header IPv6 address actions.
4474  *
4475  * @param[in] action_flags
4476  *   Holds the actions detected until now.
4477  * @param[in] action
4478  *   Pointer to the modify action.
4479  * @param[in] item_flags
4480  *   Holds the items detected.
4481  * @param[out] error
4482  *   Pointer to error structure.
4483  *
4484  * @return
4485  *   0 on success, a negative errno value otherwise and rte_errno is set.
4486  */
4487 static int
4488 flow_dv_validate_action_modify_ipv6(const uint64_t action_flags,
4489                                     const struct rte_flow_action *action,
4490                                     const uint64_t item_flags,
4491                                     struct rte_flow_error *error)
4492 {
4493         int ret = 0;
4494         uint64_t layer;
4495
4496         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
4497         if (!ret) {
4498                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
4499                                  MLX5_FLOW_LAYER_INNER_L3_IPV6 :
4500                                  MLX5_FLOW_LAYER_OUTER_L3_IPV6;
4501                 if (!(item_flags & layer))
4502                         return rte_flow_error_set(error, EINVAL,
4503                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4504                                                   NULL,
4505                                                   "no ipv6 item in pattern");
4506         }
4507         return ret;
4508 }
4509
4510 /**
4511  * Validate the modify-header TP actions.
4512  *
4513  * @param[in] action_flags
4514  *   Holds the actions detected until now.
4515  * @param[in] action
4516  *   Pointer to the modify action.
4517  * @param[in] item_flags
4518  *   Holds the items detected.
4519  * @param[out] error
4520  *   Pointer to error structure.
4521  *
4522  * @return
4523  *   0 on success, a negative errno value otherwise and rte_errno is set.
4524  */
4525 static int
4526 flow_dv_validate_action_modify_tp(const uint64_t action_flags,
4527                                   const struct rte_flow_action *action,
4528                                   const uint64_t item_flags,
4529                                   struct rte_flow_error *error)
4530 {
4531         int ret = 0;
4532         uint64_t layer;
4533
4534         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
4535         if (!ret) {
4536                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
4537                                  MLX5_FLOW_LAYER_INNER_L4 :
4538                                  MLX5_FLOW_LAYER_OUTER_L4;
4539                 if (!(item_flags & layer))
4540                         return rte_flow_error_set(error, EINVAL,
4541                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4542                                                   NULL, "no transport layer "
4543                                                   "in pattern");
4544         }
4545         return ret;
4546 }
4547
4548 /**
4549  * Validate the modify-header actions of increment/decrement
4550  * TCP Sequence-number.
4551  *
4552  * @param[in] action_flags
4553  *   Holds the actions detected until now.
4554  * @param[in] action
4555  *   Pointer to the modify action.
4556  * @param[in] item_flags
4557  *   Holds the items detected.
4558  * @param[out] error
4559  *   Pointer to error structure.
4560  *
4561  * @return
4562  *   0 on success, a negative errno value otherwise and rte_errno is set.
4563  */
4564 static int
4565 flow_dv_validate_action_modify_tcp_seq(const uint64_t action_flags,
4566                                        const struct rte_flow_action *action,
4567                                        const uint64_t item_flags,
4568                                        struct rte_flow_error *error)
4569 {
4570         int ret = 0;
4571         uint64_t layer;
4572
4573         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
4574         if (!ret) {
4575                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
4576                                  MLX5_FLOW_LAYER_INNER_L4_TCP :
4577                                  MLX5_FLOW_LAYER_OUTER_L4_TCP;
4578                 if (!(item_flags & layer))
4579                         return rte_flow_error_set(error, EINVAL,
4580                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4581                                                   NULL, "no TCP item in"
4582                                                   " pattern");
4583                 if ((action->type == RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ &&
4584                         (action_flags & MLX5_FLOW_ACTION_DEC_TCP_SEQ)) ||
4585                     (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ &&
4586                         (action_flags & MLX5_FLOW_ACTION_INC_TCP_SEQ)))
4587                         return rte_flow_error_set(error, EINVAL,
4588                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4589                                                   NULL,
4590                                                   "cannot decrease and increase"
4591                                                   " TCP sequence number"
4592                                                   " at the same time");
4593         }
4594         return ret;
4595 }
4596
4597 /**
4598  * Validate the modify-header actions of increment/decrement
4599  * TCP Acknowledgment number.
4600  *
4601  * @param[in] action_flags
4602  *   Holds the actions detected until now.
4603  * @param[in] action
4604  *   Pointer to the modify action.
4605  * @param[in] item_flags
4606  *   Holds the items detected.
4607  * @param[out] error
4608  *   Pointer to error structure.
4609  *
4610  * @return
4611  *   0 on success, a negative errno value otherwise and rte_errno is set.
4612  */
4613 static int
4614 flow_dv_validate_action_modify_tcp_ack(const uint64_t action_flags,
4615                                        const struct rte_flow_action *action,
4616                                        const uint64_t item_flags,
4617                                        struct rte_flow_error *error)
4618 {
4619         int ret = 0;
4620         uint64_t layer;
4621
4622         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
4623         if (!ret) {
4624                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
4625                                  MLX5_FLOW_LAYER_INNER_L4_TCP :
4626                                  MLX5_FLOW_LAYER_OUTER_L4_TCP;
4627                 if (!(item_flags & layer))
4628                         return rte_flow_error_set(error, EINVAL,
4629                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4630                                                   NULL, "no TCP item in"
4631                                                   " pattern");
4632                 if ((action->type == RTE_FLOW_ACTION_TYPE_INC_TCP_ACK &&
4633                         (action_flags & MLX5_FLOW_ACTION_DEC_TCP_ACK)) ||
4634                     (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK &&
4635                         (action_flags & MLX5_FLOW_ACTION_INC_TCP_ACK)))
4636                         return rte_flow_error_set(error, EINVAL,
4637                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4638                                                   NULL,
4639                                                   "cannot decrease and increase"
4640                                                   " TCP acknowledgment number"
4641                                                   " at the same time");
4642         }
4643         return ret;
4644 }
4645
4646 /**
4647  * Validate the modify-header TTL actions.
4648  *
4649  * @param[in] action_flags
4650  *   Holds the actions detected until now.
4651  * @param[in] action
4652  *   Pointer to the modify action.
4653  * @param[in] item_flags
4654  *   Holds the items detected.
4655  * @param[out] error
4656  *   Pointer to error structure.
4657  *
4658  * @return
4659  *   0 on success, a negative errno value otherwise and rte_errno is set.
4660  */
4661 static int
4662 flow_dv_validate_action_modify_ttl(const uint64_t action_flags,
4663                                    const struct rte_flow_action *action,
4664                                    const uint64_t item_flags,
4665                                    struct rte_flow_error *error)
4666 {
4667         int ret = 0;
4668         uint64_t layer;
4669
4670         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
4671         if (!ret) {
4672                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
4673                                  MLX5_FLOW_LAYER_INNER_L3 :
4674                                  MLX5_FLOW_LAYER_OUTER_L3;
4675                 if (!(item_flags & layer))
4676                         return rte_flow_error_set(error, EINVAL,
4677                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4678                                                   NULL,
4679                                                   "no IP protocol in pattern");
4680         }
4681         return ret;
4682 }
4683
4684 /**
4685  * Validate the generic modify field actions.
4686  * @param[in] dev
4687  *   Pointer to the rte_eth_dev structure.
4688  * @param[in] action_flags
4689  *   Holds the actions detected until now.
4690  * @param[in] action
4691  *   Pointer to the modify action.
4692  * @param[in] attr
4693  *   Pointer to the flow attributes.
4694  * @param[out] error
4695  *   Pointer to error structure.
4696  *
4697  * @return
4698  *   Number of header fields to modify (0 or more) on success,
4699  *   a negative errno value otherwise and rte_errno is set.
4700  */
4701 static int
4702 flow_dv_validate_action_modify_field(struct rte_eth_dev *dev,
4703                                    const uint64_t action_flags,
4704                                    const struct rte_flow_action *action,
4705                                    const struct rte_flow_attr *attr,
4706                                    struct rte_flow_error *error)
4707 {
4708         int ret = 0;
4709         struct mlx5_priv *priv = dev->data->dev_private;
4710         struct mlx5_dev_config *config = &priv->config;
4711         const struct rte_flow_action_modify_field *action_modify_field =
4712                 action->conf;
4713         uint32_t dst_width =
4714                 mlx5_flow_item_field_width(action_modify_field->dst.field);
4715         uint32_t src_width =
4716                 mlx5_flow_item_field_width(action_modify_field->src.field);
4717
4718         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
4719         if (ret)
4720                 return ret;
4721
4722         if (action_modify_field->width == 0)
4723                 return rte_flow_error_set(error, EINVAL,
4724                                 RTE_FLOW_ERROR_TYPE_ACTION, action,
4725                                 "no bits are requested to be modified");
4726         else if (action_modify_field->width > dst_width ||
4727                  action_modify_field->width > src_width)
4728                 return rte_flow_error_set(error, EINVAL,
4729                                 RTE_FLOW_ERROR_TYPE_ACTION, action,
4730                                 "cannot modify more bits than"
4731                                 " the width of a field");
4732         if (action_modify_field->dst.field != RTE_FLOW_FIELD_VALUE &&
4733             action_modify_field->dst.field != RTE_FLOW_FIELD_POINTER) {
4734                 if ((action_modify_field->dst.offset +
4735                      action_modify_field->width > dst_width) ||
4736                     (action_modify_field->dst.offset % 32))
4737                         return rte_flow_error_set(error, EINVAL,
4738                                         RTE_FLOW_ERROR_TYPE_ACTION, action,
4739                                         "destination offset is too big"
4740                                         " or not aligned to 4 bytes");
4741                 if (action_modify_field->dst.level &&
4742                     action_modify_field->dst.field != RTE_FLOW_FIELD_TAG)
4743                         return rte_flow_error_set(error, ENOTSUP,
4744                                         RTE_FLOW_ERROR_TYPE_ACTION, action,
4745                                         "inner header fields modification"
4746                                         " is not supported");
4747         }
4748         if (action_modify_field->src.field != RTE_FLOW_FIELD_VALUE &&
4749             action_modify_field->src.field != RTE_FLOW_FIELD_POINTER) {
4750                 if (!attr->transfer && !attr->group)
4751                         return rte_flow_error_set(error, ENOTSUP,
4752                                         RTE_FLOW_ERROR_TYPE_ACTION, action,
4753                                         "modify field action is not"
4754                                         " supported for group 0");
4755                 if ((action_modify_field->src.offset +
4756                      action_modify_field->width > src_width) ||
4757                     (action_modify_field->src.offset % 32))
4758                         return rte_flow_error_set(error, EINVAL,
4759                                         RTE_FLOW_ERROR_TYPE_ACTION, action,
4760                                         "source offset is too big"
4761                                         " or not aligned to 4 bytes");
4762                 if (action_modify_field->src.level &&
4763                     action_modify_field->src.field != RTE_FLOW_FIELD_TAG)
4764                         return rte_flow_error_set(error, ENOTSUP,
4765                                         RTE_FLOW_ERROR_TYPE_ACTION, action,
4766                                         "inner header fields modification"
4767                                         " is not supported");
4768         }
4769         if (action_modify_field->dst.field ==
4770             action_modify_field->src.field)
4771                 return rte_flow_error_set(error, EINVAL,
4772                                 RTE_FLOW_ERROR_TYPE_ACTION, action,
4773                                 "source and destination fields"
4774                                 " cannot be the same");
4775         if (action_modify_field->dst.field == RTE_FLOW_FIELD_VALUE ||
4776             action_modify_field->dst.field == RTE_FLOW_FIELD_POINTER)
4777                 return rte_flow_error_set(error, EINVAL,
4778                                 RTE_FLOW_ERROR_TYPE_ACTION, action,
4779                                 "immediate value or a pointer to it"
4780                                 " cannot be used as a destination");
4781         if (action_modify_field->dst.field == RTE_FLOW_FIELD_START ||
4782             action_modify_field->src.field == RTE_FLOW_FIELD_START)
4783                 return rte_flow_error_set(error, ENOTSUP,
4784                                 RTE_FLOW_ERROR_TYPE_ACTION, action,
4785                                 "modifications of an arbitrary"
4786                                 " place in a packet is not supported");
4787         if (action_modify_field->dst.field == RTE_FLOW_FIELD_VLAN_TYPE ||
4788             action_modify_field->src.field == RTE_FLOW_FIELD_VLAN_TYPE)
4789                 return rte_flow_error_set(error, ENOTSUP,
4790                                 RTE_FLOW_ERROR_TYPE_ACTION, action,
4791                                 "modifications of the 802.1Q Tag"
4792                                 " Identifier is not supported");
4793         if (action_modify_field->dst.field == RTE_FLOW_FIELD_VXLAN_VNI ||
4794             action_modify_field->src.field == RTE_FLOW_FIELD_VXLAN_VNI)
4795                 return rte_flow_error_set(error, ENOTSUP,
4796                                 RTE_FLOW_ERROR_TYPE_ACTION, action,
4797                                 "modifications of the VXLAN Network"
4798                                 " Identifier is not supported");
4799         if (action_modify_field->dst.field == RTE_FLOW_FIELD_GENEVE_VNI ||
4800             action_modify_field->src.field == RTE_FLOW_FIELD_GENEVE_VNI)
4801                 return rte_flow_error_set(error, ENOTSUP,
4802                                 RTE_FLOW_ERROR_TYPE_ACTION, action,
4803                                 "modifications of the GENEVE Network"
4804                                 " Identifier is not supported");
4805         if (action_modify_field->dst.field == RTE_FLOW_FIELD_MARK ||
4806             action_modify_field->src.field == RTE_FLOW_FIELD_MARK ||
4807             action_modify_field->dst.field == RTE_FLOW_FIELD_META ||
4808             action_modify_field->src.field == RTE_FLOW_FIELD_META) {
4809                 if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY ||
4810                     !mlx5_flow_ext_mreg_supported(dev))
4811                         return rte_flow_error_set(error, ENOTSUP,
4812                                         RTE_FLOW_ERROR_TYPE_ACTION, action,
4813                                         "cannot modify mark or metadata without"
4814                                         " extended metadata register support");
4815         }
4816         if (action_modify_field->operation != RTE_FLOW_MODIFY_SET)
4817                 return rte_flow_error_set(error, ENOTSUP,
4818                                 RTE_FLOW_ERROR_TYPE_ACTION, action,
4819                                 "add and sub operations"
4820                                 " are not supported");
4821         return (action_modify_field->width / 32) +
4822                !!(action_modify_field->width % 32);
4823 }
4824
4825 /**
4826  * Validate jump action.
4827  *
4828  * @param[in] action
4829  *   Pointer to the jump action.
4830  * @param[in] action_flags
4831  *   Holds the actions detected until now.
4832  * @param[in] attributes
4833  *   Pointer to flow attributes
4834  * @param[in] external
4835  *   Action belongs to flow rule created by request external to PMD.
4836  * @param[out] error
4837  *   Pointer to error structure.
4838  *
4839  * @return
4840  *   0 on success, a negative errno value otherwise and rte_errno is set.
4841  */
4842 static int
4843 flow_dv_validate_action_jump(struct rte_eth_dev *dev,
4844                              const struct mlx5_flow_tunnel *tunnel,
4845                              const struct rte_flow_action *action,
4846                              uint64_t action_flags,
4847                              const struct rte_flow_attr *attributes,
4848                              bool external, struct rte_flow_error *error)
4849 {
4850         uint32_t target_group, table;
4851         int ret = 0;
4852         struct flow_grp_info grp_info = {
4853                 .external = !!external,
4854                 .transfer = !!attributes->transfer,
4855                 .fdb_def_rule = 1,
4856                 .std_tbl_fix = 0
4857         };
4858         if (action_flags & (MLX5_FLOW_FATE_ACTIONS |
4859                             MLX5_FLOW_FATE_ESWITCH_ACTIONS))
4860                 return rte_flow_error_set(error, EINVAL,
4861                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
4862                                           "can't have 2 fate actions in"
4863                                           " same flow");
4864         if (!action->conf)
4865                 return rte_flow_error_set(error, EINVAL,
4866                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
4867                                           NULL, "action configuration not set");
4868         target_group =
4869                 ((const struct rte_flow_action_jump *)action->conf)->group;
4870         ret = mlx5_flow_group_to_table(dev, tunnel, target_group, &table,
4871                                        &grp_info, error);
4872         if (ret)
4873                 return ret;
4874         if (attributes->group == target_group &&
4875             !(action_flags & (MLX5_FLOW_ACTION_TUNNEL_SET |
4876                               MLX5_FLOW_ACTION_TUNNEL_MATCH)))
4877                 return rte_flow_error_set(error, EINVAL,
4878                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
4879                                           "target group must be other than"
4880                                           " the current flow group");
4881         return 0;
4882 }
4883
4884 /*
4885  * Validate the port_id action.
4886  *
4887  * @param[in] dev
4888  *   Pointer to rte_eth_dev structure.
4889  * @param[in] action_flags
4890  *   Bit-fields that holds the actions detected until now.
4891  * @param[in] action
4892  *   Port_id RTE action structure.
4893  * @param[in] attr
4894  *   Attributes of flow that includes this action.
4895  * @param[out] error
4896  *   Pointer to error structure.
4897  *
4898  * @return
4899  *   0 on success, a negative errno value otherwise and rte_errno is set.
4900  */
4901 static int
4902 flow_dv_validate_action_port_id(struct rte_eth_dev *dev,
4903                                 uint64_t action_flags,
4904                                 const struct rte_flow_action *action,
4905                                 const struct rte_flow_attr *attr,
4906                                 struct rte_flow_error *error)
4907 {
4908         const struct rte_flow_action_port_id *port_id;
4909         struct mlx5_priv *act_priv;
4910         struct mlx5_priv *dev_priv;
4911         uint16_t port;
4912
4913         if (!attr->transfer)
4914                 return rte_flow_error_set(error, ENOTSUP,
4915                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4916                                           NULL,
4917                                           "port id action is valid in transfer"
4918                                           " mode only");
4919         if (!action || !action->conf)
4920                 return rte_flow_error_set(error, ENOTSUP,
4921                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
4922                                           NULL,
4923                                           "port id action parameters must be"
4924                                           " specified");
4925         if (action_flags & (MLX5_FLOW_FATE_ACTIONS |
4926                             MLX5_FLOW_FATE_ESWITCH_ACTIONS))
4927                 return rte_flow_error_set(error, EINVAL,
4928                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
4929                                           "can have only one fate actions in"
4930                                           " a flow");
4931         dev_priv = mlx5_dev_to_eswitch_info(dev);
4932         if (!dev_priv)
4933                 return rte_flow_error_set(error, rte_errno,
4934                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4935                                           NULL,
4936                                           "failed to obtain E-Switch info");
4937         port_id = action->conf;
4938         port = port_id->original ? dev->data->port_id : port_id->id;
4939         act_priv = mlx5_port_to_eswitch_info(port, false);
4940         if (!act_priv)
4941                 return rte_flow_error_set
4942                                 (error, rte_errno,
4943                                  RTE_FLOW_ERROR_TYPE_ACTION_CONF, port_id,
4944                                  "failed to obtain E-Switch port id for port");
4945         if (act_priv->domain_id != dev_priv->domain_id)
4946                 return rte_flow_error_set
4947                                 (error, EINVAL,
4948                                  RTE_FLOW_ERROR_TYPE_ACTION, NULL,
4949                                  "port does not belong to"
4950                                  " E-Switch being configured");
4951         return 0;
4952 }
4953
4954 /**
4955  * Get the maximum number of modify header actions.
4956  *
4957  * @param dev
4958  *   Pointer to rte_eth_dev structure.
4959  * @param flags
4960  *   Flags bits to check if root level.
4961  *
4962  * @return
4963  *   Max number of modify header actions device can support.
4964  */
4965 static inline unsigned int
4966 flow_dv_modify_hdr_action_max(struct rte_eth_dev *dev __rte_unused,
4967                               uint64_t flags)
4968 {
4969         /*
4970          * There's no way to directly query the max capacity from FW.
4971          * The maximal value on root table should be assumed to be supported.
4972          */
4973         if (!(flags & MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL))
4974                 return MLX5_MAX_MODIFY_NUM;
4975         else
4976                 return MLX5_ROOT_TBL_MODIFY_NUM;
4977 }
4978
4979 /**
4980  * Validate the meter action.
4981  *
4982  * @param[in] dev
4983  *   Pointer to rte_eth_dev structure.
4984  * @param[in] action_flags
4985  *   Bit-fields that holds the actions detected until now.
4986  * @param[in] action
4987  *   Pointer to the meter action.
4988  * @param[in] attr
4989  *   Attributes of flow that includes this action.
4990  * @param[out] error
4991  *   Pointer to error structure.
4992  *
4993  * @return
4994  *   0 on success, a negative errno value otherwise and rte_ernno is set.
4995  */
4996 static int
4997 mlx5_flow_validate_action_meter(struct rte_eth_dev *dev,
4998                                 uint64_t action_flags,
4999                                 const struct rte_flow_action *action,
5000                                 const struct rte_flow_attr *attr,
5001                                 bool *def_policy,
5002                                 struct rte_flow_error *error)
5003 {
5004         struct mlx5_priv *priv = dev->data->dev_private;
5005         const struct rte_flow_action_meter *am = action->conf;
5006         struct mlx5_flow_meter_info *fm;
5007         struct mlx5_flow_meter_policy *mtr_policy;
5008         struct mlx5_flow_mtr_mng *mtrmng = priv->sh->mtrmng;
5009
5010         if (!am)
5011                 return rte_flow_error_set(error, EINVAL,
5012                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5013                                           "meter action conf is NULL");
5014
5015         if (action_flags & MLX5_FLOW_ACTION_METER)
5016                 return rte_flow_error_set(error, ENOTSUP,
5017                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5018                                           "meter chaining not support");
5019         if (action_flags & MLX5_FLOW_ACTION_JUMP)
5020                 return rte_flow_error_set(error, ENOTSUP,
5021                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5022                                           "meter with jump not support");
5023         if (!priv->mtr_en)
5024                 return rte_flow_error_set(error, ENOTSUP,
5025                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5026                                           NULL,
5027                                           "meter action not supported");
5028         fm = mlx5_flow_meter_find(priv, am->mtr_id, NULL);
5029         if (!fm)
5030                 return rte_flow_error_set(error, EINVAL,
5031                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5032                                           "Meter not found");
5033         /* aso meter can always be shared by different domains */
5034         if (fm->ref_cnt && !priv->sh->meter_aso_en &&
5035             !(fm->transfer == attr->transfer ||
5036               (!fm->ingress && !attr->ingress && attr->egress) ||
5037               (!fm->egress && !attr->egress && attr->ingress)))
5038                 return rte_flow_error_set(error, EINVAL,
5039                         RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5040                         "Flow attributes domain are either invalid "
5041                         "or have a domain conflict with current "
5042                         "meter attributes");
5043         if (fm->def_policy) {
5044                 if (!((attr->transfer &&
5045                         mtrmng->def_policy[MLX5_MTR_DOMAIN_TRANSFER]) ||
5046                         (attr->egress &&
5047                         mtrmng->def_policy[MLX5_MTR_DOMAIN_EGRESS]) ||
5048                         (attr->ingress &&
5049                         mtrmng->def_policy[MLX5_MTR_DOMAIN_INGRESS])))
5050                         return rte_flow_error_set(error, EINVAL,
5051                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5052                                           "Flow attributes domain "
5053                                           "have a conflict with current "
5054                                           "meter domain attributes");
5055                 *def_policy = true;
5056         } else {
5057                 mtr_policy = mlx5_flow_meter_policy_find(dev,
5058                                                 fm->policy_id, NULL);
5059                 if (!mtr_policy)
5060                         return rte_flow_error_set(error, EINVAL,
5061                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5062                                           "Invalid policy id for meter ");
5063                 if (!((attr->transfer && mtr_policy->transfer) ||
5064                         (attr->egress && mtr_policy->egress) ||
5065                         (attr->ingress && mtr_policy->ingress)))
5066                         return rte_flow_error_set(error, EINVAL,
5067                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5068                                           "Flow attributes domain "
5069                                           "have a conflict with current "
5070                                           "meter domain attributes");
5071                 *def_policy = false;
5072         }
5073         return 0;
5074 }
5075
5076 /**
5077  * Validate the age action.
5078  *
5079  * @param[in] action_flags
5080  *   Holds the actions detected until now.
5081  * @param[in] action
5082  *   Pointer to the age action.
5083  * @param[in] dev
5084  *   Pointer to the Ethernet device structure.
5085  * @param[out] error
5086  *   Pointer to error structure.
5087  *
5088  * @return
5089  *   0 on success, a negative errno value otherwise and rte_errno is set.
5090  */
5091 static int
5092 flow_dv_validate_action_age(uint64_t action_flags,
5093                             const struct rte_flow_action *action,
5094                             struct rte_eth_dev *dev,
5095                             struct rte_flow_error *error)
5096 {
5097         struct mlx5_priv *priv = dev->data->dev_private;
5098         const struct rte_flow_action_age *age = action->conf;
5099
5100         if (!priv->config.devx || (priv->sh->cmng.counter_fallback &&
5101             !priv->sh->aso_age_mng))
5102                 return rte_flow_error_set(error, ENOTSUP,
5103                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5104                                           NULL,
5105                                           "age action not supported");
5106         if (!(action->conf))
5107                 return rte_flow_error_set(error, EINVAL,
5108                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
5109                                           "configuration cannot be null");
5110         if (!(age->timeout))
5111                 return rte_flow_error_set(error, EINVAL,
5112                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
5113                                           "invalid timeout value 0");
5114         if (action_flags & MLX5_FLOW_ACTION_AGE)
5115                 return rte_flow_error_set(error, EINVAL,
5116                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5117                                           "duplicate age actions set");
5118         return 0;
5119 }
5120
5121 /**
5122  * Validate the modify-header IPv4 DSCP actions.
5123  *
5124  * @param[in] action_flags
5125  *   Holds the actions detected until now.
5126  * @param[in] action
5127  *   Pointer to the modify action.
5128  * @param[in] item_flags
5129  *   Holds the items detected.
5130  * @param[out] error
5131  *   Pointer to error structure.
5132  *
5133  * @return
5134  *   0 on success, a negative errno value otherwise and rte_errno is set.
5135  */
5136 static int
5137 flow_dv_validate_action_modify_ipv4_dscp(const uint64_t action_flags,
5138                                          const struct rte_flow_action *action,
5139                                          const uint64_t item_flags,
5140                                          struct rte_flow_error *error)
5141 {
5142         int ret = 0;
5143
5144         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
5145         if (!ret) {
5146                 if (!(item_flags & MLX5_FLOW_LAYER_L3_IPV4))
5147                         return rte_flow_error_set(error, EINVAL,
5148                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5149                                                   NULL,
5150                                                   "no ipv4 item in pattern");
5151         }
5152         return ret;
5153 }
5154
5155 /**
5156  * Validate the modify-header IPv6 DSCP actions.
5157  *
5158  * @param[in] action_flags
5159  *   Holds the actions detected until now.
5160  * @param[in] action
5161  *   Pointer to the modify action.
5162  * @param[in] item_flags
5163  *   Holds the items detected.
5164  * @param[out] error
5165  *   Pointer to error structure.
5166  *
5167  * @return
5168  *   0 on success, a negative errno value otherwise and rte_errno is set.
5169  */
5170 static int
5171 flow_dv_validate_action_modify_ipv6_dscp(const uint64_t action_flags,
5172                                          const struct rte_flow_action *action,
5173                                          const uint64_t item_flags,
5174                                          struct rte_flow_error *error)
5175 {
5176         int ret = 0;
5177
5178         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
5179         if (!ret) {
5180                 if (!(item_flags & MLX5_FLOW_LAYER_L3_IPV6))
5181                         return rte_flow_error_set(error, EINVAL,
5182                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5183                                                   NULL,
5184                                                   "no ipv6 item in pattern");
5185         }
5186         return ret;
5187 }
5188
5189 /**
5190  * Match modify-header resource.
5191  *
5192  * @param list
5193  *   Pointer to the hash list.
5194  * @param entry
5195  *   Pointer to exist resource entry object.
5196  * @param key
5197  *   Key of the new entry.
5198  * @param ctx
5199  *   Pointer to new modify-header resource.
5200  *
5201  * @return
5202  *   0 on matching, non-zero otherwise.
5203  */
5204 int
5205 flow_dv_modify_match_cb(struct mlx5_hlist *list __rte_unused,
5206                         struct mlx5_hlist_entry *entry,
5207                         uint64_t key __rte_unused, void *cb_ctx)
5208 {
5209         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
5210         struct mlx5_flow_dv_modify_hdr_resource *ref = ctx->data;
5211         struct mlx5_flow_dv_modify_hdr_resource *resource =
5212                         container_of(entry, typeof(*resource), entry);
5213         uint32_t key_len = sizeof(*ref) - offsetof(typeof(*ref), ft_type);
5214
5215         key_len += ref->actions_num * sizeof(ref->actions[0]);
5216         return ref->actions_num != resource->actions_num ||
5217                memcmp(&ref->ft_type, &resource->ft_type, key_len);
5218 }
5219
5220 struct mlx5_hlist_entry *
5221 flow_dv_modify_create_cb(struct mlx5_hlist *list, uint64_t key __rte_unused,
5222                          void *cb_ctx)
5223 {
5224         struct mlx5_dev_ctx_shared *sh = list->ctx;
5225         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
5226         struct mlx5dv_dr_domain *ns;
5227         struct mlx5_flow_dv_modify_hdr_resource *entry;
5228         struct mlx5_flow_dv_modify_hdr_resource *ref = ctx->data;
5229         int ret;
5230         uint32_t data_len = ref->actions_num * sizeof(ref->actions[0]);
5231         uint32_t key_len = sizeof(*ref) - offsetof(typeof(*ref), ft_type);
5232
5233         entry = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*entry) + data_len, 0,
5234                             SOCKET_ID_ANY);
5235         if (!entry) {
5236                 rte_flow_error_set(ctx->error, ENOMEM,
5237                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5238                                    "cannot allocate resource memory");
5239                 return NULL;
5240         }
5241         rte_memcpy(&entry->ft_type,
5242                    RTE_PTR_ADD(ref, offsetof(typeof(*ref), ft_type)),
5243                    key_len + data_len);
5244         if (entry->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
5245                 ns = sh->fdb_domain;
5246         else if (entry->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_TX)
5247                 ns = sh->tx_domain;
5248         else
5249                 ns = sh->rx_domain;
5250         ret = mlx5_flow_os_create_flow_action_modify_header
5251                                         (sh->ctx, ns, entry,
5252                                          data_len, &entry->action);
5253         if (ret) {
5254                 mlx5_free(entry);
5255                 rte_flow_error_set(ctx->error, ENOMEM,
5256                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5257                                    NULL, "cannot create modification action");
5258                 return NULL;
5259         }
5260         return &entry->entry;
5261 }
5262
5263 /**
5264  * Validate the sample action.
5265  *
5266  * @param[in, out] action_flags
5267  *   Holds the actions detected until now.
5268  * @param[in] action
5269  *   Pointer to the sample action.
5270  * @param[in] dev
5271  *   Pointer to the Ethernet device structure.
5272  * @param[in] attr
5273  *   Attributes of flow that includes this action.
5274  * @param[in] item_flags
5275  *   Holds the items detected.
5276  * @param[in] rss
5277  *   Pointer to the RSS action.
5278  * @param[out] sample_rss
5279  *   Pointer to the RSS action in sample action list.
5280  * @param[out] count
5281  *   Pointer to the COUNT action in sample action list.
5282  * @param[out] fdb_mirror_limit
5283  *   Pointer to the FDB mirror limitation flag.
5284  * @param[out] error
5285  *   Pointer to error structure.
5286  *
5287  * @return
5288  *   0 on success, a negative errno value otherwise and rte_errno is set.
5289  */
5290 static int
5291 flow_dv_validate_action_sample(uint64_t *action_flags,
5292                                const struct rte_flow_action *action,
5293                                struct rte_eth_dev *dev,
5294                                const struct rte_flow_attr *attr,
5295                                uint64_t item_flags,
5296                                const struct rte_flow_action_rss *rss,
5297                                const struct rte_flow_action_rss **sample_rss,
5298                                const struct rte_flow_action_count **count,
5299                                int *fdb_mirror_limit,
5300                                struct rte_flow_error *error)
5301 {
5302         struct mlx5_priv *priv = dev->data->dev_private;
5303         struct mlx5_dev_config *dev_conf = &priv->config;
5304         const struct rte_flow_action_sample *sample = action->conf;
5305         const struct rte_flow_action *act;
5306         uint64_t sub_action_flags = 0;
5307         uint16_t queue_index = 0xFFFF;
5308         int actions_n = 0;
5309         int ret;
5310
5311         if (!sample)
5312                 return rte_flow_error_set(error, EINVAL,
5313                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
5314                                           "configuration cannot be NULL");
5315         if (sample->ratio == 0)
5316                 return rte_flow_error_set(error, EINVAL,
5317                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
5318                                           "ratio value starts from 1");
5319         if (!priv->config.devx || (sample->ratio > 0 && !priv->sampler_en))
5320                 return rte_flow_error_set(error, ENOTSUP,
5321                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5322                                           NULL,
5323                                           "sample action not supported");
5324         if (*action_flags & MLX5_FLOW_ACTION_SAMPLE)
5325                 return rte_flow_error_set(error, EINVAL,
5326                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5327                                           "Multiple sample actions not "
5328                                           "supported");
5329         if (*action_flags & MLX5_FLOW_ACTION_METER)
5330                 return rte_flow_error_set(error, EINVAL,
5331                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
5332                                           "wrong action order, meter should "
5333                                           "be after sample action");
5334         if (*action_flags & MLX5_FLOW_ACTION_JUMP)
5335                 return rte_flow_error_set(error, EINVAL,
5336                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
5337                                           "wrong action order, jump should "
5338                                           "be after sample action");
5339         act = sample->actions;
5340         for (; act->type != RTE_FLOW_ACTION_TYPE_END; act++) {
5341                 if (actions_n == MLX5_DV_MAX_NUMBER_OF_ACTIONS)
5342                         return rte_flow_error_set(error, ENOTSUP,
5343                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5344                                                   act, "too many actions");
5345                 switch (act->type) {
5346                 case RTE_FLOW_ACTION_TYPE_QUEUE:
5347                         ret = mlx5_flow_validate_action_queue(act,
5348                                                               sub_action_flags,
5349                                                               dev,
5350                                                               attr, error);
5351                         if (ret < 0)
5352                                 return ret;
5353                         queue_index = ((const struct rte_flow_action_queue *)
5354                                                         (act->conf))->index;
5355                         sub_action_flags |= MLX5_FLOW_ACTION_QUEUE;
5356                         ++actions_n;
5357                         break;
5358                 case RTE_FLOW_ACTION_TYPE_RSS:
5359                         *sample_rss = act->conf;
5360                         ret = mlx5_flow_validate_action_rss(act,
5361                                                             sub_action_flags,
5362                                                             dev, attr,
5363                                                             item_flags,
5364                                                             error);
5365                         if (ret < 0)
5366                                 return ret;
5367                         if (rss && *sample_rss &&
5368                             ((*sample_rss)->level != rss->level ||
5369                             (*sample_rss)->types != rss->types))
5370                                 return rte_flow_error_set(error, ENOTSUP,
5371                                         RTE_FLOW_ERROR_TYPE_ACTION,
5372                                         NULL,
5373                                         "Can't use the different RSS types "
5374                                         "or level in the same flow");
5375                         if (*sample_rss != NULL && (*sample_rss)->queue_num)
5376                                 queue_index = (*sample_rss)->queue[0];
5377                         sub_action_flags |= MLX5_FLOW_ACTION_RSS;
5378                         ++actions_n;
5379                         break;
5380                 case RTE_FLOW_ACTION_TYPE_MARK:
5381                         ret = flow_dv_validate_action_mark(dev, act,
5382                                                            sub_action_flags,
5383                                                            attr, error);
5384                         if (ret < 0)
5385                                 return ret;
5386                         if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY)
5387                                 sub_action_flags |= MLX5_FLOW_ACTION_MARK |
5388                                                 MLX5_FLOW_ACTION_MARK_EXT;
5389                         else
5390                                 sub_action_flags |= MLX5_FLOW_ACTION_MARK;
5391                         ++actions_n;
5392                         break;
5393                 case RTE_FLOW_ACTION_TYPE_COUNT:
5394                         ret = flow_dv_validate_action_count
5395                                 (dev, is_shared_action_count(act),
5396                                  *action_flags | sub_action_flags,
5397                                  error);
5398                         if (ret < 0)
5399                                 return ret;
5400                         *count = act->conf;
5401                         sub_action_flags |= MLX5_FLOW_ACTION_COUNT;
5402                         *action_flags |= MLX5_FLOW_ACTION_COUNT;
5403                         ++actions_n;
5404                         break;
5405                 case RTE_FLOW_ACTION_TYPE_PORT_ID:
5406                         ret = flow_dv_validate_action_port_id(dev,
5407                                                               sub_action_flags,
5408                                                               act,
5409                                                               attr,
5410                                                               error);
5411                         if (ret)
5412                                 return ret;
5413                         sub_action_flags |= MLX5_FLOW_ACTION_PORT_ID;
5414                         ++actions_n;
5415                         break;
5416                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
5417                         ret = flow_dv_validate_action_raw_encap_decap
5418                                 (dev, NULL, act->conf, attr, &sub_action_flags,
5419                                  &actions_n, action, item_flags, error);
5420                         if (ret < 0)
5421                                 return ret;
5422                         ++actions_n;
5423                         break;
5424                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
5425                 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
5426                         ret = flow_dv_validate_action_l2_encap(dev,
5427                                                                sub_action_flags,
5428                                                                act, attr,
5429                                                                error);
5430                         if (ret < 0)
5431                                 return ret;
5432                         sub_action_flags |= MLX5_FLOW_ACTION_ENCAP;
5433                         ++actions_n;
5434                         break;
5435                 default:
5436                         return rte_flow_error_set(error, ENOTSUP,
5437                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5438                                                   NULL,
5439                                                   "Doesn't support optional "
5440                                                   "action");
5441                 }
5442         }
5443         if (attr->ingress && !attr->transfer) {
5444                 if (!(sub_action_flags & (MLX5_FLOW_ACTION_QUEUE |
5445                                           MLX5_FLOW_ACTION_RSS)))
5446                         return rte_flow_error_set(error, EINVAL,
5447                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5448                                                   NULL,
5449                                                   "Ingress must has a dest "
5450                                                   "QUEUE for Sample");
5451         } else if (attr->egress && !attr->transfer) {
5452                 return rte_flow_error_set(error, ENOTSUP,
5453                                           RTE_FLOW_ERROR_TYPE_ACTION,
5454                                           NULL,
5455                                           "Sample Only support Ingress "
5456                                           "or E-Switch");
5457         } else if (sample->actions->type != RTE_FLOW_ACTION_TYPE_END) {
5458                 MLX5_ASSERT(attr->transfer);
5459                 if (sample->ratio > 1)
5460                         return rte_flow_error_set(error, ENOTSUP,
5461                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5462                                                   NULL,
5463                                                   "E-Switch doesn't support "
5464                                                   "any optional action "
5465                                                   "for sampling");
5466                 if (sub_action_flags & MLX5_FLOW_ACTION_QUEUE)
5467                         return rte_flow_error_set(error, ENOTSUP,
5468                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5469                                                   NULL,
5470                                                   "unsupported action QUEUE");
5471                 if (sub_action_flags & MLX5_FLOW_ACTION_RSS)
5472                         return rte_flow_error_set(error, ENOTSUP,
5473                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5474                                                   NULL,
5475                                                   "unsupported action QUEUE");
5476                 if (!(sub_action_flags & MLX5_FLOW_ACTION_PORT_ID))
5477                         return rte_flow_error_set(error, EINVAL,
5478                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5479                                                   NULL,
5480                                                   "E-Switch must has a dest "
5481                                                   "port for mirroring");
5482                 if (!priv->config.hca_attr.reg_c_preserve &&
5483                      priv->representor_id != -1)
5484                         *fdb_mirror_limit = 1;
5485         }
5486         /* Continue validation for Xcap actions.*/
5487         if ((sub_action_flags & MLX5_FLOW_XCAP_ACTIONS) &&
5488             (queue_index == 0xFFFF ||
5489              mlx5_rxq_get_type(dev, queue_index) != MLX5_RXQ_TYPE_HAIRPIN)) {
5490                 if ((sub_action_flags & MLX5_FLOW_XCAP_ACTIONS) ==
5491                      MLX5_FLOW_XCAP_ACTIONS)
5492                         return rte_flow_error_set(error, ENOTSUP,
5493                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5494                                                   NULL, "encap and decap "
5495                                                   "combination aren't "
5496                                                   "supported");
5497                 if (!attr->transfer && attr->ingress && (sub_action_flags &
5498                                                         MLX5_FLOW_ACTION_ENCAP))
5499                         return rte_flow_error_set(error, ENOTSUP,
5500                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5501                                                   NULL, "encap is not supported"
5502                                                   " for ingress traffic");
5503         }
5504         return 0;
5505 }
5506
5507 /**
5508  * Find existing modify-header resource or create and register a new one.
5509  *
5510  * @param dev[in, out]
5511  *   Pointer to rte_eth_dev structure.
5512  * @param[in, out] resource
5513  *   Pointer to modify-header resource.
5514  * @parm[in, out] dev_flow
5515  *   Pointer to the dev_flow.
5516  * @param[out] error
5517  *   pointer to error structure.
5518  *
5519  * @return
5520  *   0 on success otherwise -errno and errno is set.
5521  */
5522 static int
5523 flow_dv_modify_hdr_resource_register
5524                         (struct rte_eth_dev *dev,
5525                          struct mlx5_flow_dv_modify_hdr_resource *resource,
5526                          struct mlx5_flow *dev_flow,
5527                          struct rte_flow_error *error)
5528 {
5529         struct mlx5_priv *priv = dev->data->dev_private;
5530         struct mlx5_dev_ctx_shared *sh = priv->sh;
5531         uint32_t key_len = sizeof(*resource) -
5532                            offsetof(typeof(*resource), ft_type) +
5533                            resource->actions_num * sizeof(resource->actions[0]);
5534         struct mlx5_hlist_entry *entry;
5535         struct mlx5_flow_cb_ctx ctx = {
5536                 .error = error,
5537                 .data = resource,
5538         };
5539         uint64_t key64;
5540
5541         resource->flags = dev_flow->dv.group ? 0 :
5542                           MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL;
5543         if (resource->actions_num > flow_dv_modify_hdr_action_max(dev,
5544                                     resource->flags))
5545                 return rte_flow_error_set(error, EOVERFLOW,
5546                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5547                                           "too many modify header items");
5548         key64 = __rte_raw_cksum(&resource->ft_type, key_len, 0);
5549         entry = mlx5_hlist_register(sh->modify_cmds, key64, &ctx);
5550         if (!entry)
5551                 return -rte_errno;
5552         resource = container_of(entry, typeof(*resource), entry);
5553         dev_flow->handle->dvh.modify_hdr = resource;
5554         return 0;
5555 }
5556
5557 /**
5558  * Get DV flow counter by index.
5559  *
5560  * @param[in] dev
5561  *   Pointer to the Ethernet device structure.
5562  * @param[in] idx
5563  *   mlx5 flow counter index in the container.
5564  * @param[out] ppool
5565  *   mlx5 flow counter pool in the container.
5566  *
5567  * @return
5568  *   Pointer to the counter, NULL otherwise.
5569  */
5570 static struct mlx5_flow_counter *
5571 flow_dv_counter_get_by_idx(struct rte_eth_dev *dev,
5572                            uint32_t idx,
5573                            struct mlx5_flow_counter_pool **ppool)
5574 {
5575         struct mlx5_priv *priv = dev->data->dev_private;
5576         struct mlx5_flow_counter_mng *cmng = &priv->sh->cmng;
5577         struct mlx5_flow_counter_pool *pool;
5578
5579         /* Decrease to original index and clear shared bit. */
5580         idx = (idx - 1) & (MLX5_CNT_SHARED_OFFSET - 1);
5581         MLX5_ASSERT(idx / MLX5_COUNTERS_PER_POOL < cmng->n);
5582         pool = cmng->pools[idx / MLX5_COUNTERS_PER_POOL];
5583         MLX5_ASSERT(pool);
5584         if (ppool)
5585                 *ppool = pool;
5586         return MLX5_POOL_GET_CNT(pool, idx % MLX5_COUNTERS_PER_POOL);
5587 }
5588
5589 /**
5590  * Check the devx counter belongs to the pool.
5591  *
5592  * @param[in] pool
5593  *   Pointer to the counter pool.
5594  * @param[in] id
5595  *   The counter devx ID.
5596  *
5597  * @return
5598  *   True if counter belongs to the pool, false otherwise.
5599  */
5600 static bool
5601 flow_dv_is_counter_in_pool(struct mlx5_flow_counter_pool *pool, int id)
5602 {
5603         int base = (pool->min_dcs->id / MLX5_COUNTERS_PER_POOL) *
5604                    MLX5_COUNTERS_PER_POOL;
5605
5606         if (id >= base && id < base + MLX5_COUNTERS_PER_POOL)
5607                 return true;
5608         return false;
5609 }
5610
5611 /**
5612  * Get a pool by devx counter ID.
5613  *
5614  * @param[in] cmng
5615  *   Pointer to the counter management.
5616  * @param[in] id
5617  *   The counter devx ID.
5618  *
5619  * @return
5620  *   The counter pool pointer if exists, NULL otherwise,
5621  */
5622 static struct mlx5_flow_counter_pool *
5623 flow_dv_find_pool_by_id(struct mlx5_flow_counter_mng *cmng, int id)
5624 {
5625         uint32_t i;
5626         struct mlx5_flow_counter_pool *pool = NULL;
5627
5628         rte_spinlock_lock(&cmng->pool_update_sl);
5629         /* Check last used pool. */
5630         if (cmng->last_pool_idx != POOL_IDX_INVALID &&
5631             flow_dv_is_counter_in_pool(cmng->pools[cmng->last_pool_idx], id)) {
5632                 pool = cmng->pools[cmng->last_pool_idx];
5633                 goto out;
5634         }
5635         /* ID out of range means no suitable pool in the container. */
5636         if (id > cmng->max_id || id < cmng->min_id)
5637                 goto out;
5638         /*
5639          * Find the pool from the end of the container, since mostly counter
5640          * ID is sequence increasing, and the last pool should be the needed
5641          * one.
5642          */
5643         i = cmng->n_valid;
5644         while (i--) {
5645                 struct mlx5_flow_counter_pool *pool_tmp = cmng->pools[i];
5646
5647                 if (flow_dv_is_counter_in_pool(pool_tmp, id)) {
5648                         pool = pool_tmp;
5649                         break;
5650                 }
5651         }
5652 out:
5653         rte_spinlock_unlock(&cmng->pool_update_sl);
5654         return pool;
5655 }
5656
5657 /**
5658  * Resize a counter container.
5659  *
5660  * @param[in] dev
5661  *   Pointer to the Ethernet device structure.
5662  *
5663  * @return
5664  *   0 on success, otherwise negative errno value and rte_errno is set.
5665  */
5666 static int
5667 flow_dv_container_resize(struct rte_eth_dev *dev)
5668 {
5669         struct mlx5_priv *priv = dev->data->dev_private;
5670         struct mlx5_flow_counter_mng *cmng = &priv->sh->cmng;
5671         void *old_pools = cmng->pools;
5672         uint32_t resize = cmng->n + MLX5_CNT_CONTAINER_RESIZE;
5673         uint32_t mem_size = sizeof(struct mlx5_flow_counter_pool *) * resize;
5674         void *pools = mlx5_malloc(MLX5_MEM_ZERO, mem_size, 0, SOCKET_ID_ANY);
5675
5676         if (!pools) {
5677                 rte_errno = ENOMEM;
5678                 return -ENOMEM;
5679         }
5680         if (old_pools)
5681                 memcpy(pools, old_pools, cmng->n *
5682                                        sizeof(struct mlx5_flow_counter_pool *));
5683         cmng->n = resize;
5684         cmng->pools = pools;
5685         if (old_pools)
5686                 mlx5_free(old_pools);
5687         return 0;
5688 }
5689
5690 /**
5691  * Query a devx flow counter.
5692  *
5693  * @param[in] dev
5694  *   Pointer to the Ethernet device structure.
5695  * @param[in] counter
5696  *   Index to the flow counter.
5697  * @param[out] pkts
5698  *   The statistics value of packets.
5699  * @param[out] bytes
5700  *   The statistics value of bytes.
5701  *
5702  * @return
5703  *   0 on success, otherwise a negative errno value and rte_errno is set.
5704  */
5705 static inline int
5706 _flow_dv_query_count(struct rte_eth_dev *dev, uint32_t counter, uint64_t *pkts,
5707                      uint64_t *bytes)
5708 {
5709         struct mlx5_priv *priv = dev->data->dev_private;
5710         struct mlx5_flow_counter_pool *pool = NULL;
5711         struct mlx5_flow_counter *cnt;
5712         int offset;
5713
5714         cnt = flow_dv_counter_get_by_idx(dev, counter, &pool);
5715         MLX5_ASSERT(pool);
5716         if (priv->sh->cmng.counter_fallback)
5717                 return mlx5_devx_cmd_flow_counter_query(cnt->dcs_when_active, 0,
5718                                         0, pkts, bytes, 0, NULL, NULL, 0);
5719         rte_spinlock_lock(&pool->sl);
5720         if (!pool->raw) {
5721                 *pkts = 0;
5722                 *bytes = 0;
5723         } else {
5724                 offset = MLX5_CNT_ARRAY_IDX(pool, cnt);
5725                 *pkts = rte_be_to_cpu_64(pool->raw->data[offset].hits);
5726                 *bytes = rte_be_to_cpu_64(pool->raw->data[offset].bytes);
5727         }
5728         rte_spinlock_unlock(&pool->sl);
5729         return 0;
5730 }
5731
5732 /**
5733  * Create and initialize a new counter pool.
5734  *
5735  * @param[in] dev
5736  *   Pointer to the Ethernet device structure.
5737  * @param[out] dcs
5738  *   The devX counter handle.
5739  * @param[in] age
5740  *   Whether the pool is for counter that was allocated for aging.
5741  * @param[in/out] cont_cur
5742  *   Pointer to the container pointer, it will be update in pool resize.
5743  *
5744  * @return
5745  *   The pool container pointer on success, NULL otherwise and rte_errno is set.
5746  */
5747 static struct mlx5_flow_counter_pool *
5748 flow_dv_pool_create(struct rte_eth_dev *dev, struct mlx5_devx_obj *dcs,
5749                     uint32_t age)
5750 {
5751         struct mlx5_priv *priv = dev->data->dev_private;
5752         struct mlx5_flow_counter_pool *pool;
5753         struct mlx5_flow_counter_mng *cmng = &priv->sh->cmng;
5754         bool fallback = priv->sh->cmng.counter_fallback;
5755         uint32_t size = sizeof(*pool);
5756
5757         size += MLX5_COUNTERS_PER_POOL * MLX5_CNT_SIZE;
5758         size += (!age ? 0 : MLX5_COUNTERS_PER_POOL * MLX5_AGE_SIZE);
5759         pool = mlx5_malloc(MLX5_MEM_ZERO, size, 0, SOCKET_ID_ANY);
5760         if (!pool) {
5761                 rte_errno = ENOMEM;
5762                 return NULL;
5763         }
5764         pool->raw = NULL;
5765         pool->is_aged = !!age;
5766         pool->query_gen = 0;
5767         pool->min_dcs = dcs;
5768         rte_spinlock_init(&pool->sl);
5769         rte_spinlock_init(&pool->csl);
5770         TAILQ_INIT(&pool->counters[0]);
5771         TAILQ_INIT(&pool->counters[1]);
5772         pool->time_of_last_age_check = MLX5_CURR_TIME_SEC;
5773         rte_spinlock_lock(&cmng->pool_update_sl);
5774         pool->index = cmng->n_valid;
5775         if (pool->index == cmng->n && flow_dv_container_resize(dev)) {
5776                 mlx5_free(pool);
5777                 rte_spinlock_unlock(&cmng->pool_update_sl);
5778                 return NULL;
5779         }
5780         cmng->pools[pool->index] = pool;
5781         cmng->n_valid++;
5782         if (unlikely(fallback)) {
5783                 int base = RTE_ALIGN_FLOOR(dcs->id, MLX5_COUNTERS_PER_POOL);
5784
5785                 if (base < cmng->min_id)
5786                         cmng->min_id = base;
5787                 if (base > cmng->max_id)
5788                         cmng->max_id = base + MLX5_COUNTERS_PER_POOL - 1;
5789                 cmng->last_pool_idx = pool->index;
5790         }
5791         rte_spinlock_unlock(&cmng->pool_update_sl);
5792         return pool;
5793 }
5794
5795 /**
5796  * Prepare a new counter and/or a new counter pool.
5797  *
5798  * @param[in] dev
5799  *   Pointer to the Ethernet device structure.
5800  * @param[out] cnt_free
5801  *   Where to put the pointer of a new counter.
5802  * @param[in] age
5803  *   Whether the pool is for counter that was allocated for aging.
5804  *
5805  * @return
5806  *   The counter pool pointer and @p cnt_free is set on success,
5807  *   NULL otherwise and rte_errno is set.
5808  */
5809 static struct mlx5_flow_counter_pool *
5810 flow_dv_counter_pool_prepare(struct rte_eth_dev *dev,
5811                              struct mlx5_flow_counter **cnt_free,
5812                              uint32_t age)
5813 {
5814         struct mlx5_priv *priv = dev->data->dev_private;
5815         struct mlx5_flow_counter_mng *cmng = &priv->sh->cmng;
5816         struct mlx5_flow_counter_pool *pool;
5817         struct mlx5_counters tmp_tq;
5818         struct mlx5_devx_obj *dcs = NULL;
5819         struct mlx5_flow_counter *cnt;
5820         enum mlx5_counter_type cnt_type =
5821                         age ? MLX5_COUNTER_TYPE_AGE : MLX5_COUNTER_TYPE_ORIGIN;
5822         bool fallback = priv->sh->cmng.counter_fallback;
5823         uint32_t i;
5824
5825         if (fallback) {
5826                 /* bulk_bitmap must be 0 for single counter allocation. */
5827                 dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->ctx, 0);
5828                 if (!dcs)
5829                         return NULL;
5830                 pool = flow_dv_find_pool_by_id(cmng, dcs->id);
5831                 if (!pool) {
5832                         pool = flow_dv_pool_create(dev, dcs, age);
5833                         if (!pool) {
5834                                 mlx5_devx_cmd_destroy(dcs);
5835                                 return NULL;
5836                         }
5837                 }
5838                 i = dcs->id % MLX5_COUNTERS_PER_POOL;
5839                 cnt = MLX5_POOL_GET_CNT(pool, i);
5840                 cnt->pool = pool;
5841                 cnt->dcs_when_free = dcs;
5842                 *cnt_free = cnt;
5843                 return pool;
5844         }
5845         dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->ctx, 0x4);
5846         if (!dcs) {
5847                 rte_errno = ENODATA;
5848                 return NULL;
5849         }
5850         pool = flow_dv_pool_create(dev, dcs, age);
5851         if (!pool) {
5852                 mlx5_devx_cmd_destroy(dcs);
5853                 return NULL;
5854         }
5855         TAILQ_INIT(&tmp_tq);
5856         for (i = 1; i < MLX5_COUNTERS_PER_POOL; ++i) {
5857                 cnt = MLX5_POOL_GET_CNT(pool, i);
5858                 cnt->pool = pool;
5859                 TAILQ_INSERT_HEAD(&tmp_tq, cnt, next);
5860         }
5861         rte_spinlock_lock(&cmng->csl[cnt_type]);
5862         TAILQ_CONCAT(&cmng->counters[cnt_type], &tmp_tq, next);
5863         rte_spinlock_unlock(&cmng->csl[cnt_type]);
5864         *cnt_free = MLX5_POOL_GET_CNT(pool, 0);
5865         (*cnt_free)->pool = pool;
5866         return pool;
5867 }
5868
5869 /**
5870  * Allocate a flow counter.
5871  *
5872  * @param[in] dev
5873  *   Pointer to the Ethernet device structure.
5874  * @param[in] age
5875  *   Whether the counter was allocated for aging.
5876  *
5877  * @return
5878  *   Index to flow counter on success, 0 otherwise and rte_errno is set.
5879  */
5880 static uint32_t
5881 flow_dv_counter_alloc(struct rte_eth_dev *dev, uint32_t age)
5882 {
5883         struct mlx5_priv *priv = dev->data->dev_private;
5884         struct mlx5_flow_counter_pool *pool = NULL;
5885         struct mlx5_flow_counter *cnt_free = NULL;
5886         bool fallback = priv->sh->cmng.counter_fallback;
5887         struct mlx5_flow_counter_mng *cmng = &priv->sh->cmng;
5888         enum mlx5_counter_type cnt_type =
5889                         age ? MLX5_COUNTER_TYPE_AGE : MLX5_COUNTER_TYPE_ORIGIN;
5890         uint32_t cnt_idx;
5891
5892         if (!priv->config.devx) {
5893                 rte_errno = ENOTSUP;
5894                 return 0;
5895         }
5896         /* Get free counters from container. */
5897         rte_spinlock_lock(&cmng->csl[cnt_type]);
5898         cnt_free = TAILQ_FIRST(&cmng->counters[cnt_type]);
5899         if (cnt_free)
5900                 TAILQ_REMOVE(&cmng->counters[cnt_type], cnt_free, next);
5901         rte_spinlock_unlock(&cmng->csl[cnt_type]);
5902         if (!cnt_free && !flow_dv_counter_pool_prepare(dev, &cnt_free, age))
5903                 goto err;
5904         pool = cnt_free->pool;
5905         if (fallback)
5906                 cnt_free->dcs_when_active = cnt_free->dcs_when_free;
5907         /* Create a DV counter action only in the first time usage. */
5908         if (!cnt_free->action) {
5909                 uint16_t offset;
5910                 struct mlx5_devx_obj *dcs;
5911                 int ret;
5912
5913                 if (!fallback) {
5914                         offset = MLX5_CNT_ARRAY_IDX(pool, cnt_free);
5915                         dcs = pool->min_dcs;
5916                 } else {
5917                         offset = 0;
5918                         dcs = cnt_free->dcs_when_free;
5919                 }
5920                 ret = mlx5_flow_os_create_flow_action_count(dcs->obj, offset,
5921                                                             &cnt_free->action);
5922                 if (ret) {
5923                         rte_errno = errno;
5924                         goto err;
5925                 }
5926         }
5927         cnt_idx = MLX5_MAKE_CNT_IDX(pool->index,
5928                                 MLX5_CNT_ARRAY_IDX(pool, cnt_free));
5929         /* Update the counter reset values. */
5930         if (_flow_dv_query_count(dev, cnt_idx, &cnt_free->hits,
5931                                  &cnt_free->bytes))
5932                 goto err;
5933         if (!fallback && !priv->sh->cmng.query_thread_on)
5934                 /* Start the asynchronous batch query by the host thread. */
5935                 mlx5_set_query_alarm(priv->sh);
5936         /*
5937          * When the count action isn't shared (by ID), shared_info field is
5938          * used for indirect action API's refcnt.
5939          * When the counter action is not shared neither by ID nor by indirect
5940          * action API, shared info must be 1.
5941          */
5942         cnt_free->shared_info.refcnt = 1;
5943         return cnt_idx;
5944 err:
5945         if (cnt_free) {
5946                 cnt_free->pool = pool;
5947                 if (fallback)
5948                         cnt_free->dcs_when_free = cnt_free->dcs_when_active;
5949                 rte_spinlock_lock(&cmng->csl[cnt_type]);
5950                 TAILQ_INSERT_TAIL(&cmng->counters[cnt_type], cnt_free, next);
5951                 rte_spinlock_unlock(&cmng->csl[cnt_type]);
5952         }
5953         return 0;
5954 }
5955
5956 /**
5957  * Allocate a shared flow counter.
5958  *
5959  * @param[in] ctx
5960  *   Pointer to the shared counter configuration.
5961  * @param[in] data
5962  *   Pointer to save the allocated counter index.
5963  *
5964  * @return
5965  *   Index to flow counter on success, 0 otherwise and rte_errno is set.
5966  */
5967
5968 static int32_t
5969 flow_dv_counter_alloc_shared_cb(void *ctx, union mlx5_l3t_data *data)
5970 {
5971         struct mlx5_shared_counter_conf *conf = ctx;
5972         struct rte_eth_dev *dev = conf->dev;
5973         struct mlx5_flow_counter *cnt;
5974
5975         data->dword = flow_dv_counter_alloc(dev, 0);
5976         data->dword |= MLX5_CNT_SHARED_OFFSET;
5977         cnt = flow_dv_counter_get_by_idx(dev, data->dword, NULL);
5978         cnt->shared_info.id = conf->id;
5979         return 0;
5980 }
5981
5982 /**
5983  * Get a shared flow counter.
5984  *
5985  * @param[in] dev
5986  *   Pointer to the Ethernet device structure.
5987  * @param[in] id
5988  *   Counter identifier.
5989  *
5990  * @return
5991  *   Index to flow counter on success, 0 otherwise and rte_errno is set.
5992  */
5993 static uint32_t
5994 flow_dv_counter_get_shared(struct rte_eth_dev *dev, uint32_t id)
5995 {
5996         struct mlx5_priv *priv = dev->data->dev_private;
5997         struct mlx5_shared_counter_conf conf = {
5998                 .dev = dev,
5999                 .id = id,
6000         };
6001         union mlx5_l3t_data data = {
6002                 .dword = 0,
6003         };
6004
6005         mlx5_l3t_prepare_entry(priv->sh->cnt_id_tbl, id, &data,
6006                                flow_dv_counter_alloc_shared_cb, &conf);
6007         return data.dword;
6008 }
6009
6010 /**
6011  * Get age param from counter index.
6012  *
6013  * @param[in] dev
6014  *   Pointer to the Ethernet device structure.
6015  * @param[in] counter
6016  *   Index to the counter handler.
6017  *
6018  * @return
6019  *   The aging parameter specified for the counter index.
6020  */
6021 static struct mlx5_age_param*
6022 flow_dv_counter_idx_get_age(struct rte_eth_dev *dev,
6023                                 uint32_t counter)
6024 {
6025         struct mlx5_flow_counter *cnt;
6026         struct mlx5_flow_counter_pool *pool = NULL;
6027
6028         flow_dv_counter_get_by_idx(dev, counter, &pool);
6029         counter = (counter - 1) % MLX5_COUNTERS_PER_POOL;
6030         cnt = MLX5_POOL_GET_CNT(pool, counter);
6031         return MLX5_CNT_TO_AGE(cnt);
6032 }
6033
6034 /**
6035  * Remove a flow counter from aged counter list.
6036  *
6037  * @param[in] dev
6038  *   Pointer to the Ethernet device structure.
6039  * @param[in] counter
6040  *   Index to the counter handler.
6041  * @param[in] cnt
6042  *   Pointer to the counter handler.
6043  */
6044 static void
6045 flow_dv_counter_remove_from_age(struct rte_eth_dev *dev,
6046                                 uint32_t counter, struct mlx5_flow_counter *cnt)
6047 {
6048         struct mlx5_age_info *age_info;
6049         struct mlx5_age_param *age_param;
6050         struct mlx5_priv *priv = dev->data->dev_private;
6051         uint16_t expected = AGE_CANDIDATE;
6052
6053         age_info = GET_PORT_AGE_INFO(priv);
6054         age_param = flow_dv_counter_idx_get_age(dev, counter);
6055         if (!__atomic_compare_exchange_n(&age_param->state, &expected,
6056                                          AGE_FREE, false, __ATOMIC_RELAXED,
6057                                          __ATOMIC_RELAXED)) {
6058                 /**
6059                  * We need the lock even it is age timeout,
6060                  * since counter may still in process.
6061                  */
6062                 rte_spinlock_lock(&age_info->aged_sl);
6063                 TAILQ_REMOVE(&age_info->aged_counters, cnt, next);
6064                 rte_spinlock_unlock(&age_info->aged_sl);
6065                 __atomic_store_n(&age_param->state, AGE_FREE, __ATOMIC_RELAXED);
6066         }
6067 }
6068
6069 /**
6070  * Release a flow counter.
6071  *
6072  * @param[in] dev
6073  *   Pointer to the Ethernet device structure.
6074  * @param[in] counter
6075  *   Index to the counter handler.
6076  */
6077 static void
6078 flow_dv_counter_free(struct rte_eth_dev *dev, uint32_t counter)
6079 {
6080         struct mlx5_priv *priv = dev->data->dev_private;
6081         struct mlx5_flow_counter_pool *pool = NULL;
6082         struct mlx5_flow_counter *cnt;
6083         enum mlx5_counter_type cnt_type;
6084
6085         if (!counter)
6086                 return;
6087         cnt = flow_dv_counter_get_by_idx(dev, counter, &pool);
6088         MLX5_ASSERT(pool);
6089         /*
6090          * If the counter action is shared by ID, the l3t_clear_entry function
6091          * reduces its references counter. If after the reduction the action is
6092          * still referenced, the function returns here and does not release it.
6093          */
6094         if (IS_LEGACY_SHARED_CNT(counter) &&
6095             mlx5_l3t_clear_entry(priv->sh->cnt_id_tbl, cnt->shared_info.id))
6096                 return;
6097         /*
6098          * If the counter action is shared by indirect action API, the atomic
6099          * function reduces its references counter. If after the reduction the
6100          * action is still referenced, the function returns here and does not
6101          * release it.
6102          * When the counter action is not shared neither by ID nor by indirect
6103          * action API, shared info is 1 before the reduction, so this condition
6104          * is failed and function doesn't return here.
6105          */
6106         if (!IS_LEGACY_SHARED_CNT(counter) &&
6107             __atomic_sub_fetch(&cnt->shared_info.refcnt, 1, __ATOMIC_RELAXED))
6108                 return;
6109         if (pool->is_aged)
6110                 flow_dv_counter_remove_from_age(dev, counter, cnt);
6111         cnt->pool = pool;
6112         /*
6113          * Put the counter back to list to be updated in none fallback mode.
6114          * Currently, we are using two list alternately, while one is in query,
6115          * add the freed counter to the other list based on the pool query_gen
6116          * value. After query finishes, add counter the list to the global
6117          * container counter list. The list changes while query starts. In
6118          * this case, lock will not be needed as query callback and release
6119          * function both operate with the different list.
6120          */
6121         if (!priv->sh->cmng.counter_fallback) {
6122                 rte_spinlock_lock(&pool->csl);
6123                 TAILQ_INSERT_TAIL(&pool->counters[pool->query_gen], cnt, next);
6124                 rte_spinlock_unlock(&pool->csl);
6125         } else {
6126                 cnt->dcs_when_free = cnt->dcs_when_active;
6127                 cnt_type = pool->is_aged ? MLX5_COUNTER_TYPE_AGE :
6128                                            MLX5_COUNTER_TYPE_ORIGIN;
6129                 rte_spinlock_lock(&priv->sh->cmng.csl[cnt_type]);
6130                 TAILQ_INSERT_TAIL(&priv->sh->cmng.counters[cnt_type],
6131                                   cnt, next);
6132                 rte_spinlock_unlock(&priv->sh->cmng.csl[cnt_type]);
6133         }
6134 }
6135
6136 /**
6137  * Resize a meter id container.
6138  *
6139  * @param[in] dev
6140  *   Pointer to the Ethernet device structure.
6141  *
6142  * @return
6143  *   0 on success, otherwise negative errno value and rte_errno is set.
6144  */
6145 static int
6146 flow_dv_mtr_container_resize(struct rte_eth_dev *dev)
6147 {
6148         struct mlx5_priv *priv = dev->data->dev_private;
6149         struct mlx5_aso_mtr_pools_mng *pools_mng =
6150                                 &priv->sh->mtrmng->pools_mng;
6151         void *old_pools = pools_mng->pools;
6152         uint32_t resize = pools_mng->n + MLX5_MTRS_CONTAINER_RESIZE;
6153         uint32_t mem_size = sizeof(struct mlx5_aso_mtr_pool *) * resize;
6154         void *pools = mlx5_malloc(MLX5_MEM_ZERO, mem_size, 0, SOCKET_ID_ANY);
6155
6156         if (!pools) {
6157                 rte_errno = ENOMEM;
6158                 return -ENOMEM;
6159         }
6160         if (!pools_mng->n)
6161                 if (mlx5_aso_queue_init(priv->sh, ASO_OPC_MOD_POLICER)) {
6162                         mlx5_free(pools);
6163                         return -ENOMEM;
6164                 }
6165         if (old_pools)
6166                 memcpy(pools, old_pools, pools_mng->n *
6167                                        sizeof(struct mlx5_aso_mtr_pool *));
6168         pools_mng->n = resize;
6169         pools_mng->pools = pools;
6170         if (old_pools)
6171                 mlx5_free(old_pools);
6172         return 0;
6173 }
6174
6175 /**
6176  * Prepare a new meter and/or a new meter pool.
6177  *
6178  * @param[in] dev
6179  *   Pointer to the Ethernet device structure.
6180  * @param[out] mtr_free
6181  *   Where to put the pointer of a new meter.g.
6182  *
6183  * @return
6184  *   The meter pool pointer and @mtr_free is set on success,
6185  *   NULL otherwise and rte_errno is set.
6186  */
6187 static struct mlx5_aso_mtr_pool *
6188 flow_dv_mtr_pool_create(struct rte_eth_dev *dev,
6189                              struct mlx5_aso_mtr **mtr_free)
6190 {
6191         struct mlx5_priv *priv = dev->data->dev_private;
6192         struct mlx5_aso_mtr_pools_mng *pools_mng =
6193                                 &priv->sh->mtrmng->pools_mng;
6194         struct mlx5_aso_mtr_pool *pool = NULL;
6195         struct mlx5_devx_obj *dcs = NULL;
6196         uint32_t i;
6197         uint32_t log_obj_size;
6198
6199         log_obj_size = rte_log2_u32(MLX5_ASO_MTRS_PER_POOL >> 1);
6200         dcs = mlx5_devx_cmd_create_flow_meter_aso_obj(priv->sh->ctx,
6201                         priv->sh->pdn, log_obj_size);
6202         if (!dcs) {
6203                 rte_errno = ENODATA;
6204                 return NULL;
6205         }
6206         pool = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*pool), 0, SOCKET_ID_ANY);
6207         if (!pool) {
6208                 rte_errno = ENOMEM;
6209                 claim_zero(mlx5_devx_cmd_destroy(dcs));
6210                 return NULL;
6211         }
6212         pool->devx_obj = dcs;
6213         pool->index = pools_mng->n_valid;
6214         if (pool->index == pools_mng->n && flow_dv_mtr_container_resize(dev)) {
6215                 mlx5_free(pool);
6216                 claim_zero(mlx5_devx_cmd_destroy(dcs));
6217                 return NULL;
6218         }
6219         pools_mng->pools[pool->index] = pool;
6220         pools_mng->n_valid++;
6221         for (i = 1; i < MLX5_ASO_MTRS_PER_POOL; ++i) {
6222                 pool->mtrs[i].offset = i;
6223                 LIST_INSERT_HEAD(&pools_mng->meters,
6224                                                 &pool->mtrs[i], next);
6225         }
6226         pool->mtrs[0].offset = 0;
6227         *mtr_free = &pool->mtrs[0];
6228         return pool;
6229 }
6230
6231 /**
6232  * Release a flow meter into pool.
6233  *
6234  * @param[in] dev
6235  *   Pointer to the Ethernet device structure.
6236  * @param[in] mtr_idx
6237  *   Index to aso flow meter.
6238  */
6239 static void
6240 flow_dv_aso_mtr_release_to_pool(struct rte_eth_dev *dev, uint32_t mtr_idx)
6241 {
6242         struct mlx5_priv *priv = dev->data->dev_private;
6243         struct mlx5_aso_mtr_pools_mng *pools_mng =
6244                                 &priv->sh->mtrmng->pools_mng;
6245         struct mlx5_aso_mtr *aso_mtr = mlx5_aso_meter_by_idx(priv, mtr_idx);
6246
6247         MLX5_ASSERT(aso_mtr);
6248         rte_spinlock_lock(&pools_mng->mtrsl);
6249         memset(&aso_mtr->fm, 0, sizeof(struct mlx5_flow_meter_info));
6250         aso_mtr->state = ASO_METER_FREE;
6251         LIST_INSERT_HEAD(&pools_mng->meters, aso_mtr, next);
6252         rte_spinlock_unlock(&pools_mng->mtrsl);
6253 }
6254
6255 /**
6256  * Allocate a aso flow meter.
6257  *
6258  * @param[in] dev
6259  *   Pointer to the Ethernet device structure.
6260  *
6261  * @return
6262  *   Index to aso flow meter on success, 0 otherwise and rte_errno is set.
6263  */
6264 static uint32_t
6265 flow_dv_mtr_alloc(struct rte_eth_dev *dev)
6266 {
6267         struct mlx5_priv *priv = dev->data->dev_private;
6268         struct mlx5_aso_mtr *mtr_free = NULL;
6269         struct mlx5_aso_mtr_pools_mng *pools_mng =
6270                                 &priv->sh->mtrmng->pools_mng;
6271         struct mlx5_aso_mtr_pool *pool;
6272         uint32_t mtr_idx = 0;
6273
6274         if (!priv->config.devx) {
6275                 rte_errno = ENOTSUP;
6276                 return 0;
6277         }
6278         /* Allocate the flow meter memory. */
6279         /* Get free meters from management. */
6280         rte_spinlock_lock(&pools_mng->mtrsl);
6281         mtr_free = LIST_FIRST(&pools_mng->meters);
6282         if (mtr_free)
6283                 LIST_REMOVE(mtr_free, next);
6284         if (!mtr_free && !flow_dv_mtr_pool_create(dev, &mtr_free)) {
6285                 rte_spinlock_unlock(&pools_mng->mtrsl);
6286                 return 0;
6287         }
6288         mtr_free->state = ASO_METER_WAIT;
6289         rte_spinlock_unlock(&pools_mng->mtrsl);
6290         pool = container_of(mtr_free,
6291                         struct mlx5_aso_mtr_pool,
6292                         mtrs[mtr_free->offset]);
6293         mtr_idx = MLX5_MAKE_MTR_IDX(pool->index, mtr_free->offset);
6294         if (!mtr_free->fm.meter_action) {
6295 #ifdef HAVE_MLX5_DR_CREATE_ACTION_ASO
6296                 struct rte_flow_error error;
6297                 uint8_t reg_id;
6298
6299                 reg_id = mlx5_flow_get_reg_id(dev, MLX5_MTR_COLOR, 0, &error);
6300                 mtr_free->fm.meter_action =
6301                         mlx5_glue->dv_create_flow_action_aso
6302                                                 (priv->sh->rx_domain,
6303                                                  pool->devx_obj->obj,
6304                                                  mtr_free->offset,
6305                                                  (1 << MLX5_FLOW_COLOR_GREEN),
6306                                                  reg_id - REG_C_0);
6307 #endif /* HAVE_MLX5_DR_CREATE_ACTION_ASO */
6308                 if (!mtr_free->fm.meter_action) {
6309                         flow_dv_aso_mtr_release_to_pool(dev, mtr_idx);
6310                         return 0;
6311                 }
6312         }
6313         return mtr_idx;
6314 }
6315
6316 /**
6317  * Verify the @p attributes will be correctly understood by the NIC and store
6318  * them in the @p flow if everything is correct.
6319  *
6320  * @param[in] dev
6321  *   Pointer to dev struct.
6322  * @param[in] attributes
6323  *   Pointer to flow attributes
6324  * @param[in] external
6325  *   This flow rule is created by request external to PMD.
6326  * @param[out] error
6327  *   Pointer to error structure.
6328  *
6329  * @return
6330  *   - 0 on success and non root table.
6331  *   - 1 on success and root table.
6332  *   - a negative errno value otherwise and rte_errno is set.
6333  */
6334 static int
6335 flow_dv_validate_attributes(struct rte_eth_dev *dev,
6336                             const struct mlx5_flow_tunnel *tunnel,
6337                             const struct rte_flow_attr *attributes,
6338                             const struct flow_grp_info *grp_info,
6339                             struct rte_flow_error *error)
6340 {
6341         struct mlx5_priv *priv = dev->data->dev_private;
6342         uint32_t lowest_priority = mlx5_get_lowest_priority(dev, attributes);
6343         int ret = 0;
6344
6345 #ifndef HAVE_MLX5DV_DR
6346         RTE_SET_USED(tunnel);
6347         RTE_SET_USED(grp_info);
6348         if (attributes->group)
6349                 return rte_flow_error_set(error, ENOTSUP,
6350                                           RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
6351                                           NULL,
6352                                           "groups are not supported");
6353 #else
6354         uint32_t table = 0;
6355
6356         ret = mlx5_flow_group_to_table(dev, tunnel, attributes->group, &table,
6357                                        grp_info, error);
6358         if (ret)
6359                 return ret;
6360         if (!table)
6361                 ret = MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL;
6362 #endif
6363         if (attributes->priority != MLX5_FLOW_LOWEST_PRIO_INDICATOR &&
6364             attributes->priority > lowest_priority)
6365                 return rte_flow_error_set(error, ENOTSUP,
6366                                           RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
6367                                           NULL,
6368                                           "priority out of range");
6369         if (attributes->transfer) {
6370                 if (!priv->config.dv_esw_en)
6371                         return rte_flow_error_set
6372                                 (error, ENOTSUP,
6373                                  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
6374                                  "E-Switch dr is not supported");
6375                 if (!(priv->representor || priv->master))
6376                         return rte_flow_error_set
6377                                 (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6378                                  NULL, "E-Switch configuration can only be"
6379                                  " done by a master or a representor device");
6380                 if (attributes->egress)
6381                         return rte_flow_error_set
6382                                 (error, ENOTSUP,
6383                                  RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, attributes,
6384                                  "egress is not supported");
6385         }
6386         if (!(attributes->egress ^ attributes->ingress))
6387                 return rte_flow_error_set(error, ENOTSUP,
6388                                           RTE_FLOW_ERROR_TYPE_ATTR, NULL,
6389                                           "must specify exactly one of "
6390                                           "ingress or egress");
6391         return ret;
6392 }
6393
6394 static uint16_t
6395 mlx5_flow_locate_proto_l3(const struct rte_flow_item **head,
6396                           const struct rte_flow_item *end)
6397 {
6398         const struct rte_flow_item *item = *head;
6399         uint16_t l3_protocol;
6400
6401         for (; item != end; item++) {
6402                 switch (item->type) {
6403                 default:
6404                         break;
6405                 case RTE_FLOW_ITEM_TYPE_IPV4:
6406                         l3_protocol = RTE_ETHER_TYPE_IPV4;
6407                         goto l3_ok;
6408                 case RTE_FLOW_ITEM_TYPE_IPV6:
6409                         l3_protocol = RTE_ETHER_TYPE_IPV6;
6410                         goto l3_ok;
6411                 case RTE_FLOW_ITEM_TYPE_ETH:
6412                         if (item->mask && item->spec) {
6413                                 MLX5_ETHER_TYPE_FROM_HEADER(rte_flow_item_eth,
6414                                                             type, item,
6415                                                             l3_protocol);
6416                                 if (l3_protocol == RTE_ETHER_TYPE_IPV4 ||
6417                                     l3_protocol == RTE_ETHER_TYPE_IPV6)
6418                                         goto l3_ok;
6419                         }
6420                         break;
6421                 case RTE_FLOW_ITEM_TYPE_VLAN:
6422                         if (item->mask && item->spec) {
6423                                 MLX5_ETHER_TYPE_FROM_HEADER(rte_flow_item_vlan,
6424                                                             inner_type, item,
6425                                                             l3_protocol);
6426                                 if (l3_protocol == RTE_ETHER_TYPE_IPV4 ||
6427                                     l3_protocol == RTE_ETHER_TYPE_IPV6)
6428                                         goto l3_ok;
6429                         }
6430                         break;
6431                 }
6432         }
6433         return 0;
6434 l3_ok:
6435         *head = item;
6436         return l3_protocol;
6437 }
6438
6439 static uint8_t
6440 mlx5_flow_locate_proto_l4(const struct rte_flow_item **head,
6441                           const struct rte_flow_item *end)
6442 {
6443         const struct rte_flow_item *item = *head;
6444         uint8_t l4_protocol;
6445
6446         for (; item != end; item++) {
6447                 switch (item->type) {
6448                 default:
6449                         break;
6450                 case RTE_FLOW_ITEM_TYPE_TCP:
6451                         l4_protocol = IPPROTO_TCP;
6452                         goto l4_ok;
6453                 case RTE_FLOW_ITEM_TYPE_UDP:
6454                         l4_protocol = IPPROTO_UDP;
6455                         goto l4_ok;
6456                 case RTE_FLOW_ITEM_TYPE_IPV4:
6457                         if (item->mask && item->spec) {
6458                                 const struct rte_flow_item_ipv4 *mask, *spec;
6459
6460                                 mask = (typeof(mask))item->mask;
6461                                 spec = (typeof(spec))item->spec;
6462                                 l4_protocol = mask->hdr.next_proto_id &
6463                                               spec->hdr.next_proto_id;
6464                                 if (l4_protocol == IPPROTO_TCP ||
6465                                     l4_protocol == IPPROTO_UDP)
6466                                         goto l4_ok;
6467                         }
6468                         break;
6469                 case RTE_FLOW_ITEM_TYPE_IPV6:
6470                         if (item->mask && item->spec) {
6471                                 const struct rte_flow_item_ipv6 *mask, *spec;
6472                                 mask = (typeof(mask))item->mask;
6473                                 spec = (typeof(spec))item->spec;
6474                                 l4_protocol = mask->hdr.proto & spec->hdr.proto;
6475                                 if (l4_protocol == IPPROTO_TCP ||
6476                                     l4_protocol == IPPROTO_UDP)
6477                                         goto l4_ok;
6478                         }
6479                         break;
6480                 }
6481         }
6482         return 0;
6483 l4_ok:
6484         *head = item;
6485         return l4_protocol;
6486 }
6487
6488 static int
6489 flow_dv_validate_item_integrity(struct rte_eth_dev *dev,
6490                                 const struct rte_flow_item *rule_items,
6491                                 const struct rte_flow_item *integrity_item,
6492                                 struct rte_flow_error *error)
6493 {
6494         struct mlx5_priv *priv = dev->data->dev_private;
6495         const struct rte_flow_item *tunnel_item, *end_item, *item = rule_items;
6496         const struct rte_flow_item_integrity *mask = (typeof(mask))
6497                                                      integrity_item->mask;
6498         const struct rte_flow_item_integrity *spec = (typeof(spec))
6499                                                      integrity_item->spec;
6500         uint32_t protocol;
6501
6502         if (!priv->config.hca_attr.pkt_integrity_match)
6503                 return rte_flow_error_set(error, ENOTSUP,
6504                                           RTE_FLOW_ERROR_TYPE_ITEM,
6505                                           integrity_item,
6506                                           "packet integrity integrity_item not supported");
6507         if (!mask)
6508                 mask = &rte_flow_item_integrity_mask;
6509         if (!mlx5_validate_integrity_item(mask))
6510                 return rte_flow_error_set(error, ENOTSUP,
6511                                           RTE_FLOW_ERROR_TYPE_ITEM,
6512                                           integrity_item,
6513                                           "unsupported integrity filter");
6514         tunnel_item = mlx5_flow_find_tunnel_item(rule_items);
6515         if (spec->level > 1) {
6516                 if (!tunnel_item)
6517                         return rte_flow_error_set(error, ENOTSUP,
6518                                                   RTE_FLOW_ERROR_TYPE_ITEM,
6519                                                   integrity_item,
6520                                                   "missing tunnel item");
6521                 item = tunnel_item;
6522                 end_item = mlx5_find_end_item(tunnel_item);
6523         } else {
6524                 end_item = tunnel_item ? tunnel_item :
6525                            mlx5_find_end_item(integrity_item);
6526         }
6527         if (mask->l3_ok || mask->ipv4_csum_ok) {
6528                 protocol = mlx5_flow_locate_proto_l3(&item, end_item);
6529                 if (!protocol)
6530                         return rte_flow_error_set(error, EINVAL,
6531                                                   RTE_FLOW_ERROR_TYPE_ITEM,
6532                                                   integrity_item,
6533                                                   "missing L3 protocol");
6534         }
6535         if (mask->l4_ok || mask->l4_csum_ok) {
6536                 protocol = mlx5_flow_locate_proto_l4(&item, end_item);
6537                 if (!protocol)
6538                         return rte_flow_error_set(error, EINVAL,
6539                                                   RTE_FLOW_ERROR_TYPE_ITEM,
6540                                                   integrity_item,
6541                                                   "missing L4 protocol");
6542         }
6543         return 0;
6544 }
6545
6546 /**
6547  * Internal validation function. For validating both actions and items.
6548  *
6549  * @param[in] dev
6550  *   Pointer to the rte_eth_dev structure.
6551  * @param[in] attr
6552  *   Pointer to the flow attributes.
6553  * @param[in] items
6554  *   Pointer to the list of items.
6555  * @param[in] actions
6556  *   Pointer to the list of actions.
6557  * @param[in] external
6558  *   This flow rule is created by request external to PMD.
6559  * @param[in] hairpin
6560  *   Number of hairpin TX actions, 0 means classic flow.
6561  * @param[out] error
6562  *   Pointer to the error structure.
6563  *
6564  * @return
6565  *   0 on success, a negative errno value otherwise and rte_errno is set.
6566  */
6567 static int
6568 flow_dv_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attr,
6569                  const struct rte_flow_item items[],
6570                  const struct rte_flow_action actions[],
6571                  bool external, int hairpin, struct rte_flow_error *error)
6572 {
6573         int ret;
6574         uint64_t action_flags = 0;
6575         uint64_t item_flags = 0;
6576         uint64_t last_item = 0;
6577         uint8_t next_protocol = 0xff;
6578         uint16_t ether_type = 0;
6579         int actions_n = 0;
6580         uint8_t item_ipv6_proto = 0;
6581         int fdb_mirror_limit = 0;
6582         int modify_after_mirror = 0;
6583         const struct rte_flow_item *geneve_item = NULL;
6584         const struct rte_flow_item *gre_item = NULL;
6585         const struct rte_flow_item *gtp_item = NULL;
6586         const struct rte_flow_action_raw_decap *decap;
6587         const struct rte_flow_action_raw_encap *encap;
6588         const struct rte_flow_action_rss *rss = NULL;
6589         const struct rte_flow_action_rss *sample_rss = NULL;
6590         const struct rte_flow_action_count *sample_count = NULL;
6591         const struct rte_flow_item_tcp nic_tcp_mask = {
6592                 .hdr = {
6593                         .tcp_flags = 0xFF,
6594                         .src_port = RTE_BE16(UINT16_MAX),
6595                         .dst_port = RTE_BE16(UINT16_MAX),
6596                 }
6597         };
6598         const struct rte_flow_item_ipv6 nic_ipv6_mask = {
6599                 .hdr = {
6600                         .src_addr =
6601                         "\xff\xff\xff\xff\xff\xff\xff\xff"
6602                         "\xff\xff\xff\xff\xff\xff\xff\xff",
6603                         .dst_addr =
6604                         "\xff\xff\xff\xff\xff\xff\xff\xff"
6605                         "\xff\xff\xff\xff\xff\xff\xff\xff",
6606                         .vtc_flow = RTE_BE32(0xffffffff),
6607                         .proto = 0xff,
6608                         .hop_limits = 0xff,
6609                 },
6610                 .has_frag_ext = 1,
6611         };
6612         const struct rte_flow_item_ecpri nic_ecpri_mask = {
6613                 .hdr = {
6614                         .common = {
6615                                 .u32 =
6616                                 RTE_BE32(((const struct rte_ecpri_common_hdr) {
6617                                         .type = 0xFF,
6618                                         }).u32),
6619                         },
6620                         .dummy[0] = 0xffffffff,
6621                 },
6622         };
6623         struct mlx5_priv *priv = dev->data->dev_private;
6624         struct mlx5_dev_config *dev_conf = &priv->config;
6625         uint16_t queue_index = 0xFFFF;
6626         const struct rte_flow_item_vlan *vlan_m = NULL;
6627         uint32_t rw_act_num = 0;
6628         uint64_t is_root;
6629         const struct mlx5_flow_tunnel *tunnel;
6630         enum mlx5_tof_rule_type tof_rule_type;
6631         struct flow_grp_info grp_info = {
6632                 .external = !!external,
6633                 .transfer = !!attr->transfer,
6634                 .fdb_def_rule = !!priv->fdb_def_rule,
6635                 .std_tbl_fix = true,
6636         };
6637         const struct rte_eth_hairpin_conf *conf;
6638         const struct rte_flow_item *rule_items = items;
6639         bool def_policy = false;
6640
6641         if (items == NULL)
6642                 return -1;
6643         tunnel = is_tunnel_offload_active(dev) ?
6644                  mlx5_get_tof(items, actions, &tof_rule_type) : NULL;
6645         if (tunnel) {
6646                 if (priv->representor)
6647                         return rte_flow_error_set
6648                                 (error, ENOTSUP,
6649                                  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6650                                  NULL, "decap not supported for VF representor");
6651                 if (tof_rule_type == MLX5_TUNNEL_OFFLOAD_SET_RULE)
6652                         action_flags |= MLX5_FLOW_ACTION_TUNNEL_SET;
6653                 else if (tof_rule_type == MLX5_TUNNEL_OFFLOAD_MATCH_RULE)
6654                         action_flags |= MLX5_FLOW_ACTION_TUNNEL_MATCH |
6655                                         MLX5_FLOW_ACTION_DECAP;
6656                 grp_info.std_tbl_fix = tunnel_use_standard_attr_group_translate
6657                                         (dev, attr, tunnel, tof_rule_type);
6658         }
6659         ret = flow_dv_validate_attributes(dev, tunnel, attr, &grp_info, error);
6660         if (ret < 0)
6661                 return ret;
6662         is_root = (uint64_t)ret;
6663         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
6664                 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
6665                 int type = items->type;
6666
6667                 if (!mlx5_flow_os_item_supported(type))
6668                         return rte_flow_error_set(error, ENOTSUP,
6669                                                   RTE_FLOW_ERROR_TYPE_ITEM,
6670                                                   NULL, "item not supported");
6671                 switch (type) {
6672                 case RTE_FLOW_ITEM_TYPE_VOID:
6673                         break;
6674                 case RTE_FLOW_ITEM_TYPE_PORT_ID:
6675                         ret = flow_dv_validate_item_port_id
6676                                         (dev, items, attr, item_flags, error);
6677                         if (ret < 0)
6678                                 return ret;
6679                         last_item = MLX5_FLOW_ITEM_PORT_ID;
6680                         break;
6681                 case RTE_FLOW_ITEM_TYPE_ETH:
6682                         ret = mlx5_flow_validate_item_eth(items, item_flags,
6683                                                           true, error);
6684                         if (ret < 0)
6685                                 return ret;
6686                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
6687                                              MLX5_FLOW_LAYER_OUTER_L2;
6688                         if (items->mask != NULL && items->spec != NULL) {
6689                                 ether_type =
6690                                         ((const struct rte_flow_item_eth *)
6691                                          items->spec)->type;
6692                                 ether_type &=
6693                                         ((const struct rte_flow_item_eth *)
6694                                          items->mask)->type;
6695                                 ether_type = rte_be_to_cpu_16(ether_type);
6696                         } else {
6697                                 ether_type = 0;
6698                         }
6699                         break;
6700                 case RTE_FLOW_ITEM_TYPE_VLAN:
6701                         ret = flow_dv_validate_item_vlan(items, item_flags,
6702                                                          dev, error);
6703                         if (ret < 0)
6704                                 return ret;
6705                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
6706                                              MLX5_FLOW_LAYER_OUTER_VLAN;
6707                         if (items->mask != NULL && items->spec != NULL) {
6708                                 ether_type =
6709                                         ((const struct rte_flow_item_vlan *)
6710                                          items->spec)->inner_type;
6711                                 ether_type &=
6712                                         ((const struct rte_flow_item_vlan *)
6713                                          items->mask)->inner_type;
6714                                 ether_type = rte_be_to_cpu_16(ether_type);
6715                         } else {
6716                                 ether_type = 0;
6717                         }
6718                         /* Store outer VLAN mask for of_push_vlan action. */
6719                         if (!tunnel)
6720                                 vlan_m = items->mask;
6721                         break;
6722                 case RTE_FLOW_ITEM_TYPE_IPV4:
6723                         mlx5_flow_tunnel_ip_check(items, next_protocol,
6724                                                   &item_flags, &tunnel);
6725                         ret = flow_dv_validate_item_ipv4(items, item_flags,
6726                                                          last_item, ether_type,
6727                                                          error);
6728                         if (ret < 0)
6729                                 return ret;
6730                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
6731                                              MLX5_FLOW_LAYER_OUTER_L3_IPV4;
6732                         if (items->mask != NULL &&
6733                             ((const struct rte_flow_item_ipv4 *)
6734                              items->mask)->hdr.next_proto_id) {
6735                                 next_protocol =
6736                                         ((const struct rte_flow_item_ipv4 *)
6737                                          (items->spec))->hdr.next_proto_id;
6738                                 next_protocol &=
6739                                         ((const struct rte_flow_item_ipv4 *)
6740                                          (items->mask))->hdr.next_proto_id;
6741                         } else {
6742                                 /* Reset for inner layer. */
6743                                 next_protocol = 0xff;
6744                         }
6745                         break;
6746                 case RTE_FLOW_ITEM_TYPE_IPV6:
6747                         mlx5_flow_tunnel_ip_check(items, next_protocol,
6748                                                   &item_flags, &tunnel);
6749                         ret = mlx5_flow_validate_item_ipv6(items, item_flags,
6750                                                            last_item,
6751                                                            ether_type,
6752                                                            &nic_ipv6_mask,
6753                                                            error);
6754                         if (ret < 0)
6755                                 return ret;
6756                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
6757                                              MLX5_FLOW_LAYER_OUTER_L3_IPV6;
6758                         if (items->mask != NULL &&
6759                             ((const struct rte_flow_item_ipv6 *)
6760                              items->mask)->hdr.proto) {
6761                                 item_ipv6_proto =
6762                                         ((const struct rte_flow_item_ipv6 *)
6763                                          items->spec)->hdr.proto;
6764                                 next_protocol =
6765                                         ((const struct rte_flow_item_ipv6 *)
6766                                          items->spec)->hdr.proto;
6767                                 next_protocol &=
6768                                         ((const struct rte_flow_item_ipv6 *)
6769                                          items->mask)->hdr.proto;
6770                         } else {
6771                                 /* Reset for inner layer. */
6772                                 next_protocol = 0xff;
6773                         }
6774                         break;
6775                 case RTE_FLOW_ITEM_TYPE_IPV6_FRAG_EXT:
6776                         ret = flow_dv_validate_item_ipv6_frag_ext(items,
6777                                                                   item_flags,
6778                                                                   error);
6779                         if (ret < 0)
6780                                 return ret;
6781                         last_item = tunnel ?
6782                                         MLX5_FLOW_LAYER_INNER_L3_IPV6_FRAG_EXT :
6783                                         MLX5_FLOW_LAYER_OUTER_L3_IPV6_FRAG_EXT;
6784                         if (items->mask != NULL &&
6785                             ((const struct rte_flow_item_ipv6_frag_ext *)
6786                              items->mask)->hdr.next_header) {
6787                                 next_protocol =
6788                                 ((const struct rte_flow_item_ipv6_frag_ext *)
6789                                  items->spec)->hdr.next_header;
6790                                 next_protocol &=
6791                                 ((const struct rte_flow_item_ipv6_frag_ext *)
6792                                  items->mask)->hdr.next_header;
6793                         } else {
6794                                 /* Reset for inner layer. */
6795                                 next_protocol = 0xff;
6796                         }
6797                         break;
6798                 case RTE_FLOW_ITEM_TYPE_TCP:
6799                         ret = mlx5_flow_validate_item_tcp
6800                                                 (items, item_flags,
6801                                                  next_protocol,
6802                                                  &nic_tcp_mask,
6803                                                  error);
6804                         if (ret < 0)
6805                                 return ret;
6806                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
6807                                              MLX5_FLOW_LAYER_OUTER_L4_TCP;
6808                         break;
6809                 case RTE_FLOW_ITEM_TYPE_UDP:
6810                         ret = mlx5_flow_validate_item_udp(items, item_flags,
6811                                                           next_protocol,
6812                                                           error);
6813                         if (ret < 0)
6814                                 return ret;
6815                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
6816                                              MLX5_FLOW_LAYER_OUTER_L4_UDP;
6817                         break;
6818                 case RTE_FLOW_ITEM_TYPE_GRE:
6819                         ret = mlx5_flow_validate_item_gre(items, item_flags,
6820                                                           next_protocol, error);
6821                         if (ret < 0)
6822                                 return ret;
6823                         gre_item = items;
6824                         last_item = MLX5_FLOW_LAYER_GRE;
6825                         break;
6826                 case RTE_FLOW_ITEM_TYPE_NVGRE:
6827                         ret = mlx5_flow_validate_item_nvgre(items, item_flags,
6828                                                             next_protocol,
6829                                                             error);
6830                         if (ret < 0)
6831                                 return ret;
6832                         last_item = MLX5_FLOW_LAYER_NVGRE;
6833                         break;
6834                 case RTE_FLOW_ITEM_TYPE_GRE_KEY:
6835                         ret = mlx5_flow_validate_item_gre_key
6836                                 (items, item_flags, gre_item, error);
6837                         if (ret < 0)
6838                                 return ret;
6839                         last_item = MLX5_FLOW_LAYER_GRE_KEY;
6840                         break;
6841                 case RTE_FLOW_ITEM_TYPE_VXLAN:
6842                         ret = mlx5_flow_validate_item_vxlan(items, item_flags,
6843                                                             error);
6844                         if (ret < 0)
6845                                 return ret;
6846                         last_item = MLX5_FLOW_LAYER_VXLAN;
6847                         break;
6848                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
6849                         ret = mlx5_flow_validate_item_vxlan_gpe(items,
6850                                                                 item_flags, dev,
6851                                                                 error);
6852                         if (ret < 0)
6853                                 return ret;
6854                         last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
6855                         break;
6856                 case RTE_FLOW_ITEM_TYPE_GENEVE:
6857                         ret = mlx5_flow_validate_item_geneve(items,
6858                                                              item_flags, dev,
6859                                                              error);
6860                         if (ret < 0)
6861                                 return ret;
6862                         geneve_item = items;
6863                         last_item = MLX5_FLOW_LAYER_GENEVE;
6864                         break;
6865                 case RTE_FLOW_ITEM_TYPE_GENEVE_OPT:
6866                         ret = mlx5_flow_validate_item_geneve_opt(items,
6867                                                                  last_item,
6868                                                                  geneve_item,
6869                                                                  dev,
6870                                                                  error);
6871                         if (ret < 0)
6872                                 return ret;
6873                         last_item = MLX5_FLOW_LAYER_GENEVE_OPT;
6874                         break;
6875                 case RTE_FLOW_ITEM_TYPE_MPLS:
6876                         ret = mlx5_flow_validate_item_mpls(dev, items,
6877                                                            item_flags,
6878                                                            last_item, error);
6879                         if (ret < 0)
6880                                 return ret;
6881                         last_item = MLX5_FLOW_LAYER_MPLS;
6882                         break;
6883
6884                 case RTE_FLOW_ITEM_TYPE_MARK:
6885                         ret = flow_dv_validate_item_mark(dev, items, attr,
6886                                                          error);
6887                         if (ret < 0)
6888                                 return ret;
6889                         last_item = MLX5_FLOW_ITEM_MARK;
6890                         break;
6891                 case RTE_FLOW_ITEM_TYPE_META:
6892                         ret = flow_dv_validate_item_meta(dev, items, attr,
6893                                                          error);
6894                         if (ret < 0)
6895                                 return ret;
6896                         last_item = MLX5_FLOW_ITEM_METADATA;
6897                         break;
6898                 case RTE_FLOW_ITEM_TYPE_ICMP:
6899                         ret = mlx5_flow_validate_item_icmp(items, item_flags,
6900                                                            next_protocol,
6901                                                            error);
6902                         if (ret < 0)
6903                                 return ret;
6904                         last_item = MLX5_FLOW_LAYER_ICMP;
6905                         break;
6906                 case RTE_FLOW_ITEM_TYPE_ICMP6:
6907                         ret = mlx5_flow_validate_item_icmp6(items, item_flags,
6908                                                             next_protocol,
6909                                                             error);
6910                         if (ret < 0)
6911                                 return ret;
6912                         item_ipv6_proto = IPPROTO_ICMPV6;
6913                         last_item = MLX5_FLOW_LAYER_ICMP6;
6914                         break;
6915                 case RTE_FLOW_ITEM_TYPE_TAG:
6916                         ret = flow_dv_validate_item_tag(dev, items,
6917                                                         attr, error);
6918                         if (ret < 0)
6919                                 return ret;
6920                         last_item = MLX5_FLOW_ITEM_TAG;
6921                         break;
6922                 case MLX5_RTE_FLOW_ITEM_TYPE_TAG:
6923                 case MLX5_RTE_FLOW_ITEM_TYPE_TX_QUEUE:
6924                         break;
6925                 case RTE_FLOW_ITEM_TYPE_GTP:
6926                         ret = flow_dv_validate_item_gtp(dev, items, item_flags,
6927                                                         error);
6928                         if (ret < 0)
6929                                 return ret;
6930                         gtp_item = items;
6931                         last_item = MLX5_FLOW_LAYER_GTP;
6932                         break;
6933                 case RTE_FLOW_ITEM_TYPE_GTP_PSC:
6934                         ret = flow_dv_validate_item_gtp_psc(items, last_item,
6935                                                             gtp_item, attr,
6936                                                             error);
6937                         if (ret < 0)
6938                                 return ret;
6939                         last_item = MLX5_FLOW_LAYER_GTP_PSC;
6940                         break;
6941                 case RTE_FLOW_ITEM_TYPE_ECPRI:
6942                         /* Capacity will be checked in the translate stage. */
6943                         ret = mlx5_flow_validate_item_ecpri(items, item_flags,
6944                                                             last_item,
6945                                                             ether_type,
6946                                                             &nic_ecpri_mask,
6947                                                             error);
6948                         if (ret < 0)
6949                                 return ret;
6950                         last_item = MLX5_FLOW_LAYER_ECPRI;
6951                         break;
6952                 case RTE_FLOW_ITEM_TYPE_INTEGRITY:
6953                         if (item_flags & MLX5_FLOW_ITEM_INTEGRITY)
6954                                 return rte_flow_error_set
6955                                         (error, ENOTSUP,
6956                                          RTE_FLOW_ERROR_TYPE_ITEM,
6957                                          NULL, "multiple integrity items not supported");
6958                         ret = flow_dv_validate_item_integrity(dev, rule_items,
6959                                                               items, error);
6960                         if (ret < 0)
6961                                 return ret;
6962                         last_item = MLX5_FLOW_ITEM_INTEGRITY;
6963                         break;
6964                 case RTE_FLOW_ITEM_TYPE_CONNTRACK:
6965                         ret = flow_dv_validate_item_aso_ct(dev, items,
6966                                                            &item_flags, error);
6967                         if (ret < 0)
6968                                 return ret;
6969                         break;
6970                 case MLX5_RTE_FLOW_ITEM_TYPE_TUNNEL:
6971                         /* tunnel offload item was processed before
6972                          * list it here as a supported type
6973                          */
6974                         break;
6975                 default:
6976                         return rte_flow_error_set(error, ENOTSUP,
6977                                                   RTE_FLOW_ERROR_TYPE_ITEM,
6978                                                   NULL, "item not supported");
6979                 }
6980                 item_flags |= last_item;
6981         }
6982         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
6983                 int type = actions->type;
6984                 bool shared_count = false;
6985
6986                 if (!mlx5_flow_os_action_supported(type))
6987                         return rte_flow_error_set(error, ENOTSUP,
6988                                                   RTE_FLOW_ERROR_TYPE_ACTION,
6989                                                   actions,
6990                                                   "action not supported");
6991                 if (actions_n == MLX5_DV_MAX_NUMBER_OF_ACTIONS)
6992                         return rte_flow_error_set(error, ENOTSUP,
6993                                                   RTE_FLOW_ERROR_TYPE_ACTION,
6994                                                   actions, "too many actions");
6995                 if (action_flags &
6996                         MLX5_FLOW_ACTION_METER_WITH_TERMINATED_POLICY)
6997                         return rte_flow_error_set(error, ENOTSUP,
6998                                 RTE_FLOW_ERROR_TYPE_ACTION,
6999                                 NULL, "meter action with policy "
7000                                 "must be the last action");
7001                 switch (type) {
7002                 case RTE_FLOW_ACTION_TYPE_VOID:
7003                         break;
7004                 case RTE_FLOW_ACTION_TYPE_PORT_ID:
7005                         ret = flow_dv_validate_action_port_id(dev,
7006                                                               action_flags,
7007                                                               actions,
7008                                                               attr,
7009                                                               error);
7010                         if (ret)
7011                                 return ret;
7012                         action_flags |= MLX5_FLOW_ACTION_PORT_ID;
7013                         ++actions_n;
7014                         break;
7015                 case RTE_FLOW_ACTION_TYPE_FLAG:
7016                         ret = flow_dv_validate_action_flag(dev, action_flags,
7017                                                            attr, error);
7018                         if (ret < 0)
7019                                 return ret;
7020                         if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
7021                                 /* Count all modify-header actions as one. */
7022                                 if (!(action_flags &
7023                                       MLX5_FLOW_MODIFY_HDR_ACTIONS))
7024                                         ++actions_n;
7025                                 action_flags |= MLX5_FLOW_ACTION_FLAG |
7026                                                 MLX5_FLOW_ACTION_MARK_EXT;
7027                                 if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7028                                         modify_after_mirror = 1;
7029
7030                         } else {
7031                                 action_flags |= MLX5_FLOW_ACTION_FLAG;
7032                                 ++actions_n;
7033                         }
7034                         rw_act_num += MLX5_ACT_NUM_SET_MARK;
7035                         break;
7036                 case RTE_FLOW_ACTION_TYPE_MARK:
7037                         ret = flow_dv_validate_action_mark(dev, actions,
7038                                                            action_flags,
7039                                                            attr, error);
7040                         if (ret < 0)
7041                                 return ret;
7042                         if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
7043                                 /* Count all modify-header actions as one. */
7044                                 if (!(action_flags &
7045                                       MLX5_FLOW_MODIFY_HDR_ACTIONS))
7046                                         ++actions_n;
7047                                 action_flags |= MLX5_FLOW_ACTION_MARK |
7048                                                 MLX5_FLOW_ACTION_MARK_EXT;
7049                                 if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7050                                         modify_after_mirror = 1;
7051                         } else {
7052                                 action_flags |= MLX5_FLOW_ACTION_MARK;
7053                                 ++actions_n;
7054                         }
7055                         rw_act_num += MLX5_ACT_NUM_SET_MARK;
7056                         break;
7057                 case RTE_FLOW_ACTION_TYPE_SET_META:
7058                         ret = flow_dv_validate_action_set_meta(dev, actions,
7059                                                                action_flags,
7060                                                                attr, error);
7061                         if (ret < 0)
7062                                 return ret;
7063                         /* Count all modify-header actions as one action. */
7064                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
7065                                 ++actions_n;
7066                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7067                                 modify_after_mirror = 1;
7068                         action_flags |= MLX5_FLOW_ACTION_SET_META;
7069                         rw_act_num += MLX5_ACT_NUM_SET_META;
7070                         break;
7071                 case RTE_FLOW_ACTION_TYPE_SET_TAG:
7072                         ret = flow_dv_validate_action_set_tag(dev, actions,
7073                                                               action_flags,
7074                                                               attr, error);
7075                         if (ret < 0)
7076                                 return ret;
7077                         /* Count all modify-header actions as one action. */
7078                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
7079                                 ++actions_n;
7080                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7081                                 modify_after_mirror = 1;
7082                         action_flags |= MLX5_FLOW_ACTION_SET_TAG;
7083                         rw_act_num += MLX5_ACT_NUM_SET_TAG;
7084                         break;
7085                 case RTE_FLOW_ACTION_TYPE_DROP:
7086                         ret = mlx5_flow_validate_action_drop(action_flags,
7087                                                              attr, error);
7088                         if (ret < 0)
7089                                 return ret;
7090                         action_flags |= MLX5_FLOW_ACTION_DROP;
7091                         ++actions_n;
7092                         break;
7093                 case RTE_FLOW_ACTION_TYPE_QUEUE:
7094                         ret = mlx5_flow_validate_action_queue(actions,
7095                                                               action_flags, dev,
7096                                                               attr, error);
7097                         if (ret < 0)
7098                                 return ret;
7099                         queue_index = ((const struct rte_flow_action_queue *)
7100                                                         (actions->conf))->index;
7101                         action_flags |= MLX5_FLOW_ACTION_QUEUE;
7102                         ++actions_n;
7103                         break;
7104                 case RTE_FLOW_ACTION_TYPE_RSS:
7105                         rss = actions->conf;
7106                         ret = mlx5_flow_validate_action_rss(actions,
7107                                                             action_flags, dev,
7108                                                             attr, item_flags,
7109                                                             error);
7110                         if (ret < 0)
7111                                 return ret;
7112                         if (rss && sample_rss &&
7113                             (sample_rss->level != rss->level ||
7114                             sample_rss->types != rss->types))
7115                                 return rte_flow_error_set(error, ENOTSUP,
7116                                         RTE_FLOW_ERROR_TYPE_ACTION,
7117                                         NULL,
7118                                         "Can't use the different RSS types "
7119                                         "or level in the same flow");
7120                         if (rss != NULL && rss->queue_num)
7121                                 queue_index = rss->queue[0];
7122                         action_flags |= MLX5_FLOW_ACTION_RSS;
7123                         ++actions_n;
7124                         break;
7125                 case MLX5_RTE_FLOW_ACTION_TYPE_DEFAULT_MISS:
7126                         ret =
7127                         mlx5_flow_validate_action_default_miss(action_flags,
7128                                         attr, error);
7129                         if (ret < 0)
7130                                 return ret;
7131                         action_flags |= MLX5_FLOW_ACTION_DEFAULT_MISS;
7132                         ++actions_n;
7133                         break;
7134                 case MLX5_RTE_FLOW_ACTION_TYPE_COUNT:
7135                 case RTE_FLOW_ACTION_TYPE_COUNT:
7136                         shared_count = is_shared_action_count(actions);
7137                         ret = flow_dv_validate_action_count(dev, shared_count,
7138                                                             action_flags,
7139                                                             error);
7140                         if (ret < 0)
7141                                 return ret;
7142                         action_flags |= MLX5_FLOW_ACTION_COUNT;
7143                         ++actions_n;
7144                         break;
7145                 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
7146                         if (flow_dv_validate_action_pop_vlan(dev,
7147                                                              action_flags,
7148                                                              actions,
7149                                                              item_flags, attr,
7150                                                              error))
7151                                 return -rte_errno;
7152                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7153                                 modify_after_mirror = 1;
7154                         action_flags |= MLX5_FLOW_ACTION_OF_POP_VLAN;
7155                         ++actions_n;
7156                         break;
7157                 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
7158                         ret = flow_dv_validate_action_push_vlan(dev,
7159                                                                 action_flags,
7160                                                                 vlan_m,
7161                                                                 actions, attr,
7162                                                                 error);
7163                         if (ret < 0)
7164                                 return ret;
7165                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7166                                 modify_after_mirror = 1;
7167                         action_flags |= MLX5_FLOW_ACTION_OF_PUSH_VLAN;
7168                         ++actions_n;
7169                         break;
7170                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
7171                         ret = flow_dv_validate_action_set_vlan_pcp
7172                                                 (action_flags, actions, error);
7173                         if (ret < 0)
7174                                 return ret;
7175                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7176                                 modify_after_mirror = 1;
7177                         /* Count PCP with push_vlan command. */
7178                         action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_PCP;
7179                         break;
7180                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
7181                         ret = flow_dv_validate_action_set_vlan_vid
7182                                                 (item_flags, action_flags,
7183                                                  actions, error);
7184                         if (ret < 0)
7185                                 return ret;
7186                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7187                                 modify_after_mirror = 1;
7188                         /* Count VID with push_vlan command. */
7189                         action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_VID;
7190                         rw_act_num += MLX5_ACT_NUM_MDF_VID;
7191                         break;
7192                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
7193                 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
7194                         ret = flow_dv_validate_action_l2_encap(dev,
7195                                                                action_flags,
7196                                                                actions, attr,
7197                                                                error);
7198                         if (ret < 0)
7199                                 return ret;
7200                         action_flags |= MLX5_FLOW_ACTION_ENCAP;
7201                         ++actions_n;
7202                         break;
7203                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
7204                 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
7205                         ret = flow_dv_validate_action_decap(dev, action_flags,
7206                                                             actions, item_flags,
7207                                                             attr, error);
7208                         if (ret < 0)
7209                                 return ret;
7210                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7211                                 modify_after_mirror = 1;
7212                         action_flags |= MLX5_FLOW_ACTION_DECAP;
7213                         ++actions_n;
7214                         break;
7215                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
7216                         ret = flow_dv_validate_action_raw_encap_decap
7217                                 (dev, NULL, actions->conf, attr, &action_flags,
7218                                  &actions_n, actions, item_flags, error);
7219                         if (ret < 0)
7220                                 return ret;
7221                         break;
7222                 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
7223                         decap = actions->conf;
7224                         while ((++actions)->type == RTE_FLOW_ACTION_TYPE_VOID)
7225                                 ;
7226                         if (actions->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
7227                                 encap = NULL;
7228                                 actions--;
7229                         } else {
7230                                 encap = actions->conf;
7231                         }
7232                         ret = flow_dv_validate_action_raw_encap_decap
7233                                            (dev,
7234                                             decap ? decap : &empty_decap, encap,
7235                                             attr, &action_flags, &actions_n,
7236                                             actions, item_flags, error);
7237                         if (ret < 0)
7238                                 return ret;
7239                         if ((action_flags & MLX5_FLOW_ACTION_SAMPLE) &&
7240                             (action_flags & MLX5_FLOW_ACTION_DECAP))
7241                                 modify_after_mirror = 1;
7242                         break;
7243                 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
7244                 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
7245                         ret = flow_dv_validate_action_modify_mac(action_flags,
7246                                                                  actions,
7247                                                                  item_flags,
7248                                                                  error);
7249                         if (ret < 0)
7250                                 return ret;
7251                         /* Count all modify-header actions as one action. */
7252                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
7253                                 ++actions_n;
7254                         action_flags |= actions->type ==
7255                                         RTE_FLOW_ACTION_TYPE_SET_MAC_SRC ?
7256                                                 MLX5_FLOW_ACTION_SET_MAC_SRC :
7257                                                 MLX5_FLOW_ACTION_SET_MAC_DST;
7258                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7259                                 modify_after_mirror = 1;
7260                         /*
7261                          * Even if the source and destination MAC addresses have
7262                          * overlap in the header with 4B alignment, the convert
7263                          * function will handle them separately and 4 SW actions
7264                          * will be created. And 2 actions will be added each
7265                          * time no matter how many bytes of address will be set.
7266                          */
7267                         rw_act_num += MLX5_ACT_NUM_MDF_MAC;
7268                         break;
7269                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
7270                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
7271                         ret = flow_dv_validate_action_modify_ipv4(action_flags,
7272                                                                   actions,
7273                                                                   item_flags,
7274                                                                   error);
7275                         if (ret < 0)
7276                                 return ret;
7277                         /* Count all modify-header actions as one action. */
7278                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
7279                                 ++actions_n;
7280                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7281                                 modify_after_mirror = 1;
7282                         action_flags |= actions->type ==
7283                                         RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC ?
7284                                                 MLX5_FLOW_ACTION_SET_IPV4_SRC :
7285                                                 MLX5_FLOW_ACTION_SET_IPV4_DST;
7286                         rw_act_num += MLX5_ACT_NUM_MDF_IPV4;
7287                         break;
7288                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
7289                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
7290                         ret = flow_dv_validate_action_modify_ipv6(action_flags,
7291                                                                   actions,
7292                                                                   item_flags,
7293                                                                   error);
7294                         if (ret < 0)
7295                                 return ret;
7296                         if (item_ipv6_proto == IPPROTO_ICMPV6)
7297                                 return rte_flow_error_set(error, ENOTSUP,
7298                                         RTE_FLOW_ERROR_TYPE_ACTION,
7299                                         actions,
7300                                         "Can't change header "
7301                                         "with ICMPv6 proto");
7302                         /* Count all modify-header actions as one action. */
7303                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
7304                                 ++actions_n;
7305                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7306                                 modify_after_mirror = 1;
7307                         action_flags |= actions->type ==
7308                                         RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC ?
7309                                                 MLX5_FLOW_ACTION_SET_IPV6_SRC :
7310                                                 MLX5_FLOW_ACTION_SET_IPV6_DST;
7311                         rw_act_num += MLX5_ACT_NUM_MDF_IPV6;
7312                         break;
7313                 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
7314                 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
7315                         ret = flow_dv_validate_action_modify_tp(action_flags,
7316                                                                 actions,
7317                                                                 item_flags,
7318                                                                 error);
7319                         if (ret < 0)
7320                                 return ret;
7321                         /* Count all modify-header actions as one action. */
7322                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
7323                                 ++actions_n;
7324                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7325                                 modify_after_mirror = 1;
7326                         action_flags |= actions->type ==
7327                                         RTE_FLOW_ACTION_TYPE_SET_TP_SRC ?
7328                                                 MLX5_FLOW_ACTION_SET_TP_SRC :
7329                                                 MLX5_FLOW_ACTION_SET_TP_DST;
7330                         rw_act_num += MLX5_ACT_NUM_MDF_PORT;
7331                         break;
7332                 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
7333                 case RTE_FLOW_ACTION_TYPE_SET_TTL:
7334                         ret = flow_dv_validate_action_modify_ttl(action_flags,
7335                                                                  actions,
7336                                                                  item_flags,
7337                                                                  error);
7338                         if (ret < 0)
7339                                 return ret;
7340                         /* Count all modify-header actions as one action. */
7341                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
7342                                 ++actions_n;
7343                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7344                                 modify_after_mirror = 1;
7345                         action_flags |= actions->type ==
7346                                         RTE_FLOW_ACTION_TYPE_SET_TTL ?
7347                                                 MLX5_FLOW_ACTION_SET_TTL :
7348                                                 MLX5_FLOW_ACTION_DEC_TTL;
7349                         rw_act_num += MLX5_ACT_NUM_MDF_TTL;
7350                         break;
7351                 case RTE_FLOW_ACTION_TYPE_JUMP:
7352                         ret = flow_dv_validate_action_jump(dev, tunnel, actions,
7353                                                            action_flags,
7354                                                            attr, external,
7355                                                            error);
7356                         if (ret)
7357                                 return ret;
7358                         if ((action_flags & MLX5_FLOW_ACTION_SAMPLE) &&
7359                             fdb_mirror_limit)
7360                                 return rte_flow_error_set(error, EINVAL,
7361                                                   RTE_FLOW_ERROR_TYPE_ACTION,
7362                                                   NULL,
7363                                                   "sample and jump action combination is not supported");
7364                         ++actions_n;
7365                         action_flags |= MLX5_FLOW_ACTION_JUMP;
7366                         break;
7367                 case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
7368                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
7369                         ret = flow_dv_validate_action_modify_tcp_seq
7370                                                                 (action_flags,
7371                                                                  actions,
7372                                                                  item_flags,
7373                                                                  error);
7374                         if (ret < 0)
7375                                 return ret;
7376                         /* Count all modify-header actions as one action. */
7377                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
7378                                 ++actions_n;
7379                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7380                                 modify_after_mirror = 1;
7381                         action_flags |= actions->type ==
7382                                         RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ ?
7383                                                 MLX5_FLOW_ACTION_INC_TCP_SEQ :
7384                                                 MLX5_FLOW_ACTION_DEC_TCP_SEQ;
7385                         rw_act_num += MLX5_ACT_NUM_MDF_TCPSEQ;
7386                         break;
7387                 case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
7388                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
7389                         ret = flow_dv_validate_action_modify_tcp_ack
7390                                                                 (action_flags,
7391                                                                  actions,
7392                                                                  item_flags,
7393                                                                  error);
7394                         if (ret < 0)
7395                                 return ret;
7396                         /* Count all modify-header actions as one action. */
7397                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
7398                                 ++actions_n;
7399                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7400                                 modify_after_mirror = 1;
7401                         action_flags |= actions->type ==
7402                                         RTE_FLOW_ACTION_TYPE_INC_TCP_ACK ?
7403                                                 MLX5_FLOW_ACTION_INC_TCP_ACK :
7404                                                 MLX5_FLOW_ACTION_DEC_TCP_ACK;
7405                         rw_act_num += MLX5_ACT_NUM_MDF_TCPACK;
7406                         break;
7407                 case MLX5_RTE_FLOW_ACTION_TYPE_MARK:
7408                         break;
7409                 case MLX5_RTE_FLOW_ACTION_TYPE_TAG:
7410                 case MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG:
7411                         rw_act_num += MLX5_ACT_NUM_SET_TAG;
7412                         break;
7413                 case RTE_FLOW_ACTION_TYPE_METER:
7414                         ret = mlx5_flow_validate_action_meter(dev,
7415                                                               action_flags,
7416                                                               actions, attr,
7417                                                               &def_policy,
7418                                                               error);
7419                         if (ret < 0)
7420                                 return ret;
7421                         action_flags |= MLX5_FLOW_ACTION_METER;
7422                         if (!def_policy)
7423                                 action_flags |=
7424                                 MLX5_FLOW_ACTION_METER_WITH_TERMINATED_POLICY;
7425                         ++actions_n;
7426                         /* Meter action will add one more TAG action. */
7427                         rw_act_num += MLX5_ACT_NUM_SET_TAG;
7428                         break;
7429                 case MLX5_RTE_FLOW_ACTION_TYPE_AGE:
7430                         if (!attr->transfer && !attr->group)
7431                                 return rte_flow_error_set(error, ENOTSUP,
7432                                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7433                                                                            NULL,
7434                           "Shared ASO age action is not supported for group 0");
7435                         if (action_flags & MLX5_FLOW_ACTION_AGE)
7436                                 return rte_flow_error_set
7437                                                   (error, EINVAL,
7438                                                    RTE_FLOW_ERROR_TYPE_ACTION,
7439                                                    NULL,
7440                                                    "duplicate age actions set");
7441                         action_flags |= MLX5_FLOW_ACTION_AGE;
7442                         ++actions_n;
7443                         break;
7444                 case RTE_FLOW_ACTION_TYPE_AGE:
7445                         ret = flow_dv_validate_action_age(action_flags,
7446                                                           actions, dev,
7447                                                           error);
7448                         if (ret < 0)
7449                                 return ret;
7450                         /*
7451                          * Validate the regular AGE action (using counter)
7452                          * mutual exclusion with share counter actions.
7453                          */
7454                         if (!priv->sh->flow_hit_aso_en) {
7455                                 if (shared_count)
7456                                         return rte_flow_error_set
7457                                                 (error, EINVAL,
7458                                                 RTE_FLOW_ERROR_TYPE_ACTION,
7459                                                 NULL,
7460                                                 "old age and shared count combination is not supported");
7461                                 if (sample_count)
7462                                         return rte_flow_error_set
7463                                                 (error, EINVAL,
7464                                                 RTE_FLOW_ERROR_TYPE_ACTION,
7465                                                 NULL,
7466                                                 "old age action and count must be in the same sub flow");
7467                         }
7468                         action_flags |= MLX5_FLOW_ACTION_AGE;
7469                         ++actions_n;
7470                         break;
7471                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP:
7472                         ret = flow_dv_validate_action_modify_ipv4_dscp
7473                                                          (action_flags,
7474                                                           actions,
7475                                                           item_flags,
7476                                                           error);
7477                         if (ret < 0)
7478                                 return ret;
7479                         /* Count all modify-header actions as one action. */
7480                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
7481                                 ++actions_n;
7482                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7483                                 modify_after_mirror = 1;
7484                         action_flags |= MLX5_FLOW_ACTION_SET_IPV4_DSCP;
7485                         rw_act_num += MLX5_ACT_NUM_SET_DSCP;
7486                         break;
7487                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP:
7488                         ret = flow_dv_validate_action_modify_ipv6_dscp
7489                                                                 (action_flags,
7490                                                                  actions,
7491                                                                  item_flags,
7492                                                                  error);
7493                         if (ret < 0)
7494                                 return ret;
7495                         /* Count all modify-header actions as one action. */
7496                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
7497                                 ++actions_n;
7498                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7499                                 modify_after_mirror = 1;
7500                         action_flags |= MLX5_FLOW_ACTION_SET_IPV6_DSCP;
7501                         rw_act_num += MLX5_ACT_NUM_SET_DSCP;
7502                         break;
7503                 case RTE_FLOW_ACTION_TYPE_SAMPLE:
7504                         ret = flow_dv_validate_action_sample(&action_flags,
7505                                                              actions, dev,
7506                                                              attr, item_flags,
7507                                                              rss, &sample_rss,
7508                                                              &sample_count,
7509                                                              &fdb_mirror_limit,
7510                                                              error);
7511                         if (ret < 0)
7512                                 return ret;
7513                         action_flags |= MLX5_FLOW_ACTION_SAMPLE;
7514                         ++actions_n;
7515                         break;
7516                 case RTE_FLOW_ACTION_TYPE_MODIFY_FIELD:
7517                         ret = flow_dv_validate_action_modify_field(dev,
7518                                                                    action_flags,
7519                                                                    actions,
7520                                                                    attr,
7521                                                                    error);
7522                         if (ret < 0)
7523                                 return ret;
7524                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7525                                 modify_after_mirror = 1;
7526                         /* Count all modify-header actions as one action. */
7527                         if (!(action_flags & MLX5_FLOW_ACTION_MODIFY_FIELD))
7528                                 ++actions_n;
7529                         action_flags |= MLX5_FLOW_ACTION_MODIFY_FIELD;
7530                         rw_act_num += ret;
7531                         break;
7532                 case RTE_FLOW_ACTION_TYPE_CONNTRACK:
7533                         ret = flow_dv_validate_action_aso_ct(dev, action_flags,
7534                                                              item_flags, attr,
7535                                                              error);
7536                         if (ret < 0)
7537                                 return ret;
7538                         action_flags |= MLX5_FLOW_ACTION_CT;
7539                         break;
7540                 case MLX5_RTE_FLOW_ACTION_TYPE_TUNNEL_SET:
7541                         /* tunnel offload action was processed before
7542                          * list it here as a supported type
7543                          */
7544                         break;
7545                 default:
7546                         return rte_flow_error_set(error, ENOTSUP,
7547                                                   RTE_FLOW_ERROR_TYPE_ACTION,
7548                                                   actions,
7549                                                   "action not supported");
7550                 }
7551         }
7552         /*
7553          * Validate actions in flow rules
7554          * - Explicit decap action is prohibited by the tunnel offload API.
7555          * - Drop action in tunnel steer rule is prohibited by the API.
7556          * - Application cannot use MARK action because it's value can mask
7557          *   tunnel default miss nitification.
7558          * - JUMP in tunnel match rule has no support in current PMD
7559          *   implementation.
7560          * - TAG & META are reserved for future uses.
7561          */
7562         if (action_flags & MLX5_FLOW_ACTION_TUNNEL_SET) {
7563                 uint64_t bad_actions_mask = MLX5_FLOW_ACTION_DECAP    |
7564                                             MLX5_FLOW_ACTION_MARK     |
7565                                             MLX5_FLOW_ACTION_SET_TAG  |
7566                                             MLX5_FLOW_ACTION_SET_META |
7567                                             MLX5_FLOW_ACTION_DROP;
7568
7569                 if (action_flags & bad_actions_mask)
7570                         return rte_flow_error_set
7571                                         (error, EINVAL,
7572                                         RTE_FLOW_ERROR_TYPE_ACTION, NULL,
7573                                         "Invalid RTE action in tunnel "
7574                                         "set decap rule");
7575                 if (!(action_flags & MLX5_FLOW_ACTION_JUMP))
7576                         return rte_flow_error_set
7577                                         (error, EINVAL,
7578                                         RTE_FLOW_ERROR_TYPE_ACTION, NULL,
7579                                         "tunnel set decap rule must terminate "
7580                                         "with JUMP");
7581                 if (!attr->ingress)
7582                         return rte_flow_error_set
7583                                         (error, EINVAL,
7584                                         RTE_FLOW_ERROR_TYPE_ACTION, NULL,
7585                                         "tunnel flows for ingress traffic only");
7586         }
7587         if (action_flags & MLX5_FLOW_ACTION_TUNNEL_MATCH) {
7588                 uint64_t bad_actions_mask = MLX5_FLOW_ACTION_JUMP    |
7589                                             MLX5_FLOW_ACTION_MARK    |
7590                                             MLX5_FLOW_ACTION_SET_TAG |
7591                                             MLX5_FLOW_ACTION_SET_META;
7592
7593                 if (action_flags & bad_actions_mask)
7594                         return rte_flow_error_set
7595                                         (error, EINVAL,
7596                                         RTE_FLOW_ERROR_TYPE_ACTION, NULL,
7597                                         "Invalid RTE action in tunnel "
7598                                         "set match rule");
7599         }
7600         /*
7601          * Validate the drop action mutual exclusion with other actions.
7602          * Drop action is mutually-exclusive with any other action, except for
7603          * Count action.
7604          * Drop action compatibility with tunnel offload was already validated.
7605          */
7606         if (action_flags & (MLX5_FLOW_ACTION_TUNNEL_MATCH |
7607                             MLX5_FLOW_ACTION_TUNNEL_MATCH));
7608         else if ((action_flags & MLX5_FLOW_ACTION_DROP) &&
7609             (action_flags & ~(MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_COUNT)))
7610                 return rte_flow_error_set(error, EINVAL,
7611                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
7612                                           "Drop action is mutually-exclusive "
7613                                           "with any other action, except for "
7614                                           "Count action");
7615         /* Eswitch has few restrictions on using items and actions */
7616         if (attr->transfer) {
7617                 if (!mlx5_flow_ext_mreg_supported(dev) &&
7618                     action_flags & MLX5_FLOW_ACTION_FLAG)
7619                         return rte_flow_error_set(error, ENOTSUP,
7620                                                   RTE_FLOW_ERROR_TYPE_ACTION,
7621                                                   NULL,
7622                                                   "unsupported action FLAG");
7623                 if (!mlx5_flow_ext_mreg_supported(dev) &&
7624                     action_flags & MLX5_FLOW_ACTION_MARK)
7625                         return rte_flow_error_set(error, ENOTSUP,
7626                                                   RTE_FLOW_ERROR_TYPE_ACTION,
7627                                                   NULL,
7628                                                   "unsupported action MARK");
7629                 if (action_flags & MLX5_FLOW_ACTION_QUEUE)
7630                         return rte_flow_error_set(error, ENOTSUP,
7631                                                   RTE_FLOW_ERROR_TYPE_ACTION,
7632                                                   NULL,
7633                                                   "unsupported action QUEUE");
7634                 if (action_flags & MLX5_FLOW_ACTION_RSS)
7635                         return rte_flow_error_set(error, ENOTSUP,
7636                                                   RTE_FLOW_ERROR_TYPE_ACTION,
7637                                                   NULL,
7638                                                   "unsupported action RSS");
7639                 if (!(action_flags & MLX5_FLOW_FATE_ESWITCH_ACTIONS))
7640                         return rte_flow_error_set(error, EINVAL,
7641                                                   RTE_FLOW_ERROR_TYPE_ACTION,
7642                                                   actions,
7643                                                   "no fate action is found");
7644         } else {
7645                 if (!(action_flags & MLX5_FLOW_FATE_ACTIONS) && attr->ingress)
7646                         return rte_flow_error_set(error, EINVAL,
7647                                                   RTE_FLOW_ERROR_TYPE_ACTION,
7648                                                   actions,
7649                                                   "no fate action is found");
7650         }
7651         /*
7652          * Continue validation for Xcap and VLAN actions.
7653          * If hairpin is working in explicit TX rule mode, there is no actions
7654          * splitting and the validation of hairpin ingress flow should be the
7655          * same as other standard flows.
7656          */
7657         if ((action_flags & (MLX5_FLOW_XCAP_ACTIONS |
7658                              MLX5_FLOW_VLAN_ACTIONS)) &&
7659             (queue_index == 0xFFFF ||
7660              mlx5_rxq_get_type(dev, queue_index) != MLX5_RXQ_TYPE_HAIRPIN ||
7661              ((conf = mlx5_rxq_get_hairpin_conf(dev, queue_index)) != NULL &&
7662              conf->tx_explicit != 0))) {
7663                 if ((action_flags & MLX5_FLOW_XCAP_ACTIONS) ==
7664                     MLX5_FLOW_XCAP_ACTIONS)
7665                         return rte_flow_error_set(error, ENOTSUP,
7666                                                   RTE_FLOW_ERROR_TYPE_ACTION,
7667                                                   NULL, "encap and decap "
7668                                                   "combination aren't supported");
7669                 if (!attr->transfer && attr->ingress) {
7670                         if (action_flags & MLX5_FLOW_ACTION_ENCAP)
7671                                 return rte_flow_error_set
7672                                                 (error, ENOTSUP,
7673                                                  RTE_FLOW_ERROR_TYPE_ACTION,
7674                                                  NULL, "encap is not supported"
7675                                                  " for ingress traffic");
7676                         else if (action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN)
7677                                 return rte_flow_error_set
7678                                                 (error, ENOTSUP,
7679                                                  RTE_FLOW_ERROR_TYPE_ACTION,
7680                                                  NULL, "push VLAN action not "
7681                                                  "supported for ingress");
7682                         else if ((action_flags & MLX5_FLOW_VLAN_ACTIONS) ==
7683                                         MLX5_FLOW_VLAN_ACTIONS)
7684                                 return rte_flow_error_set
7685                                                 (error, ENOTSUP,
7686                                                  RTE_FLOW_ERROR_TYPE_ACTION,
7687                                                  NULL, "no support for "
7688                                                  "multiple VLAN actions");
7689                 }
7690         }
7691         if (action_flags & MLX5_FLOW_ACTION_METER_WITH_TERMINATED_POLICY) {
7692                 if ((action_flags & (MLX5_FLOW_FATE_ACTIONS &
7693                         ~MLX5_FLOW_ACTION_METER_WITH_TERMINATED_POLICY)) &&
7694                         attr->ingress)
7695                         return rte_flow_error_set
7696                                 (error, ENOTSUP,
7697                                 RTE_FLOW_ERROR_TYPE_ACTION,
7698                                 NULL, "fate action not supported for "
7699                                 "meter with policy");
7700                 if (attr->egress) {
7701                         if (action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS)
7702                                 return rte_flow_error_set
7703                                         (error, ENOTSUP,
7704                                         RTE_FLOW_ERROR_TYPE_ACTION,
7705                                         NULL, "modify header action in egress "
7706                                         "cannot be done before meter action");
7707                         if (action_flags & MLX5_FLOW_ACTION_ENCAP)
7708                                 return rte_flow_error_set
7709                                         (error, ENOTSUP,
7710                                         RTE_FLOW_ERROR_TYPE_ACTION,
7711                                         NULL, "encap action in egress "
7712                                         "cannot be done before meter action");
7713                         if (action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN)
7714                                 return rte_flow_error_set
7715                                         (error, ENOTSUP,
7716                                         RTE_FLOW_ERROR_TYPE_ACTION,
7717                                         NULL, "push vlan action in egress "
7718                                         "cannot be done before meter action");
7719                 }
7720         }
7721         /*
7722          * Hairpin flow will add one more TAG action in TX implicit mode.
7723          * In TX explicit mode, there will be no hairpin flow ID.
7724          */
7725         if (hairpin > 0)
7726                 rw_act_num += MLX5_ACT_NUM_SET_TAG;
7727         /* extra metadata enabled: one more TAG action will be add. */
7728         if (dev_conf->dv_flow_en &&
7729             dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
7730             mlx5_flow_ext_mreg_supported(dev))
7731                 rw_act_num += MLX5_ACT_NUM_SET_TAG;
7732         if (rw_act_num >
7733                         flow_dv_modify_hdr_action_max(dev, is_root)) {
7734                 return rte_flow_error_set(error, ENOTSUP,
7735                                           RTE_FLOW_ERROR_TYPE_ACTION,
7736                                           NULL, "too many header modify"
7737                                           " actions to support");
7738         }
7739         /* Eswitch egress mirror and modify flow has limitation on CX5 */
7740         if (fdb_mirror_limit && modify_after_mirror)
7741                 return rte_flow_error_set(error, EINVAL,
7742                                 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
7743                                 "sample before modify action is not supported");
7744         return 0;
7745 }
7746
7747 /**
7748  * Internal preparation function. Allocates the DV flow size,
7749  * this size is constant.
7750  *
7751  * @param[in] dev
7752  *   Pointer to the rte_eth_dev structure.
7753  * @param[in] attr
7754  *   Pointer to the flow attributes.
7755  * @param[in] items
7756  *   Pointer to the list of items.
7757  * @param[in] actions
7758  *   Pointer to the list of actions.
7759  * @param[out] error
7760  *   Pointer to the error structure.
7761  *
7762  * @return
7763  *   Pointer to mlx5_flow object on success,
7764  *   otherwise NULL and rte_errno is set.
7765  */
7766 static struct mlx5_flow *
7767 flow_dv_prepare(struct rte_eth_dev *dev,
7768                 const struct rte_flow_attr *attr __rte_unused,
7769                 const struct rte_flow_item items[] __rte_unused,
7770                 const struct rte_flow_action actions[] __rte_unused,
7771                 struct rte_flow_error *error)
7772 {
7773         uint32_t handle_idx = 0;
7774         struct mlx5_flow *dev_flow;
7775         struct mlx5_flow_handle *dev_handle;
7776         struct mlx5_priv *priv = dev->data->dev_private;
7777         struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
7778
7779         MLX5_ASSERT(wks);
7780         wks->skip_matcher_reg = 0;
7781         /* In case of corrupting the memory. */
7782         if (wks->flow_idx >= MLX5_NUM_MAX_DEV_FLOWS) {
7783                 rte_flow_error_set(error, ENOSPC,
7784                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
7785                                    "not free temporary device flow");
7786                 return NULL;
7787         }
7788         dev_handle = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
7789                                    &handle_idx);
7790         if (!dev_handle) {
7791                 rte_flow_error_set(error, ENOMEM,
7792                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
7793                                    "not enough memory to create flow handle");
7794                 return NULL;
7795         }
7796         MLX5_ASSERT(wks->flow_idx < RTE_DIM(wks->flows));
7797         dev_flow = &wks->flows[wks->flow_idx++];
7798         memset(dev_flow, 0, sizeof(*dev_flow));
7799         dev_flow->handle = dev_handle;
7800         dev_flow->handle_idx = handle_idx;
7801         /*
7802          * In some old rdma-core releases, before continuing, a check of the
7803          * length of matching parameter will be done at first. It needs to use
7804          * the length without misc4 param. If the flow has misc4 support, then
7805          * the length needs to be adjusted accordingly. Each param member is
7806          * aligned with a 64B boundary naturally.
7807          */
7808         dev_flow->dv.value.size = MLX5_ST_SZ_BYTES(fte_match_param) -
7809                                   MLX5_ST_SZ_BYTES(fte_match_set_misc4);
7810         dev_flow->ingress = attr->ingress;
7811         dev_flow->dv.transfer = attr->transfer;
7812         return dev_flow;
7813 }
7814
7815 #ifdef RTE_LIBRTE_MLX5_DEBUG
7816 /**
7817  * Sanity check for match mask and value. Similar to check_valid_spec() in
7818  * kernel driver. If unmasked bit is present in value, it returns failure.
7819  *
7820  * @param match_mask
7821  *   pointer to match mask buffer.
7822  * @param match_value
7823  *   pointer to match value buffer.
7824  *
7825  * @return
7826  *   0 if valid, -EINVAL otherwise.
7827  */
7828 static int
7829 flow_dv_check_valid_spec(void *match_mask, void *match_value)
7830 {
7831         uint8_t *m = match_mask;
7832         uint8_t *v = match_value;
7833         unsigned int i;
7834
7835         for (i = 0; i < MLX5_ST_SZ_BYTES(fte_match_param); ++i) {
7836                 if (v[i] & ~m[i]) {
7837                         DRV_LOG(ERR,
7838                                 "match_value differs from match_criteria"
7839                                 " %p[%u] != %p[%u]",
7840                                 match_value, i, match_mask, i);
7841                         return -EINVAL;
7842                 }
7843         }
7844         return 0;
7845 }
7846 #endif
7847
7848 /**
7849  * Add match of ip_version.
7850  *
7851  * @param[in] group
7852  *   Flow group.
7853  * @param[in] headers_v
7854  *   Values header pointer.
7855  * @param[in] headers_m
7856  *   Masks header pointer.
7857  * @param[in] ip_version
7858  *   The IP version to set.
7859  */
7860 static inline void
7861 flow_dv_set_match_ip_version(uint32_t group,
7862                              void *headers_v,
7863                              void *headers_m,
7864                              uint8_t ip_version)
7865 {
7866         if (group == 0)
7867                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0xf);
7868         else
7869                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version,
7870                          ip_version);
7871         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_version, ip_version);
7872         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype, 0);
7873         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ethertype, 0);
7874 }
7875
7876 /**
7877  * Add Ethernet item to matcher and to the value.
7878  *
7879  * @param[in, out] matcher
7880  *   Flow matcher.
7881  * @param[in, out] key
7882  *   Flow matcher value.
7883  * @param[in] item
7884  *   Flow pattern to translate.
7885  * @param[in] inner
7886  *   Item is inner pattern.
7887  */
7888 static void
7889 flow_dv_translate_item_eth(void *matcher, void *key,
7890                            const struct rte_flow_item *item, int inner,
7891                            uint32_t group)
7892 {
7893         const struct rte_flow_item_eth *eth_m = item->mask;
7894         const struct rte_flow_item_eth *eth_v = item->spec;
7895         const struct rte_flow_item_eth nic_mask = {
7896                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
7897                 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
7898                 .type = RTE_BE16(0xffff),
7899                 .has_vlan = 0,
7900         };
7901         void *hdrs_m;
7902         void *hdrs_v;
7903         char *l24_v;
7904         unsigned int i;
7905
7906         if (!eth_v)
7907                 return;
7908         if (!eth_m)
7909                 eth_m = &nic_mask;
7910         if (inner) {
7911                 hdrs_m = MLX5_ADDR_OF(fte_match_param, matcher,
7912                                          inner_headers);
7913                 hdrs_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
7914         } else {
7915                 hdrs_m = MLX5_ADDR_OF(fte_match_param, matcher,
7916                                          outer_headers);
7917                 hdrs_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
7918         }
7919         memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, hdrs_m, dmac_47_16),
7920                &eth_m->dst, sizeof(eth_m->dst));
7921         /* The value must be in the range of the mask. */
7922         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, hdrs_v, dmac_47_16);
7923         for (i = 0; i < sizeof(eth_m->dst); ++i)
7924                 l24_v[i] = eth_m->dst.addr_bytes[i] & eth_v->dst.addr_bytes[i];
7925         memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, hdrs_m, smac_47_16),
7926                &eth_m->src, sizeof(eth_m->src));
7927         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, hdrs_v, smac_47_16);
7928         /* The value must be in the range of the mask. */
7929         for (i = 0; i < sizeof(eth_m->dst); ++i)
7930                 l24_v[i] = eth_m->src.addr_bytes[i] & eth_v->src.addr_bytes[i];
7931         /*
7932          * HW supports match on one Ethertype, the Ethertype following the last
7933          * VLAN tag of the packet (see PRM).
7934          * Set match on ethertype only if ETH header is not followed by VLAN.
7935          * HW is optimized for IPv4/IPv6. In such cases, avoid setting
7936          * ethertype, and use ip_version field instead.
7937          * eCPRI over Ether layer will use type value 0xAEFE.
7938          */
7939         if (eth_m->type == 0xFFFF) {
7940                 /* Set cvlan_tag mask for any single\multi\un-tagged case. */
7941                 MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, cvlan_tag, 1);
7942                 switch (eth_v->type) {
7943                 case RTE_BE16(RTE_ETHER_TYPE_VLAN):
7944                         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, cvlan_tag, 1);
7945                         return;
7946                 case RTE_BE16(RTE_ETHER_TYPE_QINQ):
7947                         MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, svlan_tag, 1);
7948                         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, svlan_tag, 1);
7949                         return;
7950                 case RTE_BE16(RTE_ETHER_TYPE_IPV4):
7951                         flow_dv_set_match_ip_version(group, hdrs_v, hdrs_m, 4);
7952                         return;
7953                 case RTE_BE16(RTE_ETHER_TYPE_IPV6):
7954                         flow_dv_set_match_ip_version(group, hdrs_v, hdrs_m, 6);
7955                         return;
7956                 default:
7957                         break;
7958                 }
7959         }
7960         if (eth_m->has_vlan) {
7961                 MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, cvlan_tag, 1);
7962                 if (eth_v->has_vlan) {
7963                         /*
7964                          * Here, when also has_more_vlan field in VLAN item is
7965                          * not set, only single-tagged packets will be matched.
7966                          */
7967                         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, cvlan_tag, 1);
7968                         return;
7969                 }
7970         }
7971         MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, ethertype,
7972                  rte_be_to_cpu_16(eth_m->type));
7973         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, hdrs_v, ethertype);
7974         *(uint16_t *)(l24_v) = eth_m->type & eth_v->type;
7975 }
7976
7977 /**
7978  * Add VLAN item to matcher and to the value.
7979  *
7980  * @param[in, out] dev_flow
7981  *   Flow descriptor.
7982  * @param[in, out] matcher
7983  *   Flow matcher.
7984  * @param[in, out] key
7985  *   Flow matcher value.
7986  * @param[in] item
7987  *   Flow pattern to translate.
7988  * @param[in] inner
7989  *   Item is inner pattern.
7990  */
7991 static void
7992 flow_dv_translate_item_vlan(struct mlx5_flow *dev_flow,
7993                             void *matcher, void *key,
7994                             const struct rte_flow_item *item,
7995                             int inner, uint32_t group)
7996 {
7997         const struct rte_flow_item_vlan *vlan_m = item->mask;
7998         const struct rte_flow_item_vlan *vlan_v = item->spec;
7999         void *hdrs_m;
8000         void *hdrs_v;
8001         uint16_t tci_m;
8002         uint16_t tci_v;
8003
8004         if (inner) {
8005                 hdrs_m = MLX5_ADDR_OF(fte_match_param, matcher,
8006                                          inner_headers);
8007                 hdrs_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
8008         } else {
8009                 hdrs_m = MLX5_ADDR_OF(fte_match_param, matcher,
8010                                          outer_headers);
8011                 hdrs_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
8012                 /*
8013                  * This is workaround, masks are not supported,
8014                  * and pre-validated.
8015                  */
8016                 if (vlan_v)
8017                         dev_flow->handle->vf_vlan.tag =
8018                                         rte_be_to_cpu_16(vlan_v->tci) & 0x0fff;
8019         }
8020         /*
8021          * When VLAN item exists in flow, mark packet as tagged,
8022          * even if TCI is not specified.
8023          */
8024         if (!MLX5_GET(fte_match_set_lyr_2_4, hdrs_v, svlan_tag)) {
8025                 MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, cvlan_tag, 1);
8026                 MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, cvlan_tag, 1);
8027         }
8028         if (!vlan_v)
8029                 return;
8030         if (!vlan_m)
8031                 vlan_m = &rte_flow_item_vlan_mask;
8032         tci_m = rte_be_to_cpu_16(vlan_m->tci);
8033         tci_v = rte_be_to_cpu_16(vlan_m->tci & vlan_v->tci);
8034         MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, first_vid, tci_m);
8035         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, first_vid, tci_v);
8036         MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, first_cfi, tci_m >> 12);
8037         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, first_cfi, tci_v >> 12);
8038         MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, first_prio, tci_m >> 13);
8039         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, first_prio, tci_v >> 13);
8040         /*
8041          * HW is optimized for IPv4/IPv6. In such cases, avoid setting
8042          * ethertype, and use ip_version field instead.
8043          */
8044         if (vlan_m->inner_type == 0xFFFF) {
8045                 switch (vlan_v->inner_type) {
8046                 case RTE_BE16(RTE_ETHER_TYPE_VLAN):
8047                         MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, svlan_tag, 1);
8048                         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, svlan_tag, 1);
8049                         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, cvlan_tag, 0);
8050                         return;
8051                 case RTE_BE16(RTE_ETHER_TYPE_IPV4):
8052                         flow_dv_set_match_ip_version(group, hdrs_v, hdrs_m, 4);
8053                         return;
8054                 case RTE_BE16(RTE_ETHER_TYPE_IPV6):
8055                         flow_dv_set_match_ip_version(group, hdrs_v, hdrs_m, 6);
8056                         return;
8057                 default:
8058                         break;
8059                 }
8060         }
8061         if (vlan_m->has_more_vlan && vlan_v->has_more_vlan) {
8062                 MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, svlan_tag, 1);
8063                 MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, svlan_tag, 1);
8064                 /* Only one vlan_tag bit can be set. */
8065                 MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, cvlan_tag, 0);
8066                 return;
8067         }
8068         MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, ethertype,
8069                  rte_be_to_cpu_16(vlan_m->inner_type));
8070         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, ethertype,
8071                  rte_be_to_cpu_16(vlan_m->inner_type & vlan_v->inner_type));
8072 }
8073
8074 /**
8075  * Add IPV4 item to matcher and to the value.
8076  *
8077  * @param[in, out] matcher
8078  *   Flow matcher.
8079  * @param[in, out] key
8080  *   Flow matcher value.
8081  * @param[in] item
8082  *   Flow pattern to translate.
8083  * @param[in] inner
8084  *   Item is inner pattern.
8085  * @param[in] group
8086  *   The group to insert the rule.
8087  */
8088 static void
8089 flow_dv_translate_item_ipv4(void *matcher, void *key,
8090                             const struct rte_flow_item *item,
8091                             int inner, uint32_t group)
8092 {
8093         const struct rte_flow_item_ipv4 *ipv4_m = item->mask;
8094         const struct rte_flow_item_ipv4 *ipv4_v = item->spec;
8095         const struct rte_flow_item_ipv4 nic_mask = {
8096                 .hdr = {
8097                         .src_addr = RTE_BE32(0xffffffff),
8098                         .dst_addr = RTE_BE32(0xffffffff),
8099                         .type_of_service = 0xff,
8100                         .next_proto_id = 0xff,
8101                         .time_to_live = 0xff,
8102                 },
8103         };
8104         void *headers_m;
8105         void *headers_v;
8106         char *l24_m;
8107         char *l24_v;
8108         uint8_t tos;
8109
8110         if (inner) {
8111                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8112                                          inner_headers);
8113                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
8114         } else {
8115                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8116                                          outer_headers);
8117                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
8118         }
8119         flow_dv_set_match_ip_version(group, headers_v, headers_m, 4);
8120         if (!ipv4_v)
8121                 return;
8122         if (!ipv4_m)
8123                 ipv4_m = &nic_mask;
8124         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
8125                              dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
8126         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
8127                              dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
8128         *(uint32_t *)l24_m = ipv4_m->hdr.dst_addr;
8129         *(uint32_t *)l24_v = ipv4_m->hdr.dst_addr & ipv4_v->hdr.dst_addr;
8130         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
8131                           src_ipv4_src_ipv6.ipv4_layout.ipv4);
8132         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
8133                           src_ipv4_src_ipv6.ipv4_layout.ipv4);
8134         *(uint32_t *)l24_m = ipv4_m->hdr.src_addr;
8135         *(uint32_t *)l24_v = ipv4_m->hdr.src_addr & ipv4_v->hdr.src_addr;
8136         tos = ipv4_m->hdr.type_of_service & ipv4_v->hdr.type_of_service;
8137         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn,
8138                  ipv4_m->hdr.type_of_service);
8139         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, tos);
8140         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp,
8141                  ipv4_m->hdr.type_of_service >> 2);
8142         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, tos >> 2);
8143         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
8144                  ipv4_m->hdr.next_proto_id);
8145         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
8146                  ipv4_v->hdr.next_proto_id & ipv4_m->hdr.next_proto_id);
8147         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ttl_hoplimit,
8148                  ipv4_m->hdr.time_to_live);
8149         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ttl_hoplimit,
8150                  ipv4_v->hdr.time_to_live & ipv4_m->hdr.time_to_live);
8151         MLX5_SET(fte_match_set_lyr_2_4, headers_m, frag,
8152                  !!(ipv4_m->hdr.fragment_offset));
8153         MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag,
8154                  !!(ipv4_v->hdr.fragment_offset & ipv4_m->hdr.fragment_offset));
8155 }
8156
8157 /**
8158  * Add IPV6 item to matcher and to the value.
8159  *
8160  * @param[in, out] matcher
8161  *   Flow matcher.
8162  * @param[in, out] key
8163  *   Flow matcher value.
8164  * @param[in] item
8165  *   Flow pattern to translate.
8166  * @param[in] inner
8167  *   Item is inner pattern.
8168  * @param[in] group
8169  *   The group to insert the rule.
8170  */
8171 static void
8172 flow_dv_translate_item_ipv6(void *matcher, void *key,
8173                             const struct rte_flow_item *item,
8174                             int inner, uint32_t group)
8175 {
8176         const struct rte_flow_item_ipv6 *ipv6_m = item->mask;
8177         const struct rte_flow_item_ipv6 *ipv6_v = item->spec;
8178         const struct rte_flow_item_ipv6 nic_mask = {
8179                 .hdr = {
8180                         .src_addr =
8181                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
8182                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
8183                         .dst_addr =
8184                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
8185                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
8186                         .vtc_flow = RTE_BE32(0xffffffff),
8187                         .proto = 0xff,
8188                         .hop_limits = 0xff,
8189                 },
8190         };
8191         void *headers_m;
8192         void *headers_v;
8193         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
8194         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
8195         char *l24_m;
8196         char *l24_v;
8197         uint32_t vtc_m;
8198         uint32_t vtc_v;
8199         int i;
8200         int size;
8201
8202         if (inner) {
8203                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8204                                          inner_headers);
8205                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
8206         } else {
8207                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8208                                          outer_headers);
8209                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
8210         }
8211         flow_dv_set_match_ip_version(group, headers_v, headers_m, 6);
8212         if (!ipv6_v)
8213                 return;
8214         if (!ipv6_m)
8215                 ipv6_m = &nic_mask;
8216         size = sizeof(ipv6_m->hdr.dst_addr);
8217         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
8218                              dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
8219         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
8220                              dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
8221         memcpy(l24_m, ipv6_m->hdr.dst_addr, size);
8222         for (i = 0; i < size; ++i)
8223                 l24_v[i] = l24_m[i] & ipv6_v->hdr.dst_addr[i];
8224         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
8225                              src_ipv4_src_ipv6.ipv6_layout.ipv6);
8226         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
8227                              src_ipv4_src_ipv6.ipv6_layout.ipv6);
8228         memcpy(l24_m, ipv6_m->hdr.src_addr, size);
8229         for (i = 0; i < size; ++i)
8230                 l24_v[i] = l24_m[i] & ipv6_v->hdr.src_addr[i];
8231         /* TOS. */
8232         vtc_m = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow);
8233         vtc_v = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow & ipv6_v->hdr.vtc_flow);
8234         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn, vtc_m >> 20);
8235         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, vtc_v >> 20);
8236         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp, vtc_m >> 22);
8237         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, vtc_v >> 22);
8238         /* Label. */
8239         if (inner) {
8240                 MLX5_SET(fte_match_set_misc, misc_m, inner_ipv6_flow_label,
8241                          vtc_m);
8242                 MLX5_SET(fte_match_set_misc, misc_v, inner_ipv6_flow_label,
8243                          vtc_v);
8244         } else {
8245                 MLX5_SET(fte_match_set_misc, misc_m, outer_ipv6_flow_label,
8246                          vtc_m);
8247                 MLX5_SET(fte_match_set_misc, misc_v, outer_ipv6_flow_label,
8248                          vtc_v);
8249         }
8250         /* Protocol. */
8251         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
8252                  ipv6_m->hdr.proto);
8253         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
8254                  ipv6_v->hdr.proto & ipv6_m->hdr.proto);
8255         /* Hop limit. */
8256         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ttl_hoplimit,
8257                  ipv6_m->hdr.hop_limits);
8258         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ttl_hoplimit,
8259                  ipv6_v->hdr.hop_limits & ipv6_m->hdr.hop_limits);
8260         MLX5_SET(fte_match_set_lyr_2_4, headers_m, frag,
8261                  !!(ipv6_m->has_frag_ext));
8262         MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag,
8263                  !!(ipv6_v->has_frag_ext & ipv6_m->has_frag_ext));
8264 }
8265
8266 /**
8267  * Add IPV6 fragment extension item to matcher and to the value.
8268  *
8269  * @param[in, out] matcher
8270  *   Flow matcher.
8271  * @param[in, out] key
8272  *   Flow matcher value.
8273  * @param[in] item
8274  *   Flow pattern to translate.
8275  * @param[in] inner
8276  *   Item is inner pattern.
8277  */
8278 static void
8279 flow_dv_translate_item_ipv6_frag_ext(void *matcher, void *key,
8280                                      const struct rte_flow_item *item,
8281                                      int inner)
8282 {
8283         const struct rte_flow_item_ipv6_frag_ext *ipv6_frag_ext_m = item->mask;
8284         const struct rte_flow_item_ipv6_frag_ext *ipv6_frag_ext_v = item->spec;
8285         const struct rte_flow_item_ipv6_frag_ext nic_mask = {
8286                 .hdr = {
8287                         .next_header = 0xff,
8288                         .frag_data = RTE_BE16(0xffff),
8289                 },
8290         };
8291         void *headers_m;
8292         void *headers_v;
8293
8294         if (inner) {
8295                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8296                                          inner_headers);
8297                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
8298         } else {
8299                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8300                                          outer_headers);
8301                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
8302         }
8303         /* IPv6 fragment extension item exists, so packet is IP fragment. */
8304         MLX5_SET(fte_match_set_lyr_2_4, headers_m, frag, 1);
8305         MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag, 1);
8306         if (!ipv6_frag_ext_v)
8307                 return;
8308         if (!ipv6_frag_ext_m)
8309                 ipv6_frag_ext_m = &nic_mask;
8310         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
8311                  ipv6_frag_ext_m->hdr.next_header);
8312         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
8313                  ipv6_frag_ext_v->hdr.next_header &
8314                  ipv6_frag_ext_m->hdr.next_header);
8315 }
8316
8317 /**
8318  * Add TCP item to matcher and to the value.
8319  *
8320  * @param[in, out] matcher
8321  *   Flow matcher.
8322  * @param[in, out] key
8323  *   Flow matcher value.
8324  * @param[in] item
8325  *   Flow pattern to translate.
8326  * @param[in] inner
8327  *   Item is inner pattern.
8328  */
8329 static void
8330 flow_dv_translate_item_tcp(void *matcher, void *key,
8331                            const struct rte_flow_item *item,
8332                            int inner)
8333 {
8334         const struct rte_flow_item_tcp *tcp_m = item->mask;
8335         const struct rte_flow_item_tcp *tcp_v = item->spec;
8336         void *headers_m;
8337         void *headers_v;
8338
8339         if (inner) {
8340                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8341                                          inner_headers);
8342                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
8343         } else {
8344                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8345                                          outer_headers);
8346                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
8347         }
8348         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
8349         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_TCP);
8350         if (!tcp_v)
8351                 return;
8352         if (!tcp_m)
8353                 tcp_m = &rte_flow_item_tcp_mask;
8354         MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_sport,
8355                  rte_be_to_cpu_16(tcp_m->hdr.src_port));
8356         MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
8357                  rte_be_to_cpu_16(tcp_v->hdr.src_port & tcp_m->hdr.src_port));
8358         MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_dport,
8359                  rte_be_to_cpu_16(tcp_m->hdr.dst_port));
8360         MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
8361                  rte_be_to_cpu_16(tcp_v->hdr.dst_port & tcp_m->hdr.dst_port));
8362         MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_flags,
8363                  tcp_m->hdr.tcp_flags);
8364         MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_flags,
8365                  (tcp_v->hdr.tcp_flags & tcp_m->hdr.tcp_flags));
8366 }
8367
8368 /**
8369  * Add UDP item to matcher and to the value.
8370  *
8371  * @param[in, out] matcher
8372  *   Flow matcher.
8373  * @param[in, out] key
8374  *   Flow matcher value.
8375  * @param[in] item
8376  *   Flow pattern to translate.
8377  * @param[in] inner
8378  *   Item is inner pattern.
8379  */
8380 static void
8381 flow_dv_translate_item_udp(void *matcher, void *key,
8382                            const struct rte_flow_item *item,
8383                            int inner)
8384 {
8385         const struct rte_flow_item_udp *udp_m = item->mask;
8386         const struct rte_flow_item_udp *udp_v = item->spec;
8387         void *headers_m;
8388         void *headers_v;
8389
8390         if (inner) {
8391                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8392                                          inner_headers);
8393                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
8394         } else {
8395                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8396                                          outer_headers);
8397                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
8398         }
8399         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
8400         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_UDP);
8401         if (!udp_v)
8402                 return;
8403         if (!udp_m)
8404                 udp_m = &rte_flow_item_udp_mask;
8405         MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_sport,
8406                  rte_be_to_cpu_16(udp_m->hdr.src_port));
8407         MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
8408                  rte_be_to_cpu_16(udp_v->hdr.src_port & udp_m->hdr.src_port));
8409         MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport,
8410                  rte_be_to_cpu_16(udp_m->hdr.dst_port));
8411         MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
8412                  rte_be_to_cpu_16(udp_v->hdr.dst_port & udp_m->hdr.dst_port));
8413 }
8414
8415 /**
8416  * Add GRE optional Key item to matcher and to the value.
8417  *
8418  * @param[in, out] matcher
8419  *   Flow matcher.
8420  * @param[in, out] key
8421  *   Flow matcher value.
8422  * @param[in] item
8423  *   Flow pattern to translate.
8424  * @param[in] inner
8425  *   Item is inner pattern.
8426  */
8427 static void
8428 flow_dv_translate_item_gre_key(void *matcher, void *key,
8429                                    const struct rte_flow_item *item)
8430 {
8431         const rte_be32_t *key_m = item->mask;
8432         const rte_be32_t *key_v = item->spec;
8433         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
8434         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
8435         rte_be32_t gre_key_default_mask = RTE_BE32(UINT32_MAX);
8436
8437         /* GRE K bit must be on and should already be validated */
8438         MLX5_SET(fte_match_set_misc, misc_m, gre_k_present, 1);
8439         MLX5_SET(fte_match_set_misc, misc_v, gre_k_present, 1);
8440         if (!key_v)
8441                 return;
8442         if (!key_m)
8443                 key_m = &gre_key_default_mask;
8444         MLX5_SET(fte_match_set_misc, misc_m, gre_key_h,
8445                  rte_be_to_cpu_32(*key_m) >> 8);
8446         MLX5_SET(fte_match_set_misc, misc_v, gre_key_h,
8447                  rte_be_to_cpu_32((*key_v) & (*key_m)) >> 8);
8448         MLX5_SET(fte_match_set_misc, misc_m, gre_key_l,
8449                  rte_be_to_cpu_32(*key_m) & 0xFF);
8450         MLX5_SET(fte_match_set_misc, misc_v, gre_key_l,
8451                  rte_be_to_cpu_32((*key_v) & (*key_m)) & 0xFF);
8452 }
8453
8454 /**
8455  * Add GRE item to matcher and to the value.
8456  *
8457  * @param[in, out] matcher
8458  *   Flow matcher.
8459  * @param[in, out] key
8460  *   Flow matcher value.
8461  * @param[in] item
8462  *   Flow pattern to translate.
8463  * @param[in] inner
8464  *   Item is inner pattern.
8465  */
8466 static void
8467 flow_dv_translate_item_gre(void *matcher, void *key,
8468                            const struct rte_flow_item *item,
8469                            int inner)
8470 {
8471         const struct rte_flow_item_gre *gre_m = item->mask;
8472         const struct rte_flow_item_gre *gre_v = item->spec;
8473         void *headers_m;
8474         void *headers_v;
8475         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
8476         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
8477         struct {
8478                 union {
8479                         __extension__
8480                         struct {
8481                                 uint16_t version:3;
8482                                 uint16_t rsvd0:9;
8483                                 uint16_t s_present:1;
8484                                 uint16_t k_present:1;
8485                                 uint16_t rsvd_bit1:1;
8486                                 uint16_t c_present:1;
8487                         };
8488                         uint16_t value;
8489                 };
8490         } gre_crks_rsvd0_ver_m, gre_crks_rsvd0_ver_v;
8491
8492         if (inner) {
8493                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8494                                          inner_headers);
8495                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
8496         } else {
8497                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8498                                          outer_headers);
8499                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
8500         }
8501         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
8502         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_GRE);
8503         if (!gre_v)
8504                 return;
8505         if (!gre_m)
8506                 gre_m = &rte_flow_item_gre_mask;
8507         MLX5_SET(fte_match_set_misc, misc_m, gre_protocol,
8508                  rte_be_to_cpu_16(gre_m->protocol));
8509         MLX5_SET(fte_match_set_misc, misc_v, gre_protocol,
8510                  rte_be_to_cpu_16(gre_v->protocol & gre_m->protocol));
8511         gre_crks_rsvd0_ver_m.value = rte_be_to_cpu_16(gre_m->c_rsvd0_ver);
8512         gre_crks_rsvd0_ver_v.value = rte_be_to_cpu_16(gre_v->c_rsvd0_ver);
8513         MLX5_SET(fte_match_set_misc, misc_m, gre_c_present,
8514                  gre_crks_rsvd0_ver_m.c_present);
8515         MLX5_SET(fte_match_set_misc, misc_v, gre_c_present,
8516                  gre_crks_rsvd0_ver_v.c_present &
8517                  gre_crks_rsvd0_ver_m.c_present);
8518         MLX5_SET(fte_match_set_misc, misc_m, gre_k_present,
8519                  gre_crks_rsvd0_ver_m.k_present);
8520         MLX5_SET(fte_match_set_misc, misc_v, gre_k_present,
8521                  gre_crks_rsvd0_ver_v.k_present &
8522                  gre_crks_rsvd0_ver_m.k_present);
8523         MLX5_SET(fte_match_set_misc, misc_m, gre_s_present,
8524                  gre_crks_rsvd0_ver_m.s_present);
8525         MLX5_SET(fte_match_set_misc, misc_v, gre_s_present,
8526                  gre_crks_rsvd0_ver_v.s_present &
8527                  gre_crks_rsvd0_ver_m.s_present);
8528 }
8529
8530 /**
8531  * Add NVGRE item to matcher and to the value.
8532  *
8533  * @param[in, out] matcher
8534  *   Flow matcher.
8535  * @param[in, out] key
8536  *   Flow matcher value.
8537  * @param[in] item
8538  *   Flow pattern to translate.
8539  * @param[in] inner
8540  *   Item is inner pattern.
8541  */
8542 static void
8543 flow_dv_translate_item_nvgre(void *matcher, void *key,
8544                              const struct rte_flow_item *item,
8545                              int inner)
8546 {
8547         const struct rte_flow_item_nvgre *nvgre_m = item->mask;
8548         const struct rte_flow_item_nvgre *nvgre_v = item->spec;
8549         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
8550         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
8551         const char *tni_flow_id_m;
8552         const char *tni_flow_id_v;
8553         char *gre_key_m;
8554         char *gre_key_v;
8555         int size;
8556         int i;
8557
8558         /* For NVGRE, GRE header fields must be set with defined values. */
8559         const struct rte_flow_item_gre gre_spec = {
8560                 .c_rsvd0_ver = RTE_BE16(0x2000),
8561                 .protocol = RTE_BE16(RTE_ETHER_TYPE_TEB)
8562         };
8563         const struct rte_flow_item_gre gre_mask = {
8564                 .c_rsvd0_ver = RTE_BE16(0xB000),
8565                 .protocol = RTE_BE16(UINT16_MAX),
8566         };
8567         const struct rte_flow_item gre_item = {
8568                 .spec = &gre_spec,
8569                 .mask = &gre_mask,
8570                 .last = NULL,
8571         };
8572         flow_dv_translate_item_gre(matcher, key, &gre_item, inner);
8573         if (!nvgre_v)
8574                 return;
8575         if (!nvgre_m)
8576                 nvgre_m = &rte_flow_item_nvgre_mask;
8577         tni_flow_id_m = (const char *)nvgre_m->tni;
8578         tni_flow_id_v = (const char *)nvgre_v->tni;
8579         size = sizeof(nvgre_m->tni) + sizeof(nvgre_m->flow_id);
8580         gre_key_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, gre_key_h);
8581         gre_key_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, gre_key_h);
8582         memcpy(gre_key_m, tni_flow_id_m, size);
8583         for (i = 0; i < size; ++i)
8584                 gre_key_v[i] = gre_key_m[i] & tni_flow_id_v[i];
8585 }
8586
8587 /**
8588  * Add VXLAN item to matcher and to the value.
8589  *
8590  * @param[in, out] matcher
8591  *   Flow matcher.
8592  * @param[in, out] key
8593  *   Flow matcher value.
8594  * @param[in] item
8595  *   Flow pattern to translate.
8596  * @param[in] inner
8597  *   Item is inner pattern.
8598  */
8599 static void
8600 flow_dv_translate_item_vxlan(void *matcher, void *key,
8601                              const struct rte_flow_item *item,
8602                              int inner)
8603 {
8604         const struct rte_flow_item_vxlan *vxlan_m = item->mask;
8605         const struct rte_flow_item_vxlan *vxlan_v = item->spec;
8606         void *headers_m;
8607         void *headers_v;
8608         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
8609         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
8610         char *vni_m;
8611         char *vni_v;
8612         uint16_t dport;
8613         int size;
8614         int i;
8615
8616         if (inner) {
8617                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8618                                          inner_headers);
8619                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
8620         } else {
8621                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8622                                          outer_headers);
8623                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
8624         }
8625         dport = item->type == RTE_FLOW_ITEM_TYPE_VXLAN ?
8626                 MLX5_UDP_PORT_VXLAN : MLX5_UDP_PORT_VXLAN_GPE;
8627         if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
8628                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
8629                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
8630         }
8631         if (!vxlan_v)
8632                 return;
8633         if (!vxlan_m)
8634                 vxlan_m = &rte_flow_item_vxlan_mask;
8635         size = sizeof(vxlan_m->vni);
8636         vni_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, vxlan_vni);
8637         vni_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, vxlan_vni);
8638         memcpy(vni_m, vxlan_m->vni, size);
8639         for (i = 0; i < size; ++i)
8640                 vni_v[i] = vni_m[i] & vxlan_v->vni[i];
8641 }
8642
8643 /**
8644  * Add VXLAN-GPE item to matcher and to the value.
8645  *
8646  * @param[in, out] matcher
8647  *   Flow matcher.
8648  * @param[in, out] key
8649  *   Flow matcher value.
8650  * @param[in] item
8651  *   Flow pattern to translate.
8652  * @param[in] inner
8653  *   Item is inner pattern.
8654  */
8655
8656 static void
8657 flow_dv_translate_item_vxlan_gpe(void *matcher, void *key,
8658                                  const struct rte_flow_item *item, int inner)
8659 {
8660         const struct rte_flow_item_vxlan_gpe *vxlan_m = item->mask;
8661         const struct rte_flow_item_vxlan_gpe *vxlan_v = item->spec;
8662         void *headers_m;
8663         void *headers_v;
8664         void *misc_m =
8665                 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters_3);
8666         void *misc_v =
8667                 MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
8668         char *vni_m;
8669         char *vni_v;
8670         uint16_t dport;
8671         int size;
8672         int i;
8673         uint8_t flags_m = 0xff;
8674         uint8_t flags_v = 0xc;
8675
8676         if (inner) {
8677                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8678                                          inner_headers);
8679                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
8680         } else {
8681                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8682                                          outer_headers);
8683                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
8684         }
8685         dport = item->type == RTE_FLOW_ITEM_TYPE_VXLAN ?
8686                 MLX5_UDP_PORT_VXLAN : MLX5_UDP_PORT_VXLAN_GPE;
8687         if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
8688                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
8689                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
8690         }
8691         if (!vxlan_v)
8692                 return;
8693         if (!vxlan_m)
8694                 vxlan_m = &rte_flow_item_vxlan_gpe_mask;
8695         size = sizeof(vxlan_m->vni);
8696         vni_m = MLX5_ADDR_OF(fte_match_set_misc3, misc_m, outer_vxlan_gpe_vni);
8697         vni_v = MLX5_ADDR_OF(fte_match_set_misc3, misc_v, outer_vxlan_gpe_vni);
8698         memcpy(vni_m, vxlan_m->vni, size);
8699         for (i = 0; i < size; ++i)
8700                 vni_v[i] = vni_m[i] & vxlan_v->vni[i];
8701         if (vxlan_m->flags) {
8702                 flags_m = vxlan_m->flags;
8703                 flags_v = vxlan_v->flags;
8704         }
8705         MLX5_SET(fte_match_set_misc3, misc_m, outer_vxlan_gpe_flags, flags_m);
8706         MLX5_SET(fte_match_set_misc3, misc_v, outer_vxlan_gpe_flags, flags_v);
8707         MLX5_SET(fte_match_set_misc3, misc_m, outer_vxlan_gpe_next_protocol,
8708                  vxlan_m->protocol);
8709         MLX5_SET(fte_match_set_misc3, misc_v, outer_vxlan_gpe_next_protocol,
8710                  vxlan_v->protocol);
8711 }
8712
8713 /**
8714  * Add Geneve item to matcher and to the value.
8715  *
8716  * @param[in, out] matcher
8717  *   Flow matcher.
8718  * @param[in, out] key
8719  *   Flow matcher value.
8720  * @param[in] item
8721  *   Flow pattern to translate.
8722  * @param[in] inner
8723  *   Item is inner pattern.
8724  */
8725
8726 static void
8727 flow_dv_translate_item_geneve(void *matcher, void *key,
8728                               const struct rte_flow_item *item, int inner)
8729 {
8730         const struct rte_flow_item_geneve *geneve_m = item->mask;
8731         const struct rte_flow_item_geneve *geneve_v = item->spec;
8732         void *headers_m;
8733         void *headers_v;
8734         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
8735         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
8736         uint16_t dport;
8737         uint16_t gbhdr_m;
8738         uint16_t gbhdr_v;
8739         char *vni_m;
8740         char *vni_v;
8741         size_t size, i;
8742
8743         if (inner) {
8744                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8745                                          inner_headers);
8746                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
8747         } else {
8748                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8749                                          outer_headers);
8750                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
8751         }
8752         dport = MLX5_UDP_PORT_GENEVE;
8753         if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
8754                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
8755                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
8756         }
8757         if (!geneve_v)
8758                 return;
8759         if (!geneve_m)
8760                 geneve_m = &rte_flow_item_geneve_mask;
8761         size = sizeof(geneve_m->vni);
8762         vni_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, geneve_vni);
8763         vni_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, geneve_vni);
8764         memcpy(vni_m, geneve_m->vni, size);
8765         for (i = 0; i < size; ++i)
8766                 vni_v[i] = vni_m[i] & geneve_v->vni[i];
8767         MLX5_SET(fte_match_set_misc, misc_m, geneve_protocol_type,
8768                  rte_be_to_cpu_16(geneve_m->protocol));
8769         MLX5_SET(fte_match_set_misc, misc_v, geneve_protocol_type,
8770                  rte_be_to_cpu_16(geneve_v->protocol & geneve_m->protocol));
8771         gbhdr_m = rte_be_to_cpu_16(geneve_m->ver_opt_len_o_c_rsvd0);
8772         gbhdr_v = rte_be_to_cpu_16(geneve_v->ver_opt_len_o_c_rsvd0);
8773         MLX5_SET(fte_match_set_misc, misc_m, geneve_oam,
8774                  MLX5_GENEVE_OAMF_VAL(gbhdr_m));
8775         MLX5_SET(fte_match_set_misc, misc_v, geneve_oam,
8776                  MLX5_GENEVE_OAMF_VAL(gbhdr_v) & MLX5_GENEVE_OAMF_VAL(gbhdr_m));
8777         MLX5_SET(fte_match_set_misc, misc_m, geneve_opt_len,
8778                  MLX5_GENEVE_OPTLEN_VAL(gbhdr_m));
8779         MLX5_SET(fte_match_set_misc, misc_v, geneve_opt_len,
8780                  MLX5_GENEVE_OPTLEN_VAL(gbhdr_v) &
8781                  MLX5_GENEVE_OPTLEN_VAL(gbhdr_m));
8782 }
8783
8784 /**
8785  * Create Geneve TLV option resource.
8786  *
8787  * @param dev[in, out]
8788  *   Pointer to rte_eth_dev structure.
8789  * @param[in, out] tag_be24
8790  *   Tag value in big endian then R-shift 8.
8791  * @parm[in, out] dev_flow
8792  *   Pointer to the dev_flow.
8793  * @param[out] error
8794  *   pointer to error structure.
8795  *
8796  * @return
8797  *   0 on success otherwise -errno and errno is set.
8798  */
8799
8800 int
8801 flow_dev_geneve_tlv_option_resource_register(struct rte_eth_dev *dev,
8802                                              const struct rte_flow_item *item,
8803                                              struct rte_flow_error *error)
8804 {
8805         struct mlx5_priv *priv = dev->data->dev_private;
8806         struct mlx5_dev_ctx_shared *sh = priv->sh;
8807         struct mlx5_geneve_tlv_option_resource *geneve_opt_resource =
8808                         sh->geneve_tlv_option_resource;
8809         struct mlx5_devx_obj *obj;
8810         const struct rte_flow_item_geneve_opt *geneve_opt_v = item->spec;
8811         int ret = 0;
8812
8813         if (!geneve_opt_v)
8814                 return -1;
8815         rte_spinlock_lock(&sh->geneve_tlv_opt_sl);
8816         if (geneve_opt_resource != NULL) {
8817                 if (geneve_opt_resource->option_class ==
8818                         geneve_opt_v->option_class &&
8819                         geneve_opt_resource->option_type ==
8820                         geneve_opt_v->option_type &&
8821                         geneve_opt_resource->length ==
8822                         geneve_opt_v->option_len) {
8823                         /* We already have GENVE TLV option obj allocated. */
8824                         __atomic_fetch_add(&geneve_opt_resource->refcnt, 1,
8825                                            __ATOMIC_RELAXED);
8826                 } else {
8827                         ret = rte_flow_error_set(error, ENOMEM,
8828                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
8829                                 "Only one GENEVE TLV option supported");
8830                         goto exit;
8831                 }
8832         } else {
8833                 /* Create a GENEVE TLV object and resource. */
8834                 obj = mlx5_devx_cmd_create_geneve_tlv_option(sh->ctx,
8835                                 geneve_opt_v->option_class,
8836                                 geneve_opt_v->option_type,
8837                                 geneve_opt_v->option_len);
8838                 if (!obj) {
8839                         ret = rte_flow_error_set(error, ENODATA,
8840                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
8841                                 "Failed to create GENEVE TLV Devx object");
8842                         goto exit;
8843                 }
8844                 sh->geneve_tlv_option_resource =
8845                                 mlx5_malloc(MLX5_MEM_ZERO,
8846                                                 sizeof(*geneve_opt_resource),
8847                                                 0, SOCKET_ID_ANY);
8848                 if (!sh->geneve_tlv_option_resource) {
8849                         claim_zero(mlx5_devx_cmd_destroy(obj));
8850                         ret = rte_flow_error_set(error, ENOMEM,
8851                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
8852                                 "GENEVE TLV object memory allocation failed");
8853                         goto exit;
8854                 }
8855                 geneve_opt_resource = sh->geneve_tlv_option_resource;
8856                 geneve_opt_resource->obj = obj;
8857                 geneve_opt_resource->option_class = geneve_opt_v->option_class;
8858                 geneve_opt_resource->option_type = geneve_opt_v->option_type;
8859                 geneve_opt_resource->length = geneve_opt_v->option_len;
8860                 __atomic_store_n(&geneve_opt_resource->refcnt, 1,
8861                                 __ATOMIC_RELAXED);
8862         }
8863 exit:
8864         rte_spinlock_unlock(&sh->geneve_tlv_opt_sl);
8865         return ret;
8866 }
8867
8868 /**
8869  * Add Geneve TLV option item to matcher.
8870  *
8871  * @param[in, out] dev
8872  *   Pointer to rte_eth_dev structure.
8873  * @param[in, out] matcher
8874  *   Flow matcher.
8875  * @param[in, out] key
8876  *   Flow matcher value.
8877  * @param[in] item
8878  *   Flow pattern to translate.
8879  * @param[out] error
8880  *   Pointer to error structure.
8881  */
8882 static int
8883 flow_dv_translate_item_geneve_opt(struct rte_eth_dev *dev, void *matcher,
8884                                   void *key, const struct rte_flow_item *item,
8885                                   struct rte_flow_error *error)
8886 {
8887         const struct rte_flow_item_geneve_opt *geneve_opt_m = item->mask;
8888         const struct rte_flow_item_geneve_opt *geneve_opt_v = item->spec;
8889         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
8890         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
8891         void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
8892                         misc_parameters_3);
8893         void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
8894         rte_be32_t opt_data_key = 0, opt_data_mask = 0;
8895         int ret = 0;
8896
8897         if (!geneve_opt_v)
8898                 return -1;
8899         if (!geneve_opt_m)
8900                 geneve_opt_m = &rte_flow_item_geneve_opt_mask;
8901         ret = flow_dev_geneve_tlv_option_resource_register(dev, item,
8902                                                            error);
8903         if (ret) {
8904                 DRV_LOG(ERR, "Failed to create geneve_tlv_obj");
8905                 return ret;
8906         }
8907         /*
8908          * Set the option length in GENEVE header if not requested.
8909          * The GENEVE TLV option length is expressed by the option length field
8910          * in the GENEVE header.
8911          * If the option length was not requested but the GENEVE TLV option item
8912          * is present we set the option length field implicitly.
8913          */
8914         if (!MLX5_GET16(fte_match_set_misc, misc_m, geneve_opt_len)) {
8915                 MLX5_SET(fte_match_set_misc, misc_m, geneve_opt_len,
8916                          MLX5_GENEVE_OPTLEN_MASK);
8917                 MLX5_SET(fte_match_set_misc, misc_v, geneve_opt_len,
8918                          geneve_opt_v->option_len + 1);
8919         }
8920         /* Set the data. */
8921         if (geneve_opt_v->data) {
8922                 memcpy(&opt_data_key, geneve_opt_v->data,
8923                         RTE_MIN((uint32_t)(geneve_opt_v->option_len * 4),
8924                                 sizeof(opt_data_key)));
8925                 MLX5_ASSERT((uint32_t)(geneve_opt_v->option_len * 4) <=
8926                                 sizeof(opt_data_key));
8927                 memcpy(&opt_data_mask, geneve_opt_m->data,
8928                         RTE_MIN((uint32_t)(geneve_opt_v->option_len * 4),
8929                                 sizeof(opt_data_mask)));
8930                 MLX5_ASSERT((uint32_t)(geneve_opt_v->option_len * 4) <=
8931                                 sizeof(opt_data_mask));
8932                 MLX5_SET(fte_match_set_misc3, misc3_m,
8933                                 geneve_tlv_option_0_data,
8934                                 rte_be_to_cpu_32(opt_data_mask));
8935                 MLX5_SET(fte_match_set_misc3, misc3_v,
8936                                 geneve_tlv_option_0_data,
8937                         rte_be_to_cpu_32(opt_data_key & opt_data_mask));
8938         }
8939         return ret;
8940 }
8941
8942 /**
8943  * Add MPLS item to matcher and to the value.
8944  *
8945  * @param[in, out] matcher
8946  *   Flow matcher.
8947  * @param[in, out] key
8948  *   Flow matcher value.
8949  * @param[in] item
8950  *   Flow pattern to translate.
8951  * @param[in] prev_layer
8952  *   The protocol layer indicated in previous item.
8953  * @param[in] inner
8954  *   Item is inner pattern.
8955  */
8956 static void
8957 flow_dv_translate_item_mpls(void *matcher, void *key,
8958                             const struct rte_flow_item *item,
8959                             uint64_t prev_layer,
8960                             int inner)
8961 {
8962         const uint32_t *in_mpls_m = item->mask;
8963         const uint32_t *in_mpls_v = item->spec;
8964         uint32_t *out_mpls_m = 0;
8965         uint32_t *out_mpls_v = 0;
8966         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
8967         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
8968         void *misc2_m = MLX5_ADDR_OF(fte_match_param, matcher,
8969                                      misc_parameters_2);
8970         void *misc2_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_2);
8971         void *headers_m = MLX5_ADDR_OF(fte_match_param, matcher, outer_headers);
8972         void *headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
8973
8974         switch (prev_layer) {
8975         case MLX5_FLOW_LAYER_OUTER_L4_UDP:
8976                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xffff);
8977                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
8978                          MLX5_UDP_PORT_MPLS);
8979                 break;
8980         case MLX5_FLOW_LAYER_GRE:
8981                 MLX5_SET(fte_match_set_misc, misc_m, gre_protocol, 0xffff);
8982                 MLX5_SET(fte_match_set_misc, misc_v, gre_protocol,
8983                          RTE_ETHER_TYPE_MPLS);
8984                 break;
8985         default:
8986                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
8987                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
8988                          IPPROTO_MPLS);
8989                 break;
8990         }
8991         if (!in_mpls_v)
8992                 return;
8993         if (!in_mpls_m)
8994                 in_mpls_m = (const uint32_t *)&rte_flow_item_mpls_mask;
8995         switch (prev_layer) {
8996         case MLX5_FLOW_LAYER_OUTER_L4_UDP:
8997                 out_mpls_m =
8998                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_m,
8999                                                  outer_first_mpls_over_udp);
9000                 out_mpls_v =
9001                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_v,
9002                                                  outer_first_mpls_over_udp);
9003                 break;
9004         case MLX5_FLOW_LAYER_GRE:
9005                 out_mpls_m =
9006                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_m,
9007                                                  outer_first_mpls_over_gre);
9008                 out_mpls_v =
9009                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_v,
9010                                                  outer_first_mpls_over_gre);
9011                 break;
9012         default:
9013                 /* Inner MPLS not over GRE is not supported. */
9014                 if (!inner) {
9015                         out_mpls_m =
9016                                 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2,
9017                                                          misc2_m,
9018                                                          outer_first_mpls);
9019                         out_mpls_v =
9020                                 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2,
9021                                                          misc2_v,
9022                                                          outer_first_mpls);
9023                 }
9024                 break;
9025         }
9026         if (out_mpls_m && out_mpls_v) {
9027                 *out_mpls_m = *in_mpls_m;
9028                 *out_mpls_v = *in_mpls_v & *in_mpls_m;
9029         }
9030 }
9031
9032 /**
9033  * Add metadata register item to matcher
9034  *
9035  * @param[in, out] matcher
9036  *   Flow matcher.
9037  * @param[in, out] key
9038  *   Flow matcher value.
9039  * @param[in] reg_type
9040  *   Type of device metadata register
9041  * @param[in] value
9042  *   Register value
9043  * @param[in] mask
9044  *   Register mask
9045  */
9046 static void
9047 flow_dv_match_meta_reg(void *matcher, void *key,
9048                        enum modify_reg reg_type,
9049                        uint32_t data, uint32_t mask)
9050 {
9051         void *misc2_m =
9052                 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters_2);
9053         void *misc2_v =
9054                 MLX5_ADDR_OF(fte_match_param, key, misc_parameters_2);
9055         uint32_t temp;
9056
9057         data &= mask;
9058         switch (reg_type) {
9059         case REG_A:
9060                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_a, mask);
9061                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_a, data);
9062                 break;
9063         case REG_B:
9064                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_b, mask);
9065                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_b, data);
9066                 break;
9067         case REG_C_0:
9068                 /*
9069                  * The metadata register C0 field might be divided into
9070                  * source vport index and META item value, we should set
9071                  * this field according to specified mask, not as whole one.
9072                  */
9073                 temp = MLX5_GET(fte_match_set_misc2, misc2_m, metadata_reg_c_0);
9074                 temp |= mask;
9075                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_0, temp);
9076                 temp = MLX5_GET(fte_match_set_misc2, misc2_v, metadata_reg_c_0);
9077                 temp &= ~mask;
9078                 temp |= data;
9079                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_0, temp);
9080                 break;
9081         case REG_C_1:
9082                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_1, mask);
9083                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_1, data);
9084                 break;
9085         case REG_C_2:
9086                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_2, mask);
9087                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_2, data);
9088                 break;
9089         case REG_C_3:
9090                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_3, mask);
9091                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_3, data);
9092                 break;
9093         case REG_C_4:
9094                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_4, mask);
9095                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_4, data);
9096                 break;
9097         case REG_C_5:
9098                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_5, mask);
9099                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_5, data);
9100                 break;
9101         case REG_C_6:
9102                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_6, mask);
9103                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_6, data);
9104                 break;
9105         case REG_C_7:
9106                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_7, mask);
9107                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_7, data);
9108                 break;
9109         default:
9110                 MLX5_ASSERT(false);
9111                 break;
9112         }
9113 }
9114
9115 /**
9116  * Add MARK item to matcher
9117  *
9118  * @param[in] dev
9119  *   The device to configure through.
9120  * @param[in, out] matcher
9121  *   Flow matcher.
9122  * @param[in, out] key
9123  *   Flow matcher value.
9124  * @param[in] item
9125  *   Flow pattern to translate.
9126  */
9127 static void
9128 flow_dv_translate_item_mark(struct rte_eth_dev *dev,
9129                             void *matcher, void *key,
9130                             const struct rte_flow_item *item)
9131 {
9132         struct mlx5_priv *priv = dev->data->dev_private;
9133         const struct rte_flow_item_mark *mark;
9134         uint32_t value;
9135         uint32_t mask;
9136
9137         mark = item->mask ? (const void *)item->mask :
9138                             &rte_flow_item_mark_mask;
9139         mask = mark->id & priv->sh->dv_mark_mask;
9140         mark = (const void *)item->spec;
9141         MLX5_ASSERT(mark);
9142         value = mark->id & priv->sh->dv_mark_mask & mask;
9143         if (mask) {
9144                 enum modify_reg reg;
9145
9146                 /* Get the metadata register index for the mark. */
9147                 reg = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, NULL);
9148                 MLX5_ASSERT(reg > 0);
9149                 if (reg == REG_C_0) {
9150                         struct mlx5_priv *priv = dev->data->dev_private;
9151                         uint32_t msk_c0 = priv->sh->dv_regc0_mask;
9152                         uint32_t shl_c0 = rte_bsf32(msk_c0);
9153
9154                         mask &= msk_c0;
9155                         mask <<= shl_c0;
9156                         value <<= shl_c0;
9157                 }
9158                 flow_dv_match_meta_reg(matcher, key, reg, value, mask);
9159         }
9160 }
9161
9162 /**
9163  * Add META item to matcher
9164  *
9165  * @param[in] dev
9166  *   The devich to configure through.
9167  * @param[in, out] matcher
9168  *   Flow matcher.
9169  * @param[in, out] key
9170  *   Flow matcher value.
9171  * @param[in] attr
9172  *   Attributes of flow that includes this item.
9173  * @param[in] item
9174  *   Flow pattern to translate.
9175  */
9176 static void
9177 flow_dv_translate_item_meta(struct rte_eth_dev *dev,
9178                             void *matcher, void *key,
9179                             const struct rte_flow_attr *attr,
9180                             const struct rte_flow_item *item)
9181 {
9182         const struct rte_flow_item_meta *meta_m;
9183         const struct rte_flow_item_meta *meta_v;
9184
9185         meta_m = (const void *)item->mask;
9186         if (!meta_m)
9187                 meta_m = &rte_flow_item_meta_mask;
9188         meta_v = (const void *)item->spec;
9189         if (meta_v) {
9190                 int reg;
9191                 uint32_t value = meta_v->data;
9192                 uint32_t mask = meta_m->data;
9193
9194                 reg = flow_dv_get_metadata_reg(dev, attr, NULL);
9195                 if (reg < 0)
9196                         return;
9197                 MLX5_ASSERT(reg != REG_NON);
9198                 /*
9199                  * In datapath code there is no endianness
9200                  * coversions for perfromance reasons, all
9201                  * pattern conversions are done in rte_flow.
9202                  */
9203                 value = rte_cpu_to_be_32(value);
9204                 mask = rte_cpu_to_be_32(mask);
9205                 if (reg == REG_C_0) {
9206                         struct mlx5_priv *priv = dev->data->dev_private;
9207                         uint32_t msk_c0 = priv->sh->dv_regc0_mask;
9208                         uint32_t shl_c0 = rte_bsf32(msk_c0);
9209 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
9210                         uint32_t shr_c0 = __builtin_clz(priv->sh->dv_meta_mask);
9211
9212                         value >>= shr_c0;
9213                         mask >>= shr_c0;
9214 #endif
9215                         value <<= shl_c0;
9216                         mask <<= shl_c0;
9217                         MLX5_ASSERT(msk_c0);
9218                         MLX5_ASSERT(!(~msk_c0 & mask));
9219                 }
9220                 flow_dv_match_meta_reg(matcher, key, reg, value, mask);
9221         }
9222 }
9223
9224 /**
9225  * Add vport metadata Reg C0 item to matcher
9226  *
9227  * @param[in, out] matcher
9228  *   Flow matcher.
9229  * @param[in, out] key
9230  *   Flow matcher value.
9231  * @param[in] reg
9232  *   Flow pattern to translate.
9233  */
9234 static void
9235 flow_dv_translate_item_meta_vport(void *matcher, void *key,
9236                                   uint32_t value, uint32_t mask)
9237 {
9238         flow_dv_match_meta_reg(matcher, key, REG_C_0, value, mask);
9239 }
9240
9241 /**
9242  * Add tag item to matcher
9243  *
9244  * @param[in] dev
9245  *   The devich to configure through.
9246  * @param[in, out] matcher
9247  *   Flow matcher.
9248  * @param[in, out] key
9249  *   Flow matcher value.
9250  * @param[in] item
9251  *   Flow pattern to translate.
9252  */
9253 static void
9254 flow_dv_translate_mlx5_item_tag(struct rte_eth_dev *dev,
9255                                 void *matcher, void *key,
9256                                 const struct rte_flow_item *item)
9257 {
9258         const struct mlx5_rte_flow_item_tag *tag_v = item->spec;
9259         const struct mlx5_rte_flow_item_tag *tag_m = item->mask;
9260         uint32_t mask, value;
9261
9262         MLX5_ASSERT(tag_v);
9263         value = tag_v->data;
9264         mask = tag_m ? tag_m->data : UINT32_MAX;
9265         if (tag_v->id == REG_C_0) {
9266                 struct mlx5_priv *priv = dev->data->dev_private;
9267                 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
9268                 uint32_t shl_c0 = rte_bsf32(msk_c0);
9269
9270                 mask &= msk_c0;
9271                 mask <<= shl_c0;
9272                 value <<= shl_c0;
9273         }
9274         flow_dv_match_meta_reg(matcher, key, tag_v->id, value, mask);
9275 }
9276
9277 /**
9278  * Add TAG item to matcher
9279  *
9280  * @param[in] dev
9281  *   The devich to configure through.
9282  * @param[in, out] matcher
9283  *   Flow matcher.
9284  * @param[in, out] key
9285  *   Flow matcher value.
9286  * @param[in] item
9287  *   Flow pattern to translate.
9288  */
9289 static void
9290 flow_dv_translate_item_tag(struct rte_eth_dev *dev,
9291                            void *matcher, void *key,
9292                            const struct rte_flow_item *item)
9293 {
9294         const struct rte_flow_item_tag *tag_v = item->spec;
9295         const struct rte_flow_item_tag *tag_m = item->mask;
9296         enum modify_reg reg;
9297
9298         MLX5_ASSERT(tag_v);
9299         tag_m = tag_m ? tag_m : &rte_flow_item_tag_mask;
9300         /* Get the metadata register index for the tag. */
9301         reg = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, tag_v->index, NULL);
9302         MLX5_ASSERT(reg > 0);
9303         flow_dv_match_meta_reg(matcher, key, reg, tag_v->data, tag_m->data);
9304 }
9305
9306 /**
9307  * Add source vport match to the specified matcher.
9308  *
9309  * @param[in, out] matcher
9310  *   Flow matcher.
9311  * @param[in, out] key
9312  *   Flow matcher value.
9313  * @param[in] port
9314  *   Source vport value to match
9315  * @param[in] mask
9316  *   Mask
9317  */
9318 static void
9319 flow_dv_translate_item_source_vport(void *matcher, void *key,
9320                                     int16_t port, uint16_t mask)
9321 {
9322         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
9323         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
9324
9325         MLX5_SET(fte_match_set_misc, misc_m, source_port, mask);
9326         MLX5_SET(fte_match_set_misc, misc_v, source_port, port);
9327 }
9328
9329 /**
9330  * Translate port-id item to eswitch match on  port-id.
9331  *
9332  * @param[in] dev
9333  *   The devich to configure through.
9334  * @param[in, out] matcher
9335  *   Flow matcher.
9336  * @param[in, out] key
9337  *   Flow matcher value.
9338  * @param[in] item
9339  *   Flow pattern to translate.
9340  * @param[in]
9341  *   Flow attributes.
9342  *
9343  * @return
9344  *   0 on success, a negative errno value otherwise.
9345  */
9346 static int
9347 flow_dv_translate_item_port_id(struct rte_eth_dev *dev, void *matcher,
9348                                void *key, const struct rte_flow_item *item,
9349                                const struct rte_flow_attr *attr)
9350 {
9351         const struct rte_flow_item_port_id *pid_m = item ? item->mask : NULL;
9352         const struct rte_flow_item_port_id *pid_v = item ? item->spec : NULL;
9353         struct mlx5_priv *priv;
9354         uint16_t mask, id;
9355
9356         mask = pid_m ? pid_m->id : 0xffff;
9357         id = pid_v ? pid_v->id : dev->data->port_id;
9358         priv = mlx5_port_to_eswitch_info(id, item == NULL);
9359         if (!priv)
9360                 return -rte_errno;
9361         /*
9362          * Translate to vport field or to metadata, depending on mode.
9363          * Kernel can use either misc.source_port or half of C0 metadata
9364          * register.
9365          */
9366         if (priv->vport_meta_mask) {
9367                 /*
9368                  * Provide the hint for SW steering library
9369                  * to insert the flow into ingress domain and
9370                  * save the extra vport match.
9371                  */
9372                 if (mask == 0xffff && priv->vport_id == 0xffff &&
9373                     priv->pf_bond < 0 && attr->transfer)
9374                         flow_dv_translate_item_source_vport
9375                                 (matcher, key, priv->vport_id, mask);
9376                 /*
9377                  * We should always set the vport metadata register,
9378                  * otherwise the SW steering library can drop
9379                  * the rule if wire vport metadata value is not zero,
9380                  * it depends on kernel configuration.
9381                  */
9382                 flow_dv_translate_item_meta_vport(matcher, key,
9383                                                   priv->vport_meta_tag,
9384                                                   priv->vport_meta_mask);
9385         } else {
9386                 flow_dv_translate_item_source_vport(matcher, key,
9387                                                     priv->vport_id, mask);
9388         }
9389         return 0;
9390 }
9391
9392 /**
9393  * Add ICMP6 item to matcher and to the value.
9394  *
9395  * @param[in, out] matcher
9396  *   Flow matcher.
9397  * @param[in, out] key
9398  *   Flow matcher value.
9399  * @param[in] item
9400  *   Flow pattern to translate.
9401  * @param[in] inner
9402  *   Item is inner pattern.
9403  */
9404 static void
9405 flow_dv_translate_item_icmp6(void *matcher, void *key,
9406                               const struct rte_flow_item *item,
9407                               int inner)
9408 {
9409         const struct rte_flow_item_icmp6 *icmp6_m = item->mask;
9410         const struct rte_flow_item_icmp6 *icmp6_v = item->spec;
9411         void *headers_m;
9412         void *headers_v;
9413         void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
9414                                      misc_parameters_3);
9415         void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
9416         if (inner) {
9417                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
9418                                          inner_headers);
9419                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
9420         } else {
9421                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
9422                                          outer_headers);
9423                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
9424         }
9425         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xFF);
9426         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_ICMPV6);
9427         if (!icmp6_v)
9428                 return;
9429         if (!icmp6_m)
9430                 icmp6_m = &rte_flow_item_icmp6_mask;
9431         MLX5_SET(fte_match_set_misc3, misc3_m, icmpv6_type, icmp6_m->type);
9432         MLX5_SET(fte_match_set_misc3, misc3_v, icmpv6_type,
9433                  icmp6_v->type & icmp6_m->type);
9434         MLX5_SET(fte_match_set_misc3, misc3_m, icmpv6_code, icmp6_m->code);
9435         MLX5_SET(fte_match_set_misc3, misc3_v, icmpv6_code,
9436                  icmp6_v->code & icmp6_m->code);
9437 }
9438
9439 /**
9440  * Add ICMP item to matcher and to the value.
9441  *
9442  * @param[in, out] matcher
9443  *   Flow matcher.
9444  * @param[in, out] key
9445  *   Flow matcher value.
9446  * @param[in] item
9447  *   Flow pattern to translate.
9448  * @param[in] inner
9449  *   Item is inner pattern.
9450  */
9451 static void
9452 flow_dv_translate_item_icmp(void *matcher, void *key,
9453                             const struct rte_flow_item *item,
9454                             int inner)
9455 {
9456         const struct rte_flow_item_icmp *icmp_m = item->mask;
9457         const struct rte_flow_item_icmp *icmp_v = item->spec;
9458         uint32_t icmp_header_data_m = 0;
9459         uint32_t icmp_header_data_v = 0;
9460         void *headers_m;
9461         void *headers_v;
9462         void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
9463                                      misc_parameters_3);
9464         void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
9465         if (inner) {
9466                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
9467                                          inner_headers);
9468                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
9469         } else {
9470                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
9471                                          outer_headers);
9472                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
9473         }
9474         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xFF);
9475         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_ICMP);
9476         if (!icmp_v)
9477                 return;
9478         if (!icmp_m)
9479                 icmp_m = &rte_flow_item_icmp_mask;
9480         MLX5_SET(fte_match_set_misc3, misc3_m, icmp_type,
9481                  icmp_m->hdr.icmp_type);
9482         MLX5_SET(fte_match_set_misc3, misc3_v, icmp_type,
9483                  icmp_v->hdr.icmp_type & icmp_m->hdr.icmp_type);
9484         MLX5_SET(fte_match_set_misc3, misc3_m, icmp_code,
9485                  icmp_m->hdr.icmp_code);
9486         MLX5_SET(fte_match_set_misc3, misc3_v, icmp_code,
9487                  icmp_v->hdr.icmp_code & icmp_m->hdr.icmp_code);
9488         icmp_header_data_m = rte_be_to_cpu_16(icmp_m->hdr.icmp_seq_nb);
9489         icmp_header_data_m |= rte_be_to_cpu_16(icmp_m->hdr.icmp_ident) << 16;
9490         if (icmp_header_data_m) {
9491                 icmp_header_data_v = rte_be_to_cpu_16(icmp_v->hdr.icmp_seq_nb);
9492                 icmp_header_data_v |=
9493                          rte_be_to_cpu_16(icmp_v->hdr.icmp_ident) << 16;
9494                 MLX5_SET(fte_match_set_misc3, misc3_m, icmp_header_data,
9495                          icmp_header_data_m);
9496                 MLX5_SET(fte_match_set_misc3, misc3_v, icmp_header_data,
9497                          icmp_header_data_v & icmp_header_data_m);
9498         }
9499 }
9500
9501 /**
9502  * Add GTP item to matcher and to the value.
9503  *
9504  * @param[in, out] matcher
9505  *   Flow matcher.
9506  * @param[in, out] key
9507  *   Flow matcher value.
9508  * @param[in] item
9509  *   Flow pattern to translate.
9510  * @param[in] inner
9511  *   Item is inner pattern.
9512  */
9513 static void
9514 flow_dv_translate_item_gtp(void *matcher, void *key,
9515                            const struct rte_flow_item *item, int inner)
9516 {
9517         const struct rte_flow_item_gtp *gtp_m = item->mask;
9518         const struct rte_flow_item_gtp *gtp_v = item->spec;
9519         void *headers_m;
9520         void *headers_v;
9521         void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
9522                                      misc_parameters_3);
9523         void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
9524         uint16_t dport = RTE_GTPU_UDP_PORT;
9525
9526         if (inner) {
9527                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
9528                                          inner_headers);
9529                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
9530         } else {
9531                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
9532                                          outer_headers);
9533                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
9534         }
9535         if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
9536                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
9537                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
9538         }
9539         if (!gtp_v)
9540                 return;
9541         if (!gtp_m)
9542                 gtp_m = &rte_flow_item_gtp_mask;
9543         MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_msg_flags,
9544                  gtp_m->v_pt_rsv_flags);
9545         MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_msg_flags,
9546                  gtp_v->v_pt_rsv_flags & gtp_m->v_pt_rsv_flags);
9547         MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_msg_type, gtp_m->msg_type);
9548         MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_msg_type,
9549                  gtp_v->msg_type & gtp_m->msg_type);
9550         MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_teid,
9551                  rte_be_to_cpu_32(gtp_m->teid));
9552         MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_teid,
9553                  rte_be_to_cpu_32(gtp_v->teid & gtp_m->teid));
9554 }
9555
9556 /**
9557  * Add GTP PSC item to matcher.
9558  *
9559  * @param[in, out] matcher
9560  *   Flow matcher.
9561  * @param[in, out] key
9562  *   Flow matcher value.
9563  * @param[in] item
9564  *   Flow pattern to translate.
9565  */
9566 static int
9567 flow_dv_translate_item_gtp_psc(void *matcher, void *key,
9568                                const struct rte_flow_item *item)
9569 {
9570         const struct rte_flow_item_gtp_psc *gtp_psc_m = item->mask;
9571         const struct rte_flow_item_gtp_psc *gtp_psc_v = item->spec;
9572         void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
9573                         misc_parameters_3);
9574         void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
9575         union {
9576                 uint32_t w32;
9577                 struct {
9578                         uint16_t seq_num;
9579                         uint8_t npdu_num;
9580                         uint8_t next_ext_header_type;
9581                 };
9582         } dw_2;
9583         uint8_t gtp_flags;
9584
9585         /* Always set E-flag match on one, regardless of GTP item settings. */
9586         gtp_flags = MLX5_GET(fte_match_set_misc3, misc3_m, gtpu_msg_flags);
9587         gtp_flags |= MLX5_GTP_EXT_HEADER_FLAG;
9588         MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_msg_flags, gtp_flags);
9589         gtp_flags = MLX5_GET(fte_match_set_misc3, misc3_v, gtpu_msg_flags);
9590         gtp_flags |= MLX5_GTP_EXT_HEADER_FLAG;
9591         MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_msg_flags, gtp_flags);
9592         /*Set next extension header type. */
9593         dw_2.seq_num = 0;
9594         dw_2.npdu_num = 0;
9595         dw_2.next_ext_header_type = 0xff;
9596         MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_dw_2,
9597                  rte_cpu_to_be_32(dw_2.w32));
9598         dw_2.seq_num = 0;
9599         dw_2.npdu_num = 0;
9600         dw_2.next_ext_header_type = 0x85;
9601         MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_dw_2,
9602                  rte_cpu_to_be_32(dw_2.w32));
9603         if (gtp_psc_v) {
9604                 union {
9605                         uint32_t w32;
9606                         struct {
9607                                 uint8_t len;
9608                                 uint8_t type_flags;
9609                                 uint8_t qfi;
9610                                 uint8_t reserved;
9611                         };
9612                 } dw_0;
9613
9614                 /*Set extension header PDU type and Qos. */
9615                 if (!gtp_psc_m)
9616                         gtp_psc_m = &rte_flow_item_gtp_psc_mask;
9617                 dw_0.w32 = 0;
9618                 dw_0.type_flags = MLX5_GTP_PDU_TYPE_SHIFT(gtp_psc_m->pdu_type);
9619                 dw_0.qfi = gtp_psc_m->qfi;
9620                 MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_first_ext_dw_0,
9621                          rte_cpu_to_be_32(dw_0.w32));
9622                 dw_0.w32 = 0;
9623                 dw_0.type_flags = MLX5_GTP_PDU_TYPE_SHIFT(gtp_psc_v->pdu_type &
9624                                                         gtp_psc_m->pdu_type);
9625                 dw_0.qfi = gtp_psc_v->qfi & gtp_psc_m->qfi;
9626                 MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_first_ext_dw_0,
9627                          rte_cpu_to_be_32(dw_0.w32));
9628         }
9629         return 0;
9630 }
9631
9632 /**
9633  * Add eCPRI item to matcher and to the value.
9634  *
9635  * @param[in] dev
9636  *   The devich to configure through.
9637  * @param[in, out] matcher
9638  *   Flow matcher.
9639  * @param[in, out] key
9640  *   Flow matcher value.
9641  * @param[in] item
9642  *   Flow pattern to translate.
9643  * @param[in] samples
9644  *   Sample IDs to be used in the matching.
9645  */
9646 static void
9647 flow_dv_translate_item_ecpri(struct rte_eth_dev *dev, void *matcher,
9648                              void *key, const struct rte_flow_item *item)
9649 {
9650         struct mlx5_priv *priv = dev->data->dev_private;
9651         const struct rte_flow_item_ecpri *ecpri_m = item->mask;
9652         const struct rte_flow_item_ecpri *ecpri_v = item->spec;
9653         struct rte_ecpri_common_hdr common;
9654         void *misc4_m = MLX5_ADDR_OF(fte_match_param, matcher,
9655                                      misc_parameters_4);
9656         void *misc4_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_4);
9657         uint32_t *samples;
9658         void *dw_m;
9659         void *dw_v;
9660
9661         if (!ecpri_v)
9662                 return;
9663         if (!ecpri_m)
9664                 ecpri_m = &rte_flow_item_ecpri_mask;
9665         /*
9666          * Maximal four DW samples are supported in a single matching now.
9667          * Two are used now for a eCPRI matching:
9668          * 1. Type: one byte, mask should be 0x00ff0000 in network order
9669          * 2. ID of a message: one or two bytes, mask 0xffff0000 or 0xff000000
9670          *    if any.
9671          */
9672         if (!ecpri_m->hdr.common.u32)
9673                 return;
9674         samples = priv->sh->fp[MLX5_FLEX_PARSER_ECPRI_0].ids;
9675         /* Need to take the whole DW as the mask to fill the entry. */
9676         dw_m = MLX5_ADDR_OF(fte_match_set_misc4, misc4_m,
9677                             prog_sample_field_value_0);
9678         dw_v = MLX5_ADDR_OF(fte_match_set_misc4, misc4_v,
9679                             prog_sample_field_value_0);
9680         /* Already big endian (network order) in the header. */
9681         *(uint32_t *)dw_m = ecpri_m->hdr.common.u32;
9682         *(uint32_t *)dw_v = ecpri_v->hdr.common.u32 & ecpri_m->hdr.common.u32;
9683         /* Sample#0, used for matching type, offset 0. */
9684         MLX5_SET(fte_match_set_misc4, misc4_m,
9685                  prog_sample_field_id_0, samples[0]);
9686         /* It makes no sense to set the sample ID in the mask field. */
9687         MLX5_SET(fte_match_set_misc4, misc4_v,
9688                  prog_sample_field_id_0, samples[0]);
9689         /*
9690          * Checking if message body part needs to be matched.
9691          * Some wildcard rules only matching type field should be supported.
9692          */
9693         if (ecpri_m->hdr.dummy[0]) {
9694                 common.u32 = rte_be_to_cpu_32(ecpri_v->hdr.common.u32);
9695                 switch (common.type) {
9696                 case RTE_ECPRI_MSG_TYPE_IQ_DATA:
9697                 case RTE_ECPRI_MSG_TYPE_RTC_CTRL:
9698                 case RTE_ECPRI_MSG_TYPE_DLY_MSR:
9699                         dw_m = MLX5_ADDR_OF(fte_match_set_misc4, misc4_m,
9700                                             prog_sample_field_value_1);
9701                         dw_v = MLX5_ADDR_OF(fte_match_set_misc4, misc4_v,
9702                                             prog_sample_field_value_1);
9703                         *(uint32_t *)dw_m = ecpri_m->hdr.dummy[0];
9704                         *(uint32_t *)dw_v = ecpri_v->hdr.dummy[0] &
9705                                             ecpri_m->hdr.dummy[0];
9706                         /* Sample#1, to match message body, offset 4. */
9707                         MLX5_SET(fte_match_set_misc4, misc4_m,
9708                                  prog_sample_field_id_1, samples[1]);
9709                         MLX5_SET(fte_match_set_misc4, misc4_v,
9710                                  prog_sample_field_id_1, samples[1]);
9711                         break;
9712                 default:
9713                         /* Others, do not match any sample ID. */
9714                         break;
9715                 }
9716         }
9717 }
9718
9719 /*
9720  * Add connection tracking status item to matcher
9721  *
9722  * @param[in] dev
9723  *   The devich to configure through.
9724  * @param[in, out] matcher
9725  *   Flow matcher.
9726  * @param[in, out] key
9727  *   Flow matcher value.
9728  * @param[in] item
9729  *   Flow pattern to translate.
9730  */
9731 static void
9732 flow_dv_translate_item_aso_ct(struct rte_eth_dev *dev,
9733                               void *matcher, void *key,
9734                               const struct rte_flow_item *item)
9735 {
9736         uint32_t reg_value = 0;
9737         int reg_id;
9738         /* 8LSB 0b 11/0000/11, middle 4 bits are reserved. */
9739         uint32_t reg_mask = 0;
9740         const struct rte_flow_item_conntrack *spec = item->spec;
9741         const struct rte_flow_item_conntrack *mask = item->mask;
9742         uint32_t flags;
9743         struct rte_flow_error error;
9744
9745         if (!mask)
9746                 mask = &rte_flow_item_conntrack_mask;
9747         if (!spec || !mask->flags)
9748                 return;
9749         flags = spec->flags & mask->flags;
9750         /* The conflict should be checked in the validation. */
9751         if (flags & RTE_FLOW_CONNTRACK_PKT_STATE_VALID)
9752                 reg_value |= MLX5_CT_SYNDROME_VALID;
9753         if (flags & RTE_FLOW_CONNTRACK_PKT_STATE_CHANGED)
9754                 reg_value |= MLX5_CT_SYNDROME_STATE_CHANGE;
9755         if (flags & RTE_FLOW_CONNTRACK_PKT_STATE_INVALID)
9756                 reg_value |= MLX5_CT_SYNDROME_INVALID;
9757         if (flags & RTE_FLOW_CONNTRACK_PKT_STATE_DISABLED)
9758                 reg_value |= MLX5_CT_SYNDROME_TRAP;
9759         if (flags & RTE_FLOW_CONNTRACK_PKT_STATE_BAD)
9760                 reg_value |= MLX5_CT_SYNDROME_BAD_PACKET;
9761         if (mask->flags & (RTE_FLOW_CONNTRACK_PKT_STATE_VALID |
9762                            RTE_FLOW_CONNTRACK_PKT_STATE_INVALID |
9763                            RTE_FLOW_CONNTRACK_PKT_STATE_DISABLED))
9764                 reg_mask |= 0xc0;
9765         if (mask->flags & RTE_FLOW_CONNTRACK_PKT_STATE_CHANGED)
9766                 reg_mask |= MLX5_CT_SYNDROME_STATE_CHANGE;
9767         if (mask->flags & RTE_FLOW_CONNTRACK_PKT_STATE_BAD)
9768                 reg_mask |= MLX5_CT_SYNDROME_BAD_PACKET;
9769         /* The REG_C_x value could be saved during startup. */
9770         reg_id = mlx5_flow_get_reg_id(dev, MLX5_ASO_CONNTRACK, 0, &error);
9771         if (reg_id == REG_NON)
9772                 return;
9773         flow_dv_match_meta_reg(matcher, key, (enum modify_reg)reg_id,
9774                                reg_value, reg_mask);
9775 }
9776
9777 static uint32_t matcher_zero[MLX5_ST_SZ_DW(fte_match_param)] = { 0 };
9778
9779 #define HEADER_IS_ZERO(match_criteria, headers)                              \
9780         !(memcmp(MLX5_ADDR_OF(fte_match_param, match_criteria, headers),     \
9781                  matcher_zero, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
9782
9783 /**
9784  * Calculate flow matcher enable bitmap.
9785  *
9786  * @param match_criteria
9787  *   Pointer to flow matcher criteria.
9788  *
9789  * @return
9790  *   Bitmap of enabled fields.
9791  */
9792 static uint8_t
9793 flow_dv_matcher_enable(uint32_t *match_criteria)
9794 {
9795         uint8_t match_criteria_enable;
9796
9797         match_criteria_enable =
9798                 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
9799                 MLX5_MATCH_CRITERIA_ENABLE_OUTER_BIT;
9800         match_criteria_enable |=
9801                 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
9802                 MLX5_MATCH_CRITERIA_ENABLE_MISC_BIT;
9803         match_criteria_enable |=
9804                 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
9805                 MLX5_MATCH_CRITERIA_ENABLE_INNER_BIT;
9806         match_criteria_enable |=
9807                 (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
9808                 MLX5_MATCH_CRITERIA_ENABLE_MISC2_BIT;
9809         match_criteria_enable |=
9810                 (!HEADER_IS_ZERO(match_criteria, misc_parameters_3)) <<
9811                 MLX5_MATCH_CRITERIA_ENABLE_MISC3_BIT;
9812         match_criteria_enable |=
9813                 (!HEADER_IS_ZERO(match_criteria, misc_parameters_4)) <<
9814                 MLX5_MATCH_CRITERIA_ENABLE_MISC4_BIT;
9815         return match_criteria_enable;
9816 }
9817
9818 struct mlx5_hlist_entry *
9819 flow_dv_tbl_create_cb(struct mlx5_hlist *list, uint64_t key64, void *cb_ctx)
9820 {
9821         struct mlx5_dev_ctx_shared *sh = list->ctx;
9822         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
9823         struct rte_eth_dev *dev = ctx->dev;
9824         struct mlx5_flow_tbl_data_entry *tbl_data;
9825         struct mlx5_flow_tbl_tunnel_prm *tt_prm = ctx->data;
9826         struct rte_flow_error *error = ctx->error;
9827         union mlx5_flow_tbl_key key = { .v64 = key64 };
9828         struct mlx5_flow_tbl_resource *tbl;
9829         void *domain;
9830         uint32_t idx = 0;
9831         int ret;
9832
9833         tbl_data = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_JUMP], &idx);
9834         if (!tbl_data) {
9835                 rte_flow_error_set(error, ENOMEM,
9836                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
9837                                    NULL,
9838                                    "cannot allocate flow table data entry");
9839                 return NULL;
9840         }
9841         tbl_data->idx = idx;
9842         tbl_data->tunnel = tt_prm->tunnel;
9843         tbl_data->group_id = tt_prm->group_id;
9844         tbl_data->external = !!tt_prm->external;
9845         tbl_data->tunnel_offload = is_tunnel_offload_active(dev);
9846         tbl_data->is_egress = !!key.is_egress;
9847         tbl_data->is_transfer = !!key.is_fdb;
9848         tbl_data->dummy = !!key.dummy;
9849         tbl_data->level = key.level;
9850         tbl_data->id = key.id;
9851         tbl = &tbl_data->tbl;
9852         if (key.dummy)
9853                 return &tbl_data->entry;
9854         if (key.is_fdb)
9855                 domain = sh->fdb_domain;
9856         else if (key.is_egress)
9857                 domain = sh->tx_domain;
9858         else
9859                 domain = sh->rx_domain;
9860         ret = mlx5_flow_os_create_flow_tbl(domain, key.level, &tbl->obj);
9861         if (ret) {
9862                 rte_flow_error_set(error, ENOMEM,
9863                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
9864                                    NULL, "cannot create flow table object");
9865                 mlx5_ipool_free(sh->ipool[MLX5_IPOOL_JUMP], idx);
9866                 return NULL;
9867         }
9868         if (key.level != 0) {
9869                 ret = mlx5_flow_os_create_flow_action_dest_flow_tbl
9870                                         (tbl->obj, &tbl_data->jump.action);
9871                 if (ret) {
9872                         rte_flow_error_set(error, ENOMEM,
9873                                            RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
9874                                            NULL,
9875                                            "cannot create flow jump action");
9876                         mlx5_flow_os_destroy_flow_tbl(tbl->obj);
9877                         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_JUMP], idx);
9878                         return NULL;
9879                 }
9880         }
9881         MKSTR(matcher_name, "%s_%s_%u_%u_matcher_cache",
9882               key.is_fdb ? "FDB" : "NIC", key.is_egress ? "egress" : "ingress",
9883               key.level, key.id);
9884         mlx5_cache_list_init(&tbl_data->matchers, matcher_name, 0, sh,
9885                              flow_dv_matcher_create_cb,
9886                              flow_dv_matcher_match_cb,
9887                              flow_dv_matcher_remove_cb);
9888         return &tbl_data->entry;
9889 }
9890
9891 int
9892 flow_dv_tbl_match_cb(struct mlx5_hlist *list __rte_unused,
9893                      struct mlx5_hlist_entry *entry, uint64_t key64,
9894                      void *cb_ctx __rte_unused)
9895 {
9896         struct mlx5_flow_tbl_data_entry *tbl_data =
9897                 container_of(entry, struct mlx5_flow_tbl_data_entry, entry);
9898         union mlx5_flow_tbl_key key = { .v64 = key64 };
9899
9900         return tbl_data->level != key.level ||
9901                tbl_data->id != key.id ||
9902                tbl_data->dummy != key.dummy ||
9903                tbl_data->is_transfer != !!key.is_fdb ||
9904                tbl_data->is_egress != !!key.is_egress;
9905 }
9906
9907 /**
9908  * Get a flow table.
9909  *
9910  * @param[in, out] dev
9911  *   Pointer to rte_eth_dev structure.
9912  * @param[in] table_level
9913  *   Table level to use.
9914  * @param[in] egress
9915  *   Direction of the table.
9916  * @param[in] transfer
9917  *   E-Switch or NIC flow.
9918  * @param[in] dummy
9919  *   Dummy entry for dv API.
9920  * @param[in] table_id
9921  *   Table id to use.
9922  * @param[out] error
9923  *   pointer to error structure.
9924  *
9925  * @return
9926  *   Returns tables resource based on the index, NULL in case of failed.
9927  */
9928 struct mlx5_flow_tbl_resource *
9929 flow_dv_tbl_resource_get(struct rte_eth_dev *dev,
9930                          uint32_t table_level, uint8_t egress,
9931                          uint8_t transfer,
9932                          bool external,
9933                          const struct mlx5_flow_tunnel *tunnel,
9934                          uint32_t group_id, uint8_t dummy,
9935                          uint32_t table_id,
9936                          struct rte_flow_error *error)
9937 {
9938         struct mlx5_priv *priv = dev->data->dev_private;
9939         union mlx5_flow_tbl_key table_key = {
9940                 {
9941                         .level = table_level,
9942                         .id = table_id,
9943                         .reserved = 0,
9944                         .dummy = !!dummy,
9945                         .is_fdb = !!transfer,
9946                         .is_egress = !!egress,
9947                 }
9948         };
9949         struct mlx5_flow_tbl_tunnel_prm tt_prm = {
9950                 .tunnel = tunnel,
9951                 .group_id = group_id,
9952                 .external = external,
9953         };
9954         struct mlx5_flow_cb_ctx ctx = {
9955                 .dev = dev,
9956                 .error = error,
9957                 .data = &tt_prm,
9958         };
9959         struct mlx5_hlist_entry *entry;
9960         struct mlx5_flow_tbl_data_entry *tbl_data;
9961
9962         entry = mlx5_hlist_register(priv->sh->flow_tbls, table_key.v64, &ctx);
9963         if (!entry) {
9964                 rte_flow_error_set(error, ENOMEM,
9965                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
9966                                    "cannot get table");
9967                 return NULL;
9968         }
9969         DRV_LOG(DEBUG, "table_level %u table_id %u "
9970                 "tunnel %u group %u registered.",
9971                 table_level, table_id,
9972                 tunnel ? tunnel->tunnel_id : 0, group_id);
9973         tbl_data = container_of(entry, struct mlx5_flow_tbl_data_entry, entry);
9974         return &tbl_data->tbl;
9975 }
9976
9977 void
9978 flow_dv_tbl_remove_cb(struct mlx5_hlist *list,
9979                       struct mlx5_hlist_entry *entry)
9980 {
9981         struct mlx5_dev_ctx_shared *sh = list->ctx;
9982         struct mlx5_flow_tbl_data_entry *tbl_data =
9983                 container_of(entry, struct mlx5_flow_tbl_data_entry, entry);
9984
9985         MLX5_ASSERT(entry && sh);
9986         if (tbl_data->jump.action)
9987                 mlx5_flow_os_destroy_flow_action(tbl_data->jump.action);
9988         if (tbl_data->tbl.obj)
9989                 mlx5_flow_os_destroy_flow_tbl(tbl_data->tbl.obj);
9990         if (tbl_data->tunnel_offload && tbl_data->external) {
9991                 struct mlx5_hlist_entry *he;
9992                 struct mlx5_hlist *tunnel_grp_hash;
9993                 struct mlx5_flow_tunnel_hub *thub = sh->tunnel_hub;
9994                 union tunnel_tbl_key tunnel_key = {
9995                         .tunnel_id = tbl_data->tunnel ?
9996                                         tbl_data->tunnel->tunnel_id : 0,
9997                         .group = tbl_data->group_id
9998                 };
9999                 uint32_t table_level = tbl_data->level;
10000
10001                 tunnel_grp_hash = tbl_data->tunnel ?
10002                                         tbl_data->tunnel->groups :
10003                                         thub->groups;
10004                 he = mlx5_hlist_lookup(tunnel_grp_hash, tunnel_key.val, NULL);
10005                 if (he)
10006                         mlx5_hlist_unregister(tunnel_grp_hash, he);
10007                 DRV_LOG(DEBUG,
10008                         "table_level %u id %u tunnel %u group %u released.",
10009                         table_level,
10010                         tbl_data->id,
10011                         tbl_data->tunnel ?
10012                         tbl_data->tunnel->tunnel_id : 0,
10013                         tbl_data->group_id);
10014         }
10015         mlx5_cache_list_destroy(&tbl_data->matchers);
10016         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_JUMP], tbl_data->idx);
10017 }
10018
10019 /**
10020  * Release a flow table.
10021  *
10022  * @param[in] sh
10023  *   Pointer to device shared structure.
10024  * @param[in] tbl
10025  *   Table resource to be released.
10026  *
10027  * @return
10028  *   Returns 0 if table was released, else return 1;
10029  */
10030 static int
10031 flow_dv_tbl_resource_release(struct mlx5_dev_ctx_shared *sh,
10032                              struct mlx5_flow_tbl_resource *tbl)
10033 {
10034         struct mlx5_flow_tbl_data_entry *tbl_data =
10035                 container_of(tbl, struct mlx5_flow_tbl_data_entry, tbl);
10036
10037         if (!tbl)
10038                 return 0;
10039         return mlx5_hlist_unregister(sh->flow_tbls, &tbl_data->entry);
10040 }
10041
10042 int
10043 flow_dv_matcher_match_cb(struct mlx5_cache_list *list __rte_unused,
10044                          struct mlx5_cache_entry *entry, void *cb_ctx)
10045 {
10046         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
10047         struct mlx5_flow_dv_matcher *ref = ctx->data;
10048         struct mlx5_flow_dv_matcher *cur = container_of(entry, typeof(*cur),
10049                                                         entry);
10050
10051         return cur->crc != ref->crc ||
10052                cur->priority != ref->priority ||
10053                memcmp((const void *)cur->mask.buf,
10054                       (const void *)ref->mask.buf, ref->mask.size);
10055 }
10056
10057 struct mlx5_cache_entry *
10058 flow_dv_matcher_create_cb(struct mlx5_cache_list *list,
10059                           struct mlx5_cache_entry *entry __rte_unused,
10060                           void *cb_ctx)
10061 {
10062         struct mlx5_dev_ctx_shared *sh = list->ctx;
10063         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
10064         struct mlx5_flow_dv_matcher *ref = ctx->data;
10065         struct mlx5_flow_dv_matcher *cache;
10066         struct mlx5dv_flow_matcher_attr dv_attr = {
10067                 .type = IBV_FLOW_ATTR_NORMAL,
10068                 .match_mask = (void *)&ref->mask,
10069         };
10070         struct mlx5_flow_tbl_data_entry *tbl = container_of(ref->tbl,
10071                                                             typeof(*tbl), tbl);
10072         int ret;
10073
10074         cache = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*cache), 0, SOCKET_ID_ANY);
10075         if (!cache) {
10076                 rte_flow_error_set(ctx->error, ENOMEM,
10077                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
10078                                    "cannot create matcher");
10079                 return NULL;
10080         }
10081         *cache = *ref;
10082         dv_attr.match_criteria_enable =
10083                 flow_dv_matcher_enable(cache->mask.buf);
10084         dv_attr.priority = ref->priority;
10085         if (tbl->is_egress)
10086                 dv_attr.flags |= IBV_FLOW_ATTR_FLAGS_EGRESS;
10087         ret = mlx5_flow_os_create_flow_matcher(sh->ctx, &dv_attr, tbl->tbl.obj,
10088                                                &cache->matcher_object);
10089         if (ret) {
10090                 mlx5_free(cache);
10091                 rte_flow_error_set(ctx->error, ENOMEM,
10092                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
10093                                    "cannot create matcher");
10094                 return NULL;
10095         }
10096         return &cache->entry;
10097 }
10098
10099 /**
10100  * Register the flow matcher.
10101  *
10102  * @param[in, out] dev
10103  *   Pointer to rte_eth_dev structure.
10104  * @param[in, out] matcher
10105  *   Pointer to flow matcher.
10106  * @param[in, out] key
10107  *   Pointer to flow table key.
10108  * @parm[in, out] dev_flow
10109  *   Pointer to the dev_flow.
10110  * @param[out] error
10111  *   pointer to error structure.
10112  *
10113  * @return
10114  *   0 on success otherwise -errno and errno is set.
10115  */
10116 static int
10117 flow_dv_matcher_register(struct rte_eth_dev *dev,
10118                          struct mlx5_flow_dv_matcher *ref,
10119                          union mlx5_flow_tbl_key *key,
10120                          struct mlx5_flow *dev_flow,
10121                          const struct mlx5_flow_tunnel *tunnel,
10122                          uint32_t group_id,
10123                          struct rte_flow_error *error)
10124 {
10125         struct mlx5_cache_entry *entry;
10126         struct mlx5_flow_dv_matcher *cache;
10127         struct mlx5_flow_tbl_resource *tbl;
10128         struct mlx5_flow_tbl_data_entry *tbl_data;
10129         struct mlx5_flow_cb_ctx ctx = {
10130                 .error = error,
10131                 .data = ref,
10132         };
10133
10134         /**
10135          * tunnel offload API requires this registration for cases when
10136          * tunnel match rule was inserted before tunnel set rule.
10137          */
10138         tbl = flow_dv_tbl_resource_get(dev, key->level,
10139                                        key->is_egress, key->is_fdb,
10140                                        dev_flow->external, tunnel,
10141                                        group_id, 0, key->id, error);
10142         if (!tbl)
10143                 return -rte_errno;      /* No need to refill the error info */
10144         tbl_data = container_of(tbl, struct mlx5_flow_tbl_data_entry, tbl);
10145         ref->tbl = tbl;
10146         entry = mlx5_cache_register(&tbl_data->matchers, &ctx);
10147         if (!entry) {
10148                 flow_dv_tbl_resource_release(MLX5_SH(dev), tbl);
10149                 return rte_flow_error_set(error, ENOMEM,
10150                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
10151                                           "cannot allocate ref memory");
10152         }
10153         cache = container_of(entry, typeof(*cache), entry);
10154         dev_flow->handle->dvh.matcher = cache;
10155         return 0;
10156 }
10157
10158 struct mlx5_hlist_entry *
10159 flow_dv_tag_create_cb(struct mlx5_hlist *list, uint64_t key, void *ctx)
10160 {
10161         struct mlx5_dev_ctx_shared *sh = list->ctx;
10162         struct rte_flow_error *error = ctx;
10163         struct mlx5_flow_dv_tag_resource *entry;
10164         uint32_t idx = 0;
10165         int ret;
10166
10167         entry = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_TAG], &idx);
10168         if (!entry) {
10169                 rte_flow_error_set(error, ENOMEM,
10170                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
10171                                    "cannot allocate resource memory");
10172                 return NULL;
10173         }
10174         entry->idx = idx;
10175         entry->tag_id = key;
10176         ret = mlx5_flow_os_create_flow_action_tag(key,
10177                                                   &entry->action);
10178         if (ret) {
10179                 mlx5_ipool_free(sh->ipool[MLX5_IPOOL_TAG], idx);
10180                 rte_flow_error_set(error, ENOMEM,
10181                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
10182                                    NULL, "cannot create action");
10183                 return NULL;
10184         }
10185         return &entry->entry;
10186 }
10187
10188 int
10189 flow_dv_tag_match_cb(struct mlx5_hlist *list __rte_unused,
10190                      struct mlx5_hlist_entry *entry, uint64_t key,
10191                      void *cb_ctx __rte_unused)
10192 {
10193         struct mlx5_flow_dv_tag_resource *tag =
10194                 container_of(entry, struct mlx5_flow_dv_tag_resource, entry);
10195
10196         return key != tag->tag_id;
10197 }
10198
10199 /**
10200  * Find existing tag resource or create and register a new one.
10201  *
10202  * @param dev[in, out]
10203  *   Pointer to rte_eth_dev structure.
10204  * @param[in, out] tag_be24
10205  *   Tag value in big endian then R-shift 8.
10206  * @parm[in, out] dev_flow
10207  *   Pointer to the dev_flow.
10208  * @param[out] error
10209  *   pointer to error structure.
10210  *
10211  * @return
10212  *   0 on success otherwise -errno and errno is set.
10213  */
10214 static int
10215 flow_dv_tag_resource_register
10216                         (struct rte_eth_dev *dev,
10217                          uint32_t tag_be24,
10218                          struct mlx5_flow *dev_flow,
10219                          struct rte_flow_error *error)
10220 {
10221         struct mlx5_priv *priv = dev->data->dev_private;
10222         struct mlx5_flow_dv_tag_resource *cache_resource;
10223         struct mlx5_hlist_entry *entry;
10224
10225         entry = mlx5_hlist_register(priv->sh->tag_table, tag_be24, error);
10226         if (entry) {
10227                 cache_resource = container_of
10228                         (entry, struct mlx5_flow_dv_tag_resource, entry);
10229                 dev_flow->handle->dvh.rix_tag = cache_resource->idx;
10230                 dev_flow->dv.tag_resource = cache_resource;
10231                 return 0;
10232         }
10233         return -rte_errno;
10234 }
10235
10236 void
10237 flow_dv_tag_remove_cb(struct mlx5_hlist *list,
10238                       struct mlx5_hlist_entry *entry)
10239 {
10240         struct mlx5_dev_ctx_shared *sh = list->ctx;
10241         struct mlx5_flow_dv_tag_resource *tag =
10242                 container_of(entry, struct mlx5_flow_dv_tag_resource, entry);
10243
10244         MLX5_ASSERT(tag && sh && tag->action);
10245         claim_zero(mlx5_flow_os_destroy_flow_action(tag->action));
10246         DRV_LOG(DEBUG, "Tag %p: removed.", (void *)tag);
10247         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_TAG], tag->idx);
10248 }
10249
10250 /**
10251  * Release the tag.
10252  *
10253  * @param dev
10254  *   Pointer to Ethernet device.
10255  * @param tag_idx
10256  *   Tag index.
10257  *
10258  * @return
10259  *   1 while a reference on it exists, 0 when freed.
10260  */
10261 static int
10262 flow_dv_tag_release(struct rte_eth_dev *dev,
10263                     uint32_t tag_idx)
10264 {
10265         struct mlx5_priv *priv = dev->data->dev_private;
10266         struct mlx5_flow_dv_tag_resource *tag;
10267
10268         tag = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_TAG], tag_idx);
10269         if (!tag)
10270                 return 0;
10271         DRV_LOG(DEBUG, "port %u tag %p: refcnt %d--",
10272                 dev->data->port_id, (void *)tag, tag->entry.ref_cnt);
10273         return mlx5_hlist_unregister(priv->sh->tag_table, &tag->entry);
10274 }
10275
10276 /**
10277  * Translate port ID action to vport.
10278  *
10279  * @param[in] dev
10280  *   Pointer to rte_eth_dev structure.
10281  * @param[in] action
10282  *   Pointer to the port ID action.
10283  * @param[out] dst_port_id
10284  *   The target port ID.
10285  * @param[out] error
10286  *   Pointer to the error structure.
10287  *
10288  * @return
10289  *   0 on success, a negative errno value otherwise and rte_errno is set.
10290  */
10291 static int
10292 flow_dv_translate_action_port_id(struct rte_eth_dev *dev,
10293                                  const struct rte_flow_action *action,
10294                                  uint32_t *dst_port_id,
10295                                  struct rte_flow_error *error)
10296 {
10297         uint32_t port;
10298         struct mlx5_priv *priv;
10299         const struct rte_flow_action_port_id *conf =
10300                         (const struct rte_flow_action_port_id *)action->conf;
10301
10302         port = conf->original ? dev->data->port_id : conf->id;
10303         priv = mlx5_port_to_eswitch_info(port, false);
10304         if (!priv)
10305                 return rte_flow_error_set(error, -rte_errno,
10306                                           RTE_FLOW_ERROR_TYPE_ACTION,
10307                                           NULL,
10308                                           "No eswitch info was found for port");
10309 #ifdef HAVE_MLX5DV_DR_DEVX_PORT
10310         /*
10311          * This parameter is transferred to
10312          * mlx5dv_dr_action_create_dest_ib_port().
10313          */
10314         *dst_port_id = priv->dev_port;
10315 #else
10316         /*
10317          * Legacy mode, no LAG configurations is supported.
10318          * This parameter is transferred to
10319          * mlx5dv_dr_action_create_dest_vport().
10320          */
10321         *dst_port_id = priv->vport_id;
10322 #endif
10323         return 0;
10324 }
10325
10326 /**
10327  * Create a counter with aging configuration.
10328  *
10329  * @param[in] dev
10330  *   Pointer to rte_eth_dev structure.
10331  * @param[in] dev_flow
10332  *   Pointer to the mlx5_flow.
10333  * @param[out] count
10334  *   Pointer to the counter action configuration.
10335  * @param[in] age
10336  *   Pointer to the aging action configuration.
10337  *
10338  * @return
10339  *   Index to flow counter on success, 0 otherwise.
10340  */
10341 static uint32_t
10342 flow_dv_translate_create_counter(struct rte_eth_dev *dev,
10343                                 struct mlx5_flow *dev_flow,
10344                                 const struct rte_flow_action_count *count,
10345                                 const struct rte_flow_action_age *age)
10346 {
10347         uint32_t counter;
10348         struct mlx5_age_param *age_param;
10349
10350         if (count && count->shared)
10351                 counter = flow_dv_counter_get_shared(dev, count->id);
10352         else
10353                 counter = flow_dv_counter_alloc(dev, !!age);
10354         if (!counter || age == NULL)
10355                 return counter;
10356         age_param = flow_dv_counter_idx_get_age(dev, counter);
10357         age_param->context = age->context ? age->context :
10358                 (void *)(uintptr_t)(dev_flow->flow_idx);
10359         age_param->timeout = age->timeout;
10360         age_param->port_id = dev->data->port_id;
10361         __atomic_store_n(&age_param->sec_since_last_hit, 0, __ATOMIC_RELAXED);
10362         __atomic_store_n(&age_param->state, AGE_CANDIDATE, __ATOMIC_RELAXED);
10363         return counter;
10364 }
10365
10366 /**
10367  * Add Tx queue matcher
10368  *
10369  * @param[in] dev
10370  *   Pointer to the dev struct.
10371  * @param[in, out] matcher
10372  *   Flow matcher.
10373  * @param[in, out] key
10374  *   Flow matcher value.
10375  * @param[in] item
10376  *   Flow pattern to translate.
10377  * @param[in] inner
10378  *   Item is inner pattern.
10379  */
10380 static void
10381 flow_dv_translate_item_tx_queue(struct rte_eth_dev *dev,
10382                                 void *matcher, void *key,
10383                                 const struct rte_flow_item *item)
10384 {
10385         const struct mlx5_rte_flow_item_tx_queue *queue_m;
10386         const struct mlx5_rte_flow_item_tx_queue *queue_v;
10387         void *misc_m =
10388                 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
10389         void *misc_v =
10390                 MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
10391         struct mlx5_txq_ctrl *txq;
10392         uint32_t queue;
10393
10394
10395         queue_m = (const void *)item->mask;
10396         if (!queue_m)
10397                 return;
10398         queue_v = (const void *)item->spec;
10399         if (!queue_v)
10400                 return;
10401         txq = mlx5_txq_get(dev, queue_v->queue);
10402         if (!txq)
10403                 return;
10404         queue = txq->obj->sq->id;
10405         MLX5_SET(fte_match_set_misc, misc_m, source_sqn, queue_m->queue);
10406         MLX5_SET(fte_match_set_misc, misc_v, source_sqn,
10407                  queue & queue_m->queue);
10408         mlx5_txq_release(dev, queue_v->queue);
10409 }
10410
10411 /**
10412  * Set the hash fields according to the @p flow information.
10413  *
10414  * @param[in] dev_flow
10415  *   Pointer to the mlx5_flow.
10416  * @param[in] rss_desc
10417  *   Pointer to the mlx5_flow_rss_desc.
10418  */
10419 static void
10420 flow_dv_hashfields_set(struct mlx5_flow *dev_flow,
10421                        struct mlx5_flow_rss_desc *rss_desc)
10422 {
10423         uint64_t items = dev_flow->handle->layers;
10424         int rss_inner = 0;
10425         uint64_t rss_types = rte_eth_rss_hf_refine(rss_desc->types);
10426
10427         dev_flow->hash_fields = 0;
10428 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
10429         if (rss_desc->level >= 2) {
10430                 dev_flow->hash_fields |= IBV_RX_HASH_INNER;
10431                 rss_inner = 1;
10432         }
10433 #endif
10434         if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L3_IPV4)) ||
10435             (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L3_IPV4))) {
10436                 if (rss_types & MLX5_IPV4_LAYER_TYPES) {
10437                         if (rss_types & ETH_RSS_L3_SRC_ONLY)
10438                                 dev_flow->hash_fields |= IBV_RX_HASH_SRC_IPV4;
10439                         else if (rss_types & ETH_RSS_L3_DST_ONLY)
10440                                 dev_flow->hash_fields |= IBV_RX_HASH_DST_IPV4;
10441                         else
10442                                 dev_flow->hash_fields |= MLX5_IPV4_IBV_RX_HASH;
10443                 }
10444         } else if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L3_IPV6)) ||
10445                    (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L3_IPV6))) {
10446                 if (rss_types & MLX5_IPV6_LAYER_TYPES) {
10447                         if (rss_types & ETH_RSS_L3_SRC_ONLY)
10448                                 dev_flow->hash_fields |= IBV_RX_HASH_SRC_IPV6;
10449                         else if (rss_types & ETH_RSS_L3_DST_ONLY)
10450                                 dev_flow->hash_fields |= IBV_RX_HASH_DST_IPV6;
10451                         else
10452                                 dev_flow->hash_fields |= MLX5_IPV6_IBV_RX_HASH;
10453                 }
10454         }
10455         if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L4_UDP)) ||
10456             (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L4_UDP))) {
10457                 if (rss_types & ETH_RSS_UDP) {
10458                         if (rss_types & ETH_RSS_L4_SRC_ONLY)
10459                                 dev_flow->hash_fields |=
10460                                                 IBV_RX_HASH_SRC_PORT_UDP;
10461                         else if (rss_types & ETH_RSS_L4_DST_ONLY)
10462                                 dev_flow->hash_fields |=
10463                                                 IBV_RX_HASH_DST_PORT_UDP;
10464                         else
10465                                 dev_flow->hash_fields |= MLX5_UDP_IBV_RX_HASH;
10466                 }
10467         } else if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L4_TCP)) ||
10468                    (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L4_TCP))) {
10469                 if (rss_types & ETH_RSS_TCP) {
10470                         if (rss_types & ETH_RSS_L4_SRC_ONLY)
10471                                 dev_flow->hash_fields |=
10472                                                 IBV_RX_HASH_SRC_PORT_TCP;
10473                         else if (rss_types & ETH_RSS_L4_DST_ONLY)
10474                                 dev_flow->hash_fields |=
10475                                                 IBV_RX_HASH_DST_PORT_TCP;
10476                         else
10477                                 dev_flow->hash_fields |= MLX5_TCP_IBV_RX_HASH;
10478                 }
10479         }
10480 }
10481
10482 /**
10483  * Prepare an Rx Hash queue.
10484  *
10485  * @param dev
10486  *   Pointer to Ethernet device.
10487  * @param[in] dev_flow
10488  *   Pointer to the mlx5_flow.
10489  * @param[in] rss_desc
10490  *   Pointer to the mlx5_flow_rss_desc.
10491  * @param[out] hrxq_idx
10492  *   Hash Rx queue index.
10493  *
10494  * @return
10495  *   The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
10496  */
10497 static struct mlx5_hrxq *
10498 flow_dv_hrxq_prepare(struct rte_eth_dev *dev,
10499                      struct mlx5_flow *dev_flow,
10500                      struct mlx5_flow_rss_desc *rss_desc,
10501                      uint32_t *hrxq_idx)
10502 {
10503         struct mlx5_priv *priv = dev->data->dev_private;
10504         struct mlx5_flow_handle *dh = dev_flow->handle;
10505         struct mlx5_hrxq *hrxq;
10506
10507         MLX5_ASSERT(rss_desc->queue_num);
10508         rss_desc->key_len = MLX5_RSS_HASH_KEY_LEN;
10509         rss_desc->hash_fields = dev_flow->hash_fields;
10510         rss_desc->tunnel = !!(dh->layers & MLX5_FLOW_LAYER_TUNNEL);
10511         rss_desc->shared_rss = 0;
10512         *hrxq_idx = mlx5_hrxq_get(dev, rss_desc);
10513         if (!*hrxq_idx)
10514                 return NULL;
10515         hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ],
10516                               *hrxq_idx);
10517         return hrxq;
10518 }
10519
10520 /**
10521  * Release sample sub action resource.
10522  *
10523  * @param[in, out] dev
10524  *   Pointer to rte_eth_dev structure.
10525  * @param[in] act_res
10526  *   Pointer to sample sub action resource.
10527  */
10528 static void
10529 flow_dv_sample_sub_actions_release(struct rte_eth_dev *dev,
10530                                    struct mlx5_flow_sub_actions_idx *act_res)
10531 {
10532         if (act_res->rix_hrxq) {
10533                 mlx5_hrxq_release(dev, act_res->rix_hrxq);
10534                 act_res->rix_hrxq = 0;
10535         }
10536         if (act_res->rix_encap_decap) {
10537                 flow_dv_encap_decap_resource_release(dev,
10538                                                      act_res->rix_encap_decap);
10539                 act_res->rix_encap_decap = 0;
10540         }
10541         if (act_res->rix_port_id_action) {
10542                 flow_dv_port_id_action_resource_release(dev,
10543                                                 act_res->rix_port_id_action);
10544                 act_res->rix_port_id_action = 0;
10545         }
10546         if (act_res->rix_tag) {
10547                 flow_dv_tag_release(dev, act_res->rix_tag);
10548                 act_res->rix_tag = 0;
10549         }
10550         if (act_res->rix_jump) {
10551                 flow_dv_jump_tbl_resource_release(dev, act_res->rix_jump);
10552                 act_res->rix_jump = 0;
10553         }
10554 }
10555
10556 int
10557 flow_dv_sample_match_cb(struct mlx5_cache_list *list __rte_unused,
10558                         struct mlx5_cache_entry *entry, void *cb_ctx)
10559 {
10560         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
10561         struct rte_eth_dev *dev = ctx->dev;
10562         struct mlx5_flow_dv_sample_resource *resource = ctx->data;
10563         struct mlx5_flow_dv_sample_resource *cache_resource =
10564                         container_of(entry, typeof(*cache_resource), entry);
10565
10566         if (resource->ratio == cache_resource->ratio &&
10567             resource->ft_type == cache_resource->ft_type &&
10568             resource->ft_id == cache_resource->ft_id &&
10569             resource->set_action == cache_resource->set_action &&
10570             !memcmp((void *)&resource->sample_act,
10571                     (void *)&cache_resource->sample_act,
10572                     sizeof(struct mlx5_flow_sub_actions_list))) {
10573                 /*
10574                  * Existing sample action should release the prepared
10575                  * sub-actions reference counter.
10576                  */
10577                 flow_dv_sample_sub_actions_release(dev,
10578                                                 &resource->sample_idx);
10579                 return 0;
10580         }
10581         return 1;
10582 }
10583
10584 struct mlx5_cache_entry *
10585 flow_dv_sample_create_cb(struct mlx5_cache_list *list __rte_unused,
10586                          struct mlx5_cache_entry *entry __rte_unused,
10587                          void *cb_ctx)
10588 {
10589         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
10590         struct rte_eth_dev *dev = ctx->dev;
10591         struct mlx5_flow_dv_sample_resource *resource = ctx->data;
10592         void **sample_dv_actions = resource->sub_actions;
10593         struct mlx5_flow_dv_sample_resource *cache_resource;
10594         struct mlx5dv_dr_flow_sampler_attr sampler_attr;
10595         struct mlx5_priv *priv = dev->data->dev_private;
10596         struct mlx5_dev_ctx_shared *sh = priv->sh;
10597         struct mlx5_flow_tbl_resource *tbl;
10598         uint32_t idx = 0;
10599         const uint32_t next_ft_step = 1;
10600         uint32_t next_ft_id = resource->ft_id + next_ft_step;
10601         uint8_t is_egress = 0;
10602         uint8_t is_transfer = 0;
10603         struct rte_flow_error *error = ctx->error;
10604
10605         /* Register new sample resource. */
10606         cache_resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_SAMPLE], &idx);
10607         if (!cache_resource) {
10608                 rte_flow_error_set(error, ENOMEM,
10609                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
10610                                           NULL,
10611                                           "cannot allocate resource memory");
10612                 return NULL;
10613         }
10614         *cache_resource = *resource;
10615         /* Create normal path table level */
10616         if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
10617                 is_transfer = 1;
10618         else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_TX)
10619                 is_egress = 1;
10620         tbl = flow_dv_tbl_resource_get(dev, next_ft_id,
10621                                         is_egress, is_transfer,
10622                                         true, NULL, 0, 0, 0, error);
10623         if (!tbl) {
10624                 rte_flow_error_set(error, ENOMEM,
10625                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
10626                                           NULL,
10627                                           "fail to create normal path table "
10628                                           "for sample");
10629                 goto error;
10630         }
10631         cache_resource->normal_path_tbl = tbl;
10632         if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB) {
10633                 if (!sh->default_miss_action) {
10634                         rte_flow_error_set(error, ENOMEM,
10635                                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
10636                                                 NULL,
10637                                                 "default miss action was not "
10638                                                 "created");
10639                         goto error;
10640                 }
10641                 sample_dv_actions[resource->sample_act.actions_num++] =
10642                                                 sh->default_miss_action;
10643         }
10644         /* Create a DR sample action */
10645         sampler_attr.sample_ratio = cache_resource->ratio;
10646         sampler_attr.default_next_table = tbl->obj;
10647         sampler_attr.num_sample_actions = resource->sample_act.actions_num;
10648         sampler_attr.sample_actions = (struct mlx5dv_dr_action **)
10649                                                         &sample_dv_actions[0];
10650         sampler_attr.action = cache_resource->set_action;
10651         if (mlx5_os_flow_dr_create_flow_action_sampler
10652                         (&sampler_attr, &cache_resource->verbs_action)) {
10653                 rte_flow_error_set(error, ENOMEM,
10654                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
10655                                         NULL, "cannot create sample action");
10656                 goto error;
10657         }
10658         cache_resource->idx = idx;
10659         cache_resource->dev = dev;
10660         return &cache_resource->entry;
10661 error:
10662         if (cache_resource->ft_type != MLX5DV_FLOW_TABLE_TYPE_FDB)
10663                 flow_dv_sample_sub_actions_release(dev,
10664                                                    &cache_resource->sample_idx);
10665         if (cache_resource->normal_path_tbl)
10666                 flow_dv_tbl_resource_release(MLX5_SH(dev),
10667                                 cache_resource->normal_path_tbl);
10668         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_SAMPLE], idx);
10669         return NULL;
10670
10671 }
10672
10673 /**
10674  * Find existing sample resource or create and register a new one.
10675  *
10676  * @param[in, out] dev
10677  *   Pointer to rte_eth_dev structure.
10678  * @param[in] resource
10679  *   Pointer to sample resource.
10680  * @parm[in, out] dev_flow
10681  *   Pointer to the dev_flow.
10682  * @param[out] error
10683  *   pointer to error structure.
10684  *
10685  * @return
10686  *   0 on success otherwise -errno and errno is set.
10687  */
10688 static int
10689 flow_dv_sample_resource_register(struct rte_eth_dev *dev,
10690                          struct mlx5_flow_dv_sample_resource *resource,
10691                          struct mlx5_flow *dev_flow,
10692                          struct rte_flow_error *error)
10693 {
10694         struct mlx5_flow_dv_sample_resource *cache_resource;
10695         struct mlx5_cache_entry *entry;
10696         struct mlx5_priv *priv = dev->data->dev_private;
10697         struct mlx5_flow_cb_ctx ctx = {
10698                 .dev = dev,
10699                 .error = error,
10700                 .data = resource,
10701         };
10702
10703         entry = mlx5_cache_register(&priv->sh->sample_action_list, &ctx);
10704         if (!entry)
10705                 return -rte_errno;
10706         cache_resource = container_of(entry, typeof(*cache_resource), entry);
10707         dev_flow->handle->dvh.rix_sample = cache_resource->idx;
10708         dev_flow->dv.sample_res = cache_resource;
10709         return 0;
10710 }
10711
10712 int
10713 flow_dv_dest_array_match_cb(struct mlx5_cache_list *list __rte_unused,
10714                             struct mlx5_cache_entry *entry, void *cb_ctx)
10715 {
10716         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
10717         struct mlx5_flow_dv_dest_array_resource *resource = ctx->data;
10718         struct rte_eth_dev *dev = ctx->dev;
10719         struct mlx5_flow_dv_dest_array_resource *cache_resource =
10720                         container_of(entry, typeof(*cache_resource), entry);
10721         uint32_t idx = 0;
10722
10723         if (resource->num_of_dest == cache_resource->num_of_dest &&
10724             resource->ft_type == cache_resource->ft_type &&
10725             !memcmp((void *)cache_resource->sample_act,
10726                     (void *)resource->sample_act,
10727                    (resource->num_of_dest *
10728                    sizeof(struct mlx5_flow_sub_actions_list)))) {
10729                 /*
10730                  * Existing sample action should release the prepared
10731                  * sub-actions reference counter.
10732                  */
10733                 for (idx = 0; idx < resource->num_of_dest; idx++)
10734                         flow_dv_sample_sub_actions_release(dev,
10735                                         &resource->sample_idx[idx]);
10736                 return 0;
10737         }
10738         return 1;
10739 }
10740
10741 struct mlx5_cache_entry *
10742 flow_dv_dest_array_create_cb(struct mlx5_cache_list *list __rte_unused,
10743                          struct mlx5_cache_entry *entry __rte_unused,
10744                          void *cb_ctx)
10745 {
10746         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
10747         struct rte_eth_dev *dev = ctx->dev;
10748         struct mlx5_flow_dv_dest_array_resource *cache_resource;
10749         struct mlx5_flow_dv_dest_array_resource *resource = ctx->data;
10750         struct mlx5dv_dr_action_dest_attr *dest_attr[MLX5_MAX_DEST_NUM] = { 0 };
10751         struct mlx5dv_dr_action_dest_reformat dest_reformat[MLX5_MAX_DEST_NUM];
10752         struct mlx5_priv *priv = dev->data->dev_private;
10753         struct mlx5_dev_ctx_shared *sh = priv->sh;
10754         struct mlx5_flow_sub_actions_list *sample_act;
10755         struct mlx5dv_dr_domain *domain;
10756         uint32_t idx = 0, res_idx = 0;
10757         struct rte_flow_error *error = ctx->error;
10758         uint64_t action_flags;
10759         int ret;
10760
10761         /* Register new destination array resource. */
10762         cache_resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_DEST_ARRAY],
10763                                             &res_idx);
10764         if (!cache_resource) {
10765                 rte_flow_error_set(error, ENOMEM,
10766                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
10767                                           NULL,
10768                                           "cannot allocate resource memory");
10769                 return NULL;
10770         }
10771         *cache_resource = *resource;
10772         if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
10773                 domain = sh->fdb_domain;
10774         else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_RX)
10775                 domain = sh->rx_domain;
10776         else
10777                 domain = sh->tx_domain;
10778         for (idx = 0; idx < resource->num_of_dest; idx++) {
10779                 dest_attr[idx] = (struct mlx5dv_dr_action_dest_attr *)
10780                                  mlx5_malloc(MLX5_MEM_ZERO,
10781                                  sizeof(struct mlx5dv_dr_action_dest_attr),
10782                                  0, SOCKET_ID_ANY);
10783                 if (!dest_attr[idx]) {
10784                         rte_flow_error_set(error, ENOMEM,
10785                                            RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
10786                                            NULL,
10787                                            "cannot allocate resource memory");
10788                         goto error;
10789                 }
10790                 dest_attr[idx]->type = MLX5DV_DR_ACTION_DEST;
10791                 sample_act = &resource->sample_act[idx];
10792                 action_flags = sample_act->action_flags;
10793                 switch (action_flags) {
10794                 case MLX5_FLOW_ACTION_QUEUE:
10795                         dest_attr[idx]->dest = sample_act->dr_queue_action;
10796                         break;
10797                 case (MLX5_FLOW_ACTION_PORT_ID | MLX5_FLOW_ACTION_ENCAP):
10798                         dest_attr[idx]->type = MLX5DV_DR_ACTION_DEST_REFORMAT;
10799                         dest_attr[idx]->dest_reformat = &dest_reformat[idx];
10800                         dest_attr[idx]->dest_reformat->reformat =
10801                                         sample_act->dr_encap_action;
10802                         dest_attr[idx]->dest_reformat->dest =
10803                                         sample_act->dr_port_id_action;
10804                         break;
10805                 case MLX5_FLOW_ACTION_PORT_ID:
10806                         dest_attr[idx]->dest = sample_act->dr_port_id_action;
10807                         break;
10808                 case MLX5_FLOW_ACTION_JUMP:
10809                         dest_attr[idx]->dest = sample_act->dr_jump_action;
10810                         break;
10811                 default:
10812                         rte_flow_error_set(error, EINVAL,
10813                                            RTE_FLOW_ERROR_TYPE_ACTION,
10814                                            NULL,
10815                                            "unsupported actions type");
10816                         goto error;
10817                 }
10818         }
10819         /* create a dest array actioin */
10820         ret = mlx5_os_flow_dr_create_flow_action_dest_array
10821                                                 (domain,
10822                                                  cache_resource->num_of_dest,
10823                                                  dest_attr,
10824                                                  &cache_resource->action);
10825         if (ret) {
10826                 rte_flow_error_set(error, ENOMEM,
10827                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
10828                                    NULL,
10829                                    "cannot create destination array action");
10830                 goto error;
10831         }
10832         cache_resource->idx = res_idx;
10833         cache_resource->dev = dev;
10834         for (idx = 0; idx < resource->num_of_dest; idx++)
10835                 mlx5_free(dest_attr[idx]);
10836         return &cache_resource->entry;
10837 error:
10838         for (idx = 0; idx < resource->num_of_dest; idx++) {
10839                 flow_dv_sample_sub_actions_release(dev,
10840                                 &cache_resource->sample_idx[idx]);
10841                 if (dest_attr[idx])
10842                         mlx5_free(dest_attr[idx]);
10843         }
10844
10845         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_DEST_ARRAY], res_idx);
10846         return NULL;
10847 }
10848
10849 /**
10850  * Find existing destination array resource or create and register a new one.
10851  *
10852  * @param[in, out] dev
10853  *   Pointer to rte_eth_dev structure.
10854  * @param[in] resource
10855  *   Pointer to destination array resource.
10856  * @parm[in, out] dev_flow
10857  *   Pointer to the dev_flow.
10858  * @param[out] error
10859  *   pointer to error structure.
10860  *
10861  * @return
10862  *   0 on success otherwise -errno and errno is set.
10863  */
10864 static int
10865 flow_dv_dest_array_resource_register(struct rte_eth_dev *dev,
10866                          struct mlx5_flow_dv_dest_array_resource *resource,
10867                          struct mlx5_flow *dev_flow,
10868                          struct rte_flow_error *error)
10869 {
10870         struct mlx5_flow_dv_dest_array_resource *cache_resource;
10871         struct mlx5_priv *priv = dev->data->dev_private;
10872         struct mlx5_cache_entry *entry;
10873         struct mlx5_flow_cb_ctx ctx = {
10874                 .dev = dev,
10875                 .error = error,
10876                 .data = resource,
10877         };
10878
10879         entry = mlx5_cache_register(&priv->sh->dest_array_list, &ctx);
10880         if (!entry)
10881                 return -rte_errno;
10882         cache_resource = container_of(entry, typeof(*cache_resource), entry);
10883         dev_flow->handle->dvh.rix_dest_array = cache_resource->idx;
10884         dev_flow->dv.dest_array_res = cache_resource;
10885         return 0;
10886 }
10887
10888 /**
10889  * Convert Sample action to DV specification.
10890  *
10891  * @param[in] dev
10892  *   Pointer to rte_eth_dev structure.
10893  * @param[in] action
10894  *   Pointer to sample action structure.
10895  * @param[in, out] dev_flow
10896  *   Pointer to the mlx5_flow.
10897  * @param[in] attr
10898  *   Pointer to the flow attributes.
10899  * @param[in, out] num_of_dest
10900  *   Pointer to the num of destination.
10901  * @param[in, out] sample_actions
10902  *   Pointer to sample actions list.
10903  * @param[in, out] res
10904  *   Pointer to sample resource.
10905  * @param[out] error
10906  *   Pointer to the error structure.
10907  *
10908  * @return
10909  *   0 on success, a negative errno value otherwise and rte_errno is set.
10910  */
10911 static int
10912 flow_dv_translate_action_sample(struct rte_eth_dev *dev,
10913                                 const struct rte_flow_action_sample *action,
10914                                 struct mlx5_flow *dev_flow,
10915                                 const struct rte_flow_attr *attr,
10916                                 uint32_t *num_of_dest,
10917                                 void **sample_actions,
10918                                 struct mlx5_flow_dv_sample_resource *res,
10919                                 struct rte_flow_error *error)
10920 {
10921         struct mlx5_priv *priv = dev->data->dev_private;
10922         const struct rte_flow_action *sub_actions;
10923         struct mlx5_flow_sub_actions_list *sample_act;
10924         struct mlx5_flow_sub_actions_idx *sample_idx;
10925         struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
10926         struct rte_flow *flow = dev_flow->flow;
10927         struct mlx5_flow_rss_desc *rss_desc;
10928         uint64_t action_flags = 0;
10929
10930         MLX5_ASSERT(wks);
10931         rss_desc = &wks->rss_desc;
10932         sample_act = &res->sample_act;
10933         sample_idx = &res->sample_idx;
10934         res->ratio = action->ratio;
10935         sub_actions = action->actions;
10936         for (; sub_actions->type != RTE_FLOW_ACTION_TYPE_END; sub_actions++) {
10937                 int type = sub_actions->type;
10938                 uint32_t pre_rix = 0;
10939                 void *pre_r;
10940                 switch (type) {
10941                 case RTE_FLOW_ACTION_TYPE_QUEUE:
10942                 {
10943                         const struct rte_flow_action_queue *queue;
10944                         struct mlx5_hrxq *hrxq;
10945                         uint32_t hrxq_idx;
10946
10947                         queue = sub_actions->conf;
10948                         rss_desc->queue_num = 1;
10949                         rss_desc->queue[0] = queue->index;
10950                         hrxq = flow_dv_hrxq_prepare(dev, dev_flow,
10951                                                     rss_desc, &hrxq_idx);
10952                         if (!hrxq)
10953                                 return rte_flow_error_set
10954                                         (error, rte_errno,
10955                                          RTE_FLOW_ERROR_TYPE_ACTION,
10956                                          NULL,
10957                                          "cannot create fate queue");
10958                         sample_act->dr_queue_action = hrxq->action;
10959                         sample_idx->rix_hrxq = hrxq_idx;
10960                         sample_actions[sample_act->actions_num++] =
10961                                                 hrxq->action;
10962                         (*num_of_dest)++;
10963                         action_flags |= MLX5_FLOW_ACTION_QUEUE;
10964                         if (action_flags & MLX5_FLOW_ACTION_MARK)
10965                                 dev_flow->handle->rix_hrxq = hrxq_idx;
10966                         dev_flow->handle->fate_action =
10967                                         MLX5_FLOW_FATE_QUEUE;
10968                         break;
10969                 }
10970                 case RTE_FLOW_ACTION_TYPE_RSS:
10971                 {
10972                         struct mlx5_hrxq *hrxq;
10973                         uint32_t hrxq_idx;
10974                         const struct rte_flow_action_rss *rss;
10975                         const uint8_t *rss_key;
10976
10977                         rss = sub_actions->conf;
10978                         memcpy(rss_desc->queue, rss->queue,
10979                                rss->queue_num * sizeof(uint16_t));
10980                         rss_desc->queue_num = rss->queue_num;
10981                         /* NULL RSS key indicates default RSS key. */
10982                         rss_key = !rss->key ? rss_hash_default_key : rss->key;
10983                         memcpy(rss_desc->key, rss_key, MLX5_RSS_HASH_KEY_LEN);
10984                         /*
10985                          * rss->level and rss.types should be set in advance
10986                          * when expanding items for RSS.
10987                          */
10988                         flow_dv_hashfields_set(dev_flow, rss_desc);
10989                         hrxq = flow_dv_hrxq_prepare(dev, dev_flow,
10990                                                     rss_desc, &hrxq_idx);
10991                         if (!hrxq)
10992                                 return rte_flow_error_set
10993                                         (error, rte_errno,
10994                                          RTE_FLOW_ERROR_TYPE_ACTION,
10995                                          NULL,
10996                                          "cannot create fate queue");
10997                         sample_act->dr_queue_action = hrxq->action;
10998                         sample_idx->rix_hrxq = hrxq_idx;
10999                         sample_actions[sample_act->actions_num++] =
11000                                                 hrxq->action;
11001                         (*num_of_dest)++;
11002                         action_flags |= MLX5_FLOW_ACTION_RSS;
11003                         if (action_flags & MLX5_FLOW_ACTION_MARK)
11004                                 dev_flow->handle->rix_hrxq = hrxq_idx;
11005                         dev_flow->handle->fate_action =
11006                                         MLX5_FLOW_FATE_QUEUE;
11007                         break;
11008                 }
11009                 case RTE_FLOW_ACTION_TYPE_MARK:
11010                 {
11011                         uint32_t tag_be = mlx5_flow_mark_set
11012                                 (((const struct rte_flow_action_mark *)
11013                                 (sub_actions->conf))->id);
11014
11015                         dev_flow->handle->mark = 1;
11016                         pre_rix = dev_flow->handle->dvh.rix_tag;
11017                         /* Save the mark resource before sample */
11018                         pre_r = dev_flow->dv.tag_resource;
11019                         if (flow_dv_tag_resource_register(dev, tag_be,
11020                                                   dev_flow, error))
11021                                 return -rte_errno;
11022                         MLX5_ASSERT(dev_flow->dv.tag_resource);
11023                         sample_act->dr_tag_action =
11024                                 dev_flow->dv.tag_resource->action;
11025                         sample_idx->rix_tag =
11026                                 dev_flow->handle->dvh.rix_tag;
11027                         sample_actions[sample_act->actions_num++] =
11028                                                 sample_act->dr_tag_action;
11029                         /* Recover the mark resource after sample */
11030                         dev_flow->dv.tag_resource = pre_r;
11031                         dev_flow->handle->dvh.rix_tag = pre_rix;
11032                         action_flags |= MLX5_FLOW_ACTION_MARK;
11033                         break;
11034                 }
11035                 case RTE_FLOW_ACTION_TYPE_COUNT:
11036                 {
11037                         if (!flow->counter) {
11038                                 flow->counter =
11039                                         flow_dv_translate_create_counter(dev,
11040                                                 dev_flow, sub_actions->conf,
11041                                                 0);
11042                                 if (!flow->counter)
11043                                         return rte_flow_error_set
11044                                                 (error, rte_errno,
11045                                                 RTE_FLOW_ERROR_TYPE_ACTION,
11046                                                 NULL,
11047                                                 "cannot create counter"
11048                                                 " object.");
11049                         }
11050                         sample_act->dr_cnt_action =
11051                                   (flow_dv_counter_get_by_idx(dev,
11052                                   flow->counter, NULL))->action;
11053                         sample_actions[sample_act->actions_num++] =
11054                                                 sample_act->dr_cnt_action;
11055                         action_flags |= MLX5_FLOW_ACTION_COUNT;
11056                         break;
11057                 }
11058                 case RTE_FLOW_ACTION_TYPE_PORT_ID:
11059                 {
11060                         struct mlx5_flow_dv_port_id_action_resource
11061                                         port_id_resource;
11062                         uint32_t port_id = 0;
11063
11064                         memset(&port_id_resource, 0, sizeof(port_id_resource));
11065                         /* Save the port id resource before sample */
11066                         pre_rix = dev_flow->handle->rix_port_id_action;
11067                         pre_r = dev_flow->dv.port_id_action;
11068                         if (flow_dv_translate_action_port_id(dev, sub_actions,
11069                                                              &port_id, error))
11070                                 return -rte_errno;
11071                         port_id_resource.port_id = port_id;
11072                         if (flow_dv_port_id_action_resource_register
11073                             (dev, &port_id_resource, dev_flow, error))
11074                                 return -rte_errno;
11075                         sample_act->dr_port_id_action =
11076                                 dev_flow->dv.port_id_action->action;
11077                         sample_idx->rix_port_id_action =
11078                                 dev_flow->handle->rix_port_id_action;
11079                         sample_actions[sample_act->actions_num++] =
11080                                                 sample_act->dr_port_id_action;
11081                         /* Recover the port id resource after sample */
11082                         dev_flow->dv.port_id_action = pre_r;
11083                         dev_flow->handle->rix_port_id_action = pre_rix;
11084                         (*num_of_dest)++;
11085                         action_flags |= MLX5_FLOW_ACTION_PORT_ID;
11086                         break;
11087                 }
11088                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
11089                 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
11090                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
11091                         /* Save the encap resource before sample */
11092                         pre_rix = dev_flow->handle->dvh.rix_encap_decap;
11093                         pre_r = dev_flow->dv.encap_decap;
11094                         if (flow_dv_create_action_l2_encap(dev, sub_actions,
11095                                                            dev_flow,
11096                                                            attr->transfer,
11097                                                            error))
11098                                 return -rte_errno;
11099                         sample_act->dr_encap_action =
11100                                 dev_flow->dv.encap_decap->action;
11101                         sample_idx->rix_encap_decap =
11102                                 dev_flow->handle->dvh.rix_encap_decap;
11103                         sample_actions[sample_act->actions_num++] =
11104                                                 sample_act->dr_encap_action;
11105                         /* Recover the encap resource after sample */
11106                         dev_flow->dv.encap_decap = pre_r;
11107                         dev_flow->handle->dvh.rix_encap_decap = pre_rix;
11108                         action_flags |= MLX5_FLOW_ACTION_ENCAP;
11109                         break;
11110                 default:
11111                         return rte_flow_error_set(error, EINVAL,
11112                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
11113                                 NULL,
11114                                 "Not support for sampler action");
11115                 }
11116         }
11117         sample_act->action_flags = action_flags;
11118         res->ft_id = dev_flow->dv.group;
11119         if (attr->transfer) {
11120                 union {
11121                         uint32_t action_in[MLX5_ST_SZ_DW(set_action_in)];
11122                         uint64_t set_action;
11123                 } action_ctx = { .set_action = 0 };
11124
11125                 res->ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
11126                 MLX5_SET(set_action_in, action_ctx.action_in, action_type,
11127                          MLX5_MODIFICATION_TYPE_SET);
11128                 MLX5_SET(set_action_in, action_ctx.action_in, field,
11129                          MLX5_MODI_META_REG_C_0);
11130                 MLX5_SET(set_action_in, action_ctx.action_in, data,
11131                          priv->vport_meta_tag);
11132                 res->set_action = action_ctx.set_action;
11133         } else if (attr->ingress) {
11134                 res->ft_type = MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
11135         } else {
11136                 res->ft_type = MLX5DV_FLOW_TABLE_TYPE_NIC_TX;
11137         }
11138         return 0;
11139 }
11140
11141 /**
11142  * Convert Sample action to DV specification.
11143  *
11144  * @param[in] dev
11145  *   Pointer to rte_eth_dev structure.
11146  * @param[in, out] dev_flow
11147  *   Pointer to the mlx5_flow.
11148  * @param[in] num_of_dest
11149  *   The num of destination.
11150  * @param[in, out] res
11151  *   Pointer to sample resource.
11152  * @param[in, out] mdest_res
11153  *   Pointer to destination array resource.
11154  * @param[in] sample_actions
11155  *   Pointer to sample path actions list.
11156  * @param[in] action_flags
11157  *   Holds the actions detected until now.
11158  * @param[out] error
11159  *   Pointer to the error structure.
11160  *
11161  * @return
11162  *   0 on success, a negative errno value otherwise and rte_errno is set.
11163  */
11164 static int
11165 flow_dv_create_action_sample(struct rte_eth_dev *dev,
11166                              struct mlx5_flow *dev_flow,
11167                              uint32_t num_of_dest,
11168                              struct mlx5_flow_dv_sample_resource *res,
11169                              struct mlx5_flow_dv_dest_array_resource *mdest_res,
11170                              void **sample_actions,
11171                              uint64_t action_flags,
11172                              struct rte_flow_error *error)
11173 {
11174         /* update normal path action resource into last index of array */
11175         uint32_t dest_index = MLX5_MAX_DEST_NUM - 1;
11176         struct mlx5_flow_sub_actions_list *sample_act =
11177                                         &mdest_res->sample_act[dest_index];
11178         struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
11179         struct mlx5_flow_rss_desc *rss_desc;
11180         uint32_t normal_idx = 0;
11181         struct mlx5_hrxq *hrxq;
11182         uint32_t hrxq_idx;
11183
11184         MLX5_ASSERT(wks);
11185         rss_desc = &wks->rss_desc;
11186         if (num_of_dest > 1) {
11187                 if (sample_act->action_flags & MLX5_FLOW_ACTION_QUEUE) {
11188                         /* Handle QP action for mirroring */
11189                         hrxq = flow_dv_hrxq_prepare(dev, dev_flow,
11190                                                     rss_desc, &hrxq_idx);
11191                         if (!hrxq)
11192                                 return rte_flow_error_set
11193                                      (error, rte_errno,
11194                                       RTE_FLOW_ERROR_TYPE_ACTION,
11195                                       NULL,
11196                                       "cannot create rx queue");
11197                         normal_idx++;
11198                         mdest_res->sample_idx[dest_index].rix_hrxq = hrxq_idx;
11199                         sample_act->dr_queue_action = hrxq->action;
11200                         if (action_flags & MLX5_FLOW_ACTION_MARK)
11201                                 dev_flow->handle->rix_hrxq = hrxq_idx;
11202                         dev_flow->handle->fate_action = MLX5_FLOW_FATE_QUEUE;
11203                 }
11204                 if (sample_act->action_flags & MLX5_FLOW_ACTION_ENCAP) {
11205                         normal_idx++;
11206                         mdest_res->sample_idx[dest_index].rix_encap_decap =
11207                                 dev_flow->handle->dvh.rix_encap_decap;
11208                         sample_act->dr_encap_action =
11209                                 dev_flow->dv.encap_decap->action;
11210                         dev_flow->handle->dvh.rix_encap_decap = 0;
11211                 }
11212                 if (sample_act->action_flags & MLX5_FLOW_ACTION_PORT_ID) {
11213                         normal_idx++;
11214                         mdest_res->sample_idx[dest_index].rix_port_id_action =
11215                                 dev_flow->handle->rix_port_id_action;
11216                         sample_act->dr_port_id_action =
11217                                 dev_flow->dv.port_id_action->action;
11218                         dev_flow->handle->rix_port_id_action = 0;
11219                 }
11220                 if (sample_act->action_flags & MLX5_FLOW_ACTION_JUMP) {
11221                         normal_idx++;
11222                         mdest_res->sample_idx[dest_index].rix_jump =
11223                                 dev_flow->handle->rix_jump;
11224                         sample_act->dr_jump_action =
11225                                 dev_flow->dv.jump->action;
11226                         dev_flow->handle->rix_jump = 0;
11227                 }
11228                 sample_act->actions_num = normal_idx;
11229                 /* update sample action resource into first index of array */
11230                 mdest_res->ft_type = res->ft_type;
11231                 memcpy(&mdest_res->sample_idx[0], &res->sample_idx,
11232                                 sizeof(struct mlx5_flow_sub_actions_idx));
11233                 memcpy(&mdest_res->sample_act[0], &res->sample_act,
11234                                 sizeof(struct mlx5_flow_sub_actions_list));
11235                 mdest_res->num_of_dest = num_of_dest;
11236                 if (flow_dv_dest_array_resource_register(dev, mdest_res,
11237                                                          dev_flow, error))
11238                         return rte_flow_error_set(error, EINVAL,
11239                                                   RTE_FLOW_ERROR_TYPE_ACTION,
11240                                                   NULL, "can't create sample "
11241                                                   "action");
11242         } else {
11243                 res->sub_actions = sample_actions;
11244                 if (flow_dv_sample_resource_register(dev, res, dev_flow, error))
11245                         return rte_flow_error_set(error, EINVAL,
11246                                                   RTE_FLOW_ERROR_TYPE_ACTION,
11247                                                   NULL,
11248                                                   "can't create sample action");
11249         }
11250         return 0;
11251 }
11252
11253 /**
11254  * Remove an ASO age action from age actions list.
11255  *
11256  * @param[in] dev
11257  *   Pointer to the Ethernet device structure.
11258  * @param[in] age
11259  *   Pointer to the aso age action handler.
11260  */
11261 static void
11262 flow_dv_aso_age_remove_from_age(struct rte_eth_dev *dev,
11263                                 struct mlx5_aso_age_action *age)
11264 {
11265         struct mlx5_age_info *age_info;
11266         struct mlx5_age_param *age_param = &age->age_params;
11267         struct mlx5_priv *priv = dev->data->dev_private;
11268         uint16_t expected = AGE_CANDIDATE;
11269
11270         age_info = GET_PORT_AGE_INFO(priv);
11271         if (!__atomic_compare_exchange_n(&age_param->state, &expected,
11272                                          AGE_FREE, false, __ATOMIC_RELAXED,
11273                                          __ATOMIC_RELAXED)) {
11274                 /**
11275                  * We need the lock even it is age timeout,
11276                  * since age action may still in process.
11277                  */
11278                 rte_spinlock_lock(&age_info->aged_sl);
11279                 LIST_REMOVE(age, next);
11280                 rte_spinlock_unlock(&age_info->aged_sl);
11281                 __atomic_store_n(&age_param->state, AGE_FREE, __ATOMIC_RELAXED);
11282         }
11283 }
11284
11285 /**
11286  * Release an ASO age action.
11287  *
11288  * @param[in] dev
11289  *   Pointer to the Ethernet device structure.
11290  * @param[in] age_idx
11291  *   Index of ASO age action to release.
11292  * @param[in] flow
11293  *   True if the release operation is during flow destroy operation.
11294  *   False if the release operation is during action destroy operation.
11295  *
11296  * @return
11297  *   0 when age action was removed, otherwise the number of references.
11298  */
11299 static int
11300 flow_dv_aso_age_release(struct rte_eth_dev *dev, uint32_t age_idx)
11301 {
11302         struct mlx5_priv *priv = dev->data->dev_private;
11303         struct mlx5_aso_age_mng *mng = priv->sh->aso_age_mng;
11304         struct mlx5_aso_age_action *age = flow_aso_age_get_by_idx(dev, age_idx);
11305         uint32_t ret = __atomic_sub_fetch(&age->refcnt, 1, __ATOMIC_RELAXED);
11306
11307         if (!ret) {
11308                 flow_dv_aso_age_remove_from_age(dev, age);
11309                 rte_spinlock_lock(&mng->free_sl);
11310                 LIST_INSERT_HEAD(&mng->free, age, next);
11311                 rte_spinlock_unlock(&mng->free_sl);
11312         }
11313         return ret;
11314 }
11315
11316 /**
11317  * Resize the ASO age pools array by MLX5_CNT_CONTAINER_RESIZE pools.
11318  *
11319  * @param[in] dev
11320  *   Pointer to the Ethernet device structure.
11321  *
11322  * @return
11323  *   0 on success, otherwise negative errno value and rte_errno is set.
11324  */
11325 static int
11326 flow_dv_aso_age_pools_resize(struct rte_eth_dev *dev)
11327 {
11328         struct mlx5_priv *priv = dev->data->dev_private;
11329         struct mlx5_aso_age_mng *mng = priv->sh->aso_age_mng;
11330         void *old_pools = mng->pools;
11331         uint32_t resize = mng->n + MLX5_CNT_CONTAINER_RESIZE;
11332         uint32_t mem_size = sizeof(struct mlx5_aso_age_pool *) * resize;
11333         void *pools = mlx5_malloc(MLX5_MEM_ZERO, mem_size, 0, SOCKET_ID_ANY);
11334
11335         if (!pools) {
11336                 rte_errno = ENOMEM;
11337                 return -ENOMEM;
11338         }
11339         if (old_pools) {
11340                 memcpy(pools, old_pools,
11341                        mng->n * sizeof(struct mlx5_flow_counter_pool *));
11342                 mlx5_free(old_pools);
11343         } else {
11344                 /* First ASO flow hit allocation - starting ASO data-path. */
11345                 int ret = mlx5_aso_flow_hit_queue_poll_start(priv->sh);
11346
11347                 if (ret) {
11348                         mlx5_free(pools);
11349                         return ret;
11350                 }
11351         }
11352         mng->n = resize;
11353         mng->pools = pools;
11354         return 0;
11355 }
11356
11357 /**
11358  * Create and initialize a new ASO aging pool.
11359  *
11360  * @param[in] dev
11361  *   Pointer to the Ethernet device structure.
11362  * @param[out] age_free
11363  *   Where to put the pointer of a new age action.
11364  *
11365  * @return
11366  *   The age actions pool pointer and @p age_free is set on success,
11367  *   NULL otherwise and rte_errno is set.
11368  */
11369 static struct mlx5_aso_age_pool *
11370 flow_dv_age_pool_create(struct rte_eth_dev *dev,
11371                         struct mlx5_aso_age_action **age_free)
11372 {
11373         struct mlx5_priv *priv = dev->data->dev_private;
11374         struct mlx5_aso_age_mng *mng = priv->sh->aso_age_mng;
11375         struct mlx5_aso_age_pool *pool = NULL;
11376         struct mlx5_devx_obj *obj = NULL;
11377         uint32_t i;
11378
11379         obj = mlx5_devx_cmd_create_flow_hit_aso_obj(priv->sh->ctx,
11380                                                     priv->sh->pdn);
11381         if (!obj) {
11382                 rte_errno = ENODATA;
11383                 DRV_LOG(ERR, "Failed to create flow_hit_aso_obj using DevX.");
11384                 return NULL;
11385         }
11386         pool = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*pool), 0, SOCKET_ID_ANY);
11387         if (!pool) {
11388                 claim_zero(mlx5_devx_cmd_destroy(obj));
11389                 rte_errno = ENOMEM;
11390                 return NULL;
11391         }
11392         pool->flow_hit_aso_obj = obj;
11393         pool->time_of_last_age_check = MLX5_CURR_TIME_SEC;
11394         rte_spinlock_lock(&mng->resize_sl);
11395         pool->index = mng->next;
11396         /* Resize pools array if there is no room for the new pool in it. */
11397         if (pool->index == mng->n && flow_dv_aso_age_pools_resize(dev)) {
11398                 claim_zero(mlx5_devx_cmd_destroy(obj));
11399                 mlx5_free(pool);
11400                 rte_spinlock_unlock(&mng->resize_sl);
11401                 return NULL;
11402         }
11403         mng->pools[pool->index] = pool;
11404         mng->next++;
11405         rte_spinlock_unlock(&mng->resize_sl);
11406         /* Assign the first action in the new pool, the rest go to free list. */
11407         *age_free = &pool->actions[0];
11408         for (i = 1; i < MLX5_ASO_AGE_ACTIONS_PER_POOL; i++) {
11409                 pool->actions[i].offset = i;
11410                 LIST_INSERT_HEAD(&mng->free, &pool->actions[i], next);
11411         }
11412         return pool;
11413 }
11414
11415 /**
11416  * Allocate a ASO aging bit.
11417  *
11418  * @param[in] dev
11419  *   Pointer to the Ethernet device structure.
11420  * @param[out] error
11421  *   Pointer to the error structure.
11422  *
11423  * @return
11424  *   Index to ASO age action on success, 0 otherwise and rte_errno is set.
11425  */
11426 static uint32_t
11427 flow_dv_aso_age_alloc(struct rte_eth_dev *dev, struct rte_flow_error *error)
11428 {
11429         struct mlx5_priv *priv = dev->data->dev_private;
11430         const struct mlx5_aso_age_pool *pool;
11431         struct mlx5_aso_age_action *age_free = NULL;
11432         struct mlx5_aso_age_mng *mng = priv->sh->aso_age_mng;
11433
11434         MLX5_ASSERT(mng);
11435         /* Try to get the next free age action bit. */
11436         rte_spinlock_lock(&mng->free_sl);
11437         age_free = LIST_FIRST(&mng->free);
11438         if (age_free) {
11439                 LIST_REMOVE(age_free, next);
11440         } else if (!flow_dv_age_pool_create(dev, &age_free)) {
11441                 rte_spinlock_unlock(&mng->free_sl);
11442                 rte_flow_error_set(error, rte_errno, RTE_FLOW_ERROR_TYPE_ACTION,
11443                                    NULL, "failed to create ASO age pool");
11444                 return 0; /* 0 is an error. */
11445         }
11446         rte_spinlock_unlock(&mng->free_sl);
11447         pool = container_of
11448           ((const struct mlx5_aso_age_action (*)[MLX5_ASO_AGE_ACTIONS_PER_POOL])
11449                   (age_free - age_free->offset), const struct mlx5_aso_age_pool,
11450                                                                        actions);
11451         if (!age_free->dr_action) {
11452                 int reg_c = mlx5_flow_get_reg_id(dev, MLX5_ASO_FLOW_HIT, 0,
11453                                                  error);
11454
11455                 if (reg_c < 0) {
11456                         rte_flow_error_set(error, rte_errno,
11457                                            RTE_FLOW_ERROR_TYPE_ACTION,
11458                                            NULL, "failed to get reg_c "
11459                                            "for ASO flow hit");
11460                         return 0; /* 0 is an error. */
11461                 }
11462 #ifdef HAVE_MLX5_DR_CREATE_ACTION_ASO
11463                 age_free->dr_action = mlx5_glue->dv_create_flow_action_aso
11464                                 (priv->sh->rx_domain,
11465                                  pool->flow_hit_aso_obj->obj, age_free->offset,
11466                                  MLX5DV_DR_ACTION_FLAGS_ASO_FIRST_HIT_SET,
11467                                  (reg_c - REG_C_0));
11468 #endif /* HAVE_MLX5_DR_CREATE_ACTION_ASO */
11469                 if (!age_free->dr_action) {
11470                         rte_errno = errno;
11471                         rte_spinlock_lock(&mng->free_sl);
11472                         LIST_INSERT_HEAD(&mng->free, age_free, next);
11473                         rte_spinlock_unlock(&mng->free_sl);
11474                         rte_flow_error_set(error, rte_errno,
11475                                            RTE_FLOW_ERROR_TYPE_ACTION,
11476                                            NULL, "failed to create ASO "
11477                                            "flow hit action");
11478                         return 0; /* 0 is an error. */
11479                 }
11480         }
11481         __atomic_store_n(&age_free->refcnt, 1, __ATOMIC_RELAXED);
11482         return pool->index | ((age_free->offset + 1) << 16);
11483 }
11484
11485 /**
11486  * Create a age action using ASO mechanism.
11487  *
11488  * @param[in] dev
11489  *   Pointer to rte_eth_dev structure.
11490  * @param[in] age
11491  *   Pointer to the aging action configuration.
11492  * @param[out] error
11493  *   Pointer to the error structure.
11494  *
11495  * @return
11496  *   Index to flow counter on success, 0 otherwise.
11497  */
11498 static uint32_t
11499 flow_dv_translate_create_aso_age(struct rte_eth_dev *dev,
11500                                  const struct rte_flow_action_age *age,
11501                                  struct rte_flow_error *error)
11502 {
11503         uint32_t age_idx = 0;
11504         struct mlx5_aso_age_action *aso_age;
11505
11506         age_idx = flow_dv_aso_age_alloc(dev, error);
11507         if (!age_idx)
11508                 return 0;
11509         aso_age = flow_aso_age_get_by_idx(dev, age_idx);
11510         aso_age->age_params.context = age->context;
11511         aso_age->age_params.timeout = age->timeout;
11512         aso_age->age_params.port_id = dev->data->port_id;
11513         __atomic_store_n(&aso_age->age_params.sec_since_last_hit, 0,
11514                          __ATOMIC_RELAXED);
11515         __atomic_store_n(&aso_age->age_params.state, AGE_CANDIDATE,
11516                          __ATOMIC_RELAXED);
11517         return age_idx;
11518 }
11519
11520 static void
11521 flow_dv_translate_integrity_l4(const struct rte_flow_item_integrity *mask,
11522                                const struct rte_flow_item_integrity *value,
11523                                void *headers_m, void *headers_v)
11524 {
11525         if (mask->l4_ok) {
11526                 /* application l4_ok filter aggregates all hardware l4 filters
11527                  * therefore hw l4_checksum_ok must be implicitly added here.
11528                  */
11529                 struct rte_flow_item_integrity local_item;
11530
11531                 local_item.l4_csum_ok = 1;
11532                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, l4_checksum_ok,
11533                          local_item.l4_csum_ok);
11534                 if (value->l4_ok) {
11535                         /* application l4_ok = 1 matches sets both hw flags
11536                          * l4_ok and l4_checksum_ok flags to 1.
11537                          */
11538                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
11539                                  l4_checksum_ok, local_item.l4_csum_ok);
11540                         MLX5_SET(fte_match_set_lyr_2_4, headers_m, l4_ok,
11541                                  mask->l4_ok);
11542                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, l4_ok,
11543                                  value->l4_ok);
11544                 } else {
11545                         /* application l4_ok = 0 matches on hw flag
11546                          * l4_checksum_ok = 0 only.
11547                          */
11548                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
11549                                  l4_checksum_ok, 0);
11550                 }
11551         } else if (mask->l4_csum_ok) {
11552                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, l4_checksum_ok,
11553                          mask->l4_csum_ok);
11554                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ipv4_checksum_ok,
11555                          value->l4_csum_ok);
11556         }
11557 }
11558
11559 static void
11560 flow_dv_translate_integrity_l3(const struct rte_flow_item_integrity *mask,
11561                                const struct rte_flow_item_integrity *value,
11562                                void *headers_m, void *headers_v,
11563                                bool is_ipv4)
11564 {
11565         if (mask->l3_ok) {
11566                 /* application l3_ok filter aggregates all hardware l3 filters
11567                  * therefore hw ipv4_checksum_ok must be implicitly added here.
11568                  */
11569                 struct rte_flow_item_integrity local_item;
11570
11571                 local_item.ipv4_csum_ok = !!is_ipv4;
11572                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ipv4_checksum_ok,
11573                          local_item.ipv4_csum_ok);
11574                 if (value->l3_ok) {
11575                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
11576                                  ipv4_checksum_ok, local_item.ipv4_csum_ok);
11577                         MLX5_SET(fte_match_set_lyr_2_4, headers_m, l3_ok,
11578                                  mask->l3_ok);
11579                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, l3_ok,
11580                                  value->l3_ok);
11581                 } else {
11582                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
11583                                  ipv4_checksum_ok, 0);
11584                 }
11585         } else if (mask->ipv4_csum_ok) {
11586                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ipv4_checksum_ok,
11587                          mask->ipv4_csum_ok);
11588                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ipv4_checksum_ok,
11589                          value->ipv4_csum_ok);
11590         }
11591 }
11592
11593 static void
11594 flow_dv_translate_item_integrity(void *matcher, void *key,
11595                                  const struct rte_flow_item *head_item,
11596                                  const struct rte_flow_item *integrity_item)
11597 {
11598         const struct rte_flow_item_integrity *mask = integrity_item->mask;
11599         const struct rte_flow_item_integrity *value = integrity_item->spec;
11600         const struct rte_flow_item *tunnel_item, *end_item, *item;
11601         void *headers_m;
11602         void *headers_v;
11603         uint32_t l3_protocol;
11604
11605         if (!value)
11606                 return;
11607         if (!mask)
11608                 mask = &rte_flow_item_integrity_mask;
11609         if (value->level > 1) {
11610                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
11611                                          inner_headers);
11612                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
11613         } else {
11614                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
11615                                          outer_headers);
11616                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
11617         }
11618         tunnel_item = mlx5_flow_find_tunnel_item(head_item);
11619         if (value->level > 1) {
11620                 /* tunnel item was verified during the item validation */
11621                 item = tunnel_item;
11622                 end_item = mlx5_find_end_item(tunnel_item);
11623         } else {
11624                 item = head_item;
11625                 end_item = tunnel_item ? tunnel_item :
11626                            mlx5_find_end_item(integrity_item);
11627         }
11628         l3_protocol = mask->l3_ok ?
11629                       mlx5_flow_locate_proto_l3(&item, end_item) : 0;
11630         flow_dv_translate_integrity_l3(mask, value, headers_m, headers_v,
11631                                        l3_protocol == RTE_ETHER_TYPE_IPV4);
11632         flow_dv_translate_integrity_l4(mask, value, headers_m, headers_v);
11633 }
11634
11635 /**
11636  * Prepares DV flow counter with aging configuration.
11637  * Gets it by index when exists, creates a new one when doesn't.
11638  *
11639  * @param[in] dev
11640  *   Pointer to rte_eth_dev structure.
11641  * @param[in] dev_flow
11642  *   Pointer to the mlx5_flow.
11643  * @param[in, out] flow
11644  *   Pointer to the sub flow.
11645  * @param[in] count
11646  *   Pointer to the counter action configuration.
11647  * @param[in] age
11648  *   Pointer to the aging action configuration.
11649  * @param[out] error
11650  *   Pointer to the error structure.
11651  *
11652  * @return
11653  *   Pointer to the counter, NULL otherwise.
11654  */
11655 static struct mlx5_flow_counter *
11656 flow_dv_prepare_counter(struct rte_eth_dev *dev,
11657                         struct mlx5_flow *dev_flow,
11658                         struct rte_flow *flow,
11659                         const struct rte_flow_action_count *count,
11660                         const struct rte_flow_action_age *age,
11661                         struct rte_flow_error *error)
11662 {
11663         if (!flow->counter) {
11664                 flow->counter = flow_dv_translate_create_counter(dev, dev_flow,
11665                                                                  count, age);
11666                 if (!flow->counter) {
11667                         rte_flow_error_set(error, rte_errno,
11668                                            RTE_FLOW_ERROR_TYPE_ACTION, NULL,
11669                                            "cannot create counter object.");
11670                         return NULL;
11671                 }
11672         }
11673         return flow_dv_counter_get_by_idx(dev, flow->counter, NULL);
11674 }
11675
11676 /*
11677  * Release an ASO CT action by its own device.
11678  *
11679  * @param[in] dev
11680  *   Pointer to the Ethernet device structure.
11681  * @param[in] idx
11682  *   Index of ASO CT action to release.
11683  *
11684  * @return
11685  *   0 when CT action was removed, otherwise the number of references.
11686  */
11687 static inline int
11688 flow_dv_aso_ct_dev_release(struct rte_eth_dev *dev, uint32_t idx)
11689 {
11690         struct mlx5_priv *priv = dev->data->dev_private;
11691         struct mlx5_aso_ct_pools_mng *mng = priv->sh->ct_mng;
11692         uint32_t ret;
11693         struct mlx5_aso_ct_action *ct = flow_aso_ct_get_by_dev_idx(dev, idx);
11694         enum mlx5_aso_ct_state state =
11695                         __atomic_load_n(&ct->state, __ATOMIC_RELAXED);
11696
11697         /* Cannot release when CT is in the ASO SQ. */
11698         if (state == ASO_CONNTRACK_WAIT || state == ASO_CONNTRACK_QUERY)
11699                 return -1;
11700         ret = __atomic_sub_fetch(&ct->refcnt, 1, __ATOMIC_RELAXED);
11701         if (!ret) {
11702                 if (ct->dr_action_orig) {
11703 #ifdef HAVE_MLX5_DR_ACTION_ASO_CT
11704                         claim_zero(mlx5_glue->destroy_flow_action
11705                                         (ct->dr_action_orig));
11706 #endif
11707                         ct->dr_action_orig = NULL;
11708                 }
11709                 if (ct->dr_action_rply) {
11710 #ifdef HAVE_MLX5_DR_ACTION_ASO_CT
11711                         claim_zero(mlx5_glue->destroy_flow_action
11712                                         (ct->dr_action_rply));
11713 #endif
11714                         ct->dr_action_rply = NULL;
11715                 }
11716                 /* Clear the state to free, no need in 1st allocation. */
11717                 MLX5_ASO_CT_UPDATE_STATE(ct, ASO_CONNTRACK_FREE);
11718                 rte_spinlock_lock(&mng->ct_sl);
11719                 LIST_INSERT_HEAD(&mng->free_cts, ct, next);
11720                 rte_spinlock_unlock(&mng->ct_sl);
11721         }
11722         return (int)ret;
11723 }
11724
11725 static inline int
11726 flow_dv_aso_ct_release(struct rte_eth_dev *dev, uint32_t own_idx)
11727 {
11728         uint16_t owner = (uint16_t)MLX5_INDIRECT_ACT_CT_GET_OWNER(own_idx);
11729         uint32_t idx = MLX5_INDIRECT_ACT_CT_GET_IDX(own_idx);
11730         struct rte_eth_dev *owndev = &rte_eth_devices[owner];
11731         RTE_SET_USED(dev);
11732
11733         MLX5_ASSERT(owner < RTE_MAX_ETHPORTS);
11734         if (dev->data->dev_started != 1)
11735                 return -1;
11736         return flow_dv_aso_ct_dev_release(owndev, idx);
11737 }
11738
11739 /*
11740  * Resize the ASO CT pools array by 64 pools.
11741  *
11742  * @param[in] dev
11743  *   Pointer to the Ethernet device structure.
11744  *
11745  * @return
11746  *   0 on success, otherwise negative errno value and rte_errno is set.
11747  */
11748 static int
11749 flow_dv_aso_ct_pools_resize(struct rte_eth_dev *dev)
11750 {
11751         struct mlx5_priv *priv = dev->data->dev_private;
11752         struct mlx5_aso_ct_pools_mng *mng = priv->sh->ct_mng;
11753         void *old_pools = mng->pools;
11754         /* Magic number now, need a macro. */
11755         uint32_t resize = mng->n + 64;
11756         uint32_t mem_size = sizeof(struct mlx5_aso_ct_pool *) * resize;
11757         void *pools = mlx5_malloc(MLX5_MEM_ZERO, mem_size, 0, SOCKET_ID_ANY);
11758
11759         if (!pools) {
11760                 rte_errno = ENOMEM;
11761                 return -rte_errno;
11762         }
11763         rte_rwlock_write_lock(&mng->resize_rwl);
11764         /* ASO SQ/QP was already initialized in the startup. */
11765         if (old_pools) {
11766                 /* Realloc could be an alternative choice. */
11767                 rte_memcpy(pools, old_pools,
11768                            mng->n * sizeof(struct mlx5_aso_ct_pool *));
11769                 mlx5_free(old_pools);
11770         }
11771         mng->n = resize;
11772         mng->pools = pools;
11773         rte_rwlock_write_unlock(&mng->resize_rwl);
11774         return 0;
11775 }
11776
11777 /*
11778  * Create and initialize a new ASO CT pool.
11779  *
11780  * @param[in] dev
11781  *   Pointer to the Ethernet device structure.
11782  * @param[out] ct_free
11783  *   Where to put the pointer of a new CT action.
11784  *
11785  * @return
11786  *   The CT actions pool pointer and @p ct_free is set on success,
11787  *   NULL otherwise and rte_errno is set.
11788  */
11789 static struct mlx5_aso_ct_pool *
11790 flow_dv_ct_pool_create(struct rte_eth_dev *dev,
11791                        struct mlx5_aso_ct_action **ct_free)
11792 {
11793         struct mlx5_priv *priv = dev->data->dev_private;
11794         struct mlx5_aso_ct_pools_mng *mng = priv->sh->ct_mng;
11795         struct mlx5_aso_ct_pool *pool = NULL;
11796         struct mlx5_devx_obj *obj = NULL;
11797         uint32_t i;
11798         uint32_t log_obj_size = rte_log2_u32(MLX5_ASO_CT_ACTIONS_PER_POOL);
11799
11800         obj = mlx5_devx_cmd_create_conn_track_offload_obj(priv->sh->ctx,
11801                                                 priv->sh->pdn, log_obj_size);
11802         if (!obj) {
11803                 rte_errno = ENODATA;
11804                 DRV_LOG(ERR, "Failed to create conn_track_offload_obj using DevX.");
11805                 return NULL;
11806         }
11807         pool = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*pool), 0, SOCKET_ID_ANY);
11808         if (!pool) {
11809                 rte_errno = ENOMEM;
11810                 claim_zero(mlx5_devx_cmd_destroy(obj));
11811                 return NULL;
11812         }
11813         pool->devx_obj = obj;
11814         pool->index = mng->next;
11815         /* Resize pools array if there is no room for the new pool in it. */
11816         if (pool->index == mng->n && flow_dv_aso_ct_pools_resize(dev)) {
11817                 claim_zero(mlx5_devx_cmd_destroy(obj));
11818                 mlx5_free(pool);
11819                 return NULL;
11820         }
11821         mng->pools[pool->index] = pool;
11822         mng->next++;
11823         /* Assign the first action in the new pool, the rest go to free list. */
11824         *ct_free = &pool->actions[0];
11825         /* Lock outside, the list operation is safe here. */
11826         for (i = 1; i < MLX5_ASO_CT_ACTIONS_PER_POOL; i++) {
11827                 /* refcnt is 0 when allocating the memory. */
11828                 pool->actions[i].offset = i;
11829                 LIST_INSERT_HEAD(&mng->free_cts, &pool->actions[i], next);
11830         }
11831         return pool;
11832 }
11833
11834 /*
11835  * Allocate a ASO CT action from free list.
11836  *
11837  * @param[in] dev
11838  *   Pointer to the Ethernet device structure.
11839  * @param[out] error
11840  *   Pointer to the error structure.
11841  *
11842  * @return
11843  *   Index to ASO CT action on success, 0 otherwise and rte_errno is set.
11844  */
11845 static uint32_t
11846 flow_dv_aso_ct_alloc(struct rte_eth_dev *dev, struct rte_flow_error *error)
11847 {
11848         struct mlx5_priv *priv = dev->data->dev_private;
11849         struct mlx5_aso_ct_pools_mng *mng = priv->sh->ct_mng;
11850         struct mlx5_aso_ct_action *ct = NULL;
11851         struct mlx5_aso_ct_pool *pool;
11852         uint8_t reg_c;
11853         uint32_t ct_idx;
11854
11855         MLX5_ASSERT(mng);
11856         if (!priv->config.devx) {
11857                 rte_errno = ENOTSUP;
11858                 return 0;
11859         }
11860         /* Get a free CT action, if no, a new pool will be created. */
11861         rte_spinlock_lock(&mng->ct_sl);
11862         ct = LIST_FIRST(&mng->free_cts);
11863         if (ct) {
11864                 LIST_REMOVE(ct, next);
11865         } else if (!flow_dv_ct_pool_create(dev, &ct)) {
11866                 rte_spinlock_unlock(&mng->ct_sl);
11867                 rte_flow_error_set(error, rte_errno, RTE_FLOW_ERROR_TYPE_ACTION,
11868                                    NULL, "failed to create ASO CT pool");
11869                 return 0;
11870         }
11871         rte_spinlock_unlock(&mng->ct_sl);
11872         pool = container_of(ct, struct mlx5_aso_ct_pool, actions[ct->offset]);
11873         ct_idx = MLX5_MAKE_CT_IDX(pool->index, ct->offset);
11874         /* 0: inactive, 1: created, 2+: used by flows. */
11875         __atomic_store_n(&ct->refcnt, 1, __ATOMIC_RELAXED);
11876         reg_c = mlx5_flow_get_reg_id(dev, MLX5_ASO_CONNTRACK, 0, error);
11877         if (!ct->dr_action_orig) {
11878 #ifdef HAVE_MLX5_DR_ACTION_ASO_CT
11879                 ct->dr_action_orig = mlx5_glue->dv_create_flow_action_aso
11880                         (priv->sh->rx_domain, pool->devx_obj->obj,
11881                          ct->offset,
11882                          MLX5DV_DR_ACTION_FLAGS_ASO_CT_DIRECTION_INITIATOR,
11883                          reg_c - REG_C_0);
11884 #else
11885                 RTE_SET_USED(reg_c);
11886 #endif
11887                 if (!ct->dr_action_orig) {
11888                         flow_dv_aso_ct_dev_release(dev, ct_idx);
11889                         rte_flow_error_set(error, rte_errno,
11890                                            RTE_FLOW_ERROR_TYPE_ACTION, NULL,
11891                                            "failed to create ASO CT action");
11892                         return 0;
11893                 }
11894         }
11895         if (!ct->dr_action_rply) {
11896 #ifdef HAVE_MLX5_DR_ACTION_ASO_CT
11897                 ct->dr_action_rply = mlx5_glue->dv_create_flow_action_aso
11898                         (priv->sh->rx_domain, pool->devx_obj->obj,
11899                          ct->offset,
11900                          MLX5DV_DR_ACTION_FLAGS_ASO_CT_DIRECTION_RESPONDER,
11901                          reg_c - REG_C_0);
11902 #endif
11903                 if (!ct->dr_action_rply) {
11904                         flow_dv_aso_ct_dev_release(dev, ct_idx);
11905                         rte_flow_error_set(error, rte_errno,
11906                                            RTE_FLOW_ERROR_TYPE_ACTION, NULL,
11907                                            "failed to create ASO CT action");
11908                         return 0;
11909                 }
11910         }
11911         return ct_idx;
11912 }
11913
11914 /*
11915  * Create a conntrack object with context and actions by using ASO mechanism.
11916  *
11917  * @param[in] dev
11918  *   Pointer to rte_eth_dev structure.
11919  * @param[in] pro
11920  *   Pointer to conntrack information profile.
11921  * @param[out] error
11922  *   Pointer to the error structure.
11923  *
11924  * @return
11925  *   Index to conntrack object on success, 0 otherwise.
11926  */
11927 static uint32_t
11928 flow_dv_translate_create_conntrack(struct rte_eth_dev *dev,
11929                                    const struct rte_flow_action_conntrack *pro,
11930                                    struct rte_flow_error *error)
11931 {
11932         struct mlx5_priv *priv = dev->data->dev_private;
11933         struct mlx5_dev_ctx_shared *sh = priv->sh;
11934         struct mlx5_aso_ct_action *ct;
11935         uint32_t idx;
11936
11937         if (!sh->ct_aso_en)
11938                 return rte_flow_error_set(error, ENOTSUP,
11939                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
11940                                           "Connection is not supported");
11941         idx = flow_dv_aso_ct_alloc(dev, error);
11942         if (!idx)
11943                 return rte_flow_error_set(error, rte_errno,
11944                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
11945                                           "Failed to allocate CT object");
11946         ct = flow_aso_ct_get_by_dev_idx(dev, idx);
11947         if (mlx5_aso_ct_update_by_wqe(sh, ct, pro))
11948                 return rte_flow_error_set(error, EBUSY,
11949                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
11950                                           "Failed to update CT");
11951         ct->is_original = !!pro->is_original_dir;
11952         ct->peer = pro->peer_port;
11953         return idx;
11954 }
11955
11956 /**
11957  * Fill the flow with DV spec, lock free
11958  * (mutex should be acquired by caller).
11959  *
11960  * @param[in] dev
11961  *   Pointer to rte_eth_dev structure.
11962  * @param[in, out] dev_flow
11963  *   Pointer to the sub flow.
11964  * @param[in] attr
11965  *   Pointer to the flow attributes.
11966  * @param[in] items
11967  *   Pointer to the list of items.
11968  * @param[in] actions
11969  *   Pointer to the list of actions.
11970  * @param[out] error
11971  *   Pointer to the error structure.
11972  *
11973  * @return
11974  *   0 on success, a negative errno value otherwise and rte_errno is set.
11975  */
11976 static int
11977 flow_dv_translate(struct rte_eth_dev *dev,
11978                   struct mlx5_flow *dev_flow,
11979                   const struct rte_flow_attr *attr,
11980                   const struct rte_flow_item items[],
11981                   const struct rte_flow_action actions[],
11982                   struct rte_flow_error *error)
11983 {
11984         struct mlx5_priv *priv = dev->data->dev_private;
11985         struct mlx5_dev_config *dev_conf = &priv->config;
11986         struct rte_flow *flow = dev_flow->flow;
11987         struct mlx5_flow_handle *handle = dev_flow->handle;
11988         struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
11989         struct mlx5_flow_rss_desc *rss_desc;
11990         uint64_t item_flags = 0;
11991         uint64_t last_item = 0;
11992         uint64_t action_flags = 0;
11993         struct mlx5_flow_dv_matcher matcher = {
11994                 .mask = {
11995                         .size = sizeof(matcher.mask.buf) -
11996                                 MLX5_ST_SZ_BYTES(fte_match_set_misc4),
11997                 },
11998         };
11999         int actions_n = 0;
12000         bool actions_end = false;
12001         union {
12002                 struct mlx5_flow_dv_modify_hdr_resource res;
12003                 uint8_t len[sizeof(struct mlx5_flow_dv_modify_hdr_resource) +
12004                             sizeof(struct mlx5_modification_cmd) *
12005                             (MLX5_MAX_MODIFY_NUM + 1)];
12006         } mhdr_dummy;
12007         struct mlx5_flow_dv_modify_hdr_resource *mhdr_res = &mhdr_dummy.res;
12008         const struct rte_flow_action_count *count = NULL;
12009         const struct rte_flow_action_age *non_shared_age = NULL;
12010         union flow_dv_attr flow_attr = { .attr = 0 };
12011         uint32_t tag_be;
12012         union mlx5_flow_tbl_key tbl_key;
12013         uint32_t modify_action_position = UINT32_MAX;
12014         void *match_mask = matcher.mask.buf;
12015         void *match_value = dev_flow->dv.value.buf;
12016         uint8_t next_protocol = 0xff;
12017         struct rte_vlan_hdr vlan = { 0 };
12018         struct mlx5_flow_dv_dest_array_resource mdest_res;
12019         struct mlx5_flow_dv_sample_resource sample_res;
12020         void *sample_actions[MLX5_DV_MAX_NUMBER_OF_ACTIONS] = {0};
12021         const struct rte_flow_action_sample *sample = NULL;
12022         struct mlx5_flow_sub_actions_list *sample_act;
12023         uint32_t sample_act_pos = UINT32_MAX;
12024         uint32_t age_act_pos = UINT32_MAX;
12025         uint32_t num_of_dest = 0;
12026         int tmp_actions_n = 0;
12027         uint32_t table;
12028         int ret = 0;
12029         const struct mlx5_flow_tunnel *tunnel = NULL;
12030         struct flow_grp_info grp_info = {
12031                 .external = !!dev_flow->external,
12032                 .transfer = !!attr->transfer,
12033                 .fdb_def_rule = !!priv->fdb_def_rule,
12034                 .skip_scale = dev_flow->skip_scale &
12035                         (1 << MLX5_SCALE_FLOW_GROUP_BIT),
12036                 .std_tbl_fix = true,
12037         };
12038         const struct rte_flow_item *head_item = items;
12039
12040         if (!wks)
12041                 return rte_flow_error_set(error, ENOMEM,
12042                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
12043                                           NULL,
12044                                           "failed to push flow workspace");
12045         rss_desc = &wks->rss_desc;
12046         memset(&mdest_res, 0, sizeof(struct mlx5_flow_dv_dest_array_resource));
12047         memset(&sample_res, 0, sizeof(struct mlx5_flow_dv_sample_resource));
12048         mhdr_res->ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
12049                                            MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
12050         /* update normal path action resource into last index of array */
12051         sample_act = &mdest_res.sample_act[MLX5_MAX_DEST_NUM - 1];
12052         if (is_tunnel_offload_active(dev)) {
12053                 if (dev_flow->tunnel) {
12054                         RTE_VERIFY(dev_flow->tof_type ==
12055                                    MLX5_TUNNEL_OFFLOAD_MISS_RULE);
12056                         tunnel = dev_flow->tunnel;
12057                 } else {
12058                         tunnel = mlx5_get_tof(items, actions,
12059                                               &dev_flow->tof_type);
12060                         dev_flow->tunnel = tunnel;
12061                 }
12062                 grp_info.std_tbl_fix = tunnel_use_standard_attr_group_translate
12063                                         (dev, attr, tunnel, dev_flow->tof_type);
12064         }
12065         mhdr_res->ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
12066                                            MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
12067         ret = mlx5_flow_group_to_table(dev, tunnel, attr->group, &table,
12068                                        &grp_info, error);
12069         if (ret)
12070                 return ret;
12071         dev_flow->dv.group = table;
12072         if (attr->transfer)
12073                 mhdr_res->ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
12074         /* number of actions must be set to 0 in case of dirty stack. */
12075         mhdr_res->actions_num = 0;
12076         if (is_flow_tunnel_match_rule(dev_flow->tof_type)) {
12077                 /*
12078                  * do not add decap action if match rule drops packet
12079                  * HW rejects rules with decap & drop
12080                  *
12081                  * if tunnel match rule was inserted before matching tunnel set
12082                  * rule flow table used in the match rule must be registered.
12083                  * current implementation handles that in the
12084                  * flow_dv_match_register() at the function end.
12085                  */
12086                 bool add_decap = true;
12087                 const struct rte_flow_action *ptr = actions;
12088
12089                 for (; ptr->type != RTE_FLOW_ACTION_TYPE_END; ptr++) {
12090                         if (ptr->type == RTE_FLOW_ACTION_TYPE_DROP) {
12091                                 add_decap = false;
12092                                 break;
12093                         }
12094                 }
12095                 if (add_decap) {
12096                         if (flow_dv_create_action_l2_decap(dev, dev_flow,
12097                                                            attr->transfer,
12098                                                            error))
12099                                 return -rte_errno;
12100                         dev_flow->dv.actions[actions_n++] =
12101                                         dev_flow->dv.encap_decap->action;
12102                         action_flags |= MLX5_FLOW_ACTION_DECAP;
12103                 }
12104         }
12105         for (; !actions_end ; actions++) {
12106                 const struct rte_flow_action_queue *queue;
12107                 const struct rte_flow_action_rss *rss;
12108                 const struct rte_flow_action *action = actions;
12109                 const uint8_t *rss_key;
12110                 struct mlx5_flow_tbl_resource *tbl;
12111                 struct mlx5_aso_age_action *age_act;
12112                 struct mlx5_flow_counter *cnt_act;
12113                 uint32_t port_id = 0;
12114                 struct mlx5_flow_dv_port_id_action_resource port_id_resource;
12115                 int action_type = actions->type;
12116                 const struct rte_flow_action *found_action = NULL;
12117                 uint32_t jump_group = 0;
12118                 uint32_t owner_idx;
12119                 struct mlx5_aso_ct_action *ct;
12120
12121                 if (!mlx5_flow_os_action_supported(action_type))
12122                         return rte_flow_error_set(error, ENOTSUP,
12123                                                   RTE_FLOW_ERROR_TYPE_ACTION,
12124                                                   actions,
12125                                                   "action not supported");
12126                 switch (action_type) {
12127                 case MLX5_RTE_FLOW_ACTION_TYPE_TUNNEL_SET:
12128                         action_flags |= MLX5_FLOW_ACTION_TUNNEL_SET;
12129                         break;
12130                 case RTE_FLOW_ACTION_TYPE_VOID:
12131                         break;
12132                 case RTE_FLOW_ACTION_TYPE_PORT_ID:
12133                         if (flow_dv_translate_action_port_id(dev, action,
12134                                                              &port_id, error))
12135                                 return -rte_errno;
12136                         port_id_resource.port_id = port_id;
12137                         MLX5_ASSERT(!handle->rix_port_id_action);
12138                         if (flow_dv_port_id_action_resource_register
12139                             (dev, &port_id_resource, dev_flow, error))
12140                                 return -rte_errno;
12141                         dev_flow->dv.actions[actions_n++] =
12142                                         dev_flow->dv.port_id_action->action;
12143                         action_flags |= MLX5_FLOW_ACTION_PORT_ID;
12144                         dev_flow->handle->fate_action = MLX5_FLOW_FATE_PORT_ID;
12145                         sample_act->action_flags |= MLX5_FLOW_ACTION_PORT_ID;
12146                         num_of_dest++;
12147                         break;
12148                 case RTE_FLOW_ACTION_TYPE_FLAG:
12149                         action_flags |= MLX5_FLOW_ACTION_FLAG;
12150                         dev_flow->handle->mark = 1;
12151                         if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
12152                                 struct rte_flow_action_mark mark = {
12153                                         .id = MLX5_FLOW_MARK_DEFAULT,
12154                                 };
12155
12156                                 if (flow_dv_convert_action_mark(dev, &mark,
12157                                                                 mhdr_res,
12158                                                                 error))
12159                                         return -rte_errno;
12160                                 action_flags |= MLX5_FLOW_ACTION_MARK_EXT;
12161                                 break;
12162                         }
12163                         tag_be = mlx5_flow_mark_set(MLX5_FLOW_MARK_DEFAULT);
12164                         /*
12165                          * Only one FLAG or MARK is supported per device flow
12166                          * right now. So the pointer to the tag resource must be
12167                          * zero before the register process.
12168                          */
12169                         MLX5_ASSERT(!handle->dvh.rix_tag);
12170                         if (flow_dv_tag_resource_register(dev, tag_be,
12171                                                           dev_flow, error))
12172                                 return -rte_errno;
12173                         MLX5_ASSERT(dev_flow->dv.tag_resource);
12174                         dev_flow->dv.actions[actions_n++] =
12175                                         dev_flow->dv.tag_resource->action;
12176                         break;
12177                 case RTE_FLOW_ACTION_TYPE_MARK:
12178                         action_flags |= MLX5_FLOW_ACTION_MARK;
12179                         dev_flow->handle->mark = 1;
12180                         if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
12181                                 const struct rte_flow_action_mark *mark =
12182                                         (const struct rte_flow_action_mark *)
12183                                                 actions->conf;
12184
12185                                 if (flow_dv_convert_action_mark(dev, mark,
12186                                                                 mhdr_res,
12187                                                                 error))
12188                                         return -rte_errno;
12189                                 action_flags |= MLX5_FLOW_ACTION_MARK_EXT;
12190                                 break;
12191                         }
12192                         /* Fall-through */
12193                 case MLX5_RTE_FLOW_ACTION_TYPE_MARK:
12194                         /* Legacy (non-extensive) MARK action. */
12195                         tag_be = mlx5_flow_mark_set
12196                               (((const struct rte_flow_action_mark *)
12197                                (actions->conf))->id);
12198                         MLX5_ASSERT(!handle->dvh.rix_tag);
12199                         if (flow_dv_tag_resource_register(dev, tag_be,
12200                                                           dev_flow, error))
12201                                 return -rte_errno;
12202                         MLX5_ASSERT(dev_flow->dv.tag_resource);
12203                         dev_flow->dv.actions[actions_n++] =
12204                                         dev_flow->dv.tag_resource->action;
12205                         break;
12206                 case RTE_FLOW_ACTION_TYPE_SET_META:
12207                         if (flow_dv_convert_action_set_meta
12208                                 (dev, mhdr_res, attr,
12209                                  (const struct rte_flow_action_set_meta *)
12210                                   actions->conf, error))
12211                                 return -rte_errno;
12212                         action_flags |= MLX5_FLOW_ACTION_SET_META;
12213                         break;
12214                 case RTE_FLOW_ACTION_TYPE_SET_TAG:
12215                         if (flow_dv_convert_action_set_tag
12216                                 (dev, mhdr_res,
12217                                  (const struct rte_flow_action_set_tag *)
12218                                   actions->conf, error))
12219                                 return -rte_errno;
12220                         action_flags |= MLX5_FLOW_ACTION_SET_TAG;
12221                         break;
12222                 case RTE_FLOW_ACTION_TYPE_DROP:
12223                         action_flags |= MLX5_FLOW_ACTION_DROP;
12224                         dev_flow->handle->fate_action = MLX5_FLOW_FATE_DROP;
12225                         break;
12226                 case RTE_FLOW_ACTION_TYPE_QUEUE:
12227                         queue = actions->conf;
12228                         rss_desc->queue_num = 1;
12229                         rss_desc->queue[0] = queue->index;
12230                         action_flags |= MLX5_FLOW_ACTION_QUEUE;
12231                         dev_flow->handle->fate_action = MLX5_FLOW_FATE_QUEUE;
12232                         sample_act->action_flags |= MLX5_FLOW_ACTION_QUEUE;
12233                         num_of_dest++;
12234                         break;
12235                 case RTE_FLOW_ACTION_TYPE_RSS:
12236                         rss = actions->conf;
12237                         memcpy(rss_desc->queue, rss->queue,
12238                                rss->queue_num * sizeof(uint16_t));
12239                         rss_desc->queue_num = rss->queue_num;
12240                         /* NULL RSS key indicates default RSS key. */
12241                         rss_key = !rss->key ? rss_hash_default_key : rss->key;
12242                         memcpy(rss_desc->key, rss_key, MLX5_RSS_HASH_KEY_LEN);
12243                         /*
12244                          * rss->level and rss.types should be set in advance
12245                          * when expanding items for RSS.
12246                          */
12247                         action_flags |= MLX5_FLOW_ACTION_RSS;
12248                         dev_flow->handle->fate_action = rss_desc->shared_rss ?
12249                                 MLX5_FLOW_FATE_SHARED_RSS :
12250                                 MLX5_FLOW_FATE_QUEUE;
12251                         break;
12252                 case MLX5_RTE_FLOW_ACTION_TYPE_AGE:
12253                         flow->age = (uint32_t)(uintptr_t)(action->conf);
12254                         age_act = flow_aso_age_get_by_idx(dev, flow->age);
12255                         __atomic_fetch_add(&age_act->refcnt, 1,
12256                                            __ATOMIC_RELAXED);
12257                         age_act_pos = actions_n++;
12258                         action_flags |= MLX5_FLOW_ACTION_AGE;
12259                         break;
12260                 case RTE_FLOW_ACTION_TYPE_AGE:
12261                         non_shared_age = action->conf;
12262                         age_act_pos = actions_n++;
12263                         action_flags |= MLX5_FLOW_ACTION_AGE;
12264                         break;
12265                 case MLX5_RTE_FLOW_ACTION_TYPE_COUNT:
12266                         flow->counter = (uint32_t)(uintptr_t)(action->conf);
12267                         cnt_act = flow_dv_counter_get_by_idx(dev, flow->counter,
12268                                                              NULL);
12269                         __atomic_fetch_add(&cnt_act->shared_info.refcnt, 1,
12270                                            __ATOMIC_RELAXED);
12271                         /* Save information first, will apply later. */
12272                         action_flags |= MLX5_FLOW_ACTION_COUNT;
12273                         break;
12274                 case RTE_FLOW_ACTION_TYPE_COUNT:
12275                         if (!dev_conf->devx) {
12276                                 return rte_flow_error_set
12277                                               (error, ENOTSUP,
12278                                                RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
12279                                                NULL,
12280                                                "count action not supported");
12281                         }
12282                         /* Save information first, will apply later. */
12283                         count = action->conf;
12284                         action_flags |= MLX5_FLOW_ACTION_COUNT;
12285                         break;
12286                 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
12287                         dev_flow->dv.actions[actions_n++] =
12288                                                 priv->sh->pop_vlan_action;
12289                         action_flags |= MLX5_FLOW_ACTION_OF_POP_VLAN;
12290                         break;
12291                 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
12292                         if (!(action_flags &
12293                               MLX5_FLOW_ACTION_OF_SET_VLAN_VID))
12294                                 flow_dev_get_vlan_info_from_items(items, &vlan);
12295                         vlan.eth_proto = rte_be_to_cpu_16
12296                              ((((const struct rte_flow_action_of_push_vlan *)
12297                                                    actions->conf)->ethertype));
12298                         found_action = mlx5_flow_find_action
12299                                         (actions + 1,
12300                                          RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID);
12301                         if (found_action)
12302                                 mlx5_update_vlan_vid_pcp(found_action, &vlan);
12303                         found_action = mlx5_flow_find_action
12304                                         (actions + 1,
12305                                          RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP);
12306                         if (found_action)
12307                                 mlx5_update_vlan_vid_pcp(found_action, &vlan);
12308                         if (flow_dv_create_action_push_vlan
12309                                             (dev, attr, &vlan, dev_flow, error))
12310                                 return -rte_errno;
12311                         dev_flow->dv.actions[actions_n++] =
12312                                         dev_flow->dv.push_vlan_res->action;
12313                         action_flags |= MLX5_FLOW_ACTION_OF_PUSH_VLAN;
12314                         break;
12315                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
12316                         /* of_vlan_push action handled this action */
12317                         MLX5_ASSERT(action_flags &
12318                                     MLX5_FLOW_ACTION_OF_PUSH_VLAN);
12319                         break;
12320                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
12321                         if (action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN)
12322                                 break;
12323                         flow_dev_get_vlan_info_from_items(items, &vlan);
12324                         mlx5_update_vlan_vid_pcp(actions, &vlan);
12325                         /* If no VLAN push - this is a modify header action */
12326                         if (flow_dv_convert_action_modify_vlan_vid
12327                                                 (mhdr_res, actions, error))
12328                                 return -rte_errno;
12329                         action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_VID;
12330                         break;
12331                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
12332                 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
12333                         if (flow_dv_create_action_l2_encap(dev, actions,
12334                                                            dev_flow,
12335                                                            attr->transfer,
12336                                                            error))
12337                                 return -rte_errno;
12338                         dev_flow->dv.actions[actions_n++] =
12339                                         dev_flow->dv.encap_decap->action;
12340                         action_flags |= MLX5_FLOW_ACTION_ENCAP;
12341                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
12342                                 sample_act->action_flags |=
12343                                                         MLX5_FLOW_ACTION_ENCAP;
12344                         break;
12345                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
12346                 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
12347                         if (flow_dv_create_action_l2_decap(dev, dev_flow,
12348                                                            attr->transfer,
12349                                                            error))
12350                                 return -rte_errno;
12351                         dev_flow->dv.actions[actions_n++] =
12352                                         dev_flow->dv.encap_decap->action;
12353                         action_flags |= MLX5_FLOW_ACTION_DECAP;
12354                         break;
12355                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
12356                         /* Handle encap with preceding decap. */
12357                         if (action_flags & MLX5_FLOW_ACTION_DECAP) {
12358                                 if (flow_dv_create_action_raw_encap
12359                                         (dev, actions, dev_flow, attr, error))
12360                                         return -rte_errno;
12361                                 dev_flow->dv.actions[actions_n++] =
12362                                         dev_flow->dv.encap_decap->action;
12363                         } else {
12364                                 /* Handle encap without preceding decap. */
12365                                 if (flow_dv_create_action_l2_encap
12366                                     (dev, actions, dev_flow, attr->transfer,
12367                                      error))
12368                                         return -rte_errno;
12369                                 dev_flow->dv.actions[actions_n++] =
12370                                         dev_flow->dv.encap_decap->action;
12371                         }
12372                         action_flags |= MLX5_FLOW_ACTION_ENCAP;
12373                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
12374                                 sample_act->action_flags |=
12375                                                         MLX5_FLOW_ACTION_ENCAP;
12376                         break;
12377                 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
12378                         while ((++action)->type == RTE_FLOW_ACTION_TYPE_VOID)
12379                                 ;
12380                         if (action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
12381                                 if (flow_dv_create_action_l2_decap
12382                                     (dev, dev_flow, attr->transfer, error))
12383                                         return -rte_errno;
12384                                 dev_flow->dv.actions[actions_n++] =
12385                                         dev_flow->dv.encap_decap->action;
12386                         }
12387                         /* If decap is followed by encap, handle it at encap. */
12388                         action_flags |= MLX5_FLOW_ACTION_DECAP;
12389                         break;
12390                 case MLX5_RTE_FLOW_ACTION_TYPE_JUMP:
12391                         dev_flow->dv.actions[actions_n++] =
12392                                 (void *)(uintptr_t)action->conf;
12393                         action_flags |= MLX5_FLOW_ACTION_JUMP;
12394                         break;
12395                 case RTE_FLOW_ACTION_TYPE_JUMP:
12396                         jump_group = ((const struct rte_flow_action_jump *)
12397                                                         action->conf)->group;
12398                         grp_info.std_tbl_fix = 0;
12399                         if (dev_flow->skip_scale &
12400                                 (1 << MLX5_SCALE_JUMP_FLOW_GROUP_BIT))
12401                                 grp_info.skip_scale = 1;
12402                         else
12403                                 grp_info.skip_scale = 0;
12404                         ret = mlx5_flow_group_to_table(dev, tunnel,
12405                                                        jump_group,
12406                                                        &table,
12407                                                        &grp_info, error);
12408                         if (ret)
12409                                 return ret;
12410                         tbl = flow_dv_tbl_resource_get(dev, table, attr->egress,
12411                                                        attr->transfer,
12412                                                        !!dev_flow->external,
12413                                                        tunnel, jump_group, 0,
12414                                                        0, error);
12415                         if (!tbl)
12416                                 return rte_flow_error_set
12417                                                 (error, errno,
12418                                                  RTE_FLOW_ERROR_TYPE_ACTION,
12419                                                  NULL,
12420                                                  "cannot create jump action.");
12421                         if (flow_dv_jump_tbl_resource_register
12422                             (dev, tbl, dev_flow, error)) {
12423                                 flow_dv_tbl_resource_release(MLX5_SH(dev), tbl);
12424                                 return rte_flow_error_set
12425                                                 (error, errno,
12426                                                  RTE_FLOW_ERROR_TYPE_ACTION,
12427                                                  NULL,
12428                                                  "cannot create jump action.");
12429                         }
12430                         dev_flow->dv.actions[actions_n++] =
12431                                         dev_flow->dv.jump->action;
12432                         action_flags |= MLX5_FLOW_ACTION_JUMP;
12433                         dev_flow->handle->fate_action = MLX5_FLOW_FATE_JUMP;
12434                         sample_act->action_flags |= MLX5_FLOW_ACTION_JUMP;
12435                         num_of_dest++;
12436                         break;
12437                 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
12438                 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
12439                         if (flow_dv_convert_action_modify_mac
12440                                         (mhdr_res, actions, error))
12441                                 return -rte_errno;
12442                         action_flags |= actions->type ==
12443                                         RTE_FLOW_ACTION_TYPE_SET_MAC_SRC ?
12444                                         MLX5_FLOW_ACTION_SET_MAC_SRC :
12445                                         MLX5_FLOW_ACTION_SET_MAC_DST;
12446                         break;
12447                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
12448                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
12449                         if (flow_dv_convert_action_modify_ipv4
12450                                         (mhdr_res, actions, error))
12451                                 return -rte_errno;
12452                         action_flags |= actions->type ==
12453                                         RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC ?
12454                                         MLX5_FLOW_ACTION_SET_IPV4_SRC :
12455                                         MLX5_FLOW_ACTION_SET_IPV4_DST;
12456                         break;
12457                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
12458                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
12459                         if (flow_dv_convert_action_modify_ipv6
12460                                         (mhdr_res, actions, error))
12461                                 return -rte_errno;
12462                         action_flags |= actions->type ==
12463                                         RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC ?
12464                                         MLX5_FLOW_ACTION_SET_IPV6_SRC :
12465                                         MLX5_FLOW_ACTION_SET_IPV6_DST;
12466                         break;
12467                 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
12468                 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
12469                         if (flow_dv_convert_action_modify_tp
12470                                         (mhdr_res, actions, items,
12471                                          &flow_attr, dev_flow, !!(action_flags &
12472                                          MLX5_FLOW_ACTION_DECAP), error))
12473                                 return -rte_errno;
12474                         action_flags |= actions->type ==
12475                                         RTE_FLOW_ACTION_TYPE_SET_TP_SRC ?
12476                                         MLX5_FLOW_ACTION_SET_TP_SRC :
12477                                         MLX5_FLOW_ACTION_SET_TP_DST;
12478                         break;
12479                 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
12480                         if (flow_dv_convert_action_modify_dec_ttl
12481                                         (mhdr_res, items, &flow_attr, dev_flow,
12482                                          !!(action_flags &
12483                                          MLX5_FLOW_ACTION_DECAP), error))
12484                                 return -rte_errno;
12485                         action_flags |= MLX5_FLOW_ACTION_DEC_TTL;
12486                         break;
12487                 case RTE_FLOW_ACTION_TYPE_SET_TTL:
12488                         if (flow_dv_convert_action_modify_ttl
12489                                         (mhdr_res, actions, items, &flow_attr,
12490                                          dev_flow, !!(action_flags &
12491                                          MLX5_FLOW_ACTION_DECAP), error))
12492                                 return -rte_errno;
12493                         action_flags |= MLX5_FLOW_ACTION_SET_TTL;
12494                         break;
12495                 case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
12496                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
12497                         if (flow_dv_convert_action_modify_tcp_seq
12498                                         (mhdr_res, actions, error))
12499                                 return -rte_errno;
12500                         action_flags |= actions->type ==
12501                                         RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ ?
12502                                         MLX5_FLOW_ACTION_INC_TCP_SEQ :
12503                                         MLX5_FLOW_ACTION_DEC_TCP_SEQ;
12504                         break;
12505
12506                 case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
12507                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
12508                         if (flow_dv_convert_action_modify_tcp_ack
12509                                         (mhdr_res, actions, error))
12510                                 return -rte_errno;
12511                         action_flags |= actions->type ==
12512                                         RTE_FLOW_ACTION_TYPE_INC_TCP_ACK ?
12513                                         MLX5_FLOW_ACTION_INC_TCP_ACK :
12514                                         MLX5_FLOW_ACTION_DEC_TCP_ACK;
12515                         break;
12516                 case MLX5_RTE_FLOW_ACTION_TYPE_TAG:
12517                         if (flow_dv_convert_action_set_reg
12518                                         (mhdr_res, actions, error))
12519                                 return -rte_errno;
12520                         action_flags |= MLX5_FLOW_ACTION_SET_TAG;
12521                         break;
12522                 case MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG:
12523                         if (flow_dv_convert_action_copy_mreg
12524                                         (dev, mhdr_res, actions, error))
12525                                 return -rte_errno;
12526                         action_flags |= MLX5_FLOW_ACTION_SET_TAG;
12527                         break;
12528                 case MLX5_RTE_FLOW_ACTION_TYPE_DEFAULT_MISS:
12529                         action_flags |= MLX5_FLOW_ACTION_DEFAULT_MISS;
12530                         dev_flow->handle->fate_action =
12531                                         MLX5_FLOW_FATE_DEFAULT_MISS;
12532                         break;
12533                 case RTE_FLOW_ACTION_TYPE_METER:
12534                         if (!wks->fm)
12535                                 return rte_flow_error_set(error, rte_errno,
12536                                         RTE_FLOW_ERROR_TYPE_ACTION,
12537                                         NULL, "Failed to get meter in flow.");
12538                         /* Set the meter action. */
12539                         dev_flow->dv.actions[actions_n++] =
12540                                 wks->fm->meter_action;
12541                         action_flags |= MLX5_FLOW_ACTION_METER;
12542                         break;
12543                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP:
12544                         if (flow_dv_convert_action_modify_ipv4_dscp(mhdr_res,
12545                                                               actions, error))
12546                                 return -rte_errno;
12547                         action_flags |= MLX5_FLOW_ACTION_SET_IPV4_DSCP;
12548                         break;
12549                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP:
12550                         if (flow_dv_convert_action_modify_ipv6_dscp(mhdr_res,
12551                                                               actions, error))
12552                                 return -rte_errno;
12553                         action_flags |= MLX5_FLOW_ACTION_SET_IPV6_DSCP;
12554                         break;
12555                 case RTE_FLOW_ACTION_TYPE_SAMPLE:
12556                         sample_act_pos = actions_n;
12557                         sample = (const struct rte_flow_action_sample *)
12558                                  action->conf;
12559                         actions_n++;
12560                         action_flags |= MLX5_FLOW_ACTION_SAMPLE;
12561                         /* put encap action into group if work with port id */
12562                         if ((action_flags & MLX5_FLOW_ACTION_ENCAP) &&
12563                             (action_flags & MLX5_FLOW_ACTION_PORT_ID))
12564                                 sample_act->action_flags |=
12565                                                         MLX5_FLOW_ACTION_ENCAP;
12566                         break;
12567                 case RTE_FLOW_ACTION_TYPE_MODIFY_FIELD:
12568                         if (flow_dv_convert_action_modify_field
12569                                         (dev, mhdr_res, actions, attr, error))
12570                                 return -rte_errno;
12571                         action_flags |= MLX5_FLOW_ACTION_MODIFY_FIELD;
12572                         break;
12573                 case RTE_FLOW_ACTION_TYPE_CONNTRACK:
12574                         owner_idx = (uint32_t)(uintptr_t)action->conf;
12575                         ct = flow_aso_ct_get_by_idx(dev, owner_idx);
12576                         if (!ct)
12577                                 return rte_flow_error_set(error, EINVAL,
12578                                                 RTE_FLOW_ERROR_TYPE_ACTION,
12579                                                 NULL,
12580                                                 "Failed to get CT object.");
12581                         if (mlx5_aso_ct_available(priv->sh, ct))
12582                                 return rte_flow_error_set(error, rte_errno,
12583                                                 RTE_FLOW_ERROR_TYPE_ACTION,
12584                                                 NULL,
12585                                                 "CT is unavailable.");
12586                         if (ct->is_original)
12587                                 dev_flow->dv.actions[actions_n] =
12588                                                         ct->dr_action_orig;
12589                         else
12590                                 dev_flow->dv.actions[actions_n] =
12591                                                         ct->dr_action_rply;
12592                         flow->indirect_type = MLX5_INDIRECT_ACTION_TYPE_CT;
12593                         flow->ct = owner_idx;
12594                         __atomic_fetch_add(&ct->refcnt, 1, __ATOMIC_RELAXED);
12595                         actions_n++;
12596                         action_flags |= MLX5_FLOW_ACTION_CT;
12597                         break;
12598                 case RTE_FLOW_ACTION_TYPE_END:
12599                         actions_end = true;
12600                         if (mhdr_res->actions_num) {
12601                                 /* create modify action if needed. */
12602                                 if (flow_dv_modify_hdr_resource_register
12603                                         (dev, mhdr_res, dev_flow, error))
12604                                         return -rte_errno;
12605                                 dev_flow->dv.actions[modify_action_position] =
12606                                         handle->dvh.modify_hdr->action;
12607                         }
12608                         /*
12609                          * Handle AGE and COUNT action by single HW counter
12610                          * when they are not shared.
12611                          */
12612                         if (action_flags & MLX5_FLOW_ACTION_AGE) {
12613                                 if ((non_shared_age &&
12614                                      count && !count->shared) ||
12615                                     !(priv->sh->flow_hit_aso_en &&
12616                                       attr->group)) {
12617                                         /* Creates age by counters. */
12618                                         cnt_act = flow_dv_prepare_counter
12619                                                                 (dev, dev_flow,
12620                                                                  flow, count,
12621                                                                  non_shared_age,
12622                                                                  error);
12623                                         if (!cnt_act)
12624                                                 return -rte_errno;
12625                                         dev_flow->dv.actions[age_act_pos] =
12626                                                                 cnt_act->action;
12627                                         break;
12628                                 }
12629                                 if (!flow->age && non_shared_age) {
12630                                         flow->age =
12631                                                 flow_dv_translate_create_aso_age
12632                                                                 (dev,
12633                                                                  non_shared_age,
12634                                                                  error);
12635                                         if (!flow->age)
12636                                                 return rte_flow_error_set
12637                                                     (error, rte_errno,
12638                                                      RTE_FLOW_ERROR_TYPE_ACTION,
12639                                                      NULL,
12640                                                      "can't create ASO age action");
12641                                 }
12642                                 age_act = flow_aso_age_get_by_idx(dev,
12643                                                                   flow->age);
12644                                 dev_flow->dv.actions[age_act_pos] =
12645                                                              age_act->dr_action;
12646                         }
12647                         if (action_flags & MLX5_FLOW_ACTION_COUNT) {
12648                                 /*
12649                                  * Create one count action, to be used
12650                                  * by all sub-flows.
12651                                  */
12652                                 cnt_act = flow_dv_prepare_counter(dev, dev_flow,
12653                                                                   flow, count,
12654                                                                   NULL, error);
12655                                 if (!cnt_act)
12656                                         return -rte_errno;
12657                                 dev_flow->dv.actions[actions_n++] =
12658                                                                 cnt_act->action;
12659                         }
12660                 default:
12661                         break;
12662                 }
12663                 if (mhdr_res->actions_num &&
12664                     modify_action_position == UINT32_MAX)
12665                         modify_action_position = actions_n++;
12666         }
12667         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
12668                 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
12669                 int item_type = items->type;
12670
12671                 if (!mlx5_flow_os_item_supported(item_type))
12672                         return rte_flow_error_set(error, ENOTSUP,
12673                                                   RTE_FLOW_ERROR_TYPE_ITEM,
12674                                                   NULL, "item not supported");
12675                 switch (item_type) {
12676                 case RTE_FLOW_ITEM_TYPE_PORT_ID:
12677                         flow_dv_translate_item_port_id
12678                                 (dev, match_mask, match_value, items, attr);
12679                         last_item = MLX5_FLOW_ITEM_PORT_ID;
12680                         break;
12681                 case RTE_FLOW_ITEM_TYPE_ETH:
12682                         flow_dv_translate_item_eth(match_mask, match_value,
12683                                                    items, tunnel,
12684                                                    dev_flow->dv.group);
12685                         matcher.priority = action_flags &
12686                                         MLX5_FLOW_ACTION_DEFAULT_MISS &&
12687                                         !dev_flow->external ?
12688                                         MLX5_PRIORITY_MAP_L3 :
12689                                         MLX5_PRIORITY_MAP_L2;
12690                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
12691                                              MLX5_FLOW_LAYER_OUTER_L2;
12692                         break;
12693                 case RTE_FLOW_ITEM_TYPE_VLAN:
12694                         flow_dv_translate_item_vlan(dev_flow,
12695                                                     match_mask, match_value,
12696                                                     items, tunnel,
12697                                                     dev_flow->dv.group);
12698                         matcher.priority = MLX5_PRIORITY_MAP_L2;
12699                         last_item = tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
12700                                               MLX5_FLOW_LAYER_INNER_VLAN) :
12701                                              (MLX5_FLOW_LAYER_OUTER_L2 |
12702                                               MLX5_FLOW_LAYER_OUTER_VLAN);
12703                         break;
12704                 case RTE_FLOW_ITEM_TYPE_IPV4:
12705                         mlx5_flow_tunnel_ip_check(items, next_protocol,
12706                                                   &item_flags, &tunnel);
12707                         flow_dv_translate_item_ipv4(match_mask, match_value,
12708                                                     items, tunnel,
12709                                                     dev_flow->dv.group);
12710                         matcher.priority = MLX5_PRIORITY_MAP_L3;
12711                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
12712                                              MLX5_FLOW_LAYER_OUTER_L3_IPV4;
12713                         if (items->mask != NULL &&
12714                             ((const struct rte_flow_item_ipv4 *)
12715                              items->mask)->hdr.next_proto_id) {
12716                                 next_protocol =
12717                                         ((const struct rte_flow_item_ipv4 *)
12718                                          (items->spec))->hdr.next_proto_id;
12719                                 next_protocol &=
12720                                         ((const struct rte_flow_item_ipv4 *)
12721                                          (items->mask))->hdr.next_proto_id;
12722                         } else {
12723                                 /* Reset for inner layer. */
12724                                 next_protocol = 0xff;
12725                         }
12726                         break;
12727                 case RTE_FLOW_ITEM_TYPE_IPV6:
12728                         mlx5_flow_tunnel_ip_check(items, next_protocol,
12729                                                   &item_flags, &tunnel);
12730                         flow_dv_translate_item_ipv6(match_mask, match_value,
12731                                                     items, tunnel,
12732                                                     dev_flow->dv.group);
12733                         matcher.priority = MLX5_PRIORITY_MAP_L3;
12734                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
12735                                              MLX5_FLOW_LAYER_OUTER_L3_IPV6;
12736                         if (items->mask != NULL &&
12737                             ((const struct rte_flow_item_ipv6 *)
12738                              items->mask)->hdr.proto) {
12739                                 next_protocol =
12740                                         ((const struct rte_flow_item_ipv6 *)
12741                                          items->spec)->hdr.proto;
12742                                 next_protocol &=
12743                                         ((const struct rte_flow_item_ipv6 *)
12744                                          items->mask)->hdr.proto;
12745                         } else {
12746                                 /* Reset for inner layer. */
12747                                 next_protocol = 0xff;
12748                         }
12749                         break;
12750                 case RTE_FLOW_ITEM_TYPE_IPV6_FRAG_EXT:
12751                         flow_dv_translate_item_ipv6_frag_ext(match_mask,
12752                                                              match_value,
12753                                                              items, tunnel);
12754                         last_item = tunnel ?
12755                                         MLX5_FLOW_LAYER_INNER_L3_IPV6_FRAG_EXT :
12756                                         MLX5_FLOW_LAYER_OUTER_L3_IPV6_FRAG_EXT;
12757                         if (items->mask != NULL &&
12758                             ((const struct rte_flow_item_ipv6_frag_ext *)
12759                              items->mask)->hdr.next_header) {
12760                                 next_protocol =
12761                                 ((const struct rte_flow_item_ipv6_frag_ext *)
12762                                  items->spec)->hdr.next_header;
12763                                 next_protocol &=
12764                                 ((const struct rte_flow_item_ipv6_frag_ext *)
12765                                  items->mask)->hdr.next_header;
12766                         } else {
12767                                 /* Reset for inner layer. */
12768                                 next_protocol = 0xff;
12769                         }
12770                         break;
12771                 case RTE_FLOW_ITEM_TYPE_TCP:
12772                         flow_dv_translate_item_tcp(match_mask, match_value,
12773                                                    items, tunnel);
12774                         matcher.priority = MLX5_PRIORITY_MAP_L4;
12775                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
12776                                              MLX5_FLOW_LAYER_OUTER_L4_TCP;
12777                         break;
12778                 case RTE_FLOW_ITEM_TYPE_UDP:
12779                         flow_dv_translate_item_udp(match_mask, match_value,
12780                                                    items, tunnel);
12781                         matcher.priority = MLX5_PRIORITY_MAP_L4;
12782                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
12783                                              MLX5_FLOW_LAYER_OUTER_L4_UDP;
12784                         break;
12785                 case RTE_FLOW_ITEM_TYPE_GRE:
12786                         flow_dv_translate_item_gre(match_mask, match_value,
12787                                                    items, tunnel);
12788                         matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
12789                         last_item = MLX5_FLOW_LAYER_GRE;
12790                         break;
12791                 case RTE_FLOW_ITEM_TYPE_GRE_KEY:
12792                         flow_dv_translate_item_gre_key(match_mask,
12793                                                        match_value, items);
12794                         last_item = MLX5_FLOW_LAYER_GRE_KEY;
12795                         break;
12796                 case RTE_FLOW_ITEM_TYPE_NVGRE:
12797                         flow_dv_translate_item_nvgre(match_mask, match_value,
12798                                                      items, tunnel);
12799                         matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
12800                         last_item = MLX5_FLOW_LAYER_GRE;
12801                         break;
12802                 case RTE_FLOW_ITEM_TYPE_VXLAN:
12803                         flow_dv_translate_item_vxlan(match_mask, match_value,
12804                                                      items, tunnel);
12805                         matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
12806                         last_item = MLX5_FLOW_LAYER_VXLAN;
12807                         break;
12808                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
12809                         flow_dv_translate_item_vxlan_gpe(match_mask,
12810                                                          match_value, items,
12811                                                          tunnel);
12812                         matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
12813                         last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
12814                         break;
12815                 case RTE_FLOW_ITEM_TYPE_GENEVE:
12816                         flow_dv_translate_item_geneve(match_mask, match_value,
12817                                                       items, tunnel);
12818                         matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
12819                         last_item = MLX5_FLOW_LAYER_GENEVE;
12820                         break;
12821                 case RTE_FLOW_ITEM_TYPE_GENEVE_OPT:
12822                         ret = flow_dv_translate_item_geneve_opt(dev, match_mask,
12823                                                           match_value,
12824                                                           items, error);
12825                         if (ret)
12826                                 return rte_flow_error_set(error, -ret,
12827                                         RTE_FLOW_ERROR_TYPE_ITEM, NULL,
12828                                         "cannot create GENEVE TLV option");
12829                         flow->geneve_tlv_option = 1;
12830                         last_item = MLX5_FLOW_LAYER_GENEVE_OPT;
12831                         break;
12832                 case RTE_FLOW_ITEM_TYPE_MPLS:
12833                         flow_dv_translate_item_mpls(match_mask, match_value,
12834                                                     items, last_item, tunnel);
12835                         matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
12836                         last_item = MLX5_FLOW_LAYER_MPLS;
12837                         break;
12838                 case RTE_FLOW_ITEM_TYPE_MARK:
12839                         flow_dv_translate_item_mark(dev, match_mask,
12840                                                     match_value, items);
12841                         last_item = MLX5_FLOW_ITEM_MARK;
12842                         break;
12843                 case RTE_FLOW_ITEM_TYPE_META:
12844                         flow_dv_translate_item_meta(dev, match_mask,
12845                                                     match_value, attr, items);
12846                         last_item = MLX5_FLOW_ITEM_METADATA;
12847                         break;
12848                 case RTE_FLOW_ITEM_TYPE_ICMP:
12849                         flow_dv_translate_item_icmp(match_mask, match_value,
12850                                                     items, tunnel);
12851                         last_item = MLX5_FLOW_LAYER_ICMP;
12852                         break;
12853                 case RTE_FLOW_ITEM_TYPE_ICMP6:
12854                         flow_dv_translate_item_icmp6(match_mask, match_value,
12855                                                       items, tunnel);
12856                         last_item = MLX5_FLOW_LAYER_ICMP6;
12857                         break;
12858                 case RTE_FLOW_ITEM_TYPE_TAG:
12859                         flow_dv_translate_item_tag(dev, match_mask,
12860                                                    match_value, items);
12861                         last_item = MLX5_FLOW_ITEM_TAG;
12862                         break;
12863                 case MLX5_RTE_FLOW_ITEM_TYPE_TAG:
12864                         flow_dv_translate_mlx5_item_tag(dev, match_mask,
12865                                                         match_value, items);
12866                         last_item = MLX5_FLOW_ITEM_TAG;
12867                         break;
12868                 case MLX5_RTE_FLOW_ITEM_TYPE_TX_QUEUE:
12869                         flow_dv_translate_item_tx_queue(dev, match_mask,
12870                                                         match_value,
12871                                                         items);
12872                         last_item = MLX5_FLOW_ITEM_TX_QUEUE;
12873                         break;
12874                 case RTE_FLOW_ITEM_TYPE_GTP:
12875                         flow_dv_translate_item_gtp(match_mask, match_value,
12876                                                    items, tunnel);
12877                         matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
12878                         last_item = MLX5_FLOW_LAYER_GTP;
12879                         break;
12880                 case RTE_FLOW_ITEM_TYPE_GTP_PSC:
12881                         ret = flow_dv_translate_item_gtp_psc(match_mask,
12882                                                           match_value,
12883                                                           items);
12884                         if (ret)
12885                                 return rte_flow_error_set(error, -ret,
12886                                         RTE_FLOW_ERROR_TYPE_ITEM, NULL,
12887                                         "cannot create GTP PSC item");
12888                         last_item = MLX5_FLOW_LAYER_GTP_PSC;
12889                         break;
12890                 case RTE_FLOW_ITEM_TYPE_ECPRI:
12891                         if (!mlx5_flex_parser_ecpri_exist(dev)) {
12892                                 /* Create it only the first time to be used. */
12893                                 ret = mlx5_flex_parser_ecpri_alloc(dev);
12894                                 if (ret)
12895                                         return rte_flow_error_set
12896                                                 (error, -ret,
12897                                                 RTE_FLOW_ERROR_TYPE_ITEM,
12898                                                 NULL,
12899                                                 "cannot create eCPRI parser");
12900                         }
12901                         /* Adjust the length matcher and device flow value. */
12902                         matcher.mask.size = MLX5_ST_SZ_BYTES(fte_match_param);
12903                         dev_flow->dv.value.size =
12904                                         MLX5_ST_SZ_BYTES(fte_match_param);
12905                         flow_dv_translate_item_ecpri(dev, match_mask,
12906                                                      match_value, items);
12907                         /* No other protocol should follow eCPRI layer. */
12908                         last_item = MLX5_FLOW_LAYER_ECPRI;
12909                         break;
12910                 case RTE_FLOW_ITEM_TYPE_INTEGRITY:
12911                         flow_dv_translate_item_integrity(match_mask,
12912                                                          match_value,
12913                                                          head_item, items);
12914                         break;
12915                 case RTE_FLOW_ITEM_TYPE_CONNTRACK:
12916                         flow_dv_translate_item_aso_ct(dev, match_mask,
12917                                                       match_value, items);
12918                         break;
12919                 default:
12920                         break;
12921                 }
12922                 item_flags |= last_item;
12923         }
12924         /*
12925          * When E-Switch mode is enabled, we have two cases where we need to
12926          * set the source port manually.
12927          * The first one, is in case of Nic steering rule, and the second is
12928          * E-Switch rule where no port_id item was found. In both cases
12929          * the source port is set according the current port in use.
12930          */
12931         if (!(item_flags & MLX5_FLOW_ITEM_PORT_ID) &&
12932             (priv->representor || priv->master)) {
12933                 if (flow_dv_translate_item_port_id(dev, match_mask,
12934                                                    match_value, NULL, attr))
12935                         return -rte_errno;
12936         }
12937 #ifdef RTE_LIBRTE_MLX5_DEBUG
12938         MLX5_ASSERT(!flow_dv_check_valid_spec(matcher.mask.buf,
12939                                               dev_flow->dv.value.buf));
12940 #endif
12941         /*
12942          * Layers may be already initialized from prefix flow if this dev_flow
12943          * is the suffix flow.
12944          */
12945         handle->layers |= item_flags;
12946         if (action_flags & MLX5_FLOW_ACTION_RSS)
12947                 flow_dv_hashfields_set(dev_flow, rss_desc);
12948         /* If has RSS action in the sample action, the Sample/Mirror resource
12949          * should be registered after the hash filed be update.
12950          */
12951         if (action_flags & MLX5_FLOW_ACTION_SAMPLE) {
12952                 ret = flow_dv_translate_action_sample(dev,
12953                                                       sample,
12954                                                       dev_flow, attr,
12955                                                       &num_of_dest,
12956                                                       sample_actions,
12957                                                       &sample_res,
12958                                                       error);
12959                 if (ret < 0)
12960                         return ret;
12961                 ret = flow_dv_create_action_sample(dev,
12962                                                    dev_flow,
12963                                                    num_of_dest,
12964                                                    &sample_res,
12965                                                    &mdest_res,
12966                                                    sample_actions,
12967                                                    action_flags,
12968                                                    error);
12969                 if (ret < 0)
12970                         return rte_flow_error_set
12971                                                 (error, rte_errno,
12972                                                 RTE_FLOW_ERROR_TYPE_ACTION,
12973                                                 NULL,
12974                                                 "cannot create sample action");
12975                 if (num_of_dest > 1) {
12976                         dev_flow->dv.actions[sample_act_pos] =
12977                         dev_flow->dv.dest_array_res->action;
12978                 } else {
12979                         dev_flow->dv.actions[sample_act_pos] =
12980                         dev_flow->dv.sample_res->verbs_action;
12981                 }
12982         }
12983         /*
12984          * For multiple destination (sample action with ratio=1), the encap
12985          * action and port id action will be combined into group action.
12986          * So need remove the original these actions in the flow and only
12987          * use the sample action instead of.
12988          */
12989         if (num_of_dest > 1 &&
12990             (sample_act->dr_port_id_action || sample_act->dr_jump_action)) {
12991                 int i;
12992                 void *temp_actions[MLX5_DV_MAX_NUMBER_OF_ACTIONS] = {0};
12993
12994                 for (i = 0; i < actions_n; i++) {
12995                         if ((sample_act->dr_encap_action &&
12996                                 sample_act->dr_encap_action ==
12997                                 dev_flow->dv.actions[i]) ||
12998                                 (sample_act->dr_port_id_action &&
12999                                 sample_act->dr_port_id_action ==
13000                                 dev_flow->dv.actions[i]) ||
13001                                 (sample_act->dr_jump_action &&
13002                                 sample_act->dr_jump_action ==
13003                                 dev_flow->dv.actions[i]))
13004                                 continue;
13005                         temp_actions[tmp_actions_n++] = dev_flow->dv.actions[i];
13006                 }
13007                 memcpy((void *)dev_flow->dv.actions,
13008                                 (void *)temp_actions,
13009                                 tmp_actions_n * sizeof(void *));
13010                 actions_n = tmp_actions_n;
13011         }
13012         dev_flow->dv.actions_n = actions_n;
13013         dev_flow->act_flags = action_flags;
13014         if (wks->skip_matcher_reg)
13015                 return 0;
13016         /* Register matcher. */
13017         matcher.crc = rte_raw_cksum((const void *)matcher.mask.buf,
13018                                     matcher.mask.size);
13019         matcher.priority = mlx5_get_matcher_priority(dev, attr,
13020                                         matcher.priority);
13021         /* reserved field no needs to be set to 0 here. */
13022         tbl_key.is_fdb = attr->transfer;
13023         tbl_key.is_egress = attr->egress;
13024         tbl_key.level = dev_flow->dv.group;
13025         tbl_key.id = dev_flow->dv.table_id;
13026         if (flow_dv_matcher_register(dev, &matcher, &tbl_key, dev_flow,
13027                                      tunnel, attr->group, error))
13028                 return -rte_errno;
13029         return 0;
13030 }
13031
13032 /**
13033  * Set hash RX queue by hash fields (see enum ibv_rx_hash_fields)
13034  * and tunnel.
13035  *
13036  * @param[in, out] action
13037  *   Shred RSS action holding hash RX queue objects.
13038  * @param[in] hash_fields
13039  *   Defines combination of packet fields to participate in RX hash.
13040  * @param[in] tunnel
13041  *   Tunnel type
13042  * @param[in] hrxq_idx
13043  *   Hash RX queue index to set.
13044  *
13045  * @return
13046  *   0 on success, otherwise negative errno value.
13047  */
13048 static int
13049 __flow_dv_action_rss_hrxq_set(struct mlx5_shared_action_rss *action,
13050                               const uint64_t hash_fields,
13051                               uint32_t hrxq_idx)
13052 {
13053         uint32_t *hrxqs = action->hrxq;
13054
13055         switch (hash_fields & ~IBV_RX_HASH_INNER) {
13056         case MLX5_RSS_HASH_IPV4:
13057                 /* fall-through. */
13058         case MLX5_RSS_HASH_IPV4_DST_ONLY:
13059                 /* fall-through. */
13060         case MLX5_RSS_HASH_IPV4_SRC_ONLY:
13061                 hrxqs[0] = hrxq_idx;
13062                 return 0;
13063         case MLX5_RSS_HASH_IPV4_TCP:
13064                 /* fall-through. */
13065         case MLX5_RSS_HASH_IPV4_TCP_DST_ONLY:
13066                 /* fall-through. */
13067         case MLX5_RSS_HASH_IPV4_TCP_SRC_ONLY:
13068                 hrxqs[1] = hrxq_idx;
13069                 return 0;
13070         case MLX5_RSS_HASH_IPV4_UDP:
13071                 /* fall-through. */
13072         case MLX5_RSS_HASH_IPV4_UDP_DST_ONLY:
13073                 /* fall-through. */
13074         case MLX5_RSS_HASH_IPV4_UDP_SRC_ONLY:
13075                 hrxqs[2] = hrxq_idx;
13076                 return 0;
13077         case MLX5_RSS_HASH_IPV6:
13078                 /* fall-through. */
13079         case MLX5_RSS_HASH_IPV6_DST_ONLY:
13080                 /* fall-through. */
13081         case MLX5_RSS_HASH_IPV6_SRC_ONLY:
13082                 hrxqs[3] = hrxq_idx;
13083                 return 0;
13084         case MLX5_RSS_HASH_IPV6_TCP:
13085                 /* fall-through. */
13086         case MLX5_RSS_HASH_IPV6_TCP_DST_ONLY:
13087                 /* fall-through. */
13088         case MLX5_RSS_HASH_IPV6_TCP_SRC_ONLY:
13089                 hrxqs[4] = hrxq_idx;
13090                 return 0;
13091         case MLX5_RSS_HASH_IPV6_UDP:
13092                 /* fall-through. */
13093         case MLX5_RSS_HASH_IPV6_UDP_DST_ONLY:
13094                 /* fall-through. */
13095         case MLX5_RSS_HASH_IPV6_UDP_SRC_ONLY:
13096                 hrxqs[5] = hrxq_idx;
13097                 return 0;
13098         case MLX5_RSS_HASH_NONE:
13099                 hrxqs[6] = hrxq_idx;
13100                 return 0;
13101         default:
13102                 return -1;
13103         }
13104 }
13105
13106 /**
13107  * Look up for hash RX queue by hash fields (see enum ibv_rx_hash_fields)
13108  * and tunnel.
13109  *
13110  * @param[in] dev
13111  *   Pointer to the Ethernet device structure.
13112  * @param[in] idx
13113  *   Shared RSS action ID holding hash RX queue objects.
13114  * @param[in] hash_fields
13115  *   Defines combination of packet fields to participate in RX hash.
13116  * @param[in] tunnel
13117  *   Tunnel type
13118  *
13119  * @return
13120  *   Valid hash RX queue index, otherwise 0.
13121  */
13122 static uint32_t
13123 __flow_dv_action_rss_hrxq_lookup(struct rte_eth_dev *dev, uint32_t idx,
13124                                  const uint64_t hash_fields)
13125 {
13126         struct mlx5_priv *priv = dev->data->dev_private;
13127         struct mlx5_shared_action_rss *shared_rss =
13128             mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS], idx);
13129         const uint32_t *hrxqs = shared_rss->hrxq;
13130
13131         switch (hash_fields & ~IBV_RX_HASH_INNER) {
13132         case MLX5_RSS_HASH_IPV4:
13133                 /* fall-through. */
13134         case MLX5_RSS_HASH_IPV4_DST_ONLY:
13135                 /* fall-through. */
13136         case MLX5_RSS_HASH_IPV4_SRC_ONLY:
13137                 return hrxqs[0];
13138         case MLX5_RSS_HASH_IPV4_TCP:
13139                 /* fall-through. */
13140         case MLX5_RSS_HASH_IPV4_TCP_DST_ONLY:
13141                 /* fall-through. */
13142         case MLX5_RSS_HASH_IPV4_TCP_SRC_ONLY:
13143                 return hrxqs[1];
13144         case MLX5_RSS_HASH_IPV4_UDP:
13145                 /* fall-through. */
13146         case MLX5_RSS_HASH_IPV4_UDP_DST_ONLY:
13147                 /* fall-through. */
13148         case MLX5_RSS_HASH_IPV4_UDP_SRC_ONLY:
13149                 return hrxqs[2];
13150         case MLX5_RSS_HASH_IPV6:
13151                 /* fall-through. */
13152         case MLX5_RSS_HASH_IPV6_DST_ONLY:
13153                 /* fall-through. */
13154         case MLX5_RSS_HASH_IPV6_SRC_ONLY:
13155                 return hrxqs[3];
13156         case MLX5_RSS_HASH_IPV6_TCP:
13157                 /* fall-through. */
13158         case MLX5_RSS_HASH_IPV6_TCP_DST_ONLY:
13159                 /* fall-through. */
13160         case MLX5_RSS_HASH_IPV6_TCP_SRC_ONLY:
13161                 return hrxqs[4];
13162         case MLX5_RSS_HASH_IPV6_UDP:
13163                 /* fall-through. */
13164         case MLX5_RSS_HASH_IPV6_UDP_DST_ONLY:
13165                 /* fall-through. */
13166         case MLX5_RSS_HASH_IPV6_UDP_SRC_ONLY:
13167                 return hrxqs[5];
13168         case MLX5_RSS_HASH_NONE:
13169                 return hrxqs[6];
13170         default:
13171                 return 0;
13172         }
13173
13174 }
13175
13176 /**
13177  * Apply the flow to the NIC, lock free,
13178  * (mutex should be acquired by caller).
13179  *
13180  * @param[in] dev
13181  *   Pointer to the Ethernet device structure.
13182  * @param[in, out] flow
13183  *   Pointer to flow structure.
13184  * @param[out] error
13185  *   Pointer to error structure.
13186  *
13187  * @return
13188  *   0 on success, a negative errno value otherwise and rte_errno is set.
13189  */
13190 static int
13191 flow_dv_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
13192               struct rte_flow_error *error)
13193 {
13194         struct mlx5_flow_dv_workspace *dv;
13195         struct mlx5_flow_handle *dh;
13196         struct mlx5_flow_handle_dv *dv_h;
13197         struct mlx5_flow *dev_flow;
13198         struct mlx5_priv *priv = dev->data->dev_private;
13199         uint32_t handle_idx;
13200         int n;
13201         int err;
13202         int idx;
13203         struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
13204         struct mlx5_flow_rss_desc *rss_desc = &wks->rss_desc;
13205
13206         MLX5_ASSERT(wks);
13207         for (idx = wks->flow_idx - 1; idx >= 0; idx--) {
13208                 dev_flow = &wks->flows[idx];
13209                 dv = &dev_flow->dv;
13210                 dh = dev_flow->handle;
13211                 dv_h = &dh->dvh;
13212                 n = dv->actions_n;
13213                 if (dh->fate_action == MLX5_FLOW_FATE_DROP) {
13214                         if (dv->transfer) {
13215                                 MLX5_ASSERT(priv->sh->dr_drop_action);
13216                                 dv->actions[n++] = priv->sh->dr_drop_action;
13217                         } else {
13218 #ifdef HAVE_MLX5DV_DR
13219                                 /* DR supports drop action placeholder. */
13220                                 MLX5_ASSERT(priv->sh->dr_drop_action);
13221                                 dv->actions[n++] = priv->sh->dr_drop_action;
13222 #else
13223                                 /* For DV we use the explicit drop queue. */
13224                                 MLX5_ASSERT(priv->drop_queue.hrxq);
13225                                 dv->actions[n++] =
13226                                                 priv->drop_queue.hrxq->action;
13227 #endif
13228                         }
13229                 } else if ((dh->fate_action == MLX5_FLOW_FATE_QUEUE &&
13230                            !dv_h->rix_sample && !dv_h->rix_dest_array)) {
13231                         struct mlx5_hrxq *hrxq;
13232                         uint32_t hrxq_idx;
13233
13234                         hrxq = flow_dv_hrxq_prepare(dev, dev_flow, rss_desc,
13235                                                     &hrxq_idx);
13236                         if (!hrxq) {
13237                                 rte_flow_error_set
13238                                         (error, rte_errno,
13239                                          RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
13240                                          "cannot get hash queue");
13241                                 goto error;
13242                         }
13243                         dh->rix_hrxq = hrxq_idx;
13244                         dv->actions[n++] = hrxq->action;
13245                 } else if (dh->fate_action == MLX5_FLOW_FATE_SHARED_RSS) {
13246                         struct mlx5_hrxq *hrxq = NULL;
13247                         uint32_t hrxq_idx;
13248
13249                         hrxq_idx = __flow_dv_action_rss_hrxq_lookup(dev,
13250                                                 rss_desc->shared_rss,
13251                                                 dev_flow->hash_fields);
13252                         if (hrxq_idx)
13253                                 hrxq = mlx5_ipool_get
13254                                         (priv->sh->ipool[MLX5_IPOOL_HRXQ],
13255                                          hrxq_idx);
13256                         if (!hrxq) {
13257                                 rte_flow_error_set
13258                                         (error, rte_errno,
13259                                          RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
13260                                          "cannot get hash queue");
13261                                 goto error;
13262                         }
13263                         dh->rix_srss = rss_desc->shared_rss;
13264                         dv->actions[n++] = hrxq->action;
13265                 } else if (dh->fate_action == MLX5_FLOW_FATE_DEFAULT_MISS) {
13266                         if (!priv->sh->default_miss_action) {
13267                                 rte_flow_error_set
13268                                         (error, rte_errno,
13269                                          RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
13270                                          "default miss action not be created.");
13271                                 goto error;
13272                         }
13273                         dv->actions[n++] = priv->sh->default_miss_action;
13274                 }
13275                 err = mlx5_flow_os_create_flow(dv_h->matcher->matcher_object,
13276                                                (void *)&dv->value, n,
13277                                                dv->actions, &dh->drv_flow);
13278                 if (err) {
13279                         rte_flow_error_set(error, errno,
13280                                            RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
13281                                            NULL,
13282                                            "hardware refuses to create flow");
13283                         goto error;
13284                 }
13285                 if (priv->vmwa_context &&
13286                     dh->vf_vlan.tag && !dh->vf_vlan.created) {
13287                         /*
13288                          * The rule contains the VLAN pattern.
13289                          * For VF we are going to create VLAN
13290                          * interface to make hypervisor set correct
13291                          * e-Switch vport context.
13292                          */
13293                         mlx5_vlan_vmwa_acquire(dev, &dh->vf_vlan);
13294                 }
13295         }
13296         return 0;
13297 error:
13298         err = rte_errno; /* Save rte_errno before cleanup. */
13299         SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
13300                        handle_idx, dh, next) {
13301                 /* hrxq is union, don't clear it if the flag is not set. */
13302                 if (dh->fate_action == MLX5_FLOW_FATE_QUEUE && dh->rix_hrxq) {
13303                         mlx5_hrxq_release(dev, dh->rix_hrxq);
13304                         dh->rix_hrxq = 0;
13305                 } else if (dh->fate_action == MLX5_FLOW_FATE_SHARED_RSS) {
13306                         dh->rix_srss = 0;
13307                 }
13308                 if (dh->vf_vlan.tag && dh->vf_vlan.created)
13309                         mlx5_vlan_vmwa_release(dev, &dh->vf_vlan);
13310         }
13311         rte_errno = err; /* Restore rte_errno. */
13312         return -rte_errno;
13313 }
13314
13315 void
13316 flow_dv_matcher_remove_cb(struct mlx5_cache_list *list __rte_unused,
13317                           struct mlx5_cache_entry *entry)
13318 {
13319         struct mlx5_flow_dv_matcher *cache = container_of(entry, typeof(*cache),
13320                                                           entry);
13321
13322         claim_zero(mlx5_flow_os_destroy_flow_matcher(cache->matcher_object));
13323         mlx5_free(cache);
13324 }
13325
13326 /**
13327  * Release the flow matcher.
13328  *
13329  * @param dev
13330  *   Pointer to Ethernet device.
13331  * @param port_id
13332  *   Index to port ID action resource.
13333  *
13334  * @return
13335  *   1 while a reference on it exists, 0 when freed.
13336  */
13337 static int
13338 flow_dv_matcher_release(struct rte_eth_dev *dev,
13339                         struct mlx5_flow_handle *handle)
13340 {
13341         struct mlx5_flow_dv_matcher *matcher = handle->dvh.matcher;
13342         struct mlx5_flow_tbl_data_entry *tbl = container_of(matcher->tbl,
13343                                                             typeof(*tbl), tbl);
13344         int ret;
13345
13346         MLX5_ASSERT(matcher->matcher_object);
13347         ret = mlx5_cache_unregister(&tbl->matchers, &matcher->entry);
13348         flow_dv_tbl_resource_release(MLX5_SH(dev), &tbl->tbl);
13349         return ret;
13350 }
13351
13352 /**
13353  * Release encap_decap resource.
13354  *
13355  * @param list
13356  *   Pointer to the hash list.
13357  * @param entry
13358  *   Pointer to exist resource entry object.
13359  */
13360 void
13361 flow_dv_encap_decap_remove_cb(struct mlx5_hlist *list,
13362                               struct mlx5_hlist_entry *entry)
13363 {
13364         struct mlx5_dev_ctx_shared *sh = list->ctx;
13365         struct mlx5_flow_dv_encap_decap_resource *res =
13366                 container_of(entry, typeof(*res), entry);
13367
13368         claim_zero(mlx5_flow_os_destroy_flow_action(res->action));
13369         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_DECAP_ENCAP], res->idx);
13370 }
13371
13372 /**
13373  * Release an encap/decap resource.
13374  *
13375  * @param dev
13376  *   Pointer to Ethernet device.
13377  * @param encap_decap_idx
13378  *   Index of encap decap resource.
13379  *
13380  * @return
13381  *   1 while a reference on it exists, 0 when freed.
13382  */
13383 static int
13384 flow_dv_encap_decap_resource_release(struct rte_eth_dev *dev,
13385                                      uint32_t encap_decap_idx)
13386 {
13387         struct mlx5_priv *priv = dev->data->dev_private;
13388         struct mlx5_flow_dv_encap_decap_resource *cache_resource;
13389
13390         cache_resource = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_DECAP_ENCAP],
13391                                         encap_decap_idx);
13392         if (!cache_resource)
13393                 return 0;
13394         MLX5_ASSERT(cache_resource->action);
13395         return mlx5_hlist_unregister(priv->sh->encaps_decaps,
13396                                      &cache_resource->entry);
13397 }
13398
13399 /**
13400  * Release an jump to table action resource.
13401  *
13402  * @param dev
13403  *   Pointer to Ethernet device.
13404  * @param rix_jump
13405  *   Index to the jump action resource.
13406  *
13407  * @return
13408  *   1 while a reference on it exists, 0 when freed.
13409  */
13410 static int
13411 flow_dv_jump_tbl_resource_release(struct rte_eth_dev *dev,
13412                                   uint32_t rix_jump)
13413 {
13414         struct mlx5_priv *priv = dev->data->dev_private;
13415         struct mlx5_flow_tbl_data_entry *tbl_data;
13416
13417         tbl_data = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_JUMP],
13418                                   rix_jump);
13419         if (!tbl_data)
13420                 return 0;
13421         return flow_dv_tbl_resource_release(MLX5_SH(dev), &tbl_data->tbl);
13422 }
13423
13424 void
13425 flow_dv_modify_remove_cb(struct mlx5_hlist *list __rte_unused,
13426                          struct mlx5_hlist_entry *entry)
13427 {
13428         struct mlx5_flow_dv_modify_hdr_resource *res =
13429                 container_of(entry, typeof(*res), entry);
13430
13431         claim_zero(mlx5_flow_os_destroy_flow_action(res->action));
13432         mlx5_free(entry);
13433 }
13434
13435 /**
13436  * Release a modify-header resource.
13437  *
13438  * @param dev
13439  *   Pointer to Ethernet device.
13440  * @param handle
13441  *   Pointer to mlx5_flow_handle.
13442  *
13443  * @return
13444  *   1 while a reference on it exists, 0 when freed.
13445  */
13446 static int
13447 flow_dv_modify_hdr_resource_release(struct rte_eth_dev *dev,
13448                                     struct mlx5_flow_handle *handle)
13449 {
13450         struct mlx5_priv *priv = dev->data->dev_private;
13451         struct mlx5_flow_dv_modify_hdr_resource *entry = handle->dvh.modify_hdr;
13452
13453         MLX5_ASSERT(entry->action);
13454         return mlx5_hlist_unregister(priv->sh->modify_cmds, &entry->entry);
13455 }
13456
13457 void
13458 flow_dv_port_id_remove_cb(struct mlx5_cache_list *list,
13459                           struct mlx5_cache_entry *entry)
13460 {
13461         struct mlx5_dev_ctx_shared *sh = list->ctx;
13462         struct mlx5_flow_dv_port_id_action_resource *cache =
13463                         container_of(entry, typeof(*cache), entry);
13464
13465         claim_zero(mlx5_flow_os_destroy_flow_action(cache->action));
13466         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_PORT_ID], cache->idx);
13467 }
13468
13469 /**
13470  * Release port ID action resource.
13471  *
13472  * @param dev
13473  *   Pointer to Ethernet device.
13474  * @param handle
13475  *   Pointer to mlx5_flow_handle.
13476  *
13477  * @return
13478  *   1 while a reference on it exists, 0 when freed.
13479  */
13480 static int
13481 flow_dv_port_id_action_resource_release(struct rte_eth_dev *dev,
13482                                         uint32_t port_id)
13483 {
13484         struct mlx5_priv *priv = dev->data->dev_private;
13485         struct mlx5_flow_dv_port_id_action_resource *cache;
13486
13487         cache = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_PORT_ID], port_id);
13488         if (!cache)
13489                 return 0;
13490         MLX5_ASSERT(cache->action);
13491         return mlx5_cache_unregister(&priv->sh->port_id_action_list,
13492                                      &cache->entry);
13493 }
13494
13495 /**
13496  * Release shared RSS action resource.
13497  *
13498  * @param dev
13499  *   Pointer to Ethernet device.
13500  * @param srss
13501  *   Shared RSS action index.
13502  */
13503 static void
13504 flow_dv_shared_rss_action_release(struct rte_eth_dev *dev, uint32_t srss)
13505 {
13506         struct mlx5_priv *priv = dev->data->dev_private;
13507         struct mlx5_shared_action_rss *shared_rss;
13508
13509         shared_rss = mlx5_ipool_get
13510                         (priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS], srss);
13511         __atomic_sub_fetch(&shared_rss->refcnt, 1, __ATOMIC_RELAXED);
13512 }
13513
13514 void
13515 flow_dv_push_vlan_remove_cb(struct mlx5_cache_list *list,
13516                             struct mlx5_cache_entry *entry)
13517 {
13518         struct mlx5_dev_ctx_shared *sh = list->ctx;
13519         struct mlx5_flow_dv_push_vlan_action_resource *cache =
13520                         container_of(entry, typeof(*cache), entry);
13521
13522         claim_zero(mlx5_flow_os_destroy_flow_action(cache->action));
13523         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_PUSH_VLAN], cache->idx);
13524 }
13525
13526 /**
13527  * Release push vlan action resource.
13528  *
13529  * @param dev
13530  *   Pointer to Ethernet device.
13531  * @param handle
13532  *   Pointer to mlx5_flow_handle.
13533  *
13534  * @return
13535  *   1 while a reference on it exists, 0 when freed.
13536  */
13537 static int
13538 flow_dv_push_vlan_action_resource_release(struct rte_eth_dev *dev,
13539                                           struct mlx5_flow_handle *handle)
13540 {
13541         struct mlx5_priv *priv = dev->data->dev_private;
13542         struct mlx5_flow_dv_push_vlan_action_resource *cache;
13543         uint32_t idx = handle->dvh.rix_push_vlan;
13544
13545         cache = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_PUSH_VLAN], idx);
13546         if (!cache)
13547                 return 0;
13548         MLX5_ASSERT(cache->action);
13549         return mlx5_cache_unregister(&priv->sh->push_vlan_action_list,
13550                                      &cache->entry);
13551 }
13552
13553 /**
13554  * Release the fate resource.
13555  *
13556  * @param dev
13557  *   Pointer to Ethernet device.
13558  * @param handle
13559  *   Pointer to mlx5_flow_handle.
13560  */
13561 static void
13562 flow_dv_fate_resource_release(struct rte_eth_dev *dev,
13563                                struct mlx5_flow_handle *handle)
13564 {
13565         if (!handle->rix_fate)
13566                 return;
13567         switch (handle->fate_action) {
13568         case MLX5_FLOW_FATE_QUEUE:
13569                 if (!handle->dvh.rix_sample && !handle->dvh.rix_dest_array)
13570                         mlx5_hrxq_release(dev, handle->rix_hrxq);
13571                 break;
13572         case MLX5_FLOW_FATE_JUMP:
13573                 flow_dv_jump_tbl_resource_release(dev, handle->rix_jump);
13574                 break;
13575         case MLX5_FLOW_FATE_PORT_ID:
13576                 flow_dv_port_id_action_resource_release(dev,
13577                                 handle->rix_port_id_action);
13578                 break;
13579         default:
13580                 DRV_LOG(DEBUG, "Incorrect fate action:%d", handle->fate_action);
13581                 break;
13582         }
13583         handle->rix_fate = 0;
13584 }
13585
13586 void
13587 flow_dv_sample_remove_cb(struct mlx5_cache_list *list __rte_unused,
13588                          struct mlx5_cache_entry *entry)
13589 {
13590         struct mlx5_flow_dv_sample_resource *cache_resource =
13591                         container_of(entry, typeof(*cache_resource), entry);
13592         struct rte_eth_dev *dev = cache_resource->dev;
13593         struct mlx5_priv *priv = dev->data->dev_private;
13594
13595         if (cache_resource->verbs_action)
13596                 claim_zero(mlx5_flow_os_destroy_flow_action
13597                                 (cache_resource->verbs_action));
13598         if (cache_resource->normal_path_tbl)
13599                 flow_dv_tbl_resource_release(MLX5_SH(dev),
13600                         cache_resource->normal_path_tbl);
13601         flow_dv_sample_sub_actions_release(dev,
13602                                 &cache_resource->sample_idx);
13603         mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_SAMPLE],
13604                         cache_resource->idx);
13605         DRV_LOG(DEBUG, "sample resource %p: removed",
13606                 (void *)cache_resource);
13607 }
13608
13609 /**
13610  * Release an sample resource.
13611  *
13612  * @param dev
13613  *   Pointer to Ethernet device.
13614  * @param handle
13615  *   Pointer to mlx5_flow_handle.
13616  *
13617  * @return
13618  *   1 while a reference on it exists, 0 when freed.
13619  */
13620 static int
13621 flow_dv_sample_resource_release(struct rte_eth_dev *dev,
13622                                      struct mlx5_flow_handle *handle)
13623 {
13624         struct mlx5_priv *priv = dev->data->dev_private;
13625         struct mlx5_flow_dv_sample_resource *cache_resource;
13626
13627         cache_resource = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_SAMPLE],
13628                          handle->dvh.rix_sample);
13629         if (!cache_resource)
13630                 return 0;
13631         MLX5_ASSERT(cache_resource->verbs_action);
13632         return mlx5_cache_unregister(&priv->sh->sample_action_list,
13633                                      &cache_resource->entry);
13634 }
13635
13636 void
13637 flow_dv_dest_array_remove_cb(struct mlx5_cache_list *list __rte_unused,
13638                              struct mlx5_cache_entry *entry)
13639 {
13640         struct mlx5_flow_dv_dest_array_resource *cache_resource =
13641                         container_of(entry, typeof(*cache_resource), entry);
13642         struct rte_eth_dev *dev = cache_resource->dev;
13643         struct mlx5_priv *priv = dev->data->dev_private;
13644         uint32_t i = 0;
13645
13646         MLX5_ASSERT(cache_resource->action);
13647         if (cache_resource->action)
13648                 claim_zero(mlx5_flow_os_destroy_flow_action
13649                                         (cache_resource->action));
13650         for (; i < cache_resource->num_of_dest; i++)
13651                 flow_dv_sample_sub_actions_release(dev,
13652                                 &cache_resource->sample_idx[i]);
13653         mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_DEST_ARRAY],
13654                         cache_resource->idx);
13655         DRV_LOG(DEBUG, "destination array resource %p: removed",
13656                 (void *)cache_resource);
13657 }
13658
13659 /**
13660  * Release an destination array resource.
13661  *
13662  * @param dev
13663  *   Pointer to Ethernet device.
13664  * @param handle
13665  *   Pointer to mlx5_flow_handle.
13666  *
13667  * @return
13668  *   1 while a reference on it exists, 0 when freed.
13669  */
13670 static int
13671 flow_dv_dest_array_resource_release(struct rte_eth_dev *dev,
13672                                     struct mlx5_flow_handle *handle)
13673 {
13674         struct mlx5_priv *priv = dev->data->dev_private;
13675         struct mlx5_flow_dv_dest_array_resource *cache;
13676
13677         cache = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_DEST_ARRAY],
13678                                handle->dvh.rix_dest_array);
13679         if (!cache)
13680                 return 0;
13681         MLX5_ASSERT(cache->action);
13682         return mlx5_cache_unregister(&priv->sh->dest_array_list,
13683                                      &cache->entry);
13684 }
13685
13686 static void
13687 flow_dv_geneve_tlv_option_resource_release(struct rte_eth_dev *dev)
13688 {
13689         struct mlx5_priv *priv = dev->data->dev_private;
13690         struct mlx5_dev_ctx_shared *sh = priv->sh;
13691         struct mlx5_geneve_tlv_option_resource *geneve_opt_resource =
13692                                 sh->geneve_tlv_option_resource;
13693         rte_spinlock_lock(&sh->geneve_tlv_opt_sl);
13694         if (geneve_opt_resource) {
13695                 if (!(__atomic_sub_fetch(&geneve_opt_resource->refcnt, 1,
13696                                          __ATOMIC_RELAXED))) {
13697                         claim_zero(mlx5_devx_cmd_destroy
13698                                         (geneve_opt_resource->obj));
13699                         mlx5_free(sh->geneve_tlv_option_resource);
13700                         sh->geneve_tlv_option_resource = NULL;
13701                 }
13702         }
13703         rte_spinlock_unlock(&sh->geneve_tlv_opt_sl);
13704 }
13705
13706 /**
13707  * Remove the flow from the NIC but keeps it in memory.
13708  * Lock free, (mutex should be acquired by caller).
13709  *
13710  * @param[in] dev
13711  *   Pointer to Ethernet device.
13712  * @param[in, out] flow
13713  *   Pointer to flow structure.
13714  */
13715 static void
13716 flow_dv_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
13717 {
13718         struct mlx5_flow_handle *dh;
13719         uint32_t handle_idx;
13720         struct mlx5_priv *priv = dev->data->dev_private;
13721
13722         if (!flow)
13723                 return;
13724         handle_idx = flow->dev_handles;
13725         while (handle_idx) {
13726                 dh = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
13727                                     handle_idx);
13728                 if (!dh)
13729                         return;
13730                 if (dh->drv_flow) {
13731                         claim_zero(mlx5_flow_os_destroy_flow(dh->drv_flow));
13732                         dh->drv_flow = NULL;
13733                 }
13734                 if (dh->fate_action == MLX5_FLOW_FATE_QUEUE)
13735                         flow_dv_fate_resource_release(dev, dh);
13736                 if (dh->vf_vlan.tag && dh->vf_vlan.created)
13737                         mlx5_vlan_vmwa_release(dev, &dh->vf_vlan);
13738                 handle_idx = dh->next.next;
13739         }
13740 }
13741
13742 /**
13743  * Remove the flow from the NIC and the memory.
13744  * Lock free, (mutex should be acquired by caller).
13745  *
13746  * @param[in] dev
13747  *   Pointer to the Ethernet device structure.
13748  * @param[in, out] flow
13749  *   Pointer to flow structure.
13750  */
13751 static void
13752 flow_dv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
13753 {
13754         struct mlx5_flow_handle *dev_handle;
13755         struct mlx5_priv *priv = dev->data->dev_private;
13756         struct mlx5_flow_meter_info *fm = NULL;
13757         uint32_t srss = 0;
13758
13759         if (!flow)
13760                 return;
13761         flow_dv_remove(dev, flow);
13762         if (flow->counter) {
13763                 flow_dv_counter_free(dev, flow->counter);
13764                 flow->counter = 0;
13765         }
13766         if (flow->meter) {
13767                 fm = flow_dv_meter_find_by_idx(priv, flow->meter);
13768                 if (fm)
13769                         mlx5_flow_meter_detach(priv, fm);
13770                 flow->meter = 0;
13771         }
13772         /* Keep the current age handling by default. */
13773         if (flow->indirect_type == MLX5_INDIRECT_ACTION_TYPE_CT && flow->ct)
13774                 flow_dv_aso_ct_release(dev, flow->ct);
13775         else if (flow->age)
13776                 flow_dv_aso_age_release(dev, flow->age);
13777         if (flow->geneve_tlv_option) {
13778                 flow_dv_geneve_tlv_option_resource_release(dev);
13779                 flow->geneve_tlv_option = 0;
13780         }
13781         while (flow->dev_handles) {
13782                 uint32_t tmp_idx = flow->dev_handles;
13783
13784                 dev_handle = mlx5_ipool_get(priv->sh->ipool
13785                                             [MLX5_IPOOL_MLX5_FLOW], tmp_idx);
13786                 if (!dev_handle)
13787                         return;
13788                 flow->dev_handles = dev_handle->next.next;
13789                 if (dev_handle->dvh.matcher)
13790                         flow_dv_matcher_release(dev, dev_handle);
13791                 if (dev_handle->dvh.rix_sample)
13792                         flow_dv_sample_resource_release(dev, dev_handle);
13793                 if (dev_handle->dvh.rix_dest_array)
13794                         flow_dv_dest_array_resource_release(dev, dev_handle);
13795                 if (dev_handle->dvh.rix_encap_decap)
13796                         flow_dv_encap_decap_resource_release(dev,
13797                                 dev_handle->dvh.rix_encap_decap);
13798                 if (dev_handle->dvh.modify_hdr)
13799                         flow_dv_modify_hdr_resource_release(dev, dev_handle);
13800                 if (dev_handle->dvh.rix_push_vlan)
13801                         flow_dv_push_vlan_action_resource_release(dev,
13802                                                                   dev_handle);
13803                 if (dev_handle->dvh.rix_tag)
13804                         flow_dv_tag_release(dev,
13805                                             dev_handle->dvh.rix_tag);
13806                 if (dev_handle->fate_action != MLX5_FLOW_FATE_SHARED_RSS)
13807                         flow_dv_fate_resource_release(dev, dev_handle);
13808                 else if (!srss)
13809                         srss = dev_handle->rix_srss;
13810                 if (fm && dev_handle->is_meter_flow_id &&
13811                     dev_handle->split_flow_id)
13812                         mlx5_ipool_free(fm->flow_ipool,
13813                                         dev_handle->split_flow_id);
13814                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
13815                            tmp_idx);
13816         }
13817         if (srss)
13818                 flow_dv_shared_rss_action_release(dev, srss);
13819 }
13820
13821 /**
13822  * Release array of hash RX queue objects.
13823  * Helper function.
13824  *
13825  * @param[in] dev
13826  *   Pointer to the Ethernet device structure.
13827  * @param[in, out] hrxqs
13828  *   Array of hash RX queue objects.
13829  *
13830  * @return
13831  *   Total number of references to hash RX queue objects in *hrxqs* array
13832  *   after this operation.
13833  */
13834 static int
13835 __flow_dv_hrxqs_release(struct rte_eth_dev *dev,
13836                         uint32_t (*hrxqs)[MLX5_RSS_HASH_FIELDS_LEN])
13837 {
13838         size_t i;
13839         int remaining = 0;
13840
13841         for (i = 0; i < RTE_DIM(*hrxqs); i++) {
13842                 int ret = mlx5_hrxq_release(dev, (*hrxqs)[i]);
13843
13844                 if (!ret)
13845                         (*hrxqs)[i] = 0;
13846                 remaining += ret;
13847         }
13848         return remaining;
13849 }
13850
13851 /**
13852  * Release all hash RX queue objects representing shared RSS action.
13853  *
13854  * @param[in] dev
13855  *   Pointer to the Ethernet device structure.
13856  * @param[in, out] action
13857  *   Shared RSS action to remove hash RX queue objects from.
13858  *
13859  * @return
13860  *   Total number of references to hash RX queue objects stored in *action*
13861  *   after this operation.
13862  *   Expected to be 0 if no external references held.
13863  */
13864 static int
13865 __flow_dv_action_rss_hrxqs_release(struct rte_eth_dev *dev,
13866                                  struct mlx5_shared_action_rss *shared_rss)
13867 {
13868         return __flow_dv_hrxqs_release(dev, &shared_rss->hrxq);
13869 }
13870
13871 /**
13872  * Adjust L3/L4 hash value of pre-created shared RSS hrxq according to
13873  * user input.
13874  *
13875  * Only one hash value is available for one L3+L4 combination:
13876  * for example:
13877  * MLX5_RSS_HASH_IPV4, MLX5_RSS_HASH_IPV4_SRC_ONLY, and
13878  * MLX5_RSS_HASH_IPV4_DST_ONLY are mutually exclusive so they can share
13879  * same slot in mlx5_rss_hash_fields.
13880  *
13881  * @param[in] rss
13882  *   Pointer to the shared action RSS conf.
13883  * @param[in, out] hash_field
13884  *   hash_field variable needed to be adjusted.
13885  *
13886  * @return
13887  *   void
13888  */
13889 static void
13890 __flow_dv_action_rss_l34_hash_adjust(struct mlx5_shared_action_rss *rss,
13891                                      uint64_t *hash_field)
13892 {
13893         uint64_t rss_types = rss->origin.types;
13894
13895         switch (*hash_field & ~IBV_RX_HASH_INNER) {
13896         case MLX5_RSS_HASH_IPV4:
13897                 if (rss_types & MLX5_IPV4_LAYER_TYPES) {
13898                         *hash_field &= ~MLX5_RSS_HASH_IPV4;
13899                         if (rss_types & ETH_RSS_L3_DST_ONLY)
13900                                 *hash_field |= IBV_RX_HASH_DST_IPV4;
13901                         else if (rss_types & ETH_RSS_L3_SRC_ONLY)
13902                                 *hash_field |= IBV_RX_HASH_SRC_IPV4;
13903                         else
13904                                 *hash_field |= MLX5_RSS_HASH_IPV4;
13905                 }
13906                 return;
13907         case MLX5_RSS_HASH_IPV6:
13908                 if (rss_types & MLX5_IPV6_LAYER_TYPES) {
13909                         *hash_field &= ~MLX5_RSS_HASH_IPV6;
13910                         if (rss_types & ETH_RSS_L3_DST_ONLY)
13911                                 *hash_field |= IBV_RX_HASH_DST_IPV6;
13912                         else if (rss_types & ETH_RSS_L3_SRC_ONLY)
13913                                 *hash_field |= IBV_RX_HASH_SRC_IPV6;
13914                         else
13915                                 *hash_field |= MLX5_RSS_HASH_IPV6;
13916                 }
13917                 return;
13918         case MLX5_RSS_HASH_IPV4_UDP:
13919                 /* fall-through. */
13920         case MLX5_RSS_HASH_IPV6_UDP:
13921                 if (rss_types & ETH_RSS_UDP) {
13922                         *hash_field &= ~MLX5_UDP_IBV_RX_HASH;
13923                         if (rss_types & ETH_RSS_L4_DST_ONLY)
13924                                 *hash_field |= IBV_RX_HASH_DST_PORT_UDP;
13925                         else if (rss_types & ETH_RSS_L4_SRC_ONLY)
13926                                 *hash_field |= IBV_RX_HASH_SRC_PORT_UDP;
13927                         else
13928                                 *hash_field |= MLX5_UDP_IBV_RX_HASH;
13929                 }
13930                 return;
13931         case MLX5_RSS_HASH_IPV4_TCP:
13932                 /* fall-through. */
13933         case MLX5_RSS_HASH_IPV6_TCP:
13934                 if (rss_types & ETH_RSS_TCP) {
13935                         *hash_field &= ~MLX5_TCP_IBV_RX_HASH;
13936                         if (rss_types & ETH_RSS_L4_DST_ONLY)
13937                                 *hash_field |= IBV_RX_HASH_DST_PORT_TCP;
13938                         else if (rss_types & ETH_RSS_L4_SRC_ONLY)
13939                                 *hash_field |= IBV_RX_HASH_SRC_PORT_TCP;
13940                         else
13941                                 *hash_field |= MLX5_TCP_IBV_RX_HASH;
13942                 }
13943                 return;
13944         default:
13945                 return;
13946         }
13947 }
13948
13949 /**
13950  * Setup shared RSS action.
13951  * Prepare set of hash RX queue objects sufficient to handle all valid
13952  * hash_fields combinations (see enum ibv_rx_hash_fields).
13953  *
13954  * @param[in] dev
13955  *   Pointer to the Ethernet device structure.
13956  * @param[in] action_idx
13957  *   Shared RSS action ipool index.
13958  * @param[in, out] action
13959  *   Partially initialized shared RSS action.
13960  * @param[out] error
13961  *   Perform verbose error reporting if not NULL. Initialized in case of
13962  *   error only.
13963  *
13964  * @return
13965  *   0 on success, otherwise negative errno value.
13966  */
13967 static int
13968 __flow_dv_action_rss_setup(struct rte_eth_dev *dev,
13969                            uint32_t action_idx,
13970                            struct mlx5_shared_action_rss *shared_rss,
13971                            struct rte_flow_error *error)
13972 {
13973         struct mlx5_flow_rss_desc rss_desc = { 0 };
13974         size_t i;
13975         int err;
13976
13977         if (mlx5_ind_table_obj_setup(dev, shared_rss->ind_tbl)) {
13978                 return rte_flow_error_set(error, rte_errno,
13979                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
13980                                           "cannot setup indirection table");
13981         }
13982         memcpy(rss_desc.key, shared_rss->origin.key, MLX5_RSS_HASH_KEY_LEN);
13983         rss_desc.key_len = MLX5_RSS_HASH_KEY_LEN;
13984         rss_desc.const_q = shared_rss->origin.queue;
13985         rss_desc.queue_num = shared_rss->origin.queue_num;
13986         /* Set non-zero value to indicate a shared RSS. */
13987         rss_desc.shared_rss = action_idx;
13988         rss_desc.ind_tbl = shared_rss->ind_tbl;
13989         for (i = 0; i < MLX5_RSS_HASH_FIELDS_LEN; i++) {
13990                 uint32_t hrxq_idx;
13991                 uint64_t hash_fields = mlx5_rss_hash_fields[i];
13992                 int tunnel = 0;
13993
13994                 __flow_dv_action_rss_l34_hash_adjust(shared_rss, &hash_fields);
13995                 if (shared_rss->origin.level > 1) {
13996                         hash_fields |= IBV_RX_HASH_INNER;
13997                         tunnel = 1;
13998                 }
13999                 rss_desc.tunnel = tunnel;
14000                 rss_desc.hash_fields = hash_fields;
14001                 hrxq_idx = mlx5_hrxq_get(dev, &rss_desc);
14002                 if (!hrxq_idx) {
14003                         rte_flow_error_set
14004                                 (error, rte_errno,
14005                                  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
14006                                  "cannot get hash queue");
14007                         goto error_hrxq_new;
14008                 }
14009                 err = __flow_dv_action_rss_hrxq_set
14010                         (shared_rss, hash_fields, hrxq_idx);
14011                 MLX5_ASSERT(!err);
14012         }
14013         return 0;
14014 error_hrxq_new:
14015         err = rte_errno;
14016         __flow_dv_action_rss_hrxqs_release(dev, shared_rss);
14017         if (!mlx5_ind_table_obj_release(dev, shared_rss->ind_tbl, true))
14018                 shared_rss->ind_tbl = NULL;
14019         rte_errno = err;
14020         return -rte_errno;
14021 }
14022
14023 /**
14024  * Create shared RSS action.
14025  *
14026  * @param[in] dev
14027  *   Pointer to the Ethernet device structure.
14028  * @param[in] conf
14029  *   Shared action configuration.
14030  * @param[in] rss
14031  *   RSS action specification used to create shared action.
14032  * @param[out] error
14033  *   Perform verbose error reporting if not NULL. Initialized in case of
14034  *   error only.
14035  *
14036  * @return
14037  *   A valid shared action ID in case of success, 0 otherwise and
14038  *   rte_errno is set.
14039  */
14040 static uint32_t
14041 __flow_dv_action_rss_create(struct rte_eth_dev *dev,
14042                             const struct rte_flow_indir_action_conf *conf,
14043                             const struct rte_flow_action_rss *rss,
14044                             struct rte_flow_error *error)
14045 {
14046         struct mlx5_priv *priv = dev->data->dev_private;
14047         struct mlx5_shared_action_rss *shared_rss = NULL;
14048         void *queue = NULL;
14049         struct rte_flow_action_rss *origin;
14050         const uint8_t *rss_key;
14051         uint32_t queue_size = rss->queue_num * sizeof(uint16_t);
14052         uint32_t idx;
14053
14054         RTE_SET_USED(conf);
14055         queue = mlx5_malloc(0, RTE_ALIGN_CEIL(queue_size, sizeof(void *)),
14056                             0, SOCKET_ID_ANY);
14057         shared_rss = mlx5_ipool_zmalloc
14058                          (priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS], &idx);
14059         if (!shared_rss || !queue) {
14060                 rte_flow_error_set(error, ENOMEM,
14061                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
14062                                    "cannot allocate resource memory");
14063                 goto error_rss_init;
14064         }
14065         if (idx > (1u << MLX5_INDIRECT_ACTION_TYPE_OFFSET)) {
14066                 rte_flow_error_set(error, E2BIG,
14067                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
14068                                    "rss action number out of range");
14069                 goto error_rss_init;
14070         }
14071         shared_rss->ind_tbl = mlx5_malloc(MLX5_MEM_ZERO,
14072                                           sizeof(*shared_rss->ind_tbl),
14073                                           0, SOCKET_ID_ANY);
14074         if (!shared_rss->ind_tbl) {
14075                 rte_flow_error_set(error, ENOMEM,
14076                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
14077                                    "cannot allocate resource memory");
14078                 goto error_rss_init;
14079         }
14080         memcpy(queue, rss->queue, queue_size);
14081         shared_rss->ind_tbl->queues = queue;
14082         shared_rss->ind_tbl->queues_n = rss->queue_num;
14083         origin = &shared_rss->origin;
14084         origin->func = rss->func;
14085         origin->level = rss->level;
14086         /* RSS type 0 indicates default RSS type (ETH_RSS_IP). */
14087         origin->types = !rss->types ? ETH_RSS_IP : rss->types;
14088         /* NULL RSS key indicates default RSS key. */
14089         rss_key = !rss->key ? rss_hash_default_key : rss->key;
14090         memcpy(shared_rss->key, rss_key, MLX5_RSS_HASH_KEY_LEN);
14091         origin->key = &shared_rss->key[0];
14092         origin->key_len = MLX5_RSS_HASH_KEY_LEN;
14093         origin->queue = queue;
14094         origin->queue_num = rss->queue_num;
14095         if (__flow_dv_action_rss_setup(dev, idx, shared_rss, error))
14096                 goto error_rss_init;
14097         rte_spinlock_init(&shared_rss->action_rss_sl);
14098         __atomic_add_fetch(&shared_rss->refcnt, 1, __ATOMIC_RELAXED);
14099         rte_spinlock_lock(&priv->shared_act_sl);
14100         ILIST_INSERT(priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS],
14101                      &priv->rss_shared_actions, idx, shared_rss, next);
14102         rte_spinlock_unlock(&priv->shared_act_sl);
14103         return idx;
14104 error_rss_init:
14105         if (shared_rss) {
14106                 if (shared_rss->ind_tbl)
14107                         mlx5_free(shared_rss->ind_tbl);
14108                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS],
14109                                 idx);
14110         }
14111         if (queue)
14112                 mlx5_free(queue);
14113         return 0;
14114 }
14115
14116 /**
14117  * Destroy the shared RSS action.
14118  * Release related hash RX queue objects.
14119  *
14120  * @param[in] dev
14121  *   Pointer to the Ethernet device structure.
14122  * @param[in] idx
14123  *   The shared RSS action object ID to be removed.
14124  * @param[out] error
14125  *   Perform verbose error reporting if not NULL. Initialized in case of
14126  *   error only.
14127  *
14128  * @return
14129  *   0 on success, otherwise negative errno value.
14130  */
14131 static int
14132 __flow_dv_action_rss_release(struct rte_eth_dev *dev, uint32_t idx,
14133                              struct rte_flow_error *error)
14134 {
14135         struct mlx5_priv *priv = dev->data->dev_private;
14136         struct mlx5_shared_action_rss *shared_rss =
14137             mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS], idx);
14138         uint32_t old_refcnt = 1;
14139         int remaining;
14140         uint16_t *queue = NULL;
14141
14142         if (!shared_rss)
14143                 return rte_flow_error_set(error, EINVAL,
14144                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
14145                                           "invalid shared action");
14146         remaining = __flow_dv_action_rss_hrxqs_release(dev, shared_rss);
14147         if (remaining)
14148                 return rte_flow_error_set(error, EBUSY,
14149                                           RTE_FLOW_ERROR_TYPE_ACTION,
14150                                           NULL,
14151                                           "shared rss hrxq has references");
14152         if (!__atomic_compare_exchange_n(&shared_rss->refcnt, &old_refcnt,
14153                                          0, 0, __ATOMIC_ACQUIRE,
14154                                          __ATOMIC_RELAXED))
14155                 return rte_flow_error_set(error, EBUSY,
14156                                           RTE_FLOW_ERROR_TYPE_ACTION,
14157                                           NULL,
14158                                           "shared rss has references");
14159         queue = shared_rss->ind_tbl->queues;
14160         remaining = mlx5_ind_table_obj_release(dev, shared_rss->ind_tbl, true);
14161         if (remaining)
14162                 return rte_flow_error_set(error, EBUSY,
14163                                           RTE_FLOW_ERROR_TYPE_ACTION,
14164                                           NULL,
14165                                           "shared rss indirection table has"
14166                                           " references");
14167         mlx5_free(queue);
14168         rte_spinlock_lock(&priv->shared_act_sl);
14169         ILIST_REMOVE(priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS],
14170                      &priv->rss_shared_actions, idx, shared_rss, next);
14171         rte_spinlock_unlock(&priv->shared_act_sl);
14172         mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS],
14173                         idx);
14174         return 0;
14175 }
14176
14177 /**
14178  * Create indirect action, lock free,
14179  * (mutex should be acquired by caller).
14180  * Dispatcher for action type specific call.
14181  *
14182  * @param[in] dev
14183  *   Pointer to the Ethernet device structure.
14184  * @param[in] conf
14185  *   Shared action configuration.
14186  * @param[in] action
14187  *   Action specification used to create indirect action.
14188  * @param[out] error
14189  *   Perform verbose error reporting if not NULL. Initialized in case of
14190  *   error only.
14191  *
14192  * @return
14193  *   A valid shared action handle in case of success, NULL otherwise and
14194  *   rte_errno is set.
14195  */
14196 static struct rte_flow_action_handle *
14197 flow_dv_action_create(struct rte_eth_dev *dev,
14198                       const struct rte_flow_indir_action_conf *conf,
14199                       const struct rte_flow_action *action,
14200                       struct rte_flow_error *err)
14201 {
14202         uint32_t idx = 0;
14203         uint32_t ret = 0;
14204         struct mlx5_priv *priv = dev->data->dev_private;
14205
14206         switch (action->type) {
14207         case RTE_FLOW_ACTION_TYPE_RSS:
14208                 ret = __flow_dv_action_rss_create(dev, conf, action->conf, err);
14209                 idx = (MLX5_INDIRECT_ACTION_TYPE_RSS <<
14210                        MLX5_INDIRECT_ACTION_TYPE_OFFSET) | ret;
14211                 break;
14212         case RTE_FLOW_ACTION_TYPE_AGE:
14213                 ret = flow_dv_translate_create_aso_age(dev, action->conf, err);
14214                 idx = (MLX5_INDIRECT_ACTION_TYPE_AGE <<
14215                        MLX5_INDIRECT_ACTION_TYPE_OFFSET) | ret;
14216                 if (ret) {
14217                         struct mlx5_aso_age_action *aso_age =
14218                                               flow_aso_age_get_by_idx(dev, ret);
14219
14220                         if (!aso_age->age_params.context)
14221                                 aso_age->age_params.context =
14222                                                          (void *)(uintptr_t)idx;
14223                 }
14224                 break;
14225         case RTE_FLOW_ACTION_TYPE_COUNT:
14226                 ret = flow_dv_translate_create_counter(dev, NULL, NULL, NULL);
14227                 idx = (MLX5_INDIRECT_ACTION_TYPE_COUNT <<
14228                        MLX5_INDIRECT_ACTION_TYPE_OFFSET) | ret;
14229                 break;
14230         case RTE_FLOW_ACTION_TYPE_CONNTRACK:
14231                 ret = flow_dv_translate_create_conntrack(dev, action->conf,
14232                                                          err);
14233                 idx = MLX5_INDIRECT_ACT_CT_GEN_IDX(PORT_ID(priv), ret);
14234                 break;
14235         default:
14236                 rte_flow_error_set(err, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
14237                                    NULL, "action type not supported");
14238                 break;
14239         }
14240         return ret ? (struct rte_flow_action_handle *)(uintptr_t)idx : NULL;
14241 }
14242
14243 /**
14244  * Destroy the indirect action.
14245  * Release action related resources on the NIC and the memory.
14246  * Lock free, (mutex should be acquired by caller).
14247  * Dispatcher for action type specific call.
14248  *
14249  * @param[in] dev
14250  *   Pointer to the Ethernet device structure.
14251  * @param[in] handle
14252  *   The indirect action object handle to be removed.
14253  * @param[out] error
14254  *   Perform verbose error reporting if not NULL. Initialized in case of
14255  *   error only.
14256  *
14257  * @return
14258  *   0 on success, otherwise negative errno value.
14259  */
14260 static int
14261 flow_dv_action_destroy(struct rte_eth_dev *dev,
14262                        struct rte_flow_action_handle *handle,
14263                        struct rte_flow_error *error)
14264 {
14265         uint32_t act_idx = (uint32_t)(uintptr_t)handle;
14266         uint32_t type = act_idx >> MLX5_INDIRECT_ACTION_TYPE_OFFSET;
14267         uint32_t idx = act_idx & ((1u << MLX5_INDIRECT_ACTION_TYPE_OFFSET) - 1);
14268         struct mlx5_flow_counter *cnt;
14269         uint32_t no_flow_refcnt = 1;
14270         int ret;
14271
14272         switch (type) {
14273         case MLX5_INDIRECT_ACTION_TYPE_RSS:
14274                 return __flow_dv_action_rss_release(dev, idx, error);
14275         case MLX5_INDIRECT_ACTION_TYPE_COUNT:
14276                 cnt = flow_dv_counter_get_by_idx(dev, idx, NULL);
14277                 if (!__atomic_compare_exchange_n(&cnt->shared_info.refcnt,
14278                                                  &no_flow_refcnt, 1, false,
14279                                                  __ATOMIC_ACQUIRE,
14280                                                  __ATOMIC_RELAXED))
14281                         return rte_flow_error_set(error, EBUSY,
14282                                                   RTE_FLOW_ERROR_TYPE_ACTION,
14283                                                   NULL,
14284                                                   "Indirect count action has references");
14285                 flow_dv_counter_free(dev, idx);
14286                 return 0;
14287         case MLX5_INDIRECT_ACTION_TYPE_AGE:
14288                 ret = flow_dv_aso_age_release(dev, idx);
14289                 if (ret)
14290                         /*
14291                          * In this case, the last flow has a reference will
14292                          * actually release the age action.
14293                          */
14294                         DRV_LOG(DEBUG, "Indirect age action %" PRIu32 " was"
14295                                 " released with references %d.", idx, ret);
14296                 return 0;
14297         case MLX5_INDIRECT_ACTION_TYPE_CT:
14298                 ret = flow_dv_aso_ct_release(dev, idx);
14299                 if (ret < 0)
14300                         return ret;
14301                 if (ret > 0)
14302                         DRV_LOG(DEBUG, "Connection tracking object %u still "
14303                                 "has references %d.", idx, ret);
14304                 return 0;
14305         default:
14306                 return rte_flow_error_set(error, ENOTSUP,
14307                                           RTE_FLOW_ERROR_TYPE_ACTION,
14308                                           NULL,
14309                                           "action type not supported");
14310         }
14311 }
14312
14313 /**
14314  * Updates in place shared RSS action configuration.
14315  *
14316  * @param[in] dev
14317  *   Pointer to the Ethernet device structure.
14318  * @param[in] idx
14319  *   The shared RSS action object ID to be updated.
14320  * @param[in] action_conf
14321  *   RSS action specification used to modify *shared_rss*.
14322  * @param[out] error
14323  *   Perform verbose error reporting if not NULL. Initialized in case of
14324  *   error only.
14325  *
14326  * @return
14327  *   0 on success, otherwise negative errno value.
14328  * @note: currently only support update of RSS queues.
14329  */
14330 static int
14331 __flow_dv_action_rss_update(struct rte_eth_dev *dev, uint32_t idx,
14332                             const struct rte_flow_action_rss *action_conf,
14333                             struct rte_flow_error *error)
14334 {
14335         struct mlx5_priv *priv = dev->data->dev_private;
14336         struct mlx5_shared_action_rss *shared_rss =
14337             mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS], idx);
14338         int ret = 0;
14339         void *queue = NULL;
14340         uint16_t *queue_old = NULL;
14341         uint32_t queue_size = action_conf->queue_num * sizeof(uint16_t);
14342
14343         if (!shared_rss)
14344                 return rte_flow_error_set(error, EINVAL,
14345                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
14346                                           "invalid shared action to update");
14347         if (priv->obj_ops.ind_table_modify == NULL)
14348                 return rte_flow_error_set(error, ENOTSUP,
14349                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
14350                                           "cannot modify indirection table");
14351         queue = mlx5_malloc(MLX5_MEM_ZERO,
14352                             RTE_ALIGN_CEIL(queue_size, sizeof(void *)),
14353                             0, SOCKET_ID_ANY);
14354         if (!queue)
14355                 return rte_flow_error_set(error, ENOMEM,
14356                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
14357                                           NULL,
14358                                           "cannot allocate resource memory");
14359         memcpy(queue, action_conf->queue, queue_size);
14360         MLX5_ASSERT(shared_rss->ind_tbl);
14361         rte_spinlock_lock(&shared_rss->action_rss_sl);
14362         queue_old = shared_rss->ind_tbl->queues;
14363         ret = mlx5_ind_table_obj_modify(dev, shared_rss->ind_tbl,
14364                                         queue, action_conf->queue_num, true);
14365         if (ret) {
14366                 mlx5_free(queue);
14367                 ret = rte_flow_error_set(error, rte_errno,
14368                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
14369                                           "cannot update indirection table");
14370         } else {
14371                 mlx5_free(queue_old);
14372                 shared_rss->origin.queue = queue;
14373                 shared_rss->origin.queue_num = action_conf->queue_num;
14374         }
14375         rte_spinlock_unlock(&shared_rss->action_rss_sl);
14376         return ret;
14377 }
14378
14379 /*
14380  * Updates in place conntrack context or direction.
14381  * Context update should be synchronized.
14382  *
14383  * @param[in] dev
14384  *   Pointer to the Ethernet device structure.
14385  * @param[in] idx
14386  *   The conntrack object ID to be updated.
14387  * @param[in] update
14388  *   Pointer to the structure of information to update.
14389  * @param[out] error
14390  *   Perform verbose error reporting if not NULL. Initialized in case of
14391  *   error only.
14392  *
14393  * @return
14394  *   0 on success, otherwise negative errno value.
14395  */
14396 static int
14397 __flow_dv_action_ct_update(struct rte_eth_dev *dev, uint32_t idx,
14398                            const struct rte_flow_modify_conntrack *update,
14399                            struct rte_flow_error *error)
14400 {
14401         struct mlx5_priv *priv = dev->data->dev_private;
14402         struct mlx5_aso_ct_action *ct;
14403         const struct rte_flow_action_conntrack *new_prf;
14404         int ret = 0;
14405         uint16_t owner = (uint16_t)MLX5_INDIRECT_ACT_CT_GET_OWNER(idx);
14406         uint32_t dev_idx;
14407
14408         if (PORT_ID(priv) != owner)
14409                 return rte_flow_error_set(error, EACCES,
14410                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
14411                                           NULL,
14412                                           "CT object owned by another port");
14413         dev_idx = MLX5_INDIRECT_ACT_CT_GET_IDX(idx);
14414         ct = flow_aso_ct_get_by_dev_idx(dev, dev_idx);
14415         if (!ct->refcnt)
14416                 return rte_flow_error_set(error, ENOMEM,
14417                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
14418                                           NULL,
14419                                           "CT object is inactive");
14420         new_prf = &update->new_ct;
14421         if (update->direction)
14422                 ct->is_original = !!new_prf->is_original_dir;
14423         if (update->state) {
14424                 /* Only validate the profile when it needs to be updated. */
14425                 ret = mlx5_validate_action_ct(dev, new_prf, error);
14426                 if (ret)
14427                         return ret;
14428                 ret = mlx5_aso_ct_update_by_wqe(priv->sh, ct, new_prf);
14429                 if (ret)
14430                         return rte_flow_error_set(error, EIO,
14431                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
14432                                         NULL,
14433                                         "Failed to send CT context update WQE");
14434                 /* Block until ready or a failure. */
14435                 ret = mlx5_aso_ct_available(priv->sh, ct);
14436                 if (ret)
14437                         rte_flow_error_set(error, rte_errno,
14438                                            RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
14439                                            NULL,
14440                                            "Timeout to get the CT update");
14441         }
14442         return ret;
14443 }
14444
14445 /**
14446  * Updates in place shared action configuration, lock free,
14447  * (mutex should be acquired by caller).
14448  *
14449  * @param[in] dev
14450  *   Pointer to the Ethernet device structure.
14451  * @param[in] handle
14452  *   The indirect action object handle to be updated.
14453  * @param[in] update
14454  *   Action specification used to modify the action pointed by *handle*.
14455  *   *update* could be of same type with the action pointed by the *handle*
14456  *   handle argument, or some other structures like a wrapper, depending on
14457  *   the indirect action type.
14458  * @param[out] error
14459  *   Perform verbose error reporting if not NULL. Initialized in case of
14460  *   error only.
14461  *
14462  * @return
14463  *   0 on success, otherwise negative errno value.
14464  */
14465 static int
14466 flow_dv_action_update(struct rte_eth_dev *dev,
14467                         struct rte_flow_action_handle *handle,
14468                         const void *update,
14469                         struct rte_flow_error *err)
14470 {
14471         uint32_t act_idx = (uint32_t)(uintptr_t)handle;
14472         uint32_t type = act_idx >> MLX5_INDIRECT_ACTION_TYPE_OFFSET;
14473         uint32_t idx = act_idx & ((1u << MLX5_INDIRECT_ACTION_TYPE_OFFSET) - 1);
14474         const void *action_conf;
14475
14476         switch (type) {
14477         case MLX5_INDIRECT_ACTION_TYPE_RSS:
14478                 action_conf = ((const struct rte_flow_action *)update)->conf;
14479                 return __flow_dv_action_rss_update(dev, idx, action_conf, err);
14480         case MLX5_INDIRECT_ACTION_TYPE_CT:
14481                 return __flow_dv_action_ct_update(dev, idx, update, err);
14482         default:
14483                 return rte_flow_error_set(err, ENOTSUP,
14484                                           RTE_FLOW_ERROR_TYPE_ACTION,
14485                                           NULL,
14486                                           "action type update not supported");
14487         }
14488 }
14489
14490 /**
14491  * Destroy the meter sub policy table rules.
14492  * Lock free, (mutex should be acquired by caller).
14493  *
14494  * @param[in] dev
14495  *   Pointer to Ethernet device.
14496  * @param[in] sub_policy
14497  *   Pointer to meter sub policy table.
14498  */
14499 static void
14500 __flow_dv_destroy_sub_policy_rules(struct rte_eth_dev *dev,
14501                              struct mlx5_flow_meter_sub_policy *sub_policy)
14502 {
14503         struct mlx5_flow_tbl_data_entry *tbl;
14504         int i;
14505
14506         for (i = 0; i < RTE_COLORS; i++) {
14507                 if (sub_policy->color_rule[i]) {
14508                         claim_zero(mlx5_flow_os_destroy_flow
14509                                 (sub_policy->color_rule[i]));
14510                         sub_policy->color_rule[i] = NULL;
14511                 }
14512                 if (sub_policy->color_matcher[i]) {
14513                         tbl = container_of(sub_policy->color_matcher[i]->tbl,
14514                                 typeof(*tbl), tbl);
14515                         mlx5_cache_unregister(&tbl->matchers,
14516                                       &sub_policy->color_matcher[i]->entry);
14517                         sub_policy->color_matcher[i] = NULL;
14518                 }
14519         }
14520         for (i = 0; i < MLX5_MTR_RTE_COLORS; i++) {
14521                 if (sub_policy->rix_hrxq[i]) {
14522                         mlx5_hrxq_release(dev, sub_policy->rix_hrxq[i]);
14523                         sub_policy->rix_hrxq[i] = 0;
14524                 }
14525                 if (sub_policy->jump_tbl[i]) {
14526                         flow_dv_tbl_resource_release(MLX5_SH(dev),
14527                         sub_policy->jump_tbl[i]);
14528                         sub_policy->jump_tbl[i] = NULL;
14529                 }
14530         }
14531         if (sub_policy->tbl_rsc) {
14532                 flow_dv_tbl_resource_release(MLX5_SH(dev),
14533                         sub_policy->tbl_rsc);
14534                 sub_policy->tbl_rsc = NULL;
14535         }
14536 }
14537
14538 /**
14539  * Destroy policy rules, lock free,
14540  * (mutex should be acquired by caller).
14541  * Dispatcher for action type specific call.
14542  *
14543  * @param[in] dev
14544  *   Pointer to the Ethernet device structure.
14545  * @param[in] mtr_policy
14546  *   Meter policy struct.
14547  */
14548 static void
14549 flow_dv_destroy_policy_rules(struct rte_eth_dev *dev,
14550                       struct mlx5_flow_meter_policy *mtr_policy)
14551 {
14552         uint32_t i, j;
14553         struct mlx5_flow_meter_sub_policy *sub_policy;
14554         uint16_t sub_policy_num;
14555
14556         for (i = 0; i < MLX5_MTR_DOMAIN_MAX; i++) {
14557                 sub_policy_num = (mtr_policy->sub_policy_num >>
14558                         (MLX5_MTR_SUB_POLICY_NUM_SHIFT * i)) &
14559                         MLX5_MTR_SUB_POLICY_NUM_MASK;
14560                 for (j = 0; j < sub_policy_num; j++) {
14561                         sub_policy = mtr_policy->sub_policys[i][j];
14562                         if (sub_policy)
14563                                 __flow_dv_destroy_sub_policy_rules
14564                                                 (dev, sub_policy);
14565                 }
14566         }
14567 }
14568
14569 /**
14570  * Destroy policy action, lock free,
14571  * (mutex should be acquired by caller).
14572  * Dispatcher for action type specific call.
14573  *
14574  * @param[in] dev
14575  *   Pointer to the Ethernet device structure.
14576  * @param[in] mtr_policy
14577  *   Meter policy struct.
14578  */
14579 static void
14580 flow_dv_destroy_mtr_policy_acts(struct rte_eth_dev *dev,
14581                       struct mlx5_flow_meter_policy *mtr_policy)
14582 {
14583         struct rte_flow_action *rss_action;
14584         struct mlx5_flow_handle dev_handle;
14585         uint32_t i, j;
14586
14587         for (i = 0; i < MLX5_MTR_RTE_COLORS; i++) {
14588                 if (mtr_policy->act_cnt[i].rix_mark) {
14589                         flow_dv_tag_release(dev,
14590                                 mtr_policy->act_cnt[i].rix_mark);
14591                         mtr_policy->act_cnt[i].rix_mark = 0;
14592                 }
14593                 if (mtr_policy->act_cnt[i].modify_hdr) {
14594                         dev_handle.dvh.modify_hdr =
14595                                 mtr_policy->act_cnt[i].modify_hdr;
14596                         flow_dv_modify_hdr_resource_release(dev, &dev_handle);
14597                 }
14598                 switch (mtr_policy->act_cnt[i].fate_action) {
14599                 case MLX5_FLOW_FATE_SHARED_RSS:
14600                         rss_action = mtr_policy->act_cnt[i].rss;
14601                         mlx5_free(rss_action);
14602                         break;
14603                 case MLX5_FLOW_FATE_PORT_ID:
14604                         if (mtr_policy->act_cnt[i].rix_port_id_action) {
14605                                 flow_dv_port_id_action_resource_release(dev,
14606                                 mtr_policy->act_cnt[i].rix_port_id_action);
14607                                 mtr_policy->act_cnt[i].rix_port_id_action = 0;
14608                         }
14609                         break;
14610                 case MLX5_FLOW_FATE_DROP:
14611                 case MLX5_FLOW_FATE_JUMP:
14612                         for (j = 0; j < MLX5_MTR_DOMAIN_MAX; j++)
14613                                 mtr_policy->act_cnt[i].dr_jump_action[j] =
14614                                                 NULL;
14615                         break;
14616                 default:
14617                         /*Queue action do nothing*/
14618                         break;
14619                 }
14620         }
14621         for (j = 0; j < MLX5_MTR_DOMAIN_MAX; j++)
14622                 mtr_policy->dr_drop_action[j] = NULL;
14623 }
14624
14625 /**
14626  * Create policy action per domain, lock free,
14627  * (mutex should be acquired by caller).
14628  * Dispatcher for action type specific call.
14629  *
14630  * @param[in] dev
14631  *   Pointer to the Ethernet device structure.
14632  * @param[in] mtr_policy
14633  *   Meter policy struct.
14634  * @param[in] action
14635  *   Action specification used to create meter actions.
14636  * @param[out] error
14637  *   Perform verbose error reporting if not NULL. Initialized in case of
14638  *   error only.
14639  *
14640  * @return
14641  *   0 on success, otherwise negative errno value.
14642  */
14643 static int
14644 __flow_dv_create_domain_policy_acts(struct rte_eth_dev *dev,
14645                         struct mlx5_flow_meter_policy *mtr_policy,
14646                         const struct rte_flow_action *actions[RTE_COLORS],
14647                         enum mlx5_meter_domain domain,
14648                         struct rte_mtr_error *error)
14649 {
14650         struct mlx5_priv *priv = dev->data->dev_private;
14651         struct rte_flow_error flow_err;
14652         const struct rte_flow_action *act;
14653         uint64_t action_flags = 0;
14654         struct mlx5_flow_handle dh;
14655         struct mlx5_flow dev_flow;
14656         struct mlx5_flow_dv_port_id_action_resource port_id_action;
14657         int i, ret;
14658         uint8_t egress, transfer;
14659         struct mlx5_meter_policy_action_container *act_cnt = NULL;
14660         union {
14661                 struct mlx5_flow_dv_modify_hdr_resource res;
14662                 uint8_t len[sizeof(struct mlx5_flow_dv_modify_hdr_resource) +
14663                             sizeof(struct mlx5_modification_cmd) *
14664                             (MLX5_MAX_MODIFY_NUM + 1)];
14665         } mhdr_dummy;
14666
14667         egress = (domain == MLX5_MTR_DOMAIN_EGRESS) ? 1 : 0;
14668         transfer = (domain == MLX5_MTR_DOMAIN_TRANSFER) ? 1 : 0;
14669         memset(&dh, 0, sizeof(struct mlx5_flow_handle));
14670         memset(&dev_flow, 0, sizeof(struct mlx5_flow));
14671         memset(&port_id_action, 0,
14672                 sizeof(struct mlx5_flow_dv_port_id_action_resource));
14673         dev_flow.handle = &dh;
14674         dev_flow.dv.port_id_action = &port_id_action;
14675         dev_flow.external = true;
14676         for (i = 0; i < RTE_COLORS; i++) {
14677                 if (i < MLX5_MTR_RTE_COLORS)
14678                         act_cnt = &mtr_policy->act_cnt[i];
14679                 for (act = actions[i];
14680                         act && act->type != RTE_FLOW_ACTION_TYPE_END;
14681                         act++) {
14682                         switch (act->type) {
14683                         case RTE_FLOW_ACTION_TYPE_MARK:
14684                         {
14685                                 uint32_t tag_be = mlx5_flow_mark_set
14686                                         (((const struct rte_flow_action_mark *)
14687                                         (act->conf))->id);
14688
14689                                 if (i >= MLX5_MTR_RTE_COLORS)
14690                                         return -rte_mtr_error_set(error,
14691                                           ENOTSUP,
14692                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
14693                                           NULL,
14694                                           "cannot create policy "
14695                                           "mark action for this color");
14696                                 dev_flow.handle->mark = 1;
14697                                 if (flow_dv_tag_resource_register(dev, tag_be,
14698                                                   &dev_flow, &flow_err))
14699                                         return -rte_mtr_error_set(error,
14700                                         ENOTSUP,
14701                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
14702                                         NULL,
14703                                         "cannot setup policy mark action");
14704                                 MLX5_ASSERT(dev_flow.dv.tag_resource);
14705                                 act_cnt->rix_mark =
14706                                         dev_flow.handle->dvh.rix_tag;
14707                                 if (action_flags & MLX5_FLOW_ACTION_QUEUE) {
14708                                         dev_flow.handle->rix_hrxq =
14709                         mtr_policy->sub_policys[domain][0]->rix_hrxq[i];
14710                                         flow_drv_rxq_flags_set(dev,
14711                                                 dev_flow.handle);
14712                                 }
14713                                 action_flags |= MLX5_FLOW_ACTION_MARK;
14714                                 break;
14715                         }
14716                         case RTE_FLOW_ACTION_TYPE_SET_TAG:
14717                         {
14718                                 struct mlx5_flow_dv_modify_hdr_resource
14719                                         *mhdr_res = &mhdr_dummy.res;
14720
14721                                 if (i >= MLX5_MTR_RTE_COLORS)
14722                                         return -rte_mtr_error_set(error,
14723                                           ENOTSUP,
14724                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
14725                                           NULL,
14726                                           "cannot create policy "
14727                                           "set tag action for this color");
14728                                 memset(mhdr_res, 0, sizeof(*mhdr_res));
14729                                 mhdr_res->ft_type = transfer ?
14730                                         MLX5DV_FLOW_TABLE_TYPE_FDB :
14731                                         egress ?
14732                                         MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
14733                                         MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
14734                                 if (flow_dv_convert_action_set_tag
14735                                 (dev, mhdr_res,
14736                                 (const struct rte_flow_action_set_tag *)
14737                                 act->conf,  &flow_err))
14738                                         return -rte_mtr_error_set(error,
14739                                         ENOTSUP,
14740                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
14741                                         NULL, "cannot convert policy "
14742                                         "set tag action");
14743                                 if (!mhdr_res->actions_num)
14744                                         return -rte_mtr_error_set(error,
14745                                         ENOTSUP,
14746                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
14747                                         NULL, "cannot find policy "
14748                                         "set tag action");
14749                                 /* create modify action if needed. */
14750                                 dev_flow.dv.group = 1;
14751                                 if (flow_dv_modify_hdr_resource_register
14752                                         (dev, mhdr_res, &dev_flow, &flow_err))
14753                                         return -rte_mtr_error_set(error,
14754                                         ENOTSUP,
14755                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
14756                                         NULL, "cannot register policy "
14757                                         "set tag action");
14758                                 act_cnt->modify_hdr =
14759                                 dev_flow.handle->dvh.modify_hdr;
14760                                 if (action_flags & MLX5_FLOW_ACTION_QUEUE) {
14761                                         dev_flow.handle->rix_hrxq =
14762                                 mtr_policy->sub_policys[domain][0]->rix_hrxq[i];
14763                                         flow_drv_rxq_flags_set(dev,
14764                                                 dev_flow.handle);
14765                                 }
14766                                 action_flags |= MLX5_FLOW_ACTION_SET_TAG;
14767                                 break;
14768                         }
14769                         case RTE_FLOW_ACTION_TYPE_DROP:
14770                         {
14771                                 struct mlx5_flow_mtr_mng *mtrmng =
14772                                                 priv->sh->mtrmng;
14773                                 struct mlx5_flow_tbl_data_entry *tbl_data;
14774
14775                                 /*
14776                                  * Create the drop table with
14777                                  * METER DROP level.
14778                                  */
14779                                 if (!mtrmng->drop_tbl[domain]) {
14780                                         mtrmng->drop_tbl[domain] =
14781                                         flow_dv_tbl_resource_get(dev,
14782                                         MLX5_FLOW_TABLE_LEVEL_METER,
14783                                         egress, transfer, false, NULL, 0,
14784                                         0, MLX5_MTR_TABLE_ID_DROP, &flow_err);
14785                                         if (!mtrmng->drop_tbl[domain])
14786                                                 return -rte_mtr_error_set
14787                                         (error, ENOTSUP,
14788                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
14789                                         NULL,
14790                                         "Failed to create meter drop table");
14791                                 }
14792                                 tbl_data = container_of
14793                                 (mtrmng->drop_tbl[domain],
14794                                 struct mlx5_flow_tbl_data_entry, tbl);
14795                                 if (i < MLX5_MTR_RTE_COLORS) {
14796                                         act_cnt->dr_jump_action[domain] =
14797                                                 tbl_data->jump.action;
14798                                         act_cnt->fate_action =
14799                                                 MLX5_FLOW_FATE_DROP;
14800                                 }
14801                                 if (i == RTE_COLOR_RED)
14802                                         mtr_policy->dr_drop_action[domain] =
14803                                                 tbl_data->jump.action;
14804                                 action_flags |= MLX5_FLOW_ACTION_DROP;
14805                                 break;
14806                         }
14807                         case RTE_FLOW_ACTION_TYPE_QUEUE:
14808                         {
14809                                 struct mlx5_hrxq *hrxq;
14810                                 uint32_t hrxq_idx;
14811                                 struct mlx5_flow_rss_desc rss_desc;
14812                                 struct mlx5_flow_meter_sub_policy *sub_policy =
14813                                 mtr_policy->sub_policys[domain][0];
14814
14815                                 if (i >= MLX5_MTR_RTE_COLORS)
14816                                         return -rte_mtr_error_set(error,
14817                                         ENOTSUP,
14818                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
14819                                         NULL, "cannot create policy "
14820                                         "fate queue for this color");
14821                                 memset(&rss_desc, 0,
14822                                         sizeof(struct mlx5_flow_rss_desc));
14823                                 rss_desc.queue_num = 1;
14824                                 rss_desc.const_q = act->conf;
14825                                 hrxq = flow_dv_hrxq_prepare(dev, &dev_flow,
14826                                                     &rss_desc, &hrxq_idx);
14827                                 if (!hrxq)
14828                                         return -rte_mtr_error_set(error,
14829                                         ENOTSUP,
14830                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
14831                                         NULL,
14832                                         "cannot create policy fate queue");
14833                                 sub_policy->rix_hrxq[i] = hrxq_idx;
14834                                 act_cnt->fate_action =
14835                                         MLX5_FLOW_FATE_QUEUE;
14836                                 dev_flow.handle->fate_action =
14837                                         MLX5_FLOW_FATE_QUEUE;
14838                                 if (action_flags & MLX5_FLOW_ACTION_MARK ||
14839                                     action_flags & MLX5_FLOW_ACTION_SET_TAG) {
14840                                         dev_flow.handle->rix_hrxq = hrxq_idx;
14841                                         flow_drv_rxq_flags_set(dev,
14842                                                 dev_flow.handle);
14843                                 }
14844                                 action_flags |= MLX5_FLOW_ACTION_QUEUE;
14845                                 break;
14846                         }
14847                         case RTE_FLOW_ACTION_TYPE_RSS:
14848                         {
14849                                 int rss_size;
14850
14851                                 if (i >= MLX5_MTR_RTE_COLORS)
14852                                         return -rte_mtr_error_set(error,
14853                                           ENOTSUP,
14854                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
14855                                           NULL,
14856                                           "cannot create policy "
14857                                           "rss action for this color");
14858                                 /*
14859                                  * Save RSS conf into policy struct
14860                                  * for translate stage.
14861                                  */
14862                                 rss_size = (int)rte_flow_conv
14863                                         (RTE_FLOW_CONV_OP_ACTION,
14864                                         NULL, 0, act, &flow_err);
14865                                 if (rss_size <= 0)
14866                                         return -rte_mtr_error_set(error,
14867                                           ENOTSUP,
14868                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
14869                                           NULL, "Get the wrong "
14870                                           "rss action struct size");
14871                                 act_cnt->rss = mlx5_malloc(MLX5_MEM_ZERO,
14872                                                 rss_size, 0, SOCKET_ID_ANY);
14873                                 if (!act_cnt->rss)
14874                                         return -rte_mtr_error_set(error,
14875                                           ENOTSUP,
14876                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
14877                                           NULL,
14878                                           "Fail to malloc rss action memory");
14879                                 ret = rte_flow_conv(RTE_FLOW_CONV_OP_ACTION,
14880                                         act_cnt->rss, rss_size,
14881                                         act, &flow_err);
14882                                 if (ret < 0)
14883                                         return -rte_mtr_error_set(error,
14884                                           ENOTSUP,
14885                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
14886                                           NULL, "Fail to save "
14887                                           "rss action into policy struct");
14888                                 act_cnt->fate_action =
14889                                         MLX5_FLOW_FATE_SHARED_RSS;
14890                                 action_flags |= MLX5_FLOW_ACTION_RSS;
14891                                 break;
14892                         }
14893                         case RTE_FLOW_ACTION_TYPE_PORT_ID:
14894                         {
14895                                 struct mlx5_flow_dv_port_id_action_resource
14896                                         port_id_resource;
14897                                 uint32_t port_id = 0;
14898
14899                                 if (i >= MLX5_MTR_RTE_COLORS)
14900                                         return -rte_mtr_error_set(error,
14901                                         ENOTSUP,
14902                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
14903                                         NULL, "cannot create policy "
14904                                         "port action for this color");
14905                                 memset(&port_id_resource, 0,
14906                                         sizeof(port_id_resource));
14907                                 if (flow_dv_translate_action_port_id(dev, act,
14908                                                 &port_id, &flow_err))
14909                                         return -rte_mtr_error_set(error,
14910                                         ENOTSUP,
14911                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
14912                                         NULL, "cannot translate "
14913                                         "policy port action");
14914                                 port_id_resource.port_id = port_id;
14915                                 if (flow_dv_port_id_action_resource_register
14916                                         (dev, &port_id_resource,
14917                                         &dev_flow, &flow_err))
14918                                         return -rte_mtr_error_set(error,
14919                                         ENOTSUP,
14920                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
14921                                         NULL, "cannot setup "
14922                                         "policy port action");
14923                                 act_cnt->rix_port_id_action =
14924                                         dev_flow.handle->rix_port_id_action;
14925                                 act_cnt->fate_action =
14926                                         MLX5_FLOW_FATE_PORT_ID;
14927                                 action_flags |= MLX5_FLOW_ACTION_PORT_ID;
14928                                 break;
14929                         }
14930                         case RTE_FLOW_ACTION_TYPE_JUMP:
14931                         {
14932                                 uint32_t jump_group = 0;
14933                                 uint32_t table = 0;
14934                                 struct mlx5_flow_tbl_data_entry *tbl_data;
14935                                 struct flow_grp_info grp_info = {
14936                                         .external = !!dev_flow.external,
14937                                         .transfer = !!transfer,
14938                                         .fdb_def_rule = !!priv->fdb_def_rule,
14939                                         .std_tbl_fix = 0,
14940                                         .skip_scale = dev_flow.skip_scale &
14941                                         (1 << MLX5_SCALE_FLOW_GROUP_BIT),
14942                                 };
14943                                 struct mlx5_flow_meter_sub_policy *sub_policy =
14944                                 mtr_policy->sub_policys[domain][0];
14945
14946                                 if (i >= MLX5_MTR_RTE_COLORS)
14947                                         return -rte_mtr_error_set(error,
14948                                           ENOTSUP,
14949                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
14950                                           NULL,
14951                                           "cannot create policy "
14952                                           "jump action for this color");
14953                                 jump_group =
14954                                 ((const struct rte_flow_action_jump *)
14955                                                         act->conf)->group;
14956                                 if (mlx5_flow_group_to_table(dev, NULL,
14957                                                        jump_group,
14958                                                        &table,
14959                                                        &grp_info, &flow_err))
14960                                         return -rte_mtr_error_set(error,
14961                                         ENOTSUP,
14962                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
14963                                         NULL, "cannot setup "
14964                                         "policy jump action");
14965                                 sub_policy->jump_tbl[i] =
14966                                 flow_dv_tbl_resource_get(dev,
14967                                         table, egress,
14968                                         transfer,
14969                                         !!dev_flow.external,
14970                                         NULL, jump_group, 0,
14971                                         0, &flow_err);
14972                                 if
14973                                 (!sub_policy->jump_tbl[i])
14974                                         return  -rte_mtr_error_set(error,
14975                                         ENOTSUP,
14976                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
14977                                         NULL, "cannot create jump action.");
14978                                 tbl_data = container_of
14979                                 (sub_policy->jump_tbl[i],
14980                                 struct mlx5_flow_tbl_data_entry, tbl);
14981                                 act_cnt->dr_jump_action[domain] =
14982                                         tbl_data->jump.action;
14983                                 act_cnt->fate_action =
14984                                         MLX5_FLOW_FATE_JUMP;
14985                                 action_flags |= MLX5_FLOW_ACTION_JUMP;
14986                                 break;
14987                         }
14988                         default:
14989                                 return -rte_mtr_error_set(error, ENOTSUP,
14990                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
14991                                           NULL, "action type not supported");
14992                         }
14993                 }
14994         }
14995         return 0;
14996 }
14997
14998 /**
14999  * Create policy action per domain, lock free,
15000  * (mutex should be acquired by caller).
15001  * Dispatcher for action type specific call.
15002  *
15003  * @param[in] dev
15004  *   Pointer to the Ethernet device structure.
15005  * @param[in] mtr_policy
15006  *   Meter policy struct.
15007  * @param[in] action
15008  *   Action specification used to create meter actions.
15009  * @param[out] error
15010  *   Perform verbose error reporting if not NULL. Initialized in case of
15011  *   error only.
15012  *
15013  * @return
15014  *   0 on success, otherwise negative errno value.
15015  */
15016 static int
15017 flow_dv_create_mtr_policy_acts(struct rte_eth_dev *dev,
15018                       struct mlx5_flow_meter_policy *mtr_policy,
15019                       const struct rte_flow_action *actions[RTE_COLORS],
15020                       struct rte_mtr_error *error)
15021 {
15022         int ret, i;
15023         uint16_t sub_policy_num;
15024
15025         for (i = 0; i < MLX5_MTR_DOMAIN_MAX; i++) {
15026                 sub_policy_num = (mtr_policy->sub_policy_num >>
15027                         (MLX5_MTR_SUB_POLICY_NUM_SHIFT * i)) &
15028                         MLX5_MTR_SUB_POLICY_NUM_MASK;
15029                 if (sub_policy_num) {
15030                         ret = __flow_dv_create_domain_policy_acts(dev,
15031                                 mtr_policy, actions,
15032                                 (enum mlx5_meter_domain)i, error);
15033                         if (ret)
15034                                 return ret;
15035                 }
15036         }
15037         return 0;
15038 }
15039
15040 /**
15041  * Query a DV flow rule for its statistics via DevX.
15042  *
15043  * @param[in] dev
15044  *   Pointer to Ethernet device.
15045  * @param[in] cnt_idx
15046  *   Index to the flow counter.
15047  * @param[out] data
15048  *   Data retrieved by the query.
15049  * @param[out] error
15050  *   Perform verbose error reporting if not NULL.
15051  *
15052  * @return
15053  *   0 on success, a negative errno value otherwise and rte_errno is set.
15054  */
15055 static int
15056 flow_dv_query_count(struct rte_eth_dev *dev, uint32_t cnt_idx, void *data,
15057                     struct rte_flow_error *error)
15058 {
15059         struct mlx5_priv *priv = dev->data->dev_private;
15060         struct rte_flow_query_count *qc = data;
15061
15062         if (!priv->config.devx)
15063                 return rte_flow_error_set(error, ENOTSUP,
15064                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
15065                                           NULL,
15066                                           "counters are not supported");
15067         if (cnt_idx) {
15068                 uint64_t pkts, bytes;
15069                 struct mlx5_flow_counter *cnt;
15070                 int err = _flow_dv_query_count(dev, cnt_idx, &pkts, &bytes);
15071
15072                 if (err)
15073                         return rte_flow_error_set(error, -err,
15074                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
15075                                         NULL, "cannot read counters");
15076                 cnt = flow_dv_counter_get_by_idx(dev, cnt_idx, NULL);
15077                 qc->hits_set = 1;
15078                 qc->bytes_set = 1;
15079                 qc->hits = pkts - cnt->hits;
15080                 qc->bytes = bytes - cnt->bytes;
15081                 if (qc->reset) {
15082                         cnt->hits = pkts;
15083                         cnt->bytes = bytes;
15084                 }
15085                 return 0;
15086         }
15087         return rte_flow_error_set(error, EINVAL,
15088                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
15089                                   NULL,
15090                                   "counters are not available");
15091 }
15092
15093 static int
15094 flow_dv_action_query(struct rte_eth_dev *dev,
15095                      const struct rte_flow_action_handle *handle, void *data,
15096                      struct rte_flow_error *error)
15097 {
15098         struct mlx5_age_param *age_param;
15099         struct rte_flow_query_age *resp;
15100         uint32_t act_idx = (uint32_t)(uintptr_t)handle;
15101         uint32_t type = act_idx >> MLX5_INDIRECT_ACTION_TYPE_OFFSET;
15102         uint32_t idx = act_idx & ((1u << MLX5_INDIRECT_ACTION_TYPE_OFFSET) - 1);
15103         struct mlx5_priv *priv = dev->data->dev_private;
15104         struct mlx5_aso_ct_action *ct;
15105         uint16_t owner;
15106         uint32_t dev_idx;
15107
15108         switch (type) {
15109         case MLX5_INDIRECT_ACTION_TYPE_AGE:
15110                 age_param = &flow_aso_age_get_by_idx(dev, idx)->age_params;
15111                 resp = data;
15112                 resp->aged = __atomic_load_n(&age_param->state,
15113                                               __ATOMIC_RELAXED) == AGE_TMOUT ?
15114                                                                           1 : 0;
15115                 resp->sec_since_last_hit_valid = !resp->aged;
15116                 if (resp->sec_since_last_hit_valid)
15117                         resp->sec_since_last_hit = __atomic_load_n
15118                              (&age_param->sec_since_last_hit, __ATOMIC_RELAXED);
15119                 return 0;
15120         case MLX5_INDIRECT_ACTION_TYPE_COUNT:
15121                 return flow_dv_query_count(dev, idx, data, error);
15122         case MLX5_INDIRECT_ACTION_TYPE_CT:
15123                 owner = (uint16_t)MLX5_INDIRECT_ACT_CT_GET_OWNER(idx);
15124                 if (owner != PORT_ID(priv))
15125                         return rte_flow_error_set(error, EACCES,
15126                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
15127                                         NULL,
15128                                         "CT object owned by another port");
15129                 dev_idx = MLX5_INDIRECT_ACT_CT_GET_IDX(idx);
15130                 ct = flow_aso_ct_get_by_dev_idx(dev, dev_idx);
15131                 MLX5_ASSERT(ct);
15132                 if (!ct->refcnt)
15133                         return rte_flow_error_set(error, EFAULT,
15134                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
15135                                         NULL,
15136                                         "CT object is inactive");
15137                 ((struct rte_flow_action_conntrack *)data)->peer_port =
15138                                                         ct->peer;
15139                 ((struct rte_flow_action_conntrack *)data)->is_original_dir =
15140                                                         ct->is_original;
15141                 if (mlx5_aso_ct_query_by_wqe(priv->sh, ct, data))
15142                         return rte_flow_error_set(error, EIO,
15143                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
15144                                         NULL,
15145                                         "Failed to query CT context");
15146                 return 0;
15147         default:
15148                 return rte_flow_error_set(error, ENOTSUP,
15149                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
15150                                           "action type query not supported");
15151         }
15152 }
15153
15154 /**
15155  * Query a flow rule AGE action for aging information.
15156  *
15157  * @param[in] dev
15158  *   Pointer to Ethernet device.
15159  * @param[in] flow
15160  *   Pointer to the sub flow.
15161  * @param[out] data
15162  *   data retrieved by the query.
15163  * @param[out] error
15164  *   Perform verbose error reporting if not NULL.
15165  *
15166  * @return
15167  *   0 on success, a negative errno value otherwise and rte_errno is set.
15168  */
15169 static int
15170 flow_dv_query_age(struct rte_eth_dev *dev, struct rte_flow *flow,
15171                   void *data, struct rte_flow_error *error)
15172 {
15173         struct rte_flow_query_age *resp = data;
15174         struct mlx5_age_param *age_param;
15175
15176         if (flow->age) {
15177                 struct mlx5_aso_age_action *act =
15178                                      flow_aso_age_get_by_idx(dev, flow->age);
15179
15180                 age_param = &act->age_params;
15181         } else if (flow->counter) {
15182                 age_param = flow_dv_counter_idx_get_age(dev, flow->counter);
15183
15184                 if (!age_param || !age_param->timeout)
15185                         return rte_flow_error_set
15186                                         (error, EINVAL,
15187                                          RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
15188                                          NULL, "cannot read age data");
15189         } else {
15190                 return rte_flow_error_set(error, EINVAL,
15191                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
15192                                           NULL, "age data not available");
15193         }
15194         resp->aged = __atomic_load_n(&age_param->state, __ATOMIC_RELAXED) ==
15195                                      AGE_TMOUT ? 1 : 0;
15196         resp->sec_since_last_hit_valid = !resp->aged;
15197         if (resp->sec_since_last_hit_valid)
15198                 resp->sec_since_last_hit = __atomic_load_n
15199                              (&age_param->sec_since_last_hit, __ATOMIC_RELAXED);
15200         return 0;
15201 }
15202
15203 /**
15204  * Query a flow.
15205  *
15206  * @see rte_flow_query()
15207  * @see rte_flow_ops
15208  */
15209 static int
15210 flow_dv_query(struct rte_eth_dev *dev,
15211               struct rte_flow *flow __rte_unused,
15212               const struct rte_flow_action *actions __rte_unused,
15213               void *data __rte_unused,
15214               struct rte_flow_error *error __rte_unused)
15215 {
15216         int ret = -EINVAL;
15217
15218         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
15219                 switch (actions->type) {
15220                 case RTE_FLOW_ACTION_TYPE_VOID:
15221                         break;
15222                 case RTE_FLOW_ACTION_TYPE_COUNT:
15223                         ret = flow_dv_query_count(dev, flow->counter, data,
15224                                                   error);
15225                         break;
15226                 case RTE_FLOW_ACTION_TYPE_AGE:
15227                         ret = flow_dv_query_age(dev, flow, data, error);
15228                         break;
15229                 default:
15230                         return rte_flow_error_set(error, ENOTSUP,
15231                                                   RTE_FLOW_ERROR_TYPE_ACTION,
15232                                                   actions,
15233                                                   "action not supported");
15234                 }
15235         }
15236         return ret;
15237 }
15238
15239 /**
15240  * Destroy the meter table set.
15241  * Lock free, (mutex should be acquired by caller).
15242  *
15243  * @param[in] dev
15244  *   Pointer to Ethernet device.
15245  * @param[in] fm
15246  *   Meter information table.
15247  */
15248 static void
15249 flow_dv_destroy_mtr_tbls(struct rte_eth_dev *dev,
15250                         struct mlx5_flow_meter_info *fm)
15251 {
15252         struct mlx5_priv *priv = dev->data->dev_private;
15253         int i;
15254
15255         if (!fm || !priv->config.dv_flow_en)
15256                 return;
15257         for (i = 0; i < MLX5_MTR_DOMAIN_MAX; i++) {
15258                 if (fm->drop_rule[i]) {
15259                         claim_zero(mlx5_flow_os_destroy_flow(fm->drop_rule[i]));
15260                         fm->drop_rule[i] = NULL;
15261                 }
15262         }
15263 }
15264
15265 static void
15266 flow_dv_destroy_mtr_drop_tbls(struct rte_eth_dev *dev)
15267 {
15268         struct mlx5_priv *priv = dev->data->dev_private;
15269         struct mlx5_flow_mtr_mng *mtrmng = priv->sh->mtrmng;
15270         struct mlx5_flow_tbl_data_entry *tbl;
15271         int i, j;
15272
15273         for (i = 0; i < MLX5_MTR_DOMAIN_MAX; i++) {
15274                 if (mtrmng->def_rule[i]) {
15275                         claim_zero(mlx5_flow_os_destroy_flow
15276                                         (mtrmng->def_rule[i]));
15277                         mtrmng->def_rule[i] = NULL;
15278                 }
15279                 if (mtrmng->def_matcher[i]) {
15280                         tbl = container_of(mtrmng->def_matcher[i]->tbl,
15281                                 struct mlx5_flow_tbl_data_entry, tbl);
15282                         mlx5_cache_unregister(&tbl->matchers,
15283                                       &mtrmng->def_matcher[i]->entry);
15284                         mtrmng->def_matcher[i] = NULL;
15285                 }
15286                 for (j = 0; j < MLX5_REG_BITS; j++) {
15287                         if (mtrmng->drop_matcher[i][j]) {
15288                                 tbl =
15289                                 container_of(mtrmng->drop_matcher[i][j]->tbl,
15290                                              struct mlx5_flow_tbl_data_entry,
15291                                              tbl);
15292                                 mlx5_cache_unregister(&tbl->matchers,
15293                                         &mtrmng->drop_matcher[i][j]->entry);
15294                                 mtrmng->drop_matcher[i][j] = NULL;
15295                         }
15296                 }
15297                 if (mtrmng->drop_tbl[i]) {
15298                         flow_dv_tbl_resource_release(MLX5_SH(dev),
15299                                 mtrmng->drop_tbl[i]);
15300                         mtrmng->drop_tbl[i] = NULL;
15301                 }
15302         }
15303 }
15304
15305 /* Number of meter flow actions, count and jump or count and drop. */
15306 #define METER_ACTIONS 2
15307
15308 static void
15309 __flow_dv_destroy_domain_def_policy(struct rte_eth_dev *dev,
15310                               enum mlx5_meter_domain domain)
15311 {
15312         struct mlx5_priv *priv = dev->data->dev_private;
15313         struct mlx5_flow_meter_def_policy *def_policy =
15314                         priv->sh->mtrmng->def_policy[domain];
15315
15316         __flow_dv_destroy_sub_policy_rules(dev, &def_policy->sub_policy);
15317         mlx5_free(def_policy);
15318         priv->sh->mtrmng->def_policy[domain] = NULL;
15319 }
15320
15321 /**
15322  * Destroy the default policy table set.
15323  *
15324  * @param[in] dev
15325  *   Pointer to Ethernet device.
15326  */
15327 static void
15328 flow_dv_destroy_def_policy(struct rte_eth_dev *dev)
15329 {
15330         struct mlx5_priv *priv = dev->data->dev_private;
15331         int i;
15332
15333         for (i = 0; i < MLX5_MTR_DOMAIN_MAX; i++)
15334                 if (priv->sh->mtrmng->def_policy[i])
15335                         __flow_dv_destroy_domain_def_policy(dev,
15336                                         (enum mlx5_meter_domain)i);
15337         priv->sh->mtrmng->def_policy_id = MLX5_INVALID_POLICY_ID;
15338 }
15339
15340 static int
15341 __flow_dv_create_policy_flow(struct rte_eth_dev *dev,
15342                         uint32_t color_reg_c_idx,
15343                         enum rte_color color, void *matcher_object,
15344                         int actions_n, void *actions,
15345                         bool is_default_policy, void **rule,
15346                         const struct rte_flow_attr *attr)
15347 {
15348         int ret;
15349         struct mlx5_flow_dv_match_params value = {
15350                 .size = sizeof(value.buf) -
15351                         MLX5_ST_SZ_BYTES(fte_match_set_misc4),
15352         };
15353         struct mlx5_flow_dv_match_params matcher = {
15354                 .size = sizeof(matcher.buf) -
15355                         MLX5_ST_SZ_BYTES(fte_match_set_misc4),
15356         };
15357         struct mlx5_priv *priv = dev->data->dev_private;
15358
15359         if (!is_default_policy && (priv->representor || priv->master)) {
15360                 if (flow_dv_translate_item_port_id(dev, matcher.buf,
15361                                                    value.buf, NULL, attr)) {
15362                         DRV_LOG(ERR,
15363                         "Failed to create meter policy flow with port.");
15364                         return -1;
15365                 }
15366         }
15367         flow_dv_match_meta_reg(matcher.buf, value.buf,
15368                                 (enum modify_reg)color_reg_c_idx,
15369                                 rte_col_2_mlx5_col(color),
15370                                 UINT32_MAX);
15371         ret = mlx5_flow_os_create_flow(matcher_object,
15372                         (void *)&value, actions_n, actions, rule);
15373         if (ret) {
15374                 DRV_LOG(ERR, "Failed to create meter policy flow.");
15375                 return -1;
15376         }
15377         return 0;
15378 }
15379
15380 static int
15381 __flow_dv_create_policy_matcher(struct rte_eth_dev *dev,
15382                         uint32_t color_reg_c_idx,
15383                         uint16_t priority,
15384                         struct mlx5_flow_meter_sub_policy *sub_policy,
15385                         const struct rte_flow_attr *attr,
15386                         bool is_default_policy,
15387                         struct rte_flow_error *error)
15388 {
15389         struct mlx5_cache_entry *entry;
15390         struct mlx5_flow_tbl_resource *tbl_rsc = sub_policy->tbl_rsc;
15391         struct mlx5_flow_dv_matcher matcher = {
15392                 .mask = {
15393                         .size = sizeof(matcher.mask.buf) -
15394                                 MLX5_ST_SZ_BYTES(fte_match_set_misc4),
15395                 },
15396                 .tbl = tbl_rsc,
15397         };
15398         struct mlx5_flow_dv_match_params value = {
15399                 .size = sizeof(value.buf) -
15400                         MLX5_ST_SZ_BYTES(fte_match_set_misc4),
15401         };
15402         struct mlx5_flow_cb_ctx ctx = {
15403                 .error = error,
15404                 .data = &matcher,
15405         };
15406         struct mlx5_flow_tbl_data_entry *tbl_data;
15407         struct mlx5_priv *priv = dev->data->dev_private;
15408         uint32_t color_mask = (UINT32_C(1) << MLX5_MTR_COLOR_BITS) - 1;
15409
15410         if (!is_default_policy && (priv->representor || priv->master)) {
15411                 if (flow_dv_translate_item_port_id(dev, matcher.mask.buf,
15412                                                    value.buf, NULL, attr)) {
15413                         DRV_LOG(ERR,
15414                         "Failed to register meter drop matcher with port.");
15415                         return -1;
15416                 }
15417         }
15418         tbl_data = container_of(tbl_rsc, struct mlx5_flow_tbl_data_entry, tbl);
15419         if (priority < RTE_COLOR_RED)
15420                 flow_dv_match_meta_reg(matcher.mask.buf, value.buf,
15421                         (enum modify_reg)color_reg_c_idx, 0, color_mask);
15422         matcher.priority = priority;
15423         matcher.crc = rte_raw_cksum((const void *)matcher.mask.buf,
15424                                         matcher.mask.size);
15425         entry = mlx5_cache_register(&tbl_data->matchers, &ctx);
15426         if (!entry) {
15427                 DRV_LOG(ERR, "Failed to register meter drop matcher.");
15428                 return -1;
15429         }
15430         sub_policy->color_matcher[priority] =
15431                 container_of(entry, struct mlx5_flow_dv_matcher, entry);
15432         return 0;
15433 }
15434
15435 /**
15436  * Create the policy rules per domain.
15437  *
15438  * @param[in] dev
15439  *   Pointer to Ethernet device.
15440  * @param[in] sub_policy
15441  *    Pointer to sub policy table..
15442  * @param[in] egress
15443  *   Direction of the table.
15444  * @param[in] transfer
15445  *   E-Switch or NIC flow.
15446  * @param[in] acts
15447  *   Pointer to policy action list per color.
15448  *
15449  * @return
15450  *   0 on success, -1 otherwise.
15451  */
15452 static int
15453 __flow_dv_create_domain_policy_rules(struct rte_eth_dev *dev,
15454                 struct mlx5_flow_meter_sub_policy *sub_policy,
15455                 uint8_t egress, uint8_t transfer, bool is_default_policy,
15456                 struct mlx5_meter_policy_acts acts[RTE_COLORS])
15457 {
15458         struct rte_flow_error flow_err;
15459         uint32_t color_reg_c_idx;
15460         struct rte_flow_attr attr = {
15461                 .group = MLX5_FLOW_TABLE_LEVEL_POLICY,
15462                 .priority = 0,
15463                 .ingress = 0,
15464                 .egress = !!egress,
15465                 .transfer = !!transfer,
15466                 .reserved = 0,
15467         };
15468         int i;
15469         int ret = mlx5_flow_get_reg_id(dev, MLX5_MTR_COLOR, 0, &flow_err);
15470
15471         if (ret < 0)
15472                 return -1;
15473         /* Create policy table with POLICY level. */
15474         if (!sub_policy->tbl_rsc)
15475                 sub_policy->tbl_rsc = flow_dv_tbl_resource_get(dev,
15476                                 MLX5_FLOW_TABLE_LEVEL_POLICY,
15477                                 egress, transfer, false, NULL, 0, 0,
15478                                 sub_policy->idx, &flow_err);
15479         if (!sub_policy->tbl_rsc) {
15480                 DRV_LOG(ERR,
15481                         "Failed to create meter sub policy table.");
15482                 return -1;
15483         }
15484         /* Prepare matchers. */
15485         color_reg_c_idx = ret;
15486         for (i = 0; i < RTE_COLORS; i++) {
15487                 if (i == RTE_COLOR_YELLOW || !acts[i].actions_n)
15488                         continue;
15489                 attr.priority = i;
15490                 if (!sub_policy->color_matcher[i]) {
15491                         /* Create matchers for Color. */
15492                         if (__flow_dv_create_policy_matcher(dev,
15493                                 color_reg_c_idx, i, sub_policy,
15494                                 &attr, is_default_policy, &flow_err))
15495                                 return -1;
15496                 }
15497                 /* Create flow, matching color. */
15498                 if (acts[i].actions_n)
15499                         if (__flow_dv_create_policy_flow(dev,
15500                                 color_reg_c_idx, (enum rte_color)i,
15501                                 sub_policy->color_matcher[i]->matcher_object,
15502                                 acts[i].actions_n,
15503                                 acts[i].dv_actions,
15504                                 is_default_policy,
15505                                 &sub_policy->color_rule[i],
15506                                 &attr))
15507                                 return -1;
15508         }
15509         return 0;
15510 }
15511
15512 static int
15513 __flow_dv_create_policy_acts_rules(struct rte_eth_dev *dev,
15514                         struct mlx5_flow_meter_policy *mtr_policy,
15515                         struct mlx5_flow_meter_sub_policy *sub_policy,
15516                         uint32_t domain)
15517 {
15518         struct mlx5_priv *priv = dev->data->dev_private;
15519         struct mlx5_meter_policy_acts acts[RTE_COLORS];
15520         struct mlx5_flow_dv_tag_resource *tag;
15521         struct mlx5_flow_dv_port_id_action_resource *port_action;
15522         struct mlx5_hrxq *hrxq;
15523         uint8_t egress, transfer;
15524         int i;
15525
15526         for (i = 0; i < RTE_COLORS; i++) {
15527                 acts[i].actions_n = 0;
15528                 if (i == RTE_COLOR_YELLOW)
15529                         continue;
15530                 if (i == RTE_COLOR_RED) {
15531                         /* Only support drop on red. */
15532                         acts[i].dv_actions[0] =
15533                         mtr_policy->dr_drop_action[domain];
15534                         acts[i].actions_n = 1;
15535                         continue;
15536                 }
15537                 if (mtr_policy->act_cnt[i].rix_mark) {
15538                         tag = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_TAG],
15539                                         mtr_policy->act_cnt[i].rix_mark);
15540                         if (!tag) {
15541                                 DRV_LOG(ERR, "Failed to find "
15542                                 "mark action for policy.");
15543                                 return -1;
15544                         }
15545                         acts[i].dv_actions[acts[i].actions_n] =
15546                                                 tag->action;
15547                         acts[i].actions_n++;
15548                 }
15549                 if (mtr_policy->act_cnt[i].modify_hdr) {
15550                         acts[i].dv_actions[acts[i].actions_n] =
15551                         mtr_policy->act_cnt[i].modify_hdr->action;
15552                         acts[i].actions_n++;
15553                 }
15554                 if (mtr_policy->act_cnt[i].fate_action) {
15555                         switch (mtr_policy->act_cnt[i].fate_action) {
15556                         case MLX5_FLOW_FATE_PORT_ID:
15557                                 port_action = mlx5_ipool_get
15558                                         (priv->sh->ipool[MLX5_IPOOL_PORT_ID],
15559                                 mtr_policy->act_cnt[i].rix_port_id_action);
15560                                 if (!port_action) {
15561                                         DRV_LOG(ERR, "Failed to find "
15562                                                 "port action for policy.");
15563                                         return -1;
15564                                 }
15565                                 acts[i].dv_actions[acts[i].actions_n] =
15566                                 port_action->action;
15567                                 acts[i].actions_n++;
15568                                 break;
15569                         case MLX5_FLOW_FATE_DROP:
15570                         case MLX5_FLOW_FATE_JUMP:
15571                                 acts[i].dv_actions[acts[i].actions_n] =
15572                                 mtr_policy->act_cnt[i].dr_jump_action[domain];
15573                                 acts[i].actions_n++;
15574                                 break;
15575                         case MLX5_FLOW_FATE_SHARED_RSS:
15576                         case MLX5_FLOW_FATE_QUEUE:
15577                                 hrxq = mlx5_ipool_get
15578                                 (priv->sh->ipool[MLX5_IPOOL_HRXQ],
15579                                 sub_policy->rix_hrxq[i]);
15580                                 if (!hrxq) {
15581                                         DRV_LOG(ERR, "Failed to find "
15582                                                 "queue action for policy.");
15583                                         return -1;
15584                                 }
15585                                 acts[i].dv_actions[acts[i].actions_n] =
15586                                 hrxq->action;
15587                                 acts[i].actions_n++;
15588                                 break;
15589                         default:
15590                                 /*Queue action do nothing*/
15591                                 break;
15592                         }
15593                 }
15594         }
15595         egress = (domain == MLX5_MTR_DOMAIN_EGRESS) ? 1 : 0;
15596         transfer = (domain == MLX5_MTR_DOMAIN_TRANSFER) ? 1 : 0;
15597         if (__flow_dv_create_domain_policy_rules(dev, sub_policy,
15598                                 egress, transfer, false, acts)) {
15599                 DRV_LOG(ERR,
15600                 "Failed to create policy rules per domain.");
15601                 return -1;
15602         }
15603         return 0;
15604 }
15605
15606 /**
15607  * Create the policy rules.
15608  *
15609  * @param[in] dev
15610  *   Pointer to Ethernet device.
15611  * @param[in,out] mtr_policy
15612  *   Pointer to meter policy table.
15613  *
15614  * @return
15615  *   0 on success, -1 otherwise.
15616  */
15617 static int
15618 flow_dv_create_policy_rules(struct rte_eth_dev *dev,
15619                              struct mlx5_flow_meter_policy *mtr_policy)
15620 {
15621         int i;
15622         uint16_t sub_policy_num;
15623
15624         for (i = 0; i < MLX5_MTR_DOMAIN_MAX; i++) {
15625                 sub_policy_num = (mtr_policy->sub_policy_num >>
15626                         (MLX5_MTR_SUB_POLICY_NUM_SHIFT * i)) &
15627                         MLX5_MTR_SUB_POLICY_NUM_MASK;
15628                 if (!sub_policy_num)
15629                         continue;
15630                 /* Prepare actions list and create policy rules. */
15631                 if (__flow_dv_create_policy_acts_rules(dev, mtr_policy,
15632                         mtr_policy->sub_policys[i][0], i)) {
15633                         DRV_LOG(ERR,
15634                         "Failed to create policy action list per domain.");
15635                         return -1;
15636                 }
15637         }
15638         return 0;
15639 }
15640
15641 static int
15642 __flow_dv_create_domain_def_policy(struct rte_eth_dev *dev, uint32_t domain)
15643 {
15644         struct mlx5_priv *priv = dev->data->dev_private;
15645         struct mlx5_flow_mtr_mng *mtrmng = priv->sh->mtrmng;
15646         struct mlx5_flow_meter_def_policy *def_policy;
15647         struct mlx5_flow_tbl_resource *jump_tbl;
15648         struct mlx5_flow_tbl_data_entry *tbl_data;
15649         uint8_t egress, transfer;
15650         struct rte_flow_error error;
15651         struct mlx5_meter_policy_acts acts[RTE_COLORS];
15652         int ret;
15653
15654         egress = (domain == MLX5_MTR_DOMAIN_EGRESS) ? 1 : 0;
15655         transfer = (domain == MLX5_MTR_DOMAIN_TRANSFER) ? 1 : 0;
15656         def_policy = mtrmng->def_policy[domain];
15657         if (!def_policy) {
15658                 def_policy = mlx5_malloc(MLX5_MEM_ZERO,
15659                         sizeof(struct mlx5_flow_meter_def_policy),
15660                         RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
15661                 if (!def_policy) {
15662                         DRV_LOG(ERR, "Failed to alloc "
15663                                         "default policy table.");
15664                         goto def_policy_error;
15665                 }
15666                 mtrmng->def_policy[domain] = def_policy;
15667                 /* Create the meter suffix table with SUFFIX level. */
15668                 jump_tbl = flow_dv_tbl_resource_get(dev,
15669                                 MLX5_FLOW_TABLE_LEVEL_METER,
15670                                 egress, transfer, false, NULL, 0,
15671                                 0, MLX5_MTR_TABLE_ID_SUFFIX, &error);
15672                 if (!jump_tbl) {
15673                         DRV_LOG(ERR,
15674                                 "Failed to create meter suffix table.");
15675                         goto def_policy_error;
15676                 }
15677                 def_policy->sub_policy.jump_tbl[RTE_COLOR_GREEN] = jump_tbl;
15678                 tbl_data = container_of(jump_tbl,
15679                                 struct mlx5_flow_tbl_data_entry, tbl);
15680                 def_policy->dr_jump_action[RTE_COLOR_GREEN] =
15681                                                 tbl_data->jump.action;
15682                 acts[RTE_COLOR_GREEN].dv_actions[0] =
15683                                                 tbl_data->jump.action;
15684                 acts[RTE_COLOR_GREEN].actions_n = 1;
15685                 /* Create jump action to the drop table. */
15686                 if (!mtrmng->drop_tbl[domain]) {
15687                         mtrmng->drop_tbl[domain] = flow_dv_tbl_resource_get
15688                                 (dev, MLX5_FLOW_TABLE_LEVEL_METER,
15689                                 egress, transfer, false, NULL, 0,
15690                                 0, MLX5_MTR_TABLE_ID_DROP, &error);
15691                         if (!mtrmng->drop_tbl[domain]) {
15692                                 DRV_LOG(ERR, "Failed to create "
15693                                 "meter drop table for default policy.");
15694                                 goto def_policy_error;
15695                         }
15696                 }
15697                 tbl_data = container_of(mtrmng->drop_tbl[domain],
15698                                 struct mlx5_flow_tbl_data_entry, tbl);
15699                 def_policy->dr_jump_action[RTE_COLOR_RED] =
15700                                                 tbl_data->jump.action;
15701                 acts[RTE_COLOR_RED].dv_actions[0] = tbl_data->jump.action;
15702                 acts[RTE_COLOR_RED].actions_n = 1;
15703                 /* Create default policy rules. */
15704                 ret = __flow_dv_create_domain_policy_rules(dev,
15705                                         &def_policy->sub_policy,
15706                                         egress, transfer, true, acts);
15707                 if (ret) {
15708                         DRV_LOG(ERR, "Failed to create "
15709                                 "default policy rules.");
15710                                 goto def_policy_error;
15711                 }
15712         }
15713         return 0;
15714 def_policy_error:
15715         __flow_dv_destroy_domain_def_policy(dev,
15716                         (enum mlx5_meter_domain)domain);
15717         return -1;
15718 }
15719
15720 /**
15721  * Create the default policy table set.
15722  *
15723  * @param[in] dev
15724  *   Pointer to Ethernet device.
15725  * @return
15726  *   0 on success, -1 otherwise.
15727  */
15728 static int
15729 flow_dv_create_def_policy(struct rte_eth_dev *dev)
15730 {
15731         struct mlx5_priv *priv = dev->data->dev_private;
15732         int i;
15733
15734         /* Non-termination policy table. */
15735         for (i = 0; i < MLX5_MTR_DOMAIN_MAX; i++) {
15736                 if (!priv->config.dv_esw_en && i == MLX5_MTR_DOMAIN_TRANSFER)
15737                         continue;
15738                 if (__flow_dv_create_domain_def_policy(dev, i)) {
15739                         DRV_LOG(ERR,
15740                         "Failed to create default policy");
15741                         return -1;
15742                 }
15743         }
15744         return 0;
15745 }
15746
15747 /**
15748  * Create the needed meter tables.
15749  * Lock free, (mutex should be acquired by caller).
15750  *
15751  * @param[in] dev
15752  *   Pointer to Ethernet device.
15753  * @param[in] fm
15754  *   Meter information table.
15755  * @param[in] mtr_idx
15756  *   Meter index.
15757  * @param[in] domain_bitmap
15758  *   Domain bitmap.
15759  * @return
15760  *   0 on success, -1 otherwise.
15761  */
15762 static int
15763 flow_dv_create_mtr_tbls(struct rte_eth_dev *dev,
15764                         struct mlx5_flow_meter_info *fm,
15765                         uint32_t mtr_idx,
15766                         uint8_t domain_bitmap)
15767 {
15768         struct mlx5_priv *priv = dev->data->dev_private;
15769         struct mlx5_flow_mtr_mng *mtrmng = priv->sh->mtrmng;
15770         struct rte_flow_error error;
15771         struct mlx5_flow_tbl_data_entry *tbl_data;
15772         uint8_t egress, transfer;
15773         void *actions[METER_ACTIONS];
15774         int domain, ret, i;
15775         struct mlx5_flow_counter *cnt;
15776         struct mlx5_flow_dv_match_params value = {
15777                 .size = sizeof(value.buf) -
15778                 MLX5_ST_SZ_BYTES(fte_match_set_misc4),
15779         };
15780         struct mlx5_flow_dv_match_params matcher_para = {
15781                 .size = sizeof(matcher_para.buf) -
15782                 MLX5_ST_SZ_BYTES(fte_match_set_misc4),
15783         };
15784         int mtr_id_reg_c = mlx5_flow_get_reg_id(dev, MLX5_MTR_ID,
15785                                                      0, &error);
15786         uint32_t mtr_id_mask = (UINT32_C(1) << mtrmng->max_mtr_bits) - 1;
15787         uint8_t mtr_id_offset = priv->mtr_reg_share ? MLX5_MTR_COLOR_BITS : 0;
15788         struct mlx5_cache_entry *entry;
15789         struct mlx5_flow_dv_matcher matcher = {
15790                 .mask = {
15791                         .size = sizeof(matcher.mask.buf) -
15792                         MLX5_ST_SZ_BYTES(fte_match_set_misc4),
15793                 },
15794         };
15795         struct mlx5_flow_dv_matcher *drop_matcher;
15796         struct mlx5_flow_cb_ctx ctx = {
15797                 .error = &error,
15798                 .data = &matcher,
15799         };
15800
15801         if (!priv->mtr_en || mtr_id_reg_c < 0) {
15802                 rte_errno = ENOTSUP;
15803                 return -1;
15804         }
15805         for (domain = 0; domain < MLX5_MTR_DOMAIN_MAX; domain++) {
15806                 if (!(domain_bitmap & (1 << domain)) ||
15807                         (mtrmng->def_rule[domain] && !fm->drop_cnt))
15808                         continue;
15809                 egress = (domain == MLX5_MTR_DOMAIN_EGRESS) ? 1 : 0;
15810                 transfer = (domain == MLX5_MTR_DOMAIN_TRANSFER) ? 1 : 0;
15811                 /* Create the drop table with METER DROP level. */
15812                 if (!mtrmng->drop_tbl[domain]) {
15813                         mtrmng->drop_tbl[domain] = flow_dv_tbl_resource_get(dev,
15814                                         MLX5_FLOW_TABLE_LEVEL_METER,
15815                                         egress, transfer, false, NULL, 0,
15816                                         0, MLX5_MTR_TABLE_ID_DROP, &error);
15817                         if (!mtrmng->drop_tbl[domain]) {
15818                                 DRV_LOG(ERR, "Failed to create meter drop table.");
15819                                 goto policy_error;
15820                         }
15821                 }
15822                 /* Create default matcher in drop table. */
15823                 matcher.tbl = mtrmng->drop_tbl[domain],
15824                 tbl_data = container_of(mtrmng->drop_tbl[domain],
15825                                 struct mlx5_flow_tbl_data_entry, tbl);
15826                 if (!mtrmng->def_matcher[domain]) {
15827                         flow_dv_match_meta_reg(matcher.mask.buf, value.buf,
15828                                        (enum modify_reg)mtr_id_reg_c,
15829                                        0, 0);
15830                         matcher.priority = MLX5_MTRS_DEFAULT_RULE_PRIORITY;
15831                         matcher.crc = rte_raw_cksum
15832                                         ((const void *)matcher.mask.buf,
15833                                         matcher.mask.size);
15834                         entry = mlx5_cache_register(&tbl_data->matchers, &ctx);
15835                         if (!entry) {
15836                                 DRV_LOG(ERR, "Failed to register meter "
15837                                 "drop default matcher.");
15838                                 goto policy_error;
15839                         }
15840                         mtrmng->def_matcher[domain] = container_of(entry,
15841                         struct mlx5_flow_dv_matcher, entry);
15842                 }
15843                 /* Create default rule in drop table. */
15844                 if (!mtrmng->def_rule[domain]) {
15845                         i = 0;
15846                         actions[i++] = priv->sh->dr_drop_action;
15847                         flow_dv_match_meta_reg(matcher_para.buf, value.buf,
15848                                 (enum modify_reg)mtr_id_reg_c, 0, 0);
15849                         ret = mlx5_flow_os_create_flow
15850                                 (mtrmng->def_matcher[domain]->matcher_object,
15851                                 (void *)&value, i, actions,
15852                                 &mtrmng->def_rule[domain]);
15853                         if (ret) {
15854                                 DRV_LOG(ERR, "Failed to create meter "
15855                                 "default drop rule for drop table.");
15856                                 goto policy_error;
15857                         }
15858                 }
15859                 if (!fm->drop_cnt)
15860                         continue;
15861                 MLX5_ASSERT(mtrmng->max_mtr_bits);
15862                 if (!mtrmng->drop_matcher[domain][mtrmng->max_mtr_bits - 1]) {
15863                         /* Create matchers for Drop. */
15864                         flow_dv_match_meta_reg(matcher.mask.buf, value.buf,
15865                                         (enum modify_reg)mtr_id_reg_c, 0,
15866                                         (mtr_id_mask << mtr_id_offset));
15867                         matcher.priority = MLX5_REG_BITS - mtrmng->max_mtr_bits;
15868                         matcher.crc = rte_raw_cksum
15869                                         ((const void *)matcher.mask.buf,
15870                                         matcher.mask.size);
15871                         entry = mlx5_cache_register(&tbl_data->matchers, &ctx);
15872                         if (!entry) {
15873                                 DRV_LOG(ERR,
15874                                 "Failed to register meter drop matcher.");
15875                                 goto policy_error;
15876                         }
15877                         mtrmng->drop_matcher[domain][mtrmng->max_mtr_bits - 1] =
15878                                 container_of(entry, struct mlx5_flow_dv_matcher,
15879                                              entry);
15880                 }
15881                 drop_matcher =
15882                         mtrmng->drop_matcher[domain][mtrmng->max_mtr_bits - 1];
15883                 /* Create drop rule, matching meter_id only. */
15884                 flow_dv_match_meta_reg(matcher_para.buf, value.buf,
15885                                 (enum modify_reg)mtr_id_reg_c,
15886                                 (mtr_idx << mtr_id_offset), UINT32_MAX);
15887                 i = 0;
15888                 cnt = flow_dv_counter_get_by_idx(dev,
15889                                         fm->drop_cnt, NULL);
15890                 actions[i++] = cnt->action;
15891                 actions[i++] = priv->sh->dr_drop_action;
15892                 ret = mlx5_flow_os_create_flow(drop_matcher->matcher_object,
15893                                                (void *)&value, i, actions,
15894                                                &fm->drop_rule[domain]);
15895                 if (ret) {
15896                         DRV_LOG(ERR, "Failed to create meter "
15897                                 "drop rule for drop table.");
15898                                 goto policy_error;
15899                 }
15900         }
15901         return 0;
15902 policy_error:
15903         for (i = 0; i < MLX5_MTR_DOMAIN_MAX; i++) {
15904                 if (fm->drop_rule[i]) {
15905                         claim_zero(mlx5_flow_os_destroy_flow
15906                                 (fm->drop_rule[i]));
15907                         fm->drop_rule[i] = NULL;
15908                 }
15909         }
15910         return -1;
15911 }
15912
15913 /**
15914  * Find the policy table for prefix table with RSS.
15915  *
15916  * @param[in] dev
15917  *   Pointer to Ethernet device.
15918  * @param[in] mtr_policy
15919  *   Pointer to meter policy table.
15920  * @param[in] rss_desc
15921  *   Pointer to rss_desc
15922  * @return
15923  *   Pointer to table set on success, NULL otherwise and rte_errno is set.
15924  */
15925 static struct mlx5_flow_meter_sub_policy *
15926 flow_dv_meter_sub_policy_rss_prepare(struct rte_eth_dev *dev,
15927                 struct mlx5_flow_meter_policy *mtr_policy,
15928                 struct mlx5_flow_rss_desc *rss_desc[MLX5_MTR_RTE_COLORS])
15929 {
15930         struct mlx5_priv *priv = dev->data->dev_private;
15931         struct mlx5_flow_meter_sub_policy *sub_policy = NULL;
15932         uint32_t sub_policy_idx = 0;
15933         uint32_t hrxq_idx[MLX5_MTR_RTE_COLORS] = {0};
15934         uint32_t i, j;
15935         struct mlx5_hrxq *hrxq;
15936         struct mlx5_flow_handle dh;
15937         struct mlx5_meter_policy_action_container *act_cnt;
15938         uint32_t domain = MLX5_MTR_DOMAIN_INGRESS;
15939         uint16_t sub_policy_num;
15940
15941         rte_spinlock_lock(&mtr_policy->sl);
15942         for (i = 0; i < MLX5_MTR_RTE_COLORS; i++) {
15943                 if (!rss_desc[i])
15944                         continue;
15945                 hrxq_idx[i] = mlx5_hrxq_get(dev, rss_desc[i]);
15946                 if (!hrxq_idx[i]) {
15947                         rte_spinlock_unlock(&mtr_policy->sl);
15948                         return NULL;
15949                 }
15950         }
15951         sub_policy_num = (mtr_policy->sub_policy_num >>
15952                         (MLX5_MTR_SUB_POLICY_NUM_SHIFT * domain)) &
15953                         MLX5_MTR_SUB_POLICY_NUM_MASK;
15954         for (i = 0; i < sub_policy_num;
15955                 i++) {
15956                 for (j = 0; j < MLX5_MTR_RTE_COLORS; j++) {
15957                         if (rss_desc[j] &&
15958                                 hrxq_idx[j] !=
15959                         mtr_policy->sub_policys[domain][i]->rix_hrxq[j])
15960                                 break;
15961                 }
15962                 if (j >= MLX5_MTR_RTE_COLORS) {
15963                         /*
15964                          * Found the sub policy table with
15965                          * the same queue per color
15966                          */
15967                         rte_spinlock_unlock(&mtr_policy->sl);
15968                         for (j = 0; j < MLX5_MTR_RTE_COLORS; j++)
15969                                 mlx5_hrxq_release(dev, hrxq_idx[j]);
15970                         return mtr_policy->sub_policys[domain][i];
15971                 }
15972         }
15973         /* Create sub policy. */
15974         if (!mtr_policy->sub_policys[domain][0]->rix_hrxq[0]) {
15975                 /* Reuse the first dummy sub_policy*/
15976                 sub_policy = mtr_policy->sub_policys[domain][0];
15977                 sub_policy_idx = sub_policy->idx;
15978         } else {
15979                 sub_policy = mlx5_ipool_zmalloc
15980                                 (priv->sh->ipool[MLX5_IPOOL_MTR_POLICY],
15981                                 &sub_policy_idx);
15982                 if (!sub_policy ||
15983                         sub_policy_idx > MLX5_MAX_SUB_POLICY_TBL_NUM) {
15984                         for (i = 0; i < MLX5_MTR_RTE_COLORS; i++)
15985                                 mlx5_hrxq_release(dev, hrxq_idx[i]);
15986                         goto rss_sub_policy_error;
15987                 }
15988                 sub_policy->idx = sub_policy_idx;
15989                 sub_policy->main_policy = mtr_policy;
15990         }
15991         for (i = 0; i < MLX5_MTR_RTE_COLORS; i++) {
15992                 if (!rss_desc[i])
15993                         continue;
15994                 sub_policy->rix_hrxq[i] = hrxq_idx[i];
15995                 /*
15996                  * Overwrite the last action from
15997                  * RSS action to Queue action.
15998                  */
15999                 hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ],
16000                               hrxq_idx[i]);
16001                 if (!hrxq) {
16002                         DRV_LOG(ERR, "Failed to create policy hrxq");
16003                         goto rss_sub_policy_error;
16004                 }
16005                 act_cnt = &mtr_policy->act_cnt[i];
16006                 if (act_cnt->rix_mark || act_cnt->modify_hdr) {
16007                         memset(&dh, 0, sizeof(struct mlx5_flow_handle));
16008                         if (act_cnt->rix_mark)
16009                                 dh.mark = 1;
16010                         dh.fate_action = MLX5_FLOW_FATE_QUEUE;
16011                         dh.rix_hrxq = hrxq_idx[i];
16012                         flow_drv_rxq_flags_set(dev, &dh);
16013                 }
16014         }
16015         if (__flow_dv_create_policy_acts_rules(dev, mtr_policy,
16016                 sub_policy, domain)) {
16017                 DRV_LOG(ERR, "Failed to create policy "
16018                         "rules per domain.");
16019                 goto rss_sub_policy_error;
16020         }
16021         if (sub_policy != mtr_policy->sub_policys[domain][0]) {
16022                 i = (mtr_policy->sub_policy_num >>
16023                         (MLX5_MTR_SUB_POLICY_NUM_SHIFT * domain)) &
16024                         MLX5_MTR_SUB_POLICY_NUM_MASK;
16025                 mtr_policy->sub_policys[domain][i] = sub_policy;
16026                 i++;
16027                 if (i > MLX5_MTR_RSS_MAX_SUB_POLICY)
16028                         goto rss_sub_policy_error;
16029                 mtr_policy->sub_policy_num &= ~(MLX5_MTR_SUB_POLICY_NUM_MASK <<
16030                         (MLX5_MTR_SUB_POLICY_NUM_SHIFT * domain));
16031                 mtr_policy->sub_policy_num |=
16032                         (i & MLX5_MTR_SUB_POLICY_NUM_MASK) <<
16033                         (MLX5_MTR_SUB_POLICY_NUM_SHIFT * domain);
16034         }
16035         rte_spinlock_unlock(&mtr_policy->sl);
16036         return sub_policy;
16037 rss_sub_policy_error:
16038         if (sub_policy) {
16039                 __flow_dv_destroy_sub_policy_rules(dev, sub_policy);
16040                 if (sub_policy != mtr_policy->sub_policys[domain][0]) {
16041                         i = (mtr_policy->sub_policy_num >>
16042                         (MLX5_MTR_SUB_POLICY_NUM_SHIFT * domain)) &
16043                         MLX5_MTR_SUB_POLICY_NUM_MASK;
16044                         mtr_policy->sub_policys[domain][i] = NULL;
16045                         mlx5_ipool_free
16046                         (priv->sh->ipool[MLX5_IPOOL_MTR_POLICY],
16047                                         sub_policy->idx);
16048                 }
16049         }
16050         if (sub_policy_idx)
16051                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MTR_POLICY],
16052                         sub_policy_idx);
16053         rte_spinlock_unlock(&mtr_policy->sl);
16054         return NULL;
16055 }
16056
16057 /**
16058  * Validate the batch counter support in root table.
16059  *
16060  * Create a simple flow with invalid counter and drop action on root table to
16061  * validate if batch counter with offset on root table is supported or not.
16062  *
16063  * @param[in] dev
16064  *   Pointer to rte_eth_dev structure.
16065  *
16066  * @return
16067  *   0 on success, a negative errno value otherwise and rte_errno is set.
16068  */
16069 int
16070 mlx5_flow_dv_discover_counter_offset_support(struct rte_eth_dev *dev)
16071 {
16072         struct mlx5_priv *priv = dev->data->dev_private;
16073         struct mlx5_dev_ctx_shared *sh = priv->sh;
16074         struct mlx5_flow_dv_match_params mask = {
16075                 .size = sizeof(mask.buf),
16076         };
16077         struct mlx5_flow_dv_match_params value = {
16078                 .size = sizeof(value.buf),
16079         };
16080         struct mlx5dv_flow_matcher_attr dv_attr = {
16081                 .type = IBV_FLOW_ATTR_NORMAL,
16082                 .priority = 0,
16083                 .match_criteria_enable = 0,
16084                 .match_mask = (void *)&mask,
16085         };
16086         void *actions[2] = { 0 };
16087         struct mlx5_flow_tbl_resource *tbl = NULL;
16088         struct mlx5_devx_obj *dcs = NULL;
16089         void *matcher = NULL;
16090         void *flow = NULL;
16091         int ret = -1;
16092
16093         tbl = flow_dv_tbl_resource_get(dev, 0, 0, 0, false, NULL,
16094                                         0, 0, 0, NULL);
16095         if (!tbl)
16096                 goto err;
16097         dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->ctx, 0x4);
16098         if (!dcs)
16099                 goto err;
16100         ret = mlx5_flow_os_create_flow_action_count(dcs->obj, UINT16_MAX,
16101                                                     &actions[0]);
16102         if (ret)
16103                 goto err;
16104         actions[1] = sh->dr_drop_action ? sh->dr_drop_action :
16105                                           priv->drop_queue.hrxq->action;
16106         dv_attr.match_criteria_enable = flow_dv_matcher_enable(mask.buf);
16107         ret = mlx5_flow_os_create_flow_matcher(sh->ctx, &dv_attr, tbl->obj,
16108                                                &matcher);
16109         if (ret)
16110                 goto err;
16111         ret = mlx5_flow_os_create_flow(matcher, (void *)&value, 2,
16112                                        actions, &flow);
16113 err:
16114         /*
16115          * If batch counter with offset is not supported, the driver will not
16116          * validate the invalid offset value, flow create should success.
16117          * In this case, it means batch counter is not supported in root table.
16118          *
16119          * Otherwise, if flow create is failed, counter offset is supported.
16120          */
16121         if (flow) {
16122                 DRV_LOG(INFO, "Batch counter is not supported in root "
16123                               "table. Switch to fallback mode.");
16124                 rte_errno = ENOTSUP;
16125                 ret = -rte_errno;
16126                 claim_zero(mlx5_flow_os_destroy_flow(flow));
16127         } else {
16128                 /* Check matcher to make sure validate fail at flow create. */
16129                 if (!matcher || (matcher && errno != EINVAL))
16130                         DRV_LOG(ERR, "Unexpected error in counter offset "
16131                                      "support detection");
16132                 ret = 0;
16133         }
16134         if (actions[0])
16135                 claim_zero(mlx5_flow_os_destroy_flow_action(actions[0]));
16136         if (matcher)
16137                 claim_zero(mlx5_flow_os_destroy_flow_matcher(matcher));
16138         if (tbl)
16139                 flow_dv_tbl_resource_release(MLX5_SH(dev), tbl);
16140         if (dcs)
16141                 claim_zero(mlx5_devx_cmd_destroy(dcs));
16142         return ret;
16143 }
16144
16145 /**
16146  * Query a devx counter.
16147  *
16148  * @param[in] dev
16149  *   Pointer to the Ethernet device structure.
16150  * @param[in] cnt
16151  *   Index to the flow counter.
16152  * @param[in] clear
16153  *   Set to clear the counter statistics.
16154  * @param[out] pkts
16155  *   The statistics value of packets.
16156  * @param[out] bytes
16157  *   The statistics value of bytes.
16158  *
16159  * @return
16160  *   0 on success, otherwise return -1.
16161  */
16162 static int
16163 flow_dv_counter_query(struct rte_eth_dev *dev, uint32_t counter, bool clear,
16164                       uint64_t *pkts, uint64_t *bytes)
16165 {
16166         struct mlx5_priv *priv = dev->data->dev_private;
16167         struct mlx5_flow_counter *cnt;
16168         uint64_t inn_pkts, inn_bytes;
16169         int ret;
16170
16171         if (!priv->config.devx)
16172                 return -1;
16173
16174         ret = _flow_dv_query_count(dev, counter, &inn_pkts, &inn_bytes);
16175         if (ret)
16176                 return -1;
16177         cnt = flow_dv_counter_get_by_idx(dev, counter, NULL);
16178         *pkts = inn_pkts - cnt->hits;
16179         *bytes = inn_bytes - cnt->bytes;
16180         if (clear) {
16181                 cnt->hits = inn_pkts;
16182                 cnt->bytes = inn_bytes;
16183         }
16184         return 0;
16185 }
16186
16187 /**
16188  * Get aged-out flows.
16189  *
16190  * @param[in] dev
16191  *   Pointer to the Ethernet device structure.
16192  * @param[in] context
16193  *   The address of an array of pointers to the aged-out flows contexts.
16194  * @param[in] nb_contexts
16195  *   The length of context array pointers.
16196  * @param[out] error
16197  *   Perform verbose error reporting if not NULL. Initialized in case of
16198  *   error only.
16199  *
16200  * @return
16201  *   how many contexts get in success, otherwise negative errno value.
16202  *   if nb_contexts is 0, return the amount of all aged contexts.
16203  *   if nb_contexts is not 0 , return the amount of aged flows reported
16204  *   in the context array.
16205  * @note: only stub for now
16206  */
16207 static int
16208 flow_get_aged_flows(struct rte_eth_dev *dev,
16209                     void **context,
16210                     uint32_t nb_contexts,
16211                     struct rte_flow_error *error)
16212 {
16213         struct mlx5_priv *priv = dev->data->dev_private;
16214         struct mlx5_age_info *age_info;
16215         struct mlx5_age_param *age_param;
16216         struct mlx5_flow_counter *counter;
16217         struct mlx5_aso_age_action *act;
16218         int nb_flows = 0;
16219
16220         if (nb_contexts && !context)
16221                 return rte_flow_error_set(error, EINVAL,
16222                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
16223                                           NULL, "empty context");
16224         age_info = GET_PORT_AGE_INFO(priv);
16225         rte_spinlock_lock(&age_info->aged_sl);
16226         LIST_FOREACH(act, &age_info->aged_aso, next) {
16227                 nb_flows++;
16228                 if (nb_contexts) {
16229                         context[nb_flows - 1] =
16230                                                 act->age_params.context;
16231                         if (!(--nb_contexts))
16232                                 break;
16233                 }
16234         }
16235         TAILQ_FOREACH(counter, &age_info->aged_counters, next) {
16236                 nb_flows++;
16237                 if (nb_contexts) {
16238                         age_param = MLX5_CNT_TO_AGE(counter);
16239                         context[nb_flows - 1] = age_param->context;
16240                         if (!(--nb_contexts))
16241                                 break;
16242                 }
16243         }
16244         rte_spinlock_unlock(&age_info->aged_sl);
16245         MLX5_AGE_SET(age_info, MLX5_AGE_TRIGGER);
16246         return nb_flows;
16247 }
16248
16249 /*
16250  * Mutex-protected thunk to lock-free flow_dv_counter_alloc().
16251  */
16252 static uint32_t
16253 flow_dv_counter_allocate(struct rte_eth_dev *dev)
16254 {
16255         return flow_dv_counter_alloc(dev, 0);
16256 }
16257
16258 /**
16259  * Validate indirect action.
16260  * Dispatcher for action type specific validation.
16261  *
16262  * @param[in] dev
16263  *   Pointer to the Ethernet device structure.
16264  * @param[in] conf
16265  *   Indirect action configuration.
16266  * @param[in] action
16267  *   The indirect action object to validate.
16268  * @param[out] error
16269  *   Perform verbose error reporting if not NULL. Initialized in case of
16270  *   error only.
16271  *
16272  * @return
16273  *   0 on success, otherwise negative errno value.
16274  */
16275 static int
16276 flow_dv_action_validate(struct rte_eth_dev *dev,
16277                         const struct rte_flow_indir_action_conf *conf,
16278                         const struct rte_flow_action *action,
16279                         struct rte_flow_error *err)
16280 {
16281         struct mlx5_priv *priv = dev->data->dev_private;
16282
16283         RTE_SET_USED(conf);
16284         switch (action->type) {
16285         case RTE_FLOW_ACTION_TYPE_RSS:
16286                 /*
16287                  * priv->obj_ops is set according to driver capabilities.
16288                  * When DevX capabilities are
16289                  * sufficient, it is set to devx_obj_ops.
16290                  * Otherwise, it is set to ibv_obj_ops.
16291                  * ibv_obj_ops doesn't support ind_table_modify operation.
16292                  * In this case the indirect RSS action can't be used.
16293                  */
16294                 if (priv->obj_ops.ind_table_modify == NULL)
16295                         return rte_flow_error_set
16296                                         (err, ENOTSUP,
16297                                          RTE_FLOW_ERROR_TYPE_ACTION,
16298                                          NULL,
16299                                          "Indirect RSS action not supported");
16300                 return mlx5_validate_action_rss(dev, action, err);
16301         case RTE_FLOW_ACTION_TYPE_AGE:
16302                 if (!priv->sh->aso_age_mng)
16303                         return rte_flow_error_set(err, ENOTSUP,
16304                                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
16305                                                 NULL,
16306                                                 "Indirect age action not supported");
16307                 return flow_dv_validate_action_age(0, action, dev, err);
16308         case RTE_FLOW_ACTION_TYPE_COUNT:
16309                 /*
16310                  * There are two mechanisms to share the action count.
16311                  * The old mechanism uses the shared field to share, while the
16312                  * new mechanism uses the indirect action API.
16313                  * This validation comes to make sure that the two mechanisms
16314                  * are not combined.
16315                  */
16316                 if (is_shared_action_count(action))
16317                         return rte_flow_error_set(err, ENOTSUP,
16318                                                   RTE_FLOW_ERROR_TYPE_ACTION,
16319                                                   NULL,
16320                                                   "Mix shared and indirect counter is not supported");
16321                 return flow_dv_validate_action_count(dev, true, 0, err);
16322         case RTE_FLOW_ACTION_TYPE_CONNTRACK:
16323                 if (!priv->sh->ct_aso_en)
16324                         return rte_flow_error_set(err, ENOTSUP,
16325                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
16326                                         "ASO CT is not supported");
16327                 return mlx5_validate_action_ct(dev, action->conf, err);
16328         default:
16329                 return rte_flow_error_set(err, ENOTSUP,
16330                                           RTE_FLOW_ERROR_TYPE_ACTION,
16331                                           NULL,
16332                                           "action type not supported");
16333         }
16334 }
16335
16336 /**
16337  * Validate meter policy actions.
16338  * Dispatcher for action type specific validation.
16339  *
16340  * @param[in] dev
16341  *   Pointer to the Ethernet device structure.
16342  * @param[in] action
16343  *   The meter policy action object to validate.
16344  * @param[in] attr
16345  *   Attributes of flow to determine steering domain.
16346  * @param[out] error
16347  *   Perform verbose error reporting if not NULL. Initialized in case of
16348  *   error only.
16349  *
16350  * @return
16351  *   0 on success, otherwise negative errno value.
16352  */
16353 static int
16354 flow_dv_validate_mtr_policy_acts(struct rte_eth_dev *dev,
16355                         const struct rte_flow_action *actions[RTE_COLORS],
16356                         struct rte_flow_attr *attr,
16357                         bool *is_rss,
16358                         uint8_t *domain_bitmap,
16359                         bool *is_def_policy,
16360                         struct rte_mtr_error *error)
16361 {
16362         struct mlx5_priv *priv = dev->data->dev_private;
16363         struct mlx5_dev_config *dev_conf = &priv->config;
16364         const struct rte_flow_action *act;
16365         uint64_t action_flags = 0;
16366         int actions_n;
16367         int i, ret;
16368         struct rte_flow_error flow_err;
16369         uint8_t domain_color[RTE_COLORS] = {0};
16370         uint8_t def_domain = MLX5_MTR_ALL_DOMAIN_BIT;
16371
16372         if (!priv->config.dv_esw_en)
16373                 def_domain &= ~MLX5_MTR_DOMAIN_TRANSFER_BIT;
16374         *domain_bitmap = def_domain;
16375         if (actions[RTE_COLOR_YELLOW] &&
16376                 actions[RTE_COLOR_YELLOW]->type != RTE_FLOW_ACTION_TYPE_END)
16377                 return -rte_mtr_error_set(error, ENOTSUP,
16378                                 RTE_MTR_ERROR_TYPE_METER_POLICY,
16379                                 NULL,
16380                                 "Yellow color does not support any action.");
16381         if (actions[RTE_COLOR_YELLOW] &&
16382                 actions[RTE_COLOR_YELLOW]->type != RTE_FLOW_ACTION_TYPE_DROP)
16383                 return -rte_mtr_error_set(error, ENOTSUP,
16384                                 RTE_MTR_ERROR_TYPE_METER_POLICY,
16385                                 NULL, "Red color only supports drop action.");
16386         /*
16387          * Check default policy actions:
16388          * Green/Yellow: no action, Red: drop action
16389          */
16390         if ((!actions[RTE_COLOR_GREEN] ||
16391                 actions[RTE_COLOR_GREEN]->type == RTE_FLOW_ACTION_TYPE_END)) {
16392                 *is_def_policy = true;
16393                 return 0;
16394         }
16395         flow_err.message = NULL;
16396         for (i = 0; i < RTE_COLORS; i++) {
16397                 act = actions[i];
16398                 for (action_flags = 0, actions_n = 0;
16399                         act && act->type != RTE_FLOW_ACTION_TYPE_END;
16400                         act++) {
16401                         if (actions_n == MLX5_DV_MAX_NUMBER_OF_ACTIONS)
16402                                 return -rte_mtr_error_set(error, ENOTSUP,
16403                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
16404                                           NULL, "too many actions");
16405                         switch (act->type) {
16406                         case RTE_FLOW_ACTION_TYPE_PORT_ID:
16407                                 if (!priv->config.dv_esw_en)
16408                                         return -rte_mtr_error_set(error,
16409                                         ENOTSUP,
16410                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
16411                                         NULL, "PORT action validate check"
16412                                         " fail for ESW disable");
16413                                 ret = flow_dv_validate_action_port_id(dev,
16414                                                 action_flags,
16415                                                 act, attr, &flow_err);
16416                                 if (ret)
16417                                         return -rte_mtr_error_set(error,
16418                                         ENOTSUP,
16419                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
16420                                         NULL, flow_err.message ?
16421                                         flow_err.message :
16422                                         "PORT action validate check fail");
16423                                 ++actions_n;
16424                                 action_flags |= MLX5_FLOW_ACTION_PORT_ID;
16425                                 break;
16426                         case RTE_FLOW_ACTION_TYPE_MARK:
16427                                 ret = flow_dv_validate_action_mark(dev, act,
16428                                                            action_flags,
16429                                                            attr, &flow_err);
16430                                 if (ret < 0)
16431                                         return -rte_mtr_error_set(error,
16432                                         ENOTSUP,
16433                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
16434                                         NULL, flow_err.message ?
16435                                         flow_err.message :
16436                                         "Mark action validate check fail");
16437                                 if (dev_conf->dv_xmeta_en !=
16438                                         MLX5_XMETA_MODE_LEGACY)
16439                                         return -rte_mtr_error_set(error,
16440                                         ENOTSUP,
16441                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
16442                                         NULL, "Extend MARK action is "
16443                                         "not supported. Please try use "
16444                                         "default policy for meter.");
16445                                 action_flags |= MLX5_FLOW_ACTION_MARK;
16446                                 ++actions_n;
16447                                 break;
16448                         case RTE_FLOW_ACTION_TYPE_SET_TAG:
16449                                 ret = flow_dv_validate_action_set_tag(dev,
16450                                                         act, action_flags,
16451                                                         attr, &flow_err);
16452                                 if (ret)
16453                                         return -rte_mtr_error_set(error,
16454                                         ENOTSUP,
16455                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
16456                                         NULL, flow_err.message ?
16457                                         flow_err.message :
16458                                         "Set tag action validate check fail");
16459                                 /*
16460                                  * Count all modify-header actions
16461                                  * as one action.
16462                                  */
16463                                 if (!(action_flags &
16464                                         MLX5_FLOW_MODIFY_HDR_ACTIONS))
16465                                         ++actions_n;
16466                                 action_flags |= MLX5_FLOW_ACTION_SET_TAG;
16467                                 break;
16468                         case RTE_FLOW_ACTION_TYPE_DROP:
16469                                 ret = mlx5_flow_validate_action_drop
16470                                         (action_flags,
16471                                         attr, &flow_err);
16472                                 if (ret < 0)
16473                                         return -rte_mtr_error_set(error,
16474                                         ENOTSUP,
16475                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
16476                                         NULL, flow_err.message ?
16477                                         flow_err.message :
16478                                         "Drop action validate check fail");
16479                                 action_flags |= MLX5_FLOW_ACTION_DROP;
16480                                 ++actions_n;
16481                                 break;
16482                         case RTE_FLOW_ACTION_TYPE_QUEUE:
16483                                 /*
16484                                  * Check whether extensive
16485                                  * metadata feature is engaged.
16486                                  */
16487                                 if (dev_conf->dv_flow_en &&
16488                                         (dev_conf->dv_xmeta_en !=
16489                                         MLX5_XMETA_MODE_LEGACY) &&
16490                                         mlx5_flow_ext_mreg_supported(dev))
16491                                         return -rte_mtr_error_set(error,
16492                                           ENOTSUP,
16493                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
16494                                           NULL, "Queue action with meta "
16495                                           "is not supported. Please try use "
16496                                           "default policy for meter.");
16497                                 ret = mlx5_flow_validate_action_queue(act,
16498                                                         action_flags, dev,
16499                                                         attr, &flow_err);
16500                                 if (ret < 0)
16501                                         return -rte_mtr_error_set(error,
16502                                           ENOTSUP,
16503                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
16504                                           NULL, flow_err.message ?
16505                                           flow_err.message :
16506                                           "Queue action validate check fail");
16507                                 action_flags |= MLX5_FLOW_ACTION_QUEUE;
16508                                 ++actions_n;
16509                                 break;
16510                         case RTE_FLOW_ACTION_TYPE_RSS:
16511                                 if (dev_conf->dv_flow_en &&
16512                                         (dev_conf->dv_xmeta_en !=
16513                                         MLX5_XMETA_MODE_LEGACY) &&
16514                                         mlx5_flow_ext_mreg_supported(dev))
16515                                         return -rte_mtr_error_set(error,
16516                                           ENOTSUP,
16517                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
16518                                           NULL, "RSS action with meta "
16519                                           "is not supported. Please try use "
16520                                           "default policy for meter.");
16521                                 ret = mlx5_validate_action_rss(dev, act,
16522                                                 &flow_err);
16523                                 if (ret < 0)
16524                                         return -rte_mtr_error_set(error,
16525                                           ENOTSUP,
16526                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
16527                                           NULL, flow_err.message ?
16528                                           flow_err.message :
16529                                           "RSS action validate check fail");
16530                                 action_flags |= MLX5_FLOW_ACTION_RSS;
16531                                 ++actions_n;
16532                                 *is_rss = true;
16533                                 break;
16534                         case RTE_FLOW_ACTION_TYPE_JUMP:
16535                                 ret = flow_dv_validate_action_jump(dev,
16536                                         NULL, act, action_flags,
16537                                         attr, true, &flow_err);
16538                                 if (ret)
16539                                         return -rte_mtr_error_set(error,
16540                                           ENOTSUP,
16541                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
16542                                           NULL, flow_err.message ?
16543                                           flow_err.message :
16544                                           "Jump action validate check fail");
16545                                 ++actions_n;
16546                                 action_flags |= MLX5_FLOW_ACTION_JUMP;
16547                                 break;
16548                         default:
16549                                 return -rte_mtr_error_set(error, ENOTSUP,
16550                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
16551                                         NULL,
16552                                         "Doesn't support optional action");
16553                         }
16554                 }
16555                 /* Yellow is not supported, just skip. */
16556                 if (i == RTE_COLOR_YELLOW)
16557                         continue;
16558                 if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
16559                         domain_color[i] = MLX5_MTR_DOMAIN_TRANSFER_BIT;
16560                 else if ((action_flags &
16561                         (MLX5_FLOW_ACTION_RSS | MLX5_FLOW_ACTION_QUEUE)) ||
16562                         (action_flags & MLX5_FLOW_ACTION_MARK))
16563                         /*
16564                          * Only support MLX5_XMETA_MODE_LEGACY
16565                          * so MARK action only in ingress domain.
16566                          */
16567                         domain_color[i] = MLX5_MTR_DOMAIN_INGRESS_BIT;
16568                 else
16569                         domain_color[i] = def_domain;
16570                 /*
16571                  * Validate the drop action mutual exclusion
16572                  * with other actions. Drop action is mutually-exclusive
16573                  * with any other action, except for Count action.
16574                  */
16575                 if ((action_flags & MLX5_FLOW_ACTION_DROP) &&
16576                         (action_flags & ~MLX5_FLOW_ACTION_DROP)) {
16577                         return -rte_mtr_error_set(error, ENOTSUP,
16578                                 RTE_MTR_ERROR_TYPE_METER_POLICY,
16579                                 NULL, "Drop action is mutually-exclusive "
16580                                 "with any other action");
16581                 }
16582                 /* Eswitch has few restrictions on using items and actions */
16583                 if (domain_color[i] & MLX5_MTR_DOMAIN_TRANSFER_BIT) {
16584                         if (!mlx5_flow_ext_mreg_supported(dev) &&
16585                                 action_flags & MLX5_FLOW_ACTION_MARK)
16586                                 return -rte_mtr_error_set(error, ENOTSUP,
16587                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
16588                                         NULL, "unsupported action MARK");
16589                         if (action_flags & MLX5_FLOW_ACTION_QUEUE)
16590                                 return -rte_mtr_error_set(error, ENOTSUP,
16591                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
16592                                         NULL, "unsupported action QUEUE");
16593                         if (action_flags & MLX5_FLOW_ACTION_RSS)
16594                                 return -rte_mtr_error_set(error, ENOTSUP,
16595                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
16596                                         NULL, "unsupported action RSS");
16597                         if (!(action_flags & MLX5_FLOW_FATE_ESWITCH_ACTIONS))
16598                                 return -rte_mtr_error_set(error, ENOTSUP,
16599                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
16600                                         NULL, "no fate action is found");
16601                 } else {
16602                         if (!(action_flags & MLX5_FLOW_FATE_ACTIONS) &&
16603                                 (domain_color[i] &
16604                                 MLX5_MTR_DOMAIN_INGRESS_BIT)) {
16605                                 if ((domain_color[i] &
16606                                         MLX5_MTR_DOMAIN_EGRESS_BIT))
16607                                         domain_color[i] =
16608                                         MLX5_MTR_DOMAIN_EGRESS_BIT;
16609                                 else
16610                                         return -rte_mtr_error_set(error,
16611                                         ENOTSUP,
16612                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
16613                                         NULL, "no fate action is found");
16614                         }
16615                 }
16616                 if (domain_color[i] != def_domain)
16617                         *domain_bitmap = domain_color[i];
16618         }
16619         return 0;
16620 }
16621
16622 static int
16623 flow_dv_sync_domain(struct rte_eth_dev *dev, uint32_t domains, uint32_t flags)
16624 {
16625         struct mlx5_priv *priv = dev->data->dev_private;
16626         int ret = 0;
16627
16628         if ((domains & MLX5_DOMAIN_BIT_NIC_RX) && priv->sh->rx_domain != NULL) {
16629                 ret = mlx5_os_flow_dr_sync_domain(priv->sh->rx_domain,
16630                                                 flags);
16631                 if (ret != 0)
16632                         return ret;
16633         }
16634         if ((domains & MLX5_DOMAIN_BIT_NIC_TX) && priv->sh->tx_domain != NULL) {
16635                 ret = mlx5_os_flow_dr_sync_domain(priv->sh->tx_domain, flags);
16636                 if (ret != 0)
16637                         return ret;
16638         }
16639         if ((domains & MLX5_DOMAIN_BIT_FDB) && priv->sh->fdb_domain != NULL) {
16640                 ret = mlx5_os_flow_dr_sync_domain(priv->sh->fdb_domain, flags);
16641                 if (ret != 0)
16642                         return ret;
16643         }
16644         return 0;
16645 }
16646
16647 const struct mlx5_flow_driver_ops mlx5_flow_dv_drv_ops = {
16648         .validate = flow_dv_validate,
16649         .prepare = flow_dv_prepare,
16650         .translate = flow_dv_translate,
16651         .apply = flow_dv_apply,
16652         .remove = flow_dv_remove,
16653         .destroy = flow_dv_destroy,
16654         .query = flow_dv_query,
16655         .create_mtr_tbls = flow_dv_create_mtr_tbls,
16656         .destroy_mtr_tbls = flow_dv_destroy_mtr_tbls,
16657         .destroy_mtr_drop_tbls = flow_dv_destroy_mtr_drop_tbls,
16658         .create_meter = flow_dv_mtr_alloc,
16659         .free_meter = flow_dv_aso_mtr_release_to_pool,
16660         .validate_mtr_acts = flow_dv_validate_mtr_policy_acts,
16661         .create_mtr_acts = flow_dv_create_mtr_policy_acts,
16662         .destroy_mtr_acts = flow_dv_destroy_mtr_policy_acts,
16663         .create_policy_rules = flow_dv_create_policy_rules,
16664         .destroy_policy_rules = flow_dv_destroy_policy_rules,
16665         .create_def_policy = flow_dv_create_def_policy,
16666         .destroy_def_policy = flow_dv_destroy_def_policy,
16667         .meter_sub_policy_rss_prepare = flow_dv_meter_sub_policy_rss_prepare,
16668         .counter_alloc = flow_dv_counter_allocate,
16669         .counter_free = flow_dv_counter_free,
16670         .counter_query = flow_dv_counter_query,
16671         .get_aged_flows = flow_get_aged_flows,
16672         .action_validate = flow_dv_action_validate,
16673         .action_create = flow_dv_action_create,
16674         .action_destroy = flow_dv_action_destroy,
16675         .action_update = flow_dv_action_update,
16676         .action_query = flow_dv_action_query,
16677         .sync_domain = flow_dv_sync_domain,
16678 };
16679
16680 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */
16681