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