net/bnxt: set flow error after tunnel redirection free
[dpdk.git] / drivers / net / bnxt / bnxt_flow.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2021 Broadcom
3  * All rights reserved.
4  */
5
6 #include <sys/queue.h>
7
8 #include <rte_log.h>
9 #include <rte_malloc.h>
10 #include <rte_flow.h>
11 #include <rte_flow_driver.h>
12 #include <rte_tailq.h>
13 #include <rte_alarm.h>
14 #include <rte_cycles.h>
15
16 #include "bnxt.h"
17 #include "bnxt_filter.h"
18 #include "bnxt_hwrm.h"
19 #include "bnxt_ring.h"
20 #include "bnxt_rxq.h"
21 #include "bnxt_rxr.h"
22 #include "bnxt_vnic.h"
23 #include "hsi_struct_def_dpdk.h"
24
25 static int
26 bnxt_flow_args_validate(const struct rte_flow_attr *attr,
27                         const struct rte_flow_item pattern[],
28                         const struct rte_flow_action actions[],
29                         struct rte_flow_error *error)
30 {
31         if (!pattern) {
32                 rte_flow_error_set(error,
33                                    EINVAL,
34                                    RTE_FLOW_ERROR_TYPE_ITEM_NUM,
35                                    NULL,
36                                    "NULL pattern.");
37                 return -rte_errno;
38         }
39
40         if (!actions) {
41                 rte_flow_error_set(error,
42                                    EINVAL,
43                                    RTE_FLOW_ERROR_TYPE_ACTION_NUM,
44                                    NULL,
45                                    "NULL action.");
46                 return -rte_errno;
47         }
48
49         if (!attr) {
50                 rte_flow_error_set(error,
51                                    EINVAL,
52                                    RTE_FLOW_ERROR_TYPE_ATTR,
53                                    NULL,
54                                    "NULL attribute.");
55                 return -rte_errno;
56         }
57
58         return 0;
59 }
60
61 static const struct rte_flow_item *
62 bnxt_flow_non_void_item(const struct rte_flow_item *cur)
63 {
64         while (1) {
65                 if (cur->type != RTE_FLOW_ITEM_TYPE_VOID)
66                         return cur;
67                 cur++;
68         }
69 }
70
71 static const struct rte_flow_action *
72 bnxt_flow_non_void_action(const struct rte_flow_action *cur)
73 {
74         while (1) {
75                 if (cur->type != RTE_FLOW_ACTION_TYPE_VOID)
76                         return cur;
77                 cur++;
78         }
79 }
80
81 static int
82 bnxt_filter_type_check(const struct rte_flow_item pattern[],
83                        struct rte_flow_error *error)
84 {
85         const struct rte_flow_item *item =
86                 bnxt_flow_non_void_item(pattern);
87         int use_ntuple = 1;
88         bool has_vlan = 0;
89
90         while (item->type != RTE_FLOW_ITEM_TYPE_END) {
91                 switch (item->type) {
92                 case RTE_FLOW_ITEM_TYPE_ANY:
93                 case RTE_FLOW_ITEM_TYPE_ETH:
94                         use_ntuple = 0;
95                         break;
96                 case RTE_FLOW_ITEM_TYPE_VLAN:
97                         use_ntuple = 0;
98                         has_vlan = 1;
99                         break;
100                 case RTE_FLOW_ITEM_TYPE_IPV4:
101                 case RTE_FLOW_ITEM_TYPE_IPV6:
102                 case RTE_FLOW_ITEM_TYPE_TCP:
103                 case RTE_FLOW_ITEM_TYPE_UDP:
104                         /* FALLTHROUGH */
105                         /* need ntuple match, reset exact match */
106                         use_ntuple |= 1;
107                         break;
108                 default:
109                         PMD_DRV_LOG(DEBUG, "Unknown Flow type\n");
110                         use_ntuple |= 0;
111                 }
112                 item++;
113         }
114
115         if (has_vlan && use_ntuple) {
116                 PMD_DRV_LOG(ERR,
117                             "VLAN flow cannot use NTUPLE filter\n");
118                 rte_flow_error_set(error, EINVAL,
119                                    RTE_FLOW_ERROR_TYPE_ITEM,
120                                    item,
121                                    "Cannot use VLAN with NTUPLE");
122                 return -rte_errno;
123         }
124
125         return use_ntuple;
126 }
127
128 static int
129 bnxt_validate_and_parse_flow_type(struct bnxt *bp,
130                                   const struct rte_flow_attr *attr,
131                                   const struct rte_flow_item pattern[],
132                                   struct rte_flow_error *error,
133                                   struct bnxt_filter_info *filter)
134 {
135         const struct rte_flow_item *item = bnxt_flow_non_void_item(pattern);
136         const struct rte_flow_item_vlan *vlan_spec, *vlan_mask;
137         const struct rte_flow_item_ipv4 *ipv4_spec, *ipv4_mask;
138         const struct rte_flow_item_ipv6 *ipv6_spec, *ipv6_mask;
139         const struct rte_flow_item_tcp *tcp_spec, *tcp_mask;
140         const struct rte_flow_item_udp *udp_spec, *udp_mask;
141         const struct rte_flow_item_eth *eth_spec, *eth_mask;
142         const struct rte_ether_addr *dst, *src;
143         const struct rte_flow_item_nvgre *nvgre_spec;
144         const struct rte_flow_item_nvgre *nvgre_mask;
145         const struct rte_flow_item_gre *gre_spec;
146         const struct rte_flow_item_gre *gre_mask;
147         const struct rte_flow_item_vxlan *vxlan_spec;
148         const struct rte_flow_item_vxlan *vxlan_mask;
149         uint8_t vni_mask[] = {0xFF, 0xFF, 0xFF};
150         uint8_t tni_mask[] = {0xFF, 0xFF, 0xFF};
151         const struct rte_flow_item_vf *vf_spec;
152         uint32_t tenant_id_be = 0, valid_flags = 0;
153         bool vni_masked = 0;
154         bool tni_masked = 0;
155         uint32_t en_ethertype;
156         uint8_t inner = 0;
157         uint32_t vf = 0;
158         uint32_t en = 0;
159         int use_ntuple;
160         int dflt_vnic;
161
162         use_ntuple = bnxt_filter_type_check(pattern, error);
163         if (use_ntuple < 0)
164                 return use_ntuple;
165         PMD_DRV_LOG(DEBUG, "Use NTUPLE %d\n", use_ntuple);
166
167         filter->filter_type = use_ntuple ?
168                 HWRM_CFA_NTUPLE_FILTER : HWRM_CFA_L2_FILTER;
169         en_ethertype = use_ntuple ?
170                 NTUPLE_FLTR_ALLOC_INPUT_EN_ETHERTYPE :
171                 EM_FLOW_ALLOC_INPUT_EN_ETHERTYPE;
172
173         while (item->type != RTE_FLOW_ITEM_TYPE_END) {
174                 if (item->last) {
175                         /* last or range is NOT supported as match criteria */
176                         rte_flow_error_set(error, EINVAL,
177                                            RTE_FLOW_ERROR_TYPE_ITEM,
178                                            item,
179                                            "No support for range");
180                         return -rte_errno;
181                 }
182
183                 switch (item->type) {
184                 case RTE_FLOW_ITEM_TYPE_ANY:
185                         inner =
186                         ((const struct rte_flow_item_any *)item->spec)->num > 3;
187                         if (inner)
188                                 PMD_DRV_LOG(DEBUG, "Parse inner header\n");
189                         break;
190                 case RTE_FLOW_ITEM_TYPE_ETH:
191                         if (!item->spec)
192                                 break;
193
194                         eth_spec = item->spec;
195
196                         if (item->mask)
197                                 eth_mask = item->mask;
198                         else
199                                 eth_mask = &rte_flow_item_eth_mask;
200
201                         /* Source MAC address mask cannot be partially set.
202                          * Should be All 0's or all 1's.
203                          * Destination MAC address mask must not be partially
204                          * set. Should be all 1's or all 0's.
205                          */
206                         if ((!rte_is_zero_ether_addr(&eth_mask->src) &&
207                              !rte_is_broadcast_ether_addr(&eth_mask->src)) ||
208                             (!rte_is_zero_ether_addr(&eth_mask->dst) &&
209                              !rte_is_broadcast_ether_addr(&eth_mask->dst))) {
210                                 rte_flow_error_set(error,
211                                                    EINVAL,
212                                                    RTE_FLOW_ERROR_TYPE_ITEM,
213                                                    item,
214                                                    "MAC_addr mask not valid");
215                                 return -rte_errno;
216                         }
217
218                         /* Mask is not allowed. Only exact matches are */
219                         if (eth_mask->type &&
220                             eth_mask->type != RTE_BE16(0xffff)) {
221                                 rte_flow_error_set(error, EINVAL,
222                                                    RTE_FLOW_ERROR_TYPE_ITEM,
223                                                    item,
224                                                    "ethertype mask not valid");
225                                 return -rte_errno;
226                         }
227
228                         if (rte_is_broadcast_ether_addr(&eth_mask->dst)) {
229                                 dst = &eth_spec->dst;
230                                 if (!rte_is_valid_assigned_ether_addr(dst)) {
231                                         rte_flow_error_set(error,
232                                                            EINVAL,
233                                                            RTE_FLOW_ERROR_TYPE_ITEM,
234                                                            item,
235                                                            "DMAC is invalid");
236                                         PMD_DRV_LOG(ERR,
237                                                     "DMAC is invalid!\n");
238                                         return -rte_errno;
239                                 }
240                                 rte_memcpy(filter->dst_macaddr,
241                                            &eth_spec->dst, RTE_ETHER_ADDR_LEN);
242                                 en |= use_ntuple ?
243                                         NTUPLE_FLTR_ALLOC_INPUT_EN_DST_MACADDR :
244                                         EM_FLOW_ALLOC_INPUT_EN_DST_MACADDR;
245                                 valid_flags |= inner ?
246                                         BNXT_FLOW_L2_INNER_DST_VALID_FLAG :
247                                         BNXT_FLOW_L2_DST_VALID_FLAG;
248                                 filter->priority = attr->priority;
249                                 PMD_DRV_LOG(DEBUG,
250                                             "Creating a priority flow\n");
251                         }
252                         if (rte_is_broadcast_ether_addr(&eth_mask->src)) {
253                                 src = &eth_spec->src;
254                                 if (!rte_is_valid_assigned_ether_addr(src)) {
255                                         rte_flow_error_set(error,
256                                                            EINVAL,
257                                                            RTE_FLOW_ERROR_TYPE_ITEM,
258                                                            item,
259                                                            "SMAC is invalid");
260                                         PMD_DRV_LOG(ERR,
261                                                     "SMAC is invalid!\n");
262                                         return -rte_errno;
263                                 }
264                                 rte_memcpy(filter->src_macaddr,
265                                            &eth_spec->src, RTE_ETHER_ADDR_LEN);
266                                 en |= use_ntuple ?
267                                         NTUPLE_FLTR_ALLOC_INPUT_EN_SRC_MACADDR :
268                                         EM_FLOW_ALLOC_INPUT_EN_SRC_MACADDR;
269                                 valid_flags |= inner ?
270                                         BNXT_FLOW_L2_INNER_SRC_VALID_FLAG :
271                                         BNXT_FLOW_L2_SRC_VALID_FLAG;
272                         } /*
273                            * else {
274                            *  PMD_DRV_LOG(ERR, "Handle this condition\n");
275                            * }
276                            */
277                         if (eth_mask->type) {
278                                 filter->ethertype =
279                                         rte_be_to_cpu_16(eth_spec->type);
280                                 en |= en_ethertype;
281                         }
282                         if (inner)
283                                 valid_flags |= BNXT_FLOW_PARSE_INNER_FLAG;
284
285                         break;
286                 case RTE_FLOW_ITEM_TYPE_VLAN:
287                         vlan_spec = item->spec;
288
289                         if (item->mask)
290                                 vlan_mask = item->mask;
291                         else
292                                 vlan_mask = &rte_flow_item_vlan_mask;
293
294                         if (en & en_ethertype) {
295                                 rte_flow_error_set(error, EINVAL,
296                                                    RTE_FLOW_ERROR_TYPE_ITEM,
297                                                    item,
298                                                    "VLAN TPID matching is not"
299                                                    " supported");
300                                 return -rte_errno;
301                         }
302                         if (vlan_mask->tci &&
303                             vlan_mask->tci == RTE_BE16(0x0fff)) {
304                                 /* Only the VLAN ID can be matched. */
305                                 filter->l2_ovlan =
306                                         rte_be_to_cpu_16(vlan_spec->tci &
307                                                          RTE_BE16(0x0fff));
308                                 en |= EM_FLOW_ALLOC_INPUT_EN_OVLAN_VID;
309                         } else {
310                                 rte_flow_error_set(error,
311                                                    EINVAL,
312                                                    RTE_FLOW_ERROR_TYPE_ITEM,
313                                                    item,
314                                                    "VLAN mask is invalid");
315                                 return -rte_errno;
316                         }
317                         if (vlan_mask->inner_type &&
318                             vlan_mask->inner_type != RTE_BE16(0xffff)) {
319                                 rte_flow_error_set(error, EINVAL,
320                                                    RTE_FLOW_ERROR_TYPE_ITEM,
321                                                    item,
322                                                    "inner ethertype mask not"
323                                                    " valid");
324                                 return -rte_errno;
325                         }
326                         if (vlan_mask->inner_type) {
327                                 filter->ethertype =
328                                         rte_be_to_cpu_16(vlan_spec->inner_type);
329                                 en |= en_ethertype;
330                         }
331
332                         break;
333                 case RTE_FLOW_ITEM_TYPE_IPV4:
334                         /* If mask is not involved, we could use EM filters. */
335                         ipv4_spec = item->spec;
336
337                         if (!item->spec)
338                                 break;
339
340                         if (item->mask)
341                                 ipv4_mask = item->mask;
342                         else
343                                 ipv4_mask = &rte_flow_item_ipv4_mask;
344
345                         /* Only IP DST and SRC fields are maskable. */
346                         if (ipv4_mask->hdr.version_ihl ||
347                             ipv4_mask->hdr.type_of_service ||
348                             ipv4_mask->hdr.total_length ||
349                             ipv4_mask->hdr.packet_id ||
350                             ipv4_mask->hdr.fragment_offset ||
351                             ipv4_mask->hdr.time_to_live ||
352                             ipv4_mask->hdr.next_proto_id ||
353                             ipv4_mask->hdr.hdr_checksum) {
354                                 rte_flow_error_set(error,
355                                                    EINVAL,
356                                                    RTE_FLOW_ERROR_TYPE_ITEM,
357                                                    item,
358                                                    "Invalid IPv4 mask.");
359                                 return -rte_errno;
360                         }
361
362                         filter->dst_ipaddr[0] = ipv4_spec->hdr.dst_addr;
363                         filter->src_ipaddr[0] = ipv4_spec->hdr.src_addr;
364
365                         if (use_ntuple)
366                                 en |= NTUPLE_FLTR_ALLOC_INPUT_EN_SRC_IPADDR |
367                                         NTUPLE_FLTR_ALLOC_INPUT_EN_DST_IPADDR;
368                         else
369                                 en |= EM_FLOW_ALLOC_INPUT_EN_SRC_IPADDR |
370                                         EM_FLOW_ALLOC_INPUT_EN_DST_IPADDR;
371
372                         if (ipv4_mask->hdr.src_addr) {
373                                 filter->src_ipaddr_mask[0] =
374                                         ipv4_mask->hdr.src_addr;
375                                 en |= !use_ntuple ? 0 :
376                                      NTUPLE_FLTR_ALLOC_INPUT_EN_SRC_IPADDR_MASK;
377                         }
378
379                         if (ipv4_mask->hdr.dst_addr) {
380                                 filter->dst_ipaddr_mask[0] =
381                                         ipv4_mask->hdr.dst_addr;
382                                 en |= !use_ntuple ? 0 :
383                                      NTUPLE_FLTR_ALLOC_INPUT_EN_DST_IPADDR_MASK;
384                         }
385
386                         filter->ip_addr_type = use_ntuple ?
387                          HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_IP_ADDR_TYPE_IPV4 :
388                          HWRM_CFA_EM_FLOW_ALLOC_INPUT_IP_ADDR_TYPE_IPV4;
389
390                         if (ipv4_spec->hdr.next_proto_id) {
391                                 filter->ip_protocol =
392                                         ipv4_spec->hdr.next_proto_id;
393                                 if (use_ntuple)
394                                         en |= NTUPLE_FLTR_ALLOC_IN_EN_IP_PROTO;
395                                 else
396                                         en |= EM_FLOW_ALLOC_INPUT_EN_IP_PROTO;
397                         }
398                         break;
399                 case RTE_FLOW_ITEM_TYPE_IPV6:
400                         ipv6_spec = item->spec;
401
402                         if (!item->spec)
403                                 break;
404
405                         if (item->mask)
406                                 ipv6_mask = item->mask;
407                         else
408                                 ipv6_mask = &rte_flow_item_ipv6_mask;
409
410                         /* Only IP DST and SRC fields are maskable. */
411                         if (ipv6_mask->hdr.vtc_flow ||
412                             ipv6_mask->hdr.payload_len ||
413                             ipv6_mask->hdr.proto ||
414                             ipv6_mask->hdr.hop_limits) {
415                                 rte_flow_error_set(error,
416                                                    EINVAL,
417                                                    RTE_FLOW_ERROR_TYPE_ITEM,
418                                                    item,
419                                                    "Invalid IPv6 mask.");
420                                 return -rte_errno;
421                         }
422
423                         if (use_ntuple)
424                                 en |= NTUPLE_FLTR_ALLOC_INPUT_EN_SRC_IPADDR |
425                                         NTUPLE_FLTR_ALLOC_INPUT_EN_DST_IPADDR;
426                         else
427                                 en |= EM_FLOW_ALLOC_INPUT_EN_SRC_IPADDR |
428                                         EM_FLOW_ALLOC_INPUT_EN_DST_IPADDR;
429
430                         rte_memcpy(filter->src_ipaddr,
431                                    ipv6_spec->hdr.src_addr, 16);
432                         rte_memcpy(filter->dst_ipaddr,
433                                    ipv6_spec->hdr.dst_addr, 16);
434
435                         if (!bnxt_check_zero_bytes(ipv6_mask->hdr.src_addr,
436                                                    16)) {
437                                 rte_memcpy(filter->src_ipaddr_mask,
438                                            ipv6_mask->hdr.src_addr, 16);
439                                 en |= !use_ntuple ? 0 :
440                                     NTUPLE_FLTR_ALLOC_INPUT_EN_SRC_IPADDR_MASK;
441                         }
442
443                         if (!bnxt_check_zero_bytes(ipv6_mask->hdr.dst_addr,
444                                                    16)) {
445                                 rte_memcpy(filter->dst_ipaddr_mask,
446                                            ipv6_mask->hdr.dst_addr, 16);
447                                 en |= !use_ntuple ? 0 :
448                                      NTUPLE_FLTR_ALLOC_INPUT_EN_DST_IPADDR_MASK;
449                         }
450
451                         filter->ip_addr_type = use_ntuple ?
452                                 NTUPLE_FLTR_ALLOC_INPUT_IP_ADDR_TYPE_IPV6 :
453                                 EM_FLOW_ALLOC_INPUT_IP_ADDR_TYPE_IPV6;
454                         break;
455                 case RTE_FLOW_ITEM_TYPE_TCP:
456                         tcp_spec = item->spec;
457
458                         if (!item->spec)
459                                 break;
460
461                         if (item->mask)
462                                 tcp_mask = item->mask;
463                         else
464                                 tcp_mask = &rte_flow_item_tcp_mask;
465
466                         /* Check TCP mask. Only DST & SRC ports are maskable */
467                         if (tcp_mask->hdr.sent_seq ||
468                             tcp_mask->hdr.recv_ack ||
469                             tcp_mask->hdr.data_off ||
470                             tcp_mask->hdr.tcp_flags ||
471                             tcp_mask->hdr.rx_win ||
472                             tcp_mask->hdr.cksum ||
473                             tcp_mask->hdr.tcp_urp) {
474                                 rte_flow_error_set(error,
475                                                    EINVAL,
476                                                    RTE_FLOW_ERROR_TYPE_ITEM,
477                                                    item,
478                                                    "Invalid TCP mask");
479                                 return -rte_errno;
480                         }
481
482                         filter->src_port = tcp_spec->hdr.src_port;
483                         filter->dst_port = tcp_spec->hdr.dst_port;
484
485                         if (use_ntuple)
486                                 en |= NTUPLE_FLTR_ALLOC_INPUT_EN_SRC_PORT |
487                                         NTUPLE_FLTR_ALLOC_INPUT_EN_DST_PORT;
488                         else
489                                 en |= EM_FLOW_ALLOC_INPUT_EN_SRC_PORT |
490                                         EM_FLOW_ALLOC_INPUT_EN_DST_PORT;
491
492                         if (tcp_mask->hdr.dst_port) {
493                                 filter->dst_port_mask = tcp_mask->hdr.dst_port;
494                                 en |= !use_ntuple ? 0 :
495                                   NTUPLE_FLTR_ALLOC_INPUT_EN_DST_PORT_MASK;
496                         }
497
498                         if (tcp_mask->hdr.src_port) {
499                                 filter->src_port_mask = tcp_mask->hdr.src_port;
500                                 en |= !use_ntuple ? 0 :
501                                   NTUPLE_FLTR_ALLOC_INPUT_EN_SRC_PORT_MASK;
502                         }
503                         break;
504                 case RTE_FLOW_ITEM_TYPE_UDP:
505                         udp_spec = item->spec;
506
507                         if (!item->spec)
508                                 break;
509
510                         if (item->mask)
511                                 udp_mask = item->mask;
512                         else
513                                 udp_mask = &rte_flow_item_udp_mask;
514
515                         if (udp_mask->hdr.dgram_len ||
516                             udp_mask->hdr.dgram_cksum) {
517                                 rte_flow_error_set(error,
518                                                    EINVAL,
519                                                    RTE_FLOW_ERROR_TYPE_ITEM,
520                                                    item,
521                                                    "Invalid UDP mask");
522                                 return -rte_errno;
523                         }
524
525                         filter->src_port = udp_spec->hdr.src_port;
526                         filter->dst_port = udp_spec->hdr.dst_port;
527
528                         if (use_ntuple)
529                                 en |= NTUPLE_FLTR_ALLOC_INPUT_EN_SRC_PORT |
530                                         NTUPLE_FLTR_ALLOC_INPUT_EN_DST_PORT;
531                         else
532                                 en |= EM_FLOW_ALLOC_INPUT_EN_SRC_PORT |
533                                         EM_FLOW_ALLOC_INPUT_EN_DST_PORT;
534
535                         if (udp_mask->hdr.dst_port) {
536                                 filter->dst_port_mask = udp_mask->hdr.dst_port;
537                                 en |= !use_ntuple ? 0 :
538                                   NTUPLE_FLTR_ALLOC_INPUT_EN_DST_PORT_MASK;
539                         }
540
541                         if (udp_mask->hdr.src_port) {
542                                 filter->src_port_mask = udp_mask->hdr.src_port;
543                                 en |= !use_ntuple ? 0 :
544                                   NTUPLE_FLTR_ALLOC_INPUT_EN_SRC_PORT_MASK;
545                         }
546                         break;
547                 case RTE_FLOW_ITEM_TYPE_VXLAN:
548                         vxlan_spec = item->spec;
549                         vxlan_mask = item->mask;
550                         /* Check if VXLAN item is used to describe protocol.
551                          * If yes, both spec and mask should be NULL.
552                          * If no, both spec and mask shouldn't be NULL.
553                          */
554                         if ((!vxlan_spec && vxlan_mask) ||
555                             (vxlan_spec && !vxlan_mask)) {
556                                 rte_flow_error_set(error,
557                                                    EINVAL,
558                                                    RTE_FLOW_ERROR_TYPE_ITEM,
559                                                    item,
560                                                    "Invalid VXLAN item");
561                                 return -rte_errno;
562                         }
563
564                         if (!vxlan_spec && !vxlan_mask) {
565                                 filter->tunnel_type =
566                                 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
567                                 break;
568                         }
569
570                         if (vxlan_spec->rsvd1 || vxlan_spec->rsvd0[0] ||
571                             vxlan_spec->rsvd0[1] || vxlan_spec->rsvd0[2] ||
572                             vxlan_spec->flags != 0x8) {
573                                 rte_flow_error_set(error,
574                                                    EINVAL,
575                                                    RTE_FLOW_ERROR_TYPE_ITEM,
576                                                    item,
577                                                    "Invalid VXLAN item");
578                                 return -rte_errno;
579                         }
580
581                         /* Check if VNI is masked. */
582                         if (vxlan_mask != NULL) {
583                                 vni_masked =
584                                         !!memcmp(vxlan_mask->vni, vni_mask,
585                                                  RTE_DIM(vni_mask));
586                                 if (vni_masked) {
587                                         rte_flow_error_set
588                                                 (error,
589                                                  EINVAL,
590                                                  RTE_FLOW_ERROR_TYPE_ITEM,
591                                                  item,
592                                                  "Invalid VNI mask");
593                                         return -rte_errno;
594                                 }
595
596                                 rte_memcpy(((uint8_t *)&tenant_id_be + 1),
597                                            vxlan_spec->vni, 3);
598                                 filter->vni =
599                                         rte_be_to_cpu_32(tenant_id_be);
600                                 filter->tunnel_type =
601                                  CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
602                         }
603                         break;
604                 case RTE_FLOW_ITEM_TYPE_NVGRE:
605                         nvgre_spec = item->spec;
606                         nvgre_mask = item->mask;
607                         /* Check if NVGRE item is used to describe protocol.
608                          * If yes, both spec and mask should be NULL.
609                          * If no, both spec and mask shouldn't be NULL.
610                          */
611                         if ((!nvgre_spec && nvgre_mask) ||
612                             (nvgre_spec && !nvgre_mask)) {
613                                 rte_flow_error_set(error,
614                                                    EINVAL,
615                                                    RTE_FLOW_ERROR_TYPE_ITEM,
616                                                    item,
617                                                    "Invalid NVGRE item");
618                                 return -rte_errno;
619                         }
620
621                         if (!nvgre_spec && !nvgre_mask) {
622                                 filter->tunnel_type =
623                                 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_NVGRE;
624                                 break;
625                         }
626
627                         if (nvgre_spec->c_k_s_rsvd0_ver != 0x2000 ||
628                             nvgre_spec->protocol != 0x6558) {
629                                 rte_flow_error_set(error,
630                                                    EINVAL,
631                                                    RTE_FLOW_ERROR_TYPE_ITEM,
632                                                    item,
633                                                    "Invalid NVGRE item");
634                                 return -rte_errno;
635                         }
636
637                         if (nvgre_spec && nvgre_mask) {
638                                 tni_masked =
639                                         !!memcmp(nvgre_mask->tni, tni_mask,
640                                                  RTE_DIM(tni_mask));
641                                 if (tni_masked) {
642                                         rte_flow_error_set
643                                                 (error,
644                                                  EINVAL,
645                                                  RTE_FLOW_ERROR_TYPE_ITEM,
646                                                  item,
647                                                  "Invalid TNI mask");
648                                         return -rte_errno;
649                                 }
650                                 rte_memcpy(((uint8_t *)&tenant_id_be + 1),
651                                            nvgre_spec->tni, 3);
652                                 filter->vni =
653                                         rte_be_to_cpu_32(tenant_id_be);
654                                 filter->tunnel_type =
655                                  CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_NVGRE;
656                         }
657                         break;
658
659                 case RTE_FLOW_ITEM_TYPE_GRE:
660                         gre_spec = (const struct rte_flow_item_gre *)item->spec;
661                         gre_mask = (const struct rte_flow_item_gre *)item->mask;
662
663                         /*
664                          *Check if GRE item is used to describe protocol.
665                          * If yes, both spec and mask should be NULL.
666                          * If no, both spec and mask shouldn't be NULL.
667                          */
668                         if (!!gre_spec ^ !!gre_mask) {
669                                 rte_flow_error_set(error, EINVAL,
670                                                    RTE_FLOW_ERROR_TYPE_ITEM,
671                                                    item,
672                                                    "Invalid GRE item");
673                                 return -rte_errno;
674                         }
675
676                         if (!gre_spec && !gre_mask) {
677                                 filter->tunnel_type =
678                                 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_IPGRE;
679                                 break;
680                         }
681                         break;
682
683                 case RTE_FLOW_ITEM_TYPE_VF:
684                         vf_spec = item->spec;
685                         vf = vf_spec->id;
686                         if (!BNXT_PF(bp)) {
687                                 rte_flow_error_set(error,
688                                                    EINVAL,
689                                                    RTE_FLOW_ERROR_TYPE_ITEM,
690                                                    item,
691                                                    "Configuring on a VF!");
692                                 return -rte_errno;
693                         }
694
695                         if (vf >= bp->pdev->max_vfs) {
696                                 rte_flow_error_set(error,
697                                                    EINVAL,
698                                                    RTE_FLOW_ERROR_TYPE_ITEM,
699                                                    item,
700                                                    "Incorrect VF id!");
701                                 return -rte_errno;
702                         }
703
704                         if (!attr->transfer) {
705                                 rte_flow_error_set(error,
706                                                    ENOTSUP,
707                                                    RTE_FLOW_ERROR_TYPE_ITEM,
708                                                    item,
709                                                    "Matching VF traffic without"
710                                                    " affecting it (transfer attribute)"
711                                                    " is unsupported");
712                                 return -rte_errno;
713                         }
714
715                         filter->mirror_vnic_id =
716                         dflt_vnic = bnxt_hwrm_func_qcfg_vf_dflt_vnic_id(bp, vf);
717                         if (dflt_vnic < 0) {
718                                 /* This simply indicates there's no driver
719                                  * loaded. This is not an error.
720                                  */
721                                 rte_flow_error_set
722                                         (error,
723                                          EINVAL,
724                                          RTE_FLOW_ERROR_TYPE_ITEM,
725                                          item,
726                                          "Unable to get default VNIC for VF");
727                                 return -rte_errno;
728                         }
729
730                         filter->mirror_vnic_id = dflt_vnic;
731                         en |= NTUPLE_FLTR_ALLOC_INPUT_EN_MIRROR_VNIC_ID;
732                         break;
733                 default:
734                         break;
735                 }
736                 item++;
737         }
738         filter->enables = en;
739         filter->valid_flags = valid_flags;
740
741         return 0;
742 }
743
744 /* Parse attributes */
745 static int
746 bnxt_flow_parse_attr(const struct rte_flow_attr *attr,
747                      struct rte_flow_error *error)
748 {
749         /* Must be input direction */
750         if (!attr->ingress) {
751                 rte_flow_error_set(error,
752                                    EINVAL,
753                                    RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
754                                    attr,
755                                    "Only support ingress.");
756                 return -rte_errno;
757         }
758
759         /* Not supported */
760         if (attr->egress) {
761                 rte_flow_error_set(error,
762                                    EINVAL,
763                                    RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
764                                    attr,
765                                    "No support for egress.");
766                 return -rte_errno;
767         }
768
769         return 0;
770 }
771
772 static struct bnxt_filter_info *
773 bnxt_find_matching_l2_filter(struct bnxt *bp, struct bnxt_filter_info *nf)
774 {
775         struct bnxt_filter_info *mf, *f0;
776         struct bnxt_vnic_info *vnic0;
777         int i;
778
779         vnic0 = BNXT_GET_DEFAULT_VNIC(bp);
780         f0 = STAILQ_FIRST(&vnic0->filter);
781
782         /* This flow has same DST MAC as the port/l2 filter. */
783         if (memcmp(f0->l2_addr, nf->dst_macaddr, RTE_ETHER_ADDR_LEN) == 0)
784                 return f0;
785
786         for (i = bp->max_vnics - 1; i >= 0; i--) {
787                 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
788
789                 if (vnic->fw_vnic_id == INVALID_VNIC_ID)
790                         continue;
791
792                 STAILQ_FOREACH(mf, &vnic->filter, next) {
793
794                         if (mf->matching_l2_fltr_ptr)
795                                 continue;
796
797                         if (mf->ethertype == nf->ethertype &&
798                             mf->l2_ovlan == nf->l2_ovlan &&
799                             mf->l2_ovlan_mask == nf->l2_ovlan_mask &&
800                             mf->l2_ivlan == nf->l2_ivlan &&
801                             mf->l2_ivlan_mask == nf->l2_ivlan_mask &&
802                             !memcmp(mf->src_macaddr, nf->src_macaddr,
803                                     RTE_ETHER_ADDR_LEN) &&
804                             !memcmp(mf->dst_macaddr, nf->dst_macaddr,
805                                     RTE_ETHER_ADDR_LEN))
806                                 return mf;
807                 }
808         }
809         return NULL;
810 }
811
812 static struct bnxt_filter_info *
813 bnxt_create_l2_filter(struct bnxt *bp, struct bnxt_filter_info *nf,
814                       struct bnxt_vnic_info *vnic)
815 {
816         struct bnxt_filter_info *filter1;
817         int rc;
818
819         /* Alloc new L2 filter.
820          * This flow needs MAC filter which does not match any existing
821          * L2 filters.
822          */
823         filter1 = bnxt_get_unused_filter(bp);
824         if (filter1 == NULL)
825                 return NULL;
826
827         memcpy(filter1, nf, sizeof(*filter1));
828
829         filter1->flags = HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_XDP_DISABLE;
830         filter1->flags |= HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_PATH_RX;
831         if (nf->valid_flags & BNXT_FLOW_L2_SRC_VALID_FLAG ||
832             nf->valid_flags & BNXT_FLOW_L2_DST_VALID_FLAG) {
833                 filter1->flags |=
834                         HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_OUTERMOST;
835                 PMD_DRV_LOG(DEBUG, "Create Outer filter\n");
836         }
837
838         if (nf->filter_type == HWRM_CFA_L2_FILTER &&
839             (nf->valid_flags & BNXT_FLOW_L2_SRC_VALID_FLAG ||
840              nf->valid_flags & BNXT_FLOW_L2_INNER_SRC_VALID_FLAG)) {
841                 PMD_DRV_LOG(DEBUG, "Create L2 filter for SRC MAC\n");
842                 filter1->flags |=
843                         HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_SOURCE_VALID;
844                 memcpy(filter1->l2_addr, nf->src_macaddr, RTE_ETHER_ADDR_LEN);
845         } else {
846                 PMD_DRV_LOG(DEBUG, "Create L2 filter for DST MAC\n");
847                 memcpy(filter1->l2_addr, nf->dst_macaddr, RTE_ETHER_ADDR_LEN);
848         }
849
850         if (nf->priority &&
851             (nf->valid_flags & BNXT_FLOW_L2_DST_VALID_FLAG ||
852              nf->valid_flags & BNXT_FLOW_L2_INNER_DST_VALID_FLAG)) {
853                 /* Tell the FW where to place the filter in the table. */
854                 if (nf->priority > 65535) {
855                         filter1->pri_hint =
856                         HWRM_CFA_L2_FILTER_ALLOC_INPUT_PRI_HINT_BELOW_FILTER;
857                         /* This will place the filter in TCAM */
858                         filter1->l2_filter_id_hint = (uint64_t)-1;
859                 }
860         }
861
862         if (nf->valid_flags & (BNXT_FLOW_L2_DST_VALID_FLAG |
863                                BNXT_FLOW_L2_SRC_VALID_FLAG |
864                                BNXT_FLOW_L2_INNER_SRC_VALID_FLAG |
865                                BNXT_FLOW_L2_INNER_DST_VALID_FLAG)) {
866                 filter1->enables =
867                         HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR |
868                         L2_FILTER_ALLOC_INPUT_EN_L2_ADDR_MASK;
869                 memset(filter1->l2_addr_mask, 0xff, RTE_ETHER_ADDR_LEN);
870         }
871
872         if (nf->valid_flags & BNXT_FLOW_L2_DROP_FLAG) {
873                 filter1->flags |=
874                         HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_DROP;
875                 if (nf->ethertype == RTE_ETHER_TYPE_IPV4) {
876                         /* Num VLANs for drop filter will/should be 0.
877                          * If the req is memset to 0, then the count will
878                          * be automatically set to 0.
879                          */
880                         if (nf->valid_flags & BNXT_FLOW_PARSE_INNER_FLAG) {
881                                 filter1->enables |=
882                                         L2_FILTER_ALLOC_INPUT_EN_T_NUM_VLANS;
883                         } else {
884                                 filter1->enables |=
885                                         L2_FILTER_ALLOC_INPUT_EN_NUM_VLANS;
886                                 filter1->flags |=
887                                 HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_OUTERMOST;
888                         }
889                 }
890         }
891
892         rc = bnxt_hwrm_set_l2_filter(bp, vnic->fw_vnic_id,
893                                      filter1);
894         if (rc) {
895                 bnxt_free_filter(bp, filter1);
896                 return NULL;
897         }
898         return filter1;
899 }
900
901 struct bnxt_filter_info *
902 bnxt_get_l2_filter(struct bnxt *bp, struct bnxt_filter_info *nf,
903                    struct bnxt_vnic_info *vnic)
904 {
905         struct bnxt_filter_info *l2_filter = NULL;
906
907         l2_filter = bnxt_find_matching_l2_filter(bp, nf);
908         if (l2_filter) {
909                 l2_filter->l2_ref_cnt++;
910         } else {
911                 l2_filter = bnxt_create_l2_filter(bp, nf, vnic);
912                 if (l2_filter) {
913                         STAILQ_INSERT_TAIL(&vnic->filter, l2_filter, next);
914                         l2_filter->vnic = vnic;
915                 }
916         }
917         nf->matching_l2_fltr_ptr = l2_filter;
918
919         return l2_filter;
920 }
921
922 static void bnxt_vnic_cleanup(struct bnxt *bp, struct bnxt_vnic_info *vnic)
923 {
924         if (vnic->rx_queue_cnt > 1)
925                 bnxt_hwrm_vnic_ctx_free(bp, vnic);
926
927         bnxt_hwrm_vnic_free(bp, vnic);
928
929         rte_free(vnic->fw_grp_ids);
930         vnic->fw_grp_ids = NULL;
931
932         vnic->rx_queue_cnt = 0;
933 }
934
935 static int bnxt_vnic_prep(struct bnxt *bp, struct bnxt_vnic_info *vnic,
936                           const struct rte_flow_action *act,
937                           struct rte_flow_error *error)
938 {
939         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
940         uint64_t rx_offloads = dev_conf->rxmode.offloads;
941         int rc;
942
943         if (bp->nr_vnics > bp->max_vnics - 1)
944                 return rte_flow_error_set(error, EINVAL,
945                                           RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
946                                           NULL,
947                                           "Group id is invalid");
948
949         rc = bnxt_vnic_grp_alloc(bp, vnic);
950         if (rc)
951                 return rte_flow_error_set(error, -rc,
952                                           RTE_FLOW_ERROR_TYPE_ACTION,
953                                           act,
954                                           "Failed to alloc VNIC group");
955
956         rc = bnxt_hwrm_vnic_alloc(bp, vnic);
957         if (rc) {
958                 rte_flow_error_set(error, -rc,
959                                    RTE_FLOW_ERROR_TYPE_ACTION,
960                                    act,
961                                    "Failed to alloc VNIC");
962                 goto ret;
963         }
964
965         /* RSS context is required only when there is more than one RSS ring */
966         if (vnic->rx_queue_cnt > 1) {
967                 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
968                 if (rc) {
969                         rte_flow_error_set(error, -rc,
970                                            RTE_FLOW_ERROR_TYPE_ACTION,
971                                            act,
972                                            "Failed to alloc VNIC context");
973                         goto ret;
974                 }
975         }
976
977         if (rx_offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
978                 vnic->vlan_strip = true;
979         else
980                 vnic->vlan_strip = false;
981
982         rc = bnxt_hwrm_vnic_cfg(bp, vnic);
983         if (rc) {
984                 rte_flow_error_set(error, -rc,
985                                    RTE_FLOW_ERROR_TYPE_ACTION,
986                                    act,
987                                    "Failed to configure VNIC");
988                 goto ret;
989         }
990
991         rc = bnxt_hwrm_vnic_plcmode_cfg(bp, vnic);
992         if (rc) {
993                 rte_flow_error_set(error, -rc,
994                                    RTE_FLOW_ERROR_TYPE_ACTION,
995                                    act,
996                                    "Failed to configure VNIC plcmode");
997                 goto ret;
998         }
999
1000         bp->nr_vnics++;
1001
1002         return 0;
1003
1004 ret:
1005         bnxt_vnic_cleanup(bp, vnic);
1006         return rc;
1007 }
1008
1009 static int match_vnic_rss_cfg(struct bnxt *bp,
1010                               struct bnxt_vnic_info *vnic,
1011                               const struct rte_flow_action_rss *rss)
1012 {
1013         unsigned int match = 0, i;
1014
1015         if (vnic->rx_queue_cnt != rss->queue_num)
1016                 return -EINVAL;
1017
1018         for (i = 0; i < rss->queue_num; i++) {
1019                 if (!bp->rx_queues[rss->queue[i]]->vnic->rx_queue_cnt &&
1020                     !bp->rx_queues[rss->queue[i]]->rx_started)
1021                         return -EINVAL;
1022         }
1023
1024         for (i = 0; i < vnic->rx_queue_cnt; i++) {
1025                 int j;
1026
1027                 for (j = 0; j < vnic->rx_queue_cnt; j++) {
1028                         if (bp->grp_info[rss->queue[i]].fw_grp_id ==
1029                             vnic->fw_grp_ids[j])
1030                                 match++;
1031                 }
1032         }
1033
1034         if (match != vnic->rx_queue_cnt) {
1035                 PMD_DRV_LOG(ERR,
1036                             "VNIC queue count %d vs queues matched %d\n",
1037                             match, vnic->rx_queue_cnt);
1038                 return -EINVAL;
1039         }
1040
1041         return 0;
1042 }
1043
1044 static void
1045 bnxt_update_filter_flags_en(struct bnxt_filter_info *filter,
1046                             struct bnxt_filter_info *filter1,
1047                             int use_ntuple)
1048 {
1049         if (!use_ntuple &&
1050             !(filter->valid_flags &
1051               ~(BNXT_FLOW_L2_DST_VALID_FLAG |
1052                 BNXT_FLOW_L2_SRC_VALID_FLAG |
1053                 BNXT_FLOW_L2_INNER_SRC_VALID_FLAG |
1054                 BNXT_FLOW_L2_INNER_DST_VALID_FLAG |
1055                 BNXT_FLOW_L2_DROP_FLAG |
1056                 BNXT_FLOW_PARSE_INNER_FLAG))) {
1057                 filter->flags = filter1->flags;
1058                 filter->enables = filter1->enables;
1059                 filter->filter_type = HWRM_CFA_L2_FILTER;
1060                 memcpy(filter->l2_addr, filter1->l2_addr, RTE_ETHER_ADDR_LEN);
1061                 memset(filter->l2_addr_mask, 0xff, RTE_ETHER_ADDR_LEN);
1062                 filter->pri_hint = filter1->pri_hint;
1063                 filter->l2_filter_id_hint = filter1->l2_filter_id_hint;
1064         }
1065         filter->fw_l2_filter_id = filter1->fw_l2_filter_id;
1066         filter->l2_ref_cnt = filter1->l2_ref_cnt;
1067         filter->flow_id = filter1->flow_id;
1068         PMD_DRV_LOG(DEBUG,
1069                 "l2_filter: %p fw_l2_filter_id %" PRIx64 " l2_ref_cnt %u\n",
1070                 filter1, filter->fw_l2_filter_id, filter->l2_ref_cnt);
1071 }
1072
1073 static int
1074 bnxt_validate_and_parse_flow(struct rte_eth_dev *dev,
1075                              const struct rte_flow_item pattern[],
1076                              const struct rte_flow_action actions[],
1077                              const struct rte_flow_attr *attr,
1078                              struct rte_flow_error *error,
1079                              struct bnxt_filter_info *filter)
1080 {
1081         const struct rte_flow_action *act =
1082                 bnxt_flow_non_void_action(actions);
1083         struct bnxt *bp = dev->data->dev_private;
1084         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
1085         struct bnxt_vnic_info *vnic = NULL, *vnic0 = NULL;
1086         const struct rte_flow_action_queue *act_q;
1087         const struct rte_flow_action_vf *act_vf;
1088         struct bnxt_filter_info *filter1 = NULL;
1089         const struct rte_flow_action_rss *rss;
1090         struct bnxt_rx_queue *rxq = NULL;
1091         int dflt_vnic, vnic_id;
1092         unsigned int rss_idx;
1093         uint32_t vf = 0, i;
1094         int rc, use_ntuple;
1095
1096         rc =
1097         bnxt_validate_and_parse_flow_type(bp, attr, pattern, error, filter);
1098         if (rc != 0)
1099                 goto ret;
1100
1101         rc = bnxt_flow_parse_attr(attr, error);
1102         if (rc != 0)
1103                 goto ret;
1104
1105         /* Since we support ingress attribute only - right now. */
1106         if (filter->filter_type == HWRM_CFA_EM_FILTER)
1107                 filter->flags = HWRM_CFA_EM_FLOW_ALLOC_INPUT_FLAGS_PATH_RX;
1108
1109         use_ntuple = bnxt_filter_type_check(pattern, error);
1110
1111 start:
1112         switch (act->type) {
1113         case RTE_FLOW_ACTION_TYPE_QUEUE:
1114                 /* Allow this flow. Redirect to a VNIC. */
1115                 act_q = (const struct rte_flow_action_queue *)act->conf;
1116                 if (!act_q->index || act_q->index >= bp->rx_nr_rings) {
1117                         rte_flow_error_set(error,
1118                                            EINVAL,
1119                                            RTE_FLOW_ERROR_TYPE_ACTION,
1120                                            act,
1121                                            "Invalid queue ID.");
1122                         rc = -rte_errno;
1123                         goto ret;
1124                 }
1125                 PMD_DRV_LOG(DEBUG, "Queue index %d\n", act_q->index);
1126
1127                 if (use_ntuple && !BNXT_RFS_NEEDS_VNIC(bp)) {
1128                         filter->flags =
1129                                 HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_FLAGS_DEST_RFS_RING_IDX;
1130                         filter->dst_id = act_q->index;
1131                         goto skip_vnic_alloc;
1132                 }
1133
1134                 vnic_id = attr->group;
1135                 if (!vnic_id) {
1136                         PMD_DRV_LOG(DEBUG, "Group id is 0\n");
1137                         vnic_id = act_q->index;
1138                 }
1139
1140                 BNXT_VALID_VNIC_OR_RET(bp, vnic_id);
1141
1142                 vnic = &bp->vnic_info[vnic_id];
1143                 if (vnic->rx_queue_cnt) {
1144                         if (vnic->start_grp_id != act_q->index) {
1145                                 PMD_DRV_LOG(ERR,
1146                                             "VNIC already in use\n");
1147                                 rte_flow_error_set(error,
1148                                                    EINVAL,
1149                                                    RTE_FLOW_ERROR_TYPE_ACTION,
1150                                                    act,
1151                                                    "VNIC already in use");
1152                                 rc = -rte_errno;
1153                                 goto ret;
1154                         }
1155                         goto use_vnic;
1156                 }
1157
1158                 rxq = bp->rx_queues[act_q->index];
1159
1160                 if (!(dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS) && rxq &&
1161                     vnic->fw_vnic_id != INVALID_HW_RING_ID)
1162                         goto use_vnic;
1163
1164                 if (!rxq) {
1165                         PMD_DRV_LOG(ERR,
1166                                     "Queue invalid or used with other VNIC\n");
1167                         rte_flow_error_set(error,
1168                                            EINVAL,
1169                                            RTE_FLOW_ERROR_TYPE_ACTION,
1170                                            act,
1171                                            "Queue invalid queue or in use");
1172                         rc = -rte_errno;
1173                         goto ret;
1174                 }
1175
1176                 rxq->vnic = vnic;
1177                 rxq->rx_started = 1;
1178                 vnic->rx_queue_cnt++;
1179                 vnic->start_grp_id = act_q->index;
1180                 vnic->end_grp_id = act_q->index;
1181                 vnic->func_default = 0; //This is not a default VNIC.
1182
1183                 PMD_DRV_LOG(DEBUG, "VNIC found\n");
1184
1185                 rc = bnxt_vnic_prep(bp, vnic, act, error);
1186                 if (rc)
1187                         goto ret;
1188
1189                 PMD_DRV_LOG(DEBUG,
1190                             "vnic[%d] = %p vnic->fw_grp_ids = %p\n",
1191                             act_q->index, vnic, vnic->fw_grp_ids);
1192
1193 use_vnic:
1194                 vnic->ff_pool_idx = vnic_id;
1195                 PMD_DRV_LOG(DEBUG,
1196                             "Setting vnic ff_idx %d\n", vnic->ff_pool_idx);
1197                 filter->dst_id = vnic->fw_vnic_id;
1198 skip_vnic_alloc:
1199                 /* For ntuple filter, create the L2 filter with default VNIC.
1200                  * The user specified redirect queue will be set while creating
1201                  * the ntuple filter in hardware.
1202                  */
1203                 vnic0 = BNXT_GET_DEFAULT_VNIC(bp);
1204                 if (use_ntuple)
1205                         filter1 = bnxt_get_l2_filter(bp, filter, vnic0);
1206                 else
1207                         filter1 = bnxt_get_l2_filter(bp, filter, vnic);
1208                 if (filter1 == NULL) {
1209                         rte_flow_error_set(error,
1210                                            ENOSPC,
1211                                            RTE_FLOW_ERROR_TYPE_ACTION,
1212                                            act,
1213                                            "Filter not available");
1214                         rc = -rte_errno;
1215                         goto ret;
1216                 }
1217
1218                 PMD_DRV_LOG(DEBUG, "new fltr: %p l2fltr: %p l2_ref_cnt: %d\n",
1219                             filter, filter1, filter1->l2_ref_cnt);
1220                 bnxt_update_filter_flags_en(filter, filter1, use_ntuple);
1221                 break;
1222         case RTE_FLOW_ACTION_TYPE_DROP:
1223                 vnic0 = &bp->vnic_info[0];
1224                 filter->dst_id = vnic0->fw_vnic_id;
1225                 filter->valid_flags |= BNXT_FLOW_L2_DROP_FLAG;
1226                 filter1 = bnxt_get_l2_filter(bp, filter, vnic0);
1227                 if (filter1 == NULL) {
1228                         rte_flow_error_set(error,
1229                                            ENOSPC,
1230                                            RTE_FLOW_ERROR_TYPE_ACTION,
1231                                            act,
1232                                            "Filter not available");
1233                         rc = -rte_errno;
1234                         goto ret;
1235                 }
1236
1237                 if (filter->filter_type == HWRM_CFA_EM_FILTER)
1238                         filter->flags =
1239                                 HWRM_CFA_EM_FLOW_ALLOC_INPUT_FLAGS_DROP;
1240                 else if (filter->filter_type == HWRM_CFA_NTUPLE_FILTER)
1241                         filter->flags =
1242                                 HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_FLAGS_DROP;
1243
1244                 bnxt_update_filter_flags_en(filter, filter1, use_ntuple);
1245                 break;
1246         case RTE_FLOW_ACTION_TYPE_COUNT:
1247                 vnic0 = &bp->vnic_info[0];
1248                 filter1 = bnxt_get_l2_filter(bp, filter, vnic0);
1249                 if (filter1 == NULL) {
1250                         rte_flow_error_set(error,
1251                                            ENOSPC,
1252                                            RTE_FLOW_ERROR_TYPE_ACTION,
1253                                            act,
1254                                            "New filter not available");
1255                         rc = -rte_errno;
1256                         goto ret;
1257                 }
1258
1259                 filter->fw_l2_filter_id = filter1->fw_l2_filter_id;
1260                 filter->flow_id = filter1->flow_id;
1261                 filter->flags = HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_FLAGS_METER;
1262                 break;
1263         case RTE_FLOW_ACTION_TYPE_VF:
1264                 act_vf = (const struct rte_flow_action_vf *)act->conf;
1265                 vf = act_vf->id;
1266
1267                 if (filter->tunnel_type ==
1268                     CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_VXLAN ||
1269                     filter->tunnel_type ==
1270                     CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_IPGRE) {
1271                         /* If issued on a VF, ensure id is 0 and is trusted */
1272                         if (BNXT_VF(bp)) {
1273                                 if (!BNXT_VF_IS_TRUSTED(bp) || vf) {
1274                                         rte_flow_error_set(error, EINVAL,
1275                                                 RTE_FLOW_ERROR_TYPE_ACTION,
1276                                                 act,
1277                                                 "Incorrect VF");
1278                                         rc = -rte_errno;
1279                                         goto ret;
1280                                 }
1281                         }
1282
1283                         filter->enables |= filter->tunnel_type;
1284                         filter->filter_type = HWRM_CFA_TUNNEL_REDIRECT_FILTER;
1285                         goto done;
1286                 }
1287
1288                 if (vf >= bp->pdev->max_vfs) {
1289                         rte_flow_error_set(error,
1290                                            EINVAL,
1291                                            RTE_FLOW_ERROR_TYPE_ACTION,
1292                                            act,
1293                                            "Incorrect VF id!");
1294                         rc = -rte_errno;
1295                         goto ret;
1296                 }
1297
1298                 filter->mirror_vnic_id =
1299                 dflt_vnic = bnxt_hwrm_func_qcfg_vf_dflt_vnic_id(bp, vf);
1300                 if (dflt_vnic < 0) {
1301                         /* This simply indicates there's no driver loaded.
1302                          * This is not an error.
1303                          */
1304                         rte_flow_error_set(error,
1305                                            EINVAL,
1306                                            RTE_FLOW_ERROR_TYPE_ACTION,
1307                                            act,
1308                                            "Unable to get default VNIC for VF");
1309                         rc = -rte_errno;
1310                         goto ret;
1311                 }
1312
1313                 filter->mirror_vnic_id = dflt_vnic;
1314                 filter->enables |= NTUPLE_FLTR_ALLOC_INPUT_EN_MIRROR_VNIC_ID;
1315
1316                 vnic0 = &bp->vnic_info[0];
1317                 filter1 = bnxt_get_l2_filter(bp, filter, vnic0);
1318                 if (filter1 == NULL) {
1319                         rte_flow_error_set(error,
1320                                            ENOSPC,
1321                                            RTE_FLOW_ERROR_TYPE_ACTION,
1322                                            act,
1323                                            "New filter not available");
1324                         rc = -rte_errno;
1325                         goto ret;
1326                 }
1327
1328                 filter->fw_l2_filter_id = filter1->fw_l2_filter_id;
1329                 filter->flow_id = filter1->flow_id;
1330                 break;
1331         case RTE_FLOW_ACTION_TYPE_RSS:
1332                 rss = (const struct rte_flow_action_rss *)act->conf;
1333
1334                 vnic_id = attr->group;
1335
1336                 BNXT_VALID_VNIC_OR_RET(bp, vnic_id);
1337                 vnic = &bp->vnic_info[vnic_id];
1338
1339                 /* Check if requested RSS config matches RSS config of VNIC
1340                  * only if it is not a fresh VNIC configuration.
1341                  * Otherwise the existing VNIC configuration can be used.
1342                  */
1343                 if (vnic->rx_queue_cnt) {
1344                         rc = match_vnic_rss_cfg(bp, vnic, rss);
1345                         if (rc) {
1346                                 PMD_DRV_LOG(ERR,
1347                                             "VNIC and RSS config mismatch\n");
1348                                 rte_flow_error_set(error,
1349                                                    EINVAL,
1350                                                    RTE_FLOW_ERROR_TYPE_ACTION,
1351                                                    act,
1352                                                    "VNIC and RSS cfg mismatch");
1353                                 rc = -rte_errno;
1354                                 goto ret;
1355                         }
1356                         goto vnic_found;
1357                 }
1358
1359                 for (i = 0; i < rss->queue_num; i++) {
1360                         PMD_DRV_LOG(DEBUG, "RSS action Queue %d\n",
1361                                     rss->queue[i]);
1362
1363                         if (!rss->queue[i] ||
1364                             rss->queue[i] >= bp->rx_nr_rings ||
1365                             !bp->rx_queues[rss->queue[i]]) {
1366                                 rte_flow_error_set(error,
1367                                                    EINVAL,
1368                                                    RTE_FLOW_ERROR_TYPE_ACTION,
1369                                                    act,
1370                                                    "Invalid queue ID for RSS");
1371                                 rc = -rte_errno;
1372                                 goto ret;
1373                         }
1374                         rxq = bp->rx_queues[rss->queue[i]];
1375
1376                         if (bp->vnic_info[0].fw_grp_ids[rss->queue[i]] !=
1377                             INVALID_HW_RING_ID) {
1378                                 PMD_DRV_LOG(ERR,
1379                                             "queue active with other VNIC\n");
1380                                 rte_flow_error_set(error,
1381                                                    EINVAL,
1382                                                    RTE_FLOW_ERROR_TYPE_ACTION,
1383                                                    act,
1384                                                    "Invalid queue ID for RSS");
1385                                 rc = -rte_errno;
1386                                 goto ret;
1387                         }
1388
1389                         rxq->vnic = vnic;
1390                         rxq->rx_started = 1;
1391                         vnic->rx_queue_cnt++;
1392                 }
1393
1394                 vnic->start_grp_id = rss->queue[0];
1395                 vnic->end_grp_id = rss->queue[rss->queue_num - 1];
1396                 vnic->func_default = 0; //This is not a default VNIC.
1397
1398                 rc = bnxt_vnic_prep(bp, vnic, act, error);
1399                 if (rc)
1400                         goto ret;
1401
1402                 PMD_DRV_LOG(DEBUG,
1403                             "vnic[%d] = %p vnic->fw_grp_ids = %p\n",
1404                             vnic_id, vnic, vnic->fw_grp_ids);
1405
1406                 vnic->ff_pool_idx = vnic_id;
1407                 PMD_DRV_LOG(DEBUG,
1408                             "Setting vnic ff_pool_idx %d\n", vnic->ff_pool_idx);
1409
1410                 /* This can be done only after vnic_grp_alloc is done. */
1411                 for (i = 0; i < vnic->rx_queue_cnt; i++) {
1412                         vnic->fw_grp_ids[i] =
1413                                 bp->grp_info[rss->queue[i]].fw_grp_id;
1414                         /* Make sure vnic0 does not use these rings. */
1415                         bp->vnic_info[0].fw_grp_ids[rss->queue[i]] =
1416                                 INVALID_HW_RING_ID;
1417                 }
1418
1419                 for (rss_idx = 0; rss_idx < HW_HASH_INDEX_SIZE; ) {
1420                         for (i = 0; i < vnic->rx_queue_cnt; i++)
1421                                 vnic->rss_table[rss_idx++] =
1422                                         vnic->fw_grp_ids[i];
1423                 }
1424
1425                 /* Configure RSS only if the queue count is > 1 */
1426                 if (vnic->rx_queue_cnt > 1) {
1427                         vnic->hash_type =
1428                                 bnxt_rte_to_hwrm_hash_types(rss->types);
1429                         vnic->hash_mode =
1430                         bnxt_rte_to_hwrm_hash_level(bp, rss->types, rss->level);
1431
1432                         if (!rss->key_len) {
1433                                 /* If hash key has not been specified,
1434                                  * use random hash key.
1435                                  */
1436                                 bnxt_prandom_bytes(vnic->rss_hash_key,
1437                                                    HW_HASH_KEY_SIZE);
1438                         } else {
1439                                 if (rss->key_len > HW_HASH_KEY_SIZE)
1440                                         memcpy(vnic->rss_hash_key,
1441                                                rss->key,
1442                                                HW_HASH_KEY_SIZE);
1443                                 else
1444                                         memcpy(vnic->rss_hash_key,
1445                                                rss->key,
1446                                                rss->key_len);
1447                         }
1448                         bnxt_hwrm_vnic_rss_cfg(bp, vnic);
1449                 } else {
1450                         PMD_DRV_LOG(DEBUG, "No RSS config required\n");
1451                 }
1452
1453 vnic_found:
1454                 filter->dst_id = vnic->fw_vnic_id;
1455                 filter1 = bnxt_get_l2_filter(bp, filter, vnic);
1456                 if (filter1 == NULL) {
1457                         rte_flow_error_set(error,
1458                                            ENOSPC,
1459                                            RTE_FLOW_ERROR_TYPE_ACTION,
1460                                            act,
1461                                            "New filter not available");
1462                         rc = -rte_errno;
1463                         goto ret;
1464                 }
1465
1466                 PMD_DRV_LOG(DEBUG, "L2 filter created\n");
1467                 bnxt_update_filter_flags_en(filter, filter1, use_ntuple);
1468                 break;
1469         case RTE_FLOW_ACTION_TYPE_MARK:
1470                 if (bp->mark_table == NULL) {
1471                         rte_flow_error_set(error,
1472                                            ENOMEM,
1473                                            RTE_FLOW_ERROR_TYPE_ACTION,
1474                                            act,
1475                                            "Mark table not allocated.");
1476                         rc = -rte_errno;
1477                         goto ret;
1478                 }
1479
1480                 if (bp->flags & BNXT_FLAG_RX_VECTOR_PKT_MODE) {
1481                         PMD_DRV_LOG(DEBUG,
1482                                     "Disabling vector processing for mark\n");
1483                         bp->eth_dev->rx_pkt_burst = bnxt_recv_pkts;
1484                         bp->flags &= ~BNXT_FLAG_RX_VECTOR_PKT_MODE;
1485                 }
1486
1487                 filter->valid_flags |= BNXT_FLOW_MARK_FLAG;
1488                 filter->mark = ((const struct rte_flow_action_mark *)
1489                                 act->conf)->id;
1490                 PMD_DRV_LOG(DEBUG, "Mark the flow %d\n", filter->mark);
1491                 break;
1492         default:
1493                 rte_flow_error_set(error,
1494                                    EINVAL,
1495                                    RTE_FLOW_ERROR_TYPE_ACTION,
1496                                    act,
1497                                    "Invalid action.");
1498                 rc = -rte_errno;
1499                 goto ret;
1500         }
1501
1502 done:
1503         act = bnxt_flow_non_void_action(++act);
1504         while (act->type != RTE_FLOW_ACTION_TYPE_END)
1505                 goto start;
1506
1507         return rc;
1508 ret:
1509
1510         if (filter1) {
1511                 bnxt_hwrm_clear_l2_filter(bp, filter1);
1512                 bnxt_free_filter(bp, filter1);
1513         }
1514
1515         if (rte_errno)  {
1516                 if (vnic && STAILQ_EMPTY(&vnic->filter))
1517                         vnic->rx_queue_cnt = 0;
1518
1519                 if (rxq && !vnic->rx_queue_cnt)
1520                         rxq->vnic = &bp->vnic_info[0];
1521         }
1522         return -rte_errno;
1523 }
1524
1525 static
1526 struct bnxt_vnic_info *find_matching_vnic(struct bnxt *bp,
1527                                           struct bnxt_filter_info *filter)
1528 {
1529         struct bnxt_vnic_info *vnic = NULL;
1530         unsigned int i;
1531
1532         for (i = 0; i < bp->max_vnics; i++) {
1533                 vnic = &bp->vnic_info[i];
1534                 if (vnic->fw_vnic_id != INVALID_VNIC_ID &&
1535                     filter->dst_id == vnic->fw_vnic_id) {
1536                         PMD_DRV_LOG(DEBUG, "Found matching VNIC Id %d\n",
1537                                     vnic->ff_pool_idx);
1538                         return vnic;
1539                 }
1540         }
1541         return NULL;
1542 }
1543
1544 static int
1545 bnxt_flow_validate(struct rte_eth_dev *dev,
1546                    const struct rte_flow_attr *attr,
1547                    const struct rte_flow_item pattern[],
1548                    const struct rte_flow_action actions[],
1549                    struct rte_flow_error *error)
1550 {
1551         struct bnxt *bp = dev->data->dev_private;
1552         struct bnxt_vnic_info *vnic = NULL;
1553         struct bnxt_filter_info *filter;
1554         int ret = 0;
1555
1556         bnxt_acquire_flow_lock(bp);
1557         ret = bnxt_flow_args_validate(attr, pattern, actions, error);
1558         if (ret != 0) {
1559                 bnxt_release_flow_lock(bp);
1560                 return ret;
1561         }
1562
1563         filter = bnxt_get_unused_filter(bp);
1564         if (filter == NULL) {
1565                 rte_flow_error_set(error, ENOSPC,
1566                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1567                                    "Not enough resources for a new flow");
1568                 bnxt_release_flow_lock(bp);
1569                 return -ENOSPC;
1570         }
1571
1572         ret = bnxt_validate_and_parse_flow(dev, pattern, actions, attr,
1573                                            error, filter);
1574         if (ret)
1575                 goto exit;
1576
1577         vnic = find_matching_vnic(bp, filter);
1578         if (vnic) {
1579                 if (STAILQ_EMPTY(&vnic->filter)) {
1580                         rte_free(vnic->fw_grp_ids);
1581                         bnxt_hwrm_vnic_ctx_free(bp, vnic);
1582                         bnxt_hwrm_vnic_free(bp, vnic);
1583                         vnic->rx_queue_cnt = 0;
1584                         bp->nr_vnics--;
1585                         PMD_DRV_LOG(DEBUG, "Free VNIC\n");
1586                 }
1587         }
1588
1589         if (filter->filter_type == HWRM_CFA_EM_FILTER)
1590                 bnxt_hwrm_clear_em_filter(bp, filter);
1591         else if (filter->filter_type == HWRM_CFA_NTUPLE_FILTER)
1592                 bnxt_hwrm_clear_ntuple_filter(bp, filter);
1593         else
1594                 bnxt_hwrm_clear_l2_filter(bp, filter);
1595
1596 exit:
1597         /* No need to hold on to this filter if we are just validating flow */
1598         bnxt_free_filter(bp, filter);
1599         bnxt_release_flow_lock(bp);
1600
1601         return ret;
1602 }
1603
1604 static void
1605 bnxt_update_filter(struct bnxt *bp, struct bnxt_filter_info *old_filter,
1606                    struct bnxt_filter_info *new_filter)
1607 {
1608         /* Clear the new L2 filter that was created in the previous step in
1609          * bnxt_validate_and_parse_flow. For L2 filters, we will use the new
1610          * filter which points to the new destination queue and so we clear
1611          * the previous L2 filter. For ntuple filters, we are going to reuse
1612          * the old L2 filter and create new NTUPLE filter with this new
1613          * destination queue subsequently during bnxt_flow_create. So we
1614          * decrement the ref cnt of the L2 filter that would've been bumped
1615          * up previously in bnxt_validate_and_parse_flow as the old n-tuple
1616          * filter that was referencing it will be deleted now.
1617          */
1618         bnxt_hwrm_clear_l2_filter(bp, old_filter);
1619         if (new_filter->filter_type == HWRM_CFA_L2_FILTER) {
1620                 bnxt_hwrm_set_l2_filter(bp, new_filter->dst_id, new_filter);
1621         } else {
1622                 if (new_filter->filter_type == HWRM_CFA_EM_FILTER)
1623                         bnxt_hwrm_clear_em_filter(bp, old_filter);
1624                 if (new_filter->filter_type == HWRM_CFA_NTUPLE_FILTER)
1625                         bnxt_hwrm_clear_ntuple_filter(bp, old_filter);
1626         }
1627 }
1628
1629 static int
1630 bnxt_match_filter(struct bnxt *bp, struct bnxt_filter_info *nf)
1631 {
1632         struct bnxt_filter_info *mf;
1633         struct rte_flow *flow;
1634         int i;
1635
1636         for (i = bp->max_vnics - 1; i >= 0; i--) {
1637                 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
1638
1639                 if (vnic->fw_vnic_id == INVALID_VNIC_ID)
1640                         continue;
1641
1642                 STAILQ_FOREACH(flow, &vnic->flow_list, next) {
1643                         mf = flow->filter;
1644
1645                         if (mf->filter_type == nf->filter_type &&
1646                             mf->flags == nf->flags &&
1647                             mf->src_port == nf->src_port &&
1648                             mf->src_port_mask == nf->src_port_mask &&
1649                             mf->dst_port == nf->dst_port &&
1650                             mf->dst_port_mask == nf->dst_port_mask &&
1651                             mf->ip_protocol == nf->ip_protocol &&
1652                             mf->ip_addr_type == nf->ip_addr_type &&
1653                             mf->ethertype == nf->ethertype &&
1654                             mf->vni == nf->vni &&
1655                             mf->tunnel_type == nf->tunnel_type &&
1656                             mf->l2_ovlan == nf->l2_ovlan &&
1657                             mf->l2_ovlan_mask == nf->l2_ovlan_mask &&
1658                             mf->l2_ivlan == nf->l2_ivlan &&
1659                             mf->l2_ivlan_mask == nf->l2_ivlan_mask &&
1660                             !memcmp(mf->l2_addr, nf->l2_addr,
1661                                     RTE_ETHER_ADDR_LEN) &&
1662                             !memcmp(mf->l2_addr_mask, nf->l2_addr_mask,
1663                                     RTE_ETHER_ADDR_LEN) &&
1664                             !memcmp(mf->src_macaddr, nf->src_macaddr,
1665                                     RTE_ETHER_ADDR_LEN) &&
1666                             !memcmp(mf->dst_macaddr, nf->dst_macaddr,
1667                                     RTE_ETHER_ADDR_LEN) &&
1668                             !memcmp(mf->src_ipaddr, nf->src_ipaddr,
1669                                     sizeof(nf->src_ipaddr)) &&
1670                             !memcmp(mf->src_ipaddr_mask, nf->src_ipaddr_mask,
1671                                     sizeof(nf->src_ipaddr_mask)) &&
1672                             !memcmp(mf->dst_ipaddr, nf->dst_ipaddr,
1673                                     sizeof(nf->dst_ipaddr)) &&
1674                             !memcmp(mf->dst_ipaddr_mask, nf->dst_ipaddr_mask,
1675                                     sizeof(nf->dst_ipaddr_mask))) {
1676                                 if (mf->dst_id == nf->dst_id)
1677                                         return -EEXIST;
1678                                 /* Free the old filter, update flow
1679                                  * with new filter
1680                                  */
1681                                 bnxt_update_filter(bp, mf, nf);
1682                                 STAILQ_REMOVE(&vnic->filter, mf,
1683                                               bnxt_filter_info, next);
1684                                 STAILQ_INSERT_TAIL(&vnic->filter, nf, next);
1685                                 bnxt_free_filter(bp, mf);
1686                                 flow->filter = nf;
1687                                 return -EXDEV;
1688                         }
1689                 }
1690         }
1691         return 0;
1692 }
1693
1694 static void
1695 bnxt_setup_flow_counter(struct bnxt *bp)
1696 {
1697         if (bp->fw_cap & BNXT_FW_CAP_ADV_FLOW_COUNTERS &&
1698             !(bp->flags & BNXT_FLAG_FC_THREAD) && BNXT_FLOW_XSTATS_EN(bp)) {
1699                 rte_eal_alarm_set(US_PER_S * BNXT_FC_TIMER,
1700                                   bnxt_flow_cnt_alarm_cb,
1701                                   (void *)bp);
1702                 bp->flags |= BNXT_FLAG_FC_THREAD;
1703         }
1704 }
1705
1706 void bnxt_flow_cnt_alarm_cb(void *arg)
1707 {
1708         int rc = 0;
1709         struct bnxt *bp = arg;
1710
1711         if (!bp->flow_stat->rx_fc_out_tbl.va) {
1712                 PMD_DRV_LOG(ERR, "bp->flow_stat->rx_fc_out_tbl.va is NULL?\n");
1713                 bnxt_cancel_fc_thread(bp);
1714                 return;
1715         }
1716
1717         if (!bp->flow_stat->flow_count) {
1718                 bnxt_cancel_fc_thread(bp);
1719                 return;
1720         }
1721
1722         if (!bp->eth_dev->data->dev_started) {
1723                 bnxt_cancel_fc_thread(bp);
1724                 return;
1725         }
1726
1727         rc = bnxt_flow_stats_req(bp);
1728         if (rc) {
1729                 PMD_DRV_LOG(ERR, "Flow stat alarm not rescheduled.\n");
1730                 return;
1731         }
1732
1733         rte_eal_alarm_set(US_PER_S * BNXT_FC_TIMER,
1734                           bnxt_flow_cnt_alarm_cb,
1735                           (void *)bp);
1736 }
1737
1738
1739 static struct rte_flow *
1740 bnxt_flow_create(struct rte_eth_dev *dev,
1741                  const struct rte_flow_attr *attr,
1742                  const struct rte_flow_item pattern[],
1743                  const struct rte_flow_action actions[],
1744                  struct rte_flow_error *error)
1745 {
1746         struct bnxt *bp = dev->data->dev_private;
1747         struct bnxt_vnic_info *vnic = NULL;
1748         struct bnxt_filter_info *filter;
1749         bool update_flow = false;
1750         struct rte_flow *flow;
1751         int ret = 0;
1752         uint32_t tun_type, flow_id;
1753
1754         if (BNXT_VF(bp) && !BNXT_VF_IS_TRUSTED(bp)) {
1755                 rte_flow_error_set(error, EINVAL,
1756                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1757                                    "Failed to create flow, Not a Trusted VF!");
1758                 return NULL;
1759         }
1760
1761         if (!dev->data->dev_started) {
1762                 rte_flow_error_set(error,
1763                                    EINVAL,
1764                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1765                                    NULL,
1766                                    "Device must be started");
1767                 return NULL;
1768         }
1769
1770         flow = rte_zmalloc("bnxt_flow", sizeof(struct rte_flow), 0);
1771         if (!flow) {
1772                 rte_flow_error_set(error, ENOMEM,
1773                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1774                                    "Failed to allocate memory");
1775                 return flow;
1776         }
1777
1778         bnxt_acquire_flow_lock(bp);
1779         ret = bnxt_flow_args_validate(attr, pattern, actions, error);
1780         if (ret != 0) {
1781                 PMD_DRV_LOG(ERR, "Not a validate flow.\n");
1782                 goto free_flow;
1783         }
1784
1785         filter = bnxt_get_unused_filter(bp);
1786         if (filter == NULL) {
1787                 rte_flow_error_set(error, ENOSPC,
1788                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1789                                    "Not enough resources for a new flow");
1790                 goto free_flow;
1791         }
1792
1793         ret = bnxt_validate_and_parse_flow(dev, pattern, actions, attr,
1794                                            error, filter);
1795         if (ret != 0)
1796                 goto free_filter;
1797
1798         ret = bnxt_match_filter(bp, filter);
1799         if (ret == -EEXIST) {
1800                 PMD_DRV_LOG(DEBUG, "Flow already exists.\n");
1801                 /* Clear the filter that was created as part of
1802                  * validate_and_parse_flow() above
1803                  */
1804                 bnxt_hwrm_clear_l2_filter(bp, filter);
1805                 goto free_filter;
1806         } else if (ret == -EXDEV) {
1807                 PMD_DRV_LOG(DEBUG, "Flow with same pattern exists\n");
1808                 PMD_DRV_LOG(DEBUG, "Updating with different destination\n");
1809                 update_flow = true;
1810         }
1811
1812         /* If tunnel redirection to a VF/PF is specified then only tunnel_type
1813          * is set and enable is set to the tunnel type. Issue hwrm cmd directly
1814          * in such a case.
1815          */
1816         if (filter->filter_type == HWRM_CFA_TUNNEL_REDIRECT_FILTER &&
1817             filter->enables == filter->tunnel_type) {
1818                 ret = bnxt_hwrm_tunnel_redirect_query(bp, &tun_type);
1819                 if (ret) {
1820                         rte_flow_error_set(error, -ret,
1821                                            RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1822                                            "Unable to query tunnel to VF");
1823                         goto free_filter;
1824                 }
1825                 if (tun_type == (1U << filter->tunnel_type)) {
1826                         ret =
1827                         bnxt_hwrm_tunnel_redirect_free(bp,
1828                                                        filter->tunnel_type);
1829                         if (ret) {
1830                                 PMD_DRV_LOG(ERR,
1831                                             "Unable to free existing tunnel\n");
1832                                 rte_flow_error_set(error, -ret,
1833                                                    RTE_FLOW_ERROR_TYPE_HANDLE,
1834                                                    NULL,
1835                                                    "Unable to free preexisting "
1836                                                    "tunnel on VF");
1837                                 goto free_filter;
1838                         }
1839                 }
1840                 ret = bnxt_hwrm_tunnel_redirect(bp, filter->tunnel_type);
1841                 if (ret) {
1842                         rte_flow_error_set(error, -ret,
1843                                            RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1844                                            "Unable to redirect tunnel to VF");
1845                         goto free_filter;
1846                 }
1847                 vnic = &bp->vnic_info[0];
1848                 goto done;
1849         }
1850
1851         if (filter->filter_type == HWRM_CFA_EM_FILTER) {
1852                 filter->enables |=
1853                         HWRM_CFA_EM_FLOW_ALLOC_INPUT_ENABLES_L2_FILTER_ID;
1854                 ret = bnxt_hwrm_set_em_filter(bp, filter->dst_id, filter);
1855                 if (ret != 0) {
1856                         rte_flow_error_set(error, -ret,
1857                                            RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1858                                            "Failed to create EM filter");
1859                         goto free_filter;
1860                 }
1861         }
1862
1863         if (filter->filter_type == HWRM_CFA_NTUPLE_FILTER) {
1864                 filter->enables |=
1865                         HWRM_CFA_NTUPLE_FILTER_ALLOC_INPUT_ENABLES_L2_FILTER_ID;
1866                 ret = bnxt_hwrm_set_ntuple_filter(bp, filter->dst_id, filter);
1867                 if (ret != 0) {
1868                         rte_flow_error_set(error, -ret,
1869                                            RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1870                                            "Failed to create ntuple filter");
1871                         goto free_filter;
1872                 }
1873         }
1874
1875         if (BNXT_RFS_NEEDS_VNIC(bp))
1876                 vnic = find_matching_vnic(bp, filter);
1877         else
1878                 vnic = BNXT_GET_DEFAULT_VNIC(bp);
1879 done:
1880         if (!ret || update_flow) {
1881                 flow->filter = filter;
1882                 flow->vnic = vnic;
1883                 if (update_flow) {
1884                         ret = -EXDEV;
1885                         goto free_flow;
1886                 }
1887
1888                 if (filter->valid_flags & BNXT_FLOW_MARK_FLAG) {
1889                         PMD_DRV_LOG(DEBUG,
1890                                     "Mark action: mark id 0x%x, flow id 0x%x\n",
1891                                     filter->mark, filter->flow_id);
1892
1893                         /* TCAM and EM should be 16-bit only.
1894                          * Other modes not supported.
1895                          */
1896                         flow_id = filter->flow_id & BNXT_FLOW_ID_MASK;
1897                         if (bp->mark_table[flow_id].valid) {
1898                                 rte_flow_error_set(error, EEXIST,
1899                                                    RTE_FLOW_ERROR_TYPE_HANDLE,
1900                                                    NULL,
1901                                                    "Flow with mark id exists");
1902                                 bnxt_clear_one_vnic_filter(bp, filter);
1903                                 goto free_filter;
1904                         }
1905                         bp->mark_table[flow_id].valid = true;
1906                         bp->mark_table[flow_id].mark_id = filter->mark;
1907                 }
1908
1909                 STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
1910                 STAILQ_INSERT_TAIL(&vnic->flow_list, flow, next);
1911
1912                 if (BNXT_FLOW_XSTATS_EN(bp))
1913                         bp->flow_stat->flow_count++;
1914                 bnxt_release_flow_lock(bp);
1915                 bnxt_setup_flow_counter(bp);
1916                 PMD_DRV_LOG(DEBUG, "Successfully created flow.\n");
1917                 return flow;
1918         }
1919
1920 free_filter:
1921         bnxt_free_filter(bp, filter);
1922 free_flow:
1923         if (ret == -EEXIST)
1924                 rte_flow_error_set(error, ret,
1925                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1926                                    "Matching Flow exists.");
1927         else if (ret == -EXDEV)
1928                 rte_flow_error_set(error, 0,
1929                                    RTE_FLOW_ERROR_TYPE_NONE, NULL,
1930                                    "Flow with pattern exists, updating destination queue");
1931         else if (!rte_errno)
1932                 rte_flow_error_set(error, -ret,
1933                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1934                                    "Failed to create flow.");
1935         rte_free(flow);
1936         flow = NULL;
1937         bnxt_release_flow_lock(bp);
1938         return flow;
1939 }
1940
1941 static int bnxt_handle_tunnel_redirect_destroy(struct bnxt *bp,
1942                                                struct bnxt_filter_info *filter,
1943                                                struct rte_flow_error *error)
1944 {
1945         uint16_t tun_dst_fid;
1946         uint32_t tun_type;
1947         int ret = 0;
1948
1949         ret = bnxt_hwrm_tunnel_redirect_query(bp, &tun_type);
1950         if (ret) {
1951                 rte_flow_error_set(error, -ret,
1952                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1953                                    "Unable to query tunnel to VF");
1954                 return ret;
1955         }
1956         if (tun_type == (1U << filter->tunnel_type)) {
1957                 ret = bnxt_hwrm_tunnel_redirect_info(bp, filter->tunnel_type,
1958                                                      &tun_dst_fid);
1959                 if (ret) {
1960                         rte_flow_error_set(error, -ret,
1961                                            RTE_FLOW_ERROR_TYPE_HANDLE,
1962                                            NULL,
1963                                            "tunnel_redirect info cmd fail");
1964                         return ret;
1965                 }
1966                 PMD_DRV_LOG(INFO, "Pre-existing tunnel fid = %x vf->fid = %x\n",
1967                             tun_dst_fid + bp->first_vf_id, bp->fw_fid);
1968
1969                 /* Tunnel doesn't belong to this VF, so don't send HWRM
1970                  * cmd, just delete the flow from driver
1971                  */
1972                 if (bp->fw_fid != (tun_dst_fid + bp->first_vf_id)) {
1973                         PMD_DRV_LOG(ERR,
1974                                     "Tunnel does not belong to this VF, skip hwrm_tunnel_redirect_free\n");
1975                 } else {
1976                         ret = bnxt_hwrm_tunnel_redirect_free(bp,
1977                                                         filter->tunnel_type);
1978                         if (ret) {
1979                                 rte_flow_error_set(error, -ret,
1980                                                    RTE_FLOW_ERROR_TYPE_HANDLE,
1981                                                    NULL,
1982                                                    "Unable to free tunnel redirection");
1983                                 return ret;
1984                         }
1985                 }
1986         }
1987         return ret;
1988 }
1989
1990 static int
1991 _bnxt_flow_destroy(struct bnxt *bp,
1992                    struct rte_flow *flow,
1993                     struct rte_flow_error *error)
1994 {
1995         struct bnxt_filter_info *filter;
1996         struct bnxt_vnic_info *vnic;
1997         int ret = 0;
1998         uint32_t flow_id;
1999
2000         filter = flow->filter;
2001         vnic = flow->vnic;
2002
2003         if (filter->filter_type == HWRM_CFA_TUNNEL_REDIRECT_FILTER &&
2004             filter->enables == filter->tunnel_type) {
2005                 ret = bnxt_handle_tunnel_redirect_destroy(bp, filter, error);
2006                 if (!ret)
2007                         goto done;
2008                 else
2009                         return ret;
2010         }
2011
2012         ret = bnxt_match_filter(bp, filter);
2013         if (ret == 0)
2014                 PMD_DRV_LOG(ERR, "Could not find matching flow\n");
2015
2016         if (filter->valid_flags & BNXT_FLOW_MARK_FLAG) {
2017                 flow_id = filter->flow_id & BNXT_FLOW_ID_MASK;
2018                 memset(&bp->mark_table[flow_id], 0,
2019                        sizeof(bp->mark_table[flow_id]));
2020                 filter->flow_id = 0;
2021         }
2022
2023         ret = bnxt_clear_one_vnic_filter(bp, filter);
2024
2025 done:
2026         if (!ret) {
2027                 /* If it is a L2 drop filter, when the filter is created,
2028                  * the FW updates the BC/MC records.
2029                  * Once this filter is removed, issue the set_rx_mask command
2030                  * to reset the BC/MC records in the HW to the settings
2031                  * before the drop counter is created.
2032                  */
2033                 if (filter->valid_flags & BNXT_FLOW_L2_DROP_FLAG)
2034                         bnxt_set_rx_mask_no_vlan(bp, &bp->vnic_info[0]);
2035
2036                 STAILQ_REMOVE(&vnic->filter, filter, bnxt_filter_info, next);
2037                 bnxt_free_filter(bp, filter);
2038                 STAILQ_REMOVE(&vnic->flow_list, flow, rte_flow, next);
2039                 rte_free(flow);
2040                 if (BNXT_FLOW_XSTATS_EN(bp))
2041                         bp->flow_stat->flow_count--;
2042
2043                 /* If this was the last flow associated with this vnic,
2044                  * switch the queue back to RSS pool.
2045                  */
2046                 if (vnic && !vnic->func_default &&
2047                     STAILQ_EMPTY(&vnic->flow_list)) {
2048                         rte_free(vnic->fw_grp_ids);
2049                         if (vnic->rx_queue_cnt > 1)
2050                                 bnxt_hwrm_vnic_ctx_free(bp, vnic);
2051
2052                         bnxt_hwrm_vnic_free(bp, vnic);
2053                         vnic->rx_queue_cnt = 0;
2054                         bp->nr_vnics--;
2055                 }
2056         } else {
2057                 rte_flow_error_set(error, -ret,
2058                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2059                                    "Failed to destroy flow.");
2060         }
2061
2062         return ret;
2063 }
2064
2065 static int
2066 bnxt_flow_destroy(struct rte_eth_dev *dev,
2067                   struct rte_flow *flow,
2068                   struct rte_flow_error *error)
2069 {
2070         struct bnxt *bp = dev->data->dev_private;
2071         int ret = 0;
2072
2073         bnxt_acquire_flow_lock(bp);
2074         if (!flow) {
2075                 rte_flow_error_set(error, EINVAL,
2076                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2077                                    "Invalid flow: failed to destroy flow.");
2078                 bnxt_release_flow_lock(bp);
2079                 return -EINVAL;
2080         }
2081
2082         if (!flow->filter) {
2083                 rte_flow_error_set(error, EINVAL,
2084                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2085                                    "Invalid flow: failed to destroy flow.");
2086                 bnxt_release_flow_lock(bp);
2087                 return -EINVAL;
2088         }
2089         ret = _bnxt_flow_destroy(bp, flow, error);
2090         bnxt_release_flow_lock(bp);
2091
2092         return ret;
2093 }
2094
2095 void bnxt_cancel_fc_thread(struct bnxt *bp)
2096 {
2097         bp->flags &= ~BNXT_FLAG_FC_THREAD;
2098         rte_eal_alarm_cancel(bnxt_flow_cnt_alarm_cb, (void *)bp);
2099 }
2100
2101 static int
2102 bnxt_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error)
2103 {
2104         struct bnxt *bp = dev->data->dev_private;
2105         struct bnxt_vnic_info *vnic;
2106         struct rte_flow *flow;
2107         unsigned int i;
2108         int ret = 0;
2109
2110         bnxt_acquire_flow_lock(bp);
2111         for (i = 0; i < bp->max_vnics; i++) {
2112                 vnic = &bp->vnic_info[i];
2113                 if (vnic && vnic->fw_vnic_id == INVALID_VNIC_ID)
2114                         continue;
2115
2116                 while (!STAILQ_EMPTY(&vnic->flow_list)) {
2117                         flow = STAILQ_FIRST(&vnic->flow_list);
2118
2119                         if (!flow->filter)
2120                                 continue;
2121
2122                         ret = _bnxt_flow_destroy(bp, flow, error);
2123                         if (ret)
2124                                 break;
2125                 }
2126         }
2127
2128         bnxt_cancel_fc_thread(bp);
2129         bnxt_release_flow_lock(bp);
2130
2131         return ret;
2132 }
2133
2134 const struct rte_flow_ops bnxt_flow_ops = {
2135         .validate = bnxt_flow_validate,
2136         .create = bnxt_flow_create,
2137         .destroy = bnxt_flow_destroy,
2138         .flush = bnxt_flow_flush,
2139 };