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