net/enic: support VLAN push and pop flow actions
[dpdk.git] / drivers / net / enic / enic_fm_flow.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2008-2019 Cisco Systems, Inc.  All rights reserved.
3  */
4
5 #include <errno.h>
6 #include <stdint.h>
7 #include <rte_log.h>
8 #include <rte_ethdev_driver.h>
9 #include <rte_flow_driver.h>
10 #include <rte_ether.h>
11 #include <rte_ip.h>
12 #include <rte_udp.h>
13 #include <rte_memzone.h>
14
15 #include "enic_compat.h"
16 #include "enic.h"
17 #include "vnic_dev.h"
18 #include "vnic_nic.h"
19
20 #define IP_DEFTTL  64   /* from RFC 1340. */
21 #define IP6_VTC_FLOW 0x60000000
22
23 /* Highest Item type supported by Flowman */
24 #define FM_MAX_ITEM_TYPE RTE_FLOW_ITEM_TYPE_VXLAN
25
26 /* Up to 1024 TCAM entries */
27 #define FM_MAX_TCAM_TABLE_SIZE 1024
28
29 /* Up to 4096 entries per exact match table */
30 #define FM_MAX_EXACT_TABLE_SIZE 4096
31
32 /* Number of counters to increase on for each increment */
33 #define FM_COUNTERS_EXPAND  100
34
35 #define FM_INVALID_HANDLE 0
36
37 /*
38  * Flow exact match tables (FET) in the VIC and rte_flow groups.
39  * Use a simple scheme to map groups to tables.
40  * Group 0 uses the single TCAM tables, one for each direction.
41  * Group 1, 2, ... uses its own exact match table.
42  *
43  * The TCAM tables are allocated upfront during init.
44  *
45  * Exact match tables are allocated on demand. 3 paths that lead allocations.
46  *
47  * 1. Add a flow that jumps from group 0 to group N.
48  *
49  * If N does not exist, we allocate an exact match table for it, using
50  * a dummy key. A key is required for the table.
51  *
52  * 2. Add a flow that uses group N.
53  *
54  * If N does not exist, we allocate an exact match table for it, using
55  * the flow's key. Subsequent flows to the same group all should have
56  * the same key.
57  *
58  * Without a jump flow to N, N is not reachable in hardware. No packets
59  * reach N and match.
60  *
61  * 3. Add a flow to an empty group N.
62  *
63  * N has been created via (1) and the dummy key. We free that table, allocate
64  * a new table using the new flow's key. Also re-do the existing jump flow to
65  * point to the new table.
66  */
67 #define FM_TCAM_RTE_GROUP 0
68
69 struct enic_fm_fet {
70         TAILQ_ENTRY(enic_fm_fet) list;
71         uint32_t group; /* rte_flow group ID */
72         uint64_t handle; /* Exact match table handle from flowman */
73         uint8_t ingress;
74         uint8_t default_key;
75         int ref; /* Reference count via get/put */
76         struct fm_key_template key; /* Key associated with the table */
77 };
78
79 struct enic_fm_counter {
80         SLIST_ENTRY(enic_fm_counter) next;
81         uint32_t handle;
82 };
83
84 /* rte_flow.fm */
85 struct enic_fm_flow {
86         bool counter_valid;
87         uint64_t entry_handle;
88         uint64_t action_handle;
89         struct enic_fm_counter *counter;
90         struct enic_fm_fet *fet;
91 };
92
93 struct enic_fm_jump_flow {
94         TAILQ_ENTRY(enic_fm_jump_flow) list;
95         struct rte_flow *flow;
96         uint32_t group;
97         struct fm_tcam_match_entry match;
98         struct fm_action action;
99 };
100
101 /*
102  * Flowman uses host memory for commands. This structure is allocated
103  * in DMA-able memory.
104  */
105 union enic_flowman_cmd_mem {
106         struct fm_tcam_match_table fm_tcam_match_table;
107         struct fm_exact_match_table fm_exact_match_table;
108         struct fm_tcam_match_entry fm_tcam_match_entry;
109         struct fm_exact_match_entry fm_exact_match_entry;
110         struct fm_action fm_action;
111 };
112
113 struct enic_flowman {
114         struct enic *enic;
115         /* Command buffer */
116         struct {
117                 union enic_flowman_cmd_mem *va;
118                 dma_addr_t pa;
119         } cmd;
120         /* TCAM tables allocated upfront, used for group 0 */
121         uint64_t ig_tcam_hndl;
122         uint64_t eg_tcam_hndl;
123         /* Counters */
124         SLIST_HEAD(enic_free_counters, enic_fm_counter) counters;
125         void *counter_stack;
126         uint32_t counters_alloced;
127         /* Exact match tables for groups != 0, dynamically allocated */
128         TAILQ_HEAD(fet_list, enic_fm_fet) fet_list;
129         /*
130          * Default exact match tables used for jump actions to
131          * non-existent groups.
132          */
133         struct enic_fm_fet *default_eg_fet;
134         struct enic_fm_fet *default_ig_fet;
135         /* Flows that jump to the default table above */
136         TAILQ_HEAD(jump_flow_list, enic_fm_jump_flow) jump_list;
137         /*
138          * Scratch data used during each invocation of flow_create
139          * and flow_validate.
140          */
141         struct enic_fm_fet *fet;
142         struct fm_tcam_match_entry tcam_entry;
143         struct fm_action action;
144         struct fm_action action_tmp; /* enic_fm_reorder_action_op */
145         int action_op_count;
146 };
147
148 static int enic_fm_tbl_free(struct enic_flowman *fm, uint64_t handle);
149
150 /*
151  * Common arguments passed to copy_item functions. Use this structure
152  * so we can easily add new arguments.
153  * item: Item specification.
154  * fm_tcam_entry: Flowman TCAM match entry.
155  * header_level: 0 for outer header, 1 for inner header.
156  */
157 struct copy_item_args {
158         const struct rte_flow_item *item;
159         struct fm_tcam_match_entry *fm_tcam_entry;
160         uint8_t header_level;
161 };
162
163 /* functions for copying items into flowman match */
164 typedef int (enic_copy_item_fn)(struct copy_item_args *arg);
165
166 /* Info about how to copy items into flowman match */
167 struct enic_fm_items {
168         /* Function for copying and validating an item. */
169         enic_copy_item_fn * const copy_item;
170         /* List of valid previous items. */
171         const enum rte_flow_item_type * const prev_items;
172         /*
173          * True if it's OK for this item to be the first item. For some NIC
174          * versions, it's invalid to start the stack above layer 3.
175          */
176         const uint8_t valid_start_item;
177 };
178
179 static enic_copy_item_fn enic_fm_copy_item_eth;
180 static enic_copy_item_fn enic_fm_copy_item_ipv4;
181 static enic_copy_item_fn enic_fm_copy_item_ipv6;
182 static enic_copy_item_fn enic_fm_copy_item_raw;
183 static enic_copy_item_fn enic_fm_copy_item_sctp;
184 static enic_copy_item_fn enic_fm_copy_item_tcp;
185 static enic_copy_item_fn enic_fm_copy_item_udp;
186 static enic_copy_item_fn enic_fm_copy_item_vlan;
187 static enic_copy_item_fn enic_fm_copy_item_vxlan;
188
189 /* Ingress actions */
190 static const enum rte_flow_action_type enic_fm_supported_ig_actions[] = {
191         RTE_FLOW_ACTION_TYPE_COUNT,
192         RTE_FLOW_ACTION_TYPE_DROP,
193         RTE_FLOW_ACTION_TYPE_FLAG,
194         RTE_FLOW_ACTION_TYPE_JUMP,
195         RTE_FLOW_ACTION_TYPE_MARK,
196         RTE_FLOW_ACTION_TYPE_OF_POP_VLAN,
197         RTE_FLOW_ACTION_TYPE_PORT_ID,
198         RTE_FLOW_ACTION_TYPE_PASSTHRU,
199         RTE_FLOW_ACTION_TYPE_QUEUE,
200         RTE_FLOW_ACTION_TYPE_RSS,
201         RTE_FLOW_ACTION_TYPE_VOID,
202         RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP,
203         RTE_FLOW_ACTION_TYPE_VXLAN_DECAP,
204         RTE_FLOW_ACTION_TYPE_END, /* END must be the last entry */
205 };
206
207 /* Egress actions */
208 static const enum rte_flow_action_type enic_fm_supported_eg_actions[] = {
209         RTE_FLOW_ACTION_TYPE_COUNT,
210         RTE_FLOW_ACTION_TYPE_DROP,
211         RTE_FLOW_ACTION_TYPE_JUMP,
212         RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN,
213         RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP,
214         RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID,
215         RTE_FLOW_ACTION_TYPE_PASSTHRU,
216         RTE_FLOW_ACTION_TYPE_VOID,
217         RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP,
218         RTE_FLOW_ACTION_TYPE_END,
219 };
220
221 static const struct enic_fm_items enic_fm_items[] = {
222         [RTE_FLOW_ITEM_TYPE_RAW] = {
223                 .copy_item = enic_fm_copy_item_raw,
224                 .valid_start_item = 0,
225                 .prev_items = (const enum rte_flow_item_type[]) {
226                                RTE_FLOW_ITEM_TYPE_UDP,
227                                RTE_FLOW_ITEM_TYPE_END,
228                 },
229         },
230         [RTE_FLOW_ITEM_TYPE_ETH] = {
231                 .copy_item = enic_fm_copy_item_eth,
232                 .valid_start_item = 1,
233                 .prev_items = (const enum rte_flow_item_type[]) {
234                                RTE_FLOW_ITEM_TYPE_END,
235                 },
236         },
237         [RTE_FLOW_ITEM_TYPE_VLAN] = {
238                 .copy_item = enic_fm_copy_item_vlan,
239                 .valid_start_item = 1,
240                 .prev_items = (const enum rte_flow_item_type[]) {
241                                RTE_FLOW_ITEM_TYPE_ETH,
242                                RTE_FLOW_ITEM_TYPE_END,
243                 },
244         },
245         [RTE_FLOW_ITEM_TYPE_IPV4] = {
246                 .copy_item = enic_fm_copy_item_ipv4,
247                 .valid_start_item = 1,
248                 .prev_items = (const enum rte_flow_item_type[]) {
249                                RTE_FLOW_ITEM_TYPE_ETH,
250                                RTE_FLOW_ITEM_TYPE_VLAN,
251                                RTE_FLOW_ITEM_TYPE_END,
252                 },
253         },
254         [RTE_FLOW_ITEM_TYPE_IPV6] = {
255                 .copy_item = enic_fm_copy_item_ipv6,
256                 .valid_start_item = 1,
257                 .prev_items = (const enum rte_flow_item_type[]) {
258                                RTE_FLOW_ITEM_TYPE_ETH,
259                                RTE_FLOW_ITEM_TYPE_VLAN,
260                                RTE_FLOW_ITEM_TYPE_END,
261                 },
262         },
263         [RTE_FLOW_ITEM_TYPE_UDP] = {
264                 .copy_item = enic_fm_copy_item_udp,
265                 .valid_start_item = 1,
266                 .prev_items = (const enum rte_flow_item_type[]) {
267                                RTE_FLOW_ITEM_TYPE_IPV4,
268                                RTE_FLOW_ITEM_TYPE_IPV6,
269                                RTE_FLOW_ITEM_TYPE_END,
270                 },
271         },
272         [RTE_FLOW_ITEM_TYPE_TCP] = {
273                 .copy_item = enic_fm_copy_item_tcp,
274                 .valid_start_item = 1,
275                 .prev_items = (const enum rte_flow_item_type[]) {
276                                RTE_FLOW_ITEM_TYPE_IPV4,
277                                RTE_FLOW_ITEM_TYPE_IPV6,
278                                RTE_FLOW_ITEM_TYPE_END,
279                 },
280         },
281         [RTE_FLOW_ITEM_TYPE_SCTP] = {
282                 .copy_item = enic_fm_copy_item_sctp,
283                 .valid_start_item = 0,
284                 .prev_items = (const enum rte_flow_item_type[]) {
285                                RTE_FLOW_ITEM_TYPE_IPV4,
286                                RTE_FLOW_ITEM_TYPE_IPV6,
287                                RTE_FLOW_ITEM_TYPE_END,
288                 },
289         },
290         [RTE_FLOW_ITEM_TYPE_VXLAN] = {
291                 .copy_item = enic_fm_copy_item_vxlan,
292                 .valid_start_item = 1,
293                 .prev_items = (const enum rte_flow_item_type[]) {
294                                RTE_FLOW_ITEM_TYPE_UDP,
295                                RTE_FLOW_ITEM_TYPE_END,
296                 },
297         },
298 };
299
300 static int
301 enic_fm_copy_item_eth(struct copy_item_args *arg)
302 {
303         const struct rte_flow_item *item = arg->item;
304         const struct rte_flow_item_eth *spec = item->spec;
305         const struct rte_flow_item_eth *mask = item->mask;
306         const uint8_t lvl = arg->header_level;
307         struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
308         struct fm_header_set *fm_data, *fm_mask;
309
310         ENICPMD_FUNC_TRACE();
311         /* Match all if no spec */
312         if (!spec)
313                 return 0;
314         if (!mask)
315                 mask = &rte_flow_item_eth_mask;
316         fm_data = &entry->ftm_data.fk_hdrset[lvl];
317         fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
318         fm_data->fk_header_select |= FKH_ETHER;
319         fm_mask->fk_header_select |= FKH_ETHER;
320         memcpy(&fm_data->l2.eth, spec, sizeof(*spec));
321         memcpy(&fm_mask->l2.eth, mask, sizeof(*mask));
322         return 0;
323 }
324
325 static int
326 enic_fm_copy_item_vlan(struct copy_item_args *arg)
327 {
328         const struct rte_flow_item *item = arg->item;
329         const struct rte_flow_item_vlan *spec = item->spec;
330         const struct rte_flow_item_vlan *mask = item->mask;
331         const uint8_t lvl = arg->header_level;
332         struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
333         struct fm_header_set *fm_data, *fm_mask;
334         struct rte_ether_hdr *eth_mask;
335         struct rte_ether_hdr *eth_val;
336         uint32_t meta;
337
338         ENICPMD_FUNC_TRACE();
339         fm_data = &entry->ftm_data.fk_hdrset[lvl];
340         fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
341         /* Outer and inner packet vlans need different flags */
342         meta = FKM_VLAN_PRES;
343         if (lvl > 0)
344                 meta = FKM_QTAG;
345         fm_data->fk_metadata |= meta;
346         fm_mask->fk_metadata |= meta;
347
348         /* Match all if no spec */
349         if (!spec)
350                 return 0;
351         if (!mask)
352                 mask = &rte_flow_item_vlan_mask;
353
354         eth_mask = (void *)&fm_mask->l2.eth;
355         eth_val = (void *)&fm_data->l2.eth;
356
357         /* Outer TPID cannot be matched */
358         if (eth_mask->ether_type)
359                 return -ENOTSUP;
360
361         /*
362          * When packet matching, the VIC always compares vlan-stripped
363          * L2, regardless of vlan stripping settings. So, the inner type
364          * from vlan becomes the ether type of the eth header.
365          */
366         eth_mask->ether_type = mask->inner_type;
367         eth_val->ether_type = spec->inner_type;
368         fm_data->fk_header_select |= FKH_ETHER | FKH_QTAG;
369         fm_mask->fk_header_select |= FKH_ETHER | FKH_QTAG;
370         fm_data->fk_vlan = rte_be_to_cpu_16(spec->tci);
371         fm_mask->fk_vlan = rte_be_to_cpu_16(mask->tci);
372         return 0;
373 }
374
375 static int
376 enic_fm_copy_item_ipv4(struct copy_item_args *arg)
377 {
378         const struct rte_flow_item *item = arg->item;
379         const struct rte_flow_item_ipv4 *spec = item->spec;
380         const struct rte_flow_item_ipv4 *mask = item->mask;
381         const uint8_t lvl = arg->header_level;
382         struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
383         struct fm_header_set *fm_data, *fm_mask;
384
385         ENICPMD_FUNC_TRACE();
386         fm_data = &entry->ftm_data.fk_hdrset[lvl];
387         fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
388         fm_data->fk_metadata |= FKM_IPV4;
389         fm_mask->fk_metadata |= FKM_IPV4;
390
391         if (!spec)
392                 return 0;
393         if (!mask)
394                 mask = &rte_flow_item_ipv4_mask;
395
396         fm_data->fk_header_select |= FKH_IPV4;
397         fm_mask->fk_header_select |= FKH_IPV4;
398         memcpy(&fm_data->l3.ip4, spec, sizeof(*spec));
399         memcpy(&fm_mask->l3.ip4, mask, sizeof(*mask));
400         return 0;
401 }
402
403 static int
404 enic_fm_copy_item_ipv6(struct copy_item_args *arg)
405 {
406         const struct rte_flow_item *item = arg->item;
407         const struct rte_flow_item_ipv6 *spec = item->spec;
408         const struct rte_flow_item_ipv6 *mask = item->mask;
409         const uint8_t lvl = arg->header_level;
410         struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
411         struct fm_header_set *fm_data, *fm_mask;
412
413         ENICPMD_FUNC_TRACE();
414         fm_data = &entry->ftm_data.fk_hdrset[lvl];
415         fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
416         fm_data->fk_metadata |= FKM_IPV6;
417         fm_mask->fk_metadata |= FKM_IPV6;
418
419         if (!spec)
420                 return 0;
421         if (!mask)
422                 mask = &rte_flow_item_ipv6_mask;
423
424         fm_data->fk_header_select |= FKH_IPV6;
425         fm_mask->fk_header_select |= FKH_IPV6;
426         memcpy(&fm_data->l3.ip6, spec, sizeof(*spec));
427         memcpy(&fm_mask->l3.ip6, mask, sizeof(*mask));
428         return 0;
429 }
430
431 static int
432 enic_fm_copy_item_udp(struct copy_item_args *arg)
433 {
434         const struct rte_flow_item *item = arg->item;
435         const struct rte_flow_item_udp *spec = item->spec;
436         const struct rte_flow_item_udp *mask = item->mask;
437         const uint8_t lvl = arg->header_level;
438         struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
439         struct fm_header_set *fm_data, *fm_mask;
440
441         ENICPMD_FUNC_TRACE();
442         fm_data = &entry->ftm_data.fk_hdrset[lvl];
443         fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
444         fm_data->fk_metadata |= FKM_UDP;
445         fm_mask->fk_metadata |= FKM_UDP;
446
447         if (!spec)
448                 return 0;
449         if (!mask)
450                 mask = &rte_flow_item_udp_mask;
451
452         fm_data->fk_header_select |= FKH_UDP;
453         fm_mask->fk_header_select |= FKH_UDP;
454         memcpy(&fm_data->l4.udp, spec, sizeof(*spec));
455         memcpy(&fm_mask->l4.udp, mask, sizeof(*mask));
456         return 0;
457 }
458
459 static int
460 enic_fm_copy_item_tcp(struct copy_item_args *arg)
461 {
462         const struct rte_flow_item *item = arg->item;
463         const struct rte_flow_item_tcp *spec = item->spec;
464         const struct rte_flow_item_tcp *mask = item->mask;
465         const uint8_t lvl = arg->header_level;
466         struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
467         struct fm_header_set *fm_data, *fm_mask;
468
469         ENICPMD_FUNC_TRACE();
470         fm_data = &entry->ftm_data.fk_hdrset[lvl];
471         fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
472         fm_data->fk_metadata |= FKM_TCP;
473         fm_mask->fk_metadata |= FKM_TCP;
474
475         if (!spec)
476                 return 0;
477         if (!mask)
478                 mask = &rte_flow_item_tcp_mask;
479
480         fm_data->fk_header_select |= FKH_TCP;
481         fm_mask->fk_header_select |= FKH_TCP;
482         memcpy(&fm_data->l4.tcp, spec, sizeof(*spec));
483         memcpy(&fm_mask->l4.tcp, mask, sizeof(*mask));
484         return 0;
485 }
486
487 static int
488 enic_fm_copy_item_sctp(struct copy_item_args *arg)
489 {
490         const struct rte_flow_item *item = arg->item;
491         const struct rte_flow_item_sctp *spec = item->spec;
492         const struct rte_flow_item_sctp *mask = item->mask;
493         const uint8_t lvl = arg->header_level;
494         struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
495         struct fm_header_set *fm_data, *fm_mask;
496         uint8_t *ip_proto_mask = NULL;
497         uint8_t *ip_proto = NULL;
498         uint32_t l3_fkh;
499
500         ENICPMD_FUNC_TRACE();
501         fm_data = &entry->ftm_data.fk_hdrset[lvl];
502         fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
503         /*
504          * The NIC filter API has no flags for "match sctp", so explicitly
505          * set the protocol number in the IP pattern.
506          */
507         if (fm_data->fk_metadata & FKM_IPV4) {
508                 struct rte_ipv4_hdr *ip;
509                 ip = (struct rte_ipv4_hdr *)&fm_mask->l3.ip4;
510                 ip_proto_mask = &ip->next_proto_id;
511                 ip = (struct rte_ipv4_hdr *)&fm_data->l3.ip4;
512                 ip_proto = &ip->next_proto_id;
513                 l3_fkh = FKH_IPV4;
514         } else if (fm_data->fk_metadata & FKM_IPV6) {
515                 struct rte_ipv6_hdr *ip;
516                 ip = (struct rte_ipv6_hdr *)&fm_mask->l3.ip6;
517                 ip_proto_mask = &ip->proto;
518                 ip = (struct rte_ipv6_hdr *)&fm_data->l3.ip6;
519                 ip_proto = &ip->proto;
520                 l3_fkh = FKH_IPV6;
521         } else {
522                 /* Need IPv4/IPv6 pattern first */
523                 return -EINVAL;
524         }
525         *ip_proto = IPPROTO_SCTP;
526         *ip_proto_mask = 0xff;
527         fm_data->fk_header_select |= l3_fkh;
528         fm_mask->fk_header_select |= l3_fkh;
529
530         if (!spec)
531                 return 0;
532         if (!mask)
533                 mask = &rte_flow_item_sctp_mask;
534
535         fm_data->fk_header_select |= FKH_L4RAW;
536         fm_mask->fk_header_select |= FKH_L4RAW;
537         memcpy(fm_data->l4.rawdata, spec, sizeof(*spec));
538         memcpy(fm_mask->l4.rawdata, mask, sizeof(*mask));
539         return 0;
540 }
541
542 static int
543 enic_fm_copy_item_vxlan(struct copy_item_args *arg)
544 {
545         const struct rte_flow_item *item = arg->item;
546         const struct rte_flow_item_vxlan *spec = item->spec;
547         const struct rte_flow_item_vxlan *mask = item->mask;
548         struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
549         struct fm_header_set *fm_data, *fm_mask;
550
551         ENICPMD_FUNC_TRACE();
552         /* Only 2 header levels (outer and inner) allowed */
553         if (arg->header_level > 0)
554                 return -EINVAL;
555
556         fm_data = &entry->ftm_data.fk_hdrset[0];
557         fm_mask = &entry->ftm_mask.fk_hdrset[0];
558         fm_data->fk_metadata |= FKM_VXLAN;
559         fm_mask->fk_metadata |= FKM_VXLAN;
560         /* items from here on out are inner header items */
561         arg->header_level = 1;
562
563         /* Match all if no spec */
564         if (!spec)
565                 return 0;
566         if (!mask)
567                 mask = &rte_flow_item_vxlan_mask;
568
569         fm_data->fk_header_select |= FKH_VXLAN;
570         fm_mask->fk_header_select |= FKH_VXLAN;
571         memcpy(&fm_data->vxlan, spec, sizeof(*spec));
572         memcpy(&fm_mask->vxlan, mask, sizeof(*mask));
573         return 0;
574 }
575
576 /*
577  * Currently, raw pattern match is very limited. It is intended for matching
578  * UDP tunnel header (e.g. vxlan or geneve).
579  */
580 static int
581 enic_fm_copy_item_raw(struct copy_item_args *arg)
582 {
583         const struct rte_flow_item *item = arg->item;
584         const struct rte_flow_item_raw *spec = item->spec;
585         const struct rte_flow_item_raw *mask = item->mask;
586         const uint8_t lvl = arg->header_level;
587         struct fm_tcam_match_entry *entry = arg->fm_tcam_entry;
588         struct fm_header_set *fm_data, *fm_mask;
589
590         ENICPMD_FUNC_TRACE();
591         /* Cannot be used for inner packet */
592         if (lvl > 0)
593                 return -EINVAL;
594         /* Need both spec and mask */
595         if (!spec || !mask)
596                 return -EINVAL;
597         /* Only supports relative with offset 0 */
598         if (!spec->relative || spec->offset != 0 || spec->search ||
599             spec->limit)
600                 return -EINVAL;
601         /* Need non-null pattern that fits within the NIC's filter pattern */
602         if (spec->length == 0 ||
603             spec->length + sizeof(struct rte_udp_hdr) > FM_LAYER_SIZE ||
604             !spec->pattern || !mask->pattern)
605                 return -EINVAL;
606         /*
607          * Mask fields, including length, are often set to zero. Assume that
608          * means "same as spec" to avoid breaking existing apps. If length
609          * is not zero, then it should be >= spec length.
610          *
611          * No more pattern follows this, so append to the L4 layer instead of
612          * L5 to work with both recent and older VICs.
613          */
614         if (mask->length != 0 && mask->length < spec->length)
615                 return -EINVAL;
616
617         fm_data = &entry->ftm_data.fk_hdrset[lvl];
618         fm_mask = &entry->ftm_mask.fk_hdrset[lvl];
619         fm_data->fk_header_select |= FKH_L4RAW;
620         fm_mask->fk_header_select |= FKH_L4RAW;
621         fm_data->fk_header_select &= ~FKH_UDP;
622         fm_mask->fk_header_select &= ~FKH_UDP;
623         memcpy(fm_data->l4.rawdata + sizeof(struct rte_udp_hdr),
624                spec->pattern, spec->length);
625         memcpy(fm_mask->l4.rawdata + sizeof(struct rte_udp_hdr),
626                mask->pattern, spec->length);
627         return 0;
628 }
629
630 static int
631 enic_fet_alloc(struct enic_flowman *fm, uint8_t ingress,
632                struct fm_key_template *key, int entries,
633                struct enic_fm_fet **fet_out)
634 {
635         struct fm_exact_match_table *cmd;
636         struct fm_header_set *hdr;
637         struct enic_fm_fet *fet;
638         uint64_t args[3];
639         int ret;
640
641         ENICPMD_FUNC_TRACE();
642         fet = calloc(1, sizeof(struct enic_fm_fet));
643         if (fet == NULL)
644                 return -ENOMEM;
645         cmd = &fm->cmd.va->fm_exact_match_table;
646         memset(cmd, 0, sizeof(*cmd));
647         cmd->fet_direction = ingress ? FM_INGRESS : FM_EGRESS;
648         cmd->fet_stage = FM_STAGE_LAST;
649         cmd->fet_max_entries = entries ? entries : FM_MAX_EXACT_TABLE_SIZE;
650         if (key == NULL) {
651                 hdr = &cmd->fet_key.fk_hdrset[0];
652                 memset(hdr, 0, sizeof(*hdr));
653                 hdr->fk_header_select = FKH_IPV4 | FKH_UDP;
654                 hdr->l3.ip4.fk_saddr = 0xFFFFFFFF;
655                 hdr->l3.ip4.fk_daddr = 0xFFFFFFFF;
656                 hdr->l4.udp.fk_source = 0xFFFF;
657                 hdr->l4.udp.fk_dest = 0xFFFF;
658                 fet->default_key = 1;
659         } else {
660                 memcpy(&cmd->fet_key, key, sizeof(*key));
661                 memcpy(&fet->key, key, sizeof(*key));
662                 fet->default_key = 0;
663         }
664         cmd->fet_key.fk_packet_tag = 1;
665
666         args[0] = FM_EXACT_TABLE_ALLOC;
667         args[1] = fm->cmd.pa;
668         ret = vnic_dev_flowman_cmd(fm->enic->vdev, args, 2);
669         if (ret) {
670                 ENICPMD_LOG(ERR, "cannot alloc exact match table: rc=%d", ret);
671                 free(fet);
672                 return ret;
673         }
674         fet->handle = args[0];
675         fet->ingress = ingress;
676         ENICPMD_LOG(DEBUG, "allocated exact match table: handle=0x%" PRIx64,
677                     fet->handle);
678         *fet_out = fet;
679         return 0;
680 }
681
682 static void
683 enic_fet_free(struct enic_flowman *fm, struct enic_fm_fet *fet)
684 {
685         ENICPMD_FUNC_TRACE();
686         enic_fm_tbl_free(fm, fet->handle);
687         if (!fet->default_key)
688                 TAILQ_REMOVE(&fm->fet_list, fet, list);
689         free(fet);
690 }
691
692 /*
693  * Get the exact match table for the given combination of
694  * <group, ingress, key>. Allocate one on the fly as necessary.
695  */
696 static int
697 enic_fet_get(struct enic_flowman *fm,
698              uint32_t group,
699              uint8_t ingress,
700              struct fm_key_template *key,
701              struct enic_fm_fet **fet_out,
702              struct rte_flow_error *error)
703 {
704         struct enic_fm_fet *fet;
705
706         ENICPMD_FUNC_TRACE();
707         /* See if we already have this table open */
708         TAILQ_FOREACH(fet, &fm->fet_list, list) {
709                 if (fet->group == group && fet->ingress == ingress)
710                         break;
711         }
712         if (fet == NULL) {
713                 /* Jumping to a non-existing group? Use the default table */
714                 if (key == NULL) {
715                         fet = ingress ? fm->default_ig_fet : fm->default_eg_fet;
716                 } else if (enic_fet_alloc(fm, ingress, key, 0, &fet)) {
717                         return rte_flow_error_set(error, EINVAL,
718                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
719                                 NULL, "enic: cannot get exact match table");
720                 }
721                 fet->group = group;
722                 /* Default table is never on the open table list */
723                 if (!fet->default_key)
724                         TAILQ_INSERT_HEAD(&fm->fet_list, fet, list);
725         }
726         fet->ref++;
727         *fet_out = fet;
728         ENICPMD_LOG(DEBUG, "fet_get: %s %s group=%u ref=%u",
729                     fet->default_key ? "default" : "",
730                     fet->ingress ? "ingress" : "egress",
731                     fet->group, fet->ref);
732         return 0;
733 }
734
735 static void
736 enic_fet_put(struct enic_flowman *fm, struct enic_fm_fet *fet)
737 {
738         ENICPMD_FUNC_TRACE();
739         RTE_ASSERT(fet->ref > 0);
740         fet->ref--;
741         ENICPMD_LOG(DEBUG, "fet_put: %s %s group=%u ref=%u",
742                     fet->default_key ? "default" : "",
743                     fet->ingress ? "ingress" : "egress",
744                     fet->group, fet->ref);
745         if (fet->ref == 0)
746                 enic_fet_free(fm, fet);
747 }
748
749 /* Return 1 if current item is valid on top of the previous one. */
750 static int
751 fm_item_stacking_valid(enum rte_flow_item_type prev_item,
752                        const struct enic_fm_items *item_info,
753                        uint8_t is_first_item)
754 {
755         enum rte_flow_item_type const *allowed_items = item_info->prev_items;
756
757         ENICPMD_FUNC_TRACE();
758         for (; *allowed_items != RTE_FLOW_ITEM_TYPE_END; allowed_items++) {
759                 if (prev_item == *allowed_items)
760                         return 1;
761         }
762
763         /* This is the first item in the stack. Check if that's cool */
764         if (is_first_item && item_info->valid_start_item)
765                 return 1;
766         return 0;
767 }
768
769 /*
770  * Build the flow manager match entry structure from the provided pattern.
771  * The pattern is validated as the items are copied.
772  */
773 static int
774 enic_fm_copy_entry(struct enic_flowman *fm,
775                    const struct rte_flow_item pattern[],
776                    struct rte_flow_error *error)
777 {
778         const struct enic_fm_items *item_info;
779         enum rte_flow_item_type prev_item;
780         const struct rte_flow_item *item;
781         struct copy_item_args args;
782         uint8_t prev_header_level;
783         uint8_t is_first_item;
784         int ret;
785
786         ENICPMD_FUNC_TRACE();
787         item = pattern;
788         is_first_item = 1;
789         prev_item = RTE_FLOW_ITEM_TYPE_END;
790
791         args.fm_tcam_entry = &fm->tcam_entry;
792         args.header_level = 0;
793         prev_header_level = 0;
794         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
795                 /*
796                  * Get info about how to validate and copy the item. If NULL
797                  * is returned the nic does not support the item.
798                  */
799                 if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
800                         continue;
801
802                 item_info = &enic_fm_items[item->type];
803
804                 if (item->type > FM_MAX_ITEM_TYPE ||
805                     item_info->copy_item == NULL) {
806                         return rte_flow_error_set(error, ENOTSUP,
807                                 RTE_FLOW_ERROR_TYPE_ITEM,
808                                 NULL, "enic: unsupported item");
809                 }
810
811                 /* check to see if item stacking is valid */
812                 if (!fm_item_stacking_valid(prev_item, item_info,
813                                             is_first_item))
814                         goto stacking_error;
815
816                 args.item = item;
817                 ret = item_info->copy_item(&args);
818                 if (ret)
819                         goto item_not_supported;
820                 /* Going from outer to inner? Treat it as a new packet start */
821                 if (prev_header_level != args.header_level) {
822                         prev_item = RTE_FLOW_ITEM_TYPE_END;
823                         is_first_item = 1;
824                 } else {
825                         prev_item = item->type;
826                         is_first_item = 0;
827                 }
828                 prev_header_level = args.header_level;
829         }
830         return 0;
831
832 item_not_supported:
833         return rte_flow_error_set(error, -ret, RTE_FLOW_ERROR_TYPE_ITEM,
834                                   NULL, "enic: unsupported item type");
835
836 stacking_error:
837         return rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
838                                   item, "enic: unsupported item stack");
839 }
840
841 static void
842 flow_item_skip_void(const struct rte_flow_item **item)
843 {
844         for ( ; ; (*item)++)
845                 if ((*item)->type != RTE_FLOW_ITEM_TYPE_VOID)
846                         return;
847 }
848
849 static void
850 append_template(void **template, uint8_t *off, const void *data, int len)
851 {
852         memcpy(*template, data, len);
853         *template = (char *)*template + len;
854         *off = *off + len;
855 }
856
857 static int
858 enic_fm_append_action_op(struct enic_flowman *fm,
859                          struct fm_action_op *fm_op,
860                          struct rte_flow_error *error)
861 {
862         int count;
863
864         count = fm->action_op_count;
865         ENICPMD_LOG(DEBUG, "append action op: idx=%d op=%u",
866                     count, fm_op->fa_op);
867         if (count == FM_ACTION_OP_MAX) {
868                 return rte_flow_error_set(error, EINVAL,
869                         RTE_FLOW_ERROR_TYPE_ACTION, NULL,
870                         "too many action operations");
871         }
872         fm->action.fma_action_ops[count] = *fm_op;
873         fm->action_op_count = count + 1;
874         return 0;
875 }
876
877 /* NIC requires that 1st steer appear before decap.
878  * Correct example: steer, decap, steer, steer, ...
879  */
880 static void
881 enic_fm_reorder_action_op(struct enic_flowman *fm)
882 {
883         struct fm_action_op *op, *steer, *decap;
884         struct fm_action_op tmp_op;
885
886         ENICPMD_FUNC_TRACE();
887         /* Find 1st steer and decap */
888         op = fm->action.fma_action_ops;
889         steer = NULL;
890         decap = NULL;
891         while (op->fa_op != FMOP_END) {
892                 if (!decap && op->fa_op == FMOP_DECAP_NOSTRIP)
893                         decap = op;
894                 else if (!steer && op->fa_op == FMOP_RQ_STEER)
895                         steer = op;
896                 op++;
897         }
898         /* If decap is before steer, swap */
899         if (steer && decap && decap < steer) {
900                 op = fm->action.fma_action_ops;
901                 ENICPMD_LOG(DEBUG, "swap decap %ld <-> steer %ld",
902                             (long)(decap - op), (long)(steer - op));
903                 tmp_op = *decap;
904                 *decap = *steer;
905                 *steer = tmp_op;
906         }
907 }
908
909 /* VXLAN decap is done via flowman compound action */
910 static int
911 enic_fm_copy_vxlan_decap(struct enic_flowman *fm,
912                          struct fm_tcam_match_entry *fmt,
913                          const struct rte_flow_action *action,
914                          struct rte_flow_error *error)
915 {
916         struct fm_header_set *fm_data;
917         struct fm_action_op fm_op;
918
919         ENICPMD_FUNC_TRACE();
920         fm_data = &fmt->ftm_data.fk_hdrset[0];
921         if (!(fm_data->fk_metadata & FKM_VXLAN)) {
922                 return rte_flow_error_set(error, EINVAL,
923                         RTE_FLOW_ERROR_TYPE_ACTION, action,
924                         "vxlan-decap: vxlan must be in pattern");
925         }
926
927         memset(&fm_op, 0, sizeof(fm_op));
928         fm_op.fa_op = FMOP_DECAP_NOSTRIP;
929         return enic_fm_append_action_op(fm, &fm_op, error);
930 }
931
932 /* VXLAN encap is done via flowman compound action */
933 static int
934 enic_fm_copy_vxlan_encap(struct enic_flowman *fm,
935                          const struct rte_flow_item *item,
936                          struct rte_flow_error *error)
937 {
938         struct fm_action_op fm_op;
939         struct rte_ether_hdr *eth;
940         uint16_t *ethertype;
941         void *template;
942         uint8_t off;
943
944         ENICPMD_FUNC_TRACE();
945         memset(&fm_op, 0, sizeof(fm_op));
946         fm_op.fa_op = FMOP_ENCAP;
947         template = fm->action.fma_data;
948         off = 0;
949         /*
950          * Copy flow items to the flowman template starting L2.
951          * L2 must be ethernet.
952          */
953         flow_item_skip_void(&item);
954         if (item->type != RTE_FLOW_ITEM_TYPE_ETH)
955                 return rte_flow_error_set(error, EINVAL,
956                         RTE_FLOW_ERROR_TYPE_ITEM, item,
957                         "vxlan-encap: first item should be ethernet");
958         eth = (struct rte_ether_hdr *)template;
959         ethertype = &eth->ether_type;
960         append_template(&template, &off, item->spec,
961                         sizeof(struct rte_flow_item_eth));
962         item++;
963         flow_item_skip_void(&item);
964         /* Optional VLAN */
965         if (item->type == RTE_FLOW_ITEM_TYPE_VLAN) {
966                 const struct rte_flow_item_vlan *spec;
967
968                 ENICPMD_LOG(DEBUG, "vxlan-encap: vlan");
969                 spec = item->spec;
970                 fm_op.encap.outer_vlan = rte_be_to_cpu_16(spec->tci);
971                 item++;
972                 flow_item_skip_void(&item);
973         }
974         /* L3 must be IPv4, IPv6 */
975         switch (item->type) {
976         case RTE_FLOW_ITEM_TYPE_IPV4:
977         {
978                 struct rte_ipv4_hdr *ip4;
979
980                 ENICPMD_LOG(DEBUG, "vxlan-encap: ipv4");
981                 *ethertype = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
982                 ip4 = (struct rte_ipv4_hdr *)template;
983                 /*
984                  * Offset of IPv4 length field and its initial value
985                  * (IP + UDP + VXLAN) are specified in the action. The NIC
986                  * will add inner packet length.
987                  */
988                 fm_op.encap.len1_offset = off +
989                         offsetof(struct rte_ipv4_hdr, total_length);
990                 fm_op.encap.len1_delta = sizeof(struct rte_ipv4_hdr) +
991                         sizeof(struct rte_udp_hdr) +
992                         sizeof(struct rte_vxlan_hdr);
993                 append_template(&template, &off, item->spec,
994                                 sizeof(struct rte_ipv4_hdr));
995                 ip4->version_ihl = RTE_IPV4_VHL_DEF;
996                 if (ip4->time_to_live == 0)
997                         ip4->time_to_live = IP_DEFTTL;
998                 ip4->next_proto_id = IPPROTO_UDP;
999                 break;
1000         }
1001         case RTE_FLOW_ITEM_TYPE_IPV6:
1002         {
1003                 struct rte_ipv6_hdr *ip6;
1004
1005                 ENICPMD_LOG(DEBUG, "vxlan-encap: ipv6");
1006                 *ethertype = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
1007                 ip6 = (struct rte_ipv6_hdr *)template;
1008                 fm_op.encap.len1_offset = off +
1009                         offsetof(struct rte_ipv6_hdr, payload_len);
1010                 fm_op.encap.len1_delta = sizeof(struct rte_udp_hdr) +
1011                         sizeof(struct rte_vxlan_hdr);
1012                 append_template(&template, &off, item->spec,
1013                                 sizeof(struct rte_ipv6_hdr));
1014                 ip6->vtc_flow |= rte_cpu_to_be_32(IP6_VTC_FLOW);
1015                 if (ip6->hop_limits == 0)
1016                         ip6->hop_limits = IP_DEFTTL;
1017                 ip6->proto = IPPROTO_UDP;
1018                 break;
1019         }
1020         default:
1021                 return rte_flow_error_set(error,
1022                         EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
1023                         "vxlan-encap: L3 must be IPv4/IPv6");
1024         }
1025         item++;
1026         flow_item_skip_void(&item);
1027
1028         /* L4 is UDP */
1029         if (item->type != RTE_FLOW_ITEM_TYPE_UDP)
1030                 return rte_flow_error_set(error, EINVAL,
1031                         RTE_FLOW_ERROR_TYPE_ITEM, item,
1032                         "vxlan-encap: UDP must follow IPv4/IPv6");
1033         /* UDP length = UDP + VXLAN. NIC will add inner packet length. */
1034         fm_op.encap.len2_offset =
1035                 off + offsetof(struct rte_udp_hdr, dgram_len);
1036         fm_op.encap.len2_delta =
1037                 sizeof(struct rte_udp_hdr) + sizeof(struct rte_vxlan_hdr);
1038         append_template(&template, &off, item->spec,
1039                         sizeof(struct rte_udp_hdr));
1040         item++;
1041         flow_item_skip_void(&item);
1042
1043         /* Finally VXLAN */
1044         if (item->type != RTE_FLOW_ITEM_TYPE_VXLAN)
1045                 return rte_flow_error_set(error,
1046                         EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
1047                         "vxlan-encap: VXLAN must follow UDP");
1048         append_template(&template, &off, item->spec,
1049                         sizeof(struct rte_flow_item_vxlan));
1050
1051         /*
1052          * Fill in the rest of the action structure.
1053          * Indicate that we want to encap with vxlan at packet start.
1054          */
1055         fm_op.encap.template_offset = 0;
1056         fm_op.encap.template_len = off;
1057         return enic_fm_append_action_op(fm, &fm_op, error);
1058 }
1059
1060 static int
1061 enic_fm_find_vnic(struct enic *enic, const struct rte_pci_addr *addr,
1062                   uint64_t *handle)
1063 {
1064         uint32_t bdf;
1065         uint64_t args[2];
1066         int rc;
1067
1068         ENICPMD_FUNC_TRACE();
1069         ENICPMD_LOG(DEBUG, "bdf=%x:%x:%x", addr->bus, addr->devid,
1070                     addr->function);
1071         bdf = addr->bus << 8 | addr->devid << 3 | addr->function;
1072         args[0] = FM_VNIC_FIND;
1073         args[1] = bdf;
1074         rc = vnic_dev_flowman_cmd(enic->vdev, args, 2);
1075         if (rc != 0) {
1076                 ENICPMD_LOG(ERR, "allocating counters rc=%d", rc);
1077                 return rc;
1078         }
1079         *handle = args[0];
1080         ENICPMD_LOG(DEBUG, "found vnic: handle=0x%" PRIx64, *handle);
1081         return 0;
1082 }
1083
1084 /* Translate flow actions to flowman TCAM entry actions */
1085 static int
1086 enic_fm_copy_action(struct enic_flowman *fm,
1087                     const struct rte_flow_action actions[],
1088                     uint8_t ingress,
1089                     struct rte_flow_error *error)
1090 {
1091         enum {
1092                 FATE = 1 << 0,
1093                 DECAP = 1 << 1,
1094                 PASSTHRU = 1 << 2,
1095                 COUNT = 1 << 3,
1096                 ENCAP = 1 << 4,
1097                 PUSH_VLAN = 1 << 5,
1098         };
1099         struct fm_tcam_match_entry *fmt;
1100         struct fm_action_op fm_op;
1101         bool need_ovlan_action;
1102         struct enic *enic;
1103         uint32_t overlap;
1104         uint64_t vnic_h;
1105         uint16_t ovlan;
1106         bool first_rq;
1107         int ret;
1108
1109         ENICPMD_FUNC_TRACE();
1110         fmt = &fm->tcam_entry;
1111         need_ovlan_action = false;
1112         ovlan = 0;
1113         first_rq = true;
1114         enic = fm->enic;
1115         overlap = 0;
1116         vnic_h = 0; /* 0 = current vNIC */
1117         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1118                 switch (actions->type) {
1119                 case RTE_FLOW_ACTION_TYPE_VOID:
1120                         continue;
1121                 case RTE_FLOW_ACTION_TYPE_PASSTHRU: {
1122                         if (overlap & PASSTHRU)
1123                                 goto unsupported;
1124                         overlap |= PASSTHRU;
1125                         break;
1126                 }
1127                 case RTE_FLOW_ACTION_TYPE_JUMP: {
1128                         const struct rte_flow_action_jump *jump =
1129                                 actions->conf;
1130                         struct enic_fm_fet *fet;
1131
1132                         if (overlap & FATE)
1133                                 goto unsupported;
1134                         ret = enic_fet_get(fm, jump->group, ingress, NULL,
1135                                            &fet, error);
1136                         if (ret)
1137                                 return ret;
1138                         overlap |= FATE;
1139                         memset(&fm_op, 0, sizeof(fm_op));
1140                         fm_op.fa_op = FMOP_EXACT_MATCH;
1141                         fm_op.exact.handle = fet->handle;
1142                         fm->fet = fet;
1143                         ret = enic_fm_append_action_op(fm, &fm_op, error);
1144                         if (ret)
1145                                 return ret;
1146                         break;
1147                 }
1148                 case RTE_FLOW_ACTION_TYPE_MARK: {
1149                         const struct rte_flow_action_mark *mark =
1150                                 actions->conf;
1151
1152                         if (mark->id >= ENIC_MAGIC_FILTER_ID - 1)
1153                                 return rte_flow_error_set(error, EINVAL,
1154                                         RTE_FLOW_ERROR_TYPE_ACTION,
1155                                         NULL, "invalid mark id");
1156                         memset(&fm_op, 0, sizeof(fm_op));
1157                         fm_op.fa_op = FMOP_MARK;
1158                         fm_op.mark.mark = mark->id + 1;
1159                         ret = enic_fm_append_action_op(fm, &fm_op, error);
1160                         if (ret)
1161                                 return ret;
1162                         break;
1163                 }
1164                 case RTE_FLOW_ACTION_TYPE_FLAG: {
1165                         /* ENIC_MAGIC_FILTER_ID is reserved for flagging */
1166                         memset(&fm_op, 0, sizeof(fm_op));
1167                         fm_op.fa_op = FMOP_MARK;
1168                         fm_op.mark.mark = ENIC_MAGIC_FILTER_ID;
1169                         ret = enic_fm_append_action_op(fm, &fm_op, error);
1170                         if (ret)
1171                                 return ret;
1172                         break;
1173                 }
1174                 case RTE_FLOW_ACTION_TYPE_QUEUE: {
1175                         const struct rte_flow_action_queue *queue =
1176                                 actions->conf;
1177
1178                         /*
1179                          * If fate other than QUEUE or RSS, fail. Multiple
1180                          * rss and queue actions are ok.
1181                          */
1182                         if ((overlap & FATE) && first_rq)
1183                                 goto unsupported;
1184                         first_rq = false;
1185                         overlap |= FATE;
1186                         memset(&fm_op, 0, sizeof(fm_op));
1187                         fm_op.fa_op = FMOP_RQ_STEER;
1188                         fm_op.rq_steer.rq_index =
1189                                 enic_rte_rq_idx_to_sop_idx(queue->index);
1190                         fm_op.rq_steer.rq_count = 1;
1191                         fm_op.rq_steer.vnic_handle = vnic_h;
1192                         ret = enic_fm_append_action_op(fm, &fm_op, error);
1193                         if (ret)
1194                                 return ret;
1195                         ENICPMD_LOG(DEBUG, "create QUEUE action rq: %u",
1196                                     fm_op.rq_steer.rq_index);
1197                         break;
1198                 }
1199                 case RTE_FLOW_ACTION_TYPE_DROP: {
1200                         if (overlap & FATE)
1201                                 goto unsupported;
1202                         overlap |= FATE;
1203                         memset(&fm_op, 0, sizeof(fm_op));
1204                         fm_op.fa_op = FMOP_DROP;
1205                         ret = enic_fm_append_action_op(fm, &fm_op, error);
1206                         if (ret)
1207                                 return ret;
1208                         ENICPMD_LOG(DEBUG, "create DROP action");
1209                         break;
1210                 }
1211                 case RTE_FLOW_ACTION_TYPE_COUNT: {
1212                         if (overlap & COUNT)
1213                                 goto unsupported;
1214                         overlap |= COUNT;
1215                         /* Count is associated with entry not action on VIC. */
1216                         fmt->ftm_flags |= FMEF_COUNTER;
1217                         break;
1218                 }
1219                 case RTE_FLOW_ACTION_TYPE_RSS: {
1220                         const struct rte_flow_action_rss *rss = actions->conf;
1221                         bool allow;
1222                         uint16_t i;
1223
1224                         /*
1225                          * If fate other than QUEUE or RSS, fail. Multiple
1226                          * rss and queue actions are ok.
1227                          */
1228                         if ((overlap & FATE) && first_rq)
1229                                 goto unsupported;
1230                         first_rq = false;
1231                         overlap |= FATE;
1232
1233                         /*
1234                          * Hardware only supports RSS actions on outer level
1235                          * with default type and function. Queues must be
1236                          * sequential.
1237                          */
1238                         allow = rss->func == RTE_ETH_HASH_FUNCTION_DEFAULT &&
1239                                 rss->level == 0 && (rss->types == 0 ||
1240                                 rss->types == enic->rss_hf) &&
1241                                 rss->queue_num <= enic->rq_count &&
1242                                 rss->queue[rss->queue_num - 1] < enic->rq_count;
1243
1244
1245                         /* Identity queue map needs to be sequential */
1246                         for (i = 1; i < rss->queue_num; i++)
1247                                 allow = allow && (rss->queue[i] ==
1248                                         rss->queue[i - 1] + 1);
1249                         if (!allow)
1250                                 goto unsupported;
1251
1252                         memset(&fm_op, 0, sizeof(fm_op));
1253                         fm_op.fa_op = FMOP_RQ_STEER;
1254                         fm_op.rq_steer.rq_index =
1255                                 enic_rte_rq_idx_to_sop_idx(rss->queue[0]);
1256                         fm_op.rq_steer.rq_count = rss->queue_num;
1257                         fm_op.rq_steer.vnic_handle = vnic_h;
1258                         ret = enic_fm_append_action_op(fm, &fm_op, error);
1259                         if (ret)
1260                                 return ret;
1261                         ENICPMD_LOG(DEBUG, "create QUEUE action rq: %u",
1262                                     fm_op.rq_steer.rq_index);
1263                         break;
1264                 }
1265                 case RTE_FLOW_ACTION_TYPE_PORT_ID: {
1266                         const struct rte_flow_action_port_id *port;
1267                         struct rte_pci_device *pdev;
1268                         struct rte_eth_dev *dev;
1269
1270                         port = actions->conf;
1271                         if (port->original) {
1272                                 vnic_h = 0; /* This port */
1273                                 break;
1274                         }
1275                         ENICPMD_LOG(DEBUG, "port id %u", port->id);
1276                         if (!rte_eth_dev_is_valid_port(port->id)) {
1277                                 return rte_flow_error_set(error, EINVAL,
1278                                         RTE_FLOW_ERROR_TYPE_ACTION,
1279                                         NULL, "invalid port_id");
1280                         }
1281                         dev = &rte_eth_devices[port->id];
1282                         if (!dev_is_enic(dev)) {
1283                                 return rte_flow_error_set(error, EINVAL,
1284                                         RTE_FLOW_ERROR_TYPE_ACTION,
1285                                         NULL, "port_id is not enic");
1286                         }
1287                         pdev = RTE_ETH_DEV_TO_PCI(dev);
1288                         if (enic_fm_find_vnic(enic, &pdev->addr, &vnic_h)) {
1289                                 return rte_flow_error_set(error, EINVAL,
1290                                         RTE_FLOW_ERROR_TYPE_ACTION,
1291                                         NULL, "port_id is not vnic");
1292                         }
1293                         break;
1294                 }
1295                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP: {
1296                         if (overlap & DECAP)
1297                                 goto unsupported;
1298                         overlap |= DECAP;
1299
1300                         ret = enic_fm_copy_vxlan_decap(fm, fmt, actions,
1301                                 error);
1302                         if (ret != 0)
1303                                 return ret;
1304                         break;
1305                 }
1306                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP: {
1307                         const struct rte_flow_action_vxlan_encap *encap;
1308
1309                         encap = actions->conf;
1310                         if (overlap & ENCAP)
1311                                 goto unsupported;
1312                         overlap |= ENCAP;
1313                         ret = enic_fm_copy_vxlan_encap(fm, encap->definition,
1314                                 error);
1315                         if (ret != 0)
1316                                 return ret;
1317                         break;
1318                 }
1319                 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN: {
1320                         memset(&fm_op, 0, sizeof(fm_op));
1321                         fm_op.fa_op = FMOP_POP_VLAN;
1322                         ret = enic_fm_append_action_op(fm, &fm_op, error);
1323                         if (ret)
1324                                 return ret;
1325                         break;
1326                 }
1327                 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN: {
1328                         const struct rte_flow_action_of_push_vlan *vlan;
1329
1330                         if (overlap & PASSTHRU)
1331                                 goto unsupported;
1332                         vlan = actions->conf;
1333                         if (vlan->ethertype != RTE_BE16(RTE_ETHER_TYPE_VLAN)) {
1334                                 return rte_flow_error_set(error, EINVAL,
1335                                         RTE_FLOW_ERROR_TYPE_ACTION,
1336                                         NULL, "unexpected push_vlan ethertype");
1337                         }
1338                         overlap |= PUSH_VLAN;
1339                         need_ovlan_action = true;
1340                         break;
1341                 }
1342                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP: {
1343                         const struct rte_flow_action_of_set_vlan_pcp *pcp;
1344
1345                         pcp = actions->conf;
1346                         if (pcp->vlan_pcp > 7) {
1347                                 return rte_flow_error_set(error, EINVAL,
1348                                         RTE_FLOW_ERROR_TYPE_ACTION,
1349                                         NULL, "invalid vlan_pcp");
1350                         }
1351                         need_ovlan_action = true;
1352                         ovlan |= ((uint16_t)pcp->vlan_pcp) << 13;
1353                         break;
1354                 }
1355                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID: {
1356                         const struct rte_flow_action_of_set_vlan_vid *vid;
1357
1358                         vid = actions->conf;
1359                         need_ovlan_action = true;
1360                         ovlan |= rte_be_to_cpu_16(vid->vlan_vid);
1361                         break;
1362                 }
1363                 default:
1364                         goto unsupported;
1365                 }
1366         }
1367
1368         if (!(overlap & (FATE | PASSTHRU | COUNT)))
1369                 goto unsupported;
1370         if (need_ovlan_action) {
1371                 memset(&fm_op, 0, sizeof(fm_op));
1372                 fm_op.fa_op = FMOP_SET_OVLAN;
1373                 fm_op.ovlan.vlan = ovlan;
1374                 ret = enic_fm_append_action_op(fm, &fm_op, error);
1375                 if (ret)
1376                         return ret;
1377         }
1378         memset(&fm_op, 0, sizeof(fm_op));
1379         fm_op.fa_op = FMOP_END;
1380         ret = enic_fm_append_action_op(fm, &fm_op, error);
1381         if (ret)
1382                 return ret;
1383         enic_fm_reorder_action_op(fm);
1384         return 0;
1385
1386 unsupported:
1387         return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
1388                                   NULL, "enic: unsupported action");
1389 }
1390
1391 /** Check if the action is supported */
1392 static int
1393 enic_fm_match_action(const struct rte_flow_action *action,
1394                      const enum rte_flow_action_type *supported_actions)
1395 {
1396         for (; *supported_actions != RTE_FLOW_ACTION_TYPE_END;
1397              supported_actions++) {
1398                 if (action->type == *supported_actions)
1399                         return 1;
1400         }
1401         return 0;
1402 }
1403
1404 /* Debug function to dump internal NIC action structure. */
1405 static void
1406 enic_fm_dump_tcam_actions(const struct fm_action *fm_action)
1407 {
1408         /* Manually keep in sync with FMOP commands */
1409         const char *fmop_str[FMOP_OP_MAX] = {
1410                 [FMOP_END] = "end",
1411                 [FMOP_DROP] = "drop",
1412                 [FMOP_RQ_STEER] = "steer",
1413                 [FMOP_EXACT_MATCH] = "exmatch",
1414                 [FMOP_MARK] = "mark",
1415                 [FMOP_EXT_MARK] = "ext_mark",
1416                 [FMOP_TAG] = "tag",
1417                 [FMOP_EG_HAIRPIN] = "eg_hairpin",
1418                 [FMOP_IG_HAIRPIN] = "ig_hairpin",
1419                 [FMOP_ENCAP_IVLAN] = "encap_ivlan",
1420                 [FMOP_ENCAP_NOIVLAN] = "encap_noivlan",
1421                 [FMOP_ENCAP] = "encap",
1422                 [FMOP_SET_OVLAN] = "set_ovlan",
1423                 [FMOP_DECAP_NOSTRIP] = "decap_nostrip",
1424         };
1425         const struct fm_action_op *op = &fm_action->fma_action_ops[0];
1426         char buf[128], *bp = buf;
1427         const char *op_str;
1428         int i, n, buf_len;
1429
1430         buf[0] = '\0';
1431         buf_len = sizeof(buf);
1432         for (i = 0; i < FM_ACTION_OP_MAX; i++) {
1433                 if (op->fa_op == FMOP_END)
1434                         break;
1435                 if (op->fa_op >= FMOP_OP_MAX)
1436                         op_str = "unknown";
1437                 else
1438                         op_str = fmop_str[op->fa_op];
1439                 n = snprintf(bp, buf_len, "%s,", op_str);
1440                 if (n > 0 && n < buf_len) {
1441                         bp += n;
1442                         buf_len -= n;
1443                 }
1444                 op++;
1445         }
1446         /* Remove trailing comma */
1447         if (buf[0])
1448                 *(bp - 1) = '\0';
1449         ENICPMD_LOG(DEBUG, "       Acions: %s", buf);
1450 }
1451
1452 static int
1453 bits_to_str(uint32_t bits, const char *strings[], int max,
1454             char *buf, int buf_len)
1455 {
1456         int i, n = 0, len = 0;
1457
1458         for (i = 0; i < max; i++) {
1459                 if (bits & (1 << i)) {
1460                         n = snprintf(buf, buf_len, "%s,", strings[i]);
1461                         if (n > 0 && n < buf_len) {
1462                                 buf += n;
1463                                 buf_len -= n;
1464                                 len += n;
1465                         }
1466                 }
1467         }
1468         /* Remove trailing comma */
1469         if (len) {
1470                 *(buf - 1) = '\0';
1471                 len--;
1472         }
1473         return len;
1474 }
1475
1476 /* Debug function to dump internal NIC filter structure. */
1477 static void
1478 __enic_fm_dump_tcam_match(const struct fm_header_set *fk_hdrset, char *buf,
1479                           int buf_len)
1480 {
1481         /* Manually keep in sync with FKM_BITS */
1482         const char *fm_fkm_str[FKM_BIT_COUNT] = {
1483                 [FKM_QTAG_BIT] = "qtag",
1484                 [FKM_CMD_BIT] = "cmd",
1485                 [FKM_IPV4_BIT] = "ip4",
1486                 [FKM_IPV6_BIT] = "ip6",
1487                 [FKM_ROCE_BIT] = "roce",
1488                 [FKM_UDP_BIT] = "udp",
1489                 [FKM_TCP_BIT] = "tcp",
1490                 [FKM_TCPORUDP_BIT] = "tcpportudp",
1491                 [FKM_IPFRAG_BIT] = "ipfrag",
1492                 [FKM_NVGRE_BIT] = "nvgre",
1493                 [FKM_VXLAN_BIT] = "vxlan",
1494                 [FKM_GENEVE_BIT] = "geneve",
1495                 [FKM_NSH_BIT] = "nsh",
1496                 [FKM_ROCEV2_BIT] = "rocev2",
1497                 [FKM_VLAN_PRES_BIT] = "vlan_pres",
1498                 [FKM_IPOK_BIT] = "ipok",
1499                 [FKM_L4OK_BIT] = "l4ok",
1500                 [FKM_ROCEOK_BIT] = "roceok",
1501                 [FKM_FCSOK_BIT] = "fcsok",
1502                 [FKM_EG_SPAN_BIT] = "eg_span",
1503                 [FKM_IG_SPAN_BIT] = "ig_span",
1504                 [FKM_EG_HAIRPINNED_BIT] = "eg_hairpinned",
1505         };
1506         /* Manually keep in sync with FKH_BITS */
1507         const char *fm_fkh_str[FKH_BIT_COUNT] = {
1508                 [FKH_ETHER_BIT] = "eth",
1509                 [FKH_QTAG_BIT] = "qtag",
1510                 [FKH_L2RAW_BIT] = "l2raw",
1511                 [FKH_IPV4_BIT] = "ip4",
1512                 [FKH_IPV6_BIT] = "ip6",
1513                 [FKH_L3RAW_BIT] = "l3raw",
1514                 [FKH_UDP_BIT] = "udp",
1515                 [FKH_TCP_BIT] = "tcp",
1516                 [FKH_ICMP_BIT] = "icmp",
1517                 [FKH_VXLAN_BIT] = "vxlan",
1518                 [FKH_L4RAW_BIT] = "l4raw",
1519         };
1520         uint32_t fkh_bits = fk_hdrset->fk_header_select;
1521         uint32_t fkm_bits = fk_hdrset->fk_metadata;
1522         int n;
1523
1524         if (!fkm_bits && !fkh_bits)
1525                 return;
1526         n = snprintf(buf, buf_len, "metadata(");
1527         if (n > 0 && n < buf_len) {
1528                 buf += n;
1529                 buf_len -= n;
1530         }
1531         n = bits_to_str(fkm_bits, fm_fkm_str, FKM_BIT_COUNT, buf, buf_len);
1532         if (n > 0 && n < buf_len) {
1533                 buf += n;
1534                 buf_len -= n;
1535         }
1536         n = snprintf(buf, buf_len, ") valid hdr fields(");
1537         if (n > 0 && n < buf_len) {
1538                 buf += n;
1539                 buf_len -= n;
1540         }
1541         n = bits_to_str(fkh_bits, fm_fkh_str, FKH_BIT_COUNT, buf, buf_len);
1542         if (n > 0 && n < buf_len) {
1543                 buf += n;
1544                 buf_len -= n;
1545         }
1546         snprintf(buf, buf_len, ")");
1547 }
1548
1549 static void
1550 enic_fm_dump_tcam_match(const struct fm_tcam_match_entry *match,
1551                         uint8_t ingress)
1552 {
1553         char buf[256];
1554
1555         memset(buf, 0, sizeof(buf));
1556         __enic_fm_dump_tcam_match(&match->ftm_mask.fk_hdrset[0],
1557                                   buf, sizeof(buf));
1558         ENICPMD_LOG(DEBUG, " TCAM %s Outer: %s %scounter",
1559                     (ingress) ? "IG" : "EG", buf,
1560                     (match->ftm_flags & FMEF_COUNTER) ? "" : "no ");
1561         memset(buf, 0, sizeof(buf));
1562         __enic_fm_dump_tcam_match(&match->ftm_mask.fk_hdrset[1],
1563                                   buf, sizeof(buf));
1564         if (buf[0])
1565                 ENICPMD_LOG(DEBUG, "         Inner: %s", buf);
1566 }
1567
1568 /* Debug function to dump internal NIC flow structures. */
1569 static void
1570 enic_fm_dump_tcam_entry(const struct fm_tcam_match_entry *fm_match,
1571                         const struct fm_action *fm_action,
1572                         uint8_t ingress)
1573 {
1574         if (!rte_log_can_log(enic_pmd_logtype, RTE_LOG_DEBUG))
1575                 return;
1576         enic_fm_dump_tcam_match(fm_match, ingress);
1577         enic_fm_dump_tcam_actions(fm_action);
1578 }
1579
1580 static int
1581 enic_fm_flow_parse(struct enic_flowman *fm,
1582                    const struct rte_flow_attr *attrs,
1583                    const struct rte_flow_item pattern[],
1584                    const struct rte_flow_action actions[],
1585                    struct rte_flow_error *error)
1586 {
1587         const struct rte_flow_action *action;
1588         unsigned int ret;
1589         static const enum rte_flow_action_type *sa;
1590
1591         ENICPMD_FUNC_TRACE();
1592         ret = 0;
1593         if (!pattern) {
1594                 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1595                                    NULL, "no pattern specified");
1596                 return -rte_errno;
1597         }
1598
1599         if (!actions) {
1600                 rte_flow_error_set(error, EINVAL,
1601                                    RTE_FLOW_ERROR_TYPE_ACTION_NUM,
1602                                    NULL, "no action specified");
1603                 return -rte_errno;
1604         }
1605
1606         if (attrs) {
1607                 if (attrs->priority) {
1608                         rte_flow_error_set(error, ENOTSUP,
1609                                            RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1610                                            NULL,
1611                                            "priorities are not supported");
1612                         return -rte_errno;
1613                 } else if (attrs->transfer) {
1614                         rte_flow_error_set(error, ENOTSUP,
1615                                            RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
1616                                            NULL,
1617                                            "transfer is not supported");
1618                         return -rte_errno;
1619                 } else if (attrs->ingress && attrs->egress) {
1620                         rte_flow_error_set(error, ENOTSUP,
1621                                            RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1622                                            NULL,
1623                                            "bidirectional rules not supported");
1624                         return -rte_errno;
1625                 }
1626
1627         } else {
1628                 rte_flow_error_set(error, EINVAL,
1629                                    RTE_FLOW_ERROR_TYPE_ATTR,
1630                                    NULL, "no attribute specified");
1631                 return -rte_errno;
1632         }
1633
1634         /* Verify Actions. */
1635         sa = (attrs->ingress) ? enic_fm_supported_ig_actions :
1636              enic_fm_supported_eg_actions;
1637         for (action = &actions[0]; action->type != RTE_FLOW_ACTION_TYPE_END;
1638              action++) {
1639                 if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
1640                         continue;
1641                 else if (!enic_fm_match_action(action, sa))
1642                         break;
1643         }
1644         if (action->type != RTE_FLOW_ACTION_TYPE_END) {
1645                 rte_flow_error_set(error, EPERM, RTE_FLOW_ERROR_TYPE_ACTION,
1646                                    action, "invalid action");
1647                 return -rte_errno;
1648         }
1649         ret = enic_fm_copy_entry(fm, pattern, error);
1650         if (ret)
1651                 return ret;
1652         ret = enic_fm_copy_action(fm, actions, attrs->ingress, error);
1653         return ret;
1654 }
1655
1656 static void
1657 enic_fm_counter_free(struct enic_flowman *fm, struct enic_fm_flow *fm_flow)
1658 {
1659         if (!fm_flow->counter_valid)
1660                 return;
1661         SLIST_INSERT_HEAD(&fm->counters, fm_flow->counter, next);
1662         fm_flow->counter_valid = false;
1663 }
1664
1665 static int
1666 enic_fm_more_counters(struct enic_flowman *fm)
1667 {
1668         struct enic_fm_counter *new_stack;
1669         struct enic_fm_counter *ctrs;
1670         struct enic *enic;
1671         int i, rc;
1672         uint64_t args[2];
1673
1674         ENICPMD_FUNC_TRACE();
1675         enic = fm->enic;
1676         new_stack = rte_realloc(fm->counter_stack, (fm->counters_alloced +
1677                                 FM_COUNTERS_EXPAND) *
1678                                 sizeof(struct enic_fm_counter), 0);
1679         if (new_stack == NULL) {
1680                 ENICPMD_LOG(ERR, "cannot alloc counter memory");
1681                 return -ENOMEM;
1682         }
1683         fm->counter_stack = new_stack;
1684
1685         args[0] = FM_COUNTER_BRK;
1686         args[1] = fm->counters_alloced + FM_COUNTERS_EXPAND;
1687         rc = vnic_dev_flowman_cmd(enic->vdev, args, 2);
1688         if (rc != 0) {
1689                 ENICPMD_LOG(ERR, "cannot alloc counters rc=%d", rc);
1690                 return rc;
1691         }
1692         ctrs = (struct enic_fm_counter *)fm->counter_stack +
1693                 fm->counters_alloced;
1694         for (i = 0; i < FM_COUNTERS_EXPAND; i++, ctrs++) {
1695                 ctrs->handle = fm->counters_alloced + i;
1696                 SLIST_INSERT_HEAD(&fm->counters, ctrs, next);
1697         }
1698         fm->counters_alloced += FM_COUNTERS_EXPAND;
1699         ENICPMD_LOG(DEBUG, "%u counters allocated, total: %u",
1700                     FM_COUNTERS_EXPAND, fm->counters_alloced);
1701         return 0;
1702 }
1703
1704 static int
1705 enic_fm_counter_zero(struct enic_flowman *fm, struct enic_fm_counter *c)
1706 {
1707         struct enic *enic;
1708         uint64_t args[3];
1709         int ret;
1710
1711         ENICPMD_FUNC_TRACE();
1712         enic = fm->enic;
1713         args[0] = FM_COUNTER_QUERY;
1714         args[1] = c->handle;
1715         args[2] = 1; /* clear */
1716         ret = vnic_dev_flowman_cmd(enic->vdev, args, 3);
1717         if (ret) {
1718                 ENICPMD_LOG(ERR, "counter init: rc=%d handle=0x%x",
1719                             ret, c->handle);
1720                 return ret;
1721         }
1722         return 0;
1723 }
1724
1725 static int
1726 enic_fm_counter_alloc(struct enic_flowman *fm, struct rte_flow_error *error,
1727                       struct enic_fm_counter **ctr)
1728 {
1729         struct enic_fm_counter *c;
1730         int ret;
1731
1732         ENICPMD_FUNC_TRACE();
1733         *ctr = NULL;
1734         if (SLIST_EMPTY(&fm->counters)) {
1735                 ret = enic_fm_more_counters(fm);
1736                 if (ret)
1737                         return rte_flow_error_set(error, -ret,
1738                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1739                                 NULL, "enic: out of counters");
1740         }
1741         c = SLIST_FIRST(&fm->counters);
1742         SLIST_REMOVE_HEAD(&fm->counters, next);
1743         *ctr = c;
1744         return 0;
1745 }
1746
1747 static int
1748 enic_fm_action_free(struct enic_flowman *fm, uint64_t handle)
1749 {
1750         uint64_t args[2];
1751         int rc;
1752
1753         ENICPMD_FUNC_TRACE();
1754         args[0] = FM_ACTION_FREE;
1755         args[1] = handle;
1756         rc = vnic_dev_flowman_cmd(fm->enic->vdev, args, 2);
1757         if (rc)
1758                 ENICPMD_LOG(ERR, "cannot free action: rc=%d handle=0x%" PRIx64,
1759                             rc, handle);
1760         return rc;
1761 }
1762
1763 static int
1764 enic_fm_entry_free(struct enic_flowman *fm, uint64_t handle)
1765 {
1766         uint64_t args[2];
1767         int rc;
1768
1769         ENICPMD_FUNC_TRACE();
1770         args[0] = FM_MATCH_ENTRY_REMOVE;
1771         args[1] = handle;
1772         rc = vnic_dev_flowman_cmd(fm->enic->vdev, args, 2);
1773         if (rc)
1774                 ENICPMD_LOG(ERR, "cannot free match entry: rc=%d"
1775                             " handle=0x%" PRIx64, rc, handle);
1776         return rc;
1777 }
1778
1779 static struct enic_fm_jump_flow *
1780 find_jump_flow(struct enic_flowman *fm, uint32_t group)
1781 {
1782         struct enic_fm_jump_flow *j;
1783
1784         ENICPMD_FUNC_TRACE();
1785         TAILQ_FOREACH(j, &fm->jump_list, list) {
1786                 if (j->group == group)
1787                         return j;
1788         }
1789         return NULL;
1790 }
1791
1792 static void
1793 remove_jump_flow(struct enic_flowman *fm, struct rte_flow *flow)
1794 {
1795         struct enic_fm_jump_flow *j;
1796
1797         ENICPMD_FUNC_TRACE();
1798         TAILQ_FOREACH(j, &fm->jump_list, list) {
1799                 if (j->flow == flow) {
1800                         TAILQ_REMOVE(&fm->jump_list, j, list);
1801                         free(j);
1802                         return;
1803                 }
1804         }
1805 }
1806
1807 static int
1808 save_jump_flow(struct enic_flowman *fm,
1809                struct rte_flow *flow,
1810                uint32_t group,
1811                struct fm_tcam_match_entry *match,
1812                struct fm_action *action)
1813 {
1814         struct enic_fm_jump_flow *j;
1815
1816         ENICPMD_FUNC_TRACE();
1817         j = calloc(1, sizeof(struct enic_fm_jump_flow));
1818         if (j == NULL)
1819                 return -ENOMEM;
1820         j->flow = flow;
1821         j->group = group;
1822         j->match = *match;
1823         j->action = *action;
1824         TAILQ_INSERT_HEAD(&fm->jump_list, j, list);
1825         ENICPMD_LOG(DEBUG, "saved jump flow: flow=%p group=%u", flow, group);
1826         return 0;
1827 }
1828
1829 static void
1830 __enic_fm_flow_free(struct enic_flowman *fm, struct enic_fm_flow *fm_flow)
1831 {
1832         if (fm_flow->entry_handle != FM_INVALID_HANDLE) {
1833                 enic_fm_entry_free(fm, fm_flow->entry_handle);
1834                 fm_flow->entry_handle = FM_INVALID_HANDLE;
1835         }
1836         if (fm_flow->action_handle != FM_INVALID_HANDLE) {
1837                 enic_fm_action_free(fm, fm_flow->action_handle);
1838                 fm_flow->action_handle = FM_INVALID_HANDLE;
1839         }
1840         enic_fm_counter_free(fm, fm_flow);
1841         if (fm_flow->fet) {
1842                 enic_fet_put(fm, fm_flow->fet);
1843                 fm_flow->fet = NULL;
1844         }
1845 }
1846
1847 static void
1848 enic_fm_flow_free(struct enic_flowman *fm, struct rte_flow *flow)
1849 {
1850         if (flow->fm->fet && flow->fm->fet->default_key)
1851                 remove_jump_flow(fm, flow);
1852         __enic_fm_flow_free(fm, flow->fm);
1853         free(flow->fm);
1854         free(flow);
1855 }
1856
1857 static int
1858 enic_fm_add_tcam_entry(struct enic_flowman *fm,
1859                        struct fm_tcam_match_entry *match_in,
1860                        uint64_t *entry_handle,
1861                        uint8_t ingress,
1862                        struct rte_flow_error *error)
1863 {
1864         struct fm_tcam_match_entry *ftm;
1865         uint64_t args[3];
1866         int ret;
1867
1868         ENICPMD_FUNC_TRACE();
1869         /* Copy entry to the command buffer */
1870         ftm = &fm->cmd.va->fm_tcam_match_entry;
1871         memcpy(ftm, match_in, sizeof(*ftm));
1872         /* Add TCAM entry */
1873         args[0] = FM_TCAM_ENTRY_INSTALL;
1874         args[1] = ingress ? fm->ig_tcam_hndl : fm->eg_tcam_hndl;
1875         args[2] = fm->cmd.pa;
1876         ret = vnic_dev_flowman_cmd(fm->enic->vdev, args, 3);
1877         if (ret != 0) {
1878                 ENICPMD_LOG(ERR, "cannot add %s TCAM entry: rc=%d",
1879                             ingress ? "ingress" : "egress", ret);
1880                 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1881                         NULL, "enic: devcmd(tcam-entry-install)");
1882                 return ret;
1883         }
1884         ENICPMD_LOG(DEBUG, "installed %s TCAM entry: handle=0x%" PRIx64,
1885                     ingress ? "ingress" : "egress", (uint64_t)args[0]);
1886         *entry_handle = args[0];
1887         return 0;
1888 }
1889
1890 static int
1891 enic_fm_add_exact_entry(struct enic_flowman *fm,
1892                         struct fm_tcam_match_entry *match_in,
1893                         uint64_t *entry_handle,
1894                         struct enic_fm_fet *fet,
1895                         struct rte_flow_error *error)
1896 {
1897         struct fm_exact_match_entry *fem;
1898         uint64_t args[3];
1899         int ret;
1900
1901         ENICPMD_FUNC_TRACE();
1902         /* The new entry must have the table's key */
1903         if (memcmp(fet->key.fk_hdrset, match_in->ftm_mask.fk_hdrset,
1904                    sizeof(struct fm_header_set) * FM_HDRSET_MAX)) {
1905                 return rte_flow_error_set(error, EINVAL,
1906                         RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1907                         "enic: key does not match group's key");
1908         }
1909
1910         /* Copy entry to the command buffer */
1911         fem = &fm->cmd.va->fm_exact_match_entry;
1912         /*
1913          * Translate TCAM entry to exact entry. As is only need to drop
1914          * position and mask. The mask is part of the exact match table.
1915          * Position (aka priority) is not supported in the exact match table.
1916          */
1917         fem->fem_data = match_in->ftm_data;
1918         fem->fem_flags = match_in->ftm_flags;
1919         fem->fem_action = match_in->ftm_action;
1920         fem->fem_counter = match_in->ftm_counter;
1921
1922         /* Add exact entry */
1923         args[0] = FM_EXACT_ENTRY_INSTALL;
1924         args[1] = fet->handle;
1925         args[2] = fm->cmd.pa;
1926         ret = vnic_dev_flowman_cmd(fm->enic->vdev, args, 3);
1927         if (ret != 0) {
1928                 ENICPMD_LOG(ERR, "cannot add %s exact entry: group=%u",
1929                             fet->ingress ? "ingress" : "egress", fet->group);
1930                 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1931                         NULL, "enic: devcmd(exact-entry-install)");
1932                 return ret;
1933         }
1934         ENICPMD_LOG(DEBUG, "installed %s exact entry: group=%u"
1935                     " handle=0x%" PRIx64,
1936                     fet->ingress ? "ingress" : "egress", fet->group,
1937                     (uint64_t)args[0]);
1938         *entry_handle = args[0];
1939         return 0;
1940 }
1941
1942 /* Push match-action to the NIC. */
1943 static int
1944 __enic_fm_flow_add_entry(struct enic_flowman *fm,
1945                          struct enic_fm_flow *fm_flow,
1946                          struct fm_tcam_match_entry *match_in,
1947                          struct fm_action *action_in,
1948                          uint32_t group,
1949                          uint8_t ingress,
1950                          struct rte_flow_error *error)
1951 {
1952         struct enic_fm_counter *ctr;
1953         struct fm_action *fma;
1954         uint64_t action_h;
1955         uint64_t entry_h;
1956         uint64_t args[3];
1957         int ret;
1958
1959         ENICPMD_FUNC_TRACE();
1960         /* Allocate action. */
1961         fma = &fm->cmd.va->fm_action;
1962         memcpy(fma, action_in, sizeof(*fma));
1963         args[0] = FM_ACTION_ALLOC;
1964         args[1] = fm->cmd.pa;
1965         ret = vnic_dev_flowman_cmd(fm->enic->vdev, args, 2);
1966         if (ret != 0) {
1967                 ENICPMD_LOG(ERR, "allocating TCAM table action rc=%d", ret);
1968                 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1969                         NULL, "enic: devcmd(action-alloc)");
1970                 return ret;
1971         }
1972         action_h = args[0];
1973         fm_flow->action_handle = action_h;
1974         match_in->ftm_action = action_h;
1975         ENICPMD_LOG(DEBUG, "action allocated: handle=0x%" PRIx64, action_h);
1976
1977         /* Allocate counter if requested. */
1978         if (match_in->ftm_flags & FMEF_COUNTER) {
1979                 ret = enic_fm_counter_alloc(fm, error, &ctr);
1980                 if (ret) /* error has been filled in */
1981                         return ret;
1982                 fm_flow->counter_valid = true;
1983                 fm_flow->counter = ctr;
1984                 match_in->ftm_counter = ctr->handle;
1985         }
1986
1987         /*
1988          * Get the group's table (either TCAM or exact match table) and
1989          * add entry to it. If we use the exact match table, the handler
1990          * will translate the TCAM entry (match_in) to the appropriate
1991          * exact match entry and use that instead.
1992          */
1993         entry_h = FM_INVALID_HANDLE;
1994         if (group == FM_TCAM_RTE_GROUP) {
1995                 ret = enic_fm_add_tcam_entry(fm, match_in, &entry_h, ingress,
1996                                              error);
1997                 if (ret)
1998                         return ret;
1999                 /* Jump action might have a ref to fet */
2000                 fm_flow->fet = fm->fet;
2001                 fm->fet = NULL;
2002         } else {
2003                 struct enic_fm_fet *fet = NULL;
2004
2005                 ret = enic_fet_get(fm, group, ingress,
2006                                    &match_in->ftm_mask, &fet, error);
2007                 if (ret)
2008                         return ret;
2009                 fm_flow->fet = fet;
2010                 ret = enic_fm_add_exact_entry(fm, match_in, &entry_h, fet,
2011                                               error);
2012                 if (ret)
2013                         return ret;
2014         }
2015         /* Clear counter after adding entry, as it requires in-use counter */
2016         if (fm_flow->counter_valid) {
2017                 ret = enic_fm_counter_zero(fm, fm_flow->counter);
2018                 if (ret)
2019                         return ret;
2020         }
2021         fm_flow->entry_handle = entry_h;
2022         return 0;
2023 }
2024
2025 /* Push match-action to the NIC. */
2026 static struct rte_flow *
2027 enic_fm_flow_add_entry(struct enic_flowman *fm,
2028                        struct fm_tcam_match_entry *match_in,
2029                        struct fm_action *action_in,
2030                        const struct rte_flow_attr *attrs,
2031                        struct rte_flow_error *error)
2032 {
2033         struct enic_fm_flow *fm_flow;
2034         struct rte_flow *flow;
2035
2036         ENICPMD_FUNC_TRACE();
2037         enic_fm_dump_tcam_entry(match_in, action_in, attrs->ingress);
2038         flow = calloc(1, sizeof(*flow));
2039         fm_flow = calloc(1, sizeof(*fm_flow));
2040         if (flow == NULL || fm_flow == NULL) {
2041                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
2042                         NULL, "enic: cannot allocate rte_flow");
2043                 free(flow);
2044                 free(fm_flow);
2045                 return NULL;
2046         }
2047         flow->fm = fm_flow;
2048         fm_flow->action_handle = FM_INVALID_HANDLE;
2049         fm_flow->entry_handle = FM_INVALID_HANDLE;
2050         if (__enic_fm_flow_add_entry(fm, fm_flow, match_in, action_in,
2051                                      attrs->group, attrs->ingress, error)) {
2052                 enic_fm_flow_free(fm, flow);
2053                 return NULL;
2054         }
2055         return flow;
2056 }
2057
2058 static void
2059 convert_jump_flows(struct enic_flowman *fm, struct enic_fm_fet *fet,
2060                    struct rte_flow_error *error)
2061 {
2062         struct enic_fm_flow *fm_flow;
2063         struct enic_fm_jump_flow *j;
2064         struct fm_action *fma;
2065         uint32_t group;
2066
2067         ENICPMD_FUNC_TRACE();
2068         /*
2069          * Find the saved flows that should jump to the new table (fet).
2070          * Then delete the old TCAM entry that jumps to the default table,
2071          * and add a new one that jumps to the new table.
2072          */
2073         group = fet->group;
2074         j = find_jump_flow(fm, group);
2075         while (j) {
2076                 ENICPMD_LOG(DEBUG, "convert jump flow: flow=%p group=%u",
2077                             j->flow, group);
2078                 /* Delete old entry */
2079                 fm_flow = j->flow->fm;
2080                 __enic_fm_flow_free(fm, fm_flow);
2081
2082                 /* Add new entry */
2083                 fma = &j->action;
2084                 fma->fma_action_ops[0].exact.handle = fet->handle;
2085                 if (__enic_fm_flow_add_entry(fm, fm_flow, &j->match, fma,
2086                         FM_TCAM_RTE_GROUP, fet->ingress, error)) {
2087                         /* Cannot roll back changes at the moment */
2088                         ENICPMD_LOG(ERR, "cannot convert jump flow: flow=%p",
2089                                     j->flow);
2090                 } else {
2091                         fm_flow->fet = fet;
2092                         fet->ref++;
2093                         ENICPMD_LOG(DEBUG, "convert ok: group=%u ref=%u",
2094                                     fet->group, fet->ref);
2095                 }
2096
2097                 TAILQ_REMOVE(&fm->jump_list, j, list);
2098                 free(j);
2099                 j = find_jump_flow(fm, group);
2100         }
2101 }
2102
2103 static void
2104 enic_fm_open_scratch(struct enic_flowman *fm)
2105 {
2106         fm->action_op_count = 0;
2107         fm->fet = NULL;
2108         memset(&fm->tcam_entry, 0, sizeof(fm->tcam_entry));
2109         memset(&fm->action, 0, sizeof(fm->action));
2110 }
2111
2112 static void
2113 enic_fm_close_scratch(struct enic_flowman *fm)
2114 {
2115         if (fm->fet) {
2116                 enic_fet_put(fm, fm->fet);
2117                 fm->fet = NULL;
2118         }
2119         fm->action_op_count = 0;
2120 }
2121
2122 static int
2123 enic_fm_flow_validate(struct rte_eth_dev *dev,
2124                       const struct rte_flow_attr *attrs,
2125                       const struct rte_flow_item pattern[],
2126                       const struct rte_flow_action actions[],
2127                       struct rte_flow_error *error)
2128 {
2129         struct fm_tcam_match_entry *fm_tcam_entry;
2130         struct fm_action *fm_action;
2131         struct enic_flowman *fm;
2132         int ret;
2133
2134         ENICPMD_FUNC_TRACE();
2135         fm = pmd_priv(dev)->fm;
2136         if (fm == NULL)
2137                 return -ENOTSUP;
2138         enic_fm_open_scratch(fm);
2139         ret = enic_fm_flow_parse(fm, attrs, pattern, actions, error);
2140         if (!ret) {
2141                 fm_tcam_entry = &fm->tcam_entry;
2142                 fm_action = &fm->action;
2143                 enic_fm_dump_tcam_entry(fm_tcam_entry, fm_action,
2144                                         attrs->ingress);
2145         }
2146         enic_fm_close_scratch(fm);
2147         return ret;
2148 }
2149
2150 static int
2151 enic_fm_flow_query_count(struct rte_eth_dev *dev,
2152                          struct rte_flow *flow, void *data,
2153                          struct rte_flow_error *error)
2154 {
2155         struct rte_flow_query_count *query;
2156         struct enic_fm_flow *fm_flow;
2157         struct enic *enic;
2158         uint64_t args[3];
2159         int rc;
2160
2161         ENICPMD_FUNC_TRACE();
2162         enic = pmd_priv(dev);
2163         query = data;
2164         fm_flow = flow->fm;
2165         if (!fm_flow->counter_valid)
2166                 return rte_flow_error_set(error, ENOTSUP,
2167                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2168                         "enic: flow does not have counter");
2169
2170         args[0] = FM_COUNTER_QUERY;
2171         args[1] = fm_flow->counter->handle;
2172         args[2] = query->reset;
2173         rc = vnic_dev_flowman_cmd(enic->vdev, args, 3);
2174         if (rc) {
2175                 ENICPMD_LOG(ERR, "cannot query counter: rc=%d handle=0x%x",
2176                             rc, fm_flow->counter->handle);
2177                 return rc;
2178         }
2179         query->hits_set = 1;
2180         query->hits = args[0];
2181         query->bytes_set = 1;
2182         query->bytes = args[1];
2183         return 0;
2184 }
2185
2186 static int
2187 enic_fm_flow_query(struct rte_eth_dev *dev,
2188                    struct rte_flow *flow,
2189                    const struct rte_flow_action *actions,
2190                    void *data,
2191                    struct rte_flow_error *error)
2192 {
2193         int ret = 0;
2194
2195         ENICPMD_FUNC_TRACE();
2196         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
2197                 switch (actions->type) {
2198                 case RTE_FLOW_ACTION_TYPE_VOID:
2199                         break;
2200                 case RTE_FLOW_ACTION_TYPE_COUNT:
2201                         ret = enic_fm_flow_query_count(dev, flow, data, error);
2202                         break;
2203                 default:
2204                         return rte_flow_error_set(error, ENOTSUP,
2205                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2206                                                   actions,
2207                                                   "action not supported");
2208                 }
2209                 if (ret < 0)
2210                         return ret;
2211         }
2212         return 0;
2213 }
2214
2215 static struct rte_flow *
2216 enic_fm_flow_create(struct rte_eth_dev *dev,
2217                     const struct rte_flow_attr *attrs,
2218                     const struct rte_flow_item pattern[],
2219                     const struct rte_flow_action actions[],
2220                     struct rte_flow_error *error)
2221 {
2222         struct fm_tcam_match_entry *fm_tcam_entry;
2223         struct fm_action *fm_action;
2224         struct enic_flowman *fm;
2225         struct enic_fm_fet *fet;
2226         struct rte_flow *flow;
2227         struct enic *enic;
2228         int ret;
2229
2230         ENICPMD_FUNC_TRACE();
2231         enic = pmd_priv(dev);
2232         fm = enic->fm;
2233         if (fm == NULL) {
2234                 rte_flow_error_set(error, ENOTSUP,
2235                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2236                         "flowman is not initialized");
2237                 return NULL;
2238         }
2239         enic_fm_open_scratch(fm);
2240         flow = NULL;
2241         ret = enic_fm_flow_parse(fm, attrs, pattern, actions, error);
2242         if (ret < 0)
2243                 goto error_with_scratch;
2244         fm_tcam_entry = &fm->tcam_entry;
2245         fm_action = &fm->action;
2246         flow = enic_fm_flow_add_entry(fm, fm_tcam_entry, fm_action,
2247                                       attrs, error);
2248         if (flow) {
2249                 LIST_INSERT_HEAD(&enic->flows, flow, next);
2250                 fet = flow->fm->fet;
2251                 if (fet && fet->default_key) {
2252                         /*
2253                          * Jump to non-existent group? Save the relevant info
2254                          * so we can convert this flow when that group
2255                          * materializes.
2256                          */
2257                         save_jump_flow(fm, flow, fet->group,
2258                                        fm_tcam_entry, fm_action);
2259                 } else if (fet && fet->ref == 1) {
2260                         /*
2261                          * A new table is created. Convert the saved flows
2262                          * that should jump to this group.
2263                          */
2264                         convert_jump_flows(fm, fet, error);
2265                 }
2266         }
2267
2268 error_with_scratch:
2269         enic_fm_close_scratch(fm);
2270         return flow;
2271 }
2272
2273 static int
2274 enic_fm_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
2275                      __rte_unused struct rte_flow_error *error)
2276 {
2277         struct enic *enic = pmd_priv(dev);
2278
2279         ENICPMD_FUNC_TRACE();
2280         if (enic->fm == NULL)
2281                 return 0;
2282         LIST_REMOVE(flow, next);
2283         enic_fm_flow_free(enic->fm, flow);
2284         return 0;
2285 }
2286
2287 static int
2288 enic_fm_flow_flush(struct rte_eth_dev *dev,
2289                    __rte_unused struct rte_flow_error *error)
2290 {
2291         struct enic_fm_flow *fm_flow;
2292         struct enic_flowman *fm;
2293         struct rte_flow *flow;
2294         struct enic *enic = pmd_priv(dev);
2295
2296         ENICPMD_FUNC_TRACE();
2297         if (enic->fm == NULL)
2298                 return 0;
2299         fm = enic->fm;
2300         while (!LIST_EMPTY(&enic->flows)) {
2301                 flow = LIST_FIRST(&enic->flows);
2302                 fm_flow = flow->fm;
2303                 LIST_REMOVE(flow, next);
2304                 /*
2305                  * If tables are null, then vNIC is closing, and the firmware
2306                  * has already cleaned up flowman state. So do not try to free
2307                  * resources, as it only causes errors.
2308                  */
2309                 if (fm->ig_tcam_hndl == FM_INVALID_HANDLE) {
2310                         fm_flow->entry_handle = FM_INVALID_HANDLE;
2311                         fm_flow->action_handle = FM_INVALID_HANDLE;
2312                         fm_flow->fet = NULL;
2313                 }
2314                 enic_fm_flow_free(fm, flow);
2315         }
2316         return 0;
2317 }
2318
2319 static int
2320 enic_fm_tbl_free(struct enic_flowman *fm, uint64_t handle)
2321 {
2322         uint64_t args[2];
2323         int rc;
2324
2325         args[0] = FM_MATCH_TABLE_FREE;
2326         args[1] = handle;
2327         rc = vnic_dev_flowman_cmd(fm->enic->vdev, args, 2);
2328         if (rc)
2329                 ENICPMD_LOG(ERR, "cannot free table: rc=%d handle=0x%" PRIx64,
2330                             rc, handle);
2331         return rc;
2332 }
2333
2334 static int
2335 enic_fm_tcam_tbl_alloc(struct enic_flowman *fm, uint32_t direction,
2336                         uint32_t max_entries, uint64_t *handle)
2337 {
2338         struct fm_tcam_match_table *tcam_tbl;
2339         struct enic *enic;
2340         uint64_t args[2];
2341         int rc;
2342
2343         ENICPMD_FUNC_TRACE();
2344         enic = fm->enic;
2345         tcam_tbl = &fm->cmd.va->fm_tcam_match_table;
2346         tcam_tbl->ftt_direction = direction;
2347         tcam_tbl->ftt_stage = FM_STAGE_LAST;
2348         tcam_tbl->ftt_max_entries = max_entries;
2349         args[0] = FM_TCAM_TABLE_ALLOC;
2350         args[1] = fm->cmd.pa;
2351         rc = vnic_dev_flowman_cmd(enic->vdev, args, 2);
2352         if (rc) {
2353                 ENICPMD_LOG(ERR, "cannot alloc %s TCAM table: rc=%d",
2354                             (direction == FM_INGRESS) ? "IG" : "EG", rc);
2355                 return rc;
2356         }
2357         *handle = args[0];
2358         ENICPMD_LOG(DEBUG, "%s TCAM table allocated, handle=0x%" PRIx64,
2359                     (direction == FM_INGRESS) ? "IG" : "EG", *handle);
2360         return 0;
2361 }
2362
2363 static int
2364 enic_fm_init_counters(struct enic_flowman *fm)
2365 {
2366         ENICPMD_FUNC_TRACE();
2367         SLIST_INIT(&fm->counters);
2368         return enic_fm_more_counters(fm);
2369 }
2370
2371 static void
2372 enic_fm_free_all_counters(struct enic_flowman *fm)
2373 {
2374         struct enic *enic;
2375         uint64_t args[2];
2376         int rc;
2377
2378         enic = fm->enic;
2379         args[0] = FM_COUNTER_BRK;
2380         args[1] = 0;
2381         rc = vnic_dev_flowman_cmd(enic->vdev, args, 2);
2382         if (rc != 0)
2383                 ENICPMD_LOG(ERR, "cannot free counters: rc=%d", rc);
2384         rte_free(fm->counter_stack);
2385 }
2386
2387 static int
2388 enic_fm_alloc_tcam_tables(struct enic_flowman *fm)
2389 {
2390         int rc;
2391
2392         ENICPMD_FUNC_TRACE();
2393         rc = enic_fm_tcam_tbl_alloc(fm, FM_INGRESS, FM_MAX_TCAM_TABLE_SIZE,
2394                                     &fm->ig_tcam_hndl);
2395         if (rc)
2396                 return rc;
2397         rc = enic_fm_tcam_tbl_alloc(fm, FM_EGRESS, FM_MAX_TCAM_TABLE_SIZE,
2398                                     &fm->eg_tcam_hndl);
2399         return rc;
2400 }
2401
2402 static void
2403 enic_fm_free_tcam_tables(struct enic_flowman *fm)
2404 {
2405         ENICPMD_FUNC_TRACE();
2406         if (fm->ig_tcam_hndl) {
2407                 ENICPMD_LOG(DEBUG, "free IG TCAM table handle=0x%" PRIx64,
2408                             fm->ig_tcam_hndl);
2409                 enic_fm_tbl_free(fm, fm->ig_tcam_hndl);
2410                 fm->ig_tcam_hndl = FM_INVALID_HANDLE;
2411         }
2412         if (fm->eg_tcam_hndl) {
2413                 ENICPMD_LOG(DEBUG, "free EG TCAM table handle=0x%" PRIx64,
2414                             fm->eg_tcam_hndl);
2415                 enic_fm_tbl_free(fm, fm->eg_tcam_hndl);
2416                 fm->eg_tcam_hndl = FM_INVALID_HANDLE;
2417         }
2418 }
2419
2420 int
2421 enic_fm_init(struct enic *enic)
2422 {
2423         struct enic_flowman *fm;
2424         uint8_t name[RTE_MEMZONE_NAMESIZE];
2425         int rc;
2426
2427         if (enic->flow_filter_mode != FILTER_FLOWMAN)
2428                 return 0;
2429         ENICPMD_FUNC_TRACE();
2430         fm = calloc(1, sizeof(*fm));
2431         if (fm == NULL) {
2432                 ENICPMD_LOG(ERR, "cannot alloc flowman struct");
2433                 return -ENOMEM;
2434         }
2435         fm->enic = enic;
2436         TAILQ_INIT(&fm->fet_list);
2437         TAILQ_INIT(&fm->jump_list);
2438         /* Allocate host memory for flowman commands */
2439         snprintf((char *)name, sizeof(name), "fm-cmd-%s", enic->bdf_name);
2440         fm->cmd.va = enic_alloc_consistent(enic,
2441                 sizeof(union enic_flowman_cmd_mem), &fm->cmd.pa, name);
2442         if (!fm->cmd.va) {
2443                 ENICPMD_LOG(ERR, "cannot allocate flowman command memory");
2444                 rc = -ENOMEM;
2445                 goto error_fm;
2446         }
2447         /* Allocate TCAM tables upfront as they are the main tables */
2448         rc = enic_fm_alloc_tcam_tables(fm);
2449         if (rc) {
2450                 ENICPMD_LOG(ERR, "cannot alloc TCAM tables");
2451                 goto error_cmd;
2452         }
2453         /* Then a number of counters */
2454         rc = enic_fm_init_counters(fm);
2455         if (rc) {
2456                 ENICPMD_LOG(ERR, "cannot alloc counters");
2457                 goto error_tables;
2458         }
2459         /*
2460          * One default exact match table for each direction. We hold onto
2461          * it until close.
2462          */
2463         rc = enic_fet_alloc(fm, 1, NULL, 128, &fm->default_ig_fet);
2464         if (rc) {
2465                 ENICPMD_LOG(ERR, "cannot alloc default IG exact match table");
2466                 goto error_counters;
2467         }
2468         fm->default_ig_fet->ref = 1;
2469         rc = enic_fet_alloc(fm, 0, NULL, 128, &fm->default_eg_fet);
2470         if (rc) {
2471                 ENICPMD_LOG(ERR, "cannot alloc default EG exact match table");
2472                 goto error_ig_fet;
2473         }
2474         fm->default_eg_fet->ref = 1;
2475         enic->fm = fm;
2476         return 0;
2477
2478 error_ig_fet:
2479         enic_fet_free(fm, fm->default_ig_fet);
2480 error_counters:
2481         enic_fm_free_all_counters(fm);
2482 error_tables:
2483         enic_fm_free_tcam_tables(fm);
2484 error_cmd:
2485         enic_free_consistent(enic, sizeof(union enic_flowman_cmd_mem),
2486                 fm->cmd.va, fm->cmd.pa);
2487 error_fm:
2488         free(fm);
2489         return rc;
2490 }
2491
2492 void
2493 enic_fm_destroy(struct enic *enic)
2494 {
2495         struct enic_flowman *fm;
2496         struct enic_fm_fet *fet;
2497
2498         if (enic->fm == NULL)
2499                 return;
2500         ENICPMD_FUNC_TRACE();
2501         fm = enic->fm;
2502         enic_fet_free(fm, fm->default_eg_fet);
2503         enic_fet_free(fm, fm->default_ig_fet);
2504         /* Free all exact match tables still open */
2505         while (!TAILQ_EMPTY(&fm->fet_list)) {
2506                 fet = TAILQ_FIRST(&fm->fet_list);
2507                 enic_fet_free(fm, fet);
2508         }
2509         enic_fm_free_tcam_tables(fm);
2510         enic_fm_free_all_counters(fm);
2511         enic_free_consistent(enic, sizeof(union enic_flowman_cmd_mem),
2512                 fm->cmd.va, fm->cmd.pa);
2513         fm->cmd.va = NULL;
2514         free(fm);
2515         enic->fm = NULL;
2516 }
2517
2518 const struct rte_flow_ops enic_fm_flow_ops = {
2519         .validate = enic_fm_flow_validate,
2520         .create = enic_fm_flow_create,
2521         .destroy = enic_fm_flow_destroy,
2522         .flush = enic_fm_flow_flush,
2523         .query = enic_fm_flow_query,
2524 };