9979d16f74efff6b9fdd5a5ff8a6112481b9ae31
[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_bus_pci.h>
19 #include <rte_ip.h>
20 #include <rte_gre.h>
21 #include <rte_vxlan.h>
22 #include <rte_gtp.h>
23 #include <rte_eal_paging.h>
24 #include <rte_mpls.h>
25 #include <rte_mtr.h>
26 #include <rte_mtr_driver.h>
27 #include <rte_tailq.h>
28
29 #include <mlx5_glue.h>
30 #include <mlx5_devx_cmds.h>
31 #include <mlx5_prm.h>
32 #include <mlx5_malloc.h>
33
34 #include "mlx5_defs.h"
35 #include "mlx5.h"
36 #include "mlx5_common_os.h"
37 #include "mlx5_flow.h"
38 #include "mlx5_flow_os.h"
39 #include "mlx5_rx.h"
40 #include "mlx5_tx.h"
41 #include "rte_pmd_mlx5.h"
42
43 #if defined(HAVE_IBV_FLOW_DV_SUPPORT) || !defined(HAVE_INFINIBAND_VERBS_H)
44
45 #ifndef HAVE_IBV_FLOW_DEVX_COUNTERS
46 #define MLX5DV_FLOW_ACTION_COUNTERS_DEVX 0
47 #endif
48
49 #ifndef HAVE_MLX5DV_DR_ESWITCH
50 #ifndef MLX5DV_FLOW_TABLE_TYPE_FDB
51 #define MLX5DV_FLOW_TABLE_TYPE_FDB 0
52 #endif
53 #endif
54
55 #ifndef HAVE_MLX5DV_DR
56 #define MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL 1
57 #endif
58
59 /* VLAN header definitions */
60 #define MLX5DV_FLOW_VLAN_PCP_SHIFT 13
61 #define MLX5DV_FLOW_VLAN_PCP_MASK (0x7 << MLX5DV_FLOW_VLAN_PCP_SHIFT)
62 #define MLX5DV_FLOW_VLAN_VID_MASK 0x0fff
63 #define MLX5DV_FLOW_VLAN_PCP_MASK_BE RTE_BE16(MLX5DV_FLOW_VLAN_PCP_MASK)
64 #define MLX5DV_FLOW_VLAN_VID_MASK_BE RTE_BE16(MLX5DV_FLOW_VLAN_VID_MASK)
65
66 union flow_dv_attr {
67         struct {
68                 uint32_t valid:1;
69                 uint32_t ipv4:1;
70                 uint32_t ipv6:1;
71                 uint32_t tcp:1;
72                 uint32_t udp:1;
73                 uint32_t reserved:27;
74         };
75         uint32_t attr;
76 };
77
78 static int
79 flow_dv_tbl_resource_release(struct mlx5_dev_ctx_shared *sh,
80                              struct mlx5_flow_tbl_resource *tbl);
81
82 static int
83 flow_dv_encap_decap_resource_release(struct rte_eth_dev *dev,
84                                      uint32_t encap_decap_idx);
85
86 static int
87 flow_dv_port_id_action_resource_release(struct rte_eth_dev *dev,
88                                         uint32_t port_id);
89 static void
90 flow_dv_shared_rss_action_release(struct rte_eth_dev *dev, uint32_t srss);
91
92 static int
93 flow_dv_jump_tbl_resource_release(struct rte_eth_dev *dev,
94                                   uint32_t rix_jump);
95
96 static inline uint16_t
97 mlx5_translate_tunnel_etypes(uint64_t pattern_flags)
98 {
99         if (pattern_flags & MLX5_FLOW_LAYER_INNER_L2)
100                 return RTE_ETHER_TYPE_TEB;
101         else if (pattern_flags & MLX5_FLOW_LAYER_INNER_L3_IPV4)
102                 return RTE_ETHER_TYPE_IPV4;
103         else if (pattern_flags & MLX5_FLOW_LAYER_INNER_L3_IPV6)
104                 return RTE_ETHER_TYPE_IPV6;
105         else if (pattern_flags & MLX5_FLOW_LAYER_MPLS)
106                 return RTE_ETHER_TYPE_MPLS;
107         return 0;
108 }
109
110 static int16_t
111 flow_dv_get_esw_manager_vport_id(struct rte_eth_dev *dev)
112 {
113         struct mlx5_priv *priv = dev->data->dev_private;
114
115         if (priv->pci_dev == NULL)
116                 return 0;
117         switch (priv->pci_dev->id.device_id) {
118         case PCI_DEVICE_ID_MELLANOX_CONNECTX5BF:
119         case PCI_DEVICE_ID_MELLANOX_CONNECTX6DXBF:
120         case PCI_DEVICE_ID_MELLANOX_CONNECTX7BF:
121                 return (int16_t)0xfffe;
122         default:
123                 return 0;
124         }
125 }
126
127 /**
128  * Initialize flow attributes structure according to flow items' types.
129  *
130  * flow_dv_validate() avoids multiple L3/L4 layers cases other than tunnel
131  * mode. For tunnel mode, the items to be modified are the outermost ones.
132  *
133  * @param[in] item
134  *   Pointer to item specification.
135  * @param[out] attr
136  *   Pointer to flow attributes structure.
137  * @param[in] dev_flow
138  *   Pointer to the sub flow.
139  * @param[in] tunnel_decap
140  *   Whether action is after tunnel decapsulation.
141  */
142 static void
143 flow_dv_attr_init(const struct rte_flow_item *item, union flow_dv_attr *attr,
144                   struct mlx5_flow *dev_flow, bool tunnel_decap)
145 {
146         uint64_t layers = dev_flow->handle->layers;
147
148         /*
149          * If layers is already initialized, it means this dev_flow is the
150          * suffix flow, the layers flags is set by the prefix flow. Need to
151          * use the layer flags from prefix flow as the suffix flow may not
152          * have the user defined items as the flow is split.
153          */
154         if (layers) {
155                 if (layers & MLX5_FLOW_LAYER_OUTER_L3_IPV4)
156                         attr->ipv4 = 1;
157                 else if (layers & MLX5_FLOW_LAYER_OUTER_L3_IPV6)
158                         attr->ipv6 = 1;
159                 if (layers & MLX5_FLOW_LAYER_OUTER_L4_TCP)
160                         attr->tcp = 1;
161                 else if (layers & MLX5_FLOW_LAYER_OUTER_L4_UDP)
162                         attr->udp = 1;
163                 attr->valid = 1;
164                 return;
165         }
166         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
167                 uint8_t next_protocol = 0xff;
168                 switch (item->type) {
169                 case RTE_FLOW_ITEM_TYPE_GRE:
170                 case RTE_FLOW_ITEM_TYPE_NVGRE:
171                 case RTE_FLOW_ITEM_TYPE_VXLAN:
172                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
173                 case RTE_FLOW_ITEM_TYPE_GENEVE:
174                 case RTE_FLOW_ITEM_TYPE_MPLS:
175                         if (tunnel_decap)
176                                 attr->attr = 0;
177                         break;
178                 case RTE_FLOW_ITEM_TYPE_IPV4:
179                         if (!attr->ipv6)
180                                 attr->ipv4 = 1;
181                         if (item->mask != NULL &&
182                             ((const struct rte_flow_item_ipv4 *)
183                             item->mask)->hdr.next_proto_id)
184                                 next_protocol =
185                                     ((const struct rte_flow_item_ipv4 *)
186                                       (item->spec))->hdr.next_proto_id &
187                                     ((const struct rte_flow_item_ipv4 *)
188                                       (item->mask))->hdr.next_proto_id;
189                         if ((next_protocol == IPPROTO_IPIP ||
190                             next_protocol == IPPROTO_IPV6) && tunnel_decap)
191                                 attr->attr = 0;
192                         break;
193                 case RTE_FLOW_ITEM_TYPE_IPV6:
194                         if (!attr->ipv4)
195                                 attr->ipv6 = 1;
196                         if (item->mask != NULL &&
197                             ((const struct rte_flow_item_ipv6 *)
198                             item->mask)->hdr.proto)
199                                 next_protocol =
200                                     ((const struct rte_flow_item_ipv6 *)
201                                       (item->spec))->hdr.proto &
202                                     ((const struct rte_flow_item_ipv6 *)
203                                       (item->mask))->hdr.proto;
204                         if ((next_protocol == IPPROTO_IPIP ||
205                             next_protocol == IPPROTO_IPV6) && tunnel_decap)
206                                 attr->attr = 0;
207                         break;
208                 case RTE_FLOW_ITEM_TYPE_UDP:
209                         if (!attr->tcp)
210                                 attr->udp = 1;
211                         break;
212                 case RTE_FLOW_ITEM_TYPE_TCP:
213                         if (!attr->udp)
214                                 attr->tcp = 1;
215                         break;
216                 default:
217                         break;
218                 }
219         }
220         attr->valid = 1;
221 }
222
223 /*
224  * Convert rte_mtr_color to mlx5 color.
225  *
226  * @param[in] rcol
227  *   rte_mtr_color.
228  *
229  * @return
230  *   mlx5 color.
231  */
232 static inline int
233 rte_col_2_mlx5_col(enum rte_color rcol)
234 {
235         switch (rcol) {
236         case RTE_COLOR_GREEN:
237                 return MLX5_FLOW_COLOR_GREEN;
238         case RTE_COLOR_YELLOW:
239                 return MLX5_FLOW_COLOR_YELLOW;
240         case RTE_COLOR_RED:
241                 return MLX5_FLOW_COLOR_RED;
242         default:
243                 break;
244         }
245         return MLX5_FLOW_COLOR_UNDEFINED;
246 }
247
248 struct field_modify_info {
249         uint32_t size; /* Size of field in protocol header, in bytes. */
250         uint32_t offset; /* Offset of field in protocol header, in bytes. */
251         enum mlx5_modification_field id;
252 };
253
254 struct field_modify_info modify_eth[] = {
255         {4,  0, MLX5_MODI_OUT_DMAC_47_16},
256         {2,  4, MLX5_MODI_OUT_DMAC_15_0},
257         {4,  6, MLX5_MODI_OUT_SMAC_47_16},
258         {2, 10, MLX5_MODI_OUT_SMAC_15_0},
259         {0, 0, 0},
260 };
261
262 struct field_modify_info modify_vlan_out_first_vid[] = {
263         /* Size in bits !!! */
264         {12, 0, MLX5_MODI_OUT_FIRST_VID},
265         {0, 0, 0},
266 };
267
268 struct field_modify_info modify_ipv4[] = {
269         {1,  1, MLX5_MODI_OUT_IP_DSCP},
270         {1,  8, MLX5_MODI_OUT_IPV4_TTL},
271         {4, 12, MLX5_MODI_OUT_SIPV4},
272         {4, 16, MLX5_MODI_OUT_DIPV4},
273         {0, 0, 0},
274 };
275
276 struct field_modify_info modify_ipv6[] = {
277         {1,  0, MLX5_MODI_OUT_IP_DSCP},
278         {1,  7, MLX5_MODI_OUT_IPV6_HOPLIMIT},
279         {4,  8, MLX5_MODI_OUT_SIPV6_127_96},
280         {4, 12, MLX5_MODI_OUT_SIPV6_95_64},
281         {4, 16, MLX5_MODI_OUT_SIPV6_63_32},
282         {4, 20, MLX5_MODI_OUT_SIPV6_31_0},
283         {4, 24, MLX5_MODI_OUT_DIPV6_127_96},
284         {4, 28, MLX5_MODI_OUT_DIPV6_95_64},
285         {4, 32, MLX5_MODI_OUT_DIPV6_63_32},
286         {4, 36, MLX5_MODI_OUT_DIPV6_31_0},
287         {0, 0, 0},
288 };
289
290 struct field_modify_info modify_udp[] = {
291         {2, 0, MLX5_MODI_OUT_UDP_SPORT},
292         {2, 2, MLX5_MODI_OUT_UDP_DPORT},
293         {0, 0, 0},
294 };
295
296 struct field_modify_info modify_tcp[] = {
297         {2, 0, MLX5_MODI_OUT_TCP_SPORT},
298         {2, 2, MLX5_MODI_OUT_TCP_DPORT},
299         {4, 4, MLX5_MODI_OUT_TCP_SEQ_NUM},
300         {4, 8, MLX5_MODI_OUT_TCP_ACK_NUM},
301         {0, 0, 0},
302 };
303
304 static void
305 mlx5_flow_tunnel_ip_check(const struct rte_flow_item *item __rte_unused,
306                           uint8_t next_protocol, uint64_t *item_flags,
307                           int *tunnel)
308 {
309         MLX5_ASSERT(item->type == RTE_FLOW_ITEM_TYPE_IPV4 ||
310                     item->type == RTE_FLOW_ITEM_TYPE_IPV6);
311         if (next_protocol == IPPROTO_IPIP) {
312                 *item_flags |= MLX5_FLOW_LAYER_IPIP;
313                 *tunnel = 1;
314         }
315         if (next_protocol == IPPROTO_IPV6) {
316                 *item_flags |= MLX5_FLOW_LAYER_IPV6_ENCAP;
317                 *tunnel = 1;
318         }
319 }
320
321 static inline struct mlx5_hlist *
322 flow_dv_hlist_prepare(struct mlx5_dev_ctx_shared *sh, struct mlx5_hlist **phl,
323                      const char *name, uint32_t size, bool direct_key,
324                      bool lcores_share, void *ctx,
325                      mlx5_list_create_cb cb_create,
326                      mlx5_list_match_cb cb_match,
327                      mlx5_list_remove_cb cb_remove,
328                      mlx5_list_clone_cb cb_clone,
329                      mlx5_list_clone_free_cb cb_clone_free)
330 {
331         struct mlx5_hlist *hl;
332         struct mlx5_hlist *expected = NULL;
333         char s[MLX5_NAME_SIZE];
334
335         hl = __atomic_load_n(phl, __ATOMIC_SEQ_CST);
336         if (likely(hl))
337                 return hl;
338         snprintf(s, sizeof(s), "%s_%s", sh->ibdev_name, name);
339         hl = mlx5_hlist_create(s, size, direct_key, lcores_share,
340                         ctx, cb_create, cb_match, cb_remove, cb_clone,
341                         cb_clone_free);
342         if (!hl) {
343                 DRV_LOG(ERR, "%s hash creation failed", name);
344                 rte_errno = ENOMEM;
345                 return NULL;
346         }
347         if (!__atomic_compare_exchange_n(phl, &expected, hl, false,
348                                          __ATOMIC_SEQ_CST,
349                                          __ATOMIC_SEQ_CST)) {
350                 mlx5_hlist_destroy(hl);
351                 hl = __atomic_load_n(phl, __ATOMIC_SEQ_CST);
352         }
353         return hl;
354 }
355
356 /* Update VLAN's VID/PCP based on input rte_flow_action.
357  *
358  * @param[in] action
359  *   Pointer to struct rte_flow_action.
360  * @param[out] vlan
361  *   Pointer to struct rte_vlan_hdr.
362  */
363 static void
364 mlx5_update_vlan_vid_pcp(const struct rte_flow_action *action,
365                          struct rte_vlan_hdr *vlan)
366 {
367         uint16_t vlan_tci;
368         if (action->type == RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP) {
369                 vlan_tci =
370                     ((const struct rte_flow_action_of_set_vlan_pcp *)
371                                                action->conf)->vlan_pcp;
372                 vlan_tci = vlan_tci << MLX5DV_FLOW_VLAN_PCP_SHIFT;
373                 vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_PCP_MASK;
374                 vlan->vlan_tci |= vlan_tci;
375         } else if (action->type == RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID) {
376                 vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_VID_MASK;
377                 vlan->vlan_tci |= rte_be_to_cpu_16
378                     (((const struct rte_flow_action_of_set_vlan_vid *)
379                                              action->conf)->vlan_vid);
380         }
381 }
382
383 /**
384  * Fetch 1, 2, 3 or 4 byte field from the byte array
385  * and return as unsigned integer in host-endian format.
386  *
387  * @param[in] data
388  *   Pointer to data array.
389  * @param[in] size
390  *   Size of field to extract.
391  *
392  * @return
393  *   converted field in host endian format.
394  */
395 static inline uint32_t
396 flow_dv_fetch_field(const uint8_t *data, uint32_t size)
397 {
398         uint32_t ret;
399
400         switch (size) {
401         case 1:
402                 ret = *data;
403                 break;
404         case 2:
405                 ret = rte_be_to_cpu_16(*(const unaligned_uint16_t *)data);
406                 break;
407         case 3:
408                 ret = rte_be_to_cpu_16(*(const unaligned_uint16_t *)data);
409                 ret = (ret << 8) | *(data + sizeof(uint16_t));
410                 break;
411         case 4:
412                 ret = rte_be_to_cpu_32(*(const unaligned_uint32_t *)data);
413                 break;
414         default:
415                 MLX5_ASSERT(false);
416                 ret = 0;
417                 break;
418         }
419         return ret;
420 }
421
422 /**
423  * Convert modify-header action to DV specification.
424  *
425  * Data length of each action is determined by provided field description
426  * and the item mask. Data bit offset and width of each action is determined
427  * by provided item mask.
428  *
429  * @param[in] item
430  *   Pointer to item specification.
431  * @param[in] field
432  *   Pointer to field modification information.
433  *     For MLX5_MODIFICATION_TYPE_SET specifies destination field.
434  *     For MLX5_MODIFICATION_TYPE_ADD specifies destination field.
435  *     For MLX5_MODIFICATION_TYPE_COPY specifies source field.
436  * @param[in] dcopy
437  *   Destination field info for MLX5_MODIFICATION_TYPE_COPY in @type.
438  *   Negative offset value sets the same offset as source offset.
439  *   size field is ignored, value is taken from source field.
440  * @param[in,out] resource
441  *   Pointer to the modify-header resource.
442  * @param[in] type
443  *   Type of modification.
444  * @param[out] error
445  *   Pointer to the error structure.
446  *
447  * @return
448  *   0 on success, a negative errno value otherwise and rte_errno is set.
449  */
450 static int
451 flow_dv_convert_modify_action(struct rte_flow_item *item,
452                               struct field_modify_info *field,
453                               struct field_modify_info *dcopy,
454                               struct mlx5_flow_dv_modify_hdr_resource *resource,
455                               uint32_t type, struct rte_flow_error *error)
456 {
457         uint32_t i = resource->actions_num;
458         struct mlx5_modification_cmd *actions = resource->actions;
459         uint32_t carry_b = 0;
460
461         /*
462          * The item and mask are provided in big-endian format.
463          * The fields should be presented as in big-endian format either.
464          * Mask must be always present, it defines the actual field width.
465          */
466         MLX5_ASSERT(item->mask);
467         MLX5_ASSERT(field->size);
468         do {
469                 uint32_t size_b;
470                 uint32_t off_b;
471                 uint32_t mask;
472                 uint32_t data;
473                 bool next_field = true;
474                 bool next_dcopy = true;
475
476                 if (i >= MLX5_MAX_MODIFY_NUM)
477                         return rte_flow_error_set(error, EINVAL,
478                                  RTE_FLOW_ERROR_TYPE_ACTION, NULL,
479                                  "too many items to modify");
480                 /* Fetch variable byte size mask from the array. */
481                 mask = flow_dv_fetch_field((const uint8_t *)item->mask +
482                                            field->offset, field->size);
483                 if (!mask) {
484                         ++field;
485                         continue;
486                 }
487                 /* Deduce actual data width in bits from mask value. */
488                 off_b = rte_bsf32(mask) + carry_b;
489                 size_b = sizeof(uint32_t) * CHAR_BIT -
490                          off_b - __builtin_clz(mask);
491                 MLX5_ASSERT(size_b);
492                 actions[i] = (struct mlx5_modification_cmd) {
493                         .action_type = type,
494                         .field = field->id,
495                         .offset = off_b,
496                         .length = (size_b == sizeof(uint32_t) * CHAR_BIT) ?
497                                 0 : size_b,
498                 };
499                 if (type == MLX5_MODIFICATION_TYPE_COPY) {
500                         MLX5_ASSERT(dcopy);
501                         actions[i].dst_field = dcopy->id;
502                         actions[i].dst_offset =
503                                 (int)dcopy->offset < 0 ? off_b : dcopy->offset;
504                         /* Convert entire record to big-endian format. */
505                         actions[i].data1 = rte_cpu_to_be_32(actions[i].data1);
506                         /*
507                          * Destination field overflow. Copy leftovers of
508                          * a source field to the next destination field.
509                          */
510                         carry_b = 0;
511                         if ((size_b > dcopy->size * CHAR_BIT - dcopy->offset) &&
512                             dcopy->size != 0) {
513                                 actions[i].length =
514                                         dcopy->size * CHAR_BIT - dcopy->offset;
515                                 carry_b = actions[i].length;
516                                 next_field = false;
517                         }
518                         /*
519                          * Not enough bits in a source filed to fill a
520                          * destination field. Switch to the next source.
521                          */
522                         if ((size_b < dcopy->size * CHAR_BIT - dcopy->offset) &&
523                             (size_b == field->size * CHAR_BIT - off_b)) {
524                                 actions[i].length =
525                                         field->size * CHAR_BIT - off_b;
526                                 dcopy->offset += actions[i].length;
527                                 next_dcopy = false;
528                         }
529                         if (next_dcopy)
530                                 ++dcopy;
531                 } else {
532                         MLX5_ASSERT(item->spec);
533                         data = flow_dv_fetch_field((const uint8_t *)item->spec +
534                                                    field->offset, field->size);
535                         /* Shift out the trailing masked bits from data. */
536                         data = (data & mask) >> off_b;
537                         actions[i].data1 = rte_cpu_to_be_32(data);
538                 }
539                 /* Convert entire record to expected big-endian format. */
540                 actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
541                 if (next_field)
542                         ++field;
543                 ++i;
544         } while (field->size);
545         if (resource->actions_num == i)
546                 return rte_flow_error_set(error, EINVAL,
547                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
548                                           "invalid modification flow item");
549         resource->actions_num = i;
550         return 0;
551 }
552
553 /**
554  * Convert modify-header set IPv4 address action to DV specification.
555  *
556  * @param[in,out] resource
557  *   Pointer to the modify-header resource.
558  * @param[in] action
559  *   Pointer to action specification.
560  * @param[out] error
561  *   Pointer to the error structure.
562  *
563  * @return
564  *   0 on success, a negative errno value otherwise and rte_errno is set.
565  */
566 static int
567 flow_dv_convert_action_modify_ipv4
568                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
569                          const struct rte_flow_action *action,
570                          struct rte_flow_error *error)
571 {
572         const struct rte_flow_action_set_ipv4 *conf =
573                 (const struct rte_flow_action_set_ipv4 *)(action->conf);
574         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV4 };
575         struct rte_flow_item_ipv4 ipv4;
576         struct rte_flow_item_ipv4 ipv4_mask;
577
578         memset(&ipv4, 0, sizeof(ipv4));
579         memset(&ipv4_mask, 0, sizeof(ipv4_mask));
580         if (action->type == RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC) {
581                 ipv4.hdr.src_addr = conf->ipv4_addr;
582                 ipv4_mask.hdr.src_addr = rte_flow_item_ipv4_mask.hdr.src_addr;
583         } else {
584                 ipv4.hdr.dst_addr = conf->ipv4_addr;
585                 ipv4_mask.hdr.dst_addr = rte_flow_item_ipv4_mask.hdr.dst_addr;
586         }
587         item.spec = &ipv4;
588         item.mask = &ipv4_mask;
589         return flow_dv_convert_modify_action(&item, modify_ipv4, NULL, resource,
590                                              MLX5_MODIFICATION_TYPE_SET, error);
591 }
592
593 /**
594  * Convert modify-header set IPv6 address action to DV specification.
595  *
596  * @param[in,out] resource
597  *   Pointer to the modify-header resource.
598  * @param[in] action
599  *   Pointer to action specification.
600  * @param[out] error
601  *   Pointer to the error structure.
602  *
603  * @return
604  *   0 on success, a negative errno value otherwise and rte_errno is set.
605  */
606 static int
607 flow_dv_convert_action_modify_ipv6
608                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
609                          const struct rte_flow_action *action,
610                          struct rte_flow_error *error)
611 {
612         const struct rte_flow_action_set_ipv6 *conf =
613                 (const struct rte_flow_action_set_ipv6 *)(action->conf);
614         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV6 };
615         struct rte_flow_item_ipv6 ipv6;
616         struct rte_flow_item_ipv6 ipv6_mask;
617
618         memset(&ipv6, 0, sizeof(ipv6));
619         memset(&ipv6_mask, 0, sizeof(ipv6_mask));
620         if (action->type == RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC) {
621                 memcpy(&ipv6.hdr.src_addr, &conf->ipv6_addr,
622                        sizeof(ipv6.hdr.src_addr));
623                 memcpy(&ipv6_mask.hdr.src_addr,
624                        &rte_flow_item_ipv6_mask.hdr.src_addr,
625                        sizeof(ipv6.hdr.src_addr));
626         } else {
627                 memcpy(&ipv6.hdr.dst_addr, &conf->ipv6_addr,
628                        sizeof(ipv6.hdr.dst_addr));
629                 memcpy(&ipv6_mask.hdr.dst_addr,
630                        &rte_flow_item_ipv6_mask.hdr.dst_addr,
631                        sizeof(ipv6.hdr.dst_addr));
632         }
633         item.spec = &ipv6;
634         item.mask = &ipv6_mask;
635         return flow_dv_convert_modify_action(&item, modify_ipv6, NULL, resource,
636                                              MLX5_MODIFICATION_TYPE_SET, error);
637 }
638
639 /**
640  * Convert modify-header set MAC address action to DV specification.
641  *
642  * @param[in,out] resource
643  *   Pointer to the modify-header resource.
644  * @param[in] action
645  *   Pointer to action specification.
646  * @param[out] error
647  *   Pointer to the error structure.
648  *
649  * @return
650  *   0 on success, a negative errno value otherwise and rte_errno is set.
651  */
652 static int
653 flow_dv_convert_action_modify_mac
654                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
655                          const struct rte_flow_action *action,
656                          struct rte_flow_error *error)
657 {
658         const struct rte_flow_action_set_mac *conf =
659                 (const struct rte_flow_action_set_mac *)(action->conf);
660         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_ETH };
661         struct rte_flow_item_eth eth;
662         struct rte_flow_item_eth eth_mask;
663
664         memset(&eth, 0, sizeof(eth));
665         memset(&eth_mask, 0, sizeof(eth_mask));
666         if (action->type == RTE_FLOW_ACTION_TYPE_SET_MAC_SRC) {
667                 memcpy(&eth.src.addr_bytes, &conf->mac_addr,
668                        sizeof(eth.src.addr_bytes));
669                 memcpy(&eth_mask.src.addr_bytes,
670                        &rte_flow_item_eth_mask.src.addr_bytes,
671                        sizeof(eth_mask.src.addr_bytes));
672         } else {
673                 memcpy(&eth.dst.addr_bytes, &conf->mac_addr,
674                        sizeof(eth.dst.addr_bytes));
675                 memcpy(&eth_mask.dst.addr_bytes,
676                        &rte_flow_item_eth_mask.dst.addr_bytes,
677                        sizeof(eth_mask.dst.addr_bytes));
678         }
679         item.spec = &eth;
680         item.mask = &eth_mask;
681         return flow_dv_convert_modify_action(&item, modify_eth, NULL, resource,
682                                              MLX5_MODIFICATION_TYPE_SET, error);
683 }
684
685 /**
686  * Convert modify-header set VLAN VID action to DV specification.
687  *
688  * @param[in,out] resource
689  *   Pointer to the modify-header resource.
690  * @param[in] action
691  *   Pointer to action specification.
692  * @param[out] error
693  *   Pointer to the error structure.
694  *
695  * @return
696  *   0 on success, a negative errno value otherwise and rte_errno is set.
697  */
698 static int
699 flow_dv_convert_action_modify_vlan_vid
700                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
701                          const struct rte_flow_action *action,
702                          struct rte_flow_error *error)
703 {
704         const struct rte_flow_action_of_set_vlan_vid *conf =
705                 (const struct rte_flow_action_of_set_vlan_vid *)(action->conf);
706         int i = resource->actions_num;
707         struct mlx5_modification_cmd *actions = resource->actions;
708         struct field_modify_info *field = modify_vlan_out_first_vid;
709
710         if (i >= MLX5_MAX_MODIFY_NUM)
711                 return rte_flow_error_set(error, EINVAL,
712                          RTE_FLOW_ERROR_TYPE_ACTION, NULL,
713                          "too many items to modify");
714         actions[i] = (struct mlx5_modification_cmd) {
715                 .action_type = MLX5_MODIFICATION_TYPE_SET,
716                 .field = field->id,
717                 .length = field->size,
718                 .offset = field->offset,
719         };
720         actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
721         actions[i].data1 = conf->vlan_vid;
722         actions[i].data1 = actions[i].data1 << 16;
723         resource->actions_num = ++i;
724         return 0;
725 }
726
727 /**
728  * Convert modify-header set TP action to DV specification.
729  *
730  * @param[in,out] resource
731  *   Pointer to the modify-header resource.
732  * @param[in] action
733  *   Pointer to action specification.
734  * @param[in] items
735  *   Pointer to rte_flow_item objects list.
736  * @param[in] attr
737  *   Pointer to flow attributes structure.
738  * @param[in] dev_flow
739  *   Pointer to the sub flow.
740  * @param[in] tunnel_decap
741  *   Whether action is after tunnel decapsulation.
742  * @param[out] error
743  *   Pointer to the error structure.
744  *
745  * @return
746  *   0 on success, a negative errno value otherwise and rte_errno is set.
747  */
748 static int
749 flow_dv_convert_action_modify_tp
750                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
751                          const struct rte_flow_action *action,
752                          const struct rte_flow_item *items,
753                          union flow_dv_attr *attr, struct mlx5_flow *dev_flow,
754                          bool tunnel_decap, struct rte_flow_error *error)
755 {
756         const struct rte_flow_action_set_tp *conf =
757                 (const struct rte_flow_action_set_tp *)(action->conf);
758         struct rte_flow_item item;
759         struct rte_flow_item_udp udp;
760         struct rte_flow_item_udp udp_mask;
761         struct rte_flow_item_tcp tcp;
762         struct rte_flow_item_tcp tcp_mask;
763         struct field_modify_info *field;
764
765         if (!attr->valid)
766                 flow_dv_attr_init(items, attr, dev_flow, tunnel_decap);
767         if (attr->udp) {
768                 memset(&udp, 0, sizeof(udp));
769                 memset(&udp_mask, 0, sizeof(udp_mask));
770                 if (action->type == RTE_FLOW_ACTION_TYPE_SET_TP_SRC) {
771                         udp.hdr.src_port = conf->port;
772                         udp_mask.hdr.src_port =
773                                         rte_flow_item_udp_mask.hdr.src_port;
774                 } else {
775                         udp.hdr.dst_port = conf->port;
776                         udp_mask.hdr.dst_port =
777                                         rte_flow_item_udp_mask.hdr.dst_port;
778                 }
779                 item.type = RTE_FLOW_ITEM_TYPE_UDP;
780                 item.spec = &udp;
781                 item.mask = &udp_mask;
782                 field = modify_udp;
783         } else {
784                 MLX5_ASSERT(attr->tcp);
785                 memset(&tcp, 0, sizeof(tcp));
786                 memset(&tcp_mask, 0, sizeof(tcp_mask));
787                 if (action->type == RTE_FLOW_ACTION_TYPE_SET_TP_SRC) {
788                         tcp.hdr.src_port = conf->port;
789                         tcp_mask.hdr.src_port =
790                                         rte_flow_item_tcp_mask.hdr.src_port;
791                 } else {
792                         tcp.hdr.dst_port = conf->port;
793                         tcp_mask.hdr.dst_port =
794                                         rte_flow_item_tcp_mask.hdr.dst_port;
795                 }
796                 item.type = RTE_FLOW_ITEM_TYPE_TCP;
797                 item.spec = &tcp;
798                 item.mask = &tcp_mask;
799                 field = modify_tcp;
800         }
801         return flow_dv_convert_modify_action(&item, field, NULL, resource,
802                                              MLX5_MODIFICATION_TYPE_SET, error);
803 }
804
805 /**
806  * Convert modify-header set TTL action to DV specification.
807  *
808  * @param[in,out] resource
809  *   Pointer to the modify-header resource.
810  * @param[in] action
811  *   Pointer to action specification.
812  * @param[in] items
813  *   Pointer to rte_flow_item objects list.
814  * @param[in] attr
815  *   Pointer to flow attributes structure.
816  * @param[in] dev_flow
817  *   Pointer to the sub flow.
818  * @param[in] tunnel_decap
819  *   Whether action is after tunnel decapsulation.
820  * @param[out] error
821  *   Pointer to the error structure.
822  *
823  * @return
824  *   0 on success, a negative errno value otherwise and rte_errno is set.
825  */
826 static int
827 flow_dv_convert_action_modify_ttl
828                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
829                          const struct rte_flow_action *action,
830                          const struct rte_flow_item *items,
831                          union flow_dv_attr *attr, struct mlx5_flow *dev_flow,
832                          bool tunnel_decap, struct rte_flow_error *error)
833 {
834         const struct rte_flow_action_set_ttl *conf =
835                 (const struct rte_flow_action_set_ttl *)(action->conf);
836         struct rte_flow_item item;
837         struct rte_flow_item_ipv4 ipv4;
838         struct rte_flow_item_ipv4 ipv4_mask;
839         struct rte_flow_item_ipv6 ipv6;
840         struct rte_flow_item_ipv6 ipv6_mask;
841         struct field_modify_info *field;
842
843         if (!attr->valid)
844                 flow_dv_attr_init(items, attr, dev_flow, tunnel_decap);
845         if (attr->ipv4) {
846                 memset(&ipv4, 0, sizeof(ipv4));
847                 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
848                 ipv4.hdr.time_to_live = conf->ttl_value;
849                 ipv4_mask.hdr.time_to_live = 0xFF;
850                 item.type = RTE_FLOW_ITEM_TYPE_IPV4;
851                 item.spec = &ipv4;
852                 item.mask = &ipv4_mask;
853                 field = modify_ipv4;
854         } else {
855                 MLX5_ASSERT(attr->ipv6);
856                 memset(&ipv6, 0, sizeof(ipv6));
857                 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
858                 ipv6.hdr.hop_limits = conf->ttl_value;
859                 ipv6_mask.hdr.hop_limits = 0xFF;
860                 item.type = RTE_FLOW_ITEM_TYPE_IPV6;
861                 item.spec = &ipv6;
862                 item.mask = &ipv6_mask;
863                 field = modify_ipv6;
864         }
865         return flow_dv_convert_modify_action(&item, field, NULL, resource,
866                                              MLX5_MODIFICATION_TYPE_SET, error);
867 }
868
869 /**
870  * Convert modify-header decrement TTL action to DV specification.
871  *
872  * @param[in,out] resource
873  *   Pointer to the modify-header resource.
874  * @param[in] action
875  *   Pointer to action specification.
876  * @param[in] items
877  *   Pointer to rte_flow_item objects list.
878  * @param[in] attr
879  *   Pointer to flow attributes structure.
880  * @param[in] dev_flow
881  *   Pointer to the sub flow.
882  * @param[in] tunnel_decap
883  *   Whether action is after tunnel decapsulation.
884  * @param[out] error
885  *   Pointer to the error structure.
886  *
887  * @return
888  *   0 on success, a negative errno value otherwise and rte_errno is set.
889  */
890 static int
891 flow_dv_convert_action_modify_dec_ttl
892                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
893                          const struct rte_flow_item *items,
894                          union flow_dv_attr *attr, struct mlx5_flow *dev_flow,
895                          bool tunnel_decap, struct rte_flow_error *error)
896 {
897         struct rte_flow_item item;
898         struct rte_flow_item_ipv4 ipv4;
899         struct rte_flow_item_ipv4 ipv4_mask;
900         struct rte_flow_item_ipv6 ipv6;
901         struct rte_flow_item_ipv6 ipv6_mask;
902         struct field_modify_info *field;
903
904         if (!attr->valid)
905                 flow_dv_attr_init(items, attr, dev_flow, tunnel_decap);
906         if (attr->ipv4) {
907                 memset(&ipv4, 0, sizeof(ipv4));
908                 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
909                 ipv4.hdr.time_to_live = 0xFF;
910                 ipv4_mask.hdr.time_to_live = 0xFF;
911                 item.type = RTE_FLOW_ITEM_TYPE_IPV4;
912                 item.spec = &ipv4;
913                 item.mask = &ipv4_mask;
914                 field = modify_ipv4;
915         } else {
916                 MLX5_ASSERT(attr->ipv6);
917                 memset(&ipv6, 0, sizeof(ipv6));
918                 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
919                 ipv6.hdr.hop_limits = 0xFF;
920                 ipv6_mask.hdr.hop_limits = 0xFF;
921                 item.type = RTE_FLOW_ITEM_TYPE_IPV6;
922                 item.spec = &ipv6;
923                 item.mask = &ipv6_mask;
924                 field = modify_ipv6;
925         }
926         return flow_dv_convert_modify_action(&item, field, NULL, resource,
927                                              MLX5_MODIFICATION_TYPE_ADD, error);
928 }
929
930 /**
931  * Convert modify-header increment/decrement TCP Sequence number
932  * to DV specification.
933  *
934  * @param[in,out] resource
935  *   Pointer to the modify-header resource.
936  * @param[in] action
937  *   Pointer to action specification.
938  * @param[out] error
939  *   Pointer to the error structure.
940  *
941  * @return
942  *   0 on success, a negative errno value otherwise and rte_errno is set.
943  */
944 static int
945 flow_dv_convert_action_modify_tcp_seq
946                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
947                          const struct rte_flow_action *action,
948                          struct rte_flow_error *error)
949 {
950         const rte_be32_t *conf = (const rte_be32_t *)(action->conf);
951         uint64_t value = rte_be_to_cpu_32(*conf);
952         struct rte_flow_item item;
953         struct rte_flow_item_tcp tcp;
954         struct rte_flow_item_tcp tcp_mask;
955
956         memset(&tcp, 0, sizeof(tcp));
957         memset(&tcp_mask, 0, sizeof(tcp_mask));
958         if (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ)
959                 /*
960                  * The HW has no decrement operation, only increment operation.
961                  * To simulate decrement X from Y using increment operation
962                  * we need to add UINT32_MAX X times to Y.
963                  * Each adding of UINT32_MAX decrements Y by 1.
964                  */
965                 value *= UINT32_MAX;
966         tcp.hdr.sent_seq = rte_cpu_to_be_32((uint32_t)value);
967         tcp_mask.hdr.sent_seq = RTE_BE32(UINT32_MAX);
968         item.type = RTE_FLOW_ITEM_TYPE_TCP;
969         item.spec = &tcp;
970         item.mask = &tcp_mask;
971         return flow_dv_convert_modify_action(&item, modify_tcp, NULL, resource,
972                                              MLX5_MODIFICATION_TYPE_ADD, error);
973 }
974
975 /**
976  * Convert modify-header increment/decrement TCP Acknowledgment number
977  * to DV specification.
978  *
979  * @param[in,out] resource
980  *   Pointer to the modify-header resource.
981  * @param[in] action
982  *   Pointer to action specification.
983  * @param[out] error
984  *   Pointer to the error structure.
985  *
986  * @return
987  *   0 on success, a negative errno value otherwise and rte_errno is set.
988  */
989 static int
990 flow_dv_convert_action_modify_tcp_ack
991                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
992                          const struct rte_flow_action *action,
993                          struct rte_flow_error *error)
994 {
995         const rte_be32_t *conf = (const rte_be32_t *)(action->conf);
996         uint64_t value = rte_be_to_cpu_32(*conf);
997         struct rte_flow_item item;
998         struct rte_flow_item_tcp tcp;
999         struct rte_flow_item_tcp tcp_mask;
1000
1001         memset(&tcp, 0, sizeof(tcp));
1002         memset(&tcp_mask, 0, sizeof(tcp_mask));
1003         if (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK)
1004                 /*
1005                  * The HW has no decrement operation, only increment operation.
1006                  * To simulate decrement X from Y using increment operation
1007                  * we need to add UINT32_MAX X times to Y.
1008                  * Each adding of UINT32_MAX decrements Y by 1.
1009                  */
1010                 value *= UINT32_MAX;
1011         tcp.hdr.recv_ack = rte_cpu_to_be_32((uint32_t)value);
1012         tcp_mask.hdr.recv_ack = RTE_BE32(UINT32_MAX);
1013         item.type = RTE_FLOW_ITEM_TYPE_TCP;
1014         item.spec = &tcp;
1015         item.mask = &tcp_mask;
1016         return flow_dv_convert_modify_action(&item, modify_tcp, NULL, resource,
1017                                              MLX5_MODIFICATION_TYPE_ADD, error);
1018 }
1019
1020 static enum mlx5_modification_field reg_to_field[] = {
1021         [REG_NON] = MLX5_MODI_OUT_NONE,
1022         [REG_A] = MLX5_MODI_META_DATA_REG_A,
1023         [REG_B] = MLX5_MODI_META_DATA_REG_B,
1024         [REG_C_0] = MLX5_MODI_META_REG_C_0,
1025         [REG_C_1] = MLX5_MODI_META_REG_C_1,
1026         [REG_C_2] = MLX5_MODI_META_REG_C_2,
1027         [REG_C_3] = MLX5_MODI_META_REG_C_3,
1028         [REG_C_4] = MLX5_MODI_META_REG_C_4,
1029         [REG_C_5] = MLX5_MODI_META_REG_C_5,
1030         [REG_C_6] = MLX5_MODI_META_REG_C_6,
1031         [REG_C_7] = MLX5_MODI_META_REG_C_7,
1032 };
1033
1034 /**
1035  * Convert register set to DV specification.
1036  *
1037  * @param[in,out] resource
1038  *   Pointer to the modify-header resource.
1039  * @param[in] action
1040  *   Pointer to action specification.
1041  * @param[out] error
1042  *   Pointer to the error structure.
1043  *
1044  * @return
1045  *   0 on success, a negative errno value otherwise and rte_errno is set.
1046  */
1047 static int
1048 flow_dv_convert_action_set_reg
1049                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
1050                          const struct rte_flow_action *action,
1051                          struct rte_flow_error *error)
1052 {
1053         const struct mlx5_rte_flow_action_set_tag *conf = action->conf;
1054         struct mlx5_modification_cmd *actions = resource->actions;
1055         uint32_t i = resource->actions_num;
1056
1057         if (i >= MLX5_MAX_MODIFY_NUM)
1058                 return rte_flow_error_set(error, EINVAL,
1059                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1060                                           "too many items to modify");
1061         MLX5_ASSERT(conf->id != REG_NON);
1062         MLX5_ASSERT(conf->id < (enum modify_reg)RTE_DIM(reg_to_field));
1063         actions[i] = (struct mlx5_modification_cmd) {
1064                 .action_type = MLX5_MODIFICATION_TYPE_SET,
1065                 .field = reg_to_field[conf->id],
1066                 .offset = conf->offset,
1067                 .length = conf->length,
1068         };
1069         actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
1070         actions[i].data1 = rte_cpu_to_be_32(conf->data);
1071         ++i;
1072         resource->actions_num = i;
1073         return 0;
1074 }
1075
1076 /**
1077  * Convert SET_TAG action to DV specification.
1078  *
1079  * @param[in] dev
1080  *   Pointer to the rte_eth_dev structure.
1081  * @param[in,out] resource
1082  *   Pointer to the modify-header resource.
1083  * @param[in] conf
1084  *   Pointer to action specification.
1085  * @param[out] error
1086  *   Pointer to the error structure.
1087  *
1088  * @return
1089  *   0 on success, a negative errno value otherwise and rte_errno is set.
1090  */
1091 static int
1092 flow_dv_convert_action_set_tag
1093                         (struct rte_eth_dev *dev,
1094                          struct mlx5_flow_dv_modify_hdr_resource *resource,
1095                          const struct rte_flow_action_set_tag *conf,
1096                          struct rte_flow_error *error)
1097 {
1098         rte_be32_t data = rte_cpu_to_be_32(conf->data);
1099         rte_be32_t mask = rte_cpu_to_be_32(conf->mask);
1100         struct rte_flow_item item = {
1101                 .spec = &data,
1102                 .mask = &mask,
1103         };
1104         struct field_modify_info reg_c_x[] = {
1105                 [1] = {0, 0, 0},
1106         };
1107         enum mlx5_modification_field reg_type;
1108         int ret;
1109
1110         ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, conf->index, error);
1111         if (ret < 0)
1112                 return ret;
1113         MLX5_ASSERT(ret != REG_NON);
1114         MLX5_ASSERT((unsigned int)ret < RTE_DIM(reg_to_field));
1115         reg_type = reg_to_field[ret];
1116         MLX5_ASSERT(reg_type > 0);
1117         reg_c_x[0] = (struct field_modify_info){4, 0, reg_type};
1118         return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
1119                                              MLX5_MODIFICATION_TYPE_SET, error);
1120 }
1121
1122 /**
1123  * Convert internal COPY_REG action to DV specification.
1124  *
1125  * @param[in] dev
1126  *   Pointer to the rte_eth_dev structure.
1127  * @param[in,out] res
1128  *   Pointer to the modify-header resource.
1129  * @param[in] action
1130  *   Pointer to action specification.
1131  * @param[out] error
1132  *   Pointer to the error structure.
1133  *
1134  * @return
1135  *   0 on success, a negative errno value otherwise and rte_errno is set.
1136  */
1137 static int
1138 flow_dv_convert_action_copy_mreg(struct rte_eth_dev *dev,
1139                                  struct mlx5_flow_dv_modify_hdr_resource *res,
1140                                  const struct rte_flow_action *action,
1141                                  struct rte_flow_error *error)
1142 {
1143         const struct mlx5_flow_action_copy_mreg *conf = action->conf;
1144         rte_be32_t mask = RTE_BE32(UINT32_MAX);
1145         struct rte_flow_item item = {
1146                 .spec = NULL,
1147                 .mask = &mask,
1148         };
1149         struct field_modify_info reg_src[] = {
1150                 {4, 0, reg_to_field[conf->src]},
1151                 {0, 0, 0},
1152         };
1153         struct field_modify_info reg_dst = {
1154                 .offset = 0,
1155                 .id = reg_to_field[conf->dst],
1156         };
1157         /* Adjust reg_c[0] usage according to reported mask. */
1158         if (conf->dst == REG_C_0 || conf->src == REG_C_0) {
1159                 struct mlx5_priv *priv = dev->data->dev_private;
1160                 uint32_t reg_c0 = priv->sh->dv_regc0_mask;
1161
1162                 MLX5_ASSERT(reg_c0);
1163                 MLX5_ASSERT(priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY);
1164                 if (conf->dst == REG_C_0) {
1165                         /* Copy to reg_c[0], within mask only. */
1166                         reg_dst.offset = rte_bsf32(reg_c0);
1167                         mask = rte_cpu_to_be_32(reg_c0 >> reg_dst.offset);
1168                 } else {
1169                         reg_dst.offset = 0;
1170                         mask = rte_cpu_to_be_32(reg_c0);
1171                 }
1172         }
1173         return flow_dv_convert_modify_action(&item,
1174                                              reg_src, &reg_dst, res,
1175                                              MLX5_MODIFICATION_TYPE_COPY,
1176                                              error);
1177 }
1178
1179 /**
1180  * Convert MARK action to DV specification. This routine is used
1181  * in extensive metadata only and requires metadata register to be
1182  * handled. In legacy mode hardware tag resource is engaged.
1183  *
1184  * @param[in] dev
1185  *   Pointer to the rte_eth_dev structure.
1186  * @param[in] conf
1187  *   Pointer to MARK action specification.
1188  * @param[in,out] resource
1189  *   Pointer to the modify-header resource.
1190  * @param[out] error
1191  *   Pointer to the error structure.
1192  *
1193  * @return
1194  *   0 on success, a negative errno value otherwise and rte_errno is set.
1195  */
1196 static int
1197 flow_dv_convert_action_mark(struct rte_eth_dev *dev,
1198                             const struct rte_flow_action_mark *conf,
1199                             struct mlx5_flow_dv_modify_hdr_resource *resource,
1200                             struct rte_flow_error *error)
1201 {
1202         struct mlx5_priv *priv = dev->data->dev_private;
1203         rte_be32_t mask = rte_cpu_to_be_32(MLX5_FLOW_MARK_MASK &
1204                                            priv->sh->dv_mark_mask);
1205         rte_be32_t data = rte_cpu_to_be_32(conf->id) & mask;
1206         struct rte_flow_item item = {
1207                 .spec = &data,
1208                 .mask = &mask,
1209         };
1210         struct field_modify_info reg_c_x[] = {
1211                 [1] = {0, 0, 0},
1212         };
1213         int reg;
1214
1215         if (!mask)
1216                 return rte_flow_error_set(error, EINVAL,
1217                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1218                                           NULL, "zero mark action mask");
1219         reg = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
1220         if (reg < 0)
1221                 return reg;
1222         MLX5_ASSERT(reg > 0);
1223         if (reg == REG_C_0) {
1224                 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
1225                 uint32_t shl_c0 = rte_bsf32(msk_c0);
1226
1227                 data = rte_cpu_to_be_32(rte_cpu_to_be_32(data) << shl_c0);
1228                 mask = rte_cpu_to_be_32(mask) & msk_c0;
1229                 mask = rte_cpu_to_be_32(mask << shl_c0);
1230         }
1231         reg_c_x[0] = (struct field_modify_info){4, 0, reg_to_field[reg]};
1232         return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
1233                                              MLX5_MODIFICATION_TYPE_SET, error);
1234 }
1235
1236 /**
1237  * Get metadata register index for specified steering domain.
1238  *
1239  * @param[in] dev
1240  *   Pointer to the rte_eth_dev structure.
1241  * @param[in] attr
1242  *   Attributes of flow to determine steering domain.
1243  * @param[out] error
1244  *   Pointer to the error structure.
1245  *
1246  * @return
1247  *   positive index on success, a negative errno value otherwise
1248  *   and rte_errno is set.
1249  */
1250 static enum modify_reg
1251 flow_dv_get_metadata_reg(struct rte_eth_dev *dev,
1252                          const struct rte_flow_attr *attr,
1253                          struct rte_flow_error *error)
1254 {
1255         int reg =
1256                 mlx5_flow_get_reg_id(dev, attr->transfer ?
1257                                           MLX5_METADATA_FDB :
1258                                             attr->egress ?
1259                                             MLX5_METADATA_TX :
1260                                             MLX5_METADATA_RX, 0, error);
1261         if (reg < 0)
1262                 return rte_flow_error_set(error,
1263                                           ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
1264                                           NULL, "unavailable "
1265                                           "metadata register");
1266         return reg;
1267 }
1268
1269 /**
1270  * Convert SET_META action to DV specification.
1271  *
1272  * @param[in] dev
1273  *   Pointer to the rte_eth_dev structure.
1274  * @param[in,out] resource
1275  *   Pointer to the modify-header resource.
1276  * @param[in] attr
1277  *   Attributes of flow that includes this item.
1278  * @param[in] conf
1279  *   Pointer to action specification.
1280  * @param[out] error
1281  *   Pointer to the error structure.
1282  *
1283  * @return
1284  *   0 on success, a negative errno value otherwise and rte_errno is set.
1285  */
1286 static int
1287 flow_dv_convert_action_set_meta
1288                         (struct rte_eth_dev *dev,
1289                          struct mlx5_flow_dv_modify_hdr_resource *resource,
1290                          const struct rte_flow_attr *attr,
1291                          const struct rte_flow_action_set_meta *conf,
1292                          struct rte_flow_error *error)
1293 {
1294         uint32_t mask = rte_cpu_to_be_32(conf->mask);
1295         uint32_t data = rte_cpu_to_be_32(conf->data) & mask;
1296         struct rte_flow_item item = {
1297                 .spec = &data,
1298                 .mask = &mask,
1299         };
1300         struct field_modify_info reg_c_x[] = {
1301                 [1] = {0, 0, 0},
1302         };
1303         int reg = flow_dv_get_metadata_reg(dev, attr, error);
1304
1305         if (reg < 0)
1306                 return reg;
1307         MLX5_ASSERT(reg != REG_NON);
1308         if (reg == REG_C_0) {
1309                 struct mlx5_priv *priv = dev->data->dev_private;
1310                 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
1311                 uint32_t shl_c0 = rte_bsf32(msk_c0);
1312
1313                 data = rte_cpu_to_be_32(rte_cpu_to_be_32(data) << shl_c0);
1314                 mask = rte_cpu_to_be_32(mask) & msk_c0;
1315                 mask = rte_cpu_to_be_32(mask << shl_c0);
1316         }
1317         reg_c_x[0] = (struct field_modify_info){4, 0, reg_to_field[reg]};
1318         /* The routine expects parameters in memory as big-endian ones. */
1319         return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
1320                                              MLX5_MODIFICATION_TYPE_SET, error);
1321 }
1322
1323 /**
1324  * Convert modify-header set IPv4 DSCP action to DV specification.
1325  *
1326  * @param[in,out] resource
1327  *   Pointer to the modify-header resource.
1328  * @param[in] action
1329  *   Pointer to action specification.
1330  * @param[out] error
1331  *   Pointer to the error structure.
1332  *
1333  * @return
1334  *   0 on success, a negative errno value otherwise and rte_errno is set.
1335  */
1336 static int
1337 flow_dv_convert_action_modify_ipv4_dscp
1338                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
1339                          const struct rte_flow_action *action,
1340                          struct rte_flow_error *error)
1341 {
1342         const struct rte_flow_action_set_dscp *conf =
1343                 (const struct rte_flow_action_set_dscp *)(action->conf);
1344         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV4 };
1345         struct rte_flow_item_ipv4 ipv4;
1346         struct rte_flow_item_ipv4 ipv4_mask;
1347
1348         memset(&ipv4, 0, sizeof(ipv4));
1349         memset(&ipv4_mask, 0, sizeof(ipv4_mask));
1350         ipv4.hdr.type_of_service = conf->dscp;
1351         ipv4_mask.hdr.type_of_service = RTE_IPV4_HDR_DSCP_MASK >> 2;
1352         item.spec = &ipv4;
1353         item.mask = &ipv4_mask;
1354         return flow_dv_convert_modify_action(&item, modify_ipv4, NULL, resource,
1355                                              MLX5_MODIFICATION_TYPE_SET, error);
1356 }
1357
1358 /**
1359  * Convert modify-header set IPv6 DSCP action to DV specification.
1360  *
1361  * @param[in,out] resource
1362  *   Pointer to the modify-header resource.
1363  * @param[in] action
1364  *   Pointer to action specification.
1365  * @param[out] error
1366  *   Pointer to the error structure.
1367  *
1368  * @return
1369  *   0 on success, a negative errno value otherwise and rte_errno is set.
1370  */
1371 static int
1372 flow_dv_convert_action_modify_ipv6_dscp
1373                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
1374                          const struct rte_flow_action *action,
1375                          struct rte_flow_error *error)
1376 {
1377         const struct rte_flow_action_set_dscp *conf =
1378                 (const struct rte_flow_action_set_dscp *)(action->conf);
1379         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV6 };
1380         struct rte_flow_item_ipv6 ipv6;
1381         struct rte_flow_item_ipv6 ipv6_mask;
1382
1383         memset(&ipv6, 0, sizeof(ipv6));
1384         memset(&ipv6_mask, 0, sizeof(ipv6_mask));
1385         /*
1386          * Even though the DSCP bits offset of IPv6 is not byte aligned,
1387          * rdma-core only accept the DSCP bits byte aligned start from
1388          * bit 0 to 5 as to be compatible with IPv4. No need to shift the
1389          * bits in IPv6 case as rdma-core requires byte aligned value.
1390          */
1391         ipv6.hdr.vtc_flow = conf->dscp;
1392         ipv6_mask.hdr.vtc_flow = RTE_IPV6_HDR_DSCP_MASK >> 22;
1393         item.spec = &ipv6;
1394         item.mask = &ipv6_mask;
1395         return flow_dv_convert_modify_action(&item, modify_ipv6, NULL, resource,
1396                                              MLX5_MODIFICATION_TYPE_SET, error);
1397 }
1398
1399 static int
1400 mlx5_flow_item_field_width(struct rte_eth_dev *dev,
1401                            enum rte_flow_field_id field, int inherit,
1402                            const struct rte_flow_attr *attr,
1403                            struct rte_flow_error *error)
1404 {
1405         struct mlx5_priv *priv = dev->data->dev_private;
1406
1407         switch (field) {
1408         case RTE_FLOW_FIELD_START:
1409                 return 32;
1410         case RTE_FLOW_FIELD_MAC_DST:
1411         case RTE_FLOW_FIELD_MAC_SRC:
1412                 return 48;
1413         case RTE_FLOW_FIELD_VLAN_TYPE:
1414                 return 16;
1415         case RTE_FLOW_FIELD_VLAN_ID:
1416                 return 12;
1417         case RTE_FLOW_FIELD_MAC_TYPE:
1418                 return 16;
1419         case RTE_FLOW_FIELD_IPV4_DSCP:
1420                 return 6;
1421         case RTE_FLOW_FIELD_IPV4_TTL:
1422                 return 8;
1423         case RTE_FLOW_FIELD_IPV4_SRC:
1424         case RTE_FLOW_FIELD_IPV4_DST:
1425                 return 32;
1426         case RTE_FLOW_FIELD_IPV6_DSCP:
1427                 return 6;
1428         case RTE_FLOW_FIELD_IPV6_HOPLIMIT:
1429                 return 8;
1430         case RTE_FLOW_FIELD_IPV6_SRC:
1431         case RTE_FLOW_FIELD_IPV6_DST:
1432                 return 128;
1433         case RTE_FLOW_FIELD_TCP_PORT_SRC:
1434         case RTE_FLOW_FIELD_TCP_PORT_DST:
1435                 return 16;
1436         case RTE_FLOW_FIELD_TCP_SEQ_NUM:
1437         case RTE_FLOW_FIELD_TCP_ACK_NUM:
1438                 return 32;
1439         case RTE_FLOW_FIELD_TCP_FLAGS:
1440                 return 9;
1441         case RTE_FLOW_FIELD_UDP_PORT_SRC:
1442         case RTE_FLOW_FIELD_UDP_PORT_DST:
1443                 return 16;
1444         case RTE_FLOW_FIELD_VXLAN_VNI:
1445         case RTE_FLOW_FIELD_GENEVE_VNI:
1446                 return 24;
1447         case RTE_FLOW_FIELD_GTP_TEID:
1448         case RTE_FLOW_FIELD_TAG:
1449                 return 32;
1450         case RTE_FLOW_FIELD_MARK:
1451                 return __builtin_popcount(priv->sh->dv_mark_mask);
1452         case RTE_FLOW_FIELD_META:
1453                 return (flow_dv_get_metadata_reg(dev, attr, error) == REG_C_0) ?
1454                         __builtin_popcount(priv->sh->dv_meta_mask) : 32;
1455         case RTE_FLOW_FIELD_POINTER:
1456         case RTE_FLOW_FIELD_VALUE:
1457                 return inherit < 0 ? 0 : inherit;
1458         default:
1459                 MLX5_ASSERT(false);
1460         }
1461         return 0;
1462 }
1463
1464 static void
1465 mlx5_flow_field_id_to_modify_info
1466                 (const struct rte_flow_action_modify_data *data,
1467                  struct field_modify_info *info, uint32_t *mask,
1468                  uint32_t width, uint32_t *shift, struct rte_eth_dev *dev,
1469                  const struct rte_flow_attr *attr, struct rte_flow_error *error)
1470 {
1471         struct mlx5_priv *priv = dev->data->dev_private;
1472         uint32_t idx = 0;
1473         uint32_t off = 0;
1474
1475         switch (data->field) {
1476         case RTE_FLOW_FIELD_START:
1477                 /* not supported yet */
1478                 MLX5_ASSERT(false);
1479                 break;
1480         case RTE_FLOW_FIELD_MAC_DST:
1481                 off = data->offset > 16 ? data->offset - 16 : 0;
1482                 if (mask) {
1483                         if (data->offset < 16) {
1484                                 info[idx] = (struct field_modify_info){2, 4,
1485                                                 MLX5_MODI_OUT_DMAC_15_0};
1486                                 if (width < 16) {
1487                                         mask[1] = rte_cpu_to_be_16(0xffff >>
1488                                                                  (16 - width));
1489                                         width = 0;
1490                                 } else {
1491                                         mask[1] = RTE_BE16(0xffff);
1492                                         width -= 16;
1493                                 }
1494                                 if (!width)
1495                                         break;
1496                                 ++idx;
1497                         }
1498                         info[idx] = (struct field_modify_info){4, 0,
1499                                                 MLX5_MODI_OUT_DMAC_47_16};
1500                         mask[0] = rte_cpu_to_be_32((0xffffffff >>
1501                                                     (32 - width)) << off);
1502                 } else {
1503                         if (data->offset < 16)
1504                                 info[idx++] = (struct field_modify_info){2, 0,
1505                                                 MLX5_MODI_OUT_DMAC_15_0};
1506                         info[idx] = (struct field_modify_info){4, 0,
1507                                                 MLX5_MODI_OUT_DMAC_47_16};
1508                 }
1509                 break;
1510         case RTE_FLOW_FIELD_MAC_SRC:
1511                 off = data->offset > 16 ? data->offset - 16 : 0;
1512                 if (mask) {
1513                         if (data->offset < 16) {
1514                                 info[idx] = (struct field_modify_info){2, 4,
1515                                                 MLX5_MODI_OUT_SMAC_15_0};
1516                                 if (width < 16) {
1517                                         mask[1] = rte_cpu_to_be_16(0xffff >>
1518                                                                  (16 - width));
1519                                         width = 0;
1520                                 } else {
1521                                         mask[1] = RTE_BE16(0xffff);
1522                                         width -= 16;
1523                                 }
1524                                 if (!width)
1525                                         break;
1526                                 ++idx;
1527                         }
1528                         info[idx] = (struct field_modify_info){4, 0,
1529                                                 MLX5_MODI_OUT_SMAC_47_16};
1530                         mask[0] = rte_cpu_to_be_32((0xffffffff >>
1531                                                     (32 - width)) << off);
1532                 } else {
1533                         if (data->offset < 16)
1534                                 info[idx++] = (struct field_modify_info){2, 0,
1535                                                 MLX5_MODI_OUT_SMAC_15_0};
1536                         info[idx] = (struct field_modify_info){4, 0,
1537                                                 MLX5_MODI_OUT_SMAC_47_16};
1538                 }
1539                 break;
1540         case RTE_FLOW_FIELD_VLAN_TYPE:
1541                 /* not supported yet */
1542                 break;
1543         case RTE_FLOW_FIELD_VLAN_ID:
1544                 info[idx] = (struct field_modify_info){2, 0,
1545                                         MLX5_MODI_OUT_FIRST_VID};
1546                 if (mask)
1547                         mask[idx] = rte_cpu_to_be_16(0x0fff >> (12 - width));
1548                 break;
1549         case RTE_FLOW_FIELD_MAC_TYPE:
1550                 info[idx] = (struct field_modify_info){2, 0,
1551                                         MLX5_MODI_OUT_ETHERTYPE};
1552                 if (mask)
1553                         mask[idx] = rte_cpu_to_be_16(0xffff >> (16 - width));
1554                 break;
1555         case RTE_FLOW_FIELD_IPV4_DSCP:
1556                 info[idx] = (struct field_modify_info){1, 0,
1557                                         MLX5_MODI_OUT_IP_DSCP};
1558                 if (mask)
1559                         mask[idx] = 0x3f >> (6 - width);
1560                 break;
1561         case RTE_FLOW_FIELD_IPV4_TTL:
1562                 info[idx] = (struct field_modify_info){1, 0,
1563                                         MLX5_MODI_OUT_IPV4_TTL};
1564                 if (mask)
1565                         mask[idx] = 0xff >> (8 - width);
1566                 break;
1567         case RTE_FLOW_FIELD_IPV4_SRC:
1568                 info[idx] = (struct field_modify_info){4, 0,
1569                                         MLX5_MODI_OUT_SIPV4};
1570                 if (mask)
1571                         mask[idx] = rte_cpu_to_be_32(0xffffffff >>
1572                                                      (32 - width));
1573                 break;
1574         case RTE_FLOW_FIELD_IPV4_DST:
1575                 info[idx] = (struct field_modify_info){4, 0,
1576                                         MLX5_MODI_OUT_DIPV4};
1577                 if (mask)
1578                         mask[idx] = rte_cpu_to_be_32(0xffffffff >>
1579                                                      (32 - width));
1580                 break;
1581         case RTE_FLOW_FIELD_IPV6_DSCP:
1582                 info[idx] = (struct field_modify_info){1, 0,
1583                                         MLX5_MODI_OUT_IP_DSCP};
1584                 if (mask)
1585                         mask[idx] = 0x3f >> (6 - width);
1586                 break;
1587         case RTE_FLOW_FIELD_IPV6_HOPLIMIT:
1588                 info[idx] = (struct field_modify_info){1, 0,
1589                                         MLX5_MODI_OUT_IPV6_HOPLIMIT};
1590                 if (mask)
1591                         mask[idx] = 0xff >> (8 - width);
1592                 break;
1593         case RTE_FLOW_FIELD_IPV6_SRC:
1594                 if (mask) {
1595                         if (data->offset < 32) {
1596                                 info[idx] = (struct field_modify_info){4, 12,
1597                                                 MLX5_MODI_OUT_SIPV6_31_0};
1598                                 if (width < 32) {
1599                                         mask[3] =
1600                                                 rte_cpu_to_be_32(0xffffffff >>
1601                                                                  (32 - width));
1602                                         width = 0;
1603                                 } else {
1604                                         mask[3] = RTE_BE32(0xffffffff);
1605                                         width -= 32;
1606                                 }
1607                                 if (!width)
1608                                         break;
1609                                 ++idx;
1610                         }
1611                         if (data->offset < 64) {
1612                                 info[idx] = (struct field_modify_info){4, 8,
1613                                                 MLX5_MODI_OUT_SIPV6_63_32};
1614                                 if (width < 32) {
1615                                         mask[2] =
1616                                                 rte_cpu_to_be_32(0xffffffff >>
1617                                                                  (32 - width));
1618                                         width = 0;
1619                                 } else {
1620                                         mask[2] = RTE_BE32(0xffffffff);
1621                                         width -= 32;
1622                                 }
1623                                 if (!width)
1624                                         break;
1625                                 ++idx;
1626                         }
1627                         if (data->offset < 96) {
1628                                 info[idx] = (struct field_modify_info){4, 4,
1629                                                 MLX5_MODI_OUT_SIPV6_95_64};
1630                                 if (width < 32) {
1631                                         mask[1] =
1632                                                 rte_cpu_to_be_32(0xffffffff >>
1633                                                                  (32 - width));
1634                                         width = 0;
1635                                 } else {
1636                                         mask[1] = RTE_BE32(0xffffffff);
1637                                         width -= 32;
1638                                 }
1639                                 if (!width)
1640                                         break;
1641                                 ++idx;
1642                         }
1643                         info[idx] = (struct field_modify_info){4, 0,
1644                                                 MLX5_MODI_OUT_SIPV6_127_96};
1645                         mask[0] = rte_cpu_to_be_32(0xffffffff >> (32 - width));
1646                 } else {
1647                         if (data->offset < 32)
1648                                 info[idx++] = (struct field_modify_info){4, 0,
1649                                                 MLX5_MODI_OUT_SIPV6_31_0};
1650                         if (data->offset < 64)
1651                                 info[idx++] = (struct field_modify_info){4, 0,
1652                                                 MLX5_MODI_OUT_SIPV6_63_32};
1653                         if (data->offset < 96)
1654                                 info[idx++] = (struct field_modify_info){4, 0,
1655                                                 MLX5_MODI_OUT_SIPV6_95_64};
1656                         if (data->offset < 128)
1657                                 info[idx++] = (struct field_modify_info){4, 0,
1658                                                 MLX5_MODI_OUT_SIPV6_127_96};
1659                 }
1660                 break;
1661         case RTE_FLOW_FIELD_IPV6_DST:
1662                 if (mask) {
1663                         if (data->offset < 32) {
1664                                 info[idx] = (struct field_modify_info){4, 12,
1665                                                 MLX5_MODI_OUT_DIPV6_31_0};
1666                                 if (width < 32) {
1667                                         mask[3] =
1668                                                 rte_cpu_to_be_32(0xffffffff >>
1669                                                                  (32 - width));
1670                                         width = 0;
1671                                 } else {
1672                                         mask[3] = RTE_BE32(0xffffffff);
1673                                         width -= 32;
1674                                 }
1675                                 if (!width)
1676                                         break;
1677                                 ++idx;
1678                         }
1679                         if (data->offset < 64) {
1680                                 info[idx] = (struct field_modify_info){4, 8,
1681                                                 MLX5_MODI_OUT_DIPV6_63_32};
1682                                 if (width < 32) {
1683                                         mask[2] =
1684                                                 rte_cpu_to_be_32(0xffffffff >>
1685                                                                  (32 - width));
1686                                         width = 0;
1687                                 } else {
1688                                         mask[2] = RTE_BE32(0xffffffff);
1689                                         width -= 32;
1690                                 }
1691                                 if (!width)
1692                                         break;
1693                                 ++idx;
1694                         }
1695                         if (data->offset < 96) {
1696                                 info[idx] = (struct field_modify_info){4, 4,
1697                                                 MLX5_MODI_OUT_DIPV6_95_64};
1698                                 if (width < 32) {
1699                                         mask[1] =
1700                                                 rte_cpu_to_be_32(0xffffffff >>
1701                                                                  (32 - width));
1702                                         width = 0;
1703                                 } else {
1704                                         mask[1] = RTE_BE32(0xffffffff);
1705                                         width -= 32;
1706                                 }
1707                                 if (!width)
1708                                         break;
1709                                 ++idx;
1710                         }
1711                         info[idx] = (struct field_modify_info){4, 0,
1712                                                 MLX5_MODI_OUT_DIPV6_127_96};
1713                         mask[0] = rte_cpu_to_be_32(0xffffffff >> (32 - width));
1714                 } else {
1715                         if (data->offset < 32)
1716                                 info[idx++] = (struct field_modify_info){4, 0,
1717                                                 MLX5_MODI_OUT_DIPV6_31_0};
1718                         if (data->offset < 64)
1719                                 info[idx++] = (struct field_modify_info){4, 0,
1720                                                 MLX5_MODI_OUT_DIPV6_63_32};
1721                         if (data->offset < 96)
1722                                 info[idx++] = (struct field_modify_info){4, 0,
1723                                                 MLX5_MODI_OUT_DIPV6_95_64};
1724                         if (data->offset < 128)
1725                                 info[idx++] = (struct field_modify_info){4, 0,
1726                                                 MLX5_MODI_OUT_DIPV6_127_96};
1727                 }
1728                 break;
1729         case RTE_FLOW_FIELD_TCP_PORT_SRC:
1730                 info[idx] = (struct field_modify_info){2, 0,
1731                                         MLX5_MODI_OUT_TCP_SPORT};
1732                 if (mask)
1733                         mask[idx] = rte_cpu_to_be_16(0xffff >> (16 - width));
1734                 break;
1735         case RTE_FLOW_FIELD_TCP_PORT_DST:
1736                 info[idx] = (struct field_modify_info){2, 0,
1737                                         MLX5_MODI_OUT_TCP_DPORT};
1738                 if (mask)
1739                         mask[idx] = rte_cpu_to_be_16(0xffff >> (16 - width));
1740                 break;
1741         case RTE_FLOW_FIELD_TCP_SEQ_NUM:
1742                 info[idx] = (struct field_modify_info){4, 0,
1743                                         MLX5_MODI_OUT_TCP_SEQ_NUM};
1744                 if (mask)
1745                         mask[idx] = rte_cpu_to_be_32(0xffffffff >>
1746                                                      (32 - width));
1747                 break;
1748         case RTE_FLOW_FIELD_TCP_ACK_NUM:
1749                 info[idx] = (struct field_modify_info){4, 0,
1750                                         MLX5_MODI_OUT_TCP_ACK_NUM};
1751                 if (mask)
1752                         mask[idx] = rte_cpu_to_be_32(0xffffffff >>
1753                                                      (32 - width));
1754                 break;
1755         case RTE_FLOW_FIELD_TCP_FLAGS:
1756                 info[idx] = (struct field_modify_info){2, 0,
1757                                         MLX5_MODI_OUT_TCP_FLAGS};
1758                 if (mask)
1759                         mask[idx] = rte_cpu_to_be_16(0x1ff >> (9 - width));
1760                 break;
1761         case RTE_FLOW_FIELD_UDP_PORT_SRC:
1762                 info[idx] = (struct field_modify_info){2, 0,
1763                                         MLX5_MODI_OUT_UDP_SPORT};
1764                 if (mask)
1765                         mask[idx] = rte_cpu_to_be_16(0xffff >> (16 - width));
1766                 break;
1767         case RTE_FLOW_FIELD_UDP_PORT_DST:
1768                 info[idx] = (struct field_modify_info){2, 0,
1769                                         MLX5_MODI_OUT_UDP_DPORT};
1770                 if (mask)
1771                         mask[idx] = rte_cpu_to_be_16(0xffff >> (16 - width));
1772                 break;
1773         case RTE_FLOW_FIELD_VXLAN_VNI:
1774                 /* not supported yet */
1775                 break;
1776         case RTE_FLOW_FIELD_GENEVE_VNI:
1777                 /* not supported yet*/
1778                 break;
1779         case RTE_FLOW_FIELD_GTP_TEID:
1780                 info[idx] = (struct field_modify_info){4, 0,
1781                                         MLX5_MODI_GTP_TEID};
1782                 if (mask)
1783                         mask[idx] = rte_cpu_to_be_32(0xffffffff >>
1784                                                      (32 - width));
1785                 break;
1786         case RTE_FLOW_FIELD_TAG:
1787                 {
1788                         int reg = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG,
1789                                                    data->level, error);
1790                         if (reg < 0)
1791                                 return;
1792                         MLX5_ASSERT(reg != REG_NON);
1793                         MLX5_ASSERT((unsigned int)reg < RTE_DIM(reg_to_field));
1794                         info[idx] = (struct field_modify_info){4, 0,
1795                                                 reg_to_field[reg]};
1796                         if (mask)
1797                                 mask[idx] =
1798                                         rte_cpu_to_be_32(0xffffffff >>
1799                                                          (32 - width));
1800                 }
1801                 break;
1802         case RTE_FLOW_FIELD_MARK:
1803                 {
1804                         uint32_t mark_mask = priv->sh->dv_mark_mask;
1805                         uint32_t mark_count = __builtin_popcount(mark_mask);
1806                         int reg = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK,
1807                                                        0, error);
1808                         if (reg < 0)
1809                                 return;
1810                         MLX5_ASSERT(reg != REG_NON);
1811                         MLX5_ASSERT((unsigned int)reg < RTE_DIM(reg_to_field));
1812                         info[idx] = (struct field_modify_info){4, 0,
1813                                                 reg_to_field[reg]};
1814                         if (mask)
1815                                 mask[idx] = rte_cpu_to_be_32((mark_mask >>
1816                                          (mark_count - width)) & mark_mask);
1817                 }
1818                 break;
1819         case RTE_FLOW_FIELD_META:
1820                 {
1821                         uint32_t meta_mask = priv->sh->dv_meta_mask;
1822                         uint32_t meta_count = __builtin_popcount(meta_mask);
1823                         uint32_t msk_c0 =
1824                                 rte_cpu_to_be_32(priv->sh->dv_regc0_mask);
1825                         uint32_t shl_c0 = rte_bsf32(msk_c0);
1826                         int reg = flow_dv_get_metadata_reg(dev, attr, error);
1827                         if (reg < 0)
1828                                 return;
1829                         MLX5_ASSERT(reg != REG_NON);
1830                         MLX5_ASSERT((unsigned int)reg < RTE_DIM(reg_to_field));
1831                         if (reg == REG_C_0)
1832                                 *shift = shl_c0;
1833                         info[idx] = (struct field_modify_info){4, 0,
1834                                                 reg_to_field[reg]};
1835                         if (mask)
1836                                 mask[idx] = rte_cpu_to_be_32((meta_mask >>
1837                                         (meta_count - width)) & meta_mask);
1838                 }
1839                 break;
1840         case RTE_FLOW_FIELD_POINTER:
1841         case RTE_FLOW_FIELD_VALUE:
1842         default:
1843                 MLX5_ASSERT(false);
1844                 break;
1845         }
1846 }
1847
1848 /**
1849  * Convert modify_field action to DV specification.
1850  *
1851  * @param[in] dev
1852  *   Pointer to the rte_eth_dev structure.
1853  * @param[in,out] resource
1854  *   Pointer to the modify-header resource.
1855  * @param[in] action
1856  *   Pointer to action specification.
1857  * @param[in] attr
1858  *   Attributes of flow that includes this item.
1859  * @param[out] error
1860  *   Pointer to the error structure.
1861  *
1862  * @return
1863  *   0 on success, a negative errno value otherwise and rte_errno is set.
1864  */
1865 static int
1866 flow_dv_convert_action_modify_field
1867                         (struct rte_eth_dev *dev,
1868                          struct mlx5_flow_dv_modify_hdr_resource *resource,
1869                          const struct rte_flow_action *action,
1870                          const struct rte_flow_attr *attr,
1871                          struct rte_flow_error *error)
1872 {
1873         const struct rte_flow_action_modify_field *conf =
1874                 (const struct rte_flow_action_modify_field *)(action->conf);
1875         struct rte_flow_item item = {
1876                 .spec = NULL,
1877                 .mask = NULL
1878         };
1879         struct field_modify_info field[MLX5_ACT_MAX_MOD_FIELDS] = {
1880                                                                 {0, 0, 0} };
1881         struct field_modify_info dcopy[MLX5_ACT_MAX_MOD_FIELDS] = {
1882                                                                 {0, 0, 0} };
1883         uint32_t mask[MLX5_ACT_MAX_MOD_FIELDS] = {0, 0, 0, 0, 0};
1884         uint32_t type;
1885         uint32_t shift = 0;
1886
1887         if (conf->src.field == RTE_FLOW_FIELD_POINTER ||
1888             conf->src.field == RTE_FLOW_FIELD_VALUE) {
1889                 type = MLX5_MODIFICATION_TYPE_SET;
1890                 /** For SET fill the destination field (field) first. */
1891                 mlx5_flow_field_id_to_modify_info(&conf->dst, field, mask,
1892                                                   conf->width, &shift, dev,
1893                                                   attr, error);
1894                 item.spec = conf->src.field == RTE_FLOW_FIELD_POINTER ?
1895                                         (void *)(uintptr_t)conf->src.pvalue :
1896                                         (void *)(uintptr_t)&conf->src.value;
1897         } else {
1898                 type = MLX5_MODIFICATION_TYPE_COPY;
1899                 /** For COPY fill the destination field (dcopy) without mask. */
1900                 mlx5_flow_field_id_to_modify_info(&conf->dst, dcopy, NULL,
1901                                                   conf->width, &shift, dev,
1902                                                   attr, error);
1903                 /** Then construct the source field (field) with mask. */
1904                 mlx5_flow_field_id_to_modify_info(&conf->src, field, mask,
1905                                                   conf->width, &shift,
1906                                                   dev, attr, error);
1907         }
1908         item.mask = &mask;
1909         return flow_dv_convert_modify_action(&item,
1910                         field, dcopy, resource, type, error);
1911 }
1912
1913 /**
1914  * Validate MARK item.
1915  *
1916  * @param[in] dev
1917  *   Pointer to the rte_eth_dev structure.
1918  * @param[in] item
1919  *   Item specification.
1920  * @param[in] attr
1921  *   Attributes of flow that includes this item.
1922  * @param[out] error
1923  *   Pointer to error structure.
1924  *
1925  * @return
1926  *   0 on success, a negative errno value otherwise and rte_errno is set.
1927  */
1928 static int
1929 flow_dv_validate_item_mark(struct rte_eth_dev *dev,
1930                            const struct rte_flow_item *item,
1931                            const struct rte_flow_attr *attr __rte_unused,
1932                            struct rte_flow_error *error)
1933 {
1934         struct mlx5_priv *priv = dev->data->dev_private;
1935         struct mlx5_dev_config *config = &priv->config;
1936         const struct rte_flow_item_mark *spec = item->spec;
1937         const struct rte_flow_item_mark *mask = item->mask;
1938         const struct rte_flow_item_mark nic_mask = {
1939                 .id = priv->sh->dv_mark_mask,
1940         };
1941         int ret;
1942
1943         if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
1944                 return rte_flow_error_set(error, ENOTSUP,
1945                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1946                                           "extended metadata feature"
1947                                           " isn't enabled");
1948         if (!mlx5_flow_ext_mreg_supported(dev))
1949                 return rte_flow_error_set(error, ENOTSUP,
1950                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1951                                           "extended metadata register"
1952                                           " isn't supported");
1953         if (!nic_mask.id)
1954                 return rte_flow_error_set(error, ENOTSUP,
1955                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1956                                           "extended metadata register"
1957                                           " isn't available");
1958         ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
1959         if (ret < 0)
1960                 return ret;
1961         if (!spec)
1962                 return rte_flow_error_set(error, EINVAL,
1963                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1964                                           item->spec,
1965                                           "data cannot be empty");
1966         if (spec->id >= (MLX5_FLOW_MARK_MAX & nic_mask.id))
1967                 return rte_flow_error_set(error, EINVAL,
1968                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1969                                           &spec->id,
1970                                           "mark id exceeds the limit");
1971         if (!mask)
1972                 mask = &nic_mask;
1973         if (!mask->id)
1974                 return rte_flow_error_set(error, EINVAL,
1975                                         RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
1976                                         "mask cannot be zero");
1977
1978         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1979                                         (const uint8_t *)&nic_mask,
1980                                         sizeof(struct rte_flow_item_mark),
1981                                         MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
1982         if (ret < 0)
1983                 return ret;
1984         return 0;
1985 }
1986
1987 /**
1988  * Validate META item.
1989  *
1990  * @param[in] dev
1991  *   Pointer to the rte_eth_dev structure.
1992  * @param[in] item
1993  *   Item specification.
1994  * @param[in] attr
1995  *   Attributes of flow that includes this item.
1996  * @param[out] error
1997  *   Pointer to error structure.
1998  *
1999  * @return
2000  *   0 on success, a negative errno value otherwise and rte_errno is set.
2001  */
2002 static int
2003 flow_dv_validate_item_meta(struct rte_eth_dev *dev __rte_unused,
2004                            const struct rte_flow_item *item,
2005                            const struct rte_flow_attr *attr,
2006                            struct rte_flow_error *error)
2007 {
2008         struct mlx5_priv *priv = dev->data->dev_private;
2009         struct mlx5_dev_config *config = &priv->config;
2010         const struct rte_flow_item_meta *spec = item->spec;
2011         const struct rte_flow_item_meta *mask = item->mask;
2012         struct rte_flow_item_meta nic_mask = {
2013                 .data = UINT32_MAX
2014         };
2015         int reg;
2016         int ret;
2017
2018         if (!spec)
2019                 return rte_flow_error_set(error, EINVAL,
2020                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
2021                                           item->spec,
2022                                           "data cannot be empty");
2023         if (config->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
2024                 if (!mlx5_flow_ext_mreg_supported(dev))
2025                         return rte_flow_error_set(error, ENOTSUP,
2026                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2027                                           "extended metadata register"
2028                                           " isn't supported");
2029                 reg = flow_dv_get_metadata_reg(dev, attr, error);
2030                 if (reg < 0)
2031                         return reg;
2032                 if (reg == REG_NON)
2033                         return rte_flow_error_set(error, ENOTSUP,
2034                                         RTE_FLOW_ERROR_TYPE_ITEM, item,
2035                                         "unavalable extended metadata register");
2036                 if (reg == REG_B)
2037                         return rte_flow_error_set(error, ENOTSUP,
2038                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2039                                           "match on reg_b "
2040                                           "isn't supported");
2041                 if (reg != REG_A)
2042                         nic_mask.data = priv->sh->dv_meta_mask;
2043         } else {
2044                 if (attr->transfer)
2045                         return rte_flow_error_set(error, ENOTSUP,
2046                                         RTE_FLOW_ERROR_TYPE_ITEM, item,
2047                                         "extended metadata feature "
2048                                         "should be enabled when "
2049                                         "meta item is requested "
2050                                         "with e-switch mode ");
2051                 if (attr->ingress)
2052                         return rte_flow_error_set(error, ENOTSUP,
2053                                         RTE_FLOW_ERROR_TYPE_ITEM, item,
2054                                         "match on metadata for ingress "
2055                                         "is not supported in legacy "
2056                                         "metadata mode");
2057         }
2058         if (!mask)
2059                 mask = &rte_flow_item_meta_mask;
2060         if (!mask->data)
2061                 return rte_flow_error_set(error, EINVAL,
2062                                         RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
2063                                         "mask cannot be zero");
2064
2065         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
2066                                         (const uint8_t *)&nic_mask,
2067                                         sizeof(struct rte_flow_item_meta),
2068                                         MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2069         return ret;
2070 }
2071
2072 /**
2073  * Validate TAG item.
2074  *
2075  * @param[in] dev
2076  *   Pointer to the rte_eth_dev structure.
2077  * @param[in] item
2078  *   Item specification.
2079  * @param[in] attr
2080  *   Attributes of flow that includes this item.
2081  * @param[out] error
2082  *   Pointer to error structure.
2083  *
2084  * @return
2085  *   0 on success, a negative errno value otherwise and rte_errno is set.
2086  */
2087 static int
2088 flow_dv_validate_item_tag(struct rte_eth_dev *dev,
2089                           const struct rte_flow_item *item,
2090                           const struct rte_flow_attr *attr __rte_unused,
2091                           struct rte_flow_error *error)
2092 {
2093         const struct rte_flow_item_tag *spec = item->spec;
2094         const struct rte_flow_item_tag *mask = item->mask;
2095         const struct rte_flow_item_tag nic_mask = {
2096                 .data = RTE_BE32(UINT32_MAX),
2097                 .index = 0xff,
2098         };
2099         int ret;
2100
2101         if (!mlx5_flow_ext_mreg_supported(dev))
2102                 return rte_flow_error_set(error, ENOTSUP,
2103                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2104                                           "extensive metadata register"
2105                                           " isn't supported");
2106         if (!spec)
2107                 return rte_flow_error_set(error, EINVAL,
2108                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
2109                                           item->spec,
2110                                           "data cannot be empty");
2111         if (!mask)
2112                 mask = &rte_flow_item_tag_mask;
2113         if (!mask->data)
2114                 return rte_flow_error_set(error, EINVAL,
2115                                         RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
2116                                         "mask cannot be zero");
2117
2118         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
2119                                         (const uint8_t *)&nic_mask,
2120                                         sizeof(struct rte_flow_item_tag),
2121                                         MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2122         if (ret < 0)
2123                 return ret;
2124         if (mask->index != 0xff)
2125                 return rte_flow_error_set(error, EINVAL,
2126                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
2127                                           "partial mask for tag index"
2128                                           " is not supported");
2129         ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, spec->index, error);
2130         if (ret < 0)
2131                 return ret;
2132         MLX5_ASSERT(ret != REG_NON);
2133         return 0;
2134 }
2135
2136 /**
2137  * Validate vport item.
2138  *
2139  * @param[in] dev
2140  *   Pointer to the rte_eth_dev structure.
2141  * @param[in] item
2142  *   Item specification.
2143  * @param[in] attr
2144  *   Attributes of flow that includes this item.
2145  * @param[in] item_flags
2146  *   Bit-fields that holds the items detected until now.
2147  * @param[out] error
2148  *   Pointer to error structure.
2149  *
2150  * @return
2151  *   0 on success, a negative errno value otherwise and rte_errno is set.
2152  */
2153 static int
2154 flow_dv_validate_item_port_id(struct rte_eth_dev *dev,
2155                               const struct rte_flow_item *item,
2156                               const struct rte_flow_attr *attr,
2157                               uint64_t item_flags,
2158                               struct rte_flow_error *error)
2159 {
2160         const struct rte_flow_item_port_id *spec = item->spec;
2161         const struct rte_flow_item_port_id *mask = item->mask;
2162         const struct rte_flow_item_port_id switch_mask = {
2163                         .id = 0xffffffff,
2164         };
2165         struct mlx5_priv *esw_priv;
2166         struct mlx5_priv *dev_priv;
2167         int ret;
2168
2169         if (!attr->transfer)
2170                 return rte_flow_error_set(error, EINVAL,
2171                                           RTE_FLOW_ERROR_TYPE_ITEM,
2172                                           NULL,
2173                                           "match on port id is valid only"
2174                                           " when transfer flag is enabled");
2175         if (item_flags & MLX5_FLOW_ITEM_PORT_ID)
2176                 return rte_flow_error_set(error, ENOTSUP,
2177                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2178                                           "multiple source ports are not"
2179                                           " supported");
2180         if (!mask)
2181                 mask = &switch_mask;
2182         if (mask->id != 0xffffffff)
2183                 return rte_flow_error_set(error, ENOTSUP,
2184                                            RTE_FLOW_ERROR_TYPE_ITEM_MASK,
2185                                            mask,
2186                                            "no support for partial mask on"
2187                                            " \"id\" field");
2188         ret = mlx5_flow_item_acceptable
2189                                 (item, (const uint8_t *)mask,
2190                                  (const uint8_t *)&rte_flow_item_port_id_mask,
2191                                  sizeof(struct rte_flow_item_port_id),
2192                                  MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2193         if (ret)
2194                 return ret;
2195         if (!spec)
2196                 return 0;
2197         if (spec->id == MLX5_PORT_ESW_MGR)
2198                 return 0;
2199         esw_priv = mlx5_port_to_eswitch_info(spec->id, false);
2200         if (!esw_priv)
2201                 return rte_flow_error_set(error, rte_errno,
2202                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC, spec,
2203                                           "failed to obtain E-Switch info for"
2204                                           " port");
2205         dev_priv = mlx5_dev_to_eswitch_info(dev);
2206         if (!dev_priv)
2207                 return rte_flow_error_set(error, rte_errno,
2208                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2209                                           NULL,
2210                                           "failed to obtain E-Switch info");
2211         if (esw_priv->domain_id != dev_priv->domain_id)
2212                 return rte_flow_error_set(error, EINVAL,
2213                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC, spec,
2214                                           "cannot match on a port from a"
2215                                           " different E-Switch");
2216         return 0;
2217 }
2218
2219 /**
2220  * Validate VLAN item.
2221  *
2222  * @param[in] item
2223  *   Item specification.
2224  * @param[in] item_flags
2225  *   Bit-fields that holds the items detected until now.
2226  * @param[in] dev
2227  *   Ethernet device flow is being created on.
2228  * @param[out] error
2229  *   Pointer to error structure.
2230  *
2231  * @return
2232  *   0 on success, a negative errno value otherwise and rte_errno is set.
2233  */
2234 static int
2235 flow_dv_validate_item_vlan(const struct rte_flow_item *item,
2236                            uint64_t item_flags,
2237                            struct rte_eth_dev *dev,
2238                            struct rte_flow_error *error)
2239 {
2240         const struct rte_flow_item_vlan *mask = item->mask;
2241         const struct rte_flow_item_vlan nic_mask = {
2242                 .tci = RTE_BE16(UINT16_MAX),
2243                 .inner_type = RTE_BE16(UINT16_MAX),
2244                 .has_more_vlan = 1,
2245         };
2246         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
2247         int ret;
2248         const uint64_t l34m = tunnel ? (MLX5_FLOW_LAYER_INNER_L3 |
2249                                         MLX5_FLOW_LAYER_INNER_L4) :
2250                                        (MLX5_FLOW_LAYER_OUTER_L3 |
2251                                         MLX5_FLOW_LAYER_OUTER_L4);
2252         const uint64_t vlanm = tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
2253                                         MLX5_FLOW_LAYER_OUTER_VLAN;
2254
2255         if (item_flags & vlanm)
2256                 return rte_flow_error_set(error, EINVAL,
2257                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2258                                           "multiple VLAN layers not supported");
2259         else if ((item_flags & l34m) != 0)
2260                 return rte_flow_error_set(error, EINVAL,
2261                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2262                                           "VLAN cannot follow L3/L4 layer");
2263         if (!mask)
2264                 mask = &rte_flow_item_vlan_mask;
2265         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
2266                                         (const uint8_t *)&nic_mask,
2267                                         sizeof(struct rte_flow_item_vlan),
2268                                         MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2269         if (ret)
2270                 return ret;
2271         if (!tunnel && mask->tci != RTE_BE16(0x0fff)) {
2272                 struct mlx5_priv *priv = dev->data->dev_private;
2273
2274                 if (priv->vmwa_context) {
2275                         /*
2276                          * Non-NULL context means we have a virtual machine
2277                          * and SR-IOV enabled, we have to create VLAN interface
2278                          * to make hypervisor to setup E-Switch vport
2279                          * context correctly. We avoid creating the multiple
2280                          * VLAN interfaces, so we cannot support VLAN tag mask.
2281                          */
2282                         return rte_flow_error_set(error, EINVAL,
2283                                                   RTE_FLOW_ERROR_TYPE_ITEM,
2284                                                   item,
2285                                                   "VLAN tag mask is not"
2286                                                   " supported in virtual"
2287                                                   " environment");
2288                 }
2289         }
2290         return 0;
2291 }
2292
2293 /*
2294  * GTP flags are contained in 1 byte of the format:
2295  * -------------------------------------------
2296  * | bit   | 0 - 2   | 3  | 4   | 5 | 6 | 7  |
2297  * |-----------------------------------------|
2298  * | value | Version | PT | Res | E | S | PN |
2299  * -------------------------------------------
2300  *
2301  * Matching is supported only for GTP flags E, S, PN.
2302  */
2303 #define MLX5_GTP_FLAGS_MASK     0x07
2304
2305 /**
2306  * Validate GTP item.
2307  *
2308  * @param[in] dev
2309  *   Pointer to the rte_eth_dev structure.
2310  * @param[in] item
2311  *   Item specification.
2312  * @param[in] item_flags
2313  *   Bit-fields that holds the items detected until now.
2314  * @param[out] error
2315  *   Pointer to error structure.
2316  *
2317  * @return
2318  *   0 on success, a negative errno value otherwise and rte_errno is set.
2319  */
2320 static int
2321 flow_dv_validate_item_gtp(struct rte_eth_dev *dev,
2322                           const struct rte_flow_item *item,
2323                           uint64_t item_flags,
2324                           struct rte_flow_error *error)
2325 {
2326         struct mlx5_priv *priv = dev->data->dev_private;
2327         const struct rte_flow_item_gtp *spec = item->spec;
2328         const struct rte_flow_item_gtp *mask = item->mask;
2329         const struct rte_flow_item_gtp nic_mask = {
2330                 .v_pt_rsv_flags = MLX5_GTP_FLAGS_MASK,
2331                 .msg_type = 0xff,
2332                 .teid = RTE_BE32(0xffffffff),
2333         };
2334
2335         if (!priv->config.hca_attr.tunnel_stateless_gtp)
2336                 return rte_flow_error_set(error, ENOTSUP,
2337                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2338                                           "GTP support is not enabled");
2339         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
2340                 return rte_flow_error_set(error, ENOTSUP,
2341                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2342                                           "multiple tunnel layers not"
2343                                           " supported");
2344         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
2345                 return rte_flow_error_set(error, EINVAL,
2346                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2347                                           "no outer UDP layer found");
2348         if (!mask)
2349                 mask = &rte_flow_item_gtp_mask;
2350         if (spec && spec->v_pt_rsv_flags & ~MLX5_GTP_FLAGS_MASK)
2351                 return rte_flow_error_set(error, ENOTSUP,
2352                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2353                                           "Match is supported for GTP"
2354                                           " flags only");
2355         return mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
2356                                          (const uint8_t *)&nic_mask,
2357                                          sizeof(struct rte_flow_item_gtp),
2358                                          MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2359 }
2360
2361 /**
2362  * Validate GTP PSC item.
2363  *
2364  * @param[in] item
2365  *   Item specification.
2366  * @param[in] last_item
2367  *   Previous validated item in the pattern items.
2368  * @param[in] gtp_item
2369  *   Previous GTP item specification.
2370  * @param[in] attr
2371  *   Pointer to flow attributes.
2372  * @param[out] error
2373  *   Pointer to error structure.
2374  *
2375  * @return
2376  *   0 on success, a negative errno value otherwise and rte_errno is set.
2377  */
2378 static int
2379 flow_dv_validate_item_gtp_psc(const struct rte_flow_item *item,
2380                               uint64_t last_item,
2381                               const struct rte_flow_item *gtp_item,
2382                               const struct rte_flow_attr *attr,
2383                               struct rte_flow_error *error)
2384 {
2385         const struct rte_flow_item_gtp *gtp_spec;
2386         const struct rte_flow_item_gtp *gtp_mask;
2387         const struct rte_flow_item_gtp_psc *mask;
2388         const struct rte_flow_item_gtp_psc nic_mask = {
2389                 .hdr.type = 0xF,
2390                 .hdr.qfi = 0x3F,
2391         };
2392
2393         if (!gtp_item || !(last_item & MLX5_FLOW_LAYER_GTP))
2394                 return rte_flow_error_set
2395                         (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, item,
2396                          "GTP PSC item must be preceded with GTP item");
2397         gtp_spec = gtp_item->spec;
2398         gtp_mask = gtp_item->mask ? gtp_item->mask : &rte_flow_item_gtp_mask;
2399         /* GTP spec and E flag is requested to match zero. */
2400         if (gtp_spec &&
2401                 (gtp_mask->v_pt_rsv_flags &
2402                 ~gtp_spec->v_pt_rsv_flags & MLX5_GTP_EXT_HEADER_FLAG))
2403                 return rte_flow_error_set
2404                         (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, item,
2405                          "GTP E flag must be 1 to match GTP PSC");
2406         /* Check the flow is not created in group zero. */
2407         if (!attr->transfer && !attr->group)
2408                 return rte_flow_error_set
2409                         (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2410                          "GTP PSC is not supported for group 0");
2411         /* GTP spec is here and E flag is requested to match zero. */
2412         if (!item->spec)
2413                 return 0;
2414         mask = item->mask ? item->mask : &rte_flow_item_gtp_psc_mask;
2415         return mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
2416                                          (const uint8_t *)&nic_mask,
2417                                          sizeof(struct rte_flow_item_gtp_psc),
2418                                          MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2419 }
2420
2421 /**
2422  * Validate IPV4 item.
2423  * Use existing validation function mlx5_flow_validate_item_ipv4(), and
2424  * add specific validation of fragment_offset field,
2425  *
2426  * @param[in] item
2427  *   Item specification.
2428  * @param[in] item_flags
2429  *   Bit-fields that holds the items detected until now.
2430  * @param[out] error
2431  *   Pointer to error structure.
2432  *
2433  * @return
2434  *   0 on success, a negative errno value otherwise and rte_errno is set.
2435  */
2436 static int
2437 flow_dv_validate_item_ipv4(struct rte_eth_dev *dev,
2438                            const struct rte_flow_item *item,
2439                            uint64_t item_flags, uint64_t last_item,
2440                            uint16_t ether_type, struct rte_flow_error *error)
2441 {
2442         int ret;
2443         struct mlx5_priv *priv = dev->data->dev_private;
2444         const struct rte_flow_item_ipv4 *spec = item->spec;
2445         const struct rte_flow_item_ipv4 *last = item->last;
2446         const struct rte_flow_item_ipv4 *mask = item->mask;
2447         rte_be16_t fragment_offset_spec = 0;
2448         rte_be16_t fragment_offset_last = 0;
2449         struct rte_flow_item_ipv4 nic_ipv4_mask = {
2450                 .hdr = {
2451                         .src_addr = RTE_BE32(0xffffffff),
2452                         .dst_addr = RTE_BE32(0xffffffff),
2453                         .type_of_service = 0xff,
2454                         .fragment_offset = RTE_BE16(0xffff),
2455                         .next_proto_id = 0xff,
2456                         .time_to_live = 0xff,
2457                 },
2458         };
2459
2460         if (mask && (mask->hdr.version_ihl & RTE_IPV4_HDR_IHL_MASK)) {
2461                 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
2462                 bool ihl_cap = !tunnel ? priv->config.hca_attr.outer_ipv4_ihl :
2463                                priv->config.hca_attr.inner_ipv4_ihl;
2464                 if (!ihl_cap)
2465                         return rte_flow_error_set(error, ENOTSUP,
2466                                                   RTE_FLOW_ERROR_TYPE_ITEM,
2467                                                   item,
2468                                                   "IPV4 ihl offload not supported");
2469                 nic_ipv4_mask.hdr.version_ihl = mask->hdr.version_ihl;
2470         }
2471         ret = mlx5_flow_validate_item_ipv4(item, item_flags, last_item,
2472                                            ether_type, &nic_ipv4_mask,
2473                                            MLX5_ITEM_RANGE_ACCEPTED, error);
2474         if (ret < 0)
2475                 return ret;
2476         if (spec && mask)
2477                 fragment_offset_spec = spec->hdr.fragment_offset &
2478                                        mask->hdr.fragment_offset;
2479         if (!fragment_offset_spec)
2480                 return 0;
2481         /*
2482          * spec and mask are valid, enforce using full mask to make sure the
2483          * complete value is used correctly.
2484          */
2485         if ((mask->hdr.fragment_offset & RTE_BE16(MLX5_IPV4_FRAG_OFFSET_MASK))
2486                         != RTE_BE16(MLX5_IPV4_FRAG_OFFSET_MASK))
2487                 return rte_flow_error_set(error, EINVAL,
2488                                           RTE_FLOW_ERROR_TYPE_ITEM_MASK,
2489                                           item, "must use full mask for"
2490                                           " fragment_offset");
2491         /*
2492          * Match on fragment_offset 0x2000 means MF is 1 and frag-offset is 0,
2493          * indicating this is 1st fragment of fragmented packet.
2494          * This is not yet supported in MLX5, return appropriate error message.
2495          */
2496         if (fragment_offset_spec == RTE_BE16(RTE_IPV4_HDR_MF_FLAG))
2497                 return rte_flow_error_set(error, ENOTSUP,
2498                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2499                                           "match on first fragment not "
2500                                           "supported");
2501         if (fragment_offset_spec && !last)
2502                 return rte_flow_error_set(error, ENOTSUP,
2503                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2504                                           "specified value not supported");
2505         /* spec and last are valid, validate the specified range. */
2506         fragment_offset_last = last->hdr.fragment_offset &
2507                                mask->hdr.fragment_offset;
2508         /*
2509          * Match on fragment_offset spec 0x2001 and last 0x3fff
2510          * means MF is 1 and frag-offset is > 0.
2511          * This packet is fragment 2nd and onward, excluding last.
2512          * This is not yet supported in MLX5, return appropriate
2513          * error message.
2514          */
2515         if (fragment_offset_spec == RTE_BE16(RTE_IPV4_HDR_MF_FLAG + 1) &&
2516             fragment_offset_last == RTE_BE16(MLX5_IPV4_FRAG_OFFSET_MASK))
2517                 return rte_flow_error_set(error, ENOTSUP,
2518                                           RTE_FLOW_ERROR_TYPE_ITEM_LAST,
2519                                           last, "match on following "
2520                                           "fragments not supported");
2521         /*
2522          * Match on fragment_offset spec 0x0001 and last 0x1fff
2523          * means MF is 0 and frag-offset is > 0.
2524          * This packet is last fragment of fragmented packet.
2525          * This is not yet supported in MLX5, return appropriate
2526          * error message.
2527          */
2528         if (fragment_offset_spec == RTE_BE16(1) &&
2529             fragment_offset_last == RTE_BE16(RTE_IPV4_HDR_OFFSET_MASK))
2530                 return rte_flow_error_set(error, ENOTSUP,
2531                                           RTE_FLOW_ERROR_TYPE_ITEM_LAST,
2532                                           last, "match on last "
2533                                           "fragment not supported");
2534         /*
2535          * Match on fragment_offset spec 0x0001 and last 0x3fff
2536          * means MF and/or frag-offset is not 0.
2537          * This is a fragmented packet.
2538          * Other range values are invalid and rejected.
2539          */
2540         if (!(fragment_offset_spec == RTE_BE16(1) &&
2541               fragment_offset_last == RTE_BE16(MLX5_IPV4_FRAG_OFFSET_MASK)))
2542                 return rte_flow_error_set(error, ENOTSUP,
2543                                           RTE_FLOW_ERROR_TYPE_ITEM_LAST, last,
2544                                           "specified range not supported");
2545         return 0;
2546 }
2547
2548 /**
2549  * Validate IPV6 fragment extension item.
2550  *
2551  * @param[in] item
2552  *   Item specification.
2553  * @param[in] item_flags
2554  *   Bit-fields that holds the items detected until now.
2555  * @param[out] error
2556  *   Pointer to error structure.
2557  *
2558  * @return
2559  *   0 on success, a negative errno value otherwise and rte_errno is set.
2560  */
2561 static int
2562 flow_dv_validate_item_ipv6_frag_ext(const struct rte_flow_item *item,
2563                                     uint64_t item_flags,
2564                                     struct rte_flow_error *error)
2565 {
2566         const struct rte_flow_item_ipv6_frag_ext *spec = item->spec;
2567         const struct rte_flow_item_ipv6_frag_ext *last = item->last;
2568         const struct rte_flow_item_ipv6_frag_ext *mask = item->mask;
2569         rte_be16_t frag_data_spec = 0;
2570         rte_be16_t frag_data_last = 0;
2571         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
2572         const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
2573                                       MLX5_FLOW_LAYER_OUTER_L4;
2574         int ret = 0;
2575         struct rte_flow_item_ipv6_frag_ext nic_mask = {
2576                 .hdr = {
2577                         .next_header = 0xff,
2578                         .frag_data = RTE_BE16(0xffff),
2579                 },
2580         };
2581
2582         if (item_flags & l4m)
2583                 return rte_flow_error_set(error, EINVAL,
2584                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2585                                           "ipv6 fragment extension item cannot "
2586                                           "follow L4 item.");
2587         if ((tunnel && !(item_flags & MLX5_FLOW_LAYER_INNER_L3_IPV6)) ||
2588             (!tunnel && !(item_flags & MLX5_FLOW_LAYER_OUTER_L3_IPV6)))
2589                 return rte_flow_error_set(error, EINVAL,
2590                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2591                                           "ipv6 fragment extension item must "
2592                                           "follow ipv6 item");
2593         if (spec && mask)
2594                 frag_data_spec = spec->hdr.frag_data & mask->hdr.frag_data;
2595         if (!frag_data_spec)
2596                 return 0;
2597         /*
2598          * spec and mask are valid, enforce using full mask to make sure the
2599          * complete value is used correctly.
2600          */
2601         if ((mask->hdr.frag_data & RTE_BE16(RTE_IPV6_FRAG_USED_MASK)) !=
2602                                 RTE_BE16(RTE_IPV6_FRAG_USED_MASK))
2603                 return rte_flow_error_set(error, EINVAL,
2604                                           RTE_FLOW_ERROR_TYPE_ITEM_MASK,
2605                                           item, "must use full mask for"
2606                                           " frag_data");
2607         /*
2608          * Match on frag_data 0x00001 means M is 1 and frag-offset is 0.
2609          * This is 1st fragment of fragmented packet.
2610          */
2611         if (frag_data_spec == RTE_BE16(RTE_IPV6_EHDR_MF_MASK))
2612                 return rte_flow_error_set(error, ENOTSUP,
2613                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2614                                           "match on first fragment not "
2615                                           "supported");
2616         if (frag_data_spec && !last)
2617                 return rte_flow_error_set(error, EINVAL,
2618                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2619                                           "specified value not supported");
2620         ret = mlx5_flow_item_acceptable
2621                                 (item, (const uint8_t *)mask,
2622                                  (const uint8_t *)&nic_mask,
2623                                  sizeof(struct rte_flow_item_ipv6_frag_ext),
2624                                  MLX5_ITEM_RANGE_ACCEPTED, error);
2625         if (ret)
2626                 return ret;
2627         /* spec and last are valid, validate the specified range. */
2628         frag_data_last = last->hdr.frag_data & mask->hdr.frag_data;
2629         /*
2630          * Match on frag_data spec 0x0009 and last 0xfff9
2631          * means M is 1 and frag-offset is > 0.
2632          * This packet is fragment 2nd and onward, excluding last.
2633          * This is not yet supported in MLX5, return appropriate
2634          * error message.
2635          */
2636         if (frag_data_spec == RTE_BE16(RTE_IPV6_EHDR_FO_ALIGN |
2637                                        RTE_IPV6_EHDR_MF_MASK) &&
2638             frag_data_last == RTE_BE16(RTE_IPV6_FRAG_USED_MASK))
2639                 return rte_flow_error_set(error, ENOTSUP,
2640                                           RTE_FLOW_ERROR_TYPE_ITEM_LAST,
2641                                           last, "match on following "
2642                                           "fragments not supported");
2643         /*
2644          * Match on frag_data spec 0x0008 and last 0xfff8
2645          * means M is 0 and frag-offset is > 0.
2646          * This packet is last fragment of fragmented packet.
2647          * This is not yet supported in MLX5, return appropriate
2648          * error message.
2649          */
2650         if (frag_data_spec == RTE_BE16(RTE_IPV6_EHDR_FO_ALIGN) &&
2651             frag_data_last == RTE_BE16(RTE_IPV6_EHDR_FO_MASK))
2652                 return rte_flow_error_set(error, ENOTSUP,
2653                                           RTE_FLOW_ERROR_TYPE_ITEM_LAST,
2654                                           last, "match on last "
2655                                           "fragment not supported");
2656         /* Other range values are invalid and rejected. */
2657         return rte_flow_error_set(error, EINVAL,
2658                                   RTE_FLOW_ERROR_TYPE_ITEM_LAST, last,
2659                                   "specified range not supported");
2660 }
2661
2662 /*
2663  * Validate ASO CT item.
2664  *
2665  * @param[in] dev
2666  *   Pointer to the rte_eth_dev structure.
2667  * @param[in] item
2668  *   Item specification.
2669  * @param[in] item_flags
2670  *   Pointer to bit-fields that holds the items detected until now.
2671  * @param[out] error
2672  *   Pointer to error structure.
2673  *
2674  * @return
2675  *   0 on success, a negative errno value otherwise and rte_errno is set.
2676  */
2677 static int
2678 flow_dv_validate_item_aso_ct(struct rte_eth_dev *dev,
2679                              const struct rte_flow_item *item,
2680                              uint64_t *item_flags,
2681                              struct rte_flow_error *error)
2682 {
2683         const struct rte_flow_item_conntrack *spec = item->spec;
2684         const struct rte_flow_item_conntrack *mask = item->mask;
2685         RTE_SET_USED(dev);
2686         uint32_t flags;
2687
2688         if (*item_flags & MLX5_FLOW_LAYER_ASO_CT)
2689                 return rte_flow_error_set(error, EINVAL,
2690                                           RTE_FLOW_ERROR_TYPE_ITEM, NULL,
2691                                           "Only one CT is supported");
2692         if (!mask)
2693                 mask = &rte_flow_item_conntrack_mask;
2694         flags = spec->flags & mask->flags;
2695         if ((flags & RTE_FLOW_CONNTRACK_PKT_STATE_VALID) &&
2696             ((flags & RTE_FLOW_CONNTRACK_PKT_STATE_INVALID) ||
2697              (flags & RTE_FLOW_CONNTRACK_PKT_STATE_BAD) ||
2698              (flags & RTE_FLOW_CONNTRACK_PKT_STATE_DISABLED)))
2699                 return rte_flow_error_set(error, EINVAL,
2700                                           RTE_FLOW_ERROR_TYPE_ITEM, NULL,
2701                                           "Conflict status bits");
2702         /* State change also needs to be considered. */
2703         *item_flags |= MLX5_FLOW_LAYER_ASO_CT;
2704         return 0;
2705 }
2706
2707 /**
2708  * Validate the pop VLAN action.
2709  *
2710  * @param[in] dev
2711  *   Pointer to the rte_eth_dev structure.
2712  * @param[in] action_flags
2713  *   Holds the actions detected until now.
2714  * @param[in] action
2715  *   Pointer to the pop vlan action.
2716  * @param[in] item_flags
2717  *   The items found in this flow rule.
2718  * @param[in] attr
2719  *   Pointer to flow attributes.
2720  * @param[out] error
2721  *   Pointer to error structure.
2722  *
2723  * @return
2724  *   0 on success, a negative errno value otherwise and rte_errno is set.
2725  */
2726 static int
2727 flow_dv_validate_action_pop_vlan(struct rte_eth_dev *dev,
2728                                  uint64_t action_flags,
2729                                  const struct rte_flow_action *action,
2730                                  uint64_t item_flags,
2731                                  const struct rte_flow_attr *attr,
2732                                  struct rte_flow_error *error)
2733 {
2734         const struct mlx5_priv *priv = dev->data->dev_private;
2735         struct mlx5_dev_ctx_shared *sh = priv->sh;
2736         bool direction_error = false;
2737
2738         if (!priv->sh->pop_vlan_action)
2739                 return rte_flow_error_set(error, ENOTSUP,
2740                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2741                                           NULL,
2742                                           "pop vlan action is not supported");
2743         /* Pop VLAN is not supported in egress except for CX6 FDB mode. */
2744         if (attr->transfer) {
2745                 bool fdb_tx = priv->representor_id != UINT16_MAX;
2746                 bool is_cx5 = sh->steering_format_version ==
2747                     MLX5_STEERING_LOGIC_FORMAT_CONNECTX_5;
2748
2749                 if (fdb_tx && is_cx5)
2750                         direction_error = true;
2751         } else if (attr->egress) {
2752                 direction_error = true;
2753         }
2754         if (direction_error)
2755                 return rte_flow_error_set(error, ENOTSUP,
2756                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
2757                                           NULL,
2758                                           "pop vlan action not supported for egress");
2759         if (action_flags & MLX5_FLOW_VLAN_ACTIONS)
2760                 return rte_flow_error_set(error, ENOTSUP,
2761                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2762                                           "no support for multiple VLAN "
2763                                           "actions");
2764         /* Pop VLAN with preceding Decap requires inner header with VLAN. */
2765         if ((action_flags & MLX5_FLOW_ACTION_DECAP) &&
2766             !(item_flags & MLX5_FLOW_LAYER_INNER_VLAN))
2767                 return rte_flow_error_set(error, ENOTSUP,
2768                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2769                                           NULL,
2770                                           "cannot pop vlan after decap without "
2771                                           "match on inner vlan in the flow");
2772         /* Pop VLAN without preceding Decap requires outer header with VLAN. */
2773         if (!(action_flags & MLX5_FLOW_ACTION_DECAP) &&
2774             !(item_flags & MLX5_FLOW_LAYER_OUTER_VLAN))
2775                 return rte_flow_error_set(error, ENOTSUP,
2776                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2777                                           NULL,
2778                                           "cannot pop vlan without a "
2779                                           "match on (outer) vlan in the flow");
2780         if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
2781                 return rte_flow_error_set(error, EINVAL,
2782                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2783                                           "wrong action order, port_id should "
2784                                           "be after pop VLAN action");
2785         if (!attr->transfer && priv->representor)
2786                 return rte_flow_error_set(error, ENOTSUP,
2787                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2788                                           "pop vlan action for VF representor "
2789                                           "not supported on NIC table");
2790         return 0;
2791 }
2792
2793 /**
2794  * Get VLAN default info from vlan match info.
2795  *
2796  * @param[in] items
2797  *   the list of item specifications.
2798  * @param[out] vlan
2799  *   pointer VLAN info to fill to.
2800  *
2801  * @return
2802  *   0 on success, a negative errno value otherwise and rte_errno is set.
2803  */
2804 static void
2805 flow_dev_get_vlan_info_from_items(const struct rte_flow_item *items,
2806                                   struct rte_vlan_hdr *vlan)
2807 {
2808         const struct rte_flow_item_vlan nic_mask = {
2809                 .tci = RTE_BE16(MLX5DV_FLOW_VLAN_PCP_MASK |
2810                                 MLX5DV_FLOW_VLAN_VID_MASK),
2811                 .inner_type = RTE_BE16(0xffff),
2812         };
2813
2814         if (items == NULL)
2815                 return;
2816         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
2817                 int type = items->type;
2818
2819                 if (type == RTE_FLOW_ITEM_TYPE_VLAN ||
2820                     type == MLX5_RTE_FLOW_ITEM_TYPE_VLAN)
2821                         break;
2822         }
2823         if (items->type != RTE_FLOW_ITEM_TYPE_END) {
2824                 const struct rte_flow_item_vlan *vlan_m = items->mask;
2825                 const struct rte_flow_item_vlan *vlan_v = items->spec;
2826
2827                 /* If VLAN item in pattern doesn't contain data, return here. */
2828                 if (!vlan_v)
2829                         return;
2830                 if (!vlan_m)
2831                         vlan_m = &nic_mask;
2832                 /* Only full match values are accepted */
2833                 if ((vlan_m->tci & MLX5DV_FLOW_VLAN_PCP_MASK_BE) ==
2834                      MLX5DV_FLOW_VLAN_PCP_MASK_BE) {
2835                         vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_PCP_MASK;
2836                         vlan->vlan_tci |=
2837                                 rte_be_to_cpu_16(vlan_v->tci &
2838                                                  MLX5DV_FLOW_VLAN_PCP_MASK_BE);
2839                 }
2840                 if ((vlan_m->tci & MLX5DV_FLOW_VLAN_VID_MASK_BE) ==
2841                      MLX5DV_FLOW_VLAN_VID_MASK_BE) {
2842                         vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_VID_MASK;
2843                         vlan->vlan_tci |=
2844                                 rte_be_to_cpu_16(vlan_v->tci &
2845                                                  MLX5DV_FLOW_VLAN_VID_MASK_BE);
2846                 }
2847                 if (vlan_m->inner_type == nic_mask.inner_type)
2848                         vlan->eth_proto = rte_be_to_cpu_16(vlan_v->inner_type &
2849                                                            vlan_m->inner_type);
2850         }
2851 }
2852
2853 /**
2854  * Validate the push VLAN action.
2855  *
2856  * @param[in] dev
2857  *   Pointer to the rte_eth_dev structure.
2858  * @param[in] action_flags
2859  *   Holds the actions detected until now.
2860  * @param[in] item_flags
2861  *   The items found in this flow rule.
2862  * @param[in] action
2863  *   Pointer to the action structure.
2864  * @param[in] attr
2865  *   Pointer to flow attributes
2866  * @param[out] error
2867  *   Pointer to error structure.
2868  *
2869  * @return
2870  *   0 on success, a negative errno value otherwise and rte_errno is set.
2871  */
2872 static int
2873 flow_dv_validate_action_push_vlan(struct rte_eth_dev *dev,
2874                                   uint64_t action_flags,
2875                                   const struct rte_flow_item_vlan *vlan_m,
2876                                   const struct rte_flow_action *action,
2877                                   const struct rte_flow_attr *attr,
2878                                   struct rte_flow_error *error)
2879 {
2880         const struct rte_flow_action_of_push_vlan *push_vlan = action->conf;
2881         const struct mlx5_priv *priv = dev->data->dev_private;
2882         struct mlx5_dev_ctx_shared *sh = priv->sh;
2883         bool direction_error = false;
2884
2885         if (push_vlan->ethertype != RTE_BE16(RTE_ETHER_TYPE_VLAN) &&
2886             push_vlan->ethertype != RTE_BE16(RTE_ETHER_TYPE_QINQ))
2887                 return rte_flow_error_set(error, EINVAL,
2888                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2889                                           "invalid vlan ethertype");
2890         if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
2891                 return rte_flow_error_set(error, EINVAL,
2892                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2893                                           "wrong action order, port_id should "
2894                                           "be after push VLAN");
2895         /* Push VLAN is not supported in ingress except for CX6 FDB mode. */
2896         if (attr->transfer) {
2897                 bool fdb_tx = priv->representor_id != UINT16_MAX;
2898                 bool is_cx5 = sh->steering_format_version ==
2899                     MLX5_STEERING_LOGIC_FORMAT_CONNECTX_5;
2900
2901                 if (!fdb_tx && is_cx5)
2902                         direction_error = true;
2903         } else if (attr->ingress) {
2904                 direction_error = true;
2905         }
2906         if (direction_error)
2907                 return rte_flow_error_set(error, ENOTSUP,
2908                                           RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
2909                                           NULL,
2910                                           "push vlan action not supported for ingress");
2911         if (!attr->transfer && priv->representor)
2912                 return rte_flow_error_set(error, ENOTSUP,
2913                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2914                                           "push vlan action for VF representor "
2915                                           "not supported on NIC table");
2916         if (vlan_m &&
2917             (vlan_m->tci & MLX5DV_FLOW_VLAN_PCP_MASK_BE) &&
2918             (vlan_m->tci & MLX5DV_FLOW_VLAN_PCP_MASK_BE) !=
2919                 MLX5DV_FLOW_VLAN_PCP_MASK_BE &&
2920             !(action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_PCP) &&
2921             !(mlx5_flow_find_action
2922                 (action + 1, RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP)))
2923                 return rte_flow_error_set(error, EINVAL,
2924                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2925                                           "not full match mask on VLAN PCP and "
2926                                           "there is no of_set_vlan_pcp action, "
2927                                           "push VLAN action cannot figure out "
2928                                           "PCP value");
2929         if (vlan_m &&
2930             (vlan_m->tci & MLX5DV_FLOW_VLAN_VID_MASK_BE) &&
2931             (vlan_m->tci & MLX5DV_FLOW_VLAN_VID_MASK_BE) !=
2932                 MLX5DV_FLOW_VLAN_VID_MASK_BE &&
2933             !(action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_VID) &&
2934             !(mlx5_flow_find_action
2935                 (action + 1, RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID)))
2936                 return rte_flow_error_set(error, EINVAL,
2937                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2938                                           "not full match mask on VLAN VID and "
2939                                           "there is no of_set_vlan_vid action, "
2940                                           "push VLAN action cannot figure out "
2941                                           "VID value");
2942         (void)attr;
2943         return 0;
2944 }
2945
2946 /**
2947  * Validate the set VLAN PCP.
2948  *
2949  * @param[in] action_flags
2950  *   Holds the actions detected until now.
2951  * @param[in] actions
2952  *   Pointer to the list of actions remaining in the flow rule.
2953  * @param[out] error
2954  *   Pointer to error structure.
2955  *
2956  * @return
2957  *   0 on success, a negative errno value otherwise and rte_errno is set.
2958  */
2959 static int
2960 flow_dv_validate_action_set_vlan_pcp(uint64_t action_flags,
2961                                      const struct rte_flow_action actions[],
2962                                      struct rte_flow_error *error)
2963 {
2964         const struct rte_flow_action *action = actions;
2965         const struct rte_flow_action_of_set_vlan_pcp *conf = action->conf;
2966
2967         if (conf->vlan_pcp > 7)
2968                 return rte_flow_error_set(error, EINVAL,
2969                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2970                                           "VLAN PCP value is too big");
2971         if (!(action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN))
2972                 return rte_flow_error_set(error, ENOTSUP,
2973                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2974                                           "set VLAN PCP action must follow "
2975                                           "the push VLAN action");
2976         if (action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_PCP)
2977                 return rte_flow_error_set(error, ENOTSUP,
2978                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2979                                           "Multiple VLAN PCP modification are "
2980                                           "not supported");
2981         if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
2982                 return rte_flow_error_set(error, EINVAL,
2983                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2984                                           "wrong action order, port_id should "
2985                                           "be after set VLAN PCP");
2986         return 0;
2987 }
2988
2989 /**
2990  * Validate the set VLAN VID.
2991  *
2992  * @param[in] item_flags
2993  *   Holds the items detected in this rule.
2994  * @param[in] action_flags
2995  *   Holds the actions detected until now.
2996  * @param[in] actions
2997  *   Pointer to the list of actions remaining in the flow rule.
2998  * @param[out] error
2999  *   Pointer to error structure.
3000  *
3001  * @return
3002  *   0 on success, a negative errno value otherwise and rte_errno is set.
3003  */
3004 static int
3005 flow_dv_validate_action_set_vlan_vid(uint64_t item_flags,
3006                                      uint64_t action_flags,
3007                                      const struct rte_flow_action actions[],
3008                                      struct rte_flow_error *error)
3009 {
3010         const struct rte_flow_action *action = actions;
3011         const struct rte_flow_action_of_set_vlan_vid *conf = action->conf;
3012
3013         if (rte_be_to_cpu_16(conf->vlan_vid) > 0xFFE)
3014                 return rte_flow_error_set(error, EINVAL,
3015                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3016                                           "VLAN VID value is too big");
3017         if (!(action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN) &&
3018             !(item_flags & MLX5_FLOW_LAYER_OUTER_VLAN))
3019                 return rte_flow_error_set(error, ENOTSUP,
3020                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3021                                           "set VLAN VID action must follow push"
3022                                           " VLAN action or match on VLAN item");
3023         if (action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_VID)
3024                 return rte_flow_error_set(error, ENOTSUP,
3025                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3026                                           "Multiple VLAN VID modifications are "
3027                                           "not supported");
3028         if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
3029                 return rte_flow_error_set(error, EINVAL,
3030                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3031                                           "wrong action order, port_id should "
3032                                           "be after set VLAN VID");
3033         return 0;
3034 }
3035
3036 /*
3037  * Validate the FLAG action.
3038  *
3039  * @param[in] dev
3040  *   Pointer to the rte_eth_dev structure.
3041  * @param[in] action_flags
3042  *   Holds the actions detected until now.
3043  * @param[in] attr
3044  *   Pointer to flow attributes
3045  * @param[out] error
3046  *   Pointer to error structure.
3047  *
3048  * @return
3049  *   0 on success, a negative errno value otherwise and rte_errno is set.
3050  */
3051 static int
3052 flow_dv_validate_action_flag(struct rte_eth_dev *dev,
3053                              uint64_t action_flags,
3054                              const struct rte_flow_attr *attr,
3055                              struct rte_flow_error *error)
3056 {
3057         struct mlx5_priv *priv = dev->data->dev_private;
3058         struct mlx5_dev_config *config = &priv->config;
3059         int ret;
3060
3061         /* Fall back if no extended metadata register support. */
3062         if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
3063                 return mlx5_flow_validate_action_flag(action_flags, attr,
3064                                                       error);
3065         /* Extensive metadata mode requires registers. */
3066         if (!mlx5_flow_ext_mreg_supported(dev))
3067                 return rte_flow_error_set(error, ENOTSUP,
3068                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3069                                           "no metadata registers "
3070                                           "to support flag action");
3071         if (!(priv->sh->dv_mark_mask & MLX5_FLOW_MARK_DEFAULT))
3072                 return rte_flow_error_set(error, ENOTSUP,
3073                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3074                                           "extended metadata register"
3075                                           " isn't available");
3076         ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
3077         if (ret < 0)
3078                 return ret;
3079         MLX5_ASSERT(ret > 0);
3080         if (action_flags & MLX5_FLOW_ACTION_MARK)
3081                 return rte_flow_error_set(error, EINVAL,
3082                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3083                                           "can't mark and flag in same flow");
3084         if (action_flags & MLX5_FLOW_ACTION_FLAG)
3085                 return rte_flow_error_set(error, EINVAL,
3086                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3087                                           "can't have 2 flag"
3088                                           " actions in same flow");
3089         return 0;
3090 }
3091
3092 /**
3093  * Validate MARK action.
3094  *
3095  * @param[in] dev
3096  *   Pointer to the rte_eth_dev structure.
3097  * @param[in] action
3098  *   Pointer to action.
3099  * @param[in] action_flags
3100  *   Holds the actions detected until now.
3101  * @param[in] attr
3102  *   Pointer to flow attributes
3103  * @param[out] error
3104  *   Pointer to error structure.
3105  *
3106  * @return
3107  *   0 on success, a negative errno value otherwise and rte_errno is set.
3108  */
3109 static int
3110 flow_dv_validate_action_mark(struct rte_eth_dev *dev,
3111                              const struct rte_flow_action *action,
3112                              uint64_t action_flags,
3113                              const struct rte_flow_attr *attr,
3114                              struct rte_flow_error *error)
3115 {
3116         struct mlx5_priv *priv = dev->data->dev_private;
3117         struct mlx5_dev_config *config = &priv->config;
3118         const struct rte_flow_action_mark *mark = action->conf;
3119         int ret;
3120
3121         if (is_tunnel_offload_active(dev))
3122                 return rte_flow_error_set(error, ENOTSUP,
3123                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3124                                           "no mark action "
3125                                           "if tunnel offload active");
3126         /* Fall back if no extended metadata register support. */
3127         if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
3128                 return mlx5_flow_validate_action_mark(action, action_flags,
3129                                                       attr, error);
3130         /* Extensive metadata mode requires registers. */
3131         if (!mlx5_flow_ext_mreg_supported(dev))
3132                 return rte_flow_error_set(error, ENOTSUP,
3133                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3134                                           "no metadata registers "
3135                                           "to support mark action");
3136         if (!priv->sh->dv_mark_mask)
3137                 return rte_flow_error_set(error, ENOTSUP,
3138                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3139                                           "extended metadata register"
3140                                           " isn't available");
3141         ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
3142         if (ret < 0)
3143                 return ret;
3144         MLX5_ASSERT(ret > 0);
3145         if (!mark)
3146                 return rte_flow_error_set(error, EINVAL,
3147                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3148                                           "configuration cannot be null");
3149         if (mark->id >= (MLX5_FLOW_MARK_MAX & priv->sh->dv_mark_mask))
3150                 return rte_flow_error_set(error, EINVAL,
3151                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
3152                                           &mark->id,
3153                                           "mark id exceeds the limit");
3154         if (action_flags & MLX5_FLOW_ACTION_FLAG)
3155                 return rte_flow_error_set(error, EINVAL,
3156                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3157                                           "can't flag and mark in same flow");
3158         if (action_flags & MLX5_FLOW_ACTION_MARK)
3159                 return rte_flow_error_set(error, EINVAL,
3160                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3161                                           "can't have 2 mark actions in same"
3162                                           " flow");
3163         return 0;
3164 }
3165
3166 /**
3167  * Validate SET_META action.
3168  *
3169  * @param[in] dev
3170  *   Pointer to the rte_eth_dev structure.
3171  * @param[in] action
3172  *   Pointer to the action structure.
3173  * @param[in] action_flags
3174  *   Holds the actions detected until now.
3175  * @param[in] attr
3176  *   Pointer to flow attributes
3177  * @param[out] error
3178  *   Pointer to error structure.
3179  *
3180  * @return
3181  *   0 on success, a negative errno value otherwise and rte_errno is set.
3182  */
3183 static int
3184 flow_dv_validate_action_set_meta(struct rte_eth_dev *dev,
3185                                  const struct rte_flow_action *action,
3186                                  uint64_t action_flags __rte_unused,
3187                                  const struct rte_flow_attr *attr,
3188                                  struct rte_flow_error *error)
3189 {
3190         struct mlx5_priv *priv = dev->data->dev_private;
3191         struct mlx5_dev_config *config = &priv->config;
3192         const struct rte_flow_action_set_meta *conf;
3193         uint32_t nic_mask = UINT32_MAX;
3194         int reg;
3195
3196         if (config->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
3197             !mlx5_flow_ext_mreg_supported(dev))
3198                 return rte_flow_error_set(error, ENOTSUP,
3199                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3200                                           "extended metadata register"
3201                                           " isn't supported");
3202         reg = flow_dv_get_metadata_reg(dev, attr, error);
3203         if (reg < 0)
3204                 return reg;
3205         if (reg == REG_NON)
3206                 return rte_flow_error_set(error, ENOTSUP,
3207                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3208                                           "unavalable extended metadata register");
3209         if (reg != REG_A && reg != REG_B) {
3210                 struct mlx5_priv *priv = dev->data->dev_private;
3211
3212                 nic_mask = priv->sh->dv_meta_mask;
3213         }
3214         if (!(action->conf))
3215                 return rte_flow_error_set(error, EINVAL,
3216                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3217                                           "configuration cannot be null");
3218         conf = (const struct rte_flow_action_set_meta *)action->conf;
3219         if (!conf->mask)
3220                 return rte_flow_error_set(error, EINVAL,
3221                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3222                                           "zero mask doesn't have any effect");
3223         if (conf->mask & ~nic_mask)
3224                 return rte_flow_error_set(error, EINVAL,
3225                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3226                                           "meta data must be within reg C0");
3227         return 0;
3228 }
3229
3230 /**
3231  * Validate SET_TAG action.
3232  *
3233  * @param[in] dev
3234  *   Pointer to the rte_eth_dev structure.
3235  * @param[in] action
3236  *   Pointer to the action structure.
3237  * @param[in] action_flags
3238  *   Holds the actions detected until now.
3239  * @param[in] attr
3240  *   Pointer to flow attributes
3241  * @param[out] error
3242  *   Pointer to error structure.
3243  *
3244  * @return
3245  *   0 on success, a negative errno value otherwise and rte_errno is set.
3246  */
3247 static int
3248 flow_dv_validate_action_set_tag(struct rte_eth_dev *dev,
3249                                 const struct rte_flow_action *action,
3250                                 uint64_t action_flags,
3251                                 const struct rte_flow_attr *attr,
3252                                 struct rte_flow_error *error)
3253 {
3254         const struct rte_flow_action_set_tag *conf;
3255         const uint64_t terminal_action_flags =
3256                 MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_QUEUE |
3257                 MLX5_FLOW_ACTION_RSS;
3258         int ret;
3259
3260         if (!mlx5_flow_ext_mreg_supported(dev))
3261                 return rte_flow_error_set(error, ENOTSUP,
3262                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3263                                           "extensive metadata register"
3264                                           " isn't supported");
3265         if (!(action->conf))
3266                 return rte_flow_error_set(error, EINVAL,
3267                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3268                                           "configuration cannot be null");
3269         conf = (const struct rte_flow_action_set_tag *)action->conf;
3270         if (!conf->mask)
3271                 return rte_flow_error_set(error, EINVAL,
3272                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3273                                           "zero mask doesn't have any effect");
3274         ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, conf->index, error);
3275         if (ret < 0)
3276                 return ret;
3277         if (!attr->transfer && attr->ingress &&
3278             (action_flags & terminal_action_flags))
3279                 return rte_flow_error_set(error, EINVAL,
3280                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3281                                           "set_tag has no effect"
3282                                           " with terminal actions");
3283         return 0;
3284 }
3285
3286 /**
3287  * Validate count action.
3288  *
3289  * @param[in] dev
3290  *   Pointer to rte_eth_dev structure.
3291  * @param[in] shared
3292  *   Indicator if action is shared.
3293  * @param[in] action_flags
3294  *   Holds the actions detected until now.
3295  * @param[out] error
3296  *   Pointer to error structure.
3297  *
3298  * @return
3299  *   0 on success, a negative errno value otherwise and rte_errno is set.
3300  */
3301 static int
3302 flow_dv_validate_action_count(struct rte_eth_dev *dev, bool shared,
3303                               uint64_t action_flags,
3304                               struct rte_flow_error *error)
3305 {
3306         struct mlx5_priv *priv = dev->data->dev_private;
3307
3308         if (!priv->sh->devx)
3309                 goto notsup_err;
3310         if (action_flags & MLX5_FLOW_ACTION_COUNT)
3311                 return rte_flow_error_set(error, EINVAL,
3312                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3313                                           "duplicate count actions set");
3314         if (shared && (action_flags & MLX5_FLOW_ACTION_AGE) &&
3315             !priv->sh->flow_hit_aso_en)
3316                 return rte_flow_error_set(error, EINVAL,
3317                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3318                                           "old age and shared count combination is not supported");
3319 #ifdef HAVE_IBV_FLOW_DEVX_COUNTERS
3320         return 0;
3321 #endif
3322 notsup_err:
3323         return rte_flow_error_set
3324                       (error, ENOTSUP,
3325                        RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3326                        NULL,
3327                        "count action not supported");
3328 }
3329
3330 /**
3331  * Validate the L2 encap action.
3332  *
3333  * @param[in] dev
3334  *   Pointer to the rte_eth_dev structure.
3335  * @param[in] action_flags
3336  *   Holds the actions detected until now.
3337  * @param[in] action
3338  *   Pointer to the action structure.
3339  * @param[in] attr
3340  *   Pointer to flow attributes.
3341  * @param[out] error
3342  *   Pointer to error structure.
3343  *
3344  * @return
3345  *   0 on success, a negative errno value otherwise and rte_errno is set.
3346  */
3347 static int
3348 flow_dv_validate_action_l2_encap(struct rte_eth_dev *dev,
3349                                  uint64_t action_flags,
3350                                  const struct rte_flow_action *action,
3351                                  const struct rte_flow_attr *attr,
3352                                  struct rte_flow_error *error)
3353 {
3354         const struct mlx5_priv *priv = dev->data->dev_private;
3355
3356         if (!(action->conf))
3357                 return rte_flow_error_set(error, EINVAL,
3358                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
3359                                           "configuration cannot be null");
3360         if (action_flags & MLX5_FLOW_ACTION_ENCAP)
3361                 return rte_flow_error_set(error, EINVAL,
3362                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3363                                           "can only have a single encap action "
3364                                           "in a flow");
3365         if (!attr->transfer && priv->representor)
3366                 return rte_flow_error_set(error, ENOTSUP,
3367                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3368                                           "encap action for VF representor "
3369                                           "not supported on NIC table");
3370         return 0;
3371 }
3372
3373 /**
3374  * Validate a decap action.
3375  *
3376  * @param[in] dev
3377  *   Pointer to the rte_eth_dev structure.
3378  * @param[in] action_flags
3379  *   Holds the actions detected until now.
3380  * @param[in] action
3381  *   Pointer to the action structure.
3382  * @param[in] item_flags
3383  *   Holds the items detected.
3384  * @param[in] attr
3385  *   Pointer to flow attributes
3386  * @param[out] error
3387  *   Pointer to error structure.
3388  *
3389  * @return
3390  *   0 on success, a negative errno value otherwise and rte_errno is set.
3391  */
3392 static int
3393 flow_dv_validate_action_decap(struct rte_eth_dev *dev,
3394                               uint64_t action_flags,
3395                               const struct rte_flow_action *action,
3396                               const uint64_t item_flags,
3397                               const struct rte_flow_attr *attr,
3398                               struct rte_flow_error *error)
3399 {
3400         const struct mlx5_priv *priv = dev->data->dev_private;
3401
3402         if (priv->config.hca_attr.scatter_fcs_w_decap_disable &&
3403             !priv->config.decap_en)
3404                 return rte_flow_error_set(error, ENOTSUP,
3405                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3406                                           "decap is not enabled");
3407         if (action_flags & MLX5_FLOW_XCAP_ACTIONS)
3408                 return rte_flow_error_set(error, ENOTSUP,
3409                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3410                                           action_flags &
3411                                           MLX5_FLOW_ACTION_DECAP ? "can only "
3412                                           "have a single decap action" : "decap "
3413                                           "after encap is not supported");
3414         if (action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS)
3415                 return rte_flow_error_set(error, EINVAL,
3416                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3417                                           "can't have decap action after"
3418                                           " modify action");
3419         if (attr->egress)
3420                 return rte_flow_error_set(error, ENOTSUP,
3421                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
3422                                           NULL,
3423                                           "decap action not supported for "
3424                                           "egress");
3425         if (!attr->transfer && priv->representor)
3426                 return rte_flow_error_set(error, ENOTSUP,
3427                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3428                                           "decap action for VF representor "
3429                                           "not supported on NIC table");
3430         if (action->type == RTE_FLOW_ACTION_TYPE_VXLAN_DECAP &&
3431             !(item_flags & MLX5_FLOW_LAYER_VXLAN))
3432                 return rte_flow_error_set(error, ENOTSUP,
3433                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3434                                 "VXLAN item should be present for VXLAN decap");
3435         return 0;
3436 }
3437
3438 const struct rte_flow_action_raw_decap empty_decap = {.data = NULL, .size = 0,};
3439
3440 /**
3441  * Validate the raw encap and decap actions.
3442  *
3443  * @param[in] dev
3444  *   Pointer to the rte_eth_dev structure.
3445  * @param[in] decap
3446  *   Pointer to the decap action.
3447  * @param[in] encap
3448  *   Pointer to the encap action.
3449  * @param[in] attr
3450  *   Pointer to flow attributes
3451  * @param[in/out] action_flags
3452  *   Holds the actions detected until now.
3453  * @param[out] actions_n
3454  *   pointer to the number of actions counter.
3455  * @param[in] action
3456  *   Pointer to the action structure.
3457  * @param[in] item_flags
3458  *   Holds the items detected.
3459  * @param[out] error
3460  *   Pointer to error structure.
3461  *
3462  * @return
3463  *   0 on success, a negative errno value otherwise and rte_errno is set.
3464  */
3465 static int
3466 flow_dv_validate_action_raw_encap_decap
3467         (struct rte_eth_dev *dev,
3468          const struct rte_flow_action_raw_decap *decap,
3469          const struct rte_flow_action_raw_encap *encap,
3470          const struct rte_flow_attr *attr, uint64_t *action_flags,
3471          int *actions_n, const struct rte_flow_action *action,
3472          uint64_t item_flags, struct rte_flow_error *error)
3473 {
3474         const struct mlx5_priv *priv = dev->data->dev_private;
3475         int ret;
3476
3477         if (encap && (!encap->size || !encap->data))
3478                 return rte_flow_error_set(error, EINVAL,
3479                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3480                                           "raw encap data cannot be empty");
3481         if (decap && encap) {
3482                 if (decap->size <= MLX5_ENCAPSULATION_DECISION_SIZE &&
3483                     encap->size > MLX5_ENCAPSULATION_DECISION_SIZE)
3484                         /* L3 encap. */
3485                         decap = NULL;
3486                 else if (encap->size <=
3487                            MLX5_ENCAPSULATION_DECISION_SIZE &&
3488                            decap->size >
3489                            MLX5_ENCAPSULATION_DECISION_SIZE)
3490                         /* L3 decap. */
3491                         encap = NULL;
3492                 else if (encap->size >
3493                            MLX5_ENCAPSULATION_DECISION_SIZE &&
3494                            decap->size >
3495                            MLX5_ENCAPSULATION_DECISION_SIZE)
3496                         /* 2 L2 actions: encap and decap. */
3497                         ;
3498                 else
3499                         return rte_flow_error_set(error,
3500                                 ENOTSUP,
3501                                 RTE_FLOW_ERROR_TYPE_ACTION,
3502                                 NULL, "unsupported too small "
3503                                 "raw decap and too small raw "
3504                                 "encap combination");
3505         }
3506         if (decap) {
3507                 ret = flow_dv_validate_action_decap(dev, *action_flags, action,
3508                                                     item_flags, attr, error);
3509                 if (ret < 0)
3510                         return ret;
3511                 *action_flags |= MLX5_FLOW_ACTION_DECAP;
3512                 ++(*actions_n);
3513         }
3514         if (encap) {
3515                 if (encap->size <= MLX5_ENCAPSULATION_DECISION_SIZE)
3516                         return rte_flow_error_set(error, ENOTSUP,
3517                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3518                                                   NULL,
3519                                                   "small raw encap size");
3520                 if (*action_flags & MLX5_FLOW_ACTION_ENCAP)
3521                         return rte_flow_error_set(error, EINVAL,
3522                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3523                                                   NULL,
3524                                                   "more than one encap action");
3525                 if (!attr->transfer && priv->representor)
3526                         return rte_flow_error_set
3527                                         (error, ENOTSUP,
3528                                          RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3529                                          "encap action for VF representor "
3530                                          "not supported on NIC table");
3531                 *action_flags |= MLX5_FLOW_ACTION_ENCAP;
3532                 ++(*actions_n);
3533         }
3534         return 0;
3535 }
3536
3537 /*
3538  * Validate the ASO CT action.
3539  *
3540  * @param[in] dev
3541  *   Pointer to the rte_eth_dev structure.
3542  * @param[in] action_flags
3543  *   Holds the actions detected until now.
3544  * @param[in] item_flags
3545  *   The items found in this flow rule.
3546  * @param[in] attr
3547  *   Pointer to flow attributes.
3548  * @param[out] error
3549  *   Pointer to error structure.
3550  *
3551  * @return
3552  *   0 on success, a negative errno value otherwise and rte_errno is set.
3553  */
3554 static int
3555 flow_dv_validate_action_aso_ct(struct rte_eth_dev *dev,
3556                                uint64_t action_flags,
3557                                uint64_t item_flags,
3558                                const struct rte_flow_attr *attr,
3559                                struct rte_flow_error *error)
3560 {
3561         RTE_SET_USED(dev);
3562
3563         if (attr->group == 0 && !attr->transfer)
3564                 return rte_flow_error_set(error, ENOTSUP,
3565                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3566                                           NULL,
3567                                           "Only support non-root table");
3568         if (action_flags & MLX5_FLOW_FATE_ACTIONS)
3569                 return rte_flow_error_set(error, ENOTSUP,
3570                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3571                                           "CT cannot follow a fate action");
3572         if ((action_flags & MLX5_FLOW_ACTION_METER) ||
3573             (action_flags & MLX5_FLOW_ACTION_AGE))
3574                 return rte_flow_error_set(error, EINVAL,
3575                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3576                                           "Only one ASO action is supported");
3577         if (action_flags & MLX5_FLOW_ACTION_ENCAP)
3578                 return rte_flow_error_set(error, EINVAL,
3579                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3580                                           "Encap cannot exist before CT");
3581         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_TCP))
3582                 return rte_flow_error_set(error, EINVAL,
3583                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3584                                           "Not a outer TCP packet");
3585         return 0;
3586 }
3587
3588 int
3589 flow_dv_encap_decap_match_cb(void *tool_ctx __rte_unused,
3590                              struct mlx5_list_entry *entry, void *cb_ctx)
3591 {
3592         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
3593         struct mlx5_flow_dv_encap_decap_resource *ctx_resource = ctx->data;
3594         struct mlx5_flow_dv_encap_decap_resource *resource;
3595
3596         resource = container_of(entry, struct mlx5_flow_dv_encap_decap_resource,
3597                                 entry);
3598         if (resource->reformat_type == ctx_resource->reformat_type &&
3599             resource->ft_type == ctx_resource->ft_type &&
3600             resource->flags == ctx_resource->flags &&
3601             resource->size == ctx_resource->size &&
3602             !memcmp((const void *)resource->buf,
3603                     (const void *)ctx_resource->buf,
3604                     resource->size))
3605                 return 0;
3606         return -1;
3607 }
3608
3609 struct mlx5_list_entry *
3610 flow_dv_encap_decap_create_cb(void *tool_ctx, void *cb_ctx)
3611 {
3612         struct mlx5_dev_ctx_shared *sh = tool_ctx;
3613         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
3614         struct mlx5dv_dr_domain *domain;
3615         struct mlx5_flow_dv_encap_decap_resource *ctx_resource = ctx->data;
3616         struct mlx5_flow_dv_encap_decap_resource *resource;
3617         uint32_t idx;
3618         int ret;
3619
3620         if (ctx_resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
3621                 domain = sh->fdb_domain;
3622         else if (ctx_resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_RX)
3623                 domain = sh->rx_domain;
3624         else
3625                 domain = sh->tx_domain;
3626         /* Register new encap/decap resource. */
3627         resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_DECAP_ENCAP], &idx);
3628         if (!resource) {
3629                 rte_flow_error_set(ctx->error, ENOMEM,
3630                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3631                                    "cannot allocate resource memory");
3632                 return NULL;
3633         }
3634         *resource = *ctx_resource;
3635         resource->idx = idx;
3636         ret = mlx5_flow_os_create_flow_action_packet_reformat(sh->cdev->ctx,
3637                                                               domain, resource,
3638                                                              &resource->action);
3639         if (ret) {
3640                 mlx5_ipool_free(sh->ipool[MLX5_IPOOL_DECAP_ENCAP], idx);
3641                 rte_flow_error_set(ctx->error, ENOMEM,
3642                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3643                                    NULL, "cannot create action");
3644                 return NULL;
3645         }
3646
3647         return &resource->entry;
3648 }
3649
3650 struct mlx5_list_entry *
3651 flow_dv_encap_decap_clone_cb(void *tool_ctx, struct mlx5_list_entry *oentry,
3652                              void *cb_ctx)
3653 {
3654         struct mlx5_dev_ctx_shared *sh = tool_ctx;
3655         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
3656         struct mlx5_flow_dv_encap_decap_resource *cache_resource;
3657         uint32_t idx;
3658
3659         cache_resource = mlx5_ipool_malloc(sh->ipool[MLX5_IPOOL_DECAP_ENCAP],
3660                                            &idx);
3661         if (!cache_resource) {
3662                 rte_flow_error_set(ctx->error, ENOMEM,
3663                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3664                                    "cannot allocate resource memory");
3665                 return NULL;
3666         }
3667         memcpy(cache_resource, oentry, sizeof(*cache_resource));
3668         cache_resource->idx = idx;
3669         return &cache_resource->entry;
3670 }
3671
3672 void
3673 flow_dv_encap_decap_clone_free_cb(void *tool_ctx, struct mlx5_list_entry *entry)
3674 {
3675         struct mlx5_dev_ctx_shared *sh = tool_ctx;
3676         struct mlx5_flow_dv_encap_decap_resource *res =
3677                                        container_of(entry, typeof(*res), entry);
3678
3679         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_DECAP_ENCAP], res->idx);
3680 }
3681
3682 /**
3683  * Find existing encap/decap resource or create and register a new one.
3684  *
3685  * @param[in, out] dev
3686  *   Pointer to rte_eth_dev structure.
3687  * @param[in, out] resource
3688  *   Pointer to encap/decap resource.
3689  * @parm[in, out] dev_flow
3690  *   Pointer to the dev_flow.
3691  * @param[out] error
3692  *   pointer to error structure.
3693  *
3694  * @return
3695  *   0 on success otherwise -errno and errno is set.
3696  */
3697 static int
3698 flow_dv_encap_decap_resource_register
3699                         (struct rte_eth_dev *dev,
3700                          struct mlx5_flow_dv_encap_decap_resource *resource,
3701                          struct mlx5_flow *dev_flow,
3702                          struct rte_flow_error *error)
3703 {
3704         struct mlx5_priv *priv = dev->data->dev_private;
3705         struct mlx5_dev_ctx_shared *sh = priv->sh;
3706         struct mlx5_list_entry *entry;
3707         union {
3708                 struct {
3709                         uint32_t ft_type:8;
3710                         uint32_t refmt_type:8;
3711                         /*
3712                          * Header reformat actions can be shared between
3713                          * non-root tables. One bit to indicate non-root
3714                          * table or not.
3715                          */
3716                         uint32_t is_root:1;
3717                         uint32_t reserve:15;
3718                 };
3719                 uint32_t v32;
3720         } encap_decap_key = {
3721                 {
3722                         .ft_type = resource->ft_type,
3723                         .refmt_type = resource->reformat_type,
3724                         .is_root = !!dev_flow->dv.group,
3725                         .reserve = 0,
3726                 }
3727         };
3728         struct mlx5_flow_cb_ctx ctx = {
3729                 .error = error,
3730                 .data = resource,
3731         };
3732         struct mlx5_hlist *encaps_decaps;
3733         uint64_t key64;
3734
3735         encaps_decaps = flow_dv_hlist_prepare(sh, &sh->encaps_decaps,
3736                                 "encaps_decaps",
3737                                 MLX5_FLOW_ENCAP_DECAP_HTABLE_SZ,
3738                                 true, true, sh,
3739                                 flow_dv_encap_decap_create_cb,
3740                                 flow_dv_encap_decap_match_cb,
3741                                 flow_dv_encap_decap_remove_cb,
3742                                 flow_dv_encap_decap_clone_cb,
3743                                 flow_dv_encap_decap_clone_free_cb);
3744         if (unlikely(!encaps_decaps))
3745                 return -rte_errno;
3746         resource->flags = dev_flow->dv.group ? 0 : 1;
3747         key64 =  __rte_raw_cksum(&encap_decap_key.v32,
3748                                  sizeof(encap_decap_key.v32), 0);
3749         if (resource->reformat_type !=
3750             MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TUNNEL_TO_L2 &&
3751             resource->size)
3752                 key64 = __rte_raw_cksum(resource->buf, resource->size, key64);
3753         entry = mlx5_hlist_register(encaps_decaps, key64, &ctx);
3754         if (!entry)
3755                 return -rte_errno;
3756         resource = container_of(entry, typeof(*resource), entry);
3757         dev_flow->dv.encap_decap = resource;
3758         dev_flow->handle->dvh.rix_encap_decap = resource->idx;
3759         return 0;
3760 }
3761
3762 /**
3763  * Find existing table jump resource or create and register a new one.
3764  *
3765  * @param[in, out] dev
3766  *   Pointer to rte_eth_dev structure.
3767  * @param[in, out] tbl
3768  *   Pointer to flow table resource.
3769  * @parm[in, out] dev_flow
3770  *   Pointer to the dev_flow.
3771  * @param[out] error
3772  *   pointer to error structure.
3773  *
3774  * @return
3775  *   0 on success otherwise -errno and errno is set.
3776  */
3777 static int
3778 flow_dv_jump_tbl_resource_register
3779                         (struct rte_eth_dev *dev __rte_unused,
3780                          struct mlx5_flow_tbl_resource *tbl,
3781                          struct mlx5_flow *dev_flow,
3782                          struct rte_flow_error *error __rte_unused)
3783 {
3784         struct mlx5_flow_tbl_data_entry *tbl_data =
3785                 container_of(tbl, struct mlx5_flow_tbl_data_entry, tbl);
3786
3787         MLX5_ASSERT(tbl);
3788         MLX5_ASSERT(tbl_data->jump.action);
3789         dev_flow->handle->rix_jump = tbl_data->idx;
3790         dev_flow->dv.jump = &tbl_data->jump;
3791         return 0;
3792 }
3793
3794 int
3795 flow_dv_port_id_match_cb(void *tool_ctx __rte_unused,
3796                          struct mlx5_list_entry *entry, void *cb_ctx)
3797 {
3798         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
3799         struct mlx5_flow_dv_port_id_action_resource *ref = ctx->data;
3800         struct mlx5_flow_dv_port_id_action_resource *res =
3801                                        container_of(entry, typeof(*res), entry);
3802
3803         return ref->port_id != res->port_id;
3804 }
3805
3806 struct mlx5_list_entry *
3807 flow_dv_port_id_create_cb(void *tool_ctx, void *cb_ctx)
3808 {
3809         struct mlx5_dev_ctx_shared *sh = tool_ctx;
3810         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
3811         struct mlx5_flow_dv_port_id_action_resource *ref = ctx->data;
3812         struct mlx5_flow_dv_port_id_action_resource *resource;
3813         uint32_t idx;
3814         int ret;
3815
3816         /* Register new port id action resource. */
3817         resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_PORT_ID], &idx);
3818         if (!resource) {
3819                 rte_flow_error_set(ctx->error, ENOMEM,
3820                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3821                                    "cannot allocate port_id action memory");
3822                 return NULL;
3823         }
3824         *resource = *ref;
3825         ret = mlx5_flow_os_create_flow_action_dest_port(sh->fdb_domain,
3826                                                         ref->port_id,
3827                                                         &resource->action);
3828         if (ret) {
3829                 mlx5_ipool_free(sh->ipool[MLX5_IPOOL_PORT_ID], idx);
3830                 rte_flow_error_set(ctx->error, ENOMEM,
3831                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3832                                    "cannot create action");
3833                 return NULL;
3834         }
3835         resource->idx = idx;
3836         return &resource->entry;
3837 }
3838
3839 struct mlx5_list_entry *
3840 flow_dv_port_id_clone_cb(void *tool_ctx,
3841                          struct mlx5_list_entry *entry __rte_unused,
3842                          void *cb_ctx)
3843 {
3844         struct mlx5_dev_ctx_shared *sh = tool_ctx;
3845         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
3846         struct mlx5_flow_dv_port_id_action_resource *resource;
3847         uint32_t idx;
3848
3849         resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_PORT_ID], &idx);
3850         if (!resource) {
3851                 rte_flow_error_set(ctx->error, ENOMEM,
3852                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3853                                    "cannot allocate port_id action memory");
3854                 return NULL;
3855         }
3856         memcpy(resource, entry, sizeof(*resource));
3857         resource->idx = idx;
3858         return &resource->entry;
3859 }
3860
3861 void
3862 flow_dv_port_id_clone_free_cb(void *tool_ctx, struct mlx5_list_entry *entry)
3863 {
3864         struct mlx5_dev_ctx_shared *sh = tool_ctx;
3865         struct mlx5_flow_dv_port_id_action_resource *resource =
3866                                   container_of(entry, typeof(*resource), entry);
3867
3868         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_PORT_ID], resource->idx);
3869 }
3870
3871 /**
3872  * Find existing table port ID resource or create and register a new one.
3873  *
3874  * @param[in, out] dev
3875  *   Pointer to rte_eth_dev structure.
3876  * @param[in, out] ref
3877  *   Pointer to port ID action resource reference.
3878  * @parm[in, out] dev_flow
3879  *   Pointer to the dev_flow.
3880  * @param[out] error
3881  *   pointer to error structure.
3882  *
3883  * @return
3884  *   0 on success otherwise -errno and errno is set.
3885  */
3886 static int
3887 flow_dv_port_id_action_resource_register
3888                         (struct rte_eth_dev *dev,
3889                          struct mlx5_flow_dv_port_id_action_resource *ref,
3890                          struct mlx5_flow *dev_flow,
3891                          struct rte_flow_error *error)
3892 {
3893         struct mlx5_priv *priv = dev->data->dev_private;
3894         struct mlx5_list_entry *entry;
3895         struct mlx5_flow_dv_port_id_action_resource *resource;
3896         struct mlx5_flow_cb_ctx ctx = {
3897                 .error = error,
3898                 .data = ref,
3899         };
3900
3901         entry = mlx5_list_register(priv->sh->port_id_action_list, &ctx);
3902         if (!entry)
3903                 return -rte_errno;
3904         resource = container_of(entry, typeof(*resource), entry);
3905         dev_flow->dv.port_id_action = resource;
3906         dev_flow->handle->rix_port_id_action = resource->idx;
3907         return 0;
3908 }
3909
3910 int
3911 flow_dv_push_vlan_match_cb(void *tool_ctx __rte_unused,
3912                            struct mlx5_list_entry *entry, void *cb_ctx)
3913 {
3914         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
3915         struct mlx5_flow_dv_push_vlan_action_resource *ref = ctx->data;
3916         struct mlx5_flow_dv_push_vlan_action_resource *res =
3917                                        container_of(entry, typeof(*res), entry);
3918
3919         return ref->vlan_tag != res->vlan_tag || ref->ft_type != res->ft_type;
3920 }
3921
3922 struct mlx5_list_entry *
3923 flow_dv_push_vlan_create_cb(void *tool_ctx, void *cb_ctx)
3924 {
3925         struct mlx5_dev_ctx_shared *sh = tool_ctx;
3926         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
3927         struct mlx5_flow_dv_push_vlan_action_resource *ref = ctx->data;
3928         struct mlx5_flow_dv_push_vlan_action_resource *resource;
3929         struct mlx5dv_dr_domain *domain;
3930         uint32_t idx;
3931         int ret;
3932
3933         /* Register new port id action resource. */
3934         resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_PUSH_VLAN], &idx);
3935         if (!resource) {
3936                 rte_flow_error_set(ctx->error, ENOMEM,
3937                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3938                                    "cannot allocate push_vlan action memory");
3939                 return NULL;
3940         }
3941         *resource = *ref;
3942         if (ref->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
3943                 domain = sh->fdb_domain;
3944         else if (ref->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_RX)
3945                 domain = sh->rx_domain;
3946         else
3947                 domain = sh->tx_domain;
3948         ret = mlx5_flow_os_create_flow_action_push_vlan(domain, ref->vlan_tag,
3949                                                         &resource->action);
3950         if (ret) {
3951                 mlx5_ipool_free(sh->ipool[MLX5_IPOOL_PUSH_VLAN], idx);
3952                 rte_flow_error_set(ctx->error, ENOMEM,
3953                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3954                                    "cannot create push vlan action");
3955                 return NULL;
3956         }
3957         resource->idx = idx;
3958         return &resource->entry;
3959 }
3960
3961 struct mlx5_list_entry *
3962 flow_dv_push_vlan_clone_cb(void *tool_ctx,
3963                            struct mlx5_list_entry *entry __rte_unused,
3964                            void *cb_ctx)
3965 {
3966         struct mlx5_dev_ctx_shared *sh = tool_ctx;
3967         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
3968         struct mlx5_flow_dv_push_vlan_action_resource *resource;
3969         uint32_t idx;
3970
3971         resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_PUSH_VLAN], &idx);
3972         if (!resource) {
3973                 rte_flow_error_set(ctx->error, ENOMEM,
3974                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3975                                    "cannot allocate push_vlan action memory");
3976                 return NULL;
3977         }
3978         memcpy(resource, entry, sizeof(*resource));
3979         resource->idx = idx;
3980         return &resource->entry;
3981 }
3982
3983 void
3984 flow_dv_push_vlan_clone_free_cb(void *tool_ctx, struct mlx5_list_entry *entry)
3985 {
3986         struct mlx5_dev_ctx_shared *sh = tool_ctx;
3987         struct mlx5_flow_dv_push_vlan_action_resource *resource =
3988                                   container_of(entry, typeof(*resource), entry);
3989
3990         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_PUSH_VLAN], resource->idx);
3991 }
3992
3993 /**
3994  * Find existing push vlan resource or create and register a new one.
3995  *
3996  * @param [in, out] dev
3997  *   Pointer to rte_eth_dev structure.
3998  * @param[in, out] ref
3999  *   Pointer to port ID action resource reference.
4000  * @parm[in, out] dev_flow
4001  *   Pointer to the dev_flow.
4002  * @param[out] error
4003  *   pointer to error structure.
4004  *
4005  * @return
4006  *   0 on success otherwise -errno and errno is set.
4007  */
4008 static int
4009 flow_dv_push_vlan_action_resource_register
4010                        (struct rte_eth_dev *dev,
4011                         struct mlx5_flow_dv_push_vlan_action_resource *ref,
4012                         struct mlx5_flow *dev_flow,
4013                         struct rte_flow_error *error)
4014 {
4015         struct mlx5_priv *priv = dev->data->dev_private;
4016         struct mlx5_flow_dv_push_vlan_action_resource *resource;
4017         struct mlx5_list_entry *entry;
4018         struct mlx5_flow_cb_ctx ctx = {
4019                 .error = error,
4020                 .data = ref,
4021         };
4022
4023         entry = mlx5_list_register(priv->sh->push_vlan_action_list, &ctx);
4024         if (!entry)
4025                 return -rte_errno;
4026         resource = container_of(entry, typeof(*resource), entry);
4027
4028         dev_flow->handle->dvh.rix_push_vlan = resource->idx;
4029         dev_flow->dv.push_vlan_res = resource;
4030         return 0;
4031 }
4032
4033 /**
4034  * Get the size of specific rte_flow_item_type hdr size
4035  *
4036  * @param[in] item_type
4037  *   Tested rte_flow_item_type.
4038  *
4039  * @return
4040  *   sizeof struct item_type, 0 if void or irrelevant.
4041  */
4042 static size_t
4043 flow_dv_get_item_hdr_len(const enum rte_flow_item_type item_type)
4044 {
4045         size_t retval;
4046
4047         switch (item_type) {
4048         case RTE_FLOW_ITEM_TYPE_ETH:
4049                 retval = sizeof(struct rte_ether_hdr);
4050                 break;
4051         case RTE_FLOW_ITEM_TYPE_VLAN:
4052                 retval = sizeof(struct rte_vlan_hdr);
4053                 break;
4054         case RTE_FLOW_ITEM_TYPE_IPV4:
4055                 retval = sizeof(struct rte_ipv4_hdr);
4056                 break;
4057         case RTE_FLOW_ITEM_TYPE_IPV6:
4058                 retval = sizeof(struct rte_ipv6_hdr);
4059                 break;
4060         case RTE_FLOW_ITEM_TYPE_UDP:
4061                 retval = sizeof(struct rte_udp_hdr);
4062                 break;
4063         case RTE_FLOW_ITEM_TYPE_TCP:
4064                 retval = sizeof(struct rte_tcp_hdr);
4065                 break;
4066         case RTE_FLOW_ITEM_TYPE_VXLAN:
4067         case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
4068                 retval = sizeof(struct rte_vxlan_hdr);
4069                 break;
4070         case RTE_FLOW_ITEM_TYPE_GRE:
4071         case RTE_FLOW_ITEM_TYPE_NVGRE:
4072                 retval = sizeof(struct rte_gre_hdr);
4073                 break;
4074         case RTE_FLOW_ITEM_TYPE_MPLS:
4075                 retval = sizeof(struct rte_mpls_hdr);
4076                 break;
4077         case RTE_FLOW_ITEM_TYPE_VOID: /* Fall through. */
4078         default:
4079                 retval = 0;
4080                 break;
4081         }
4082         return retval;
4083 }
4084
4085 #define MLX5_ENCAP_IPV4_VERSION         0x40
4086 #define MLX5_ENCAP_IPV4_IHL_MIN         0x05
4087 #define MLX5_ENCAP_IPV4_TTL_DEF         0x40
4088 #define MLX5_ENCAP_IPV6_VTC_FLOW        0x60000000
4089 #define MLX5_ENCAP_IPV6_HOP_LIMIT       0xff
4090 #define MLX5_ENCAP_VXLAN_FLAGS          0x08000000
4091 #define MLX5_ENCAP_VXLAN_GPE_FLAGS      0x04
4092
4093 /**
4094  * Convert the encap action data from list of rte_flow_item to raw buffer
4095  *
4096  * @param[in] items
4097  *   Pointer to rte_flow_item objects list.
4098  * @param[out] buf
4099  *   Pointer to the output buffer.
4100  * @param[out] size
4101  *   Pointer to the output buffer size.
4102  * @param[out] error
4103  *   Pointer to the error structure.
4104  *
4105  * @return
4106  *   0 on success, a negative errno value otherwise and rte_errno is set.
4107  */
4108 static int
4109 flow_dv_convert_encap_data(const struct rte_flow_item *items, uint8_t *buf,
4110                            size_t *size, struct rte_flow_error *error)
4111 {
4112         struct rte_ether_hdr *eth = NULL;
4113         struct rte_vlan_hdr *vlan = NULL;
4114         struct rte_ipv4_hdr *ipv4 = NULL;
4115         struct rte_ipv6_hdr *ipv6 = NULL;
4116         struct rte_udp_hdr *udp = NULL;
4117         struct rte_vxlan_hdr *vxlan = NULL;
4118         struct rte_vxlan_gpe_hdr *vxlan_gpe = NULL;
4119         struct rte_gre_hdr *gre = NULL;
4120         size_t len;
4121         size_t temp_size = 0;
4122
4123         if (!items)
4124                 return rte_flow_error_set(error, EINVAL,
4125                                           RTE_FLOW_ERROR_TYPE_ACTION,
4126                                           NULL, "invalid empty data");
4127         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
4128                 len = flow_dv_get_item_hdr_len(items->type);
4129                 if (len + temp_size > MLX5_ENCAP_MAX_LEN)
4130                         return rte_flow_error_set(error, EINVAL,
4131                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4132                                                   (void *)items->type,
4133                                                   "items total size is too big"
4134                                                   " for encap action");
4135                 rte_memcpy((void *)&buf[temp_size], items->spec, len);
4136                 switch (items->type) {
4137                 case RTE_FLOW_ITEM_TYPE_ETH:
4138                         eth = (struct rte_ether_hdr *)&buf[temp_size];
4139                         break;
4140                 case RTE_FLOW_ITEM_TYPE_VLAN:
4141                         vlan = (struct rte_vlan_hdr *)&buf[temp_size];
4142                         if (!eth)
4143                                 return rte_flow_error_set(error, EINVAL,
4144                                                 RTE_FLOW_ERROR_TYPE_ACTION,
4145                                                 (void *)items->type,
4146                                                 "eth header not found");
4147                         if (!eth->ether_type)
4148                                 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_VLAN);
4149                         break;
4150                 case RTE_FLOW_ITEM_TYPE_IPV4:
4151                         ipv4 = (struct rte_ipv4_hdr *)&buf[temp_size];
4152                         if (!vlan && !eth)
4153                                 return rte_flow_error_set(error, EINVAL,
4154                                                 RTE_FLOW_ERROR_TYPE_ACTION,
4155                                                 (void *)items->type,
4156                                                 "neither eth nor vlan"
4157                                                 " header found");
4158                         if (vlan && !vlan->eth_proto)
4159                                 vlan->eth_proto = RTE_BE16(RTE_ETHER_TYPE_IPV4);
4160                         else if (eth && !eth->ether_type)
4161                                 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_IPV4);
4162                         if (!ipv4->version_ihl)
4163                                 ipv4->version_ihl = MLX5_ENCAP_IPV4_VERSION |
4164                                                     MLX5_ENCAP_IPV4_IHL_MIN;
4165                         if (!ipv4->time_to_live)
4166                                 ipv4->time_to_live = MLX5_ENCAP_IPV4_TTL_DEF;
4167                         break;
4168                 case RTE_FLOW_ITEM_TYPE_IPV6:
4169                         ipv6 = (struct rte_ipv6_hdr *)&buf[temp_size];
4170                         if (!vlan && !eth)
4171                                 return rte_flow_error_set(error, EINVAL,
4172                                                 RTE_FLOW_ERROR_TYPE_ACTION,
4173                                                 (void *)items->type,
4174                                                 "neither eth nor vlan"
4175                                                 " header found");
4176                         if (vlan && !vlan->eth_proto)
4177                                 vlan->eth_proto = RTE_BE16(RTE_ETHER_TYPE_IPV6);
4178                         else if (eth && !eth->ether_type)
4179                                 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_IPV6);
4180                         if (!ipv6->vtc_flow)
4181                                 ipv6->vtc_flow =
4182                                         RTE_BE32(MLX5_ENCAP_IPV6_VTC_FLOW);
4183                         if (!ipv6->hop_limits)
4184                                 ipv6->hop_limits = MLX5_ENCAP_IPV6_HOP_LIMIT;
4185                         break;
4186                 case RTE_FLOW_ITEM_TYPE_UDP:
4187                         udp = (struct rte_udp_hdr *)&buf[temp_size];
4188                         if (!ipv4 && !ipv6)
4189                                 return rte_flow_error_set(error, EINVAL,
4190                                                 RTE_FLOW_ERROR_TYPE_ACTION,
4191                                                 (void *)items->type,
4192                                                 "ip header not found");
4193                         if (ipv4 && !ipv4->next_proto_id)
4194                                 ipv4->next_proto_id = IPPROTO_UDP;
4195                         else if (ipv6 && !ipv6->proto)
4196                                 ipv6->proto = IPPROTO_UDP;
4197                         break;
4198                 case RTE_FLOW_ITEM_TYPE_VXLAN:
4199                         vxlan = (struct rte_vxlan_hdr *)&buf[temp_size];
4200                         if (!udp)
4201                                 return rte_flow_error_set(error, EINVAL,
4202                                                 RTE_FLOW_ERROR_TYPE_ACTION,
4203                                                 (void *)items->type,
4204                                                 "udp header not found");
4205                         if (!udp->dst_port)
4206                                 udp->dst_port = RTE_BE16(MLX5_UDP_PORT_VXLAN);
4207                         if (!vxlan->vx_flags)
4208                                 vxlan->vx_flags =
4209                                         RTE_BE32(MLX5_ENCAP_VXLAN_FLAGS);
4210                         break;
4211                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
4212                         vxlan_gpe = (struct rte_vxlan_gpe_hdr *)&buf[temp_size];
4213                         if (!udp)
4214                                 return rte_flow_error_set(error, EINVAL,
4215                                                 RTE_FLOW_ERROR_TYPE_ACTION,
4216                                                 (void *)items->type,
4217                                                 "udp header not found");
4218                         if (!vxlan_gpe->proto)
4219                                 return rte_flow_error_set(error, EINVAL,
4220                                                 RTE_FLOW_ERROR_TYPE_ACTION,
4221                                                 (void *)items->type,
4222                                                 "next protocol not found");
4223                         if (!udp->dst_port)
4224                                 udp->dst_port =
4225                                         RTE_BE16(MLX5_UDP_PORT_VXLAN_GPE);
4226                         if (!vxlan_gpe->vx_flags)
4227                                 vxlan_gpe->vx_flags =
4228                                                 MLX5_ENCAP_VXLAN_GPE_FLAGS;
4229                         break;
4230                 case RTE_FLOW_ITEM_TYPE_GRE:
4231                 case RTE_FLOW_ITEM_TYPE_NVGRE:
4232                         gre = (struct rte_gre_hdr *)&buf[temp_size];
4233                         if (!gre->proto)
4234                                 return rte_flow_error_set(error, EINVAL,
4235                                                 RTE_FLOW_ERROR_TYPE_ACTION,
4236                                                 (void *)items->type,
4237                                                 "next protocol not found");
4238                         if (!ipv4 && !ipv6)
4239                                 return rte_flow_error_set(error, EINVAL,
4240                                                 RTE_FLOW_ERROR_TYPE_ACTION,
4241                                                 (void *)items->type,
4242                                                 "ip header not found");
4243                         if (ipv4 && !ipv4->next_proto_id)
4244                                 ipv4->next_proto_id = IPPROTO_GRE;
4245                         else if (ipv6 && !ipv6->proto)
4246                                 ipv6->proto = IPPROTO_GRE;
4247                         break;
4248                 case RTE_FLOW_ITEM_TYPE_VOID:
4249                         break;
4250                 default:
4251                         return rte_flow_error_set(error, EINVAL,
4252                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4253                                                   (void *)items->type,
4254                                                   "unsupported item type");
4255                         break;
4256                 }
4257                 temp_size += len;
4258         }
4259         *size = temp_size;
4260         return 0;
4261 }
4262
4263 static int
4264 flow_dv_zero_encap_udp_csum(void *data, struct rte_flow_error *error)
4265 {
4266         struct rte_ether_hdr *eth = NULL;
4267         struct rte_vlan_hdr *vlan = NULL;
4268         struct rte_ipv6_hdr *ipv6 = NULL;
4269         struct rte_udp_hdr *udp = NULL;
4270         char *next_hdr;
4271         uint16_t proto;
4272
4273         eth = (struct rte_ether_hdr *)data;
4274         next_hdr = (char *)(eth + 1);
4275         proto = RTE_BE16(eth->ether_type);
4276
4277         /* VLAN skipping */
4278         while (proto == RTE_ETHER_TYPE_VLAN || proto == RTE_ETHER_TYPE_QINQ) {
4279                 vlan = (struct rte_vlan_hdr *)next_hdr;
4280                 proto = RTE_BE16(vlan->eth_proto);
4281                 next_hdr += sizeof(struct rte_vlan_hdr);
4282         }
4283
4284         /* HW calculates IPv4 csum. no need to proceed */
4285         if (proto == RTE_ETHER_TYPE_IPV4)
4286                 return 0;
4287
4288         /* non IPv4/IPv6 header. not supported */
4289         if (proto != RTE_ETHER_TYPE_IPV6) {
4290                 return rte_flow_error_set(error, ENOTSUP,
4291                                           RTE_FLOW_ERROR_TYPE_ACTION,
4292                                           NULL, "Cannot offload non IPv4/IPv6");
4293         }
4294
4295         ipv6 = (struct rte_ipv6_hdr *)next_hdr;
4296
4297         /* ignore non UDP */
4298         if (ipv6->proto != IPPROTO_UDP)
4299                 return 0;
4300
4301         udp = (struct rte_udp_hdr *)(ipv6 + 1);
4302         udp->dgram_cksum = 0;
4303
4304         return 0;
4305 }
4306
4307 /**
4308  * Convert L2 encap action to DV specification.
4309  *
4310  * @param[in] dev
4311  *   Pointer to rte_eth_dev structure.
4312  * @param[in] action
4313  *   Pointer to action structure.
4314  * @param[in, out] dev_flow
4315  *   Pointer to the mlx5_flow.
4316  * @param[in] transfer
4317  *   Mark if the flow is E-Switch flow.
4318  * @param[out] error
4319  *   Pointer to the error structure.
4320  *
4321  * @return
4322  *   0 on success, a negative errno value otherwise and rte_errno is set.
4323  */
4324 static int
4325 flow_dv_create_action_l2_encap(struct rte_eth_dev *dev,
4326                                const struct rte_flow_action *action,
4327                                struct mlx5_flow *dev_flow,
4328                                uint8_t transfer,
4329                                struct rte_flow_error *error)
4330 {
4331         const struct rte_flow_item *encap_data;
4332         const struct rte_flow_action_raw_encap *raw_encap_data;
4333         struct mlx5_flow_dv_encap_decap_resource res = {
4334                 .reformat_type =
4335                         MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L2_TUNNEL,
4336                 .ft_type = transfer ? MLX5DV_FLOW_TABLE_TYPE_FDB :
4337                                       MLX5DV_FLOW_TABLE_TYPE_NIC_TX,
4338         };
4339
4340         if (action->type == RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
4341                 raw_encap_data =
4342                         (const struct rte_flow_action_raw_encap *)action->conf;
4343                 res.size = raw_encap_data->size;
4344                 memcpy(res.buf, raw_encap_data->data, res.size);
4345         } else {
4346                 if (action->type == RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP)
4347                         encap_data =
4348                                 ((const struct rte_flow_action_vxlan_encap *)
4349                                                 action->conf)->definition;
4350                 else
4351                         encap_data =
4352                                 ((const struct rte_flow_action_nvgre_encap *)
4353                                                 action->conf)->definition;
4354                 if (flow_dv_convert_encap_data(encap_data, res.buf,
4355                                                &res.size, error))
4356                         return -rte_errno;
4357         }
4358         if (flow_dv_zero_encap_udp_csum(res.buf, error))
4359                 return -rte_errno;
4360         if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
4361                 return rte_flow_error_set(error, EINVAL,
4362                                           RTE_FLOW_ERROR_TYPE_ACTION,
4363                                           NULL, "can't create L2 encap action");
4364         return 0;
4365 }
4366
4367 /**
4368  * Convert L2 decap action to DV specification.
4369  *
4370  * @param[in] dev
4371  *   Pointer to rte_eth_dev structure.
4372  * @param[in, out] dev_flow
4373  *   Pointer to the mlx5_flow.
4374  * @param[in] transfer
4375  *   Mark if the flow is E-Switch flow.
4376  * @param[out] error
4377  *   Pointer to the error structure.
4378  *
4379  * @return
4380  *   0 on success, a negative errno value otherwise and rte_errno is set.
4381  */
4382 static int
4383 flow_dv_create_action_l2_decap(struct rte_eth_dev *dev,
4384                                struct mlx5_flow *dev_flow,
4385                                uint8_t transfer,
4386                                struct rte_flow_error *error)
4387 {
4388         struct mlx5_flow_dv_encap_decap_resource res = {
4389                 .size = 0,
4390                 .reformat_type =
4391                         MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TUNNEL_TO_L2,
4392                 .ft_type = transfer ? MLX5DV_FLOW_TABLE_TYPE_FDB :
4393                                       MLX5DV_FLOW_TABLE_TYPE_NIC_RX,
4394         };
4395
4396         if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
4397                 return rte_flow_error_set(error, EINVAL,
4398                                           RTE_FLOW_ERROR_TYPE_ACTION,
4399                                           NULL, "can't create L2 decap action");
4400         return 0;
4401 }
4402
4403 /**
4404  * Convert raw decap/encap (L3 tunnel) action to DV specification.
4405  *
4406  * @param[in] dev
4407  *   Pointer to rte_eth_dev structure.
4408  * @param[in] action
4409  *   Pointer to action structure.
4410  * @param[in, out] dev_flow
4411  *   Pointer to the mlx5_flow.
4412  * @param[in] attr
4413  *   Pointer to the flow attributes.
4414  * @param[out] error
4415  *   Pointer to the error structure.
4416  *
4417  * @return
4418  *   0 on success, a negative errno value otherwise and rte_errno is set.
4419  */
4420 static int
4421 flow_dv_create_action_raw_encap(struct rte_eth_dev *dev,
4422                                 const struct rte_flow_action *action,
4423                                 struct mlx5_flow *dev_flow,
4424                                 const struct rte_flow_attr *attr,
4425                                 struct rte_flow_error *error)
4426 {
4427         const struct rte_flow_action_raw_encap *encap_data;
4428         struct mlx5_flow_dv_encap_decap_resource res;
4429
4430         memset(&res, 0, sizeof(res));
4431         encap_data = (const struct rte_flow_action_raw_encap *)action->conf;
4432         res.size = encap_data->size;
4433         memcpy(res.buf, encap_data->data, res.size);
4434         res.reformat_type = res.size < MLX5_ENCAPSULATION_DECISION_SIZE ?
4435                 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L3_TUNNEL_TO_L2 :
4436                 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L3_TUNNEL;
4437         if (attr->transfer)
4438                 res.ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
4439         else
4440                 res.ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
4441                                              MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
4442         if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
4443                 return rte_flow_error_set(error, EINVAL,
4444                                           RTE_FLOW_ERROR_TYPE_ACTION,
4445                                           NULL, "can't create encap action");
4446         return 0;
4447 }
4448
4449 /**
4450  * Create action push VLAN.
4451  *
4452  * @param[in] dev
4453  *   Pointer to rte_eth_dev structure.
4454  * @param[in] attr
4455  *   Pointer to the flow attributes.
4456  * @param[in] vlan
4457  *   Pointer to the vlan to push to the Ethernet header.
4458  * @param[in, out] dev_flow
4459  *   Pointer to the mlx5_flow.
4460  * @param[out] error
4461  *   Pointer to the error structure.
4462  *
4463  * @return
4464  *   0 on success, a negative errno value otherwise and rte_errno is set.
4465  */
4466 static int
4467 flow_dv_create_action_push_vlan(struct rte_eth_dev *dev,
4468                                 const struct rte_flow_attr *attr,
4469                                 const struct rte_vlan_hdr *vlan,
4470                                 struct mlx5_flow *dev_flow,
4471                                 struct rte_flow_error *error)
4472 {
4473         struct mlx5_flow_dv_push_vlan_action_resource res;
4474
4475         memset(&res, 0, sizeof(res));
4476         res.vlan_tag =
4477                 rte_cpu_to_be_32(((uint32_t)vlan->eth_proto) << 16 |
4478                                  vlan->vlan_tci);
4479         if (attr->transfer)
4480                 res.ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
4481         else
4482                 res.ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
4483                                              MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
4484         return flow_dv_push_vlan_action_resource_register
4485                                             (dev, &res, dev_flow, error);
4486 }
4487
4488 /**
4489  * Validate the modify-header actions.
4490  *
4491  * @param[in] action_flags
4492  *   Holds the actions detected until now.
4493  * @param[in] action
4494  *   Pointer to the modify action.
4495  * @param[out] error
4496  *   Pointer to error structure.
4497  *
4498  * @return
4499  *   0 on success, a negative errno value otherwise and rte_errno is set.
4500  */
4501 static int
4502 flow_dv_validate_action_modify_hdr(const uint64_t action_flags,
4503                                    const struct rte_flow_action *action,
4504                                    struct rte_flow_error *error)
4505 {
4506         if (action->type != RTE_FLOW_ACTION_TYPE_DEC_TTL && !action->conf)
4507                 return rte_flow_error_set(error, EINVAL,
4508                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
4509                                           NULL, "action configuration not set");
4510         if (action_flags & MLX5_FLOW_ACTION_ENCAP)
4511                 return rte_flow_error_set(error, EINVAL,
4512                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
4513                                           "can't have encap action before"
4514                                           " modify action");
4515         return 0;
4516 }
4517
4518 /**
4519  * Validate the modify-header MAC address actions.
4520  *
4521  * @param[in] action_flags
4522  *   Holds the actions detected until now.
4523  * @param[in] action
4524  *   Pointer to the modify action.
4525  * @param[in] item_flags
4526  *   Holds the items detected.
4527  * @param[out] error
4528  *   Pointer to error structure.
4529  *
4530  * @return
4531  *   0 on success, a negative errno value otherwise and rte_errno is set.
4532  */
4533 static int
4534 flow_dv_validate_action_modify_mac(const uint64_t action_flags,
4535                                    const struct rte_flow_action *action,
4536                                    const uint64_t item_flags,
4537                                    struct rte_flow_error *error)
4538 {
4539         int ret = 0;
4540
4541         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
4542         if (!ret) {
4543                 if (!(item_flags & MLX5_FLOW_LAYER_L2))
4544                         return rte_flow_error_set(error, EINVAL,
4545                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4546                                                   NULL,
4547                                                   "no L2 item in pattern");
4548         }
4549         return ret;
4550 }
4551
4552 /**
4553  * Validate the modify-header IPv4 address actions.
4554  *
4555  * @param[in] action_flags
4556  *   Holds the actions detected until now.
4557  * @param[in] action
4558  *   Pointer to the modify action.
4559  * @param[in] item_flags
4560  *   Holds the items detected.
4561  * @param[out] error
4562  *   Pointer to error structure.
4563  *
4564  * @return
4565  *   0 on success, a negative errno value otherwise and rte_errno is set.
4566  */
4567 static int
4568 flow_dv_validate_action_modify_ipv4(const uint64_t action_flags,
4569                                     const struct rte_flow_action *action,
4570                                     const uint64_t item_flags,
4571                                     struct rte_flow_error *error)
4572 {
4573         int ret = 0;
4574         uint64_t layer;
4575
4576         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
4577         if (!ret) {
4578                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
4579                                  MLX5_FLOW_LAYER_INNER_L3_IPV4 :
4580                                  MLX5_FLOW_LAYER_OUTER_L3_IPV4;
4581                 if (!(item_flags & layer))
4582                         return rte_flow_error_set(error, EINVAL,
4583                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4584                                                   NULL,
4585                                                   "no ipv4 item in pattern");
4586         }
4587         return ret;
4588 }
4589
4590 /**
4591  * Validate the modify-header IPv6 address actions.
4592  *
4593  * @param[in] action_flags
4594  *   Holds the actions detected until now.
4595  * @param[in] action
4596  *   Pointer to the modify action.
4597  * @param[in] item_flags
4598  *   Holds the items detected.
4599  * @param[out] error
4600  *   Pointer to error structure.
4601  *
4602  * @return
4603  *   0 on success, a negative errno value otherwise and rte_errno is set.
4604  */
4605 static int
4606 flow_dv_validate_action_modify_ipv6(const uint64_t action_flags,
4607                                     const struct rte_flow_action *action,
4608                                     const uint64_t item_flags,
4609                                     struct rte_flow_error *error)
4610 {
4611         int ret = 0;
4612         uint64_t layer;
4613
4614         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
4615         if (!ret) {
4616                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
4617                                  MLX5_FLOW_LAYER_INNER_L3_IPV6 :
4618                                  MLX5_FLOW_LAYER_OUTER_L3_IPV6;
4619                 if (!(item_flags & layer))
4620                         return rte_flow_error_set(error, EINVAL,
4621                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4622                                                   NULL,
4623                                                   "no ipv6 item in pattern");
4624         }
4625         return ret;
4626 }
4627
4628 /**
4629  * Validate the modify-header TP actions.
4630  *
4631  * @param[in] action_flags
4632  *   Holds the actions detected until now.
4633  * @param[in] action
4634  *   Pointer to the modify action.
4635  * @param[in] item_flags
4636  *   Holds the items detected.
4637  * @param[out] error
4638  *   Pointer to error structure.
4639  *
4640  * @return
4641  *   0 on success, a negative errno value otherwise and rte_errno is set.
4642  */
4643 static int
4644 flow_dv_validate_action_modify_tp(const uint64_t action_flags,
4645                                   const struct rte_flow_action *action,
4646                                   const uint64_t item_flags,
4647                                   struct rte_flow_error *error)
4648 {
4649         int ret = 0;
4650         uint64_t layer;
4651
4652         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
4653         if (!ret) {
4654                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
4655                                  MLX5_FLOW_LAYER_INNER_L4 :
4656                                  MLX5_FLOW_LAYER_OUTER_L4;
4657                 if (!(item_flags & layer))
4658                         return rte_flow_error_set(error, EINVAL,
4659                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4660                                                   NULL, "no transport layer "
4661                                                   "in pattern");
4662         }
4663         return ret;
4664 }
4665
4666 /**
4667  * Validate the modify-header actions of increment/decrement
4668  * TCP Sequence-number.
4669  *
4670  * @param[in] action_flags
4671  *   Holds the actions detected until now.
4672  * @param[in] action
4673  *   Pointer to the modify action.
4674  * @param[in] item_flags
4675  *   Holds the items detected.
4676  * @param[out] error
4677  *   Pointer to error structure.
4678  *
4679  * @return
4680  *   0 on success, a negative errno value otherwise and rte_errno is set.
4681  */
4682 static int
4683 flow_dv_validate_action_modify_tcp_seq(const uint64_t action_flags,
4684                                        const struct rte_flow_action *action,
4685                                        const uint64_t item_flags,
4686                                        struct rte_flow_error *error)
4687 {
4688         int ret = 0;
4689         uint64_t layer;
4690
4691         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
4692         if (!ret) {
4693                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
4694                                  MLX5_FLOW_LAYER_INNER_L4_TCP :
4695                                  MLX5_FLOW_LAYER_OUTER_L4_TCP;
4696                 if (!(item_flags & layer))
4697                         return rte_flow_error_set(error, EINVAL,
4698                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4699                                                   NULL, "no TCP item in"
4700                                                   " pattern");
4701                 if ((action->type == RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ &&
4702                         (action_flags & MLX5_FLOW_ACTION_DEC_TCP_SEQ)) ||
4703                     (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ &&
4704                         (action_flags & MLX5_FLOW_ACTION_INC_TCP_SEQ)))
4705                         return rte_flow_error_set(error, EINVAL,
4706                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4707                                                   NULL,
4708                                                   "cannot decrease and increase"
4709                                                   " TCP sequence number"
4710                                                   " at the same time");
4711         }
4712         return ret;
4713 }
4714
4715 /**
4716  * Validate the modify-header actions of increment/decrement
4717  * TCP Acknowledgment number.
4718  *
4719  * @param[in] action_flags
4720  *   Holds the actions detected until now.
4721  * @param[in] action
4722  *   Pointer to the modify action.
4723  * @param[in] item_flags
4724  *   Holds the items detected.
4725  * @param[out] error
4726  *   Pointer to error structure.
4727  *
4728  * @return
4729  *   0 on success, a negative errno value otherwise and rte_errno is set.
4730  */
4731 static int
4732 flow_dv_validate_action_modify_tcp_ack(const uint64_t action_flags,
4733                                        const struct rte_flow_action *action,
4734                                        const uint64_t item_flags,
4735                                        struct rte_flow_error *error)
4736 {
4737         int ret = 0;
4738         uint64_t layer;
4739
4740         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
4741         if (!ret) {
4742                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
4743                                  MLX5_FLOW_LAYER_INNER_L4_TCP :
4744                                  MLX5_FLOW_LAYER_OUTER_L4_TCP;
4745                 if (!(item_flags & layer))
4746                         return rte_flow_error_set(error, EINVAL,
4747                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4748                                                   NULL, "no TCP item in"
4749                                                   " pattern");
4750                 if ((action->type == RTE_FLOW_ACTION_TYPE_INC_TCP_ACK &&
4751                         (action_flags & MLX5_FLOW_ACTION_DEC_TCP_ACK)) ||
4752                     (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK &&
4753                         (action_flags & MLX5_FLOW_ACTION_INC_TCP_ACK)))
4754                         return rte_flow_error_set(error, EINVAL,
4755                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4756                                                   NULL,
4757                                                   "cannot decrease and increase"
4758                                                   " TCP acknowledgment number"
4759                                                   " at the same time");
4760         }
4761         return ret;
4762 }
4763
4764 /**
4765  * Validate the modify-header TTL actions.
4766  *
4767  * @param[in] action_flags
4768  *   Holds the actions detected until now.
4769  * @param[in] action
4770  *   Pointer to the modify action.
4771  * @param[in] item_flags
4772  *   Holds the items detected.
4773  * @param[out] error
4774  *   Pointer to error structure.
4775  *
4776  * @return
4777  *   0 on success, a negative errno value otherwise and rte_errno is set.
4778  */
4779 static int
4780 flow_dv_validate_action_modify_ttl(const uint64_t action_flags,
4781                                    const struct rte_flow_action *action,
4782                                    const uint64_t item_flags,
4783                                    struct rte_flow_error *error)
4784 {
4785         int ret = 0;
4786         uint64_t layer;
4787
4788         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
4789         if (!ret) {
4790                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
4791                                  MLX5_FLOW_LAYER_INNER_L3 :
4792                                  MLX5_FLOW_LAYER_OUTER_L3;
4793                 if (!(item_flags & layer))
4794                         return rte_flow_error_set(error, EINVAL,
4795                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4796                                                   NULL,
4797                                                   "no IP protocol in pattern");
4798         }
4799         return ret;
4800 }
4801
4802 /**
4803  * Validate the generic modify field actions.
4804  * @param[in] dev
4805  *   Pointer to the rte_eth_dev structure.
4806  * @param[in] action_flags
4807  *   Holds the actions detected until now.
4808  * @param[in] action
4809  *   Pointer to the modify action.
4810  * @param[in] attr
4811  *   Pointer to the flow attributes.
4812  * @param[out] error
4813  *   Pointer to error structure.
4814  *
4815  * @return
4816  *   Number of header fields to modify (0 or more) on success,
4817  *   a negative errno value otherwise and rte_errno is set.
4818  */
4819 static int
4820 flow_dv_validate_action_modify_field(struct rte_eth_dev *dev,
4821                                    const uint64_t action_flags,
4822                                    const struct rte_flow_action *action,
4823                                    const struct rte_flow_attr *attr,
4824                                    struct rte_flow_error *error)
4825 {
4826         int ret = 0;
4827         struct mlx5_priv *priv = dev->data->dev_private;
4828         struct mlx5_dev_config *config = &priv->config;
4829         const struct rte_flow_action_modify_field *action_modify_field =
4830                 action->conf;
4831         uint32_t dst_width = mlx5_flow_item_field_width(dev,
4832                                 action_modify_field->dst.field,
4833                                 -1, attr, error);
4834         uint32_t src_width = mlx5_flow_item_field_width(dev,
4835                                 action_modify_field->src.field,
4836                                 dst_width, attr, error);
4837
4838         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
4839         if (ret)
4840                 return ret;
4841
4842         if (action_modify_field->width == 0)
4843                 return rte_flow_error_set(error, EINVAL,
4844                                 RTE_FLOW_ERROR_TYPE_ACTION, action,
4845                                 "no bits are requested to be modified");
4846         else if (action_modify_field->width > dst_width ||
4847                  action_modify_field->width > src_width)
4848                 return rte_flow_error_set(error, EINVAL,
4849                                 RTE_FLOW_ERROR_TYPE_ACTION, action,
4850                                 "cannot modify more bits than"
4851                                 " the width of a field");
4852         if (action_modify_field->dst.field != RTE_FLOW_FIELD_VALUE &&
4853             action_modify_field->dst.field != RTE_FLOW_FIELD_POINTER) {
4854                 if ((action_modify_field->dst.offset +
4855                      action_modify_field->width > dst_width) ||
4856                     (action_modify_field->dst.offset % 32))
4857                         return rte_flow_error_set(error, EINVAL,
4858                                         RTE_FLOW_ERROR_TYPE_ACTION, action,
4859                                         "destination offset is too big"
4860                                         " or not aligned to 4 bytes");
4861                 if (action_modify_field->dst.level &&
4862                     action_modify_field->dst.field != RTE_FLOW_FIELD_TAG)
4863                         return rte_flow_error_set(error, ENOTSUP,
4864                                         RTE_FLOW_ERROR_TYPE_ACTION, action,
4865                                         "inner header fields modification"
4866                                         " is not supported");
4867         }
4868         if (action_modify_field->src.field != RTE_FLOW_FIELD_VALUE &&
4869             action_modify_field->src.field != RTE_FLOW_FIELD_POINTER) {
4870                 if (!attr->transfer && !attr->group)
4871                         return rte_flow_error_set(error, ENOTSUP,
4872                                         RTE_FLOW_ERROR_TYPE_ACTION, action,
4873                                         "modify field action is not"
4874                                         " supported for group 0");
4875                 if ((action_modify_field->src.offset +
4876                      action_modify_field->width > src_width) ||
4877                     (action_modify_field->src.offset % 32))
4878                         return rte_flow_error_set(error, EINVAL,
4879                                         RTE_FLOW_ERROR_TYPE_ACTION, action,
4880                                         "source offset is too big"
4881                                         " or not aligned to 4 bytes");
4882                 if (action_modify_field->src.level &&
4883                     action_modify_field->src.field != RTE_FLOW_FIELD_TAG)
4884                         return rte_flow_error_set(error, ENOTSUP,
4885                                         RTE_FLOW_ERROR_TYPE_ACTION, action,
4886                                         "inner header fields modification"
4887                                         " is not supported");
4888         }
4889         if ((action_modify_field->dst.field ==
4890              action_modify_field->src.field) &&
4891             (action_modify_field->dst.level ==
4892              action_modify_field->src.level))
4893                 return rte_flow_error_set(error, EINVAL,
4894                                 RTE_FLOW_ERROR_TYPE_ACTION, action,
4895                                 "source and destination fields"
4896                                 " cannot be the same");
4897         if (action_modify_field->dst.field == RTE_FLOW_FIELD_VALUE ||
4898             action_modify_field->dst.field == RTE_FLOW_FIELD_POINTER ||
4899             action_modify_field->dst.field == RTE_FLOW_FIELD_MARK)
4900                 return rte_flow_error_set(error, EINVAL,
4901                                 RTE_FLOW_ERROR_TYPE_ACTION, action,
4902                                 "mark, immediate value or a pointer to it"
4903                                 " cannot be used as a destination");
4904         if (action_modify_field->dst.field == RTE_FLOW_FIELD_START ||
4905             action_modify_field->src.field == RTE_FLOW_FIELD_START)
4906                 return rte_flow_error_set(error, ENOTSUP,
4907                                 RTE_FLOW_ERROR_TYPE_ACTION, action,
4908                                 "modifications of an arbitrary"
4909                                 " place in a packet is not supported");
4910         if (action_modify_field->dst.field == RTE_FLOW_FIELD_VLAN_TYPE ||
4911             action_modify_field->src.field == RTE_FLOW_FIELD_VLAN_TYPE)
4912                 return rte_flow_error_set(error, ENOTSUP,
4913                                 RTE_FLOW_ERROR_TYPE_ACTION, action,
4914                                 "modifications of the 802.1Q Tag"
4915                                 " Identifier is not supported");
4916         if (action_modify_field->dst.field == RTE_FLOW_FIELD_VXLAN_VNI ||
4917             action_modify_field->src.field == RTE_FLOW_FIELD_VXLAN_VNI)
4918                 return rte_flow_error_set(error, ENOTSUP,
4919                                 RTE_FLOW_ERROR_TYPE_ACTION, action,
4920                                 "modifications of the VXLAN Network"
4921                                 " Identifier is not supported");
4922         if (action_modify_field->dst.field == RTE_FLOW_FIELD_GENEVE_VNI ||
4923             action_modify_field->src.field == RTE_FLOW_FIELD_GENEVE_VNI)
4924                 return rte_flow_error_set(error, ENOTSUP,
4925                                 RTE_FLOW_ERROR_TYPE_ACTION, action,
4926                                 "modifications of the GENEVE Network"
4927                                 " Identifier is not supported");
4928         if (action_modify_field->dst.field == RTE_FLOW_FIELD_MARK ||
4929             action_modify_field->src.field == RTE_FLOW_FIELD_MARK)
4930                 if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY ||
4931                     !mlx5_flow_ext_mreg_supported(dev))
4932                         return rte_flow_error_set(error, ENOTSUP,
4933                                         RTE_FLOW_ERROR_TYPE_ACTION, action,
4934                                         "cannot modify mark in legacy mode"
4935                                         " or without extensive registers");
4936         if (action_modify_field->dst.field == RTE_FLOW_FIELD_META ||
4937             action_modify_field->src.field == RTE_FLOW_FIELD_META) {
4938                 if (config->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
4939                     !mlx5_flow_ext_mreg_supported(dev))
4940                         return rte_flow_error_set(error, ENOTSUP,
4941                                         RTE_FLOW_ERROR_TYPE_ACTION, action,
4942                                         "cannot modify meta without"
4943                                         " extensive registers support");
4944                 ret = flow_dv_get_metadata_reg(dev, attr, error);
4945                 if (ret < 0 || ret == REG_NON)
4946                         return rte_flow_error_set(error, ENOTSUP,
4947                                         RTE_FLOW_ERROR_TYPE_ACTION, action,
4948                                         "cannot modify meta without"
4949                                         " extensive registers available");
4950         }
4951         if (action_modify_field->operation != RTE_FLOW_MODIFY_SET)
4952                 return rte_flow_error_set(error, ENOTSUP,
4953                                 RTE_FLOW_ERROR_TYPE_ACTION, action,
4954                                 "add and sub operations"
4955                                 " are not supported");
4956         return (action_modify_field->width / 32) +
4957                !!(action_modify_field->width % 32);
4958 }
4959
4960 /**
4961  * Validate jump action.
4962  *
4963  * @param[in] action
4964  *   Pointer to the jump action.
4965  * @param[in] action_flags
4966  *   Holds the actions detected until now.
4967  * @param[in] attributes
4968  *   Pointer to flow attributes
4969  * @param[in] external
4970  *   Action belongs to flow rule created by request external to PMD.
4971  * @param[out] error
4972  *   Pointer to error structure.
4973  *
4974  * @return
4975  *   0 on success, a negative errno value otherwise and rte_errno is set.
4976  */
4977 static int
4978 flow_dv_validate_action_jump(struct rte_eth_dev *dev,
4979                              const struct mlx5_flow_tunnel *tunnel,
4980                              const struct rte_flow_action *action,
4981                              uint64_t action_flags,
4982                              const struct rte_flow_attr *attributes,
4983                              bool external, struct rte_flow_error *error)
4984 {
4985         uint32_t target_group, table;
4986         int ret = 0;
4987         struct flow_grp_info grp_info = {
4988                 .external = !!external,
4989                 .transfer = !!attributes->transfer,
4990                 .fdb_def_rule = 1,
4991                 .std_tbl_fix = 0
4992         };
4993         if (action_flags & (MLX5_FLOW_FATE_ACTIONS |
4994                             MLX5_FLOW_FATE_ESWITCH_ACTIONS))
4995                 return rte_flow_error_set(error, EINVAL,
4996                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
4997                                           "can't have 2 fate actions in"
4998                                           " same flow");
4999         if (!action->conf)
5000                 return rte_flow_error_set(error, EINVAL,
5001                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
5002                                           NULL, "action configuration not set");
5003         target_group =
5004                 ((const struct rte_flow_action_jump *)action->conf)->group;
5005         ret = mlx5_flow_group_to_table(dev, tunnel, target_group, &table,
5006                                        &grp_info, error);
5007         if (ret)
5008                 return ret;
5009         if (attributes->group == target_group &&
5010             !(action_flags & (MLX5_FLOW_ACTION_TUNNEL_SET |
5011                               MLX5_FLOW_ACTION_TUNNEL_MATCH)))
5012                 return rte_flow_error_set(error, EINVAL,
5013                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5014                                           "target group must be other than"
5015                                           " the current flow group");
5016         return 0;
5017 }
5018
5019 /*
5020  * Validate action PORT_ID / REPRESENTED_PORT.
5021  *
5022  * @param[in] dev
5023  *   Pointer to rte_eth_dev structure.
5024  * @param[in] action_flags
5025  *   Bit-fields that holds the actions detected until now.
5026  * @param[in] action
5027  *   PORT_ID / REPRESENTED_PORT action structure.
5028  * @param[in] attr
5029  *   Attributes of flow that includes this action.
5030  * @param[out] error
5031  *   Pointer to error structure.
5032  *
5033  * @return
5034  *   0 on success, a negative errno value otherwise and rte_errno is set.
5035  */
5036 static int
5037 flow_dv_validate_action_port_id(struct rte_eth_dev *dev,
5038                                 uint64_t action_flags,
5039                                 const struct rte_flow_action *action,
5040                                 const struct rte_flow_attr *attr,
5041                                 struct rte_flow_error *error)
5042 {
5043         const struct rte_flow_action_port_id *port_id;
5044         const struct rte_flow_action_ethdev *ethdev;
5045         struct mlx5_priv *act_priv;
5046         struct mlx5_priv *dev_priv;
5047         uint16_t port;
5048
5049         if (!attr->transfer)
5050                 return rte_flow_error_set(error, ENOTSUP,
5051                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5052                                           NULL,
5053                                           "port action is valid in transfer"
5054                                           " mode only");
5055         if (!action || !action->conf)
5056                 return rte_flow_error_set(error, ENOTSUP,
5057                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
5058                                           NULL,
5059                                           "port action parameters must be"
5060                                           " specified");
5061         if (action_flags & (MLX5_FLOW_FATE_ACTIONS |
5062                             MLX5_FLOW_FATE_ESWITCH_ACTIONS))
5063                 return rte_flow_error_set(error, EINVAL,
5064                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5065                                           "can have only one fate actions in"
5066                                           " a flow");
5067         dev_priv = mlx5_dev_to_eswitch_info(dev);
5068         if (!dev_priv)
5069                 return rte_flow_error_set(error, rte_errno,
5070                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5071                                           NULL,
5072                                           "failed to obtain E-Switch info");
5073         switch (action->type) {
5074         case RTE_FLOW_ACTION_TYPE_PORT_ID:
5075                 port_id = action->conf;
5076                 port = port_id->original ? dev->data->port_id : port_id->id;
5077                 break;
5078         case RTE_FLOW_ACTION_TYPE_REPRESENTED_PORT:
5079                 ethdev = action->conf;
5080                 port = ethdev->port_id;
5081                 break;
5082         default:
5083                 MLX5_ASSERT(false);
5084                 return rte_flow_error_set
5085                                 (error, EINVAL,
5086                                  RTE_FLOW_ERROR_TYPE_ACTION, action,
5087                                  "unknown E-Switch action");
5088         }
5089         act_priv = mlx5_port_to_eswitch_info(port, false);
5090         if (!act_priv)
5091                 return rte_flow_error_set
5092                                 (error, rte_errno,
5093                                  RTE_FLOW_ERROR_TYPE_ACTION_CONF, action->conf,
5094                                  "failed to obtain E-Switch port id for port");
5095         if (act_priv->domain_id != dev_priv->domain_id)
5096                 return rte_flow_error_set
5097                                 (error, EINVAL,
5098                                  RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5099                                  "port does not belong to"
5100                                  " E-Switch being configured");
5101         return 0;
5102 }
5103
5104 /**
5105  * Get the maximum number of modify header actions.
5106  *
5107  * @param dev
5108  *   Pointer to rte_eth_dev structure.
5109  * @param root
5110  *   Whether action is on root table.
5111  *
5112  * @return
5113  *   Max number of modify header actions device can support.
5114  */
5115 static inline unsigned int
5116 flow_dv_modify_hdr_action_max(struct rte_eth_dev *dev __rte_unused,
5117                               bool root)
5118 {
5119         /*
5120          * There's no way to directly query the max capacity from FW.
5121          * The maximal value on root table should be assumed to be supported.
5122          */
5123         if (!root)
5124                 return MLX5_MAX_MODIFY_NUM;
5125         else
5126                 return MLX5_ROOT_TBL_MODIFY_NUM;
5127 }
5128
5129 /**
5130  * Validate the meter action.
5131  *
5132  * @param[in] dev
5133  *   Pointer to rte_eth_dev structure.
5134  * @param[in] action_flags
5135  *   Bit-fields that holds the actions detected until now.
5136  * @param[in] item_flags
5137  *   Holds the items detected.
5138  * @param[in] action
5139  *   Pointer to the meter action.
5140  * @param[in] attr
5141  *   Attributes of flow that includes this action.
5142  * @param[in] port_id_item
5143  *   Pointer to item indicating port id.
5144  * @param[out] error
5145  *   Pointer to error structure.
5146  *
5147  * @return
5148  *   0 on success, a negative errno value otherwise and rte_ernno is set.
5149  */
5150 static int
5151 mlx5_flow_validate_action_meter(struct rte_eth_dev *dev,
5152                                 uint64_t action_flags, uint64_t item_flags,
5153                                 const struct rte_flow_action *action,
5154                                 const struct rte_flow_attr *attr,
5155                                 const struct rte_flow_item *port_id_item,
5156                                 bool *def_policy,
5157                                 struct rte_flow_error *error)
5158 {
5159         struct mlx5_priv *priv = dev->data->dev_private;
5160         const struct rte_flow_action_meter *am = action->conf;
5161         struct mlx5_flow_meter_info *fm;
5162         struct mlx5_flow_meter_policy *mtr_policy;
5163         struct mlx5_flow_mtr_mng *mtrmng = priv->sh->mtrmng;
5164
5165         if (!am)
5166                 return rte_flow_error_set(error, EINVAL,
5167                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5168                                           "meter action conf is NULL");
5169
5170         if (action_flags & MLX5_FLOW_ACTION_METER)
5171                 return rte_flow_error_set(error, ENOTSUP,
5172                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5173                                           "meter chaining not support");
5174         if (action_flags & MLX5_FLOW_ACTION_JUMP)
5175                 return rte_flow_error_set(error, ENOTSUP,
5176                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5177                                           "meter with jump not support");
5178         if (!priv->mtr_en)
5179                 return rte_flow_error_set(error, ENOTSUP,
5180                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5181                                           NULL,
5182                                           "meter action not supported");
5183         fm = mlx5_flow_meter_find(priv, am->mtr_id, NULL);
5184         if (!fm)
5185                 return rte_flow_error_set(error, EINVAL,
5186                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5187                                           "Meter not found");
5188         /* aso meter can always be shared by different domains */
5189         if (fm->ref_cnt && !priv->sh->meter_aso_en &&
5190             !(fm->transfer == attr->transfer ||
5191               (!fm->ingress && !attr->ingress && attr->egress) ||
5192               (!fm->egress && !attr->egress && attr->ingress)))
5193                 return rte_flow_error_set(error, EINVAL,
5194                         RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5195                         "Flow attributes domain are either invalid "
5196                         "or have a domain conflict with current "
5197                         "meter attributes");
5198         if (fm->def_policy) {
5199                 if (!((attr->transfer &&
5200                         mtrmng->def_policy[MLX5_MTR_DOMAIN_TRANSFER]) ||
5201                         (attr->egress &&
5202                         mtrmng->def_policy[MLX5_MTR_DOMAIN_EGRESS]) ||
5203                         (attr->ingress &&
5204                         mtrmng->def_policy[MLX5_MTR_DOMAIN_INGRESS])))
5205                         return rte_flow_error_set(error, EINVAL,
5206                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5207                                           "Flow attributes domain "
5208                                           "have a conflict with current "
5209                                           "meter domain attributes");
5210                 *def_policy = true;
5211         } else {
5212                 mtr_policy = mlx5_flow_meter_policy_find(dev,
5213                                                 fm->policy_id, NULL);
5214                 if (!mtr_policy)
5215                         return rte_flow_error_set(error, EINVAL,
5216                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5217                                           "Invalid policy id for meter ");
5218                 if (!((attr->transfer && mtr_policy->transfer) ||
5219                         (attr->egress && mtr_policy->egress) ||
5220                         (attr->ingress && mtr_policy->ingress)))
5221                         return rte_flow_error_set(error, EINVAL,
5222                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5223                                           "Flow attributes domain "
5224                                           "have a conflict with current "
5225                                           "meter domain attributes");
5226                 if (attr->transfer && mtr_policy->dev) {
5227                         /**
5228                          * When policy has fate action of port_id,
5229                          * the flow should have the same src port as policy.
5230                          */
5231                         struct mlx5_priv *policy_port_priv =
5232                                         mtr_policy->dev->data->dev_private;
5233                         int32_t flow_src_port = priv->representor_id;
5234
5235                         if (port_id_item) {
5236                                 const struct rte_flow_item_port_id *spec =
5237                                                         port_id_item->spec;
5238                                 struct mlx5_priv *port_priv =
5239                                         mlx5_port_to_eswitch_info(spec->id,
5240                                                                   false);
5241                                 if (!port_priv)
5242                                         return rte_flow_error_set(error,
5243                                                 rte_errno,
5244                                                 RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
5245                                                 spec,
5246                                                 "Failed to get port info.");
5247                                 flow_src_port = port_priv->representor_id;
5248                         }
5249                         if (flow_src_port != policy_port_priv->representor_id)
5250                                 return rte_flow_error_set(error,
5251                                                 rte_errno,
5252                                                 RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
5253                                                 NULL,
5254                                                 "Flow and meter policy "
5255                                                 "have different src port.");
5256                 } else if (mtr_policy->is_rss) {
5257                         struct mlx5_flow_meter_policy *fp;
5258                         struct mlx5_meter_policy_action_container *acg;
5259                         struct mlx5_meter_policy_action_container *acy;
5260                         const struct rte_flow_action *rss_act;
5261                         int ret;
5262
5263                         fp = mlx5_flow_meter_hierarchy_get_final_policy(dev,
5264                                                                 mtr_policy);
5265                         if (fp == NULL)
5266                                 return rte_flow_error_set(error, EINVAL,
5267                                         RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5268                                                   "Unable to get the final "
5269                                                   "policy in the hierarchy");
5270                         acg = &fp->act_cnt[RTE_COLOR_GREEN];
5271                         acy = &fp->act_cnt[RTE_COLOR_YELLOW];
5272                         MLX5_ASSERT(acg->fate_action ==
5273                                     MLX5_FLOW_FATE_SHARED_RSS ||
5274                                     acy->fate_action ==
5275                                     MLX5_FLOW_FATE_SHARED_RSS);
5276                         if (acg->fate_action == MLX5_FLOW_FATE_SHARED_RSS)
5277                                 rss_act = acg->rss;
5278                         else
5279                                 rss_act = acy->rss;
5280                         ret = mlx5_flow_validate_action_rss(rss_act,
5281                                         action_flags, dev, attr,
5282                                         item_flags, error);
5283                         if (ret)
5284                                 return ret;
5285                 }
5286                 *def_policy = false;
5287         }
5288         return 0;
5289 }
5290
5291 /**
5292  * Validate the age action.
5293  *
5294  * @param[in] action_flags
5295  *   Holds the actions detected until now.
5296  * @param[in] action
5297  *   Pointer to the age action.
5298  * @param[in] dev
5299  *   Pointer to the Ethernet device structure.
5300  * @param[out] error
5301  *   Pointer to error structure.
5302  *
5303  * @return
5304  *   0 on success, a negative errno value otherwise and rte_errno is set.
5305  */
5306 static int
5307 flow_dv_validate_action_age(uint64_t action_flags,
5308                             const struct rte_flow_action *action,
5309                             struct rte_eth_dev *dev,
5310                             struct rte_flow_error *error)
5311 {
5312         struct mlx5_priv *priv = dev->data->dev_private;
5313         const struct rte_flow_action_age *age = action->conf;
5314
5315         if (!priv->sh->devx || (priv->sh->cmng.counter_fallback &&
5316             !priv->sh->aso_age_mng))
5317                 return rte_flow_error_set(error, ENOTSUP,
5318                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5319                                           NULL,
5320                                           "age action not supported");
5321         if (!(action->conf))
5322                 return rte_flow_error_set(error, EINVAL,
5323                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
5324                                           "configuration cannot be null");
5325         if (!(age->timeout))
5326                 return rte_flow_error_set(error, EINVAL,
5327                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
5328                                           "invalid timeout value 0");
5329         if (action_flags & MLX5_FLOW_ACTION_AGE)
5330                 return rte_flow_error_set(error, EINVAL,
5331                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5332                                           "duplicate age actions set");
5333         return 0;
5334 }
5335
5336 /**
5337  * Validate the modify-header IPv4 DSCP actions.
5338  *
5339  * @param[in] action_flags
5340  *   Holds the actions detected until now.
5341  * @param[in] action
5342  *   Pointer to the modify action.
5343  * @param[in] item_flags
5344  *   Holds the items detected.
5345  * @param[out] error
5346  *   Pointer to error structure.
5347  *
5348  * @return
5349  *   0 on success, a negative errno value otherwise and rte_errno is set.
5350  */
5351 static int
5352 flow_dv_validate_action_modify_ipv4_dscp(const uint64_t action_flags,
5353                                          const struct rte_flow_action *action,
5354                                          const uint64_t item_flags,
5355                                          struct rte_flow_error *error)
5356 {
5357         int ret = 0;
5358
5359         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
5360         if (!ret) {
5361                 if (!(item_flags & MLX5_FLOW_LAYER_L3_IPV4))
5362                         return rte_flow_error_set(error, EINVAL,
5363                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5364                                                   NULL,
5365                                                   "no ipv4 item in pattern");
5366         }
5367         return ret;
5368 }
5369
5370 /**
5371  * Validate the modify-header IPv6 DSCP actions.
5372  *
5373  * @param[in] action_flags
5374  *   Holds the actions detected until now.
5375  * @param[in] action
5376  *   Pointer to the modify action.
5377  * @param[in] item_flags
5378  *   Holds the items detected.
5379  * @param[out] error
5380  *   Pointer to error structure.
5381  *
5382  * @return
5383  *   0 on success, a negative errno value otherwise and rte_errno is set.
5384  */
5385 static int
5386 flow_dv_validate_action_modify_ipv6_dscp(const uint64_t action_flags,
5387                                          const struct rte_flow_action *action,
5388                                          const uint64_t item_flags,
5389                                          struct rte_flow_error *error)
5390 {
5391         int ret = 0;
5392
5393         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
5394         if (!ret) {
5395                 if (!(item_flags & MLX5_FLOW_LAYER_L3_IPV6))
5396                         return rte_flow_error_set(error, EINVAL,
5397                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5398                                                   NULL,
5399                                                   "no ipv6 item in pattern");
5400         }
5401         return ret;
5402 }
5403
5404 int
5405 flow_dv_modify_match_cb(void *tool_ctx __rte_unused,
5406                         struct mlx5_list_entry *entry, void *cb_ctx)
5407 {
5408         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
5409         struct mlx5_flow_dv_modify_hdr_resource *ref = ctx->data;
5410         struct mlx5_flow_dv_modify_hdr_resource *resource =
5411                                   container_of(entry, typeof(*resource), entry);
5412         uint32_t key_len = sizeof(*ref) - offsetof(typeof(*ref), ft_type);
5413
5414         key_len += ref->actions_num * sizeof(ref->actions[0]);
5415         return ref->actions_num != resource->actions_num ||
5416                memcmp(&ref->ft_type, &resource->ft_type, key_len);
5417 }
5418
5419 static struct mlx5_indexed_pool *
5420 flow_dv_modify_ipool_get(struct mlx5_dev_ctx_shared *sh, uint8_t index)
5421 {
5422         struct mlx5_indexed_pool *ipool = __atomic_load_n
5423                                      (&sh->mdh_ipools[index], __ATOMIC_SEQ_CST);
5424
5425         if (!ipool) {
5426                 struct mlx5_indexed_pool *expected = NULL;
5427                 struct mlx5_indexed_pool_config cfg =
5428                     (struct mlx5_indexed_pool_config) {
5429                        .size = sizeof(struct mlx5_flow_dv_modify_hdr_resource) +
5430                                                                    (index + 1) *
5431                                            sizeof(struct mlx5_modification_cmd),
5432                        .trunk_size = 64,
5433                        .grow_trunk = 3,
5434                        .grow_shift = 2,
5435                        .need_lock = 1,
5436                        .release_mem_en = !!sh->reclaim_mode,
5437                        .per_core_cache = sh->reclaim_mode ? 0 : (1 << 16),
5438                        .malloc = mlx5_malloc,
5439                        .free = mlx5_free,
5440                        .type = "mlx5_modify_action_resource",
5441                 };
5442
5443                 cfg.size = RTE_ALIGN(cfg.size, sizeof(ipool));
5444                 ipool = mlx5_ipool_create(&cfg);
5445                 if (!ipool)
5446                         return NULL;
5447                 if (!__atomic_compare_exchange_n(&sh->mdh_ipools[index],
5448                                                  &expected, ipool, false,
5449                                                  __ATOMIC_SEQ_CST,
5450                                                  __ATOMIC_SEQ_CST)) {
5451                         mlx5_ipool_destroy(ipool);
5452                         ipool = __atomic_load_n(&sh->mdh_ipools[index],
5453                                                 __ATOMIC_SEQ_CST);
5454                 }
5455         }
5456         return ipool;
5457 }
5458
5459 struct mlx5_list_entry *
5460 flow_dv_modify_create_cb(void *tool_ctx, void *cb_ctx)
5461 {
5462         struct mlx5_dev_ctx_shared *sh = tool_ctx;
5463         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
5464         struct mlx5dv_dr_domain *ns;
5465         struct mlx5_flow_dv_modify_hdr_resource *entry;
5466         struct mlx5_flow_dv_modify_hdr_resource *ref = ctx->data;
5467         struct mlx5_indexed_pool *ipool = flow_dv_modify_ipool_get(sh,
5468                                                           ref->actions_num - 1);
5469         int ret;
5470         uint32_t data_len = ref->actions_num * sizeof(ref->actions[0]);
5471         uint32_t key_len = sizeof(*ref) - offsetof(typeof(*ref), ft_type);
5472         uint32_t idx;
5473
5474         if (unlikely(!ipool)) {
5475                 rte_flow_error_set(ctx->error, ENOMEM,
5476                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5477                                    NULL, "cannot allocate modify ipool");
5478                 return NULL;
5479         }
5480         entry = mlx5_ipool_zmalloc(ipool, &idx);
5481         if (!entry) {
5482                 rte_flow_error_set(ctx->error, ENOMEM,
5483                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5484                                    "cannot allocate resource memory");
5485                 return NULL;
5486         }
5487         rte_memcpy(&entry->ft_type,
5488                    RTE_PTR_ADD(ref, offsetof(typeof(*ref), ft_type)),
5489                    key_len + data_len);
5490         if (entry->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
5491                 ns = sh->fdb_domain;
5492         else if (entry->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_TX)
5493                 ns = sh->tx_domain;
5494         else
5495                 ns = sh->rx_domain;
5496         ret = mlx5_flow_os_create_flow_action_modify_header
5497                                         (sh->cdev->ctx, ns, entry,
5498                                          data_len, &entry->action);
5499         if (ret) {
5500                 mlx5_ipool_free(sh->mdh_ipools[ref->actions_num - 1], idx);
5501                 rte_flow_error_set(ctx->error, ENOMEM,
5502                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5503                                    NULL, "cannot create modification action");
5504                 return NULL;
5505         }
5506         entry->idx = idx;
5507         return &entry->entry;
5508 }
5509
5510 struct mlx5_list_entry *
5511 flow_dv_modify_clone_cb(void *tool_ctx, struct mlx5_list_entry *oentry,
5512                         void *cb_ctx)
5513 {
5514         struct mlx5_dev_ctx_shared *sh = tool_ctx;
5515         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
5516         struct mlx5_flow_dv_modify_hdr_resource *entry;
5517         struct mlx5_flow_dv_modify_hdr_resource *ref = ctx->data;
5518         uint32_t data_len = ref->actions_num * sizeof(ref->actions[0]);
5519         uint32_t idx;
5520
5521         entry = mlx5_ipool_malloc(sh->mdh_ipools[ref->actions_num - 1],
5522                                   &idx);
5523         if (!entry) {
5524                 rte_flow_error_set(ctx->error, ENOMEM,
5525                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5526                                    "cannot allocate resource memory");
5527                 return NULL;
5528         }
5529         memcpy(entry, oentry, sizeof(*entry) + data_len);
5530         entry->idx = idx;
5531         return &entry->entry;
5532 }
5533
5534 void
5535 flow_dv_modify_clone_free_cb(void *tool_ctx, struct mlx5_list_entry *entry)
5536 {
5537         struct mlx5_dev_ctx_shared *sh = tool_ctx;
5538         struct mlx5_flow_dv_modify_hdr_resource *res =
5539                 container_of(entry, typeof(*res), entry);
5540
5541         mlx5_ipool_free(sh->mdh_ipools[res->actions_num - 1], res->idx);
5542 }
5543
5544 /**
5545  * Validate the sample action.
5546  *
5547  * @param[in, out] action_flags
5548  *   Holds the actions detected until now.
5549  * @param[in] action
5550  *   Pointer to the sample action.
5551  * @param[in] dev
5552  *   Pointer to the Ethernet device structure.
5553  * @param[in] attr
5554  *   Attributes of flow that includes this action.
5555  * @param[in] item_flags
5556  *   Holds the items detected.
5557  * @param[in] rss
5558  *   Pointer to the RSS action.
5559  * @param[out] sample_rss
5560  *   Pointer to the RSS action in sample action list.
5561  * @param[out] count
5562  *   Pointer to the COUNT action in sample action list.
5563  * @param[out] fdb_mirror_limit
5564  *   Pointer to the FDB mirror limitation flag.
5565  * @param[out] error
5566  *   Pointer to error structure.
5567  *
5568  * @return
5569  *   0 on success, a negative errno value otherwise and rte_errno is set.
5570  */
5571 static int
5572 flow_dv_validate_action_sample(uint64_t *action_flags,
5573                                const struct rte_flow_action *action,
5574                                struct rte_eth_dev *dev,
5575                                const struct rte_flow_attr *attr,
5576                                uint64_t item_flags,
5577                                const struct rte_flow_action_rss *rss,
5578                                const struct rte_flow_action_rss **sample_rss,
5579                                const struct rte_flow_action_count **count,
5580                                int *fdb_mirror_limit,
5581                                struct rte_flow_error *error)
5582 {
5583         struct mlx5_priv *priv = dev->data->dev_private;
5584         struct mlx5_dev_config *dev_conf = &priv->config;
5585         const struct rte_flow_action_sample *sample = action->conf;
5586         const struct rte_flow_action *act;
5587         uint64_t sub_action_flags = 0;
5588         uint16_t queue_index = 0xFFFF;
5589         int actions_n = 0;
5590         int ret;
5591
5592         if (!sample)
5593                 return rte_flow_error_set(error, EINVAL,
5594                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
5595                                           "configuration cannot be NULL");
5596         if (sample->ratio == 0)
5597                 return rte_flow_error_set(error, EINVAL,
5598                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
5599                                           "ratio value starts from 1");
5600         if (!priv->sh->devx || (sample->ratio > 0 && !priv->sampler_en))
5601                 return rte_flow_error_set(error, ENOTSUP,
5602                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5603                                           NULL,
5604                                           "sample action not supported");
5605         if (*action_flags & MLX5_FLOW_ACTION_SAMPLE)
5606                 return rte_flow_error_set(error, EINVAL,
5607                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5608                                           "Multiple sample actions not "
5609                                           "supported");
5610         if (*action_flags & MLX5_FLOW_ACTION_METER)
5611                 return rte_flow_error_set(error, EINVAL,
5612                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
5613                                           "wrong action order, meter should "
5614                                           "be after sample action");
5615         if (*action_flags & MLX5_FLOW_ACTION_JUMP)
5616                 return rte_flow_error_set(error, EINVAL,
5617                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
5618                                           "wrong action order, jump should "
5619                                           "be after sample action");
5620         if (*action_flags & MLX5_FLOW_ACTION_CT)
5621                 return rte_flow_error_set(error, EINVAL,
5622                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
5623                                           "Sample after CT not supported");
5624         act = sample->actions;
5625         for (; act->type != RTE_FLOW_ACTION_TYPE_END; act++) {
5626                 if (actions_n == MLX5_DV_MAX_NUMBER_OF_ACTIONS)
5627                         return rte_flow_error_set(error, ENOTSUP,
5628                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5629                                                   act, "too many actions");
5630                 switch (act->type) {
5631                 case RTE_FLOW_ACTION_TYPE_QUEUE:
5632                         ret = mlx5_flow_validate_action_queue(act,
5633                                                               sub_action_flags,
5634                                                               dev,
5635                                                               attr, error);
5636                         if (ret < 0)
5637                                 return ret;
5638                         queue_index = ((const struct rte_flow_action_queue *)
5639                                                         (act->conf))->index;
5640                         sub_action_flags |= MLX5_FLOW_ACTION_QUEUE;
5641                         ++actions_n;
5642                         break;
5643                 case RTE_FLOW_ACTION_TYPE_RSS:
5644                         *sample_rss = act->conf;
5645                         ret = mlx5_flow_validate_action_rss(act,
5646                                                             sub_action_flags,
5647                                                             dev, attr,
5648                                                             item_flags,
5649                                                             error);
5650                         if (ret < 0)
5651                                 return ret;
5652                         if (rss && *sample_rss &&
5653                             ((*sample_rss)->level != rss->level ||
5654                             (*sample_rss)->types != rss->types))
5655                                 return rte_flow_error_set(error, ENOTSUP,
5656                                         RTE_FLOW_ERROR_TYPE_ACTION,
5657                                         NULL,
5658                                         "Can't use the different RSS types "
5659                                         "or level in the same flow");
5660                         if (*sample_rss != NULL && (*sample_rss)->queue_num)
5661                                 queue_index = (*sample_rss)->queue[0];
5662                         sub_action_flags |= MLX5_FLOW_ACTION_RSS;
5663                         ++actions_n;
5664                         break;
5665                 case RTE_FLOW_ACTION_TYPE_MARK:
5666                         ret = flow_dv_validate_action_mark(dev, act,
5667                                                            sub_action_flags,
5668                                                            attr, error);
5669                         if (ret < 0)
5670                                 return ret;
5671                         if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY)
5672                                 sub_action_flags |= MLX5_FLOW_ACTION_MARK |
5673                                                 MLX5_FLOW_ACTION_MARK_EXT;
5674                         else
5675                                 sub_action_flags |= MLX5_FLOW_ACTION_MARK;
5676                         ++actions_n;
5677                         break;
5678                 case RTE_FLOW_ACTION_TYPE_COUNT:
5679                         ret = flow_dv_validate_action_count
5680                                 (dev, false, *action_flags | sub_action_flags,
5681                                  error);
5682                         if (ret < 0)
5683                                 return ret;
5684                         *count = act->conf;
5685                         sub_action_flags |= MLX5_FLOW_ACTION_COUNT;
5686                         *action_flags |= MLX5_FLOW_ACTION_COUNT;
5687                         ++actions_n;
5688                         break;
5689                 case RTE_FLOW_ACTION_TYPE_PORT_ID:
5690                 case RTE_FLOW_ACTION_TYPE_REPRESENTED_PORT:
5691                         ret = flow_dv_validate_action_port_id(dev,
5692                                                               sub_action_flags,
5693                                                               act,
5694                                                               attr,
5695                                                               error);
5696                         if (ret)
5697                                 return ret;
5698                         sub_action_flags |= MLX5_FLOW_ACTION_PORT_ID;
5699                         ++actions_n;
5700                         break;
5701                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
5702                         ret = flow_dv_validate_action_raw_encap_decap
5703                                 (dev, NULL, act->conf, attr, &sub_action_flags,
5704                                  &actions_n, action, item_flags, error);
5705                         if (ret < 0)
5706                                 return ret;
5707                         ++actions_n;
5708                         break;
5709                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
5710                 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
5711                         ret = flow_dv_validate_action_l2_encap(dev,
5712                                                                sub_action_flags,
5713                                                                act, attr,
5714                                                                error);
5715                         if (ret < 0)
5716                                 return ret;
5717                         sub_action_flags |= MLX5_FLOW_ACTION_ENCAP;
5718                         ++actions_n;
5719                         break;
5720                 default:
5721                         return rte_flow_error_set(error, ENOTSUP,
5722                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5723                                                   NULL,
5724                                                   "Doesn't support optional "
5725                                                   "action");
5726                 }
5727         }
5728         if (attr->ingress && !attr->transfer) {
5729                 if (!(sub_action_flags & (MLX5_FLOW_ACTION_QUEUE |
5730                                           MLX5_FLOW_ACTION_RSS)))
5731                         return rte_flow_error_set(error, EINVAL,
5732                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5733                                                   NULL,
5734                                                   "Ingress must has a dest "
5735                                                   "QUEUE for Sample");
5736         } else if (attr->egress && !attr->transfer) {
5737                 return rte_flow_error_set(error, ENOTSUP,
5738                                           RTE_FLOW_ERROR_TYPE_ACTION,
5739                                           NULL,
5740                                           "Sample Only support Ingress "
5741                                           "or E-Switch");
5742         } else if (sample->actions->type != RTE_FLOW_ACTION_TYPE_END) {
5743                 MLX5_ASSERT(attr->transfer);
5744                 if (sample->ratio > 1)
5745                         return rte_flow_error_set(error, ENOTSUP,
5746                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5747                                                   NULL,
5748                                                   "E-Switch doesn't support "
5749                                                   "any optional action "
5750                                                   "for sampling");
5751                 if (sub_action_flags & MLX5_FLOW_ACTION_QUEUE)
5752                         return rte_flow_error_set(error, ENOTSUP,
5753                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5754                                                   NULL,
5755                                                   "unsupported action QUEUE");
5756                 if (sub_action_flags & MLX5_FLOW_ACTION_RSS)
5757                         return rte_flow_error_set(error, ENOTSUP,
5758                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5759                                                   NULL,
5760                                                   "unsupported action QUEUE");
5761                 if (!(sub_action_flags & MLX5_FLOW_ACTION_PORT_ID))
5762                         return rte_flow_error_set(error, EINVAL,
5763                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5764                                                   NULL,
5765                                                   "E-Switch must has a dest "
5766                                                   "port for mirroring");
5767                 if (!priv->config.hca_attr.reg_c_preserve &&
5768                      priv->representor_id != UINT16_MAX)
5769                         *fdb_mirror_limit = 1;
5770         }
5771         /* Continue validation for Xcap actions.*/
5772         if ((sub_action_flags & MLX5_FLOW_XCAP_ACTIONS) &&
5773             (queue_index == 0xFFFF ||
5774              mlx5_rxq_get_type(dev, queue_index) != MLX5_RXQ_TYPE_HAIRPIN)) {
5775                 if ((sub_action_flags & MLX5_FLOW_XCAP_ACTIONS) ==
5776                      MLX5_FLOW_XCAP_ACTIONS)
5777                         return rte_flow_error_set(error, ENOTSUP,
5778                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5779                                                   NULL, "encap and decap "
5780                                                   "combination aren't "
5781                                                   "supported");
5782                 if (!attr->transfer && attr->ingress && (sub_action_flags &
5783                                                         MLX5_FLOW_ACTION_ENCAP))
5784                         return rte_flow_error_set(error, ENOTSUP,
5785                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5786                                                   NULL, "encap is not supported"
5787                                                   " for ingress traffic");
5788         }
5789         return 0;
5790 }
5791
5792 /**
5793  * Find existing modify-header resource or create and register a new one.
5794  *
5795  * @param dev[in, out]
5796  *   Pointer to rte_eth_dev structure.
5797  * @param[in, out] resource
5798  *   Pointer to modify-header resource.
5799  * @parm[in, out] dev_flow
5800  *   Pointer to the dev_flow.
5801  * @param[out] error
5802  *   pointer to error structure.
5803  *
5804  * @return
5805  *   0 on success otherwise -errno and errno is set.
5806  */
5807 static int
5808 flow_dv_modify_hdr_resource_register
5809                         (struct rte_eth_dev *dev,
5810                          struct mlx5_flow_dv_modify_hdr_resource *resource,
5811                          struct mlx5_flow *dev_flow,
5812                          struct rte_flow_error *error)
5813 {
5814         struct mlx5_priv *priv = dev->data->dev_private;
5815         struct mlx5_dev_ctx_shared *sh = priv->sh;
5816         uint32_t key_len = sizeof(*resource) -
5817                            offsetof(typeof(*resource), ft_type) +
5818                            resource->actions_num * sizeof(resource->actions[0]);
5819         struct mlx5_list_entry *entry;
5820         struct mlx5_flow_cb_ctx ctx = {
5821                 .error = error,
5822                 .data = resource,
5823         };
5824         struct mlx5_hlist *modify_cmds;
5825         uint64_t key64;
5826
5827         modify_cmds = flow_dv_hlist_prepare(sh, &sh->modify_cmds,
5828                                 "hdr_modify",
5829                                 MLX5_FLOW_HDR_MODIFY_HTABLE_SZ,
5830                                 true, false, sh,
5831                                 flow_dv_modify_create_cb,
5832                                 flow_dv_modify_match_cb,
5833                                 flow_dv_modify_remove_cb,
5834                                 flow_dv_modify_clone_cb,
5835                                 flow_dv_modify_clone_free_cb);
5836         if (unlikely(!modify_cmds))
5837                 return -rte_errno;
5838         resource->root = !dev_flow->dv.group;
5839         if (resource->actions_num > flow_dv_modify_hdr_action_max(dev,
5840                                                                 resource->root))
5841                 return rte_flow_error_set(error, EOVERFLOW,
5842                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5843                                           "too many modify header items");
5844         key64 = __rte_raw_cksum(&resource->ft_type, key_len, 0);
5845         entry = mlx5_hlist_register(modify_cmds, key64, &ctx);
5846         if (!entry)
5847                 return -rte_errno;
5848         resource = container_of(entry, typeof(*resource), entry);
5849         dev_flow->handle->dvh.modify_hdr = resource;
5850         return 0;
5851 }
5852
5853 /**
5854  * Get DV flow counter by index.
5855  *
5856  * @param[in] dev
5857  *   Pointer to the Ethernet device structure.
5858  * @param[in] idx
5859  *   mlx5 flow counter index in the container.
5860  * @param[out] ppool
5861  *   mlx5 flow counter pool in the container.
5862  *
5863  * @return
5864  *   Pointer to the counter, NULL otherwise.
5865  */
5866 static struct mlx5_flow_counter *
5867 flow_dv_counter_get_by_idx(struct rte_eth_dev *dev,
5868                            uint32_t idx,
5869                            struct mlx5_flow_counter_pool **ppool)
5870 {
5871         struct mlx5_priv *priv = dev->data->dev_private;
5872         struct mlx5_flow_counter_mng *cmng = &priv->sh->cmng;
5873         struct mlx5_flow_counter_pool *pool;
5874
5875         /* Decrease to original index and clear shared bit. */
5876         idx = (idx - 1) & (MLX5_CNT_SHARED_OFFSET - 1);
5877         MLX5_ASSERT(idx / MLX5_COUNTERS_PER_POOL < cmng->n);
5878         pool = cmng->pools[idx / MLX5_COUNTERS_PER_POOL];
5879         MLX5_ASSERT(pool);
5880         if (ppool)
5881                 *ppool = pool;
5882         return MLX5_POOL_GET_CNT(pool, idx % MLX5_COUNTERS_PER_POOL);
5883 }
5884
5885 /**
5886  * Check the devx counter belongs to the pool.
5887  *
5888  * @param[in] pool
5889  *   Pointer to the counter pool.
5890  * @param[in] id
5891  *   The counter devx ID.
5892  *
5893  * @return
5894  *   True if counter belongs to the pool, false otherwise.
5895  */
5896 static bool
5897 flow_dv_is_counter_in_pool(struct mlx5_flow_counter_pool *pool, int id)
5898 {
5899         int base = (pool->min_dcs->id / MLX5_COUNTERS_PER_POOL) *
5900                    MLX5_COUNTERS_PER_POOL;
5901
5902         if (id >= base && id < base + MLX5_COUNTERS_PER_POOL)
5903                 return true;
5904         return false;
5905 }
5906
5907 /**
5908  * Get a pool by devx counter ID.
5909  *
5910  * @param[in] cmng
5911  *   Pointer to the counter management.
5912  * @param[in] id
5913  *   The counter devx ID.
5914  *
5915  * @return
5916  *   The counter pool pointer if exists, NULL otherwise,
5917  */
5918 static struct mlx5_flow_counter_pool *
5919 flow_dv_find_pool_by_id(struct mlx5_flow_counter_mng *cmng, int id)
5920 {
5921         uint32_t i;
5922         struct mlx5_flow_counter_pool *pool = NULL;
5923
5924         rte_spinlock_lock(&cmng->pool_update_sl);
5925         /* Check last used pool. */
5926         if (cmng->last_pool_idx != POOL_IDX_INVALID &&
5927             flow_dv_is_counter_in_pool(cmng->pools[cmng->last_pool_idx], id)) {
5928                 pool = cmng->pools[cmng->last_pool_idx];
5929                 goto out;
5930         }
5931         /* ID out of range means no suitable pool in the container. */
5932         if (id > cmng->max_id || id < cmng->min_id)
5933                 goto out;
5934         /*
5935          * Find the pool from the end of the container, since mostly counter
5936          * ID is sequence increasing, and the last pool should be the needed
5937          * one.
5938          */
5939         i = cmng->n_valid;
5940         while (i--) {
5941                 struct mlx5_flow_counter_pool *pool_tmp = cmng->pools[i];
5942
5943                 if (flow_dv_is_counter_in_pool(pool_tmp, id)) {
5944                         pool = pool_tmp;
5945                         break;
5946                 }
5947         }
5948 out:
5949         rte_spinlock_unlock(&cmng->pool_update_sl);
5950         return pool;
5951 }
5952
5953 /**
5954  * Resize a counter container.
5955  *
5956  * @param[in] dev
5957  *   Pointer to the Ethernet device structure.
5958  *
5959  * @return
5960  *   0 on success, otherwise negative errno value and rte_errno is set.
5961  */
5962 static int
5963 flow_dv_container_resize(struct rte_eth_dev *dev)
5964 {
5965         struct mlx5_priv *priv = dev->data->dev_private;
5966         struct mlx5_flow_counter_mng *cmng = &priv->sh->cmng;
5967         void *old_pools = cmng->pools;
5968         uint32_t resize = cmng->n + MLX5_CNT_CONTAINER_RESIZE;
5969         uint32_t mem_size = sizeof(struct mlx5_flow_counter_pool *) * resize;
5970         void *pools = mlx5_malloc(MLX5_MEM_ZERO, mem_size, 0, SOCKET_ID_ANY);
5971
5972         if (!pools) {
5973                 rte_errno = ENOMEM;
5974                 return -ENOMEM;
5975         }
5976         if (old_pools)
5977                 memcpy(pools, old_pools, cmng->n *
5978                                        sizeof(struct mlx5_flow_counter_pool *));
5979         cmng->n = resize;
5980         cmng->pools = pools;
5981         if (old_pools)
5982                 mlx5_free(old_pools);
5983         return 0;
5984 }
5985
5986 /**
5987  * Query a devx flow counter.
5988  *
5989  * @param[in] dev
5990  *   Pointer to the Ethernet device structure.
5991  * @param[in] counter
5992  *   Index to the flow counter.
5993  * @param[out] pkts
5994  *   The statistics value of packets.
5995  * @param[out] bytes
5996  *   The statistics value of bytes.
5997  *
5998  * @return
5999  *   0 on success, otherwise a negative errno value and rte_errno is set.
6000  */
6001 static inline int
6002 _flow_dv_query_count(struct rte_eth_dev *dev, uint32_t counter, uint64_t *pkts,
6003                      uint64_t *bytes)
6004 {
6005         struct mlx5_priv *priv = dev->data->dev_private;
6006         struct mlx5_flow_counter_pool *pool = NULL;
6007         struct mlx5_flow_counter *cnt;
6008         int offset;
6009
6010         cnt = flow_dv_counter_get_by_idx(dev, counter, &pool);
6011         MLX5_ASSERT(pool);
6012         if (priv->sh->cmng.counter_fallback)
6013                 return mlx5_devx_cmd_flow_counter_query(cnt->dcs_when_active, 0,
6014                                         0, pkts, bytes, 0, NULL, NULL, 0);
6015         rte_spinlock_lock(&pool->sl);
6016         if (!pool->raw) {
6017                 *pkts = 0;
6018                 *bytes = 0;
6019         } else {
6020                 offset = MLX5_CNT_ARRAY_IDX(pool, cnt);
6021                 *pkts = rte_be_to_cpu_64(pool->raw->data[offset].hits);
6022                 *bytes = rte_be_to_cpu_64(pool->raw->data[offset].bytes);
6023         }
6024         rte_spinlock_unlock(&pool->sl);
6025         return 0;
6026 }
6027
6028 /**
6029  * Create and initialize a new counter pool.
6030  *
6031  * @param[in] dev
6032  *   Pointer to the Ethernet device structure.
6033  * @param[out] dcs
6034  *   The devX counter handle.
6035  * @param[in] age
6036  *   Whether the pool is for counter that was allocated for aging.
6037  * @param[in/out] cont_cur
6038  *   Pointer to the container pointer, it will be update in pool resize.
6039  *
6040  * @return
6041  *   The pool container pointer on success, NULL otherwise and rte_errno is set.
6042  */
6043 static struct mlx5_flow_counter_pool *
6044 flow_dv_pool_create(struct rte_eth_dev *dev, struct mlx5_devx_obj *dcs,
6045                     uint32_t age)
6046 {
6047         struct mlx5_priv *priv = dev->data->dev_private;
6048         struct mlx5_flow_counter_pool *pool;
6049         struct mlx5_flow_counter_mng *cmng = &priv->sh->cmng;
6050         bool fallback = priv->sh->cmng.counter_fallback;
6051         uint32_t size = sizeof(*pool);
6052
6053         size += MLX5_COUNTERS_PER_POOL * MLX5_CNT_SIZE;
6054         size += (!age ? 0 : MLX5_COUNTERS_PER_POOL * MLX5_AGE_SIZE);
6055         pool = mlx5_malloc(MLX5_MEM_ZERO, size, 0, SOCKET_ID_ANY);
6056         if (!pool) {
6057                 rte_errno = ENOMEM;
6058                 return NULL;
6059         }
6060         pool->raw = NULL;
6061         pool->is_aged = !!age;
6062         pool->query_gen = 0;
6063         pool->min_dcs = dcs;
6064         rte_spinlock_init(&pool->sl);
6065         rte_spinlock_init(&pool->csl);
6066         TAILQ_INIT(&pool->counters[0]);
6067         TAILQ_INIT(&pool->counters[1]);
6068         pool->time_of_last_age_check = MLX5_CURR_TIME_SEC;
6069         rte_spinlock_lock(&cmng->pool_update_sl);
6070         pool->index = cmng->n_valid;
6071         if (pool->index == cmng->n && flow_dv_container_resize(dev)) {
6072                 mlx5_free(pool);
6073                 rte_spinlock_unlock(&cmng->pool_update_sl);
6074                 return NULL;
6075         }
6076         cmng->pools[pool->index] = pool;
6077         cmng->n_valid++;
6078         if (unlikely(fallback)) {
6079                 int base = RTE_ALIGN_FLOOR(dcs->id, MLX5_COUNTERS_PER_POOL);
6080
6081                 if (base < cmng->min_id)
6082                         cmng->min_id = base;
6083                 if (base > cmng->max_id)
6084                         cmng->max_id = base + MLX5_COUNTERS_PER_POOL - 1;
6085                 cmng->last_pool_idx = pool->index;
6086         }
6087         rte_spinlock_unlock(&cmng->pool_update_sl);
6088         return pool;
6089 }
6090
6091 /**
6092  * Prepare a new counter and/or a new counter pool.
6093  *
6094  * @param[in] dev
6095  *   Pointer to the Ethernet device structure.
6096  * @param[out] cnt_free
6097  *   Where to put the pointer of a new counter.
6098  * @param[in] age
6099  *   Whether the pool is for counter that was allocated for aging.
6100  *
6101  * @return
6102  *   The counter pool pointer and @p cnt_free is set on success,
6103  *   NULL otherwise and rte_errno is set.
6104  */
6105 static struct mlx5_flow_counter_pool *
6106 flow_dv_counter_pool_prepare(struct rte_eth_dev *dev,
6107                              struct mlx5_flow_counter **cnt_free,
6108                              uint32_t age)
6109 {
6110         struct mlx5_priv *priv = dev->data->dev_private;
6111         struct mlx5_flow_counter_mng *cmng = &priv->sh->cmng;
6112         struct mlx5_flow_counter_pool *pool;
6113         struct mlx5_counters tmp_tq;
6114         struct mlx5_devx_obj *dcs = NULL;
6115         struct mlx5_flow_counter *cnt;
6116         enum mlx5_counter_type cnt_type =
6117                         age ? MLX5_COUNTER_TYPE_AGE : MLX5_COUNTER_TYPE_ORIGIN;
6118         bool fallback = priv->sh->cmng.counter_fallback;
6119         uint32_t i;
6120
6121         if (fallback) {
6122                 /* bulk_bitmap must be 0 for single counter allocation. */
6123                 dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->cdev->ctx, 0);
6124                 if (!dcs)
6125                         return NULL;
6126                 pool = flow_dv_find_pool_by_id(cmng, dcs->id);
6127                 if (!pool) {
6128                         pool = flow_dv_pool_create(dev, dcs, age);
6129                         if (!pool) {
6130                                 mlx5_devx_cmd_destroy(dcs);
6131                                 return NULL;
6132                         }
6133                 }
6134                 i = dcs->id % MLX5_COUNTERS_PER_POOL;
6135                 cnt = MLX5_POOL_GET_CNT(pool, i);
6136                 cnt->pool = pool;
6137                 cnt->dcs_when_free = dcs;
6138                 *cnt_free = cnt;
6139                 return pool;
6140         }
6141         dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->cdev->ctx, 0x4);
6142         if (!dcs) {
6143                 rte_errno = ENODATA;
6144                 return NULL;
6145         }
6146         pool = flow_dv_pool_create(dev, dcs, age);
6147         if (!pool) {
6148                 mlx5_devx_cmd_destroy(dcs);
6149                 return NULL;
6150         }
6151         TAILQ_INIT(&tmp_tq);
6152         for (i = 1; i < MLX5_COUNTERS_PER_POOL; ++i) {
6153                 cnt = MLX5_POOL_GET_CNT(pool, i);
6154                 cnt->pool = pool;
6155                 TAILQ_INSERT_HEAD(&tmp_tq, cnt, next);
6156         }
6157         rte_spinlock_lock(&cmng->csl[cnt_type]);
6158         TAILQ_CONCAT(&cmng->counters[cnt_type], &tmp_tq, next);
6159         rte_spinlock_unlock(&cmng->csl[cnt_type]);
6160         *cnt_free = MLX5_POOL_GET_CNT(pool, 0);
6161         (*cnt_free)->pool = pool;
6162         return pool;
6163 }
6164
6165 /**
6166  * Allocate a flow counter.
6167  *
6168  * @param[in] dev
6169  *   Pointer to the Ethernet device structure.
6170  * @param[in] age
6171  *   Whether the counter was allocated for aging.
6172  *
6173  * @return
6174  *   Index to flow counter on success, 0 otherwise and rte_errno is set.
6175  */
6176 static uint32_t
6177 flow_dv_counter_alloc(struct rte_eth_dev *dev, uint32_t age)
6178 {
6179         struct mlx5_priv *priv = dev->data->dev_private;
6180         struct mlx5_flow_counter_pool *pool = NULL;
6181         struct mlx5_flow_counter *cnt_free = NULL;
6182         bool fallback = priv->sh->cmng.counter_fallback;
6183         struct mlx5_flow_counter_mng *cmng = &priv->sh->cmng;
6184         enum mlx5_counter_type cnt_type =
6185                         age ? MLX5_COUNTER_TYPE_AGE : MLX5_COUNTER_TYPE_ORIGIN;
6186         uint32_t cnt_idx;
6187
6188         if (!priv->sh->devx) {
6189                 rte_errno = ENOTSUP;
6190                 return 0;
6191         }
6192         /* Get free counters from container. */
6193         rte_spinlock_lock(&cmng->csl[cnt_type]);
6194         cnt_free = TAILQ_FIRST(&cmng->counters[cnt_type]);
6195         if (cnt_free)
6196                 TAILQ_REMOVE(&cmng->counters[cnt_type], cnt_free, next);
6197         rte_spinlock_unlock(&cmng->csl[cnt_type]);
6198         if (!cnt_free && !flow_dv_counter_pool_prepare(dev, &cnt_free, age))
6199                 goto err;
6200         pool = cnt_free->pool;
6201         if (fallback)
6202                 cnt_free->dcs_when_active = cnt_free->dcs_when_free;
6203         /* Create a DV counter action only in the first time usage. */
6204         if (!cnt_free->action) {
6205                 uint16_t offset;
6206                 struct mlx5_devx_obj *dcs;
6207                 int ret;
6208
6209                 if (!fallback) {
6210                         offset = MLX5_CNT_ARRAY_IDX(pool, cnt_free);
6211                         dcs = pool->min_dcs;
6212                 } else {
6213                         offset = 0;
6214                         dcs = cnt_free->dcs_when_free;
6215                 }
6216                 ret = mlx5_flow_os_create_flow_action_count(dcs->obj, offset,
6217                                                             &cnt_free->action);
6218                 if (ret) {
6219                         rte_errno = errno;
6220                         goto err;
6221                 }
6222         }
6223         cnt_idx = MLX5_MAKE_CNT_IDX(pool->index,
6224                                 MLX5_CNT_ARRAY_IDX(pool, cnt_free));
6225         /* Update the counter reset values. */
6226         if (_flow_dv_query_count(dev, cnt_idx, &cnt_free->hits,
6227                                  &cnt_free->bytes))
6228                 goto err;
6229         if (!fallback && !priv->sh->cmng.query_thread_on)
6230                 /* Start the asynchronous batch query by the host thread. */
6231                 mlx5_set_query_alarm(priv->sh);
6232         /*
6233          * When the count action isn't shared (by ID), shared_info field is
6234          * used for indirect action API's refcnt.
6235          * When the counter action is not shared neither by ID nor by indirect
6236          * action API, shared info must be 1.
6237          */
6238         cnt_free->shared_info.refcnt = 1;
6239         return cnt_idx;
6240 err:
6241         if (cnt_free) {
6242                 cnt_free->pool = pool;
6243                 if (fallback)
6244                         cnt_free->dcs_when_free = cnt_free->dcs_when_active;
6245                 rte_spinlock_lock(&cmng->csl[cnt_type]);
6246                 TAILQ_INSERT_TAIL(&cmng->counters[cnt_type], cnt_free, next);
6247                 rte_spinlock_unlock(&cmng->csl[cnt_type]);
6248         }
6249         return 0;
6250 }
6251
6252 /**
6253  * Get age param from counter index.
6254  *
6255  * @param[in] dev
6256  *   Pointer to the Ethernet device structure.
6257  * @param[in] counter
6258  *   Index to the counter handler.
6259  *
6260  * @return
6261  *   The aging parameter specified for the counter index.
6262  */
6263 static struct mlx5_age_param*
6264 flow_dv_counter_idx_get_age(struct rte_eth_dev *dev,
6265                                 uint32_t counter)
6266 {
6267         struct mlx5_flow_counter *cnt;
6268         struct mlx5_flow_counter_pool *pool = NULL;
6269
6270         flow_dv_counter_get_by_idx(dev, counter, &pool);
6271         counter = (counter - 1) % MLX5_COUNTERS_PER_POOL;
6272         cnt = MLX5_POOL_GET_CNT(pool, counter);
6273         return MLX5_CNT_TO_AGE(cnt);
6274 }
6275
6276 /**
6277  * Remove a flow counter from aged counter list.
6278  *
6279  * @param[in] dev
6280  *   Pointer to the Ethernet device structure.
6281  * @param[in] counter
6282  *   Index to the counter handler.
6283  * @param[in] cnt
6284  *   Pointer to the counter handler.
6285  */
6286 static void
6287 flow_dv_counter_remove_from_age(struct rte_eth_dev *dev,
6288                                 uint32_t counter, struct mlx5_flow_counter *cnt)
6289 {
6290         struct mlx5_age_info *age_info;
6291         struct mlx5_age_param *age_param;
6292         struct mlx5_priv *priv = dev->data->dev_private;
6293         uint16_t expected = AGE_CANDIDATE;
6294
6295         age_info = GET_PORT_AGE_INFO(priv);
6296         age_param = flow_dv_counter_idx_get_age(dev, counter);
6297         if (!__atomic_compare_exchange_n(&age_param->state, &expected,
6298                                          AGE_FREE, false, __ATOMIC_RELAXED,
6299                                          __ATOMIC_RELAXED)) {
6300                 /**
6301                  * We need the lock even it is age timeout,
6302                  * since counter may still in process.
6303                  */
6304                 rte_spinlock_lock(&age_info->aged_sl);
6305                 TAILQ_REMOVE(&age_info->aged_counters, cnt, next);
6306                 rte_spinlock_unlock(&age_info->aged_sl);
6307                 __atomic_store_n(&age_param->state, AGE_FREE, __ATOMIC_RELAXED);
6308         }
6309 }
6310
6311 /**
6312  * Release a flow counter.
6313  *
6314  * @param[in] dev
6315  *   Pointer to the Ethernet device structure.
6316  * @param[in] counter
6317  *   Index to the counter handler.
6318  */
6319 static void
6320 flow_dv_counter_free(struct rte_eth_dev *dev, uint32_t counter)
6321 {
6322         struct mlx5_priv *priv = dev->data->dev_private;
6323         struct mlx5_flow_counter_pool *pool = NULL;
6324         struct mlx5_flow_counter *cnt;
6325         enum mlx5_counter_type cnt_type;
6326
6327         if (!counter)
6328                 return;
6329         cnt = flow_dv_counter_get_by_idx(dev, counter, &pool);
6330         MLX5_ASSERT(pool);
6331         if (pool->is_aged) {
6332                 flow_dv_counter_remove_from_age(dev, counter, cnt);
6333         } else {
6334                 /*
6335                  * If the counter action is shared by indirect action API,
6336                  * the atomic function reduces its references counter.
6337                  * If after the reduction the action is still referenced, the
6338                  * function returns here and does not release it.
6339                  * When the counter action is not shared by
6340                  * indirect action API, shared info is 1 before the reduction,
6341                  * so this condition is failed and function doesn't return here.
6342                  */
6343                 if (__atomic_sub_fetch(&cnt->shared_info.refcnt, 1,
6344                                        __ATOMIC_RELAXED))
6345                         return;
6346         }
6347         cnt->pool = pool;
6348         /*
6349          * Put the counter back to list to be updated in none fallback mode.
6350          * Currently, we are using two list alternately, while one is in query,
6351          * add the freed counter to the other list based on the pool query_gen
6352          * value. After query finishes, add counter the list to the global
6353          * container counter list. The list changes while query starts. In
6354          * this case, lock will not be needed as query callback and release
6355          * function both operate with the different list.
6356          */
6357         if (!priv->sh->cmng.counter_fallback) {
6358                 rte_spinlock_lock(&pool->csl);
6359                 TAILQ_INSERT_TAIL(&pool->counters[pool->query_gen], cnt, next);
6360                 rte_spinlock_unlock(&pool->csl);
6361         } else {
6362                 cnt->dcs_when_free = cnt->dcs_when_active;
6363                 cnt_type = pool->is_aged ? MLX5_COUNTER_TYPE_AGE :
6364                                            MLX5_COUNTER_TYPE_ORIGIN;
6365                 rte_spinlock_lock(&priv->sh->cmng.csl[cnt_type]);
6366                 TAILQ_INSERT_TAIL(&priv->sh->cmng.counters[cnt_type],
6367                                   cnt, next);
6368                 rte_spinlock_unlock(&priv->sh->cmng.csl[cnt_type]);
6369         }
6370 }
6371
6372 /**
6373  * Resize a meter id container.
6374  *
6375  * @param[in] dev
6376  *   Pointer to the Ethernet device structure.
6377  *
6378  * @return
6379  *   0 on success, otherwise negative errno value and rte_errno is set.
6380  */
6381 static int
6382 flow_dv_mtr_container_resize(struct rte_eth_dev *dev)
6383 {
6384         struct mlx5_priv *priv = dev->data->dev_private;
6385         struct mlx5_aso_mtr_pools_mng *pools_mng =
6386                                 &priv->sh->mtrmng->pools_mng;
6387         void *old_pools = pools_mng->pools;
6388         uint32_t resize = pools_mng->n + MLX5_MTRS_CONTAINER_RESIZE;
6389         uint32_t mem_size = sizeof(struct mlx5_aso_mtr_pool *) * resize;
6390         void *pools = mlx5_malloc(MLX5_MEM_ZERO, mem_size, 0, SOCKET_ID_ANY);
6391
6392         if (!pools) {
6393                 rte_errno = ENOMEM;
6394                 return -ENOMEM;
6395         }
6396         if (!pools_mng->n)
6397                 if (mlx5_aso_queue_init(priv->sh, ASO_OPC_MOD_POLICER)) {
6398                         mlx5_free(pools);
6399                         return -ENOMEM;
6400                 }
6401         if (old_pools)
6402                 memcpy(pools, old_pools, pools_mng->n *
6403                                        sizeof(struct mlx5_aso_mtr_pool *));
6404         pools_mng->n = resize;
6405         pools_mng->pools = pools;
6406         if (old_pools)
6407                 mlx5_free(old_pools);
6408         return 0;
6409 }
6410
6411 /**
6412  * Prepare a new meter and/or a new meter pool.
6413  *
6414  * @param[in] dev
6415  *   Pointer to the Ethernet device structure.
6416  * @param[out] mtr_free
6417  *   Where to put the pointer of a new meter.g.
6418  *
6419  * @return
6420  *   The meter pool pointer and @mtr_free is set on success,
6421  *   NULL otherwise and rte_errno is set.
6422  */
6423 static struct mlx5_aso_mtr_pool *
6424 flow_dv_mtr_pool_create(struct rte_eth_dev *dev, struct mlx5_aso_mtr **mtr_free)
6425 {
6426         struct mlx5_priv *priv = dev->data->dev_private;
6427         struct mlx5_aso_mtr_pools_mng *pools_mng = &priv->sh->mtrmng->pools_mng;
6428         struct mlx5_aso_mtr_pool *pool = NULL;
6429         struct mlx5_devx_obj *dcs = NULL;
6430         uint32_t i;
6431         uint32_t log_obj_size;
6432
6433         log_obj_size = rte_log2_u32(MLX5_ASO_MTRS_PER_POOL >> 1);
6434         dcs = mlx5_devx_cmd_create_flow_meter_aso_obj(priv->sh->cdev->ctx,
6435                                                       priv->sh->cdev->pdn,
6436                                                       log_obj_size);
6437         if (!dcs) {
6438                 rte_errno = ENODATA;
6439                 return NULL;
6440         }
6441         pool = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*pool), 0, SOCKET_ID_ANY);
6442         if (!pool) {
6443                 rte_errno = ENOMEM;
6444                 claim_zero(mlx5_devx_cmd_destroy(dcs));
6445                 return NULL;
6446         }
6447         pool->devx_obj = dcs;
6448         rte_rwlock_write_lock(&pools_mng->resize_mtrwl);
6449         pool->index = pools_mng->n_valid;
6450         if (pool->index == pools_mng->n && flow_dv_mtr_container_resize(dev)) {
6451                 mlx5_free(pool);
6452                 claim_zero(mlx5_devx_cmd_destroy(dcs));
6453                 rte_rwlock_write_unlock(&pools_mng->resize_mtrwl);
6454                 return NULL;
6455         }
6456         pools_mng->pools[pool->index] = pool;
6457         pools_mng->n_valid++;
6458         rte_rwlock_write_unlock(&pools_mng->resize_mtrwl);
6459         for (i = 1; i < MLX5_ASO_MTRS_PER_POOL; ++i) {
6460                 pool->mtrs[i].offset = i;
6461                 LIST_INSERT_HEAD(&pools_mng->meters, &pool->mtrs[i], next);
6462         }
6463         pool->mtrs[0].offset = 0;
6464         *mtr_free = &pool->mtrs[0];
6465         return pool;
6466 }
6467
6468 /**
6469  * Release a flow meter into pool.
6470  *
6471  * @param[in] dev
6472  *   Pointer to the Ethernet device structure.
6473  * @param[in] mtr_idx
6474  *   Index to aso flow meter.
6475  */
6476 static void
6477 flow_dv_aso_mtr_release_to_pool(struct rte_eth_dev *dev, uint32_t mtr_idx)
6478 {
6479         struct mlx5_priv *priv = dev->data->dev_private;
6480         struct mlx5_aso_mtr_pools_mng *pools_mng =
6481                                 &priv->sh->mtrmng->pools_mng;
6482         struct mlx5_aso_mtr *aso_mtr = mlx5_aso_meter_by_idx(priv, mtr_idx);
6483
6484         MLX5_ASSERT(aso_mtr);
6485         rte_spinlock_lock(&pools_mng->mtrsl);
6486         memset(&aso_mtr->fm, 0, sizeof(struct mlx5_flow_meter_info));
6487         aso_mtr->state = ASO_METER_FREE;
6488         LIST_INSERT_HEAD(&pools_mng->meters, aso_mtr, next);
6489         rte_spinlock_unlock(&pools_mng->mtrsl);
6490 }
6491
6492 /**
6493  * Allocate a aso flow meter.
6494  *
6495  * @param[in] dev
6496  *   Pointer to the Ethernet device structure.
6497  *
6498  * @return
6499  *   Index to aso flow meter on success, 0 otherwise and rte_errno is set.
6500  */
6501 static uint32_t
6502 flow_dv_mtr_alloc(struct rte_eth_dev *dev)
6503 {
6504         struct mlx5_priv *priv = dev->data->dev_private;
6505         struct mlx5_aso_mtr *mtr_free = NULL;
6506         struct mlx5_aso_mtr_pools_mng *pools_mng =
6507                                 &priv->sh->mtrmng->pools_mng;
6508         struct mlx5_aso_mtr_pool *pool;
6509         uint32_t mtr_idx = 0;
6510
6511         if (!priv->sh->devx) {
6512                 rte_errno = ENOTSUP;
6513                 return 0;
6514         }
6515         /* Allocate the flow meter memory. */
6516         /* Get free meters from management. */
6517         rte_spinlock_lock(&pools_mng->mtrsl);
6518         mtr_free = LIST_FIRST(&pools_mng->meters);
6519         if (mtr_free)
6520                 LIST_REMOVE(mtr_free, next);
6521         if (!mtr_free && !flow_dv_mtr_pool_create(dev, &mtr_free)) {
6522                 rte_spinlock_unlock(&pools_mng->mtrsl);
6523                 return 0;
6524         }
6525         mtr_free->state = ASO_METER_WAIT;
6526         rte_spinlock_unlock(&pools_mng->mtrsl);
6527         pool = container_of(mtr_free,
6528                         struct mlx5_aso_mtr_pool,
6529                         mtrs[mtr_free->offset]);
6530         mtr_idx = MLX5_MAKE_MTR_IDX(pool->index, mtr_free->offset);
6531         if (!mtr_free->fm.meter_action) {
6532 #ifdef HAVE_MLX5_DR_CREATE_ACTION_ASO
6533                 struct rte_flow_error error;
6534                 uint8_t reg_id;
6535
6536                 reg_id = mlx5_flow_get_reg_id(dev, MLX5_MTR_COLOR, 0, &error);
6537                 mtr_free->fm.meter_action =
6538                         mlx5_glue->dv_create_flow_action_aso
6539                                                 (priv->sh->rx_domain,
6540                                                  pool->devx_obj->obj,
6541                                                  mtr_free->offset,
6542                                                  (1 << MLX5_FLOW_COLOR_GREEN),
6543                                                  reg_id - REG_C_0);
6544 #endif /* HAVE_MLX5_DR_CREATE_ACTION_ASO */
6545                 if (!mtr_free->fm.meter_action) {
6546                         flow_dv_aso_mtr_release_to_pool(dev, mtr_idx);
6547                         return 0;
6548                 }
6549         }
6550         return mtr_idx;
6551 }
6552
6553 /**
6554  * Verify the @p attributes will be correctly understood by the NIC and store
6555  * them in the @p flow if everything is correct.
6556  *
6557  * @param[in] dev
6558  *   Pointer to dev struct.
6559  * @param[in] attributes
6560  *   Pointer to flow attributes
6561  * @param[in] external
6562  *   This flow rule is created by request external to PMD.
6563  * @param[out] error
6564  *   Pointer to error structure.
6565  *
6566  * @return
6567  *   - 0 on success and non root table.
6568  *   - 1 on success and root table.
6569  *   - a negative errno value otherwise and rte_errno is set.
6570  */
6571 static int
6572 flow_dv_validate_attributes(struct rte_eth_dev *dev,
6573                             const struct mlx5_flow_tunnel *tunnel,
6574                             const struct rte_flow_attr *attributes,
6575                             const struct flow_grp_info *grp_info,
6576                             struct rte_flow_error *error)
6577 {
6578         struct mlx5_priv *priv = dev->data->dev_private;
6579         uint32_t lowest_priority = mlx5_get_lowest_priority(dev, attributes);
6580         int ret = 0;
6581
6582 #ifndef HAVE_MLX5DV_DR
6583         RTE_SET_USED(tunnel);
6584         RTE_SET_USED(grp_info);
6585         if (attributes->group)
6586                 return rte_flow_error_set(error, ENOTSUP,
6587                                           RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
6588                                           NULL,
6589                                           "groups are not supported");
6590 #else
6591         uint32_t table = 0;
6592
6593         ret = mlx5_flow_group_to_table(dev, tunnel, attributes->group, &table,
6594                                        grp_info, error);
6595         if (ret)
6596                 return ret;
6597         if (!table)
6598                 ret = MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL;
6599 #endif
6600         if (attributes->priority != MLX5_FLOW_LOWEST_PRIO_INDICATOR &&
6601             attributes->priority > lowest_priority)
6602                 return rte_flow_error_set(error, ENOTSUP,
6603                                           RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
6604                                           NULL,
6605                                           "priority out of range");
6606         if (attributes->transfer) {
6607                 if (!priv->config.dv_esw_en)
6608                         return rte_flow_error_set
6609                                 (error, ENOTSUP,
6610                                  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
6611                                  "E-Switch dr is not supported");
6612                 if (!(priv->representor || priv->master))
6613                         return rte_flow_error_set
6614                                 (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6615                                  NULL, "E-Switch configuration can only be"
6616                                  " done by a master or a representor device");
6617                 if (attributes->egress)
6618                         return rte_flow_error_set
6619                                 (error, ENOTSUP,
6620                                  RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, attributes,
6621                                  "egress is not supported");
6622         }
6623         if (!(attributes->egress ^ attributes->ingress))
6624                 return rte_flow_error_set(error, ENOTSUP,
6625                                           RTE_FLOW_ERROR_TYPE_ATTR, NULL,
6626                                           "must specify exactly one of "
6627                                           "ingress or egress");
6628         return ret;
6629 }
6630
6631 static int
6632 validate_integrity_bits(const struct rte_flow_item_integrity *mask,
6633                         int64_t pattern_flags, uint64_t l3_flags,
6634                         uint64_t l4_flags, uint64_t ip4_flag,
6635                         struct rte_flow_error *error)
6636 {
6637         if (mask->l3_ok && !(pattern_flags & l3_flags))
6638                 return rte_flow_error_set(error, EINVAL,
6639                                           RTE_FLOW_ERROR_TYPE_ITEM,
6640                                           NULL, "missing L3 protocol");
6641
6642         if (mask->ipv4_csum_ok && !(pattern_flags & ip4_flag))
6643                 return rte_flow_error_set(error, EINVAL,
6644                                           RTE_FLOW_ERROR_TYPE_ITEM,
6645                                           NULL, "missing IPv4 protocol");
6646
6647         if ((mask->l4_ok || mask->l4_csum_ok) && !(pattern_flags & l4_flags))
6648                 return rte_flow_error_set(error, EINVAL,
6649                                           RTE_FLOW_ERROR_TYPE_ITEM,
6650                                           NULL, "missing L4 protocol");
6651
6652         return 0;
6653 }
6654
6655 static int
6656 flow_dv_validate_item_integrity_post(const struct
6657                                      rte_flow_item *integrity_items[2],
6658                                      int64_t pattern_flags,
6659                                      struct rte_flow_error *error)
6660 {
6661         const struct rte_flow_item_integrity *mask;
6662         int ret;
6663
6664         if (pattern_flags & MLX5_FLOW_ITEM_OUTER_INTEGRITY) {
6665                 mask = (typeof(mask))integrity_items[0]->mask;
6666                 ret = validate_integrity_bits(mask, pattern_flags,
6667                                               MLX5_FLOW_LAYER_OUTER_L3,
6668                                               MLX5_FLOW_LAYER_OUTER_L4,
6669                                               MLX5_FLOW_LAYER_OUTER_L3_IPV4,
6670                                               error);
6671                 if (ret)
6672                         return ret;
6673         }
6674         if (pattern_flags & MLX5_FLOW_ITEM_INNER_INTEGRITY) {
6675                 mask = (typeof(mask))integrity_items[1]->mask;
6676                 ret = validate_integrity_bits(mask, pattern_flags,
6677                                               MLX5_FLOW_LAYER_INNER_L3,
6678                                               MLX5_FLOW_LAYER_INNER_L4,
6679                                               MLX5_FLOW_LAYER_INNER_L3_IPV4,
6680                                               error);
6681                 if (ret)
6682                         return ret;
6683         }
6684         return 0;
6685 }
6686
6687 static int
6688 flow_dv_validate_item_integrity(struct rte_eth_dev *dev,
6689                                 const struct rte_flow_item *integrity_item,
6690                                 uint64_t pattern_flags, uint64_t *last_item,
6691                                 const struct rte_flow_item *integrity_items[2],
6692                                 struct rte_flow_error *error)
6693 {
6694         struct mlx5_priv *priv = dev->data->dev_private;
6695         const struct rte_flow_item_integrity *mask = (typeof(mask))
6696                                                      integrity_item->mask;
6697         const struct rte_flow_item_integrity *spec = (typeof(spec))
6698                                                      integrity_item->spec;
6699
6700         if (!priv->config.hca_attr.pkt_integrity_match)
6701                 return rte_flow_error_set(error, ENOTSUP,
6702                                           RTE_FLOW_ERROR_TYPE_ITEM,
6703                                           integrity_item,
6704                                           "packet integrity integrity_item not supported");
6705         if (!spec)
6706                 return rte_flow_error_set(error, ENOTSUP,
6707                                           RTE_FLOW_ERROR_TYPE_ITEM,
6708                                           integrity_item,
6709                                           "no spec for integrity item");
6710         if (!mask)
6711                 mask = &rte_flow_item_integrity_mask;
6712         if (!mlx5_validate_integrity_item(mask))
6713                 return rte_flow_error_set(error, ENOTSUP,
6714                                           RTE_FLOW_ERROR_TYPE_ITEM,
6715                                           integrity_item,
6716                                           "unsupported integrity filter");
6717         if (spec->level > 1) {
6718                 if (pattern_flags & MLX5_FLOW_ITEM_INNER_INTEGRITY)
6719                         return rte_flow_error_set
6720                                 (error, ENOTSUP,
6721                                  RTE_FLOW_ERROR_TYPE_ITEM,
6722                                  NULL, "multiple inner integrity items not supported");
6723                 integrity_items[1] = integrity_item;
6724                 *last_item |= MLX5_FLOW_ITEM_INNER_INTEGRITY;
6725         } else {
6726                 if (pattern_flags & MLX5_FLOW_ITEM_OUTER_INTEGRITY)
6727                         return rte_flow_error_set
6728                                 (error, ENOTSUP,
6729                                  RTE_FLOW_ERROR_TYPE_ITEM,
6730                                  NULL, "multiple outer integrity items not supported");
6731                 integrity_items[0] = integrity_item;
6732                 *last_item |= MLX5_FLOW_ITEM_OUTER_INTEGRITY;
6733         }
6734         return 0;
6735 }
6736
6737 static int
6738 flow_dv_validate_item_flex(struct rte_eth_dev *dev,
6739                            const struct rte_flow_item *item,
6740                            uint64_t item_flags,
6741                            uint64_t *last_item,
6742                            bool is_inner,
6743                            struct rte_flow_error *error)
6744 {
6745         const struct rte_flow_item_flex *flow_spec = item->spec;
6746         const struct rte_flow_item_flex *flow_mask = item->mask;
6747         struct mlx5_flex_item *flex;
6748
6749         if (!flow_spec)
6750                 return rte_flow_error_set(error, EINVAL,
6751                                           RTE_FLOW_ERROR_TYPE_ITEM, NULL,
6752                                           "flex flow item spec cannot be NULL");
6753         if (!flow_mask)
6754                 return rte_flow_error_set(error, EINVAL,
6755                                           RTE_FLOW_ERROR_TYPE_ITEM, NULL,
6756                                           "flex flow item mask cannot be NULL");
6757         if (item->last)
6758                 return rte_flow_error_set(error, ENOTSUP,
6759                                           RTE_FLOW_ERROR_TYPE_ITEM, NULL,
6760                                           "flex flow item last not supported");
6761         if (mlx5_flex_acquire_index(dev, flow_spec->handle, false) < 0)
6762                 return rte_flow_error_set(error, EINVAL,
6763                                           RTE_FLOW_ERROR_TYPE_ITEM, NULL,
6764                                           "invalid flex flow item handle");
6765         flex = (struct mlx5_flex_item *)flow_spec->handle;
6766         switch (flex->tunnel_mode) {
6767         case FLEX_TUNNEL_MODE_SINGLE:
6768                 if (item_flags &
6769                     (MLX5_FLOW_ITEM_OUTER_FLEX | MLX5_FLOW_ITEM_INNER_FLEX))
6770                         rte_flow_error_set(error, EINVAL,
6771                                            RTE_FLOW_ERROR_TYPE_ITEM,
6772                                            NULL, "multiple flex items not supported");
6773                 break;
6774         case FLEX_TUNNEL_MODE_OUTER:
6775                 if (is_inner)
6776                         rte_flow_error_set(error, EINVAL,
6777                                            RTE_FLOW_ERROR_TYPE_ITEM,
6778                                            NULL, "inner flex item was not configured");
6779                 if (item_flags & MLX5_FLOW_ITEM_OUTER_FLEX)
6780                         rte_flow_error_set(error, ENOTSUP,
6781                                            RTE_FLOW_ERROR_TYPE_ITEM,
6782                                            NULL, "multiple flex items not supported");
6783                 break;
6784         case FLEX_TUNNEL_MODE_INNER:
6785                 if (!is_inner)
6786                         rte_flow_error_set(error, EINVAL,
6787                                            RTE_FLOW_ERROR_TYPE_ITEM,
6788                                            NULL, "outer flex item was not configured");
6789                 if (item_flags & MLX5_FLOW_ITEM_INNER_FLEX)
6790                         rte_flow_error_set(error, EINVAL,
6791                                            RTE_FLOW_ERROR_TYPE_ITEM,
6792                                            NULL, "multiple flex items not supported");
6793                 break;
6794         case FLEX_TUNNEL_MODE_MULTI:
6795                 if ((is_inner && (item_flags & MLX5_FLOW_ITEM_INNER_FLEX)) ||
6796                     (!is_inner && (item_flags & MLX5_FLOW_ITEM_OUTER_FLEX))) {
6797                         rte_flow_error_set(error, EINVAL,
6798                                            RTE_FLOW_ERROR_TYPE_ITEM,
6799                                            NULL, "multiple flex items not supported");
6800                 }
6801                 break;
6802         case FLEX_TUNNEL_MODE_TUNNEL:
6803                 if (is_inner || (item_flags & MLX5_FLOW_ITEM_FLEX_TUNNEL))
6804                         rte_flow_error_set(error, EINVAL,
6805                                            RTE_FLOW_ERROR_TYPE_ITEM,
6806                                            NULL, "multiple flex tunnel items not supported");
6807                 break;
6808         default:
6809                 rte_flow_error_set(error, EINVAL,
6810                                    RTE_FLOW_ERROR_TYPE_ITEM,
6811                                    NULL, "invalid flex item configuration");
6812         }
6813         *last_item = flex->tunnel_mode == FLEX_TUNNEL_MODE_TUNNEL ?
6814                      MLX5_FLOW_ITEM_FLEX_TUNNEL : is_inner ?
6815                      MLX5_FLOW_ITEM_INNER_FLEX : MLX5_FLOW_ITEM_OUTER_FLEX;
6816         return 0;
6817 }
6818
6819 /**
6820  * Internal validation function. For validating both actions and items.
6821  *
6822  * @param[in] dev
6823  *   Pointer to the rte_eth_dev structure.
6824  * @param[in] attr
6825  *   Pointer to the flow attributes.
6826  * @param[in] items
6827  *   Pointer to the list of items.
6828  * @param[in] actions
6829  *   Pointer to the list of actions.
6830  * @param[in] external
6831  *   This flow rule is created by request external to PMD.
6832  * @param[in] hairpin
6833  *   Number of hairpin TX actions, 0 means classic flow.
6834  * @param[out] error
6835  *   Pointer to the error structure.
6836  *
6837  * @return
6838  *   0 on success, a negative errno value otherwise and rte_errno is set.
6839  */
6840 static int
6841 flow_dv_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attr,
6842                  const struct rte_flow_item items[],
6843                  const struct rte_flow_action actions[],
6844                  bool external, int hairpin, struct rte_flow_error *error)
6845 {
6846         int ret;
6847         uint64_t action_flags = 0;
6848         uint64_t item_flags = 0;
6849         uint64_t last_item = 0;
6850         uint8_t next_protocol = 0xff;
6851         uint16_t ether_type = 0;
6852         int actions_n = 0;
6853         uint8_t item_ipv6_proto = 0;
6854         int fdb_mirror_limit = 0;
6855         int modify_after_mirror = 0;
6856         const struct rte_flow_item *geneve_item = NULL;
6857         const struct rte_flow_item *gre_item = NULL;
6858         const struct rte_flow_item *gtp_item = NULL;
6859         const struct rte_flow_action_raw_decap *decap;
6860         const struct rte_flow_action_raw_encap *encap;
6861         const struct rte_flow_action_rss *rss = NULL;
6862         const struct rte_flow_action_rss *sample_rss = NULL;
6863         const struct rte_flow_action_count *sample_count = NULL;
6864         const struct rte_flow_item_tcp nic_tcp_mask = {
6865                 .hdr = {
6866                         .tcp_flags = 0xFF,
6867                         .src_port = RTE_BE16(UINT16_MAX),
6868                         .dst_port = RTE_BE16(UINT16_MAX),
6869                 }
6870         };
6871         const struct rte_flow_item_ipv6 nic_ipv6_mask = {
6872                 .hdr = {
6873                         .src_addr =
6874                         "\xff\xff\xff\xff\xff\xff\xff\xff"
6875                         "\xff\xff\xff\xff\xff\xff\xff\xff",
6876                         .dst_addr =
6877                         "\xff\xff\xff\xff\xff\xff\xff\xff"
6878                         "\xff\xff\xff\xff\xff\xff\xff\xff",
6879                         .vtc_flow = RTE_BE32(0xffffffff),
6880                         .proto = 0xff,
6881                         .hop_limits = 0xff,
6882                 },
6883                 .has_frag_ext = 1,
6884         };
6885         const struct rte_flow_item_ecpri nic_ecpri_mask = {
6886                 .hdr = {
6887                         .common = {
6888                                 .u32 =
6889                                 RTE_BE32(((const struct rte_ecpri_common_hdr) {
6890                                         .type = 0xFF,
6891                                         }).u32),
6892                         },
6893                         .dummy[0] = 0xffffffff,
6894                 },
6895         };
6896         struct mlx5_priv *priv = dev->data->dev_private;
6897         struct mlx5_dev_config *dev_conf = &priv->config;
6898         uint16_t queue_index = 0xFFFF;
6899         const struct rte_flow_item_vlan *vlan_m = NULL;
6900         uint32_t rw_act_num = 0;
6901         uint64_t is_root;
6902         const struct mlx5_flow_tunnel *tunnel;
6903         enum mlx5_tof_rule_type tof_rule_type;
6904         struct flow_grp_info grp_info = {
6905                 .external = !!external,
6906                 .transfer = !!attr->transfer,
6907                 .fdb_def_rule = !!priv->fdb_def_rule,
6908                 .std_tbl_fix = true,
6909         };
6910         const struct rte_eth_hairpin_conf *conf;
6911         const struct rte_flow_item *integrity_items[2] = {NULL, NULL};
6912         const struct rte_flow_item *port_id_item = NULL;
6913         bool def_policy = false;
6914         uint16_t udp_dport = 0;
6915
6916         if (items == NULL)
6917                 return -1;
6918         tunnel = is_tunnel_offload_active(dev) ?
6919                  mlx5_get_tof(items, actions, &tof_rule_type) : NULL;
6920         if (tunnel) {
6921                 if (!priv->config.dv_flow_en)
6922                         return rte_flow_error_set
6923                                 (error, ENOTSUP,
6924                                  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6925                                  NULL, "tunnel offload requires DV flow interface");
6926                 if (priv->representor)
6927                         return rte_flow_error_set
6928                                 (error, ENOTSUP,
6929                                  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6930                                  NULL, "decap not supported for VF representor");
6931                 if (tof_rule_type == MLX5_TUNNEL_OFFLOAD_SET_RULE)
6932                         action_flags |= MLX5_FLOW_ACTION_TUNNEL_SET;
6933                 else if (tof_rule_type == MLX5_TUNNEL_OFFLOAD_MATCH_RULE)
6934                         action_flags |= MLX5_FLOW_ACTION_TUNNEL_MATCH |
6935                                         MLX5_FLOW_ACTION_DECAP;
6936                 grp_info.std_tbl_fix = tunnel_use_standard_attr_group_translate
6937                                         (dev, attr, tunnel, tof_rule_type);
6938         }
6939         ret = flow_dv_validate_attributes(dev, tunnel, attr, &grp_info, error);
6940         if (ret < 0)
6941                 return ret;
6942         is_root = (uint64_t)ret;
6943         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
6944                 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
6945                 int type = items->type;
6946
6947                 if (!mlx5_flow_os_item_supported(type))
6948                         return rte_flow_error_set(error, ENOTSUP,
6949                                                   RTE_FLOW_ERROR_TYPE_ITEM,
6950                                                   NULL, "item not supported");
6951                 switch (type) {
6952                 case RTE_FLOW_ITEM_TYPE_VOID:
6953                         break;
6954                 case RTE_FLOW_ITEM_TYPE_PORT_ID:
6955                         ret = flow_dv_validate_item_port_id
6956                                         (dev, items, attr, item_flags, error);
6957                         if (ret < 0)
6958                                 return ret;
6959                         last_item = MLX5_FLOW_ITEM_PORT_ID;
6960                         port_id_item = items;
6961                         break;
6962                 case RTE_FLOW_ITEM_TYPE_ETH:
6963                         ret = mlx5_flow_validate_item_eth(items, item_flags,
6964                                                           true, error);
6965                         if (ret < 0)
6966                                 return ret;
6967                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
6968                                              MLX5_FLOW_LAYER_OUTER_L2;
6969                         if (items->mask != NULL && items->spec != NULL) {
6970                                 ether_type =
6971                                         ((const struct rte_flow_item_eth *)
6972                                          items->spec)->type;
6973                                 ether_type &=
6974                                         ((const struct rte_flow_item_eth *)
6975                                          items->mask)->type;
6976                                 ether_type = rte_be_to_cpu_16(ether_type);
6977                         } else {
6978                                 ether_type = 0;
6979                         }
6980                         break;
6981                 case RTE_FLOW_ITEM_TYPE_VLAN:
6982                         ret = flow_dv_validate_item_vlan(items, item_flags,
6983                                                          dev, error);
6984                         if (ret < 0)
6985                                 return ret;
6986                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
6987                                              MLX5_FLOW_LAYER_OUTER_VLAN;
6988                         if (items->mask != NULL && items->spec != NULL) {
6989                                 ether_type =
6990                                         ((const struct rte_flow_item_vlan *)
6991                                          items->spec)->inner_type;
6992                                 ether_type &=
6993                                         ((const struct rte_flow_item_vlan *)
6994                                          items->mask)->inner_type;
6995                                 ether_type = rte_be_to_cpu_16(ether_type);
6996                         } else {
6997                                 ether_type = 0;
6998                         }
6999                         /* Store outer VLAN mask for of_push_vlan action. */
7000                         if (!tunnel)
7001                                 vlan_m = items->mask;
7002                         break;
7003                 case RTE_FLOW_ITEM_TYPE_IPV4:
7004                         mlx5_flow_tunnel_ip_check(items, next_protocol,
7005                                                   &item_flags, &tunnel);
7006                         ret = flow_dv_validate_item_ipv4(dev, items, item_flags,
7007                                                          last_item, ether_type,
7008                                                          error);
7009                         if (ret < 0)
7010                                 return ret;
7011                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
7012                                              MLX5_FLOW_LAYER_OUTER_L3_IPV4;
7013                         if (items->mask != NULL &&
7014                             ((const struct rte_flow_item_ipv4 *)
7015                              items->mask)->hdr.next_proto_id) {
7016                                 next_protocol =
7017                                         ((const struct rte_flow_item_ipv4 *)
7018                                          (items->spec))->hdr.next_proto_id;
7019                                 next_protocol &=
7020                                         ((const struct rte_flow_item_ipv4 *)
7021                                          (items->mask))->hdr.next_proto_id;
7022                         } else {
7023                                 /* Reset for inner layer. */
7024                                 next_protocol = 0xff;
7025                         }
7026                         break;
7027                 case RTE_FLOW_ITEM_TYPE_IPV6:
7028                         mlx5_flow_tunnel_ip_check(items, next_protocol,
7029                                                   &item_flags, &tunnel);
7030                         ret = mlx5_flow_validate_item_ipv6(items, item_flags,
7031                                                            last_item,
7032                                                            ether_type,
7033                                                            &nic_ipv6_mask,
7034                                                            error);
7035                         if (ret < 0)
7036                                 return ret;
7037                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
7038                                              MLX5_FLOW_LAYER_OUTER_L3_IPV6;
7039                         if (items->mask != NULL &&
7040                             ((const struct rte_flow_item_ipv6 *)
7041                              items->mask)->hdr.proto) {
7042                                 item_ipv6_proto =
7043                                         ((const struct rte_flow_item_ipv6 *)
7044                                          items->spec)->hdr.proto;
7045                                 next_protocol =
7046                                         ((const struct rte_flow_item_ipv6 *)
7047                                          items->spec)->hdr.proto;
7048                                 next_protocol &=
7049                                         ((const struct rte_flow_item_ipv6 *)
7050                                          items->mask)->hdr.proto;
7051                         } else {
7052                                 /* Reset for inner layer. */
7053                                 next_protocol = 0xff;
7054                         }
7055                         break;
7056                 case RTE_FLOW_ITEM_TYPE_IPV6_FRAG_EXT:
7057                         ret = flow_dv_validate_item_ipv6_frag_ext(items,
7058                                                                   item_flags,
7059                                                                   error);
7060                         if (ret < 0)
7061                                 return ret;
7062                         last_item = tunnel ?
7063                                         MLX5_FLOW_LAYER_INNER_L3_IPV6_FRAG_EXT :
7064                                         MLX5_FLOW_LAYER_OUTER_L3_IPV6_FRAG_EXT;
7065                         if (items->mask != NULL &&
7066                             ((const struct rte_flow_item_ipv6_frag_ext *)
7067                              items->mask)->hdr.next_header) {
7068                                 next_protocol =
7069                                 ((const struct rte_flow_item_ipv6_frag_ext *)
7070                                  items->spec)->hdr.next_header;
7071                                 next_protocol &=
7072                                 ((const struct rte_flow_item_ipv6_frag_ext *)
7073                                  items->mask)->hdr.next_header;
7074                         } else {
7075                                 /* Reset for inner layer. */
7076                                 next_protocol = 0xff;
7077                         }
7078                         break;
7079                 case RTE_FLOW_ITEM_TYPE_TCP:
7080                         ret = mlx5_flow_validate_item_tcp
7081                                                 (items, item_flags,
7082                                                  next_protocol,
7083                                                  &nic_tcp_mask,
7084                                                  error);
7085                         if (ret < 0)
7086                                 return ret;
7087                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
7088                                              MLX5_FLOW_LAYER_OUTER_L4_TCP;
7089                         break;
7090                 case RTE_FLOW_ITEM_TYPE_UDP:
7091                         ret = mlx5_flow_validate_item_udp(items, item_flags,
7092                                                           next_protocol,
7093                                                           error);
7094                         const struct rte_flow_item_udp *spec = items->spec;
7095                         const struct rte_flow_item_udp *mask = items->mask;
7096                         if (!mask)
7097                                 mask = &rte_flow_item_udp_mask;
7098                         if (spec != NULL)
7099                                 udp_dport = rte_be_to_cpu_16
7100                                                 (spec->hdr.dst_port &
7101                                                  mask->hdr.dst_port);
7102                         if (ret < 0)
7103                                 return ret;
7104                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
7105                                              MLX5_FLOW_LAYER_OUTER_L4_UDP;
7106                         break;
7107                 case RTE_FLOW_ITEM_TYPE_GRE:
7108                         ret = mlx5_flow_validate_item_gre(items, item_flags,
7109                                                           next_protocol, error);
7110                         if (ret < 0)
7111                                 return ret;
7112                         gre_item = items;
7113                         last_item = MLX5_FLOW_LAYER_GRE;
7114                         break;
7115                 case RTE_FLOW_ITEM_TYPE_NVGRE:
7116                         ret = mlx5_flow_validate_item_nvgre(items, item_flags,
7117                                                             next_protocol,
7118                                                             error);
7119                         if (ret < 0)
7120                                 return ret;
7121                         last_item = MLX5_FLOW_LAYER_NVGRE;
7122                         break;
7123                 case RTE_FLOW_ITEM_TYPE_GRE_KEY:
7124                         ret = mlx5_flow_validate_item_gre_key
7125                                 (items, item_flags, gre_item, error);
7126                         if (ret < 0)
7127                                 return ret;
7128                         last_item = MLX5_FLOW_LAYER_GRE_KEY;
7129                         break;
7130                 case RTE_FLOW_ITEM_TYPE_VXLAN:
7131                         ret = mlx5_flow_validate_item_vxlan(dev, udp_dport,
7132                                                             items, item_flags,
7133                                                             attr, error);
7134                         if (ret < 0)
7135                                 return ret;
7136                         last_item = MLX5_FLOW_LAYER_VXLAN;
7137                         break;
7138                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
7139                         ret = mlx5_flow_validate_item_vxlan_gpe(items,
7140                                                                 item_flags, dev,
7141                                                                 error);
7142                         if (ret < 0)
7143                                 return ret;
7144                         last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
7145                         break;
7146                 case RTE_FLOW_ITEM_TYPE_GENEVE:
7147                         ret = mlx5_flow_validate_item_geneve(items,
7148                                                              item_flags, dev,
7149                                                              error);
7150                         if (ret < 0)
7151                                 return ret;
7152                         geneve_item = items;
7153                         last_item = MLX5_FLOW_LAYER_GENEVE;
7154                         break;
7155                 case RTE_FLOW_ITEM_TYPE_GENEVE_OPT:
7156                         ret = mlx5_flow_validate_item_geneve_opt(items,
7157                                                                  last_item,
7158                                                                  geneve_item,
7159                                                                  dev,
7160                                                                  error);
7161                         if (ret < 0)
7162                                 return ret;
7163                         last_item = MLX5_FLOW_LAYER_GENEVE_OPT;
7164                         break;
7165                 case RTE_FLOW_ITEM_TYPE_MPLS:
7166                         ret = mlx5_flow_validate_item_mpls(dev, items,
7167                                                            item_flags,
7168                                                            last_item, error);
7169                         if (ret < 0)
7170                                 return ret;
7171                         last_item = MLX5_FLOW_LAYER_MPLS;
7172                         break;
7173
7174                 case RTE_FLOW_ITEM_TYPE_MARK:
7175                         ret = flow_dv_validate_item_mark(dev, items, attr,
7176                                                          error);
7177                         if (ret < 0)
7178                                 return ret;
7179                         last_item = MLX5_FLOW_ITEM_MARK;
7180                         break;
7181                 case RTE_FLOW_ITEM_TYPE_META:
7182                         ret = flow_dv_validate_item_meta(dev, items, attr,
7183                                                          error);
7184                         if (ret < 0)
7185                                 return ret;
7186                         last_item = MLX5_FLOW_ITEM_METADATA;
7187                         break;
7188                 case RTE_FLOW_ITEM_TYPE_ICMP:
7189                         ret = mlx5_flow_validate_item_icmp(items, item_flags,
7190                                                            next_protocol,
7191                                                            error);
7192                         if (ret < 0)
7193                                 return ret;
7194                         last_item = MLX5_FLOW_LAYER_ICMP;
7195                         break;
7196                 case RTE_FLOW_ITEM_TYPE_ICMP6:
7197                         ret = mlx5_flow_validate_item_icmp6(items, item_flags,
7198                                                             next_protocol,
7199                                                             error);
7200                         if (ret < 0)
7201                                 return ret;
7202                         item_ipv6_proto = IPPROTO_ICMPV6;
7203                         last_item = MLX5_FLOW_LAYER_ICMP6;
7204                         break;
7205                 case RTE_FLOW_ITEM_TYPE_TAG:
7206                         ret = flow_dv_validate_item_tag(dev, items,
7207                                                         attr, error);
7208                         if (ret < 0)
7209                                 return ret;
7210                         last_item = MLX5_FLOW_ITEM_TAG;
7211                         break;
7212                 case MLX5_RTE_FLOW_ITEM_TYPE_TAG:
7213                 case MLX5_RTE_FLOW_ITEM_TYPE_TX_QUEUE:
7214                         break;
7215                 case RTE_FLOW_ITEM_TYPE_GTP:
7216                         ret = flow_dv_validate_item_gtp(dev, items, item_flags,
7217                                                         error);
7218                         if (ret < 0)
7219                                 return ret;
7220                         gtp_item = items;
7221                         last_item = MLX5_FLOW_LAYER_GTP;
7222                         break;
7223                 case RTE_FLOW_ITEM_TYPE_GTP_PSC:
7224                         ret = flow_dv_validate_item_gtp_psc(items, last_item,
7225                                                             gtp_item, attr,
7226                                                             error);
7227                         if (ret < 0)
7228                                 return ret;
7229                         last_item = MLX5_FLOW_LAYER_GTP_PSC;
7230                         break;
7231                 case RTE_FLOW_ITEM_TYPE_ECPRI:
7232                         /* Capacity will be checked in the translate stage. */
7233                         ret = mlx5_flow_validate_item_ecpri(items, item_flags,
7234                                                             last_item,
7235                                                             ether_type,
7236                                                             &nic_ecpri_mask,
7237                                                             error);
7238                         if (ret < 0)
7239                                 return ret;
7240                         last_item = MLX5_FLOW_LAYER_ECPRI;
7241                         break;
7242                 case RTE_FLOW_ITEM_TYPE_INTEGRITY:
7243                         ret = flow_dv_validate_item_integrity(dev, items,
7244                                                               item_flags,
7245                                                               &last_item,
7246                                                               integrity_items,
7247                                                               error);
7248                         if (ret < 0)
7249                                 return ret;
7250                         break;
7251                 case RTE_FLOW_ITEM_TYPE_CONNTRACK:
7252                         ret = flow_dv_validate_item_aso_ct(dev, items,
7253                                                            &item_flags, error);
7254                         if (ret < 0)
7255                                 return ret;
7256                         break;
7257                 case MLX5_RTE_FLOW_ITEM_TYPE_TUNNEL:
7258                         /* tunnel offload item was processed before
7259                          * list it here as a supported type
7260                          */
7261                         break;
7262                 case RTE_FLOW_ITEM_TYPE_FLEX:
7263                         ret = flow_dv_validate_item_flex(dev, items, item_flags,
7264                                                          &last_item,
7265                                                          tunnel != 0, error);
7266                         if (ret < 0)
7267                                 return ret;
7268                         break;
7269                 default:
7270                         return rte_flow_error_set(error, ENOTSUP,
7271                                                   RTE_FLOW_ERROR_TYPE_ITEM,
7272                                                   NULL, "item not supported");
7273                 }
7274                 item_flags |= last_item;
7275         }
7276         if (item_flags & MLX5_FLOW_ITEM_INTEGRITY) {
7277                 ret = flow_dv_validate_item_integrity_post(integrity_items,
7278                                                            item_flags, error);
7279                 if (ret)
7280                         return ret;
7281         }
7282         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
7283                 int type = actions->type;
7284                 bool shared_count = false;
7285
7286                 if (!mlx5_flow_os_action_supported(type))
7287                         return rte_flow_error_set(error, ENOTSUP,
7288                                                   RTE_FLOW_ERROR_TYPE_ACTION,
7289                                                   actions,
7290                                                   "action not supported");
7291                 if (actions_n == MLX5_DV_MAX_NUMBER_OF_ACTIONS)
7292                         return rte_flow_error_set(error, ENOTSUP,
7293                                                   RTE_FLOW_ERROR_TYPE_ACTION,
7294                                                   actions, "too many actions");
7295                 if (action_flags &
7296                         MLX5_FLOW_ACTION_METER_WITH_TERMINATED_POLICY)
7297                         return rte_flow_error_set(error, ENOTSUP,
7298                                 RTE_FLOW_ERROR_TYPE_ACTION,
7299                                 NULL, "meter action with policy "
7300                                 "must be the last action");
7301                 switch (type) {
7302                 case RTE_FLOW_ACTION_TYPE_VOID:
7303                         break;
7304                 case RTE_FLOW_ACTION_TYPE_PORT_ID:
7305                 case RTE_FLOW_ACTION_TYPE_REPRESENTED_PORT:
7306                         ret = flow_dv_validate_action_port_id(dev,
7307                                                               action_flags,
7308                                                               actions,
7309                                                               attr,
7310                                                               error);
7311                         if (ret)
7312                                 return ret;
7313                         action_flags |= MLX5_FLOW_ACTION_PORT_ID;
7314                         ++actions_n;
7315                         break;
7316                 case RTE_FLOW_ACTION_TYPE_FLAG:
7317                         ret = flow_dv_validate_action_flag(dev, action_flags,
7318                                                            attr, error);
7319                         if (ret < 0)
7320                                 return ret;
7321                         if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
7322                                 /* Count all modify-header actions as one. */
7323                                 if (!(action_flags &
7324                                       MLX5_FLOW_MODIFY_HDR_ACTIONS))
7325                                         ++actions_n;
7326                                 action_flags |= MLX5_FLOW_ACTION_FLAG |
7327                                                 MLX5_FLOW_ACTION_MARK_EXT;
7328                                 if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7329                                         modify_after_mirror = 1;
7330
7331                         } else {
7332                                 action_flags |= MLX5_FLOW_ACTION_FLAG;
7333                                 ++actions_n;
7334                         }
7335                         rw_act_num += MLX5_ACT_NUM_SET_MARK;
7336                         break;
7337                 case RTE_FLOW_ACTION_TYPE_MARK:
7338                         ret = flow_dv_validate_action_mark(dev, actions,
7339                                                            action_flags,
7340                                                            attr, error);
7341                         if (ret < 0)
7342                                 return ret;
7343                         if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
7344                                 /* Count all modify-header actions as one. */
7345                                 if (!(action_flags &
7346                                       MLX5_FLOW_MODIFY_HDR_ACTIONS))
7347                                         ++actions_n;
7348                                 action_flags |= MLX5_FLOW_ACTION_MARK |
7349                                                 MLX5_FLOW_ACTION_MARK_EXT;
7350                                 if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7351                                         modify_after_mirror = 1;
7352                         } else {
7353                                 action_flags |= MLX5_FLOW_ACTION_MARK;
7354                                 ++actions_n;
7355                         }
7356                         rw_act_num += MLX5_ACT_NUM_SET_MARK;
7357                         break;
7358                 case RTE_FLOW_ACTION_TYPE_SET_META:
7359                         ret = flow_dv_validate_action_set_meta(dev, actions,
7360                                                                action_flags,
7361                                                                attr, error);
7362                         if (ret < 0)
7363                                 return ret;
7364                         /* Count all modify-header actions as one action. */
7365                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
7366                                 ++actions_n;
7367                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7368                                 modify_after_mirror = 1;
7369                         action_flags |= MLX5_FLOW_ACTION_SET_META;
7370                         rw_act_num += MLX5_ACT_NUM_SET_META;
7371                         break;
7372                 case RTE_FLOW_ACTION_TYPE_SET_TAG:
7373                         ret = flow_dv_validate_action_set_tag(dev, actions,
7374                                                               action_flags,
7375                                                               attr, error);
7376                         if (ret < 0)
7377                                 return ret;
7378                         /* Count all modify-header actions as one action. */
7379                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
7380                                 ++actions_n;
7381                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7382                                 modify_after_mirror = 1;
7383                         action_flags |= MLX5_FLOW_ACTION_SET_TAG;
7384                         rw_act_num += MLX5_ACT_NUM_SET_TAG;
7385                         break;
7386                 case RTE_FLOW_ACTION_TYPE_DROP:
7387                         ret = mlx5_flow_validate_action_drop(action_flags,
7388                                                              attr, error);
7389                         if (ret < 0)
7390                                 return ret;
7391                         action_flags |= MLX5_FLOW_ACTION_DROP;
7392                         ++actions_n;
7393                         break;
7394                 case RTE_FLOW_ACTION_TYPE_QUEUE:
7395                         ret = mlx5_flow_validate_action_queue(actions,
7396                                                               action_flags, dev,
7397                                                               attr, error);
7398                         if (ret < 0)
7399                                 return ret;
7400                         queue_index = ((const struct rte_flow_action_queue *)
7401                                                         (actions->conf))->index;
7402                         action_flags |= MLX5_FLOW_ACTION_QUEUE;
7403                         ++actions_n;
7404                         break;
7405                 case RTE_FLOW_ACTION_TYPE_RSS:
7406                         rss = actions->conf;
7407                         ret = mlx5_flow_validate_action_rss(actions,
7408                                                             action_flags, dev,
7409                                                             attr, item_flags,
7410                                                             error);
7411                         if (ret < 0)
7412                                 return ret;
7413                         if (rss && sample_rss &&
7414                             (sample_rss->level != rss->level ||
7415                             sample_rss->types != rss->types))
7416                                 return rte_flow_error_set(error, ENOTSUP,
7417                                         RTE_FLOW_ERROR_TYPE_ACTION,
7418                                         NULL,
7419                                         "Can't use the different RSS types "
7420                                         "or level in the same flow");
7421                         if (rss != NULL && rss->queue_num)
7422                                 queue_index = rss->queue[0];
7423                         action_flags |= MLX5_FLOW_ACTION_RSS;
7424                         ++actions_n;
7425                         break;
7426                 case MLX5_RTE_FLOW_ACTION_TYPE_DEFAULT_MISS:
7427                         ret =
7428                         mlx5_flow_validate_action_default_miss(action_flags,
7429                                         attr, error);
7430                         if (ret < 0)
7431                                 return ret;
7432                         action_flags |= MLX5_FLOW_ACTION_DEFAULT_MISS;
7433                         ++actions_n;
7434                         break;
7435                 case MLX5_RTE_FLOW_ACTION_TYPE_COUNT:
7436                         shared_count = true;
7437                         /* fall-through. */
7438                 case RTE_FLOW_ACTION_TYPE_COUNT:
7439                         ret = flow_dv_validate_action_count(dev, shared_count,
7440                                                             action_flags,
7441                                                             error);
7442                         if (ret < 0)
7443                                 return ret;
7444                         action_flags |= MLX5_FLOW_ACTION_COUNT;
7445                         ++actions_n;
7446                         break;
7447                 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
7448                         if (flow_dv_validate_action_pop_vlan(dev,
7449                                                              action_flags,
7450                                                              actions,
7451                                                              item_flags, attr,
7452                                                              error))
7453                                 return -rte_errno;
7454                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7455                                 modify_after_mirror = 1;
7456                         action_flags |= MLX5_FLOW_ACTION_OF_POP_VLAN;
7457                         ++actions_n;
7458                         break;
7459                 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
7460                         ret = flow_dv_validate_action_push_vlan(dev,
7461                                                                 action_flags,
7462                                                                 vlan_m,
7463                                                                 actions, attr,
7464                                                                 error);
7465                         if (ret < 0)
7466                                 return ret;
7467                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7468                                 modify_after_mirror = 1;
7469                         action_flags |= MLX5_FLOW_ACTION_OF_PUSH_VLAN;
7470                         ++actions_n;
7471                         break;
7472                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
7473                         ret = flow_dv_validate_action_set_vlan_pcp
7474                                                 (action_flags, actions, error);
7475                         if (ret < 0)
7476                                 return ret;
7477                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7478                                 modify_after_mirror = 1;
7479                         /* Count PCP with push_vlan command. */
7480                         action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_PCP;
7481                         break;
7482                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
7483                         ret = flow_dv_validate_action_set_vlan_vid
7484                                                 (item_flags, action_flags,
7485                                                  actions, error);
7486                         if (ret < 0)
7487                                 return ret;
7488                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7489                                 modify_after_mirror = 1;
7490                         /* Count VID with push_vlan command. */
7491                         action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_VID;
7492                         rw_act_num += MLX5_ACT_NUM_MDF_VID;
7493                         break;
7494                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
7495                 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
7496                         ret = flow_dv_validate_action_l2_encap(dev,
7497                                                                action_flags,
7498                                                                actions, attr,
7499                                                                error);
7500                         if (ret < 0)
7501                                 return ret;
7502                         action_flags |= MLX5_FLOW_ACTION_ENCAP;
7503                         ++actions_n;
7504                         break;
7505                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
7506                 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
7507                         ret = flow_dv_validate_action_decap(dev, action_flags,
7508                                                             actions, item_flags,
7509                                                             attr, error);
7510                         if (ret < 0)
7511                                 return ret;
7512                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7513                                 modify_after_mirror = 1;
7514                         action_flags |= MLX5_FLOW_ACTION_DECAP;
7515                         ++actions_n;
7516                         break;
7517                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
7518                         ret = flow_dv_validate_action_raw_encap_decap
7519                                 (dev, NULL, actions->conf, attr, &action_flags,
7520                                  &actions_n, actions, item_flags, error);
7521                         if (ret < 0)
7522                                 return ret;
7523                         break;
7524                 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
7525                         decap = actions->conf;
7526                         while ((++actions)->type == RTE_FLOW_ACTION_TYPE_VOID)
7527                                 ;
7528                         if (actions->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
7529                                 encap = NULL;
7530                                 actions--;
7531                         } else {
7532                                 encap = actions->conf;
7533                         }
7534                         ret = flow_dv_validate_action_raw_encap_decap
7535                                            (dev,
7536                                             decap ? decap : &empty_decap, encap,
7537                                             attr, &action_flags, &actions_n,
7538                                             actions, item_flags, error);
7539                         if (ret < 0)
7540                                 return ret;
7541                         if ((action_flags & MLX5_FLOW_ACTION_SAMPLE) &&
7542                             (action_flags & MLX5_FLOW_ACTION_DECAP))
7543                                 modify_after_mirror = 1;
7544                         break;
7545                 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
7546                 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
7547                         ret = flow_dv_validate_action_modify_mac(action_flags,
7548                                                                  actions,
7549                                                                  item_flags,
7550                                                                  error);
7551                         if (ret < 0)
7552                                 return ret;
7553                         /* Count all modify-header actions as one action. */
7554                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
7555                                 ++actions_n;
7556                         action_flags |= actions->type ==
7557                                         RTE_FLOW_ACTION_TYPE_SET_MAC_SRC ?
7558                                                 MLX5_FLOW_ACTION_SET_MAC_SRC :
7559                                                 MLX5_FLOW_ACTION_SET_MAC_DST;
7560                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7561                                 modify_after_mirror = 1;
7562                         /*
7563                          * Even if the source and destination MAC addresses have
7564                          * overlap in the header with 4B alignment, the convert
7565                          * function will handle them separately and 4 SW actions
7566                          * will be created. And 2 actions will be added each
7567                          * time no matter how many bytes of address will be set.
7568                          */
7569                         rw_act_num += MLX5_ACT_NUM_MDF_MAC;
7570                         break;
7571                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
7572                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
7573                         ret = flow_dv_validate_action_modify_ipv4(action_flags,
7574                                                                   actions,
7575                                                                   item_flags,
7576                                                                   error);
7577                         if (ret < 0)
7578                                 return ret;
7579                         /* Count all modify-header actions as one action. */
7580                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
7581                                 ++actions_n;
7582                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7583                                 modify_after_mirror = 1;
7584                         action_flags |= actions->type ==
7585                                         RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC ?
7586                                                 MLX5_FLOW_ACTION_SET_IPV4_SRC :
7587                                                 MLX5_FLOW_ACTION_SET_IPV4_DST;
7588                         rw_act_num += MLX5_ACT_NUM_MDF_IPV4;
7589                         break;
7590                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
7591                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
7592                         ret = flow_dv_validate_action_modify_ipv6(action_flags,
7593                                                                   actions,
7594                                                                   item_flags,
7595                                                                   error);
7596                         if (ret < 0)
7597                                 return ret;
7598                         if (item_ipv6_proto == IPPROTO_ICMPV6)
7599                                 return rte_flow_error_set(error, ENOTSUP,
7600                                         RTE_FLOW_ERROR_TYPE_ACTION,
7601                                         actions,
7602                                         "Can't change header "
7603                                         "with ICMPv6 proto");
7604                         /* Count all modify-header actions as one action. */
7605                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
7606                                 ++actions_n;
7607                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7608                                 modify_after_mirror = 1;
7609                         action_flags |= actions->type ==
7610                                         RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC ?
7611                                                 MLX5_FLOW_ACTION_SET_IPV6_SRC :
7612                                                 MLX5_FLOW_ACTION_SET_IPV6_DST;
7613                         rw_act_num += MLX5_ACT_NUM_MDF_IPV6;
7614                         break;
7615                 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
7616                 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
7617                         ret = flow_dv_validate_action_modify_tp(action_flags,
7618                                                                 actions,
7619                                                                 item_flags,
7620                                                                 error);
7621                         if (ret < 0)
7622                                 return ret;
7623                         /* Count all modify-header actions as one action. */
7624                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
7625                                 ++actions_n;
7626                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7627                                 modify_after_mirror = 1;
7628                         action_flags |= actions->type ==
7629                                         RTE_FLOW_ACTION_TYPE_SET_TP_SRC ?
7630                                                 MLX5_FLOW_ACTION_SET_TP_SRC :
7631                                                 MLX5_FLOW_ACTION_SET_TP_DST;
7632                         rw_act_num += MLX5_ACT_NUM_MDF_PORT;
7633                         break;
7634                 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
7635                 case RTE_FLOW_ACTION_TYPE_SET_TTL:
7636                         ret = flow_dv_validate_action_modify_ttl(action_flags,
7637                                                                  actions,
7638                                                                  item_flags,
7639                                                                  error);
7640                         if (ret < 0)
7641                                 return ret;
7642                         /* Count all modify-header actions as one action. */
7643                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
7644                                 ++actions_n;
7645                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7646                                 modify_after_mirror = 1;
7647                         action_flags |= actions->type ==
7648                                         RTE_FLOW_ACTION_TYPE_SET_TTL ?
7649                                                 MLX5_FLOW_ACTION_SET_TTL :
7650                                                 MLX5_FLOW_ACTION_DEC_TTL;
7651                         rw_act_num += MLX5_ACT_NUM_MDF_TTL;
7652                         break;
7653                 case RTE_FLOW_ACTION_TYPE_JUMP:
7654                         ret = flow_dv_validate_action_jump(dev, tunnel, actions,
7655                                                            action_flags,
7656                                                            attr, external,
7657                                                            error);
7658                         if (ret)
7659                                 return ret;
7660                         if ((action_flags & MLX5_FLOW_ACTION_SAMPLE) &&
7661                             fdb_mirror_limit)
7662                                 return rte_flow_error_set(error, EINVAL,
7663                                                   RTE_FLOW_ERROR_TYPE_ACTION,
7664                                                   NULL,
7665                                                   "sample and jump action combination is not supported");
7666                         ++actions_n;
7667                         action_flags |= MLX5_FLOW_ACTION_JUMP;
7668                         break;
7669                 case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
7670                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
7671                         ret = flow_dv_validate_action_modify_tcp_seq
7672                                                                 (action_flags,
7673                                                                  actions,
7674                                                                  item_flags,
7675                                                                  error);
7676                         if (ret < 0)
7677                                 return ret;
7678                         /* Count all modify-header actions as one action. */
7679                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
7680                                 ++actions_n;
7681                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7682                                 modify_after_mirror = 1;
7683                         action_flags |= actions->type ==
7684                                         RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ ?
7685                                                 MLX5_FLOW_ACTION_INC_TCP_SEQ :
7686                                                 MLX5_FLOW_ACTION_DEC_TCP_SEQ;
7687                         rw_act_num += MLX5_ACT_NUM_MDF_TCPSEQ;
7688                         break;
7689                 case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
7690                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
7691                         ret = flow_dv_validate_action_modify_tcp_ack
7692                                                                 (action_flags,
7693                                                                  actions,
7694                                                                  item_flags,
7695                                                                  error);
7696                         if (ret < 0)
7697                                 return ret;
7698                         /* Count all modify-header actions as one action. */
7699                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
7700                                 ++actions_n;
7701                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7702                                 modify_after_mirror = 1;
7703                         action_flags |= actions->type ==
7704                                         RTE_FLOW_ACTION_TYPE_INC_TCP_ACK ?
7705                                                 MLX5_FLOW_ACTION_INC_TCP_ACK :
7706                                                 MLX5_FLOW_ACTION_DEC_TCP_ACK;
7707                         rw_act_num += MLX5_ACT_NUM_MDF_TCPACK;
7708                         break;
7709                 case MLX5_RTE_FLOW_ACTION_TYPE_MARK:
7710                         break;
7711                 case MLX5_RTE_FLOW_ACTION_TYPE_TAG:
7712                 case MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG:
7713                         rw_act_num += MLX5_ACT_NUM_SET_TAG;
7714                         break;
7715                 case RTE_FLOW_ACTION_TYPE_METER:
7716                         ret = mlx5_flow_validate_action_meter(dev,
7717                                                               action_flags,
7718                                                               item_flags,
7719                                                               actions, attr,
7720                                                               port_id_item,
7721                                                               &def_policy,
7722                                                               error);
7723                         if (ret < 0)
7724                                 return ret;
7725                         action_flags |= MLX5_FLOW_ACTION_METER;
7726                         if (!def_policy)
7727                                 action_flags |=
7728                                 MLX5_FLOW_ACTION_METER_WITH_TERMINATED_POLICY;
7729                         ++actions_n;
7730                         /* Meter action will add one more TAG action. */
7731                         rw_act_num += MLX5_ACT_NUM_SET_TAG;
7732                         break;
7733                 case MLX5_RTE_FLOW_ACTION_TYPE_AGE:
7734                         if (!attr->transfer && !attr->group)
7735                                 return rte_flow_error_set(error, ENOTSUP,
7736                                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7737                                                                            NULL,
7738                           "Shared ASO age action is not supported for group 0");
7739                         if (action_flags & MLX5_FLOW_ACTION_AGE)
7740                                 return rte_flow_error_set
7741                                                   (error, EINVAL,
7742                                                    RTE_FLOW_ERROR_TYPE_ACTION,
7743                                                    NULL,
7744                                                    "duplicate age actions set");
7745                         action_flags |= MLX5_FLOW_ACTION_AGE;
7746                         ++actions_n;
7747                         break;
7748                 case RTE_FLOW_ACTION_TYPE_AGE:
7749                         ret = flow_dv_validate_action_age(action_flags,
7750                                                           actions, dev,
7751                                                           error);
7752                         if (ret < 0)
7753                                 return ret;
7754                         /*
7755                          * Validate the regular AGE action (using counter)
7756                          * mutual exclusion with share counter actions.
7757                          */
7758                         if (!priv->sh->flow_hit_aso_en) {
7759                                 if (shared_count)
7760                                         return rte_flow_error_set
7761                                                 (error, EINVAL,
7762                                                 RTE_FLOW_ERROR_TYPE_ACTION,
7763                                                 NULL,
7764                                                 "old age and shared count combination is not supported");
7765                                 if (sample_count)
7766                                         return rte_flow_error_set
7767                                                 (error, EINVAL,
7768                                                 RTE_FLOW_ERROR_TYPE_ACTION,
7769                                                 NULL,
7770                                                 "old age action and count must be in the same sub flow");
7771                         }
7772                         action_flags |= MLX5_FLOW_ACTION_AGE;
7773                         ++actions_n;
7774                         break;
7775                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP:
7776                         ret = flow_dv_validate_action_modify_ipv4_dscp
7777                                                          (action_flags,
7778                                                           actions,
7779                                                           item_flags,
7780                                                           error);
7781                         if (ret < 0)
7782                                 return ret;
7783                         /* Count all modify-header actions as one action. */
7784                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
7785                                 ++actions_n;
7786                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7787                                 modify_after_mirror = 1;
7788                         action_flags |= MLX5_FLOW_ACTION_SET_IPV4_DSCP;
7789                         rw_act_num += MLX5_ACT_NUM_SET_DSCP;
7790                         break;
7791                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP:
7792                         ret = flow_dv_validate_action_modify_ipv6_dscp
7793                                                                 (action_flags,
7794                                                                  actions,
7795                                                                  item_flags,
7796                                                                  error);
7797                         if (ret < 0)
7798                                 return ret;
7799                         /* Count all modify-header actions as one action. */
7800                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
7801                                 ++actions_n;
7802                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7803                                 modify_after_mirror = 1;
7804                         action_flags |= MLX5_FLOW_ACTION_SET_IPV6_DSCP;
7805                         rw_act_num += MLX5_ACT_NUM_SET_DSCP;
7806                         break;
7807                 case RTE_FLOW_ACTION_TYPE_SAMPLE:
7808                         ret = flow_dv_validate_action_sample(&action_flags,
7809                                                              actions, dev,
7810                                                              attr, item_flags,
7811                                                              rss, &sample_rss,
7812                                                              &sample_count,
7813                                                              &fdb_mirror_limit,
7814                                                              error);
7815                         if (ret < 0)
7816                                 return ret;
7817                         action_flags |= MLX5_FLOW_ACTION_SAMPLE;
7818                         ++actions_n;
7819                         break;
7820                 case RTE_FLOW_ACTION_TYPE_MODIFY_FIELD:
7821                         ret = flow_dv_validate_action_modify_field(dev,
7822                                                                    action_flags,
7823                                                                    actions,
7824                                                                    attr,
7825                                                                    error);
7826                         if (ret < 0)
7827                                 return ret;
7828                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
7829                                 modify_after_mirror = 1;
7830                         /* Count all modify-header actions as one action. */
7831                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
7832                                 ++actions_n;
7833                         action_flags |= MLX5_FLOW_ACTION_MODIFY_FIELD;
7834                         rw_act_num += ret;
7835                         break;
7836                 case RTE_FLOW_ACTION_TYPE_CONNTRACK:
7837                         ret = flow_dv_validate_action_aso_ct(dev, action_flags,
7838                                                              item_flags, attr,
7839                                                              error);
7840                         if (ret < 0)
7841                                 return ret;
7842                         action_flags |= MLX5_FLOW_ACTION_CT;
7843                         break;
7844                 case MLX5_RTE_FLOW_ACTION_TYPE_TUNNEL_SET:
7845                         /* tunnel offload action was processed before
7846                          * list it here as a supported type
7847                          */
7848                         break;
7849                 default:
7850                         return rte_flow_error_set(error, ENOTSUP,
7851                                                   RTE_FLOW_ERROR_TYPE_ACTION,
7852                                                   actions,
7853                                                   "action not supported");
7854                 }
7855         }
7856         /*
7857          * Validate actions in flow rules
7858          * - Explicit decap action is prohibited by the tunnel offload API.
7859          * - Drop action in tunnel steer rule is prohibited by the API.
7860          * - Application cannot use MARK action because it's value can mask
7861          *   tunnel default miss nitification.
7862          * - JUMP in tunnel match rule has no support in current PMD
7863          *   implementation.
7864          * - TAG & META are reserved for future uses.
7865          */
7866         if (action_flags & MLX5_FLOW_ACTION_TUNNEL_SET) {
7867                 uint64_t bad_actions_mask = MLX5_FLOW_ACTION_DECAP    |
7868                                             MLX5_FLOW_ACTION_MARK     |
7869                                             MLX5_FLOW_ACTION_SET_TAG  |
7870                                             MLX5_FLOW_ACTION_SET_META |
7871                                             MLX5_FLOW_ACTION_DROP;
7872
7873                 if (action_flags & bad_actions_mask)
7874                         return rte_flow_error_set
7875                                         (error, EINVAL,
7876                                         RTE_FLOW_ERROR_TYPE_ACTION, NULL,
7877                                         "Invalid RTE action in tunnel "
7878                                         "set decap rule");
7879                 if (!(action_flags & MLX5_FLOW_ACTION_JUMP))
7880                         return rte_flow_error_set
7881                                         (error, EINVAL,
7882                                         RTE_FLOW_ERROR_TYPE_ACTION, NULL,
7883                                         "tunnel set decap rule must terminate "
7884                                         "with JUMP");
7885                 if (!attr->ingress)
7886                         return rte_flow_error_set
7887                                         (error, EINVAL,
7888                                         RTE_FLOW_ERROR_TYPE_ACTION, NULL,
7889                                         "tunnel flows for ingress traffic only");
7890         }
7891         if (action_flags & MLX5_FLOW_ACTION_TUNNEL_MATCH) {
7892                 uint64_t bad_actions_mask = MLX5_FLOW_ACTION_JUMP    |
7893                                             MLX5_FLOW_ACTION_MARK    |
7894                                             MLX5_FLOW_ACTION_SET_TAG |
7895                                             MLX5_FLOW_ACTION_SET_META;
7896
7897                 if (action_flags & bad_actions_mask)
7898                         return rte_flow_error_set
7899                                         (error, EINVAL,
7900                                         RTE_FLOW_ERROR_TYPE_ACTION, NULL,
7901                                         "Invalid RTE action in tunnel "
7902                                         "set match rule");
7903         }
7904         /*
7905          * Validate the drop action mutual exclusion with other actions.
7906          * Drop action is mutually-exclusive with any other action, except for
7907          * Count action.
7908          * Drop action compatibility with tunnel offload was already validated.
7909          */
7910         if (action_flags & (MLX5_FLOW_ACTION_TUNNEL_MATCH |
7911                             MLX5_FLOW_ACTION_TUNNEL_MATCH));
7912         else if ((action_flags & MLX5_FLOW_ACTION_DROP) &&
7913             (action_flags & ~(MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_COUNT)))
7914                 return rte_flow_error_set(error, EINVAL,
7915                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
7916                                           "Drop action is mutually-exclusive "
7917                                           "with any other action, except for "
7918                                           "Count action");
7919         /* Eswitch has few restrictions on using items and actions */
7920         if (attr->transfer) {
7921                 if (!mlx5_flow_ext_mreg_supported(dev) &&
7922                     action_flags & MLX5_FLOW_ACTION_FLAG)
7923                         return rte_flow_error_set(error, ENOTSUP,
7924                                                   RTE_FLOW_ERROR_TYPE_ACTION,
7925                                                   NULL,
7926                                                   "unsupported action FLAG");
7927                 if (!mlx5_flow_ext_mreg_supported(dev) &&
7928                     action_flags & MLX5_FLOW_ACTION_MARK)
7929                         return rte_flow_error_set(error, ENOTSUP,
7930                                                   RTE_FLOW_ERROR_TYPE_ACTION,
7931                                                   NULL,
7932                                                   "unsupported action MARK");
7933                 if (action_flags & MLX5_FLOW_ACTION_QUEUE)
7934                         return rte_flow_error_set(error, ENOTSUP,
7935                                                   RTE_FLOW_ERROR_TYPE_ACTION,
7936                                                   NULL,
7937                                                   "unsupported action QUEUE");
7938                 if (action_flags & MLX5_FLOW_ACTION_RSS)
7939                         return rte_flow_error_set(error, ENOTSUP,
7940                                                   RTE_FLOW_ERROR_TYPE_ACTION,
7941                                                   NULL,
7942                                                   "unsupported action RSS");
7943                 if (!(action_flags & MLX5_FLOW_FATE_ESWITCH_ACTIONS))
7944                         return rte_flow_error_set(error, EINVAL,
7945                                                   RTE_FLOW_ERROR_TYPE_ACTION,
7946                                                   actions,
7947                                                   "no fate action is found");
7948         } else {
7949                 if (!(action_flags & MLX5_FLOW_FATE_ACTIONS) && attr->ingress)
7950                         return rte_flow_error_set(error, EINVAL,
7951                                                   RTE_FLOW_ERROR_TYPE_ACTION,
7952                                                   actions,
7953                                                   "no fate action is found");
7954         }
7955         /*
7956          * Continue validation for Xcap and VLAN actions.
7957          * If hairpin is working in explicit TX rule mode, there is no actions
7958          * splitting and the validation of hairpin ingress flow should be the
7959          * same as other standard flows.
7960          */
7961         if ((action_flags & (MLX5_FLOW_XCAP_ACTIONS |
7962                              MLX5_FLOW_VLAN_ACTIONS)) &&
7963             (queue_index == 0xFFFF ||
7964              mlx5_rxq_get_type(dev, queue_index) != MLX5_RXQ_TYPE_HAIRPIN ||
7965              ((conf = mlx5_rxq_get_hairpin_conf(dev, queue_index)) != NULL &&
7966              conf->tx_explicit != 0))) {
7967                 if ((action_flags & MLX5_FLOW_XCAP_ACTIONS) ==
7968                     MLX5_FLOW_XCAP_ACTIONS)
7969                         return rte_flow_error_set(error, ENOTSUP,
7970                                                   RTE_FLOW_ERROR_TYPE_ACTION,
7971                                                   NULL, "encap and decap "
7972                                                   "combination aren't supported");
7973                 if (!attr->transfer && attr->ingress) {
7974                         if (action_flags & MLX5_FLOW_ACTION_ENCAP)
7975                                 return rte_flow_error_set
7976                                                 (error, ENOTSUP,
7977                                                  RTE_FLOW_ERROR_TYPE_ACTION,
7978                                                  NULL, "encap is not supported"
7979                                                  " for ingress traffic");
7980                         else if (action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN)
7981                                 return rte_flow_error_set
7982                                                 (error, ENOTSUP,
7983                                                  RTE_FLOW_ERROR_TYPE_ACTION,
7984                                                  NULL, "push VLAN action not "
7985                                                  "supported for ingress");
7986                         else if ((action_flags & MLX5_FLOW_VLAN_ACTIONS) ==
7987                                         MLX5_FLOW_VLAN_ACTIONS)
7988                                 return rte_flow_error_set
7989                                                 (error, ENOTSUP,
7990                                                  RTE_FLOW_ERROR_TYPE_ACTION,
7991                                                  NULL, "no support for "
7992                                                  "multiple VLAN actions");
7993                 }
7994         }
7995         if (action_flags & MLX5_FLOW_ACTION_METER_WITH_TERMINATED_POLICY) {
7996                 if ((action_flags & (MLX5_FLOW_FATE_ACTIONS &
7997                         ~MLX5_FLOW_ACTION_METER_WITH_TERMINATED_POLICY)) &&
7998                         attr->ingress)
7999                         return rte_flow_error_set
8000                                 (error, ENOTSUP,
8001                                 RTE_FLOW_ERROR_TYPE_ACTION,
8002                                 NULL, "fate action not supported for "
8003                                 "meter with policy");
8004                 if (attr->egress) {
8005                         if (action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS)
8006                                 return rte_flow_error_set
8007                                         (error, ENOTSUP,
8008                                         RTE_FLOW_ERROR_TYPE_ACTION,
8009                                         NULL, "modify header action in egress "
8010                                         "cannot be done before meter action");
8011                         if (action_flags & MLX5_FLOW_ACTION_ENCAP)
8012                                 return rte_flow_error_set
8013                                         (error, ENOTSUP,
8014                                         RTE_FLOW_ERROR_TYPE_ACTION,
8015                                         NULL, "encap action in egress "
8016                                         "cannot be done before meter action");
8017                         if (action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN)
8018                                 return rte_flow_error_set
8019                                         (error, ENOTSUP,
8020                                         RTE_FLOW_ERROR_TYPE_ACTION,
8021                                         NULL, "push vlan action in egress "
8022                                         "cannot be done before meter action");
8023                 }
8024         }
8025         /*
8026          * Hairpin flow will add one more TAG action in TX implicit mode.
8027          * In TX explicit mode, there will be no hairpin flow ID.
8028          */
8029         if (hairpin > 0)
8030                 rw_act_num += MLX5_ACT_NUM_SET_TAG;
8031         /* extra metadata enabled: one more TAG action will be add. */
8032         if (dev_conf->dv_flow_en &&
8033             dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
8034             mlx5_flow_ext_mreg_supported(dev))
8035                 rw_act_num += MLX5_ACT_NUM_SET_TAG;
8036         if (rw_act_num >
8037                         flow_dv_modify_hdr_action_max(dev, is_root)) {
8038                 return rte_flow_error_set(error, ENOTSUP,
8039                                           RTE_FLOW_ERROR_TYPE_ACTION,
8040                                           NULL, "too many header modify"
8041                                           " actions to support");
8042         }
8043         /* Eswitch egress mirror and modify flow has limitation on CX5 */
8044         if (fdb_mirror_limit && modify_after_mirror)
8045                 return rte_flow_error_set(error, EINVAL,
8046                                 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
8047                                 "sample before modify action is not supported");
8048         return 0;
8049 }
8050
8051 /**
8052  * Internal preparation function. Allocates the DV flow size,
8053  * this size is constant.
8054  *
8055  * @param[in] dev
8056  *   Pointer to the rte_eth_dev structure.
8057  * @param[in] attr
8058  *   Pointer to the flow attributes.
8059  * @param[in] items
8060  *   Pointer to the list of items.
8061  * @param[in] actions
8062  *   Pointer to the list of actions.
8063  * @param[out] error
8064  *   Pointer to the error structure.
8065  *
8066  * @return
8067  *   Pointer to mlx5_flow object on success,
8068  *   otherwise NULL and rte_errno is set.
8069  */
8070 static struct mlx5_flow *
8071 flow_dv_prepare(struct rte_eth_dev *dev,
8072                 const struct rte_flow_attr *attr __rte_unused,
8073                 const struct rte_flow_item items[] __rte_unused,
8074                 const struct rte_flow_action actions[] __rte_unused,
8075                 struct rte_flow_error *error)
8076 {
8077         uint32_t handle_idx = 0;
8078         struct mlx5_flow *dev_flow;
8079         struct mlx5_flow_handle *dev_handle;
8080         struct mlx5_priv *priv = dev->data->dev_private;
8081         struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
8082
8083         MLX5_ASSERT(wks);
8084         wks->skip_matcher_reg = 0;
8085         wks->policy = NULL;
8086         wks->final_policy = NULL;
8087         /* In case of corrupting the memory. */
8088         if (wks->flow_idx >= MLX5_NUM_MAX_DEV_FLOWS) {
8089                 rte_flow_error_set(error, ENOSPC,
8090                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
8091                                    "not free temporary device flow");
8092                 return NULL;
8093         }
8094         dev_handle = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
8095                                    &handle_idx);
8096         if (!dev_handle) {
8097                 rte_flow_error_set(error, ENOMEM,
8098                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
8099                                    "not enough memory to create flow handle");
8100                 return NULL;
8101         }
8102         MLX5_ASSERT(wks->flow_idx < RTE_DIM(wks->flows));
8103         dev_flow = &wks->flows[wks->flow_idx++];
8104         memset(dev_flow, 0, sizeof(*dev_flow));
8105         dev_flow->handle = dev_handle;
8106         dev_flow->handle_idx = handle_idx;
8107         dev_flow->dv.value.size = MLX5_ST_SZ_BYTES(fte_match_param);
8108         dev_flow->ingress = attr->ingress;
8109         dev_flow->dv.transfer = attr->transfer;
8110         return dev_flow;
8111 }
8112
8113 #ifdef RTE_LIBRTE_MLX5_DEBUG
8114 /**
8115  * Sanity check for match mask and value. Similar to check_valid_spec() in
8116  * kernel driver. If unmasked bit is present in value, it returns failure.
8117  *
8118  * @param match_mask
8119  *   pointer to match mask buffer.
8120  * @param match_value
8121  *   pointer to match value buffer.
8122  *
8123  * @return
8124  *   0 if valid, -EINVAL otherwise.
8125  */
8126 static int
8127 flow_dv_check_valid_spec(void *match_mask, void *match_value)
8128 {
8129         uint8_t *m = match_mask;
8130         uint8_t *v = match_value;
8131         unsigned int i;
8132
8133         for (i = 0; i < MLX5_ST_SZ_BYTES(fte_match_param); ++i) {
8134                 if (v[i] & ~m[i]) {
8135                         DRV_LOG(ERR,
8136                                 "match_value differs from match_criteria"
8137                                 " %p[%u] != %p[%u]",
8138                                 match_value, i, match_mask, i);
8139                         return -EINVAL;
8140                 }
8141         }
8142         return 0;
8143 }
8144 #endif
8145
8146 /**
8147  * Add match of ip_version.
8148  *
8149  * @param[in] group
8150  *   Flow group.
8151  * @param[in] headers_v
8152  *   Values header pointer.
8153  * @param[in] headers_m
8154  *   Masks header pointer.
8155  * @param[in] ip_version
8156  *   The IP version to set.
8157  */
8158 static inline void
8159 flow_dv_set_match_ip_version(uint32_t group,
8160                              void *headers_v,
8161                              void *headers_m,
8162                              uint8_t ip_version)
8163 {
8164         if (group == 0)
8165                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0xf);
8166         else
8167                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version,
8168                          ip_version);
8169         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_version, ip_version);
8170         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype, 0);
8171         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ethertype, 0);
8172 }
8173
8174 /**
8175  * Add Ethernet item to matcher and to the value.
8176  *
8177  * @param[in, out] matcher
8178  *   Flow matcher.
8179  * @param[in, out] key
8180  *   Flow matcher value.
8181  * @param[in] item
8182  *   Flow pattern to translate.
8183  * @param[in] inner
8184  *   Item is inner pattern.
8185  */
8186 static void
8187 flow_dv_translate_item_eth(void *matcher, void *key,
8188                            const struct rte_flow_item *item, int inner,
8189                            uint32_t group)
8190 {
8191         const struct rte_flow_item_eth *eth_m = item->mask;
8192         const struct rte_flow_item_eth *eth_v = item->spec;
8193         const struct rte_flow_item_eth nic_mask = {
8194                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
8195                 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
8196                 .type = RTE_BE16(0xffff),
8197                 .has_vlan = 0,
8198         };
8199         void *hdrs_m;
8200         void *hdrs_v;
8201         char *l24_v;
8202         unsigned int i;
8203
8204         if (!eth_v)
8205                 return;
8206         if (!eth_m)
8207                 eth_m = &nic_mask;
8208         if (inner) {
8209                 hdrs_m = MLX5_ADDR_OF(fte_match_param, matcher,
8210                                          inner_headers);
8211                 hdrs_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
8212         } else {
8213                 hdrs_m = MLX5_ADDR_OF(fte_match_param, matcher,
8214                                          outer_headers);
8215                 hdrs_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
8216         }
8217         memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, hdrs_m, dmac_47_16),
8218                &eth_m->dst, sizeof(eth_m->dst));
8219         /* The value must be in the range of the mask. */
8220         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, hdrs_v, dmac_47_16);
8221         for (i = 0; i < sizeof(eth_m->dst); ++i)
8222                 l24_v[i] = eth_m->dst.addr_bytes[i] & eth_v->dst.addr_bytes[i];
8223         memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, hdrs_m, smac_47_16),
8224                &eth_m->src, sizeof(eth_m->src));
8225         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, hdrs_v, smac_47_16);
8226         /* The value must be in the range of the mask. */
8227         for (i = 0; i < sizeof(eth_m->dst); ++i)
8228                 l24_v[i] = eth_m->src.addr_bytes[i] & eth_v->src.addr_bytes[i];
8229         /*
8230          * HW supports match on one Ethertype, the Ethertype following the last
8231          * VLAN tag of the packet (see PRM).
8232          * Set match on ethertype only if ETH header is not followed by VLAN.
8233          * HW is optimized for IPv4/IPv6. In such cases, avoid setting
8234          * ethertype, and use ip_version field instead.
8235          * eCPRI over Ether layer will use type value 0xAEFE.
8236          */
8237         if (eth_m->type == 0xFFFF) {
8238                 /* Set cvlan_tag mask for any single\multi\un-tagged case. */
8239                 MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, cvlan_tag, 1);
8240                 switch (eth_v->type) {
8241                 case RTE_BE16(RTE_ETHER_TYPE_VLAN):
8242                         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, cvlan_tag, 1);
8243                         return;
8244                 case RTE_BE16(RTE_ETHER_TYPE_QINQ):
8245                         MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, svlan_tag, 1);
8246                         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, svlan_tag, 1);
8247                         return;
8248                 case RTE_BE16(RTE_ETHER_TYPE_IPV4):
8249                         flow_dv_set_match_ip_version(group, hdrs_v, hdrs_m, 4);
8250                         return;
8251                 case RTE_BE16(RTE_ETHER_TYPE_IPV6):
8252                         flow_dv_set_match_ip_version(group, hdrs_v, hdrs_m, 6);
8253                         return;
8254                 default:
8255                         break;
8256                 }
8257         }
8258         if (eth_m->has_vlan) {
8259                 MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, cvlan_tag, 1);
8260                 if (eth_v->has_vlan) {
8261                         /*
8262                          * Here, when also has_more_vlan field in VLAN item is
8263                          * not set, only single-tagged packets will be matched.
8264                          */
8265                         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, cvlan_tag, 1);
8266                         return;
8267                 }
8268         }
8269         MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, ethertype,
8270                  rte_be_to_cpu_16(eth_m->type));
8271         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, hdrs_v, ethertype);
8272         *(uint16_t *)(l24_v) = eth_m->type & eth_v->type;
8273 }
8274
8275 /**
8276  * Add VLAN item to matcher and to the value.
8277  *
8278  * @param[in, out] dev_flow
8279  *   Flow descriptor.
8280  * @param[in, out] matcher
8281  *   Flow matcher.
8282  * @param[in, out] key
8283  *   Flow matcher value.
8284  * @param[in] item
8285  *   Flow pattern to translate.
8286  * @param[in] inner
8287  *   Item is inner pattern.
8288  */
8289 static void
8290 flow_dv_translate_item_vlan(struct mlx5_flow *dev_flow,
8291                             void *matcher, void *key,
8292                             const struct rte_flow_item *item,
8293                             int inner, uint32_t group)
8294 {
8295         const struct rte_flow_item_vlan *vlan_m = item->mask;
8296         const struct rte_flow_item_vlan *vlan_v = item->spec;
8297         void *hdrs_m;
8298         void *hdrs_v;
8299         uint16_t tci_m;
8300         uint16_t tci_v;
8301
8302         if (inner) {
8303                 hdrs_m = MLX5_ADDR_OF(fte_match_param, matcher,
8304                                          inner_headers);
8305                 hdrs_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
8306         } else {
8307                 hdrs_m = MLX5_ADDR_OF(fte_match_param, matcher,
8308                                          outer_headers);
8309                 hdrs_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
8310                 /*
8311                  * This is workaround, masks are not supported,
8312                  * and pre-validated.
8313                  */
8314                 if (vlan_v)
8315                         dev_flow->handle->vf_vlan.tag =
8316                                         rte_be_to_cpu_16(vlan_v->tci) & 0x0fff;
8317         }
8318         /*
8319          * When VLAN item exists in flow, mark packet as tagged,
8320          * even if TCI is not specified.
8321          */
8322         if (!MLX5_GET(fte_match_set_lyr_2_4, hdrs_v, svlan_tag)) {
8323                 MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, cvlan_tag, 1);
8324                 MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, cvlan_tag, 1);
8325         }
8326         if (!vlan_v)
8327                 return;
8328         if (!vlan_m)
8329                 vlan_m = &rte_flow_item_vlan_mask;
8330         tci_m = rte_be_to_cpu_16(vlan_m->tci);
8331         tci_v = rte_be_to_cpu_16(vlan_m->tci & vlan_v->tci);
8332         MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, first_vid, tci_m);
8333         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, first_vid, tci_v);
8334         MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, first_cfi, tci_m >> 12);
8335         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, first_cfi, tci_v >> 12);
8336         MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, first_prio, tci_m >> 13);
8337         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, first_prio, tci_v >> 13);
8338         /*
8339          * HW is optimized for IPv4/IPv6. In such cases, avoid setting
8340          * ethertype, and use ip_version field instead.
8341          */
8342         if (vlan_m->inner_type == 0xFFFF) {
8343                 switch (vlan_v->inner_type) {
8344                 case RTE_BE16(RTE_ETHER_TYPE_VLAN):
8345                         MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, svlan_tag, 1);
8346                         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, svlan_tag, 1);
8347                         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, cvlan_tag, 0);
8348                         return;
8349                 case RTE_BE16(RTE_ETHER_TYPE_IPV4):
8350                         flow_dv_set_match_ip_version(group, hdrs_v, hdrs_m, 4);
8351                         return;
8352                 case RTE_BE16(RTE_ETHER_TYPE_IPV6):
8353                         flow_dv_set_match_ip_version(group, hdrs_v, hdrs_m, 6);
8354                         return;
8355                 default:
8356                         break;
8357                 }
8358         }
8359         if (vlan_m->has_more_vlan && vlan_v->has_more_vlan) {
8360                 MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, svlan_tag, 1);
8361                 MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, svlan_tag, 1);
8362                 /* Only one vlan_tag bit can be set. */
8363                 MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, cvlan_tag, 0);
8364                 return;
8365         }
8366         MLX5_SET(fte_match_set_lyr_2_4, hdrs_m, ethertype,
8367                  rte_be_to_cpu_16(vlan_m->inner_type));
8368         MLX5_SET(fte_match_set_lyr_2_4, hdrs_v, ethertype,
8369                  rte_be_to_cpu_16(vlan_m->inner_type & vlan_v->inner_type));
8370 }
8371
8372 /**
8373  * Add IPV4 item to matcher and to the value.
8374  *
8375  * @param[in, out] matcher
8376  *   Flow matcher.
8377  * @param[in, out] key
8378  *   Flow matcher value.
8379  * @param[in] item
8380  *   Flow pattern to translate.
8381  * @param[in] inner
8382  *   Item is inner pattern.
8383  * @param[in] group
8384  *   The group to insert the rule.
8385  */
8386 static void
8387 flow_dv_translate_item_ipv4(void *matcher, void *key,
8388                             const struct rte_flow_item *item,
8389                             int inner, uint32_t group)
8390 {
8391         const struct rte_flow_item_ipv4 *ipv4_m = item->mask;
8392         const struct rte_flow_item_ipv4 *ipv4_v = item->spec;
8393         const struct rte_flow_item_ipv4 nic_mask = {
8394                 .hdr = {
8395                         .src_addr = RTE_BE32(0xffffffff),
8396                         .dst_addr = RTE_BE32(0xffffffff),
8397                         .type_of_service = 0xff,
8398                         .next_proto_id = 0xff,
8399                         .time_to_live = 0xff,
8400                 },
8401         };
8402         void *headers_m;
8403         void *headers_v;
8404         char *l24_m;
8405         char *l24_v;
8406         uint8_t tos, ihl_m, ihl_v;
8407
8408         if (inner) {
8409                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8410                                          inner_headers);
8411                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
8412         } else {
8413                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8414                                          outer_headers);
8415                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
8416         }
8417         flow_dv_set_match_ip_version(group, headers_v, headers_m, 4);
8418         if (!ipv4_v)
8419                 return;
8420         if (!ipv4_m)
8421                 ipv4_m = &nic_mask;
8422         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
8423                              dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
8424         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
8425                              dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
8426         *(uint32_t *)l24_m = ipv4_m->hdr.dst_addr;
8427         *(uint32_t *)l24_v = ipv4_m->hdr.dst_addr & ipv4_v->hdr.dst_addr;
8428         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
8429                           src_ipv4_src_ipv6.ipv4_layout.ipv4);
8430         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
8431                           src_ipv4_src_ipv6.ipv4_layout.ipv4);
8432         *(uint32_t *)l24_m = ipv4_m->hdr.src_addr;
8433         *(uint32_t *)l24_v = ipv4_m->hdr.src_addr & ipv4_v->hdr.src_addr;
8434         tos = ipv4_m->hdr.type_of_service & ipv4_v->hdr.type_of_service;
8435         ihl_m = ipv4_m->hdr.version_ihl & RTE_IPV4_HDR_IHL_MASK;
8436         ihl_v = ipv4_v->hdr.version_ihl & RTE_IPV4_HDR_IHL_MASK;
8437         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ipv4_ihl, ihl_m);
8438         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ipv4_ihl, ihl_m & ihl_v);
8439         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn,
8440                  ipv4_m->hdr.type_of_service);
8441         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, tos);
8442         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp,
8443                  ipv4_m->hdr.type_of_service >> 2);
8444         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, tos >> 2);
8445         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
8446                  ipv4_m->hdr.next_proto_id);
8447         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
8448                  ipv4_v->hdr.next_proto_id & ipv4_m->hdr.next_proto_id);
8449         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ttl_hoplimit,
8450                  ipv4_m->hdr.time_to_live);
8451         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ttl_hoplimit,
8452                  ipv4_v->hdr.time_to_live & ipv4_m->hdr.time_to_live);
8453         MLX5_SET(fte_match_set_lyr_2_4, headers_m, frag,
8454                  !!(ipv4_m->hdr.fragment_offset));
8455         MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag,
8456                  !!(ipv4_v->hdr.fragment_offset & ipv4_m->hdr.fragment_offset));
8457 }
8458
8459 /**
8460  * Add IPV6 item to matcher and to the value.
8461  *
8462  * @param[in, out] matcher
8463  *   Flow matcher.
8464  * @param[in, out] key
8465  *   Flow matcher value.
8466  * @param[in] item
8467  *   Flow pattern to translate.
8468  * @param[in] inner
8469  *   Item is inner pattern.
8470  * @param[in] group
8471  *   The group to insert the rule.
8472  */
8473 static void
8474 flow_dv_translate_item_ipv6(void *matcher, void *key,
8475                             const struct rte_flow_item *item,
8476                             int inner, uint32_t group)
8477 {
8478         const struct rte_flow_item_ipv6 *ipv6_m = item->mask;
8479         const struct rte_flow_item_ipv6 *ipv6_v = item->spec;
8480         const struct rte_flow_item_ipv6 nic_mask = {
8481                 .hdr = {
8482                         .src_addr =
8483                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
8484                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
8485                         .dst_addr =
8486                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
8487                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
8488                         .vtc_flow = RTE_BE32(0xffffffff),
8489                         .proto = 0xff,
8490                         .hop_limits = 0xff,
8491                 },
8492         };
8493         void *headers_m;
8494         void *headers_v;
8495         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
8496         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
8497         char *l24_m;
8498         char *l24_v;
8499         uint32_t vtc_m;
8500         uint32_t vtc_v;
8501         int i;
8502         int size;
8503
8504         if (inner) {
8505                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8506                                          inner_headers);
8507                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
8508         } else {
8509                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8510                                          outer_headers);
8511                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
8512         }
8513         flow_dv_set_match_ip_version(group, headers_v, headers_m, 6);
8514         if (!ipv6_v)
8515                 return;
8516         if (!ipv6_m)
8517                 ipv6_m = &nic_mask;
8518         size = sizeof(ipv6_m->hdr.dst_addr);
8519         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
8520                              dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
8521         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
8522                              dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
8523         memcpy(l24_m, ipv6_m->hdr.dst_addr, size);
8524         for (i = 0; i < size; ++i)
8525                 l24_v[i] = l24_m[i] & ipv6_v->hdr.dst_addr[i];
8526         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
8527                              src_ipv4_src_ipv6.ipv6_layout.ipv6);
8528         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
8529                              src_ipv4_src_ipv6.ipv6_layout.ipv6);
8530         memcpy(l24_m, ipv6_m->hdr.src_addr, size);
8531         for (i = 0; i < size; ++i)
8532                 l24_v[i] = l24_m[i] & ipv6_v->hdr.src_addr[i];
8533         /* TOS. */
8534         vtc_m = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow);
8535         vtc_v = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow & ipv6_v->hdr.vtc_flow);
8536         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn, vtc_m >> 20);
8537         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, vtc_v >> 20);
8538         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp, vtc_m >> 22);
8539         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, vtc_v >> 22);
8540         /* Label. */
8541         if (inner) {
8542                 MLX5_SET(fte_match_set_misc, misc_m, inner_ipv6_flow_label,
8543                          vtc_m);
8544                 MLX5_SET(fte_match_set_misc, misc_v, inner_ipv6_flow_label,
8545                          vtc_v);
8546         } else {
8547                 MLX5_SET(fte_match_set_misc, misc_m, outer_ipv6_flow_label,
8548                          vtc_m);
8549                 MLX5_SET(fte_match_set_misc, misc_v, outer_ipv6_flow_label,
8550                          vtc_v);
8551         }
8552         /* Protocol. */
8553         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
8554                  ipv6_m->hdr.proto);
8555         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
8556                  ipv6_v->hdr.proto & ipv6_m->hdr.proto);
8557         /* Hop limit. */
8558         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ttl_hoplimit,
8559                  ipv6_m->hdr.hop_limits);
8560         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ttl_hoplimit,
8561                  ipv6_v->hdr.hop_limits & ipv6_m->hdr.hop_limits);
8562         MLX5_SET(fte_match_set_lyr_2_4, headers_m, frag,
8563                  !!(ipv6_m->has_frag_ext));
8564         MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag,
8565                  !!(ipv6_v->has_frag_ext & ipv6_m->has_frag_ext));
8566 }
8567
8568 /**
8569  * Add IPV6 fragment extension item to matcher and to the value.
8570  *
8571  * @param[in, out] matcher
8572  *   Flow matcher.
8573  * @param[in, out] key
8574  *   Flow matcher value.
8575  * @param[in] item
8576  *   Flow pattern to translate.
8577  * @param[in] inner
8578  *   Item is inner pattern.
8579  */
8580 static void
8581 flow_dv_translate_item_ipv6_frag_ext(void *matcher, void *key,
8582                                      const struct rte_flow_item *item,
8583                                      int inner)
8584 {
8585         const struct rte_flow_item_ipv6_frag_ext *ipv6_frag_ext_m = item->mask;
8586         const struct rte_flow_item_ipv6_frag_ext *ipv6_frag_ext_v = item->spec;
8587         const struct rte_flow_item_ipv6_frag_ext nic_mask = {
8588                 .hdr = {
8589                         .next_header = 0xff,
8590                         .frag_data = RTE_BE16(0xffff),
8591                 },
8592         };
8593         void *headers_m;
8594         void *headers_v;
8595
8596         if (inner) {
8597                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8598                                          inner_headers);
8599                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
8600         } else {
8601                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8602                                          outer_headers);
8603                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
8604         }
8605         /* IPv6 fragment extension item exists, so packet is IP fragment. */
8606         MLX5_SET(fte_match_set_lyr_2_4, headers_m, frag, 1);
8607         MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag, 1);
8608         if (!ipv6_frag_ext_v)
8609                 return;
8610         if (!ipv6_frag_ext_m)
8611                 ipv6_frag_ext_m = &nic_mask;
8612         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
8613                  ipv6_frag_ext_m->hdr.next_header);
8614         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
8615                  ipv6_frag_ext_v->hdr.next_header &
8616                  ipv6_frag_ext_m->hdr.next_header);
8617 }
8618
8619 /**
8620  * Add TCP item to matcher and to the value.
8621  *
8622  * @param[in, out] matcher
8623  *   Flow matcher.
8624  * @param[in, out] key
8625  *   Flow matcher value.
8626  * @param[in] item
8627  *   Flow pattern to translate.
8628  * @param[in] inner
8629  *   Item is inner pattern.
8630  */
8631 static void
8632 flow_dv_translate_item_tcp(void *matcher, void *key,
8633                            const struct rte_flow_item *item,
8634                            int inner)
8635 {
8636         const struct rte_flow_item_tcp *tcp_m = item->mask;
8637         const struct rte_flow_item_tcp *tcp_v = item->spec;
8638         void *headers_m;
8639         void *headers_v;
8640
8641         if (inner) {
8642                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8643                                          inner_headers);
8644                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
8645         } else {
8646                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8647                                          outer_headers);
8648                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
8649         }
8650         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
8651         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_TCP);
8652         if (!tcp_v)
8653                 return;
8654         if (!tcp_m)
8655                 tcp_m = &rte_flow_item_tcp_mask;
8656         MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_sport,
8657                  rte_be_to_cpu_16(tcp_m->hdr.src_port));
8658         MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
8659                  rte_be_to_cpu_16(tcp_v->hdr.src_port & tcp_m->hdr.src_port));
8660         MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_dport,
8661                  rte_be_to_cpu_16(tcp_m->hdr.dst_port));
8662         MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
8663                  rte_be_to_cpu_16(tcp_v->hdr.dst_port & tcp_m->hdr.dst_port));
8664         MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_flags,
8665                  tcp_m->hdr.tcp_flags);
8666         MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_flags,
8667                  (tcp_v->hdr.tcp_flags & tcp_m->hdr.tcp_flags));
8668 }
8669
8670 /**
8671  * Add UDP item to matcher and to the value.
8672  *
8673  * @param[in, out] matcher
8674  *   Flow matcher.
8675  * @param[in, out] key
8676  *   Flow matcher value.
8677  * @param[in] item
8678  *   Flow pattern to translate.
8679  * @param[in] inner
8680  *   Item is inner pattern.
8681  */
8682 static void
8683 flow_dv_translate_item_udp(void *matcher, void *key,
8684                            const struct rte_flow_item *item,
8685                            int inner)
8686 {
8687         const struct rte_flow_item_udp *udp_m = item->mask;
8688         const struct rte_flow_item_udp *udp_v = item->spec;
8689         void *headers_m;
8690         void *headers_v;
8691
8692         if (inner) {
8693                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8694                                          inner_headers);
8695                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
8696         } else {
8697                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8698                                          outer_headers);
8699                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
8700         }
8701         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
8702         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_UDP);
8703         if (!udp_v)
8704                 return;
8705         if (!udp_m)
8706                 udp_m = &rte_flow_item_udp_mask;
8707         MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_sport,
8708                  rte_be_to_cpu_16(udp_m->hdr.src_port));
8709         MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
8710                  rte_be_to_cpu_16(udp_v->hdr.src_port & udp_m->hdr.src_port));
8711         MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport,
8712                  rte_be_to_cpu_16(udp_m->hdr.dst_port));
8713         MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
8714                  rte_be_to_cpu_16(udp_v->hdr.dst_port & udp_m->hdr.dst_port));
8715 }
8716
8717 /**
8718  * Add GRE optional Key item to matcher and to the value.
8719  *
8720  * @param[in, out] matcher
8721  *   Flow matcher.
8722  * @param[in, out] key
8723  *   Flow matcher value.
8724  * @param[in] item
8725  *   Flow pattern to translate.
8726  * @param[in] inner
8727  *   Item is inner pattern.
8728  */
8729 static void
8730 flow_dv_translate_item_gre_key(void *matcher, void *key,
8731                                    const struct rte_flow_item *item)
8732 {
8733         const rte_be32_t *key_m = item->mask;
8734         const rte_be32_t *key_v = item->spec;
8735         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
8736         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
8737         rte_be32_t gre_key_default_mask = RTE_BE32(UINT32_MAX);
8738
8739         /* GRE K bit must be on and should already be validated */
8740         MLX5_SET(fte_match_set_misc, misc_m, gre_k_present, 1);
8741         MLX5_SET(fte_match_set_misc, misc_v, gre_k_present, 1);
8742         if (!key_v)
8743                 return;
8744         if (!key_m)
8745                 key_m = &gre_key_default_mask;
8746         MLX5_SET(fte_match_set_misc, misc_m, gre_key_h,
8747                  rte_be_to_cpu_32(*key_m) >> 8);
8748         MLX5_SET(fte_match_set_misc, misc_v, gre_key_h,
8749                  rte_be_to_cpu_32((*key_v) & (*key_m)) >> 8);
8750         MLX5_SET(fte_match_set_misc, misc_m, gre_key_l,
8751                  rte_be_to_cpu_32(*key_m) & 0xFF);
8752         MLX5_SET(fte_match_set_misc, misc_v, gre_key_l,
8753                  rte_be_to_cpu_32((*key_v) & (*key_m)) & 0xFF);
8754 }
8755
8756 /**
8757  * Add GRE item to matcher and to the value.
8758  *
8759  * @param[in, out] matcher
8760  *   Flow matcher.
8761  * @param[in, out] key
8762  *   Flow matcher value.
8763  * @param[in] item
8764  *   Flow pattern to translate.
8765  * @param[in] pattern_flags
8766  *   Accumulated pattern flags.
8767  */
8768 static void
8769 flow_dv_translate_item_gre(void *matcher, void *key,
8770                            const struct rte_flow_item *item,
8771                            uint64_t pattern_flags)
8772 {
8773         static const struct rte_flow_item_gre empty_gre = {0,};
8774         const struct rte_flow_item_gre *gre_m = item->mask;
8775         const struct rte_flow_item_gre *gre_v = item->spec;
8776         void *headers_m = MLX5_ADDR_OF(fte_match_param, matcher, outer_headers);
8777         void *headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
8778         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
8779         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
8780         struct {
8781                 union {
8782                         __extension__
8783                         struct {
8784                                 uint16_t version:3;
8785                                 uint16_t rsvd0:9;
8786                                 uint16_t s_present:1;
8787                                 uint16_t k_present:1;
8788                                 uint16_t rsvd_bit1:1;
8789                                 uint16_t c_present:1;
8790                         };
8791                         uint16_t value;
8792                 };
8793         } gre_crks_rsvd0_ver_m, gre_crks_rsvd0_ver_v;
8794         uint16_t protocol_m, protocol_v;
8795
8796         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
8797         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_GRE);
8798         if (!gre_v) {
8799                 gre_v = &empty_gre;
8800                 gre_m = &empty_gre;
8801         } else {
8802                 if (!gre_m)
8803                         gre_m = &rte_flow_item_gre_mask;
8804         }
8805         gre_crks_rsvd0_ver_m.value = rte_be_to_cpu_16(gre_m->c_rsvd0_ver);
8806         gre_crks_rsvd0_ver_v.value = rte_be_to_cpu_16(gre_v->c_rsvd0_ver);
8807         MLX5_SET(fte_match_set_misc, misc_m, gre_c_present,
8808                  gre_crks_rsvd0_ver_m.c_present);
8809         MLX5_SET(fte_match_set_misc, misc_v, gre_c_present,
8810                  gre_crks_rsvd0_ver_v.c_present &
8811                  gre_crks_rsvd0_ver_m.c_present);
8812         MLX5_SET(fte_match_set_misc, misc_m, gre_k_present,
8813                  gre_crks_rsvd0_ver_m.k_present);
8814         MLX5_SET(fte_match_set_misc, misc_v, gre_k_present,
8815                  gre_crks_rsvd0_ver_v.k_present &
8816                  gre_crks_rsvd0_ver_m.k_present);
8817         MLX5_SET(fte_match_set_misc, misc_m, gre_s_present,
8818                  gre_crks_rsvd0_ver_m.s_present);
8819         MLX5_SET(fte_match_set_misc, misc_v, gre_s_present,
8820                  gre_crks_rsvd0_ver_v.s_present &
8821                  gre_crks_rsvd0_ver_m.s_present);
8822         protocol_m = rte_be_to_cpu_16(gre_m->protocol);
8823         protocol_v = rte_be_to_cpu_16(gre_v->protocol);
8824         if (!protocol_m) {
8825                 /* Force next protocol to prevent matchers duplication */
8826                 protocol_m = 0xFFFF;
8827                 protocol_v = mlx5_translate_tunnel_etypes(pattern_flags);
8828         }
8829         MLX5_SET(fte_match_set_misc, misc_m, gre_protocol, protocol_m);
8830         MLX5_SET(fte_match_set_misc, misc_v, gre_protocol,
8831                  protocol_m & protocol_v);
8832 }
8833
8834 /**
8835  * Add NVGRE item to matcher and to the value.
8836  *
8837  * @param[in, out] matcher
8838  *   Flow matcher.
8839  * @param[in, out] key
8840  *   Flow matcher value.
8841  * @param[in] item
8842  *   Flow pattern to translate.
8843  * @param[in] pattern_flags
8844  *   Accumulated pattern flags.
8845  */
8846 static void
8847 flow_dv_translate_item_nvgre(void *matcher, void *key,
8848                              const struct rte_flow_item *item,
8849                              unsigned long pattern_flags)
8850 {
8851         const struct rte_flow_item_nvgre *nvgre_m = item->mask;
8852         const struct rte_flow_item_nvgre *nvgre_v = item->spec;
8853         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
8854         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
8855         const char *tni_flow_id_m;
8856         const char *tni_flow_id_v;
8857         char *gre_key_m;
8858         char *gre_key_v;
8859         int size;
8860         int i;
8861
8862         /* For NVGRE, GRE header fields must be set with defined values. */
8863         const struct rte_flow_item_gre gre_spec = {
8864                 .c_rsvd0_ver = RTE_BE16(0x2000),
8865                 .protocol = RTE_BE16(RTE_ETHER_TYPE_TEB)
8866         };
8867         const struct rte_flow_item_gre gre_mask = {
8868                 .c_rsvd0_ver = RTE_BE16(0xB000),
8869                 .protocol = RTE_BE16(UINT16_MAX),
8870         };
8871         const struct rte_flow_item gre_item = {
8872                 .spec = &gre_spec,
8873                 .mask = &gre_mask,
8874                 .last = NULL,
8875         };
8876         flow_dv_translate_item_gre(matcher, key, &gre_item, pattern_flags);
8877         if (!nvgre_v)
8878                 return;
8879         if (!nvgre_m)
8880                 nvgre_m = &rte_flow_item_nvgre_mask;
8881         tni_flow_id_m = (const char *)nvgre_m->tni;
8882         tni_flow_id_v = (const char *)nvgre_v->tni;
8883         size = sizeof(nvgre_m->tni) + sizeof(nvgre_m->flow_id);
8884         gre_key_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, gre_key_h);
8885         gre_key_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, gre_key_h);
8886         memcpy(gre_key_m, tni_flow_id_m, size);
8887         for (i = 0; i < size; ++i)
8888                 gre_key_v[i] = gre_key_m[i] & tni_flow_id_v[i];
8889 }
8890
8891 /**
8892  * Add VXLAN item to matcher and to the value.
8893  *
8894  * @param[in] dev
8895  *   Pointer to the Ethernet device structure.
8896  * @param[in] attr
8897  *   Flow rule attributes.
8898  * @param[in, out] matcher
8899  *   Flow matcher.
8900  * @param[in, out] key
8901  *   Flow matcher value.
8902  * @param[in] item
8903  *   Flow pattern to translate.
8904  * @param[in] inner
8905  *   Item is inner pattern.
8906  */
8907 static void
8908 flow_dv_translate_item_vxlan(struct rte_eth_dev *dev,
8909                              const struct rte_flow_attr *attr,
8910                              void *matcher, void *key,
8911                              const struct rte_flow_item *item,
8912                              int inner)
8913 {
8914         const struct rte_flow_item_vxlan *vxlan_m = item->mask;
8915         const struct rte_flow_item_vxlan *vxlan_v = item->spec;
8916         void *headers_m;
8917         void *headers_v;
8918         void *misc5_m;
8919         void *misc5_v;
8920         uint32_t *tunnel_header_v;
8921         uint32_t *tunnel_header_m;
8922         uint16_t dport;
8923         struct mlx5_priv *priv = dev->data->dev_private;
8924         const struct rte_flow_item_vxlan nic_mask = {
8925                 .vni = "\xff\xff\xff",
8926                 .rsvd1 = 0xff,
8927         };
8928
8929         if (inner) {
8930                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8931                                          inner_headers);
8932                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
8933         } else {
8934                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
8935                                          outer_headers);
8936                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
8937         }
8938         dport = item->type == RTE_FLOW_ITEM_TYPE_VXLAN ?
8939                 MLX5_UDP_PORT_VXLAN : MLX5_UDP_PORT_VXLAN_GPE;
8940         if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
8941                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
8942                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
8943         }
8944         dport = MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport);
8945         if (!vxlan_v)
8946                 return;
8947         if (!vxlan_m) {
8948                 if ((!attr->group && !priv->sh->tunnel_header_0_1) ||
8949                     (attr->group && !priv->sh->misc5_cap))
8950                         vxlan_m = &rte_flow_item_vxlan_mask;
8951                 else
8952                         vxlan_m = &nic_mask;
8953         }
8954         if ((priv->sh->steering_format_version ==
8955             MLX5_STEERING_LOGIC_FORMAT_CONNECTX_5 &&
8956             dport != MLX5_UDP_PORT_VXLAN) ||
8957             (!attr->group && !attr->transfer && !priv->sh->tunnel_header_0_1) ||
8958             ((attr->group || attr->transfer) && !priv->sh->misc5_cap)) {
8959                 void *misc_m;
8960                 void *misc_v;
8961                 char *vni_m;
8962                 char *vni_v;
8963                 int size;
8964                 int i;
8965                 misc_m = MLX5_ADDR_OF(fte_match_param,
8966                                       matcher, misc_parameters);
8967                 misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
8968                 size = sizeof(vxlan_m->vni);
8969                 vni_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, vxlan_vni);
8970                 vni_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, vxlan_vni);
8971                 memcpy(vni_m, vxlan_m->vni, size);
8972                 for (i = 0; i < size; ++i)
8973                         vni_v[i] = vni_m[i] & vxlan_v->vni[i];
8974                 return;
8975         }
8976         misc5_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters_5);
8977         misc5_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_5);
8978         tunnel_header_v = (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc5,
8979                                                    misc5_v,
8980                                                    tunnel_header_1);
8981         tunnel_header_m = (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc5,
8982                                                    misc5_m,
8983                                                    tunnel_header_1);
8984         *tunnel_header_v = (vxlan_v->vni[0] & vxlan_m->vni[0]) |
8985                            (vxlan_v->vni[1] & vxlan_m->vni[1]) << 8 |
8986                            (vxlan_v->vni[2] & vxlan_m->vni[2]) << 16;
8987         if (*tunnel_header_v)
8988                 *tunnel_header_m = vxlan_m->vni[0] |
8989                         vxlan_m->vni[1] << 8 |
8990                         vxlan_m->vni[2] << 16;
8991         else
8992                 *tunnel_header_m = 0x0;
8993         *tunnel_header_v |= (vxlan_v->rsvd1 & vxlan_m->rsvd1) << 24;
8994         if (vxlan_v->rsvd1 & vxlan_m->rsvd1)
8995                 *tunnel_header_m |= vxlan_m->rsvd1 << 24;
8996 }
8997
8998 /**
8999  * Add VXLAN-GPE item to matcher and to the value.
9000  *
9001  * @param[in, out] matcher
9002  *   Flow matcher.
9003  * @param[in, out] key
9004  *   Flow matcher value.
9005  * @param[in] item
9006  *   Flow pattern to translate.
9007  * @param[in] inner
9008  *   Item is inner pattern.
9009  */
9010
9011 static void
9012 flow_dv_translate_item_vxlan_gpe(void *matcher, void *key,
9013                                  const struct rte_flow_item *item,
9014                                  const uint64_t pattern_flags)
9015 {
9016         static const struct rte_flow_item_vxlan_gpe dummy_vxlan_gpe_hdr = {0, };
9017         const struct rte_flow_item_vxlan_gpe *vxlan_m = item->mask;
9018         const struct rte_flow_item_vxlan_gpe *vxlan_v = item->spec;
9019         /* The item was validated to be on the outer side */
9020         void *headers_m = MLX5_ADDR_OF(fte_match_param, matcher, outer_headers);
9021         void *headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
9022         void *misc_m =
9023                 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters_3);
9024         void *misc_v =
9025                 MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
9026         char *vni_m =
9027                 MLX5_ADDR_OF(fte_match_set_misc3, misc_m, outer_vxlan_gpe_vni);
9028         char *vni_v =
9029                 MLX5_ADDR_OF(fte_match_set_misc3, misc_v, outer_vxlan_gpe_vni);
9030         int i, size = sizeof(vxlan_m->vni);
9031         uint8_t flags_m = 0xff;
9032         uint8_t flags_v = 0xc;
9033         uint8_t m_protocol, v_protocol;
9034
9035         if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
9036                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
9037                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
9038                          MLX5_UDP_PORT_VXLAN_GPE);
9039         }
9040         if (!vxlan_v) {
9041                 vxlan_v = &dummy_vxlan_gpe_hdr;
9042                 vxlan_m = &dummy_vxlan_gpe_hdr;
9043         } else {
9044                 if (!vxlan_m)
9045                         vxlan_m = &rte_flow_item_vxlan_gpe_mask;
9046         }
9047         memcpy(vni_m, vxlan_m->vni, size);
9048         for (i = 0; i < size; ++i)
9049                 vni_v[i] = vni_m[i] & vxlan_v->vni[i];
9050         if (vxlan_m->flags) {
9051                 flags_m = vxlan_m->flags;
9052                 flags_v = vxlan_v->flags;
9053         }
9054         MLX5_SET(fte_match_set_misc3, misc_m, outer_vxlan_gpe_flags, flags_m);
9055         MLX5_SET(fte_match_set_misc3, misc_v, outer_vxlan_gpe_flags, flags_v);
9056         m_protocol = vxlan_m->protocol;
9057         v_protocol = vxlan_v->protocol;
9058         if (!m_protocol) {
9059                 m_protocol = 0xff;
9060                 /* Force next protocol to ensure next headers parsing. */
9061                 if (pattern_flags & MLX5_FLOW_LAYER_INNER_L2)
9062                         v_protocol = RTE_VXLAN_GPE_TYPE_ETH;
9063                 else if (pattern_flags & MLX5_FLOW_LAYER_INNER_L3_IPV4)
9064                         v_protocol = RTE_VXLAN_GPE_TYPE_IPV4;
9065                 else if (pattern_flags & MLX5_FLOW_LAYER_INNER_L3_IPV6)
9066                         v_protocol = RTE_VXLAN_GPE_TYPE_IPV6;
9067         }
9068         MLX5_SET(fte_match_set_misc3, misc_m,
9069                  outer_vxlan_gpe_next_protocol, m_protocol);
9070         MLX5_SET(fte_match_set_misc3, misc_v,
9071                  outer_vxlan_gpe_next_protocol, m_protocol & v_protocol);
9072 }
9073
9074 /**
9075  * Add Geneve item to matcher and to the value.
9076  *
9077  * @param[in, out] matcher
9078  *   Flow matcher.
9079  * @param[in, out] key
9080  *   Flow matcher value.
9081  * @param[in] item
9082  *   Flow pattern to translate.
9083  * @param[in] inner
9084  *   Item is inner pattern.
9085  */
9086
9087 static void
9088 flow_dv_translate_item_geneve(void *matcher, void *key,
9089                               const struct rte_flow_item *item,
9090                               uint64_t pattern_flags)
9091 {
9092         static const struct rte_flow_item_geneve empty_geneve = {0,};
9093         const struct rte_flow_item_geneve *geneve_m = item->mask;
9094         const struct rte_flow_item_geneve *geneve_v = item->spec;
9095         /* GENEVE flow item validation allows single tunnel item */
9096         void *headers_m = MLX5_ADDR_OF(fte_match_param, matcher, outer_headers);
9097         void *headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
9098         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
9099         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
9100         uint16_t gbhdr_m;
9101         uint16_t gbhdr_v;
9102         char *vni_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, geneve_vni);
9103         char *vni_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, geneve_vni);
9104         size_t size = sizeof(geneve_m->vni), i;
9105         uint16_t protocol_m, protocol_v;
9106
9107         if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
9108                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
9109                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
9110                          MLX5_UDP_PORT_GENEVE);
9111         }
9112         if (!geneve_v) {
9113                 geneve_v = &empty_geneve;
9114                 geneve_m = &empty_geneve;
9115         } else {
9116                 if (!geneve_m)
9117                         geneve_m = &rte_flow_item_geneve_mask;
9118         }
9119         memcpy(vni_m, geneve_m->vni, size);
9120         for (i = 0; i < size; ++i)
9121                 vni_v[i] = vni_m[i] & geneve_v->vni[i];
9122         gbhdr_m = rte_be_to_cpu_16(geneve_m->ver_opt_len_o_c_rsvd0);
9123         gbhdr_v = rte_be_to_cpu_16(geneve_v->ver_opt_len_o_c_rsvd0);
9124         MLX5_SET(fte_match_set_misc, misc_m, geneve_oam,
9125                  MLX5_GENEVE_OAMF_VAL(gbhdr_m));
9126         MLX5_SET(fte_match_set_misc, misc_v, geneve_oam,
9127                  MLX5_GENEVE_OAMF_VAL(gbhdr_v) & MLX5_GENEVE_OAMF_VAL(gbhdr_m));
9128         MLX5_SET(fte_match_set_misc, misc_m, geneve_opt_len,
9129                  MLX5_GENEVE_OPTLEN_VAL(gbhdr_m));
9130         MLX5_SET(fte_match_set_misc, misc_v, geneve_opt_len,
9131                  MLX5_GENEVE_OPTLEN_VAL(gbhdr_v) &
9132                  MLX5_GENEVE_OPTLEN_VAL(gbhdr_m));
9133         protocol_m = rte_be_to_cpu_16(geneve_m->protocol);
9134         protocol_v = rte_be_to_cpu_16(geneve_v->protocol);
9135         if (!protocol_m) {
9136                 /* Force next protocol to prevent matchers duplication */
9137                 protocol_m = 0xFFFF;
9138                 protocol_v = mlx5_translate_tunnel_etypes(pattern_flags);
9139         }
9140         MLX5_SET(fte_match_set_misc, misc_m, geneve_protocol_type, protocol_m);
9141         MLX5_SET(fte_match_set_misc, misc_v, geneve_protocol_type,
9142                  protocol_m & protocol_v);
9143 }
9144
9145 /**
9146  * Create Geneve TLV option resource.
9147  *
9148  * @param dev[in, out]
9149  *   Pointer to rte_eth_dev structure.
9150  * @param[in, out] tag_be24
9151  *   Tag value in big endian then R-shift 8.
9152  * @parm[in, out] dev_flow
9153  *   Pointer to the dev_flow.
9154  * @param[out] error
9155  *   pointer to error structure.
9156  *
9157  * @return
9158  *   0 on success otherwise -errno and errno is set.
9159  */
9160
9161 int
9162 flow_dev_geneve_tlv_option_resource_register(struct rte_eth_dev *dev,
9163                                              const struct rte_flow_item *item,
9164                                              struct rte_flow_error *error)
9165 {
9166         struct mlx5_priv *priv = dev->data->dev_private;
9167         struct mlx5_dev_ctx_shared *sh = priv->sh;
9168         struct mlx5_geneve_tlv_option_resource *geneve_opt_resource =
9169                         sh->geneve_tlv_option_resource;
9170         struct mlx5_devx_obj *obj;
9171         const struct rte_flow_item_geneve_opt *geneve_opt_v = item->spec;
9172         int ret = 0;
9173
9174         if (!geneve_opt_v)
9175                 return -1;
9176         rte_spinlock_lock(&sh->geneve_tlv_opt_sl);
9177         if (geneve_opt_resource != NULL) {
9178                 if (geneve_opt_resource->option_class ==
9179                         geneve_opt_v->option_class &&
9180                         geneve_opt_resource->option_type ==
9181                         geneve_opt_v->option_type &&
9182                         geneve_opt_resource->length ==
9183                         geneve_opt_v->option_len) {
9184                         /* We already have GENVE TLV option obj allocated. */
9185                         __atomic_fetch_add(&geneve_opt_resource->refcnt, 1,
9186                                            __ATOMIC_RELAXED);
9187                 } else {
9188                         ret = rte_flow_error_set(error, ENOMEM,
9189                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
9190                                 "Only one GENEVE TLV option supported");
9191                         goto exit;
9192                 }
9193         } else {
9194                 /* Create a GENEVE TLV object and resource. */
9195                 obj = mlx5_devx_cmd_create_geneve_tlv_option(sh->cdev->ctx,
9196                                 geneve_opt_v->option_class,
9197                                 geneve_opt_v->option_type,
9198                                 geneve_opt_v->option_len);
9199                 if (!obj) {
9200                         ret = rte_flow_error_set(error, ENODATA,
9201                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
9202                                 "Failed to create GENEVE TLV Devx object");
9203                         goto exit;
9204                 }
9205                 sh->geneve_tlv_option_resource =
9206                                 mlx5_malloc(MLX5_MEM_ZERO,
9207                                                 sizeof(*geneve_opt_resource),
9208                                                 0, SOCKET_ID_ANY);
9209                 if (!sh->geneve_tlv_option_resource) {
9210                         claim_zero(mlx5_devx_cmd_destroy(obj));
9211                         ret = rte_flow_error_set(error, ENOMEM,
9212                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
9213                                 "GENEVE TLV object memory allocation failed");
9214                         goto exit;
9215                 }
9216                 geneve_opt_resource = sh->geneve_tlv_option_resource;
9217                 geneve_opt_resource->obj = obj;
9218                 geneve_opt_resource->option_class = geneve_opt_v->option_class;
9219                 geneve_opt_resource->option_type = geneve_opt_v->option_type;
9220                 geneve_opt_resource->length = geneve_opt_v->option_len;
9221                 __atomic_store_n(&geneve_opt_resource->refcnt, 1,
9222                                 __ATOMIC_RELAXED);
9223         }
9224 exit:
9225         rte_spinlock_unlock(&sh->geneve_tlv_opt_sl);
9226         return ret;
9227 }
9228
9229 /**
9230  * Add Geneve TLV option item to matcher.
9231  *
9232  * @param[in, out] dev
9233  *   Pointer to rte_eth_dev structure.
9234  * @param[in, out] matcher
9235  *   Flow matcher.
9236  * @param[in, out] key
9237  *   Flow matcher value.
9238  * @param[in] item
9239  *   Flow pattern to translate.
9240  * @param[out] error
9241  *   Pointer to error structure.
9242  */
9243 static int
9244 flow_dv_translate_item_geneve_opt(struct rte_eth_dev *dev, void *matcher,
9245                                   void *key, const struct rte_flow_item *item,
9246                                   struct rte_flow_error *error)
9247 {
9248         const struct rte_flow_item_geneve_opt *geneve_opt_m = item->mask;
9249         const struct rte_flow_item_geneve_opt *geneve_opt_v = item->spec;
9250         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
9251         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
9252         void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
9253                         misc_parameters_3);
9254         void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
9255         rte_be32_t opt_data_key = 0, opt_data_mask = 0;
9256         int ret = 0;
9257
9258         if (!geneve_opt_v)
9259                 return -1;
9260         if (!geneve_opt_m)
9261                 geneve_opt_m = &rte_flow_item_geneve_opt_mask;
9262         ret = flow_dev_geneve_tlv_option_resource_register(dev, item,
9263                                                            error);
9264         if (ret) {
9265                 DRV_LOG(ERR, "Failed to create geneve_tlv_obj");
9266                 return ret;
9267         }
9268         /*
9269          * Set the option length in GENEVE header if not requested.
9270          * The GENEVE TLV option length is expressed by the option length field
9271          * in the GENEVE header.
9272          * If the option length was not requested but the GENEVE TLV option item
9273          * is present we set the option length field implicitly.
9274          */
9275         if (!MLX5_GET16(fte_match_set_misc, misc_m, geneve_opt_len)) {
9276                 MLX5_SET(fte_match_set_misc, misc_m, geneve_opt_len,
9277                          MLX5_GENEVE_OPTLEN_MASK);
9278                 MLX5_SET(fte_match_set_misc, misc_v, geneve_opt_len,
9279                          geneve_opt_v->option_len + 1);
9280         }
9281         MLX5_SET(fte_match_set_misc, misc_m, geneve_tlv_option_0_exist, 1);
9282         MLX5_SET(fte_match_set_misc, misc_v, geneve_tlv_option_0_exist, 1);
9283         /* Set the data. */
9284         if (geneve_opt_v->data) {
9285                 memcpy(&opt_data_key, geneve_opt_v->data,
9286                         RTE_MIN((uint32_t)(geneve_opt_v->option_len * 4),
9287                                 sizeof(opt_data_key)));
9288                 MLX5_ASSERT((uint32_t)(geneve_opt_v->option_len * 4) <=
9289                                 sizeof(opt_data_key));
9290                 memcpy(&opt_data_mask, geneve_opt_m->data,
9291                         RTE_MIN((uint32_t)(geneve_opt_v->option_len * 4),
9292                                 sizeof(opt_data_mask)));
9293                 MLX5_ASSERT((uint32_t)(geneve_opt_v->option_len * 4) <=
9294                                 sizeof(opt_data_mask));
9295                 MLX5_SET(fte_match_set_misc3, misc3_m,
9296                                 geneve_tlv_option_0_data,
9297                                 rte_be_to_cpu_32(opt_data_mask));
9298                 MLX5_SET(fte_match_set_misc3, misc3_v,
9299                                 geneve_tlv_option_0_data,
9300                         rte_be_to_cpu_32(opt_data_key & opt_data_mask));
9301         }
9302         return ret;
9303 }
9304
9305 /**
9306  * Add MPLS item to matcher and to the value.
9307  *
9308  * @param[in, out] matcher
9309  *   Flow matcher.
9310  * @param[in, out] key
9311  *   Flow matcher value.
9312  * @param[in] item
9313  *   Flow pattern to translate.
9314  * @param[in] prev_layer
9315  *   The protocol layer indicated in previous item.
9316  * @param[in] inner
9317  *   Item is inner pattern.
9318  */
9319 static void
9320 flow_dv_translate_item_mpls(void *matcher, void *key,
9321                             const struct rte_flow_item *item,
9322                             uint64_t prev_layer,
9323                             int inner)
9324 {
9325         const uint32_t *in_mpls_m = item->mask;
9326         const uint32_t *in_mpls_v = item->spec;
9327         uint32_t *out_mpls_m = 0;
9328         uint32_t *out_mpls_v = 0;
9329         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
9330         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
9331         void *misc2_m = MLX5_ADDR_OF(fte_match_param, matcher,
9332                                      misc_parameters_2);
9333         void *misc2_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_2);
9334         void *headers_m = MLX5_ADDR_OF(fte_match_param, matcher, outer_headers);
9335         void *headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
9336
9337         switch (prev_layer) {
9338         case MLX5_FLOW_LAYER_OUTER_L4_UDP:
9339                 if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
9340                         MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport,
9341                                  0xffff);
9342                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
9343                                  MLX5_UDP_PORT_MPLS);
9344                 }
9345                 break;
9346         case MLX5_FLOW_LAYER_GRE:
9347                 /* Fall-through. */
9348         case MLX5_FLOW_LAYER_GRE_KEY:
9349                 if (!MLX5_GET16(fte_match_set_misc, misc_v, gre_protocol)) {
9350                         MLX5_SET(fte_match_set_misc, misc_m, gre_protocol,
9351                                  0xffff);
9352                         MLX5_SET(fte_match_set_misc, misc_v, gre_protocol,
9353                                  RTE_ETHER_TYPE_MPLS);
9354                 }
9355                 break;
9356         default:
9357                 break;
9358         }
9359         if (!in_mpls_v)
9360                 return;
9361         if (!in_mpls_m)
9362                 in_mpls_m = (const uint32_t *)&rte_flow_item_mpls_mask;
9363         switch (prev_layer) {
9364         case MLX5_FLOW_LAYER_OUTER_L4_UDP:
9365                 out_mpls_m =
9366                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_m,
9367                                                  outer_first_mpls_over_udp);
9368                 out_mpls_v =
9369                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_v,
9370                                                  outer_first_mpls_over_udp);
9371                 break;
9372         case MLX5_FLOW_LAYER_GRE:
9373                 out_mpls_m =
9374                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_m,
9375                                                  outer_first_mpls_over_gre);
9376                 out_mpls_v =
9377                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_v,
9378                                                  outer_first_mpls_over_gre);
9379                 break;
9380         default:
9381                 /* Inner MPLS not over GRE is not supported. */
9382                 if (!inner) {
9383                         out_mpls_m =
9384                                 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2,
9385                                                          misc2_m,
9386                                                          outer_first_mpls);
9387                         out_mpls_v =
9388                                 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2,
9389                                                          misc2_v,
9390                                                          outer_first_mpls);
9391                 }
9392                 break;
9393         }
9394         if (out_mpls_m && out_mpls_v) {
9395                 *out_mpls_m = *in_mpls_m;
9396                 *out_mpls_v = *in_mpls_v & *in_mpls_m;
9397         }
9398 }
9399
9400 /**
9401  * Add metadata register item to matcher
9402  *
9403  * @param[in, out] matcher
9404  *   Flow matcher.
9405  * @param[in, out] key
9406  *   Flow matcher value.
9407  * @param[in] reg_type
9408  *   Type of device metadata register
9409  * @param[in] value
9410  *   Register value
9411  * @param[in] mask
9412  *   Register mask
9413  */
9414 static void
9415 flow_dv_match_meta_reg(void *matcher, void *key,
9416                        enum modify_reg reg_type,
9417                        uint32_t data, uint32_t mask)
9418 {
9419         void *misc2_m =
9420                 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters_2);
9421         void *misc2_v =
9422                 MLX5_ADDR_OF(fte_match_param, key, misc_parameters_2);
9423         uint32_t temp;
9424
9425         data &= mask;
9426         switch (reg_type) {
9427         case REG_A:
9428                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_a, mask);
9429                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_a, data);
9430                 break;
9431         case REG_B:
9432                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_b, mask);
9433                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_b, data);
9434                 break;
9435         case REG_C_0:
9436                 /*
9437                  * The metadata register C0 field might be divided into
9438                  * source vport index and META item value, we should set
9439                  * this field according to specified mask, not as whole one.
9440                  */
9441                 temp = MLX5_GET(fte_match_set_misc2, misc2_m, metadata_reg_c_0);
9442                 temp |= mask;
9443                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_0, temp);
9444                 temp = MLX5_GET(fte_match_set_misc2, misc2_v, metadata_reg_c_0);
9445                 temp &= ~mask;
9446                 temp |= data;
9447                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_0, temp);
9448                 break;
9449         case REG_C_1:
9450                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_1, mask);
9451                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_1, data);
9452                 break;
9453         case REG_C_2:
9454                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_2, mask);
9455                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_2, data);
9456                 break;
9457         case REG_C_3:
9458                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_3, mask);
9459                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_3, data);
9460                 break;
9461         case REG_C_4:
9462                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_4, mask);
9463                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_4, data);
9464                 break;
9465         case REG_C_5:
9466                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_5, mask);
9467                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_5, data);
9468                 break;
9469         case REG_C_6:
9470                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_6, mask);
9471                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_6, data);
9472                 break;
9473         case REG_C_7:
9474                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_7, mask);
9475                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_7, data);
9476                 break;
9477         default:
9478                 MLX5_ASSERT(false);
9479                 break;
9480         }
9481 }
9482
9483 /**
9484  * Add MARK item to matcher
9485  *
9486  * @param[in] dev
9487  *   The device to configure through.
9488  * @param[in, out] matcher
9489  *   Flow matcher.
9490  * @param[in, out] key
9491  *   Flow matcher value.
9492  * @param[in] item
9493  *   Flow pattern to translate.
9494  */
9495 static void
9496 flow_dv_translate_item_mark(struct rte_eth_dev *dev,
9497                             void *matcher, void *key,
9498                             const struct rte_flow_item *item)
9499 {
9500         struct mlx5_priv *priv = dev->data->dev_private;
9501         const struct rte_flow_item_mark *mark;
9502         uint32_t value;
9503         uint32_t mask;
9504
9505         mark = item->mask ? (const void *)item->mask :
9506                             &rte_flow_item_mark_mask;
9507         mask = mark->id & priv->sh->dv_mark_mask;
9508         mark = (const void *)item->spec;
9509         MLX5_ASSERT(mark);
9510         value = mark->id & priv->sh->dv_mark_mask & mask;
9511         if (mask) {
9512                 enum modify_reg reg;
9513
9514                 /* Get the metadata register index for the mark. */
9515                 reg = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, NULL);
9516                 MLX5_ASSERT(reg > 0);
9517                 if (reg == REG_C_0) {
9518                         struct mlx5_priv *priv = dev->data->dev_private;
9519                         uint32_t msk_c0 = priv->sh->dv_regc0_mask;
9520                         uint32_t shl_c0 = rte_bsf32(msk_c0);
9521
9522                         mask &= msk_c0;
9523                         mask <<= shl_c0;
9524                         value <<= shl_c0;
9525                 }
9526                 flow_dv_match_meta_reg(matcher, key, reg, value, mask);
9527         }
9528 }
9529
9530 /**
9531  * Add META item to matcher
9532  *
9533  * @param[in] dev
9534  *   The devich to configure through.
9535  * @param[in, out] matcher
9536  *   Flow matcher.
9537  * @param[in, out] key
9538  *   Flow matcher value.
9539  * @param[in] attr
9540  *   Attributes of flow that includes this item.
9541  * @param[in] item
9542  *   Flow pattern to translate.
9543  */
9544 static void
9545 flow_dv_translate_item_meta(struct rte_eth_dev *dev,
9546                             void *matcher, void *key,
9547                             const struct rte_flow_attr *attr,
9548                             const struct rte_flow_item *item)
9549 {
9550         const struct rte_flow_item_meta *meta_m;
9551         const struct rte_flow_item_meta *meta_v;
9552
9553         meta_m = (const void *)item->mask;
9554         if (!meta_m)
9555                 meta_m = &rte_flow_item_meta_mask;
9556         meta_v = (const void *)item->spec;
9557         if (meta_v) {
9558                 int reg;
9559                 uint32_t value = meta_v->data;
9560                 uint32_t mask = meta_m->data;
9561
9562                 reg = flow_dv_get_metadata_reg(dev, attr, NULL);
9563                 if (reg < 0)
9564                         return;
9565                 MLX5_ASSERT(reg != REG_NON);
9566                 if (reg == REG_C_0) {
9567                         struct mlx5_priv *priv = dev->data->dev_private;
9568                         uint32_t msk_c0 = priv->sh->dv_regc0_mask;
9569                         uint32_t shl_c0 = rte_bsf32(msk_c0);
9570
9571                         mask &= msk_c0;
9572                         mask <<= shl_c0;
9573                         value <<= shl_c0;
9574                 }
9575                 flow_dv_match_meta_reg(matcher, key, reg, value, mask);
9576         }
9577 }
9578
9579 /**
9580  * Add vport metadata Reg C0 item to matcher
9581  *
9582  * @param[in, out] matcher
9583  *   Flow matcher.
9584  * @param[in, out] key
9585  *   Flow matcher value.
9586  * @param[in] reg
9587  *   Flow pattern to translate.
9588  */
9589 static void
9590 flow_dv_translate_item_meta_vport(void *matcher, void *key,
9591                                   uint32_t value, uint32_t mask)
9592 {
9593         flow_dv_match_meta_reg(matcher, key, REG_C_0, value, mask);
9594 }
9595
9596 /**
9597  * Add tag item to matcher
9598  *
9599  * @param[in] dev
9600  *   The devich to configure through.
9601  * @param[in, out] matcher
9602  *   Flow matcher.
9603  * @param[in, out] key
9604  *   Flow matcher value.
9605  * @param[in] item
9606  *   Flow pattern to translate.
9607  */
9608 static void
9609 flow_dv_translate_mlx5_item_tag(struct rte_eth_dev *dev,
9610                                 void *matcher, void *key,
9611                                 const struct rte_flow_item *item)
9612 {
9613         const struct mlx5_rte_flow_item_tag *tag_v = item->spec;
9614         const struct mlx5_rte_flow_item_tag *tag_m = item->mask;
9615         uint32_t mask, value;
9616
9617         MLX5_ASSERT(tag_v);
9618         value = tag_v->data;
9619         mask = tag_m ? tag_m->data : UINT32_MAX;
9620         if (tag_v->id == REG_C_0) {
9621                 struct mlx5_priv *priv = dev->data->dev_private;
9622                 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
9623                 uint32_t shl_c0 = rte_bsf32(msk_c0);
9624
9625                 mask &= msk_c0;
9626                 mask <<= shl_c0;
9627                 value <<= shl_c0;
9628         }
9629         flow_dv_match_meta_reg(matcher, key, tag_v->id, value, mask);
9630 }
9631
9632 /**
9633  * Add TAG item to matcher
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  */
9644 static void
9645 flow_dv_translate_item_tag(struct rte_eth_dev *dev,
9646                            void *matcher, void *key,
9647                            const struct rte_flow_item *item)
9648 {
9649         const struct rte_flow_item_tag *tag_v = item->spec;
9650         const struct rte_flow_item_tag *tag_m = item->mask;
9651         enum modify_reg reg;
9652
9653         MLX5_ASSERT(tag_v);
9654         tag_m = tag_m ? tag_m : &rte_flow_item_tag_mask;
9655         /* Get the metadata register index for the tag. */
9656         reg = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, tag_v->index, NULL);
9657         MLX5_ASSERT(reg > 0);
9658         flow_dv_match_meta_reg(matcher, key, reg, tag_v->data, tag_m->data);
9659 }
9660
9661 /**
9662  * Add source vport match to the specified matcher.
9663  *
9664  * @param[in, out] matcher
9665  *   Flow matcher.
9666  * @param[in, out] key
9667  *   Flow matcher value.
9668  * @param[in] port
9669  *   Source vport value to match
9670  * @param[in] mask
9671  *   Mask
9672  */
9673 static void
9674 flow_dv_translate_item_source_vport(void *matcher, void *key,
9675                                     int16_t port, uint16_t mask)
9676 {
9677         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
9678         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
9679
9680         MLX5_SET(fte_match_set_misc, misc_m, source_port, mask);
9681         MLX5_SET(fte_match_set_misc, misc_v, source_port, port);
9682 }
9683
9684 /**
9685  * Translate port-id item to eswitch match on  port-id.
9686  *
9687  * @param[in] dev
9688  *   The devich to configure through.
9689  * @param[in, out] matcher
9690  *   Flow matcher.
9691  * @param[in, out] key
9692  *   Flow matcher value.
9693  * @param[in] item
9694  *   Flow pattern to translate.
9695  * @param[in]
9696  *   Flow attributes.
9697  *
9698  * @return
9699  *   0 on success, a negative errno value otherwise.
9700  */
9701 static int
9702 flow_dv_translate_item_port_id(struct rte_eth_dev *dev, void *matcher,
9703                                void *key, const struct rte_flow_item *item,
9704                                const struct rte_flow_attr *attr)
9705 {
9706         const struct rte_flow_item_port_id *pid_m = item ? item->mask : NULL;
9707         const struct rte_flow_item_port_id *pid_v = item ? item->spec : NULL;
9708         struct mlx5_priv *priv;
9709         uint16_t mask, id;
9710
9711         if (pid_v && pid_v->id == MLX5_PORT_ESW_MGR) {
9712                 flow_dv_translate_item_source_vport(matcher, key,
9713                         flow_dv_get_esw_manager_vport_id(dev), 0xffff);
9714                 return 0;
9715         }
9716         mask = pid_m ? pid_m->id : 0xffff;
9717         id = pid_v ? pid_v->id : dev->data->port_id;
9718         priv = mlx5_port_to_eswitch_info(id, item == NULL);
9719         if (!priv)
9720                 return -rte_errno;
9721         /*
9722          * Translate to vport field or to metadata, depending on mode.
9723          * Kernel can use either misc.source_port or half of C0 metadata
9724          * register.
9725          */
9726         if (priv->vport_meta_mask) {
9727                 /*
9728                  * Provide the hint for SW steering library
9729                  * to insert the flow into ingress domain and
9730                  * save the extra vport match.
9731                  */
9732                 if (mask == 0xffff && priv->vport_id == 0xffff &&
9733                     priv->pf_bond < 0 && attr->transfer)
9734                         flow_dv_translate_item_source_vport
9735                                 (matcher, key, priv->vport_id, mask);
9736                 /*
9737                  * We should always set the vport metadata register,
9738                  * otherwise the SW steering library can drop
9739                  * the rule if wire vport metadata value is not zero,
9740                  * it depends on kernel configuration.
9741                  */
9742                 flow_dv_translate_item_meta_vport(matcher, key,
9743                                                   priv->vport_meta_tag,
9744                                                   priv->vport_meta_mask);
9745         } else {
9746                 flow_dv_translate_item_source_vport(matcher, key,
9747                                                     priv->vport_id, mask);
9748         }
9749         return 0;
9750 }
9751
9752 /**
9753  * Add ICMP6 item to matcher and to the value.
9754  *
9755  * @param[in, out] matcher
9756  *   Flow matcher.
9757  * @param[in, out] key
9758  *   Flow matcher value.
9759  * @param[in] item
9760  *   Flow pattern to translate.
9761  * @param[in] inner
9762  *   Item is inner pattern.
9763  */
9764 static void
9765 flow_dv_translate_item_icmp6(void *matcher, void *key,
9766                               const struct rte_flow_item *item,
9767                               int inner)
9768 {
9769         const struct rte_flow_item_icmp6 *icmp6_m = item->mask;
9770         const struct rte_flow_item_icmp6 *icmp6_v = item->spec;
9771         void *headers_m;
9772         void *headers_v;
9773         void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
9774                                      misc_parameters_3);
9775         void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
9776         if (inner) {
9777                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
9778                                          inner_headers);
9779                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
9780         } else {
9781                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
9782                                          outer_headers);
9783                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
9784         }
9785         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xFF);
9786         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_ICMPV6);
9787         if (!icmp6_v)
9788                 return;
9789         if (!icmp6_m)
9790                 icmp6_m = &rte_flow_item_icmp6_mask;
9791         MLX5_SET(fte_match_set_misc3, misc3_m, icmpv6_type, icmp6_m->type);
9792         MLX5_SET(fte_match_set_misc3, misc3_v, icmpv6_type,
9793                  icmp6_v->type & icmp6_m->type);
9794         MLX5_SET(fte_match_set_misc3, misc3_m, icmpv6_code, icmp6_m->code);
9795         MLX5_SET(fte_match_set_misc3, misc3_v, icmpv6_code,
9796                  icmp6_v->code & icmp6_m->code);
9797 }
9798
9799 /**
9800  * Add ICMP item to matcher and to the value.
9801  *
9802  * @param[in, out] matcher
9803  *   Flow matcher.
9804  * @param[in, out] key
9805  *   Flow matcher value.
9806  * @param[in] item
9807  *   Flow pattern to translate.
9808  * @param[in] inner
9809  *   Item is inner pattern.
9810  */
9811 static void
9812 flow_dv_translate_item_icmp(void *matcher, void *key,
9813                             const struct rte_flow_item *item,
9814                             int inner)
9815 {
9816         const struct rte_flow_item_icmp *icmp_m = item->mask;
9817         const struct rte_flow_item_icmp *icmp_v = item->spec;
9818         uint32_t icmp_header_data_m = 0;
9819         uint32_t icmp_header_data_v = 0;
9820         void *headers_m;
9821         void *headers_v;
9822         void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
9823                                      misc_parameters_3);
9824         void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
9825         if (inner) {
9826                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
9827                                          inner_headers);
9828                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
9829         } else {
9830                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
9831                                          outer_headers);
9832                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
9833         }
9834         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xFF);
9835         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_ICMP);
9836         if (!icmp_v)
9837                 return;
9838         if (!icmp_m)
9839                 icmp_m = &rte_flow_item_icmp_mask;
9840         MLX5_SET(fte_match_set_misc3, misc3_m, icmp_type,
9841                  icmp_m->hdr.icmp_type);
9842         MLX5_SET(fte_match_set_misc3, misc3_v, icmp_type,
9843                  icmp_v->hdr.icmp_type & icmp_m->hdr.icmp_type);
9844         MLX5_SET(fte_match_set_misc3, misc3_m, icmp_code,
9845                  icmp_m->hdr.icmp_code);
9846         MLX5_SET(fte_match_set_misc3, misc3_v, icmp_code,
9847                  icmp_v->hdr.icmp_code & icmp_m->hdr.icmp_code);
9848         icmp_header_data_m = rte_be_to_cpu_16(icmp_m->hdr.icmp_seq_nb);
9849         icmp_header_data_m |= rte_be_to_cpu_16(icmp_m->hdr.icmp_ident) << 16;
9850         if (icmp_header_data_m) {
9851                 icmp_header_data_v = rte_be_to_cpu_16(icmp_v->hdr.icmp_seq_nb);
9852                 icmp_header_data_v |=
9853                          rte_be_to_cpu_16(icmp_v->hdr.icmp_ident) << 16;
9854                 MLX5_SET(fte_match_set_misc3, misc3_m, icmp_header_data,
9855                          icmp_header_data_m);
9856                 MLX5_SET(fte_match_set_misc3, misc3_v, icmp_header_data,
9857                          icmp_header_data_v & icmp_header_data_m);
9858         }
9859 }
9860
9861 /**
9862  * Add GTP item to matcher and to the value.
9863  *
9864  * @param[in, out] matcher
9865  *   Flow matcher.
9866  * @param[in, out] key
9867  *   Flow matcher value.
9868  * @param[in] item
9869  *   Flow pattern to translate.
9870  * @param[in] inner
9871  *   Item is inner pattern.
9872  */
9873 static void
9874 flow_dv_translate_item_gtp(void *matcher, void *key,
9875                            const struct rte_flow_item *item, int inner)
9876 {
9877         const struct rte_flow_item_gtp *gtp_m = item->mask;
9878         const struct rte_flow_item_gtp *gtp_v = item->spec;
9879         void *headers_m;
9880         void *headers_v;
9881         void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
9882                                      misc_parameters_3);
9883         void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
9884         uint16_t dport = RTE_GTPU_UDP_PORT;
9885
9886         if (inner) {
9887                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
9888                                          inner_headers);
9889                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
9890         } else {
9891                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
9892                                          outer_headers);
9893                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
9894         }
9895         if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
9896                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
9897                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
9898         }
9899         if (!gtp_v)
9900                 return;
9901         if (!gtp_m)
9902                 gtp_m = &rte_flow_item_gtp_mask;
9903         MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_msg_flags,
9904                  gtp_m->v_pt_rsv_flags);
9905         MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_msg_flags,
9906                  gtp_v->v_pt_rsv_flags & gtp_m->v_pt_rsv_flags);
9907         MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_msg_type, gtp_m->msg_type);
9908         MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_msg_type,
9909                  gtp_v->msg_type & gtp_m->msg_type);
9910         MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_teid,
9911                  rte_be_to_cpu_32(gtp_m->teid));
9912         MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_teid,
9913                  rte_be_to_cpu_32(gtp_v->teid & gtp_m->teid));
9914 }
9915
9916 /**
9917  * Add GTP PSC item to matcher.
9918  *
9919  * @param[in, out] matcher
9920  *   Flow matcher.
9921  * @param[in, out] key
9922  *   Flow matcher value.
9923  * @param[in] item
9924  *   Flow pattern to translate.
9925  */
9926 static int
9927 flow_dv_translate_item_gtp_psc(void *matcher, void *key,
9928                                const struct rte_flow_item *item)
9929 {
9930         const struct rte_flow_item_gtp_psc *gtp_psc_m = item->mask;
9931         const struct rte_flow_item_gtp_psc *gtp_psc_v = item->spec;
9932         void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
9933                         misc_parameters_3);
9934         void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
9935         union {
9936                 uint32_t w32;
9937                 struct {
9938                         uint16_t seq_num;
9939                         uint8_t npdu_num;
9940                         uint8_t next_ext_header_type;
9941                 };
9942         } dw_2;
9943         uint8_t gtp_flags;
9944
9945         /* Always set E-flag match on one, regardless of GTP item settings. */
9946         gtp_flags = MLX5_GET(fte_match_set_misc3, misc3_m, gtpu_msg_flags);
9947         gtp_flags |= MLX5_GTP_EXT_HEADER_FLAG;
9948         MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_msg_flags, gtp_flags);
9949         gtp_flags = MLX5_GET(fte_match_set_misc3, misc3_v, gtpu_msg_flags);
9950         gtp_flags |= MLX5_GTP_EXT_HEADER_FLAG;
9951         MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_msg_flags, gtp_flags);
9952         /*Set next extension header type. */
9953         dw_2.seq_num = 0;
9954         dw_2.npdu_num = 0;
9955         dw_2.next_ext_header_type = 0xff;
9956         MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_dw_2,
9957                  rte_cpu_to_be_32(dw_2.w32));
9958         dw_2.seq_num = 0;
9959         dw_2.npdu_num = 0;
9960         dw_2.next_ext_header_type = 0x85;
9961         MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_dw_2,
9962                  rte_cpu_to_be_32(dw_2.w32));
9963         if (gtp_psc_v) {
9964                 union {
9965                         uint32_t w32;
9966                         struct {
9967                                 uint8_t len;
9968                                 uint8_t type_flags;
9969                                 uint8_t qfi;
9970                                 uint8_t reserved;
9971                         };
9972                 } dw_0;
9973
9974                 /*Set extension header PDU type and Qos. */
9975                 if (!gtp_psc_m)
9976                         gtp_psc_m = &rte_flow_item_gtp_psc_mask;
9977                 dw_0.w32 = 0;
9978                 dw_0.type_flags = MLX5_GTP_PDU_TYPE_SHIFT(gtp_psc_m->hdr.type);
9979                 dw_0.qfi = gtp_psc_m->hdr.qfi;
9980                 MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_first_ext_dw_0,
9981                          rte_cpu_to_be_32(dw_0.w32));
9982                 dw_0.w32 = 0;
9983                 dw_0.type_flags = MLX5_GTP_PDU_TYPE_SHIFT(gtp_psc_v->hdr.type &
9984                                                         gtp_psc_m->hdr.type);
9985                 dw_0.qfi = gtp_psc_v->hdr.qfi & gtp_psc_m->hdr.qfi;
9986                 MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_first_ext_dw_0,
9987                          rte_cpu_to_be_32(dw_0.w32));
9988         }
9989         return 0;
9990 }
9991
9992 /**
9993  * Add eCPRI item to matcher and to the value.
9994  *
9995  * @param[in] dev
9996  *   The devich to configure through.
9997  * @param[in, out] matcher
9998  *   Flow matcher.
9999  * @param[in, out] key
10000  *   Flow matcher value.
10001  * @param[in] item
10002  *   Flow pattern to translate.
10003  * @param[in] last_item
10004  *   Last item flags.
10005  */
10006 static void
10007 flow_dv_translate_item_ecpri(struct rte_eth_dev *dev, void *matcher,
10008                              void *key, const struct rte_flow_item *item,
10009                              uint64_t last_item)
10010 {
10011         struct mlx5_priv *priv = dev->data->dev_private;
10012         const struct rte_flow_item_ecpri *ecpri_m = item->mask;
10013         const struct rte_flow_item_ecpri *ecpri_v = item->spec;
10014         struct rte_ecpri_common_hdr common;
10015         void *misc4_m = MLX5_ADDR_OF(fte_match_param, matcher,
10016                                      misc_parameters_4);
10017         void *misc4_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_4);
10018         uint32_t *samples;
10019         void *dw_m;
10020         void *dw_v;
10021
10022         /*
10023          * In case of eCPRI over Ethernet, if EtherType is not specified,
10024          * match on eCPRI EtherType implicitly.
10025          */
10026         if (last_item & MLX5_FLOW_LAYER_OUTER_L2) {
10027                 void *hdrs_m, *hdrs_v, *l2m, *l2v;
10028
10029                 hdrs_m = MLX5_ADDR_OF(fte_match_param, matcher, outer_headers);
10030                 hdrs_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
10031                 l2m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, hdrs_m, ethertype);
10032                 l2v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, hdrs_v, ethertype);
10033                 if (*(uint16_t *)l2m == 0 && *(uint16_t *)l2v == 0) {
10034                         *(uint16_t *)l2m = UINT16_MAX;
10035                         *(uint16_t *)l2v = RTE_BE16(RTE_ETHER_TYPE_ECPRI);
10036                 }
10037         }
10038         if (!ecpri_v)
10039                 return;
10040         if (!ecpri_m)
10041                 ecpri_m = &rte_flow_item_ecpri_mask;
10042         /*
10043          * Maximal four DW samples are supported in a single matching now.
10044          * Two are used now for a eCPRI matching:
10045          * 1. Type: one byte, mask should be 0x00ff0000 in network order
10046          * 2. ID of a message: one or two bytes, mask 0xffff0000 or 0xff000000
10047          *    if any.
10048          */
10049         if (!ecpri_m->hdr.common.u32)
10050                 return;
10051         samples = priv->sh->ecpri_parser.ids;
10052         /* Need to take the whole DW as the mask to fill the entry. */
10053         dw_m = MLX5_ADDR_OF(fte_match_set_misc4, misc4_m,
10054                             prog_sample_field_value_0);
10055         dw_v = MLX5_ADDR_OF(fte_match_set_misc4, misc4_v,
10056                             prog_sample_field_value_0);
10057         /* Already big endian (network order) in the header. */
10058         *(uint32_t *)dw_m = ecpri_m->hdr.common.u32;
10059         *(uint32_t *)dw_v = ecpri_v->hdr.common.u32 & ecpri_m->hdr.common.u32;
10060         /* Sample#0, used for matching type, offset 0. */
10061         MLX5_SET(fte_match_set_misc4, misc4_m,
10062                  prog_sample_field_id_0, samples[0]);
10063         /* It makes no sense to set the sample ID in the mask field. */
10064         MLX5_SET(fte_match_set_misc4, misc4_v,
10065                  prog_sample_field_id_0, samples[0]);
10066         /*
10067          * Checking if message body part needs to be matched.
10068          * Some wildcard rules only matching type field should be supported.
10069          */
10070         if (ecpri_m->hdr.dummy[0]) {
10071                 common.u32 = rte_be_to_cpu_32(ecpri_v->hdr.common.u32);
10072                 switch (common.type) {
10073                 case RTE_ECPRI_MSG_TYPE_IQ_DATA:
10074                 case RTE_ECPRI_MSG_TYPE_RTC_CTRL:
10075                 case RTE_ECPRI_MSG_TYPE_DLY_MSR:
10076                         dw_m = MLX5_ADDR_OF(fte_match_set_misc4, misc4_m,
10077                                             prog_sample_field_value_1);
10078                         dw_v = MLX5_ADDR_OF(fte_match_set_misc4, misc4_v,
10079                                             prog_sample_field_value_1);
10080                         *(uint32_t *)dw_m = ecpri_m->hdr.dummy[0];
10081                         *(uint32_t *)dw_v = ecpri_v->hdr.dummy[0] &
10082                                             ecpri_m->hdr.dummy[0];
10083                         /* Sample#1, to match message body, offset 4. */
10084                         MLX5_SET(fte_match_set_misc4, misc4_m,
10085                                  prog_sample_field_id_1, samples[1]);
10086                         MLX5_SET(fte_match_set_misc4, misc4_v,
10087                                  prog_sample_field_id_1, samples[1]);
10088                         break;
10089                 default:
10090                         /* Others, do not match any sample ID. */
10091                         break;
10092                 }
10093         }
10094 }
10095
10096 /*
10097  * Add connection tracking status item to matcher
10098  *
10099  * @param[in] dev
10100  *   The devich to configure through.
10101  * @param[in, out] matcher
10102  *   Flow matcher.
10103  * @param[in, out] key
10104  *   Flow matcher value.
10105  * @param[in] item
10106  *   Flow pattern to translate.
10107  */
10108 static void
10109 flow_dv_translate_item_aso_ct(struct rte_eth_dev *dev,
10110                               void *matcher, void *key,
10111                               const struct rte_flow_item *item)
10112 {
10113         uint32_t reg_value = 0;
10114         int reg_id;
10115         /* 8LSB 0b 11/0000/11, middle 4 bits are reserved. */
10116         uint32_t reg_mask = 0;
10117         const struct rte_flow_item_conntrack *spec = item->spec;
10118         const struct rte_flow_item_conntrack *mask = item->mask;
10119         uint32_t flags;
10120         struct rte_flow_error error;
10121
10122         if (!mask)
10123                 mask = &rte_flow_item_conntrack_mask;
10124         if (!spec || !mask->flags)
10125                 return;
10126         flags = spec->flags & mask->flags;
10127         /* The conflict should be checked in the validation. */
10128         if (flags & RTE_FLOW_CONNTRACK_PKT_STATE_VALID)
10129                 reg_value |= MLX5_CT_SYNDROME_VALID;
10130         if (flags & RTE_FLOW_CONNTRACK_PKT_STATE_CHANGED)
10131                 reg_value |= MLX5_CT_SYNDROME_STATE_CHANGE;
10132         if (flags & RTE_FLOW_CONNTRACK_PKT_STATE_INVALID)
10133                 reg_value |= MLX5_CT_SYNDROME_INVALID;
10134         if (flags & RTE_FLOW_CONNTRACK_PKT_STATE_DISABLED)
10135                 reg_value |= MLX5_CT_SYNDROME_TRAP;
10136         if (flags & RTE_FLOW_CONNTRACK_PKT_STATE_BAD)
10137                 reg_value |= MLX5_CT_SYNDROME_BAD_PACKET;
10138         if (mask->flags & (RTE_FLOW_CONNTRACK_PKT_STATE_VALID |
10139                            RTE_FLOW_CONNTRACK_PKT_STATE_INVALID |
10140                            RTE_FLOW_CONNTRACK_PKT_STATE_DISABLED))
10141                 reg_mask |= 0xc0;
10142         if (mask->flags & RTE_FLOW_CONNTRACK_PKT_STATE_CHANGED)
10143                 reg_mask |= MLX5_CT_SYNDROME_STATE_CHANGE;
10144         if (mask->flags & RTE_FLOW_CONNTRACK_PKT_STATE_BAD)
10145                 reg_mask |= MLX5_CT_SYNDROME_BAD_PACKET;
10146         /* The REG_C_x value could be saved during startup. */
10147         reg_id = mlx5_flow_get_reg_id(dev, MLX5_ASO_CONNTRACK, 0, &error);
10148         if (reg_id == REG_NON)
10149                 return;
10150         flow_dv_match_meta_reg(matcher, key, (enum modify_reg)reg_id,
10151                                reg_value, reg_mask);
10152 }
10153
10154 static void
10155 flow_dv_translate_item_flex(struct rte_eth_dev *dev, void *matcher, void *key,
10156                             const struct rte_flow_item *item,
10157                             struct mlx5_flow *dev_flow, bool is_inner)
10158 {
10159         const struct rte_flow_item_flex *spec =
10160                 (const struct rte_flow_item_flex *)item->spec;
10161         int index = mlx5_flex_acquire_index(dev, spec->handle, false);
10162
10163         MLX5_ASSERT(index >= 0 && index <= (int)(sizeof(uint32_t) * CHAR_BIT));
10164         if (index < 0)
10165                 return;
10166         if (!(dev_flow->handle->flex_item & RTE_BIT32(index))) {
10167                 /* Don't count both inner and outer flex items in one rule. */
10168                 if (mlx5_flex_acquire_index(dev, spec->handle, true) != index)
10169                         MLX5_ASSERT(false);
10170                 dev_flow->handle->flex_item |= RTE_BIT32(index);
10171         }
10172         mlx5_flex_flow_translate_item(dev, matcher, key, item, is_inner);
10173 }
10174
10175 static uint32_t matcher_zero[MLX5_ST_SZ_DW(fte_match_param)] = { 0 };
10176
10177 #define HEADER_IS_ZERO(match_criteria, headers)                              \
10178         !(memcmp(MLX5_ADDR_OF(fte_match_param, match_criteria, headers),     \
10179                  matcher_zero, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
10180
10181 /**
10182  * Calculate flow matcher enable bitmap.
10183  *
10184  * @param match_criteria
10185  *   Pointer to flow matcher criteria.
10186  *
10187  * @return
10188  *   Bitmap of enabled fields.
10189  */
10190 static uint8_t
10191 flow_dv_matcher_enable(uint32_t *match_criteria)
10192 {
10193         uint8_t match_criteria_enable;
10194
10195         match_criteria_enable =
10196                 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
10197                 MLX5_MATCH_CRITERIA_ENABLE_OUTER_BIT;
10198         match_criteria_enable |=
10199                 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
10200                 MLX5_MATCH_CRITERIA_ENABLE_MISC_BIT;
10201         match_criteria_enable |=
10202                 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
10203                 MLX5_MATCH_CRITERIA_ENABLE_INNER_BIT;
10204         match_criteria_enable |=
10205                 (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
10206                 MLX5_MATCH_CRITERIA_ENABLE_MISC2_BIT;
10207         match_criteria_enable |=
10208                 (!HEADER_IS_ZERO(match_criteria, misc_parameters_3)) <<
10209                 MLX5_MATCH_CRITERIA_ENABLE_MISC3_BIT;
10210         match_criteria_enable |=
10211                 (!HEADER_IS_ZERO(match_criteria, misc_parameters_4)) <<
10212                 MLX5_MATCH_CRITERIA_ENABLE_MISC4_BIT;
10213         match_criteria_enable |=
10214                 (!HEADER_IS_ZERO(match_criteria, misc_parameters_5)) <<
10215                 MLX5_MATCH_CRITERIA_ENABLE_MISC5_BIT;
10216         return match_criteria_enable;
10217 }
10218
10219 static void
10220 __flow_dv_adjust_buf_size(size_t *size, uint8_t match_criteria)
10221 {
10222         /*
10223          * Check flow matching criteria first, subtract misc5/4 length if flow
10224          * doesn't own misc5/4 parameters. In some old rdma-core releases,
10225          * misc5/4 are not supported, and matcher creation failure is expected
10226          * w/o subtration. If misc5 is provided, misc4 must be counted in since
10227          * misc5 is right after misc4.
10228          */
10229         if (!(match_criteria & (1 << MLX5_MATCH_CRITERIA_ENABLE_MISC5_BIT))) {
10230                 *size = MLX5_ST_SZ_BYTES(fte_match_param) -
10231                         MLX5_ST_SZ_BYTES(fte_match_set_misc5);
10232                 if (!(match_criteria & (1 <<
10233                         MLX5_MATCH_CRITERIA_ENABLE_MISC4_BIT))) {
10234                         *size -= MLX5_ST_SZ_BYTES(fte_match_set_misc4);
10235                 }
10236         }
10237 }
10238
10239 static struct mlx5_list_entry *
10240 flow_dv_matcher_clone_cb(void *tool_ctx __rte_unused,
10241                          struct mlx5_list_entry *entry, void *cb_ctx)
10242 {
10243         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
10244         struct mlx5_flow_dv_matcher *ref = ctx->data;
10245         struct mlx5_flow_tbl_data_entry *tbl = container_of(ref->tbl,
10246                                                             typeof(*tbl), tbl);
10247         struct mlx5_flow_dv_matcher *resource = mlx5_malloc(MLX5_MEM_ANY,
10248                                                             sizeof(*resource),
10249                                                             0, SOCKET_ID_ANY);
10250
10251         if (!resource) {
10252                 rte_flow_error_set(ctx->error, ENOMEM,
10253                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
10254                                    "cannot create matcher");
10255                 return NULL;
10256         }
10257         memcpy(resource, entry, sizeof(*resource));
10258         resource->tbl = &tbl->tbl;
10259         return &resource->entry;
10260 }
10261
10262 static void
10263 flow_dv_matcher_clone_free_cb(void *tool_ctx __rte_unused,
10264                              struct mlx5_list_entry *entry)
10265 {
10266         mlx5_free(entry);
10267 }
10268
10269 struct mlx5_list_entry *
10270 flow_dv_tbl_create_cb(void *tool_ctx, void *cb_ctx)
10271 {
10272         struct mlx5_dev_ctx_shared *sh = tool_ctx;
10273         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
10274         struct rte_eth_dev *dev = ctx->dev;
10275         struct mlx5_flow_tbl_data_entry *tbl_data;
10276         struct mlx5_flow_tbl_tunnel_prm *tt_prm = ctx->data2;
10277         struct rte_flow_error *error = ctx->error;
10278         union mlx5_flow_tbl_key key = { .v64 = *(uint64_t *)(ctx->data) };
10279         struct mlx5_flow_tbl_resource *tbl;
10280         void *domain;
10281         uint32_t idx = 0;
10282         int ret;
10283
10284         tbl_data = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_JUMP], &idx);
10285         if (!tbl_data) {
10286                 rte_flow_error_set(error, ENOMEM,
10287                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
10288                                    NULL,
10289                                    "cannot allocate flow table data entry");
10290                 return NULL;
10291         }
10292         tbl_data->idx = idx;
10293         tbl_data->tunnel = tt_prm->tunnel;
10294         tbl_data->group_id = tt_prm->group_id;
10295         tbl_data->external = !!tt_prm->external;
10296         tbl_data->tunnel_offload = is_tunnel_offload_active(dev);
10297         tbl_data->is_egress = !!key.is_egress;
10298         tbl_data->is_transfer = !!key.is_fdb;
10299         tbl_data->dummy = !!key.dummy;
10300         tbl_data->level = key.level;
10301         tbl_data->id = key.id;
10302         tbl = &tbl_data->tbl;
10303         if (key.dummy)
10304                 return &tbl_data->entry;
10305         if (key.is_fdb)
10306                 domain = sh->fdb_domain;
10307         else if (key.is_egress)
10308                 domain = sh->tx_domain;
10309         else
10310                 domain = sh->rx_domain;
10311         ret = mlx5_flow_os_create_flow_tbl(domain, key.level, &tbl->obj);
10312         if (ret) {
10313                 rte_flow_error_set(error, ENOMEM,
10314                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
10315                                    NULL, "cannot create flow table object");
10316                 mlx5_ipool_free(sh->ipool[MLX5_IPOOL_JUMP], idx);
10317                 return NULL;
10318         }
10319         if (key.level != 0) {
10320                 ret = mlx5_flow_os_create_flow_action_dest_flow_tbl
10321                                         (tbl->obj, &tbl_data->jump.action);
10322                 if (ret) {
10323                         rte_flow_error_set(error, ENOMEM,
10324                                            RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
10325                                            NULL,
10326                                            "cannot create flow jump action");
10327                         mlx5_flow_os_destroy_flow_tbl(tbl->obj);
10328                         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_JUMP], idx);
10329                         return NULL;
10330                 }
10331         }
10332         MKSTR(matcher_name, "%s_%s_%u_%u_matcher_list",
10333               key.is_fdb ? "FDB" : "NIC", key.is_egress ? "egress" : "ingress",
10334               key.level, key.id);
10335         tbl_data->matchers = mlx5_list_create(matcher_name, sh, true,
10336                                               flow_dv_matcher_create_cb,
10337                                               flow_dv_matcher_match_cb,
10338                                               flow_dv_matcher_remove_cb,
10339                                               flow_dv_matcher_clone_cb,
10340                                               flow_dv_matcher_clone_free_cb);
10341         if (!tbl_data->matchers) {
10342                 rte_flow_error_set(error, ENOMEM,
10343                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
10344                                    NULL,
10345                                    "cannot create tbl matcher list");
10346                 mlx5_flow_os_destroy_flow_action(tbl_data->jump.action);
10347                 mlx5_flow_os_destroy_flow_tbl(tbl->obj);
10348                 mlx5_ipool_free(sh->ipool[MLX5_IPOOL_JUMP], idx);
10349                 return NULL;
10350         }
10351         return &tbl_data->entry;
10352 }
10353
10354 int
10355 flow_dv_tbl_match_cb(void *tool_ctx __rte_unused, struct mlx5_list_entry *entry,
10356                      void *cb_ctx)
10357 {
10358         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
10359         struct mlx5_flow_tbl_data_entry *tbl_data =
10360                 container_of(entry, struct mlx5_flow_tbl_data_entry, entry);
10361         union mlx5_flow_tbl_key key = { .v64 =  *(uint64_t *)(ctx->data) };
10362
10363         return tbl_data->level != key.level ||
10364                tbl_data->id != key.id ||
10365                tbl_data->dummy != key.dummy ||
10366                tbl_data->is_transfer != !!key.is_fdb ||
10367                tbl_data->is_egress != !!key.is_egress;
10368 }
10369
10370 struct mlx5_list_entry *
10371 flow_dv_tbl_clone_cb(void *tool_ctx, struct mlx5_list_entry *oentry,
10372                       void *cb_ctx)
10373 {
10374         struct mlx5_dev_ctx_shared *sh = tool_ctx;
10375         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
10376         struct mlx5_flow_tbl_data_entry *tbl_data;
10377         struct rte_flow_error *error = ctx->error;
10378         uint32_t idx = 0;
10379
10380         tbl_data = mlx5_ipool_malloc(sh->ipool[MLX5_IPOOL_JUMP], &idx);
10381         if (!tbl_data) {
10382                 rte_flow_error_set(error, ENOMEM,
10383                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
10384                                    NULL,
10385                                    "cannot allocate flow table data entry");
10386                 return NULL;
10387         }
10388         memcpy(tbl_data, oentry, sizeof(*tbl_data));
10389         tbl_data->idx = idx;
10390         return &tbl_data->entry;
10391 }
10392
10393 void
10394 flow_dv_tbl_clone_free_cb(void *tool_ctx, struct mlx5_list_entry *entry)
10395 {
10396         struct mlx5_dev_ctx_shared *sh = tool_ctx;
10397         struct mlx5_flow_tbl_data_entry *tbl_data =
10398                     container_of(entry, struct mlx5_flow_tbl_data_entry, entry);
10399
10400         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_JUMP], tbl_data->idx);
10401 }
10402
10403 /**
10404  * Get a flow table.
10405  *
10406  * @param[in, out] dev
10407  *   Pointer to rte_eth_dev structure.
10408  * @param[in] table_level
10409  *   Table level to use.
10410  * @param[in] egress
10411  *   Direction of the table.
10412  * @param[in] transfer
10413  *   E-Switch or NIC flow.
10414  * @param[in] dummy
10415  *   Dummy entry for dv API.
10416  * @param[in] table_id
10417  *   Table id to use.
10418  * @param[out] error
10419  *   pointer to error structure.
10420  *
10421  * @return
10422  *   Returns tables resource based on the index, NULL in case of failed.
10423  */
10424 struct mlx5_flow_tbl_resource *
10425 flow_dv_tbl_resource_get(struct rte_eth_dev *dev,
10426                          uint32_t table_level, uint8_t egress,
10427                          uint8_t transfer,
10428                          bool external,
10429                          const struct mlx5_flow_tunnel *tunnel,
10430                          uint32_t group_id, uint8_t dummy,
10431                          uint32_t table_id,
10432                          struct rte_flow_error *error)
10433 {
10434         struct mlx5_priv *priv = dev->data->dev_private;
10435         union mlx5_flow_tbl_key table_key = {
10436                 {
10437                         .level = table_level,
10438                         .id = table_id,
10439                         .reserved = 0,
10440                         .dummy = !!dummy,
10441                         .is_fdb = !!transfer,
10442                         .is_egress = !!egress,
10443                 }
10444         };
10445         struct mlx5_flow_tbl_tunnel_prm tt_prm = {
10446                 .tunnel = tunnel,
10447                 .group_id = group_id,
10448                 .external = external,
10449         };
10450         struct mlx5_flow_cb_ctx ctx = {
10451                 .dev = dev,
10452                 .error = error,
10453                 .data = &table_key.v64,
10454                 .data2 = &tt_prm,
10455         };
10456         struct mlx5_list_entry *entry;
10457         struct mlx5_flow_tbl_data_entry *tbl_data;
10458
10459         entry = mlx5_hlist_register(priv->sh->flow_tbls, table_key.v64, &ctx);
10460         if (!entry) {
10461                 rte_flow_error_set(error, ENOMEM,
10462                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
10463                                    "cannot get table");
10464                 return NULL;
10465         }
10466         DRV_LOG(DEBUG, "table_level %u table_id %u "
10467                 "tunnel %u group %u registered.",
10468                 table_level, table_id,
10469                 tunnel ? tunnel->tunnel_id : 0, group_id);
10470         tbl_data = container_of(entry, struct mlx5_flow_tbl_data_entry, entry);
10471         return &tbl_data->tbl;
10472 }
10473
10474 void
10475 flow_dv_tbl_remove_cb(void *tool_ctx, struct mlx5_list_entry *entry)
10476 {
10477         struct mlx5_dev_ctx_shared *sh = tool_ctx;
10478         struct mlx5_flow_tbl_data_entry *tbl_data =
10479                     container_of(entry, struct mlx5_flow_tbl_data_entry, entry);
10480
10481         MLX5_ASSERT(entry && sh);
10482         if (tbl_data->jump.action)
10483                 mlx5_flow_os_destroy_flow_action(tbl_data->jump.action);
10484         if (tbl_data->tbl.obj)
10485                 mlx5_flow_os_destroy_flow_tbl(tbl_data->tbl.obj);
10486         if (tbl_data->tunnel_offload && tbl_data->external) {
10487                 struct mlx5_list_entry *he;
10488                 struct mlx5_hlist *tunnel_grp_hash;
10489                 struct mlx5_flow_tunnel_hub *thub = sh->tunnel_hub;
10490                 union tunnel_tbl_key tunnel_key = {
10491                         .tunnel_id = tbl_data->tunnel ?
10492                                         tbl_data->tunnel->tunnel_id : 0,
10493                         .group = tbl_data->group_id
10494                 };
10495                 uint32_t table_level = tbl_data->level;
10496                 struct mlx5_flow_cb_ctx ctx = {
10497                         .data = (void *)&tunnel_key.val,
10498                 };
10499
10500                 tunnel_grp_hash = tbl_data->tunnel ?
10501                                         tbl_data->tunnel->groups :
10502                                         thub->groups;
10503                 he = mlx5_hlist_lookup(tunnel_grp_hash, tunnel_key.val, &ctx);
10504                 if (he)
10505                         mlx5_hlist_unregister(tunnel_grp_hash, he);
10506                 DRV_LOG(DEBUG,
10507                         "table_level %u id %u tunnel %u group %u released.",
10508                         table_level,
10509                         tbl_data->id,
10510                         tbl_data->tunnel ?
10511                         tbl_data->tunnel->tunnel_id : 0,
10512                         tbl_data->group_id);
10513         }
10514         mlx5_list_destroy(tbl_data->matchers);
10515         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_JUMP], tbl_data->idx);
10516 }
10517
10518 /**
10519  * Release a flow table.
10520  *
10521  * @param[in] sh
10522  *   Pointer to device shared structure.
10523  * @param[in] tbl
10524  *   Table resource to be released.
10525  *
10526  * @return
10527  *   Returns 0 if table was released, else return 1;
10528  */
10529 static int
10530 flow_dv_tbl_resource_release(struct mlx5_dev_ctx_shared *sh,
10531                              struct mlx5_flow_tbl_resource *tbl)
10532 {
10533         struct mlx5_flow_tbl_data_entry *tbl_data =
10534                 container_of(tbl, struct mlx5_flow_tbl_data_entry, tbl);
10535
10536         if (!tbl)
10537                 return 0;
10538         return mlx5_hlist_unregister(sh->flow_tbls, &tbl_data->entry);
10539 }
10540
10541 int
10542 flow_dv_matcher_match_cb(void *tool_ctx __rte_unused,
10543                          struct mlx5_list_entry *entry, void *cb_ctx)
10544 {
10545         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
10546         struct mlx5_flow_dv_matcher *ref = ctx->data;
10547         struct mlx5_flow_dv_matcher *cur = container_of(entry, typeof(*cur),
10548                                                         entry);
10549
10550         return cur->crc != ref->crc ||
10551                cur->priority != ref->priority ||
10552                memcmp((const void *)cur->mask.buf,
10553                       (const void *)ref->mask.buf, ref->mask.size);
10554 }
10555
10556 struct mlx5_list_entry *
10557 flow_dv_matcher_create_cb(void *tool_ctx, void *cb_ctx)
10558 {
10559         struct mlx5_dev_ctx_shared *sh = tool_ctx;
10560         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
10561         struct mlx5_flow_dv_matcher *ref = ctx->data;
10562         struct mlx5_flow_dv_matcher *resource;
10563         struct mlx5dv_flow_matcher_attr dv_attr = {
10564                 .type = IBV_FLOW_ATTR_NORMAL,
10565                 .match_mask = (void *)&ref->mask,
10566         };
10567         struct mlx5_flow_tbl_data_entry *tbl = container_of(ref->tbl,
10568                                                             typeof(*tbl), tbl);
10569         int ret;
10570
10571         resource = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*resource), 0,
10572                                SOCKET_ID_ANY);
10573         if (!resource) {
10574                 rte_flow_error_set(ctx->error, ENOMEM,
10575                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
10576                                    "cannot create matcher");
10577                 return NULL;
10578         }
10579         *resource = *ref;
10580         dv_attr.match_criteria_enable =
10581                 flow_dv_matcher_enable(resource->mask.buf);
10582         __flow_dv_adjust_buf_size(&ref->mask.size,
10583                                   dv_attr.match_criteria_enable);
10584         dv_attr.priority = ref->priority;
10585         if (tbl->is_egress)
10586                 dv_attr.flags |= IBV_FLOW_ATTR_FLAGS_EGRESS;
10587         ret = mlx5_flow_os_create_flow_matcher(sh->cdev->ctx, &dv_attr,
10588                                                tbl->tbl.obj,
10589                                                &resource->matcher_object);
10590         if (ret) {
10591                 mlx5_free(resource);
10592                 rte_flow_error_set(ctx->error, ENOMEM,
10593                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
10594                                    "cannot create matcher");
10595                 return NULL;
10596         }
10597         return &resource->entry;
10598 }
10599
10600 /**
10601  * Register the flow matcher.
10602  *
10603  * @param[in, out] dev
10604  *   Pointer to rte_eth_dev structure.
10605  * @param[in, out] matcher
10606  *   Pointer to flow matcher.
10607  * @param[in, out] key
10608  *   Pointer to flow table key.
10609  * @parm[in, out] dev_flow
10610  *   Pointer to the dev_flow.
10611  * @param[out] error
10612  *   pointer to error structure.
10613  *
10614  * @return
10615  *   0 on success otherwise -errno and errno is set.
10616  */
10617 static int
10618 flow_dv_matcher_register(struct rte_eth_dev *dev,
10619                          struct mlx5_flow_dv_matcher *ref,
10620                          union mlx5_flow_tbl_key *key,
10621                          struct mlx5_flow *dev_flow,
10622                          const struct mlx5_flow_tunnel *tunnel,
10623                          uint32_t group_id,
10624                          struct rte_flow_error *error)
10625 {
10626         struct mlx5_list_entry *entry;
10627         struct mlx5_flow_dv_matcher *resource;
10628         struct mlx5_flow_tbl_resource *tbl;
10629         struct mlx5_flow_tbl_data_entry *tbl_data;
10630         struct mlx5_flow_cb_ctx ctx = {
10631                 .error = error,
10632                 .data = ref,
10633         };
10634         /**
10635          * tunnel offload API requires this registration for cases when
10636          * tunnel match rule was inserted before tunnel set rule.
10637          */
10638         tbl = flow_dv_tbl_resource_get(dev, key->level,
10639                                        key->is_egress, key->is_fdb,
10640                                        dev_flow->external, tunnel,
10641                                        group_id, 0, key->id, error);
10642         if (!tbl)
10643                 return -rte_errno;      /* No need to refill the error info */
10644         tbl_data = container_of(tbl, struct mlx5_flow_tbl_data_entry, tbl);
10645         ref->tbl = tbl;
10646         entry = mlx5_list_register(tbl_data->matchers, &ctx);
10647         if (!entry) {
10648                 flow_dv_tbl_resource_release(MLX5_SH(dev), tbl);
10649                 return rte_flow_error_set(error, ENOMEM,
10650                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
10651                                           "cannot allocate ref memory");
10652         }
10653         resource = container_of(entry, typeof(*resource), entry);
10654         dev_flow->handle->dvh.matcher = resource;
10655         return 0;
10656 }
10657
10658 struct mlx5_list_entry *
10659 flow_dv_tag_create_cb(void *tool_ctx, void *cb_ctx)
10660 {
10661         struct mlx5_dev_ctx_shared *sh = tool_ctx;
10662         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
10663         struct mlx5_flow_dv_tag_resource *entry;
10664         uint32_t idx = 0;
10665         int ret;
10666
10667         entry = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_TAG], &idx);
10668         if (!entry) {
10669                 rte_flow_error_set(ctx->error, ENOMEM,
10670                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
10671                                    "cannot allocate resource memory");
10672                 return NULL;
10673         }
10674         entry->idx = idx;
10675         entry->tag_id = *(uint32_t *)(ctx->data);
10676         ret = mlx5_flow_os_create_flow_action_tag(entry->tag_id,
10677                                                   &entry->action);
10678         if (ret) {
10679                 mlx5_ipool_free(sh->ipool[MLX5_IPOOL_TAG], idx);
10680                 rte_flow_error_set(ctx->error, ENOMEM,
10681                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
10682                                    NULL, "cannot create action");
10683                 return NULL;
10684         }
10685         return &entry->entry;
10686 }
10687
10688 int
10689 flow_dv_tag_match_cb(void *tool_ctx __rte_unused, struct mlx5_list_entry *entry,
10690                      void *cb_ctx)
10691 {
10692         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
10693         struct mlx5_flow_dv_tag_resource *tag =
10694                    container_of(entry, struct mlx5_flow_dv_tag_resource, entry);
10695
10696         return *(uint32_t *)(ctx->data) != tag->tag_id;
10697 }
10698
10699 struct mlx5_list_entry *
10700 flow_dv_tag_clone_cb(void *tool_ctx, struct mlx5_list_entry *oentry,
10701                      void *cb_ctx)
10702 {
10703         struct mlx5_dev_ctx_shared *sh = tool_ctx;
10704         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
10705         struct mlx5_flow_dv_tag_resource *entry;
10706         uint32_t idx = 0;
10707
10708         entry = mlx5_ipool_malloc(sh->ipool[MLX5_IPOOL_TAG], &idx);
10709         if (!entry) {
10710                 rte_flow_error_set(ctx->error, ENOMEM,
10711                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
10712                                    "cannot allocate tag resource memory");
10713                 return NULL;
10714         }
10715         memcpy(entry, oentry, sizeof(*entry));
10716         entry->idx = idx;
10717         return &entry->entry;
10718 }
10719
10720 void
10721 flow_dv_tag_clone_free_cb(void *tool_ctx, struct mlx5_list_entry *entry)
10722 {
10723         struct mlx5_dev_ctx_shared *sh = tool_ctx;
10724         struct mlx5_flow_dv_tag_resource *tag =
10725                    container_of(entry, struct mlx5_flow_dv_tag_resource, entry);
10726
10727         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_TAG], tag->idx);
10728 }
10729
10730 /**
10731  * Find existing tag resource or create and register a new one.
10732  *
10733  * @param dev[in, out]
10734  *   Pointer to rte_eth_dev structure.
10735  * @param[in, out] tag_be24
10736  *   Tag value in big endian then R-shift 8.
10737  * @parm[in, out] dev_flow
10738  *   Pointer to the dev_flow.
10739  * @param[out] error
10740  *   pointer to error structure.
10741  *
10742  * @return
10743  *   0 on success otherwise -errno and errno is set.
10744  */
10745 static int
10746 flow_dv_tag_resource_register
10747                         (struct rte_eth_dev *dev,
10748                          uint32_t tag_be24,
10749                          struct mlx5_flow *dev_flow,
10750                          struct rte_flow_error *error)
10751 {
10752         struct mlx5_priv *priv = dev->data->dev_private;
10753         struct mlx5_flow_dv_tag_resource *resource;
10754         struct mlx5_list_entry *entry;
10755         struct mlx5_flow_cb_ctx ctx = {
10756                                         .error = error,
10757                                         .data = &tag_be24,
10758                                         };
10759         struct mlx5_hlist *tag_table;
10760
10761         tag_table = flow_dv_hlist_prepare(priv->sh, &priv->sh->tag_table,
10762                                       "tags",
10763                                       MLX5_TAGS_HLIST_ARRAY_SIZE,
10764                                       false, false, priv->sh,
10765                                       flow_dv_tag_create_cb,
10766                                       flow_dv_tag_match_cb,
10767                                       flow_dv_tag_remove_cb,
10768                                       flow_dv_tag_clone_cb,
10769                                       flow_dv_tag_clone_free_cb);
10770         if (unlikely(!tag_table))
10771                 return -rte_errno;
10772         entry = mlx5_hlist_register(tag_table, tag_be24, &ctx);
10773         if (entry) {
10774                 resource = container_of(entry, struct mlx5_flow_dv_tag_resource,
10775                                         entry);
10776                 dev_flow->handle->dvh.rix_tag = resource->idx;
10777                 dev_flow->dv.tag_resource = resource;
10778                 return 0;
10779         }
10780         return -rte_errno;
10781 }
10782
10783 void
10784 flow_dv_tag_remove_cb(void *tool_ctx, struct mlx5_list_entry *entry)
10785 {
10786         struct mlx5_dev_ctx_shared *sh = tool_ctx;
10787         struct mlx5_flow_dv_tag_resource *tag =
10788                    container_of(entry, struct mlx5_flow_dv_tag_resource, entry);
10789
10790         MLX5_ASSERT(tag && sh && tag->action);
10791         claim_zero(mlx5_flow_os_destroy_flow_action(tag->action));
10792         DRV_LOG(DEBUG, "Tag %p: removed.", (void *)tag);
10793         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_TAG], tag->idx);
10794 }
10795
10796 /**
10797  * Release the tag.
10798  *
10799  * @param dev
10800  *   Pointer to Ethernet device.
10801  * @param tag_idx
10802  *   Tag index.
10803  *
10804  * @return
10805  *   1 while a reference on it exists, 0 when freed.
10806  */
10807 static int
10808 flow_dv_tag_release(struct rte_eth_dev *dev,
10809                     uint32_t tag_idx)
10810 {
10811         struct mlx5_priv *priv = dev->data->dev_private;
10812         struct mlx5_flow_dv_tag_resource *tag;
10813
10814         tag = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_TAG], tag_idx);
10815         if (!tag)
10816                 return 0;
10817         DRV_LOG(DEBUG, "port %u tag %p: refcnt %d--",
10818                 dev->data->port_id, (void *)tag, tag->entry.ref_cnt);
10819         return mlx5_hlist_unregister(priv->sh->tag_table, &tag->entry);
10820 }
10821
10822 /**
10823  * Translate action PORT_ID / REPRESENTED_PORT to vport.
10824  *
10825  * @param[in] dev
10826  *   Pointer to rte_eth_dev structure.
10827  * @param[in] action
10828  *   Pointer to action PORT_ID / REPRESENTED_PORT.
10829  * @param[out] dst_port_id
10830  *   The target port ID.
10831  * @param[out] error
10832  *   Pointer to the error structure.
10833  *
10834  * @return
10835  *   0 on success, a negative errno value otherwise and rte_errno is set.
10836  */
10837 static int
10838 flow_dv_translate_action_port_id(struct rte_eth_dev *dev,
10839                                  const struct rte_flow_action *action,
10840                                  uint32_t *dst_port_id,
10841                                  struct rte_flow_error *error)
10842 {
10843         uint32_t port;
10844         struct mlx5_priv *priv;
10845
10846         switch (action->type) {
10847         case RTE_FLOW_ACTION_TYPE_PORT_ID: {
10848                 const struct rte_flow_action_port_id *conf;
10849
10850                 conf = (const struct rte_flow_action_port_id *)action->conf;
10851                 port = conf->original ? dev->data->port_id : conf->id;
10852                 break;
10853         }
10854         case RTE_FLOW_ACTION_TYPE_REPRESENTED_PORT: {
10855                 const struct rte_flow_action_ethdev *ethdev;
10856
10857                 ethdev = (const struct rte_flow_action_ethdev *)action->conf;
10858                 port = ethdev->port_id;
10859                 break;
10860         }
10861         default:
10862                 MLX5_ASSERT(false);
10863                 return rte_flow_error_set(error, EINVAL,
10864                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
10865                                           "unknown E-Switch action");
10866         }
10867
10868         priv = mlx5_port_to_eswitch_info(port, false);
10869         if (!priv)
10870                 return rte_flow_error_set(error, -rte_errno,
10871                                           RTE_FLOW_ERROR_TYPE_ACTION,
10872                                           NULL,
10873                                           "No eswitch info was found for port");
10874 #ifdef HAVE_MLX5DV_DR_CREATE_DEST_IB_PORT
10875         /*
10876          * This parameter is transferred to
10877          * mlx5dv_dr_action_create_dest_ib_port().
10878          */
10879         *dst_port_id = priv->dev_port;
10880 #else
10881         /*
10882          * Legacy mode, no LAG configurations is supported.
10883          * This parameter is transferred to
10884          * mlx5dv_dr_action_create_dest_vport().
10885          */
10886         *dst_port_id = priv->vport_id;
10887 #endif
10888         return 0;
10889 }
10890
10891 /**
10892  * Create a counter with aging configuration.
10893  *
10894  * @param[in] dev
10895  *   Pointer to rte_eth_dev structure.
10896  * @param[in] dev_flow
10897  *   Pointer to the mlx5_flow.
10898  * @param[out] count
10899  *   Pointer to the counter action configuration.
10900  * @param[in] age
10901  *   Pointer to the aging action configuration.
10902  *
10903  * @return
10904  *   Index to flow counter on success, 0 otherwise.
10905  */
10906 static uint32_t
10907 flow_dv_translate_create_counter(struct rte_eth_dev *dev,
10908                                 struct mlx5_flow *dev_flow,
10909                                 const struct rte_flow_action_count *count
10910                                         __rte_unused,
10911                                 const struct rte_flow_action_age *age)
10912 {
10913         uint32_t counter;
10914         struct mlx5_age_param *age_param;
10915
10916         counter = flow_dv_counter_alloc(dev, !!age);
10917         if (!counter || age == NULL)
10918                 return counter;
10919         age_param = flow_dv_counter_idx_get_age(dev, counter);
10920         age_param->context = age->context ? age->context :
10921                 (void *)(uintptr_t)(dev_flow->flow_idx);
10922         age_param->timeout = age->timeout;
10923         age_param->port_id = dev->data->port_id;
10924         __atomic_store_n(&age_param->sec_since_last_hit, 0, __ATOMIC_RELAXED);
10925         __atomic_store_n(&age_param->state, AGE_CANDIDATE, __ATOMIC_RELAXED);
10926         return counter;
10927 }
10928
10929 /**
10930  * Add Tx queue matcher
10931  *
10932  * @param[in] dev
10933  *   Pointer to the dev struct.
10934  * @param[in, out] matcher
10935  *   Flow matcher.
10936  * @param[in, out] key
10937  *   Flow matcher value.
10938  * @param[in] item
10939  *   Flow pattern to translate.
10940  * @param[in] inner
10941  *   Item is inner pattern.
10942  */
10943 static void
10944 flow_dv_translate_item_tx_queue(struct rte_eth_dev *dev,
10945                                 void *matcher, void *key,
10946                                 const struct rte_flow_item *item)
10947 {
10948         const struct mlx5_rte_flow_item_tx_queue *queue_m;
10949         const struct mlx5_rte_flow_item_tx_queue *queue_v;
10950         void *misc_m =
10951                 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
10952         void *misc_v =
10953                 MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
10954         struct mlx5_txq_ctrl *txq;
10955         uint32_t queue, mask;
10956
10957         queue_m = (const void *)item->mask;
10958         queue_v = (const void *)item->spec;
10959         if (!queue_v)
10960                 return;
10961         txq = mlx5_txq_get(dev, queue_v->queue);
10962         if (!txq)
10963                 return;
10964         if (txq->type == MLX5_TXQ_TYPE_HAIRPIN)
10965                 queue = txq->obj->sq->id;
10966         else
10967                 queue = txq->obj->sq_obj.sq->id;
10968         mask = queue_m == NULL ? UINT32_MAX : queue_m->queue;
10969         MLX5_SET(fte_match_set_misc, misc_m, source_sqn, mask);
10970         MLX5_SET(fte_match_set_misc, misc_v, source_sqn, queue & mask);
10971         mlx5_txq_release(dev, queue_v->queue);
10972 }
10973
10974 /**
10975  * Set the hash fields according to the @p flow information.
10976  *
10977  * @param[in] dev_flow
10978  *   Pointer to the mlx5_flow.
10979  * @param[in] rss_desc
10980  *   Pointer to the mlx5_flow_rss_desc.
10981  */
10982 static void
10983 flow_dv_hashfields_set(struct mlx5_flow *dev_flow,
10984                        struct mlx5_flow_rss_desc *rss_desc)
10985 {
10986         uint64_t items = dev_flow->handle->layers;
10987         int rss_inner = 0;
10988         uint64_t rss_types = rte_eth_rss_hf_refine(rss_desc->types);
10989
10990         dev_flow->hash_fields = 0;
10991 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
10992         if (rss_desc->level >= 2)
10993                 rss_inner = 1;
10994 #endif
10995         if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L3_IPV4)) ||
10996             (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L3_IPV4))) {
10997                 if (rss_types & MLX5_IPV4_LAYER_TYPES) {
10998                         if (rss_types & RTE_ETH_RSS_L3_SRC_ONLY)
10999                                 dev_flow->hash_fields |= IBV_RX_HASH_SRC_IPV4;
11000                         else if (rss_types & RTE_ETH_RSS_L3_DST_ONLY)
11001                                 dev_flow->hash_fields |= IBV_RX_HASH_DST_IPV4;
11002                         else
11003                                 dev_flow->hash_fields |= MLX5_IPV4_IBV_RX_HASH;
11004                 }
11005         } else if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L3_IPV6)) ||
11006                    (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L3_IPV6))) {
11007                 if (rss_types & MLX5_IPV6_LAYER_TYPES) {
11008                         if (rss_types & RTE_ETH_RSS_L3_SRC_ONLY)
11009                                 dev_flow->hash_fields |= IBV_RX_HASH_SRC_IPV6;
11010                         else if (rss_types & RTE_ETH_RSS_L3_DST_ONLY)
11011                                 dev_flow->hash_fields |= IBV_RX_HASH_DST_IPV6;
11012                         else
11013                                 dev_flow->hash_fields |= MLX5_IPV6_IBV_RX_HASH;
11014                 }
11015         }
11016         if (dev_flow->hash_fields == 0)
11017                 /*
11018                  * There is no match between the RSS types and the
11019                  * L3 protocol (IPv4/IPv6) defined in the flow rule.
11020                  */
11021                 return;
11022         if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L4_UDP)) ||
11023             (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L4_UDP))) {
11024                 if (rss_types & RTE_ETH_RSS_UDP) {
11025                         if (rss_types & RTE_ETH_RSS_L4_SRC_ONLY)
11026                                 dev_flow->hash_fields |=
11027                                                 IBV_RX_HASH_SRC_PORT_UDP;
11028                         else if (rss_types & RTE_ETH_RSS_L4_DST_ONLY)
11029                                 dev_flow->hash_fields |=
11030                                                 IBV_RX_HASH_DST_PORT_UDP;
11031                         else
11032                                 dev_flow->hash_fields |= MLX5_UDP_IBV_RX_HASH;
11033                 }
11034         } else if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L4_TCP)) ||
11035                    (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L4_TCP))) {
11036                 if (rss_types & RTE_ETH_RSS_TCP) {
11037                         if (rss_types & RTE_ETH_RSS_L4_SRC_ONLY)
11038                                 dev_flow->hash_fields |=
11039                                                 IBV_RX_HASH_SRC_PORT_TCP;
11040                         else if (rss_types & RTE_ETH_RSS_L4_DST_ONLY)
11041                                 dev_flow->hash_fields |=
11042                                                 IBV_RX_HASH_DST_PORT_TCP;
11043                         else
11044                                 dev_flow->hash_fields |= MLX5_TCP_IBV_RX_HASH;
11045                 }
11046         }
11047         if (rss_inner)
11048                 dev_flow->hash_fields |= IBV_RX_HASH_INNER;
11049 }
11050
11051 /**
11052  * Prepare an Rx Hash queue.
11053  *
11054  * @param dev
11055  *   Pointer to Ethernet device.
11056  * @param[in] dev_flow
11057  *   Pointer to the mlx5_flow.
11058  * @param[in] rss_desc
11059  *   Pointer to the mlx5_flow_rss_desc.
11060  * @param[out] hrxq_idx
11061  *   Hash Rx queue index.
11062  *
11063  * @return
11064  *   The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
11065  */
11066 static struct mlx5_hrxq *
11067 flow_dv_hrxq_prepare(struct rte_eth_dev *dev,
11068                      struct mlx5_flow *dev_flow,
11069                      struct mlx5_flow_rss_desc *rss_desc,
11070                      uint32_t *hrxq_idx)
11071 {
11072         struct mlx5_priv *priv = dev->data->dev_private;
11073         struct mlx5_flow_handle *dh = dev_flow->handle;
11074         struct mlx5_hrxq *hrxq;
11075
11076         MLX5_ASSERT(rss_desc->queue_num);
11077         rss_desc->key_len = MLX5_RSS_HASH_KEY_LEN;
11078         rss_desc->hash_fields = dev_flow->hash_fields;
11079         rss_desc->tunnel = !!(dh->layers & MLX5_FLOW_LAYER_TUNNEL);
11080         rss_desc->shared_rss = 0;
11081         if (rss_desc->hash_fields == 0)
11082                 rss_desc->queue_num = 1;
11083         *hrxq_idx = mlx5_hrxq_get(dev, rss_desc);
11084         if (!*hrxq_idx)
11085                 return NULL;
11086         hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ],
11087                               *hrxq_idx);
11088         return hrxq;
11089 }
11090
11091 /**
11092  * Release sample sub action resource.
11093  *
11094  * @param[in, out] dev
11095  *   Pointer to rte_eth_dev structure.
11096  * @param[in] act_res
11097  *   Pointer to sample sub action resource.
11098  */
11099 static void
11100 flow_dv_sample_sub_actions_release(struct rte_eth_dev *dev,
11101                                    struct mlx5_flow_sub_actions_idx *act_res)
11102 {
11103         if (act_res->rix_hrxq) {
11104                 mlx5_hrxq_release(dev, act_res->rix_hrxq);
11105                 act_res->rix_hrxq = 0;
11106         }
11107         if (act_res->rix_encap_decap) {
11108                 flow_dv_encap_decap_resource_release(dev,
11109                                                      act_res->rix_encap_decap);
11110                 act_res->rix_encap_decap = 0;
11111         }
11112         if (act_res->rix_port_id_action) {
11113                 flow_dv_port_id_action_resource_release(dev,
11114                                                 act_res->rix_port_id_action);
11115                 act_res->rix_port_id_action = 0;
11116         }
11117         if (act_res->rix_tag) {
11118                 flow_dv_tag_release(dev, act_res->rix_tag);
11119                 act_res->rix_tag = 0;
11120         }
11121         if (act_res->rix_jump) {
11122                 flow_dv_jump_tbl_resource_release(dev, act_res->rix_jump);
11123                 act_res->rix_jump = 0;
11124         }
11125 }
11126
11127 int
11128 flow_dv_sample_match_cb(void *tool_ctx __rte_unused,
11129                         struct mlx5_list_entry *entry, void *cb_ctx)
11130 {
11131         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
11132         struct rte_eth_dev *dev = ctx->dev;
11133         struct mlx5_flow_dv_sample_resource *ctx_resource = ctx->data;
11134         struct mlx5_flow_dv_sample_resource *resource = container_of(entry,
11135                                                               typeof(*resource),
11136                                                               entry);
11137
11138         if (ctx_resource->ratio == resource->ratio &&
11139             ctx_resource->ft_type == resource->ft_type &&
11140             ctx_resource->ft_id == resource->ft_id &&
11141             ctx_resource->set_action == resource->set_action &&
11142             !memcmp((void *)&ctx_resource->sample_act,
11143                     (void *)&resource->sample_act,
11144                     sizeof(struct mlx5_flow_sub_actions_list))) {
11145                 /*
11146                  * Existing sample action should release the prepared
11147                  * sub-actions reference counter.
11148                  */
11149                 flow_dv_sample_sub_actions_release(dev,
11150                                                    &ctx_resource->sample_idx);
11151                 return 0;
11152         }
11153         return 1;
11154 }
11155
11156 struct mlx5_list_entry *
11157 flow_dv_sample_create_cb(void *tool_ctx __rte_unused, void *cb_ctx)
11158 {
11159         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
11160         struct rte_eth_dev *dev = ctx->dev;
11161         struct mlx5_flow_dv_sample_resource *ctx_resource = ctx->data;
11162         void **sample_dv_actions = ctx_resource->sub_actions;
11163         struct mlx5_flow_dv_sample_resource *resource;
11164         struct mlx5dv_dr_flow_sampler_attr sampler_attr;
11165         struct mlx5_priv *priv = dev->data->dev_private;
11166         struct mlx5_dev_ctx_shared *sh = priv->sh;
11167         struct mlx5_flow_tbl_resource *tbl;
11168         uint32_t idx = 0;
11169         const uint32_t next_ft_step = 1;
11170         uint32_t next_ft_id = ctx_resource->ft_id + next_ft_step;
11171         uint8_t is_egress = 0;
11172         uint8_t is_transfer = 0;
11173         struct rte_flow_error *error = ctx->error;
11174
11175         /* Register new sample resource. */
11176         resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_SAMPLE], &idx);
11177         if (!resource) {
11178                 rte_flow_error_set(error, ENOMEM,
11179                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
11180                                           NULL,
11181                                           "cannot allocate resource memory");
11182                 return NULL;
11183         }
11184         *resource = *ctx_resource;
11185         /* Create normal path table level */
11186         if (ctx_resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
11187                 is_transfer = 1;
11188         else if (ctx_resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_TX)
11189                 is_egress = 1;
11190         tbl = flow_dv_tbl_resource_get(dev, next_ft_id,
11191                                         is_egress, is_transfer,
11192                                         true, NULL, 0, 0, 0, error);
11193         if (!tbl) {
11194                 rte_flow_error_set(error, ENOMEM,
11195                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
11196                                           NULL,
11197                                           "fail to create normal path table "
11198                                           "for sample");
11199                 goto error;
11200         }
11201         resource->normal_path_tbl = tbl;
11202         if (ctx_resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB) {
11203                 if (!sh->default_miss_action) {
11204                         rte_flow_error_set(error, ENOMEM,
11205                                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
11206                                                 NULL,
11207                                                 "default miss action was not "
11208                                                 "created");
11209                         goto error;
11210                 }
11211                 sample_dv_actions[ctx_resource->sample_act.actions_num++] =
11212                                                 sh->default_miss_action;
11213         }
11214         /* Create a DR sample action */
11215         sampler_attr.sample_ratio = resource->ratio;
11216         sampler_attr.default_next_table = tbl->obj;
11217         sampler_attr.num_sample_actions = ctx_resource->sample_act.actions_num;
11218         sampler_attr.sample_actions = (struct mlx5dv_dr_action **)
11219                                                         &sample_dv_actions[0];
11220         sampler_attr.action = resource->set_action;
11221         if (mlx5_os_flow_dr_create_flow_action_sampler
11222                         (&sampler_attr, &resource->verbs_action)) {
11223                 rte_flow_error_set(error, ENOMEM,
11224                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
11225                                         NULL, "cannot create sample action");
11226                 goto error;
11227         }
11228         resource->idx = idx;
11229         resource->dev = dev;
11230         return &resource->entry;
11231 error:
11232         if (resource->ft_type != MLX5DV_FLOW_TABLE_TYPE_FDB)
11233                 flow_dv_sample_sub_actions_release(dev,
11234                                                    &resource->sample_idx);
11235         if (resource->normal_path_tbl)
11236                 flow_dv_tbl_resource_release(MLX5_SH(dev),
11237                                 resource->normal_path_tbl);
11238         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_SAMPLE], idx);
11239         return NULL;
11240
11241 }
11242
11243 struct mlx5_list_entry *
11244 flow_dv_sample_clone_cb(void *tool_ctx __rte_unused,
11245                          struct mlx5_list_entry *entry __rte_unused,
11246                          void *cb_ctx)
11247 {
11248         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
11249         struct rte_eth_dev *dev = ctx->dev;
11250         struct mlx5_flow_dv_sample_resource *resource;
11251         struct mlx5_priv *priv = dev->data->dev_private;
11252         struct mlx5_dev_ctx_shared *sh = priv->sh;
11253         uint32_t idx = 0;
11254
11255         resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_SAMPLE], &idx);
11256         if (!resource) {
11257                 rte_flow_error_set(ctx->error, ENOMEM,
11258                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
11259                                           NULL,
11260                                           "cannot allocate resource memory");
11261                 return NULL;
11262         }
11263         memcpy(resource, entry, sizeof(*resource));
11264         resource->idx = idx;
11265         resource->dev = dev;
11266         return &resource->entry;
11267 }
11268
11269 void
11270 flow_dv_sample_clone_free_cb(void *tool_ctx __rte_unused,
11271                              struct mlx5_list_entry *entry)
11272 {
11273         struct mlx5_flow_dv_sample_resource *resource =
11274                                   container_of(entry, typeof(*resource), entry);
11275         struct rte_eth_dev *dev = resource->dev;
11276         struct mlx5_priv *priv = dev->data->dev_private;
11277
11278         mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_SAMPLE], resource->idx);
11279 }
11280
11281 /**
11282  * Find existing sample resource or create and register a new one.
11283  *
11284  * @param[in, out] dev
11285  *   Pointer to rte_eth_dev structure.
11286  * @param[in] ref
11287  *   Pointer to sample resource reference.
11288  * @parm[in, out] dev_flow
11289  *   Pointer to the dev_flow.
11290  * @param[out] error
11291  *   pointer to error structure.
11292  *
11293  * @return
11294  *   0 on success otherwise -errno and errno is set.
11295  */
11296 static int
11297 flow_dv_sample_resource_register(struct rte_eth_dev *dev,
11298                          struct mlx5_flow_dv_sample_resource *ref,
11299                          struct mlx5_flow *dev_flow,
11300                          struct rte_flow_error *error)
11301 {
11302         struct mlx5_flow_dv_sample_resource *resource;
11303         struct mlx5_list_entry *entry;
11304         struct mlx5_priv *priv = dev->data->dev_private;
11305         struct mlx5_flow_cb_ctx ctx = {
11306                 .dev = dev,
11307                 .error = error,
11308                 .data = ref,
11309         };
11310
11311         entry = mlx5_list_register(priv->sh->sample_action_list, &ctx);
11312         if (!entry)
11313                 return -rte_errno;
11314         resource = container_of(entry, typeof(*resource), entry);
11315         dev_flow->handle->dvh.rix_sample = resource->idx;
11316         dev_flow->dv.sample_res = resource;
11317         return 0;
11318 }
11319
11320 int
11321 flow_dv_dest_array_match_cb(void *tool_ctx __rte_unused,
11322                             struct mlx5_list_entry *entry, void *cb_ctx)
11323 {
11324         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
11325         struct mlx5_flow_dv_dest_array_resource *ctx_resource = ctx->data;
11326         struct rte_eth_dev *dev = ctx->dev;
11327         struct mlx5_flow_dv_dest_array_resource *resource =
11328                                   container_of(entry, typeof(*resource), entry);
11329         uint32_t idx = 0;
11330
11331         if (ctx_resource->num_of_dest == resource->num_of_dest &&
11332             ctx_resource->ft_type == resource->ft_type &&
11333             !memcmp((void *)resource->sample_act,
11334                     (void *)ctx_resource->sample_act,
11335                    (ctx_resource->num_of_dest *
11336                    sizeof(struct mlx5_flow_sub_actions_list)))) {
11337                 /*
11338                  * Existing sample action should release the prepared
11339                  * sub-actions reference counter.
11340                  */
11341                 for (idx = 0; idx < ctx_resource->num_of_dest; idx++)
11342                         flow_dv_sample_sub_actions_release(dev,
11343                                         &ctx_resource->sample_idx[idx]);
11344                 return 0;
11345         }
11346         return 1;
11347 }
11348
11349 struct mlx5_list_entry *
11350 flow_dv_dest_array_create_cb(void *tool_ctx __rte_unused, void *cb_ctx)
11351 {
11352         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
11353         struct rte_eth_dev *dev = ctx->dev;
11354         struct mlx5_flow_dv_dest_array_resource *resource;
11355         struct mlx5_flow_dv_dest_array_resource *ctx_resource = ctx->data;
11356         struct mlx5dv_dr_action_dest_attr *dest_attr[MLX5_MAX_DEST_NUM] = { 0 };
11357         struct mlx5dv_dr_action_dest_reformat dest_reformat[MLX5_MAX_DEST_NUM];
11358         struct mlx5_priv *priv = dev->data->dev_private;
11359         struct mlx5_dev_ctx_shared *sh = priv->sh;
11360         struct mlx5_flow_sub_actions_list *sample_act;
11361         struct mlx5dv_dr_domain *domain;
11362         uint32_t idx = 0, res_idx = 0;
11363         struct rte_flow_error *error = ctx->error;
11364         uint64_t action_flags;
11365         int ret;
11366
11367         /* Register new destination array resource. */
11368         resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_DEST_ARRAY],
11369                                             &res_idx);
11370         if (!resource) {
11371                 rte_flow_error_set(error, ENOMEM,
11372                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
11373                                           NULL,
11374                                           "cannot allocate resource memory");
11375                 return NULL;
11376         }
11377         *resource = *ctx_resource;
11378         if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
11379                 domain = sh->fdb_domain;
11380         else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_RX)
11381                 domain = sh->rx_domain;
11382         else
11383                 domain = sh->tx_domain;
11384         for (idx = 0; idx < ctx_resource->num_of_dest; idx++) {
11385                 dest_attr[idx] = (struct mlx5dv_dr_action_dest_attr *)
11386                                  mlx5_malloc(MLX5_MEM_ZERO,
11387                                  sizeof(struct mlx5dv_dr_action_dest_attr),
11388                                  0, SOCKET_ID_ANY);
11389                 if (!dest_attr[idx]) {
11390                         rte_flow_error_set(error, ENOMEM,
11391                                            RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
11392                                            NULL,
11393                                            "cannot allocate resource memory");
11394                         goto error;
11395                 }
11396                 dest_attr[idx]->type = MLX5DV_DR_ACTION_DEST;
11397                 sample_act = &ctx_resource->sample_act[idx];
11398                 action_flags = sample_act->action_flags;
11399                 switch (action_flags) {
11400                 case MLX5_FLOW_ACTION_QUEUE:
11401                         dest_attr[idx]->dest = sample_act->dr_queue_action;
11402                         break;
11403                 case (MLX5_FLOW_ACTION_PORT_ID | MLX5_FLOW_ACTION_ENCAP):
11404                         dest_attr[idx]->type = MLX5DV_DR_ACTION_DEST_REFORMAT;
11405                         dest_attr[idx]->dest_reformat = &dest_reformat[idx];
11406                         dest_attr[idx]->dest_reformat->reformat =
11407                                         sample_act->dr_encap_action;
11408                         dest_attr[idx]->dest_reformat->dest =
11409                                         sample_act->dr_port_id_action;
11410                         break;
11411                 case MLX5_FLOW_ACTION_PORT_ID:
11412                         dest_attr[idx]->dest = sample_act->dr_port_id_action;
11413                         break;
11414                 case MLX5_FLOW_ACTION_JUMP:
11415                         dest_attr[idx]->dest = sample_act->dr_jump_action;
11416                         break;
11417                 default:
11418                         rte_flow_error_set(error, EINVAL,
11419                                            RTE_FLOW_ERROR_TYPE_ACTION,
11420                                            NULL,
11421                                            "unsupported actions type");
11422                         goto error;
11423                 }
11424         }
11425         /* create a dest array actioin */
11426         ret = mlx5_os_flow_dr_create_flow_action_dest_array
11427                                                 (domain,
11428                                                  resource->num_of_dest,
11429                                                  dest_attr,
11430                                                  &resource->action);
11431         if (ret) {
11432                 rte_flow_error_set(error, ENOMEM,
11433                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
11434                                    NULL,
11435                                    "cannot create destination array action");
11436                 goto error;
11437         }
11438         resource->idx = res_idx;
11439         resource->dev = dev;
11440         for (idx = 0; idx < ctx_resource->num_of_dest; idx++)
11441                 mlx5_free(dest_attr[idx]);
11442         return &resource->entry;
11443 error:
11444         for (idx = 0; idx < ctx_resource->num_of_dest; idx++) {
11445                 flow_dv_sample_sub_actions_release(dev,
11446                                                    &resource->sample_idx[idx]);
11447                 if (dest_attr[idx])
11448                         mlx5_free(dest_attr[idx]);
11449         }
11450         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_DEST_ARRAY], res_idx);
11451         return NULL;
11452 }
11453
11454 struct mlx5_list_entry *
11455 flow_dv_dest_array_clone_cb(void *tool_ctx __rte_unused,
11456                             struct mlx5_list_entry *entry __rte_unused,
11457                             void *cb_ctx)
11458 {
11459         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
11460         struct rte_eth_dev *dev = ctx->dev;
11461         struct mlx5_flow_dv_dest_array_resource *resource;
11462         struct mlx5_priv *priv = dev->data->dev_private;
11463         struct mlx5_dev_ctx_shared *sh = priv->sh;
11464         uint32_t res_idx = 0;
11465         struct rte_flow_error *error = ctx->error;
11466
11467         resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_DEST_ARRAY],
11468                                       &res_idx);
11469         if (!resource) {
11470                 rte_flow_error_set(error, ENOMEM,
11471                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
11472                                           NULL,
11473                                           "cannot allocate dest-array memory");
11474                 return NULL;
11475         }
11476         memcpy(resource, entry, sizeof(*resource));
11477         resource->idx = res_idx;
11478         resource->dev = dev;
11479         return &resource->entry;
11480 }
11481
11482 void
11483 flow_dv_dest_array_clone_free_cb(void *tool_ctx __rte_unused,
11484                                  struct mlx5_list_entry *entry)
11485 {
11486         struct mlx5_flow_dv_dest_array_resource *resource =
11487                         container_of(entry, typeof(*resource), entry);
11488         struct rte_eth_dev *dev = resource->dev;
11489         struct mlx5_priv *priv = dev->data->dev_private;
11490
11491         mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_DEST_ARRAY], resource->idx);
11492 }
11493
11494 /**
11495  * Find existing destination array resource or create and register a new one.
11496  *
11497  * @param[in, out] dev
11498  *   Pointer to rte_eth_dev structure.
11499  * @param[in] ref
11500  *   Pointer to destination array resource reference.
11501  * @parm[in, out] dev_flow
11502  *   Pointer to the dev_flow.
11503  * @param[out] error
11504  *   pointer to error structure.
11505  *
11506  * @return
11507  *   0 on success otherwise -errno and errno is set.
11508  */
11509 static int
11510 flow_dv_dest_array_resource_register(struct rte_eth_dev *dev,
11511                          struct mlx5_flow_dv_dest_array_resource *ref,
11512                          struct mlx5_flow *dev_flow,
11513                          struct rte_flow_error *error)
11514 {
11515         struct mlx5_flow_dv_dest_array_resource *resource;
11516         struct mlx5_priv *priv = dev->data->dev_private;
11517         struct mlx5_list_entry *entry;
11518         struct mlx5_flow_cb_ctx ctx = {
11519                 .dev = dev,
11520                 .error = error,
11521                 .data = ref,
11522         };
11523
11524         entry = mlx5_list_register(priv->sh->dest_array_list, &ctx);
11525         if (!entry)
11526                 return -rte_errno;
11527         resource = container_of(entry, typeof(*resource), entry);
11528         dev_flow->handle->dvh.rix_dest_array = resource->idx;
11529         dev_flow->dv.dest_array_res = resource;
11530         return 0;
11531 }
11532
11533 /**
11534  * Convert Sample action to DV specification.
11535  *
11536  * @param[in] dev
11537  *   Pointer to rte_eth_dev structure.
11538  * @param[in] action
11539  *   Pointer to sample action structure.
11540  * @param[in, out] dev_flow
11541  *   Pointer to the mlx5_flow.
11542  * @param[in] attr
11543  *   Pointer to the flow attributes.
11544  * @param[in, out] num_of_dest
11545  *   Pointer to the num of destination.
11546  * @param[in, out] sample_actions
11547  *   Pointer to sample actions list.
11548  * @param[in, out] res
11549  *   Pointer to sample resource.
11550  * @param[out] error
11551  *   Pointer to the error structure.
11552  *
11553  * @return
11554  *   0 on success, a negative errno value otherwise and rte_errno is set.
11555  */
11556 static int
11557 flow_dv_translate_action_sample(struct rte_eth_dev *dev,
11558                                 const struct rte_flow_action_sample *action,
11559                                 struct mlx5_flow *dev_flow,
11560                                 const struct rte_flow_attr *attr,
11561                                 uint32_t *num_of_dest,
11562                                 void **sample_actions,
11563                                 struct mlx5_flow_dv_sample_resource *res,
11564                                 struct rte_flow_error *error)
11565 {
11566         struct mlx5_priv *priv = dev->data->dev_private;
11567         const struct rte_flow_action *sub_actions;
11568         struct mlx5_flow_sub_actions_list *sample_act;
11569         struct mlx5_flow_sub_actions_idx *sample_idx;
11570         struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
11571         struct rte_flow *flow = dev_flow->flow;
11572         struct mlx5_flow_rss_desc *rss_desc;
11573         uint64_t action_flags = 0;
11574
11575         MLX5_ASSERT(wks);
11576         rss_desc = &wks->rss_desc;
11577         sample_act = &res->sample_act;
11578         sample_idx = &res->sample_idx;
11579         res->ratio = action->ratio;
11580         sub_actions = action->actions;
11581         for (; sub_actions->type != RTE_FLOW_ACTION_TYPE_END; sub_actions++) {
11582                 int type = sub_actions->type;
11583                 uint32_t pre_rix = 0;
11584                 void *pre_r;
11585                 switch (type) {
11586                 case RTE_FLOW_ACTION_TYPE_QUEUE:
11587                 {
11588                         const struct rte_flow_action_queue *queue;
11589                         struct mlx5_hrxq *hrxq;
11590                         uint32_t hrxq_idx;
11591
11592                         queue = sub_actions->conf;
11593                         rss_desc->queue_num = 1;
11594                         rss_desc->queue[0] = queue->index;
11595                         hrxq = flow_dv_hrxq_prepare(dev, dev_flow,
11596                                                     rss_desc, &hrxq_idx);
11597                         if (!hrxq)
11598                                 return rte_flow_error_set
11599                                         (error, rte_errno,
11600                                          RTE_FLOW_ERROR_TYPE_ACTION,
11601                                          NULL,
11602                                          "cannot create fate queue");
11603                         sample_act->dr_queue_action = hrxq->action;
11604                         sample_idx->rix_hrxq = hrxq_idx;
11605                         sample_actions[sample_act->actions_num++] =
11606                                                 hrxq->action;
11607                         (*num_of_dest)++;
11608                         action_flags |= MLX5_FLOW_ACTION_QUEUE;
11609                         if (action_flags & MLX5_FLOW_ACTION_MARK)
11610                                 dev_flow->handle->rix_hrxq = hrxq_idx;
11611                         dev_flow->handle->fate_action =
11612                                         MLX5_FLOW_FATE_QUEUE;
11613                         break;
11614                 }
11615                 case RTE_FLOW_ACTION_TYPE_RSS:
11616                 {
11617                         struct mlx5_hrxq *hrxq;
11618                         uint32_t hrxq_idx;
11619                         const struct rte_flow_action_rss *rss;
11620                         const uint8_t *rss_key;
11621
11622                         rss = sub_actions->conf;
11623                         memcpy(rss_desc->queue, rss->queue,
11624                                rss->queue_num * sizeof(uint16_t));
11625                         rss_desc->queue_num = rss->queue_num;
11626                         /* NULL RSS key indicates default RSS key. */
11627                         rss_key = !rss->key ? rss_hash_default_key : rss->key;
11628                         memcpy(rss_desc->key, rss_key, MLX5_RSS_HASH_KEY_LEN);
11629                         /*
11630                          * rss->level and rss.types should be set in advance
11631                          * when expanding items for RSS.
11632                          */
11633                         flow_dv_hashfields_set(dev_flow, rss_desc);
11634                         hrxq = flow_dv_hrxq_prepare(dev, dev_flow,
11635                                                     rss_desc, &hrxq_idx);
11636                         if (!hrxq)
11637                                 return rte_flow_error_set
11638                                         (error, rte_errno,
11639                                          RTE_FLOW_ERROR_TYPE_ACTION,
11640                                          NULL,
11641                                          "cannot create fate queue");
11642                         sample_act->dr_queue_action = hrxq->action;
11643                         sample_idx->rix_hrxq = hrxq_idx;
11644                         sample_actions[sample_act->actions_num++] =
11645                                                 hrxq->action;
11646                         (*num_of_dest)++;
11647                         action_flags |= MLX5_FLOW_ACTION_RSS;
11648                         if (action_flags & MLX5_FLOW_ACTION_MARK)
11649                                 dev_flow->handle->rix_hrxq = hrxq_idx;
11650                         dev_flow->handle->fate_action =
11651                                         MLX5_FLOW_FATE_QUEUE;
11652                         break;
11653                 }
11654                 case RTE_FLOW_ACTION_TYPE_MARK:
11655                 {
11656                         uint32_t tag_be = mlx5_flow_mark_set
11657                                 (((const struct rte_flow_action_mark *)
11658                                 (sub_actions->conf))->id);
11659
11660                         dev_flow->handle->mark = 1;
11661                         pre_rix = dev_flow->handle->dvh.rix_tag;
11662                         /* Save the mark resource before sample */
11663                         pre_r = dev_flow->dv.tag_resource;
11664                         if (flow_dv_tag_resource_register(dev, tag_be,
11665                                                   dev_flow, error))
11666                                 return -rte_errno;
11667                         MLX5_ASSERT(dev_flow->dv.tag_resource);
11668                         sample_act->dr_tag_action =
11669                                 dev_flow->dv.tag_resource->action;
11670                         sample_idx->rix_tag =
11671                                 dev_flow->handle->dvh.rix_tag;
11672                         sample_actions[sample_act->actions_num++] =
11673                                                 sample_act->dr_tag_action;
11674                         /* Recover the mark resource after sample */
11675                         dev_flow->dv.tag_resource = pre_r;
11676                         dev_flow->handle->dvh.rix_tag = pre_rix;
11677                         action_flags |= MLX5_FLOW_ACTION_MARK;
11678                         break;
11679                 }
11680                 case RTE_FLOW_ACTION_TYPE_COUNT:
11681                 {
11682                         if (!flow->counter) {
11683                                 flow->counter =
11684                                         flow_dv_translate_create_counter(dev,
11685                                                 dev_flow, sub_actions->conf,
11686                                                 0);
11687                                 if (!flow->counter)
11688                                         return rte_flow_error_set
11689                                                 (error, rte_errno,
11690                                                 RTE_FLOW_ERROR_TYPE_ACTION,
11691                                                 NULL,
11692                                                 "cannot create counter"
11693                                                 " object.");
11694                         }
11695                         sample_act->dr_cnt_action =
11696                                   (flow_dv_counter_get_by_idx(dev,
11697                                   flow->counter, NULL))->action;
11698                         sample_actions[sample_act->actions_num++] =
11699                                                 sample_act->dr_cnt_action;
11700                         action_flags |= MLX5_FLOW_ACTION_COUNT;
11701                         break;
11702                 }
11703                 case RTE_FLOW_ACTION_TYPE_PORT_ID:
11704                 case RTE_FLOW_ACTION_TYPE_REPRESENTED_PORT:
11705                 {
11706                         struct mlx5_flow_dv_port_id_action_resource
11707                                         port_id_resource;
11708                         uint32_t port_id = 0;
11709
11710                         memset(&port_id_resource, 0, sizeof(port_id_resource));
11711                         /* Save the port id resource before sample */
11712                         pre_rix = dev_flow->handle->rix_port_id_action;
11713                         pre_r = dev_flow->dv.port_id_action;
11714                         if (flow_dv_translate_action_port_id(dev, sub_actions,
11715                                                              &port_id, error))
11716                                 return -rte_errno;
11717                         port_id_resource.port_id = port_id;
11718                         if (flow_dv_port_id_action_resource_register
11719                             (dev, &port_id_resource, dev_flow, error))
11720                                 return -rte_errno;
11721                         sample_act->dr_port_id_action =
11722                                 dev_flow->dv.port_id_action->action;
11723                         sample_idx->rix_port_id_action =
11724                                 dev_flow->handle->rix_port_id_action;
11725                         sample_actions[sample_act->actions_num++] =
11726                                                 sample_act->dr_port_id_action;
11727                         /* Recover the port id resource after sample */
11728                         dev_flow->dv.port_id_action = pre_r;
11729                         dev_flow->handle->rix_port_id_action = pre_rix;
11730                         (*num_of_dest)++;
11731                         action_flags |= MLX5_FLOW_ACTION_PORT_ID;
11732                         break;
11733                 }
11734                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
11735                 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
11736                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
11737                         /* Save the encap resource before sample */
11738                         pre_rix = dev_flow->handle->dvh.rix_encap_decap;
11739                         pre_r = dev_flow->dv.encap_decap;
11740                         if (flow_dv_create_action_l2_encap(dev, sub_actions,
11741                                                            dev_flow,
11742                                                            attr->transfer,
11743                                                            error))
11744                                 return -rte_errno;
11745                         sample_act->dr_encap_action =
11746                                 dev_flow->dv.encap_decap->action;
11747                         sample_idx->rix_encap_decap =
11748                                 dev_flow->handle->dvh.rix_encap_decap;
11749                         sample_actions[sample_act->actions_num++] =
11750                                                 sample_act->dr_encap_action;
11751                         /* Recover the encap resource after sample */
11752                         dev_flow->dv.encap_decap = pre_r;
11753                         dev_flow->handle->dvh.rix_encap_decap = pre_rix;
11754                         action_flags |= MLX5_FLOW_ACTION_ENCAP;
11755                         break;
11756                 default:
11757                         return rte_flow_error_set(error, EINVAL,
11758                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
11759                                 NULL,
11760                                 "Not support for sampler action");
11761                 }
11762         }
11763         sample_act->action_flags = action_flags;
11764         res->ft_id = dev_flow->dv.group;
11765         if (attr->transfer) {
11766                 union {
11767                         uint32_t action_in[MLX5_ST_SZ_DW(set_action_in)];
11768                         uint64_t set_action;
11769                 } action_ctx = { .set_action = 0 };
11770
11771                 res->ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
11772                 MLX5_SET(set_action_in, action_ctx.action_in, action_type,
11773                          MLX5_MODIFICATION_TYPE_SET);
11774                 MLX5_SET(set_action_in, action_ctx.action_in, field,
11775                          MLX5_MODI_META_REG_C_0);
11776                 MLX5_SET(set_action_in, action_ctx.action_in, data,
11777                          priv->vport_meta_tag);
11778                 res->set_action = action_ctx.set_action;
11779         } else if (attr->ingress) {
11780                 res->ft_type = MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
11781         } else {
11782                 res->ft_type = MLX5DV_FLOW_TABLE_TYPE_NIC_TX;
11783         }
11784         return 0;
11785 }
11786
11787 /**
11788  * Convert Sample action to DV specification.
11789  *
11790  * @param[in] dev
11791  *   Pointer to rte_eth_dev structure.
11792  * @param[in, out] dev_flow
11793  *   Pointer to the mlx5_flow.
11794  * @param[in] num_of_dest
11795  *   The num of destination.
11796  * @param[in, out] res
11797  *   Pointer to sample resource.
11798  * @param[in, out] mdest_res
11799  *   Pointer to destination array resource.
11800  * @param[in] sample_actions
11801  *   Pointer to sample path actions list.
11802  * @param[in] action_flags
11803  *   Holds the actions detected until now.
11804  * @param[out] error
11805  *   Pointer to the error structure.
11806  *
11807  * @return
11808  *   0 on success, a negative errno value otherwise and rte_errno is set.
11809  */
11810 static int
11811 flow_dv_create_action_sample(struct rte_eth_dev *dev,
11812                              struct mlx5_flow *dev_flow,
11813                              uint32_t num_of_dest,
11814                              struct mlx5_flow_dv_sample_resource *res,
11815                              struct mlx5_flow_dv_dest_array_resource *mdest_res,
11816                              void **sample_actions,
11817                              uint64_t action_flags,
11818                              struct rte_flow_error *error)
11819 {
11820         /* update normal path action resource into last index of array */
11821         uint32_t dest_index = MLX5_MAX_DEST_NUM - 1;
11822         struct mlx5_flow_sub_actions_list *sample_act =
11823                                         &mdest_res->sample_act[dest_index];
11824         struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
11825         struct mlx5_flow_rss_desc *rss_desc;
11826         uint32_t normal_idx = 0;
11827         struct mlx5_hrxq *hrxq;
11828         uint32_t hrxq_idx;
11829
11830         MLX5_ASSERT(wks);
11831         rss_desc = &wks->rss_desc;
11832         if (num_of_dest > 1) {
11833                 if (sample_act->action_flags & MLX5_FLOW_ACTION_QUEUE) {
11834                         /* Handle QP action for mirroring */
11835                         hrxq = flow_dv_hrxq_prepare(dev, dev_flow,
11836                                                     rss_desc, &hrxq_idx);
11837                         if (!hrxq)
11838                                 return rte_flow_error_set
11839                                      (error, rte_errno,
11840                                       RTE_FLOW_ERROR_TYPE_ACTION,
11841                                       NULL,
11842                                       "cannot create rx queue");
11843                         normal_idx++;
11844                         mdest_res->sample_idx[dest_index].rix_hrxq = hrxq_idx;
11845                         sample_act->dr_queue_action = hrxq->action;
11846                         if (action_flags & MLX5_FLOW_ACTION_MARK)
11847                                 dev_flow->handle->rix_hrxq = hrxq_idx;
11848                         dev_flow->handle->fate_action = MLX5_FLOW_FATE_QUEUE;
11849                 }
11850                 if (sample_act->action_flags & MLX5_FLOW_ACTION_ENCAP) {
11851                         normal_idx++;
11852                         mdest_res->sample_idx[dest_index].rix_encap_decap =
11853                                 dev_flow->handle->dvh.rix_encap_decap;
11854                         sample_act->dr_encap_action =
11855                                 dev_flow->dv.encap_decap->action;
11856                         dev_flow->handle->dvh.rix_encap_decap = 0;
11857                 }
11858                 if (sample_act->action_flags & MLX5_FLOW_ACTION_PORT_ID) {
11859                         normal_idx++;
11860                         mdest_res->sample_idx[dest_index].rix_port_id_action =
11861                                 dev_flow->handle->rix_port_id_action;
11862                         sample_act->dr_port_id_action =
11863                                 dev_flow->dv.port_id_action->action;
11864                         dev_flow->handle->rix_port_id_action = 0;
11865                 }
11866                 if (sample_act->action_flags & MLX5_FLOW_ACTION_JUMP) {
11867                         normal_idx++;
11868                         mdest_res->sample_idx[dest_index].rix_jump =
11869                                 dev_flow->handle->rix_jump;
11870                         sample_act->dr_jump_action =
11871                                 dev_flow->dv.jump->action;
11872                         dev_flow->handle->rix_jump = 0;
11873                 }
11874                 sample_act->actions_num = normal_idx;
11875                 /* update sample action resource into first index of array */
11876                 mdest_res->ft_type = res->ft_type;
11877                 memcpy(&mdest_res->sample_idx[0], &res->sample_idx,
11878                                 sizeof(struct mlx5_flow_sub_actions_idx));
11879                 memcpy(&mdest_res->sample_act[0], &res->sample_act,
11880                                 sizeof(struct mlx5_flow_sub_actions_list));
11881                 mdest_res->num_of_dest = num_of_dest;
11882                 if (flow_dv_dest_array_resource_register(dev, mdest_res,
11883                                                          dev_flow, error))
11884                         return rte_flow_error_set(error, EINVAL,
11885                                                   RTE_FLOW_ERROR_TYPE_ACTION,
11886                                                   NULL, "can't create sample "
11887                                                   "action");
11888         } else {
11889                 res->sub_actions = sample_actions;
11890                 if (flow_dv_sample_resource_register(dev, res, dev_flow, error))
11891                         return rte_flow_error_set(error, EINVAL,
11892                                                   RTE_FLOW_ERROR_TYPE_ACTION,
11893                                                   NULL,
11894                                                   "can't create sample action");
11895         }
11896         return 0;
11897 }
11898
11899 /**
11900  * Remove an ASO age action from age actions list.
11901  *
11902  * @param[in] dev
11903  *   Pointer to the Ethernet device structure.
11904  * @param[in] age
11905  *   Pointer to the aso age action handler.
11906  */
11907 static void
11908 flow_dv_aso_age_remove_from_age(struct rte_eth_dev *dev,
11909                                 struct mlx5_aso_age_action *age)
11910 {
11911         struct mlx5_age_info *age_info;
11912         struct mlx5_age_param *age_param = &age->age_params;
11913         struct mlx5_priv *priv = dev->data->dev_private;
11914         uint16_t expected = AGE_CANDIDATE;
11915
11916         age_info = GET_PORT_AGE_INFO(priv);
11917         if (!__atomic_compare_exchange_n(&age_param->state, &expected,
11918                                          AGE_FREE, false, __ATOMIC_RELAXED,
11919                                          __ATOMIC_RELAXED)) {
11920                 /**
11921                  * We need the lock even it is age timeout,
11922                  * since age action may still in process.
11923                  */
11924                 rte_spinlock_lock(&age_info->aged_sl);
11925                 LIST_REMOVE(age, next);
11926                 rte_spinlock_unlock(&age_info->aged_sl);
11927                 __atomic_store_n(&age_param->state, AGE_FREE, __ATOMIC_RELAXED);
11928         }
11929 }
11930
11931 /**
11932  * Release an ASO age action.
11933  *
11934  * @param[in] dev
11935  *   Pointer to the Ethernet device structure.
11936  * @param[in] age_idx
11937  *   Index of ASO age action to release.
11938  * @param[in] flow
11939  *   True if the release operation is during flow destroy operation.
11940  *   False if the release operation is during action destroy operation.
11941  *
11942  * @return
11943  *   0 when age action was removed, otherwise the number of references.
11944  */
11945 static int
11946 flow_dv_aso_age_release(struct rte_eth_dev *dev, uint32_t age_idx)
11947 {
11948         struct mlx5_priv *priv = dev->data->dev_private;
11949         struct mlx5_aso_age_mng *mng = priv->sh->aso_age_mng;
11950         struct mlx5_aso_age_action *age = flow_aso_age_get_by_idx(dev, age_idx);
11951         uint32_t ret = __atomic_sub_fetch(&age->refcnt, 1, __ATOMIC_RELAXED);
11952
11953         if (!ret) {
11954                 flow_dv_aso_age_remove_from_age(dev, age);
11955                 rte_spinlock_lock(&mng->free_sl);
11956                 LIST_INSERT_HEAD(&mng->free, age, next);
11957                 rte_spinlock_unlock(&mng->free_sl);
11958         }
11959         return ret;
11960 }
11961
11962 /**
11963  * Resize the ASO age pools array by MLX5_CNT_CONTAINER_RESIZE pools.
11964  *
11965  * @param[in] dev
11966  *   Pointer to the Ethernet device structure.
11967  *
11968  * @return
11969  *   0 on success, otherwise negative errno value and rte_errno is set.
11970  */
11971 static int
11972 flow_dv_aso_age_pools_resize(struct rte_eth_dev *dev)
11973 {
11974         struct mlx5_priv *priv = dev->data->dev_private;
11975         struct mlx5_aso_age_mng *mng = priv->sh->aso_age_mng;
11976         void *old_pools = mng->pools;
11977         uint32_t resize = mng->n + MLX5_CNT_CONTAINER_RESIZE;
11978         uint32_t mem_size = sizeof(struct mlx5_aso_age_pool *) * resize;
11979         void *pools = mlx5_malloc(MLX5_MEM_ZERO, mem_size, 0, SOCKET_ID_ANY);
11980
11981         if (!pools) {
11982                 rte_errno = ENOMEM;
11983                 return -ENOMEM;
11984         }
11985         if (old_pools) {
11986                 memcpy(pools, old_pools,
11987                        mng->n * sizeof(struct mlx5_flow_counter_pool *));
11988                 mlx5_free(old_pools);
11989         } else {
11990                 /* First ASO flow hit allocation - starting ASO data-path. */
11991                 int ret = mlx5_aso_flow_hit_queue_poll_start(priv->sh);
11992
11993                 if (ret) {
11994                         mlx5_free(pools);
11995                         return ret;
11996                 }
11997         }
11998         mng->n = resize;
11999         mng->pools = pools;
12000         return 0;
12001 }
12002
12003 /**
12004  * Create and initialize a new ASO aging pool.
12005  *
12006  * @param[in] dev
12007  *   Pointer to the Ethernet device structure.
12008  * @param[out] age_free
12009  *   Where to put the pointer of a new age action.
12010  *
12011  * @return
12012  *   The age actions pool pointer and @p age_free is set on success,
12013  *   NULL otherwise and rte_errno is set.
12014  */
12015 static struct mlx5_aso_age_pool *
12016 flow_dv_age_pool_create(struct rte_eth_dev *dev,
12017                         struct mlx5_aso_age_action **age_free)
12018 {
12019         struct mlx5_priv *priv = dev->data->dev_private;
12020         struct mlx5_aso_age_mng *mng = priv->sh->aso_age_mng;
12021         struct mlx5_aso_age_pool *pool = NULL;
12022         struct mlx5_devx_obj *obj = NULL;
12023         uint32_t i;
12024
12025         obj = mlx5_devx_cmd_create_flow_hit_aso_obj(priv->sh->cdev->ctx,
12026                                                     priv->sh->cdev->pdn);
12027         if (!obj) {
12028                 rte_errno = ENODATA;
12029                 DRV_LOG(ERR, "Failed to create flow_hit_aso_obj using DevX.");
12030                 return NULL;
12031         }
12032         pool = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*pool), 0, SOCKET_ID_ANY);
12033         if (!pool) {
12034                 claim_zero(mlx5_devx_cmd_destroy(obj));
12035                 rte_errno = ENOMEM;
12036                 return NULL;
12037         }
12038         pool->flow_hit_aso_obj = obj;
12039         pool->time_of_last_age_check = MLX5_CURR_TIME_SEC;
12040         rte_rwlock_write_lock(&mng->resize_rwl);
12041         pool->index = mng->next;
12042         /* Resize pools array if there is no room for the new pool in it. */
12043         if (pool->index == mng->n && flow_dv_aso_age_pools_resize(dev)) {
12044                 claim_zero(mlx5_devx_cmd_destroy(obj));
12045                 mlx5_free(pool);
12046                 rte_rwlock_write_unlock(&mng->resize_rwl);
12047                 return NULL;
12048         }
12049         mng->pools[pool->index] = pool;
12050         mng->next++;
12051         rte_rwlock_write_unlock(&mng->resize_rwl);
12052         /* Assign the first action in the new pool, the rest go to free list. */
12053         *age_free = &pool->actions[0];
12054         for (i = 1; i < MLX5_ASO_AGE_ACTIONS_PER_POOL; i++) {
12055                 pool->actions[i].offset = i;
12056                 LIST_INSERT_HEAD(&mng->free, &pool->actions[i], next);
12057         }
12058         return pool;
12059 }
12060
12061 /**
12062  * Allocate a ASO aging bit.
12063  *
12064  * @param[in] dev
12065  *   Pointer to the Ethernet device structure.
12066  * @param[out] error
12067  *   Pointer to the error structure.
12068  *
12069  * @return
12070  *   Index to ASO age action on success, 0 otherwise and rte_errno is set.
12071  */
12072 static uint32_t
12073 flow_dv_aso_age_alloc(struct rte_eth_dev *dev, struct rte_flow_error *error)
12074 {
12075         struct mlx5_priv *priv = dev->data->dev_private;
12076         const struct mlx5_aso_age_pool *pool;
12077         struct mlx5_aso_age_action *age_free = NULL;
12078         struct mlx5_aso_age_mng *mng = priv->sh->aso_age_mng;
12079
12080         MLX5_ASSERT(mng);
12081         /* Try to get the next free age action bit. */
12082         rte_spinlock_lock(&mng->free_sl);
12083         age_free = LIST_FIRST(&mng->free);
12084         if (age_free) {
12085                 LIST_REMOVE(age_free, next);
12086         } else if (!flow_dv_age_pool_create(dev, &age_free)) {
12087                 rte_spinlock_unlock(&mng->free_sl);
12088                 rte_flow_error_set(error, rte_errno, RTE_FLOW_ERROR_TYPE_ACTION,
12089                                    NULL, "failed to create ASO age pool");
12090                 return 0; /* 0 is an error. */
12091         }
12092         rte_spinlock_unlock(&mng->free_sl);
12093         pool = container_of
12094           ((const struct mlx5_aso_age_action (*)[MLX5_ASO_AGE_ACTIONS_PER_POOL])
12095                   (age_free - age_free->offset), const struct mlx5_aso_age_pool,
12096                                                                        actions);
12097         if (!age_free->dr_action) {
12098                 int reg_c = mlx5_flow_get_reg_id(dev, MLX5_ASO_FLOW_HIT, 0,
12099                                                  error);
12100
12101                 if (reg_c < 0) {
12102                         rte_flow_error_set(error, rte_errno,
12103                                            RTE_FLOW_ERROR_TYPE_ACTION,
12104                                            NULL, "failed to get reg_c "
12105                                            "for ASO flow hit");
12106                         return 0; /* 0 is an error. */
12107                 }
12108 #ifdef HAVE_MLX5_DR_CREATE_ACTION_ASO
12109                 age_free->dr_action = mlx5_glue->dv_create_flow_action_aso
12110                                 (priv->sh->rx_domain,
12111                                  pool->flow_hit_aso_obj->obj, age_free->offset,
12112                                  MLX5DV_DR_ACTION_FLAGS_ASO_FIRST_HIT_SET,
12113                                  (reg_c - REG_C_0));
12114 #endif /* HAVE_MLX5_DR_CREATE_ACTION_ASO */
12115                 if (!age_free->dr_action) {
12116                         rte_errno = errno;
12117                         rte_spinlock_lock(&mng->free_sl);
12118                         LIST_INSERT_HEAD(&mng->free, age_free, next);
12119                         rte_spinlock_unlock(&mng->free_sl);
12120                         rte_flow_error_set(error, rte_errno,
12121                                            RTE_FLOW_ERROR_TYPE_ACTION,
12122                                            NULL, "failed to create ASO "
12123                                            "flow hit action");
12124                         return 0; /* 0 is an error. */
12125                 }
12126         }
12127         __atomic_store_n(&age_free->refcnt, 1, __ATOMIC_RELAXED);
12128         return pool->index | ((age_free->offset + 1) << 16);
12129 }
12130
12131 /**
12132  * Initialize flow ASO age parameters.
12133  *
12134  * @param[in] dev
12135  *   Pointer to rte_eth_dev structure.
12136  * @param[in] age_idx
12137  *   Index of ASO age action.
12138  * @param[in] context
12139  *   Pointer to flow counter age context.
12140  * @param[in] timeout
12141  *   Aging timeout in seconds.
12142  *
12143  */
12144 static void
12145 flow_dv_aso_age_params_init(struct rte_eth_dev *dev,
12146                             uint32_t age_idx,
12147                             void *context,
12148                             uint32_t timeout)
12149 {
12150         struct mlx5_aso_age_action *aso_age;
12151
12152         aso_age = flow_aso_age_get_by_idx(dev, age_idx);
12153         MLX5_ASSERT(aso_age);
12154         aso_age->age_params.context = context;
12155         aso_age->age_params.timeout = timeout;
12156         aso_age->age_params.port_id = dev->data->port_id;
12157         __atomic_store_n(&aso_age->age_params.sec_since_last_hit, 0,
12158                          __ATOMIC_RELAXED);
12159         __atomic_store_n(&aso_age->age_params.state, AGE_CANDIDATE,
12160                          __ATOMIC_RELAXED);
12161 }
12162
12163 static void
12164 flow_dv_translate_integrity_l4(const struct rte_flow_item_integrity *mask,
12165                                const struct rte_flow_item_integrity *value,
12166                                void *headers_m, void *headers_v)
12167 {
12168         if (mask->l4_ok) {
12169                 /* RTE l4_ok filter aggregates hardware l4_ok and
12170                  * l4_checksum_ok filters.
12171                  * Positive RTE l4_ok match requires hardware match on both L4
12172                  * hardware integrity bits.
12173                  * For negative match, check hardware l4_checksum_ok bit only,
12174                  * because hardware sets that bit to 0 for all packets
12175                  * with bad L4.
12176                  */
12177                 if (value->l4_ok) {
12178                         MLX5_SET(fte_match_set_lyr_2_4, headers_m, l4_ok, 1);
12179                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, l4_ok, 1);
12180                 }
12181                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, l4_checksum_ok, 1);
12182                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, l4_checksum_ok,
12183                          !!value->l4_ok);
12184         }
12185         if (mask->l4_csum_ok) {
12186                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, l4_checksum_ok, 1);
12187                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, l4_checksum_ok,
12188                          value->l4_csum_ok);
12189         }
12190 }
12191
12192 static void
12193 flow_dv_translate_integrity_l3(const struct rte_flow_item_integrity *mask,
12194                                const struct rte_flow_item_integrity *value,
12195                                void *headers_m, void *headers_v, bool is_ipv4)
12196 {
12197         if (mask->l3_ok) {
12198                 /* RTE l3_ok filter aggregates for IPv4 hardware l3_ok and
12199                  * ipv4_csum_ok filters.
12200                  * Positive RTE l3_ok match requires hardware match on both L3
12201                  * hardware integrity bits.
12202                  * For negative match, check hardware l3_csum_ok bit only,
12203                  * because hardware sets that bit to 0 for all packets
12204                  * with bad L3.
12205                  */
12206                 if (is_ipv4) {
12207                         if (value->l3_ok) {
12208                                 MLX5_SET(fte_match_set_lyr_2_4, headers_m,
12209                                          l3_ok, 1);
12210                                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
12211                                          l3_ok, 1);
12212                         }
12213                         MLX5_SET(fte_match_set_lyr_2_4, headers_m,
12214                                  ipv4_checksum_ok, 1);
12215                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
12216                                  ipv4_checksum_ok, !!value->l3_ok);
12217                 } else {
12218                         MLX5_SET(fte_match_set_lyr_2_4, headers_m, l3_ok, 1);
12219                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, l3_ok,
12220                                  value->l3_ok);
12221                 }
12222         }
12223         if (mask->ipv4_csum_ok) {
12224                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ipv4_checksum_ok, 1);
12225                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ipv4_checksum_ok,
12226                          value->ipv4_csum_ok);
12227         }
12228 }
12229
12230 static void
12231 set_integrity_bits(void *headers_m, void *headers_v,
12232                    const struct rte_flow_item *integrity_item, bool is_l3_ip4)
12233 {
12234         const struct rte_flow_item_integrity *spec = integrity_item->spec;
12235         const struct rte_flow_item_integrity *mask = integrity_item->mask;
12236
12237         /* Integrity bits validation cleared spec pointer */
12238         MLX5_ASSERT(spec != NULL);
12239         if (!mask)
12240                 mask = &rte_flow_item_integrity_mask;
12241         flow_dv_translate_integrity_l3(mask, spec, headers_m, headers_v,
12242                                        is_l3_ip4);
12243         flow_dv_translate_integrity_l4(mask, spec, headers_m, headers_v);
12244 }
12245
12246 static void
12247 flow_dv_translate_item_integrity_post(void *matcher, void *key,
12248                                       const
12249                                       struct rte_flow_item *integrity_items[2],
12250                                       uint64_t pattern_flags)
12251 {
12252         void *headers_m, *headers_v;
12253         bool is_l3_ip4;
12254
12255         if (pattern_flags & MLX5_FLOW_ITEM_INNER_INTEGRITY) {
12256                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
12257                                          inner_headers);
12258                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
12259                 is_l3_ip4 = (pattern_flags & MLX5_FLOW_LAYER_INNER_L3_IPV4) !=
12260                             0;
12261                 set_integrity_bits(headers_m, headers_v,
12262                                    integrity_items[1], is_l3_ip4);
12263         }
12264         if (pattern_flags & MLX5_FLOW_ITEM_OUTER_INTEGRITY) {
12265                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
12266                                          outer_headers);
12267                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
12268                 is_l3_ip4 = (pattern_flags & MLX5_FLOW_LAYER_OUTER_L3_IPV4) !=
12269                             0;
12270                 set_integrity_bits(headers_m, headers_v,
12271                                    integrity_items[0], is_l3_ip4);
12272         }
12273 }
12274
12275 static void
12276 flow_dv_translate_item_integrity(const struct rte_flow_item *item,
12277                                  const struct rte_flow_item *integrity_items[2],
12278                                  uint64_t *last_item)
12279 {
12280         const struct rte_flow_item_integrity *spec = (typeof(spec))item->spec;
12281
12282         /* integrity bits validation cleared spec pointer */
12283         MLX5_ASSERT(spec != NULL);
12284         if (spec->level > 1) {
12285                 integrity_items[1] = item;
12286                 *last_item |= MLX5_FLOW_ITEM_INNER_INTEGRITY;
12287         } else {
12288                 integrity_items[0] = item;
12289                 *last_item |= MLX5_FLOW_ITEM_OUTER_INTEGRITY;
12290         }
12291 }
12292
12293 /**
12294  * Prepares DV flow counter with aging configuration.
12295  * Gets it by index when exists, creates a new one when doesn't.
12296  *
12297  * @param[in] dev
12298  *   Pointer to rte_eth_dev structure.
12299  * @param[in] dev_flow
12300  *   Pointer to the mlx5_flow.
12301  * @param[in, out] flow
12302  *   Pointer to the sub flow.
12303  * @param[in] count
12304  *   Pointer to the counter action configuration.
12305  * @param[in] age
12306  *   Pointer to the aging action configuration.
12307  * @param[out] error
12308  *   Pointer to the error structure.
12309  *
12310  * @return
12311  *   Pointer to the counter, NULL otherwise.
12312  */
12313 static struct mlx5_flow_counter *
12314 flow_dv_prepare_counter(struct rte_eth_dev *dev,
12315                         struct mlx5_flow *dev_flow,
12316                         struct rte_flow *flow,
12317                         const struct rte_flow_action_count *count,
12318                         const struct rte_flow_action_age *age,
12319                         struct rte_flow_error *error)
12320 {
12321         if (!flow->counter) {
12322                 flow->counter = flow_dv_translate_create_counter(dev, dev_flow,
12323                                                                  count, age);
12324                 if (!flow->counter) {
12325                         rte_flow_error_set(error, rte_errno,
12326                                            RTE_FLOW_ERROR_TYPE_ACTION, NULL,
12327                                            "cannot create counter object.");
12328                         return NULL;
12329                 }
12330         }
12331         return flow_dv_counter_get_by_idx(dev, flow->counter, NULL);
12332 }
12333
12334 /*
12335  * Release an ASO CT action by its own device.
12336  *
12337  * @param[in] dev
12338  *   Pointer to the Ethernet device structure.
12339  * @param[in] idx
12340  *   Index of ASO CT action to release.
12341  *
12342  * @return
12343  *   0 when CT action was removed, otherwise the number of references.
12344  */
12345 static inline int
12346 flow_dv_aso_ct_dev_release(struct rte_eth_dev *dev, uint32_t idx)
12347 {
12348         struct mlx5_priv *priv = dev->data->dev_private;
12349         struct mlx5_aso_ct_pools_mng *mng = priv->sh->ct_mng;
12350         uint32_t ret;
12351         struct mlx5_aso_ct_action *ct = flow_aso_ct_get_by_dev_idx(dev, idx);
12352         enum mlx5_aso_ct_state state =
12353                         __atomic_load_n(&ct->state, __ATOMIC_RELAXED);
12354
12355         /* Cannot release when CT is in the ASO SQ. */
12356         if (state == ASO_CONNTRACK_WAIT || state == ASO_CONNTRACK_QUERY)
12357                 return -1;
12358         ret = __atomic_sub_fetch(&ct->refcnt, 1, __ATOMIC_RELAXED);
12359         if (!ret) {
12360                 if (ct->dr_action_orig) {
12361 #ifdef HAVE_MLX5_DR_ACTION_ASO_CT
12362                         claim_zero(mlx5_glue->destroy_flow_action
12363                                         (ct->dr_action_orig));
12364 #endif
12365                         ct->dr_action_orig = NULL;
12366                 }
12367                 if (ct->dr_action_rply) {
12368 #ifdef HAVE_MLX5_DR_ACTION_ASO_CT
12369                         claim_zero(mlx5_glue->destroy_flow_action
12370                                         (ct->dr_action_rply));
12371 #endif
12372                         ct->dr_action_rply = NULL;
12373                 }
12374                 /* Clear the state to free, no need in 1st allocation. */
12375                 MLX5_ASO_CT_UPDATE_STATE(ct, ASO_CONNTRACK_FREE);
12376                 rte_spinlock_lock(&mng->ct_sl);
12377                 LIST_INSERT_HEAD(&mng->free_cts, ct, next);
12378                 rte_spinlock_unlock(&mng->ct_sl);
12379         }
12380         return (int)ret;
12381 }
12382
12383 static inline int
12384 flow_dv_aso_ct_release(struct rte_eth_dev *dev, uint32_t own_idx,
12385                        struct rte_flow_error *error)
12386 {
12387         uint16_t owner = (uint16_t)MLX5_INDIRECT_ACT_CT_GET_OWNER(own_idx);
12388         uint32_t idx = MLX5_INDIRECT_ACT_CT_GET_IDX(own_idx);
12389         struct rte_eth_dev *owndev = &rte_eth_devices[owner];
12390         int ret;
12391
12392         MLX5_ASSERT(owner < RTE_MAX_ETHPORTS);
12393         if (dev->data->dev_started != 1)
12394                 return rte_flow_error_set(error, EAGAIN,
12395                                           RTE_FLOW_ERROR_TYPE_ACTION,
12396                                           NULL,
12397                                           "Indirect CT action cannot be destroyed when the port is stopped");
12398         ret = flow_dv_aso_ct_dev_release(owndev, idx);
12399         if (ret < 0)
12400                 return rte_flow_error_set(error, EAGAIN,
12401                                           RTE_FLOW_ERROR_TYPE_ACTION,
12402                                           NULL,
12403                                           "Current state prevents indirect CT action from being destroyed");
12404         return ret;
12405 }
12406
12407 /*
12408  * Resize the ASO CT pools array by 64 pools.
12409  *
12410  * @param[in] dev
12411  *   Pointer to the Ethernet device structure.
12412  *
12413  * @return
12414  *   0 on success, otherwise negative errno value and rte_errno is set.
12415  */
12416 static int
12417 flow_dv_aso_ct_pools_resize(struct rte_eth_dev *dev)
12418 {
12419         struct mlx5_priv *priv = dev->data->dev_private;
12420         struct mlx5_aso_ct_pools_mng *mng = priv->sh->ct_mng;
12421         void *old_pools = mng->pools;
12422         /* Magic number now, need a macro. */
12423         uint32_t resize = mng->n + 64;
12424         uint32_t mem_size = sizeof(struct mlx5_aso_ct_pool *) * resize;
12425         void *pools = mlx5_malloc(MLX5_MEM_ZERO, mem_size, 0, SOCKET_ID_ANY);
12426
12427         if (!pools) {
12428                 rte_errno = ENOMEM;
12429                 return -rte_errno;
12430         }
12431         rte_rwlock_write_lock(&mng->resize_rwl);
12432         /* ASO SQ/QP was already initialized in the startup. */
12433         if (old_pools) {
12434                 /* Realloc could be an alternative choice. */
12435                 rte_memcpy(pools, old_pools,
12436                            mng->n * sizeof(struct mlx5_aso_ct_pool *));
12437                 mlx5_free(old_pools);
12438         }
12439         mng->n = resize;
12440         mng->pools = pools;
12441         rte_rwlock_write_unlock(&mng->resize_rwl);
12442         return 0;
12443 }
12444
12445 /*
12446  * Create and initialize a new ASO CT pool.
12447  *
12448  * @param[in] dev
12449  *   Pointer to the Ethernet device structure.
12450  * @param[out] ct_free
12451  *   Where to put the pointer of a new CT action.
12452  *
12453  * @return
12454  *   The CT actions pool pointer and @p ct_free is set on success,
12455  *   NULL otherwise and rte_errno is set.
12456  */
12457 static struct mlx5_aso_ct_pool *
12458 flow_dv_ct_pool_create(struct rte_eth_dev *dev,
12459                        struct mlx5_aso_ct_action **ct_free)
12460 {
12461         struct mlx5_priv *priv = dev->data->dev_private;
12462         struct mlx5_aso_ct_pools_mng *mng = priv->sh->ct_mng;
12463         struct mlx5_aso_ct_pool *pool = NULL;
12464         struct mlx5_devx_obj *obj = NULL;
12465         uint32_t i;
12466         uint32_t log_obj_size = rte_log2_u32(MLX5_ASO_CT_ACTIONS_PER_POOL);
12467
12468         obj = mlx5_devx_cmd_create_conn_track_offload_obj(priv->sh->cdev->ctx,
12469                                                           priv->sh->cdev->pdn,
12470                                                           log_obj_size);
12471         if (!obj) {
12472                 rte_errno = ENODATA;
12473                 DRV_LOG(ERR, "Failed to create conn_track_offload_obj using DevX.");
12474                 return NULL;
12475         }
12476         pool = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*pool), 0, SOCKET_ID_ANY);
12477         if (!pool) {
12478                 rte_errno = ENOMEM;
12479                 claim_zero(mlx5_devx_cmd_destroy(obj));
12480                 return NULL;
12481         }
12482         pool->devx_obj = obj;
12483         pool->index = mng->next;
12484         /* Resize pools array if there is no room for the new pool in it. */
12485         if (pool->index == mng->n && flow_dv_aso_ct_pools_resize(dev)) {
12486                 claim_zero(mlx5_devx_cmd_destroy(obj));
12487                 mlx5_free(pool);
12488                 return NULL;
12489         }
12490         mng->pools[pool->index] = pool;
12491         mng->next++;
12492         /* Assign the first action in the new pool, the rest go to free list. */
12493         *ct_free = &pool->actions[0];
12494         /* Lock outside, the list operation is safe here. */
12495         for (i = 1; i < MLX5_ASO_CT_ACTIONS_PER_POOL; i++) {
12496                 /* refcnt is 0 when allocating the memory. */
12497                 pool->actions[i].offset = i;
12498                 LIST_INSERT_HEAD(&mng->free_cts, &pool->actions[i], next);
12499         }
12500         return pool;
12501 }
12502
12503 /*
12504  * Allocate a ASO CT action from free list.
12505  *
12506  * @param[in] dev
12507  *   Pointer to the Ethernet device structure.
12508  * @param[out] error
12509  *   Pointer to the error structure.
12510  *
12511  * @return
12512  *   Index to ASO CT action on success, 0 otherwise and rte_errno is set.
12513  */
12514 static uint32_t
12515 flow_dv_aso_ct_alloc(struct rte_eth_dev *dev, struct rte_flow_error *error)
12516 {
12517         struct mlx5_priv *priv = dev->data->dev_private;
12518         struct mlx5_aso_ct_pools_mng *mng = priv->sh->ct_mng;
12519         struct mlx5_aso_ct_action *ct = NULL;
12520         struct mlx5_aso_ct_pool *pool;
12521         uint8_t reg_c;
12522         uint32_t ct_idx;
12523
12524         MLX5_ASSERT(mng);
12525         if (!priv->sh->devx) {
12526                 rte_errno = ENOTSUP;
12527                 return 0;
12528         }
12529         /* Get a free CT action, if no, a new pool will be created. */
12530         rte_spinlock_lock(&mng->ct_sl);
12531         ct = LIST_FIRST(&mng->free_cts);
12532         if (ct) {
12533                 LIST_REMOVE(ct, next);
12534         } else if (!flow_dv_ct_pool_create(dev, &ct)) {
12535                 rte_spinlock_unlock(&mng->ct_sl);
12536                 rte_flow_error_set(error, rte_errno, RTE_FLOW_ERROR_TYPE_ACTION,
12537                                    NULL, "failed to create ASO CT pool");
12538                 return 0;
12539         }
12540         rte_spinlock_unlock(&mng->ct_sl);
12541         pool = container_of(ct, struct mlx5_aso_ct_pool, actions[ct->offset]);
12542         ct_idx = MLX5_MAKE_CT_IDX(pool->index, ct->offset);
12543         /* 0: inactive, 1: created, 2+: used by flows. */
12544         __atomic_store_n(&ct->refcnt, 1, __ATOMIC_RELAXED);
12545         reg_c = mlx5_flow_get_reg_id(dev, MLX5_ASO_CONNTRACK, 0, error);
12546         if (!ct->dr_action_orig) {
12547 #ifdef HAVE_MLX5_DR_ACTION_ASO_CT
12548                 ct->dr_action_orig = mlx5_glue->dv_create_flow_action_aso
12549                         (priv->sh->rx_domain, pool->devx_obj->obj,
12550                          ct->offset,
12551                          MLX5DV_DR_ACTION_FLAGS_ASO_CT_DIRECTION_INITIATOR,
12552                          reg_c - REG_C_0);
12553 #else
12554                 RTE_SET_USED(reg_c);
12555 #endif
12556                 if (!ct->dr_action_orig) {
12557                         flow_dv_aso_ct_dev_release(dev, ct_idx);
12558                         rte_flow_error_set(error, rte_errno,
12559                                            RTE_FLOW_ERROR_TYPE_ACTION, NULL,
12560                                            "failed to create ASO CT action");
12561                         return 0;
12562                 }
12563         }
12564         if (!ct->dr_action_rply) {
12565 #ifdef HAVE_MLX5_DR_ACTION_ASO_CT
12566                 ct->dr_action_rply = mlx5_glue->dv_create_flow_action_aso
12567                         (priv->sh->rx_domain, pool->devx_obj->obj,
12568                          ct->offset,
12569                          MLX5DV_DR_ACTION_FLAGS_ASO_CT_DIRECTION_RESPONDER,
12570                          reg_c - REG_C_0);
12571 #endif
12572                 if (!ct->dr_action_rply) {
12573                         flow_dv_aso_ct_dev_release(dev, ct_idx);
12574                         rte_flow_error_set(error, rte_errno,
12575                                            RTE_FLOW_ERROR_TYPE_ACTION, NULL,
12576                                            "failed to create ASO CT action");
12577                         return 0;
12578                 }
12579         }
12580         return ct_idx;
12581 }
12582
12583 /*
12584  * Create a conntrack object with context and actions by using ASO mechanism.
12585  *
12586  * @param[in] dev
12587  *   Pointer to rte_eth_dev structure.
12588  * @param[in] pro
12589  *   Pointer to conntrack information profile.
12590  * @param[out] error
12591  *   Pointer to the error structure.
12592  *
12593  * @return
12594  *   Index to conntrack object on success, 0 otherwise.
12595  */
12596 static uint32_t
12597 flow_dv_translate_create_conntrack(struct rte_eth_dev *dev,
12598                                    const struct rte_flow_action_conntrack *pro,
12599                                    struct rte_flow_error *error)
12600 {
12601         struct mlx5_priv *priv = dev->data->dev_private;
12602         struct mlx5_dev_ctx_shared *sh = priv->sh;
12603         struct mlx5_aso_ct_action *ct;
12604         uint32_t idx;
12605
12606         if (!sh->ct_aso_en)
12607                 return rte_flow_error_set(error, ENOTSUP,
12608                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
12609                                           "Connection is not supported");
12610         idx = flow_dv_aso_ct_alloc(dev, error);
12611         if (!idx)
12612                 return rte_flow_error_set(error, rte_errno,
12613                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
12614                                           "Failed to allocate CT object");
12615         ct = flow_aso_ct_get_by_dev_idx(dev, idx);
12616         if (mlx5_aso_ct_update_by_wqe(sh, ct, pro))
12617                 return rte_flow_error_set(error, EBUSY,
12618                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
12619                                           "Failed to update CT");
12620         ct->is_original = !!pro->is_original_dir;
12621         ct->peer = pro->peer_port;
12622         return idx;
12623 }
12624
12625 /**
12626  * Fill the flow with DV spec, lock free
12627  * (mutex should be acquired by caller).
12628  *
12629  * @param[in] dev
12630  *   Pointer to rte_eth_dev structure.
12631  * @param[in, out] dev_flow
12632  *   Pointer to the sub flow.
12633  * @param[in] attr
12634  *   Pointer to the flow attributes.
12635  * @param[in] items
12636  *   Pointer to the list of items.
12637  * @param[in] actions
12638  *   Pointer to the list of actions.
12639  * @param[out] error
12640  *   Pointer to the error structure.
12641  *
12642  * @return
12643  *   0 on success, a negative errno value otherwise and rte_errno is set.
12644  */
12645 static int
12646 flow_dv_translate(struct rte_eth_dev *dev,
12647                   struct mlx5_flow *dev_flow,
12648                   const struct rte_flow_attr *attr,
12649                   const struct rte_flow_item items[],
12650                   const struct rte_flow_action actions[],
12651                   struct rte_flow_error *error)
12652 {
12653         struct mlx5_priv *priv = dev->data->dev_private;
12654         struct mlx5_dev_config *dev_conf = &priv->config;
12655         struct rte_flow *flow = dev_flow->flow;
12656         struct mlx5_flow_handle *handle = dev_flow->handle;
12657         struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
12658         struct mlx5_flow_rss_desc *rss_desc;
12659         uint64_t item_flags = 0;
12660         uint64_t last_item = 0;
12661         uint64_t action_flags = 0;
12662         struct mlx5_flow_dv_matcher matcher = {
12663                 .mask = {
12664                         .size = sizeof(matcher.mask.buf),
12665                 },
12666         };
12667         int actions_n = 0;
12668         bool actions_end = false;
12669         union {
12670                 struct mlx5_flow_dv_modify_hdr_resource res;
12671                 uint8_t len[sizeof(struct mlx5_flow_dv_modify_hdr_resource) +
12672                             sizeof(struct mlx5_modification_cmd) *
12673                             (MLX5_MAX_MODIFY_NUM + 1)];
12674         } mhdr_dummy;
12675         struct mlx5_flow_dv_modify_hdr_resource *mhdr_res = &mhdr_dummy.res;
12676         const struct rte_flow_action_count *count = NULL;
12677         const struct rte_flow_action_age *non_shared_age = NULL;
12678         union flow_dv_attr flow_attr = { .attr = 0 };
12679         uint32_t tag_be;
12680         union mlx5_flow_tbl_key tbl_key;
12681         uint32_t modify_action_position = UINT32_MAX;
12682         void *match_mask = matcher.mask.buf;
12683         void *match_value = dev_flow->dv.value.buf;
12684         uint8_t next_protocol = 0xff;
12685         struct rte_vlan_hdr vlan = { 0 };
12686         struct mlx5_flow_dv_dest_array_resource mdest_res;
12687         struct mlx5_flow_dv_sample_resource sample_res;
12688         void *sample_actions[MLX5_DV_MAX_NUMBER_OF_ACTIONS] = {0};
12689         const struct rte_flow_action_sample *sample = NULL;
12690         struct mlx5_flow_sub_actions_list *sample_act;
12691         uint32_t sample_act_pos = UINT32_MAX;
12692         uint32_t age_act_pos = UINT32_MAX;
12693         uint32_t num_of_dest = 0;
12694         int tmp_actions_n = 0;
12695         uint32_t table;
12696         int ret = 0;
12697         const struct mlx5_flow_tunnel *tunnel = NULL;
12698         struct flow_grp_info grp_info = {
12699                 .external = !!dev_flow->external,
12700                 .transfer = !!attr->transfer,
12701                 .fdb_def_rule = !!priv->fdb_def_rule,
12702                 .skip_scale = dev_flow->skip_scale &
12703                         (1 << MLX5_SCALE_FLOW_GROUP_BIT),
12704                 .std_tbl_fix = true,
12705         };
12706         const struct rte_flow_item *integrity_items[2] = {NULL, NULL};
12707         const struct rte_flow_item *tunnel_item = NULL;
12708
12709         if (!wks)
12710                 return rte_flow_error_set(error, ENOMEM,
12711                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
12712                                           NULL,
12713                                           "failed to push flow workspace");
12714         rss_desc = &wks->rss_desc;
12715         memset(&mdest_res, 0, sizeof(struct mlx5_flow_dv_dest_array_resource));
12716         memset(&sample_res, 0, sizeof(struct mlx5_flow_dv_sample_resource));
12717         mhdr_res->ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
12718                                            MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
12719         /* update normal path action resource into last index of array */
12720         sample_act = &mdest_res.sample_act[MLX5_MAX_DEST_NUM - 1];
12721         if (is_tunnel_offload_active(dev)) {
12722                 if (dev_flow->tunnel) {
12723                         RTE_VERIFY(dev_flow->tof_type ==
12724                                    MLX5_TUNNEL_OFFLOAD_MISS_RULE);
12725                         tunnel = dev_flow->tunnel;
12726                 } else {
12727                         tunnel = mlx5_get_tof(items, actions,
12728                                               &dev_flow->tof_type);
12729                         dev_flow->tunnel = tunnel;
12730                 }
12731                 grp_info.std_tbl_fix = tunnel_use_standard_attr_group_translate
12732                                         (dev, attr, tunnel, dev_flow->tof_type);
12733         }
12734         mhdr_res->ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
12735                                            MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
12736         ret = mlx5_flow_group_to_table(dev, tunnel, attr->group, &table,
12737                                        &grp_info, error);
12738         if (ret)
12739                 return ret;
12740         dev_flow->dv.group = table;
12741         if (attr->transfer)
12742                 mhdr_res->ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
12743         /* number of actions must be set to 0 in case of dirty stack. */
12744         mhdr_res->actions_num = 0;
12745         if (is_flow_tunnel_match_rule(dev_flow->tof_type)) {
12746                 /*
12747                  * do not add decap action if match rule drops packet
12748                  * HW rejects rules with decap & drop
12749                  *
12750                  * if tunnel match rule was inserted before matching tunnel set
12751                  * rule flow table used in the match rule must be registered.
12752                  * current implementation handles that in the
12753                  * flow_dv_match_register() at the function end.
12754                  */
12755                 bool add_decap = true;
12756                 const struct rte_flow_action *ptr = actions;
12757
12758                 for (; ptr->type != RTE_FLOW_ACTION_TYPE_END; ptr++) {
12759                         if (ptr->type == RTE_FLOW_ACTION_TYPE_DROP) {
12760                                 add_decap = false;
12761                                 break;
12762                         }
12763                 }
12764                 if (add_decap) {
12765                         if (flow_dv_create_action_l2_decap(dev, dev_flow,
12766                                                            attr->transfer,
12767                                                            error))
12768                                 return -rte_errno;
12769                         dev_flow->dv.actions[actions_n++] =
12770                                         dev_flow->dv.encap_decap->action;
12771                         action_flags |= MLX5_FLOW_ACTION_DECAP;
12772                 }
12773         }
12774         for (; !actions_end ; actions++) {
12775                 const struct rte_flow_action_queue *queue;
12776                 const struct rte_flow_action_rss *rss;
12777                 const struct rte_flow_action *action = actions;
12778                 const uint8_t *rss_key;
12779                 struct mlx5_flow_tbl_resource *tbl;
12780                 struct mlx5_aso_age_action *age_act;
12781                 struct mlx5_flow_counter *cnt_act;
12782                 uint32_t port_id = 0;
12783                 struct mlx5_flow_dv_port_id_action_resource port_id_resource;
12784                 int action_type = actions->type;
12785                 const struct rte_flow_action *found_action = NULL;
12786                 uint32_t jump_group = 0;
12787                 uint32_t owner_idx;
12788                 struct mlx5_aso_ct_action *ct;
12789
12790                 if (!mlx5_flow_os_action_supported(action_type))
12791                         return rte_flow_error_set(error, ENOTSUP,
12792                                                   RTE_FLOW_ERROR_TYPE_ACTION,
12793                                                   actions,
12794                                                   "action not supported");
12795                 switch (action_type) {
12796                 case MLX5_RTE_FLOW_ACTION_TYPE_TUNNEL_SET:
12797                         action_flags |= MLX5_FLOW_ACTION_TUNNEL_SET;
12798                         break;
12799                 case RTE_FLOW_ACTION_TYPE_VOID:
12800                         break;
12801                 case RTE_FLOW_ACTION_TYPE_PORT_ID:
12802                 case RTE_FLOW_ACTION_TYPE_REPRESENTED_PORT:
12803                         if (flow_dv_translate_action_port_id(dev, action,
12804                                                              &port_id, error))
12805                                 return -rte_errno;
12806                         port_id_resource.port_id = port_id;
12807                         MLX5_ASSERT(!handle->rix_port_id_action);
12808                         if (flow_dv_port_id_action_resource_register
12809                             (dev, &port_id_resource, dev_flow, error))
12810                                 return -rte_errno;
12811                         dev_flow->dv.actions[actions_n++] =
12812                                         dev_flow->dv.port_id_action->action;
12813                         action_flags |= MLX5_FLOW_ACTION_PORT_ID;
12814                         dev_flow->handle->fate_action = MLX5_FLOW_FATE_PORT_ID;
12815                         sample_act->action_flags |= MLX5_FLOW_ACTION_PORT_ID;
12816                         num_of_dest++;
12817                         break;
12818                 case RTE_FLOW_ACTION_TYPE_FLAG:
12819                         action_flags |= MLX5_FLOW_ACTION_FLAG;
12820                         dev_flow->handle->mark = 1;
12821                         if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
12822                                 struct rte_flow_action_mark mark = {
12823                                         .id = MLX5_FLOW_MARK_DEFAULT,
12824                                 };
12825
12826                                 if (flow_dv_convert_action_mark(dev, &mark,
12827                                                                 mhdr_res,
12828                                                                 error))
12829                                         return -rte_errno;
12830                                 action_flags |= MLX5_FLOW_ACTION_MARK_EXT;
12831                                 break;
12832                         }
12833                         tag_be = mlx5_flow_mark_set(MLX5_FLOW_MARK_DEFAULT);
12834                         /*
12835                          * Only one FLAG or MARK is supported per device flow
12836                          * right now. So the pointer to the tag resource must be
12837                          * zero before the register process.
12838                          */
12839                         MLX5_ASSERT(!handle->dvh.rix_tag);
12840                         if (flow_dv_tag_resource_register(dev, tag_be,
12841                                                           dev_flow, error))
12842                                 return -rte_errno;
12843                         MLX5_ASSERT(dev_flow->dv.tag_resource);
12844                         dev_flow->dv.actions[actions_n++] =
12845                                         dev_flow->dv.tag_resource->action;
12846                         break;
12847                 case RTE_FLOW_ACTION_TYPE_MARK:
12848                         action_flags |= MLX5_FLOW_ACTION_MARK;
12849                         dev_flow->handle->mark = 1;
12850                         if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
12851                                 const struct rte_flow_action_mark *mark =
12852                                         (const struct rte_flow_action_mark *)
12853                                                 actions->conf;
12854
12855                                 if (flow_dv_convert_action_mark(dev, mark,
12856                                                                 mhdr_res,
12857                                                                 error))
12858                                         return -rte_errno;
12859                                 action_flags |= MLX5_FLOW_ACTION_MARK_EXT;
12860                                 break;
12861                         }
12862                         /* Fall-through */
12863                 case MLX5_RTE_FLOW_ACTION_TYPE_MARK:
12864                         /* Legacy (non-extensive) MARK action. */
12865                         tag_be = mlx5_flow_mark_set
12866                               (((const struct rte_flow_action_mark *)
12867                                (actions->conf))->id);
12868                         MLX5_ASSERT(!handle->dvh.rix_tag);
12869                         if (flow_dv_tag_resource_register(dev, tag_be,
12870                                                           dev_flow, error))
12871                                 return -rte_errno;
12872                         MLX5_ASSERT(dev_flow->dv.tag_resource);
12873                         dev_flow->dv.actions[actions_n++] =
12874                                         dev_flow->dv.tag_resource->action;
12875                         break;
12876                 case RTE_FLOW_ACTION_TYPE_SET_META:
12877                         if (flow_dv_convert_action_set_meta
12878                                 (dev, mhdr_res, attr,
12879                                  (const struct rte_flow_action_set_meta *)
12880                                   actions->conf, error))
12881                                 return -rte_errno;
12882                         action_flags |= MLX5_FLOW_ACTION_SET_META;
12883                         break;
12884                 case RTE_FLOW_ACTION_TYPE_SET_TAG:
12885                         if (flow_dv_convert_action_set_tag
12886                                 (dev, mhdr_res,
12887                                  (const struct rte_flow_action_set_tag *)
12888                                   actions->conf, error))
12889                                 return -rte_errno;
12890                         action_flags |= MLX5_FLOW_ACTION_SET_TAG;
12891                         break;
12892                 case RTE_FLOW_ACTION_TYPE_DROP:
12893                         action_flags |= MLX5_FLOW_ACTION_DROP;
12894                         dev_flow->handle->fate_action = MLX5_FLOW_FATE_DROP;
12895                         break;
12896                 case RTE_FLOW_ACTION_TYPE_QUEUE:
12897                         queue = actions->conf;
12898                         rss_desc->queue_num = 1;
12899                         rss_desc->queue[0] = queue->index;
12900                         action_flags |= MLX5_FLOW_ACTION_QUEUE;
12901                         dev_flow->handle->fate_action = MLX5_FLOW_FATE_QUEUE;
12902                         sample_act->action_flags |= MLX5_FLOW_ACTION_QUEUE;
12903                         num_of_dest++;
12904                         break;
12905                 case RTE_FLOW_ACTION_TYPE_RSS:
12906                         rss = actions->conf;
12907                         memcpy(rss_desc->queue, rss->queue,
12908                                rss->queue_num * sizeof(uint16_t));
12909                         rss_desc->queue_num = rss->queue_num;
12910                         /* NULL RSS key indicates default RSS key. */
12911                         rss_key = !rss->key ? rss_hash_default_key : rss->key;
12912                         memcpy(rss_desc->key, rss_key, MLX5_RSS_HASH_KEY_LEN);
12913                         /*
12914                          * rss->level and rss.types should be set in advance
12915                          * when expanding items for RSS.
12916                          */
12917                         action_flags |= MLX5_FLOW_ACTION_RSS;
12918                         dev_flow->handle->fate_action = rss_desc->shared_rss ?
12919                                 MLX5_FLOW_FATE_SHARED_RSS :
12920                                 MLX5_FLOW_FATE_QUEUE;
12921                         break;
12922                 case MLX5_RTE_FLOW_ACTION_TYPE_AGE:
12923                         owner_idx = (uint32_t)(uintptr_t)action->conf;
12924                         age_act = flow_aso_age_get_by_idx(dev, owner_idx);
12925                         if (flow->age == 0) {
12926                                 flow->age = owner_idx;
12927                                 __atomic_fetch_add(&age_act->refcnt, 1,
12928                                                    __ATOMIC_RELAXED);
12929                         }
12930                         age_act_pos = actions_n++;
12931                         action_flags |= MLX5_FLOW_ACTION_AGE;
12932                         break;
12933                 case RTE_FLOW_ACTION_TYPE_AGE:
12934                         non_shared_age = action->conf;
12935                         age_act_pos = actions_n++;
12936                         action_flags |= MLX5_FLOW_ACTION_AGE;
12937                         break;
12938                 case MLX5_RTE_FLOW_ACTION_TYPE_COUNT:
12939                         owner_idx = (uint32_t)(uintptr_t)action->conf;
12940                         cnt_act = flow_dv_counter_get_by_idx(dev, owner_idx,
12941                                                              NULL);
12942                         MLX5_ASSERT(cnt_act != NULL);
12943                         /**
12944                          * When creating meter drop flow in drop table, the
12945                          * counter should not overwrite the rte flow counter.
12946                          */
12947                         if (attr->group == MLX5_FLOW_TABLE_LEVEL_METER &&
12948                             dev_flow->dv.table_id == MLX5_MTR_TABLE_ID_DROP) {
12949                                 dev_flow->dv.actions[actions_n++] =
12950                                                         cnt_act->action;
12951                         } else {
12952                                 if (flow->counter == 0) {
12953                                         flow->counter = owner_idx;
12954                                         __atomic_fetch_add
12955                                                 (&cnt_act->shared_info.refcnt,
12956                                                  1, __ATOMIC_RELAXED);
12957                                 }
12958                                 /* Save information first, will apply later. */
12959                                 action_flags |= MLX5_FLOW_ACTION_COUNT;
12960                         }
12961                         break;
12962                 case RTE_FLOW_ACTION_TYPE_COUNT:
12963                         if (!priv->sh->devx) {
12964                                 return rte_flow_error_set
12965                                               (error, ENOTSUP,
12966                                                RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
12967                                                NULL,
12968                                                "count action not supported");
12969                         }
12970                         /* Save information first, will apply later. */
12971                         count = action->conf;
12972                         action_flags |= MLX5_FLOW_ACTION_COUNT;
12973                         break;
12974                 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
12975                         dev_flow->dv.actions[actions_n++] =
12976                                                 priv->sh->pop_vlan_action;
12977                         action_flags |= MLX5_FLOW_ACTION_OF_POP_VLAN;
12978                         break;
12979                 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
12980                         if (!(action_flags &
12981                               MLX5_FLOW_ACTION_OF_SET_VLAN_VID))
12982                                 flow_dev_get_vlan_info_from_items(items, &vlan);
12983                         vlan.eth_proto = rte_be_to_cpu_16
12984                              ((((const struct rte_flow_action_of_push_vlan *)
12985                                                    actions->conf)->ethertype));
12986                         found_action = mlx5_flow_find_action
12987                                         (actions + 1,
12988                                          RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID);
12989                         if (found_action)
12990                                 mlx5_update_vlan_vid_pcp(found_action, &vlan);
12991                         found_action = mlx5_flow_find_action
12992                                         (actions + 1,
12993                                          RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP);
12994                         if (found_action)
12995                                 mlx5_update_vlan_vid_pcp(found_action, &vlan);
12996                         if (flow_dv_create_action_push_vlan
12997                                             (dev, attr, &vlan, dev_flow, error))
12998                                 return -rte_errno;
12999                         dev_flow->dv.actions[actions_n++] =
13000                                         dev_flow->dv.push_vlan_res->action;
13001                         action_flags |= MLX5_FLOW_ACTION_OF_PUSH_VLAN;
13002                         break;
13003                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
13004                         /* of_vlan_push action handled this action */
13005                         MLX5_ASSERT(action_flags &
13006                                     MLX5_FLOW_ACTION_OF_PUSH_VLAN);
13007                         break;
13008                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
13009                         if (action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN)
13010                                 break;
13011                         flow_dev_get_vlan_info_from_items(items, &vlan);
13012                         mlx5_update_vlan_vid_pcp(actions, &vlan);
13013                         /* If no VLAN push - this is a modify header action */
13014                         if (flow_dv_convert_action_modify_vlan_vid
13015                                                 (mhdr_res, actions, error))
13016                                 return -rte_errno;
13017                         action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_VID;
13018                         break;
13019                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
13020                 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
13021                         if (flow_dv_create_action_l2_encap(dev, actions,
13022                                                            dev_flow,
13023                                                            attr->transfer,
13024                                                            error))
13025                                 return -rte_errno;
13026                         dev_flow->dv.actions[actions_n++] =
13027                                         dev_flow->dv.encap_decap->action;
13028                         action_flags |= MLX5_FLOW_ACTION_ENCAP;
13029                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
13030                                 sample_act->action_flags |=
13031                                                         MLX5_FLOW_ACTION_ENCAP;
13032                         break;
13033                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
13034                 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
13035                         if (flow_dv_create_action_l2_decap(dev, dev_flow,
13036                                                            attr->transfer,
13037                                                            error))
13038                                 return -rte_errno;
13039                         dev_flow->dv.actions[actions_n++] =
13040                                         dev_flow->dv.encap_decap->action;
13041                         action_flags |= MLX5_FLOW_ACTION_DECAP;
13042                         break;
13043                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
13044                         /* Handle encap with preceding decap. */
13045                         if (action_flags & MLX5_FLOW_ACTION_DECAP) {
13046                                 if (flow_dv_create_action_raw_encap
13047                                         (dev, actions, dev_flow, attr, error))
13048                                         return -rte_errno;
13049                                 dev_flow->dv.actions[actions_n++] =
13050                                         dev_flow->dv.encap_decap->action;
13051                         } else {
13052                                 /* Handle encap without preceding decap. */
13053                                 if (flow_dv_create_action_l2_encap
13054                                     (dev, actions, dev_flow, attr->transfer,
13055                                      error))
13056                                         return -rte_errno;
13057                                 dev_flow->dv.actions[actions_n++] =
13058                                         dev_flow->dv.encap_decap->action;
13059                         }
13060                         action_flags |= MLX5_FLOW_ACTION_ENCAP;
13061                         if (action_flags & MLX5_FLOW_ACTION_SAMPLE)
13062                                 sample_act->action_flags |=
13063                                                         MLX5_FLOW_ACTION_ENCAP;
13064                         break;
13065                 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
13066                         while ((++action)->type == RTE_FLOW_ACTION_TYPE_VOID)
13067                                 ;
13068                         if (action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
13069                                 if (flow_dv_create_action_l2_decap
13070                                     (dev, dev_flow, attr->transfer, error))
13071                                         return -rte_errno;
13072                                 dev_flow->dv.actions[actions_n++] =
13073                                         dev_flow->dv.encap_decap->action;
13074                         }
13075                         /* If decap is followed by encap, handle it at encap. */
13076                         action_flags |= MLX5_FLOW_ACTION_DECAP;
13077                         break;
13078                 case MLX5_RTE_FLOW_ACTION_TYPE_JUMP:
13079                         dev_flow->dv.actions[actions_n++] =
13080                                 (void *)(uintptr_t)action->conf;
13081                         action_flags |= MLX5_FLOW_ACTION_JUMP;
13082                         break;
13083                 case RTE_FLOW_ACTION_TYPE_JUMP:
13084                         jump_group = ((const struct rte_flow_action_jump *)
13085                                                         action->conf)->group;
13086                         grp_info.std_tbl_fix = 0;
13087                         if (dev_flow->skip_scale &
13088                                 (1 << MLX5_SCALE_JUMP_FLOW_GROUP_BIT))
13089                                 grp_info.skip_scale = 1;
13090                         else
13091                                 grp_info.skip_scale = 0;
13092                         ret = mlx5_flow_group_to_table(dev, tunnel,
13093                                                        jump_group,
13094                                                        &table,
13095                                                        &grp_info, error);
13096                         if (ret)
13097                                 return ret;
13098                         tbl = flow_dv_tbl_resource_get(dev, table, attr->egress,
13099                                                        attr->transfer,
13100                                                        !!dev_flow->external,
13101                                                        tunnel, jump_group, 0,
13102                                                        0, error);
13103                         if (!tbl)
13104                                 return rte_flow_error_set
13105                                                 (error, errno,
13106                                                  RTE_FLOW_ERROR_TYPE_ACTION,
13107                                                  NULL,
13108                                                  "cannot create jump action.");
13109                         if (flow_dv_jump_tbl_resource_register
13110                             (dev, tbl, dev_flow, error)) {
13111                                 flow_dv_tbl_resource_release(MLX5_SH(dev), tbl);
13112                                 return rte_flow_error_set
13113                                                 (error, errno,
13114                                                  RTE_FLOW_ERROR_TYPE_ACTION,
13115                                                  NULL,
13116                                                  "cannot create jump action.");
13117                         }
13118                         dev_flow->dv.actions[actions_n++] =
13119                                         dev_flow->dv.jump->action;
13120                         action_flags |= MLX5_FLOW_ACTION_JUMP;
13121                         dev_flow->handle->fate_action = MLX5_FLOW_FATE_JUMP;
13122                         sample_act->action_flags |= MLX5_FLOW_ACTION_JUMP;
13123                         num_of_dest++;
13124                         break;
13125                 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
13126                 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
13127                         if (flow_dv_convert_action_modify_mac
13128                                         (mhdr_res, actions, error))
13129                                 return -rte_errno;
13130                         action_flags |= actions->type ==
13131                                         RTE_FLOW_ACTION_TYPE_SET_MAC_SRC ?
13132                                         MLX5_FLOW_ACTION_SET_MAC_SRC :
13133                                         MLX5_FLOW_ACTION_SET_MAC_DST;
13134                         break;
13135                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
13136                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
13137                         if (flow_dv_convert_action_modify_ipv4
13138                                         (mhdr_res, actions, error))
13139                                 return -rte_errno;
13140                         action_flags |= actions->type ==
13141                                         RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC ?
13142                                         MLX5_FLOW_ACTION_SET_IPV4_SRC :
13143                                         MLX5_FLOW_ACTION_SET_IPV4_DST;
13144                         break;
13145                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
13146                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
13147                         if (flow_dv_convert_action_modify_ipv6
13148                                         (mhdr_res, actions, error))
13149                                 return -rte_errno;
13150                         action_flags |= actions->type ==
13151                                         RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC ?
13152                                         MLX5_FLOW_ACTION_SET_IPV6_SRC :
13153                                         MLX5_FLOW_ACTION_SET_IPV6_DST;
13154                         break;
13155                 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
13156                 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
13157                         if (flow_dv_convert_action_modify_tp
13158                                         (mhdr_res, actions, items,
13159                                          &flow_attr, dev_flow, !!(action_flags &
13160                                          MLX5_FLOW_ACTION_DECAP), error))
13161                                 return -rte_errno;
13162                         action_flags |= actions->type ==
13163                                         RTE_FLOW_ACTION_TYPE_SET_TP_SRC ?
13164                                         MLX5_FLOW_ACTION_SET_TP_SRC :
13165                                         MLX5_FLOW_ACTION_SET_TP_DST;
13166                         break;
13167                 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
13168                         if (flow_dv_convert_action_modify_dec_ttl
13169                                         (mhdr_res, items, &flow_attr, dev_flow,
13170                                          !!(action_flags &
13171                                          MLX5_FLOW_ACTION_DECAP), error))
13172                                 return -rte_errno;
13173                         action_flags |= MLX5_FLOW_ACTION_DEC_TTL;
13174                         break;
13175                 case RTE_FLOW_ACTION_TYPE_SET_TTL:
13176                         if (flow_dv_convert_action_modify_ttl
13177                                         (mhdr_res, actions, items, &flow_attr,
13178                                          dev_flow, !!(action_flags &
13179                                          MLX5_FLOW_ACTION_DECAP), error))
13180                                 return -rte_errno;
13181                         action_flags |= MLX5_FLOW_ACTION_SET_TTL;
13182                         break;
13183                 case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
13184                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
13185                         if (flow_dv_convert_action_modify_tcp_seq
13186                                         (mhdr_res, actions, error))
13187                                 return -rte_errno;
13188                         action_flags |= actions->type ==
13189                                         RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ ?
13190                                         MLX5_FLOW_ACTION_INC_TCP_SEQ :
13191                                         MLX5_FLOW_ACTION_DEC_TCP_SEQ;
13192                         break;
13193
13194                 case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
13195                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
13196                         if (flow_dv_convert_action_modify_tcp_ack
13197                                         (mhdr_res, actions, error))
13198                                 return -rte_errno;
13199                         action_flags |= actions->type ==
13200                                         RTE_FLOW_ACTION_TYPE_INC_TCP_ACK ?
13201                                         MLX5_FLOW_ACTION_INC_TCP_ACK :
13202                                         MLX5_FLOW_ACTION_DEC_TCP_ACK;
13203                         break;
13204                 case MLX5_RTE_FLOW_ACTION_TYPE_TAG:
13205                         if (flow_dv_convert_action_set_reg
13206                                         (mhdr_res, actions, error))
13207                                 return -rte_errno;
13208                         action_flags |= MLX5_FLOW_ACTION_SET_TAG;
13209                         break;
13210                 case MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG:
13211                         if (flow_dv_convert_action_copy_mreg
13212                                         (dev, mhdr_res, actions, error))
13213                                 return -rte_errno;
13214                         action_flags |= MLX5_FLOW_ACTION_SET_TAG;
13215                         break;
13216                 case MLX5_RTE_FLOW_ACTION_TYPE_DEFAULT_MISS:
13217                         action_flags |= MLX5_FLOW_ACTION_DEFAULT_MISS;
13218                         dev_flow->handle->fate_action =
13219                                         MLX5_FLOW_FATE_DEFAULT_MISS;
13220                         break;
13221                 case RTE_FLOW_ACTION_TYPE_METER:
13222                         if (!wks->fm)
13223                                 return rte_flow_error_set(error, rte_errno,
13224                                         RTE_FLOW_ERROR_TYPE_ACTION,
13225                                         NULL, "Failed to get meter in flow.");
13226                         /* Set the meter action. */
13227                         dev_flow->dv.actions[actions_n++] =
13228                                 wks->fm->meter_action;
13229                         action_flags |= MLX5_FLOW_ACTION_METER;
13230                         break;
13231                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP:
13232                         if (flow_dv_convert_action_modify_ipv4_dscp(mhdr_res,
13233                                                               actions, error))
13234                                 return -rte_errno;
13235                         action_flags |= MLX5_FLOW_ACTION_SET_IPV4_DSCP;
13236                         break;
13237                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP:
13238                         if (flow_dv_convert_action_modify_ipv6_dscp(mhdr_res,
13239                                                               actions, error))
13240                                 return -rte_errno;
13241                         action_flags |= MLX5_FLOW_ACTION_SET_IPV6_DSCP;
13242                         break;
13243                 case RTE_FLOW_ACTION_TYPE_SAMPLE:
13244                         sample_act_pos = actions_n;
13245                         sample = (const struct rte_flow_action_sample *)
13246                                  action->conf;
13247                         actions_n++;
13248                         action_flags |= MLX5_FLOW_ACTION_SAMPLE;
13249                         /* put encap action into group if work with port id */
13250                         if ((action_flags & MLX5_FLOW_ACTION_ENCAP) &&
13251                             (action_flags & MLX5_FLOW_ACTION_PORT_ID))
13252                                 sample_act->action_flags |=
13253                                                         MLX5_FLOW_ACTION_ENCAP;
13254                         break;
13255                 case RTE_FLOW_ACTION_TYPE_MODIFY_FIELD:
13256                         if (flow_dv_convert_action_modify_field
13257                                         (dev, mhdr_res, actions, attr, error))
13258                                 return -rte_errno;
13259                         action_flags |= MLX5_FLOW_ACTION_MODIFY_FIELD;
13260                         break;
13261                 case RTE_FLOW_ACTION_TYPE_CONNTRACK:
13262                         owner_idx = (uint32_t)(uintptr_t)action->conf;
13263                         ct = flow_aso_ct_get_by_idx(dev, owner_idx);
13264                         if (!ct)
13265                                 return rte_flow_error_set(error, EINVAL,
13266                                                 RTE_FLOW_ERROR_TYPE_ACTION,
13267                                                 NULL,
13268                                                 "Failed to get CT object.");
13269                         if (mlx5_aso_ct_available(priv->sh, ct))
13270                                 return rte_flow_error_set(error, rte_errno,
13271                                                 RTE_FLOW_ERROR_TYPE_ACTION,
13272                                                 NULL,
13273                                                 "CT is unavailable.");
13274                         if (ct->is_original)
13275                                 dev_flow->dv.actions[actions_n] =
13276                                                         ct->dr_action_orig;
13277                         else
13278                                 dev_flow->dv.actions[actions_n] =
13279                                                         ct->dr_action_rply;
13280                         if (flow->ct == 0) {
13281                                 flow->indirect_type =
13282                                                 MLX5_INDIRECT_ACTION_TYPE_CT;
13283                                 flow->ct = owner_idx;
13284                                 __atomic_fetch_add(&ct->refcnt, 1,
13285                                                    __ATOMIC_RELAXED);
13286                         }
13287                         actions_n++;
13288                         action_flags |= MLX5_FLOW_ACTION_CT;
13289                         break;
13290                 case RTE_FLOW_ACTION_TYPE_END:
13291                         actions_end = true;
13292                         if (mhdr_res->actions_num) {
13293                                 /* create modify action if needed. */
13294                                 if (flow_dv_modify_hdr_resource_register
13295                                         (dev, mhdr_res, dev_flow, error))
13296                                         return -rte_errno;
13297                                 dev_flow->dv.actions[modify_action_position] =
13298                                         handle->dvh.modify_hdr->action;
13299                         }
13300                         /*
13301                          * Handle AGE and COUNT action by single HW counter
13302                          * when they are not shared.
13303                          */
13304                         if (action_flags & MLX5_FLOW_ACTION_AGE) {
13305                                 if ((non_shared_age && count) ||
13306                                     !(priv->sh->flow_hit_aso_en &&
13307                                       (attr->group || attr->transfer))) {
13308                                         /* Creates age by counters. */
13309                                         cnt_act = flow_dv_prepare_counter
13310                                                                 (dev, dev_flow,
13311                                                                  flow, count,
13312                                                                  non_shared_age,
13313                                                                  error);
13314                                         if (!cnt_act)
13315                                                 return -rte_errno;
13316                                         dev_flow->dv.actions[age_act_pos] =
13317                                                                 cnt_act->action;
13318                                         break;
13319                                 }
13320                                 if (!flow->age && non_shared_age) {
13321                                         flow->age = flow_dv_aso_age_alloc
13322                                                                 (dev, error);
13323                                         if (!flow->age)
13324                                                 return -rte_errno;
13325                                         flow_dv_aso_age_params_init
13326                                                     (dev, flow->age,
13327                                                      non_shared_age->context ?
13328                                                      non_shared_age->context :
13329                                                      (void *)(uintptr_t)
13330                                                      (dev_flow->flow_idx),
13331                                                      non_shared_age->timeout);
13332                                 }
13333                                 age_act = flow_aso_age_get_by_idx(dev,
13334                                                                   flow->age);
13335                                 dev_flow->dv.actions[age_act_pos] =
13336                                                              age_act->dr_action;
13337                         }
13338                         if (action_flags & MLX5_FLOW_ACTION_COUNT) {
13339                                 /*
13340                                  * Create one count action, to be used
13341                                  * by all sub-flows.
13342                                  */
13343                                 cnt_act = flow_dv_prepare_counter(dev, dev_flow,
13344                                                                   flow, count,
13345                                                                   NULL, error);
13346                                 if (!cnt_act)
13347                                         return -rte_errno;
13348                                 dev_flow->dv.actions[actions_n++] =
13349                                                                 cnt_act->action;
13350                         }
13351                 default:
13352                         break;
13353                 }
13354                 if (mhdr_res->actions_num &&
13355                     modify_action_position == UINT32_MAX)
13356                         modify_action_position = actions_n++;
13357         }
13358         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
13359                 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
13360                 int item_type = items->type;
13361
13362                 if (!mlx5_flow_os_item_supported(item_type))
13363                         return rte_flow_error_set(error, ENOTSUP,
13364                                                   RTE_FLOW_ERROR_TYPE_ITEM,
13365                                                   NULL, "item not supported");
13366                 switch (item_type) {
13367                 case RTE_FLOW_ITEM_TYPE_PORT_ID:
13368                         flow_dv_translate_item_port_id
13369                                 (dev, match_mask, match_value, items, attr);
13370                         last_item = MLX5_FLOW_ITEM_PORT_ID;
13371                         break;
13372                 case RTE_FLOW_ITEM_TYPE_ETH:
13373                         flow_dv_translate_item_eth(match_mask, match_value,
13374                                                    items, tunnel,
13375                                                    dev_flow->dv.group);
13376                         matcher.priority = action_flags &
13377                                         MLX5_FLOW_ACTION_DEFAULT_MISS &&
13378                                         !dev_flow->external ?
13379                                         MLX5_PRIORITY_MAP_L3 :
13380                                         MLX5_PRIORITY_MAP_L2;
13381                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
13382                                              MLX5_FLOW_LAYER_OUTER_L2;
13383                         break;
13384                 case RTE_FLOW_ITEM_TYPE_VLAN:
13385                         flow_dv_translate_item_vlan(dev_flow,
13386                                                     match_mask, match_value,
13387                                                     items, tunnel,
13388                                                     dev_flow->dv.group);
13389                         matcher.priority = MLX5_PRIORITY_MAP_L2;
13390                         last_item = tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
13391                                               MLX5_FLOW_LAYER_INNER_VLAN) :
13392                                              (MLX5_FLOW_LAYER_OUTER_L2 |
13393                                               MLX5_FLOW_LAYER_OUTER_VLAN);
13394                         break;
13395                 case RTE_FLOW_ITEM_TYPE_IPV4:
13396                         mlx5_flow_tunnel_ip_check(items, next_protocol,
13397                                                   &item_flags, &tunnel);
13398                         flow_dv_translate_item_ipv4(match_mask, match_value,
13399                                                     items, tunnel,
13400                                                     dev_flow->dv.group);
13401                         matcher.priority = MLX5_PRIORITY_MAP_L3;
13402                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
13403                                              MLX5_FLOW_LAYER_OUTER_L3_IPV4;
13404                         if (items->mask != NULL &&
13405                             ((const struct rte_flow_item_ipv4 *)
13406                              items->mask)->hdr.next_proto_id) {
13407                                 next_protocol =
13408                                         ((const struct rte_flow_item_ipv4 *)
13409                                          (items->spec))->hdr.next_proto_id;
13410                                 next_protocol &=
13411                                         ((const struct rte_flow_item_ipv4 *)
13412                                          (items->mask))->hdr.next_proto_id;
13413                         } else {
13414                                 /* Reset for inner layer. */
13415                                 next_protocol = 0xff;
13416                         }
13417                         break;
13418                 case RTE_FLOW_ITEM_TYPE_IPV6:
13419                         mlx5_flow_tunnel_ip_check(items, next_protocol,
13420                                                   &item_flags, &tunnel);
13421                         flow_dv_translate_item_ipv6(match_mask, match_value,
13422                                                     items, tunnel,
13423                                                     dev_flow->dv.group);
13424                         matcher.priority = MLX5_PRIORITY_MAP_L3;
13425                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
13426                                              MLX5_FLOW_LAYER_OUTER_L3_IPV6;
13427                         if (items->mask != NULL &&
13428                             ((const struct rte_flow_item_ipv6 *)
13429                              items->mask)->hdr.proto) {
13430                                 next_protocol =
13431                                         ((const struct rte_flow_item_ipv6 *)
13432                                          items->spec)->hdr.proto;
13433                                 next_protocol &=
13434                                         ((const struct rte_flow_item_ipv6 *)
13435                                          items->mask)->hdr.proto;
13436                         } else {
13437                                 /* Reset for inner layer. */
13438                                 next_protocol = 0xff;
13439                         }
13440                         break;
13441                 case RTE_FLOW_ITEM_TYPE_IPV6_FRAG_EXT:
13442                         flow_dv_translate_item_ipv6_frag_ext(match_mask,
13443                                                              match_value,
13444                                                              items, tunnel);
13445                         last_item = tunnel ?
13446                                         MLX5_FLOW_LAYER_INNER_L3_IPV6_FRAG_EXT :
13447                                         MLX5_FLOW_LAYER_OUTER_L3_IPV6_FRAG_EXT;
13448                         if (items->mask != NULL &&
13449                             ((const struct rte_flow_item_ipv6_frag_ext *)
13450                              items->mask)->hdr.next_header) {
13451                                 next_protocol =
13452                                 ((const struct rte_flow_item_ipv6_frag_ext *)
13453                                  items->spec)->hdr.next_header;
13454                                 next_protocol &=
13455                                 ((const struct rte_flow_item_ipv6_frag_ext *)
13456                                  items->mask)->hdr.next_header;
13457                         } else {
13458                                 /* Reset for inner layer. */
13459                                 next_protocol = 0xff;
13460                         }
13461                         break;
13462                 case RTE_FLOW_ITEM_TYPE_TCP:
13463                         flow_dv_translate_item_tcp(match_mask, match_value,
13464                                                    items, tunnel);
13465                         matcher.priority = MLX5_PRIORITY_MAP_L4;
13466                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
13467                                              MLX5_FLOW_LAYER_OUTER_L4_TCP;
13468                         break;
13469                 case RTE_FLOW_ITEM_TYPE_UDP:
13470                         flow_dv_translate_item_udp(match_mask, match_value,
13471                                                    items, tunnel);
13472                         matcher.priority = MLX5_PRIORITY_MAP_L4;
13473                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
13474                                              MLX5_FLOW_LAYER_OUTER_L4_UDP;
13475                         break;
13476                 case RTE_FLOW_ITEM_TYPE_GRE:
13477                         matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
13478                         last_item = MLX5_FLOW_LAYER_GRE;
13479                         tunnel_item = items;
13480                         break;
13481                 case RTE_FLOW_ITEM_TYPE_GRE_KEY:
13482                         flow_dv_translate_item_gre_key(match_mask,
13483                                                        match_value, items);
13484                         last_item = MLX5_FLOW_LAYER_GRE_KEY;
13485                         break;
13486                 case RTE_FLOW_ITEM_TYPE_NVGRE:
13487                         matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
13488                         last_item = MLX5_FLOW_LAYER_GRE;
13489                         tunnel_item = items;
13490                         break;
13491                 case RTE_FLOW_ITEM_TYPE_VXLAN:
13492                         flow_dv_translate_item_vxlan(dev, attr,
13493                                                      match_mask, match_value,
13494                                                      items, tunnel);
13495                         matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
13496                         last_item = MLX5_FLOW_LAYER_VXLAN;
13497                         break;
13498                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
13499                         matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
13500                         last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
13501                         tunnel_item = items;
13502                         break;
13503                 case RTE_FLOW_ITEM_TYPE_GENEVE:
13504                         matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
13505                         last_item = MLX5_FLOW_LAYER_GENEVE;
13506                         tunnel_item = items;
13507                         break;
13508                 case RTE_FLOW_ITEM_TYPE_GENEVE_OPT:
13509                         ret = flow_dv_translate_item_geneve_opt(dev, match_mask,
13510                                                           match_value,
13511                                                           items, error);
13512                         if (ret)
13513                                 return rte_flow_error_set(error, -ret,
13514                                         RTE_FLOW_ERROR_TYPE_ITEM, NULL,
13515                                         "cannot create GENEVE TLV option");
13516                         flow->geneve_tlv_option = 1;
13517                         last_item = MLX5_FLOW_LAYER_GENEVE_OPT;
13518                         break;
13519                 case RTE_FLOW_ITEM_TYPE_MPLS:
13520                         flow_dv_translate_item_mpls(match_mask, match_value,
13521                                                     items, last_item, tunnel);
13522                         matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
13523                         last_item = MLX5_FLOW_LAYER_MPLS;
13524                         break;
13525                 case RTE_FLOW_ITEM_TYPE_MARK:
13526                         flow_dv_translate_item_mark(dev, match_mask,
13527                                                     match_value, items);
13528                         last_item = MLX5_FLOW_ITEM_MARK;
13529                         break;
13530                 case RTE_FLOW_ITEM_TYPE_META:
13531                         flow_dv_translate_item_meta(dev, match_mask,
13532                                                     match_value, attr, items);
13533                         last_item = MLX5_FLOW_ITEM_METADATA;
13534                         break;
13535                 case RTE_FLOW_ITEM_TYPE_ICMP:
13536                         flow_dv_translate_item_icmp(match_mask, match_value,
13537                                                     items, tunnel);
13538                         last_item = MLX5_FLOW_LAYER_ICMP;
13539                         break;
13540                 case RTE_FLOW_ITEM_TYPE_ICMP6:
13541                         flow_dv_translate_item_icmp6(match_mask, match_value,
13542                                                       items, tunnel);
13543                         last_item = MLX5_FLOW_LAYER_ICMP6;
13544                         break;
13545                 case RTE_FLOW_ITEM_TYPE_TAG:
13546                         flow_dv_translate_item_tag(dev, match_mask,
13547                                                    match_value, items);
13548                         last_item = MLX5_FLOW_ITEM_TAG;
13549                         break;
13550                 case MLX5_RTE_FLOW_ITEM_TYPE_TAG:
13551                         flow_dv_translate_mlx5_item_tag(dev, match_mask,
13552                                                         match_value, items);
13553                         last_item = MLX5_FLOW_ITEM_TAG;
13554                         break;
13555                 case MLX5_RTE_FLOW_ITEM_TYPE_TX_QUEUE:
13556                         flow_dv_translate_item_tx_queue(dev, match_mask,
13557                                                         match_value,
13558                                                         items);
13559                         last_item = MLX5_FLOW_ITEM_TX_QUEUE;
13560                         break;
13561                 case RTE_FLOW_ITEM_TYPE_GTP:
13562                         flow_dv_translate_item_gtp(match_mask, match_value,
13563                                                    items, tunnel);
13564                         matcher.priority = MLX5_TUNNEL_PRIO_GET(rss_desc);
13565                         last_item = MLX5_FLOW_LAYER_GTP;
13566                         break;
13567                 case RTE_FLOW_ITEM_TYPE_GTP_PSC:
13568                         ret = flow_dv_translate_item_gtp_psc(match_mask,
13569                                                           match_value,
13570                                                           items);
13571                         if (ret)
13572                                 return rte_flow_error_set(error, -ret,
13573                                         RTE_FLOW_ERROR_TYPE_ITEM, NULL,
13574                                         "cannot create GTP PSC item");
13575                         last_item = MLX5_FLOW_LAYER_GTP_PSC;
13576                         break;
13577                 case RTE_FLOW_ITEM_TYPE_ECPRI:
13578                         if (!mlx5_flex_parser_ecpri_exist(dev)) {
13579                                 /* Create it only the first time to be used. */
13580                                 ret = mlx5_flex_parser_ecpri_alloc(dev);
13581                                 if (ret)
13582                                         return rte_flow_error_set
13583                                                 (error, -ret,
13584                                                 RTE_FLOW_ERROR_TYPE_ITEM,
13585                                                 NULL,
13586                                                 "cannot create eCPRI parser");
13587                         }
13588                         flow_dv_translate_item_ecpri(dev, match_mask,
13589                                                      match_value, items,
13590                                                      last_item);
13591                         /* No other protocol should follow eCPRI layer. */
13592                         last_item = MLX5_FLOW_LAYER_ECPRI;
13593                         break;
13594                 case RTE_FLOW_ITEM_TYPE_INTEGRITY:
13595                         flow_dv_translate_item_integrity(items, integrity_items,
13596                                                          &last_item);
13597                         break;
13598                 case RTE_FLOW_ITEM_TYPE_CONNTRACK:
13599                         flow_dv_translate_item_aso_ct(dev, match_mask,
13600                                                       match_value, items);
13601                         break;
13602                 case RTE_FLOW_ITEM_TYPE_FLEX:
13603                         flow_dv_translate_item_flex(dev, match_mask,
13604                                                     match_value, items,
13605                                                     dev_flow, tunnel != 0);
13606                         last_item = tunnel ? MLX5_FLOW_ITEM_INNER_FLEX :
13607                                     MLX5_FLOW_ITEM_OUTER_FLEX;
13608                         break;
13609                 default:
13610                         break;
13611                 }
13612                 item_flags |= last_item;
13613         }
13614         /*
13615          * When E-Switch mode is enabled, we have two cases where we need to
13616          * set the source port manually.
13617          * The first one, is in case of Nic steering rule, and the second is
13618          * E-Switch rule where no port_id item was found. In both cases
13619          * the source port is set according the current port in use.
13620          */
13621         if (!(item_flags & MLX5_FLOW_ITEM_PORT_ID) &&
13622             (priv->representor || priv->master)) {
13623                 if (flow_dv_translate_item_port_id(dev, match_mask,
13624                                                    match_value, NULL, attr))
13625                         return -rte_errno;
13626         }
13627         if (item_flags & MLX5_FLOW_ITEM_INTEGRITY) {
13628                 flow_dv_translate_item_integrity_post(match_mask, match_value,
13629                                                       integrity_items,
13630                                                       item_flags);
13631         }
13632         if (item_flags & MLX5_FLOW_LAYER_VXLAN_GPE)
13633                 flow_dv_translate_item_vxlan_gpe(match_mask, match_value,
13634                                                  tunnel_item, item_flags);
13635         else if (item_flags & MLX5_FLOW_LAYER_GENEVE)
13636                 flow_dv_translate_item_geneve(match_mask, match_value,
13637                                               tunnel_item, item_flags);
13638         else if (item_flags & MLX5_FLOW_LAYER_GRE) {
13639                 if (tunnel_item->type == RTE_FLOW_ITEM_TYPE_GRE)
13640                         flow_dv_translate_item_gre(match_mask, match_value,
13641                                                    tunnel_item, item_flags);
13642                 else if (tunnel_item->type == RTE_FLOW_ITEM_TYPE_NVGRE)
13643                         flow_dv_translate_item_nvgre(match_mask, match_value,
13644                                                      tunnel_item, item_flags);
13645                 else
13646                         MLX5_ASSERT(false);
13647         }
13648 #ifdef RTE_LIBRTE_MLX5_DEBUG
13649         MLX5_ASSERT(!flow_dv_check_valid_spec(matcher.mask.buf,
13650                                               dev_flow->dv.value.buf));
13651 #endif
13652         /*
13653          * Layers may be already initialized from prefix flow if this dev_flow
13654          * is the suffix flow.
13655          */
13656         handle->layers |= item_flags;
13657         if (action_flags & MLX5_FLOW_ACTION_RSS)
13658                 flow_dv_hashfields_set(dev_flow, rss_desc);
13659         /* If has RSS action in the sample action, the Sample/Mirror resource
13660          * should be registered after the hash filed be update.
13661          */
13662         if (action_flags & MLX5_FLOW_ACTION_SAMPLE) {
13663                 ret = flow_dv_translate_action_sample(dev,
13664                                                       sample,
13665                                                       dev_flow, attr,
13666                                                       &num_of_dest,
13667                                                       sample_actions,
13668                                                       &sample_res,
13669                                                       error);
13670                 if (ret < 0)
13671                         return ret;
13672                 ret = flow_dv_create_action_sample(dev,
13673                                                    dev_flow,
13674                                                    num_of_dest,
13675                                                    &sample_res,
13676                                                    &mdest_res,
13677                                                    sample_actions,
13678                                                    action_flags,
13679                                                    error);
13680                 if (ret < 0)
13681                         return rte_flow_error_set
13682                                                 (error, rte_errno,
13683                                                 RTE_FLOW_ERROR_TYPE_ACTION,
13684                                                 NULL,
13685                                                 "cannot create sample action");
13686                 if (num_of_dest > 1) {
13687                         dev_flow->dv.actions[sample_act_pos] =
13688                         dev_flow->dv.dest_array_res->action;
13689                 } else {
13690                         dev_flow->dv.actions[sample_act_pos] =
13691                         dev_flow->dv.sample_res->verbs_action;
13692                 }
13693         }
13694         /*
13695          * For multiple destination (sample action with ratio=1), the encap
13696          * action and port id action will be combined into group action.
13697          * So need remove the original these actions in the flow and only
13698          * use the sample action instead of.
13699          */
13700         if (num_of_dest > 1 &&
13701             (sample_act->dr_port_id_action || sample_act->dr_jump_action)) {
13702                 int i;
13703                 void *temp_actions[MLX5_DV_MAX_NUMBER_OF_ACTIONS] = {0};
13704
13705                 for (i = 0; i < actions_n; i++) {
13706                         if ((sample_act->dr_encap_action &&
13707                                 sample_act->dr_encap_action ==
13708                                 dev_flow->dv.actions[i]) ||
13709                                 (sample_act->dr_port_id_action &&
13710                                 sample_act->dr_port_id_action ==
13711                                 dev_flow->dv.actions[i]) ||
13712                                 (sample_act->dr_jump_action &&
13713                                 sample_act->dr_jump_action ==
13714                                 dev_flow->dv.actions[i]))
13715                                 continue;
13716                         temp_actions[tmp_actions_n++] = dev_flow->dv.actions[i];
13717                 }
13718                 memcpy((void *)dev_flow->dv.actions,
13719                                 (void *)temp_actions,
13720                                 tmp_actions_n * sizeof(void *));
13721                 actions_n = tmp_actions_n;
13722         }
13723         dev_flow->dv.actions_n = actions_n;
13724         dev_flow->act_flags = action_flags;
13725         if (wks->skip_matcher_reg)
13726                 return 0;
13727         /* Register matcher. */
13728         matcher.crc = rte_raw_cksum((const void *)matcher.mask.buf,
13729                                     matcher.mask.size);
13730         matcher.priority = mlx5_get_matcher_priority(dev, attr,
13731                                                      matcher.priority,
13732                                                      dev_flow->external);
13733         /**
13734          * When creating meter drop flow in drop table, using original
13735          * 5-tuple match, the matcher priority should be lower than
13736          * mtr_id matcher.
13737          */
13738         if (attr->group == MLX5_FLOW_TABLE_LEVEL_METER &&
13739             dev_flow->dv.table_id == MLX5_MTR_TABLE_ID_DROP &&
13740             matcher.priority <= MLX5_REG_BITS)
13741                 matcher.priority += MLX5_REG_BITS;
13742         /* reserved field no needs to be set to 0 here. */
13743         tbl_key.is_fdb = attr->transfer;
13744         tbl_key.is_egress = attr->egress;
13745         tbl_key.level = dev_flow->dv.group;
13746         tbl_key.id = dev_flow->dv.table_id;
13747         if (flow_dv_matcher_register(dev, &matcher, &tbl_key, dev_flow,
13748                                      tunnel, attr->group, error))
13749                 return -rte_errno;
13750         return 0;
13751 }
13752
13753 /**
13754  * Set hash RX queue by hash fields (see enum ibv_rx_hash_fields)
13755  * and tunnel.
13756  *
13757  * @param[in, out] action
13758  *   Shred RSS action holding hash RX queue objects.
13759  * @param[in] hash_fields
13760  *   Defines combination of packet fields to participate in RX hash.
13761  * @param[in] tunnel
13762  *   Tunnel type
13763  * @param[in] hrxq_idx
13764  *   Hash RX queue index to set.
13765  *
13766  * @return
13767  *   0 on success, otherwise negative errno value.
13768  */
13769 static int
13770 __flow_dv_action_rss_hrxq_set(struct mlx5_shared_action_rss *action,
13771                               const uint64_t hash_fields,
13772                               uint32_t hrxq_idx)
13773 {
13774         uint32_t *hrxqs = action->hrxq;
13775
13776         switch (hash_fields & ~IBV_RX_HASH_INNER) {
13777         case MLX5_RSS_HASH_IPV4:
13778                 /* fall-through. */
13779         case MLX5_RSS_HASH_IPV4_DST_ONLY:
13780                 /* fall-through. */
13781         case MLX5_RSS_HASH_IPV4_SRC_ONLY:
13782                 hrxqs[0] = hrxq_idx;
13783                 return 0;
13784         case MLX5_RSS_HASH_IPV4_TCP:
13785                 /* fall-through. */
13786         case MLX5_RSS_HASH_IPV4_TCP_DST_ONLY:
13787                 /* fall-through. */
13788         case MLX5_RSS_HASH_IPV4_TCP_SRC_ONLY:
13789                 hrxqs[1] = hrxq_idx;
13790                 return 0;
13791         case MLX5_RSS_HASH_IPV4_UDP:
13792                 /* fall-through. */
13793         case MLX5_RSS_HASH_IPV4_UDP_DST_ONLY:
13794                 /* fall-through. */
13795         case MLX5_RSS_HASH_IPV4_UDP_SRC_ONLY:
13796                 hrxqs[2] = hrxq_idx;
13797                 return 0;
13798         case MLX5_RSS_HASH_IPV6:
13799                 /* fall-through. */
13800         case MLX5_RSS_HASH_IPV6_DST_ONLY:
13801                 /* fall-through. */
13802         case MLX5_RSS_HASH_IPV6_SRC_ONLY:
13803                 hrxqs[3] = hrxq_idx;
13804                 return 0;
13805         case MLX5_RSS_HASH_IPV6_TCP:
13806                 /* fall-through. */
13807         case MLX5_RSS_HASH_IPV6_TCP_DST_ONLY:
13808                 /* fall-through. */
13809         case MLX5_RSS_HASH_IPV6_TCP_SRC_ONLY:
13810                 hrxqs[4] = hrxq_idx;
13811                 return 0;
13812         case MLX5_RSS_HASH_IPV6_UDP:
13813                 /* fall-through. */
13814         case MLX5_RSS_HASH_IPV6_UDP_DST_ONLY:
13815                 /* fall-through. */
13816         case MLX5_RSS_HASH_IPV6_UDP_SRC_ONLY:
13817                 hrxqs[5] = hrxq_idx;
13818                 return 0;
13819         case MLX5_RSS_HASH_NONE:
13820                 hrxqs[6] = hrxq_idx;
13821                 return 0;
13822         default:
13823                 return -1;
13824         }
13825 }
13826
13827 /**
13828  * Look up for hash RX queue by hash fields (see enum ibv_rx_hash_fields)
13829  * and tunnel.
13830  *
13831  * @param[in] dev
13832  *   Pointer to the Ethernet device structure.
13833  * @param[in] idx
13834  *   Shared RSS action ID holding hash RX queue objects.
13835  * @param[in] hash_fields
13836  *   Defines combination of packet fields to participate in RX hash.
13837  * @param[in] tunnel
13838  *   Tunnel type
13839  *
13840  * @return
13841  *   Valid hash RX queue index, otherwise 0.
13842  */
13843 static uint32_t
13844 __flow_dv_action_rss_hrxq_lookup(struct rte_eth_dev *dev, uint32_t idx,
13845                                  const uint64_t hash_fields)
13846 {
13847         struct mlx5_priv *priv = dev->data->dev_private;
13848         struct mlx5_shared_action_rss *shared_rss =
13849             mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS], idx);
13850         const uint32_t *hrxqs = shared_rss->hrxq;
13851
13852         switch (hash_fields & ~IBV_RX_HASH_INNER) {
13853         case MLX5_RSS_HASH_IPV4:
13854                 /* fall-through. */
13855         case MLX5_RSS_HASH_IPV4_DST_ONLY:
13856                 /* fall-through. */
13857         case MLX5_RSS_HASH_IPV4_SRC_ONLY:
13858                 return hrxqs[0];
13859         case MLX5_RSS_HASH_IPV4_TCP:
13860                 /* fall-through. */
13861         case MLX5_RSS_HASH_IPV4_TCP_DST_ONLY:
13862                 /* fall-through. */
13863         case MLX5_RSS_HASH_IPV4_TCP_SRC_ONLY:
13864                 return hrxqs[1];
13865         case MLX5_RSS_HASH_IPV4_UDP:
13866                 /* fall-through. */
13867         case MLX5_RSS_HASH_IPV4_UDP_DST_ONLY:
13868                 /* fall-through. */
13869         case MLX5_RSS_HASH_IPV4_UDP_SRC_ONLY:
13870                 return hrxqs[2];
13871         case MLX5_RSS_HASH_IPV6:
13872                 /* fall-through. */
13873         case MLX5_RSS_HASH_IPV6_DST_ONLY:
13874                 /* fall-through. */
13875         case MLX5_RSS_HASH_IPV6_SRC_ONLY:
13876                 return hrxqs[3];
13877         case MLX5_RSS_HASH_IPV6_TCP:
13878                 /* fall-through. */
13879         case MLX5_RSS_HASH_IPV6_TCP_DST_ONLY:
13880                 /* fall-through. */
13881         case MLX5_RSS_HASH_IPV6_TCP_SRC_ONLY:
13882                 return hrxqs[4];
13883         case MLX5_RSS_HASH_IPV6_UDP:
13884                 /* fall-through. */
13885         case MLX5_RSS_HASH_IPV6_UDP_DST_ONLY:
13886                 /* fall-through. */
13887         case MLX5_RSS_HASH_IPV6_UDP_SRC_ONLY:
13888                 return hrxqs[5];
13889         case MLX5_RSS_HASH_NONE:
13890                 return hrxqs[6];
13891         default:
13892                 return 0;
13893         }
13894
13895 }
13896
13897 /**
13898  * Apply the flow to the NIC, lock free,
13899  * (mutex should be acquired by caller).
13900  *
13901  * @param[in] dev
13902  *   Pointer to the Ethernet device structure.
13903  * @param[in, out] flow
13904  *   Pointer to flow structure.
13905  * @param[out] error
13906  *   Pointer to error structure.
13907  *
13908  * @return
13909  *   0 on success, a negative errno value otherwise and rte_errno is set.
13910  */
13911 static int
13912 flow_dv_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
13913               struct rte_flow_error *error)
13914 {
13915         struct mlx5_flow_dv_workspace *dv;
13916         struct mlx5_flow_handle *dh;
13917         struct mlx5_flow_handle_dv *dv_h;
13918         struct mlx5_flow *dev_flow;
13919         struct mlx5_priv *priv = dev->data->dev_private;
13920         uint32_t handle_idx;
13921         int n;
13922         int err;
13923         int idx;
13924         struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
13925         struct mlx5_flow_rss_desc *rss_desc = &wks->rss_desc;
13926         uint8_t misc_mask;
13927
13928         MLX5_ASSERT(wks);
13929         for (idx = wks->flow_idx - 1; idx >= 0; idx--) {
13930                 dev_flow = &wks->flows[idx];
13931                 dv = &dev_flow->dv;
13932                 dh = dev_flow->handle;
13933                 dv_h = &dh->dvh;
13934                 n = dv->actions_n;
13935                 if (dh->fate_action == MLX5_FLOW_FATE_DROP) {
13936                         if (dv->transfer) {
13937                                 MLX5_ASSERT(priv->sh->dr_drop_action);
13938                                 dv->actions[n++] = priv->sh->dr_drop_action;
13939                         } else {
13940 #ifdef HAVE_MLX5DV_DR
13941                                 /* DR supports drop action placeholder. */
13942                                 MLX5_ASSERT(priv->sh->dr_drop_action);
13943                                 dv->actions[n++] = dv->group ?
13944                                         priv->sh->dr_drop_action :
13945                                         priv->root_drop_action;
13946 #else
13947                                 /* For DV we use the explicit drop queue. */
13948                                 MLX5_ASSERT(priv->drop_queue.hrxq);
13949                                 dv->actions[n++] =
13950                                                 priv->drop_queue.hrxq->action;
13951 #endif
13952                         }
13953                 } else if ((dh->fate_action == MLX5_FLOW_FATE_QUEUE &&
13954                            !dv_h->rix_sample && !dv_h->rix_dest_array)) {
13955                         struct mlx5_hrxq *hrxq;
13956                         uint32_t hrxq_idx;
13957
13958                         hrxq = flow_dv_hrxq_prepare(dev, dev_flow, rss_desc,
13959                                                     &hrxq_idx);
13960                         if (!hrxq) {
13961                                 rte_flow_error_set
13962                                         (error, rte_errno,
13963                                          RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
13964                                          "cannot get hash queue");
13965                                 goto error;
13966                         }
13967                         dh->rix_hrxq = hrxq_idx;
13968                         dv->actions[n++] = hrxq->action;
13969                 } else if (dh->fate_action == MLX5_FLOW_FATE_SHARED_RSS) {
13970                         struct mlx5_hrxq *hrxq = NULL;
13971                         uint32_t hrxq_idx;
13972
13973                         hrxq_idx = __flow_dv_action_rss_hrxq_lookup(dev,
13974                                                 rss_desc->shared_rss,
13975                                                 dev_flow->hash_fields);
13976                         if (hrxq_idx)
13977                                 hrxq = mlx5_ipool_get
13978                                         (priv->sh->ipool[MLX5_IPOOL_HRXQ],
13979                                          hrxq_idx);
13980                         if (!hrxq) {
13981                                 rte_flow_error_set
13982                                         (error, rte_errno,
13983                                          RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
13984                                          "cannot get hash queue");
13985                                 goto error;
13986                         }
13987                         dh->rix_srss = rss_desc->shared_rss;
13988                         dv->actions[n++] = hrxq->action;
13989                 } else if (dh->fate_action == MLX5_FLOW_FATE_DEFAULT_MISS) {
13990                         if (!priv->sh->default_miss_action) {
13991                                 rte_flow_error_set
13992                                         (error, rte_errno,
13993                                          RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
13994                                          "default miss action not be created.");
13995                                 goto error;
13996                         }
13997                         dv->actions[n++] = priv->sh->default_miss_action;
13998                 }
13999                 misc_mask = flow_dv_matcher_enable(dv->value.buf);
14000                 __flow_dv_adjust_buf_size(&dv->value.size, misc_mask);
14001                 err = mlx5_flow_os_create_flow(dv_h->matcher->matcher_object,
14002                                                (void *)&dv->value, n,
14003                                                dv->actions, &dh->drv_flow);
14004                 if (err) {
14005                         rte_flow_error_set
14006                                 (error, errno,
14007                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
14008                                 NULL,
14009                                 (!priv->config.allow_duplicate_pattern &&
14010                                 errno == EEXIST) ?
14011                                 "duplicating pattern is not allowed" :
14012                                 "hardware refuses to create flow");
14013                         goto error;
14014                 }
14015                 if (priv->vmwa_context &&
14016                     dh->vf_vlan.tag && !dh->vf_vlan.created) {
14017                         /*
14018                          * The rule contains the VLAN pattern.
14019                          * For VF we are going to create VLAN
14020                          * interface to make hypervisor set correct
14021                          * e-Switch vport context.
14022                          */
14023                         mlx5_vlan_vmwa_acquire(dev, &dh->vf_vlan);
14024                 }
14025         }
14026         return 0;
14027 error:
14028         err = rte_errno; /* Save rte_errno before cleanup. */
14029         SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
14030                        handle_idx, dh, next) {
14031                 /* hrxq is union, don't clear it if the flag is not set. */
14032                 if (dh->fate_action == MLX5_FLOW_FATE_QUEUE && dh->rix_hrxq) {
14033                         mlx5_hrxq_release(dev, dh->rix_hrxq);
14034                         dh->rix_hrxq = 0;
14035                 } else if (dh->fate_action == MLX5_FLOW_FATE_SHARED_RSS) {
14036                         dh->rix_srss = 0;
14037                 }
14038                 if (dh->vf_vlan.tag && dh->vf_vlan.created)
14039                         mlx5_vlan_vmwa_release(dev, &dh->vf_vlan);
14040         }
14041         rte_errno = err; /* Restore rte_errno. */
14042         return -rte_errno;
14043 }
14044
14045 void
14046 flow_dv_matcher_remove_cb(void *tool_ctx __rte_unused,
14047                           struct mlx5_list_entry *entry)
14048 {
14049         struct mlx5_flow_dv_matcher *resource = container_of(entry,
14050                                                              typeof(*resource),
14051                                                              entry);
14052
14053         claim_zero(mlx5_flow_os_destroy_flow_matcher(resource->matcher_object));
14054         mlx5_free(resource);
14055 }
14056
14057 /**
14058  * Release the flow matcher.
14059  *
14060  * @param dev
14061  *   Pointer to Ethernet device.
14062  * @param port_id
14063  *   Index to port ID action resource.
14064  *
14065  * @return
14066  *   1 while a reference on it exists, 0 when freed.
14067  */
14068 static int
14069 flow_dv_matcher_release(struct rte_eth_dev *dev,
14070                         struct mlx5_flow_handle *handle)
14071 {
14072         struct mlx5_flow_dv_matcher *matcher = handle->dvh.matcher;
14073         struct mlx5_flow_tbl_data_entry *tbl = container_of(matcher->tbl,
14074                                                             typeof(*tbl), tbl);
14075         int ret;
14076
14077         MLX5_ASSERT(matcher->matcher_object);
14078         ret = mlx5_list_unregister(tbl->matchers, &matcher->entry);
14079         flow_dv_tbl_resource_release(MLX5_SH(dev), &tbl->tbl);
14080         return ret;
14081 }
14082
14083 void
14084 flow_dv_encap_decap_remove_cb(void *tool_ctx, struct mlx5_list_entry *entry)
14085 {
14086         struct mlx5_dev_ctx_shared *sh = tool_ctx;
14087         struct mlx5_flow_dv_encap_decap_resource *res =
14088                                        container_of(entry, typeof(*res), entry);
14089
14090         claim_zero(mlx5_flow_os_destroy_flow_action(res->action));
14091         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_DECAP_ENCAP], res->idx);
14092 }
14093
14094 /**
14095  * Release an encap/decap resource.
14096  *
14097  * @param dev
14098  *   Pointer to Ethernet device.
14099  * @param encap_decap_idx
14100  *   Index of encap decap resource.
14101  *
14102  * @return
14103  *   1 while a reference on it exists, 0 when freed.
14104  */
14105 static int
14106 flow_dv_encap_decap_resource_release(struct rte_eth_dev *dev,
14107                                      uint32_t encap_decap_idx)
14108 {
14109         struct mlx5_priv *priv = dev->data->dev_private;
14110         struct mlx5_flow_dv_encap_decap_resource *resource;
14111
14112         resource = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_DECAP_ENCAP],
14113                                   encap_decap_idx);
14114         if (!resource)
14115                 return 0;
14116         MLX5_ASSERT(resource->action);
14117         return mlx5_hlist_unregister(priv->sh->encaps_decaps, &resource->entry);
14118 }
14119
14120 /**
14121  * Release an jump to table action resource.
14122  *
14123  * @param dev
14124  *   Pointer to Ethernet device.
14125  * @param rix_jump
14126  *   Index to the jump action resource.
14127  *
14128  * @return
14129  *   1 while a reference on it exists, 0 when freed.
14130  */
14131 static int
14132 flow_dv_jump_tbl_resource_release(struct rte_eth_dev *dev,
14133                                   uint32_t rix_jump)
14134 {
14135         struct mlx5_priv *priv = dev->data->dev_private;
14136         struct mlx5_flow_tbl_data_entry *tbl_data;
14137
14138         tbl_data = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_JUMP],
14139                                   rix_jump);
14140         if (!tbl_data)
14141                 return 0;
14142         return flow_dv_tbl_resource_release(MLX5_SH(dev), &tbl_data->tbl);
14143 }
14144
14145 void
14146 flow_dv_modify_remove_cb(void *tool_ctx, struct mlx5_list_entry *entry)
14147 {
14148         struct mlx5_flow_dv_modify_hdr_resource *res =
14149                 container_of(entry, typeof(*res), entry);
14150         struct mlx5_dev_ctx_shared *sh = tool_ctx;
14151
14152         claim_zero(mlx5_flow_os_destroy_flow_action(res->action));
14153         mlx5_ipool_free(sh->mdh_ipools[res->actions_num - 1], res->idx);
14154 }
14155
14156 /**
14157  * Release a modify-header resource.
14158  *
14159  * @param dev
14160  *   Pointer to Ethernet device.
14161  * @param handle
14162  *   Pointer to mlx5_flow_handle.
14163  *
14164  * @return
14165  *   1 while a reference on it exists, 0 when freed.
14166  */
14167 static int
14168 flow_dv_modify_hdr_resource_release(struct rte_eth_dev *dev,
14169                                     struct mlx5_flow_handle *handle)
14170 {
14171         struct mlx5_priv *priv = dev->data->dev_private;
14172         struct mlx5_flow_dv_modify_hdr_resource *entry = handle->dvh.modify_hdr;
14173
14174         MLX5_ASSERT(entry->action);
14175         return mlx5_hlist_unregister(priv->sh->modify_cmds, &entry->entry);
14176 }
14177
14178 void
14179 flow_dv_port_id_remove_cb(void *tool_ctx, struct mlx5_list_entry *entry)
14180 {
14181         struct mlx5_dev_ctx_shared *sh = tool_ctx;
14182         struct mlx5_flow_dv_port_id_action_resource *resource =
14183                                   container_of(entry, typeof(*resource), entry);
14184
14185         claim_zero(mlx5_flow_os_destroy_flow_action(resource->action));
14186         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_PORT_ID], resource->idx);
14187 }
14188
14189 /**
14190  * Release port ID action resource.
14191  *
14192  * @param dev
14193  *   Pointer to Ethernet device.
14194  * @param handle
14195  *   Pointer to mlx5_flow_handle.
14196  *
14197  * @return
14198  *   1 while a reference on it exists, 0 when freed.
14199  */
14200 static int
14201 flow_dv_port_id_action_resource_release(struct rte_eth_dev *dev,
14202                                         uint32_t port_id)
14203 {
14204         struct mlx5_priv *priv = dev->data->dev_private;
14205         struct mlx5_flow_dv_port_id_action_resource *resource;
14206
14207         resource = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_PORT_ID], port_id);
14208         if (!resource)
14209                 return 0;
14210         MLX5_ASSERT(resource->action);
14211         return mlx5_list_unregister(priv->sh->port_id_action_list,
14212                                     &resource->entry);
14213 }
14214
14215 /**
14216  * Release shared RSS action resource.
14217  *
14218  * @param dev
14219  *   Pointer to Ethernet device.
14220  * @param srss
14221  *   Shared RSS action index.
14222  */
14223 static void
14224 flow_dv_shared_rss_action_release(struct rte_eth_dev *dev, uint32_t srss)
14225 {
14226         struct mlx5_priv *priv = dev->data->dev_private;
14227         struct mlx5_shared_action_rss *shared_rss;
14228
14229         shared_rss = mlx5_ipool_get
14230                         (priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS], srss);
14231         __atomic_sub_fetch(&shared_rss->refcnt, 1, __ATOMIC_RELAXED);
14232 }
14233
14234 void
14235 flow_dv_push_vlan_remove_cb(void *tool_ctx, struct mlx5_list_entry *entry)
14236 {
14237         struct mlx5_dev_ctx_shared *sh = tool_ctx;
14238         struct mlx5_flow_dv_push_vlan_action_resource *resource =
14239                         container_of(entry, typeof(*resource), entry);
14240
14241         claim_zero(mlx5_flow_os_destroy_flow_action(resource->action));
14242         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_PUSH_VLAN], resource->idx);
14243 }
14244
14245 /**
14246  * Release push vlan action resource.
14247  *
14248  * @param dev
14249  *   Pointer to Ethernet device.
14250  * @param handle
14251  *   Pointer to mlx5_flow_handle.
14252  *
14253  * @return
14254  *   1 while a reference on it exists, 0 when freed.
14255  */
14256 static int
14257 flow_dv_push_vlan_action_resource_release(struct rte_eth_dev *dev,
14258                                           struct mlx5_flow_handle *handle)
14259 {
14260         struct mlx5_priv *priv = dev->data->dev_private;
14261         struct mlx5_flow_dv_push_vlan_action_resource *resource;
14262         uint32_t idx = handle->dvh.rix_push_vlan;
14263
14264         resource = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_PUSH_VLAN], idx);
14265         if (!resource)
14266                 return 0;
14267         MLX5_ASSERT(resource->action);
14268         return mlx5_list_unregister(priv->sh->push_vlan_action_list,
14269                                     &resource->entry);
14270 }
14271
14272 /**
14273  * Release the fate resource.
14274  *
14275  * @param dev
14276  *   Pointer to Ethernet device.
14277  * @param handle
14278  *   Pointer to mlx5_flow_handle.
14279  */
14280 static void
14281 flow_dv_fate_resource_release(struct rte_eth_dev *dev,
14282                                struct mlx5_flow_handle *handle)
14283 {
14284         if (!handle->rix_fate)
14285                 return;
14286         switch (handle->fate_action) {
14287         case MLX5_FLOW_FATE_QUEUE:
14288                 if (!handle->dvh.rix_sample && !handle->dvh.rix_dest_array)
14289                         mlx5_hrxq_release(dev, handle->rix_hrxq);
14290                 break;
14291         case MLX5_FLOW_FATE_JUMP:
14292                 flow_dv_jump_tbl_resource_release(dev, handle->rix_jump);
14293                 break;
14294         case MLX5_FLOW_FATE_PORT_ID:
14295                 flow_dv_port_id_action_resource_release(dev,
14296                                 handle->rix_port_id_action);
14297                 break;
14298         default:
14299                 DRV_LOG(DEBUG, "Incorrect fate action:%d", handle->fate_action);
14300                 break;
14301         }
14302         handle->rix_fate = 0;
14303 }
14304
14305 void
14306 flow_dv_sample_remove_cb(void *tool_ctx __rte_unused,
14307                          struct mlx5_list_entry *entry)
14308 {
14309         struct mlx5_flow_dv_sample_resource *resource = container_of(entry,
14310                                                               typeof(*resource),
14311                                                               entry);
14312         struct rte_eth_dev *dev = resource->dev;
14313         struct mlx5_priv *priv = dev->data->dev_private;
14314
14315         if (resource->verbs_action)
14316                 claim_zero(mlx5_flow_os_destroy_flow_action
14317                                                       (resource->verbs_action));
14318         if (resource->normal_path_tbl)
14319                 flow_dv_tbl_resource_release(MLX5_SH(dev),
14320                                              resource->normal_path_tbl);
14321         flow_dv_sample_sub_actions_release(dev, &resource->sample_idx);
14322         mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_SAMPLE], resource->idx);
14323         DRV_LOG(DEBUG, "sample resource %p: removed", (void *)resource);
14324 }
14325
14326 /**
14327  * Release an sample resource.
14328  *
14329  * @param dev
14330  *   Pointer to Ethernet device.
14331  * @param handle
14332  *   Pointer to mlx5_flow_handle.
14333  *
14334  * @return
14335  *   1 while a reference on it exists, 0 when freed.
14336  */
14337 static int
14338 flow_dv_sample_resource_release(struct rte_eth_dev *dev,
14339                                      struct mlx5_flow_handle *handle)
14340 {
14341         struct mlx5_priv *priv = dev->data->dev_private;
14342         struct mlx5_flow_dv_sample_resource *resource;
14343
14344         resource = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_SAMPLE],
14345                                   handle->dvh.rix_sample);
14346         if (!resource)
14347                 return 0;
14348         MLX5_ASSERT(resource->verbs_action);
14349         return mlx5_list_unregister(priv->sh->sample_action_list,
14350                                     &resource->entry);
14351 }
14352
14353 void
14354 flow_dv_dest_array_remove_cb(void *tool_ctx __rte_unused,
14355                              struct mlx5_list_entry *entry)
14356 {
14357         struct mlx5_flow_dv_dest_array_resource *resource =
14358                         container_of(entry, typeof(*resource), entry);
14359         struct rte_eth_dev *dev = resource->dev;
14360         struct mlx5_priv *priv = dev->data->dev_private;
14361         uint32_t i = 0;
14362
14363         MLX5_ASSERT(resource->action);
14364         if (resource->action)
14365                 claim_zero(mlx5_flow_os_destroy_flow_action(resource->action));
14366         for (; i < resource->num_of_dest; i++)
14367                 flow_dv_sample_sub_actions_release(dev,
14368                                                    &resource->sample_idx[i]);
14369         mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_DEST_ARRAY], resource->idx);
14370         DRV_LOG(DEBUG, "destination array resource %p: removed",
14371                 (void *)resource);
14372 }
14373
14374 /**
14375  * Release an destination array resource.
14376  *
14377  * @param dev
14378  *   Pointer to Ethernet device.
14379  * @param handle
14380  *   Pointer to mlx5_flow_handle.
14381  *
14382  * @return
14383  *   1 while a reference on it exists, 0 when freed.
14384  */
14385 static int
14386 flow_dv_dest_array_resource_release(struct rte_eth_dev *dev,
14387                                     struct mlx5_flow_handle *handle)
14388 {
14389         struct mlx5_priv *priv = dev->data->dev_private;
14390         struct mlx5_flow_dv_dest_array_resource *resource;
14391
14392         resource = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_DEST_ARRAY],
14393                                   handle->dvh.rix_dest_array);
14394         if (!resource)
14395                 return 0;
14396         MLX5_ASSERT(resource->action);
14397         return mlx5_list_unregister(priv->sh->dest_array_list,
14398                                     &resource->entry);
14399 }
14400
14401 static void
14402 flow_dv_geneve_tlv_option_resource_release(struct rte_eth_dev *dev)
14403 {
14404         struct mlx5_priv *priv = dev->data->dev_private;
14405         struct mlx5_dev_ctx_shared *sh = priv->sh;
14406         struct mlx5_geneve_tlv_option_resource *geneve_opt_resource =
14407                                 sh->geneve_tlv_option_resource;
14408         rte_spinlock_lock(&sh->geneve_tlv_opt_sl);
14409         if (geneve_opt_resource) {
14410                 if (!(__atomic_sub_fetch(&geneve_opt_resource->refcnt, 1,
14411                                          __ATOMIC_RELAXED))) {
14412                         claim_zero(mlx5_devx_cmd_destroy
14413                                         (geneve_opt_resource->obj));
14414                         mlx5_free(sh->geneve_tlv_option_resource);
14415                         sh->geneve_tlv_option_resource = NULL;
14416                 }
14417         }
14418         rte_spinlock_unlock(&sh->geneve_tlv_opt_sl);
14419 }
14420
14421 /**
14422  * Remove the flow from the NIC but keeps it in memory.
14423  * Lock free, (mutex should be acquired by caller).
14424  *
14425  * @param[in] dev
14426  *   Pointer to Ethernet device.
14427  * @param[in, out] flow
14428  *   Pointer to flow structure.
14429  */
14430 static void
14431 flow_dv_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
14432 {
14433         struct mlx5_flow_handle *dh;
14434         uint32_t handle_idx;
14435         struct mlx5_priv *priv = dev->data->dev_private;
14436
14437         if (!flow)
14438                 return;
14439         handle_idx = flow->dev_handles;
14440         while (handle_idx) {
14441                 dh = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
14442                                     handle_idx);
14443                 if (!dh)
14444                         return;
14445                 if (dh->drv_flow) {
14446                         claim_zero(mlx5_flow_os_destroy_flow(dh->drv_flow));
14447                         dh->drv_flow = NULL;
14448                 }
14449                 if (dh->fate_action == MLX5_FLOW_FATE_QUEUE)
14450                         flow_dv_fate_resource_release(dev, dh);
14451                 if (dh->vf_vlan.tag && dh->vf_vlan.created)
14452                         mlx5_vlan_vmwa_release(dev, &dh->vf_vlan);
14453                 handle_idx = dh->next.next;
14454         }
14455 }
14456
14457 /**
14458  * Remove the flow from the NIC and the memory.
14459  * Lock free, (mutex should be acquired by caller).
14460  *
14461  * @param[in] dev
14462  *   Pointer to the Ethernet device structure.
14463  * @param[in, out] flow
14464  *   Pointer to flow structure.
14465  */
14466 static void
14467 flow_dv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
14468 {
14469         struct mlx5_flow_handle *dev_handle;
14470         struct mlx5_priv *priv = dev->data->dev_private;
14471         struct mlx5_flow_meter_info *fm = NULL;
14472         uint32_t srss = 0;
14473
14474         if (!flow)
14475                 return;
14476         flow_dv_remove(dev, flow);
14477         if (flow->counter) {
14478                 flow_dv_counter_free(dev, flow->counter);
14479                 flow->counter = 0;
14480         }
14481         if (flow->meter) {
14482                 fm = flow_dv_meter_find_by_idx(priv, flow->meter);
14483                 if (fm)
14484                         mlx5_flow_meter_detach(priv, fm);
14485                 flow->meter = 0;
14486         }
14487         /* Keep the current age handling by default. */
14488         if (flow->indirect_type == MLX5_INDIRECT_ACTION_TYPE_CT && flow->ct)
14489                 flow_dv_aso_ct_release(dev, flow->ct, NULL);
14490         else if (flow->age)
14491                 flow_dv_aso_age_release(dev, flow->age);
14492         if (flow->geneve_tlv_option) {
14493                 flow_dv_geneve_tlv_option_resource_release(dev);
14494                 flow->geneve_tlv_option = 0;
14495         }
14496         while (flow->dev_handles) {
14497                 uint32_t tmp_idx = flow->dev_handles;
14498
14499                 dev_handle = mlx5_ipool_get(priv->sh->ipool
14500                                             [MLX5_IPOOL_MLX5_FLOW], tmp_idx);
14501                 if (!dev_handle)
14502                         return;
14503                 flow->dev_handles = dev_handle->next.next;
14504                 while (dev_handle->flex_item) {
14505                         int index = rte_bsf32(dev_handle->flex_item);
14506
14507                         mlx5_flex_release_index(dev, index);
14508                         dev_handle->flex_item &= ~RTE_BIT32(index);
14509                 }
14510                 if (dev_handle->dvh.matcher)
14511                         flow_dv_matcher_release(dev, dev_handle);
14512                 if (dev_handle->dvh.rix_sample)
14513                         flow_dv_sample_resource_release(dev, dev_handle);
14514                 if (dev_handle->dvh.rix_dest_array)
14515                         flow_dv_dest_array_resource_release(dev, dev_handle);
14516                 if (dev_handle->dvh.rix_encap_decap)
14517                         flow_dv_encap_decap_resource_release(dev,
14518                                 dev_handle->dvh.rix_encap_decap);
14519                 if (dev_handle->dvh.modify_hdr)
14520                         flow_dv_modify_hdr_resource_release(dev, dev_handle);
14521                 if (dev_handle->dvh.rix_push_vlan)
14522                         flow_dv_push_vlan_action_resource_release(dev,
14523                                                                   dev_handle);
14524                 if (dev_handle->dvh.rix_tag)
14525                         flow_dv_tag_release(dev,
14526                                             dev_handle->dvh.rix_tag);
14527                 if (dev_handle->fate_action != MLX5_FLOW_FATE_SHARED_RSS)
14528                         flow_dv_fate_resource_release(dev, dev_handle);
14529                 else if (!srss)
14530                         srss = dev_handle->rix_srss;
14531                 if (fm && dev_handle->is_meter_flow_id &&
14532                     dev_handle->split_flow_id)
14533                         mlx5_ipool_free(fm->flow_ipool,
14534                                         dev_handle->split_flow_id);
14535                 else if (dev_handle->split_flow_id &&
14536                     !dev_handle->is_meter_flow_id)
14537                         mlx5_ipool_free(priv->sh->ipool
14538                                         [MLX5_IPOOL_RSS_EXPANTION_FLOW_ID],
14539                                         dev_handle->split_flow_id);
14540                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
14541                            tmp_idx);
14542         }
14543         if (srss)
14544                 flow_dv_shared_rss_action_release(dev, srss);
14545 }
14546
14547 /**
14548  * Release array of hash RX queue objects.
14549  * Helper function.
14550  *
14551  * @param[in] dev
14552  *   Pointer to the Ethernet device structure.
14553  * @param[in, out] hrxqs
14554  *   Array of hash RX queue objects.
14555  *
14556  * @return
14557  *   Total number of references to hash RX queue objects in *hrxqs* array
14558  *   after this operation.
14559  */
14560 static int
14561 __flow_dv_hrxqs_release(struct rte_eth_dev *dev,
14562                         uint32_t (*hrxqs)[MLX5_RSS_HASH_FIELDS_LEN])
14563 {
14564         size_t i;
14565         int remaining = 0;
14566
14567         for (i = 0; i < RTE_DIM(*hrxqs); i++) {
14568                 int ret = mlx5_hrxq_release(dev, (*hrxqs)[i]);
14569
14570                 if (!ret)
14571                         (*hrxqs)[i] = 0;
14572                 remaining += ret;
14573         }
14574         return remaining;
14575 }
14576
14577 /**
14578  * Release all hash RX queue objects representing shared RSS action.
14579  *
14580  * @param[in] dev
14581  *   Pointer to the Ethernet device structure.
14582  * @param[in, out] action
14583  *   Shared RSS action to remove hash RX queue objects from.
14584  *
14585  * @return
14586  *   Total number of references to hash RX queue objects stored in *action*
14587  *   after this operation.
14588  *   Expected to be 0 if no external references held.
14589  */
14590 static int
14591 __flow_dv_action_rss_hrxqs_release(struct rte_eth_dev *dev,
14592                                  struct mlx5_shared_action_rss *shared_rss)
14593 {
14594         return __flow_dv_hrxqs_release(dev, &shared_rss->hrxq);
14595 }
14596
14597 /**
14598  * Adjust L3/L4 hash value of pre-created shared RSS hrxq according to
14599  * user input.
14600  *
14601  * Only one hash value is available for one L3+L4 combination:
14602  * for example:
14603  * MLX5_RSS_HASH_IPV4, MLX5_RSS_HASH_IPV4_SRC_ONLY, and
14604  * MLX5_RSS_HASH_IPV4_DST_ONLY are mutually exclusive so they can share
14605  * same slot in mlx5_rss_hash_fields.
14606  *
14607  * @param[in] rss
14608  *   Pointer to the shared action RSS conf.
14609  * @param[in, out] hash_field
14610  *   hash_field variable needed to be adjusted.
14611  *
14612  * @return
14613  *   void
14614  */
14615 static void
14616 __flow_dv_action_rss_l34_hash_adjust(struct mlx5_shared_action_rss *rss,
14617                                      uint64_t *hash_field)
14618 {
14619         uint64_t rss_types = rss->origin.types;
14620
14621         switch (*hash_field & ~IBV_RX_HASH_INNER) {
14622         case MLX5_RSS_HASH_IPV4:
14623                 if (rss_types & MLX5_IPV4_LAYER_TYPES) {
14624                         *hash_field &= ~MLX5_RSS_HASH_IPV4;
14625                         if (rss_types & RTE_ETH_RSS_L3_DST_ONLY)
14626                                 *hash_field |= IBV_RX_HASH_DST_IPV4;
14627                         else if (rss_types & RTE_ETH_RSS_L3_SRC_ONLY)
14628                                 *hash_field |= IBV_RX_HASH_SRC_IPV4;
14629                         else
14630                                 *hash_field |= MLX5_RSS_HASH_IPV4;
14631                 }
14632                 return;
14633         case MLX5_RSS_HASH_IPV6:
14634                 if (rss_types & MLX5_IPV6_LAYER_TYPES) {
14635                         *hash_field &= ~MLX5_RSS_HASH_IPV6;
14636                         if (rss_types & RTE_ETH_RSS_L3_DST_ONLY)
14637                                 *hash_field |= IBV_RX_HASH_DST_IPV6;
14638                         else if (rss_types & RTE_ETH_RSS_L3_SRC_ONLY)
14639                                 *hash_field |= IBV_RX_HASH_SRC_IPV6;
14640                         else
14641                                 *hash_field |= MLX5_RSS_HASH_IPV6;
14642                 }
14643                 return;
14644         case MLX5_RSS_HASH_IPV4_UDP:
14645                 /* fall-through. */
14646         case MLX5_RSS_HASH_IPV6_UDP:
14647                 if (rss_types & RTE_ETH_RSS_UDP) {
14648                         *hash_field &= ~MLX5_UDP_IBV_RX_HASH;
14649                         if (rss_types & RTE_ETH_RSS_L4_DST_ONLY)
14650                                 *hash_field |= IBV_RX_HASH_DST_PORT_UDP;
14651                         else if (rss_types & RTE_ETH_RSS_L4_SRC_ONLY)
14652                                 *hash_field |= IBV_RX_HASH_SRC_PORT_UDP;
14653                         else
14654                                 *hash_field |= MLX5_UDP_IBV_RX_HASH;
14655                 }
14656                 return;
14657         case MLX5_RSS_HASH_IPV4_TCP:
14658                 /* fall-through. */
14659         case MLX5_RSS_HASH_IPV6_TCP:
14660                 if (rss_types & RTE_ETH_RSS_TCP) {
14661                         *hash_field &= ~MLX5_TCP_IBV_RX_HASH;
14662                         if (rss_types & RTE_ETH_RSS_L4_DST_ONLY)
14663                                 *hash_field |= IBV_RX_HASH_DST_PORT_TCP;
14664                         else if (rss_types & RTE_ETH_RSS_L4_SRC_ONLY)
14665                                 *hash_field |= IBV_RX_HASH_SRC_PORT_TCP;
14666                         else
14667                                 *hash_field |= MLX5_TCP_IBV_RX_HASH;
14668                 }
14669                 return;
14670         default:
14671                 return;
14672         }
14673 }
14674
14675 /**
14676  * Setup shared RSS action.
14677  * Prepare set of hash RX queue objects sufficient to handle all valid
14678  * hash_fields combinations (see enum ibv_rx_hash_fields).
14679  *
14680  * @param[in] dev
14681  *   Pointer to the Ethernet device structure.
14682  * @param[in] action_idx
14683  *   Shared RSS action ipool index.
14684  * @param[in, out] action
14685  *   Partially initialized shared RSS action.
14686  * @param[out] error
14687  *   Perform verbose error reporting if not NULL. Initialized in case of
14688  *   error only.
14689  *
14690  * @return
14691  *   0 on success, otherwise negative errno value.
14692  */
14693 static int
14694 __flow_dv_action_rss_setup(struct rte_eth_dev *dev,
14695                            uint32_t action_idx,
14696                            struct mlx5_shared_action_rss *shared_rss,
14697                            struct rte_flow_error *error)
14698 {
14699         struct mlx5_flow_rss_desc rss_desc = { 0 };
14700         size_t i;
14701         int err;
14702
14703         if (mlx5_ind_table_obj_setup(dev, shared_rss->ind_tbl,
14704                                      !!dev->data->dev_started)) {
14705                 return rte_flow_error_set(error, rte_errno,
14706                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
14707                                           "cannot setup indirection table");
14708         }
14709         memcpy(rss_desc.key, shared_rss->origin.key, MLX5_RSS_HASH_KEY_LEN);
14710         rss_desc.key_len = MLX5_RSS_HASH_KEY_LEN;
14711         rss_desc.const_q = shared_rss->origin.queue;
14712         rss_desc.queue_num = shared_rss->origin.queue_num;
14713         /* Set non-zero value to indicate a shared RSS. */
14714         rss_desc.shared_rss = action_idx;
14715         rss_desc.ind_tbl = shared_rss->ind_tbl;
14716         for (i = 0; i < MLX5_RSS_HASH_FIELDS_LEN; i++) {
14717                 uint32_t hrxq_idx;
14718                 uint64_t hash_fields = mlx5_rss_hash_fields[i];
14719                 int tunnel = 0;
14720
14721                 __flow_dv_action_rss_l34_hash_adjust(shared_rss, &hash_fields);
14722                 if (shared_rss->origin.level > 1) {
14723                         hash_fields |= IBV_RX_HASH_INNER;
14724                         tunnel = 1;
14725                 }
14726                 rss_desc.tunnel = tunnel;
14727                 rss_desc.hash_fields = hash_fields;
14728                 hrxq_idx = mlx5_hrxq_get(dev, &rss_desc);
14729                 if (!hrxq_idx) {
14730                         rte_flow_error_set
14731                                 (error, rte_errno,
14732                                  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
14733                                  "cannot get hash queue");
14734                         goto error_hrxq_new;
14735                 }
14736                 err = __flow_dv_action_rss_hrxq_set
14737                         (shared_rss, hash_fields, hrxq_idx);
14738                 MLX5_ASSERT(!err);
14739         }
14740         return 0;
14741 error_hrxq_new:
14742         err = rte_errno;
14743         __flow_dv_action_rss_hrxqs_release(dev, shared_rss);
14744         if (!mlx5_ind_table_obj_release(dev, shared_rss->ind_tbl, true, true))
14745                 shared_rss->ind_tbl = NULL;
14746         rte_errno = err;
14747         return -rte_errno;
14748 }
14749
14750 /**
14751  * Create shared RSS action.
14752  *
14753  * @param[in] dev
14754  *   Pointer to the Ethernet device structure.
14755  * @param[in] conf
14756  *   Shared action configuration.
14757  * @param[in] rss
14758  *   RSS action specification used to create shared action.
14759  * @param[out] error
14760  *   Perform verbose error reporting if not NULL. Initialized in case of
14761  *   error only.
14762  *
14763  * @return
14764  *   A valid shared action ID in case of success, 0 otherwise and
14765  *   rte_errno is set.
14766  */
14767 static uint32_t
14768 __flow_dv_action_rss_create(struct rte_eth_dev *dev,
14769                             const struct rte_flow_indir_action_conf *conf,
14770                             const struct rte_flow_action_rss *rss,
14771                             struct rte_flow_error *error)
14772 {
14773         struct mlx5_priv *priv = dev->data->dev_private;
14774         struct mlx5_shared_action_rss *shared_rss = NULL;
14775         void *queue = NULL;
14776         struct rte_flow_action_rss *origin;
14777         const uint8_t *rss_key;
14778         uint32_t queue_size = rss->queue_num * sizeof(uint16_t);
14779         uint32_t idx;
14780
14781         RTE_SET_USED(conf);
14782         queue = mlx5_malloc(0, RTE_ALIGN_CEIL(queue_size, sizeof(void *)),
14783                             0, SOCKET_ID_ANY);
14784         shared_rss = mlx5_ipool_zmalloc
14785                          (priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS], &idx);
14786         if (!shared_rss || !queue) {
14787                 rte_flow_error_set(error, ENOMEM,
14788                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
14789                                    "cannot allocate resource memory");
14790                 goto error_rss_init;
14791         }
14792         if (idx > (1u << MLX5_INDIRECT_ACTION_TYPE_OFFSET)) {
14793                 rte_flow_error_set(error, E2BIG,
14794                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
14795                                    "rss action number out of range");
14796                 goto error_rss_init;
14797         }
14798         shared_rss->ind_tbl = mlx5_malloc(MLX5_MEM_ZERO,
14799                                           sizeof(*shared_rss->ind_tbl),
14800                                           0, SOCKET_ID_ANY);
14801         if (!shared_rss->ind_tbl) {
14802                 rte_flow_error_set(error, ENOMEM,
14803                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
14804                                    "cannot allocate resource memory");
14805                 goto error_rss_init;
14806         }
14807         memcpy(queue, rss->queue, queue_size);
14808         shared_rss->ind_tbl->queues = queue;
14809         shared_rss->ind_tbl->queues_n = rss->queue_num;
14810         origin = &shared_rss->origin;
14811         origin->func = rss->func;
14812         origin->level = rss->level;
14813         /* RSS type 0 indicates default RSS type (RTE_ETH_RSS_IP). */
14814         origin->types = !rss->types ? RTE_ETH_RSS_IP : rss->types;
14815         /* NULL RSS key indicates default RSS key. */
14816         rss_key = !rss->key ? rss_hash_default_key : rss->key;
14817         memcpy(shared_rss->key, rss_key, MLX5_RSS_HASH_KEY_LEN);
14818         origin->key = &shared_rss->key[0];
14819         origin->key_len = MLX5_RSS_HASH_KEY_LEN;
14820         origin->queue = queue;
14821         origin->queue_num = rss->queue_num;
14822         if (__flow_dv_action_rss_setup(dev, idx, shared_rss, error))
14823                 goto error_rss_init;
14824         rte_spinlock_init(&shared_rss->action_rss_sl);
14825         __atomic_add_fetch(&shared_rss->refcnt, 1, __ATOMIC_RELAXED);
14826         rte_spinlock_lock(&priv->shared_act_sl);
14827         ILIST_INSERT(priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS],
14828                      &priv->rss_shared_actions, idx, shared_rss, next);
14829         rte_spinlock_unlock(&priv->shared_act_sl);
14830         return idx;
14831 error_rss_init:
14832         if (shared_rss) {
14833                 if (shared_rss->ind_tbl)
14834                         mlx5_free(shared_rss->ind_tbl);
14835                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS],
14836                                 idx);
14837         }
14838         if (queue)
14839                 mlx5_free(queue);
14840         return 0;
14841 }
14842
14843 /**
14844  * Destroy the shared RSS action.
14845  * Release related hash RX queue objects.
14846  *
14847  * @param[in] dev
14848  *   Pointer to the Ethernet device structure.
14849  * @param[in] idx
14850  *   The shared RSS action object ID to be removed.
14851  * @param[out] error
14852  *   Perform verbose error reporting if not NULL. Initialized in case of
14853  *   error only.
14854  *
14855  * @return
14856  *   0 on success, otherwise negative errno value.
14857  */
14858 static int
14859 __flow_dv_action_rss_release(struct rte_eth_dev *dev, uint32_t idx,
14860                              struct rte_flow_error *error)
14861 {
14862         struct mlx5_priv *priv = dev->data->dev_private;
14863         struct mlx5_shared_action_rss *shared_rss =
14864             mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS], idx);
14865         uint32_t old_refcnt = 1;
14866         int remaining;
14867         uint16_t *queue = NULL;
14868
14869         if (!shared_rss)
14870                 return rte_flow_error_set(error, EINVAL,
14871                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
14872                                           "invalid shared action");
14873         if (!__atomic_compare_exchange_n(&shared_rss->refcnt, &old_refcnt,
14874                                          0, 0, __ATOMIC_ACQUIRE,
14875                                          __ATOMIC_RELAXED))
14876                 return rte_flow_error_set(error, EBUSY,
14877                                           RTE_FLOW_ERROR_TYPE_ACTION,
14878                                           NULL,
14879                                           "shared rss has references");
14880         remaining = __flow_dv_action_rss_hrxqs_release(dev, shared_rss);
14881         if (remaining)
14882                 return rte_flow_error_set(error, EBUSY,
14883                                           RTE_FLOW_ERROR_TYPE_ACTION,
14884                                           NULL,
14885                                           "shared rss hrxq has references");
14886         queue = shared_rss->ind_tbl->queues;
14887         remaining = mlx5_ind_table_obj_release(dev, shared_rss->ind_tbl, true,
14888                                                !!dev->data->dev_started);
14889         if (remaining)
14890                 return rte_flow_error_set(error, EBUSY,
14891                                           RTE_FLOW_ERROR_TYPE_ACTION,
14892                                           NULL,
14893                                           "shared rss indirection table has"
14894                                           " references");
14895         mlx5_free(queue);
14896         rte_spinlock_lock(&priv->shared_act_sl);
14897         ILIST_REMOVE(priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS],
14898                      &priv->rss_shared_actions, idx, shared_rss, next);
14899         rte_spinlock_unlock(&priv->shared_act_sl);
14900         mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS],
14901                         idx);
14902         return 0;
14903 }
14904
14905 /**
14906  * Create indirect action, lock free,
14907  * (mutex should be acquired by caller).
14908  * Dispatcher for action type specific call.
14909  *
14910  * @param[in] dev
14911  *   Pointer to the Ethernet device structure.
14912  * @param[in] conf
14913  *   Shared action configuration.
14914  * @param[in] action
14915  *   Action specification used to create indirect action.
14916  * @param[out] error
14917  *   Perform verbose error reporting if not NULL. Initialized in case of
14918  *   error only.
14919  *
14920  * @return
14921  *   A valid shared action handle in case of success, NULL otherwise and
14922  *   rte_errno is set.
14923  */
14924 static struct rte_flow_action_handle *
14925 flow_dv_action_create(struct rte_eth_dev *dev,
14926                       const struct rte_flow_indir_action_conf *conf,
14927                       const struct rte_flow_action *action,
14928                       struct rte_flow_error *err)
14929 {
14930         struct mlx5_priv *priv = dev->data->dev_private;
14931         uint32_t age_idx = 0;
14932         uint32_t idx = 0;
14933         uint32_t ret = 0;
14934
14935         switch (action->type) {
14936         case RTE_FLOW_ACTION_TYPE_RSS:
14937                 ret = __flow_dv_action_rss_create(dev, conf, action->conf, err);
14938                 idx = (MLX5_INDIRECT_ACTION_TYPE_RSS <<
14939                        MLX5_INDIRECT_ACTION_TYPE_OFFSET) | ret;
14940                 break;
14941         case RTE_FLOW_ACTION_TYPE_AGE:
14942                 age_idx = flow_dv_aso_age_alloc(dev, err);
14943                 if (!age_idx) {
14944                         ret = -rte_errno;
14945                         break;
14946                 }
14947                 idx = (MLX5_INDIRECT_ACTION_TYPE_AGE <<
14948                        MLX5_INDIRECT_ACTION_TYPE_OFFSET) | age_idx;
14949                 flow_dv_aso_age_params_init(dev, age_idx,
14950                                         ((const struct rte_flow_action_age *)
14951                                                 action->conf)->context ?
14952                                         ((const struct rte_flow_action_age *)
14953                                                 action->conf)->context :
14954                                         (void *)(uintptr_t)idx,
14955                                         ((const struct rte_flow_action_age *)
14956                                                 action->conf)->timeout);
14957                 ret = age_idx;
14958                 break;
14959         case RTE_FLOW_ACTION_TYPE_COUNT:
14960                 ret = flow_dv_translate_create_counter(dev, NULL, NULL, NULL);
14961                 idx = (MLX5_INDIRECT_ACTION_TYPE_COUNT <<
14962                        MLX5_INDIRECT_ACTION_TYPE_OFFSET) | ret;
14963                 break;
14964         case RTE_FLOW_ACTION_TYPE_CONNTRACK:
14965                 ret = flow_dv_translate_create_conntrack(dev, action->conf,
14966                                                          err);
14967                 idx = MLX5_INDIRECT_ACT_CT_GEN_IDX(PORT_ID(priv), ret);
14968                 break;
14969         default:
14970                 rte_flow_error_set(err, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
14971                                    NULL, "action type not supported");
14972                 break;
14973         }
14974         return ret ? (struct rte_flow_action_handle *)(uintptr_t)idx : NULL;
14975 }
14976
14977 /**
14978  * Destroy the indirect action.
14979  * Release action related resources on the NIC and the memory.
14980  * Lock free, (mutex should be acquired by caller).
14981  * Dispatcher for action type specific call.
14982  *
14983  * @param[in] dev
14984  *   Pointer to the Ethernet device structure.
14985  * @param[in] handle
14986  *   The indirect action object handle to be removed.
14987  * @param[out] error
14988  *   Perform verbose error reporting if not NULL. Initialized in case of
14989  *   error only.
14990  *
14991  * @return
14992  *   0 on success, otherwise negative errno value.
14993  */
14994 static int
14995 flow_dv_action_destroy(struct rte_eth_dev *dev,
14996                        struct rte_flow_action_handle *handle,
14997                        struct rte_flow_error *error)
14998 {
14999         uint32_t act_idx = (uint32_t)(uintptr_t)handle;
15000         uint32_t type = act_idx >> MLX5_INDIRECT_ACTION_TYPE_OFFSET;
15001         uint32_t idx = act_idx & ((1u << MLX5_INDIRECT_ACTION_TYPE_OFFSET) - 1);
15002         struct mlx5_flow_counter *cnt;
15003         uint32_t no_flow_refcnt = 1;
15004         int ret;
15005
15006         switch (type) {
15007         case MLX5_INDIRECT_ACTION_TYPE_RSS:
15008                 return __flow_dv_action_rss_release(dev, idx, error);
15009         case MLX5_INDIRECT_ACTION_TYPE_COUNT:
15010                 cnt = flow_dv_counter_get_by_idx(dev, idx, NULL);
15011                 if (!__atomic_compare_exchange_n(&cnt->shared_info.refcnt,
15012                                                  &no_flow_refcnt, 1, false,
15013                                                  __ATOMIC_ACQUIRE,
15014                                                  __ATOMIC_RELAXED))
15015                         return rte_flow_error_set(error, EBUSY,
15016                                                   RTE_FLOW_ERROR_TYPE_ACTION,
15017                                                   NULL,
15018                                                   "Indirect count action has references");
15019                 flow_dv_counter_free(dev, idx);
15020                 return 0;
15021         case MLX5_INDIRECT_ACTION_TYPE_AGE:
15022                 ret = flow_dv_aso_age_release(dev, idx);
15023                 if (ret)
15024                         /*
15025                          * In this case, the last flow has a reference will
15026                          * actually release the age action.
15027                          */
15028                         DRV_LOG(DEBUG, "Indirect age action %" PRIu32 " was"
15029                                 " released with references %d.", idx, ret);
15030                 return 0;
15031         case MLX5_INDIRECT_ACTION_TYPE_CT:
15032                 ret = flow_dv_aso_ct_release(dev, idx, error);
15033                 if (ret < 0)
15034                         return ret;
15035                 if (ret > 0)
15036                         DRV_LOG(DEBUG, "Connection tracking object %u still "
15037                                 "has references %d.", idx, ret);
15038                 return 0;
15039         default:
15040                 return rte_flow_error_set(error, ENOTSUP,
15041                                           RTE_FLOW_ERROR_TYPE_ACTION,
15042                                           NULL,
15043                                           "action type not supported");
15044         }
15045 }
15046
15047 /**
15048  * Updates in place shared RSS action configuration.
15049  *
15050  * @param[in] dev
15051  *   Pointer to the Ethernet device structure.
15052  * @param[in] idx
15053  *   The shared RSS action object ID to be updated.
15054  * @param[in] action_conf
15055  *   RSS action specification used to modify *shared_rss*.
15056  * @param[out] error
15057  *   Perform verbose error reporting if not NULL. Initialized in case of
15058  *   error only.
15059  *
15060  * @return
15061  *   0 on success, otherwise negative errno value.
15062  * @note: currently only support update of RSS queues.
15063  */
15064 static int
15065 __flow_dv_action_rss_update(struct rte_eth_dev *dev, uint32_t idx,
15066                             const struct rte_flow_action_rss *action_conf,
15067                             struct rte_flow_error *error)
15068 {
15069         struct mlx5_priv *priv = dev->data->dev_private;
15070         struct mlx5_shared_action_rss *shared_rss =
15071             mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS], idx);
15072         int ret = 0;
15073         void *queue = NULL;
15074         uint16_t *queue_old = NULL;
15075         uint32_t queue_size = action_conf->queue_num * sizeof(uint16_t);
15076         bool dev_started = !!dev->data->dev_started;
15077
15078         if (!shared_rss)
15079                 return rte_flow_error_set(error, EINVAL,
15080                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
15081                                           "invalid shared action to update");
15082         if (priv->obj_ops.ind_table_modify == NULL)
15083                 return rte_flow_error_set(error, ENOTSUP,
15084                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
15085                                           "cannot modify indirection table");
15086         queue = mlx5_malloc(MLX5_MEM_ZERO,
15087                             RTE_ALIGN_CEIL(queue_size, sizeof(void *)),
15088                             0, SOCKET_ID_ANY);
15089         if (!queue)
15090                 return rte_flow_error_set(error, ENOMEM,
15091                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
15092                                           NULL,
15093                                           "cannot allocate resource memory");
15094         memcpy(queue, action_conf->queue, queue_size);
15095         MLX5_ASSERT(shared_rss->ind_tbl);
15096         rte_spinlock_lock(&shared_rss->action_rss_sl);
15097         queue_old = shared_rss->ind_tbl->queues;
15098         ret = mlx5_ind_table_obj_modify(dev, shared_rss->ind_tbl,
15099                                         queue, action_conf->queue_num,
15100                                         true /* standalone */,
15101                                         dev_started /* ref_new_qs */,
15102                                         dev_started /* deref_old_qs */);
15103         if (ret) {
15104                 mlx5_free(queue);
15105                 ret = rte_flow_error_set(error, rte_errno,
15106                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
15107                                           "cannot update indirection table");
15108         } else {
15109                 mlx5_free(queue_old);
15110                 shared_rss->origin.queue = queue;
15111                 shared_rss->origin.queue_num = action_conf->queue_num;
15112         }
15113         rte_spinlock_unlock(&shared_rss->action_rss_sl);
15114         return ret;
15115 }
15116
15117 /*
15118  * Updates in place conntrack context or direction.
15119  * Context update should be synchronized.
15120  *
15121  * @param[in] dev
15122  *   Pointer to the Ethernet device structure.
15123  * @param[in] idx
15124  *   The conntrack object ID to be updated.
15125  * @param[in] update
15126  *   Pointer to the structure of information to update.
15127  * @param[out] error
15128  *   Perform verbose error reporting if not NULL. Initialized in case of
15129  *   error only.
15130  *
15131  * @return
15132  *   0 on success, otherwise negative errno value.
15133  */
15134 static int
15135 __flow_dv_action_ct_update(struct rte_eth_dev *dev, uint32_t idx,
15136                            const struct rte_flow_modify_conntrack *update,
15137                            struct rte_flow_error *error)
15138 {
15139         struct mlx5_priv *priv = dev->data->dev_private;
15140         struct mlx5_aso_ct_action *ct;
15141         const struct rte_flow_action_conntrack *new_prf;
15142         int ret = 0;
15143         uint16_t owner = (uint16_t)MLX5_INDIRECT_ACT_CT_GET_OWNER(idx);
15144         uint32_t dev_idx;
15145
15146         if (PORT_ID(priv) != owner)
15147                 return rte_flow_error_set(error, EACCES,
15148                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
15149                                           NULL,
15150                                           "CT object owned by another port");
15151         dev_idx = MLX5_INDIRECT_ACT_CT_GET_IDX(idx);
15152         ct = flow_aso_ct_get_by_dev_idx(dev, dev_idx);
15153         if (!ct->refcnt)
15154                 return rte_flow_error_set(error, ENOMEM,
15155                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
15156                                           NULL,
15157                                           "CT object is inactive");
15158         new_prf = &update->new_ct;
15159         if (update->direction)
15160                 ct->is_original = !!new_prf->is_original_dir;
15161         if (update->state) {
15162                 /* Only validate the profile when it needs to be updated. */
15163                 ret = mlx5_validate_action_ct(dev, new_prf, error);
15164                 if (ret)
15165                         return ret;
15166                 ret = mlx5_aso_ct_update_by_wqe(priv->sh, ct, new_prf);
15167                 if (ret)
15168                         return rte_flow_error_set(error, EIO,
15169                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
15170                                         NULL,
15171                                         "Failed to send CT context update WQE");
15172                 /* Block until ready or a failure. */
15173                 ret = mlx5_aso_ct_available(priv->sh, ct);
15174                 if (ret)
15175                         rte_flow_error_set(error, rte_errno,
15176                                            RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
15177                                            NULL,
15178                                            "Timeout to get the CT update");
15179         }
15180         return ret;
15181 }
15182
15183 /**
15184  * Updates in place shared action configuration, lock free,
15185  * (mutex should be acquired by caller).
15186  *
15187  * @param[in] dev
15188  *   Pointer to the Ethernet device structure.
15189  * @param[in] handle
15190  *   The indirect action object handle to be updated.
15191  * @param[in] update
15192  *   Action specification used to modify the action pointed by *handle*.
15193  *   *update* could be of same type with the action pointed by the *handle*
15194  *   handle argument, or some other structures like a wrapper, depending on
15195  *   the indirect action type.
15196  * @param[out] error
15197  *   Perform verbose error reporting if not NULL. Initialized in case of
15198  *   error only.
15199  *
15200  * @return
15201  *   0 on success, otherwise negative errno value.
15202  */
15203 static int
15204 flow_dv_action_update(struct rte_eth_dev *dev,
15205                         struct rte_flow_action_handle *handle,
15206                         const void *update,
15207                         struct rte_flow_error *err)
15208 {
15209         uint32_t act_idx = (uint32_t)(uintptr_t)handle;
15210         uint32_t type = act_idx >> MLX5_INDIRECT_ACTION_TYPE_OFFSET;
15211         uint32_t idx = act_idx & ((1u << MLX5_INDIRECT_ACTION_TYPE_OFFSET) - 1);
15212         const void *action_conf;
15213
15214         switch (type) {
15215         case MLX5_INDIRECT_ACTION_TYPE_RSS:
15216                 action_conf = ((const struct rte_flow_action *)update)->conf;
15217                 return __flow_dv_action_rss_update(dev, idx, action_conf, err);
15218         case MLX5_INDIRECT_ACTION_TYPE_CT:
15219                 return __flow_dv_action_ct_update(dev, idx, update, err);
15220         default:
15221                 return rte_flow_error_set(err, ENOTSUP,
15222                                           RTE_FLOW_ERROR_TYPE_ACTION,
15223                                           NULL,
15224                                           "action type update not supported");
15225         }
15226 }
15227
15228 /**
15229  * Destroy the meter sub policy table rules.
15230  * Lock free, (mutex should be acquired by caller).
15231  *
15232  * @param[in] dev
15233  *   Pointer to Ethernet device.
15234  * @param[in] sub_policy
15235  *   Pointer to meter sub policy table.
15236  */
15237 static void
15238 __flow_dv_destroy_sub_policy_rules(struct rte_eth_dev *dev,
15239                              struct mlx5_flow_meter_sub_policy *sub_policy)
15240 {
15241         struct mlx5_priv *priv = dev->data->dev_private;
15242         struct mlx5_flow_tbl_data_entry *tbl;
15243         struct mlx5_flow_meter_policy *policy = sub_policy->main_policy;
15244         struct mlx5_flow_meter_info *next_fm;
15245         struct mlx5_sub_policy_color_rule *color_rule;
15246         void *tmp;
15247         uint32_t i;
15248
15249         for (i = 0; i < RTE_COLORS; i++) {
15250                 next_fm = NULL;
15251                 if (i == RTE_COLOR_GREEN && policy &&
15252                     policy->act_cnt[i].fate_action == MLX5_FLOW_FATE_MTR)
15253                         next_fm = mlx5_flow_meter_find(priv,
15254                                         policy->act_cnt[i].next_mtr_id, NULL);
15255                 RTE_TAILQ_FOREACH_SAFE(color_rule, &sub_policy->color_rules[i],
15256                                    next_port, tmp) {
15257                         claim_zero(mlx5_flow_os_destroy_flow(color_rule->rule));
15258                         tbl = container_of(color_rule->matcher->tbl,
15259                                            typeof(*tbl), tbl);
15260                         mlx5_list_unregister(tbl->matchers,
15261                                              &color_rule->matcher->entry);
15262                         TAILQ_REMOVE(&sub_policy->color_rules[i],
15263                                      color_rule, next_port);
15264                         mlx5_free(color_rule);
15265                         if (next_fm)
15266                                 mlx5_flow_meter_detach(priv, next_fm);
15267                 }
15268         }
15269         for (i = 0; i < MLX5_MTR_RTE_COLORS; i++) {
15270                 if (sub_policy->rix_hrxq[i]) {
15271                         if (policy && !policy->is_hierarchy)
15272                                 mlx5_hrxq_release(dev, sub_policy->rix_hrxq[i]);
15273                         sub_policy->rix_hrxq[i] = 0;
15274                 }
15275                 if (sub_policy->jump_tbl[i]) {
15276                         flow_dv_tbl_resource_release(MLX5_SH(dev),
15277                                                      sub_policy->jump_tbl[i]);
15278                         sub_policy->jump_tbl[i] = NULL;
15279                 }
15280         }
15281         if (sub_policy->tbl_rsc) {
15282                 flow_dv_tbl_resource_release(MLX5_SH(dev),
15283                                              sub_policy->tbl_rsc);
15284                 sub_policy->tbl_rsc = NULL;
15285         }
15286 }
15287
15288 /**
15289  * Destroy policy rules, lock free,
15290  * (mutex should be acquired by caller).
15291  * Dispatcher for action type specific call.
15292  *
15293  * @param[in] dev
15294  *   Pointer to the Ethernet device structure.
15295  * @param[in] mtr_policy
15296  *   Meter policy struct.
15297  */
15298 static void
15299 flow_dv_destroy_policy_rules(struct rte_eth_dev *dev,
15300                              struct mlx5_flow_meter_policy *mtr_policy)
15301 {
15302         uint32_t i, j;
15303         struct mlx5_flow_meter_sub_policy *sub_policy;
15304         uint16_t sub_policy_num;
15305
15306         for (i = 0; i < MLX5_MTR_DOMAIN_MAX; i++) {
15307                 sub_policy_num = (mtr_policy->sub_policy_num >>
15308                         (MLX5_MTR_SUB_POLICY_NUM_SHIFT * i)) &
15309                         MLX5_MTR_SUB_POLICY_NUM_MASK;
15310                 for (j = 0; j < sub_policy_num; j++) {
15311                         sub_policy = mtr_policy->sub_policys[i][j];
15312                         if (sub_policy)
15313                                 __flow_dv_destroy_sub_policy_rules(dev,
15314                                                                    sub_policy);
15315                 }
15316         }
15317 }
15318
15319 /**
15320  * Destroy policy action, lock free,
15321  * (mutex should be acquired by caller).
15322  * Dispatcher for action type specific call.
15323  *
15324  * @param[in] dev
15325  *   Pointer to the Ethernet device structure.
15326  * @param[in] mtr_policy
15327  *   Meter policy struct.
15328  */
15329 static void
15330 flow_dv_destroy_mtr_policy_acts(struct rte_eth_dev *dev,
15331                       struct mlx5_flow_meter_policy *mtr_policy)
15332 {
15333         struct rte_flow_action *rss_action;
15334         struct mlx5_flow_handle dev_handle;
15335         uint32_t i, j;
15336
15337         for (i = 0; i < MLX5_MTR_RTE_COLORS; i++) {
15338                 if (mtr_policy->act_cnt[i].rix_mark) {
15339                         flow_dv_tag_release(dev,
15340                                 mtr_policy->act_cnt[i].rix_mark);
15341                         mtr_policy->act_cnt[i].rix_mark = 0;
15342                 }
15343                 if (mtr_policy->act_cnt[i].modify_hdr) {
15344                         dev_handle.dvh.modify_hdr =
15345                                 mtr_policy->act_cnt[i].modify_hdr;
15346                         flow_dv_modify_hdr_resource_release(dev, &dev_handle);
15347                 }
15348                 switch (mtr_policy->act_cnt[i].fate_action) {
15349                 case MLX5_FLOW_FATE_SHARED_RSS:
15350                         rss_action = mtr_policy->act_cnt[i].rss;
15351                         mlx5_free(rss_action);
15352                         break;
15353                 case MLX5_FLOW_FATE_PORT_ID:
15354                         if (mtr_policy->act_cnt[i].rix_port_id_action) {
15355                                 flow_dv_port_id_action_resource_release(dev,
15356                                 mtr_policy->act_cnt[i].rix_port_id_action);
15357                                 mtr_policy->act_cnt[i].rix_port_id_action = 0;
15358                         }
15359                         break;
15360                 case MLX5_FLOW_FATE_DROP:
15361                 case MLX5_FLOW_FATE_JUMP:
15362                         for (j = 0; j < MLX5_MTR_DOMAIN_MAX; j++)
15363                                 mtr_policy->act_cnt[i].dr_jump_action[j] =
15364                                                 NULL;
15365                         break;
15366                 default:
15367                         /*Queue action do nothing*/
15368                         break;
15369                 }
15370         }
15371         for (j = 0; j < MLX5_MTR_DOMAIN_MAX; j++)
15372                 mtr_policy->dr_drop_action[j] = NULL;
15373 }
15374
15375 /**
15376  * Create policy action per domain, lock free,
15377  * (mutex should be acquired by caller).
15378  * Dispatcher for action type specific call.
15379  *
15380  * @param[in] dev
15381  *   Pointer to the Ethernet device structure.
15382  * @param[in] mtr_policy
15383  *   Meter policy struct.
15384  * @param[in] action
15385  *   Action specification used to create meter actions.
15386  * @param[out] error
15387  *   Perform verbose error reporting if not NULL. Initialized in case of
15388  *   error only.
15389  *
15390  * @return
15391  *   0 on success, otherwise negative errno value.
15392  */
15393 static int
15394 __flow_dv_create_domain_policy_acts(struct rte_eth_dev *dev,
15395                         struct mlx5_flow_meter_policy *mtr_policy,
15396                         const struct rte_flow_action *actions[RTE_COLORS],
15397                         enum mlx5_meter_domain domain,
15398                         struct rte_mtr_error *error)
15399 {
15400         struct mlx5_priv *priv = dev->data->dev_private;
15401         struct rte_flow_error flow_err;
15402         const struct rte_flow_action *act;
15403         uint64_t action_flags;
15404         struct mlx5_flow_handle dh;
15405         struct mlx5_flow dev_flow;
15406         struct mlx5_flow_dv_port_id_action_resource port_id_action;
15407         int i, ret;
15408         uint8_t egress, transfer;
15409         struct mlx5_meter_policy_action_container *act_cnt = NULL;
15410         union {
15411                 struct mlx5_flow_dv_modify_hdr_resource res;
15412                 uint8_t len[sizeof(struct mlx5_flow_dv_modify_hdr_resource) +
15413                             sizeof(struct mlx5_modification_cmd) *
15414                             (MLX5_MAX_MODIFY_NUM + 1)];
15415         } mhdr_dummy;
15416         struct mlx5_flow_dv_modify_hdr_resource *mhdr_res = &mhdr_dummy.res;
15417
15418         egress = (domain == MLX5_MTR_DOMAIN_EGRESS) ? 1 : 0;
15419         transfer = (domain == MLX5_MTR_DOMAIN_TRANSFER) ? 1 : 0;
15420         memset(&dh, 0, sizeof(struct mlx5_flow_handle));
15421         memset(&dev_flow, 0, sizeof(struct mlx5_flow));
15422         memset(&port_id_action, 0,
15423                sizeof(struct mlx5_flow_dv_port_id_action_resource));
15424         memset(mhdr_res, 0, sizeof(*mhdr_res));
15425         mhdr_res->ft_type = transfer ? MLX5DV_FLOW_TABLE_TYPE_FDB :
15426                                        (egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
15427                                         MLX5DV_FLOW_TABLE_TYPE_NIC_RX);
15428         dev_flow.handle = &dh;
15429         dev_flow.dv.port_id_action = &port_id_action;
15430         dev_flow.external = true;
15431         for (i = 0; i < RTE_COLORS; i++) {
15432                 if (i < MLX5_MTR_RTE_COLORS)
15433                         act_cnt = &mtr_policy->act_cnt[i];
15434                 /* Skip the color policy actions creation. */
15435                 if ((i == RTE_COLOR_YELLOW && mtr_policy->skip_y) ||
15436                     (i == RTE_COLOR_GREEN && mtr_policy->skip_g))
15437                         continue;
15438                 action_flags = 0;
15439                 for (act = actions[i];
15440                      act && act->type != RTE_FLOW_ACTION_TYPE_END; act++) {
15441                         switch (act->type) {
15442                         case RTE_FLOW_ACTION_TYPE_MARK:
15443                         {
15444                                 uint32_t tag_be = mlx5_flow_mark_set
15445                                         (((const struct rte_flow_action_mark *)
15446                                         (act->conf))->id);
15447
15448                                 if (i >= MLX5_MTR_RTE_COLORS)
15449                                         return -rte_mtr_error_set(error,
15450                                           ENOTSUP,
15451                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
15452                                           NULL,
15453                                           "cannot create policy "
15454                                           "mark action for this color");
15455                                 dev_flow.handle->mark = 1;
15456                                 if (flow_dv_tag_resource_register(dev, tag_be,
15457                                                   &dev_flow, &flow_err))
15458                                         return -rte_mtr_error_set(error,
15459                                         ENOTSUP,
15460                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
15461                                         NULL,
15462                                         "cannot setup policy mark action");
15463                                 MLX5_ASSERT(dev_flow.dv.tag_resource);
15464                                 act_cnt->rix_mark =
15465                                         dev_flow.handle->dvh.rix_tag;
15466                                 action_flags |= MLX5_FLOW_ACTION_MARK;
15467                                 break;
15468                         }
15469                         case RTE_FLOW_ACTION_TYPE_SET_TAG:
15470                                 if (i >= MLX5_MTR_RTE_COLORS)
15471                                         return -rte_mtr_error_set(error,
15472                                           ENOTSUP,
15473                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
15474                                           NULL,
15475                                           "cannot create policy "
15476                                           "set tag action for this color");
15477                                 if (flow_dv_convert_action_set_tag
15478                                 (dev, mhdr_res,
15479                                 (const struct rte_flow_action_set_tag *)
15480                                 act->conf,  &flow_err))
15481                                         return -rte_mtr_error_set(error,
15482                                         ENOTSUP,
15483                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
15484                                         NULL, "cannot convert policy "
15485                                         "set tag action");
15486                                 if (!mhdr_res->actions_num)
15487                                         return -rte_mtr_error_set(error,
15488                                         ENOTSUP,
15489                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
15490                                         NULL, "cannot find policy "
15491                                         "set tag action");
15492                                 action_flags |= MLX5_FLOW_ACTION_SET_TAG;
15493                                 break;
15494                         case RTE_FLOW_ACTION_TYPE_DROP:
15495                         {
15496                                 struct mlx5_flow_mtr_mng *mtrmng =
15497                                                 priv->sh->mtrmng;
15498                                 struct mlx5_flow_tbl_data_entry *tbl_data;
15499
15500                                 /*
15501                                  * Create the drop table with
15502                                  * METER DROP level.
15503                                  */
15504                                 if (!mtrmng->drop_tbl[domain]) {
15505                                         mtrmng->drop_tbl[domain] =
15506                                         flow_dv_tbl_resource_get(dev,
15507                                         MLX5_FLOW_TABLE_LEVEL_METER,
15508                                         egress, transfer, false, NULL, 0,
15509                                         0, MLX5_MTR_TABLE_ID_DROP, &flow_err);
15510                                         if (!mtrmng->drop_tbl[domain])
15511                                                 return -rte_mtr_error_set
15512                                         (error, ENOTSUP,
15513                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
15514                                         NULL,
15515                                         "Failed to create meter drop table");
15516                                 }
15517                                 tbl_data = container_of
15518                                 (mtrmng->drop_tbl[domain],
15519                                 struct mlx5_flow_tbl_data_entry, tbl);
15520                                 if (i < MLX5_MTR_RTE_COLORS) {
15521                                         act_cnt->dr_jump_action[domain] =
15522                                                 tbl_data->jump.action;
15523                                         act_cnt->fate_action =
15524                                                 MLX5_FLOW_FATE_DROP;
15525                                 }
15526                                 if (i == RTE_COLOR_RED)
15527                                         mtr_policy->dr_drop_action[domain] =
15528                                                 tbl_data->jump.action;
15529                                 action_flags |= MLX5_FLOW_ACTION_DROP;
15530                                 break;
15531                         }
15532                         case RTE_FLOW_ACTION_TYPE_QUEUE:
15533                         {
15534                                 if (i >= MLX5_MTR_RTE_COLORS)
15535                                         return -rte_mtr_error_set(error,
15536                                         ENOTSUP,
15537                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
15538                                         NULL, "cannot create policy "
15539                                         "fate queue for this color");
15540                                 act_cnt->queue =
15541                                 ((const struct rte_flow_action_queue *)
15542                                         (act->conf))->index;
15543                                 act_cnt->fate_action =
15544                                         MLX5_FLOW_FATE_QUEUE;
15545                                 dev_flow.handle->fate_action =
15546                                         MLX5_FLOW_FATE_QUEUE;
15547                                 mtr_policy->is_queue = 1;
15548                                 action_flags |= MLX5_FLOW_ACTION_QUEUE;
15549                                 break;
15550                         }
15551                         case RTE_FLOW_ACTION_TYPE_RSS:
15552                         {
15553                                 int rss_size;
15554
15555                                 if (i >= MLX5_MTR_RTE_COLORS)
15556                                         return -rte_mtr_error_set(error,
15557                                           ENOTSUP,
15558                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
15559                                           NULL,
15560                                           "cannot create policy "
15561                                           "rss action for this color");
15562                                 /*
15563                                  * Save RSS conf into policy struct
15564                                  * for translate stage.
15565                                  */
15566                                 rss_size = (int)rte_flow_conv
15567                                         (RTE_FLOW_CONV_OP_ACTION,
15568                                         NULL, 0, act, &flow_err);
15569                                 if (rss_size <= 0)
15570                                         return -rte_mtr_error_set(error,
15571                                           ENOTSUP,
15572                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
15573                                           NULL, "Get the wrong "
15574                                           "rss action struct size");
15575                                 act_cnt->rss = mlx5_malloc(MLX5_MEM_ZERO,
15576                                                 rss_size, 0, SOCKET_ID_ANY);
15577                                 if (!act_cnt->rss)
15578                                         return -rte_mtr_error_set(error,
15579                                           ENOTSUP,
15580                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
15581                                           NULL,
15582                                           "Fail to malloc rss action memory");
15583                                 ret = rte_flow_conv(RTE_FLOW_CONV_OP_ACTION,
15584                                         act_cnt->rss, rss_size,
15585                                         act, &flow_err);
15586                                 if (ret < 0)
15587                                         return -rte_mtr_error_set(error,
15588                                           ENOTSUP,
15589                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
15590                                           NULL, "Fail to save "
15591                                           "rss action into policy struct");
15592                                 act_cnt->fate_action =
15593                                         MLX5_FLOW_FATE_SHARED_RSS;
15594                                 action_flags |= MLX5_FLOW_ACTION_RSS;
15595                                 break;
15596                         }
15597                         case RTE_FLOW_ACTION_TYPE_PORT_ID:
15598                         case RTE_FLOW_ACTION_TYPE_REPRESENTED_PORT:
15599                         {
15600                                 struct mlx5_flow_dv_port_id_action_resource
15601                                         port_id_resource;
15602                                 uint32_t port_id = 0;
15603
15604                                 if (i >= MLX5_MTR_RTE_COLORS)
15605                                         return -rte_mtr_error_set(error,
15606                                         ENOTSUP,
15607                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
15608                                         NULL, "cannot create policy "
15609                                         "port action for this color");
15610                                 memset(&port_id_resource, 0,
15611                                         sizeof(port_id_resource));
15612                                 if (flow_dv_translate_action_port_id(dev, act,
15613                                                 &port_id, &flow_err))
15614                                         return -rte_mtr_error_set(error,
15615                                         ENOTSUP,
15616                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
15617                                         NULL, "cannot translate "
15618                                         "policy port action");
15619                                 port_id_resource.port_id = port_id;
15620                                 if (flow_dv_port_id_action_resource_register
15621                                         (dev, &port_id_resource,
15622                                         &dev_flow, &flow_err))
15623                                         return -rte_mtr_error_set(error,
15624                                         ENOTSUP,
15625                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
15626                                         NULL, "cannot setup "
15627                                         "policy port action");
15628                                 act_cnt->rix_port_id_action =
15629                                         dev_flow.handle->rix_port_id_action;
15630                                 act_cnt->fate_action =
15631                                         MLX5_FLOW_FATE_PORT_ID;
15632                                 action_flags |= MLX5_FLOW_ACTION_PORT_ID;
15633                                 break;
15634                         }
15635                         case RTE_FLOW_ACTION_TYPE_JUMP:
15636                         {
15637                                 uint32_t jump_group = 0;
15638                                 uint32_t table = 0;
15639                                 struct mlx5_flow_tbl_data_entry *tbl_data;
15640                                 struct flow_grp_info grp_info = {
15641                                         .external = !!dev_flow.external,
15642                                         .transfer = !!transfer,
15643                                         .fdb_def_rule = !!priv->fdb_def_rule,
15644                                         .std_tbl_fix = 0,
15645                                         .skip_scale = dev_flow.skip_scale &
15646                                         (1 << MLX5_SCALE_FLOW_GROUP_BIT),
15647                                 };
15648                                 struct mlx5_flow_meter_sub_policy *sub_policy =
15649                                         mtr_policy->sub_policys[domain][0];
15650
15651                                 if (i >= MLX5_MTR_RTE_COLORS)
15652                                         return -rte_mtr_error_set(error,
15653                                           ENOTSUP,
15654                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
15655                                           NULL,
15656                                           "cannot create policy "
15657                                           "jump action for this color");
15658                                 jump_group =
15659                                 ((const struct rte_flow_action_jump *)
15660                                                         act->conf)->group;
15661                                 if (mlx5_flow_group_to_table(dev, NULL,
15662                                                        jump_group,
15663                                                        &table,
15664                                                        &grp_info, &flow_err))
15665                                         return -rte_mtr_error_set(error,
15666                                         ENOTSUP,
15667                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
15668                                         NULL, "cannot setup "
15669                                         "policy jump action");
15670                                 sub_policy->jump_tbl[i] =
15671                                 flow_dv_tbl_resource_get(dev,
15672                                         table, egress,
15673                                         transfer,
15674                                         !!dev_flow.external,
15675                                         NULL, jump_group, 0,
15676                                         0, &flow_err);
15677                                 if
15678                                 (!sub_policy->jump_tbl[i])
15679                                         return  -rte_mtr_error_set(error,
15680                                         ENOTSUP,
15681                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
15682                                         NULL, "cannot create jump action.");
15683                                 tbl_data = container_of
15684                                 (sub_policy->jump_tbl[i],
15685                                 struct mlx5_flow_tbl_data_entry, tbl);
15686                                 act_cnt->dr_jump_action[domain] =
15687                                         tbl_data->jump.action;
15688                                 act_cnt->fate_action =
15689                                         MLX5_FLOW_FATE_JUMP;
15690                                 action_flags |= MLX5_FLOW_ACTION_JUMP;
15691                                 break;
15692                         }
15693                         /*
15694                          * No need to check meter hierarchy for Y or R colors
15695                          * here since it is done in the validation stage.
15696                          */
15697                         case RTE_FLOW_ACTION_TYPE_METER:
15698                         {
15699                                 const struct rte_flow_action_meter *mtr;
15700                                 struct mlx5_flow_meter_info *next_fm;
15701                                 struct mlx5_flow_meter_policy *next_policy;
15702                                 struct rte_flow_action tag_action;
15703                                 struct mlx5_rte_flow_action_set_tag set_tag;
15704                                 uint32_t next_mtr_idx = 0;
15705
15706                                 mtr = act->conf;
15707                                 next_fm = mlx5_flow_meter_find(priv,
15708                                                         mtr->mtr_id,
15709                                                         &next_mtr_idx);
15710                                 if (!next_fm)
15711                                         return -rte_mtr_error_set(error, EINVAL,
15712                                                 RTE_MTR_ERROR_TYPE_MTR_ID, NULL,
15713                                                 "Fail to find next meter.");
15714                                 if (next_fm->def_policy)
15715                                         return -rte_mtr_error_set(error, EINVAL,
15716                                                 RTE_MTR_ERROR_TYPE_MTR_ID, NULL,
15717                                 "Hierarchy only supports termination meter.");
15718                                 next_policy = mlx5_flow_meter_policy_find(dev,
15719                                                 next_fm->policy_id, NULL);
15720                                 MLX5_ASSERT(next_policy);
15721                                 if (next_fm->drop_cnt) {
15722                                         set_tag.id =
15723                                                 (enum modify_reg)
15724                                                 mlx5_flow_get_reg_id(dev,
15725                                                 MLX5_MTR_ID,
15726                                                 0,
15727                                                 (struct rte_flow_error *)error);
15728                                         set_tag.offset = (priv->mtr_reg_share ?
15729                                                 MLX5_MTR_COLOR_BITS : 0);
15730                                         set_tag.length = (priv->mtr_reg_share ?
15731                                                MLX5_MTR_IDLE_BITS_IN_COLOR_REG :
15732                                                MLX5_REG_BITS);
15733                                         set_tag.data = next_mtr_idx;
15734                                         tag_action.type =
15735                                                 (enum rte_flow_action_type)
15736                                                 MLX5_RTE_FLOW_ACTION_TYPE_TAG;
15737                                         tag_action.conf = &set_tag;
15738                                         if (flow_dv_convert_action_set_reg
15739                                                 (mhdr_res, &tag_action,
15740                                                 (struct rte_flow_error *)error))
15741                                                 return -rte_errno;
15742                                         action_flags |=
15743                                                 MLX5_FLOW_ACTION_SET_TAG;
15744                                 }
15745                                 act_cnt->fate_action = MLX5_FLOW_FATE_MTR;
15746                                 act_cnt->next_mtr_id = next_fm->meter_id;
15747                                 act_cnt->next_sub_policy = NULL;
15748                                 mtr_policy->is_hierarchy = 1;
15749                                 mtr_policy->dev = next_policy->dev;
15750                                 action_flags |=
15751                                 MLX5_FLOW_ACTION_METER_WITH_TERMINATED_POLICY;
15752                                 break;
15753                         }
15754                         default:
15755                                 return -rte_mtr_error_set(error, ENOTSUP,
15756                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
15757                                           NULL, "action type not supported");
15758                         }
15759                         if (action_flags & MLX5_FLOW_ACTION_SET_TAG) {
15760                                 /* create modify action if needed. */
15761                                 dev_flow.dv.group = 1;
15762                                 if (flow_dv_modify_hdr_resource_register
15763                                         (dev, mhdr_res, &dev_flow, &flow_err))
15764                                         return -rte_mtr_error_set(error,
15765                                                 ENOTSUP,
15766                                                 RTE_MTR_ERROR_TYPE_METER_POLICY,
15767                                                 NULL, "cannot register policy "
15768                                                 "set tag action");
15769                                 act_cnt->modify_hdr =
15770                                         dev_flow.handle->dvh.modify_hdr;
15771                         }
15772                 }
15773         }
15774         return 0;
15775 }
15776
15777 /**
15778  * Create policy action per domain, lock free,
15779  * (mutex should be acquired by caller).
15780  * Dispatcher for action type specific call.
15781  *
15782  * @param[in] dev
15783  *   Pointer to the Ethernet device structure.
15784  * @param[in] mtr_policy
15785  *   Meter policy struct.
15786  * @param[in] action
15787  *   Action specification used to create meter actions.
15788  * @param[out] error
15789  *   Perform verbose error reporting if not NULL. Initialized in case of
15790  *   error only.
15791  *
15792  * @return
15793  *   0 on success, otherwise negative errno value.
15794  */
15795 static int
15796 flow_dv_create_mtr_policy_acts(struct rte_eth_dev *dev,
15797                       struct mlx5_flow_meter_policy *mtr_policy,
15798                       const struct rte_flow_action *actions[RTE_COLORS],
15799                       struct rte_mtr_error *error)
15800 {
15801         int ret, i;
15802         uint16_t sub_policy_num;
15803
15804         for (i = 0; i < MLX5_MTR_DOMAIN_MAX; i++) {
15805                 sub_policy_num = (mtr_policy->sub_policy_num >>
15806                         (MLX5_MTR_SUB_POLICY_NUM_SHIFT * i)) &
15807                         MLX5_MTR_SUB_POLICY_NUM_MASK;
15808                 if (sub_policy_num) {
15809                         ret = __flow_dv_create_domain_policy_acts(dev,
15810                                 mtr_policy, actions,
15811                                 (enum mlx5_meter_domain)i, error);
15812                         /* Cleaning resource is done in the caller level. */
15813                         if (ret)
15814                                 return ret;
15815                 }
15816         }
15817         return 0;
15818 }
15819
15820 /**
15821  * Query a DV flow rule for its statistics via DevX.
15822  *
15823  * @param[in] dev
15824  *   Pointer to Ethernet device.
15825  * @param[in] cnt_idx
15826  *   Index to the flow counter.
15827  * @param[out] data
15828  *   Data retrieved by the query.
15829  * @param[out] error
15830  *   Perform verbose error reporting if not NULL.
15831  *
15832  * @return
15833  *   0 on success, a negative errno value otherwise and rte_errno is set.
15834  */
15835 int
15836 flow_dv_query_count(struct rte_eth_dev *dev, uint32_t cnt_idx, void *data,
15837                     struct rte_flow_error *error)
15838 {
15839         struct mlx5_priv *priv = dev->data->dev_private;
15840         struct rte_flow_query_count *qc = data;
15841
15842         if (!priv->sh->devx)
15843                 return rte_flow_error_set(error, ENOTSUP,
15844                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
15845                                           NULL,
15846                                           "counters are not supported");
15847         if (cnt_idx) {
15848                 uint64_t pkts, bytes;
15849                 struct mlx5_flow_counter *cnt;
15850                 int err = _flow_dv_query_count(dev, cnt_idx, &pkts, &bytes);
15851
15852                 if (err)
15853                         return rte_flow_error_set(error, -err,
15854                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
15855                                         NULL, "cannot read counters");
15856                 cnt = flow_dv_counter_get_by_idx(dev, cnt_idx, NULL);
15857                 qc->hits_set = 1;
15858                 qc->bytes_set = 1;
15859                 qc->hits = pkts - cnt->hits;
15860                 qc->bytes = bytes - cnt->bytes;
15861                 if (qc->reset) {
15862                         cnt->hits = pkts;
15863                         cnt->bytes = bytes;
15864                 }
15865                 return 0;
15866         }
15867         return rte_flow_error_set(error, EINVAL,
15868                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
15869                                   NULL,
15870                                   "counters are not available");
15871 }
15872
15873
15874 /**
15875  * Query counter's action pointer for a DV flow rule via DevX.
15876  *
15877  * @param[in] dev
15878  *   Pointer to Ethernet device.
15879  * @param[in] cnt_idx
15880  *   Index to the flow counter.
15881  * @param[out] action_ptr
15882  *   Action pointer for counter.
15883  * @param[out] error
15884  *   Perform verbose error reporting if not NULL.
15885  *
15886  * @return
15887  *   0 on success, a negative errno value otherwise and rte_errno is set.
15888  */
15889 int
15890 flow_dv_query_count_ptr(struct rte_eth_dev *dev, uint32_t cnt_idx,
15891         void **action_ptr, struct rte_flow_error *error)
15892 {
15893         struct mlx5_priv *priv = dev->data->dev_private;
15894
15895         if (!priv->sh->devx || !action_ptr)
15896                 return rte_flow_error_set(error, ENOTSUP,
15897                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
15898                                           NULL,
15899                                           "counters are not supported");
15900
15901         if (cnt_idx) {
15902                 struct mlx5_flow_counter *cnt = NULL;
15903                 cnt = flow_dv_counter_get_by_idx(dev, cnt_idx, NULL);
15904                 if (cnt) {
15905                         *action_ptr = cnt->action;
15906                         return 0;
15907                 }
15908         }
15909         return rte_flow_error_set(error, EINVAL,
15910                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
15911                                   NULL,
15912                                   "counters are not available");
15913 }
15914
15915 static int
15916 flow_dv_action_query(struct rte_eth_dev *dev,
15917                      const struct rte_flow_action_handle *handle, void *data,
15918                      struct rte_flow_error *error)
15919 {
15920         struct mlx5_age_param *age_param;
15921         struct rte_flow_query_age *resp;
15922         uint32_t act_idx = (uint32_t)(uintptr_t)handle;
15923         uint32_t type = act_idx >> MLX5_INDIRECT_ACTION_TYPE_OFFSET;
15924         uint32_t idx = act_idx & ((1u << MLX5_INDIRECT_ACTION_TYPE_OFFSET) - 1);
15925         struct mlx5_priv *priv = dev->data->dev_private;
15926         struct mlx5_aso_ct_action *ct;
15927         uint16_t owner;
15928         uint32_t dev_idx;
15929
15930         switch (type) {
15931         case MLX5_INDIRECT_ACTION_TYPE_AGE:
15932                 age_param = &flow_aso_age_get_by_idx(dev, idx)->age_params;
15933                 resp = data;
15934                 resp->aged = __atomic_load_n(&age_param->state,
15935                                               __ATOMIC_RELAXED) == AGE_TMOUT ?
15936                                                                           1 : 0;
15937                 resp->sec_since_last_hit_valid = !resp->aged;
15938                 if (resp->sec_since_last_hit_valid)
15939                         resp->sec_since_last_hit = __atomic_load_n
15940                              (&age_param->sec_since_last_hit, __ATOMIC_RELAXED);
15941                 return 0;
15942         case MLX5_INDIRECT_ACTION_TYPE_COUNT:
15943                 return flow_dv_query_count(dev, idx, data, error);
15944         case MLX5_INDIRECT_ACTION_TYPE_CT:
15945                 owner = (uint16_t)MLX5_INDIRECT_ACT_CT_GET_OWNER(idx);
15946                 if (owner != PORT_ID(priv))
15947                         return rte_flow_error_set(error, EACCES,
15948                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
15949                                         NULL,
15950                                         "CT object owned by another port");
15951                 dev_idx = MLX5_INDIRECT_ACT_CT_GET_IDX(idx);
15952                 ct = flow_aso_ct_get_by_dev_idx(dev, dev_idx);
15953                 MLX5_ASSERT(ct);
15954                 if (!ct->refcnt)
15955                         return rte_flow_error_set(error, EFAULT,
15956                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
15957                                         NULL,
15958                                         "CT object is inactive");
15959                 ((struct rte_flow_action_conntrack *)data)->peer_port =
15960                                                         ct->peer;
15961                 ((struct rte_flow_action_conntrack *)data)->is_original_dir =
15962                                                         ct->is_original;
15963                 if (mlx5_aso_ct_query_by_wqe(priv->sh, ct, data))
15964                         return rte_flow_error_set(error, EIO,
15965                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
15966                                         NULL,
15967                                         "Failed to query CT context");
15968                 return 0;
15969         default:
15970                 return rte_flow_error_set(error, ENOTSUP,
15971                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
15972                                           "action type query not supported");
15973         }
15974 }
15975
15976 /**
15977  * Query a flow rule AGE action for aging information.
15978  *
15979  * @param[in] dev
15980  *   Pointer to Ethernet device.
15981  * @param[in] flow
15982  *   Pointer to the sub flow.
15983  * @param[out] data
15984  *   data retrieved by the query.
15985  * @param[out] error
15986  *   Perform verbose error reporting if not NULL.
15987  *
15988  * @return
15989  *   0 on success, a negative errno value otherwise and rte_errno is set.
15990  */
15991 static int
15992 flow_dv_query_age(struct rte_eth_dev *dev, struct rte_flow *flow,
15993                   void *data, struct rte_flow_error *error)
15994 {
15995         struct rte_flow_query_age *resp = data;
15996         struct mlx5_age_param *age_param;
15997
15998         if (flow->age) {
15999                 struct mlx5_aso_age_action *act =
16000                                      flow_aso_age_get_by_idx(dev, flow->age);
16001
16002                 age_param = &act->age_params;
16003         } else if (flow->counter) {
16004                 age_param = flow_dv_counter_idx_get_age(dev, flow->counter);
16005
16006                 if (!age_param || !age_param->timeout)
16007                         return rte_flow_error_set
16008                                         (error, EINVAL,
16009                                          RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
16010                                          NULL, "cannot read age data");
16011         } else {
16012                 return rte_flow_error_set(error, EINVAL,
16013                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
16014                                           NULL, "age data not available");
16015         }
16016         resp->aged = __atomic_load_n(&age_param->state, __ATOMIC_RELAXED) ==
16017                                      AGE_TMOUT ? 1 : 0;
16018         resp->sec_since_last_hit_valid = !resp->aged;
16019         if (resp->sec_since_last_hit_valid)
16020                 resp->sec_since_last_hit = __atomic_load_n
16021                              (&age_param->sec_since_last_hit, __ATOMIC_RELAXED);
16022         return 0;
16023 }
16024
16025 /**
16026  * Query a flow.
16027  *
16028  * @see rte_flow_query()
16029  * @see rte_flow_ops
16030  */
16031 static int
16032 flow_dv_query(struct rte_eth_dev *dev,
16033               struct rte_flow *flow __rte_unused,
16034               const struct rte_flow_action *actions __rte_unused,
16035               void *data __rte_unused,
16036               struct rte_flow_error *error __rte_unused)
16037 {
16038         int ret = -EINVAL;
16039
16040         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
16041                 switch (actions->type) {
16042                 case RTE_FLOW_ACTION_TYPE_VOID:
16043                         break;
16044                 case RTE_FLOW_ACTION_TYPE_COUNT:
16045                         ret = flow_dv_query_count(dev, flow->counter, data,
16046                                                   error);
16047                         break;
16048                 case RTE_FLOW_ACTION_TYPE_AGE:
16049                         ret = flow_dv_query_age(dev, flow, data, error);
16050                         break;
16051                 default:
16052                         return rte_flow_error_set(error, ENOTSUP,
16053                                                   RTE_FLOW_ERROR_TYPE_ACTION,
16054                                                   actions,
16055                                                   "action not supported");
16056                 }
16057         }
16058         return ret;
16059 }
16060
16061 /**
16062  * Destroy the meter table set.
16063  * Lock free, (mutex should be acquired by caller).
16064  *
16065  * @param[in] dev
16066  *   Pointer to Ethernet device.
16067  * @param[in] fm
16068  *   Meter information table.
16069  */
16070 static void
16071 flow_dv_destroy_mtr_tbls(struct rte_eth_dev *dev,
16072                         struct mlx5_flow_meter_info *fm)
16073 {
16074         struct mlx5_priv *priv = dev->data->dev_private;
16075         int i;
16076
16077         if (!fm || !priv->config.dv_flow_en)
16078                 return;
16079         for (i = 0; i < MLX5_MTR_DOMAIN_MAX; i++) {
16080                 if (fm->drop_rule[i]) {
16081                         claim_zero(mlx5_flow_os_destroy_flow(fm->drop_rule[i]));
16082                         fm->drop_rule[i] = NULL;
16083                 }
16084         }
16085 }
16086
16087 static void
16088 flow_dv_destroy_mtr_drop_tbls(struct rte_eth_dev *dev)
16089 {
16090         struct mlx5_priv *priv = dev->data->dev_private;
16091         struct mlx5_flow_mtr_mng *mtrmng = priv->sh->mtrmng;
16092         struct mlx5_flow_tbl_data_entry *tbl;
16093         int i, j;
16094
16095         for (i = 0; i < MLX5_MTR_DOMAIN_MAX; i++) {
16096                 if (mtrmng->def_rule[i]) {
16097                         claim_zero(mlx5_flow_os_destroy_flow
16098                                         (mtrmng->def_rule[i]));
16099                         mtrmng->def_rule[i] = NULL;
16100                 }
16101                 if (mtrmng->def_matcher[i]) {
16102                         tbl = container_of(mtrmng->def_matcher[i]->tbl,
16103                                 struct mlx5_flow_tbl_data_entry, tbl);
16104                         mlx5_list_unregister(tbl->matchers,
16105                                              &mtrmng->def_matcher[i]->entry);
16106                         mtrmng->def_matcher[i] = NULL;
16107                 }
16108                 for (j = 0; j < MLX5_REG_BITS; j++) {
16109                         if (mtrmng->drop_matcher[i][j]) {
16110                                 tbl =
16111                                 container_of(mtrmng->drop_matcher[i][j]->tbl,
16112                                              struct mlx5_flow_tbl_data_entry,
16113                                              tbl);
16114                                 mlx5_list_unregister(tbl->matchers,
16115                                             &mtrmng->drop_matcher[i][j]->entry);
16116                                 mtrmng->drop_matcher[i][j] = NULL;
16117                         }
16118                 }
16119                 if (mtrmng->drop_tbl[i]) {
16120                         flow_dv_tbl_resource_release(MLX5_SH(dev),
16121                                 mtrmng->drop_tbl[i]);
16122                         mtrmng->drop_tbl[i] = NULL;
16123                 }
16124         }
16125 }
16126
16127 /* Number of meter flow actions, count and jump or count and drop. */
16128 #define METER_ACTIONS 2
16129
16130 static void
16131 __flow_dv_destroy_domain_def_policy(struct rte_eth_dev *dev,
16132                                     enum mlx5_meter_domain domain)
16133 {
16134         struct mlx5_priv *priv = dev->data->dev_private;
16135         struct mlx5_flow_meter_def_policy *def_policy =
16136                         priv->sh->mtrmng->def_policy[domain];
16137
16138         __flow_dv_destroy_sub_policy_rules(dev, &def_policy->sub_policy);
16139         mlx5_free(def_policy);
16140         priv->sh->mtrmng->def_policy[domain] = NULL;
16141 }
16142
16143 /**
16144  * Destroy the default policy table set.
16145  *
16146  * @param[in] dev
16147  *   Pointer to Ethernet device.
16148  */
16149 static void
16150 flow_dv_destroy_def_policy(struct rte_eth_dev *dev)
16151 {
16152         struct mlx5_priv *priv = dev->data->dev_private;
16153         int i;
16154
16155         for (i = 0; i < MLX5_MTR_DOMAIN_MAX; i++)
16156                 if (priv->sh->mtrmng->def_policy[i])
16157                         __flow_dv_destroy_domain_def_policy(dev,
16158                                         (enum mlx5_meter_domain)i);
16159         priv->sh->mtrmng->def_policy_id = MLX5_INVALID_POLICY_ID;
16160 }
16161
16162 static int
16163 __flow_dv_create_policy_flow(struct rte_eth_dev *dev,
16164                         uint32_t color_reg_c_idx,
16165                         enum rte_color color, void *matcher_object,
16166                         int actions_n, void *actions,
16167                         bool match_src_port, const struct rte_flow_item *item,
16168                         void **rule, const struct rte_flow_attr *attr)
16169 {
16170         int ret;
16171         struct mlx5_flow_dv_match_params value = {
16172                 .size = sizeof(value.buf),
16173         };
16174         struct mlx5_flow_dv_match_params matcher = {
16175                 .size = sizeof(matcher.buf),
16176         };
16177         struct mlx5_priv *priv = dev->data->dev_private;
16178         uint8_t misc_mask;
16179
16180         if (match_src_port && (priv->representor || priv->master)) {
16181                 if (flow_dv_translate_item_port_id(dev, matcher.buf,
16182                                                    value.buf, item, attr)) {
16183                         DRV_LOG(ERR, "Failed to create meter policy%d flow's"
16184                                 " value with port.", color);
16185                         return -1;
16186                 }
16187         }
16188         flow_dv_match_meta_reg(matcher.buf, value.buf,
16189                                (enum modify_reg)color_reg_c_idx,
16190                                rte_col_2_mlx5_col(color), UINT32_MAX);
16191         misc_mask = flow_dv_matcher_enable(value.buf);
16192         __flow_dv_adjust_buf_size(&value.size, misc_mask);
16193         ret = mlx5_flow_os_create_flow(matcher_object, (void *)&value,
16194                                        actions_n, actions, rule);
16195         if (ret) {
16196                 DRV_LOG(ERR, "Failed to create meter policy%d flow.", color);
16197                 return -1;
16198         }
16199         return 0;
16200 }
16201
16202 static int
16203 __flow_dv_create_policy_matcher(struct rte_eth_dev *dev,
16204                         uint32_t color_reg_c_idx,
16205                         uint16_t priority,
16206                         struct mlx5_flow_meter_sub_policy *sub_policy,
16207                         const struct rte_flow_attr *attr,
16208                         bool match_src_port,
16209                         const struct rte_flow_item *item,
16210                         struct mlx5_flow_dv_matcher **policy_matcher,
16211                         struct rte_flow_error *error)
16212 {
16213         struct mlx5_list_entry *entry;
16214         struct mlx5_flow_tbl_resource *tbl_rsc = sub_policy->tbl_rsc;
16215         struct mlx5_flow_dv_matcher matcher = {
16216                 .mask = {
16217                         .size = sizeof(matcher.mask.buf),
16218                 },
16219                 .tbl = tbl_rsc,
16220         };
16221         struct mlx5_flow_dv_match_params value = {
16222                 .size = sizeof(value.buf),
16223         };
16224         struct mlx5_flow_cb_ctx ctx = {
16225                 .error = error,
16226                 .data = &matcher,
16227         };
16228         struct mlx5_flow_tbl_data_entry *tbl_data;
16229         struct mlx5_priv *priv = dev->data->dev_private;
16230         const uint32_t color_mask = (UINT32_C(1) << MLX5_MTR_COLOR_BITS) - 1;
16231
16232         if (match_src_port && (priv->representor || priv->master)) {
16233                 if (flow_dv_translate_item_port_id(dev, matcher.mask.buf,
16234                                                    value.buf, item, attr)) {
16235                         DRV_LOG(ERR, "Failed to register meter policy%d matcher"
16236                                 " with port.", priority);
16237                         return -1;
16238                 }
16239         }
16240         tbl_data = container_of(tbl_rsc, struct mlx5_flow_tbl_data_entry, tbl);
16241         if (priority < RTE_COLOR_RED)
16242                 flow_dv_match_meta_reg(matcher.mask.buf, value.buf,
16243                         (enum modify_reg)color_reg_c_idx, 0, color_mask);
16244         matcher.priority = priority;
16245         matcher.crc = rte_raw_cksum((const void *)matcher.mask.buf,
16246                                     matcher.mask.size);
16247         entry = mlx5_list_register(tbl_data->matchers, &ctx);
16248         if (!entry) {
16249                 DRV_LOG(ERR, "Failed to register meter drop matcher.");
16250                 return -1;
16251         }
16252         *policy_matcher =
16253                 container_of(entry, struct mlx5_flow_dv_matcher, entry);
16254         return 0;
16255 }
16256
16257 /**
16258  * Create the policy rules per domain.
16259  *
16260  * @param[in] dev
16261  *   Pointer to Ethernet device.
16262  * @param[in] sub_policy
16263  *    Pointer to sub policy table..
16264  * @param[in] egress
16265  *   Direction of the table.
16266  * @param[in] transfer
16267  *   E-Switch or NIC flow.
16268  * @param[in] acts
16269  *   Pointer to policy action list per color.
16270  *
16271  * @return
16272  *   0 on success, -1 otherwise.
16273  */
16274 static int
16275 __flow_dv_create_domain_policy_rules(struct rte_eth_dev *dev,
16276                 struct mlx5_flow_meter_sub_policy *sub_policy,
16277                 uint8_t egress, uint8_t transfer, bool match_src_port,
16278                 struct mlx5_meter_policy_acts acts[RTE_COLORS])
16279 {
16280         struct mlx5_priv *priv = dev->data->dev_private;
16281         struct rte_flow_error flow_err;
16282         uint32_t color_reg_c_idx;
16283         struct rte_flow_attr attr = {
16284                 .group = MLX5_FLOW_TABLE_LEVEL_POLICY,
16285                 .priority = 0,
16286                 .ingress = 0,
16287                 .egress = !!egress,
16288                 .transfer = !!transfer,
16289                 .reserved = 0,
16290         };
16291         int i;
16292         int ret = mlx5_flow_get_reg_id(dev, MLX5_MTR_COLOR, 0, &flow_err);
16293         struct mlx5_sub_policy_color_rule *color_rule;
16294         bool svport_match;
16295         struct mlx5_sub_policy_color_rule *tmp_rules[RTE_COLORS] = {NULL};
16296
16297         if (ret < 0)
16298                 return -1;
16299         /* Create policy table with POLICY level. */
16300         if (!sub_policy->tbl_rsc)
16301                 sub_policy->tbl_rsc = flow_dv_tbl_resource_get(dev,
16302                                 MLX5_FLOW_TABLE_LEVEL_POLICY,
16303                                 egress, transfer, false, NULL, 0, 0,
16304                                 sub_policy->idx, &flow_err);
16305         if (!sub_policy->tbl_rsc) {
16306                 DRV_LOG(ERR,
16307                         "Failed to create meter sub policy table.");
16308                 return -1;
16309         }
16310         /* Prepare matchers. */
16311         color_reg_c_idx = ret;
16312         for (i = 0; i < RTE_COLORS; i++) {
16313                 TAILQ_INIT(&sub_policy->color_rules[i]);
16314                 if (!acts[i].actions_n)
16315                         continue;
16316                 color_rule = mlx5_malloc(MLX5_MEM_ZERO,
16317                                 sizeof(struct mlx5_sub_policy_color_rule),
16318                                 0, SOCKET_ID_ANY);
16319                 if (!color_rule) {
16320                         DRV_LOG(ERR, "No memory to create color rule.");
16321                         goto err_exit;
16322                 }
16323                 tmp_rules[i] = color_rule;
16324                 TAILQ_INSERT_TAIL(&sub_policy->color_rules[i],
16325                                   color_rule, next_port);
16326                 color_rule->src_port = priv->representor_id;
16327                 /* No use. */
16328                 attr.priority = i;
16329                 /* Create matchers for colors. */
16330                 svport_match = (i != RTE_COLOR_RED) ? match_src_port : false;
16331                 if (__flow_dv_create_policy_matcher(dev, color_reg_c_idx,
16332                                 MLX5_MTR_POLICY_MATCHER_PRIO, sub_policy,
16333                                 &attr, svport_match, NULL,
16334                                 &color_rule->matcher, &flow_err)) {
16335                         DRV_LOG(ERR, "Failed to create color%u matcher.", i);
16336                         goto err_exit;
16337                 }
16338                 /* Create flow, matching color. */
16339                 if (__flow_dv_create_policy_flow(dev,
16340                                 color_reg_c_idx, (enum rte_color)i,
16341                                 color_rule->matcher->matcher_object,
16342                                 acts[i].actions_n, acts[i].dv_actions,
16343                                 svport_match, NULL, &color_rule->rule,
16344                                 &attr)) {
16345                         DRV_LOG(ERR, "Failed to create color%u rule.", i);
16346                         goto err_exit;
16347                 }
16348         }
16349         return 0;
16350 err_exit:
16351         /* All the policy rules will be cleared. */
16352         do {
16353                 color_rule = tmp_rules[i];
16354                 if (color_rule) {
16355                         if (color_rule->rule)
16356                                 mlx5_flow_os_destroy_flow(color_rule->rule);
16357                         if (color_rule->matcher) {
16358                                 struct mlx5_flow_tbl_data_entry *tbl =
16359                                         container_of(color_rule->matcher->tbl,
16360                                                      typeof(*tbl), tbl);
16361                                 mlx5_list_unregister(tbl->matchers,
16362                                                 &color_rule->matcher->entry);
16363                         }
16364                         TAILQ_REMOVE(&sub_policy->color_rules[i],
16365                                      color_rule, next_port);
16366                         mlx5_free(color_rule);
16367                 }
16368         } while (i--);
16369         return -1;
16370 }
16371
16372 static int
16373 __flow_dv_create_policy_acts_rules(struct rte_eth_dev *dev,
16374                         struct mlx5_flow_meter_policy *mtr_policy,
16375                         struct mlx5_flow_meter_sub_policy *sub_policy,
16376                         uint32_t domain)
16377 {
16378         struct mlx5_priv *priv = dev->data->dev_private;
16379         struct mlx5_meter_policy_acts acts[RTE_COLORS];
16380         struct mlx5_flow_dv_tag_resource *tag;
16381         struct mlx5_flow_dv_port_id_action_resource *port_action;
16382         struct mlx5_hrxq *hrxq;
16383         struct mlx5_flow_meter_info *next_fm = NULL;
16384         struct mlx5_flow_meter_policy *next_policy;
16385         struct mlx5_flow_meter_sub_policy *next_sub_policy;
16386         struct mlx5_flow_tbl_data_entry *tbl_data;
16387         struct rte_flow_error error;
16388         uint8_t egress = (domain == MLX5_MTR_DOMAIN_EGRESS) ? 1 : 0;
16389         uint8_t transfer = (domain == MLX5_MTR_DOMAIN_TRANSFER) ? 1 : 0;
16390         bool mtr_first = egress || (transfer && priv->representor_id != UINT16_MAX);
16391         bool match_src_port = false;
16392         int i;
16393
16394         /* If RSS or Queue, no previous actions / rules is created. */
16395         for (i = 0; i < RTE_COLORS; i++) {
16396                 acts[i].actions_n = 0;
16397                 if (i == RTE_COLOR_RED) {
16398                         /* Only support drop on red. */
16399                         acts[i].dv_actions[0] =
16400                                 mtr_policy->dr_drop_action[domain];
16401                         acts[i].actions_n = 1;
16402                         continue;
16403                 }
16404                 if (i == RTE_COLOR_GREEN &&
16405                     mtr_policy->act_cnt[i].fate_action == MLX5_FLOW_FATE_MTR) {
16406                         struct rte_flow_attr attr = {
16407                                 .transfer = transfer
16408                         };
16409
16410                         next_fm = mlx5_flow_meter_find(priv,
16411                                         mtr_policy->act_cnt[i].next_mtr_id,
16412                                         NULL);
16413                         if (!next_fm) {
16414                                 DRV_LOG(ERR,
16415                                         "Failed to get next hierarchy meter.");
16416                                 goto err_exit;
16417                         }
16418                         if (mlx5_flow_meter_attach(priv, next_fm,
16419                                                    &attr, &error)) {
16420                                 DRV_LOG(ERR, "%s", error.message);
16421                                 next_fm = NULL;
16422                                 goto err_exit;
16423                         }
16424                         /* Meter action must be the first for TX. */
16425                         if (mtr_first) {
16426                                 acts[i].dv_actions[acts[i].actions_n] =
16427                                         next_fm->meter_action;
16428                                 acts[i].actions_n++;
16429                         }
16430                 }
16431                 if (mtr_policy->act_cnt[i].rix_mark) {
16432                         tag = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_TAG],
16433                                         mtr_policy->act_cnt[i].rix_mark);
16434                         if (!tag) {
16435                                 DRV_LOG(ERR, "Failed to find "
16436                                 "mark action for policy.");
16437                                 goto err_exit;
16438                         }
16439                         acts[i].dv_actions[acts[i].actions_n] = tag->action;
16440                         acts[i].actions_n++;
16441                 }
16442                 if (mtr_policy->act_cnt[i].modify_hdr) {
16443                         acts[i].dv_actions[acts[i].actions_n] =
16444                                 mtr_policy->act_cnt[i].modify_hdr->action;
16445                         acts[i].actions_n++;
16446                 }
16447                 if (mtr_policy->act_cnt[i].fate_action) {
16448                         switch (mtr_policy->act_cnt[i].fate_action) {
16449                         case MLX5_FLOW_FATE_PORT_ID:
16450                                 port_action = mlx5_ipool_get
16451                                         (priv->sh->ipool[MLX5_IPOOL_PORT_ID],
16452                                 mtr_policy->act_cnt[i].rix_port_id_action);
16453                                 if (!port_action) {
16454                                         DRV_LOG(ERR, "Failed to find "
16455                                                 "port action for policy.");
16456                                         goto err_exit;
16457                                 }
16458                                 acts[i].dv_actions[acts[i].actions_n] =
16459                                         port_action->action;
16460                                 acts[i].actions_n++;
16461                                 mtr_policy->dev = dev;
16462                                 match_src_port = true;
16463                                 break;
16464                         case MLX5_FLOW_FATE_DROP:
16465                         case MLX5_FLOW_FATE_JUMP:
16466                                 acts[i].dv_actions[acts[i].actions_n] =
16467                                 mtr_policy->act_cnt[i].dr_jump_action[domain];
16468                                 acts[i].actions_n++;
16469                                 break;
16470                         case MLX5_FLOW_FATE_SHARED_RSS:
16471                         case MLX5_FLOW_FATE_QUEUE:
16472                                 hrxq = mlx5_ipool_get
16473                                         (priv->sh->ipool[MLX5_IPOOL_HRXQ],
16474                                          sub_policy->rix_hrxq[i]);
16475                                 if (!hrxq) {
16476                                         DRV_LOG(ERR, "Failed to find "
16477                                                 "queue action for policy.");
16478                                         goto err_exit;
16479                                 }
16480                                 acts[i].dv_actions[acts[i].actions_n] =
16481                                         hrxq->action;
16482                                 acts[i].actions_n++;
16483                                 break;
16484                         case MLX5_FLOW_FATE_MTR:
16485                                 if (!next_fm) {
16486                                         DRV_LOG(ERR,
16487                                                 "No next hierarchy meter.");
16488                                         goto err_exit;
16489                                 }
16490                                 if (!mtr_first) {
16491                                         acts[i].dv_actions[acts[i].actions_n] =
16492                                                         next_fm->meter_action;
16493                                         acts[i].actions_n++;
16494                                 }
16495                                 if (mtr_policy->act_cnt[i].next_sub_policy) {
16496                                         next_sub_policy =
16497                                         mtr_policy->act_cnt[i].next_sub_policy;
16498                                 } else {
16499                                         next_policy =
16500                                                 mlx5_flow_meter_policy_find(dev,
16501                                                 next_fm->policy_id, NULL);
16502                                         MLX5_ASSERT(next_policy);
16503                                         next_sub_policy =
16504                                         next_policy->sub_policys[domain][0];
16505                                 }
16506                                 tbl_data =
16507                                         container_of(next_sub_policy->tbl_rsc,
16508                                         struct mlx5_flow_tbl_data_entry, tbl);
16509                                 acts[i].dv_actions[acts[i].actions_n++] =
16510                                                         tbl_data->jump.action;
16511                                 if (mtr_policy->act_cnt[i].modify_hdr)
16512                                         match_src_port = !!transfer;
16513                                 break;
16514                         default:
16515                                 /*Queue action do nothing*/
16516                                 break;
16517                         }
16518                 }
16519         }
16520         if (__flow_dv_create_domain_policy_rules(dev, sub_policy,
16521                                 egress, transfer, match_src_port, acts)) {
16522                 DRV_LOG(ERR,
16523                         "Failed to create policy rules per domain.");
16524                 goto err_exit;
16525         }
16526         return 0;
16527 err_exit:
16528         if (next_fm)
16529                 mlx5_flow_meter_detach(priv, next_fm);
16530         return -1;
16531 }
16532
16533 /**
16534  * Create the policy rules.
16535  *
16536  * @param[in] dev
16537  *   Pointer to Ethernet device.
16538  * @param[in,out] mtr_policy
16539  *   Pointer to meter policy table.
16540  *
16541  * @return
16542  *   0 on success, -1 otherwise.
16543  */
16544 static int
16545 flow_dv_create_policy_rules(struct rte_eth_dev *dev,
16546                              struct mlx5_flow_meter_policy *mtr_policy)
16547 {
16548         int i;
16549         uint16_t sub_policy_num;
16550
16551         for (i = 0; i < MLX5_MTR_DOMAIN_MAX; i++) {
16552                 sub_policy_num = (mtr_policy->sub_policy_num >>
16553                         (MLX5_MTR_SUB_POLICY_NUM_SHIFT * i)) &
16554                         MLX5_MTR_SUB_POLICY_NUM_MASK;
16555                 if (!sub_policy_num)
16556                         continue;
16557                 /* Prepare actions list and create policy rules. */
16558                 if (__flow_dv_create_policy_acts_rules(dev, mtr_policy,
16559                         mtr_policy->sub_policys[i][0], i)) {
16560                         DRV_LOG(ERR, "Failed to create policy action "
16561                                 "list per domain.");
16562                         return -1;
16563                 }
16564         }
16565         return 0;
16566 }
16567
16568 static int
16569 __flow_dv_create_domain_def_policy(struct rte_eth_dev *dev, uint32_t domain)
16570 {
16571         struct mlx5_priv *priv = dev->data->dev_private;
16572         struct mlx5_flow_mtr_mng *mtrmng = priv->sh->mtrmng;
16573         struct mlx5_flow_meter_def_policy *def_policy;
16574         struct mlx5_flow_tbl_resource *jump_tbl;
16575         struct mlx5_flow_tbl_data_entry *tbl_data;
16576         uint8_t egress, transfer;
16577         struct rte_flow_error error;
16578         struct mlx5_meter_policy_acts acts[RTE_COLORS];
16579         int ret;
16580
16581         egress = (domain == MLX5_MTR_DOMAIN_EGRESS) ? 1 : 0;
16582         transfer = (domain == MLX5_MTR_DOMAIN_TRANSFER) ? 1 : 0;
16583         def_policy = mtrmng->def_policy[domain];
16584         if (!def_policy) {
16585                 def_policy = mlx5_malloc(MLX5_MEM_ZERO,
16586                         sizeof(struct mlx5_flow_meter_def_policy),
16587                         RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
16588                 if (!def_policy) {
16589                         DRV_LOG(ERR, "Failed to alloc default policy table.");
16590                         goto def_policy_error;
16591                 }
16592                 mtrmng->def_policy[domain] = def_policy;
16593                 /* Create the meter suffix table with SUFFIX level. */
16594                 jump_tbl = flow_dv_tbl_resource_get(dev,
16595                                 MLX5_FLOW_TABLE_LEVEL_METER,
16596                                 egress, transfer, false, NULL, 0,
16597                                 0, MLX5_MTR_TABLE_ID_SUFFIX, &error);
16598                 if (!jump_tbl) {
16599                         DRV_LOG(ERR,
16600                                 "Failed to create meter suffix table.");
16601                         goto def_policy_error;
16602                 }
16603                 def_policy->sub_policy.jump_tbl[RTE_COLOR_GREEN] = jump_tbl;
16604                 tbl_data = container_of(jump_tbl,
16605                                         struct mlx5_flow_tbl_data_entry, tbl);
16606                 def_policy->dr_jump_action[RTE_COLOR_GREEN] =
16607                                                 tbl_data->jump.action;
16608                 acts[RTE_COLOR_GREEN].dv_actions[0] = tbl_data->jump.action;
16609                 acts[RTE_COLOR_GREEN].actions_n = 1;
16610                 /*
16611                  * YELLOW has the same default policy as GREEN does.
16612                  * G & Y share the same table and action. The 2nd time of table
16613                  * resource getting is just to update the reference count for
16614                  * the releasing stage.
16615                  */
16616                 jump_tbl = flow_dv_tbl_resource_get(dev,
16617                                 MLX5_FLOW_TABLE_LEVEL_METER,
16618                                 egress, transfer, false, NULL, 0,
16619                                 0, MLX5_MTR_TABLE_ID_SUFFIX, &error);
16620                 if (!jump_tbl) {
16621                         DRV_LOG(ERR,
16622                                 "Failed to get meter suffix table.");
16623                         goto def_policy_error;
16624                 }
16625                 def_policy->sub_policy.jump_tbl[RTE_COLOR_YELLOW] = jump_tbl;
16626                 tbl_data = container_of(jump_tbl,
16627                                         struct mlx5_flow_tbl_data_entry, tbl);
16628                 def_policy->dr_jump_action[RTE_COLOR_YELLOW] =
16629                                                 tbl_data->jump.action;
16630                 acts[RTE_COLOR_YELLOW].dv_actions[0] = tbl_data->jump.action;
16631                 acts[RTE_COLOR_YELLOW].actions_n = 1;
16632                 /* Create jump action to the drop table. */
16633                 if (!mtrmng->drop_tbl[domain]) {
16634                         mtrmng->drop_tbl[domain] = flow_dv_tbl_resource_get
16635                                 (dev, MLX5_FLOW_TABLE_LEVEL_METER,
16636                                  egress, transfer, false, NULL, 0,
16637                                  0, MLX5_MTR_TABLE_ID_DROP, &error);
16638                         if (!mtrmng->drop_tbl[domain]) {
16639                                 DRV_LOG(ERR, "Failed to create meter "
16640                                         "drop table for default policy.");
16641                                 goto def_policy_error;
16642                         }
16643                 }
16644                 /* all RED: unique Drop table for jump action. */
16645                 tbl_data = container_of(mtrmng->drop_tbl[domain],
16646                                         struct mlx5_flow_tbl_data_entry, tbl);
16647                 def_policy->dr_jump_action[RTE_COLOR_RED] =
16648                                                 tbl_data->jump.action;
16649                 acts[RTE_COLOR_RED].dv_actions[0] = tbl_data->jump.action;
16650                 acts[RTE_COLOR_RED].actions_n = 1;
16651                 /* Create default policy rules. */
16652                 ret = __flow_dv_create_domain_policy_rules(dev,
16653                                         &def_policy->sub_policy,
16654                                         egress, transfer, false, acts);
16655                 if (ret) {
16656                         DRV_LOG(ERR, "Failed to create default policy rules.");
16657                         goto def_policy_error;
16658                 }
16659         }
16660         return 0;
16661 def_policy_error:
16662         __flow_dv_destroy_domain_def_policy(dev,
16663                                             (enum mlx5_meter_domain)domain);
16664         return -1;
16665 }
16666
16667 /**
16668  * Create the default policy table set.
16669  *
16670  * @param[in] dev
16671  *   Pointer to Ethernet device.
16672  * @return
16673  *   0 on success, -1 otherwise.
16674  */
16675 static int
16676 flow_dv_create_def_policy(struct rte_eth_dev *dev)
16677 {
16678         struct mlx5_priv *priv = dev->data->dev_private;
16679         int i;
16680
16681         /* Non-termination policy table. */
16682         for (i = 0; i < MLX5_MTR_DOMAIN_MAX; i++) {
16683                 if (!priv->config.dv_esw_en && i == MLX5_MTR_DOMAIN_TRANSFER)
16684                         continue;
16685                 if (__flow_dv_create_domain_def_policy(dev, i)) {
16686                         DRV_LOG(ERR, "Failed to create default policy");
16687                         /* Rollback the created default policies for others. */
16688                         flow_dv_destroy_def_policy(dev);
16689                         return -1;
16690                 }
16691         }
16692         return 0;
16693 }
16694
16695 /**
16696  * Create the needed meter tables.
16697  * Lock free, (mutex should be acquired by caller).
16698  *
16699  * @param[in] dev
16700  *   Pointer to Ethernet device.
16701  * @param[in] fm
16702  *   Meter information table.
16703  * @param[in] mtr_idx
16704  *   Meter index.
16705  * @param[in] domain_bitmap
16706  *   Domain bitmap.
16707  * @return
16708  *   0 on success, -1 otherwise.
16709  */
16710 static int
16711 flow_dv_create_mtr_tbls(struct rte_eth_dev *dev,
16712                         struct mlx5_flow_meter_info *fm,
16713                         uint32_t mtr_idx,
16714                         uint8_t domain_bitmap)
16715 {
16716         struct mlx5_priv *priv = dev->data->dev_private;
16717         struct mlx5_flow_mtr_mng *mtrmng = priv->sh->mtrmng;
16718         struct rte_flow_error error;
16719         struct mlx5_flow_tbl_data_entry *tbl_data;
16720         uint8_t egress, transfer;
16721         void *actions[METER_ACTIONS];
16722         int domain, ret, i;
16723         struct mlx5_flow_counter *cnt;
16724         struct mlx5_flow_dv_match_params value = {
16725                 .size = sizeof(value.buf),
16726         };
16727         struct mlx5_flow_dv_match_params matcher_para = {
16728                 .size = sizeof(matcher_para.buf),
16729         };
16730         int mtr_id_reg_c = mlx5_flow_get_reg_id(dev, MLX5_MTR_ID,
16731                                                      0, &error);
16732         uint32_t mtr_id_mask = (UINT32_C(1) << mtrmng->max_mtr_bits) - 1;
16733         uint8_t mtr_id_offset = priv->mtr_reg_share ? MLX5_MTR_COLOR_BITS : 0;
16734         struct mlx5_list_entry *entry;
16735         struct mlx5_flow_dv_matcher matcher = {
16736                 .mask = {
16737                         .size = sizeof(matcher.mask.buf),
16738                 },
16739         };
16740         struct mlx5_flow_dv_matcher *drop_matcher;
16741         struct mlx5_flow_cb_ctx ctx = {
16742                 .error = &error,
16743                 .data = &matcher,
16744         };
16745         uint8_t misc_mask;
16746
16747         if (!priv->mtr_en || mtr_id_reg_c < 0) {
16748                 rte_errno = ENOTSUP;
16749                 return -1;
16750         }
16751         for (domain = 0; domain < MLX5_MTR_DOMAIN_MAX; domain++) {
16752                 if (!(domain_bitmap & (1 << domain)) ||
16753                         (mtrmng->def_rule[domain] && !fm->drop_cnt))
16754                         continue;
16755                 egress = (domain == MLX5_MTR_DOMAIN_EGRESS) ? 1 : 0;
16756                 transfer = (domain == MLX5_MTR_DOMAIN_TRANSFER) ? 1 : 0;
16757                 /* Create the drop table with METER DROP level. */
16758                 if (!mtrmng->drop_tbl[domain]) {
16759                         mtrmng->drop_tbl[domain] = flow_dv_tbl_resource_get(dev,
16760                                         MLX5_FLOW_TABLE_LEVEL_METER,
16761                                         egress, transfer, false, NULL, 0,
16762                                         0, MLX5_MTR_TABLE_ID_DROP, &error);
16763                         if (!mtrmng->drop_tbl[domain]) {
16764                                 DRV_LOG(ERR, "Failed to create meter drop table.");
16765                                 goto policy_error;
16766                         }
16767                 }
16768                 /* Create default matcher in drop table. */
16769                 matcher.tbl = mtrmng->drop_tbl[domain],
16770                 tbl_data = container_of(mtrmng->drop_tbl[domain],
16771                                 struct mlx5_flow_tbl_data_entry, tbl);
16772                 if (!mtrmng->def_matcher[domain]) {
16773                         flow_dv_match_meta_reg(matcher.mask.buf, value.buf,
16774                                        (enum modify_reg)mtr_id_reg_c,
16775                                        0, 0);
16776                         matcher.priority = MLX5_MTRS_DEFAULT_RULE_PRIORITY;
16777                         matcher.crc = rte_raw_cksum
16778                                         ((const void *)matcher.mask.buf,
16779                                         matcher.mask.size);
16780                         entry = mlx5_list_register(tbl_data->matchers, &ctx);
16781                         if (!entry) {
16782                                 DRV_LOG(ERR, "Failed to register meter "
16783                                 "drop default matcher.");
16784                                 goto policy_error;
16785                         }
16786                         mtrmng->def_matcher[domain] = container_of(entry,
16787                         struct mlx5_flow_dv_matcher, entry);
16788                 }
16789                 /* Create default rule in drop table. */
16790                 if (!mtrmng->def_rule[domain]) {
16791                         i = 0;
16792                         actions[i++] = priv->sh->dr_drop_action;
16793                         flow_dv_match_meta_reg(matcher_para.buf, value.buf,
16794                                 (enum modify_reg)mtr_id_reg_c, 0, 0);
16795                         misc_mask = flow_dv_matcher_enable(value.buf);
16796                         __flow_dv_adjust_buf_size(&value.size, misc_mask);
16797                         ret = mlx5_flow_os_create_flow
16798                                 (mtrmng->def_matcher[domain]->matcher_object,
16799                                 (void *)&value, i, actions,
16800                                 &mtrmng->def_rule[domain]);
16801                         if (ret) {
16802                                 DRV_LOG(ERR, "Failed to create meter "
16803                                 "default drop rule for drop table.");
16804                                 goto policy_error;
16805                         }
16806                 }
16807                 if (!fm->drop_cnt)
16808                         continue;
16809                 MLX5_ASSERT(mtrmng->max_mtr_bits);
16810                 if (!mtrmng->drop_matcher[domain][mtrmng->max_mtr_bits - 1]) {
16811                         /* Create matchers for Drop. */
16812                         flow_dv_match_meta_reg(matcher.mask.buf, value.buf,
16813                                         (enum modify_reg)mtr_id_reg_c, 0,
16814                                         (mtr_id_mask << mtr_id_offset));
16815                         matcher.priority = MLX5_REG_BITS - mtrmng->max_mtr_bits;
16816                         matcher.crc = rte_raw_cksum
16817                                         ((const void *)matcher.mask.buf,
16818                                         matcher.mask.size);
16819                         entry = mlx5_list_register(tbl_data->matchers, &ctx);
16820                         if (!entry) {
16821                                 DRV_LOG(ERR,
16822                                 "Failed to register meter drop matcher.");
16823                                 goto policy_error;
16824                         }
16825                         mtrmng->drop_matcher[domain][mtrmng->max_mtr_bits - 1] =
16826                                 container_of(entry, struct mlx5_flow_dv_matcher,
16827                                              entry);
16828                 }
16829                 drop_matcher =
16830                         mtrmng->drop_matcher[domain][mtrmng->max_mtr_bits - 1];
16831                 /* Create drop rule, matching meter_id only. */
16832                 flow_dv_match_meta_reg(matcher_para.buf, value.buf,
16833                                 (enum modify_reg)mtr_id_reg_c,
16834                                 (mtr_idx << mtr_id_offset), UINT32_MAX);
16835                 i = 0;
16836                 cnt = flow_dv_counter_get_by_idx(dev,
16837                                         fm->drop_cnt, NULL);
16838                 actions[i++] = cnt->action;
16839                 actions[i++] = priv->sh->dr_drop_action;
16840                 misc_mask = flow_dv_matcher_enable(value.buf);
16841                 __flow_dv_adjust_buf_size(&value.size, misc_mask);
16842                 ret = mlx5_flow_os_create_flow(drop_matcher->matcher_object,
16843                                                (void *)&value, i, actions,
16844                                                &fm->drop_rule[domain]);
16845                 if (ret) {
16846                         DRV_LOG(ERR, "Failed to create meter "
16847                                 "drop rule for drop table.");
16848                                 goto policy_error;
16849                 }
16850         }
16851         return 0;
16852 policy_error:
16853         for (i = 0; i < MLX5_MTR_DOMAIN_MAX; i++) {
16854                 if (fm->drop_rule[i]) {
16855                         claim_zero(mlx5_flow_os_destroy_flow
16856                                 (fm->drop_rule[i]));
16857                         fm->drop_rule[i] = NULL;
16858                 }
16859         }
16860         return -1;
16861 }
16862
16863 static struct mlx5_flow_meter_sub_policy *
16864 __flow_dv_meter_get_rss_sub_policy(struct rte_eth_dev *dev,
16865                 struct mlx5_flow_meter_policy *mtr_policy,
16866                 struct mlx5_flow_rss_desc *rss_desc[MLX5_MTR_RTE_COLORS],
16867                 struct mlx5_flow_meter_sub_policy *next_sub_policy,
16868                 bool *is_reuse)
16869 {
16870         struct mlx5_priv *priv = dev->data->dev_private;
16871         struct mlx5_flow_meter_sub_policy *sub_policy = NULL;
16872         uint32_t sub_policy_idx = 0;
16873         uint32_t hrxq_idx[MLX5_MTR_RTE_COLORS] = {0};
16874         uint32_t i, j;
16875         struct mlx5_hrxq *hrxq;
16876         struct mlx5_flow_handle dh;
16877         struct mlx5_meter_policy_action_container *act_cnt;
16878         uint32_t domain = MLX5_MTR_DOMAIN_INGRESS;
16879         uint16_t sub_policy_num;
16880
16881         rte_spinlock_lock(&mtr_policy->sl);
16882         for (i = 0; i < MLX5_MTR_RTE_COLORS; i++) {
16883                 if (!rss_desc[i])
16884                         continue;
16885                 hrxq_idx[i] = mlx5_hrxq_get(dev, rss_desc[i]);
16886                 if (!hrxq_idx[i]) {
16887                         rte_spinlock_unlock(&mtr_policy->sl);
16888                         return NULL;
16889                 }
16890         }
16891         sub_policy_num = (mtr_policy->sub_policy_num >>
16892                         (MLX5_MTR_SUB_POLICY_NUM_SHIFT * domain)) &
16893                         MLX5_MTR_SUB_POLICY_NUM_MASK;
16894         for (j = 0; j < sub_policy_num; j++) {
16895                 for (i = 0; i < MLX5_MTR_RTE_COLORS; i++) {
16896                         if (rss_desc[i] &&
16897                             hrxq_idx[i] !=
16898                             mtr_policy->sub_policys[domain][j]->rix_hrxq[i])
16899                                 break;
16900                 }
16901                 if (i >= MLX5_MTR_RTE_COLORS) {
16902                         /*
16903                          * Found the sub policy table with
16904                          * the same queue per color.
16905                          */
16906                         rte_spinlock_unlock(&mtr_policy->sl);
16907                         for (i = 0; i < MLX5_MTR_RTE_COLORS; i++)
16908                                 mlx5_hrxq_release(dev, hrxq_idx[i]);
16909                         *is_reuse = true;
16910                         return mtr_policy->sub_policys[domain][j];
16911                 }
16912         }
16913         /* Create sub policy. */
16914         if (!mtr_policy->sub_policys[domain][0]->rix_hrxq[0]) {
16915                 /* Reuse the first pre-allocated sub_policy. */
16916                 sub_policy = mtr_policy->sub_policys[domain][0];
16917                 sub_policy_idx = sub_policy->idx;
16918         } else {
16919                 sub_policy = mlx5_ipool_zmalloc
16920                                 (priv->sh->ipool[MLX5_IPOOL_MTR_POLICY],
16921                                  &sub_policy_idx);
16922                 if (!sub_policy ||
16923                     sub_policy_idx > MLX5_MAX_SUB_POLICY_TBL_NUM) {
16924                         for (i = 0; i < MLX5_MTR_RTE_COLORS; i++)
16925                                 mlx5_hrxq_release(dev, hrxq_idx[i]);
16926                         goto rss_sub_policy_error;
16927                 }
16928                 sub_policy->idx = sub_policy_idx;
16929                 sub_policy->main_policy = mtr_policy;
16930         }
16931         for (i = 0; i < MLX5_MTR_RTE_COLORS; i++) {
16932                 if (!rss_desc[i])
16933                         continue;
16934                 sub_policy->rix_hrxq[i] = hrxq_idx[i];
16935                 if (mtr_policy->is_hierarchy) {
16936                         act_cnt = &mtr_policy->act_cnt[i];
16937                         act_cnt->next_sub_policy = next_sub_policy;
16938                         mlx5_hrxq_release(dev, hrxq_idx[i]);
16939                 } else {
16940                         /*
16941                          * Overwrite the last action from
16942                          * RSS action to Queue action.
16943                          */
16944                         hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ],
16945                                               hrxq_idx[i]);
16946                         if (!hrxq) {
16947                                 DRV_LOG(ERR, "Failed to get policy hrxq");
16948                                 goto rss_sub_policy_error;
16949                         }
16950                         act_cnt = &mtr_policy->act_cnt[i];
16951                         if (act_cnt->rix_mark || act_cnt->modify_hdr) {
16952                                 memset(&dh, 0, sizeof(struct mlx5_flow_handle));
16953                                 if (act_cnt->rix_mark)
16954                                         dh.mark = 1;
16955                                 dh.fate_action = MLX5_FLOW_FATE_QUEUE;
16956                                 dh.rix_hrxq = hrxq_idx[i];
16957                                 flow_drv_rxq_flags_set(dev, &dh);
16958                         }
16959                 }
16960         }
16961         if (__flow_dv_create_policy_acts_rules(dev, mtr_policy,
16962                                                sub_policy, domain)) {
16963                 DRV_LOG(ERR, "Failed to create policy "
16964                         "rules for ingress domain.");
16965                 goto rss_sub_policy_error;
16966         }
16967         if (sub_policy != mtr_policy->sub_policys[domain][0]) {
16968                 i = (mtr_policy->sub_policy_num >>
16969                         (MLX5_MTR_SUB_POLICY_NUM_SHIFT * domain)) &
16970                         MLX5_MTR_SUB_POLICY_NUM_MASK;
16971                 if (i >= MLX5_MTR_RSS_MAX_SUB_POLICY) {
16972                         DRV_LOG(ERR, "No free sub-policy slot.");
16973                         goto rss_sub_policy_error;
16974                 }
16975                 mtr_policy->sub_policys[domain][i] = sub_policy;
16976                 i++;
16977                 mtr_policy->sub_policy_num &= ~(MLX5_MTR_SUB_POLICY_NUM_MASK <<
16978                         (MLX5_MTR_SUB_POLICY_NUM_SHIFT * domain));
16979                 mtr_policy->sub_policy_num |=
16980                         (i & MLX5_MTR_SUB_POLICY_NUM_MASK) <<
16981                         (MLX5_MTR_SUB_POLICY_NUM_SHIFT * domain);
16982         }
16983         rte_spinlock_unlock(&mtr_policy->sl);
16984         *is_reuse = false;
16985         return sub_policy;
16986 rss_sub_policy_error:
16987         if (sub_policy) {
16988                 __flow_dv_destroy_sub_policy_rules(dev, sub_policy);
16989                 if (sub_policy != mtr_policy->sub_policys[domain][0]) {
16990                         i = (mtr_policy->sub_policy_num >>
16991                         (MLX5_MTR_SUB_POLICY_NUM_SHIFT * domain)) &
16992                         MLX5_MTR_SUB_POLICY_NUM_MASK;
16993                         mtr_policy->sub_policys[domain][i] = NULL;
16994                         mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MTR_POLICY],
16995                                         sub_policy->idx);
16996                 }
16997         }
16998         rte_spinlock_unlock(&mtr_policy->sl);
16999         return NULL;
17000 }
17001
17002 /**
17003  * Find the policy table for prefix table with RSS.
17004  *
17005  * @param[in] dev
17006  *   Pointer to Ethernet device.
17007  * @param[in] mtr_policy
17008  *   Pointer to meter policy table.
17009  * @param[in] rss_desc
17010  *   Pointer to rss_desc
17011  * @return
17012  *   Pointer to table set on success, NULL otherwise and rte_errno is set.
17013  */
17014 static struct mlx5_flow_meter_sub_policy *
17015 flow_dv_meter_sub_policy_rss_prepare(struct rte_eth_dev *dev,
17016                 struct mlx5_flow_meter_policy *mtr_policy,
17017                 struct mlx5_flow_rss_desc *rss_desc[MLX5_MTR_RTE_COLORS])
17018 {
17019         struct mlx5_priv *priv = dev->data->dev_private;
17020         struct mlx5_flow_meter_sub_policy *sub_policy = NULL;
17021         struct mlx5_flow_meter_info *next_fm;
17022         struct mlx5_flow_meter_policy *next_policy;
17023         struct mlx5_flow_meter_sub_policy *next_sub_policy = NULL;
17024         struct mlx5_flow_meter_policy *policies[MLX5_MTR_CHAIN_MAX_NUM];
17025         struct mlx5_flow_meter_sub_policy *sub_policies[MLX5_MTR_CHAIN_MAX_NUM];
17026         uint32_t domain = MLX5_MTR_DOMAIN_INGRESS;
17027         bool reuse_sub_policy;
17028         uint32_t i = 0;
17029         uint32_t j = 0;
17030
17031         while (true) {
17032                 /* Iterate hierarchy to get all policies in this hierarchy. */
17033                 policies[i++] = mtr_policy;
17034                 if (!mtr_policy->is_hierarchy)
17035                         break;
17036                 if (i >= MLX5_MTR_CHAIN_MAX_NUM) {
17037                         DRV_LOG(ERR, "Exceed max meter number in hierarchy.");
17038                         return NULL;
17039                 }
17040                 next_fm = mlx5_flow_meter_find(priv,
17041                         mtr_policy->act_cnt[RTE_COLOR_GREEN].next_mtr_id, NULL);
17042                 if (!next_fm) {
17043                         DRV_LOG(ERR, "Failed to get next meter in hierarchy.");
17044                         return NULL;
17045                 }
17046                 next_policy =
17047                         mlx5_flow_meter_policy_find(dev, next_fm->policy_id,
17048                                                     NULL);
17049                 MLX5_ASSERT(next_policy);
17050                 mtr_policy = next_policy;
17051         }
17052         while (i) {
17053                 /**
17054                  * From last policy to the first one in hierarchy,
17055                  * create / get the sub policy for each of them.
17056                  */
17057                 sub_policy = __flow_dv_meter_get_rss_sub_policy(dev,
17058                                                         policies[--i],
17059                                                         rss_desc,
17060                                                         next_sub_policy,
17061                                                         &reuse_sub_policy);
17062                 if (!sub_policy) {
17063                         DRV_LOG(ERR, "Failed to get the sub policy.");
17064                         goto err_exit;
17065                 }
17066                 if (!reuse_sub_policy)
17067                         sub_policies[j++] = sub_policy;
17068                 next_sub_policy = sub_policy;
17069         }
17070         return sub_policy;
17071 err_exit:
17072         while (j) {
17073                 uint16_t sub_policy_num;
17074
17075                 sub_policy = sub_policies[--j];
17076                 mtr_policy = sub_policy->main_policy;
17077                 __flow_dv_destroy_sub_policy_rules(dev, sub_policy);
17078                 if (sub_policy != mtr_policy->sub_policys[domain][0]) {
17079                         sub_policy_num = (mtr_policy->sub_policy_num >>
17080                                 (MLX5_MTR_SUB_POLICY_NUM_SHIFT * domain)) &
17081                                 MLX5_MTR_SUB_POLICY_NUM_MASK;
17082                         mtr_policy->sub_policys[domain][sub_policy_num - 1] =
17083                                                                         NULL;
17084                         sub_policy_num--;
17085                         mtr_policy->sub_policy_num &=
17086                                 ~(MLX5_MTR_SUB_POLICY_NUM_MASK <<
17087                                   (MLX5_MTR_SUB_POLICY_NUM_SHIFT * i));
17088                         mtr_policy->sub_policy_num |=
17089                         (sub_policy_num & MLX5_MTR_SUB_POLICY_NUM_MASK) <<
17090                         (MLX5_MTR_SUB_POLICY_NUM_SHIFT * i);
17091                         mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MTR_POLICY],
17092                                         sub_policy->idx);
17093                 }
17094         }
17095         return NULL;
17096 }
17097
17098 /**
17099  * Create the sub policy tag rule for all meters in hierarchy.
17100  *
17101  * @param[in] dev
17102  *   Pointer to Ethernet device.
17103  * @param[in] fm
17104  *   Meter information table.
17105  * @param[in] src_port
17106  *   The src port this extra rule should use.
17107  * @param[in] item
17108  *   The src port match item.
17109  * @param[out] error
17110  *   Perform verbose error reporting if not NULL.
17111  * @return
17112  *   0 on success, a negative errno value otherwise and rte_errno is set.
17113  */
17114 static int
17115 flow_dv_meter_hierarchy_rule_create(struct rte_eth_dev *dev,
17116                                 struct mlx5_flow_meter_info *fm,
17117                                 int32_t src_port,
17118                                 const struct rte_flow_item *item,
17119                                 struct rte_flow_error *error)
17120 {
17121         struct mlx5_priv *priv = dev->data->dev_private;
17122         struct mlx5_flow_meter_policy *mtr_policy;
17123         struct mlx5_flow_meter_sub_policy *sub_policy;
17124         struct mlx5_flow_meter_info *next_fm = NULL;
17125         struct mlx5_flow_meter_policy *next_policy;
17126         struct mlx5_flow_meter_sub_policy *next_sub_policy;
17127         struct mlx5_flow_tbl_data_entry *tbl_data;
17128         struct mlx5_sub_policy_color_rule *color_rule;
17129         struct mlx5_meter_policy_acts acts;
17130         uint32_t color_reg_c_idx;
17131         bool mtr_first = (src_port != UINT16_MAX) ? true : false;
17132         struct rte_flow_attr attr = {
17133                 .group = MLX5_FLOW_TABLE_LEVEL_POLICY,
17134                 .priority = 0,
17135                 .ingress = 0,
17136                 .egress = 0,
17137                 .transfer = 1,
17138                 .reserved = 0,
17139         };
17140         uint32_t domain = MLX5_MTR_DOMAIN_TRANSFER;
17141         int i;
17142
17143         mtr_policy = mlx5_flow_meter_policy_find(dev, fm->policy_id, NULL);
17144         MLX5_ASSERT(mtr_policy);
17145         if (!mtr_policy->is_hierarchy)
17146                 return 0;
17147         next_fm = mlx5_flow_meter_find(priv,
17148                         mtr_policy->act_cnt[RTE_COLOR_GREEN].next_mtr_id, NULL);
17149         if (!next_fm) {
17150                 return rte_flow_error_set(error, EINVAL,
17151                                 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
17152                                 "Failed to find next meter in hierarchy.");
17153         }
17154         if (!next_fm->drop_cnt)
17155                 goto exit;
17156         color_reg_c_idx = mlx5_flow_get_reg_id(dev, MLX5_MTR_COLOR, 0, error);
17157         sub_policy = mtr_policy->sub_policys[domain][0];
17158         for (i = 0; i < RTE_COLORS; i++) {
17159                 bool rule_exist = false;
17160                 struct mlx5_meter_policy_action_container *act_cnt;
17161
17162                 if (i >= RTE_COLOR_YELLOW)
17163                         break;
17164                 TAILQ_FOREACH(color_rule,
17165                               &sub_policy->color_rules[i], next_port)
17166                         if (color_rule->src_port == src_port) {
17167                                 rule_exist = true;
17168                                 break;
17169                         }
17170                 if (rule_exist)
17171                         continue;
17172                 color_rule = mlx5_malloc(MLX5_MEM_ZERO,
17173                                 sizeof(struct mlx5_sub_policy_color_rule),
17174                                 0, SOCKET_ID_ANY);
17175                 if (!color_rule)
17176                         return rte_flow_error_set(error, ENOMEM,
17177                                 RTE_FLOW_ERROR_TYPE_ACTION,
17178                                 NULL, "No memory to create tag color rule.");
17179                 color_rule->src_port = src_port;
17180                 attr.priority = i;
17181                 next_policy = mlx5_flow_meter_policy_find(dev,
17182                                                 next_fm->policy_id, NULL);
17183                 MLX5_ASSERT(next_policy);
17184                 next_sub_policy = next_policy->sub_policys[domain][0];
17185                 tbl_data = container_of(next_sub_policy->tbl_rsc,
17186                                         struct mlx5_flow_tbl_data_entry, tbl);
17187                 act_cnt = &mtr_policy->act_cnt[i];
17188                 if (mtr_first) {
17189                         acts.dv_actions[0] = next_fm->meter_action;
17190                         acts.dv_actions[1] = act_cnt->modify_hdr->action;
17191                 } else {
17192                         acts.dv_actions[0] = act_cnt->modify_hdr->action;
17193                         acts.dv_actions[1] = next_fm->meter_action;
17194                 }
17195                 acts.dv_actions[2] = tbl_data->jump.action;
17196                 acts.actions_n = 3;
17197                 if (mlx5_flow_meter_attach(priv, next_fm, &attr, error)) {
17198                         next_fm = NULL;
17199                         goto err_exit;
17200                 }
17201                 if (__flow_dv_create_policy_matcher(dev, color_reg_c_idx,
17202                                 MLX5_MTR_POLICY_MATCHER_PRIO, sub_policy,
17203                                 &attr, true, item,
17204                                 &color_rule->matcher, error)) {
17205                         rte_flow_error_set(error, errno,
17206                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
17207                                 "Failed to create hierarchy meter matcher.");
17208                         goto err_exit;
17209                 }
17210                 if (__flow_dv_create_policy_flow(dev, color_reg_c_idx,
17211                                         (enum rte_color)i,
17212                                         color_rule->matcher->matcher_object,
17213                                         acts.actions_n, acts.dv_actions,
17214                                         true, item,
17215                                         &color_rule->rule, &attr)) {
17216                         rte_flow_error_set(error, errno,
17217                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
17218                                 "Failed to create hierarchy meter rule.");
17219                         goto err_exit;
17220                 }
17221                 TAILQ_INSERT_TAIL(&sub_policy->color_rules[i],
17222                                   color_rule, next_port);
17223         }
17224 exit:
17225         /**
17226          * Recursive call to iterate all meters in hierarchy and
17227          * create needed rules.
17228          */
17229         return flow_dv_meter_hierarchy_rule_create(dev, next_fm,
17230                                                 src_port, item, error);
17231 err_exit:
17232         if (color_rule) {
17233                 if (color_rule->rule)
17234                         mlx5_flow_os_destroy_flow(color_rule->rule);
17235                 if (color_rule->matcher) {
17236                         struct mlx5_flow_tbl_data_entry *tbl =
17237                                 container_of(color_rule->matcher->tbl,
17238                                                 typeof(*tbl), tbl);
17239                         mlx5_list_unregister(tbl->matchers,
17240                                                 &color_rule->matcher->entry);
17241                 }
17242                 mlx5_free(color_rule);
17243         }
17244         if (next_fm)
17245                 mlx5_flow_meter_detach(priv, next_fm);
17246         return -rte_errno;
17247 }
17248
17249 /**
17250  * Destroy the sub policy table with RX queue.
17251  *
17252  * @param[in] dev
17253  *   Pointer to Ethernet device.
17254  * @param[in] mtr_policy
17255  *   Pointer to meter policy table.
17256  */
17257 static void
17258 flow_dv_destroy_sub_policy_with_rxq(struct rte_eth_dev *dev,
17259                                     struct mlx5_flow_meter_policy *mtr_policy)
17260 {
17261         struct mlx5_priv *priv = dev->data->dev_private;
17262         struct mlx5_flow_meter_sub_policy *sub_policy = NULL;
17263         uint32_t domain = MLX5_MTR_DOMAIN_INGRESS;
17264         uint32_t i, j;
17265         uint16_t sub_policy_num, new_policy_num;
17266
17267         rte_spinlock_lock(&mtr_policy->sl);
17268         for (i = 0; i < MLX5_MTR_RTE_COLORS; i++) {
17269                 switch (mtr_policy->act_cnt[i].fate_action) {
17270                 case MLX5_FLOW_FATE_SHARED_RSS:
17271                         sub_policy_num = (mtr_policy->sub_policy_num >>
17272                         (MLX5_MTR_SUB_POLICY_NUM_SHIFT * domain)) &
17273                         MLX5_MTR_SUB_POLICY_NUM_MASK;
17274                         new_policy_num = sub_policy_num;
17275                         for (j = 0; j < sub_policy_num; j++) {
17276                                 sub_policy =
17277                                         mtr_policy->sub_policys[domain][j];
17278                                 if (sub_policy) {
17279                                         __flow_dv_destroy_sub_policy_rules(dev,
17280                                                 sub_policy);
17281                                 if (sub_policy !=
17282                                         mtr_policy->sub_policys[domain][0]) {
17283                                         mtr_policy->sub_policys[domain][j] =
17284                                                                 NULL;
17285                                         mlx5_ipool_free
17286                                 (priv->sh->ipool[MLX5_IPOOL_MTR_POLICY],
17287                                                 sub_policy->idx);
17288                                                 new_policy_num--;
17289                                         }
17290                                 }
17291                         }
17292                         if (new_policy_num != sub_policy_num) {
17293                                 mtr_policy->sub_policy_num &=
17294                                 ~(MLX5_MTR_SUB_POLICY_NUM_MASK <<
17295                                 (MLX5_MTR_SUB_POLICY_NUM_SHIFT * domain));
17296                                 mtr_policy->sub_policy_num |=
17297                                 (new_policy_num &
17298                                         MLX5_MTR_SUB_POLICY_NUM_MASK) <<
17299                                 (MLX5_MTR_SUB_POLICY_NUM_SHIFT * domain);
17300                         }
17301                         break;
17302                 case MLX5_FLOW_FATE_QUEUE:
17303                         sub_policy = mtr_policy->sub_policys[domain][0];
17304                         __flow_dv_destroy_sub_policy_rules(dev,
17305                                                            sub_policy);
17306                         break;
17307                 default:
17308                         /*Other actions without queue and do nothing*/
17309                         break;
17310                 }
17311         }
17312         rte_spinlock_unlock(&mtr_policy->sl);
17313 }
17314 /**
17315  * Check whether the DR drop action is supported on the root table or not.
17316  *
17317  * Create a simple flow with DR drop action on root table to validate
17318  * if DR drop action on root table is supported or not.
17319  *
17320  * @param[in] dev
17321  *   Pointer to rte_eth_dev structure.
17322  *
17323  * @return
17324  *   0 on success, a negative errno value otherwise and rte_errno is set.
17325  */
17326 int
17327 mlx5_flow_discover_dr_action_support(struct rte_eth_dev *dev)
17328 {
17329         struct mlx5_priv *priv = dev->data->dev_private;
17330         struct mlx5_dev_ctx_shared *sh = priv->sh;
17331         struct mlx5_flow_dv_match_params mask = {
17332                 .size = sizeof(mask.buf),
17333         };
17334         struct mlx5_flow_dv_match_params value = {
17335                 .size = sizeof(value.buf),
17336         };
17337         struct mlx5dv_flow_matcher_attr dv_attr = {
17338                 .type = IBV_FLOW_ATTR_NORMAL,
17339                 .priority = 0,
17340                 .match_criteria_enable = 0,
17341                 .match_mask = (void *)&mask,
17342         };
17343         struct mlx5_flow_tbl_resource *tbl = NULL;
17344         void *matcher = NULL;
17345         void *flow = NULL;
17346         int ret = -1;
17347
17348         tbl = flow_dv_tbl_resource_get(dev, 0, 0, 0, false, NULL,
17349                                         0, 0, 0, NULL);
17350         if (!tbl)
17351                 goto err;
17352         dv_attr.match_criteria_enable = flow_dv_matcher_enable(mask.buf);
17353         __flow_dv_adjust_buf_size(&mask.size, dv_attr.match_criteria_enable);
17354         ret = mlx5_flow_os_create_flow_matcher(sh->cdev->ctx, &dv_attr,
17355                                                tbl->obj, &matcher);
17356         if (ret)
17357                 goto err;
17358         __flow_dv_adjust_buf_size(&value.size, dv_attr.match_criteria_enable);
17359         ret = mlx5_flow_os_create_flow(matcher, (void *)&value, 1,
17360                                        &sh->dr_drop_action, &flow);
17361 err:
17362         /*
17363          * If DR drop action is not supported on root table, flow create will
17364          * be failed with EOPNOTSUPP or EPROTONOSUPPORT.
17365          */
17366         if (!flow) {
17367                 if (matcher &&
17368                     (errno == EPROTONOSUPPORT || errno == EOPNOTSUPP))
17369                         DRV_LOG(INFO, "DR drop action is not supported in root table.");
17370                 else
17371                         DRV_LOG(ERR, "Unexpected error in DR drop action support detection");
17372                 ret = -1;
17373         } else {
17374                 claim_zero(mlx5_flow_os_destroy_flow(flow));
17375         }
17376         if (matcher)
17377                 claim_zero(mlx5_flow_os_destroy_flow_matcher(matcher));
17378         if (tbl)
17379                 flow_dv_tbl_resource_release(MLX5_SH(dev), tbl);
17380         return ret;
17381 }
17382
17383 /**
17384  * Validate the batch counter support in root table.
17385  *
17386  * Create a simple flow with invalid counter and drop action on root table to
17387  * validate if batch counter with offset on root table is supported or not.
17388  *
17389  * @param[in] dev
17390  *   Pointer to rte_eth_dev structure.
17391  *
17392  * @return
17393  *   0 on success, a negative errno value otherwise and rte_errno is set.
17394  */
17395 int
17396 mlx5_flow_dv_discover_counter_offset_support(struct rte_eth_dev *dev)
17397 {
17398         struct mlx5_priv *priv = dev->data->dev_private;
17399         struct mlx5_dev_ctx_shared *sh = priv->sh;
17400         struct mlx5_flow_dv_match_params mask = {
17401                 .size = sizeof(mask.buf),
17402         };
17403         struct mlx5_flow_dv_match_params value = {
17404                 .size = sizeof(value.buf),
17405         };
17406         struct mlx5dv_flow_matcher_attr dv_attr = {
17407                 .type = IBV_FLOW_ATTR_NORMAL | IBV_FLOW_ATTR_FLAGS_EGRESS,
17408                 .priority = 0,
17409                 .match_criteria_enable = 0,
17410                 .match_mask = (void *)&mask,
17411         };
17412         void *actions[2] = { 0 };
17413         struct mlx5_flow_tbl_resource *tbl = NULL;
17414         struct mlx5_devx_obj *dcs = NULL;
17415         void *matcher = NULL;
17416         void *flow = NULL;
17417         int ret = -1;
17418
17419         tbl = flow_dv_tbl_resource_get(dev, 0, 1, 0, false, NULL,
17420                                         0, 0, 0, NULL);
17421         if (!tbl)
17422                 goto err;
17423         dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->cdev->ctx, 0x4);
17424         if (!dcs)
17425                 goto err;
17426         ret = mlx5_flow_os_create_flow_action_count(dcs->obj, UINT16_MAX,
17427                                                     &actions[0]);
17428         if (ret)
17429                 goto err;
17430         dv_attr.match_criteria_enable = flow_dv_matcher_enable(mask.buf);
17431         __flow_dv_adjust_buf_size(&mask.size, dv_attr.match_criteria_enable);
17432         ret = mlx5_flow_os_create_flow_matcher(sh->cdev->ctx, &dv_attr,
17433                                                tbl->obj, &matcher);
17434         if (ret)
17435                 goto err;
17436         __flow_dv_adjust_buf_size(&value.size, dv_attr.match_criteria_enable);
17437         ret = mlx5_flow_os_create_flow(matcher, (void *)&value, 1,
17438                                        actions, &flow);
17439 err:
17440         /*
17441          * If batch counter with offset is not supported, the driver will not
17442          * validate the invalid offset value, flow create should success.
17443          * In this case, it means batch counter is not supported in root table.
17444          *
17445          * Otherwise, if flow create is failed, counter offset is supported.
17446          */
17447         if (flow) {
17448                 DRV_LOG(INFO, "Batch counter is not supported in root "
17449                               "table. Switch to fallback mode.");
17450                 rte_errno = ENOTSUP;
17451                 ret = -rte_errno;
17452                 claim_zero(mlx5_flow_os_destroy_flow(flow));
17453         } else {
17454                 /* Check matcher to make sure validate fail at flow create. */
17455                 if (!matcher || (matcher && errno != EINVAL))
17456                         DRV_LOG(ERR, "Unexpected error in counter offset "
17457                                      "support detection");
17458                 ret = 0;
17459         }
17460         if (actions[0])
17461                 claim_zero(mlx5_flow_os_destroy_flow_action(actions[0]));
17462         if (matcher)
17463                 claim_zero(mlx5_flow_os_destroy_flow_matcher(matcher));
17464         if (tbl)
17465                 flow_dv_tbl_resource_release(MLX5_SH(dev), tbl);
17466         if (dcs)
17467                 claim_zero(mlx5_devx_cmd_destroy(dcs));
17468         return ret;
17469 }
17470
17471 /**
17472  * Query a devx counter.
17473  *
17474  * @param[in] dev
17475  *   Pointer to the Ethernet device structure.
17476  * @param[in] cnt
17477  *   Index to the flow counter.
17478  * @param[in] clear
17479  *   Set to clear the counter statistics.
17480  * @param[out] pkts
17481  *   The statistics value of packets.
17482  * @param[out] bytes
17483  *   The statistics value of bytes.
17484  *
17485  * @return
17486  *   0 on success, otherwise return -1.
17487  */
17488 static int
17489 flow_dv_counter_query(struct rte_eth_dev *dev, uint32_t counter, bool clear,
17490                       uint64_t *pkts, uint64_t *bytes)
17491 {
17492         struct mlx5_priv *priv = dev->data->dev_private;
17493         struct mlx5_flow_counter *cnt;
17494         uint64_t inn_pkts, inn_bytes;
17495         int ret;
17496
17497         if (!priv->sh->devx)
17498                 return -1;
17499
17500         ret = _flow_dv_query_count(dev, counter, &inn_pkts, &inn_bytes);
17501         if (ret)
17502                 return -1;
17503         cnt = flow_dv_counter_get_by_idx(dev, counter, NULL);
17504         *pkts = inn_pkts - cnt->hits;
17505         *bytes = inn_bytes - cnt->bytes;
17506         if (clear) {
17507                 cnt->hits = inn_pkts;
17508                 cnt->bytes = inn_bytes;
17509         }
17510         return 0;
17511 }
17512
17513 /**
17514  * Get aged-out flows.
17515  *
17516  * @param[in] dev
17517  *   Pointer to the Ethernet device structure.
17518  * @param[in] context
17519  *   The address of an array of pointers to the aged-out flows contexts.
17520  * @param[in] nb_contexts
17521  *   The length of context array pointers.
17522  * @param[out] error
17523  *   Perform verbose error reporting if not NULL. Initialized in case of
17524  *   error only.
17525  *
17526  * @return
17527  *   how many contexts get in success, otherwise negative errno value.
17528  *   if nb_contexts is 0, return the amount of all aged contexts.
17529  *   if nb_contexts is not 0 , return the amount of aged flows reported
17530  *   in the context array.
17531  * @note: only stub for now
17532  */
17533 static int
17534 flow_dv_get_aged_flows(struct rte_eth_dev *dev,
17535                     void **context,
17536                     uint32_t nb_contexts,
17537                     struct rte_flow_error *error)
17538 {
17539         struct mlx5_priv *priv = dev->data->dev_private;
17540         struct mlx5_age_info *age_info;
17541         struct mlx5_age_param *age_param;
17542         struct mlx5_flow_counter *counter;
17543         struct mlx5_aso_age_action *act;
17544         int nb_flows = 0;
17545
17546         if (nb_contexts && !context)
17547                 return rte_flow_error_set(error, EINVAL,
17548                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
17549                                           NULL, "empty context");
17550         age_info = GET_PORT_AGE_INFO(priv);
17551         rte_spinlock_lock(&age_info->aged_sl);
17552         LIST_FOREACH(act, &age_info->aged_aso, next) {
17553                 nb_flows++;
17554                 if (nb_contexts) {
17555                         context[nb_flows - 1] =
17556                                                 act->age_params.context;
17557                         if (!(--nb_contexts))
17558                                 break;
17559                 }
17560         }
17561         TAILQ_FOREACH(counter, &age_info->aged_counters, next) {
17562                 nb_flows++;
17563                 if (nb_contexts) {
17564                         age_param = MLX5_CNT_TO_AGE(counter);
17565                         context[nb_flows - 1] = age_param->context;
17566                         if (!(--nb_contexts))
17567                                 break;
17568                 }
17569         }
17570         rte_spinlock_unlock(&age_info->aged_sl);
17571         MLX5_AGE_SET(age_info, MLX5_AGE_TRIGGER);
17572         return nb_flows;
17573 }
17574
17575 /*
17576  * Mutex-protected thunk to lock-free flow_dv_counter_alloc().
17577  */
17578 static uint32_t
17579 flow_dv_counter_allocate(struct rte_eth_dev *dev)
17580 {
17581         return flow_dv_counter_alloc(dev, 0);
17582 }
17583
17584 /**
17585  * Validate indirect action.
17586  * Dispatcher for action type specific validation.
17587  *
17588  * @param[in] dev
17589  *   Pointer to the Ethernet device structure.
17590  * @param[in] conf
17591  *   Indirect action configuration.
17592  * @param[in] action
17593  *   The indirect action object to validate.
17594  * @param[out] error
17595  *   Perform verbose error reporting if not NULL. Initialized in case of
17596  *   error only.
17597  *
17598  * @return
17599  *   0 on success, otherwise negative errno value.
17600  */
17601 static int
17602 flow_dv_action_validate(struct rte_eth_dev *dev,
17603                         const struct rte_flow_indir_action_conf *conf,
17604                         const struct rte_flow_action *action,
17605                         struct rte_flow_error *err)
17606 {
17607         struct mlx5_priv *priv = dev->data->dev_private;
17608
17609         RTE_SET_USED(conf);
17610         switch (action->type) {
17611         case RTE_FLOW_ACTION_TYPE_RSS:
17612                 /*
17613                  * priv->obj_ops is set according to driver capabilities.
17614                  * When DevX capabilities are
17615                  * sufficient, it is set to devx_obj_ops.
17616                  * Otherwise, it is set to ibv_obj_ops.
17617                  * ibv_obj_ops doesn't support ind_table_modify operation.
17618                  * In this case the indirect RSS action can't be used.
17619                  */
17620                 if (priv->obj_ops.ind_table_modify == NULL)
17621                         return rte_flow_error_set
17622                                         (err, ENOTSUP,
17623                                          RTE_FLOW_ERROR_TYPE_ACTION,
17624                                          NULL,
17625                                          "Indirect RSS action not supported");
17626                 return mlx5_validate_action_rss(dev, action, err);
17627         case RTE_FLOW_ACTION_TYPE_AGE:
17628                 if (!priv->sh->aso_age_mng)
17629                         return rte_flow_error_set(err, ENOTSUP,
17630                                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
17631                                                 NULL,
17632                                                 "Indirect age action not supported");
17633                 return flow_dv_validate_action_age(0, action, dev, err);
17634         case RTE_FLOW_ACTION_TYPE_COUNT:
17635                 return flow_dv_validate_action_count(dev, true, 0, err);
17636         case RTE_FLOW_ACTION_TYPE_CONNTRACK:
17637                 if (!priv->sh->ct_aso_en)
17638                         return rte_flow_error_set(err, ENOTSUP,
17639                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
17640                                         "ASO CT is not supported");
17641                 return mlx5_validate_action_ct(dev, action->conf, err);
17642         default:
17643                 return rte_flow_error_set(err, ENOTSUP,
17644                                           RTE_FLOW_ERROR_TYPE_ACTION,
17645                                           NULL,
17646                                           "action type not supported");
17647         }
17648 }
17649
17650 /*
17651  * Check if the RSS configurations for colors of a meter policy match
17652  * each other, except the queues.
17653  *
17654  * @param[in] r1
17655  *   Pointer to the first RSS flow action.
17656  * @param[in] r2
17657  *   Pointer to the second RSS flow action.
17658  *
17659  * @return
17660  *   0 on match, 1 on conflict.
17661  */
17662 static inline int
17663 flow_dv_mtr_policy_rss_compare(const struct rte_flow_action_rss *r1,
17664                                const struct rte_flow_action_rss *r2)
17665 {
17666         if (r1 == NULL || r2 == NULL)
17667                 return 0;
17668         if (!(r1->level <= 1 && r2->level <= 1) &&
17669             !(r1->level > 1 && r2->level > 1))
17670                 return 1;
17671         if (r1->types != r2->types &&
17672             !((r1->types == 0 || r1->types == RTE_ETH_RSS_IP) &&
17673               (r2->types == 0 || r2->types == RTE_ETH_RSS_IP)))
17674                 return 1;
17675         if (r1->key || r2->key) {
17676                 const void *key1 = r1->key ? r1->key : rss_hash_default_key;
17677                 const void *key2 = r2->key ? r2->key : rss_hash_default_key;
17678
17679                 if (memcmp(key1, key2, MLX5_RSS_HASH_KEY_LEN))
17680                         return 1;
17681         }
17682         return 0;
17683 }
17684
17685 /**
17686  * Validate the meter hierarchy chain for meter policy.
17687  *
17688  * @param[in] dev
17689  *   Pointer to the Ethernet device structure.
17690  * @param[in] meter_id
17691  *   Meter id.
17692  * @param[in] action_flags
17693  *   Holds the actions detected until now.
17694  * @param[out] is_rss
17695  *   Is RSS or not.
17696  * @param[out] hierarchy_domain
17697  *   The domain bitmap for hierarchy policy.
17698  * @param[out] error
17699  *   Perform verbose error reporting if not NULL. Initialized in case of
17700  *   error only.
17701  *
17702  * @return
17703  *   0 on success, otherwise negative errno value with error set.
17704  */
17705 static int
17706 flow_dv_validate_policy_mtr_hierarchy(struct rte_eth_dev *dev,
17707                                   uint32_t meter_id,
17708                                   uint64_t action_flags,
17709                                   bool *is_rss,
17710                                   uint8_t *hierarchy_domain,
17711                                   struct rte_mtr_error *error)
17712 {
17713         struct mlx5_priv *priv = dev->data->dev_private;
17714         struct mlx5_flow_meter_info *fm;
17715         struct mlx5_flow_meter_policy *policy;
17716         uint8_t cnt = 1;
17717
17718         if (action_flags & (MLX5_FLOW_FATE_ACTIONS |
17719                             MLX5_FLOW_FATE_ESWITCH_ACTIONS))
17720                 return -rte_mtr_error_set(error, EINVAL,
17721                                         RTE_MTR_ERROR_TYPE_POLICER_ACTION_GREEN,
17722                                         NULL,
17723                                         "Multiple fate actions not supported.");
17724         *hierarchy_domain = 0;
17725         while (true) {
17726                 fm = mlx5_flow_meter_find(priv, meter_id, NULL);
17727                 if (!fm)
17728                         return -rte_mtr_error_set(error, EINVAL,
17729                                                 RTE_MTR_ERROR_TYPE_MTR_ID, NULL,
17730                                         "Meter not found in meter hierarchy.");
17731                 if (fm->def_policy)
17732                         return -rte_mtr_error_set(error, EINVAL,
17733                                         RTE_MTR_ERROR_TYPE_MTR_ID, NULL,
17734                         "Non termination meter not supported in hierarchy.");
17735                 policy = mlx5_flow_meter_policy_find(dev, fm->policy_id, NULL);
17736                 MLX5_ASSERT(policy);
17737                 /**
17738                  * Only inherit the supported domains of the first meter in
17739                  * hierarchy.
17740                  * One meter supports at least one domain.
17741                  */
17742                 if (!*hierarchy_domain) {
17743                         if (policy->transfer)
17744                                 *hierarchy_domain |=
17745                                                 MLX5_MTR_DOMAIN_TRANSFER_BIT;
17746                         if (policy->ingress)
17747                                 *hierarchy_domain |=
17748                                                 MLX5_MTR_DOMAIN_INGRESS_BIT;
17749                         if (policy->egress)
17750                                 *hierarchy_domain |= MLX5_MTR_DOMAIN_EGRESS_BIT;
17751                 }
17752                 if (!policy->is_hierarchy) {
17753                         *is_rss = policy->is_rss;
17754                         break;
17755                 }
17756                 meter_id = policy->act_cnt[RTE_COLOR_GREEN].next_mtr_id;
17757                 if (++cnt >= MLX5_MTR_CHAIN_MAX_NUM)
17758                         return -rte_mtr_error_set(error, EINVAL,
17759                                         RTE_MTR_ERROR_TYPE_METER_POLICY, NULL,
17760                                         "Exceed max hierarchy meter number.");
17761         }
17762         return 0;
17763 }
17764
17765 /**
17766  * Validate meter policy actions.
17767  * Dispatcher for action type specific validation.
17768  *
17769  * @param[in] dev
17770  *   Pointer to the Ethernet device structure.
17771  * @param[in] action
17772  *   The meter policy action object to validate.
17773  * @param[in] attr
17774  *   Attributes of flow to determine steering domain.
17775  * @param[out] error
17776  *   Perform verbose error reporting if not NULL. Initialized in case of
17777  *   error only.
17778  *
17779  * @return
17780  *   0 on success, otherwise negative errno value.
17781  */
17782 static int
17783 flow_dv_validate_mtr_policy_acts(struct rte_eth_dev *dev,
17784                         const struct rte_flow_action *actions[RTE_COLORS],
17785                         struct rte_flow_attr *attr,
17786                         bool *is_rss,
17787                         uint8_t *domain_bitmap,
17788                         uint8_t *policy_mode,
17789                         struct rte_mtr_error *error)
17790 {
17791         struct mlx5_priv *priv = dev->data->dev_private;
17792         struct mlx5_dev_config *dev_conf = &priv->config;
17793         const struct rte_flow_action *act;
17794         uint64_t action_flags[RTE_COLORS] = {0};
17795         int actions_n;
17796         int i, ret;
17797         struct rte_flow_error flow_err;
17798         uint8_t domain_color[RTE_COLORS] = {0};
17799         uint8_t def_domain = MLX5_MTR_ALL_DOMAIN_BIT;
17800         uint8_t hierarchy_domain = 0;
17801         const struct rte_flow_action_meter *mtr;
17802         bool def_green = false;
17803         bool def_yellow = false;
17804         const struct rte_flow_action_rss *rss_color[RTE_COLORS] = {NULL};
17805
17806         if (!priv->config.dv_esw_en)
17807                 def_domain &= ~MLX5_MTR_DOMAIN_TRANSFER_BIT;
17808         *domain_bitmap = def_domain;
17809         /* Red color could only support DROP action. */
17810         if (!actions[RTE_COLOR_RED] ||
17811             actions[RTE_COLOR_RED]->type != RTE_FLOW_ACTION_TYPE_DROP)
17812                 return -rte_mtr_error_set(error, ENOTSUP,
17813                                 RTE_MTR_ERROR_TYPE_METER_POLICY,
17814                                 NULL, "Red color only supports drop action.");
17815         /*
17816          * Check default policy actions:
17817          * Green / Yellow: no action, Red: drop action
17818          * Either G or Y will trigger default policy actions to be created.
17819          */
17820         if (!actions[RTE_COLOR_GREEN] ||
17821             actions[RTE_COLOR_GREEN]->type == RTE_FLOW_ACTION_TYPE_END)
17822                 def_green = true;
17823         if (!actions[RTE_COLOR_YELLOW] ||
17824             actions[RTE_COLOR_YELLOW]->type == RTE_FLOW_ACTION_TYPE_END)
17825                 def_yellow = true;
17826         if (def_green && def_yellow) {
17827                 *policy_mode = MLX5_MTR_POLICY_MODE_DEF;
17828                 return 0;
17829         } else if (!def_green && def_yellow) {
17830                 *policy_mode = MLX5_MTR_POLICY_MODE_OG;
17831         } else if (def_green && !def_yellow) {
17832                 *policy_mode = MLX5_MTR_POLICY_MODE_OY;
17833         } else {
17834                 *policy_mode = MLX5_MTR_POLICY_MODE_ALL;
17835         }
17836         /* Set to empty string in case of NULL pointer access by user. */
17837         flow_err.message = "";
17838         for (i = 0; i < RTE_COLORS; i++) {
17839                 act = actions[i];
17840                 for (action_flags[i] = 0, actions_n = 0;
17841                      act && act->type != RTE_FLOW_ACTION_TYPE_END;
17842                      act++) {
17843                         if (actions_n == MLX5_DV_MAX_NUMBER_OF_ACTIONS)
17844                                 return -rte_mtr_error_set(error, ENOTSUP,
17845                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
17846                                           NULL, "too many actions");
17847                         switch (act->type) {
17848                         case RTE_FLOW_ACTION_TYPE_PORT_ID:
17849                         case RTE_FLOW_ACTION_TYPE_REPRESENTED_PORT:
17850                                 if (!priv->config.dv_esw_en)
17851                                         return -rte_mtr_error_set(error,
17852                                         ENOTSUP,
17853                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
17854                                         NULL, "PORT action validate check"
17855                                         " fail for ESW disable");
17856                                 ret = flow_dv_validate_action_port_id(dev,
17857                                                 action_flags[i],
17858                                                 act, attr, &flow_err);
17859                                 if (ret)
17860                                         return -rte_mtr_error_set(error,
17861                                         ENOTSUP,
17862                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
17863                                         NULL, flow_err.message ?
17864                                         flow_err.message :
17865                                         "PORT action validate check fail");
17866                                 ++actions_n;
17867                                 action_flags[i] |= MLX5_FLOW_ACTION_PORT_ID;
17868                                 break;
17869                         case RTE_FLOW_ACTION_TYPE_MARK:
17870                                 ret = flow_dv_validate_action_mark(dev, act,
17871                                                            action_flags[i],
17872                                                            attr, &flow_err);
17873                                 if (ret < 0)
17874                                         return -rte_mtr_error_set(error,
17875                                         ENOTSUP,
17876                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
17877                                         NULL, flow_err.message ?
17878                                         flow_err.message :
17879                                         "Mark action validate check fail");
17880                                 if (dev_conf->dv_xmeta_en !=
17881                                         MLX5_XMETA_MODE_LEGACY)
17882                                         return -rte_mtr_error_set(error,
17883                                         ENOTSUP,
17884                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
17885                                         NULL, "Extend MARK action is "
17886                                         "not supported. Please try use "
17887                                         "default policy for meter.");
17888                                 action_flags[i] |= MLX5_FLOW_ACTION_MARK;
17889                                 ++actions_n;
17890                                 break;
17891                         case RTE_FLOW_ACTION_TYPE_SET_TAG:
17892                                 ret = flow_dv_validate_action_set_tag(dev,
17893                                                         act, action_flags[i],
17894                                                         attr, &flow_err);
17895                                 if (ret)
17896                                         return -rte_mtr_error_set(error,
17897                                         ENOTSUP,
17898                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
17899                                         NULL, flow_err.message ?
17900                                         flow_err.message :
17901                                         "Set tag action validate check fail");
17902                                 action_flags[i] |= MLX5_FLOW_ACTION_SET_TAG;
17903                                 ++actions_n;
17904                                 break;
17905                         case RTE_FLOW_ACTION_TYPE_DROP:
17906                                 ret = mlx5_flow_validate_action_drop
17907                                         (action_flags[i], attr, &flow_err);
17908                                 if (ret < 0)
17909                                         return -rte_mtr_error_set(error,
17910                                         ENOTSUP,
17911                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
17912                                         NULL, flow_err.message ?
17913                                         flow_err.message :
17914                                         "Drop action validate check fail");
17915                                 action_flags[i] |= MLX5_FLOW_ACTION_DROP;
17916                                 ++actions_n;
17917                                 break;
17918                         case RTE_FLOW_ACTION_TYPE_QUEUE:
17919                                 /*
17920                                  * Check whether extensive
17921                                  * metadata feature is engaged.
17922                                  */
17923                                 if (dev_conf->dv_flow_en &&
17924                                     (dev_conf->dv_xmeta_en !=
17925                                      MLX5_XMETA_MODE_LEGACY) &&
17926                                     mlx5_flow_ext_mreg_supported(dev))
17927                                         return -rte_mtr_error_set(error,
17928                                           ENOTSUP,
17929                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
17930                                           NULL, "Queue action with meta "
17931                                           "is not supported. Please try use "
17932                                           "default policy for meter.");
17933                                 ret = mlx5_flow_validate_action_queue(act,
17934                                                         action_flags[i], dev,
17935                                                         attr, &flow_err);
17936                                 if (ret < 0)
17937                                         return -rte_mtr_error_set(error,
17938                                           ENOTSUP,
17939                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
17940                                           NULL, flow_err.message ?
17941                                           flow_err.message :
17942                                           "Queue action validate check fail");
17943                                 action_flags[i] |= MLX5_FLOW_ACTION_QUEUE;
17944                                 ++actions_n;
17945                                 break;
17946                         case RTE_FLOW_ACTION_TYPE_RSS:
17947                                 if (dev_conf->dv_flow_en &&
17948                                     (dev_conf->dv_xmeta_en !=
17949                                      MLX5_XMETA_MODE_LEGACY) &&
17950                                     mlx5_flow_ext_mreg_supported(dev))
17951                                         return -rte_mtr_error_set(error,
17952                                           ENOTSUP,
17953                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
17954                                           NULL, "RSS action with meta "
17955                                           "is not supported. Please try use "
17956                                           "default policy for meter.");
17957                                 ret = mlx5_validate_action_rss(dev, act,
17958                                                                &flow_err);
17959                                 if (ret < 0)
17960                                         return -rte_mtr_error_set(error,
17961                                           ENOTSUP,
17962                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
17963                                           NULL, flow_err.message ?
17964                                           flow_err.message :
17965                                           "RSS action validate check fail");
17966                                 action_flags[i] |= MLX5_FLOW_ACTION_RSS;
17967                                 ++actions_n;
17968                                 /* Either G or Y will set the RSS. */
17969                                 rss_color[i] = act->conf;
17970                                 break;
17971                         case RTE_FLOW_ACTION_TYPE_JUMP:
17972                                 ret = flow_dv_validate_action_jump(dev,
17973                                         NULL, act, action_flags[i],
17974                                         attr, true, &flow_err);
17975                                 if (ret)
17976                                         return -rte_mtr_error_set(error,
17977                                           ENOTSUP,
17978                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
17979                                           NULL, flow_err.message ?
17980                                           flow_err.message :
17981                                           "Jump action validate check fail");
17982                                 ++actions_n;
17983                                 action_flags[i] |= MLX5_FLOW_ACTION_JUMP;
17984                                 break;
17985                         /*
17986                          * Only the last meter in the hierarchy will support
17987                          * the YELLOW color steering. Then in the meter policy
17988                          * actions list, there should be no other meter inside.
17989                          */
17990                         case RTE_FLOW_ACTION_TYPE_METER:
17991                                 if (i != RTE_COLOR_GREEN)
17992                                         return -rte_mtr_error_set(error,
17993                                                 ENOTSUP,
17994                                                 RTE_MTR_ERROR_TYPE_METER_POLICY,
17995                                                 NULL,
17996                                                 "Meter hierarchy only supports GREEN color.");
17997                                 if (*policy_mode != MLX5_MTR_POLICY_MODE_OG)
17998                                         return -rte_mtr_error_set(error,
17999                                                 ENOTSUP,
18000                                                 RTE_MTR_ERROR_TYPE_METER_POLICY,
18001                                                 NULL,
18002                                                 "No yellow policy should be provided in meter hierarchy.");
18003                                 mtr = act->conf;
18004                                 ret = flow_dv_validate_policy_mtr_hierarchy(dev,
18005                                                         mtr->mtr_id,
18006                                                         action_flags[i],
18007                                                         is_rss,
18008                                                         &hierarchy_domain,
18009                                                         error);
18010                                 if (ret)
18011                                         return ret;
18012                                 ++actions_n;
18013                                 action_flags[i] |=
18014                                 MLX5_FLOW_ACTION_METER_WITH_TERMINATED_POLICY;
18015                                 break;
18016                         default:
18017                                 return -rte_mtr_error_set(error, ENOTSUP,
18018                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
18019                                         NULL,
18020                                         "Doesn't support optional action");
18021                         }
18022                 }
18023                 if (action_flags[i] & MLX5_FLOW_ACTION_PORT_ID) {
18024                         domain_color[i] = MLX5_MTR_DOMAIN_TRANSFER_BIT;
18025                 } else if ((action_flags[i] &
18026                           (MLX5_FLOW_ACTION_RSS | MLX5_FLOW_ACTION_QUEUE)) ||
18027                           (action_flags[i] & MLX5_FLOW_ACTION_MARK)) {
18028                         /*
18029                          * Only support MLX5_XMETA_MODE_LEGACY
18030                          * so MARK action is only in ingress domain.
18031                          */
18032                         domain_color[i] = MLX5_MTR_DOMAIN_INGRESS_BIT;
18033                 } else {
18034                         domain_color[i] = def_domain;
18035                         if (action_flags[i] &&
18036                             !(action_flags[i] & MLX5_FLOW_FATE_ESWITCH_ACTIONS))
18037                                 domain_color[i] &=
18038                                 ~MLX5_MTR_DOMAIN_TRANSFER_BIT;
18039                 }
18040                 if (action_flags[i] &
18041                     MLX5_FLOW_ACTION_METER_WITH_TERMINATED_POLICY)
18042                         domain_color[i] &= hierarchy_domain;
18043                 /*
18044                  * Non-termination actions only support NIC Tx domain.
18045                  * The adjustion should be skipped when there is no
18046                  * action or only END is provided. The default domains
18047                  * bit-mask is set to find the MIN intersection.
18048                  * The action flags checking should also be skipped.
18049                  */
18050                 if ((def_green && i == RTE_COLOR_GREEN) ||
18051                     (def_yellow && i == RTE_COLOR_YELLOW))
18052                         continue;
18053                 /*
18054                  * Validate the drop action mutual exclusion
18055                  * with other actions. Drop action is mutually-exclusive
18056                  * with any other action, except for Count action.
18057                  */
18058                 if ((action_flags[i] & MLX5_FLOW_ACTION_DROP) &&
18059                     (action_flags[i] & ~MLX5_FLOW_ACTION_DROP)) {
18060                         return -rte_mtr_error_set(error, ENOTSUP,
18061                                 RTE_MTR_ERROR_TYPE_METER_POLICY,
18062                                 NULL, "Drop action is mutually-exclusive "
18063                                 "with any other action");
18064                 }
18065                 /* Eswitch has few restrictions on using items and actions */
18066                 if (domain_color[i] & MLX5_MTR_DOMAIN_TRANSFER_BIT) {
18067                         if (!mlx5_flow_ext_mreg_supported(dev) &&
18068                             action_flags[i] & MLX5_FLOW_ACTION_MARK)
18069                                 return -rte_mtr_error_set(error, ENOTSUP,
18070                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
18071                                         NULL, "unsupported action MARK");
18072                         if (action_flags[i] & MLX5_FLOW_ACTION_QUEUE)
18073                                 return -rte_mtr_error_set(error, ENOTSUP,
18074                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
18075                                         NULL, "unsupported action QUEUE");
18076                         if (action_flags[i] & MLX5_FLOW_ACTION_RSS)
18077                                 return -rte_mtr_error_set(error, ENOTSUP,
18078                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
18079                                         NULL, "unsupported action RSS");
18080                         if (!(action_flags[i] & MLX5_FLOW_FATE_ESWITCH_ACTIONS))
18081                                 return -rte_mtr_error_set(error, ENOTSUP,
18082                                         RTE_MTR_ERROR_TYPE_METER_POLICY,
18083                                         NULL, "no fate action is found");
18084                 } else {
18085                         if (!(action_flags[i] & MLX5_FLOW_FATE_ACTIONS) &&
18086                             (domain_color[i] & MLX5_MTR_DOMAIN_INGRESS_BIT)) {
18087                                 if ((domain_color[i] &
18088                                      MLX5_MTR_DOMAIN_EGRESS_BIT))
18089                                         domain_color[i] =
18090                                                 MLX5_MTR_DOMAIN_EGRESS_BIT;
18091                                 else
18092                                         return -rte_mtr_error_set(error,
18093                                                 ENOTSUP,
18094                                                 RTE_MTR_ERROR_TYPE_METER_POLICY,
18095                                                 NULL,
18096                                                 "no fate action is found");
18097                         }
18098                 }
18099         }
18100         /* If both colors have RSS, the attributes should be the same. */
18101         if (flow_dv_mtr_policy_rss_compare(rss_color[RTE_COLOR_GREEN],
18102                                            rss_color[RTE_COLOR_YELLOW]))
18103                 return -rte_mtr_error_set(error, EINVAL,
18104                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
18105                                           NULL, "policy RSS attr conflict");
18106         if (rss_color[RTE_COLOR_GREEN] || rss_color[RTE_COLOR_YELLOW])
18107                 *is_rss = true;
18108         /* "domain_color[C]" is non-zero for each color, default is ALL. */
18109         if (!def_green && !def_yellow &&
18110             domain_color[RTE_COLOR_GREEN] != domain_color[RTE_COLOR_YELLOW] &&
18111             !(action_flags[RTE_COLOR_GREEN] & MLX5_FLOW_ACTION_DROP) &&
18112             !(action_flags[RTE_COLOR_YELLOW] & MLX5_FLOW_ACTION_DROP))
18113                 return -rte_mtr_error_set(error, EINVAL,
18114                                           RTE_MTR_ERROR_TYPE_METER_POLICY,
18115                                           NULL, "policy domains conflict");
18116         /*
18117          * At least one color policy is listed in the actions, the domains
18118          * to be supported should be the intersection.
18119          */
18120         *domain_bitmap = domain_color[RTE_COLOR_GREEN] &
18121                          domain_color[RTE_COLOR_YELLOW];
18122         return 0;
18123 }
18124
18125 static int
18126 flow_dv_sync_domain(struct rte_eth_dev *dev, uint32_t domains, uint32_t flags)
18127 {
18128         struct mlx5_priv *priv = dev->data->dev_private;
18129         int ret = 0;
18130
18131         if ((domains & MLX5_DOMAIN_BIT_NIC_RX) && priv->sh->rx_domain != NULL) {
18132                 ret = mlx5_os_flow_dr_sync_domain(priv->sh->rx_domain,
18133                                                 flags);
18134                 if (ret != 0)
18135                         return ret;
18136         }
18137         if ((domains & MLX5_DOMAIN_BIT_NIC_TX) && priv->sh->tx_domain != NULL) {
18138                 ret = mlx5_os_flow_dr_sync_domain(priv->sh->tx_domain, flags);
18139                 if (ret != 0)
18140                         return ret;
18141         }
18142         if ((domains & MLX5_DOMAIN_BIT_FDB) && priv->sh->fdb_domain != NULL) {
18143                 ret = mlx5_os_flow_dr_sync_domain(priv->sh->fdb_domain, flags);
18144                 if (ret != 0)
18145                         return ret;
18146         }
18147         return 0;
18148 }
18149
18150 /**
18151  * Discover the number of available flow priorities
18152  * by trying to create a flow with the highest priority value
18153  * for each possible number.
18154  *
18155  * @param[in] dev
18156  *   Ethernet device.
18157  * @param[in] vprio
18158  *   List of possible number of available priorities.
18159  * @param[in] vprio_n
18160  *   Size of @p vprio array.
18161  * @return
18162  *   On success, number of available flow priorities.
18163  *   On failure, a negative errno-style code and rte_errno is set.
18164  */
18165 static int
18166 flow_dv_discover_priorities(struct rte_eth_dev *dev,
18167                             const uint16_t *vprio, int vprio_n)
18168 {
18169         struct mlx5_priv *priv = dev->data->dev_private;
18170         struct mlx5_indexed_pool *pool = priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW];
18171         struct rte_flow_item_eth eth;
18172         struct rte_flow_item item = {
18173                 .type = RTE_FLOW_ITEM_TYPE_ETH,
18174                 .spec = &eth,
18175                 .mask = &eth,
18176         };
18177         struct mlx5_flow_dv_matcher matcher = {
18178                 .mask = {
18179                         .size = sizeof(matcher.mask.buf),
18180                 },
18181         };
18182         union mlx5_flow_tbl_key tbl_key;
18183         struct mlx5_flow flow;
18184         void *action;
18185         struct rte_flow_error error;
18186         uint8_t misc_mask;
18187         int i, err, ret = -ENOTSUP;
18188
18189         /*
18190          * Prepare a flow with a catch-all pattern and a drop action.
18191          * Use drop queue, because shared drop action may be unavailable.
18192          */
18193         action = priv->drop_queue.hrxq->action;
18194         if (action == NULL) {
18195                 DRV_LOG(ERR, "Priority discovery requires a drop action");
18196                 rte_errno = ENOTSUP;
18197                 return -rte_errno;
18198         }
18199         memset(&flow, 0, sizeof(flow));
18200         flow.handle = mlx5_ipool_zmalloc(pool, &flow.handle_idx);
18201         if (flow.handle == NULL) {
18202                 DRV_LOG(ERR, "Cannot create flow handle");
18203                 rte_errno = ENOMEM;
18204                 return -rte_errno;
18205         }
18206         flow.ingress = true;
18207         flow.dv.value.size = MLX5_ST_SZ_BYTES(fte_match_param);
18208         flow.dv.actions[0] = action;
18209         flow.dv.actions_n = 1;
18210         memset(&eth, 0, sizeof(eth));
18211         flow_dv_translate_item_eth(matcher.mask.buf, flow.dv.value.buf,
18212                                    &item, /* inner */ false, /* group */ 0);
18213         matcher.crc = rte_raw_cksum(matcher.mask.buf, matcher.mask.size);
18214         for (i = 0; i < vprio_n; i++) {
18215                 /* Configure the next proposed maximum priority. */
18216                 matcher.priority = vprio[i] - 1;
18217                 memset(&tbl_key, 0, sizeof(tbl_key));
18218                 err = flow_dv_matcher_register(dev, &matcher, &tbl_key, &flow,
18219                                                /* tunnel */ NULL,
18220                                                /* group */ 0,
18221                                                &error);
18222                 if (err != 0) {
18223                         /* This action is pure SW and must always succeed. */
18224                         DRV_LOG(ERR, "Cannot register matcher");
18225                         ret = -rte_errno;
18226                         break;
18227                 }
18228                 /* Try to apply the flow to HW. */
18229                 misc_mask = flow_dv_matcher_enable(flow.dv.value.buf);
18230                 __flow_dv_adjust_buf_size(&flow.dv.value.size, misc_mask);
18231                 err = mlx5_flow_os_create_flow
18232                                 (flow.handle->dvh.matcher->matcher_object,
18233                                  (void *)&flow.dv.value, flow.dv.actions_n,
18234                                  flow.dv.actions, &flow.handle->drv_flow);
18235                 if (err == 0) {
18236                         claim_zero(mlx5_flow_os_destroy_flow
18237                                                 (flow.handle->drv_flow));
18238                         flow.handle->drv_flow = NULL;
18239                 }
18240                 claim_zero(flow_dv_matcher_release(dev, flow.handle));
18241                 if (err != 0)
18242                         break;
18243                 ret = vprio[i];
18244         }
18245         mlx5_ipool_free(pool, flow.handle_idx);
18246         /* Set rte_errno if no expected priority value matched. */
18247         if (ret < 0)
18248                 rte_errno = -ret;
18249         return ret;
18250 }
18251
18252 const struct mlx5_flow_driver_ops mlx5_flow_dv_drv_ops = {
18253         .validate = flow_dv_validate,
18254         .prepare = flow_dv_prepare,
18255         .translate = flow_dv_translate,
18256         .apply = flow_dv_apply,
18257         .remove = flow_dv_remove,
18258         .destroy = flow_dv_destroy,
18259         .query = flow_dv_query,
18260         .create_mtr_tbls = flow_dv_create_mtr_tbls,
18261         .destroy_mtr_tbls = flow_dv_destroy_mtr_tbls,
18262         .destroy_mtr_drop_tbls = flow_dv_destroy_mtr_drop_tbls,
18263         .create_meter = flow_dv_mtr_alloc,
18264         .free_meter = flow_dv_aso_mtr_release_to_pool,
18265         .validate_mtr_acts = flow_dv_validate_mtr_policy_acts,
18266         .create_mtr_acts = flow_dv_create_mtr_policy_acts,
18267         .destroy_mtr_acts = flow_dv_destroy_mtr_policy_acts,
18268         .create_policy_rules = flow_dv_create_policy_rules,
18269         .destroy_policy_rules = flow_dv_destroy_policy_rules,
18270         .create_def_policy = flow_dv_create_def_policy,
18271         .destroy_def_policy = flow_dv_destroy_def_policy,
18272         .meter_sub_policy_rss_prepare = flow_dv_meter_sub_policy_rss_prepare,
18273         .meter_hierarchy_rule_create = flow_dv_meter_hierarchy_rule_create,
18274         .destroy_sub_policy_with_rxq = flow_dv_destroy_sub_policy_with_rxq,
18275         .counter_alloc = flow_dv_counter_allocate,
18276         .counter_free = flow_dv_counter_free,
18277         .counter_query = flow_dv_counter_query,
18278         .get_aged_flows = flow_dv_get_aged_flows,
18279         .action_validate = flow_dv_action_validate,
18280         .action_create = flow_dv_action_create,
18281         .action_destroy = flow_dv_action_destroy,
18282         .action_update = flow_dv_action_update,
18283         .action_query = flow_dv_action_query,
18284         .sync_domain = flow_dv_sync_domain,
18285         .discover_priorities = flow_dv_discover_priorities,
18286         .item_create = flow_dv_item_create,
18287         .item_release = flow_dv_item_release,
18288 };
18289
18290 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */
18291