net/mlx5: optimize action flags in flow handle
[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 /* Verbs header. */
12 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
13 #ifdef PEDANTIC
14 #pragma GCC diagnostic ignored "-Wpedantic"
15 #endif
16 #include <infiniband/verbs.h>
17 #ifdef PEDANTIC
18 #pragma GCC diagnostic error "-Wpedantic"
19 #endif
20
21 #include <rte_common.h>
22 #include <rte_ether.h>
23 #include <rte_ethdev_driver.h>
24 #include <rte_flow.h>
25 #include <rte_flow_driver.h>
26 #include <rte_malloc.h>
27 #include <rte_ip.h>
28 #include <rte_gre.h>
29 #include <rte_vxlan.h>
30 #include <rte_gtp.h>
31
32 #include <mlx5_glue.h>
33 #include <mlx5_devx_cmds.h>
34 #include <mlx5_prm.h>
35
36 #include "mlx5_defs.h"
37 #include "mlx5.h"
38 #include "mlx5_flow.h"
39 #include "mlx5_rxtx.h"
40
41 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
42
43 #ifndef HAVE_IBV_FLOW_DEVX_COUNTERS
44 #define MLX5DV_FLOW_ACTION_COUNTERS_DEVX 0
45 #endif
46
47 #ifndef HAVE_MLX5DV_DR_ESWITCH
48 #ifndef MLX5DV_FLOW_TABLE_TYPE_FDB
49 #define MLX5DV_FLOW_TABLE_TYPE_FDB 0
50 #endif
51 #endif
52
53 #ifndef HAVE_MLX5DV_DR
54 #define MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL 1
55 #endif
56
57 /* VLAN header definitions */
58 #define MLX5DV_FLOW_VLAN_PCP_SHIFT 13
59 #define MLX5DV_FLOW_VLAN_PCP_MASK (0x7 << MLX5DV_FLOW_VLAN_PCP_SHIFT)
60 #define MLX5DV_FLOW_VLAN_VID_MASK 0x0fff
61 #define MLX5DV_FLOW_VLAN_PCP_MASK_BE RTE_BE16(MLX5DV_FLOW_VLAN_PCP_MASK)
62 #define MLX5DV_FLOW_VLAN_VID_MASK_BE RTE_BE16(MLX5DV_FLOW_VLAN_VID_MASK)
63
64 union flow_dv_attr {
65         struct {
66                 uint32_t valid:1;
67                 uint32_t ipv4:1;
68                 uint32_t ipv6:1;
69                 uint32_t tcp:1;
70                 uint32_t udp:1;
71                 uint32_t reserved:27;
72         };
73         uint32_t attr;
74 };
75
76 static int
77 flow_dv_tbl_resource_release(struct rte_eth_dev *dev,
78                              struct mlx5_flow_tbl_resource *tbl);
79
80 /**
81  * Initialize flow attributes structure according to flow items' types.
82  *
83  * flow_dv_validate() avoids multiple L3/L4 layers cases other than tunnel
84  * mode. For tunnel mode, the items to be modified are the outermost ones.
85  *
86  * @param[in] item
87  *   Pointer to item specification.
88  * @param[out] attr
89  *   Pointer to flow attributes structure.
90  * @param[in] dev_flow
91  *   Pointer to the sub flow.
92  * @param[in] tunnel_decap
93  *   Whether action is after tunnel decapsulation.
94  */
95 static void
96 flow_dv_attr_init(const struct rte_flow_item *item, union flow_dv_attr *attr,
97                   struct mlx5_flow *dev_flow, bool tunnel_decap)
98 {
99         uint64_t layers = dev_flow->handle->layers;
100
101         /*
102          * If layers is already initialized, it means this dev_flow is the
103          * suffix flow, the layers flags is set by the prefix flow. Need to
104          * use the layer flags from prefix flow as the suffix flow may not
105          * have the user defined items as the flow is split.
106          */
107         if (layers) {
108                 if (layers & MLX5_FLOW_LAYER_OUTER_L3_IPV4)
109                         attr->ipv4 = 1;
110                 else if (layers & MLX5_FLOW_LAYER_OUTER_L3_IPV6)
111                         attr->ipv6 = 1;
112                 if (layers & MLX5_FLOW_LAYER_OUTER_L4_TCP)
113                         attr->tcp = 1;
114                 else if (layers & MLX5_FLOW_LAYER_OUTER_L4_UDP)
115                         attr->udp = 1;
116                 attr->valid = 1;
117                 return;
118         }
119         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
120                 uint8_t next_protocol = 0xff;
121                 switch (item->type) {
122                 case RTE_FLOW_ITEM_TYPE_GRE:
123                 case RTE_FLOW_ITEM_TYPE_NVGRE:
124                 case RTE_FLOW_ITEM_TYPE_VXLAN:
125                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
126                 case RTE_FLOW_ITEM_TYPE_GENEVE:
127                 case RTE_FLOW_ITEM_TYPE_MPLS:
128                         if (tunnel_decap)
129                                 attr->attr = 0;
130                         break;
131                 case RTE_FLOW_ITEM_TYPE_IPV4:
132                         if (!attr->ipv6)
133                                 attr->ipv4 = 1;
134                         if (item->mask != NULL &&
135                             ((const struct rte_flow_item_ipv4 *)
136                             item->mask)->hdr.next_proto_id)
137                                 next_protocol =
138                                     ((const struct rte_flow_item_ipv4 *)
139                                       (item->spec))->hdr.next_proto_id &
140                                     ((const struct rte_flow_item_ipv4 *)
141                                       (item->mask))->hdr.next_proto_id;
142                         if ((next_protocol == IPPROTO_IPIP ||
143                             next_protocol == IPPROTO_IPV6) && tunnel_decap)
144                                 attr->attr = 0;
145                         break;
146                 case RTE_FLOW_ITEM_TYPE_IPV6:
147                         if (!attr->ipv4)
148                                 attr->ipv6 = 1;
149                         if (item->mask != NULL &&
150                             ((const struct rte_flow_item_ipv6 *)
151                             item->mask)->hdr.proto)
152                                 next_protocol =
153                                     ((const struct rte_flow_item_ipv6 *)
154                                       (item->spec))->hdr.proto &
155                                     ((const struct rte_flow_item_ipv6 *)
156                                       (item->mask))->hdr.proto;
157                         if ((next_protocol == IPPROTO_IPIP ||
158                             next_protocol == IPPROTO_IPV6) && tunnel_decap)
159                                 attr->attr = 0;
160                         break;
161                 case RTE_FLOW_ITEM_TYPE_UDP:
162                         if (!attr->tcp)
163                                 attr->udp = 1;
164                         break;
165                 case RTE_FLOW_ITEM_TYPE_TCP:
166                         if (!attr->udp)
167                                 attr->tcp = 1;
168                         break;
169                 default:
170                         break;
171                 }
172         }
173         attr->valid = 1;
174 }
175
176 /**
177  * Convert rte_mtr_color to mlx5 color.
178  *
179  * @param[in] rcol
180  *   rte_mtr_color.
181  *
182  * @return
183  *   mlx5 color.
184  */
185 static int
186 rte_col_2_mlx5_col(enum rte_color rcol)
187 {
188         switch (rcol) {
189         case RTE_COLOR_GREEN:
190                 return MLX5_FLOW_COLOR_GREEN;
191         case RTE_COLOR_YELLOW:
192                 return MLX5_FLOW_COLOR_YELLOW;
193         case RTE_COLOR_RED:
194                 return MLX5_FLOW_COLOR_RED;
195         default:
196                 break;
197         }
198         return MLX5_FLOW_COLOR_UNDEFINED;
199 }
200
201 struct field_modify_info {
202         uint32_t size; /* Size of field in protocol header, in bytes. */
203         uint32_t offset; /* Offset of field in protocol header, in bytes. */
204         enum mlx5_modification_field id;
205 };
206
207 struct field_modify_info modify_eth[] = {
208         {4,  0, MLX5_MODI_OUT_DMAC_47_16},
209         {2,  4, MLX5_MODI_OUT_DMAC_15_0},
210         {4,  6, MLX5_MODI_OUT_SMAC_47_16},
211         {2, 10, MLX5_MODI_OUT_SMAC_15_0},
212         {0, 0, 0},
213 };
214
215 struct field_modify_info modify_vlan_out_first_vid[] = {
216         /* Size in bits !!! */
217         {12, 0, MLX5_MODI_OUT_FIRST_VID},
218         {0, 0, 0},
219 };
220
221 struct field_modify_info modify_ipv4[] = {
222         {1,  1, MLX5_MODI_OUT_IP_DSCP},
223         {1,  8, MLX5_MODI_OUT_IPV4_TTL},
224         {4, 12, MLX5_MODI_OUT_SIPV4},
225         {4, 16, MLX5_MODI_OUT_DIPV4},
226         {0, 0, 0},
227 };
228
229 struct field_modify_info modify_ipv6[] = {
230         {1,  0, MLX5_MODI_OUT_IP_DSCP},
231         {1,  7, MLX5_MODI_OUT_IPV6_HOPLIMIT},
232         {4,  8, MLX5_MODI_OUT_SIPV6_127_96},
233         {4, 12, MLX5_MODI_OUT_SIPV6_95_64},
234         {4, 16, MLX5_MODI_OUT_SIPV6_63_32},
235         {4, 20, MLX5_MODI_OUT_SIPV6_31_0},
236         {4, 24, MLX5_MODI_OUT_DIPV6_127_96},
237         {4, 28, MLX5_MODI_OUT_DIPV6_95_64},
238         {4, 32, MLX5_MODI_OUT_DIPV6_63_32},
239         {4, 36, MLX5_MODI_OUT_DIPV6_31_0},
240         {0, 0, 0},
241 };
242
243 struct field_modify_info modify_udp[] = {
244         {2, 0, MLX5_MODI_OUT_UDP_SPORT},
245         {2, 2, MLX5_MODI_OUT_UDP_DPORT},
246         {0, 0, 0},
247 };
248
249 struct field_modify_info modify_tcp[] = {
250         {2, 0, MLX5_MODI_OUT_TCP_SPORT},
251         {2, 2, MLX5_MODI_OUT_TCP_DPORT},
252         {4, 4, MLX5_MODI_OUT_TCP_SEQ_NUM},
253         {4, 8, MLX5_MODI_OUT_TCP_ACK_NUM},
254         {0, 0, 0},
255 };
256
257 static void
258 mlx5_flow_tunnel_ip_check(const struct rte_flow_item *item __rte_unused,
259                           uint8_t next_protocol, uint64_t *item_flags,
260                           int *tunnel)
261 {
262         MLX5_ASSERT(item->type == RTE_FLOW_ITEM_TYPE_IPV4 ||
263                     item->type == RTE_FLOW_ITEM_TYPE_IPV6);
264         if (next_protocol == IPPROTO_IPIP) {
265                 *item_flags |= MLX5_FLOW_LAYER_IPIP;
266                 *tunnel = 1;
267         }
268         if (next_protocol == IPPROTO_IPV6) {
269                 *item_flags |= MLX5_FLOW_LAYER_IPV6_ENCAP;
270                 *tunnel = 1;
271         }
272 }
273
274 /**
275  * Acquire the synchronizing object to protect multithreaded access
276  * to shared dv context. Lock occurs only if context is actually
277  * shared, i.e. we have multiport IB device and representors are
278  * created.
279  *
280  * @param[in] dev
281  *   Pointer to the rte_eth_dev structure.
282  */
283 static void
284 flow_dv_shared_lock(struct rte_eth_dev *dev)
285 {
286         struct mlx5_priv *priv = dev->data->dev_private;
287         struct mlx5_ibv_shared *sh = priv->sh;
288
289         if (sh->dv_refcnt > 1) {
290                 int ret;
291
292                 ret = pthread_mutex_lock(&sh->dv_mutex);
293                 MLX5_ASSERT(!ret);
294                 (void)ret;
295         }
296 }
297
298 static void
299 flow_dv_shared_unlock(struct rte_eth_dev *dev)
300 {
301         struct mlx5_priv *priv = dev->data->dev_private;
302         struct mlx5_ibv_shared *sh = priv->sh;
303
304         if (sh->dv_refcnt > 1) {
305                 int ret;
306
307                 ret = pthread_mutex_unlock(&sh->dv_mutex);
308                 MLX5_ASSERT(!ret);
309                 (void)ret;
310         }
311 }
312
313 /* Update VLAN's VID/PCP based on input rte_flow_action.
314  *
315  * @param[in] action
316  *   Pointer to struct rte_flow_action.
317  * @param[out] vlan
318  *   Pointer to struct rte_vlan_hdr.
319  */
320 static void
321 mlx5_update_vlan_vid_pcp(const struct rte_flow_action *action,
322                          struct rte_vlan_hdr *vlan)
323 {
324         uint16_t vlan_tci;
325         if (action->type == RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP) {
326                 vlan_tci =
327                     ((const struct rte_flow_action_of_set_vlan_pcp *)
328                                                action->conf)->vlan_pcp;
329                 vlan_tci = vlan_tci << MLX5DV_FLOW_VLAN_PCP_SHIFT;
330                 vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_PCP_MASK;
331                 vlan->vlan_tci |= vlan_tci;
332         } else if (action->type == RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID) {
333                 vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_VID_MASK;
334                 vlan->vlan_tci |= rte_be_to_cpu_16
335                     (((const struct rte_flow_action_of_set_vlan_vid *)
336                                              action->conf)->vlan_vid);
337         }
338 }
339
340 /**
341  * Fetch 1, 2, 3 or 4 byte field from the byte array
342  * and return as unsigned integer in host-endian format.
343  *
344  * @param[in] data
345  *   Pointer to data array.
346  * @param[in] size
347  *   Size of field to extract.
348  *
349  * @return
350  *   converted field in host endian format.
351  */
352 static inline uint32_t
353 flow_dv_fetch_field(const uint8_t *data, uint32_t size)
354 {
355         uint32_t ret;
356
357         switch (size) {
358         case 1:
359                 ret = *data;
360                 break;
361         case 2:
362                 ret = rte_be_to_cpu_16(*(const unaligned_uint16_t *)data);
363                 break;
364         case 3:
365                 ret = rte_be_to_cpu_16(*(const unaligned_uint16_t *)data);
366                 ret = (ret << 8) | *(data + sizeof(uint16_t));
367                 break;
368         case 4:
369                 ret = rte_be_to_cpu_32(*(const unaligned_uint32_t *)data);
370                 break;
371         default:
372                 MLX5_ASSERT(false);
373                 ret = 0;
374                 break;
375         }
376         return ret;
377 }
378
379 /**
380  * Convert modify-header action to DV specification.
381  *
382  * Data length of each action is determined by provided field description
383  * and the item mask. Data bit offset and width of each action is determined
384  * by provided item mask.
385  *
386  * @param[in] item
387  *   Pointer to item specification.
388  * @param[in] field
389  *   Pointer to field modification information.
390  *     For MLX5_MODIFICATION_TYPE_SET specifies destination field.
391  *     For MLX5_MODIFICATION_TYPE_ADD specifies destination field.
392  *     For MLX5_MODIFICATION_TYPE_COPY specifies source field.
393  * @param[in] dcopy
394  *   Destination field info for MLX5_MODIFICATION_TYPE_COPY in @type.
395  *   Negative offset value sets the same offset as source offset.
396  *   size field is ignored, value is taken from source field.
397  * @param[in,out] resource
398  *   Pointer to the modify-header resource.
399  * @param[in] type
400  *   Type of modification.
401  * @param[out] error
402  *   Pointer to the error structure.
403  *
404  * @return
405  *   0 on success, a negative errno value otherwise and rte_errno is set.
406  */
407 static int
408 flow_dv_convert_modify_action(struct rte_flow_item *item,
409                               struct field_modify_info *field,
410                               struct field_modify_info *dcopy,
411                               struct mlx5_flow_dv_modify_hdr_resource *resource,
412                               uint32_t type, struct rte_flow_error *error)
413 {
414         uint32_t i = resource->actions_num;
415         struct mlx5_modification_cmd *actions = resource->actions;
416
417         /*
418          * The item and mask are provided in big-endian format.
419          * The fields should be presented as in big-endian format either.
420          * Mask must be always present, it defines the actual field width.
421          */
422         MLX5_ASSERT(item->mask);
423         MLX5_ASSERT(field->size);
424         do {
425                 unsigned int size_b;
426                 unsigned int off_b;
427                 uint32_t mask;
428                 uint32_t data;
429
430                 if (i >= MLX5_MAX_MODIFY_NUM)
431                         return rte_flow_error_set(error, EINVAL,
432                                  RTE_FLOW_ERROR_TYPE_ACTION, NULL,
433                                  "too many items to modify");
434                 /* Fetch variable byte size mask from the array. */
435                 mask = flow_dv_fetch_field((const uint8_t *)item->mask +
436                                            field->offset, field->size);
437                 if (!mask) {
438                         ++field;
439                         continue;
440                 }
441                 /* Deduce actual data width in bits from mask value. */
442                 off_b = rte_bsf32(mask);
443                 size_b = sizeof(uint32_t) * CHAR_BIT -
444                          off_b - __builtin_clz(mask);
445                 MLX5_ASSERT(size_b);
446                 size_b = size_b == sizeof(uint32_t) * CHAR_BIT ? 0 : size_b;
447                 actions[i] = (struct mlx5_modification_cmd) {
448                         .action_type = type,
449                         .field = field->id,
450                         .offset = off_b,
451                         .length = size_b,
452                 };
453                 /* Convert entire record to expected big-endian format. */
454                 actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
455                 if (type == MLX5_MODIFICATION_TYPE_COPY) {
456                         MLX5_ASSERT(dcopy);
457                         actions[i].dst_field = dcopy->id;
458                         actions[i].dst_offset =
459                                 (int)dcopy->offset < 0 ? off_b : dcopy->offset;
460                         /* Convert entire record to big-endian format. */
461                         actions[i].data1 = rte_cpu_to_be_32(actions[i].data1);
462                 } else {
463                         MLX5_ASSERT(item->spec);
464                         data = flow_dv_fetch_field((const uint8_t *)item->spec +
465                                                    field->offset, field->size);
466                         /* Shift out the trailing masked bits from data. */
467                         data = (data & mask) >> off_b;
468                         actions[i].data1 = rte_cpu_to_be_32(data);
469                 }
470                 ++i;
471                 ++field;
472         } while (field->size);
473         if (resource->actions_num == i)
474                 return rte_flow_error_set(error, EINVAL,
475                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
476                                           "invalid modification flow item");
477         resource->actions_num = i;
478         return 0;
479 }
480
481 /**
482  * Convert modify-header set IPv4 address action to DV specification.
483  *
484  * @param[in,out] resource
485  *   Pointer to the modify-header resource.
486  * @param[in] action
487  *   Pointer to action specification.
488  * @param[out] error
489  *   Pointer to the error structure.
490  *
491  * @return
492  *   0 on success, a negative errno value otherwise and rte_errno is set.
493  */
494 static int
495 flow_dv_convert_action_modify_ipv4
496                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
497                          const struct rte_flow_action *action,
498                          struct rte_flow_error *error)
499 {
500         const struct rte_flow_action_set_ipv4 *conf =
501                 (const struct rte_flow_action_set_ipv4 *)(action->conf);
502         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV4 };
503         struct rte_flow_item_ipv4 ipv4;
504         struct rte_flow_item_ipv4 ipv4_mask;
505
506         memset(&ipv4, 0, sizeof(ipv4));
507         memset(&ipv4_mask, 0, sizeof(ipv4_mask));
508         if (action->type == RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC) {
509                 ipv4.hdr.src_addr = conf->ipv4_addr;
510                 ipv4_mask.hdr.src_addr = rte_flow_item_ipv4_mask.hdr.src_addr;
511         } else {
512                 ipv4.hdr.dst_addr = conf->ipv4_addr;
513                 ipv4_mask.hdr.dst_addr = rte_flow_item_ipv4_mask.hdr.dst_addr;
514         }
515         item.spec = &ipv4;
516         item.mask = &ipv4_mask;
517         return flow_dv_convert_modify_action(&item, modify_ipv4, NULL, resource,
518                                              MLX5_MODIFICATION_TYPE_SET, error);
519 }
520
521 /**
522  * Convert modify-header set IPv6 address action to DV specification.
523  *
524  * @param[in,out] resource
525  *   Pointer to the modify-header resource.
526  * @param[in] action
527  *   Pointer to action specification.
528  * @param[out] error
529  *   Pointer to the error structure.
530  *
531  * @return
532  *   0 on success, a negative errno value otherwise and rte_errno is set.
533  */
534 static int
535 flow_dv_convert_action_modify_ipv6
536                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
537                          const struct rte_flow_action *action,
538                          struct rte_flow_error *error)
539 {
540         const struct rte_flow_action_set_ipv6 *conf =
541                 (const struct rte_flow_action_set_ipv6 *)(action->conf);
542         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV6 };
543         struct rte_flow_item_ipv6 ipv6;
544         struct rte_flow_item_ipv6 ipv6_mask;
545
546         memset(&ipv6, 0, sizeof(ipv6));
547         memset(&ipv6_mask, 0, sizeof(ipv6_mask));
548         if (action->type == RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC) {
549                 memcpy(&ipv6.hdr.src_addr, &conf->ipv6_addr,
550                        sizeof(ipv6.hdr.src_addr));
551                 memcpy(&ipv6_mask.hdr.src_addr,
552                        &rte_flow_item_ipv6_mask.hdr.src_addr,
553                        sizeof(ipv6.hdr.src_addr));
554         } else {
555                 memcpy(&ipv6.hdr.dst_addr, &conf->ipv6_addr,
556                        sizeof(ipv6.hdr.dst_addr));
557                 memcpy(&ipv6_mask.hdr.dst_addr,
558                        &rte_flow_item_ipv6_mask.hdr.dst_addr,
559                        sizeof(ipv6.hdr.dst_addr));
560         }
561         item.spec = &ipv6;
562         item.mask = &ipv6_mask;
563         return flow_dv_convert_modify_action(&item, modify_ipv6, NULL, resource,
564                                              MLX5_MODIFICATION_TYPE_SET, error);
565 }
566
567 /**
568  * Convert modify-header set MAC address action to DV specification.
569  *
570  * @param[in,out] resource
571  *   Pointer to the modify-header resource.
572  * @param[in] action
573  *   Pointer to action specification.
574  * @param[out] error
575  *   Pointer to the error structure.
576  *
577  * @return
578  *   0 on success, a negative errno value otherwise and rte_errno is set.
579  */
580 static int
581 flow_dv_convert_action_modify_mac
582                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
583                          const struct rte_flow_action *action,
584                          struct rte_flow_error *error)
585 {
586         const struct rte_flow_action_set_mac *conf =
587                 (const struct rte_flow_action_set_mac *)(action->conf);
588         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_ETH };
589         struct rte_flow_item_eth eth;
590         struct rte_flow_item_eth eth_mask;
591
592         memset(&eth, 0, sizeof(eth));
593         memset(&eth_mask, 0, sizeof(eth_mask));
594         if (action->type == RTE_FLOW_ACTION_TYPE_SET_MAC_SRC) {
595                 memcpy(&eth.src.addr_bytes, &conf->mac_addr,
596                        sizeof(eth.src.addr_bytes));
597                 memcpy(&eth_mask.src.addr_bytes,
598                        &rte_flow_item_eth_mask.src.addr_bytes,
599                        sizeof(eth_mask.src.addr_bytes));
600         } else {
601                 memcpy(&eth.dst.addr_bytes, &conf->mac_addr,
602                        sizeof(eth.dst.addr_bytes));
603                 memcpy(&eth_mask.dst.addr_bytes,
604                        &rte_flow_item_eth_mask.dst.addr_bytes,
605                        sizeof(eth_mask.dst.addr_bytes));
606         }
607         item.spec = &eth;
608         item.mask = &eth_mask;
609         return flow_dv_convert_modify_action(&item, modify_eth, NULL, resource,
610                                              MLX5_MODIFICATION_TYPE_SET, error);
611 }
612
613 /**
614  * Convert modify-header set VLAN VID action to DV specification.
615  *
616  * @param[in,out] resource
617  *   Pointer to the modify-header resource.
618  * @param[in] action
619  *   Pointer to action specification.
620  * @param[out] error
621  *   Pointer to the error structure.
622  *
623  * @return
624  *   0 on success, a negative errno value otherwise and rte_errno is set.
625  */
626 static int
627 flow_dv_convert_action_modify_vlan_vid
628                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
629                          const struct rte_flow_action *action,
630                          struct rte_flow_error *error)
631 {
632         const struct rte_flow_action_of_set_vlan_vid *conf =
633                 (const struct rte_flow_action_of_set_vlan_vid *)(action->conf);
634         int i = resource->actions_num;
635         struct mlx5_modification_cmd *actions = resource->actions;
636         struct field_modify_info *field = modify_vlan_out_first_vid;
637
638         if (i >= MLX5_MAX_MODIFY_NUM)
639                 return rte_flow_error_set(error, EINVAL,
640                          RTE_FLOW_ERROR_TYPE_ACTION, NULL,
641                          "too many items to modify");
642         actions[i] = (struct mlx5_modification_cmd) {
643                 .action_type = MLX5_MODIFICATION_TYPE_SET,
644                 .field = field->id,
645                 .length = field->size,
646                 .offset = field->offset,
647         };
648         actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
649         actions[i].data1 = conf->vlan_vid;
650         actions[i].data1 = actions[i].data1 << 16;
651         resource->actions_num = ++i;
652         return 0;
653 }
654
655 /**
656  * Convert modify-header set TP action to DV specification.
657  *
658  * @param[in,out] resource
659  *   Pointer to the modify-header resource.
660  * @param[in] action
661  *   Pointer to action specification.
662  * @param[in] items
663  *   Pointer to rte_flow_item objects list.
664  * @param[in] attr
665  *   Pointer to flow attributes structure.
666  * @param[in] dev_flow
667  *   Pointer to the sub flow.
668  * @param[in] tunnel_decap
669  *   Whether action is after tunnel decapsulation.
670  * @param[out] error
671  *   Pointer to the error structure.
672  *
673  * @return
674  *   0 on success, a negative errno value otherwise and rte_errno is set.
675  */
676 static int
677 flow_dv_convert_action_modify_tp
678                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
679                          const struct rte_flow_action *action,
680                          const struct rte_flow_item *items,
681                          union flow_dv_attr *attr, struct mlx5_flow *dev_flow,
682                          bool tunnel_decap, struct rte_flow_error *error)
683 {
684         const struct rte_flow_action_set_tp *conf =
685                 (const struct rte_flow_action_set_tp *)(action->conf);
686         struct rte_flow_item item;
687         struct rte_flow_item_udp udp;
688         struct rte_flow_item_udp udp_mask;
689         struct rte_flow_item_tcp tcp;
690         struct rte_flow_item_tcp tcp_mask;
691         struct field_modify_info *field;
692
693         if (!attr->valid)
694                 flow_dv_attr_init(items, attr, dev_flow, tunnel_decap);
695         if (attr->udp) {
696                 memset(&udp, 0, sizeof(udp));
697                 memset(&udp_mask, 0, sizeof(udp_mask));
698                 if (action->type == RTE_FLOW_ACTION_TYPE_SET_TP_SRC) {
699                         udp.hdr.src_port = conf->port;
700                         udp_mask.hdr.src_port =
701                                         rte_flow_item_udp_mask.hdr.src_port;
702                 } else {
703                         udp.hdr.dst_port = conf->port;
704                         udp_mask.hdr.dst_port =
705                                         rte_flow_item_udp_mask.hdr.dst_port;
706                 }
707                 item.type = RTE_FLOW_ITEM_TYPE_UDP;
708                 item.spec = &udp;
709                 item.mask = &udp_mask;
710                 field = modify_udp;
711         } else {
712                 MLX5_ASSERT(attr->tcp);
713                 memset(&tcp, 0, sizeof(tcp));
714                 memset(&tcp_mask, 0, sizeof(tcp_mask));
715                 if (action->type == RTE_FLOW_ACTION_TYPE_SET_TP_SRC) {
716                         tcp.hdr.src_port = conf->port;
717                         tcp_mask.hdr.src_port =
718                                         rte_flow_item_tcp_mask.hdr.src_port;
719                 } else {
720                         tcp.hdr.dst_port = conf->port;
721                         tcp_mask.hdr.dst_port =
722                                         rte_flow_item_tcp_mask.hdr.dst_port;
723                 }
724                 item.type = RTE_FLOW_ITEM_TYPE_TCP;
725                 item.spec = &tcp;
726                 item.mask = &tcp_mask;
727                 field = modify_tcp;
728         }
729         return flow_dv_convert_modify_action(&item, field, NULL, resource,
730                                              MLX5_MODIFICATION_TYPE_SET, error);
731 }
732
733 /**
734  * Convert modify-header set TTL action to DV specification.
735  *
736  * @param[in,out] resource
737  *   Pointer to the modify-header resource.
738  * @param[in] action
739  *   Pointer to action specification.
740  * @param[in] items
741  *   Pointer to rte_flow_item objects list.
742  * @param[in] attr
743  *   Pointer to flow attributes structure.
744  * @param[in] dev_flow
745  *   Pointer to the sub flow.
746  * @param[in] tunnel_decap
747  *   Whether action is after tunnel decapsulation.
748  * @param[out] error
749  *   Pointer to the error structure.
750  *
751  * @return
752  *   0 on success, a negative errno value otherwise and rte_errno is set.
753  */
754 static int
755 flow_dv_convert_action_modify_ttl
756                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
757                          const struct rte_flow_action *action,
758                          const struct rte_flow_item *items,
759                          union flow_dv_attr *attr, struct mlx5_flow *dev_flow,
760                          bool tunnel_decap, struct rte_flow_error *error)
761 {
762         const struct rte_flow_action_set_ttl *conf =
763                 (const struct rte_flow_action_set_ttl *)(action->conf);
764         struct rte_flow_item item;
765         struct rte_flow_item_ipv4 ipv4;
766         struct rte_flow_item_ipv4 ipv4_mask;
767         struct rte_flow_item_ipv6 ipv6;
768         struct rte_flow_item_ipv6 ipv6_mask;
769         struct field_modify_info *field;
770
771         if (!attr->valid)
772                 flow_dv_attr_init(items, attr, dev_flow, tunnel_decap);
773         if (attr->ipv4) {
774                 memset(&ipv4, 0, sizeof(ipv4));
775                 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
776                 ipv4.hdr.time_to_live = conf->ttl_value;
777                 ipv4_mask.hdr.time_to_live = 0xFF;
778                 item.type = RTE_FLOW_ITEM_TYPE_IPV4;
779                 item.spec = &ipv4;
780                 item.mask = &ipv4_mask;
781                 field = modify_ipv4;
782         } else {
783                 MLX5_ASSERT(attr->ipv6);
784                 memset(&ipv6, 0, sizeof(ipv6));
785                 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
786                 ipv6.hdr.hop_limits = conf->ttl_value;
787                 ipv6_mask.hdr.hop_limits = 0xFF;
788                 item.type = RTE_FLOW_ITEM_TYPE_IPV6;
789                 item.spec = &ipv6;
790                 item.mask = &ipv6_mask;
791                 field = modify_ipv6;
792         }
793         return flow_dv_convert_modify_action(&item, field, NULL, resource,
794                                              MLX5_MODIFICATION_TYPE_SET, error);
795 }
796
797 /**
798  * Convert modify-header decrement TTL action to DV specification.
799  *
800  * @param[in,out] resource
801  *   Pointer to the modify-header resource.
802  * @param[in] action
803  *   Pointer to action specification.
804  * @param[in] items
805  *   Pointer to rte_flow_item objects list.
806  * @param[in] attr
807  *   Pointer to flow attributes structure.
808  * @param[in] dev_flow
809  *   Pointer to the sub flow.
810  * @param[in] tunnel_decap
811  *   Whether action is after tunnel decapsulation.
812  * @param[out] error
813  *   Pointer to the error structure.
814  *
815  * @return
816  *   0 on success, a negative errno value otherwise and rte_errno is set.
817  */
818 static int
819 flow_dv_convert_action_modify_dec_ttl
820                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
821                          const struct rte_flow_item *items,
822                          union flow_dv_attr *attr, struct mlx5_flow *dev_flow,
823                          bool tunnel_decap, struct rte_flow_error *error)
824 {
825         struct rte_flow_item item;
826         struct rte_flow_item_ipv4 ipv4;
827         struct rte_flow_item_ipv4 ipv4_mask;
828         struct rte_flow_item_ipv6 ipv6;
829         struct rte_flow_item_ipv6 ipv6_mask;
830         struct field_modify_info *field;
831
832         if (!attr->valid)
833                 flow_dv_attr_init(items, attr, dev_flow, tunnel_decap);
834         if (attr->ipv4) {
835                 memset(&ipv4, 0, sizeof(ipv4));
836                 memset(&ipv4_mask, 0, sizeof(ipv4_mask));
837                 ipv4.hdr.time_to_live = 0xFF;
838                 ipv4_mask.hdr.time_to_live = 0xFF;
839                 item.type = RTE_FLOW_ITEM_TYPE_IPV4;
840                 item.spec = &ipv4;
841                 item.mask = &ipv4_mask;
842                 field = modify_ipv4;
843         } else {
844                 MLX5_ASSERT(attr->ipv6);
845                 memset(&ipv6, 0, sizeof(ipv6));
846                 memset(&ipv6_mask, 0, sizeof(ipv6_mask));
847                 ipv6.hdr.hop_limits = 0xFF;
848                 ipv6_mask.hdr.hop_limits = 0xFF;
849                 item.type = RTE_FLOW_ITEM_TYPE_IPV6;
850                 item.spec = &ipv6;
851                 item.mask = &ipv6_mask;
852                 field = modify_ipv6;
853         }
854         return flow_dv_convert_modify_action(&item, field, NULL, resource,
855                                              MLX5_MODIFICATION_TYPE_ADD, error);
856 }
857
858 /**
859  * Convert modify-header increment/decrement TCP Sequence number
860  * to DV specification.
861  *
862  * @param[in,out] resource
863  *   Pointer to the modify-header resource.
864  * @param[in] action
865  *   Pointer to action specification.
866  * @param[out] error
867  *   Pointer to the error structure.
868  *
869  * @return
870  *   0 on success, a negative errno value otherwise and rte_errno is set.
871  */
872 static int
873 flow_dv_convert_action_modify_tcp_seq
874                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
875                          const struct rte_flow_action *action,
876                          struct rte_flow_error *error)
877 {
878         const rte_be32_t *conf = (const rte_be32_t *)(action->conf);
879         uint64_t value = rte_be_to_cpu_32(*conf);
880         struct rte_flow_item item;
881         struct rte_flow_item_tcp tcp;
882         struct rte_flow_item_tcp tcp_mask;
883
884         memset(&tcp, 0, sizeof(tcp));
885         memset(&tcp_mask, 0, sizeof(tcp_mask));
886         if (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ)
887                 /*
888                  * The HW has no decrement operation, only increment operation.
889                  * To simulate decrement X from Y using increment operation
890                  * we need to add UINT32_MAX X times to Y.
891                  * Each adding of UINT32_MAX decrements Y by 1.
892                  */
893                 value *= UINT32_MAX;
894         tcp.hdr.sent_seq = rte_cpu_to_be_32((uint32_t)value);
895         tcp_mask.hdr.sent_seq = RTE_BE32(UINT32_MAX);
896         item.type = RTE_FLOW_ITEM_TYPE_TCP;
897         item.spec = &tcp;
898         item.mask = &tcp_mask;
899         return flow_dv_convert_modify_action(&item, modify_tcp, NULL, resource,
900                                              MLX5_MODIFICATION_TYPE_ADD, error);
901 }
902
903 /**
904  * Convert modify-header increment/decrement TCP Acknowledgment number
905  * to DV specification.
906  *
907  * @param[in,out] resource
908  *   Pointer to the modify-header resource.
909  * @param[in] action
910  *   Pointer to action specification.
911  * @param[out] error
912  *   Pointer to the error structure.
913  *
914  * @return
915  *   0 on success, a negative errno value otherwise and rte_errno is set.
916  */
917 static int
918 flow_dv_convert_action_modify_tcp_ack
919                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
920                          const struct rte_flow_action *action,
921                          struct rte_flow_error *error)
922 {
923         const rte_be32_t *conf = (const rte_be32_t *)(action->conf);
924         uint64_t value = rte_be_to_cpu_32(*conf);
925         struct rte_flow_item item;
926         struct rte_flow_item_tcp tcp;
927         struct rte_flow_item_tcp tcp_mask;
928
929         memset(&tcp, 0, sizeof(tcp));
930         memset(&tcp_mask, 0, sizeof(tcp_mask));
931         if (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK)
932                 /*
933                  * The HW has no decrement operation, only increment operation.
934                  * To simulate decrement X from Y using increment operation
935                  * we need to add UINT32_MAX X times to Y.
936                  * Each adding of UINT32_MAX decrements Y by 1.
937                  */
938                 value *= UINT32_MAX;
939         tcp.hdr.recv_ack = rte_cpu_to_be_32((uint32_t)value);
940         tcp_mask.hdr.recv_ack = RTE_BE32(UINT32_MAX);
941         item.type = RTE_FLOW_ITEM_TYPE_TCP;
942         item.spec = &tcp;
943         item.mask = &tcp_mask;
944         return flow_dv_convert_modify_action(&item, modify_tcp, NULL, resource,
945                                              MLX5_MODIFICATION_TYPE_ADD, error);
946 }
947
948 static enum mlx5_modification_field reg_to_field[] = {
949         [REG_NONE] = MLX5_MODI_OUT_NONE,
950         [REG_A] = MLX5_MODI_META_DATA_REG_A,
951         [REG_B] = MLX5_MODI_META_DATA_REG_B,
952         [REG_C_0] = MLX5_MODI_META_REG_C_0,
953         [REG_C_1] = MLX5_MODI_META_REG_C_1,
954         [REG_C_2] = MLX5_MODI_META_REG_C_2,
955         [REG_C_3] = MLX5_MODI_META_REG_C_3,
956         [REG_C_4] = MLX5_MODI_META_REG_C_4,
957         [REG_C_5] = MLX5_MODI_META_REG_C_5,
958         [REG_C_6] = MLX5_MODI_META_REG_C_6,
959         [REG_C_7] = MLX5_MODI_META_REG_C_7,
960 };
961
962 /**
963  * Convert register set to DV specification.
964  *
965  * @param[in,out] resource
966  *   Pointer to the modify-header resource.
967  * @param[in] action
968  *   Pointer to action specification.
969  * @param[out] error
970  *   Pointer to the error structure.
971  *
972  * @return
973  *   0 on success, a negative errno value otherwise and rte_errno is set.
974  */
975 static int
976 flow_dv_convert_action_set_reg
977                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
978                          const struct rte_flow_action *action,
979                          struct rte_flow_error *error)
980 {
981         const struct mlx5_rte_flow_action_set_tag *conf = action->conf;
982         struct mlx5_modification_cmd *actions = resource->actions;
983         uint32_t i = resource->actions_num;
984
985         if (i >= MLX5_MAX_MODIFY_NUM)
986                 return rte_flow_error_set(error, EINVAL,
987                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
988                                           "too many items to modify");
989         MLX5_ASSERT(conf->id != REG_NONE);
990         MLX5_ASSERT(conf->id < RTE_DIM(reg_to_field));
991         actions[i] = (struct mlx5_modification_cmd) {
992                 .action_type = MLX5_MODIFICATION_TYPE_SET,
993                 .field = reg_to_field[conf->id],
994         };
995         actions[i].data0 = rte_cpu_to_be_32(actions[i].data0);
996         actions[i].data1 = rte_cpu_to_be_32(conf->data);
997         ++i;
998         resource->actions_num = i;
999         return 0;
1000 }
1001
1002 /**
1003  * Convert SET_TAG action to DV specification.
1004  *
1005  * @param[in] dev
1006  *   Pointer to the rte_eth_dev structure.
1007  * @param[in,out] resource
1008  *   Pointer to the modify-header resource.
1009  * @param[in] conf
1010  *   Pointer to action specification.
1011  * @param[out] error
1012  *   Pointer to the error structure.
1013  *
1014  * @return
1015  *   0 on success, a negative errno value otherwise and rte_errno is set.
1016  */
1017 static int
1018 flow_dv_convert_action_set_tag
1019                         (struct rte_eth_dev *dev,
1020                          struct mlx5_flow_dv_modify_hdr_resource *resource,
1021                          const struct rte_flow_action_set_tag *conf,
1022                          struct rte_flow_error *error)
1023 {
1024         rte_be32_t data = rte_cpu_to_be_32(conf->data);
1025         rte_be32_t mask = rte_cpu_to_be_32(conf->mask);
1026         struct rte_flow_item item = {
1027                 .spec = &data,
1028                 .mask = &mask,
1029         };
1030         struct field_modify_info reg_c_x[] = {
1031                 [1] = {0, 0, 0},
1032         };
1033         enum mlx5_modification_field reg_type;
1034         int ret;
1035
1036         ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, conf->index, error);
1037         if (ret < 0)
1038                 return ret;
1039         MLX5_ASSERT(ret != REG_NONE);
1040         MLX5_ASSERT((unsigned int)ret < RTE_DIM(reg_to_field));
1041         reg_type = reg_to_field[ret];
1042         MLX5_ASSERT(reg_type > 0);
1043         reg_c_x[0] = (struct field_modify_info){4, 0, reg_type};
1044         return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
1045                                              MLX5_MODIFICATION_TYPE_SET, error);
1046 }
1047
1048 /**
1049  * Convert internal COPY_REG action to DV specification.
1050  *
1051  * @param[in] dev
1052  *   Pointer to the rte_eth_dev structure.
1053  * @param[in,out] res
1054  *   Pointer to the modify-header resource.
1055  * @param[in] action
1056  *   Pointer to action specification.
1057  * @param[out] error
1058  *   Pointer to the error structure.
1059  *
1060  * @return
1061  *   0 on success, a negative errno value otherwise and rte_errno is set.
1062  */
1063 static int
1064 flow_dv_convert_action_copy_mreg(struct rte_eth_dev *dev,
1065                                  struct mlx5_flow_dv_modify_hdr_resource *res,
1066                                  const struct rte_flow_action *action,
1067                                  struct rte_flow_error *error)
1068 {
1069         const struct mlx5_flow_action_copy_mreg *conf = action->conf;
1070         rte_be32_t mask = RTE_BE32(UINT32_MAX);
1071         struct rte_flow_item item = {
1072                 .spec = NULL,
1073                 .mask = &mask,
1074         };
1075         struct field_modify_info reg_src[] = {
1076                 {4, 0, reg_to_field[conf->src]},
1077                 {0, 0, 0},
1078         };
1079         struct field_modify_info reg_dst = {
1080                 .offset = 0,
1081                 .id = reg_to_field[conf->dst],
1082         };
1083         /* Adjust reg_c[0] usage according to reported mask. */
1084         if (conf->dst == REG_C_0 || conf->src == REG_C_0) {
1085                 struct mlx5_priv *priv = dev->data->dev_private;
1086                 uint32_t reg_c0 = priv->sh->dv_regc0_mask;
1087
1088                 MLX5_ASSERT(reg_c0);
1089                 MLX5_ASSERT(priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY);
1090                 if (conf->dst == REG_C_0) {
1091                         /* Copy to reg_c[0], within mask only. */
1092                         reg_dst.offset = rte_bsf32(reg_c0);
1093                         /*
1094                          * Mask is ignoring the enianness, because
1095                          * there is no conversion in datapath.
1096                          */
1097 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
1098                         /* Copy from destination lower bits to reg_c[0]. */
1099                         mask = reg_c0 >> reg_dst.offset;
1100 #else
1101                         /* Copy from destination upper bits to reg_c[0]. */
1102                         mask = reg_c0 << (sizeof(reg_c0) * CHAR_BIT -
1103                                           rte_fls_u32(reg_c0));
1104 #endif
1105                 } else {
1106                         mask = rte_cpu_to_be_32(reg_c0);
1107 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
1108                         /* Copy from reg_c[0] to destination lower bits. */
1109                         reg_dst.offset = 0;
1110 #else
1111                         /* Copy from reg_c[0] to destination upper bits. */
1112                         reg_dst.offset = sizeof(reg_c0) * CHAR_BIT -
1113                                          (rte_fls_u32(reg_c0) -
1114                                           rte_bsf32(reg_c0));
1115 #endif
1116                 }
1117         }
1118         return flow_dv_convert_modify_action(&item,
1119                                              reg_src, &reg_dst, res,
1120                                              MLX5_MODIFICATION_TYPE_COPY,
1121                                              error);
1122 }
1123
1124 /**
1125  * Convert MARK action to DV specification. This routine is used
1126  * in extensive metadata only and requires metadata register to be
1127  * handled. In legacy mode hardware tag resource is engaged.
1128  *
1129  * @param[in] dev
1130  *   Pointer to the rte_eth_dev structure.
1131  * @param[in] conf
1132  *   Pointer to MARK action specification.
1133  * @param[in,out] resource
1134  *   Pointer to the modify-header resource.
1135  * @param[out] error
1136  *   Pointer to the error structure.
1137  *
1138  * @return
1139  *   0 on success, a negative errno value otherwise and rte_errno is set.
1140  */
1141 static int
1142 flow_dv_convert_action_mark(struct rte_eth_dev *dev,
1143                             const struct rte_flow_action_mark *conf,
1144                             struct mlx5_flow_dv_modify_hdr_resource *resource,
1145                             struct rte_flow_error *error)
1146 {
1147         struct mlx5_priv *priv = dev->data->dev_private;
1148         rte_be32_t mask = rte_cpu_to_be_32(MLX5_FLOW_MARK_MASK &
1149                                            priv->sh->dv_mark_mask);
1150         rte_be32_t data = rte_cpu_to_be_32(conf->id) & mask;
1151         struct rte_flow_item item = {
1152                 .spec = &data,
1153                 .mask = &mask,
1154         };
1155         struct field_modify_info reg_c_x[] = {
1156                 {4, 0, 0}, /* dynamic instead of MLX5_MODI_META_REG_C_1. */
1157                 {0, 0, 0},
1158         };
1159         int reg;
1160
1161         if (!mask)
1162                 return rte_flow_error_set(error, EINVAL,
1163                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1164                                           NULL, "zero mark action mask");
1165         reg = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
1166         if (reg < 0)
1167                 return reg;
1168         MLX5_ASSERT(reg > 0);
1169         if (reg == REG_C_0) {
1170                 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
1171                 uint32_t shl_c0 = rte_bsf32(msk_c0);
1172
1173                 data = rte_cpu_to_be_32(rte_cpu_to_be_32(data) << shl_c0);
1174                 mask = rte_cpu_to_be_32(mask) & msk_c0;
1175                 mask = rte_cpu_to_be_32(mask << shl_c0);
1176         }
1177         reg_c_x[0].id = reg_to_field[reg];
1178         return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
1179                                              MLX5_MODIFICATION_TYPE_SET, error);
1180 }
1181
1182 /**
1183  * Get metadata register index for specified steering domain.
1184  *
1185  * @param[in] dev
1186  *   Pointer to the rte_eth_dev structure.
1187  * @param[in] attr
1188  *   Attributes of flow to determine steering domain.
1189  * @param[out] error
1190  *   Pointer to the error structure.
1191  *
1192  * @return
1193  *   positive index on success, a negative errno value otherwise
1194  *   and rte_errno is set.
1195  */
1196 static enum modify_reg
1197 flow_dv_get_metadata_reg(struct rte_eth_dev *dev,
1198                          const struct rte_flow_attr *attr,
1199                          struct rte_flow_error *error)
1200 {
1201         int reg =
1202                 mlx5_flow_get_reg_id(dev, attr->transfer ?
1203                                           MLX5_METADATA_FDB :
1204                                             attr->egress ?
1205                                             MLX5_METADATA_TX :
1206                                             MLX5_METADATA_RX, 0, error);
1207         if (reg < 0)
1208                 return rte_flow_error_set(error,
1209                                           ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
1210                                           NULL, "unavailable "
1211                                           "metadata register");
1212         return reg;
1213 }
1214
1215 /**
1216  * Convert SET_META action to DV specification.
1217  *
1218  * @param[in] dev
1219  *   Pointer to the rte_eth_dev structure.
1220  * @param[in,out] resource
1221  *   Pointer to the modify-header resource.
1222  * @param[in] attr
1223  *   Attributes of flow that includes this item.
1224  * @param[in] conf
1225  *   Pointer to action specification.
1226  * @param[out] error
1227  *   Pointer to the error structure.
1228  *
1229  * @return
1230  *   0 on success, a negative errno value otherwise and rte_errno is set.
1231  */
1232 static int
1233 flow_dv_convert_action_set_meta
1234                         (struct rte_eth_dev *dev,
1235                          struct mlx5_flow_dv_modify_hdr_resource *resource,
1236                          const struct rte_flow_attr *attr,
1237                          const struct rte_flow_action_set_meta *conf,
1238                          struct rte_flow_error *error)
1239 {
1240         uint32_t data = conf->data;
1241         uint32_t mask = conf->mask;
1242         struct rte_flow_item item = {
1243                 .spec = &data,
1244                 .mask = &mask,
1245         };
1246         struct field_modify_info reg_c_x[] = {
1247                 [1] = {0, 0, 0},
1248         };
1249         int reg = flow_dv_get_metadata_reg(dev, attr, error);
1250
1251         if (reg < 0)
1252                 return reg;
1253         /*
1254          * In datapath code there is no endianness
1255          * coversions for perfromance reasons, all
1256          * pattern conversions are done in rte_flow.
1257          */
1258         if (reg == REG_C_0) {
1259                 struct mlx5_priv *priv = dev->data->dev_private;
1260                 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
1261                 uint32_t shl_c0;
1262
1263                 MLX5_ASSERT(msk_c0);
1264 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
1265                 shl_c0 = rte_bsf32(msk_c0);
1266 #else
1267                 shl_c0 = sizeof(msk_c0) * CHAR_BIT - rte_fls_u32(msk_c0);
1268 #endif
1269                 mask <<= shl_c0;
1270                 data <<= shl_c0;
1271                 MLX5_ASSERT(!(~msk_c0 & rte_cpu_to_be_32(mask)));
1272         }
1273         reg_c_x[0] = (struct field_modify_info){4, 0, reg_to_field[reg]};
1274         /* The routine expects parameters in memory as big-endian ones. */
1275         return flow_dv_convert_modify_action(&item, reg_c_x, NULL, resource,
1276                                              MLX5_MODIFICATION_TYPE_SET, error);
1277 }
1278
1279 /**
1280  * Convert modify-header set IPv4 DSCP action to DV specification.
1281  *
1282  * @param[in,out] resource
1283  *   Pointer to the modify-header resource.
1284  * @param[in] action
1285  *   Pointer to action specification.
1286  * @param[out] error
1287  *   Pointer to the error structure.
1288  *
1289  * @return
1290  *   0 on success, a negative errno value otherwise and rte_errno is set.
1291  */
1292 static int
1293 flow_dv_convert_action_modify_ipv4_dscp
1294                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
1295                          const struct rte_flow_action *action,
1296                          struct rte_flow_error *error)
1297 {
1298         const struct rte_flow_action_set_dscp *conf =
1299                 (const struct rte_flow_action_set_dscp *)(action->conf);
1300         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV4 };
1301         struct rte_flow_item_ipv4 ipv4;
1302         struct rte_flow_item_ipv4 ipv4_mask;
1303
1304         memset(&ipv4, 0, sizeof(ipv4));
1305         memset(&ipv4_mask, 0, sizeof(ipv4_mask));
1306         ipv4.hdr.type_of_service = conf->dscp;
1307         ipv4_mask.hdr.type_of_service = RTE_IPV4_HDR_DSCP_MASK >> 2;
1308         item.spec = &ipv4;
1309         item.mask = &ipv4_mask;
1310         return flow_dv_convert_modify_action(&item, modify_ipv4, NULL, resource,
1311                                              MLX5_MODIFICATION_TYPE_SET, error);
1312 }
1313
1314 /**
1315  * Convert modify-header set IPv6 DSCP action to DV specification.
1316  *
1317  * @param[in,out] resource
1318  *   Pointer to the modify-header resource.
1319  * @param[in] action
1320  *   Pointer to action specification.
1321  * @param[out] error
1322  *   Pointer to the error structure.
1323  *
1324  * @return
1325  *   0 on success, a negative errno value otherwise and rte_errno is set.
1326  */
1327 static int
1328 flow_dv_convert_action_modify_ipv6_dscp
1329                         (struct mlx5_flow_dv_modify_hdr_resource *resource,
1330                          const struct rte_flow_action *action,
1331                          struct rte_flow_error *error)
1332 {
1333         const struct rte_flow_action_set_dscp *conf =
1334                 (const struct rte_flow_action_set_dscp *)(action->conf);
1335         struct rte_flow_item item = { .type = RTE_FLOW_ITEM_TYPE_IPV6 };
1336         struct rte_flow_item_ipv6 ipv6;
1337         struct rte_flow_item_ipv6 ipv6_mask;
1338
1339         memset(&ipv6, 0, sizeof(ipv6));
1340         memset(&ipv6_mask, 0, sizeof(ipv6_mask));
1341         /*
1342          * Even though the DSCP bits offset of IPv6 is not byte aligned,
1343          * rdma-core only accept the DSCP bits byte aligned start from
1344          * bit 0 to 5 as to be compatible with IPv4. No need to shift the
1345          * bits in IPv6 case as rdma-core requires byte aligned value.
1346          */
1347         ipv6.hdr.vtc_flow = conf->dscp;
1348         ipv6_mask.hdr.vtc_flow = RTE_IPV6_HDR_DSCP_MASK >> 22;
1349         item.spec = &ipv6;
1350         item.mask = &ipv6_mask;
1351         return flow_dv_convert_modify_action(&item, modify_ipv6, NULL, resource,
1352                                              MLX5_MODIFICATION_TYPE_SET, error);
1353 }
1354
1355 /**
1356  * Validate MARK item.
1357  *
1358  * @param[in] dev
1359  *   Pointer to the rte_eth_dev structure.
1360  * @param[in] item
1361  *   Item specification.
1362  * @param[in] attr
1363  *   Attributes of flow that includes this item.
1364  * @param[out] error
1365  *   Pointer to error structure.
1366  *
1367  * @return
1368  *   0 on success, a negative errno value otherwise and rte_errno is set.
1369  */
1370 static int
1371 flow_dv_validate_item_mark(struct rte_eth_dev *dev,
1372                            const struct rte_flow_item *item,
1373                            const struct rte_flow_attr *attr __rte_unused,
1374                            struct rte_flow_error *error)
1375 {
1376         struct mlx5_priv *priv = dev->data->dev_private;
1377         struct mlx5_dev_config *config = &priv->config;
1378         const struct rte_flow_item_mark *spec = item->spec;
1379         const struct rte_flow_item_mark *mask = item->mask;
1380         const struct rte_flow_item_mark nic_mask = {
1381                 .id = priv->sh->dv_mark_mask,
1382         };
1383         int ret;
1384
1385         if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
1386                 return rte_flow_error_set(error, ENOTSUP,
1387                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1388                                           "extended metadata feature"
1389                                           " isn't enabled");
1390         if (!mlx5_flow_ext_mreg_supported(dev))
1391                 return rte_flow_error_set(error, ENOTSUP,
1392                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1393                                           "extended metadata register"
1394                                           " isn't supported");
1395         if (!nic_mask.id)
1396                 return rte_flow_error_set(error, ENOTSUP,
1397                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1398                                           "extended metadata register"
1399                                           " isn't available");
1400         ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
1401         if (ret < 0)
1402                 return ret;
1403         if (!spec)
1404                 return rte_flow_error_set(error, EINVAL,
1405                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1406                                           item->spec,
1407                                           "data cannot be empty");
1408         if (spec->id >= (MLX5_FLOW_MARK_MAX & nic_mask.id))
1409                 return rte_flow_error_set(error, EINVAL,
1410                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1411                                           &spec->id,
1412                                           "mark id exceeds the limit");
1413         if (!mask)
1414                 mask = &nic_mask;
1415         if (!mask->id)
1416                 return rte_flow_error_set(error, EINVAL,
1417                                         RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
1418                                         "mask cannot be zero");
1419
1420         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1421                                         (const uint8_t *)&nic_mask,
1422                                         sizeof(struct rte_flow_item_mark),
1423                                         error);
1424         if (ret < 0)
1425                 return ret;
1426         return 0;
1427 }
1428
1429 /**
1430  * Validate META item.
1431  *
1432  * @param[in] dev
1433  *   Pointer to the rte_eth_dev structure.
1434  * @param[in] item
1435  *   Item specification.
1436  * @param[in] attr
1437  *   Attributes of flow that includes this item.
1438  * @param[out] error
1439  *   Pointer to error structure.
1440  *
1441  * @return
1442  *   0 on success, a negative errno value otherwise and rte_errno is set.
1443  */
1444 static int
1445 flow_dv_validate_item_meta(struct rte_eth_dev *dev __rte_unused,
1446                            const struct rte_flow_item *item,
1447                            const struct rte_flow_attr *attr,
1448                            struct rte_flow_error *error)
1449 {
1450         struct mlx5_priv *priv = dev->data->dev_private;
1451         struct mlx5_dev_config *config = &priv->config;
1452         const struct rte_flow_item_meta *spec = item->spec;
1453         const struct rte_flow_item_meta *mask = item->mask;
1454         struct rte_flow_item_meta nic_mask = {
1455                 .data = UINT32_MAX
1456         };
1457         int reg;
1458         int ret;
1459
1460         if (!spec)
1461                 return rte_flow_error_set(error, EINVAL,
1462                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1463                                           item->spec,
1464                                           "data cannot be empty");
1465         if (config->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
1466                 if (!mlx5_flow_ext_mreg_supported(dev))
1467                         return rte_flow_error_set(error, ENOTSUP,
1468                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1469                                           "extended metadata register"
1470                                           " isn't supported");
1471                 reg = flow_dv_get_metadata_reg(dev, attr, error);
1472                 if (reg < 0)
1473                         return reg;
1474                 if (reg == REG_B)
1475                         return rte_flow_error_set(error, ENOTSUP,
1476                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1477                                           "match on reg_b "
1478                                           "isn't supported");
1479                 if (reg != REG_A)
1480                         nic_mask.data = priv->sh->dv_meta_mask;
1481         }
1482         if (!mask)
1483                 mask = &rte_flow_item_meta_mask;
1484         if (!mask->data)
1485                 return rte_flow_error_set(error, EINVAL,
1486                                         RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
1487                                         "mask cannot be zero");
1488
1489         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1490                                         (const uint8_t *)&nic_mask,
1491                                         sizeof(struct rte_flow_item_meta),
1492                                         error);
1493         return ret;
1494 }
1495
1496 /**
1497  * Validate TAG item.
1498  *
1499  * @param[in] dev
1500  *   Pointer to the rte_eth_dev structure.
1501  * @param[in] item
1502  *   Item specification.
1503  * @param[in] attr
1504  *   Attributes of flow that includes this item.
1505  * @param[out] error
1506  *   Pointer to error structure.
1507  *
1508  * @return
1509  *   0 on success, a negative errno value otherwise and rte_errno is set.
1510  */
1511 static int
1512 flow_dv_validate_item_tag(struct rte_eth_dev *dev,
1513                           const struct rte_flow_item *item,
1514                           const struct rte_flow_attr *attr __rte_unused,
1515                           struct rte_flow_error *error)
1516 {
1517         const struct rte_flow_item_tag *spec = item->spec;
1518         const struct rte_flow_item_tag *mask = item->mask;
1519         const struct rte_flow_item_tag nic_mask = {
1520                 .data = RTE_BE32(UINT32_MAX),
1521                 .index = 0xff,
1522         };
1523         int ret;
1524
1525         if (!mlx5_flow_ext_mreg_supported(dev))
1526                 return rte_flow_error_set(error, ENOTSUP,
1527                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1528                                           "extensive metadata register"
1529                                           " isn't supported");
1530         if (!spec)
1531                 return rte_flow_error_set(error, EINVAL,
1532                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1533                                           item->spec,
1534                                           "data cannot be empty");
1535         if (!mask)
1536                 mask = &rte_flow_item_tag_mask;
1537         if (!mask->data)
1538                 return rte_flow_error_set(error, EINVAL,
1539                                         RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
1540                                         "mask cannot be zero");
1541
1542         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1543                                         (const uint8_t *)&nic_mask,
1544                                         sizeof(struct rte_flow_item_tag),
1545                                         error);
1546         if (ret < 0)
1547                 return ret;
1548         if (mask->index != 0xff)
1549                 return rte_flow_error_set(error, EINVAL,
1550                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC, NULL,
1551                                           "partial mask for tag index"
1552                                           " is not supported");
1553         ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, spec->index, error);
1554         if (ret < 0)
1555                 return ret;
1556         MLX5_ASSERT(ret != REG_NONE);
1557         return 0;
1558 }
1559
1560 /**
1561  * Validate vport item.
1562  *
1563  * @param[in] dev
1564  *   Pointer to the rte_eth_dev structure.
1565  * @param[in] item
1566  *   Item specification.
1567  * @param[in] attr
1568  *   Attributes of flow that includes this item.
1569  * @param[in] item_flags
1570  *   Bit-fields that holds the items detected until now.
1571  * @param[out] error
1572  *   Pointer to error structure.
1573  *
1574  * @return
1575  *   0 on success, a negative errno value otherwise and rte_errno is set.
1576  */
1577 static int
1578 flow_dv_validate_item_port_id(struct rte_eth_dev *dev,
1579                               const struct rte_flow_item *item,
1580                               const struct rte_flow_attr *attr,
1581                               uint64_t item_flags,
1582                               struct rte_flow_error *error)
1583 {
1584         const struct rte_flow_item_port_id *spec = item->spec;
1585         const struct rte_flow_item_port_id *mask = item->mask;
1586         const struct rte_flow_item_port_id switch_mask = {
1587                         .id = 0xffffffff,
1588         };
1589         struct mlx5_priv *esw_priv;
1590         struct mlx5_priv *dev_priv;
1591         int ret;
1592
1593         if (!attr->transfer)
1594                 return rte_flow_error_set(error, EINVAL,
1595                                           RTE_FLOW_ERROR_TYPE_ITEM,
1596                                           NULL,
1597                                           "match on port id is valid only"
1598                                           " when transfer flag is enabled");
1599         if (item_flags & MLX5_FLOW_ITEM_PORT_ID)
1600                 return rte_flow_error_set(error, ENOTSUP,
1601                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1602                                           "multiple source ports are not"
1603                                           " supported");
1604         if (!mask)
1605                 mask = &switch_mask;
1606         if (mask->id != 0xffffffff)
1607                 return rte_flow_error_set(error, ENOTSUP,
1608                                            RTE_FLOW_ERROR_TYPE_ITEM_MASK,
1609                                            mask,
1610                                            "no support for partial mask on"
1611                                            " \"id\" field");
1612         ret = mlx5_flow_item_acceptable
1613                                 (item, (const uint8_t *)mask,
1614                                  (const uint8_t *)&rte_flow_item_port_id_mask,
1615                                  sizeof(struct rte_flow_item_port_id),
1616                                  error);
1617         if (ret)
1618                 return ret;
1619         if (!spec)
1620                 return 0;
1621         esw_priv = mlx5_port_to_eswitch_info(spec->id, false);
1622         if (!esw_priv)
1623                 return rte_flow_error_set(error, rte_errno,
1624                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC, spec,
1625                                           "failed to obtain E-Switch info for"
1626                                           " port");
1627         dev_priv = mlx5_dev_to_eswitch_info(dev);
1628         if (!dev_priv)
1629                 return rte_flow_error_set(error, rte_errno,
1630                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1631                                           NULL,
1632                                           "failed to obtain E-Switch info");
1633         if (esw_priv->domain_id != dev_priv->domain_id)
1634                 return rte_flow_error_set(error, EINVAL,
1635                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC, spec,
1636                                           "cannot match on a port from a"
1637                                           " different E-Switch");
1638         return 0;
1639 }
1640
1641 /**
1642  * Validate GTP item.
1643  *
1644  * @param[in] dev
1645  *   Pointer to the rte_eth_dev structure.
1646  * @param[in] item
1647  *   Item specification.
1648  * @param[in] item_flags
1649  *   Bit-fields that holds the items detected until now.
1650  * @param[out] error
1651  *   Pointer to error structure.
1652  *
1653  * @return
1654  *   0 on success, a negative errno value otherwise and rte_errno is set.
1655  */
1656 static int
1657 flow_dv_validate_item_gtp(struct rte_eth_dev *dev,
1658                           const struct rte_flow_item *item,
1659                           uint64_t item_flags,
1660                           struct rte_flow_error *error)
1661 {
1662         struct mlx5_priv *priv = dev->data->dev_private;
1663         const struct rte_flow_item_gtp *mask = item->mask;
1664         const struct rte_flow_item_gtp nic_mask = {
1665                 .msg_type = 0xff,
1666                 .teid = RTE_BE32(0xffffffff),
1667         };
1668
1669         if (!priv->config.hca_attr.tunnel_stateless_gtp)
1670                 return rte_flow_error_set(error, ENOTSUP,
1671                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1672                                           "GTP support is not enabled");
1673         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
1674                 return rte_flow_error_set(error, ENOTSUP,
1675                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1676                                           "multiple tunnel layers not"
1677                                           " supported");
1678         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
1679                 return rte_flow_error_set(error, EINVAL,
1680                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1681                                           "no outer UDP layer found");
1682         if (!mask)
1683                 mask = &rte_flow_item_gtp_mask;
1684         return mlx5_flow_item_acceptable
1685                 (item, (const uint8_t *)mask,
1686                  (const uint8_t *)&nic_mask,
1687                  sizeof(struct rte_flow_item_gtp),
1688                  error);
1689 }
1690
1691 /**
1692  * Validate the pop VLAN action.
1693  *
1694  * @param[in] dev
1695  *   Pointer to the rte_eth_dev structure.
1696  * @param[in] action_flags
1697  *   Holds the actions detected until now.
1698  * @param[in] action
1699  *   Pointer to the pop vlan action.
1700  * @param[in] item_flags
1701  *   The items found in this flow rule.
1702  * @param[in] attr
1703  *   Pointer to flow attributes.
1704  * @param[out] error
1705  *   Pointer to error structure.
1706  *
1707  * @return
1708  *   0 on success, a negative errno value otherwise and rte_errno is set.
1709  */
1710 static int
1711 flow_dv_validate_action_pop_vlan(struct rte_eth_dev *dev,
1712                                  uint64_t action_flags,
1713                                  const struct rte_flow_action *action,
1714                                  uint64_t item_flags,
1715                                  const struct rte_flow_attr *attr,
1716                                  struct rte_flow_error *error)
1717 {
1718         const struct mlx5_priv *priv = dev->data->dev_private;
1719
1720         (void)action;
1721         (void)attr;
1722         if (!priv->sh->pop_vlan_action)
1723                 return rte_flow_error_set(error, ENOTSUP,
1724                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1725                                           NULL,
1726                                           "pop vlan action is not supported");
1727         if (attr->egress)
1728                 return rte_flow_error_set(error, ENOTSUP,
1729                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
1730                                           NULL,
1731                                           "pop vlan action not supported for "
1732                                           "egress");
1733         if (action_flags & MLX5_FLOW_VLAN_ACTIONS)
1734                 return rte_flow_error_set(error, ENOTSUP,
1735                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1736                                           "no support for multiple VLAN "
1737                                           "actions");
1738         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_VLAN))
1739                 return rte_flow_error_set(error, ENOTSUP,
1740                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1741                                           NULL,
1742                                           "cannot pop vlan without a "
1743                                           "match on (outer) vlan in the flow");
1744         if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
1745                 return rte_flow_error_set(error, EINVAL,
1746                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1747                                           "wrong action order, port_id should "
1748                                           "be after pop VLAN action");
1749         if (!attr->transfer && priv->representor)
1750                 return rte_flow_error_set(error, ENOTSUP,
1751                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1752                                           "pop vlan action for VF representor "
1753                                           "not supported on NIC table");
1754         return 0;
1755 }
1756
1757 /**
1758  * Get VLAN default info from vlan match info.
1759  *
1760  * @param[in] items
1761  *   the list of item specifications.
1762  * @param[out] vlan
1763  *   pointer VLAN info to fill to.
1764  *
1765  * @return
1766  *   0 on success, a negative errno value otherwise and rte_errno is set.
1767  */
1768 static void
1769 flow_dev_get_vlan_info_from_items(const struct rte_flow_item *items,
1770                                   struct rte_vlan_hdr *vlan)
1771 {
1772         const struct rte_flow_item_vlan nic_mask = {
1773                 .tci = RTE_BE16(MLX5DV_FLOW_VLAN_PCP_MASK |
1774                                 MLX5DV_FLOW_VLAN_VID_MASK),
1775                 .inner_type = RTE_BE16(0xffff),
1776         };
1777
1778         if (items == NULL)
1779                 return;
1780         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1781                 int type = items->type;
1782
1783                 if (type == RTE_FLOW_ITEM_TYPE_VLAN ||
1784                     type == MLX5_RTE_FLOW_ITEM_TYPE_VLAN)
1785                         break;
1786         }
1787         if (items->type != RTE_FLOW_ITEM_TYPE_END) {
1788                 const struct rte_flow_item_vlan *vlan_m = items->mask;
1789                 const struct rte_flow_item_vlan *vlan_v = items->spec;
1790
1791                 if (!vlan_m)
1792                         vlan_m = &nic_mask;
1793                 /* Only full match values are accepted */
1794                 if ((vlan_m->tci & MLX5DV_FLOW_VLAN_PCP_MASK_BE) ==
1795                      MLX5DV_FLOW_VLAN_PCP_MASK_BE) {
1796                         vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_PCP_MASK;
1797                         vlan->vlan_tci |=
1798                                 rte_be_to_cpu_16(vlan_v->tci &
1799                                                  MLX5DV_FLOW_VLAN_PCP_MASK_BE);
1800                 }
1801                 if ((vlan_m->tci & MLX5DV_FLOW_VLAN_VID_MASK_BE) ==
1802                      MLX5DV_FLOW_VLAN_VID_MASK_BE) {
1803                         vlan->vlan_tci &= ~MLX5DV_FLOW_VLAN_VID_MASK;
1804                         vlan->vlan_tci |=
1805                                 rte_be_to_cpu_16(vlan_v->tci &
1806                                                  MLX5DV_FLOW_VLAN_VID_MASK_BE);
1807                 }
1808                 if (vlan_m->inner_type == nic_mask.inner_type)
1809                         vlan->eth_proto = rte_be_to_cpu_16(vlan_v->inner_type &
1810                                                            vlan_m->inner_type);
1811         }
1812 }
1813
1814 /**
1815  * Validate the push VLAN action.
1816  *
1817  * @param[in] dev
1818  *   Pointer to the rte_eth_dev structure.
1819  * @param[in] action_flags
1820  *   Holds the actions detected until now.
1821  * @param[in] item_flags
1822  *   The items found in this flow rule.
1823  * @param[in] action
1824  *   Pointer to the action structure.
1825  * @param[in] attr
1826  *   Pointer to flow attributes
1827  * @param[out] error
1828  *   Pointer to error structure.
1829  *
1830  * @return
1831  *   0 on success, a negative errno value otherwise and rte_errno is set.
1832  */
1833 static int
1834 flow_dv_validate_action_push_vlan(struct rte_eth_dev *dev,
1835                                   uint64_t action_flags,
1836                                   const struct rte_flow_item_vlan *vlan_m,
1837                                   const struct rte_flow_action *action,
1838                                   const struct rte_flow_attr *attr,
1839                                   struct rte_flow_error *error)
1840 {
1841         const struct rte_flow_action_of_push_vlan *push_vlan = action->conf;
1842         const struct mlx5_priv *priv = dev->data->dev_private;
1843
1844         if (!attr->transfer && attr->ingress)
1845                 return rte_flow_error_set(error, ENOTSUP,
1846                                           RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1847                                           NULL,
1848                                           "push VLAN action not supported for "
1849                                           "ingress");
1850         if (push_vlan->ethertype != RTE_BE16(RTE_ETHER_TYPE_VLAN) &&
1851             push_vlan->ethertype != RTE_BE16(RTE_ETHER_TYPE_QINQ))
1852                 return rte_flow_error_set(error, EINVAL,
1853                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1854                                           "invalid vlan ethertype");
1855         if (action_flags & MLX5_FLOW_VLAN_ACTIONS)
1856                 return rte_flow_error_set(error, ENOTSUP,
1857                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1858                                           "no support for multiple VLAN "
1859                                           "actions");
1860         if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
1861                 return rte_flow_error_set(error, EINVAL,
1862                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1863                                           "wrong action order, port_id should "
1864                                           "be after push VLAN");
1865         if (!attr->transfer && priv->representor)
1866                 return rte_flow_error_set(error, ENOTSUP,
1867                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1868                                           "push vlan action for VF representor "
1869                                           "not supported on NIC table");
1870         if (vlan_m &&
1871             (vlan_m->tci & MLX5DV_FLOW_VLAN_PCP_MASK_BE) &&
1872             (vlan_m->tci & MLX5DV_FLOW_VLAN_PCP_MASK_BE) !=
1873                 MLX5DV_FLOW_VLAN_PCP_MASK_BE &&
1874             !(action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_PCP) &&
1875             !(mlx5_flow_find_action
1876                 (action + 1, RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP)))
1877                 return rte_flow_error_set(error, EINVAL,
1878                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1879                                           "not full match mask on VLAN PCP and "
1880                                           "there is no of_set_vlan_pcp action, "
1881                                           "push VLAN action cannot figure out "
1882                                           "PCP value");
1883         if (vlan_m &&
1884             (vlan_m->tci & MLX5DV_FLOW_VLAN_VID_MASK_BE) &&
1885             (vlan_m->tci & MLX5DV_FLOW_VLAN_VID_MASK_BE) !=
1886                 MLX5DV_FLOW_VLAN_VID_MASK_BE &&
1887             !(action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_VID) &&
1888             !(mlx5_flow_find_action
1889                 (action + 1, RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID)))
1890                 return rte_flow_error_set(error, EINVAL,
1891                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1892                                           "not full match mask on VLAN VID and "
1893                                           "there is no of_set_vlan_vid action, "
1894                                           "push VLAN action cannot figure out "
1895                                           "VID value");
1896         (void)attr;
1897         return 0;
1898 }
1899
1900 /**
1901  * Validate the set VLAN PCP.
1902  *
1903  * @param[in] action_flags
1904  *   Holds the actions detected until now.
1905  * @param[in] actions
1906  *   Pointer to the list of actions remaining in the flow rule.
1907  * @param[out] error
1908  *   Pointer to error structure.
1909  *
1910  * @return
1911  *   0 on success, a negative errno value otherwise and rte_errno is set.
1912  */
1913 static int
1914 flow_dv_validate_action_set_vlan_pcp(uint64_t action_flags,
1915                                      const struct rte_flow_action actions[],
1916                                      struct rte_flow_error *error)
1917 {
1918         const struct rte_flow_action *action = actions;
1919         const struct rte_flow_action_of_set_vlan_pcp *conf = action->conf;
1920
1921         if (conf->vlan_pcp > 7)
1922                 return rte_flow_error_set(error, EINVAL,
1923                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1924                                           "VLAN PCP value is too big");
1925         if (!(action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN))
1926                 return rte_flow_error_set(error, ENOTSUP,
1927                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1928                                           "set VLAN PCP action must follow "
1929                                           "the push VLAN action");
1930         if (action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_PCP)
1931                 return rte_flow_error_set(error, ENOTSUP,
1932                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1933                                           "Multiple VLAN PCP modification are "
1934                                           "not supported");
1935         if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
1936                 return rte_flow_error_set(error, EINVAL,
1937                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1938                                           "wrong action order, port_id should "
1939                                           "be after set VLAN PCP");
1940         return 0;
1941 }
1942
1943 /**
1944  * Validate the set VLAN VID.
1945  *
1946  * @param[in] item_flags
1947  *   Holds the items detected in this rule.
1948  * @param[in] action_flags
1949  *   Holds the actions detected until now.
1950  * @param[in] actions
1951  *   Pointer to the list of actions remaining in the flow rule.
1952  * @param[out] error
1953  *   Pointer to error structure.
1954  *
1955  * @return
1956  *   0 on success, a negative errno value otherwise and rte_errno is set.
1957  */
1958 static int
1959 flow_dv_validate_action_set_vlan_vid(uint64_t item_flags,
1960                                      uint64_t action_flags,
1961                                      const struct rte_flow_action actions[],
1962                                      struct rte_flow_error *error)
1963 {
1964         const struct rte_flow_action *action = actions;
1965         const struct rte_flow_action_of_set_vlan_vid *conf = action->conf;
1966
1967         if (conf->vlan_vid > RTE_BE16(0xFFE))
1968                 return rte_flow_error_set(error, EINVAL,
1969                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1970                                           "VLAN VID value is too big");
1971         if (!(action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN) &&
1972             !(item_flags & MLX5_FLOW_LAYER_OUTER_VLAN))
1973                 return rte_flow_error_set(error, ENOTSUP,
1974                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1975                                           "set VLAN VID action must follow push"
1976                                           " VLAN action or match on VLAN item");
1977         if (action_flags & MLX5_FLOW_ACTION_OF_SET_VLAN_VID)
1978                 return rte_flow_error_set(error, ENOTSUP,
1979                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1980                                           "Multiple VLAN VID modifications are "
1981                                           "not supported");
1982         if (action_flags & MLX5_FLOW_ACTION_PORT_ID)
1983                 return rte_flow_error_set(error, EINVAL,
1984                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1985                                           "wrong action order, port_id should "
1986                                           "be after set VLAN VID");
1987         return 0;
1988 }
1989
1990 /*
1991  * Validate the FLAG action.
1992  *
1993  * @param[in] dev
1994  *   Pointer to the rte_eth_dev structure.
1995  * @param[in] action_flags
1996  *   Holds the actions detected until now.
1997  * @param[in] attr
1998  *   Pointer to flow attributes
1999  * @param[out] error
2000  *   Pointer to error structure.
2001  *
2002  * @return
2003  *   0 on success, a negative errno value otherwise and rte_errno is set.
2004  */
2005 static int
2006 flow_dv_validate_action_flag(struct rte_eth_dev *dev,
2007                              uint64_t action_flags,
2008                              const struct rte_flow_attr *attr,
2009                              struct rte_flow_error *error)
2010 {
2011         struct mlx5_priv *priv = dev->data->dev_private;
2012         struct mlx5_dev_config *config = &priv->config;
2013         int ret;
2014
2015         /* Fall back if no extended metadata register support. */
2016         if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
2017                 return mlx5_flow_validate_action_flag(action_flags, attr,
2018                                                       error);
2019         /* Extensive metadata mode requires registers. */
2020         if (!mlx5_flow_ext_mreg_supported(dev))
2021                 return rte_flow_error_set(error, ENOTSUP,
2022                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2023                                           "no metadata registers "
2024                                           "to support flag action");
2025         if (!(priv->sh->dv_mark_mask & MLX5_FLOW_MARK_DEFAULT))
2026                 return rte_flow_error_set(error, ENOTSUP,
2027                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2028                                           "extended metadata register"
2029                                           " isn't available");
2030         ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
2031         if (ret < 0)
2032                 return ret;
2033         MLX5_ASSERT(ret > 0);
2034         if (action_flags & MLX5_FLOW_ACTION_MARK)
2035                 return rte_flow_error_set(error, EINVAL,
2036                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2037                                           "can't mark and flag in same flow");
2038         if (action_flags & MLX5_FLOW_ACTION_FLAG)
2039                 return rte_flow_error_set(error, EINVAL,
2040                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2041                                           "can't have 2 flag"
2042                                           " actions in same flow");
2043         return 0;
2044 }
2045
2046 /**
2047  * Validate MARK action.
2048  *
2049  * @param[in] dev
2050  *   Pointer to the rte_eth_dev structure.
2051  * @param[in] action
2052  *   Pointer to action.
2053  * @param[in] action_flags
2054  *   Holds the actions detected until now.
2055  * @param[in] attr
2056  *   Pointer to flow attributes
2057  * @param[out] error
2058  *   Pointer to error structure.
2059  *
2060  * @return
2061  *   0 on success, a negative errno value otherwise and rte_errno is set.
2062  */
2063 static int
2064 flow_dv_validate_action_mark(struct rte_eth_dev *dev,
2065                              const struct rte_flow_action *action,
2066                              uint64_t action_flags,
2067                              const struct rte_flow_attr *attr,
2068                              struct rte_flow_error *error)
2069 {
2070         struct mlx5_priv *priv = dev->data->dev_private;
2071         struct mlx5_dev_config *config = &priv->config;
2072         const struct rte_flow_action_mark *mark = action->conf;
2073         int ret;
2074
2075         /* Fall back if no extended metadata register support. */
2076         if (config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY)
2077                 return mlx5_flow_validate_action_mark(action, action_flags,
2078                                                       attr, error);
2079         /* Extensive metadata mode requires registers. */
2080         if (!mlx5_flow_ext_mreg_supported(dev))
2081                 return rte_flow_error_set(error, ENOTSUP,
2082                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2083                                           "no metadata registers "
2084                                           "to support mark action");
2085         if (!priv->sh->dv_mark_mask)
2086                 return rte_flow_error_set(error, ENOTSUP,
2087                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2088                                           "extended metadata register"
2089                                           " isn't available");
2090         ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
2091         if (ret < 0)
2092                 return ret;
2093         MLX5_ASSERT(ret > 0);
2094         if (!mark)
2095                 return rte_flow_error_set(error, EINVAL,
2096                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2097                                           "configuration cannot be null");
2098         if (mark->id >= (MLX5_FLOW_MARK_MAX & priv->sh->dv_mark_mask))
2099                 return rte_flow_error_set(error, EINVAL,
2100                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
2101                                           &mark->id,
2102                                           "mark id exceeds the limit");
2103         if (action_flags & MLX5_FLOW_ACTION_FLAG)
2104                 return rte_flow_error_set(error, EINVAL,
2105                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2106                                           "can't flag and mark in same flow");
2107         if (action_flags & MLX5_FLOW_ACTION_MARK)
2108                 return rte_flow_error_set(error, EINVAL,
2109                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2110                                           "can't have 2 mark actions in same"
2111                                           " flow");
2112         return 0;
2113 }
2114
2115 /**
2116  * Validate SET_META action.
2117  *
2118  * @param[in] dev
2119  *   Pointer to the rte_eth_dev structure.
2120  * @param[in] action
2121  *   Pointer to the action structure.
2122  * @param[in] action_flags
2123  *   Holds the actions detected until now.
2124  * @param[in] attr
2125  *   Pointer to flow attributes
2126  * @param[out] error
2127  *   Pointer to error structure.
2128  *
2129  * @return
2130  *   0 on success, a negative errno value otherwise and rte_errno is set.
2131  */
2132 static int
2133 flow_dv_validate_action_set_meta(struct rte_eth_dev *dev,
2134                                  const struct rte_flow_action *action,
2135                                  uint64_t action_flags __rte_unused,
2136                                  const struct rte_flow_attr *attr,
2137                                  struct rte_flow_error *error)
2138 {
2139         const struct rte_flow_action_set_meta *conf;
2140         uint32_t nic_mask = UINT32_MAX;
2141         int reg;
2142
2143         if (!mlx5_flow_ext_mreg_supported(dev))
2144                 return rte_flow_error_set(error, ENOTSUP,
2145                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2146                                           "extended metadata register"
2147                                           " isn't supported");
2148         reg = flow_dv_get_metadata_reg(dev, attr, error);
2149         if (reg < 0)
2150                 return reg;
2151         if (reg != REG_A && reg != REG_B) {
2152                 struct mlx5_priv *priv = dev->data->dev_private;
2153
2154                 nic_mask = priv->sh->dv_meta_mask;
2155         }
2156         if (!(action->conf))
2157                 return rte_flow_error_set(error, EINVAL,
2158                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2159                                           "configuration cannot be null");
2160         conf = (const struct rte_flow_action_set_meta *)action->conf;
2161         if (!conf->mask)
2162                 return rte_flow_error_set(error, EINVAL,
2163                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2164                                           "zero mask doesn't have any effect");
2165         if (conf->mask & ~nic_mask)
2166                 return rte_flow_error_set(error, EINVAL,
2167                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2168                                           "meta data must be within reg C0");
2169         return 0;
2170 }
2171
2172 /**
2173  * Validate SET_TAG action.
2174  *
2175  * @param[in] dev
2176  *   Pointer to the rte_eth_dev structure.
2177  * @param[in] action
2178  *   Pointer to the action structure.
2179  * @param[in] action_flags
2180  *   Holds the actions detected until now.
2181  * @param[in] attr
2182  *   Pointer to flow attributes
2183  * @param[out] error
2184  *   Pointer to error structure.
2185  *
2186  * @return
2187  *   0 on success, a negative errno value otherwise and rte_errno is set.
2188  */
2189 static int
2190 flow_dv_validate_action_set_tag(struct rte_eth_dev *dev,
2191                                 const struct rte_flow_action *action,
2192                                 uint64_t action_flags,
2193                                 const struct rte_flow_attr *attr,
2194                                 struct rte_flow_error *error)
2195 {
2196         const struct rte_flow_action_set_tag *conf;
2197         const uint64_t terminal_action_flags =
2198                 MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_QUEUE |
2199                 MLX5_FLOW_ACTION_RSS;
2200         int ret;
2201
2202         if (!mlx5_flow_ext_mreg_supported(dev))
2203                 return rte_flow_error_set(error, ENOTSUP,
2204                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2205                                           "extensive metadata register"
2206                                           " isn't supported");
2207         if (!(action->conf))
2208                 return rte_flow_error_set(error, EINVAL,
2209                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2210                                           "configuration cannot be null");
2211         conf = (const struct rte_flow_action_set_tag *)action->conf;
2212         if (!conf->mask)
2213                 return rte_flow_error_set(error, EINVAL,
2214                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2215                                           "zero mask doesn't have any effect");
2216         ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, conf->index, error);
2217         if (ret < 0)
2218                 return ret;
2219         if (!attr->transfer && attr->ingress &&
2220             (action_flags & terminal_action_flags))
2221                 return rte_flow_error_set(error, EINVAL,
2222                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2223                                           "set_tag has no effect"
2224                                           " with terminal actions");
2225         return 0;
2226 }
2227
2228 /**
2229  * Validate count action.
2230  *
2231  * @param[in] dev
2232  *   Pointer to rte_eth_dev structure.
2233  * @param[out] error
2234  *   Pointer to error structure.
2235  *
2236  * @return
2237  *   0 on success, a negative errno value otherwise and rte_errno is set.
2238  */
2239 static int
2240 flow_dv_validate_action_count(struct rte_eth_dev *dev,
2241                               struct rte_flow_error *error)
2242 {
2243         struct mlx5_priv *priv = dev->data->dev_private;
2244
2245         if (!priv->config.devx)
2246                 goto notsup_err;
2247 #ifdef HAVE_IBV_FLOW_DEVX_COUNTERS
2248         return 0;
2249 #endif
2250 notsup_err:
2251         return rte_flow_error_set
2252                       (error, ENOTSUP,
2253                        RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2254                        NULL,
2255                        "count action not supported");
2256 }
2257
2258 /**
2259  * Validate the L2 encap action.
2260  *
2261  * @param[in] dev
2262  *   Pointer to the rte_eth_dev structure.
2263  * @param[in] action_flags
2264  *   Holds the actions detected until now.
2265  * @param[in] action
2266  *   Pointer to the action structure.
2267  * @param[in] attr
2268  *   Pointer to flow attributes.
2269  * @param[out] error
2270  *   Pointer to error structure.
2271  *
2272  * @return
2273  *   0 on success, a negative errno value otherwise and rte_errno is set.
2274  */
2275 static int
2276 flow_dv_validate_action_l2_encap(struct rte_eth_dev *dev,
2277                                  uint64_t action_flags,
2278                                  const struct rte_flow_action *action,
2279                                  const struct rte_flow_attr *attr,
2280                                  struct rte_flow_error *error)
2281 {
2282         const struct mlx5_priv *priv = dev->data->dev_private;
2283
2284         if (!(action->conf))
2285                 return rte_flow_error_set(error, EINVAL,
2286                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
2287                                           "configuration cannot be null");
2288         if (action_flags & MLX5_FLOW_ACTION_ENCAP)
2289                 return rte_flow_error_set(error, EINVAL,
2290                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2291                                           "can only have a single encap action "
2292                                           "in a flow");
2293         if (!attr->transfer && priv->representor)
2294                 return rte_flow_error_set(error, ENOTSUP,
2295                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2296                                           "encap action for VF representor "
2297                                           "not supported on NIC table");
2298         return 0;
2299 }
2300
2301 /**
2302  * Validate a decap action.
2303  *
2304  * @param[in] dev
2305  *   Pointer to the rte_eth_dev structure.
2306  * @param[in] action_flags
2307  *   Holds the actions detected until now.
2308  * @param[in] attr
2309  *   Pointer to flow attributes
2310  * @param[out] error
2311  *   Pointer to error structure.
2312  *
2313  * @return
2314  *   0 on success, a negative errno value otherwise and rte_errno is set.
2315  */
2316 static int
2317 flow_dv_validate_action_decap(struct rte_eth_dev *dev,
2318                               uint64_t action_flags,
2319                               const struct rte_flow_attr *attr,
2320                               struct rte_flow_error *error)
2321 {
2322         const struct mlx5_priv *priv = dev->data->dev_private;
2323
2324         if (action_flags & MLX5_FLOW_XCAP_ACTIONS)
2325                 return rte_flow_error_set(error, ENOTSUP,
2326                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2327                                           action_flags &
2328                                           MLX5_FLOW_ACTION_DECAP ? "can only "
2329                                           "have a single decap action" : "decap "
2330                                           "after encap is not supported");
2331         if (action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS)
2332                 return rte_flow_error_set(error, EINVAL,
2333                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2334                                           "can't have decap action after"
2335                                           " modify action");
2336         if (attr->egress)
2337                 return rte_flow_error_set(error, ENOTSUP,
2338                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
2339                                           NULL,
2340                                           "decap action not supported for "
2341                                           "egress");
2342         if (!attr->transfer && priv->representor)
2343                 return rte_flow_error_set(error, ENOTSUP,
2344                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2345                                           "decap action for VF representor "
2346                                           "not supported on NIC table");
2347         return 0;
2348 }
2349
2350 const struct rte_flow_action_raw_decap empty_decap = {.data = NULL, .size = 0,};
2351
2352 /**
2353  * Validate the raw encap and decap actions.
2354  *
2355  * @param[in] dev
2356  *   Pointer to the rte_eth_dev structure.
2357  * @param[in] decap
2358  *   Pointer to the decap action.
2359  * @param[in] encap
2360  *   Pointer to the encap action.
2361  * @param[in] attr
2362  *   Pointer to flow attributes
2363  * @param[in/out] action_flags
2364  *   Holds the actions detected until now.
2365  * @param[out] actions_n
2366  *   pointer to the number of actions counter.
2367  * @param[out] error
2368  *   Pointer to error structure.
2369  *
2370  * @return
2371  *   0 on success, a negative errno value otherwise and rte_errno is set.
2372  */
2373 static int
2374 flow_dv_validate_action_raw_encap_decap
2375         (struct rte_eth_dev *dev,
2376          const struct rte_flow_action_raw_decap *decap,
2377          const struct rte_flow_action_raw_encap *encap,
2378          const struct rte_flow_attr *attr, uint64_t *action_flags,
2379          int *actions_n, struct rte_flow_error *error)
2380 {
2381         const struct mlx5_priv *priv = dev->data->dev_private;
2382         int ret;
2383
2384         if (encap && (!encap->size || !encap->data))
2385                 return rte_flow_error_set(error, EINVAL,
2386                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2387                                           "raw encap data cannot be empty");
2388         if (decap && encap) {
2389                 if (decap->size <= MLX5_ENCAPSULATION_DECISION_SIZE &&
2390                     encap->size > MLX5_ENCAPSULATION_DECISION_SIZE)
2391                         /* L3 encap. */
2392                         decap = NULL;
2393                 else if (encap->size <=
2394                            MLX5_ENCAPSULATION_DECISION_SIZE &&
2395                            decap->size >
2396                            MLX5_ENCAPSULATION_DECISION_SIZE)
2397                         /* L3 decap. */
2398                         encap = NULL;
2399                 else if (encap->size >
2400                            MLX5_ENCAPSULATION_DECISION_SIZE &&
2401                            decap->size >
2402                            MLX5_ENCAPSULATION_DECISION_SIZE)
2403                         /* 2 L2 actions: encap and decap. */
2404                         ;
2405                 else
2406                         return rte_flow_error_set(error,
2407                                 ENOTSUP,
2408                                 RTE_FLOW_ERROR_TYPE_ACTION,
2409                                 NULL, "unsupported too small "
2410                                 "raw decap and too small raw "
2411                                 "encap combination");
2412         }
2413         if (decap) {
2414                 ret = flow_dv_validate_action_decap(dev, *action_flags, attr,
2415                                                     error);
2416                 if (ret < 0)
2417                         return ret;
2418                 *action_flags |= MLX5_FLOW_ACTION_DECAP;
2419                 ++(*actions_n);
2420         }
2421         if (encap) {
2422                 if (encap->size <= MLX5_ENCAPSULATION_DECISION_SIZE)
2423                         return rte_flow_error_set(error, ENOTSUP,
2424                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2425                                                   NULL,
2426                                                   "small raw encap size");
2427                 if (*action_flags & MLX5_FLOW_ACTION_ENCAP)
2428                         return rte_flow_error_set(error, EINVAL,
2429                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2430                                                   NULL,
2431                                                   "more than one encap action");
2432                 if (!attr->transfer && priv->representor)
2433                         return rte_flow_error_set
2434                                         (error, ENOTSUP,
2435                                          RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2436                                          "encap action for VF representor "
2437                                          "not supported on NIC table");
2438                 *action_flags |= MLX5_FLOW_ACTION_ENCAP;
2439                 ++(*actions_n);
2440         }
2441         return 0;
2442 }
2443
2444 /**
2445  * Find existing encap/decap resource or create and register a new one.
2446  *
2447  * @param[in, out] dev
2448  *   Pointer to rte_eth_dev structure.
2449  * @param[in, out] resource
2450  *   Pointer to encap/decap resource.
2451  * @parm[in, out] dev_flow
2452  *   Pointer to the dev_flow.
2453  * @param[out] error
2454  *   pointer to error structure.
2455  *
2456  * @return
2457  *   0 on success otherwise -errno and errno is set.
2458  */
2459 static int
2460 flow_dv_encap_decap_resource_register
2461                         (struct rte_eth_dev *dev,
2462                          struct mlx5_flow_dv_encap_decap_resource *resource,
2463                          struct mlx5_flow *dev_flow,
2464                          struct rte_flow_error *error)
2465 {
2466         struct mlx5_priv *priv = dev->data->dev_private;
2467         struct mlx5_ibv_shared *sh = priv->sh;
2468         struct mlx5_flow_dv_encap_decap_resource *cache_resource;
2469         struct mlx5dv_dr_domain *domain;
2470         uint32_t idx = 0;
2471
2472         resource->flags = dev_flow->dv.group ? 0 : 1;
2473         if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
2474                 domain = sh->fdb_domain;
2475         else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_RX)
2476                 domain = sh->rx_domain;
2477         else
2478                 domain = sh->tx_domain;
2479         /* Lookup a matching resource from cache. */
2480         ILIST_FOREACH(sh->ipool[MLX5_IPOOL_DECAP_ENCAP], sh->encaps_decaps, idx,
2481                       cache_resource, next) {
2482                 if (resource->reformat_type == cache_resource->reformat_type &&
2483                     resource->ft_type == cache_resource->ft_type &&
2484                     resource->flags == cache_resource->flags &&
2485                     resource->size == cache_resource->size &&
2486                     !memcmp((const void *)resource->buf,
2487                             (const void *)cache_resource->buf,
2488                             resource->size)) {
2489                         DRV_LOG(DEBUG, "encap/decap resource %p: refcnt %d++",
2490                                 (void *)cache_resource,
2491                                 rte_atomic32_read(&cache_resource->refcnt));
2492                         rte_atomic32_inc(&cache_resource->refcnt);
2493                         dev_flow->handle->dvh.encap_decap = idx;
2494                         dev_flow->dv.encap_decap = cache_resource;
2495                         return 0;
2496                 }
2497         }
2498         /* Register new encap/decap resource. */
2499         cache_resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_DECAP_ENCAP],
2500                                        &dev_flow->handle->dvh.encap_decap);
2501         if (!cache_resource)
2502                 return rte_flow_error_set(error, ENOMEM,
2503                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2504                                           "cannot allocate resource memory");
2505         *cache_resource = *resource;
2506         cache_resource->verbs_action =
2507                 mlx5_glue->dv_create_flow_action_packet_reformat
2508                         (sh->ctx, cache_resource->reformat_type,
2509                          cache_resource->ft_type, domain, cache_resource->flags,
2510                          cache_resource->size,
2511                          (cache_resource->size ? cache_resource->buf : NULL));
2512         if (!cache_resource->verbs_action) {
2513                 rte_free(cache_resource);
2514                 return rte_flow_error_set(error, ENOMEM,
2515                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2516                                           NULL, "cannot create action");
2517         }
2518         rte_atomic32_init(&cache_resource->refcnt);
2519         rte_atomic32_inc(&cache_resource->refcnt);
2520         ILIST_INSERT(sh->ipool[MLX5_IPOOL_DECAP_ENCAP], &sh->encaps_decaps,
2521                      dev_flow->handle->dvh.encap_decap, cache_resource, next);
2522         dev_flow->dv.encap_decap = cache_resource;
2523         DRV_LOG(DEBUG, "new encap/decap resource %p: refcnt %d++",
2524                 (void *)cache_resource,
2525                 rte_atomic32_read(&cache_resource->refcnt));
2526         return 0;
2527 }
2528
2529 /**
2530  * Find existing table jump resource or create and register a new one.
2531  *
2532  * @param[in, out] dev
2533  *   Pointer to rte_eth_dev structure.
2534  * @param[in, out] tbl
2535  *   Pointer to flow table resource.
2536  * @parm[in, out] dev_flow
2537  *   Pointer to the dev_flow.
2538  * @param[out] error
2539  *   pointer to error structure.
2540  *
2541  * @return
2542  *   0 on success otherwise -errno and errno is set.
2543  */
2544 static int
2545 flow_dv_jump_tbl_resource_register
2546                         (struct rte_eth_dev *dev __rte_unused,
2547                          struct mlx5_flow_tbl_resource *tbl,
2548                          struct mlx5_flow *dev_flow,
2549                          struct rte_flow_error *error)
2550 {
2551         struct mlx5_flow_tbl_data_entry *tbl_data =
2552                 container_of(tbl, struct mlx5_flow_tbl_data_entry, tbl);
2553         int cnt;
2554
2555         MLX5_ASSERT(tbl);
2556         cnt = rte_atomic32_read(&tbl_data->jump.refcnt);
2557         if (!cnt) {
2558                 tbl_data->jump.action =
2559                         mlx5_glue->dr_create_flow_action_dest_flow_tbl
2560                         (tbl->obj);
2561                 if (!tbl_data->jump.action)
2562                         return rte_flow_error_set(error, ENOMEM,
2563                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2564                                         NULL, "cannot create jump action");
2565                 DRV_LOG(DEBUG, "new jump table resource %p: refcnt %d++",
2566                         (void *)&tbl_data->jump, cnt);
2567         } else {
2568                 /* old jump should not make the table ref++. */
2569                 flow_dv_tbl_resource_release(dev, &tbl_data->tbl);
2570                 MLX5_ASSERT(tbl_data->jump.action);
2571                 DRV_LOG(DEBUG, "existed jump table resource %p: refcnt %d++",
2572                         (void *)&tbl_data->jump, cnt);
2573         }
2574         rte_atomic32_inc(&tbl_data->jump.refcnt);
2575         dev_flow->handle->jump = tbl_data->idx;
2576         dev_flow->dv.jump = &tbl_data->jump;
2577         return 0;
2578 }
2579
2580 /**
2581  * Find existing table port ID resource or create and register a new one.
2582  *
2583  * @param[in, out] dev
2584  *   Pointer to rte_eth_dev structure.
2585  * @param[in, out] resource
2586  *   Pointer to port ID action resource.
2587  * @parm[in, out] dev_flow
2588  *   Pointer to the dev_flow.
2589  * @param[out] error
2590  *   pointer to error structure.
2591  *
2592  * @return
2593  *   0 on success otherwise -errno and errno is set.
2594  */
2595 static int
2596 flow_dv_port_id_action_resource_register
2597                         (struct rte_eth_dev *dev,
2598                          struct mlx5_flow_dv_port_id_action_resource *resource,
2599                          struct mlx5_flow *dev_flow,
2600                          struct rte_flow_error *error)
2601 {
2602         struct mlx5_priv *priv = dev->data->dev_private;
2603         struct mlx5_ibv_shared *sh = priv->sh;
2604         struct mlx5_flow_dv_port_id_action_resource *cache_resource;
2605         uint32_t idx = 0;
2606
2607         /* Lookup a matching resource from cache. */
2608         ILIST_FOREACH(sh->ipool[MLX5_IPOOL_PORT_ID], sh->port_id_action_list,
2609                       idx, cache_resource, next) {
2610                 if (resource->port_id == cache_resource->port_id) {
2611                         DRV_LOG(DEBUG, "port id action resource resource %p: "
2612                                 "refcnt %d++",
2613                                 (void *)cache_resource,
2614                                 rte_atomic32_read(&cache_resource->refcnt));
2615                         rte_atomic32_inc(&cache_resource->refcnt);
2616                         dev_flow->handle->port_id_action = idx;
2617                         dev_flow->dv.port_id_action = cache_resource;
2618                         return 0;
2619                 }
2620         }
2621         /* Register new port id action resource. */
2622         cache_resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_PORT_ID],
2623                                        &dev_flow->handle->port_id_action);
2624         if (!cache_resource)
2625                 return rte_flow_error_set(error, ENOMEM,
2626                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2627                                           "cannot allocate resource memory");
2628         *cache_resource = *resource;
2629         /*
2630          * Depending on rdma_core version the glue routine calls
2631          * either mlx5dv_dr_action_create_dest_ib_port(domain, ibv_port)
2632          * or mlx5dv_dr_action_create_dest_vport(domain, vport_id).
2633          */
2634         cache_resource->action =
2635                 mlx5_glue->dr_create_flow_action_dest_port
2636                         (priv->sh->fdb_domain, resource->port_id);
2637         if (!cache_resource->action) {
2638                 rte_free(cache_resource);
2639                 return rte_flow_error_set(error, ENOMEM,
2640                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2641                                           NULL, "cannot create action");
2642         }
2643         rte_atomic32_init(&cache_resource->refcnt);
2644         rte_atomic32_inc(&cache_resource->refcnt);
2645         ILIST_INSERT(sh->ipool[MLX5_IPOOL_PORT_ID], &sh->port_id_action_list,
2646                      dev_flow->handle->port_id_action, cache_resource, next);
2647         dev_flow->dv.port_id_action = cache_resource;
2648         DRV_LOG(DEBUG, "new port id action resource %p: refcnt %d++",
2649                 (void *)cache_resource,
2650                 rte_atomic32_read(&cache_resource->refcnt));
2651         return 0;
2652 }
2653
2654 /**
2655  * Find existing push vlan resource or create and register a new one.
2656  *
2657  * @param [in, out] dev
2658  *   Pointer to rte_eth_dev structure.
2659  * @param[in, out] resource
2660  *   Pointer to port ID action resource.
2661  * @parm[in, out] dev_flow
2662  *   Pointer to the dev_flow.
2663  * @param[out] error
2664  *   pointer to error structure.
2665  *
2666  * @return
2667  *   0 on success otherwise -errno and errno is set.
2668  */
2669 static int
2670 flow_dv_push_vlan_action_resource_register
2671                        (struct rte_eth_dev *dev,
2672                         struct mlx5_flow_dv_push_vlan_action_resource *resource,
2673                         struct mlx5_flow *dev_flow,
2674                         struct rte_flow_error *error)
2675 {
2676         struct mlx5_priv *priv = dev->data->dev_private;
2677         struct mlx5_ibv_shared *sh = priv->sh;
2678         struct mlx5_flow_dv_push_vlan_action_resource *cache_resource;
2679         struct mlx5dv_dr_domain *domain;
2680         uint32_t idx = 0;
2681
2682         /* Lookup a matching resource from cache. */
2683         ILIST_FOREACH(sh->ipool[MLX5_IPOOL_PUSH_VLAN],
2684                       sh->push_vlan_action_list, idx, cache_resource, next) {
2685                 if (resource->vlan_tag == cache_resource->vlan_tag &&
2686                     resource->ft_type == cache_resource->ft_type) {
2687                         DRV_LOG(DEBUG, "push-VLAN action resource resource %p: "
2688                                 "refcnt %d++",
2689                                 (void *)cache_resource,
2690                                 rte_atomic32_read(&cache_resource->refcnt));
2691                         rte_atomic32_inc(&cache_resource->refcnt);
2692                         dev_flow->handle->dvh.push_vlan_res = idx;
2693                         dev_flow->dv.push_vlan_res = cache_resource;
2694                         return 0;
2695                 }
2696         }
2697         /* Register new push_vlan action resource. */
2698         cache_resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_PUSH_VLAN],
2699                                        &dev_flow->handle->dvh.push_vlan_res);
2700         if (!cache_resource)
2701                 return rte_flow_error_set(error, ENOMEM,
2702                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2703                                           "cannot allocate resource memory");
2704         *cache_resource = *resource;
2705         if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
2706                 domain = sh->fdb_domain;
2707         else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_RX)
2708                 domain = sh->rx_domain;
2709         else
2710                 domain = sh->tx_domain;
2711         cache_resource->action =
2712                 mlx5_glue->dr_create_flow_action_push_vlan(domain,
2713                                                            resource->vlan_tag);
2714         if (!cache_resource->action) {
2715                 rte_free(cache_resource);
2716                 return rte_flow_error_set(error, ENOMEM,
2717                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2718                                           NULL, "cannot create action");
2719         }
2720         rte_atomic32_init(&cache_resource->refcnt);
2721         rte_atomic32_inc(&cache_resource->refcnt);
2722         ILIST_INSERT(sh->ipool[MLX5_IPOOL_PUSH_VLAN],
2723                      &sh->push_vlan_action_list,
2724                      dev_flow->handle->dvh.push_vlan_res,
2725                      cache_resource, next);
2726         dev_flow->dv.push_vlan_res = cache_resource;
2727         DRV_LOG(DEBUG, "new push vlan action resource %p: refcnt %d++",
2728                 (void *)cache_resource,
2729                 rte_atomic32_read(&cache_resource->refcnt));
2730         return 0;
2731 }
2732 /**
2733  * Get the size of specific rte_flow_item_type
2734  *
2735  * @param[in] item_type
2736  *   Tested rte_flow_item_type.
2737  *
2738  * @return
2739  *   sizeof struct item_type, 0 if void or irrelevant.
2740  */
2741 static size_t
2742 flow_dv_get_item_len(const enum rte_flow_item_type item_type)
2743 {
2744         size_t retval;
2745
2746         switch (item_type) {
2747         case RTE_FLOW_ITEM_TYPE_ETH:
2748                 retval = sizeof(struct rte_flow_item_eth);
2749                 break;
2750         case RTE_FLOW_ITEM_TYPE_VLAN:
2751                 retval = sizeof(struct rte_flow_item_vlan);
2752                 break;
2753         case RTE_FLOW_ITEM_TYPE_IPV4:
2754                 retval = sizeof(struct rte_flow_item_ipv4);
2755                 break;
2756         case RTE_FLOW_ITEM_TYPE_IPV6:
2757                 retval = sizeof(struct rte_flow_item_ipv6);
2758                 break;
2759         case RTE_FLOW_ITEM_TYPE_UDP:
2760                 retval = sizeof(struct rte_flow_item_udp);
2761                 break;
2762         case RTE_FLOW_ITEM_TYPE_TCP:
2763                 retval = sizeof(struct rte_flow_item_tcp);
2764                 break;
2765         case RTE_FLOW_ITEM_TYPE_VXLAN:
2766                 retval = sizeof(struct rte_flow_item_vxlan);
2767                 break;
2768         case RTE_FLOW_ITEM_TYPE_GRE:
2769                 retval = sizeof(struct rte_flow_item_gre);
2770                 break;
2771         case RTE_FLOW_ITEM_TYPE_NVGRE:
2772                 retval = sizeof(struct rte_flow_item_nvgre);
2773                 break;
2774         case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
2775                 retval = sizeof(struct rte_flow_item_vxlan_gpe);
2776                 break;
2777         case RTE_FLOW_ITEM_TYPE_MPLS:
2778                 retval = sizeof(struct rte_flow_item_mpls);
2779                 break;
2780         case RTE_FLOW_ITEM_TYPE_VOID: /* Fall through. */
2781         default:
2782                 retval = 0;
2783                 break;
2784         }
2785         return retval;
2786 }
2787
2788 #define MLX5_ENCAP_IPV4_VERSION         0x40
2789 #define MLX5_ENCAP_IPV4_IHL_MIN         0x05
2790 #define MLX5_ENCAP_IPV4_TTL_DEF         0x40
2791 #define MLX5_ENCAP_IPV6_VTC_FLOW        0x60000000
2792 #define MLX5_ENCAP_IPV6_HOP_LIMIT       0xff
2793 #define MLX5_ENCAP_VXLAN_FLAGS          0x08000000
2794 #define MLX5_ENCAP_VXLAN_GPE_FLAGS      0x04
2795
2796 /**
2797  * Convert the encap action data from list of rte_flow_item to raw buffer
2798  *
2799  * @param[in] items
2800  *   Pointer to rte_flow_item objects list.
2801  * @param[out] buf
2802  *   Pointer to the output buffer.
2803  * @param[out] size
2804  *   Pointer to the output buffer size.
2805  * @param[out] error
2806  *   Pointer to the error structure.
2807  *
2808  * @return
2809  *   0 on success, a negative errno value otherwise and rte_errno is set.
2810  */
2811 static int
2812 flow_dv_convert_encap_data(const struct rte_flow_item *items, uint8_t *buf,
2813                            size_t *size, struct rte_flow_error *error)
2814 {
2815         struct rte_ether_hdr *eth = NULL;
2816         struct rte_vlan_hdr *vlan = NULL;
2817         struct rte_ipv4_hdr *ipv4 = NULL;
2818         struct rte_ipv6_hdr *ipv6 = NULL;
2819         struct rte_udp_hdr *udp = NULL;
2820         struct rte_vxlan_hdr *vxlan = NULL;
2821         struct rte_vxlan_gpe_hdr *vxlan_gpe = NULL;
2822         struct rte_gre_hdr *gre = NULL;
2823         size_t len;
2824         size_t temp_size = 0;
2825
2826         if (!items)
2827                 return rte_flow_error_set(error, EINVAL,
2828                                           RTE_FLOW_ERROR_TYPE_ACTION,
2829                                           NULL, "invalid empty data");
2830         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
2831                 len = flow_dv_get_item_len(items->type);
2832                 if (len + temp_size > MLX5_ENCAP_MAX_LEN)
2833                         return rte_flow_error_set(error, EINVAL,
2834                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2835                                                   (void *)items->type,
2836                                                   "items total size is too big"
2837                                                   " for encap action");
2838                 rte_memcpy((void *)&buf[temp_size], items->spec, len);
2839                 switch (items->type) {
2840                 case RTE_FLOW_ITEM_TYPE_ETH:
2841                         eth = (struct rte_ether_hdr *)&buf[temp_size];
2842                         break;
2843                 case RTE_FLOW_ITEM_TYPE_VLAN:
2844                         vlan = (struct rte_vlan_hdr *)&buf[temp_size];
2845                         if (!eth)
2846                                 return rte_flow_error_set(error, EINVAL,
2847                                                 RTE_FLOW_ERROR_TYPE_ACTION,
2848                                                 (void *)items->type,
2849                                                 "eth header not found");
2850                         if (!eth->ether_type)
2851                                 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_VLAN);
2852                         break;
2853                 case RTE_FLOW_ITEM_TYPE_IPV4:
2854                         ipv4 = (struct rte_ipv4_hdr *)&buf[temp_size];
2855                         if (!vlan && !eth)
2856                                 return rte_flow_error_set(error, EINVAL,
2857                                                 RTE_FLOW_ERROR_TYPE_ACTION,
2858                                                 (void *)items->type,
2859                                                 "neither eth nor vlan"
2860                                                 " header found");
2861                         if (vlan && !vlan->eth_proto)
2862                                 vlan->eth_proto = RTE_BE16(RTE_ETHER_TYPE_IPV4);
2863                         else if (eth && !eth->ether_type)
2864                                 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_IPV4);
2865                         if (!ipv4->version_ihl)
2866                                 ipv4->version_ihl = MLX5_ENCAP_IPV4_VERSION |
2867                                                     MLX5_ENCAP_IPV4_IHL_MIN;
2868                         if (!ipv4->time_to_live)
2869                                 ipv4->time_to_live = MLX5_ENCAP_IPV4_TTL_DEF;
2870                         break;
2871                 case RTE_FLOW_ITEM_TYPE_IPV6:
2872                         ipv6 = (struct rte_ipv6_hdr *)&buf[temp_size];
2873                         if (!vlan && !eth)
2874                                 return rte_flow_error_set(error, EINVAL,
2875                                                 RTE_FLOW_ERROR_TYPE_ACTION,
2876                                                 (void *)items->type,
2877                                                 "neither eth nor vlan"
2878                                                 " header found");
2879                         if (vlan && !vlan->eth_proto)
2880                                 vlan->eth_proto = RTE_BE16(RTE_ETHER_TYPE_IPV6);
2881                         else if (eth && !eth->ether_type)
2882                                 eth->ether_type = RTE_BE16(RTE_ETHER_TYPE_IPV6);
2883                         if (!ipv6->vtc_flow)
2884                                 ipv6->vtc_flow =
2885                                         RTE_BE32(MLX5_ENCAP_IPV6_VTC_FLOW);
2886                         if (!ipv6->hop_limits)
2887                                 ipv6->hop_limits = MLX5_ENCAP_IPV6_HOP_LIMIT;
2888                         break;
2889                 case RTE_FLOW_ITEM_TYPE_UDP:
2890                         udp = (struct rte_udp_hdr *)&buf[temp_size];
2891                         if (!ipv4 && !ipv6)
2892                                 return rte_flow_error_set(error, EINVAL,
2893                                                 RTE_FLOW_ERROR_TYPE_ACTION,
2894                                                 (void *)items->type,
2895                                                 "ip header not found");
2896                         if (ipv4 && !ipv4->next_proto_id)
2897                                 ipv4->next_proto_id = IPPROTO_UDP;
2898                         else if (ipv6 && !ipv6->proto)
2899                                 ipv6->proto = IPPROTO_UDP;
2900                         break;
2901                 case RTE_FLOW_ITEM_TYPE_VXLAN:
2902                         vxlan = (struct rte_vxlan_hdr *)&buf[temp_size];
2903                         if (!udp)
2904                                 return rte_flow_error_set(error, EINVAL,
2905                                                 RTE_FLOW_ERROR_TYPE_ACTION,
2906                                                 (void *)items->type,
2907                                                 "udp header not found");
2908                         if (!udp->dst_port)
2909                                 udp->dst_port = RTE_BE16(MLX5_UDP_PORT_VXLAN);
2910                         if (!vxlan->vx_flags)
2911                                 vxlan->vx_flags =
2912                                         RTE_BE32(MLX5_ENCAP_VXLAN_FLAGS);
2913                         break;
2914                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
2915                         vxlan_gpe = (struct rte_vxlan_gpe_hdr *)&buf[temp_size];
2916                         if (!udp)
2917                                 return rte_flow_error_set(error, EINVAL,
2918                                                 RTE_FLOW_ERROR_TYPE_ACTION,
2919                                                 (void *)items->type,
2920                                                 "udp header not found");
2921                         if (!vxlan_gpe->proto)
2922                                 return rte_flow_error_set(error, EINVAL,
2923                                                 RTE_FLOW_ERROR_TYPE_ACTION,
2924                                                 (void *)items->type,
2925                                                 "next protocol not found");
2926                         if (!udp->dst_port)
2927                                 udp->dst_port =
2928                                         RTE_BE16(MLX5_UDP_PORT_VXLAN_GPE);
2929                         if (!vxlan_gpe->vx_flags)
2930                                 vxlan_gpe->vx_flags =
2931                                                 MLX5_ENCAP_VXLAN_GPE_FLAGS;
2932                         break;
2933                 case RTE_FLOW_ITEM_TYPE_GRE:
2934                 case RTE_FLOW_ITEM_TYPE_NVGRE:
2935                         gre = (struct rte_gre_hdr *)&buf[temp_size];
2936                         if (!gre->proto)
2937                                 return rte_flow_error_set(error, EINVAL,
2938                                                 RTE_FLOW_ERROR_TYPE_ACTION,
2939                                                 (void *)items->type,
2940                                                 "next protocol not found");
2941                         if (!ipv4 && !ipv6)
2942                                 return rte_flow_error_set(error, EINVAL,
2943                                                 RTE_FLOW_ERROR_TYPE_ACTION,
2944                                                 (void *)items->type,
2945                                                 "ip header not found");
2946                         if (ipv4 && !ipv4->next_proto_id)
2947                                 ipv4->next_proto_id = IPPROTO_GRE;
2948                         else if (ipv6 && !ipv6->proto)
2949                                 ipv6->proto = IPPROTO_GRE;
2950                         break;
2951                 case RTE_FLOW_ITEM_TYPE_VOID:
2952                         break;
2953                 default:
2954                         return rte_flow_error_set(error, EINVAL,
2955                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2956                                                   (void *)items->type,
2957                                                   "unsupported item type");
2958                         break;
2959                 }
2960                 temp_size += len;
2961         }
2962         *size = temp_size;
2963         return 0;
2964 }
2965
2966 static int
2967 flow_dv_zero_encap_udp_csum(void *data, struct rte_flow_error *error)
2968 {
2969         struct rte_ether_hdr *eth = NULL;
2970         struct rte_vlan_hdr *vlan = NULL;
2971         struct rte_ipv6_hdr *ipv6 = NULL;
2972         struct rte_udp_hdr *udp = NULL;
2973         char *next_hdr;
2974         uint16_t proto;
2975
2976         eth = (struct rte_ether_hdr *)data;
2977         next_hdr = (char *)(eth + 1);
2978         proto = RTE_BE16(eth->ether_type);
2979
2980         /* VLAN skipping */
2981         while (proto == RTE_ETHER_TYPE_VLAN || proto == RTE_ETHER_TYPE_QINQ) {
2982                 vlan = (struct rte_vlan_hdr *)next_hdr;
2983                 proto = RTE_BE16(vlan->eth_proto);
2984                 next_hdr += sizeof(struct rte_vlan_hdr);
2985         }
2986
2987         /* HW calculates IPv4 csum. no need to proceed */
2988         if (proto == RTE_ETHER_TYPE_IPV4)
2989                 return 0;
2990
2991         /* non IPv4/IPv6 header. not supported */
2992         if (proto != RTE_ETHER_TYPE_IPV6) {
2993                 return rte_flow_error_set(error, ENOTSUP,
2994                                           RTE_FLOW_ERROR_TYPE_ACTION,
2995                                           NULL, "Cannot offload non IPv4/IPv6");
2996         }
2997
2998         ipv6 = (struct rte_ipv6_hdr *)next_hdr;
2999
3000         /* ignore non UDP */
3001         if (ipv6->proto != IPPROTO_UDP)
3002                 return 0;
3003
3004         udp = (struct rte_udp_hdr *)(ipv6 + 1);
3005         udp->dgram_cksum = 0;
3006
3007         return 0;
3008 }
3009
3010 /**
3011  * Convert L2 encap action to DV specification.
3012  *
3013  * @param[in] dev
3014  *   Pointer to rte_eth_dev structure.
3015  * @param[in] action
3016  *   Pointer to action structure.
3017  * @param[in, out] dev_flow
3018  *   Pointer to the mlx5_flow.
3019  * @param[in] transfer
3020  *   Mark if the flow is E-Switch flow.
3021  * @param[out] error
3022  *   Pointer to the error structure.
3023  *
3024  * @return
3025  *   0 on success, a negative errno value otherwise and rte_errno is set.
3026  */
3027 static int
3028 flow_dv_create_action_l2_encap(struct rte_eth_dev *dev,
3029                                const struct rte_flow_action *action,
3030                                struct mlx5_flow *dev_flow,
3031                                uint8_t transfer,
3032                                struct rte_flow_error *error)
3033 {
3034         const struct rte_flow_item *encap_data;
3035         const struct rte_flow_action_raw_encap *raw_encap_data;
3036         struct mlx5_flow_dv_encap_decap_resource res = {
3037                 .reformat_type =
3038                         MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L2_TUNNEL,
3039                 .ft_type = transfer ? MLX5DV_FLOW_TABLE_TYPE_FDB :
3040                                       MLX5DV_FLOW_TABLE_TYPE_NIC_TX,
3041         };
3042
3043         if (action->type == RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
3044                 raw_encap_data =
3045                         (const struct rte_flow_action_raw_encap *)action->conf;
3046                 res.size = raw_encap_data->size;
3047                 memcpy(res.buf, raw_encap_data->data, res.size);
3048         } else {
3049                 if (action->type == RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP)
3050                         encap_data =
3051                                 ((const struct rte_flow_action_vxlan_encap *)
3052                                                 action->conf)->definition;
3053                 else
3054                         encap_data =
3055                                 ((const struct rte_flow_action_nvgre_encap *)
3056                                                 action->conf)->definition;
3057                 if (flow_dv_convert_encap_data(encap_data, res.buf,
3058                                                &res.size, error))
3059                         return -rte_errno;
3060         }
3061         if (flow_dv_zero_encap_udp_csum(res.buf, error))
3062                 return -rte_errno;
3063         if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
3064                 return rte_flow_error_set(error, EINVAL,
3065                                           RTE_FLOW_ERROR_TYPE_ACTION,
3066                                           NULL, "can't create L2 encap action");
3067         return 0;
3068 }
3069
3070 /**
3071  * Convert L2 decap action to DV specification.
3072  *
3073  * @param[in] dev
3074  *   Pointer to rte_eth_dev structure.
3075  * @param[in, out] dev_flow
3076  *   Pointer to the mlx5_flow.
3077  * @param[in] transfer
3078  *   Mark if the flow is E-Switch flow.
3079  * @param[out] error
3080  *   Pointer to the error structure.
3081  *
3082  * @return
3083  *   0 on success, a negative errno value otherwise and rte_errno is set.
3084  */
3085 static int
3086 flow_dv_create_action_l2_decap(struct rte_eth_dev *dev,
3087                                struct mlx5_flow *dev_flow,
3088                                uint8_t transfer,
3089                                struct rte_flow_error *error)
3090 {
3091         struct mlx5_flow_dv_encap_decap_resource res = {
3092                 .size = 0,
3093                 .reformat_type =
3094                         MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TUNNEL_TO_L2,
3095                 .ft_type = transfer ? MLX5DV_FLOW_TABLE_TYPE_FDB :
3096                                       MLX5DV_FLOW_TABLE_TYPE_NIC_RX,
3097         };
3098
3099         if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
3100                 return rte_flow_error_set(error, EINVAL,
3101                                           RTE_FLOW_ERROR_TYPE_ACTION,
3102                                           NULL, "can't create L2 decap action");
3103         return 0;
3104 }
3105
3106 /**
3107  * Convert raw decap/encap (L3 tunnel) action to DV specification.
3108  *
3109  * @param[in] dev
3110  *   Pointer to rte_eth_dev structure.
3111  * @param[in] action
3112  *   Pointer to action structure.
3113  * @param[in, out] dev_flow
3114  *   Pointer to the mlx5_flow.
3115  * @param[in] attr
3116  *   Pointer to the flow attributes.
3117  * @param[out] error
3118  *   Pointer to the error structure.
3119  *
3120  * @return
3121  *   0 on success, a negative errno value otherwise and rte_errno is set.
3122  */
3123 static int
3124 flow_dv_create_action_raw_encap(struct rte_eth_dev *dev,
3125                                 const struct rte_flow_action *action,
3126                                 struct mlx5_flow *dev_flow,
3127                                 const struct rte_flow_attr *attr,
3128                                 struct rte_flow_error *error)
3129 {
3130         const struct rte_flow_action_raw_encap *encap_data;
3131         struct mlx5_flow_dv_encap_decap_resource res;
3132
3133         memset(&res, 0, sizeof(res));
3134         encap_data = (const struct rte_flow_action_raw_encap *)action->conf;
3135         res.size = encap_data->size;
3136         memcpy(res.buf, encap_data->data, res.size);
3137         res.reformat_type = res.size < MLX5_ENCAPSULATION_DECISION_SIZE ?
3138                 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L3_TUNNEL_TO_L2 :
3139                 MLX5DV_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L3_TUNNEL;
3140         if (attr->transfer)
3141                 res.ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
3142         else
3143                 res.ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
3144                                              MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
3145         if (flow_dv_encap_decap_resource_register(dev, &res, dev_flow, error))
3146                 return rte_flow_error_set(error, EINVAL,
3147                                           RTE_FLOW_ERROR_TYPE_ACTION,
3148                                           NULL, "can't create encap action");
3149         return 0;
3150 }
3151
3152 /**
3153  * Create action push VLAN.
3154  *
3155  * @param[in] dev
3156  *   Pointer to rte_eth_dev structure.
3157  * @param[in] attr
3158  *   Pointer to the flow attributes.
3159  * @param[in] vlan
3160  *   Pointer to the vlan to push to the Ethernet header.
3161  * @param[in, out] dev_flow
3162  *   Pointer to the mlx5_flow.
3163  * @param[out] error
3164  *   Pointer to the error structure.
3165  *
3166  * @return
3167  *   0 on success, a negative errno value otherwise and rte_errno is set.
3168  */
3169 static int
3170 flow_dv_create_action_push_vlan(struct rte_eth_dev *dev,
3171                                 const struct rte_flow_attr *attr,
3172                                 const struct rte_vlan_hdr *vlan,
3173                                 struct mlx5_flow *dev_flow,
3174                                 struct rte_flow_error *error)
3175 {
3176         struct mlx5_flow_dv_push_vlan_action_resource res;
3177
3178         memset(&res, 0, sizeof(res));
3179         res.vlan_tag =
3180                 rte_cpu_to_be_32(((uint32_t)vlan->eth_proto) << 16 |
3181                                  vlan->vlan_tci);
3182         if (attr->transfer)
3183                 res.ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
3184         else
3185                 res.ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
3186                                              MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
3187         return flow_dv_push_vlan_action_resource_register
3188                                             (dev, &res, dev_flow, error);
3189 }
3190
3191 /**
3192  * Validate the modify-header actions.
3193  *
3194  * @param[in] action_flags
3195  *   Holds the actions detected until now.
3196  * @param[in] action
3197  *   Pointer to the modify action.
3198  * @param[out] error
3199  *   Pointer to error structure.
3200  *
3201  * @return
3202  *   0 on success, a negative errno value otherwise and rte_errno is set.
3203  */
3204 static int
3205 flow_dv_validate_action_modify_hdr(const uint64_t action_flags,
3206                                    const struct rte_flow_action *action,
3207                                    struct rte_flow_error *error)
3208 {
3209         if (action->type != RTE_FLOW_ACTION_TYPE_DEC_TTL && !action->conf)
3210                 return rte_flow_error_set(error, EINVAL,
3211                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
3212                                           NULL, "action configuration not set");
3213         if (action_flags & MLX5_FLOW_ACTION_ENCAP)
3214                 return rte_flow_error_set(error, EINVAL,
3215                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3216                                           "can't have encap action before"
3217                                           " modify action");
3218         return 0;
3219 }
3220
3221 /**
3222  * Validate the modify-header MAC address actions.
3223  *
3224  * @param[in] action_flags
3225  *   Holds the actions detected until now.
3226  * @param[in] action
3227  *   Pointer to the modify action.
3228  * @param[in] item_flags
3229  *   Holds the items detected.
3230  * @param[out] error
3231  *   Pointer to error structure.
3232  *
3233  * @return
3234  *   0 on success, a negative errno value otherwise and rte_errno is set.
3235  */
3236 static int
3237 flow_dv_validate_action_modify_mac(const uint64_t action_flags,
3238                                    const struct rte_flow_action *action,
3239                                    const uint64_t item_flags,
3240                                    struct rte_flow_error *error)
3241 {
3242         int ret = 0;
3243
3244         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3245         if (!ret) {
3246                 if (!(item_flags & MLX5_FLOW_LAYER_L2))
3247                         return rte_flow_error_set(error, EINVAL,
3248                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3249                                                   NULL,
3250                                                   "no L2 item in pattern");
3251         }
3252         return ret;
3253 }
3254
3255 /**
3256  * Validate the modify-header IPv4 address actions.
3257  *
3258  * @param[in] action_flags
3259  *   Holds the actions detected until now.
3260  * @param[in] action
3261  *   Pointer to the modify action.
3262  * @param[in] item_flags
3263  *   Holds the items detected.
3264  * @param[out] error
3265  *   Pointer to error structure.
3266  *
3267  * @return
3268  *   0 on success, a negative errno value otherwise and rte_errno is set.
3269  */
3270 static int
3271 flow_dv_validate_action_modify_ipv4(const uint64_t action_flags,
3272                                     const struct rte_flow_action *action,
3273                                     const uint64_t item_flags,
3274                                     struct rte_flow_error *error)
3275 {
3276         int ret = 0;
3277         uint64_t layer;
3278
3279         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3280         if (!ret) {
3281                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3282                                  MLX5_FLOW_LAYER_INNER_L3_IPV4 :
3283                                  MLX5_FLOW_LAYER_OUTER_L3_IPV4;
3284                 if (!(item_flags & layer))
3285                         return rte_flow_error_set(error, EINVAL,
3286                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3287                                                   NULL,
3288                                                   "no ipv4 item in pattern");
3289         }
3290         return ret;
3291 }
3292
3293 /**
3294  * Validate the modify-header IPv6 address actions.
3295  *
3296  * @param[in] action_flags
3297  *   Holds the actions detected until now.
3298  * @param[in] action
3299  *   Pointer to the modify action.
3300  * @param[in] item_flags
3301  *   Holds the items detected.
3302  * @param[out] error
3303  *   Pointer to error structure.
3304  *
3305  * @return
3306  *   0 on success, a negative errno value otherwise and rte_errno is set.
3307  */
3308 static int
3309 flow_dv_validate_action_modify_ipv6(const uint64_t action_flags,
3310                                     const struct rte_flow_action *action,
3311                                     const uint64_t item_flags,
3312                                     struct rte_flow_error *error)
3313 {
3314         int ret = 0;
3315         uint64_t layer;
3316
3317         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3318         if (!ret) {
3319                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3320                                  MLX5_FLOW_LAYER_INNER_L3_IPV6 :
3321                                  MLX5_FLOW_LAYER_OUTER_L3_IPV6;
3322                 if (!(item_flags & layer))
3323                         return rte_flow_error_set(error, EINVAL,
3324                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3325                                                   NULL,
3326                                                   "no ipv6 item in pattern");
3327         }
3328         return ret;
3329 }
3330
3331 /**
3332  * Validate the modify-header TP actions.
3333  *
3334  * @param[in] action_flags
3335  *   Holds the actions detected until now.
3336  * @param[in] action
3337  *   Pointer to the modify action.
3338  * @param[in] item_flags
3339  *   Holds the items detected.
3340  * @param[out] error
3341  *   Pointer to error structure.
3342  *
3343  * @return
3344  *   0 on success, a negative errno value otherwise and rte_errno is set.
3345  */
3346 static int
3347 flow_dv_validate_action_modify_tp(const uint64_t action_flags,
3348                                   const struct rte_flow_action *action,
3349                                   const uint64_t item_flags,
3350                                   struct rte_flow_error *error)
3351 {
3352         int ret = 0;
3353         uint64_t layer;
3354
3355         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3356         if (!ret) {
3357                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3358                                  MLX5_FLOW_LAYER_INNER_L4 :
3359                                  MLX5_FLOW_LAYER_OUTER_L4;
3360                 if (!(item_flags & layer))
3361                         return rte_flow_error_set(error, EINVAL,
3362                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3363                                                   NULL, "no transport layer "
3364                                                   "in pattern");
3365         }
3366         return ret;
3367 }
3368
3369 /**
3370  * Validate the modify-header actions of increment/decrement
3371  * TCP Sequence-number.
3372  *
3373  * @param[in] action_flags
3374  *   Holds the actions detected until now.
3375  * @param[in] action
3376  *   Pointer to the modify action.
3377  * @param[in] item_flags
3378  *   Holds the items detected.
3379  * @param[out] error
3380  *   Pointer to error structure.
3381  *
3382  * @return
3383  *   0 on success, a negative errno value otherwise and rte_errno is set.
3384  */
3385 static int
3386 flow_dv_validate_action_modify_tcp_seq(const uint64_t action_flags,
3387                                        const struct rte_flow_action *action,
3388                                        const uint64_t item_flags,
3389                                        struct rte_flow_error *error)
3390 {
3391         int ret = 0;
3392         uint64_t layer;
3393
3394         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3395         if (!ret) {
3396                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3397                                  MLX5_FLOW_LAYER_INNER_L4_TCP :
3398                                  MLX5_FLOW_LAYER_OUTER_L4_TCP;
3399                 if (!(item_flags & layer))
3400                         return rte_flow_error_set(error, EINVAL,
3401                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3402                                                   NULL, "no TCP item in"
3403                                                   " pattern");
3404                 if ((action->type == RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ &&
3405                         (action_flags & MLX5_FLOW_ACTION_DEC_TCP_SEQ)) ||
3406                     (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ &&
3407                         (action_flags & MLX5_FLOW_ACTION_INC_TCP_SEQ)))
3408                         return rte_flow_error_set(error, EINVAL,
3409                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3410                                                   NULL,
3411                                                   "cannot decrease and increase"
3412                                                   " TCP sequence number"
3413                                                   " at the same time");
3414         }
3415         return ret;
3416 }
3417
3418 /**
3419  * Validate the modify-header actions of increment/decrement
3420  * TCP Acknowledgment number.
3421  *
3422  * @param[in] action_flags
3423  *   Holds the actions detected until now.
3424  * @param[in] action
3425  *   Pointer to the modify action.
3426  * @param[in] item_flags
3427  *   Holds the items detected.
3428  * @param[out] error
3429  *   Pointer to error structure.
3430  *
3431  * @return
3432  *   0 on success, a negative errno value otherwise and rte_errno is set.
3433  */
3434 static int
3435 flow_dv_validate_action_modify_tcp_ack(const uint64_t action_flags,
3436                                        const struct rte_flow_action *action,
3437                                        const uint64_t item_flags,
3438                                        struct rte_flow_error *error)
3439 {
3440         int ret = 0;
3441         uint64_t layer;
3442
3443         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3444         if (!ret) {
3445                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3446                                  MLX5_FLOW_LAYER_INNER_L4_TCP :
3447                                  MLX5_FLOW_LAYER_OUTER_L4_TCP;
3448                 if (!(item_flags & layer))
3449                         return rte_flow_error_set(error, EINVAL,
3450                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3451                                                   NULL, "no TCP item in"
3452                                                   " pattern");
3453                 if ((action->type == RTE_FLOW_ACTION_TYPE_INC_TCP_ACK &&
3454                         (action_flags & MLX5_FLOW_ACTION_DEC_TCP_ACK)) ||
3455                     (action->type == RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK &&
3456                         (action_flags & MLX5_FLOW_ACTION_INC_TCP_ACK)))
3457                         return rte_flow_error_set(error, EINVAL,
3458                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3459                                                   NULL,
3460                                                   "cannot decrease and increase"
3461                                                   " TCP acknowledgment number"
3462                                                   " at the same time");
3463         }
3464         return ret;
3465 }
3466
3467 /**
3468  * Validate the modify-header TTL actions.
3469  *
3470  * @param[in] action_flags
3471  *   Holds the actions detected until now.
3472  * @param[in] action
3473  *   Pointer to the modify action.
3474  * @param[in] item_flags
3475  *   Holds the items detected.
3476  * @param[out] error
3477  *   Pointer to error structure.
3478  *
3479  * @return
3480  *   0 on success, a negative errno value otherwise and rte_errno is set.
3481  */
3482 static int
3483 flow_dv_validate_action_modify_ttl(const uint64_t action_flags,
3484                                    const struct rte_flow_action *action,
3485                                    const uint64_t item_flags,
3486                                    struct rte_flow_error *error)
3487 {
3488         int ret = 0;
3489         uint64_t layer;
3490
3491         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3492         if (!ret) {
3493                 layer = (action_flags & MLX5_FLOW_ACTION_DECAP) ?
3494                                  MLX5_FLOW_LAYER_INNER_L3 :
3495                                  MLX5_FLOW_LAYER_OUTER_L3;
3496                 if (!(item_flags & layer))
3497                         return rte_flow_error_set(error, EINVAL,
3498                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3499                                                   NULL,
3500                                                   "no IP protocol in pattern");
3501         }
3502         return ret;
3503 }
3504
3505 /**
3506  * Validate jump action.
3507  *
3508  * @param[in] action
3509  *   Pointer to the jump action.
3510  * @param[in] action_flags
3511  *   Holds the actions detected until now.
3512  * @param[in] attributes
3513  *   Pointer to flow attributes
3514  * @param[in] external
3515  *   Action belongs to flow rule created by request external to PMD.
3516  * @param[out] error
3517  *   Pointer to error structure.
3518  *
3519  * @return
3520  *   0 on success, a negative errno value otherwise and rte_errno is set.
3521  */
3522 static int
3523 flow_dv_validate_action_jump(const struct rte_flow_action *action,
3524                              uint64_t action_flags,
3525                              const struct rte_flow_attr *attributes,
3526                              bool external, struct rte_flow_error *error)
3527 {
3528         uint32_t target_group, table;
3529         int ret = 0;
3530
3531         if (action_flags & (MLX5_FLOW_FATE_ACTIONS |
3532                             MLX5_FLOW_FATE_ESWITCH_ACTIONS))
3533                 return rte_flow_error_set(error, EINVAL,
3534                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3535                                           "can't have 2 fate actions in"
3536                                           " same flow");
3537         if (action_flags & MLX5_FLOW_ACTION_METER)
3538                 return rte_flow_error_set(error, ENOTSUP,
3539                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3540                                           "jump with meter not support");
3541         if (!action->conf)
3542                 return rte_flow_error_set(error, EINVAL,
3543                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
3544                                           NULL, "action configuration not set");
3545         target_group =
3546                 ((const struct rte_flow_action_jump *)action->conf)->group;
3547         ret = mlx5_flow_group_to_table(attributes, external, target_group,
3548                                        true, &table, error);
3549         if (ret)
3550                 return ret;
3551         if (attributes->group == target_group)
3552                 return rte_flow_error_set(error, EINVAL,
3553                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3554                                           "target group must be other than"
3555                                           " the current flow group");
3556         return 0;
3557 }
3558
3559 /*
3560  * Validate the port_id action.
3561  *
3562  * @param[in] dev
3563  *   Pointer to rte_eth_dev structure.
3564  * @param[in] action_flags
3565  *   Bit-fields that holds the actions detected until now.
3566  * @param[in] action
3567  *   Port_id RTE action structure.
3568  * @param[in] attr
3569  *   Attributes of flow that includes this action.
3570  * @param[out] error
3571  *   Pointer to error structure.
3572  *
3573  * @return
3574  *   0 on success, a negative errno value otherwise and rte_errno is set.
3575  */
3576 static int
3577 flow_dv_validate_action_port_id(struct rte_eth_dev *dev,
3578                                 uint64_t action_flags,
3579                                 const struct rte_flow_action *action,
3580                                 const struct rte_flow_attr *attr,
3581                                 struct rte_flow_error *error)
3582 {
3583         const struct rte_flow_action_port_id *port_id;
3584         struct mlx5_priv *act_priv;
3585         struct mlx5_priv *dev_priv;
3586         uint16_t port;
3587
3588         if (!attr->transfer)
3589                 return rte_flow_error_set(error, ENOTSUP,
3590                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3591                                           NULL,
3592                                           "port id action is valid in transfer"
3593                                           " mode only");
3594         if (!action || !action->conf)
3595                 return rte_flow_error_set(error, ENOTSUP,
3596                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
3597                                           NULL,
3598                                           "port id action parameters must be"
3599                                           " specified");
3600         if (action_flags & (MLX5_FLOW_FATE_ACTIONS |
3601                             MLX5_FLOW_FATE_ESWITCH_ACTIONS))
3602                 return rte_flow_error_set(error, EINVAL,
3603                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3604                                           "can have only one fate actions in"
3605                                           " a flow");
3606         dev_priv = mlx5_dev_to_eswitch_info(dev);
3607         if (!dev_priv)
3608                 return rte_flow_error_set(error, rte_errno,
3609                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3610                                           NULL,
3611                                           "failed to obtain E-Switch info");
3612         port_id = action->conf;
3613         port = port_id->original ? dev->data->port_id : port_id->id;
3614         act_priv = mlx5_port_to_eswitch_info(port, false);
3615         if (!act_priv)
3616                 return rte_flow_error_set
3617                                 (error, rte_errno,
3618                                  RTE_FLOW_ERROR_TYPE_ACTION_CONF, port_id,
3619                                  "failed to obtain E-Switch port id for port");
3620         if (act_priv->domain_id != dev_priv->domain_id)
3621                 return rte_flow_error_set
3622                                 (error, EINVAL,
3623                                  RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3624                                  "port does not belong to"
3625                                  " E-Switch being configured");
3626         return 0;
3627 }
3628
3629 /**
3630  * Get the maximum number of modify header actions.
3631  *
3632  * @param dev
3633  *   Pointer to rte_eth_dev structure.
3634  * @param flags
3635  *   Flags bits to check if root level.
3636  *
3637  * @return
3638  *   Max number of modify header actions device can support.
3639  */
3640 static unsigned int
3641 flow_dv_modify_hdr_action_max(struct rte_eth_dev *dev, uint64_t flags)
3642 {
3643         /*
3644          * There's no way to directly query the max cap. Although it has to be
3645          * acquried by iterative trial, it is a safe assumption that more
3646          * actions are supported by FW if extensive metadata register is
3647          * supported. (Only in the root table)
3648          */
3649         if (!(flags & MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL))
3650                 return MLX5_MAX_MODIFY_NUM;
3651         else
3652                 return mlx5_flow_ext_mreg_supported(dev) ?
3653                                         MLX5_ROOT_TBL_MODIFY_NUM :
3654                                         MLX5_ROOT_TBL_MODIFY_NUM_NO_MREG;
3655 }
3656
3657 /**
3658  * Validate the meter action.
3659  *
3660  * @param[in] dev
3661  *   Pointer to rte_eth_dev structure.
3662  * @param[in] action_flags
3663  *   Bit-fields that holds the actions detected until now.
3664  * @param[in] action
3665  *   Pointer to the meter action.
3666  * @param[in] attr
3667  *   Attributes of flow that includes this action.
3668  * @param[out] error
3669  *   Pointer to error structure.
3670  *
3671  * @return
3672  *   0 on success, a negative errno value otherwise and rte_ernno is set.
3673  */
3674 static int
3675 mlx5_flow_validate_action_meter(struct rte_eth_dev *dev,
3676                                 uint64_t action_flags,
3677                                 const struct rte_flow_action *action,
3678                                 const struct rte_flow_attr *attr,
3679                                 struct rte_flow_error *error)
3680 {
3681         struct mlx5_priv *priv = dev->data->dev_private;
3682         const struct rte_flow_action_meter *am = action->conf;
3683         struct mlx5_flow_meter *fm;
3684
3685         if (!am)
3686                 return rte_flow_error_set(error, EINVAL,
3687                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3688                                           "meter action conf is NULL");
3689
3690         if (action_flags & MLX5_FLOW_ACTION_METER)
3691                 return rte_flow_error_set(error, ENOTSUP,
3692                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3693                                           "meter chaining not support");
3694         if (action_flags & MLX5_FLOW_ACTION_JUMP)
3695                 return rte_flow_error_set(error, ENOTSUP,
3696                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3697                                           "meter with jump not support");
3698         if (!priv->mtr_en)
3699                 return rte_flow_error_set(error, ENOTSUP,
3700                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3701                                           NULL,
3702                                           "meter action not supported");
3703         fm = mlx5_flow_meter_find(priv, am->mtr_id);
3704         if (!fm)
3705                 return rte_flow_error_set(error, EINVAL,
3706                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3707                                           "Meter not found");
3708         if (fm->ref_cnt && (!(fm->attr.transfer == attr->transfer ||
3709               (!fm->attr.ingress && !attr->ingress && attr->egress) ||
3710               (!fm->attr.egress && !attr->egress && attr->ingress))))
3711                 return rte_flow_error_set(error, EINVAL,
3712                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3713                                           "Flow attributes are either invalid "
3714                                           "or have a conflict with current "
3715                                           "meter attributes");
3716         return 0;
3717 }
3718
3719 /**
3720  * Validate the modify-header IPv4 DSCP actions.
3721  *
3722  * @param[in] action_flags
3723  *   Holds the actions detected until now.
3724  * @param[in] action
3725  *   Pointer to the modify action.
3726  * @param[in] item_flags
3727  *   Holds the items detected.
3728  * @param[out] error
3729  *   Pointer to error structure.
3730  *
3731  * @return
3732  *   0 on success, a negative errno value otherwise and rte_errno is set.
3733  */
3734 static int
3735 flow_dv_validate_action_modify_ipv4_dscp(const uint64_t action_flags,
3736                                          const struct rte_flow_action *action,
3737                                          const uint64_t item_flags,
3738                                          struct rte_flow_error *error)
3739 {
3740         int ret = 0;
3741
3742         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3743         if (!ret) {
3744                 if (!(item_flags & MLX5_FLOW_LAYER_L3_IPV4))
3745                         return rte_flow_error_set(error, EINVAL,
3746                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3747                                                   NULL,
3748                                                   "no ipv4 item in pattern");
3749         }
3750         return ret;
3751 }
3752
3753 /**
3754  * Validate the modify-header IPv6 DSCP actions.
3755  *
3756  * @param[in] action_flags
3757  *   Holds the actions detected until now.
3758  * @param[in] action
3759  *   Pointer to the modify action.
3760  * @param[in] item_flags
3761  *   Holds the items detected.
3762  * @param[out] error
3763  *   Pointer to error structure.
3764  *
3765  * @return
3766  *   0 on success, a negative errno value otherwise and rte_errno is set.
3767  */
3768 static int
3769 flow_dv_validate_action_modify_ipv6_dscp(const uint64_t action_flags,
3770                                          const struct rte_flow_action *action,
3771                                          const uint64_t item_flags,
3772                                          struct rte_flow_error *error)
3773 {
3774         int ret = 0;
3775
3776         ret = flow_dv_validate_action_modify_hdr(action_flags, action, error);
3777         if (!ret) {
3778                 if (!(item_flags & MLX5_FLOW_LAYER_L3_IPV6))
3779                         return rte_flow_error_set(error, EINVAL,
3780                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3781                                                   NULL,
3782                                                   "no ipv6 item in pattern");
3783         }
3784         return ret;
3785 }
3786
3787 /**
3788  * Find existing modify-header resource or create and register a new one.
3789  *
3790  * @param dev[in, out]
3791  *   Pointer to rte_eth_dev structure.
3792  * @param[in, out] resource
3793  *   Pointer to modify-header resource.
3794  * @parm[in, out] dev_flow
3795  *   Pointer to the dev_flow.
3796  * @param[out] error
3797  *   pointer to error structure.
3798  *
3799  * @return
3800  *   0 on success otherwise -errno and errno is set.
3801  */
3802 static int
3803 flow_dv_modify_hdr_resource_register
3804                         (struct rte_eth_dev *dev,
3805                          struct mlx5_flow_dv_modify_hdr_resource *resource,
3806                          struct mlx5_flow *dev_flow,
3807                          struct rte_flow_error *error)
3808 {
3809         struct mlx5_priv *priv = dev->data->dev_private;
3810         struct mlx5_ibv_shared *sh = priv->sh;
3811         struct mlx5_flow_dv_modify_hdr_resource *cache_resource;
3812         struct mlx5dv_dr_domain *ns;
3813         uint32_t actions_len;
3814
3815         resource->flags = dev_flow->dv.group ? 0 :
3816                           MLX5DV_DR_ACTION_FLAGS_ROOT_LEVEL;
3817         if (resource->actions_num > flow_dv_modify_hdr_action_max(dev,
3818                                     resource->flags))
3819                 return rte_flow_error_set(error, EOVERFLOW,
3820                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3821                                           "too many modify header items");
3822         if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_FDB)
3823                 ns = sh->fdb_domain;
3824         else if (resource->ft_type == MLX5DV_FLOW_TABLE_TYPE_NIC_TX)
3825                 ns = sh->tx_domain;
3826         else
3827                 ns = sh->rx_domain;
3828         /* Lookup a matching resource from cache. */
3829         actions_len = resource->actions_num * sizeof(resource->actions[0]);
3830         LIST_FOREACH(cache_resource, &sh->modify_cmds, next) {
3831                 if (resource->ft_type == cache_resource->ft_type &&
3832                     resource->actions_num == cache_resource->actions_num &&
3833                     resource->flags == cache_resource->flags &&
3834                     !memcmp((const void *)resource->actions,
3835                             (const void *)cache_resource->actions,
3836                             actions_len)) {
3837                         DRV_LOG(DEBUG, "modify-header resource %p: refcnt %d++",
3838                                 (void *)cache_resource,
3839                                 rte_atomic32_read(&cache_resource->refcnt));
3840                         rte_atomic32_inc(&cache_resource->refcnt);
3841                         dev_flow->handle->dvh.modify_hdr = cache_resource;
3842                         return 0;
3843                 }
3844         }
3845         /* Register new modify-header resource. */
3846         cache_resource = rte_calloc(__func__, 1,
3847                                     sizeof(*cache_resource) + actions_len, 0);
3848         if (!cache_resource)
3849                 return rte_flow_error_set(error, ENOMEM,
3850                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3851                                           "cannot allocate resource memory");
3852         *cache_resource = *resource;
3853         rte_memcpy(cache_resource->actions, resource->actions, actions_len);
3854         cache_resource->verbs_action =
3855                 mlx5_glue->dv_create_flow_action_modify_header
3856                                         (sh->ctx, cache_resource->ft_type, ns,
3857                                          cache_resource->flags, actions_len,
3858                                          (uint64_t *)cache_resource->actions);
3859         if (!cache_resource->verbs_action) {
3860                 rte_free(cache_resource);
3861                 return rte_flow_error_set(error, ENOMEM,
3862                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3863                                           NULL, "cannot create action");
3864         }
3865         rte_atomic32_init(&cache_resource->refcnt);
3866         rte_atomic32_inc(&cache_resource->refcnt);
3867         LIST_INSERT_HEAD(&sh->modify_cmds, cache_resource, next);
3868         dev_flow->handle->dvh.modify_hdr = cache_resource;
3869         DRV_LOG(DEBUG, "new modify-header resource %p: refcnt %d++",
3870                 (void *)cache_resource,
3871                 rte_atomic32_read(&cache_resource->refcnt));
3872         return 0;
3873 }
3874
3875 /**
3876  * Get DV flow counter by index.
3877  *
3878  * @param[in] dev
3879  *   Pointer to the Ethernet device structure.
3880  * @param[in] idx
3881  *   mlx5 flow counter index in the container.
3882  * @param[out] ppool
3883  *   mlx5 flow counter pool in the container,
3884  *
3885  * @return
3886  *   Pointer to the counter, NULL otherwise.
3887  */
3888 static struct mlx5_flow_counter *
3889 flow_dv_counter_get_by_idx(struct rte_eth_dev *dev,
3890                            uint32_t idx,
3891                            struct mlx5_flow_counter_pool **ppool)
3892 {
3893         struct mlx5_priv *priv = dev->data->dev_private;
3894         struct mlx5_pools_container *cont;
3895         struct mlx5_flow_counter_pool *pool;
3896         uint32_t batch = 0;
3897
3898         idx--;
3899         if (idx >= MLX5_CNT_BATCH_OFFSET) {
3900                 idx -= MLX5_CNT_BATCH_OFFSET;
3901                 batch = 1;
3902         }
3903         cont = MLX5_CNT_CONTAINER(priv->sh, batch, 0);
3904         MLX5_ASSERT(idx / MLX5_COUNTERS_PER_POOL < cont->n);
3905         pool = cont->pools[idx / MLX5_COUNTERS_PER_POOL];
3906         MLX5_ASSERT(pool);
3907         if (ppool)
3908                 *ppool = pool;
3909         return &pool->counters_raw[idx % MLX5_COUNTERS_PER_POOL];
3910 }
3911
3912 /**
3913  * Get a pool by devx counter ID.
3914  *
3915  * @param[in] cont
3916  *   Pointer to the counter container.
3917  * @param[in] id
3918  *   The counter devx ID.
3919  *
3920  * @return
3921  *   The counter pool pointer if exists, NULL otherwise,
3922  */
3923 static struct mlx5_flow_counter_pool *
3924 flow_dv_find_pool_by_id(struct mlx5_pools_container *cont, int id)
3925 {
3926         uint32_t i;
3927         uint32_t n_valid = rte_atomic16_read(&cont->n_valid);
3928
3929         for (i = 0; i < n_valid; i++) {
3930                 struct mlx5_flow_counter_pool *pool = cont->pools[i];
3931                 int base = (pool->min_dcs->id / MLX5_COUNTERS_PER_POOL) *
3932                            MLX5_COUNTERS_PER_POOL;
3933
3934                 if (id >= base && id < base + MLX5_COUNTERS_PER_POOL) {
3935                         /*
3936                          * Move the pool to the head, as counter allocate
3937                          * always gets the first pool in the container.
3938                          */
3939                         if (pool != TAILQ_FIRST(&cont->pool_list)) {
3940                                 TAILQ_REMOVE(&cont->pool_list, pool, next);
3941                                 TAILQ_INSERT_HEAD(&cont->pool_list, pool, next);
3942                         }
3943                         return pool;
3944                 }
3945         }
3946         return NULL;
3947 }
3948
3949 /**
3950  * Allocate a new memory for the counter values wrapped by all the needed
3951  * management.
3952  *
3953  * @param[in] dev
3954  *   Pointer to the Ethernet device structure.
3955  * @param[in] raws_n
3956  *   The raw memory areas - each one for MLX5_COUNTERS_PER_POOL counters.
3957  *
3958  * @return
3959  *   The new memory management pointer on success, otherwise NULL and rte_errno
3960  *   is set.
3961  */
3962 static struct mlx5_counter_stats_mem_mng *
3963 flow_dv_create_counter_stat_mem_mng(struct rte_eth_dev *dev, int raws_n)
3964 {
3965         struct mlx5_ibv_shared *sh = ((struct mlx5_priv *)
3966                                         (dev->data->dev_private))->sh;
3967         struct mlx5_devx_mkey_attr mkey_attr;
3968         struct mlx5_counter_stats_mem_mng *mem_mng;
3969         volatile struct flow_counter_stats *raw_data;
3970         int size = (sizeof(struct flow_counter_stats) *
3971                         MLX5_COUNTERS_PER_POOL +
3972                         sizeof(struct mlx5_counter_stats_raw)) * raws_n +
3973                         sizeof(struct mlx5_counter_stats_mem_mng);
3974         uint8_t *mem = rte_calloc(__func__, 1, size, sysconf(_SC_PAGESIZE));
3975         int i;
3976
3977         if (!mem) {
3978                 rte_errno = ENOMEM;
3979                 return NULL;
3980         }
3981         mem_mng = (struct mlx5_counter_stats_mem_mng *)(mem + size) - 1;
3982         size = sizeof(*raw_data) * MLX5_COUNTERS_PER_POOL * raws_n;
3983         mem_mng->umem = mlx5_glue->devx_umem_reg(sh->ctx, mem, size,
3984                                                  IBV_ACCESS_LOCAL_WRITE);
3985         if (!mem_mng->umem) {
3986                 rte_errno = errno;
3987                 rte_free(mem);
3988                 return NULL;
3989         }
3990         mkey_attr.addr = (uintptr_t)mem;
3991         mkey_attr.size = size;
3992         mkey_attr.umem_id = mem_mng->umem->umem_id;
3993         mkey_attr.pd = sh->pdn;
3994         mkey_attr.log_entity_size = 0;
3995         mkey_attr.pg_access = 0;
3996         mkey_attr.klm_array = NULL;
3997         mkey_attr.klm_num = 0;
3998         mkey_attr.relaxed_ordering = 1;
3999         mem_mng->dm = mlx5_devx_cmd_mkey_create(sh->ctx, &mkey_attr);
4000         if (!mem_mng->dm) {
4001                 mlx5_glue->devx_umem_dereg(mem_mng->umem);
4002                 rte_errno = errno;
4003                 rte_free(mem);
4004                 return NULL;
4005         }
4006         mem_mng->raws = (struct mlx5_counter_stats_raw *)(mem + size);
4007         raw_data = (volatile struct flow_counter_stats *)mem;
4008         for (i = 0; i < raws_n; ++i) {
4009                 mem_mng->raws[i].mem_mng = mem_mng;
4010                 mem_mng->raws[i].data = raw_data + i * MLX5_COUNTERS_PER_POOL;
4011         }
4012         LIST_INSERT_HEAD(&sh->cmng.mem_mngs, mem_mng, next);
4013         return mem_mng;
4014 }
4015
4016 /**
4017  * Resize a counter container.
4018  *
4019  * @param[in] dev
4020  *   Pointer to the Ethernet device structure.
4021  * @param[in] batch
4022  *   Whether the pool is for counter that was allocated by batch command.
4023  *
4024  * @return
4025  *   The new container pointer on success, otherwise NULL and rte_errno is set.
4026  */
4027 static struct mlx5_pools_container *
4028 flow_dv_container_resize(struct rte_eth_dev *dev, uint32_t batch)
4029 {
4030         struct mlx5_priv *priv = dev->data->dev_private;
4031         struct mlx5_pools_container *cont =
4032                         MLX5_CNT_CONTAINER(priv->sh, batch, 0);
4033         struct mlx5_pools_container *new_cont =
4034                         MLX5_CNT_CONTAINER_UNUSED(priv->sh, batch, 0);
4035         struct mlx5_counter_stats_mem_mng *mem_mng = NULL;
4036         uint32_t resize = cont->n + MLX5_CNT_CONTAINER_RESIZE;
4037         uint32_t mem_size = sizeof(struct mlx5_flow_counter_pool *) * resize;
4038         int i;
4039
4040         /* Fallback mode has no background thread. Skip the check. */
4041         if (!priv->counter_fallback &&
4042             cont != MLX5_CNT_CONTAINER(priv->sh, batch, 1)) {
4043                 /* The last resize still hasn't detected by the host thread. */
4044                 rte_errno = EAGAIN;
4045                 return NULL;
4046         }
4047         new_cont->pools = rte_calloc(__func__, 1, mem_size, 0);
4048         if (!new_cont->pools) {
4049                 rte_errno = ENOMEM;
4050                 return NULL;
4051         }
4052         if (cont->n)
4053                 memcpy(new_cont->pools, cont->pools, cont->n *
4054                        sizeof(struct mlx5_flow_counter_pool *));
4055         /*
4056          * Fallback mode query the counter directly, no background query
4057          * resources are needed.
4058          */
4059         if (!priv->counter_fallback) {
4060                 mem_mng = flow_dv_create_counter_stat_mem_mng(dev,
4061                         MLX5_CNT_CONTAINER_RESIZE + MLX5_MAX_PENDING_QUERIES);
4062                 if (!mem_mng) {
4063                         rte_free(new_cont->pools);
4064                         return NULL;
4065                 }
4066                 for (i = 0; i < MLX5_MAX_PENDING_QUERIES; ++i)
4067                         LIST_INSERT_HEAD(&priv->sh->cmng.free_stat_raws,
4068                                          mem_mng->raws +
4069                                          MLX5_CNT_CONTAINER_RESIZE +
4070                                          i, next);
4071         } else {
4072                 /*
4073                  * Release the old container pools directly as no background
4074                  * thread helps that.
4075                  */
4076                 rte_free(cont->pools);
4077         }
4078         new_cont->n = resize;
4079         rte_atomic16_set(&new_cont->n_valid, rte_atomic16_read(&cont->n_valid));
4080         TAILQ_INIT(&new_cont->pool_list);
4081         TAILQ_CONCAT(&new_cont->pool_list, &cont->pool_list, next);
4082         new_cont->init_mem_mng = mem_mng;
4083         rte_cio_wmb();
4084          /* Flip the master container. */
4085         priv->sh->cmng.mhi[batch] ^= (uint8_t)1;
4086         return new_cont;
4087 }
4088
4089 /**
4090  * Query a devx flow counter.
4091  *
4092  * @param[in] dev
4093  *   Pointer to the Ethernet device structure.
4094  * @param[in] cnt
4095  *   Index to the flow counter.
4096  * @param[out] pkts
4097  *   The statistics value of packets.
4098  * @param[out] bytes
4099  *   The statistics value of bytes.
4100  *
4101  * @return
4102  *   0 on success, otherwise a negative errno value and rte_errno is set.
4103  */
4104 static inline int
4105 _flow_dv_query_count(struct rte_eth_dev *dev, uint32_t counter, uint64_t *pkts,
4106                      uint64_t *bytes)
4107 {
4108         struct mlx5_priv *priv = dev->data->dev_private;
4109         struct mlx5_flow_counter_pool *pool = NULL;
4110         struct mlx5_flow_counter *cnt;
4111         struct mlx5_flow_counter_ext *cnt_ext = NULL;
4112         int offset;
4113
4114         cnt = flow_dv_counter_get_by_idx(dev, counter, &pool);
4115         MLX5_ASSERT(pool);
4116         if (counter < MLX5_CNT_BATCH_OFFSET) {
4117                 cnt_ext = MLX5_CNT_TO_CNT_EXT(pool, cnt);
4118                 if (priv->counter_fallback)
4119                         return mlx5_devx_cmd_flow_counter_query(cnt_ext->dcs, 0,
4120                                         0, pkts, bytes, 0, NULL, NULL, 0);
4121         }
4122
4123         rte_spinlock_lock(&pool->sl);
4124         /*
4125          * The single counters allocation may allocate smaller ID than the
4126          * current allocated in parallel to the host reading.
4127          * In this case the new counter values must be reported as 0.
4128          */
4129         if (unlikely(cnt_ext && cnt_ext->dcs->id < pool->raw->min_dcs_id)) {
4130                 *pkts = 0;
4131                 *bytes = 0;
4132         } else {
4133                 offset = cnt - &pool->counters_raw[0];
4134                 *pkts = rte_be_to_cpu_64(pool->raw->data[offset].hits);
4135                 *bytes = rte_be_to_cpu_64(pool->raw->data[offset].bytes);
4136         }
4137         rte_spinlock_unlock(&pool->sl);
4138         return 0;
4139 }
4140
4141 /**
4142  * Create and initialize a new counter pool.
4143  *
4144  * @param[in] dev
4145  *   Pointer to the Ethernet device structure.
4146  * @param[out] dcs
4147  *   The devX counter handle.
4148  * @param[in] batch
4149  *   Whether the pool is for counter that was allocated by batch command.
4150  * @param[in/out] cont_cur
4151  *   Pointer to the container pointer, it will be update in pool resize.
4152  *
4153  * @return
4154  *   The pool container pointer on success, NULL otherwise and rte_errno is set.
4155  */
4156 static struct mlx5_pools_container *
4157 flow_dv_pool_create(struct rte_eth_dev *dev, struct mlx5_devx_obj *dcs,
4158                     uint32_t batch)
4159 {
4160         struct mlx5_priv *priv = dev->data->dev_private;
4161         struct mlx5_flow_counter_pool *pool;
4162         struct mlx5_pools_container *cont = MLX5_CNT_CONTAINER(priv->sh, batch,
4163                                                                0);
4164         int16_t n_valid = rte_atomic16_read(&cont->n_valid);
4165         uint32_t size;
4166
4167         if (cont->n == n_valid) {
4168                 cont = flow_dv_container_resize(dev, batch);
4169                 if (!cont)
4170                         return NULL;
4171         }
4172         size = sizeof(*pool);
4173         if (!batch)
4174                 size += MLX5_COUNTERS_PER_POOL *
4175                         sizeof(struct mlx5_flow_counter_ext);
4176         pool = rte_calloc(__func__, 1, size, 0);
4177         if (!pool) {
4178                 rte_errno = ENOMEM;
4179                 return NULL;
4180         }
4181         pool->min_dcs = dcs;
4182         if (!priv->counter_fallback)
4183                 pool->raw = cont->init_mem_mng->raws + n_valid %
4184                                                      MLX5_CNT_CONTAINER_RESIZE;
4185         pool->raw_hw = NULL;
4186         rte_spinlock_init(&pool->sl);
4187         /*
4188          * The generation of the new allocated counters in this pool is 0, 2 in
4189          * the pool generation makes all the counters valid for allocation.
4190          * The start and end query generation protect the counters be released
4191          * between the query and update gap period will not be reallocated
4192          * without the last query finished and stats updated to the memory.
4193          */
4194         rte_atomic64_set(&pool->start_query_gen, 0x2);
4195         /*
4196          * There's no background query thread for fallback mode, set the
4197          * end_query_gen to the maximum value since no need to wait for
4198          * statistics update.
4199          */
4200         rte_atomic64_set(&pool->end_query_gen, priv->counter_fallback ?
4201                          INT64_MAX : 0x2);
4202         TAILQ_INIT(&pool->counters);
4203         TAILQ_INSERT_HEAD(&cont->pool_list, pool, next);
4204         pool->index = n_valid;
4205         cont->pools[n_valid] = pool;
4206         /* Pool initialization must be updated before host thread access. */
4207         rte_cio_wmb();
4208         rte_atomic16_add(&cont->n_valid, 1);
4209         return cont;
4210 }
4211
4212 /**
4213  * Prepare a new counter and/or a new counter pool.
4214  *
4215  * @param[in] dev
4216  *   Pointer to the Ethernet device structure.
4217  * @param[out] cnt_free
4218  *   Where to put the pointer of a new counter.
4219  * @param[in] batch
4220  *   Whether the pool is for counter that was allocated by batch command.
4221  *
4222  * @return
4223  *   The counter container pointer and @p cnt_free is set on success,
4224  *   NULL otherwise and rte_errno is set.
4225  */
4226 static struct mlx5_pools_container *
4227 flow_dv_counter_pool_prepare(struct rte_eth_dev *dev,
4228                              struct mlx5_flow_counter **cnt_free,
4229                              uint32_t batch)
4230 {
4231         struct mlx5_priv *priv = dev->data->dev_private;
4232         struct mlx5_pools_container *cont;
4233         struct mlx5_flow_counter_pool *pool;
4234         struct mlx5_devx_obj *dcs = NULL;
4235         struct mlx5_flow_counter *cnt;
4236         uint32_t i;
4237
4238         cont = MLX5_CNT_CONTAINER(priv->sh, batch, 0);
4239         if (!batch) {
4240                 /* bulk_bitmap must be 0 for single counter allocation. */
4241                 dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->ctx, 0);
4242                 if (!dcs)
4243                         return NULL;
4244                 pool = flow_dv_find_pool_by_id(cont, dcs->id);
4245                 if (!pool) {
4246                         cont = flow_dv_pool_create(dev, dcs, batch);
4247                         if (!cont) {
4248                                 mlx5_devx_cmd_destroy(dcs);
4249                                 return NULL;
4250                         }
4251                         pool = TAILQ_FIRST(&cont->pool_list);
4252                 } else if (dcs->id < pool->min_dcs->id) {
4253                         rte_atomic64_set(&pool->a64_dcs,
4254                                          (int64_t)(uintptr_t)dcs);
4255                 }
4256                 i = dcs->id % MLX5_COUNTERS_PER_POOL;
4257                 cnt = &pool->counters_raw[i];
4258                 TAILQ_INSERT_HEAD(&pool->counters, cnt, next);
4259                 MLX5_GET_POOL_CNT_EXT(pool, i)->dcs = dcs;
4260                 *cnt_free = cnt;
4261                 return cont;
4262         }
4263         /* bulk_bitmap is in 128 counters units. */
4264         if (priv->config.hca_attr.flow_counter_bulk_alloc_bitmap & 0x4)
4265                 dcs = mlx5_devx_cmd_flow_counter_alloc(priv->sh->ctx, 0x4);
4266         if (!dcs) {
4267                 rte_errno = ENODATA;
4268                 return NULL;
4269         }
4270         cont = flow_dv_pool_create(dev, dcs, batch);
4271         if (!cont) {
4272                 mlx5_devx_cmd_destroy(dcs);
4273                 return NULL;
4274         }
4275         pool = TAILQ_FIRST(&cont->pool_list);
4276         for (i = 0; i < MLX5_COUNTERS_PER_POOL; ++i) {
4277                 cnt = &pool->counters_raw[i];
4278                 TAILQ_INSERT_HEAD(&pool->counters, cnt, next);
4279         }
4280         *cnt_free = &pool->counters_raw[0];
4281         return cont;
4282 }
4283
4284 /**
4285  * Search for existed shared counter.
4286  *
4287  * @param[in] cont
4288  *   Pointer to the relevant counter pool container.
4289  * @param[in] id
4290  *   The shared counter ID to search.
4291  * @param[out] ppool
4292  *   mlx5 flow counter pool in the container,
4293  *
4294  * @return
4295  *   NULL if not existed, otherwise pointer to the shared extend counter.
4296  */
4297 static struct mlx5_flow_counter_ext *
4298 flow_dv_counter_shared_search(struct mlx5_pools_container *cont, uint32_t id,
4299                               struct mlx5_flow_counter_pool **ppool)
4300 {
4301         static struct mlx5_flow_counter_ext *cnt;
4302         struct mlx5_flow_counter_pool *pool;
4303         uint32_t i;
4304         uint32_t n_valid = rte_atomic16_read(&cont->n_valid);
4305
4306         for (i = 0; i < n_valid; i++) {
4307                 pool = cont->pools[i];
4308                 for (i = 0; i < MLX5_COUNTERS_PER_POOL; ++i) {
4309                         cnt = MLX5_GET_POOL_CNT_EXT(pool, i);
4310                         if (cnt->ref_cnt && cnt->shared && cnt->id == id) {
4311                                 if (ppool)
4312                                         *ppool = cont->pools[i];
4313                                 return cnt;
4314                         }
4315                 }
4316         }
4317         return NULL;
4318 }
4319
4320 /**
4321  * Allocate a flow counter.
4322  *
4323  * @param[in] dev
4324  *   Pointer to the Ethernet device structure.
4325  * @param[in] shared
4326  *   Indicate if this counter is shared with other flows.
4327  * @param[in] id
4328  *   Counter identifier.
4329  * @param[in] group
4330  *   Counter flow group.
4331  *
4332  * @return
4333  *   Index to flow counter on success, 0 otherwise and rte_errno is set.
4334  */
4335 static uint32_t
4336 flow_dv_counter_alloc(struct rte_eth_dev *dev, uint32_t shared, uint32_t id,
4337                       uint16_t group)
4338 {
4339         struct mlx5_priv *priv = dev->data->dev_private;
4340         struct mlx5_flow_counter_pool *pool = NULL;
4341         struct mlx5_flow_counter *cnt_free = NULL;
4342         struct mlx5_flow_counter_ext *cnt_ext = NULL;
4343         /*
4344          * Currently group 0 flow counter cannot be assigned to a flow if it is
4345          * not the first one in the batch counter allocation, so it is better
4346          * to allocate counters one by one for these flows in a separate
4347          * container.
4348          * A counter can be shared between different groups so need to take
4349          * shared counters from the single container.
4350          */
4351         uint32_t batch = (group && !shared && !priv->counter_fallback) ? 1 : 0;
4352         struct mlx5_pools_container *cont = MLX5_CNT_CONTAINER(priv->sh, batch,
4353                                                                0);
4354         uint32_t cnt_idx;
4355
4356         if (!priv->config.devx) {
4357                 rte_errno = ENOTSUP;
4358                 return 0;
4359         }
4360         if (shared) {
4361                 cnt_ext = flow_dv_counter_shared_search(cont, id, &pool);
4362                 if (cnt_ext) {
4363                         if (cnt_ext->ref_cnt + 1 == 0) {
4364                                 rte_errno = E2BIG;
4365                                 return 0;
4366                         }
4367                         cnt_ext->ref_cnt++;
4368                         cnt_idx = pool->index * MLX5_COUNTERS_PER_POOL +
4369                                   (cnt_ext->dcs->id % MLX5_COUNTERS_PER_POOL)
4370                                   + 1;
4371                         return cnt_idx;
4372                 }
4373         }
4374         /* Pools which has a free counters are in the start. */
4375         TAILQ_FOREACH(pool, &cont->pool_list, next) {
4376                 /*
4377                  * The free counter reset values must be updated between the
4378                  * counter release to the counter allocation, so, at least one
4379                  * query must be done in this time. ensure it by saving the
4380                  * query generation in the release time.
4381                  * The free list is sorted according to the generation - so if
4382                  * the first one is not updated, all the others are not
4383                  * updated too.
4384                  */
4385                 cnt_free = TAILQ_FIRST(&pool->counters);
4386                 if (cnt_free && cnt_free->query_gen <
4387                     rte_atomic64_read(&pool->end_query_gen))
4388                         break;
4389                 cnt_free = NULL;
4390         }
4391         if (!cnt_free) {
4392                 cont = flow_dv_counter_pool_prepare(dev, &cnt_free, batch);
4393                 if (!cont)
4394                         return 0;
4395                 pool = TAILQ_FIRST(&cont->pool_list);
4396         }
4397         if (!batch)
4398                 cnt_ext = MLX5_CNT_TO_CNT_EXT(pool, cnt_free);
4399         /* Create a DV counter action only in the first time usage. */
4400         if (!cnt_free->action) {
4401                 uint16_t offset;
4402                 struct mlx5_devx_obj *dcs;
4403
4404                 if (batch) {
4405                         offset = cnt_free - &pool->counters_raw[0];
4406                         dcs = pool->min_dcs;
4407                 } else {
4408                         offset = 0;
4409                         dcs = cnt_ext->dcs;
4410                 }
4411                 cnt_free->action = mlx5_glue->dv_create_flow_action_counter
4412                                         (dcs->obj, offset);
4413                 if (!cnt_free->action) {
4414                         rte_errno = errno;
4415                         return 0;
4416                 }
4417         }
4418         cnt_idx = MLX5_MAKE_CNT_IDX(pool->index,
4419                                     (cnt_free - pool->counters_raw));
4420         cnt_idx += batch * MLX5_CNT_BATCH_OFFSET;
4421         /* Update the counter reset values. */
4422         if (_flow_dv_query_count(dev, cnt_idx, &cnt_free->hits,
4423                                  &cnt_free->bytes))
4424                 return 0;
4425         if (cnt_ext) {
4426                 cnt_ext->shared = shared;
4427                 cnt_ext->ref_cnt = 1;
4428                 cnt_ext->id = id;
4429         }
4430         if (!priv->counter_fallback && !priv->sh->cmng.query_thread_on)
4431                 /* Start the asynchronous batch query by the host thread. */
4432                 mlx5_set_query_alarm(priv->sh);
4433         TAILQ_REMOVE(&pool->counters, cnt_free, next);
4434         if (TAILQ_EMPTY(&pool->counters)) {
4435                 /* Move the pool to the end of the container pool list. */
4436                 TAILQ_REMOVE(&cont->pool_list, pool, next);
4437                 TAILQ_INSERT_TAIL(&cont->pool_list, pool, next);
4438         }
4439         return cnt_idx;
4440 }
4441
4442 /**
4443  * Release a flow counter.
4444  *
4445  * @param[in] dev
4446  *   Pointer to the Ethernet device structure.
4447  * @param[in] counter
4448  *   Index to the counter handler.
4449  */
4450 static void
4451 flow_dv_counter_release(struct rte_eth_dev *dev, uint32_t counter)
4452 {
4453         struct mlx5_flow_counter_pool *pool = NULL;
4454         struct mlx5_flow_counter *cnt;
4455         struct mlx5_flow_counter_ext *cnt_ext = NULL;
4456
4457         if (!counter)
4458                 return;
4459         cnt = flow_dv_counter_get_by_idx(dev, counter, &pool);
4460         MLX5_ASSERT(pool);
4461         if (counter < MLX5_CNT_BATCH_OFFSET) {
4462                 cnt_ext = MLX5_CNT_TO_CNT_EXT(pool, cnt);
4463                 if (cnt_ext && --cnt_ext->ref_cnt)
4464                         return;
4465         }
4466         /* Put the counter in the end - the last updated one. */
4467         TAILQ_INSERT_TAIL(&pool->counters, cnt, next);
4468         /*
4469          * Counters released between query trigger and handler need
4470          * to wait the next round of query. Since the packets arrive
4471          * in the gap period will not be taken into account to the
4472          * old counter.
4473          */
4474         cnt->query_gen = rte_atomic64_read(&pool->start_query_gen);
4475 }
4476
4477 /**
4478  * Verify the @p attributes will be correctly understood by the NIC and store
4479  * them in the @p flow if everything is correct.
4480  *
4481  * @param[in] dev
4482  *   Pointer to dev struct.
4483  * @param[in] attributes
4484  *   Pointer to flow attributes
4485  * @param[in] external
4486  *   This flow rule is created by request external to PMD.
4487  * @param[out] error
4488  *   Pointer to error structure.
4489  *
4490  * @return
4491  *   0 on success, a negative errno value otherwise and rte_errno is set.
4492  */
4493 static int
4494 flow_dv_validate_attributes(struct rte_eth_dev *dev,
4495                             const struct rte_flow_attr *attributes,
4496                             bool external __rte_unused,
4497                             struct rte_flow_error *error)
4498 {
4499         struct mlx5_priv *priv = dev->data->dev_private;
4500         uint32_t priority_max = priv->config.flow_prio - 1;
4501
4502 #ifndef HAVE_MLX5DV_DR
4503         if (attributes->group)
4504                 return rte_flow_error_set(error, ENOTSUP,
4505                                           RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
4506                                           NULL,
4507                                           "groups are not supported");
4508 #else
4509         uint32_t table;
4510         int ret;
4511
4512         ret = mlx5_flow_group_to_table(attributes, external,
4513                                        attributes->group, !!priv->fdb_def_rule,
4514                                        &table, error);
4515         if (ret)
4516                 return ret;
4517 #endif
4518         if (attributes->priority != MLX5_FLOW_PRIO_RSVD &&
4519             attributes->priority >= priority_max)
4520                 return rte_flow_error_set(error, ENOTSUP,
4521                                           RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
4522                                           NULL,
4523                                           "priority out of range");
4524         if (attributes->transfer) {
4525                 if (!priv->config.dv_esw_en)
4526                         return rte_flow_error_set
4527                                 (error, ENOTSUP,
4528                                  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4529                                  "E-Switch dr is not supported");
4530                 if (!(priv->representor || priv->master))
4531                         return rte_flow_error_set
4532                                 (error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4533                                  NULL, "E-Switch configuration can only be"
4534                                  " done by a master or a representor device");
4535                 if (attributes->egress)
4536                         return rte_flow_error_set
4537                                 (error, ENOTSUP,
4538                                  RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, attributes,
4539                                  "egress is not supported");
4540         }
4541         if (!(attributes->egress ^ attributes->ingress))
4542                 return rte_flow_error_set(error, ENOTSUP,
4543                                           RTE_FLOW_ERROR_TYPE_ATTR, NULL,
4544                                           "must specify exactly one of "
4545                                           "ingress or egress");
4546         return 0;
4547 }
4548
4549 /**
4550  * Internal validation function. For validating both actions and items.
4551  *
4552  * @param[in] dev
4553  *   Pointer to the rte_eth_dev structure.
4554  * @param[in] attr
4555  *   Pointer to the flow attributes.
4556  * @param[in] items
4557  *   Pointer to the list of items.
4558  * @param[in] actions
4559  *   Pointer to the list of actions.
4560  * @param[in] external
4561  *   This flow rule is created by request external to PMD.
4562  * @param[out] error
4563  *   Pointer to the error structure.
4564  *
4565  * @return
4566  *   0 on success, a negative errno value otherwise and rte_errno is set.
4567  */
4568 static int
4569 flow_dv_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attr,
4570                  const struct rte_flow_item items[],
4571                  const struct rte_flow_action actions[],
4572                  bool external, struct rte_flow_error *error)
4573 {
4574         int ret;
4575         uint64_t action_flags = 0;
4576         uint64_t item_flags = 0;
4577         uint64_t last_item = 0;
4578         uint8_t next_protocol = 0xff;
4579         uint16_t ether_type = 0;
4580         int actions_n = 0;
4581         uint8_t item_ipv6_proto = 0;
4582         const struct rte_flow_item *gre_item = NULL;
4583         const struct rte_flow_action_raw_decap *decap;
4584         const struct rte_flow_action_raw_encap *encap;
4585         const struct rte_flow_action_rss *rss;
4586         const struct rte_flow_item_tcp nic_tcp_mask = {
4587                 .hdr = {
4588                         .tcp_flags = 0xFF,
4589                         .src_port = RTE_BE16(UINT16_MAX),
4590                         .dst_port = RTE_BE16(UINT16_MAX),
4591                 }
4592         };
4593         const struct rte_flow_item_ipv4 nic_ipv4_mask = {
4594                 .hdr = {
4595                         .src_addr = RTE_BE32(0xffffffff),
4596                         .dst_addr = RTE_BE32(0xffffffff),
4597                         .type_of_service = 0xff,
4598                         .next_proto_id = 0xff,
4599                         .time_to_live = 0xff,
4600                 },
4601         };
4602         const struct rte_flow_item_ipv6 nic_ipv6_mask = {
4603                 .hdr = {
4604                         .src_addr =
4605                         "\xff\xff\xff\xff\xff\xff\xff\xff"
4606                         "\xff\xff\xff\xff\xff\xff\xff\xff",
4607                         .dst_addr =
4608                         "\xff\xff\xff\xff\xff\xff\xff\xff"
4609                         "\xff\xff\xff\xff\xff\xff\xff\xff",
4610                         .vtc_flow = RTE_BE32(0xffffffff),
4611                         .proto = 0xff,
4612                         .hop_limits = 0xff,
4613                 },
4614         };
4615         struct mlx5_priv *priv = dev->data->dev_private;
4616         struct mlx5_dev_config *dev_conf = &priv->config;
4617         uint16_t queue_index = 0xFFFF;
4618         const struct rte_flow_item_vlan *vlan_m = NULL;
4619
4620         if (items == NULL)
4621                 return -1;
4622         ret = flow_dv_validate_attributes(dev, attr, external, error);
4623         if (ret < 0)
4624                 return ret;
4625         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
4626                 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
4627                 int type = items->type;
4628
4629                 switch (type) {
4630                 case RTE_FLOW_ITEM_TYPE_VOID:
4631                         break;
4632                 case RTE_FLOW_ITEM_TYPE_PORT_ID:
4633                         ret = flow_dv_validate_item_port_id
4634                                         (dev, items, attr, item_flags, error);
4635                         if (ret < 0)
4636                                 return ret;
4637                         last_item = MLX5_FLOW_ITEM_PORT_ID;
4638                         break;
4639                 case RTE_FLOW_ITEM_TYPE_ETH:
4640                         ret = mlx5_flow_validate_item_eth(items, item_flags,
4641                                                           error);
4642                         if (ret < 0)
4643                                 return ret;
4644                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
4645                                              MLX5_FLOW_LAYER_OUTER_L2;
4646                         if (items->mask != NULL && items->spec != NULL) {
4647                                 ether_type =
4648                                         ((const struct rte_flow_item_eth *)
4649                                          items->spec)->type;
4650                                 ether_type &=
4651                                         ((const struct rte_flow_item_eth *)
4652                                          items->mask)->type;
4653                                 ether_type = rte_be_to_cpu_16(ether_type);
4654                         } else {
4655                                 ether_type = 0;
4656                         }
4657                         break;
4658                 case RTE_FLOW_ITEM_TYPE_VLAN:
4659                         ret = mlx5_flow_validate_item_vlan(items, item_flags,
4660                                                            dev, error);
4661                         if (ret < 0)
4662                                 return ret;
4663                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
4664                                              MLX5_FLOW_LAYER_OUTER_VLAN;
4665                         if (items->mask != NULL && items->spec != NULL) {
4666                                 ether_type =
4667                                         ((const struct rte_flow_item_vlan *)
4668                                          items->spec)->inner_type;
4669                                 ether_type &=
4670                                         ((const struct rte_flow_item_vlan *)
4671                                          items->mask)->inner_type;
4672                                 ether_type = rte_be_to_cpu_16(ether_type);
4673                         } else {
4674                                 ether_type = 0;
4675                         }
4676                         /* Store outer VLAN mask for of_push_vlan action. */
4677                         if (!tunnel)
4678                                 vlan_m = items->mask;
4679                         break;
4680                 case RTE_FLOW_ITEM_TYPE_IPV4:
4681                         mlx5_flow_tunnel_ip_check(items, next_protocol,
4682                                                   &item_flags, &tunnel);
4683                         ret = mlx5_flow_validate_item_ipv4(items, item_flags,
4684                                                            last_item,
4685                                                            ether_type,
4686                                                            &nic_ipv4_mask,
4687                                                            error);
4688                         if (ret < 0)
4689                                 return ret;
4690                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
4691                                              MLX5_FLOW_LAYER_OUTER_L3_IPV4;
4692                         if (items->mask != NULL &&
4693                             ((const struct rte_flow_item_ipv4 *)
4694                              items->mask)->hdr.next_proto_id) {
4695                                 next_protocol =
4696                                         ((const struct rte_flow_item_ipv4 *)
4697                                          (items->spec))->hdr.next_proto_id;
4698                                 next_protocol &=
4699                                         ((const struct rte_flow_item_ipv4 *)
4700                                          (items->mask))->hdr.next_proto_id;
4701                         } else {
4702                                 /* Reset for inner layer. */
4703                                 next_protocol = 0xff;
4704                         }
4705                         break;
4706                 case RTE_FLOW_ITEM_TYPE_IPV6:
4707                         mlx5_flow_tunnel_ip_check(items, next_protocol,
4708                                                   &item_flags, &tunnel);
4709                         ret = mlx5_flow_validate_item_ipv6(items, item_flags,
4710                                                            last_item,
4711                                                            ether_type,
4712                                                            &nic_ipv6_mask,
4713                                                            error);
4714                         if (ret < 0)
4715                                 return ret;
4716                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
4717                                              MLX5_FLOW_LAYER_OUTER_L3_IPV6;
4718                         if (items->mask != NULL &&
4719                             ((const struct rte_flow_item_ipv6 *)
4720                              items->mask)->hdr.proto) {
4721                                 item_ipv6_proto =
4722                                         ((const struct rte_flow_item_ipv6 *)
4723                                          items->spec)->hdr.proto;
4724                                 next_protocol =
4725                                         ((const struct rte_flow_item_ipv6 *)
4726                                          items->spec)->hdr.proto;
4727                                 next_protocol &=
4728                                         ((const struct rte_flow_item_ipv6 *)
4729                                          items->mask)->hdr.proto;
4730                         } else {
4731                                 /* Reset for inner layer. */
4732                                 next_protocol = 0xff;
4733                         }
4734                         break;
4735                 case RTE_FLOW_ITEM_TYPE_TCP:
4736                         ret = mlx5_flow_validate_item_tcp
4737                                                 (items, item_flags,
4738                                                  next_protocol,
4739                                                  &nic_tcp_mask,
4740                                                  error);
4741                         if (ret < 0)
4742                                 return ret;
4743                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
4744                                              MLX5_FLOW_LAYER_OUTER_L4_TCP;
4745                         break;
4746                 case RTE_FLOW_ITEM_TYPE_UDP:
4747                         ret = mlx5_flow_validate_item_udp(items, item_flags,
4748                                                           next_protocol,
4749                                                           error);
4750                         if (ret < 0)
4751                                 return ret;
4752                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
4753                                              MLX5_FLOW_LAYER_OUTER_L4_UDP;
4754                         break;
4755                 case RTE_FLOW_ITEM_TYPE_GRE:
4756                         ret = mlx5_flow_validate_item_gre(items, item_flags,
4757                                                           next_protocol, error);
4758                         if (ret < 0)
4759                                 return ret;
4760                         gre_item = items;
4761                         last_item = MLX5_FLOW_LAYER_GRE;
4762                         break;
4763                 case RTE_FLOW_ITEM_TYPE_NVGRE:
4764                         ret = mlx5_flow_validate_item_nvgre(items, item_flags,
4765                                                             next_protocol,
4766                                                             error);
4767                         if (ret < 0)
4768                                 return ret;
4769                         last_item = MLX5_FLOW_LAYER_NVGRE;
4770                         break;
4771                 case RTE_FLOW_ITEM_TYPE_GRE_KEY:
4772                         ret = mlx5_flow_validate_item_gre_key
4773                                 (items, item_flags, gre_item, error);
4774                         if (ret < 0)
4775                                 return ret;
4776                         last_item = MLX5_FLOW_LAYER_GRE_KEY;
4777                         break;
4778                 case RTE_FLOW_ITEM_TYPE_VXLAN:
4779                         ret = mlx5_flow_validate_item_vxlan(items, item_flags,
4780                                                             error);
4781                         if (ret < 0)
4782                                 return ret;
4783                         last_item = MLX5_FLOW_LAYER_VXLAN;
4784                         break;
4785                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
4786                         ret = mlx5_flow_validate_item_vxlan_gpe(items,
4787                                                                 item_flags, dev,
4788                                                                 error);
4789                         if (ret < 0)
4790                                 return ret;
4791                         last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
4792                         break;
4793                 case RTE_FLOW_ITEM_TYPE_GENEVE:
4794                         ret = mlx5_flow_validate_item_geneve(items,
4795                                                              item_flags, dev,
4796                                                              error);
4797                         if (ret < 0)
4798                                 return ret;
4799                         last_item = MLX5_FLOW_LAYER_GENEVE;
4800                         break;
4801                 case RTE_FLOW_ITEM_TYPE_MPLS:
4802                         ret = mlx5_flow_validate_item_mpls(dev, items,
4803                                                            item_flags,
4804                                                            last_item, error);
4805                         if (ret < 0)
4806                                 return ret;
4807                         last_item = MLX5_FLOW_LAYER_MPLS;
4808                         break;
4809
4810                 case RTE_FLOW_ITEM_TYPE_MARK:
4811                         ret = flow_dv_validate_item_mark(dev, items, attr,
4812                                                          error);
4813                         if (ret < 0)
4814                                 return ret;
4815                         last_item = MLX5_FLOW_ITEM_MARK;
4816                         break;
4817                 case RTE_FLOW_ITEM_TYPE_META:
4818                         ret = flow_dv_validate_item_meta(dev, items, attr,
4819                                                          error);
4820                         if (ret < 0)
4821                                 return ret;
4822                         last_item = MLX5_FLOW_ITEM_METADATA;
4823                         break;
4824                 case RTE_FLOW_ITEM_TYPE_ICMP:
4825                         ret = mlx5_flow_validate_item_icmp(items, item_flags,
4826                                                            next_protocol,
4827                                                            error);
4828                         if (ret < 0)
4829                                 return ret;
4830                         last_item = MLX5_FLOW_LAYER_ICMP;
4831                         break;
4832                 case RTE_FLOW_ITEM_TYPE_ICMP6:
4833                         ret = mlx5_flow_validate_item_icmp6(items, item_flags,
4834                                                             next_protocol,
4835                                                             error);
4836                         if (ret < 0)
4837                                 return ret;
4838                         item_ipv6_proto = IPPROTO_ICMPV6;
4839                         last_item = MLX5_FLOW_LAYER_ICMP6;
4840                         break;
4841                 case RTE_FLOW_ITEM_TYPE_TAG:
4842                         ret = flow_dv_validate_item_tag(dev, items,
4843                                                         attr, error);
4844                         if (ret < 0)
4845                                 return ret;
4846                         last_item = MLX5_FLOW_ITEM_TAG;
4847                         break;
4848                 case MLX5_RTE_FLOW_ITEM_TYPE_TAG:
4849                 case MLX5_RTE_FLOW_ITEM_TYPE_TX_QUEUE:
4850                         break;
4851                 case RTE_FLOW_ITEM_TYPE_GTP:
4852                         ret = flow_dv_validate_item_gtp(dev, items, item_flags,
4853                                                         error);
4854                         if (ret < 0)
4855                                 return ret;
4856                         last_item = MLX5_FLOW_LAYER_GTP;
4857                         break;
4858                 default:
4859                         return rte_flow_error_set(error, ENOTSUP,
4860                                                   RTE_FLOW_ERROR_TYPE_ITEM,
4861                                                   NULL, "item not supported");
4862                 }
4863                 item_flags |= last_item;
4864         }
4865         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
4866                 int type = actions->type;
4867                 if (actions_n == MLX5_DV_MAX_NUMBER_OF_ACTIONS)
4868                         return rte_flow_error_set(error, ENOTSUP,
4869                                                   RTE_FLOW_ERROR_TYPE_ACTION,
4870                                                   actions, "too many actions");
4871                 switch (type) {
4872                 case RTE_FLOW_ACTION_TYPE_VOID:
4873                         break;
4874                 case RTE_FLOW_ACTION_TYPE_PORT_ID:
4875                         ret = flow_dv_validate_action_port_id(dev,
4876                                                               action_flags,
4877                                                               actions,
4878                                                               attr,
4879                                                               error);
4880                         if (ret)
4881                                 return ret;
4882                         action_flags |= MLX5_FLOW_ACTION_PORT_ID;
4883                         ++actions_n;
4884                         break;
4885                 case RTE_FLOW_ACTION_TYPE_FLAG:
4886                         ret = flow_dv_validate_action_flag(dev, action_flags,
4887                                                            attr, error);
4888                         if (ret < 0)
4889                                 return ret;
4890                         if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
4891                                 /* Count all modify-header actions as one. */
4892                                 if (!(action_flags &
4893                                       MLX5_FLOW_MODIFY_HDR_ACTIONS))
4894                                         ++actions_n;
4895                                 action_flags |= MLX5_FLOW_ACTION_FLAG |
4896                                                 MLX5_FLOW_ACTION_MARK_EXT;
4897                         } else {
4898                                 action_flags |= MLX5_FLOW_ACTION_FLAG;
4899                                 ++actions_n;
4900                         }
4901                         break;
4902                 case RTE_FLOW_ACTION_TYPE_MARK:
4903                         ret = flow_dv_validate_action_mark(dev, actions,
4904                                                            action_flags,
4905                                                            attr, error);
4906                         if (ret < 0)
4907                                 return ret;
4908                         if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
4909                                 /* Count all modify-header actions as one. */
4910                                 if (!(action_flags &
4911                                       MLX5_FLOW_MODIFY_HDR_ACTIONS))
4912                                         ++actions_n;
4913                                 action_flags |= MLX5_FLOW_ACTION_MARK |
4914                                                 MLX5_FLOW_ACTION_MARK_EXT;
4915                         } else {
4916                                 action_flags |= MLX5_FLOW_ACTION_MARK;
4917                                 ++actions_n;
4918                         }
4919                         break;
4920                 case RTE_FLOW_ACTION_TYPE_SET_META:
4921                         ret = flow_dv_validate_action_set_meta(dev, actions,
4922                                                                action_flags,
4923                                                                attr, error);
4924                         if (ret < 0)
4925                                 return ret;
4926                         /* Count all modify-header actions as one action. */
4927                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
4928                                 ++actions_n;
4929                         action_flags |= MLX5_FLOW_ACTION_SET_META;
4930                         break;
4931                 case RTE_FLOW_ACTION_TYPE_SET_TAG:
4932                         ret = flow_dv_validate_action_set_tag(dev, actions,
4933                                                               action_flags,
4934                                                               attr, error);
4935                         if (ret < 0)
4936                                 return ret;
4937                         /* Count all modify-header actions as one action. */
4938                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
4939                                 ++actions_n;
4940                         action_flags |= MLX5_FLOW_ACTION_SET_TAG;
4941                         break;
4942                 case RTE_FLOW_ACTION_TYPE_DROP:
4943                         ret = mlx5_flow_validate_action_drop(action_flags,
4944                                                              attr, error);
4945                         if (ret < 0)
4946                                 return ret;
4947                         action_flags |= MLX5_FLOW_ACTION_DROP;
4948                         ++actions_n;
4949                         break;
4950                 case RTE_FLOW_ACTION_TYPE_QUEUE:
4951                         ret = mlx5_flow_validate_action_queue(actions,
4952                                                               action_flags, dev,
4953                                                               attr, error);
4954                         if (ret < 0)
4955                                 return ret;
4956                         queue_index = ((const struct rte_flow_action_queue *)
4957                                                         (actions->conf))->index;
4958                         action_flags |= MLX5_FLOW_ACTION_QUEUE;
4959                         ++actions_n;
4960                         break;
4961                 case RTE_FLOW_ACTION_TYPE_RSS:
4962                         rss = actions->conf;
4963                         ret = mlx5_flow_validate_action_rss(actions,
4964                                                             action_flags, dev,
4965                                                             attr, item_flags,
4966                                                             error);
4967                         if (ret < 0)
4968                                 return ret;
4969                         if (rss != NULL && rss->queue_num)
4970                                 queue_index = rss->queue[0];
4971                         action_flags |= MLX5_FLOW_ACTION_RSS;
4972                         ++actions_n;
4973                         break;
4974                 case RTE_FLOW_ACTION_TYPE_COUNT:
4975                         ret = flow_dv_validate_action_count(dev, error);
4976                         if (ret < 0)
4977                                 return ret;
4978                         action_flags |= MLX5_FLOW_ACTION_COUNT;
4979                         ++actions_n;
4980                         break;
4981                 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
4982                         if (flow_dv_validate_action_pop_vlan(dev,
4983                                                              action_flags,
4984                                                              actions,
4985                                                              item_flags, attr,
4986                                                              error))
4987                                 return -rte_errno;
4988                         action_flags |= MLX5_FLOW_ACTION_OF_POP_VLAN;
4989                         ++actions_n;
4990                         break;
4991                 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
4992                         ret = flow_dv_validate_action_push_vlan(dev,
4993                                                                 action_flags,
4994                                                                 vlan_m,
4995                                                                 actions, attr,
4996                                                                 error);
4997                         if (ret < 0)
4998                                 return ret;
4999                         action_flags |= MLX5_FLOW_ACTION_OF_PUSH_VLAN;
5000                         ++actions_n;
5001                         break;
5002                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
5003                         ret = flow_dv_validate_action_set_vlan_pcp
5004                                                 (action_flags, actions, error);
5005                         if (ret < 0)
5006                                 return ret;
5007                         /* Count PCP with push_vlan command. */
5008                         action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_PCP;
5009                         break;
5010                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
5011                         ret = flow_dv_validate_action_set_vlan_vid
5012                                                 (item_flags, action_flags,
5013                                                  actions, error);
5014                         if (ret < 0)
5015                                 return ret;
5016                         /* Count VID with push_vlan command. */
5017                         action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_VID;
5018                         break;
5019                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
5020                 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
5021                         ret = flow_dv_validate_action_l2_encap(dev,
5022                                                                action_flags,
5023                                                                actions, attr,
5024                                                                error);
5025                         if (ret < 0)
5026                                 return ret;
5027                         action_flags |= MLX5_FLOW_ACTION_ENCAP;
5028                         ++actions_n;
5029                         break;
5030                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
5031                 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
5032                         ret = flow_dv_validate_action_decap(dev, action_flags,
5033                                                             attr, error);
5034                         if (ret < 0)
5035                                 return ret;
5036                         action_flags |= MLX5_FLOW_ACTION_DECAP;
5037                         ++actions_n;
5038                         break;
5039                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
5040                         ret = flow_dv_validate_action_raw_encap_decap
5041                                 (dev, NULL, actions->conf, attr, &action_flags,
5042                                  &actions_n, error);
5043                         if (ret < 0)
5044                                 return ret;
5045                         break;
5046                 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
5047                         decap = actions->conf;
5048                         while ((++actions)->type == RTE_FLOW_ACTION_TYPE_VOID)
5049                                 ;
5050                         if (actions->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
5051                                 encap = NULL;
5052                                 actions--;
5053                         } else {
5054                                 encap = actions->conf;
5055                         }
5056                         ret = flow_dv_validate_action_raw_encap_decap
5057                                            (dev,
5058                                             decap ? decap : &empty_decap, encap,
5059                                             attr, &action_flags, &actions_n,
5060                                             error);
5061                         if (ret < 0)
5062                                 return ret;
5063                         break;
5064                 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
5065                 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
5066                         ret = flow_dv_validate_action_modify_mac(action_flags,
5067                                                                  actions,
5068                                                                  item_flags,
5069                                                                  error);
5070                         if (ret < 0)
5071                                 return ret;
5072                         /* Count all modify-header actions as one action. */
5073                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5074                                 ++actions_n;
5075                         action_flags |= actions->type ==
5076                                         RTE_FLOW_ACTION_TYPE_SET_MAC_SRC ?
5077                                                 MLX5_FLOW_ACTION_SET_MAC_SRC :
5078                                                 MLX5_FLOW_ACTION_SET_MAC_DST;
5079                         break;
5080
5081                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
5082                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
5083                         ret = flow_dv_validate_action_modify_ipv4(action_flags,
5084                                                                   actions,
5085                                                                   item_flags,
5086                                                                   error);
5087                         if (ret < 0)
5088                                 return ret;
5089                         /* Count all modify-header actions as one action. */
5090                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5091                                 ++actions_n;
5092                         action_flags |= actions->type ==
5093                                         RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC ?
5094                                                 MLX5_FLOW_ACTION_SET_IPV4_SRC :
5095                                                 MLX5_FLOW_ACTION_SET_IPV4_DST;
5096                         break;
5097                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
5098                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
5099                         ret = flow_dv_validate_action_modify_ipv6(action_flags,
5100                                                                   actions,
5101                                                                   item_flags,
5102                                                                   error);
5103                         if (ret < 0)
5104                                 return ret;
5105                         if (item_ipv6_proto == IPPROTO_ICMPV6)
5106                                 return rte_flow_error_set(error, ENOTSUP,
5107                                         RTE_FLOW_ERROR_TYPE_ACTION,
5108                                         actions,
5109                                         "Can't change header "
5110                                         "with ICMPv6 proto");
5111                         /* Count all modify-header actions as one action. */
5112                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5113                                 ++actions_n;
5114                         action_flags |= actions->type ==
5115                                         RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC ?
5116                                                 MLX5_FLOW_ACTION_SET_IPV6_SRC :
5117                                                 MLX5_FLOW_ACTION_SET_IPV6_DST;
5118                         break;
5119                 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
5120                 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
5121                         ret = flow_dv_validate_action_modify_tp(action_flags,
5122                                                                 actions,
5123                                                                 item_flags,
5124                                                                 error);
5125                         if (ret < 0)
5126                                 return ret;
5127                         /* Count all modify-header actions as one action. */
5128                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5129                                 ++actions_n;
5130                         action_flags |= actions->type ==
5131                                         RTE_FLOW_ACTION_TYPE_SET_TP_SRC ?
5132                                                 MLX5_FLOW_ACTION_SET_TP_SRC :
5133                                                 MLX5_FLOW_ACTION_SET_TP_DST;
5134                         break;
5135                 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
5136                 case RTE_FLOW_ACTION_TYPE_SET_TTL:
5137                         ret = flow_dv_validate_action_modify_ttl(action_flags,
5138                                                                  actions,
5139                                                                  item_flags,
5140                                                                  error);
5141                         if (ret < 0)
5142                                 return ret;
5143                         /* Count all modify-header actions as one action. */
5144                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5145                                 ++actions_n;
5146                         action_flags |= actions->type ==
5147                                         RTE_FLOW_ACTION_TYPE_SET_TTL ?
5148                                                 MLX5_FLOW_ACTION_SET_TTL :
5149                                                 MLX5_FLOW_ACTION_DEC_TTL;
5150                         break;
5151                 case RTE_FLOW_ACTION_TYPE_JUMP:
5152                         ret = flow_dv_validate_action_jump(actions,
5153                                                            action_flags,
5154                                                            attr, external,
5155                                                            error);
5156                         if (ret)
5157                                 return ret;
5158                         ++actions_n;
5159                         action_flags |= MLX5_FLOW_ACTION_JUMP;
5160                         break;
5161                 case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
5162                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
5163                         ret = flow_dv_validate_action_modify_tcp_seq
5164                                                                 (action_flags,
5165                                                                  actions,
5166                                                                  item_flags,
5167                                                                  error);
5168                         if (ret < 0)
5169                                 return ret;
5170                         /* Count all modify-header actions as one action. */
5171                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5172                                 ++actions_n;
5173                         action_flags |= actions->type ==
5174                                         RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ ?
5175                                                 MLX5_FLOW_ACTION_INC_TCP_SEQ :
5176                                                 MLX5_FLOW_ACTION_DEC_TCP_SEQ;
5177                         break;
5178                 case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
5179                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
5180                         ret = flow_dv_validate_action_modify_tcp_ack
5181                                                                 (action_flags,
5182                                                                  actions,
5183                                                                  item_flags,
5184                                                                  error);
5185                         if (ret < 0)
5186                                 return ret;
5187                         /* Count all modify-header actions as one action. */
5188                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5189                                 ++actions_n;
5190                         action_flags |= actions->type ==
5191                                         RTE_FLOW_ACTION_TYPE_INC_TCP_ACK ?
5192                                                 MLX5_FLOW_ACTION_INC_TCP_ACK :
5193                                                 MLX5_FLOW_ACTION_DEC_TCP_ACK;
5194                         break;
5195                 case MLX5_RTE_FLOW_ACTION_TYPE_TAG:
5196                 case MLX5_RTE_FLOW_ACTION_TYPE_MARK:
5197                 case MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG:
5198                         break;
5199                 case RTE_FLOW_ACTION_TYPE_METER:
5200                         ret = mlx5_flow_validate_action_meter(dev,
5201                                                               action_flags,
5202                                                               actions, attr,
5203                                                               error);
5204                         if (ret < 0)
5205                                 return ret;
5206                         action_flags |= MLX5_FLOW_ACTION_METER;
5207                         ++actions_n;
5208                         break;
5209                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP:
5210                         ret = flow_dv_validate_action_modify_ipv4_dscp
5211                                                          (action_flags,
5212                                                           actions,
5213                                                           item_flags,
5214                                                           error);
5215                         if (ret < 0)
5216                                 return ret;
5217                         /* Count all modify-header actions as one action. */
5218                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5219                                 ++actions_n;
5220                         action_flags |= MLX5_FLOW_ACTION_SET_IPV4_DSCP;
5221                         break;
5222                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP:
5223                         ret = flow_dv_validate_action_modify_ipv6_dscp
5224                                                                 (action_flags,
5225                                                                  actions,
5226                                                                  item_flags,
5227                                                                  error);
5228                         if (ret < 0)
5229                                 return ret;
5230                         /* Count all modify-header actions as one action. */
5231                         if (!(action_flags & MLX5_FLOW_MODIFY_HDR_ACTIONS))
5232                                 ++actions_n;
5233                         action_flags |= MLX5_FLOW_ACTION_SET_IPV6_DSCP;
5234                         break;
5235                 default:
5236                         return rte_flow_error_set(error, ENOTSUP,
5237                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5238                                                   actions,
5239                                                   "action not supported");
5240                 }
5241         }
5242         /*
5243          * Validate the drop action mutual exclusion with other actions.
5244          * Drop action is mutually-exclusive with any other action, except for
5245          * Count action.
5246          */
5247         if ((action_flags & MLX5_FLOW_ACTION_DROP) &&
5248             (action_flags & ~(MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_COUNT)))
5249                 return rte_flow_error_set(error, EINVAL,
5250                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5251                                           "Drop action is mutually-exclusive "
5252                                           "with any other action, except for "
5253                                           "Count action");
5254         /* Eswitch has few restrictions on using items and actions */
5255         if (attr->transfer) {
5256                 if (!mlx5_flow_ext_mreg_supported(dev) &&
5257                     action_flags & MLX5_FLOW_ACTION_FLAG)
5258                         return rte_flow_error_set(error, ENOTSUP,
5259                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5260                                                   NULL,
5261                                                   "unsupported action FLAG");
5262                 if (!mlx5_flow_ext_mreg_supported(dev) &&
5263                     action_flags & MLX5_FLOW_ACTION_MARK)
5264                         return rte_flow_error_set(error, ENOTSUP,
5265                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5266                                                   NULL,
5267                                                   "unsupported action MARK");
5268                 if (action_flags & MLX5_FLOW_ACTION_QUEUE)
5269                         return rte_flow_error_set(error, ENOTSUP,
5270                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5271                                                   NULL,
5272                                                   "unsupported action QUEUE");
5273                 if (action_flags & MLX5_FLOW_ACTION_RSS)
5274                         return rte_flow_error_set(error, ENOTSUP,
5275                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5276                                                   NULL,
5277                                                   "unsupported action RSS");
5278                 if (!(action_flags & MLX5_FLOW_FATE_ESWITCH_ACTIONS))
5279                         return rte_flow_error_set(error, EINVAL,
5280                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5281                                                   actions,
5282                                                   "no fate action is found");
5283         } else {
5284                 if (!(action_flags & MLX5_FLOW_FATE_ACTIONS) && attr->ingress)
5285                         return rte_flow_error_set(error, EINVAL,
5286                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5287                                                   actions,
5288                                                   "no fate action is found");
5289         }
5290         /* Continue validation for Xcap actions.*/
5291         if ((action_flags & MLX5_FLOW_XCAP_ACTIONS) && (queue_index == 0xFFFF ||
5292             mlx5_rxq_get_type(dev, queue_index) != MLX5_RXQ_TYPE_HAIRPIN)) {
5293                 if ((action_flags & MLX5_FLOW_XCAP_ACTIONS) ==
5294                     MLX5_FLOW_XCAP_ACTIONS)
5295                         return rte_flow_error_set(error, ENOTSUP,
5296                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5297                                                   NULL, "encap and decap "
5298                                                   "combination aren't supported");
5299                 if (!attr->transfer && attr->ingress && (action_flags &
5300                                                         MLX5_FLOW_ACTION_ENCAP))
5301                         return rte_flow_error_set(error, ENOTSUP,
5302                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5303                                                   NULL, "encap is not supported"
5304                                                   " for ingress traffic");
5305         }
5306         return 0;
5307 }
5308
5309 /**
5310  * Internal preparation function. Allocates the DV flow size,
5311  * this size is constant.
5312  *
5313  * @param[in] dev
5314  *   Pointer to the rte_eth_dev structure.
5315  * @param[in] attr
5316  *   Pointer to the flow attributes.
5317  * @param[in] items
5318  *   Pointer to the list of items.
5319  * @param[in] actions
5320  *   Pointer to the list of actions.
5321  * @param[out] error
5322  *   Pointer to the error structure.
5323  *
5324  * @return
5325  *   Pointer to mlx5_flow object on success,
5326  *   otherwise NULL and rte_errno is set.
5327  */
5328 static struct mlx5_flow *
5329 flow_dv_prepare(struct rte_eth_dev *dev,
5330                 const struct rte_flow_attr *attr __rte_unused,
5331                 const struct rte_flow_item items[] __rte_unused,
5332                 const struct rte_flow_action actions[] __rte_unused,
5333                 struct rte_flow_error *error)
5334 {
5335         uint32_t handle_idx = 0;
5336         struct mlx5_flow *dev_flow;
5337         struct mlx5_flow_handle *dev_handle;
5338         struct mlx5_priv *priv = dev->data->dev_private;
5339
5340         /* In case of corrupting the memory. */
5341         if (priv->flow_idx >= MLX5_NUM_MAX_DEV_FLOWS) {
5342                 rte_flow_error_set(error, ENOSPC,
5343                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5344                                    "not free temporary device flow");
5345                 return NULL;
5346         }
5347         dev_handle = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
5348                                    &handle_idx);
5349         if (!dev_handle) {
5350                 rte_flow_error_set(error, ENOMEM,
5351                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5352                                    "not enough memory to create flow handle");
5353                 return NULL;
5354         }
5355         /* No multi-thread supporting. */
5356         dev_flow = &((struct mlx5_flow *)priv->inter_flows)[priv->flow_idx++];
5357         dev_flow->handle = dev_handle;
5358         dev_flow->handle_idx = handle_idx;
5359         dev_flow->dv.value.size = MLX5_ST_SZ_BYTES(fte_match_param);
5360         /*
5361          * The matching value needs to be cleared to 0 before using. In the
5362          * past, it will be automatically cleared when using rte_*alloc
5363          * API. The time consumption will be almost the same as before.
5364          */
5365         memset(dev_flow->dv.value.buf, 0, MLX5_ST_SZ_BYTES(fte_match_param));
5366         dev_flow->ingress = attr->ingress;
5367         dev_flow->dv.transfer = attr->transfer;
5368         return dev_flow;
5369 }
5370
5371 #ifdef RTE_LIBRTE_MLX5_DEBUG
5372 /**
5373  * Sanity check for match mask and value. Similar to check_valid_spec() in
5374  * kernel driver. If unmasked bit is present in value, it returns failure.
5375  *
5376  * @param match_mask
5377  *   pointer to match mask buffer.
5378  * @param match_value
5379  *   pointer to match value buffer.
5380  *
5381  * @return
5382  *   0 if valid, -EINVAL otherwise.
5383  */
5384 static int
5385 flow_dv_check_valid_spec(void *match_mask, void *match_value)
5386 {
5387         uint8_t *m = match_mask;
5388         uint8_t *v = match_value;
5389         unsigned int i;
5390
5391         for (i = 0; i < MLX5_ST_SZ_BYTES(fte_match_param); ++i) {
5392                 if (v[i] & ~m[i]) {
5393                         DRV_LOG(ERR,
5394                                 "match_value differs from match_criteria"
5395                                 " %p[%u] != %p[%u]",
5396                                 match_value, i, match_mask, i);
5397                         return -EINVAL;
5398                 }
5399         }
5400         return 0;
5401 }
5402 #endif
5403
5404 /**
5405  * Add Ethernet item to matcher and to the value.
5406  *
5407  * @param[in, out] matcher
5408  *   Flow matcher.
5409  * @param[in, out] key
5410  *   Flow matcher value.
5411  * @param[in] item
5412  *   Flow pattern to translate.
5413  * @param[in] inner
5414  *   Item is inner pattern.
5415  */
5416 static void
5417 flow_dv_translate_item_eth(void *matcher, void *key,
5418                            const struct rte_flow_item *item, int inner)
5419 {
5420         const struct rte_flow_item_eth *eth_m = item->mask;
5421         const struct rte_flow_item_eth *eth_v = item->spec;
5422         const struct rte_flow_item_eth nic_mask = {
5423                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
5424                 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
5425                 .type = RTE_BE16(0xffff),
5426         };
5427         void *headers_m;
5428         void *headers_v;
5429         char *l24_v;
5430         unsigned int i;
5431
5432         if (!eth_v)
5433                 return;
5434         if (!eth_m)
5435                 eth_m = &nic_mask;
5436         if (inner) {
5437                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5438                                          inner_headers);
5439                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5440         } else {
5441                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5442                                          outer_headers);
5443                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5444         }
5445         memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m, dmac_47_16),
5446                &eth_m->dst, sizeof(eth_m->dst));
5447         /* The value must be in the range of the mask. */
5448         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, dmac_47_16);
5449         for (i = 0; i < sizeof(eth_m->dst); ++i)
5450                 l24_v[i] = eth_m->dst.addr_bytes[i] & eth_v->dst.addr_bytes[i];
5451         memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m, smac_47_16),
5452                &eth_m->src, sizeof(eth_m->src));
5453         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, smac_47_16);
5454         /* The value must be in the range of the mask. */
5455         for (i = 0; i < sizeof(eth_m->dst); ++i)
5456                 l24_v[i] = eth_m->src.addr_bytes[i] & eth_v->src.addr_bytes[i];
5457         if (eth_v->type) {
5458                 /* When ethertype is present set mask for tagged VLAN. */
5459                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, cvlan_tag, 1);
5460                 /* Set value for tagged VLAN if ethertype is 802.1Q. */
5461                 if (eth_v->type == RTE_BE16(RTE_ETHER_TYPE_VLAN) ||
5462                     eth_v->type == RTE_BE16(RTE_ETHER_TYPE_QINQ)) {
5463                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, cvlan_tag,
5464                                  1);
5465                         /* Return here to avoid setting match on ethertype. */
5466                         return;
5467                 }
5468         }
5469         /*
5470          * HW supports match on one Ethertype, the Ethertype following the last
5471          * VLAN tag of the packet (see PRM).
5472          * Set match on ethertype only if ETH header is not followed by VLAN.
5473          */
5474         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ethertype,
5475                  rte_be_to_cpu_16(eth_m->type));
5476         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, ethertype);
5477         *(uint16_t *)(l24_v) = eth_m->type & eth_v->type;
5478 }
5479
5480 /**
5481  * Add VLAN item to matcher and to the value.
5482  *
5483  * @param[in, out] dev_flow
5484  *   Flow descriptor.
5485  * @param[in, out] matcher
5486  *   Flow matcher.
5487  * @param[in, out] key
5488  *   Flow matcher value.
5489  * @param[in] item
5490  *   Flow pattern to translate.
5491  * @param[in] inner
5492  *   Item is inner pattern.
5493  */
5494 static void
5495 flow_dv_translate_item_vlan(struct mlx5_flow *dev_flow,
5496                             void *matcher, void *key,
5497                             const struct rte_flow_item *item,
5498                             int inner)
5499 {
5500         const struct rte_flow_item_vlan *vlan_m = item->mask;
5501         const struct rte_flow_item_vlan *vlan_v = item->spec;
5502         void *headers_m;
5503         void *headers_v;
5504         uint16_t tci_m;
5505         uint16_t tci_v;
5506
5507         if (!vlan_v)
5508                 return;
5509         if (!vlan_m)
5510                 vlan_m = &rte_flow_item_vlan_mask;
5511         if (inner) {
5512                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5513                                          inner_headers);
5514                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5515         } else {
5516                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5517                                          outer_headers);
5518                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5519                 /*
5520                  * This is workaround, masks are not supported,
5521                  * and pre-validated.
5522                  */
5523                 dev_flow->handle->vf_vlan.tag =
5524                         rte_be_to_cpu_16(vlan_v->tci) & 0x0fff;
5525         }
5526         tci_m = rte_be_to_cpu_16(vlan_m->tci);
5527         tci_v = rte_be_to_cpu_16(vlan_m->tci & vlan_v->tci);
5528         MLX5_SET(fte_match_set_lyr_2_4, headers_m, cvlan_tag, 1);
5529         MLX5_SET(fte_match_set_lyr_2_4, headers_v, cvlan_tag, 1);
5530         MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_vid, tci_m);
5531         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_vid, tci_v);
5532         MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_cfi, tci_m >> 12);
5533         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_cfi, tci_v >> 12);
5534         MLX5_SET(fte_match_set_lyr_2_4, headers_m, first_prio, tci_m >> 13);
5535         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_prio, tci_v >> 13);
5536         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ethertype,
5537                  rte_be_to_cpu_16(vlan_m->inner_type));
5538         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype,
5539                  rte_be_to_cpu_16(vlan_m->inner_type & vlan_v->inner_type));
5540 }
5541
5542 /**
5543  * Add IPV4 item to matcher and to the value.
5544  *
5545  * @param[in, out] matcher
5546  *   Flow matcher.
5547  * @param[in, out] key
5548  *   Flow matcher value.
5549  * @param[in] item
5550  *   Flow pattern to translate.
5551  * @param[in] item_flags
5552  *   Bit-fields that holds the items detected until now.
5553  * @param[in] inner
5554  *   Item is inner pattern.
5555  * @param[in] group
5556  *   The group to insert the rule.
5557  */
5558 static void
5559 flow_dv_translate_item_ipv4(void *matcher, void *key,
5560                             const struct rte_flow_item *item,
5561                             const uint64_t item_flags,
5562                             int inner, uint32_t group)
5563 {
5564         const struct rte_flow_item_ipv4 *ipv4_m = item->mask;
5565         const struct rte_flow_item_ipv4 *ipv4_v = item->spec;
5566         const struct rte_flow_item_ipv4 nic_mask = {
5567                 .hdr = {
5568                         .src_addr = RTE_BE32(0xffffffff),
5569                         .dst_addr = RTE_BE32(0xffffffff),
5570                         .type_of_service = 0xff,
5571                         .next_proto_id = 0xff,
5572                         .time_to_live = 0xff,
5573                 },
5574         };
5575         void *headers_m;
5576         void *headers_v;
5577         char *l24_m;
5578         char *l24_v;
5579         uint8_t tos;
5580
5581         if (inner) {
5582                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5583                                          inner_headers);
5584                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5585         } else {
5586                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5587                                          outer_headers);
5588                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5589         }
5590         if (group == 0)
5591                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0xf);
5592         else
5593                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0x4);
5594         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_version, 4);
5595         /*
5596          * On outer header (which must contains L2), or inner header with L2,
5597          * set cvlan_tag mask bit to mark this packet as untagged.
5598          * This should be done even if item->spec is empty.
5599          */
5600         if (!inner || item_flags & MLX5_FLOW_LAYER_INNER_L2)
5601                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, cvlan_tag, 1);
5602         if (!ipv4_v)
5603                 return;
5604         if (!ipv4_m)
5605                 ipv4_m = &nic_mask;
5606         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
5607                              dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
5608         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
5609                              dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
5610         *(uint32_t *)l24_m = ipv4_m->hdr.dst_addr;
5611         *(uint32_t *)l24_v = ipv4_m->hdr.dst_addr & ipv4_v->hdr.dst_addr;
5612         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
5613                           src_ipv4_src_ipv6.ipv4_layout.ipv4);
5614         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
5615                           src_ipv4_src_ipv6.ipv4_layout.ipv4);
5616         *(uint32_t *)l24_m = ipv4_m->hdr.src_addr;
5617         *(uint32_t *)l24_v = ipv4_m->hdr.src_addr & ipv4_v->hdr.src_addr;
5618         tos = ipv4_m->hdr.type_of_service & ipv4_v->hdr.type_of_service;
5619         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn,
5620                  ipv4_m->hdr.type_of_service);
5621         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, tos);
5622         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp,
5623                  ipv4_m->hdr.type_of_service >> 2);
5624         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, tos >> 2);
5625         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
5626                  ipv4_m->hdr.next_proto_id);
5627         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
5628                  ipv4_v->hdr.next_proto_id & ipv4_m->hdr.next_proto_id);
5629         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ttl_hoplimit,
5630                  ipv4_m->hdr.time_to_live);
5631         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ttl_hoplimit,
5632                  ipv4_v->hdr.time_to_live & ipv4_m->hdr.time_to_live);
5633 }
5634
5635 /**
5636  * Add IPV6 item to matcher and to the value.
5637  *
5638  * @param[in, out] matcher
5639  *   Flow matcher.
5640  * @param[in, out] key
5641  *   Flow matcher value.
5642  * @param[in] item
5643  *   Flow pattern to translate.
5644  * @param[in] item_flags
5645  *   Bit-fields that holds the items detected until now.
5646  * @param[in] inner
5647  *   Item is inner pattern.
5648  * @param[in] group
5649  *   The group to insert the rule.
5650  */
5651 static void
5652 flow_dv_translate_item_ipv6(void *matcher, void *key,
5653                             const struct rte_flow_item *item,
5654                             const uint64_t item_flags,
5655                             int inner, uint32_t group)
5656 {
5657         const struct rte_flow_item_ipv6 *ipv6_m = item->mask;
5658         const struct rte_flow_item_ipv6 *ipv6_v = item->spec;
5659         const struct rte_flow_item_ipv6 nic_mask = {
5660                 .hdr = {
5661                         .src_addr =
5662                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
5663                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
5664                         .dst_addr =
5665                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
5666                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
5667                         .vtc_flow = RTE_BE32(0xffffffff),
5668                         .proto = 0xff,
5669                         .hop_limits = 0xff,
5670                 },
5671         };
5672         void *headers_m;
5673         void *headers_v;
5674         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5675         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5676         char *l24_m;
5677         char *l24_v;
5678         uint32_t vtc_m;
5679         uint32_t vtc_v;
5680         int i;
5681         int size;
5682
5683         if (inner) {
5684                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5685                                          inner_headers);
5686                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5687         } else {
5688                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5689                                          outer_headers);
5690                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5691         }
5692         if (group == 0)
5693                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0xf);
5694         else
5695                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 0x6);
5696         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_version, 6);
5697         /*
5698          * On outer header (which must contains L2), or inner header with L2,
5699          * set cvlan_tag mask bit to mark this packet as untagged.
5700          * This should be done even if item->spec is empty.
5701          */
5702         if (!inner || item_flags & MLX5_FLOW_LAYER_INNER_L2)
5703                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, cvlan_tag, 1);
5704         if (!ipv6_v)
5705                 return;
5706         if (!ipv6_m)
5707                 ipv6_m = &nic_mask;
5708         size = sizeof(ipv6_m->hdr.dst_addr);
5709         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
5710                              dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
5711         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
5712                              dst_ipv4_dst_ipv6.ipv6_layout.ipv6);
5713         memcpy(l24_m, ipv6_m->hdr.dst_addr, size);
5714         for (i = 0; i < size; ++i)
5715                 l24_v[i] = l24_m[i] & ipv6_v->hdr.dst_addr[i];
5716         l24_m = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_m,
5717                              src_ipv4_src_ipv6.ipv6_layout.ipv6);
5718         l24_v = MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
5719                              src_ipv4_src_ipv6.ipv6_layout.ipv6);
5720         memcpy(l24_m, ipv6_m->hdr.src_addr, size);
5721         for (i = 0; i < size; ++i)
5722                 l24_v[i] = l24_m[i] & ipv6_v->hdr.src_addr[i];
5723         /* TOS. */
5724         vtc_m = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow);
5725         vtc_v = rte_be_to_cpu_32(ipv6_m->hdr.vtc_flow & ipv6_v->hdr.vtc_flow);
5726         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ecn, vtc_m >> 20);
5727         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, vtc_v >> 20);
5728         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_dscp, vtc_m >> 22);
5729         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, vtc_v >> 22);
5730         /* Label. */
5731         if (inner) {
5732                 MLX5_SET(fte_match_set_misc, misc_m, inner_ipv6_flow_label,
5733                          vtc_m);
5734                 MLX5_SET(fte_match_set_misc, misc_v, inner_ipv6_flow_label,
5735                          vtc_v);
5736         } else {
5737                 MLX5_SET(fte_match_set_misc, misc_m, outer_ipv6_flow_label,
5738                          vtc_m);
5739                 MLX5_SET(fte_match_set_misc, misc_v, outer_ipv6_flow_label,
5740                          vtc_v);
5741         }
5742         /* Protocol. */
5743         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol,
5744                  ipv6_m->hdr.proto);
5745         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
5746                  ipv6_v->hdr.proto & ipv6_m->hdr.proto);
5747         /* Hop limit. */
5748         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_ttl_hoplimit,
5749                  ipv6_m->hdr.hop_limits);
5750         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ttl_hoplimit,
5751                  ipv6_v->hdr.hop_limits & ipv6_m->hdr.hop_limits);
5752 }
5753
5754 /**
5755  * Add TCP item to matcher and to the value.
5756  *
5757  * @param[in, out] matcher
5758  *   Flow matcher.
5759  * @param[in, out] key
5760  *   Flow matcher value.
5761  * @param[in] item
5762  *   Flow pattern to translate.
5763  * @param[in] inner
5764  *   Item is inner pattern.
5765  */
5766 static void
5767 flow_dv_translate_item_tcp(void *matcher, void *key,
5768                            const struct rte_flow_item *item,
5769                            int inner)
5770 {
5771         const struct rte_flow_item_tcp *tcp_m = item->mask;
5772         const struct rte_flow_item_tcp *tcp_v = item->spec;
5773         void *headers_m;
5774         void *headers_v;
5775
5776         if (inner) {
5777                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5778                                          inner_headers);
5779                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5780         } else {
5781                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5782                                          outer_headers);
5783                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5784         }
5785         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
5786         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_TCP);
5787         if (!tcp_v)
5788                 return;
5789         if (!tcp_m)
5790                 tcp_m = &rte_flow_item_tcp_mask;
5791         MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_sport,
5792                  rte_be_to_cpu_16(tcp_m->hdr.src_port));
5793         MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
5794                  rte_be_to_cpu_16(tcp_v->hdr.src_port & tcp_m->hdr.src_port));
5795         MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_dport,
5796                  rte_be_to_cpu_16(tcp_m->hdr.dst_port));
5797         MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
5798                  rte_be_to_cpu_16(tcp_v->hdr.dst_port & tcp_m->hdr.dst_port));
5799         MLX5_SET(fte_match_set_lyr_2_4, headers_m, tcp_flags,
5800                  tcp_m->hdr.tcp_flags);
5801         MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_flags,
5802                  (tcp_v->hdr.tcp_flags & tcp_m->hdr.tcp_flags));
5803 }
5804
5805 /**
5806  * Add UDP item to matcher and to the value.
5807  *
5808  * @param[in, out] matcher
5809  *   Flow matcher.
5810  * @param[in, out] key
5811  *   Flow matcher value.
5812  * @param[in] item
5813  *   Flow pattern to translate.
5814  * @param[in] inner
5815  *   Item is inner pattern.
5816  */
5817 static void
5818 flow_dv_translate_item_udp(void *matcher, void *key,
5819                            const struct rte_flow_item *item,
5820                            int inner)
5821 {
5822         const struct rte_flow_item_udp *udp_m = item->mask;
5823         const struct rte_flow_item_udp *udp_v = item->spec;
5824         void *headers_m;
5825         void *headers_v;
5826
5827         if (inner) {
5828                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5829                                          inner_headers);
5830                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5831         } else {
5832                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5833                                          outer_headers);
5834                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5835         }
5836         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
5837         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_UDP);
5838         if (!udp_v)
5839                 return;
5840         if (!udp_m)
5841                 udp_m = &rte_flow_item_udp_mask;
5842         MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_sport,
5843                  rte_be_to_cpu_16(udp_m->hdr.src_port));
5844         MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
5845                  rte_be_to_cpu_16(udp_v->hdr.src_port & udp_m->hdr.src_port));
5846         MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport,
5847                  rte_be_to_cpu_16(udp_m->hdr.dst_port));
5848         MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
5849                  rte_be_to_cpu_16(udp_v->hdr.dst_port & udp_m->hdr.dst_port));
5850 }
5851
5852 /**
5853  * Add GRE optional Key item to matcher and to the value.
5854  *
5855  * @param[in, out] matcher
5856  *   Flow matcher.
5857  * @param[in, out] key
5858  *   Flow matcher value.
5859  * @param[in] item
5860  *   Flow pattern to translate.
5861  * @param[in] inner
5862  *   Item is inner pattern.
5863  */
5864 static void
5865 flow_dv_translate_item_gre_key(void *matcher, void *key,
5866                                    const struct rte_flow_item *item)
5867 {
5868         const rte_be32_t *key_m = item->mask;
5869         const rte_be32_t *key_v = item->spec;
5870         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5871         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5872         rte_be32_t gre_key_default_mask = RTE_BE32(UINT32_MAX);
5873
5874         /* GRE K bit must be on and should already be validated */
5875         MLX5_SET(fte_match_set_misc, misc_m, gre_k_present, 1);
5876         MLX5_SET(fte_match_set_misc, misc_v, gre_k_present, 1);
5877         if (!key_v)
5878                 return;
5879         if (!key_m)
5880                 key_m = &gre_key_default_mask;
5881         MLX5_SET(fte_match_set_misc, misc_m, gre_key_h,
5882                  rte_be_to_cpu_32(*key_m) >> 8);
5883         MLX5_SET(fte_match_set_misc, misc_v, gre_key_h,
5884                  rte_be_to_cpu_32((*key_v) & (*key_m)) >> 8);
5885         MLX5_SET(fte_match_set_misc, misc_m, gre_key_l,
5886                  rte_be_to_cpu_32(*key_m) & 0xFF);
5887         MLX5_SET(fte_match_set_misc, misc_v, gre_key_l,
5888                  rte_be_to_cpu_32((*key_v) & (*key_m)) & 0xFF);
5889 }
5890
5891 /**
5892  * Add GRE item to matcher and to the value.
5893  *
5894  * @param[in, out] matcher
5895  *   Flow matcher.
5896  * @param[in, out] key
5897  *   Flow matcher value.
5898  * @param[in] item
5899  *   Flow pattern to translate.
5900  * @param[in] inner
5901  *   Item is inner pattern.
5902  */
5903 static void
5904 flow_dv_translate_item_gre(void *matcher, void *key,
5905                            const struct rte_flow_item *item,
5906                            int inner)
5907 {
5908         const struct rte_flow_item_gre *gre_m = item->mask;
5909         const struct rte_flow_item_gre *gre_v = item->spec;
5910         void *headers_m;
5911         void *headers_v;
5912         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5913         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5914         struct {
5915                 union {
5916                         __extension__
5917                         struct {
5918                                 uint16_t version:3;
5919                                 uint16_t rsvd0:9;
5920                                 uint16_t s_present:1;
5921                                 uint16_t k_present:1;
5922                                 uint16_t rsvd_bit1:1;
5923                                 uint16_t c_present:1;
5924                         };
5925                         uint16_t value;
5926                 };
5927         } gre_crks_rsvd0_ver_m, gre_crks_rsvd0_ver_v;
5928
5929         if (inner) {
5930                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5931                                          inner_headers);
5932                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
5933         } else {
5934                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
5935                                          outer_headers);
5936                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
5937         }
5938         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
5939         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_GRE);
5940         if (!gre_v)
5941                 return;
5942         if (!gre_m)
5943                 gre_m = &rte_flow_item_gre_mask;
5944         MLX5_SET(fte_match_set_misc, misc_m, gre_protocol,
5945                  rte_be_to_cpu_16(gre_m->protocol));
5946         MLX5_SET(fte_match_set_misc, misc_v, gre_protocol,
5947                  rte_be_to_cpu_16(gre_v->protocol & gre_m->protocol));
5948         gre_crks_rsvd0_ver_m.value = rte_be_to_cpu_16(gre_m->c_rsvd0_ver);
5949         gre_crks_rsvd0_ver_v.value = rte_be_to_cpu_16(gre_v->c_rsvd0_ver);
5950         MLX5_SET(fte_match_set_misc, misc_m, gre_c_present,
5951                  gre_crks_rsvd0_ver_m.c_present);
5952         MLX5_SET(fte_match_set_misc, misc_v, gre_c_present,
5953                  gre_crks_rsvd0_ver_v.c_present &
5954                  gre_crks_rsvd0_ver_m.c_present);
5955         MLX5_SET(fte_match_set_misc, misc_m, gre_k_present,
5956                  gre_crks_rsvd0_ver_m.k_present);
5957         MLX5_SET(fte_match_set_misc, misc_v, gre_k_present,
5958                  gre_crks_rsvd0_ver_v.k_present &
5959                  gre_crks_rsvd0_ver_m.k_present);
5960         MLX5_SET(fte_match_set_misc, misc_m, gre_s_present,
5961                  gre_crks_rsvd0_ver_m.s_present);
5962         MLX5_SET(fte_match_set_misc, misc_v, gre_s_present,
5963                  gre_crks_rsvd0_ver_v.s_present &
5964                  gre_crks_rsvd0_ver_m.s_present);
5965 }
5966
5967 /**
5968  * Add NVGRE item to matcher and to the value.
5969  *
5970  * @param[in, out] matcher
5971  *   Flow matcher.
5972  * @param[in, out] key
5973  *   Flow matcher value.
5974  * @param[in] item
5975  *   Flow pattern to translate.
5976  * @param[in] inner
5977  *   Item is inner pattern.
5978  */
5979 static void
5980 flow_dv_translate_item_nvgre(void *matcher, void *key,
5981                              const struct rte_flow_item *item,
5982                              int inner)
5983 {
5984         const struct rte_flow_item_nvgre *nvgre_m = item->mask;
5985         const struct rte_flow_item_nvgre *nvgre_v = item->spec;
5986         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
5987         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
5988         const char *tni_flow_id_m = (const char *)nvgre_m->tni;
5989         const char *tni_flow_id_v = (const char *)nvgre_v->tni;
5990         char *gre_key_m;
5991         char *gre_key_v;
5992         int size;
5993         int i;
5994
5995         /* For NVGRE, GRE header fields must be set with defined values. */
5996         const struct rte_flow_item_gre gre_spec = {
5997                 .c_rsvd0_ver = RTE_BE16(0x2000),
5998                 .protocol = RTE_BE16(RTE_ETHER_TYPE_TEB)
5999         };
6000         const struct rte_flow_item_gre gre_mask = {
6001                 .c_rsvd0_ver = RTE_BE16(0xB000),
6002                 .protocol = RTE_BE16(UINT16_MAX),
6003         };
6004         const struct rte_flow_item gre_item = {
6005                 .spec = &gre_spec,
6006                 .mask = &gre_mask,
6007                 .last = NULL,
6008         };
6009         flow_dv_translate_item_gre(matcher, key, &gre_item, inner);
6010         if (!nvgre_v)
6011                 return;
6012         if (!nvgre_m)
6013                 nvgre_m = &rte_flow_item_nvgre_mask;
6014         size = sizeof(nvgre_m->tni) + sizeof(nvgre_m->flow_id);
6015         gre_key_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, gre_key_h);
6016         gre_key_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, gre_key_h);
6017         memcpy(gre_key_m, tni_flow_id_m, size);
6018         for (i = 0; i < size; ++i)
6019                 gre_key_v[i] = gre_key_m[i] & tni_flow_id_v[i];
6020 }
6021
6022 /**
6023  * Add VXLAN item to matcher and to the value.
6024  *
6025  * @param[in, out] matcher
6026  *   Flow matcher.
6027  * @param[in, out] key
6028  *   Flow matcher value.
6029  * @param[in] item
6030  *   Flow pattern to translate.
6031  * @param[in] inner
6032  *   Item is inner pattern.
6033  */
6034 static void
6035 flow_dv_translate_item_vxlan(void *matcher, void *key,
6036                              const struct rte_flow_item *item,
6037                              int inner)
6038 {
6039         const struct rte_flow_item_vxlan *vxlan_m = item->mask;
6040         const struct rte_flow_item_vxlan *vxlan_v = item->spec;
6041         void *headers_m;
6042         void *headers_v;
6043         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
6044         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
6045         char *vni_m;
6046         char *vni_v;
6047         uint16_t dport;
6048         int size;
6049         int i;
6050
6051         if (inner) {
6052                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6053                                          inner_headers);
6054                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6055         } else {
6056                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6057                                          outer_headers);
6058                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6059         }
6060         dport = item->type == RTE_FLOW_ITEM_TYPE_VXLAN ?
6061                 MLX5_UDP_PORT_VXLAN : MLX5_UDP_PORT_VXLAN_GPE;
6062         if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
6063                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
6064                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
6065         }
6066         if (!vxlan_v)
6067                 return;
6068         if (!vxlan_m)
6069                 vxlan_m = &rte_flow_item_vxlan_mask;
6070         size = sizeof(vxlan_m->vni);
6071         vni_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, vxlan_vni);
6072         vni_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, vxlan_vni);
6073         memcpy(vni_m, vxlan_m->vni, size);
6074         for (i = 0; i < size; ++i)
6075                 vni_v[i] = vni_m[i] & vxlan_v->vni[i];
6076 }
6077
6078 /**
6079  * Add VXLAN-GPE item to matcher and to the value.
6080  *
6081  * @param[in, out] matcher
6082  *   Flow matcher.
6083  * @param[in, out] key
6084  *   Flow matcher value.
6085  * @param[in] item
6086  *   Flow pattern to translate.
6087  * @param[in] inner
6088  *   Item is inner pattern.
6089  */
6090
6091 static void
6092 flow_dv_translate_item_vxlan_gpe(void *matcher, void *key,
6093                                  const struct rte_flow_item *item, int inner)
6094 {
6095         const struct rte_flow_item_vxlan_gpe *vxlan_m = item->mask;
6096         const struct rte_flow_item_vxlan_gpe *vxlan_v = item->spec;
6097         void *headers_m;
6098         void *headers_v;
6099         void *misc_m =
6100                 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters_3);
6101         void *misc_v =
6102                 MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
6103         char *vni_m;
6104         char *vni_v;
6105         uint16_t dport;
6106         int size;
6107         int i;
6108         uint8_t flags_m = 0xff;
6109         uint8_t flags_v = 0xc;
6110
6111         if (inner) {
6112                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6113                                          inner_headers);
6114                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6115         } else {
6116                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6117                                          outer_headers);
6118                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6119         }
6120         dport = item->type == RTE_FLOW_ITEM_TYPE_VXLAN ?
6121                 MLX5_UDP_PORT_VXLAN : MLX5_UDP_PORT_VXLAN_GPE;
6122         if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
6123                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
6124                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
6125         }
6126         if (!vxlan_v)
6127                 return;
6128         if (!vxlan_m)
6129                 vxlan_m = &rte_flow_item_vxlan_gpe_mask;
6130         size = sizeof(vxlan_m->vni);
6131         vni_m = MLX5_ADDR_OF(fte_match_set_misc3, misc_m, outer_vxlan_gpe_vni);
6132         vni_v = MLX5_ADDR_OF(fte_match_set_misc3, misc_v, outer_vxlan_gpe_vni);
6133         memcpy(vni_m, vxlan_m->vni, size);
6134         for (i = 0; i < size; ++i)
6135                 vni_v[i] = vni_m[i] & vxlan_v->vni[i];
6136         if (vxlan_m->flags) {
6137                 flags_m = vxlan_m->flags;
6138                 flags_v = vxlan_v->flags;
6139         }
6140         MLX5_SET(fte_match_set_misc3, misc_m, outer_vxlan_gpe_flags, flags_m);
6141         MLX5_SET(fte_match_set_misc3, misc_v, outer_vxlan_gpe_flags, flags_v);
6142         MLX5_SET(fte_match_set_misc3, misc_m, outer_vxlan_gpe_next_protocol,
6143                  vxlan_m->protocol);
6144         MLX5_SET(fte_match_set_misc3, misc_v, outer_vxlan_gpe_next_protocol,
6145                  vxlan_v->protocol);
6146 }
6147
6148 /**
6149  * Add Geneve item to matcher and to the value.
6150  *
6151  * @param[in, out] matcher
6152  *   Flow matcher.
6153  * @param[in, out] key
6154  *   Flow matcher value.
6155  * @param[in] item
6156  *   Flow pattern to translate.
6157  * @param[in] inner
6158  *   Item is inner pattern.
6159  */
6160
6161 static void
6162 flow_dv_translate_item_geneve(void *matcher, void *key,
6163                               const struct rte_flow_item *item, int inner)
6164 {
6165         const struct rte_flow_item_geneve *geneve_m = item->mask;
6166         const struct rte_flow_item_geneve *geneve_v = item->spec;
6167         void *headers_m;
6168         void *headers_v;
6169         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
6170         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
6171         uint16_t dport;
6172         uint16_t gbhdr_m;
6173         uint16_t gbhdr_v;
6174         char *vni_m;
6175         char *vni_v;
6176         size_t size, i;
6177
6178         if (inner) {
6179                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6180                                          inner_headers);
6181                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6182         } else {
6183                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6184                                          outer_headers);
6185                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6186         }
6187         dport = MLX5_UDP_PORT_GENEVE;
6188         if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
6189                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
6190                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
6191         }
6192         if (!geneve_v)
6193                 return;
6194         if (!geneve_m)
6195                 geneve_m = &rte_flow_item_geneve_mask;
6196         size = sizeof(geneve_m->vni);
6197         vni_m = MLX5_ADDR_OF(fte_match_set_misc, misc_m, geneve_vni);
6198         vni_v = MLX5_ADDR_OF(fte_match_set_misc, misc_v, geneve_vni);
6199         memcpy(vni_m, geneve_m->vni, size);
6200         for (i = 0; i < size; ++i)
6201                 vni_v[i] = vni_m[i] & geneve_v->vni[i];
6202         MLX5_SET(fte_match_set_misc, misc_m, geneve_protocol_type,
6203                  rte_be_to_cpu_16(geneve_m->protocol));
6204         MLX5_SET(fte_match_set_misc, misc_v, geneve_protocol_type,
6205                  rte_be_to_cpu_16(geneve_v->protocol & geneve_m->protocol));
6206         gbhdr_m = rte_be_to_cpu_16(geneve_m->ver_opt_len_o_c_rsvd0);
6207         gbhdr_v = rte_be_to_cpu_16(geneve_v->ver_opt_len_o_c_rsvd0);
6208         MLX5_SET(fte_match_set_misc, misc_m, geneve_oam,
6209                  MLX5_GENEVE_OAMF_VAL(gbhdr_m));
6210         MLX5_SET(fte_match_set_misc, misc_v, geneve_oam,
6211                  MLX5_GENEVE_OAMF_VAL(gbhdr_v) & MLX5_GENEVE_OAMF_VAL(gbhdr_m));
6212         MLX5_SET(fte_match_set_misc, misc_m, geneve_opt_len,
6213                  MLX5_GENEVE_OPTLEN_VAL(gbhdr_m));
6214         MLX5_SET(fte_match_set_misc, misc_v, geneve_opt_len,
6215                  MLX5_GENEVE_OPTLEN_VAL(gbhdr_v) &
6216                  MLX5_GENEVE_OPTLEN_VAL(gbhdr_m));
6217 }
6218
6219 /**
6220  * Add MPLS item to matcher and to the value.
6221  *
6222  * @param[in, out] matcher
6223  *   Flow matcher.
6224  * @param[in, out] key
6225  *   Flow matcher value.
6226  * @param[in] item
6227  *   Flow pattern to translate.
6228  * @param[in] prev_layer
6229  *   The protocol layer indicated in previous item.
6230  * @param[in] inner
6231  *   Item is inner pattern.
6232  */
6233 static void
6234 flow_dv_translate_item_mpls(void *matcher, void *key,
6235                             const struct rte_flow_item *item,
6236                             uint64_t prev_layer,
6237                             int inner)
6238 {
6239         const uint32_t *in_mpls_m = item->mask;
6240         const uint32_t *in_mpls_v = item->spec;
6241         uint32_t *out_mpls_m = 0;
6242         uint32_t *out_mpls_v = 0;
6243         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
6244         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
6245         void *misc2_m = MLX5_ADDR_OF(fte_match_param, matcher,
6246                                      misc_parameters_2);
6247         void *misc2_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_2);
6248         void *headers_m = MLX5_ADDR_OF(fte_match_param, matcher, outer_headers);
6249         void *headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6250
6251         switch (prev_layer) {
6252         case MLX5_FLOW_LAYER_OUTER_L4_UDP:
6253                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xffff);
6254                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
6255                          MLX5_UDP_PORT_MPLS);
6256                 break;
6257         case MLX5_FLOW_LAYER_GRE:
6258                 MLX5_SET(fte_match_set_misc, misc_m, gre_protocol, 0xffff);
6259                 MLX5_SET(fte_match_set_misc, misc_v, gre_protocol,
6260                          RTE_ETHER_TYPE_MPLS);
6261                 break;
6262         default:
6263                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
6264                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
6265                          IPPROTO_MPLS);
6266                 break;
6267         }
6268         if (!in_mpls_v)
6269                 return;
6270         if (!in_mpls_m)
6271                 in_mpls_m = (const uint32_t *)&rte_flow_item_mpls_mask;
6272         switch (prev_layer) {
6273         case MLX5_FLOW_LAYER_OUTER_L4_UDP:
6274                 out_mpls_m =
6275                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_m,
6276                                                  outer_first_mpls_over_udp);
6277                 out_mpls_v =
6278                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_v,
6279                                                  outer_first_mpls_over_udp);
6280                 break;
6281         case MLX5_FLOW_LAYER_GRE:
6282                 out_mpls_m =
6283                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_m,
6284                                                  outer_first_mpls_over_gre);
6285                 out_mpls_v =
6286                         (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2, misc2_v,
6287                                                  outer_first_mpls_over_gre);
6288                 break;
6289         default:
6290                 /* Inner MPLS not over GRE is not supported. */
6291                 if (!inner) {
6292                         out_mpls_m =
6293                                 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2,
6294                                                          misc2_m,
6295                                                          outer_first_mpls);
6296                         out_mpls_v =
6297                                 (uint32_t *)MLX5_ADDR_OF(fte_match_set_misc2,
6298                                                          misc2_v,
6299                                                          outer_first_mpls);
6300                 }
6301                 break;
6302         }
6303         if (out_mpls_m && out_mpls_v) {
6304                 *out_mpls_m = *in_mpls_m;
6305                 *out_mpls_v = *in_mpls_v & *in_mpls_m;
6306         }
6307 }
6308
6309 /**
6310  * Add metadata register item to matcher
6311  *
6312  * @param[in, out] matcher
6313  *   Flow matcher.
6314  * @param[in, out] key
6315  *   Flow matcher value.
6316  * @param[in] reg_type
6317  *   Type of device metadata register
6318  * @param[in] value
6319  *   Register value
6320  * @param[in] mask
6321  *   Register mask
6322  */
6323 static void
6324 flow_dv_match_meta_reg(void *matcher, void *key,
6325                        enum modify_reg reg_type,
6326                        uint32_t data, uint32_t mask)
6327 {
6328         void *misc2_m =
6329                 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters_2);
6330         void *misc2_v =
6331                 MLX5_ADDR_OF(fte_match_param, key, misc_parameters_2);
6332         uint32_t temp;
6333
6334         data &= mask;
6335         switch (reg_type) {
6336         case REG_A:
6337                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_a, mask);
6338                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_a, data);
6339                 break;
6340         case REG_B:
6341                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_b, mask);
6342                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_b, data);
6343                 break;
6344         case REG_C_0:
6345                 /*
6346                  * The metadata register C0 field might be divided into
6347                  * source vport index and META item value, we should set
6348                  * this field according to specified mask, not as whole one.
6349                  */
6350                 temp = MLX5_GET(fte_match_set_misc2, misc2_m, metadata_reg_c_0);
6351                 temp |= mask;
6352                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_0, temp);
6353                 temp = MLX5_GET(fte_match_set_misc2, misc2_v, metadata_reg_c_0);
6354                 temp &= ~mask;
6355                 temp |= data;
6356                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_0, temp);
6357                 break;
6358         case REG_C_1:
6359                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_1, mask);
6360                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_1, data);
6361                 break;
6362         case REG_C_2:
6363                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_2, mask);
6364                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_2, data);
6365                 break;
6366         case REG_C_3:
6367                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_3, mask);
6368                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_3, data);
6369                 break;
6370         case REG_C_4:
6371                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_4, mask);
6372                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_4, data);
6373                 break;
6374         case REG_C_5:
6375                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_5, mask);
6376                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_5, data);
6377                 break;
6378         case REG_C_6:
6379                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_6, mask);
6380                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_6, data);
6381                 break;
6382         case REG_C_7:
6383                 MLX5_SET(fte_match_set_misc2, misc2_m, metadata_reg_c_7, mask);
6384                 MLX5_SET(fte_match_set_misc2, misc2_v, metadata_reg_c_7, data);
6385                 break;
6386         default:
6387                 MLX5_ASSERT(false);
6388                 break;
6389         }
6390 }
6391
6392 /**
6393  * Add MARK item to matcher
6394  *
6395  * @param[in] dev
6396  *   The device to configure through.
6397  * @param[in, out] matcher
6398  *   Flow matcher.
6399  * @param[in, out] key
6400  *   Flow matcher value.
6401  * @param[in] item
6402  *   Flow pattern to translate.
6403  */
6404 static void
6405 flow_dv_translate_item_mark(struct rte_eth_dev *dev,
6406                             void *matcher, void *key,
6407                             const struct rte_flow_item *item)
6408 {
6409         struct mlx5_priv *priv = dev->data->dev_private;
6410         const struct rte_flow_item_mark *mark;
6411         uint32_t value;
6412         uint32_t mask;
6413
6414         mark = item->mask ? (const void *)item->mask :
6415                             &rte_flow_item_mark_mask;
6416         mask = mark->id & priv->sh->dv_mark_mask;
6417         mark = (const void *)item->spec;
6418         MLX5_ASSERT(mark);
6419         value = mark->id & priv->sh->dv_mark_mask & mask;
6420         if (mask) {
6421                 enum modify_reg reg;
6422
6423                 /* Get the metadata register index for the mark. */
6424                 reg = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, NULL);
6425                 MLX5_ASSERT(reg > 0);
6426                 if (reg == REG_C_0) {
6427                         struct mlx5_priv *priv = dev->data->dev_private;
6428                         uint32_t msk_c0 = priv->sh->dv_regc0_mask;
6429                         uint32_t shl_c0 = rte_bsf32(msk_c0);
6430
6431                         mask &= msk_c0;
6432                         mask <<= shl_c0;
6433                         value <<= shl_c0;
6434                 }
6435                 flow_dv_match_meta_reg(matcher, key, reg, value, mask);
6436         }
6437 }
6438
6439 /**
6440  * Add META item to matcher
6441  *
6442  * @param[in] dev
6443  *   The devich to configure through.
6444  * @param[in, out] matcher
6445  *   Flow matcher.
6446  * @param[in, out] key
6447  *   Flow matcher value.
6448  * @param[in] attr
6449  *   Attributes of flow that includes this item.
6450  * @param[in] item
6451  *   Flow pattern to translate.
6452  */
6453 static void
6454 flow_dv_translate_item_meta(struct rte_eth_dev *dev,
6455                             void *matcher, void *key,
6456                             const struct rte_flow_attr *attr,
6457                             const struct rte_flow_item *item)
6458 {
6459         const struct rte_flow_item_meta *meta_m;
6460         const struct rte_flow_item_meta *meta_v;
6461
6462         meta_m = (const void *)item->mask;
6463         if (!meta_m)
6464                 meta_m = &rte_flow_item_meta_mask;
6465         meta_v = (const void *)item->spec;
6466         if (meta_v) {
6467                 int reg;
6468                 uint32_t value = meta_v->data;
6469                 uint32_t mask = meta_m->data;
6470
6471                 reg = flow_dv_get_metadata_reg(dev, attr, NULL);
6472                 if (reg < 0)
6473                         return;
6474                 /*
6475                  * In datapath code there is no endianness
6476                  * coversions for perfromance reasons, all
6477                  * pattern conversions are done in rte_flow.
6478                  */
6479                 value = rte_cpu_to_be_32(value);
6480                 mask = rte_cpu_to_be_32(mask);
6481                 if (reg == REG_C_0) {
6482                         struct mlx5_priv *priv = dev->data->dev_private;
6483                         uint32_t msk_c0 = priv->sh->dv_regc0_mask;
6484                         uint32_t shl_c0 = rte_bsf32(msk_c0);
6485 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
6486                         uint32_t shr_c0 = __builtin_clz(priv->sh->dv_meta_mask);
6487
6488                         value >>= shr_c0;
6489                         mask >>= shr_c0;
6490 #endif
6491                         value <<= shl_c0;
6492                         mask <<= shl_c0;
6493                         MLX5_ASSERT(msk_c0);
6494                         MLX5_ASSERT(!(~msk_c0 & mask));
6495                 }
6496                 flow_dv_match_meta_reg(matcher, key, reg, value, mask);
6497         }
6498 }
6499
6500 /**
6501  * Add vport metadata Reg C0 item to matcher
6502  *
6503  * @param[in, out] matcher
6504  *   Flow matcher.
6505  * @param[in, out] key
6506  *   Flow matcher value.
6507  * @param[in] reg
6508  *   Flow pattern to translate.
6509  */
6510 static void
6511 flow_dv_translate_item_meta_vport(void *matcher, void *key,
6512                                   uint32_t value, uint32_t mask)
6513 {
6514         flow_dv_match_meta_reg(matcher, key, REG_C_0, value, mask);
6515 }
6516
6517 /**
6518  * Add tag item to matcher
6519  *
6520  * @param[in] dev
6521  *   The devich to configure through.
6522  * @param[in, out] matcher
6523  *   Flow matcher.
6524  * @param[in, out] key
6525  *   Flow matcher value.
6526  * @param[in] item
6527  *   Flow pattern to translate.
6528  */
6529 static void
6530 flow_dv_translate_mlx5_item_tag(struct rte_eth_dev *dev,
6531                                 void *matcher, void *key,
6532                                 const struct rte_flow_item *item)
6533 {
6534         const struct mlx5_rte_flow_item_tag *tag_v = item->spec;
6535         const struct mlx5_rte_flow_item_tag *tag_m = item->mask;
6536         uint32_t mask, value;
6537
6538         MLX5_ASSERT(tag_v);
6539         value = tag_v->data;
6540         mask = tag_m ? tag_m->data : UINT32_MAX;
6541         if (tag_v->id == REG_C_0) {
6542                 struct mlx5_priv *priv = dev->data->dev_private;
6543                 uint32_t msk_c0 = priv->sh->dv_regc0_mask;
6544                 uint32_t shl_c0 = rte_bsf32(msk_c0);
6545
6546                 mask &= msk_c0;
6547                 mask <<= shl_c0;
6548                 value <<= shl_c0;
6549         }
6550         flow_dv_match_meta_reg(matcher, key, tag_v->id, value, mask);
6551 }
6552
6553 /**
6554  * Add TAG item to matcher
6555  *
6556  * @param[in] dev
6557  *   The devich to configure through.
6558  * @param[in, out] matcher
6559  *   Flow matcher.
6560  * @param[in, out] key
6561  *   Flow matcher value.
6562  * @param[in] item
6563  *   Flow pattern to translate.
6564  */
6565 static void
6566 flow_dv_translate_item_tag(struct rte_eth_dev *dev,
6567                            void *matcher, void *key,
6568                            const struct rte_flow_item *item)
6569 {
6570         const struct rte_flow_item_tag *tag_v = item->spec;
6571         const struct rte_flow_item_tag *tag_m = item->mask;
6572         enum modify_reg reg;
6573
6574         MLX5_ASSERT(tag_v);
6575         tag_m = tag_m ? tag_m : &rte_flow_item_tag_mask;
6576         /* Get the metadata register index for the tag. */
6577         reg = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, tag_v->index, NULL);
6578         MLX5_ASSERT(reg > 0);
6579         flow_dv_match_meta_reg(matcher, key, reg, tag_v->data, tag_m->data);
6580 }
6581
6582 /**
6583  * Add source vport match to the specified matcher.
6584  *
6585  * @param[in, out] matcher
6586  *   Flow matcher.
6587  * @param[in, out] key
6588  *   Flow matcher value.
6589  * @param[in] port
6590  *   Source vport value to match
6591  * @param[in] mask
6592  *   Mask
6593  */
6594 static void
6595 flow_dv_translate_item_source_vport(void *matcher, void *key,
6596                                     int16_t port, uint16_t mask)
6597 {
6598         void *misc_m = MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
6599         void *misc_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
6600
6601         MLX5_SET(fte_match_set_misc, misc_m, source_port, mask);
6602         MLX5_SET(fte_match_set_misc, misc_v, source_port, port);
6603 }
6604
6605 /**
6606  * Translate port-id item to eswitch match on  port-id.
6607  *
6608  * @param[in] dev
6609  *   The devich to configure through.
6610  * @param[in, out] matcher
6611  *   Flow matcher.
6612  * @param[in, out] key
6613  *   Flow matcher value.
6614  * @param[in] item
6615  *   Flow pattern to translate.
6616  *
6617  * @return
6618  *   0 on success, a negative errno value otherwise.
6619  */
6620 static int
6621 flow_dv_translate_item_port_id(struct rte_eth_dev *dev, void *matcher,
6622                                void *key, const struct rte_flow_item *item)
6623 {
6624         const struct rte_flow_item_port_id *pid_m = item ? item->mask : NULL;
6625         const struct rte_flow_item_port_id *pid_v = item ? item->spec : NULL;
6626         struct mlx5_priv *priv;
6627         uint16_t mask, id;
6628
6629         mask = pid_m ? pid_m->id : 0xffff;
6630         id = pid_v ? pid_v->id : dev->data->port_id;
6631         priv = mlx5_port_to_eswitch_info(id, item == NULL);
6632         if (!priv)
6633                 return -rte_errno;
6634         /* Translate to vport field or to metadata, depending on mode. */
6635         if (priv->vport_meta_mask)
6636                 flow_dv_translate_item_meta_vport(matcher, key,
6637                                                   priv->vport_meta_tag,
6638                                                   priv->vport_meta_mask);
6639         else
6640                 flow_dv_translate_item_source_vport(matcher, key,
6641                                                     priv->vport_id, mask);
6642         return 0;
6643 }
6644
6645 /**
6646  * Add ICMP6 item to matcher and to the value.
6647  *
6648  * @param[in, out] matcher
6649  *   Flow matcher.
6650  * @param[in, out] key
6651  *   Flow matcher value.
6652  * @param[in] item
6653  *   Flow pattern to translate.
6654  * @param[in] inner
6655  *   Item is inner pattern.
6656  */
6657 static void
6658 flow_dv_translate_item_icmp6(void *matcher, void *key,
6659                               const struct rte_flow_item *item,
6660                               int inner)
6661 {
6662         const struct rte_flow_item_icmp6 *icmp6_m = item->mask;
6663         const struct rte_flow_item_icmp6 *icmp6_v = item->spec;
6664         void *headers_m;
6665         void *headers_v;
6666         void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
6667                                      misc_parameters_3);
6668         void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
6669         if (inner) {
6670                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6671                                          inner_headers);
6672                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6673         } else {
6674                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6675                                          outer_headers);
6676                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6677         }
6678         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xFF);
6679         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_ICMPV6);
6680         if (!icmp6_v)
6681                 return;
6682         if (!icmp6_m)
6683                 icmp6_m = &rte_flow_item_icmp6_mask;
6684         /*
6685          * Force flow only to match the non-fragmented IPv6 ICMPv6 packets.
6686          * If only the protocol is specified, no need to match the frag.
6687          */
6688         MLX5_SET(fte_match_set_lyr_2_4, headers_m, frag, 1);
6689         MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag, 0);
6690         MLX5_SET(fte_match_set_misc3, misc3_m, icmpv6_type, icmp6_m->type);
6691         MLX5_SET(fte_match_set_misc3, misc3_v, icmpv6_type,
6692                  icmp6_v->type & icmp6_m->type);
6693         MLX5_SET(fte_match_set_misc3, misc3_m, icmpv6_code, icmp6_m->code);
6694         MLX5_SET(fte_match_set_misc3, misc3_v, icmpv6_code,
6695                  icmp6_v->code & icmp6_m->code);
6696 }
6697
6698 /**
6699  * Add ICMP item to matcher and to the value.
6700  *
6701  * @param[in, out] matcher
6702  *   Flow matcher.
6703  * @param[in, out] key
6704  *   Flow matcher value.
6705  * @param[in] item
6706  *   Flow pattern to translate.
6707  * @param[in] inner
6708  *   Item is inner pattern.
6709  */
6710 static void
6711 flow_dv_translate_item_icmp(void *matcher, void *key,
6712                             const struct rte_flow_item *item,
6713                             int inner)
6714 {
6715         const struct rte_flow_item_icmp *icmp_m = item->mask;
6716         const struct rte_flow_item_icmp *icmp_v = item->spec;
6717         void *headers_m;
6718         void *headers_v;
6719         void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
6720                                      misc_parameters_3);
6721         void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
6722         if (inner) {
6723                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6724                                          inner_headers);
6725                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6726         } else {
6727                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6728                                          outer_headers);
6729                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6730         }
6731         MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xFF);
6732         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_ICMP);
6733         if (!icmp_v)
6734                 return;
6735         if (!icmp_m)
6736                 icmp_m = &rte_flow_item_icmp_mask;
6737         /*
6738          * Force flow only to match the non-fragmented IPv4 ICMP packets.
6739          * If only the protocol is specified, no need to match the frag.
6740          */
6741         MLX5_SET(fte_match_set_lyr_2_4, headers_m, frag, 1);
6742         MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag, 0);
6743         MLX5_SET(fte_match_set_misc3, misc3_m, icmp_type,
6744                  icmp_m->hdr.icmp_type);
6745         MLX5_SET(fte_match_set_misc3, misc3_v, icmp_type,
6746                  icmp_v->hdr.icmp_type & icmp_m->hdr.icmp_type);
6747         MLX5_SET(fte_match_set_misc3, misc3_m, icmp_code,
6748                  icmp_m->hdr.icmp_code);
6749         MLX5_SET(fte_match_set_misc3, misc3_v, icmp_code,
6750                  icmp_v->hdr.icmp_code & icmp_m->hdr.icmp_code);
6751 }
6752
6753 /**
6754  * Add GTP item to matcher and to the value.
6755  *
6756  * @param[in, out] matcher
6757  *   Flow matcher.
6758  * @param[in, out] key
6759  *   Flow matcher value.
6760  * @param[in] item
6761  *   Flow pattern to translate.
6762  * @param[in] inner
6763  *   Item is inner pattern.
6764  */
6765 static void
6766 flow_dv_translate_item_gtp(void *matcher, void *key,
6767                            const struct rte_flow_item *item, int inner)
6768 {
6769         const struct rte_flow_item_gtp *gtp_m = item->mask;
6770         const struct rte_flow_item_gtp *gtp_v = item->spec;
6771         void *headers_m;
6772         void *headers_v;
6773         void *misc3_m = MLX5_ADDR_OF(fte_match_param, matcher,
6774                                      misc_parameters_3);
6775         void *misc3_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_3);
6776         uint16_t dport = RTE_GTPU_UDP_PORT;
6777
6778         if (inner) {
6779                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6780                                          inner_headers);
6781                 headers_v = MLX5_ADDR_OF(fte_match_param, key, inner_headers);
6782         } else {
6783                 headers_m = MLX5_ADDR_OF(fte_match_param, matcher,
6784                                          outer_headers);
6785                 headers_v = MLX5_ADDR_OF(fte_match_param, key, outer_headers);
6786         }
6787         if (!MLX5_GET16(fte_match_set_lyr_2_4, headers_v, udp_dport)) {
6788                 MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xFFFF);
6789                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, dport);
6790         }
6791         if (!gtp_v)
6792                 return;
6793         if (!gtp_m)
6794                 gtp_m = &rte_flow_item_gtp_mask;
6795         MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_msg_type, gtp_m->msg_type);
6796         MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_msg_type,
6797                  gtp_v->msg_type & gtp_m->msg_type);
6798         MLX5_SET(fte_match_set_misc3, misc3_m, gtpu_teid,
6799                  rte_be_to_cpu_32(gtp_m->teid));
6800         MLX5_SET(fte_match_set_misc3, misc3_v, gtpu_teid,
6801                  rte_be_to_cpu_32(gtp_v->teid & gtp_m->teid));
6802 }
6803
6804 static uint32_t matcher_zero[MLX5_ST_SZ_DW(fte_match_param)] = { 0 };
6805
6806 #define HEADER_IS_ZERO(match_criteria, headers)                              \
6807         !(memcmp(MLX5_ADDR_OF(fte_match_param, match_criteria, headers),     \
6808                  matcher_zero, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
6809
6810 /**
6811  * Calculate flow matcher enable bitmap.
6812  *
6813  * @param match_criteria
6814  *   Pointer to flow matcher criteria.
6815  *
6816  * @return
6817  *   Bitmap of enabled fields.
6818  */
6819 static uint8_t
6820 flow_dv_matcher_enable(uint32_t *match_criteria)
6821 {
6822         uint8_t match_criteria_enable;
6823
6824         match_criteria_enable =
6825                 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
6826                 MLX5_MATCH_CRITERIA_ENABLE_OUTER_BIT;
6827         match_criteria_enable |=
6828                 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
6829                 MLX5_MATCH_CRITERIA_ENABLE_MISC_BIT;
6830         match_criteria_enable |=
6831                 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
6832                 MLX5_MATCH_CRITERIA_ENABLE_INNER_BIT;
6833         match_criteria_enable |=
6834                 (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
6835                 MLX5_MATCH_CRITERIA_ENABLE_MISC2_BIT;
6836         match_criteria_enable |=
6837                 (!HEADER_IS_ZERO(match_criteria, misc_parameters_3)) <<
6838                 MLX5_MATCH_CRITERIA_ENABLE_MISC3_BIT;
6839         return match_criteria_enable;
6840 }
6841
6842
6843 /**
6844  * Get a flow table.
6845  *
6846  * @param[in, out] dev
6847  *   Pointer to rte_eth_dev structure.
6848  * @param[in] table_id
6849  *   Table id to use.
6850  * @param[in] egress
6851  *   Direction of the table.
6852  * @param[in] transfer
6853  *   E-Switch or NIC flow.
6854  * @param[out] error
6855  *   pointer to error structure.
6856  *
6857  * @return
6858  *   Returns tables resource based on the index, NULL in case of failed.
6859  */
6860 static struct mlx5_flow_tbl_resource *
6861 flow_dv_tbl_resource_get(struct rte_eth_dev *dev,
6862                          uint32_t table_id, uint8_t egress,
6863                          uint8_t transfer,
6864                          struct rte_flow_error *error)
6865 {
6866         struct mlx5_priv *priv = dev->data->dev_private;
6867         struct mlx5_ibv_shared *sh = priv->sh;
6868         struct mlx5_flow_tbl_resource *tbl;
6869         union mlx5_flow_tbl_key table_key = {
6870                 {
6871                         .table_id = table_id,
6872                         .reserved = 0,
6873                         .domain = !!transfer,
6874                         .direction = !!egress,
6875                 }
6876         };
6877         struct mlx5_hlist_entry *pos = mlx5_hlist_lookup(sh->flow_tbls,
6878                                                          table_key.v64);
6879         struct mlx5_flow_tbl_data_entry *tbl_data;
6880         uint32_t idx = 0;
6881         int ret;
6882         void *domain;
6883
6884         if (pos) {
6885                 tbl_data = container_of(pos, struct mlx5_flow_tbl_data_entry,
6886                                         entry);
6887                 tbl = &tbl_data->tbl;
6888                 rte_atomic32_inc(&tbl->refcnt);
6889                 return tbl;
6890         }
6891         tbl_data = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_JUMP], &idx);
6892         if (!tbl_data) {
6893                 rte_flow_error_set(error, ENOMEM,
6894                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6895                                    NULL,
6896                                    "cannot allocate flow table data entry");
6897                 return NULL;
6898         }
6899         tbl_data->idx = idx;
6900         tbl = &tbl_data->tbl;
6901         pos = &tbl_data->entry;
6902         if (transfer)
6903                 domain = sh->fdb_domain;
6904         else if (egress)
6905                 domain = sh->tx_domain;
6906         else
6907                 domain = sh->rx_domain;
6908         tbl->obj = mlx5_glue->dr_create_flow_tbl(domain, table_id);
6909         if (!tbl->obj) {
6910                 rte_flow_error_set(error, ENOMEM,
6911                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6912                                    NULL, "cannot create flow table object");
6913                 mlx5_ipool_free(sh->ipool[MLX5_IPOOL_JUMP], idx);
6914                 return NULL;
6915         }
6916         /*
6917          * No multi-threads now, but still better to initialize the reference
6918          * count before insert it into the hash list.
6919          */
6920         rte_atomic32_init(&tbl->refcnt);
6921         /* Jump action reference count is initialized here. */
6922         rte_atomic32_init(&tbl_data->jump.refcnt);
6923         pos->key = table_key.v64;
6924         ret = mlx5_hlist_insert(sh->flow_tbls, pos);
6925         if (ret < 0) {
6926                 rte_flow_error_set(error, -ret,
6927                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
6928                                    "cannot insert flow table data entry");
6929                 mlx5_glue->dr_destroy_flow_tbl(tbl->obj);
6930                 mlx5_ipool_free(sh->ipool[MLX5_IPOOL_JUMP], idx);
6931         }
6932         rte_atomic32_inc(&tbl->refcnt);
6933         return tbl;
6934 }
6935
6936 /**
6937  * Release a flow table.
6938  *
6939  * @param[in] dev
6940  *   Pointer to rte_eth_dev structure.
6941  * @param[in] tbl
6942  *   Table resource to be released.
6943  *
6944  * @return
6945  *   Returns 0 if table was released, else return 1;
6946  */
6947 static int
6948 flow_dv_tbl_resource_release(struct rte_eth_dev *dev,
6949                              struct mlx5_flow_tbl_resource *tbl)
6950 {
6951         struct mlx5_priv *priv = dev->data->dev_private;
6952         struct mlx5_ibv_shared *sh = priv->sh;
6953         struct mlx5_flow_tbl_data_entry *tbl_data =
6954                 container_of(tbl, struct mlx5_flow_tbl_data_entry, tbl);
6955
6956         if (!tbl)
6957                 return 0;
6958         if (rte_atomic32_dec_and_test(&tbl->refcnt)) {
6959                 struct mlx5_hlist_entry *pos = &tbl_data->entry;
6960
6961                 mlx5_glue->dr_destroy_flow_tbl(tbl->obj);
6962                 tbl->obj = NULL;
6963                 /* remove the entry from the hash list and free memory. */
6964                 mlx5_hlist_remove(sh->flow_tbls, pos);
6965                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_JUMP],
6966                                 tbl_data->idx);
6967                 return 0;
6968         }
6969         return 1;
6970 }
6971
6972 /**
6973  * Register the flow matcher.
6974  *
6975  * @param[in, out] dev
6976  *   Pointer to rte_eth_dev structure.
6977  * @param[in, out] matcher
6978  *   Pointer to flow matcher.
6979  * @param[in, out] key
6980  *   Pointer to flow table key.
6981  * @parm[in, out] dev_flow
6982  *   Pointer to the dev_flow.
6983  * @param[out] error
6984  *   pointer to error structure.
6985  *
6986  * @return
6987  *   0 on success otherwise -errno and errno is set.
6988  */
6989 static int
6990 flow_dv_matcher_register(struct rte_eth_dev *dev,
6991                          struct mlx5_flow_dv_matcher *matcher,
6992                          union mlx5_flow_tbl_key *key,
6993                          struct mlx5_flow *dev_flow,
6994                          struct rte_flow_error *error)
6995 {
6996         struct mlx5_priv *priv = dev->data->dev_private;
6997         struct mlx5_ibv_shared *sh = priv->sh;
6998         struct mlx5_flow_dv_matcher *cache_matcher;
6999         struct mlx5dv_flow_matcher_attr dv_attr = {
7000                 .type = IBV_FLOW_ATTR_NORMAL,
7001                 .match_mask = (void *)&matcher->mask,
7002         };
7003         struct mlx5_flow_tbl_resource *tbl;
7004         struct mlx5_flow_tbl_data_entry *tbl_data;
7005
7006         tbl = flow_dv_tbl_resource_get(dev, key->table_id, key->direction,
7007                                        key->domain, error);
7008         if (!tbl)
7009                 return -rte_errno;      /* No need to refill the error info */
7010         tbl_data = container_of(tbl, struct mlx5_flow_tbl_data_entry, tbl);
7011         /* Lookup from cache. */
7012         LIST_FOREACH(cache_matcher, &tbl_data->matchers, next) {
7013                 if (matcher->crc == cache_matcher->crc &&
7014                     matcher->priority == cache_matcher->priority &&
7015                     !memcmp((const void *)matcher->mask.buf,
7016                             (const void *)cache_matcher->mask.buf,
7017                             cache_matcher->mask.size)) {
7018                         DRV_LOG(DEBUG,
7019                                 "%s group %u priority %hd use %s "
7020                                 "matcher %p: refcnt %d++",
7021                                 key->domain ? "FDB" : "NIC", key->table_id,
7022                                 cache_matcher->priority,
7023                                 key->direction ? "tx" : "rx",
7024                                 (void *)cache_matcher,
7025                                 rte_atomic32_read(&cache_matcher->refcnt));
7026                         rte_atomic32_inc(&cache_matcher->refcnt);
7027                         dev_flow->handle->dvh.matcher = cache_matcher;
7028                         /* old matcher should not make the table ref++. */
7029                         flow_dv_tbl_resource_release(dev, tbl);
7030                         return 0;
7031                 }
7032         }
7033         /* Register new matcher. */
7034         cache_matcher = rte_calloc(__func__, 1, sizeof(*cache_matcher), 0);
7035         if (!cache_matcher) {
7036                 flow_dv_tbl_resource_release(dev, tbl);
7037                 return rte_flow_error_set(error, ENOMEM,
7038                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
7039                                           "cannot allocate matcher memory");
7040         }
7041         *cache_matcher = *matcher;
7042         dv_attr.match_criteria_enable =
7043                 flow_dv_matcher_enable(cache_matcher->mask.buf);
7044         dv_attr.priority = matcher->priority;
7045         if (key->direction)
7046                 dv_attr.flags |= IBV_FLOW_ATTR_FLAGS_EGRESS;
7047         cache_matcher->matcher_object =
7048                 mlx5_glue->dv_create_flow_matcher(sh->ctx, &dv_attr, tbl->obj);
7049         if (!cache_matcher->matcher_object) {
7050                 rte_free(cache_matcher);
7051 #ifdef HAVE_MLX5DV_DR
7052                 flow_dv_tbl_resource_release(dev, tbl);
7053 #endif
7054                 return rte_flow_error_set(error, ENOMEM,
7055                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7056                                           NULL, "cannot create matcher");
7057         }
7058         /* Save the table information */
7059         cache_matcher->tbl = tbl;
7060         rte_atomic32_init(&cache_matcher->refcnt);
7061         /* only matcher ref++, table ref++ already done above in get API. */
7062         rte_atomic32_inc(&cache_matcher->refcnt);
7063         LIST_INSERT_HEAD(&tbl_data->matchers, cache_matcher, next);
7064         dev_flow->handle->dvh.matcher = cache_matcher;
7065         DRV_LOG(DEBUG, "%s group %u priority %hd new %s matcher %p: refcnt %d",
7066                 key->domain ? "FDB" : "NIC", key->table_id,
7067                 cache_matcher->priority,
7068                 key->direction ? "tx" : "rx", (void *)cache_matcher,
7069                 rte_atomic32_read(&cache_matcher->refcnt));
7070         return 0;
7071 }
7072
7073 /**
7074  * Find existing tag resource or create and register a new one.
7075  *
7076  * @param dev[in, out]
7077  *   Pointer to rte_eth_dev structure.
7078  * @param[in, out] tag_be24
7079  *   Tag value in big endian then R-shift 8.
7080  * @parm[in, out] dev_flow
7081  *   Pointer to the dev_flow.
7082  * @param[out] error
7083  *   pointer to error structure.
7084  *
7085  * @return
7086  *   0 on success otherwise -errno and errno is set.
7087  */
7088 static int
7089 flow_dv_tag_resource_register
7090                         (struct rte_eth_dev *dev,
7091                          uint32_t tag_be24,
7092                          struct mlx5_flow *dev_flow,
7093                          struct rte_flow_error *error)
7094 {
7095         struct mlx5_priv *priv = dev->data->dev_private;
7096         struct mlx5_ibv_shared *sh = priv->sh;
7097         struct mlx5_flow_dv_tag_resource *cache_resource;
7098         struct mlx5_hlist_entry *entry;
7099
7100         /* Lookup a matching resource from cache. */
7101         entry = mlx5_hlist_lookup(sh->tag_table, (uint64_t)tag_be24);
7102         if (entry) {
7103                 cache_resource = container_of
7104                         (entry, struct mlx5_flow_dv_tag_resource, entry);
7105                 rte_atomic32_inc(&cache_resource->refcnt);
7106                 dev_flow->handle->dvh.tag_resource = cache_resource->idx;
7107                 dev_flow->dv.tag_resource = cache_resource;
7108                 DRV_LOG(DEBUG, "cached tag resource %p: refcnt now %d++",
7109                         (void *)cache_resource,
7110                         rte_atomic32_read(&cache_resource->refcnt));
7111                 return 0;
7112         }
7113         /* Register new resource. */
7114         cache_resource = mlx5_ipool_zmalloc(sh->ipool[MLX5_IPOOL_TAG],
7115                                        &dev_flow->handle->dvh.tag_resource);
7116         if (!cache_resource)
7117                 return rte_flow_error_set(error, ENOMEM,
7118                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
7119                                           "cannot allocate resource memory");
7120         cache_resource->entry.key = (uint64_t)tag_be24;
7121         cache_resource->action = mlx5_glue->dv_create_flow_action_tag(tag_be24);
7122         if (!cache_resource->action) {
7123                 rte_free(cache_resource);
7124                 return rte_flow_error_set(error, ENOMEM,
7125                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7126                                           NULL, "cannot create action");
7127         }
7128         rte_atomic32_init(&cache_resource->refcnt);
7129         rte_atomic32_inc(&cache_resource->refcnt);
7130         if (mlx5_hlist_insert(sh->tag_table, &cache_resource->entry)) {
7131                 mlx5_glue->destroy_flow_action(cache_resource->action);
7132                 rte_free(cache_resource);
7133                 return rte_flow_error_set(error, EEXIST,
7134                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7135                                           NULL, "cannot insert tag");
7136         }
7137         dev_flow->dv.tag_resource = cache_resource;
7138         DRV_LOG(DEBUG, "new tag resource %p: refcnt now %d++",
7139                 (void *)cache_resource,
7140                 rte_atomic32_read(&cache_resource->refcnt));
7141         return 0;
7142 }
7143
7144 /**
7145  * Release the tag.
7146  *
7147  * @param dev
7148  *   Pointer to Ethernet device.
7149  * @param tag_idx
7150  *   Tag index.
7151  *
7152  * @return
7153  *   1 while a reference on it exists, 0 when freed.
7154  */
7155 static int
7156 flow_dv_tag_release(struct rte_eth_dev *dev,
7157                     uint32_t tag_idx)
7158 {
7159         struct mlx5_priv *priv = dev->data->dev_private;
7160         struct mlx5_ibv_shared *sh = priv->sh;
7161         struct mlx5_flow_dv_tag_resource *tag;
7162
7163         tag = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_TAG], tag_idx);
7164         if (!tag)
7165                 return 0;
7166         DRV_LOG(DEBUG, "port %u tag %p: refcnt %d--",
7167                 dev->data->port_id, (void *)tag,
7168                 rte_atomic32_read(&tag->refcnt));
7169         if (rte_atomic32_dec_and_test(&tag->refcnt)) {
7170                 claim_zero(mlx5_glue->destroy_flow_action(tag->action));
7171                 mlx5_hlist_remove(sh->tag_table, &tag->entry);
7172                 DRV_LOG(DEBUG, "port %u tag %p: removed",
7173                         dev->data->port_id, (void *)tag);
7174                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_TAG], tag_idx);
7175                 return 0;
7176         }
7177         return 1;
7178 }
7179
7180 /**
7181  * Translate port ID action to vport.
7182  *
7183  * @param[in] dev
7184  *   Pointer to rte_eth_dev structure.
7185  * @param[in] action
7186  *   Pointer to the port ID action.
7187  * @param[out] dst_port_id
7188  *   The target port ID.
7189  * @param[out] error
7190  *   Pointer to the error structure.
7191  *
7192  * @return
7193  *   0 on success, a negative errno value otherwise and rte_errno is set.
7194  */
7195 static int
7196 flow_dv_translate_action_port_id(struct rte_eth_dev *dev,
7197                                  const struct rte_flow_action *action,
7198                                  uint32_t *dst_port_id,
7199                                  struct rte_flow_error *error)
7200 {
7201         uint32_t port;
7202         struct mlx5_priv *priv;
7203         const struct rte_flow_action_port_id *conf =
7204                         (const struct rte_flow_action_port_id *)action->conf;
7205
7206         port = conf->original ? dev->data->port_id : conf->id;
7207         priv = mlx5_port_to_eswitch_info(port, false);
7208         if (!priv)
7209                 return rte_flow_error_set(error, -rte_errno,
7210                                           RTE_FLOW_ERROR_TYPE_ACTION,
7211                                           NULL,
7212                                           "No eswitch info was found for port");
7213 #ifdef HAVE_MLX5DV_DR_DEVX_PORT
7214         /*
7215          * This parameter is transferred to
7216          * mlx5dv_dr_action_create_dest_ib_port().
7217          */
7218         *dst_port_id = priv->ibv_port;
7219 #else
7220         /*
7221          * Legacy mode, no LAG configurations is supported.
7222          * This parameter is transferred to
7223          * mlx5dv_dr_action_create_dest_vport().
7224          */
7225         *dst_port_id = priv->vport_id;
7226 #endif
7227         return 0;
7228 }
7229
7230 /**
7231  * Add Tx queue matcher
7232  *
7233  * @param[in] dev
7234  *   Pointer to the dev struct.
7235  * @param[in, out] matcher
7236  *   Flow matcher.
7237  * @param[in, out] key
7238  *   Flow matcher value.
7239  * @param[in] item
7240  *   Flow pattern to translate.
7241  * @param[in] inner
7242  *   Item is inner pattern.
7243  */
7244 static void
7245 flow_dv_translate_item_tx_queue(struct rte_eth_dev *dev,
7246                                 void *matcher, void *key,
7247                                 const struct rte_flow_item *item)
7248 {
7249         const struct mlx5_rte_flow_item_tx_queue *queue_m;
7250         const struct mlx5_rte_flow_item_tx_queue *queue_v;
7251         void *misc_m =
7252                 MLX5_ADDR_OF(fte_match_param, matcher, misc_parameters);
7253         void *misc_v =
7254                 MLX5_ADDR_OF(fte_match_param, key, misc_parameters);
7255         struct mlx5_txq_ctrl *txq;
7256         uint32_t queue;
7257
7258
7259         queue_m = (const void *)item->mask;
7260         if (!queue_m)
7261                 return;
7262         queue_v = (const void *)item->spec;
7263         if (!queue_v)
7264                 return;
7265         txq = mlx5_txq_get(dev, queue_v->queue);
7266         if (!txq)
7267                 return;
7268         queue = txq->obj->sq->id;
7269         MLX5_SET(fte_match_set_misc, misc_m, source_sqn, queue_m->queue);
7270         MLX5_SET(fte_match_set_misc, misc_v, source_sqn,
7271                  queue & queue_m->queue);
7272         mlx5_txq_release(dev, queue_v->queue);
7273 }
7274
7275 /**
7276  * Set the hash fields according to the @p flow information.
7277  *
7278  * @param[in] dev_flow
7279  *   Pointer to the mlx5_flow.
7280  */
7281 static void
7282 flow_dv_hashfields_set(struct mlx5_flow *dev_flow)
7283 {
7284         struct rte_flow *flow = dev_flow->flow;
7285         uint64_t items = dev_flow->handle->layers;
7286         int rss_inner = 0;
7287         uint64_t rss_types = rte_eth_rss_hf_refine(flow->rss.types);
7288
7289         dev_flow->hash_fields = 0;
7290 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
7291         if (flow->rss.level >= 2) {
7292                 dev_flow->hash_fields |= IBV_RX_HASH_INNER;
7293                 rss_inner = 1;
7294         }
7295 #endif
7296         if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L3_IPV4)) ||
7297             (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L3_IPV4))) {
7298                 if (rss_types & MLX5_IPV4_LAYER_TYPES) {
7299                         if (rss_types & ETH_RSS_L3_SRC_ONLY)
7300                                 dev_flow->hash_fields |= IBV_RX_HASH_SRC_IPV4;
7301                         else if (rss_types & ETH_RSS_L3_DST_ONLY)
7302                                 dev_flow->hash_fields |= IBV_RX_HASH_DST_IPV4;
7303                         else
7304                                 dev_flow->hash_fields |= MLX5_IPV4_IBV_RX_HASH;
7305                 }
7306         } else if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L3_IPV6)) ||
7307                    (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L3_IPV6))) {
7308                 if (rss_types & MLX5_IPV6_LAYER_TYPES) {
7309                         if (rss_types & ETH_RSS_L3_SRC_ONLY)
7310                                 dev_flow->hash_fields |= IBV_RX_HASH_SRC_IPV6;
7311                         else if (rss_types & ETH_RSS_L3_DST_ONLY)
7312                                 dev_flow->hash_fields |= IBV_RX_HASH_DST_IPV6;
7313                         else
7314                                 dev_flow->hash_fields |= MLX5_IPV6_IBV_RX_HASH;
7315                 }
7316         }
7317         if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L4_UDP)) ||
7318             (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L4_UDP))) {
7319                 if (rss_types & ETH_RSS_UDP) {
7320                         if (rss_types & ETH_RSS_L4_SRC_ONLY)
7321                                 dev_flow->hash_fields |=
7322                                                 IBV_RX_HASH_SRC_PORT_UDP;
7323                         else if (rss_types & ETH_RSS_L4_DST_ONLY)
7324                                 dev_flow->hash_fields |=
7325                                                 IBV_RX_HASH_DST_PORT_UDP;
7326                         else
7327                                 dev_flow->hash_fields |= MLX5_UDP_IBV_RX_HASH;
7328                 }
7329         } else if ((rss_inner && (items & MLX5_FLOW_LAYER_INNER_L4_TCP)) ||
7330                    (!rss_inner && (items & MLX5_FLOW_LAYER_OUTER_L4_TCP))) {
7331                 if (rss_types & ETH_RSS_TCP) {
7332                         if (rss_types & ETH_RSS_L4_SRC_ONLY)
7333                                 dev_flow->hash_fields |=
7334                                                 IBV_RX_HASH_SRC_PORT_TCP;
7335                         else if (rss_types & ETH_RSS_L4_DST_ONLY)
7336                                 dev_flow->hash_fields |=
7337                                                 IBV_RX_HASH_DST_PORT_TCP;
7338                         else
7339                                 dev_flow->hash_fields |= MLX5_TCP_IBV_RX_HASH;
7340                 }
7341         }
7342 }
7343
7344 /**
7345  * Fill the flow with DV spec, lock free
7346  * (mutex should be acquired by caller).
7347  *
7348  * @param[in] dev
7349  *   Pointer to rte_eth_dev structure.
7350  * @param[in, out] dev_flow
7351  *   Pointer to the sub flow.
7352  * @param[in] attr
7353  *   Pointer to the flow attributes.
7354  * @param[in] items
7355  *   Pointer to the list of items.
7356  * @param[in] actions
7357  *   Pointer to the list of actions.
7358  * @param[out] error
7359  *   Pointer to the error structure.
7360  *
7361  * @return
7362  *   0 on success, a negative errno value otherwise and rte_errno is set.
7363  */
7364 static int
7365 __flow_dv_translate(struct rte_eth_dev *dev,
7366                     struct mlx5_flow *dev_flow,
7367                     const struct rte_flow_attr *attr,
7368                     const struct rte_flow_item items[],
7369                     const struct rte_flow_action actions[],
7370                     struct rte_flow_error *error)
7371 {
7372         struct mlx5_priv *priv = dev->data->dev_private;
7373         struct mlx5_dev_config *dev_conf = &priv->config;
7374         struct rte_flow *flow = dev_flow->flow;
7375         struct mlx5_flow_handle *handle = dev_flow->handle;
7376         uint64_t item_flags = 0;
7377         uint64_t last_item = 0;
7378         uint64_t action_flags = 0;
7379         uint64_t priority = attr->priority;
7380         struct mlx5_flow_dv_matcher matcher = {
7381                 .mask = {
7382                         .size = sizeof(matcher.mask.buf),
7383                 },
7384         };
7385         int actions_n = 0;
7386         bool actions_end = false;
7387         union {
7388                 struct mlx5_flow_dv_modify_hdr_resource res;
7389                 uint8_t len[sizeof(struct mlx5_flow_dv_modify_hdr_resource) +
7390                             sizeof(struct mlx5_modification_cmd) *
7391                             (MLX5_MAX_MODIFY_NUM + 1)];
7392         } mhdr_dummy;
7393         struct mlx5_flow_dv_modify_hdr_resource *mhdr_res = &mhdr_dummy.res;
7394         union flow_dv_attr flow_attr = { .attr = 0 };
7395         uint32_t tag_be;
7396         union mlx5_flow_tbl_key tbl_key;
7397         uint32_t modify_action_position = UINT32_MAX;
7398         void *match_mask = matcher.mask.buf;
7399         void *match_value = dev_flow->dv.value.buf;
7400         uint8_t next_protocol = 0xff;
7401         struct rte_vlan_hdr vlan = { 0 };
7402         uint32_t table;
7403         int ret = 0;
7404
7405         mhdr_res->ft_type = attr->egress ? MLX5DV_FLOW_TABLE_TYPE_NIC_TX :
7406                                            MLX5DV_FLOW_TABLE_TYPE_NIC_RX;
7407         ret = mlx5_flow_group_to_table(attr, dev_flow->external, attr->group,
7408                                        !!priv->fdb_def_rule, &table, error);
7409         if (ret)
7410                 return ret;
7411         dev_flow->dv.group = table;
7412         if (attr->transfer)
7413                 mhdr_res->ft_type = MLX5DV_FLOW_TABLE_TYPE_FDB;
7414         if (priority == MLX5_FLOW_PRIO_RSVD)
7415                 priority = dev_conf->flow_prio - 1;
7416         /* number of actions must be set to 0 in case of dirty stack. */
7417         mhdr_res->actions_num = 0;
7418         for (; !actions_end ; actions++) {
7419                 const struct rte_flow_action_queue *queue;
7420                 const struct rte_flow_action_rss *rss;
7421                 const struct rte_flow_action *action = actions;
7422                 const struct rte_flow_action_count *count = action->conf;
7423                 const uint8_t *rss_key;
7424                 const struct rte_flow_action_jump *jump_data;
7425                 const struct rte_flow_action_meter *mtr;
7426                 struct mlx5_flow_tbl_resource *tbl;
7427                 uint32_t port_id = 0;
7428                 struct mlx5_flow_dv_port_id_action_resource port_id_resource;
7429                 int action_type = actions->type;
7430                 const struct rte_flow_action *found_action = NULL;
7431
7432                 switch (action_type) {
7433                 case RTE_FLOW_ACTION_TYPE_VOID:
7434                         break;
7435                 case RTE_FLOW_ACTION_TYPE_PORT_ID:
7436                         if (flow_dv_translate_action_port_id(dev, action,
7437                                                              &port_id, error))
7438                                 return -rte_errno;
7439                         memset(&port_id_resource, 0, sizeof(port_id_resource));
7440                         port_id_resource.port_id = port_id;
7441                         if (flow_dv_port_id_action_resource_register
7442                             (dev, &port_id_resource, dev_flow, error))
7443                                 return -rte_errno;
7444                         MLX5_ASSERT(!handle->port_id_action);
7445                         dev_flow->dv.actions[actions_n++] =
7446                                         dev_flow->dv.port_id_action->action;
7447                         action_flags |= MLX5_FLOW_ACTION_PORT_ID;
7448                         dev_flow->handle->fate_action = MLX5_FLOW_FATE_PORT_ID;
7449                         break;
7450                 case RTE_FLOW_ACTION_TYPE_FLAG:
7451                         action_flags |= MLX5_FLOW_ACTION_FLAG;
7452                         dev_flow->handle->mark = 1;
7453                         if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
7454                                 struct rte_flow_action_mark mark = {
7455                                         .id = MLX5_FLOW_MARK_DEFAULT,
7456                                 };
7457
7458                                 if (flow_dv_convert_action_mark(dev, &mark,
7459                                                                 mhdr_res,
7460                                                                 error))
7461                                         return -rte_errno;
7462                                 action_flags |= MLX5_FLOW_ACTION_MARK_EXT;
7463                                 break;
7464                         }
7465                         tag_be = mlx5_flow_mark_set(MLX5_FLOW_MARK_DEFAULT);
7466                         /*
7467                          * Only one FLAG or MARK is supported per device flow
7468                          * right now. So the pointer to the tag resource must be
7469                          * zero before the register process.
7470                          */
7471                         MLX5_ASSERT(!handle->dvh.tag_resource);
7472                         if (flow_dv_tag_resource_register(dev, tag_be,
7473                                                           dev_flow, error))
7474                                 return -rte_errno;
7475                         MLX5_ASSERT(dev_flow->dv.tag_resource);
7476                         dev_flow->dv.actions[actions_n++] =
7477                                         dev_flow->dv.tag_resource->action;
7478                         break;
7479                 case RTE_FLOW_ACTION_TYPE_MARK:
7480                         action_flags |= MLX5_FLOW_ACTION_MARK;
7481                         dev_flow->handle->mark = 1;
7482                         if (dev_conf->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
7483                                 const struct rte_flow_action_mark *mark =
7484                                         (const struct rte_flow_action_mark *)
7485                                                 actions->conf;
7486
7487                                 if (flow_dv_convert_action_mark(dev, mark,
7488                                                                 mhdr_res,
7489                                                                 error))
7490                                         return -rte_errno;
7491                                 action_flags |= MLX5_FLOW_ACTION_MARK_EXT;
7492                                 break;
7493                         }
7494                         /* Fall-through */
7495                 case MLX5_RTE_FLOW_ACTION_TYPE_MARK:
7496                         /* Legacy (non-extensive) MARK action. */
7497                         tag_be = mlx5_flow_mark_set
7498                               (((const struct rte_flow_action_mark *)
7499                                (actions->conf))->id);
7500                         MLX5_ASSERT(!handle->dvh.tag_resource);
7501                         if (flow_dv_tag_resource_register(dev, tag_be,
7502                                                           dev_flow, error))
7503                                 return -rte_errno;
7504                         MLX5_ASSERT(dev_flow->dv.tag_resource);
7505                         dev_flow->dv.actions[actions_n++] =
7506                                         dev_flow->dv.tag_resource->action;
7507                         break;
7508                 case RTE_FLOW_ACTION_TYPE_SET_META:
7509                         if (flow_dv_convert_action_set_meta
7510                                 (dev, mhdr_res, attr,
7511                                  (const struct rte_flow_action_set_meta *)
7512                                   actions->conf, error))
7513                                 return -rte_errno;
7514                         action_flags |= MLX5_FLOW_ACTION_SET_META;
7515                         break;
7516                 case RTE_FLOW_ACTION_TYPE_SET_TAG:
7517                         if (flow_dv_convert_action_set_tag
7518                                 (dev, mhdr_res,
7519                                  (const struct rte_flow_action_set_tag *)
7520                                   actions->conf, error))
7521                                 return -rte_errno;
7522                         action_flags |= MLX5_FLOW_ACTION_SET_TAG;
7523                         break;
7524                 case RTE_FLOW_ACTION_TYPE_DROP:
7525                         action_flags |= MLX5_FLOW_ACTION_DROP;
7526                         dev_flow->handle->fate_action = MLX5_FLOW_FATE_DROP;
7527                         break;
7528                 case RTE_FLOW_ACTION_TYPE_QUEUE:
7529                         MLX5_ASSERT(flow->rss.queue);
7530                         queue = actions->conf;
7531                         flow->rss.queue_num = 1;
7532                         (*flow->rss.queue)[0] = queue->index;
7533                         action_flags |= MLX5_FLOW_ACTION_QUEUE;
7534                         dev_flow->handle->fate_action = MLX5_FLOW_FATE_QUEUE;
7535                         break;
7536                 case RTE_FLOW_ACTION_TYPE_RSS:
7537                         MLX5_ASSERT(flow->rss.queue);
7538                         rss = actions->conf;
7539                         if (flow->rss.queue)
7540                                 memcpy((*flow->rss.queue), rss->queue,
7541                                        rss->queue_num * sizeof(uint16_t));
7542                         flow->rss.queue_num = rss->queue_num;
7543                         /* NULL RSS key indicates default RSS key. */
7544                         rss_key = !rss->key ? rss_hash_default_key : rss->key;
7545                         memcpy(flow->rss.key, rss_key, MLX5_RSS_HASH_KEY_LEN);
7546                         /*
7547                          * rss->level and rss.types should be set in advance
7548                          * when expanding items for RSS.
7549                          */
7550                         action_flags |= MLX5_FLOW_ACTION_RSS;
7551                         dev_flow->handle->fate_action = MLX5_FLOW_FATE_QUEUE;
7552                         break;
7553                 case RTE_FLOW_ACTION_TYPE_COUNT:
7554                         if (!dev_conf->devx) {
7555                                 rte_errno = ENOTSUP;
7556                                 goto cnt_err;
7557                         }
7558                         flow->counter = flow_dv_counter_alloc(dev,
7559                                                         count->shared,
7560                                                         count->id,
7561                                                         dev_flow->dv.group);
7562                         if (!flow->counter)
7563                                 goto cnt_err;
7564                         dev_flow->dv.actions[actions_n++] =
7565                                   (flow_dv_counter_get_by_idx(dev,
7566                                   flow->counter, NULL))->action;
7567                         action_flags |= MLX5_FLOW_ACTION_COUNT;
7568                         break;
7569 cnt_err:
7570                         if (rte_errno == ENOTSUP)
7571                                 return rte_flow_error_set
7572                                               (error, ENOTSUP,
7573                                                RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7574                                                NULL,
7575                                                "count action not supported");
7576                         else
7577                                 return rte_flow_error_set
7578                                                 (error, rte_errno,
7579                                                  RTE_FLOW_ERROR_TYPE_ACTION,
7580                                                  action,
7581                                                  "cannot create counter"
7582                                                   " object.");
7583                         break;
7584                 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
7585                         dev_flow->dv.actions[actions_n++] =
7586                                                 priv->sh->pop_vlan_action;
7587                         action_flags |= MLX5_FLOW_ACTION_OF_POP_VLAN;
7588                         break;
7589                 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
7590                         if (!(action_flags &
7591                               MLX5_FLOW_ACTION_OF_SET_VLAN_VID))
7592                                 flow_dev_get_vlan_info_from_items(items, &vlan);
7593                         vlan.eth_proto = rte_be_to_cpu_16
7594                              ((((const struct rte_flow_action_of_push_vlan *)
7595                                                    actions->conf)->ethertype));
7596                         found_action = mlx5_flow_find_action
7597                                         (actions + 1,
7598                                          RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID);
7599                         if (found_action)
7600                                 mlx5_update_vlan_vid_pcp(found_action, &vlan);
7601                         found_action = mlx5_flow_find_action
7602                                         (actions + 1,
7603                                          RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP);
7604                         if (found_action)
7605                                 mlx5_update_vlan_vid_pcp(found_action, &vlan);
7606                         if (flow_dv_create_action_push_vlan
7607                                             (dev, attr, &vlan, dev_flow, error))
7608                                 return -rte_errno;
7609                         dev_flow->dv.actions[actions_n++] =
7610                                         dev_flow->dv.push_vlan_res->action;
7611                         action_flags |= MLX5_FLOW_ACTION_OF_PUSH_VLAN;
7612                         break;
7613                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
7614                         /* of_vlan_push action handled this action */
7615                         MLX5_ASSERT(action_flags &
7616                                     MLX5_FLOW_ACTION_OF_PUSH_VLAN);
7617                         break;
7618                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
7619                         if (action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN)
7620                                 break;
7621                         flow_dev_get_vlan_info_from_items(items, &vlan);
7622                         mlx5_update_vlan_vid_pcp(actions, &vlan);
7623                         /* If no VLAN push - this is a modify header action */
7624                         if (flow_dv_convert_action_modify_vlan_vid
7625                                                 (mhdr_res, actions, error))
7626                                 return -rte_errno;
7627                         action_flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_VID;
7628                         break;
7629                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
7630                 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
7631                         if (flow_dv_create_action_l2_encap(dev, actions,
7632                                                            dev_flow,
7633                                                            attr->transfer,
7634                                                            error))
7635                                 return -rte_errno;
7636                         dev_flow->dv.actions[actions_n++] =
7637                                         dev_flow->dv.encap_decap->verbs_action;
7638                         action_flags |= MLX5_FLOW_ACTION_ENCAP;
7639                         break;
7640                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
7641                 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
7642                         if (flow_dv_create_action_l2_decap(dev, dev_flow,
7643                                                            attr->transfer,
7644                                                            error))
7645                                 return -rte_errno;
7646                         dev_flow->dv.actions[actions_n++] =
7647                                         dev_flow->dv.encap_decap->verbs_action;
7648                         action_flags |= MLX5_FLOW_ACTION_DECAP;
7649                         break;
7650                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
7651                         /* Handle encap with preceding decap. */
7652                         if (action_flags & MLX5_FLOW_ACTION_DECAP) {
7653                                 if (flow_dv_create_action_raw_encap
7654                                         (dev, actions, dev_flow, attr, error))
7655                                         return -rte_errno;
7656                                 dev_flow->dv.actions[actions_n++] =
7657                                         dev_flow->dv.encap_decap->verbs_action;
7658                         } else {
7659                                 /* Handle encap without preceding decap. */
7660                                 if (flow_dv_create_action_l2_encap
7661                                     (dev, actions, dev_flow, attr->transfer,
7662                                      error))
7663                                         return -rte_errno;
7664                                 dev_flow->dv.actions[actions_n++] =
7665                                         dev_flow->dv.encap_decap->verbs_action;
7666                         }
7667                         action_flags |= MLX5_FLOW_ACTION_ENCAP;
7668                         break;
7669                 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
7670                         while ((++action)->type == RTE_FLOW_ACTION_TYPE_VOID)
7671                                 ;
7672                         if (action->type != RTE_FLOW_ACTION_TYPE_RAW_ENCAP) {
7673                                 if (flow_dv_create_action_l2_decap
7674                                     (dev, dev_flow, attr->transfer, error))
7675                                         return -rte_errno;
7676                                 dev_flow->dv.actions[actions_n++] =
7677                                         dev_flow->dv.encap_decap->verbs_action;
7678                         }
7679                         /* If decap is followed by encap, handle it at encap. */
7680                         action_flags |= MLX5_FLOW_ACTION_DECAP;
7681                         break;
7682                 case RTE_FLOW_ACTION_TYPE_JUMP:
7683                         jump_data = action->conf;
7684                         ret = mlx5_flow_group_to_table(attr, dev_flow->external,
7685                                                        jump_data->group,
7686                                                        !!priv->fdb_def_rule,
7687                                                        &table, error);
7688                         if (ret)
7689                                 return ret;
7690                         tbl = flow_dv_tbl_resource_get(dev, table,
7691                                                        attr->egress,
7692                                                        attr->transfer, error);
7693                         if (!tbl)
7694                                 return rte_flow_error_set
7695                                                 (error, errno,
7696                                                  RTE_FLOW_ERROR_TYPE_ACTION,
7697                                                  NULL,
7698                                                  "cannot create jump action.");
7699                         if (flow_dv_jump_tbl_resource_register
7700                             (dev, tbl, dev_flow, error)) {
7701                                 flow_dv_tbl_resource_release(dev, tbl);
7702                                 return rte_flow_error_set
7703                                                 (error, errno,
7704                                                  RTE_FLOW_ERROR_TYPE_ACTION,
7705                                                  NULL,
7706                                                  "cannot create jump action.");
7707                         }
7708                         dev_flow->dv.actions[actions_n++] =
7709                                         dev_flow->dv.jump->action;
7710                         action_flags |= MLX5_FLOW_ACTION_JUMP;
7711                         dev_flow->handle->fate_action = MLX5_FLOW_FATE_JUMP;
7712                         break;
7713                 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
7714                 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
7715                         if (flow_dv_convert_action_modify_mac
7716                                         (mhdr_res, actions, error))
7717                                 return -rte_errno;
7718                         action_flags |= actions->type ==
7719                                         RTE_FLOW_ACTION_TYPE_SET_MAC_SRC ?
7720                                         MLX5_FLOW_ACTION_SET_MAC_SRC :
7721                                         MLX5_FLOW_ACTION_SET_MAC_DST;
7722                         break;
7723                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
7724                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
7725                         if (flow_dv_convert_action_modify_ipv4
7726                                         (mhdr_res, actions, error))
7727                                 return -rte_errno;
7728                         action_flags |= actions->type ==
7729                                         RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC ?
7730                                         MLX5_FLOW_ACTION_SET_IPV4_SRC :
7731                                         MLX5_FLOW_ACTION_SET_IPV4_DST;
7732                         break;
7733                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
7734                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
7735                         if (flow_dv_convert_action_modify_ipv6
7736                                         (mhdr_res, actions, error))
7737                                 return -rte_errno;
7738                         action_flags |= actions->type ==
7739                                         RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC ?
7740                                         MLX5_FLOW_ACTION_SET_IPV6_SRC :
7741                                         MLX5_FLOW_ACTION_SET_IPV6_DST;
7742                         break;
7743                 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
7744                 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
7745                         if (flow_dv_convert_action_modify_tp
7746                                         (mhdr_res, actions, items,
7747                                          &flow_attr, dev_flow, !!(action_flags &
7748                                          MLX5_FLOW_ACTION_DECAP), error))
7749                                 return -rte_errno;
7750                         action_flags |= actions->type ==
7751                                         RTE_FLOW_ACTION_TYPE_SET_TP_SRC ?
7752                                         MLX5_FLOW_ACTION_SET_TP_SRC :
7753                                         MLX5_FLOW_ACTION_SET_TP_DST;
7754                         break;
7755                 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
7756                         if (flow_dv_convert_action_modify_dec_ttl
7757                                         (mhdr_res, items, &flow_attr, dev_flow,
7758                                          !!(action_flags &
7759                                          MLX5_FLOW_ACTION_DECAP), error))
7760                                 return -rte_errno;
7761                         action_flags |= MLX5_FLOW_ACTION_DEC_TTL;
7762                         break;
7763                 case RTE_FLOW_ACTION_TYPE_SET_TTL:
7764                         if (flow_dv_convert_action_modify_ttl
7765                                         (mhdr_res, actions, items, &flow_attr,
7766                                          dev_flow, !!(action_flags &
7767                                          MLX5_FLOW_ACTION_DECAP), error))
7768                                 return -rte_errno;
7769                         action_flags |= MLX5_FLOW_ACTION_SET_TTL;
7770                         break;
7771                 case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
7772                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
7773                         if (flow_dv_convert_action_modify_tcp_seq
7774                                         (mhdr_res, actions, error))
7775                                 return -rte_errno;
7776                         action_flags |= actions->type ==
7777                                         RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ ?
7778                                         MLX5_FLOW_ACTION_INC_TCP_SEQ :
7779                                         MLX5_FLOW_ACTION_DEC_TCP_SEQ;
7780                         break;
7781
7782                 case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
7783                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
7784                         if (flow_dv_convert_action_modify_tcp_ack
7785                                         (mhdr_res, actions, error))
7786                                 return -rte_errno;
7787                         action_flags |= actions->type ==
7788                                         RTE_FLOW_ACTION_TYPE_INC_TCP_ACK ?
7789                                         MLX5_FLOW_ACTION_INC_TCP_ACK :
7790                                         MLX5_FLOW_ACTION_DEC_TCP_ACK;
7791                         break;
7792                 case MLX5_RTE_FLOW_ACTION_TYPE_TAG:
7793                         if (flow_dv_convert_action_set_reg
7794                                         (mhdr_res, actions, error))
7795                                 return -rte_errno;
7796                         action_flags |= MLX5_FLOW_ACTION_SET_TAG;
7797                         break;
7798                 case MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG:
7799                         if (flow_dv_convert_action_copy_mreg
7800                                         (dev, mhdr_res, actions, error))
7801                                 return -rte_errno;
7802                         action_flags |= MLX5_FLOW_ACTION_SET_TAG;
7803                         break;
7804                 case RTE_FLOW_ACTION_TYPE_METER:
7805                         mtr = actions->conf;
7806                         if (!flow->meter) {
7807                                 flow->meter = mlx5_flow_meter_attach(priv,
7808                                                         mtr->mtr_id, attr,
7809                                                         error);
7810                                 if (!flow->meter)
7811                                         return rte_flow_error_set(error,
7812                                                 rte_errno,
7813                                                 RTE_FLOW_ERROR_TYPE_ACTION,
7814                                                 NULL,
7815                                                 "meter not found "
7816                                                 "or invalid parameters");
7817                         }
7818                         /* Set the meter action. */
7819                         dev_flow->dv.actions[actions_n++] =
7820                                 flow->meter->mfts->meter_action;
7821                         action_flags |= MLX5_FLOW_ACTION_METER;
7822                         break;
7823                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP:
7824                         if (flow_dv_convert_action_modify_ipv4_dscp(mhdr_res,
7825                                                               actions, error))
7826                                 return -rte_errno;
7827                         action_flags |= MLX5_FLOW_ACTION_SET_IPV4_DSCP;
7828                         break;
7829                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP:
7830                         if (flow_dv_convert_action_modify_ipv6_dscp(mhdr_res,
7831                                                               actions, error))
7832                                 return -rte_errno;
7833                         action_flags |= MLX5_FLOW_ACTION_SET_IPV6_DSCP;
7834                         break;
7835                 case RTE_FLOW_ACTION_TYPE_END:
7836                         actions_end = true;
7837                         if (mhdr_res->actions_num) {
7838                                 /* create modify action if needed. */
7839                                 if (flow_dv_modify_hdr_resource_register
7840                                         (dev, mhdr_res, dev_flow, error))
7841                                         return -rte_errno;
7842                                 dev_flow->dv.actions[modify_action_position] =
7843                                         handle->dvh.modify_hdr->verbs_action;
7844                         }
7845                         break;
7846                 default:
7847                         break;
7848                 }
7849                 if (mhdr_res->actions_num &&
7850                     modify_action_position == UINT32_MAX)
7851                         modify_action_position = actions_n++;
7852         }
7853         dev_flow->dv.actions_n = actions_n;
7854         dev_flow->act_flags = action_flags;
7855         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
7856                 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
7857                 int item_type = items->type;
7858
7859                 switch (item_type) {
7860                 case RTE_FLOW_ITEM_TYPE_PORT_ID:
7861                         flow_dv_translate_item_port_id(dev, match_mask,
7862                                                        match_value, items);
7863                         last_item = MLX5_FLOW_ITEM_PORT_ID;
7864                         break;
7865                 case RTE_FLOW_ITEM_TYPE_ETH:
7866                         flow_dv_translate_item_eth(match_mask, match_value,
7867                                                    items, tunnel);
7868                         matcher.priority = MLX5_PRIORITY_MAP_L2;
7869                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
7870                                              MLX5_FLOW_LAYER_OUTER_L2;
7871                         break;
7872                 case RTE_FLOW_ITEM_TYPE_VLAN:
7873                         flow_dv_translate_item_vlan(dev_flow,
7874                                                     match_mask, match_value,
7875                                                     items, tunnel);
7876                         matcher.priority = MLX5_PRIORITY_MAP_L2;
7877                         last_item = tunnel ? (MLX5_FLOW_LAYER_INNER_L2 |
7878                                               MLX5_FLOW_LAYER_INNER_VLAN) :
7879                                              (MLX5_FLOW_LAYER_OUTER_L2 |
7880                                               MLX5_FLOW_LAYER_OUTER_VLAN);
7881                         break;
7882                 case RTE_FLOW_ITEM_TYPE_IPV4:
7883                         mlx5_flow_tunnel_ip_check(items, next_protocol,
7884                                                   &item_flags, &tunnel);
7885                         flow_dv_translate_item_ipv4(match_mask, match_value,
7886                                                     items, item_flags, tunnel,
7887                                                     dev_flow->dv.group);
7888                         matcher.priority = MLX5_PRIORITY_MAP_L3;
7889                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
7890                                              MLX5_FLOW_LAYER_OUTER_L3_IPV4;
7891                         if (items->mask != NULL &&
7892                             ((const struct rte_flow_item_ipv4 *)
7893                              items->mask)->hdr.next_proto_id) {
7894                                 next_protocol =
7895                                         ((const struct rte_flow_item_ipv4 *)
7896                                          (items->spec))->hdr.next_proto_id;
7897                                 next_protocol &=
7898                                         ((const struct rte_flow_item_ipv4 *)
7899                                          (items->mask))->hdr.next_proto_id;
7900                         } else {
7901                                 /* Reset for inner layer. */
7902                                 next_protocol = 0xff;
7903                         }
7904                         break;
7905                 case RTE_FLOW_ITEM_TYPE_IPV6:
7906                         mlx5_flow_tunnel_ip_check(items, next_protocol,
7907                                                   &item_flags, &tunnel);
7908                         flow_dv_translate_item_ipv6(match_mask, match_value,
7909                                                     items, item_flags, tunnel,
7910                                                     dev_flow->dv.group);
7911                         matcher.priority = MLX5_PRIORITY_MAP_L3;
7912                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
7913                                              MLX5_FLOW_LAYER_OUTER_L3_IPV6;
7914                         if (items->mask != NULL &&
7915                             ((const struct rte_flow_item_ipv6 *)
7916                              items->mask)->hdr.proto) {
7917                                 next_protocol =
7918                                         ((const struct rte_flow_item_ipv6 *)
7919                                          items->spec)->hdr.proto;
7920                                 next_protocol &=
7921                                         ((const struct rte_flow_item_ipv6 *)
7922                                          items->mask)->hdr.proto;
7923                         } else {
7924                                 /* Reset for inner layer. */
7925                                 next_protocol = 0xff;
7926                         }
7927                         break;
7928                 case RTE_FLOW_ITEM_TYPE_TCP:
7929                         flow_dv_translate_item_tcp(match_mask, match_value,
7930                                                    items, tunnel);
7931                         matcher.priority = MLX5_PRIORITY_MAP_L4;
7932                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_TCP :
7933                                              MLX5_FLOW_LAYER_OUTER_L4_TCP;
7934                         break;
7935                 case RTE_FLOW_ITEM_TYPE_UDP:
7936                         flow_dv_translate_item_udp(match_mask, match_value,
7937                                                    items, tunnel);
7938                         matcher.priority = MLX5_PRIORITY_MAP_L4;
7939                         last_item = tunnel ? MLX5_FLOW_LAYER_INNER_L4_UDP :
7940                                              MLX5_FLOW_LAYER_OUTER_L4_UDP;
7941                         break;
7942                 case RTE_FLOW_ITEM_TYPE_GRE:
7943                         flow_dv_translate_item_gre(match_mask, match_value,
7944                                                    items, tunnel);
7945                         matcher.priority = flow->rss.level >= 2 ?
7946                                     MLX5_PRIORITY_MAP_L2 : MLX5_PRIORITY_MAP_L4;
7947                         last_item = MLX5_FLOW_LAYER_GRE;
7948                         break;
7949                 case RTE_FLOW_ITEM_TYPE_GRE_KEY:
7950                         flow_dv_translate_item_gre_key(match_mask,
7951                                                        match_value, items);
7952                         last_item = MLX5_FLOW_LAYER_GRE_KEY;
7953                         break;
7954                 case RTE_FLOW_ITEM_TYPE_NVGRE:
7955                         flow_dv_translate_item_nvgre(match_mask, match_value,
7956                                                      items, tunnel);
7957                         matcher.priority = flow->rss.level >= 2 ?
7958                                     MLX5_PRIORITY_MAP_L2 : MLX5_PRIORITY_MAP_L4;
7959                         last_item = MLX5_FLOW_LAYER_GRE;
7960                         break;
7961                 case RTE_FLOW_ITEM_TYPE_VXLAN:
7962                         flow_dv_translate_item_vxlan(match_mask, match_value,
7963                                                      items, tunnel);
7964                         matcher.priority = flow->rss.level >= 2 ?
7965                                     MLX5_PRIORITY_MAP_L2 : MLX5_PRIORITY_MAP_L4;
7966                         last_item = MLX5_FLOW_LAYER_VXLAN;
7967                         break;
7968                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
7969                         flow_dv_translate_item_vxlan_gpe(match_mask,
7970                                                          match_value, items,
7971                                                          tunnel);
7972                         matcher.priority = flow->rss.level >= 2 ?
7973                                     MLX5_PRIORITY_MAP_L2 : MLX5_PRIORITY_MAP_L4;
7974                         last_item = MLX5_FLOW_LAYER_VXLAN_GPE;
7975                         break;
7976                 case RTE_FLOW_ITEM_TYPE_GENEVE:
7977                         flow_dv_translate_item_geneve(match_mask, match_value,
7978                                                       items, tunnel);
7979                         matcher.priority = flow->rss.level >= 2 ?
7980                                     MLX5_PRIORITY_MAP_L2 : MLX5_PRIORITY_MAP_L4;
7981                         last_item = MLX5_FLOW_LAYER_GENEVE;
7982                         break;
7983                 case RTE_FLOW_ITEM_TYPE_MPLS:
7984                         flow_dv_translate_item_mpls(match_mask, match_value,
7985                                                     items, last_item, tunnel);
7986                         matcher.priority = flow->rss.level >= 2 ?
7987                                     MLX5_PRIORITY_MAP_L2 : MLX5_PRIORITY_MAP_L4;
7988                         last_item = MLX5_FLOW_LAYER_MPLS;
7989                         break;
7990                 case RTE_FLOW_ITEM_TYPE_MARK:
7991                         flow_dv_translate_item_mark(dev, match_mask,
7992                                                     match_value, items);
7993                         last_item = MLX5_FLOW_ITEM_MARK;
7994                         break;
7995                 case RTE_FLOW_ITEM_TYPE_META:
7996                         flow_dv_translate_item_meta(dev, match_mask,
7997                                                     match_value, attr, items);
7998                         last_item = MLX5_FLOW_ITEM_METADATA;
7999                         break;
8000                 case RTE_FLOW_ITEM_TYPE_ICMP:
8001                         flow_dv_translate_item_icmp(match_mask, match_value,
8002                                                     items, tunnel);
8003                         last_item = MLX5_FLOW_LAYER_ICMP;
8004                         break;
8005                 case RTE_FLOW_ITEM_TYPE_ICMP6:
8006                         flow_dv_translate_item_icmp6(match_mask, match_value,
8007                                                       items, tunnel);
8008                         last_item = MLX5_FLOW_LAYER_ICMP6;
8009                         break;
8010                 case RTE_FLOW_ITEM_TYPE_TAG:
8011                         flow_dv_translate_item_tag(dev, match_mask,
8012                                                    match_value, items);
8013                         last_item = MLX5_FLOW_ITEM_TAG;
8014                         break;
8015                 case MLX5_RTE_FLOW_ITEM_TYPE_TAG:
8016                         flow_dv_translate_mlx5_item_tag(dev, match_mask,
8017                                                         match_value, items);
8018                         last_item = MLX5_FLOW_ITEM_TAG;
8019                         break;
8020                 case MLX5_RTE_FLOW_ITEM_TYPE_TX_QUEUE:
8021                         flow_dv_translate_item_tx_queue(dev, match_mask,
8022                                                         match_value,
8023                                                         items);
8024                         last_item = MLX5_FLOW_ITEM_TX_QUEUE;
8025                         break;
8026                 case RTE_FLOW_ITEM_TYPE_GTP:
8027                         flow_dv_translate_item_gtp(match_mask, match_value,
8028                                                    items, tunnel);
8029                         matcher.priority = flow->rss.level >= 2 ?
8030                                     MLX5_PRIORITY_MAP_L2 : MLX5_PRIORITY_MAP_L4;
8031                         last_item = MLX5_FLOW_LAYER_GTP;
8032                         break;
8033                 default:
8034                         break;
8035                 }
8036                 item_flags |= last_item;
8037         }
8038         /*
8039          * When E-Switch mode is enabled, we have two cases where we need to
8040          * set the source port manually.
8041          * The first one, is in case of Nic steering rule, and the second is
8042          * E-Switch rule where no port_id item was found. In both cases
8043          * the source port is set according the current port in use.
8044          */
8045         if (!(item_flags & MLX5_FLOW_ITEM_PORT_ID) &&
8046             (priv->representor || priv->master)) {
8047                 if (flow_dv_translate_item_port_id(dev, match_mask,
8048                                                    match_value, NULL))
8049                         return -rte_errno;
8050         }
8051 #ifdef RTE_LIBRTE_MLX5_DEBUG
8052         MLX5_ASSERT(!flow_dv_check_valid_spec(matcher.mask.buf,
8053                                               dev_flow->dv.value.buf));
8054 #endif
8055         /*
8056          * Layers may be already initialized from prefix flow if this dev_flow
8057          * is the suffix flow.
8058          */
8059         handle->layers |= item_flags;
8060         if (action_flags & MLX5_FLOW_ACTION_RSS)
8061                 flow_dv_hashfields_set(dev_flow);
8062         /* Register matcher. */
8063         matcher.crc = rte_raw_cksum((const void *)matcher.mask.buf,
8064                                     matcher.mask.size);
8065         matcher.priority = mlx5_flow_adjust_priority(dev, priority,
8066                                                      matcher.priority);
8067         /* reserved field no needs to be set to 0 here. */
8068         tbl_key.domain = attr->transfer;
8069         tbl_key.direction = attr->egress;
8070         tbl_key.table_id = dev_flow->dv.group;
8071         if (flow_dv_matcher_register(dev, &matcher, &tbl_key, dev_flow, error))
8072                 return -rte_errno;
8073         return 0;
8074 }
8075
8076 /**
8077  * Apply the flow to the NIC, lock free,
8078  * (mutex should be acquired by caller).
8079  *
8080  * @param[in] dev
8081  *   Pointer to the Ethernet device structure.
8082  * @param[in, out] flow
8083  *   Pointer to flow structure.
8084  * @param[out] error
8085  *   Pointer to error structure.
8086  *
8087  * @return
8088  *   0 on success, a negative errno value otherwise and rte_errno is set.
8089  */
8090 static int
8091 __flow_dv_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
8092                 struct rte_flow_error *error)
8093 {
8094         struct mlx5_flow_dv_workspace *dv;
8095         struct mlx5_flow_handle *dh;
8096         struct mlx5_flow_handle_dv *dv_h;
8097         struct mlx5_flow *dev_flow;
8098         struct mlx5_priv *priv = dev->data->dev_private;
8099         uint32_t handle_idx;
8100         int n;
8101         int err;
8102         int idx;
8103
8104         for (idx = priv->flow_idx - 1; idx >= priv->flow_nested_idx; idx--) {
8105                 dev_flow = &((struct mlx5_flow *)priv->inter_flows)[idx];
8106                 dv = &dev_flow->dv;
8107                 dh = dev_flow->handle;
8108                 dv_h = &dh->dvh;
8109                 n = dv->actions_n;
8110                 if (dh->fate_action == MLX5_FLOW_FATE_DROP) {
8111                         if (dv->transfer) {
8112                                 dv->actions[n++] = priv->sh->esw_drop_action;
8113                         } else {
8114                                 struct mlx5_hrxq *drop_hrxq;
8115                                 drop_hrxq = mlx5_hrxq_drop_new(dev);
8116                                 if (!drop_hrxq) {
8117                                         rte_flow_error_set
8118                                                 (error, errno,
8119                                                  RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8120                                                  NULL,
8121                                                  "cannot get drop hash queue");
8122                                         goto error;
8123                                 }
8124                                 /*
8125                                  * Drop queues will be released by the specify
8126                                  * mlx5_hrxq_drop_release() function. Assign
8127                                  * the special index to hrxq to mark the queue
8128                                  * has been allocated.
8129                                  */
8130                                 dh->hrxq = UINT32_MAX;
8131                                 dv->actions[n++] = drop_hrxq->action;
8132                         }
8133                 } else if (dh->fate_action == MLX5_FLOW_FATE_QUEUE) {
8134                         struct mlx5_hrxq *hrxq;
8135                         uint32_t hrxq_idx;
8136
8137                         MLX5_ASSERT(flow->rss.queue);
8138                         hrxq_idx = mlx5_hrxq_get(dev, flow->rss.key,
8139                                                  MLX5_RSS_HASH_KEY_LEN,
8140                                                  dev_flow->hash_fields,
8141                                                  (*flow->rss.queue),
8142                                                  flow->rss.queue_num);
8143                         if (!hrxq_idx) {
8144                                 hrxq_idx = mlx5_hrxq_new
8145                                                 (dev, flow->rss.key,
8146                                                 MLX5_RSS_HASH_KEY_LEN,
8147                                                 dev_flow->hash_fields,
8148                                                 (*flow->rss.queue),
8149                                                 flow->rss.queue_num,
8150                                                 !!(dh->layers &
8151                                                 MLX5_FLOW_LAYER_TUNNEL));
8152                         }
8153                         hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ],
8154                                               hrxq_idx);
8155                         if (!hrxq) {
8156                                 rte_flow_error_set
8157                                         (error, rte_errno,
8158                                          RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
8159                                          "cannot get hash queue");
8160                                 goto error;
8161                         }
8162                         dh->hrxq = hrxq_idx;
8163                         dv->actions[n++] = hrxq->action;
8164                 }
8165                 dh->ib_flow =
8166                         mlx5_glue->dv_create_flow(dv_h->matcher->matcher_object,
8167                                                   (void *)&dv->value, n,
8168                                                   dv->actions);
8169                 if (!dh->ib_flow) {
8170                         rte_flow_error_set(error, errno,
8171                                            RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8172                                            NULL,
8173                                            "hardware refuses to create flow");
8174                         goto error;
8175                 }
8176                 if (priv->vmwa_context &&
8177                     dh->vf_vlan.tag && !dh->vf_vlan.created) {
8178                         /*
8179                          * The rule contains the VLAN pattern.
8180                          * For VF we are going to create VLAN
8181                          * interface to make hypervisor set correct
8182                          * e-Switch vport context.
8183                          */
8184                         mlx5_vlan_vmwa_acquire(dev, &dh->vf_vlan);
8185                 }
8186         }
8187         return 0;
8188 error:
8189         err = rte_errno; /* Save rte_errno before cleanup. */
8190         SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
8191                        handle_idx, dh, next) {
8192                 /* hrxq is union, don't clear it if the flag is not set. */
8193                 if (dh->hrxq) {
8194                         if (dh->fate_action == MLX5_FLOW_FATE_DROP) {
8195                                 mlx5_hrxq_drop_release(dev);
8196                                 dh->hrxq = 0;
8197                         } else if (dh->fate_action == MLX5_FLOW_FATE_QUEUE) {
8198                                 mlx5_hrxq_release(dev, dh->hrxq);
8199                                 dh->hrxq = 0;
8200                         }
8201                 }
8202                 if (dh->vf_vlan.tag && dh->vf_vlan.created)
8203                         mlx5_vlan_vmwa_release(dev, &dh->vf_vlan);
8204         }
8205         rte_errno = err; /* Restore rte_errno. */
8206         return -rte_errno;
8207 }
8208
8209 /**
8210  * Release the flow matcher.
8211  *
8212  * @param dev
8213  *   Pointer to Ethernet device.
8214  * @param handle
8215  *   Pointer to mlx5_flow_handle.
8216  *
8217  * @return
8218  *   1 while a reference on it exists, 0 when freed.
8219  */
8220 static int
8221 flow_dv_matcher_release(struct rte_eth_dev *dev,
8222                         struct mlx5_flow_handle *handle)
8223 {
8224         struct mlx5_flow_dv_matcher *matcher = handle->dvh.matcher;
8225
8226         MLX5_ASSERT(matcher->matcher_object);
8227         DRV_LOG(DEBUG, "port %u matcher %p: refcnt %d--",
8228                 dev->data->port_id, (void *)matcher,
8229                 rte_atomic32_read(&matcher->refcnt));
8230         if (rte_atomic32_dec_and_test(&matcher->refcnt)) {
8231                 claim_zero(mlx5_glue->dv_destroy_flow_matcher
8232                            (matcher->matcher_object));
8233                 LIST_REMOVE(matcher, next);
8234                 /* table ref-- in release interface. */
8235                 flow_dv_tbl_resource_release(dev, matcher->tbl);
8236                 rte_free(matcher);
8237                 DRV_LOG(DEBUG, "port %u matcher %p: removed",
8238                         dev->data->port_id, (void *)matcher);
8239                 return 0;
8240         }
8241         return 1;
8242 }
8243
8244 /**
8245  * Release an encap/decap resource.
8246  *
8247  * @param dev
8248  *   Pointer to Ethernet device.
8249  * @param handle
8250  *   Pointer to mlx5_flow_handle.
8251  *
8252  * @return
8253  *   1 while a reference on it exists, 0 when freed.
8254  */
8255 static int
8256 flow_dv_encap_decap_resource_release(struct rte_eth_dev *dev,
8257                                      struct mlx5_flow_handle *handle)
8258 {
8259         struct mlx5_priv *priv = dev->data->dev_private;
8260         uint32_t idx = handle->dvh.encap_decap;
8261         struct mlx5_flow_dv_encap_decap_resource *cache_resource;
8262
8263         cache_resource = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_DECAP_ENCAP],
8264                          idx);
8265         if (!cache_resource)
8266                 return 0;
8267         MLX5_ASSERT(cache_resource->verbs_action);
8268         DRV_LOG(DEBUG, "encap/decap resource %p: refcnt %d--",
8269                 (void *)cache_resource,
8270                 rte_atomic32_read(&cache_resource->refcnt));
8271         if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
8272                 claim_zero(mlx5_glue->destroy_flow_action
8273                                 (cache_resource->verbs_action));
8274                 ILIST_REMOVE(priv->sh->ipool[MLX5_IPOOL_DECAP_ENCAP],
8275                              &priv->sh->encaps_decaps, idx,
8276                              cache_resource, next);
8277                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_DECAP_ENCAP], idx);
8278                 DRV_LOG(DEBUG, "encap/decap resource %p: removed",
8279                         (void *)cache_resource);
8280                 return 0;
8281         }
8282         return 1;
8283 }
8284
8285 /**
8286  * Release an jump to table action resource.
8287  *
8288  * @param dev
8289  *   Pointer to Ethernet device.
8290  * @param handle
8291  *   Pointer to mlx5_flow_handle.
8292  *
8293  * @return
8294  *   1 while a reference on it exists, 0 when freed.
8295  */
8296 static int
8297 flow_dv_jump_tbl_resource_release(struct rte_eth_dev *dev,
8298                                   struct mlx5_flow_handle *handle)
8299 {
8300         struct mlx5_priv *priv = dev->data->dev_private;
8301         struct mlx5_flow_dv_jump_tbl_resource *cache_resource;
8302         struct mlx5_flow_tbl_data_entry *tbl_data;
8303
8304         tbl_data = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_JUMP],
8305                              handle->jump);
8306         if (!tbl_data)
8307                 return 0;
8308         cache_resource = &tbl_data->jump;
8309         MLX5_ASSERT(cache_resource->action);
8310         DRV_LOG(DEBUG, "jump table resource %p: refcnt %d--",
8311                 (void *)cache_resource,
8312                 rte_atomic32_read(&cache_resource->refcnt));
8313         if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
8314                 claim_zero(mlx5_glue->destroy_flow_action
8315                                 (cache_resource->action));
8316                 /* jump action memory free is inside the table release. */
8317                 flow_dv_tbl_resource_release(dev, &tbl_data->tbl);
8318                 DRV_LOG(DEBUG, "jump table resource %p: removed",
8319                         (void *)cache_resource);
8320                 return 0;
8321         }
8322         return 1;
8323 }
8324
8325 /**
8326  * Release a modify-header resource.
8327  *
8328  * @param handle
8329  *   Pointer to mlx5_flow_handle.
8330  *
8331  * @return
8332  *   1 while a reference on it exists, 0 when freed.
8333  */
8334 static int
8335 flow_dv_modify_hdr_resource_release(struct mlx5_flow_handle *handle)
8336 {
8337         struct mlx5_flow_dv_modify_hdr_resource *cache_resource =
8338                                                         handle->dvh.modify_hdr;
8339
8340         MLX5_ASSERT(cache_resource->verbs_action);
8341         DRV_LOG(DEBUG, "modify-header resource %p: refcnt %d--",
8342                 (void *)cache_resource,
8343                 rte_atomic32_read(&cache_resource->refcnt));
8344         if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
8345                 claim_zero(mlx5_glue->destroy_flow_action
8346                                 (cache_resource->verbs_action));
8347                 LIST_REMOVE(cache_resource, next);
8348                 rte_free(cache_resource);
8349                 DRV_LOG(DEBUG, "modify-header resource %p: removed",
8350                         (void *)cache_resource);
8351                 return 0;
8352         }
8353         return 1;
8354 }
8355
8356 /**
8357  * Release port ID action resource.
8358  *
8359  * @param dev
8360  *   Pointer to Ethernet device.
8361  * @param handle
8362  *   Pointer to mlx5_flow_handle.
8363  *
8364  * @return
8365  *   1 while a reference on it exists, 0 when freed.
8366  */
8367 static int
8368 flow_dv_port_id_action_resource_release(struct rte_eth_dev *dev,
8369                                         struct mlx5_flow_handle *handle)
8370 {
8371         struct mlx5_priv *priv = dev->data->dev_private;
8372         struct mlx5_flow_dv_port_id_action_resource *cache_resource;
8373         uint32_t idx = handle->port_id_action;
8374
8375         cache_resource = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_PORT_ID],
8376                                         idx);
8377         if (!cache_resource)
8378                 return 0;
8379         MLX5_ASSERT(cache_resource->action);
8380         DRV_LOG(DEBUG, "port ID action resource %p: refcnt %d--",
8381                 (void *)cache_resource,
8382                 rte_atomic32_read(&cache_resource->refcnt));
8383         if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
8384                 claim_zero(mlx5_glue->destroy_flow_action
8385                                 (cache_resource->action));
8386                 ILIST_REMOVE(priv->sh->ipool[MLX5_IPOOL_PORT_ID],
8387                              &priv->sh->port_id_action_list, idx,
8388                              cache_resource, next);
8389                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_PORT_ID], idx);
8390                 DRV_LOG(DEBUG, "port id action resource %p: removed",
8391                         (void *)cache_resource);
8392                 return 0;
8393         }
8394         return 1;
8395 }
8396
8397 /**
8398  * Release push vlan action resource.
8399  *
8400  * @param dev
8401  *   Pointer to Ethernet device.
8402  * @param handle
8403  *   Pointer to mlx5_flow_handle.
8404  *
8405  * @return
8406  *   1 while a reference on it exists, 0 when freed.
8407  */
8408 static int
8409 flow_dv_push_vlan_action_resource_release(struct rte_eth_dev *dev,
8410                                           struct mlx5_flow_handle *handle)
8411 {
8412         struct mlx5_priv *priv = dev->data->dev_private;
8413         uint32_t idx = handle->dvh.push_vlan_res;
8414         struct mlx5_flow_dv_push_vlan_action_resource *cache_resource;
8415
8416         cache_resource = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_PUSH_VLAN],
8417                                         idx);
8418         if (!cache_resource)
8419                 return 0;
8420         MLX5_ASSERT(cache_resource->action);
8421         DRV_LOG(DEBUG, "push VLAN action resource %p: refcnt %d--",
8422                 (void *)cache_resource,
8423                 rte_atomic32_read(&cache_resource->refcnt));
8424         if (rte_atomic32_dec_and_test(&cache_resource->refcnt)) {
8425                 claim_zero(mlx5_glue->destroy_flow_action
8426                                 (cache_resource->action));
8427                 ILIST_REMOVE(priv->sh->ipool[MLX5_IPOOL_PUSH_VLAN],
8428                              &priv->sh->push_vlan_action_list, idx,
8429                              cache_resource, next);
8430                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_PUSH_VLAN], idx);
8431                 DRV_LOG(DEBUG, "push vlan action resource %p: removed",
8432                         (void *)cache_resource);
8433                 return 0;
8434         }
8435         return 1;
8436 }
8437
8438 /**
8439  * Release the fate resource.
8440  *
8441  * @param dev
8442  *   Pointer to Ethernet device.
8443  * @param handle
8444  *   Pointer to mlx5_flow_handle.
8445  */
8446 static void
8447 flow_dv_fate_resource_release(struct rte_eth_dev *dev,
8448                                struct mlx5_flow_handle *handle)
8449 {
8450         if (!handle->fate_idx)
8451                 return;
8452         if (handle->fate_action == MLX5_FLOW_FATE_DROP)
8453                 mlx5_hrxq_drop_release(dev);
8454         else if (handle->fate_action == MLX5_FLOW_FATE_QUEUE)
8455                 mlx5_hrxq_release(dev, handle->hrxq);
8456         else if (handle->fate_action == MLX5_FLOW_FATE_JUMP)
8457                 flow_dv_jump_tbl_resource_release(dev, handle);
8458         else if (handle->fate_action == MLX5_FLOW_FATE_PORT_ID)
8459                 flow_dv_port_id_action_resource_release(dev, handle);
8460         else
8461                 DRV_LOG(DEBUG, "Incorrect fate action:%d", handle->fate_action);
8462         handle->fate_idx = 0;
8463 }
8464
8465 /**
8466  * Remove the flow from the NIC but keeps it in memory.
8467  * Lock free, (mutex should be acquired by caller).
8468  *
8469  * @param[in] dev
8470  *   Pointer to Ethernet device.
8471  * @param[in, out] flow
8472  *   Pointer to flow structure.
8473  */
8474 static void
8475 __flow_dv_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
8476 {
8477         struct mlx5_flow_handle *dh;
8478         uint32_t handle_idx;
8479         struct mlx5_priv *priv = dev->data->dev_private;
8480
8481         if (!flow)
8482                 return;
8483         handle_idx = flow->dev_handles;
8484         while (handle_idx) {
8485                 dh = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
8486                                     handle_idx);
8487                 if (!dh)
8488                         return;
8489                 if (dh->ib_flow) {
8490                         claim_zero(mlx5_glue->dv_destroy_flow(dh->ib_flow));
8491                         dh->ib_flow = NULL;
8492                 }
8493                 if (dh->fate_action == MLX5_FLOW_FATE_DROP ||
8494                     dh->fate_action == MLX5_FLOW_FATE_QUEUE)
8495                         flow_dv_fate_resource_release(dev, dh);
8496                 if (dh->vf_vlan.tag && dh->vf_vlan.created)
8497                         mlx5_vlan_vmwa_release(dev, &dh->vf_vlan);
8498                 handle_idx = dh->next.next;
8499         }
8500 }
8501
8502 /**
8503  * Remove the flow from the NIC and the memory.
8504  * Lock free, (mutex should be acquired by caller).
8505  *
8506  * @param[in] dev
8507  *   Pointer to the Ethernet device structure.
8508  * @param[in, out] flow
8509  *   Pointer to flow structure.
8510  */
8511 static void
8512 __flow_dv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
8513 {
8514         struct mlx5_flow_handle *dev_handle;
8515         struct mlx5_priv *priv = dev->data->dev_private;
8516
8517         if (!flow)
8518                 return;
8519         __flow_dv_remove(dev, flow);
8520         if (flow->counter) {
8521                 flow_dv_counter_release(dev, flow->counter);
8522                 flow->counter = 0;
8523         }
8524         if (flow->meter) {
8525                 mlx5_flow_meter_detach(flow->meter);
8526                 flow->meter = NULL;
8527         }
8528         while (flow->dev_handles) {
8529                 uint32_t tmp_idx = flow->dev_handles;
8530
8531                 dev_handle = mlx5_ipool_get(priv->sh->ipool
8532                                             [MLX5_IPOOL_MLX5_FLOW], tmp_idx);
8533                 if (!dev_handle)
8534                         return;
8535                 flow->dev_handles = dev_handle->next.next;
8536                 if (dev_handle->dvh.matcher)
8537                         flow_dv_matcher_release(dev, dev_handle);
8538                 if (dev_handle->dvh.encap_decap)
8539                         flow_dv_encap_decap_resource_release(dev, dev_handle);
8540                 if (dev_handle->dvh.modify_hdr)
8541                         flow_dv_modify_hdr_resource_release(dev_handle);
8542                 if (dev_handle->dvh.push_vlan_res)
8543                         flow_dv_push_vlan_action_resource_release(dev,
8544                                                                   dev_handle);
8545                 if (dev_handle->dvh.tag_resource)
8546                         flow_dv_tag_release(dev,
8547                                             dev_handle->dvh.tag_resource);
8548                 flow_dv_fate_resource_release(dev, dev_handle);
8549                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
8550                            tmp_idx);
8551         }
8552 }
8553
8554 /**
8555  * Query a dv flow  rule for its statistics via devx.
8556  *
8557  * @param[in] dev
8558  *   Pointer to Ethernet device.
8559  * @param[in] flow
8560  *   Pointer to the sub flow.
8561  * @param[out] data
8562  *   data retrieved by the query.
8563  * @param[out] error
8564  *   Perform verbose error reporting if not NULL.
8565  *
8566  * @return
8567  *   0 on success, a negative errno value otherwise and rte_errno is set.
8568  */
8569 static int
8570 flow_dv_query_count(struct rte_eth_dev *dev, struct rte_flow *flow,
8571                     void *data, struct rte_flow_error *error)
8572 {
8573         struct mlx5_priv *priv = dev->data->dev_private;
8574         struct rte_flow_query_count *qc = data;
8575
8576         if (!priv->config.devx)
8577                 return rte_flow_error_set(error, ENOTSUP,
8578                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8579                                           NULL,
8580                                           "counters are not supported");
8581         if (flow->counter) {
8582                 uint64_t pkts, bytes;
8583                 struct mlx5_flow_counter *cnt;
8584
8585                 cnt = flow_dv_counter_get_by_idx(dev, flow->counter,
8586                                                  NULL);
8587                 int err = _flow_dv_query_count(dev, flow->counter, &pkts,
8588                                                &bytes);
8589
8590                 if (err)
8591                         return rte_flow_error_set(error, -err,
8592                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8593                                         NULL, "cannot read counters");
8594                 qc->hits_set = 1;
8595                 qc->bytes_set = 1;
8596                 qc->hits = pkts - cnt->hits;
8597                 qc->bytes = bytes - cnt->bytes;
8598                 if (qc->reset) {
8599                         cnt->hits = pkts;
8600                         cnt->bytes = bytes;
8601                 }
8602                 return 0;
8603         }
8604         return rte_flow_error_set(error, EINVAL,
8605                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8606                                   NULL,
8607                                   "counters are not available");
8608 }
8609
8610 /**
8611  * Query a flow.
8612  *
8613  * @see rte_flow_query()
8614  * @see rte_flow_ops
8615  */
8616 static int
8617 flow_dv_query(struct rte_eth_dev *dev,
8618               struct rte_flow *flow __rte_unused,
8619               const struct rte_flow_action *actions __rte_unused,
8620               void *data __rte_unused,
8621               struct rte_flow_error *error __rte_unused)
8622 {
8623         int ret = -EINVAL;
8624
8625         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
8626                 switch (actions->type) {
8627                 case RTE_FLOW_ACTION_TYPE_VOID:
8628                         break;
8629                 case RTE_FLOW_ACTION_TYPE_COUNT:
8630                         ret = flow_dv_query_count(dev, flow, data, error);
8631                         break;
8632                 default:
8633                         return rte_flow_error_set(error, ENOTSUP,
8634                                                   RTE_FLOW_ERROR_TYPE_ACTION,
8635                                                   actions,
8636                                                   "action not supported");
8637                 }
8638         }
8639         return ret;
8640 }
8641
8642 /**
8643  * Destroy the meter table set.
8644  * Lock free, (mutex should be acquired by caller).
8645  *
8646  * @param[in] dev
8647  *   Pointer to Ethernet device.
8648  * @param[in] tbl
8649  *   Pointer to the meter table set.
8650  *
8651  * @return
8652  *   Always 0.
8653  */
8654 static int
8655 flow_dv_destroy_mtr_tbl(struct rte_eth_dev *dev,
8656                         struct mlx5_meter_domains_infos *tbl)
8657 {
8658         struct mlx5_priv *priv = dev->data->dev_private;
8659         struct mlx5_meter_domains_infos *mtd =
8660                                 (struct mlx5_meter_domains_infos *)tbl;
8661
8662         if (!mtd || !priv->config.dv_flow_en)
8663                 return 0;
8664         if (mtd->ingress.policer_rules[RTE_MTR_DROPPED])
8665                 claim_zero(mlx5_glue->dv_destroy_flow
8666                           (mtd->ingress.policer_rules[RTE_MTR_DROPPED]));
8667         if (mtd->egress.policer_rules[RTE_MTR_DROPPED])
8668                 claim_zero(mlx5_glue->dv_destroy_flow
8669                           (mtd->egress.policer_rules[RTE_MTR_DROPPED]));
8670         if (mtd->transfer.policer_rules[RTE_MTR_DROPPED])
8671                 claim_zero(mlx5_glue->dv_destroy_flow
8672                           (mtd->transfer.policer_rules[RTE_MTR_DROPPED]));
8673         if (mtd->egress.color_matcher)
8674                 claim_zero(mlx5_glue->dv_destroy_flow_matcher
8675                           (mtd->egress.color_matcher));
8676         if (mtd->egress.any_matcher)
8677                 claim_zero(mlx5_glue->dv_destroy_flow_matcher
8678                           (mtd->egress.any_matcher));
8679         if (mtd->egress.tbl)
8680                 claim_zero(flow_dv_tbl_resource_release(dev,
8681                                                         mtd->egress.tbl));
8682         if (mtd->egress.sfx_tbl)
8683                 claim_zero(flow_dv_tbl_resource_release(dev,
8684                                                         mtd->egress.sfx_tbl));
8685         if (mtd->ingress.color_matcher)
8686                 claim_zero(mlx5_glue->dv_destroy_flow_matcher
8687                           (mtd->ingress.color_matcher));
8688         if (mtd->ingress.any_matcher)
8689                 claim_zero(mlx5_glue->dv_destroy_flow_matcher
8690                           (mtd->ingress.any_matcher));
8691         if (mtd->ingress.tbl)
8692                 claim_zero(flow_dv_tbl_resource_release(dev,
8693                                                         mtd->ingress.tbl));
8694         if (mtd->ingress.sfx_tbl)
8695                 claim_zero(flow_dv_tbl_resource_release(dev,
8696                                                         mtd->ingress.sfx_tbl));
8697         if (mtd->transfer.color_matcher)
8698                 claim_zero(mlx5_glue->dv_destroy_flow_matcher
8699                           (mtd->transfer.color_matcher));
8700         if (mtd->transfer.any_matcher)
8701                 claim_zero(mlx5_glue->dv_destroy_flow_matcher
8702                           (mtd->transfer.any_matcher));
8703         if (mtd->transfer.tbl)
8704                 claim_zero(flow_dv_tbl_resource_release(dev,
8705                                                         mtd->transfer.tbl));
8706         if (mtd->transfer.sfx_tbl)
8707                 claim_zero(flow_dv_tbl_resource_release(dev,
8708                                                         mtd->transfer.sfx_tbl));
8709         if (mtd->drop_actn)
8710                 claim_zero(mlx5_glue->destroy_flow_action(mtd->drop_actn));
8711         rte_free(mtd);
8712         return 0;
8713 }
8714
8715 /* Number of meter flow actions, count and jump or count and drop. */
8716 #define METER_ACTIONS 2
8717
8718 /**
8719  * Create specify domain meter table and suffix table.
8720  *
8721  * @param[in] dev
8722  *   Pointer to Ethernet device.
8723  * @param[in,out] mtb
8724  *   Pointer to DV meter table set.
8725  * @param[in] egress
8726  *   Table attribute.
8727  * @param[in] transfer
8728  *   Table attribute.
8729  * @param[in] color_reg_c_idx
8730  *   Reg C index for color match.
8731  *
8732  * @return
8733  *   0 on success, -1 otherwise and rte_errno is set.
8734  */
8735 static int
8736 flow_dv_prepare_mtr_tables(struct rte_eth_dev *dev,
8737                            struct mlx5_meter_domains_infos *mtb,
8738                            uint8_t egress, uint8_t transfer,
8739                            uint32_t color_reg_c_idx)
8740 {
8741         struct mlx5_priv *priv = dev->data->dev_private;
8742         struct mlx5_ibv_shared *sh = priv->sh;
8743         struct mlx5_flow_dv_match_params mask = {
8744                 .size = sizeof(mask.buf),
8745         };
8746         struct mlx5_flow_dv_match_params value = {
8747                 .size = sizeof(value.buf),
8748         };
8749         struct mlx5dv_flow_matcher_attr dv_attr = {
8750                 .type = IBV_FLOW_ATTR_NORMAL,
8751                 .priority = 0,
8752                 .match_criteria_enable = 0,
8753                 .match_mask = (void *)&mask,
8754         };
8755         void *actions[METER_ACTIONS];
8756         struct mlx5_meter_domain_info *dtb;
8757         struct rte_flow_error error;
8758         int i = 0;
8759
8760         if (transfer)
8761                 dtb = &mtb->transfer;
8762         else if (egress)
8763                 dtb = &mtb->egress;
8764         else
8765                 dtb = &mtb->ingress;
8766         /* Create the meter table with METER level. */
8767         dtb->tbl = flow_dv_tbl_resource_get(dev, MLX5_FLOW_TABLE_LEVEL_METER,
8768                                             egress, transfer, &error);
8769         if (!dtb->tbl) {
8770                 DRV_LOG(ERR, "Failed to create meter policer table.");
8771                 return -1;
8772         }
8773         /* Create the meter suffix table with SUFFIX level. */
8774         dtb->sfx_tbl = flow_dv_tbl_resource_get(dev,
8775                                             MLX5_FLOW_TABLE_LEVEL_SUFFIX,
8776                                             egress, transfer, &error);
8777         if (!dtb->sfx_tbl) {
8778                 DRV_LOG(ERR, "Failed to create meter suffix table.");
8779                 return -1;
8780         }
8781         /* Create matchers, Any and Color. */
8782         dv_attr.priority = 3;
8783         dv_attr.match_criteria_enable = 0;
8784         dtb->any_matcher = mlx5_glue->dv_create_flow_matcher(sh->ctx,
8785                                                              &dv_attr,
8786                                                              dtb->tbl->obj);
8787         if (!dtb->any_matcher) {
8788                 DRV_LOG(ERR, "Failed to create meter"
8789                              " policer default matcher.");
8790                 goto error_exit;
8791         }
8792         dv_attr.priority = 0;
8793         dv_attr.match_criteria_enable =
8794                                 1 << MLX5_MATCH_CRITERIA_ENABLE_MISC2_BIT;
8795         flow_dv_match_meta_reg(mask.buf, value.buf, color_reg_c_idx,
8796                                rte_col_2_mlx5_col(RTE_COLORS), UINT8_MAX);
8797         dtb->color_matcher = mlx5_glue->dv_create_flow_matcher(sh->ctx,
8798                                                                &dv_attr,
8799                                                                dtb->tbl->obj);
8800         if (!dtb->color_matcher) {
8801                 DRV_LOG(ERR, "Failed to create meter policer color matcher.");
8802                 goto error_exit;
8803         }
8804         if (mtb->count_actns[RTE_MTR_DROPPED])
8805                 actions[i++] = mtb->count_actns[RTE_MTR_DROPPED];
8806         actions[i++] = mtb->drop_actn;
8807         /* Default rule: lowest priority, match any, actions: drop. */
8808         dtb->policer_rules[RTE_MTR_DROPPED] =
8809                         mlx5_glue->dv_create_flow(dtb->any_matcher,
8810                                                  (void *)&value, i, actions);
8811         if (!dtb->policer_rules[RTE_MTR_DROPPED]) {
8812                 DRV_LOG(ERR, "Failed to create meter policer drop rule.");
8813                 goto error_exit;
8814         }
8815         return 0;
8816 error_exit:
8817         return -1;
8818 }
8819
8820 /**
8821  * Create the needed meter and suffix tables.
8822  * Lock free, (mutex should be acquired by caller).
8823  *
8824  * @param[in] dev
8825  *   Pointer to Ethernet device.
8826  * @param[in] fm
8827  *   Pointer to the flow meter.
8828  *
8829  * @return
8830  *   Pointer to table set on success, NULL otherwise and rte_errno is set.
8831  */
8832 static struct mlx5_meter_domains_infos *
8833 flow_dv_create_mtr_tbl(struct rte_eth_dev *dev,
8834                        const struct mlx5_flow_meter *fm)
8835 {
8836         struct mlx5_priv *priv = dev->data->dev_private;
8837         struct mlx5_meter_domains_infos *mtb;
8838         int ret;
8839         int i;
8840
8841         if (!priv->mtr_en) {
8842                 rte_errno = ENOTSUP;
8843                 return NULL;
8844         }
8845         mtb = rte_calloc(__func__, 1, sizeof(*mtb), 0);
8846         if (!mtb) {
8847                 DRV_LOG(ERR, "Failed to allocate memory for meter.");
8848                 return NULL;
8849         }
8850         /* Create meter count actions */
8851         for (i = 0; i <= RTE_MTR_DROPPED; i++) {
8852                 struct mlx5_flow_counter *cnt;
8853                 if (!fm->policer_stats.cnt[i])
8854                         continue;
8855                 cnt = flow_dv_counter_get_by_idx(dev,
8856                       fm->policer_stats.cnt[i], NULL);
8857                 mtb->count_actns[i] = cnt->action;
8858         }
8859         /* Create drop action. */
8860         mtb->drop_actn = mlx5_glue->dr_create_flow_action_drop();
8861         if (!mtb->drop_actn) {
8862                 DRV_LOG(ERR, "Failed to create drop action.");
8863                 goto error_exit;
8864         }
8865         /* Egress meter table. */
8866         ret = flow_dv_prepare_mtr_tables(dev, mtb, 1, 0, priv->mtr_color_reg);
8867         if (ret) {
8868                 DRV_LOG(ERR, "Failed to prepare egress meter table.");
8869                 goto error_exit;
8870         }
8871         /* Ingress meter table. */
8872         ret = flow_dv_prepare_mtr_tables(dev, mtb, 0, 0, priv->mtr_color_reg);
8873         if (ret) {
8874                 DRV_LOG(ERR, "Failed to prepare ingress meter table.");
8875                 goto error_exit;
8876         }
8877         /* FDB meter table. */
8878         if (priv->config.dv_esw_en) {
8879                 ret = flow_dv_prepare_mtr_tables(dev, mtb, 0, 1,
8880                                                  priv->mtr_color_reg);
8881                 if (ret) {
8882                         DRV_LOG(ERR, "Failed to prepare fdb meter table.");
8883                         goto error_exit;
8884                 }
8885         }
8886         return mtb;
8887 error_exit:
8888         flow_dv_destroy_mtr_tbl(dev, mtb);
8889         return NULL;
8890 }
8891
8892 /**
8893  * Destroy domain policer rule.
8894  *
8895  * @param[in] dt
8896  *   Pointer to domain table.
8897  */
8898 static void
8899 flow_dv_destroy_domain_policer_rule(struct mlx5_meter_domain_info *dt)
8900 {
8901         int i;
8902
8903         for (i = 0; i < RTE_MTR_DROPPED; i++) {
8904                 if (dt->policer_rules[i]) {
8905                         claim_zero(mlx5_glue->dv_destroy_flow
8906                                   (dt->policer_rules[i]));
8907                         dt->policer_rules[i] = NULL;
8908                 }
8909         }
8910         if (dt->jump_actn) {
8911                 claim_zero(mlx5_glue->destroy_flow_action(dt->jump_actn));
8912                 dt->jump_actn = NULL;
8913         }
8914 }
8915
8916 /**
8917  * Destroy policer rules.
8918  *
8919  * @param[in] dev
8920  *   Pointer to Ethernet device.
8921  * @param[in] fm
8922  *   Pointer to flow meter structure.
8923  * @param[in] attr
8924  *   Pointer to flow attributes.
8925  *
8926  * @return
8927  *   Always 0.
8928  */
8929 static int
8930 flow_dv_destroy_policer_rules(struct rte_eth_dev *dev __rte_unused,
8931                               const struct mlx5_flow_meter *fm,
8932                               const struct rte_flow_attr *attr)
8933 {
8934         struct mlx5_meter_domains_infos *mtb = fm ? fm->mfts : NULL;
8935
8936         if (!mtb)
8937                 return 0;
8938         if (attr->egress)
8939                 flow_dv_destroy_domain_policer_rule(&mtb->egress);
8940         if (attr->ingress)
8941                 flow_dv_destroy_domain_policer_rule(&mtb->ingress);
8942         if (attr->transfer)
8943                 flow_dv_destroy_domain_policer_rule(&mtb->transfer);
8944         return 0;
8945 }
8946
8947 /**
8948  * Create specify domain meter policer rule.
8949  *
8950  * @param[in] fm
8951  *   Pointer to flow meter structure.
8952  * @param[in] mtb
8953  *   Pointer to DV meter table set.
8954  * @param[in] mtr_reg_c
8955  *   Color match REG_C.
8956  *
8957  * @return
8958  *   0 on success, -1 otherwise.
8959  */
8960 static int
8961 flow_dv_create_policer_forward_rule(struct mlx5_flow_meter *fm,
8962                                     struct mlx5_meter_domain_info *dtb,
8963                                     uint8_t mtr_reg_c)
8964 {
8965         struct mlx5_flow_dv_match_params matcher = {
8966                 .size = sizeof(matcher.buf),
8967         };
8968         struct mlx5_flow_dv_match_params value = {
8969                 .size = sizeof(value.buf),
8970         };
8971         struct mlx5_meter_domains_infos *mtb = fm->mfts;
8972         void *actions[METER_ACTIONS];
8973         int i;
8974
8975         /* Create jump action. */
8976         if (!dtb->jump_actn)
8977                 dtb->jump_actn =
8978                         mlx5_glue->dr_create_flow_action_dest_flow_tbl
8979                                                         (dtb->sfx_tbl->obj);
8980         if (!dtb->jump_actn) {
8981                 DRV_LOG(ERR, "Failed to create policer jump action.");
8982                 goto error;
8983         }
8984         for (i = 0; i < RTE_MTR_DROPPED; i++) {
8985                 int j = 0;
8986
8987                 flow_dv_match_meta_reg(matcher.buf, value.buf, mtr_reg_c,
8988                                        rte_col_2_mlx5_col(i), UINT8_MAX);
8989                 if (mtb->count_actns[i])
8990                         actions[j++] = mtb->count_actns[i];
8991                 if (fm->params.action[i] == MTR_POLICER_ACTION_DROP)
8992                         actions[j++] = mtb->drop_actn;
8993                 else
8994                         actions[j++] = dtb->jump_actn;
8995                 dtb->policer_rules[i] =
8996                         mlx5_glue->dv_create_flow(dtb->color_matcher,
8997                                                  (void *)&value,
8998                                                   j, actions);
8999                 if (!dtb->policer_rules[i]) {
9000                         DRV_LOG(ERR, "Failed to create policer rule.");
9001                         goto error;
9002                 }
9003         }
9004         return 0;
9005 error:
9006         rte_errno = errno;
9007         return -1;
9008 }
9009
9010 /**
9011  * Create policer rules.
9012  *
9013  * @param[in] dev
9014  *   Pointer to Ethernet device.
9015  * @param[in] fm
9016  *   Pointer to flow meter structure.
9017  * @param[in] attr
9018  *   Pointer to flow attributes.
9019  *
9020  * @return
9021  *   0 on success, -1 otherwise.
9022  */
9023 static int
9024 flow_dv_create_policer_rules(struct rte_eth_dev *dev,
9025                              struct mlx5_flow_meter *fm,
9026                              const struct rte_flow_attr *attr)
9027 {
9028         struct mlx5_priv *priv = dev->data->dev_private;
9029         struct mlx5_meter_domains_infos *mtb = fm->mfts;
9030         int ret;
9031
9032         if (attr->egress) {
9033                 ret = flow_dv_create_policer_forward_rule(fm, &mtb->egress,
9034                                                 priv->mtr_color_reg);
9035                 if (ret) {
9036                         DRV_LOG(ERR, "Failed to create egress policer.");
9037                         goto error;
9038                 }
9039         }
9040         if (attr->ingress) {
9041                 ret = flow_dv_create_policer_forward_rule(fm, &mtb->ingress,
9042                                                 priv->mtr_color_reg);
9043                 if (ret) {
9044                         DRV_LOG(ERR, "Failed to create ingress policer.");
9045                         goto error;
9046                 }
9047         }
9048         if (attr->transfer) {
9049                 ret = flow_dv_create_policer_forward_rule(fm, &mtb->transfer,
9050                                                 priv->mtr_color_reg);
9051                 if (ret) {
9052                         DRV_LOG(ERR, "Failed to create transfer policer.");
9053                         goto error;
9054                 }
9055         }
9056         return 0;
9057 error:
9058         flow_dv_destroy_policer_rules(dev, fm, attr);
9059         return -1;
9060 }
9061
9062 /**
9063  * Query a devx counter.
9064  *
9065  * @param[in] dev
9066  *   Pointer to the Ethernet device structure.
9067  * @param[in] cnt
9068  *   Index to the flow counter.
9069  * @param[in] clear
9070  *   Set to clear the counter statistics.
9071  * @param[out] pkts
9072  *   The statistics value of packets.
9073  * @param[out] bytes
9074  *   The statistics value of bytes.
9075  *
9076  * @return
9077  *   0 on success, otherwise return -1.
9078  */
9079 static int
9080 flow_dv_counter_query(struct rte_eth_dev *dev, uint32_t counter, bool clear,
9081                       uint64_t *pkts, uint64_t *bytes)
9082 {
9083         struct mlx5_priv *priv = dev->data->dev_private;
9084         struct mlx5_flow_counter *cnt;
9085         uint64_t inn_pkts, inn_bytes;
9086         int ret;
9087
9088         if (!priv->config.devx)
9089                 return -1;
9090
9091         ret = _flow_dv_query_count(dev, counter, &inn_pkts, &inn_bytes);
9092         if (ret)
9093                 return -1;
9094         cnt = flow_dv_counter_get_by_idx(dev, counter, NULL);
9095         *pkts = inn_pkts - cnt->hits;
9096         *bytes = inn_bytes - cnt->bytes;
9097         if (clear) {
9098                 cnt->hits = inn_pkts;
9099                 cnt->bytes = inn_bytes;
9100         }
9101         return 0;
9102 }
9103
9104 /*
9105  * Mutex-protected thunk to lock-free  __flow_dv_translate().
9106  */
9107 static int
9108 flow_dv_translate(struct rte_eth_dev *dev,
9109                   struct mlx5_flow *dev_flow,
9110                   const struct rte_flow_attr *attr,
9111                   const struct rte_flow_item items[],
9112                   const struct rte_flow_action actions[],
9113                   struct rte_flow_error *error)
9114 {
9115         int ret;
9116
9117         flow_dv_shared_lock(dev);
9118         ret = __flow_dv_translate(dev, dev_flow, attr, items, actions, error);
9119         flow_dv_shared_unlock(dev);
9120         return ret;
9121 }
9122
9123 /*
9124  * Mutex-protected thunk to lock-free  __flow_dv_apply().
9125  */
9126 static int
9127 flow_dv_apply(struct rte_eth_dev *dev,
9128               struct rte_flow *flow,
9129               struct rte_flow_error *error)
9130 {
9131         int ret;
9132
9133         flow_dv_shared_lock(dev);
9134         ret = __flow_dv_apply(dev, flow, error);
9135         flow_dv_shared_unlock(dev);
9136         return ret;
9137 }
9138
9139 /*
9140  * Mutex-protected thunk to lock-free __flow_dv_remove().
9141  */
9142 static void
9143 flow_dv_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
9144 {
9145         flow_dv_shared_lock(dev);
9146         __flow_dv_remove(dev, flow);
9147         flow_dv_shared_unlock(dev);
9148 }
9149
9150 /*
9151  * Mutex-protected thunk to lock-free __flow_dv_destroy().
9152  */
9153 static void
9154 flow_dv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
9155 {
9156         flow_dv_shared_lock(dev);
9157         __flow_dv_destroy(dev, flow);
9158         flow_dv_shared_unlock(dev);
9159 }
9160
9161 /*
9162  * Mutex-protected thunk to lock-free flow_dv_counter_alloc().
9163  */
9164 static uint32_t
9165 flow_dv_counter_allocate(struct rte_eth_dev *dev)
9166 {
9167         uint32_t cnt;
9168
9169         flow_dv_shared_lock(dev);
9170         cnt = flow_dv_counter_alloc(dev, 0, 0, 1);
9171         flow_dv_shared_unlock(dev);
9172         return cnt;
9173 }
9174
9175 /*
9176  * Mutex-protected thunk to lock-free flow_dv_counter_release().
9177  */
9178 static void
9179 flow_dv_counter_free(struct rte_eth_dev *dev, uint32_t cnt)
9180 {
9181         flow_dv_shared_lock(dev);
9182         flow_dv_counter_release(dev, cnt);
9183         flow_dv_shared_unlock(dev);
9184 }
9185
9186 const struct mlx5_flow_driver_ops mlx5_flow_dv_drv_ops = {
9187         .validate = flow_dv_validate,
9188         .prepare = flow_dv_prepare,
9189         .translate = flow_dv_translate,
9190         .apply = flow_dv_apply,
9191         .remove = flow_dv_remove,
9192         .destroy = flow_dv_destroy,
9193         .query = flow_dv_query,
9194         .create_mtr_tbls = flow_dv_create_mtr_tbl,
9195         .destroy_mtr_tbls = flow_dv_destroy_mtr_tbl,
9196         .create_policer_rules = flow_dv_create_policer_rules,
9197         .destroy_policer_rules = flow_dv_destroy_policer_rules,
9198         .counter_alloc = flow_dv_counter_allocate,
9199         .counter_free = flow_dv_counter_free,
9200         .counter_query = flow_dv_counter_query,
9201 };
9202
9203 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */