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