common/mlx5: fix physical port name pattern recognition
[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 /* NIC requires that 1st steer appear before decap.
874  * Correct example: steer, decap, steer, steer, ...
875  */
876 static void
877 enic_fm_reorder_action_op(struct enic_flowman *fm)
878 {
879         struct fm_action_op *op, *steer, *decap;
880         struct fm_action_op tmp_op;
881
882         ENICPMD_FUNC_TRACE();
883         /* Find 1st steer and decap */
884         op = fm->action.fma_action_ops;
885         steer = NULL;
886         decap = NULL;
887         while (op->fa_op != FMOP_END) {
888                 if (!decap && op->fa_op == FMOP_DECAP_NOSTRIP)
889                         decap = op;
890                 else if (!steer && op->fa_op == FMOP_RQ_STEER)
891                         steer = op;
892                 op++;
893         }
894         /* If decap is before steer, swap */
895         if (steer && decap && decap < steer) {
896                 op = fm->action.fma_action_ops;
897                 ENICPMD_LOG(DEBUG, "swap decap %ld <-> steer %ld",
898                             (long)(decap - op), (long)(steer - op));
899                 tmp_op = *decap;
900                 *decap = *steer;
901                 *steer = tmp_op;
902         }
903 }
904
905 /* VXLAN decap is done via flowman compound action */
906 static int
907 enic_fm_copy_vxlan_decap(struct enic_flowman *fm,
908                          struct fm_tcam_match_entry *fmt,
909                          const struct rte_flow_action *action,
910                          struct rte_flow_error *error)
911 {
912         struct fm_header_set *fm_data;
913         struct fm_action_op fm_op;
914
915         ENICPMD_FUNC_TRACE();
916         fm_data = &fmt->ftm_data.fk_hdrset[0];
917         if (!(fm_data->fk_metadata & FKM_VXLAN)) {
918                 return rte_flow_error_set(error, EINVAL,
919                         RTE_FLOW_ERROR_TYPE_ACTION, action,
920                         "vxlan-decap: vxlan must be in pattern");
921         }
922
923         memset(&fm_op, 0, sizeof(fm_op));
924         fm_op.fa_op = FMOP_DECAP_NOSTRIP;
925         return enic_fm_append_action_op(fm, &fm_op, error);
926 }
927
928 /* VXLAN encap is done via flowman compound action */
929 static int
930 enic_fm_copy_vxlan_encap(struct enic_flowman *fm,
931                          const struct rte_flow_item *item,
932                          struct rte_flow_error *error)
933 {
934         struct fm_action_op fm_op;
935         struct rte_ether_hdr *eth;
936         uint16_t *ethertype;
937         void *template;
938         uint8_t off;
939
940         ENICPMD_FUNC_TRACE();
941         memset(&fm_op, 0, sizeof(fm_op));
942         fm_op.fa_op = FMOP_ENCAP;
943         template = fm->action.fma_data;
944         off = 0;
945         /*
946          * Copy flow items to the flowman template starting L2.
947          * L2 must be ethernet.
948          */
949         flow_item_skip_void(&item);
950         if (item->type != RTE_FLOW_ITEM_TYPE_ETH)
951                 return rte_flow_error_set(error, EINVAL,
952                         RTE_FLOW_ERROR_TYPE_ITEM, item,
953                         "vxlan-encap: first item should be ethernet");
954         eth = (struct rte_ether_hdr *)template;
955         ethertype = &eth->ether_type;
956         append_template(&template, &off, item->spec,
957                         sizeof(struct rte_flow_item_eth));
958         item++;
959         flow_item_skip_void(&item);
960         /* Optional VLAN */
961         if (item->type == RTE_FLOW_ITEM_TYPE_VLAN) {
962                 const struct rte_flow_item_vlan *spec;
963
964                 ENICPMD_LOG(DEBUG, "vxlan-encap: vlan");
965                 spec = item->spec;
966                 fm_op.encap.outer_vlan = rte_be_to_cpu_16(spec->tci);
967                 item++;
968                 flow_item_skip_void(&item);
969         }
970         /* L3 must be IPv4, IPv6 */
971         switch (item->type) {
972         case RTE_FLOW_ITEM_TYPE_IPV4:
973         {
974                 struct rte_ipv4_hdr *ip4;
975
976                 ENICPMD_LOG(DEBUG, "vxlan-encap: ipv4");
977                 *ethertype = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
978                 ip4 = (struct rte_ipv4_hdr *)template;
979                 /*
980                  * Offset of IPv4 length field and its initial value
981                  * (IP + UDP + VXLAN) are specified in the action. The NIC
982                  * will add inner packet length.
983                  */
984                 fm_op.encap.len1_offset = off +
985                         offsetof(struct rte_ipv4_hdr, total_length);
986                 fm_op.encap.len1_delta = sizeof(struct rte_ipv4_hdr) +
987                         sizeof(struct rte_udp_hdr) +
988                         sizeof(struct rte_vxlan_hdr);
989                 append_template(&template, &off, item->spec,
990                                 sizeof(struct rte_ipv4_hdr));
991                 ip4->version_ihl = RTE_IPV4_VHL_DEF;
992                 if (ip4->time_to_live == 0)
993                         ip4->time_to_live = IP_DEFTTL;
994                 ip4->next_proto_id = IPPROTO_UDP;
995                 break;
996         }
997         case RTE_FLOW_ITEM_TYPE_IPV6:
998         {
999                 struct rte_ipv6_hdr *ip6;
1000
1001                 ENICPMD_LOG(DEBUG, "vxlan-encap: ipv6");
1002                 *ethertype = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
1003                 ip6 = (struct rte_ipv6_hdr *)template;
1004                 fm_op.encap.len1_offset = off +
1005                         offsetof(struct rte_ipv6_hdr, payload_len);
1006                 fm_op.encap.len1_delta = sizeof(struct rte_udp_hdr) +
1007                         sizeof(struct rte_vxlan_hdr);
1008                 append_template(&template, &off, item->spec,
1009                                 sizeof(struct rte_ipv6_hdr));
1010                 ip6->vtc_flow |= rte_cpu_to_be_32(IP6_VTC_FLOW);
1011                 if (ip6->hop_limits == 0)
1012                         ip6->hop_limits = IP_DEFTTL;
1013                 ip6->proto = IPPROTO_UDP;
1014                 break;
1015         }
1016         default:
1017                 return rte_flow_error_set(error,
1018                         EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
1019                         "vxlan-encap: L3 must be IPv4/IPv6");
1020         }
1021         item++;
1022         flow_item_skip_void(&item);
1023
1024         /* L4 is UDP */
1025         if (item->type != RTE_FLOW_ITEM_TYPE_UDP)
1026                 return rte_flow_error_set(error, EINVAL,
1027                         RTE_FLOW_ERROR_TYPE_ITEM, item,
1028                         "vxlan-encap: UDP must follow IPv4/IPv6");
1029         /* UDP length = UDP + VXLAN. NIC will add inner packet length. */
1030         fm_op.encap.len2_offset =
1031                 off + offsetof(struct rte_udp_hdr, dgram_len);
1032         fm_op.encap.len2_delta =
1033                 sizeof(struct rte_udp_hdr) + sizeof(struct rte_vxlan_hdr);
1034         append_template(&template, &off, item->spec,
1035                         sizeof(struct rte_udp_hdr));
1036         item++;
1037         flow_item_skip_void(&item);
1038
1039         /* Finally VXLAN */
1040         if (item->type != RTE_FLOW_ITEM_TYPE_VXLAN)
1041                 return rte_flow_error_set(error,
1042                         EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
1043                         "vxlan-encap: VXLAN must follow UDP");
1044         append_template(&template, &off, item->spec,
1045                         sizeof(struct rte_flow_item_vxlan));
1046
1047         /*
1048          * Fill in the rest of the action structure.
1049          * Indicate that we want to encap with vxlan at packet start.
1050          */
1051         fm_op.encap.template_offset = 0;
1052         fm_op.encap.template_len = off;
1053         return enic_fm_append_action_op(fm, &fm_op, error);
1054 }
1055
1056 static int
1057 enic_fm_find_vnic(struct enic *enic, const struct rte_pci_addr *addr,
1058                   uint64_t *handle)
1059 {
1060         uint32_t bdf;
1061         uint64_t args[2];
1062         int rc;
1063
1064         ENICPMD_FUNC_TRACE();
1065         ENICPMD_LOG(DEBUG, "bdf=%x:%x:%x", addr->bus, addr->devid,
1066                     addr->function);
1067         bdf = addr->bus << 8 | addr->devid << 3 | addr->function;
1068         args[0] = FM_VNIC_FIND;
1069         args[1] = bdf;
1070         rc = vnic_dev_flowman_cmd(enic->vdev, args, 2);
1071         if (rc != 0) {
1072                 ENICPMD_LOG(ERR, "allocating counters rc=%d", rc);
1073                 return rc;
1074         }
1075         *handle = args[0];
1076         ENICPMD_LOG(DEBUG, "found vnic: handle=0x%" PRIx64, *handle);
1077         return 0;
1078 }
1079
1080 /* Translate flow actions to flowman TCAM entry actions */
1081 static int
1082 enic_fm_copy_action(struct enic_flowman *fm,
1083                     const struct rte_flow_action actions[],
1084                     uint8_t ingress,
1085                     struct rte_flow_error *error)
1086 {
1087         enum {
1088                 FATE = 1 << 0,
1089                 DECAP = 1 << 1,
1090                 PASSTHRU = 1 << 2,
1091                 COUNT = 1 << 3,
1092                 ENCAP = 1 << 4,
1093         };
1094         struct fm_tcam_match_entry *fmt;
1095         struct fm_action_op fm_op;
1096         struct enic *enic;
1097         uint32_t overlap;
1098         uint64_t vnic_h;
1099         bool first_rq;
1100         int ret;
1101
1102         ENICPMD_FUNC_TRACE();
1103         fmt = &fm->tcam_entry;
1104         first_rq = true;
1105         enic = fm->enic;
1106         overlap = 0;
1107         vnic_h = 0; /* 0 = current vNIC */
1108         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1109                 switch (actions->type) {
1110                 case RTE_FLOW_ACTION_TYPE_VOID:
1111                         continue;
1112                 case RTE_FLOW_ACTION_TYPE_PASSTHRU: {
1113                         if (overlap & PASSTHRU)
1114                                 goto unsupported;
1115                         overlap |= PASSTHRU;
1116                         break;
1117                 }
1118                 case RTE_FLOW_ACTION_TYPE_JUMP: {
1119                         const struct rte_flow_action_jump *jump =
1120                                 actions->conf;
1121                         struct enic_fm_fet *fet;
1122
1123                         if (overlap & FATE)
1124                                 goto unsupported;
1125                         ret = enic_fet_get(fm, jump->group, ingress, NULL,
1126                                            &fet, error);
1127                         if (ret)
1128                                 return ret;
1129                         overlap |= FATE;
1130                         memset(&fm_op, 0, sizeof(fm_op));
1131                         fm_op.fa_op = FMOP_EXACT_MATCH;
1132                         fm_op.exact.handle = fet->handle;
1133                         fm->fet = fet;
1134                         ret = enic_fm_append_action_op(fm, &fm_op, error);
1135                         if (ret)
1136                                 return ret;
1137                         break;
1138                 }
1139                 case RTE_FLOW_ACTION_TYPE_MARK: {
1140                         const struct rte_flow_action_mark *mark =
1141                                 actions->conf;
1142
1143                         if (mark->id >= ENIC_MAGIC_FILTER_ID - 1)
1144                                 return rte_flow_error_set(error, EINVAL,
1145                                         RTE_FLOW_ERROR_TYPE_ACTION,
1146                                         NULL, "invalid mark id");
1147                         memset(&fm_op, 0, sizeof(fm_op));
1148                         fm_op.fa_op = FMOP_MARK;
1149                         fm_op.mark.mark = mark->id + 1;
1150                         ret = enic_fm_append_action_op(fm, &fm_op, error);
1151                         if (ret)
1152                                 return ret;
1153                         break;
1154                 }
1155                 case RTE_FLOW_ACTION_TYPE_FLAG: {
1156                         /* ENIC_MAGIC_FILTER_ID is reserved for flagging */
1157                         memset(&fm_op, 0, sizeof(fm_op));
1158                         fm_op.fa_op = FMOP_MARK;
1159                         fm_op.mark.mark = ENIC_MAGIC_FILTER_ID;
1160                         ret = enic_fm_append_action_op(fm, &fm_op, error);
1161                         if (ret)
1162                                 return ret;
1163                         break;
1164                 }
1165                 case RTE_FLOW_ACTION_TYPE_QUEUE: {
1166                         const struct rte_flow_action_queue *queue =
1167                                 actions->conf;
1168
1169                         /*
1170                          * If fate other than QUEUE or RSS, fail. Multiple
1171                          * rss and queue actions are ok.
1172                          */
1173                         if ((overlap & FATE) && first_rq)
1174                                 goto unsupported;
1175                         first_rq = false;
1176                         overlap |= FATE;
1177                         memset(&fm_op, 0, sizeof(fm_op));
1178                         fm_op.fa_op = FMOP_RQ_STEER;
1179                         fm_op.rq_steer.rq_index =
1180                                 enic_rte_rq_idx_to_sop_idx(queue->index);
1181                         fm_op.rq_steer.rq_count = 1;
1182                         fm_op.rq_steer.vnic_handle = vnic_h;
1183                         ret = enic_fm_append_action_op(fm, &fm_op, error);
1184                         if (ret)
1185                                 return ret;
1186                         ENICPMD_LOG(DEBUG, "create QUEUE action rq: %u",
1187                                     fm_op.rq_steer.rq_index);
1188                         break;
1189                 }
1190                 case RTE_FLOW_ACTION_TYPE_DROP: {
1191                         if (overlap & FATE)
1192                                 goto unsupported;
1193                         overlap |= FATE;
1194                         memset(&fm_op, 0, sizeof(fm_op));
1195                         fm_op.fa_op = FMOP_DROP;
1196                         ret = enic_fm_append_action_op(fm, &fm_op, error);
1197                         if (ret)
1198                                 return ret;
1199                         ENICPMD_LOG(DEBUG, "create DROP action");
1200                         break;
1201                 }
1202                 case RTE_FLOW_ACTION_TYPE_COUNT: {
1203                         if (overlap & COUNT)
1204                                 goto unsupported;
1205                         overlap |= COUNT;
1206                         /* Count is associated with entry not action on VIC. */
1207                         fmt->ftm_flags |= FMEF_COUNTER;
1208                         break;
1209                 }
1210                 case RTE_FLOW_ACTION_TYPE_RSS: {
1211                         const struct rte_flow_action_rss *rss = actions->conf;
1212                         bool allow;
1213                         uint16_t i;
1214
1215                         /*
1216                          * If fate other than QUEUE or RSS, fail. Multiple
1217                          * rss and queue actions are ok.
1218                          */
1219                         if ((overlap & FATE) && first_rq)
1220                                 goto unsupported;
1221                         first_rq = false;
1222                         overlap |= FATE;
1223
1224                         /*
1225                          * Hardware only supports RSS actions on outer level
1226                          * with default type and function. Queues must be
1227                          * sequential.
1228                          */
1229                         allow = rss->func == RTE_ETH_HASH_FUNCTION_DEFAULT &&
1230                                 rss->level == 0 && (rss->types == 0 ||
1231                                 rss->types == enic->rss_hf) &&
1232                                 rss->queue_num <= enic->rq_count &&
1233                                 rss->queue[rss->queue_num - 1] < enic->rq_count;
1234
1235
1236                         /* Identity queue map needs to be sequential */
1237                         for (i = 1; i < rss->queue_num; i++)
1238                                 allow = allow && (rss->queue[i] ==
1239                                         rss->queue[i - 1] + 1);
1240                         if (!allow)
1241                                 goto unsupported;
1242
1243                         memset(&fm_op, 0, sizeof(fm_op));
1244                         fm_op.fa_op = FMOP_RQ_STEER;
1245                         fm_op.rq_steer.rq_index =
1246                                 enic_rte_rq_idx_to_sop_idx(rss->queue[0]);
1247                         fm_op.rq_steer.rq_count = rss->queue_num;
1248                         fm_op.rq_steer.vnic_handle = vnic_h;
1249                         ret = enic_fm_append_action_op(fm, &fm_op, error);
1250                         if (ret)
1251                                 return ret;
1252                         ENICPMD_LOG(DEBUG, "create QUEUE action rq: %u",
1253                                     fm_op.rq_steer.rq_index);
1254                         break;
1255                 }
1256                 case RTE_FLOW_ACTION_TYPE_PORT_ID: {
1257                         const struct rte_flow_action_port_id *port;
1258                         struct rte_pci_device *pdev;
1259                         struct rte_eth_dev *dev;
1260
1261                         port = actions->conf;
1262                         if (port->original) {
1263                                 vnic_h = 0; /* This port */
1264                                 break;
1265                         }
1266                         ENICPMD_LOG(DEBUG, "port id %u", port->id);
1267                         if (!rte_eth_dev_is_valid_port(port->id)) {
1268                                 return rte_flow_error_set(error, EINVAL,
1269                                         RTE_FLOW_ERROR_TYPE_ACTION,
1270                                         NULL, "invalid port_id");
1271                         }
1272                         dev = &rte_eth_devices[port->id];
1273                         if (!dev_is_enic(dev)) {
1274                                 return rte_flow_error_set(error, EINVAL,
1275                                         RTE_FLOW_ERROR_TYPE_ACTION,
1276                                         NULL, "port_id is not enic");
1277                         }
1278                         pdev = RTE_ETH_DEV_TO_PCI(dev);
1279                         if (enic_fm_find_vnic(enic, &pdev->addr, &vnic_h)) {
1280                                 return rte_flow_error_set(error, EINVAL,
1281                                         RTE_FLOW_ERROR_TYPE_ACTION,
1282                                         NULL, "port_id is not vnic");
1283                         }
1284                         break;
1285                 }
1286                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP: {
1287                         if (overlap & DECAP)
1288                                 goto unsupported;
1289                         overlap |= DECAP;
1290
1291                         ret = enic_fm_copy_vxlan_decap(fm, fmt, actions,
1292                                 error);
1293                         if (ret != 0)
1294                                 return ret;
1295                         break;
1296                 }
1297                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP: {
1298                         const struct rte_flow_action_vxlan_encap *encap;
1299
1300                         encap = actions->conf;
1301                         if (overlap & ENCAP)
1302                                 goto unsupported;
1303                         overlap |= ENCAP;
1304                         ret = enic_fm_copy_vxlan_encap(fm, encap->definition,
1305                                 error);
1306                         if (ret != 0)
1307                                 return ret;
1308                         break;
1309                 }
1310                 default:
1311                         goto unsupported;
1312                 }
1313         }
1314
1315         if (!(overlap & (FATE | PASSTHRU | COUNT)))
1316                 goto unsupported;
1317         memset(&fm_op, 0, sizeof(fm_op));
1318         fm_op.fa_op = FMOP_END;
1319         ret = enic_fm_append_action_op(fm, &fm_op, error);
1320         if (ret)
1321                 return ret;
1322         enic_fm_reorder_action_op(fm);
1323         return 0;
1324
1325 unsupported:
1326         return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
1327                                   NULL, "enic: unsupported action");
1328 }
1329
1330 /** Check if the action is supported */
1331 static int
1332 enic_fm_match_action(const struct rte_flow_action *action,
1333                      const enum rte_flow_action_type *supported_actions)
1334 {
1335         for (; *supported_actions != RTE_FLOW_ACTION_TYPE_END;
1336              supported_actions++) {
1337                 if (action->type == *supported_actions)
1338                         return 1;
1339         }
1340         return 0;
1341 }
1342
1343 /* Debug function to dump internal NIC action structure. */
1344 static void
1345 enic_fm_dump_tcam_actions(const struct fm_action *fm_action)
1346 {
1347         /* Manually keep in sync with FMOP commands */
1348         const char *fmop_str[FMOP_OP_MAX] = {
1349                 [FMOP_END] = "end",
1350                 [FMOP_DROP] = "drop",
1351                 [FMOP_RQ_STEER] = "steer",
1352                 [FMOP_EXACT_MATCH] = "exmatch",
1353                 [FMOP_MARK] = "mark",
1354                 [FMOP_EXT_MARK] = "ext_mark",
1355                 [FMOP_TAG] = "tag",
1356                 [FMOP_EG_HAIRPIN] = "eg_hairpin",
1357                 [FMOP_IG_HAIRPIN] = "ig_hairpin",
1358                 [FMOP_ENCAP_IVLAN] = "encap_ivlan",
1359                 [FMOP_ENCAP_NOIVLAN] = "encap_noivlan",
1360                 [FMOP_ENCAP] = "encap",
1361                 [FMOP_SET_OVLAN] = "set_ovlan",
1362                 [FMOP_DECAP_NOSTRIP] = "decap_nostrip",
1363         };
1364         const struct fm_action_op *op = &fm_action->fma_action_ops[0];
1365         char buf[128], *bp = buf;
1366         const char *op_str;
1367         int i, n, buf_len;
1368
1369         buf[0] = '\0';
1370         buf_len = sizeof(buf);
1371         for (i = 0; i < FM_ACTION_OP_MAX; i++) {
1372                 if (op->fa_op == FMOP_END)
1373                         break;
1374                 if (op->fa_op >= FMOP_OP_MAX)
1375                         op_str = "unknown";
1376                 else
1377                         op_str = fmop_str[op->fa_op];
1378                 n = snprintf(bp, buf_len, "%s,", op_str);
1379                 if (n > 0 && n < buf_len) {
1380                         bp += n;
1381                         buf_len -= n;
1382                 }
1383                 op++;
1384         }
1385         /* Remove trailing comma */
1386         if (buf[0])
1387                 *(bp - 1) = '\0';
1388         ENICPMD_LOG(DEBUG, "       Acions: %s", buf);
1389 }
1390
1391 static int
1392 bits_to_str(uint32_t bits, const char *strings[], int max,
1393             char *buf, int buf_len)
1394 {
1395         int i, n = 0, len = 0;
1396
1397         for (i = 0; i < max; i++) {
1398                 if (bits & (1 << i)) {
1399                         n = snprintf(buf, buf_len, "%s,", strings[i]);
1400                         if (n > 0 && n < buf_len) {
1401                                 buf += n;
1402                                 buf_len -= n;
1403                                 len += n;
1404                         }
1405                 }
1406         }
1407         /* Remove trailing comma */
1408         if (len) {
1409                 *(buf - 1) = '\0';
1410                 len--;
1411         }
1412         return len;
1413 }
1414
1415 /* Debug function to dump internal NIC filter structure. */
1416 static void
1417 __enic_fm_dump_tcam_match(const struct fm_header_set *fk_hdrset, char *buf,
1418                           int buf_len)
1419 {
1420         /* Manually keep in sync with FKM_BITS */
1421         const char *fm_fkm_str[FKM_BIT_COUNT] = {
1422                 [FKM_QTAG_BIT] = "qtag",
1423                 [FKM_CMD_BIT] = "cmd",
1424                 [FKM_IPV4_BIT] = "ip4",
1425                 [FKM_IPV6_BIT] = "ip6",
1426                 [FKM_ROCE_BIT] = "roce",
1427                 [FKM_UDP_BIT] = "udp",
1428                 [FKM_TCP_BIT] = "tcp",
1429                 [FKM_TCPORUDP_BIT] = "tcpportudp",
1430                 [FKM_IPFRAG_BIT] = "ipfrag",
1431                 [FKM_NVGRE_BIT] = "nvgre",
1432                 [FKM_VXLAN_BIT] = "vxlan",
1433                 [FKM_GENEVE_BIT] = "geneve",
1434                 [FKM_NSH_BIT] = "nsh",
1435                 [FKM_ROCEV2_BIT] = "rocev2",
1436                 [FKM_VLAN_PRES_BIT] = "vlan_pres",
1437                 [FKM_IPOK_BIT] = "ipok",
1438                 [FKM_L4OK_BIT] = "l4ok",
1439                 [FKM_ROCEOK_BIT] = "roceok",
1440                 [FKM_FCSOK_BIT] = "fcsok",
1441                 [FKM_EG_SPAN_BIT] = "eg_span",
1442                 [FKM_IG_SPAN_BIT] = "ig_span",
1443                 [FKM_EG_HAIRPINNED_BIT] = "eg_hairpinned",
1444         };
1445         /* Manually keep in sync with FKH_BITS */
1446         const char *fm_fkh_str[FKH_BIT_COUNT] = {
1447                 [FKH_ETHER_BIT] = "eth",
1448                 [FKH_QTAG_BIT] = "qtag",
1449                 [FKH_L2RAW_BIT] = "l2raw",
1450                 [FKH_IPV4_BIT] = "ip4",
1451                 [FKH_IPV6_BIT] = "ip6",
1452                 [FKH_L3RAW_BIT] = "l3raw",
1453                 [FKH_UDP_BIT] = "udp",
1454                 [FKH_TCP_BIT] = "tcp",
1455                 [FKH_ICMP_BIT] = "icmp",
1456                 [FKH_VXLAN_BIT] = "vxlan",
1457                 [FKH_L4RAW_BIT] = "l4raw",
1458         };
1459         uint32_t fkh_bits = fk_hdrset->fk_header_select;
1460         uint32_t fkm_bits = fk_hdrset->fk_metadata;
1461         int n;
1462
1463         if (!fkm_bits && !fkh_bits)
1464                 return;
1465         n = snprintf(buf, buf_len, "metadata(");
1466         if (n > 0 && n < buf_len) {
1467                 buf += n;
1468                 buf_len -= n;
1469         }
1470         n = bits_to_str(fkm_bits, fm_fkm_str, FKM_BIT_COUNT, buf, buf_len);
1471         if (n > 0 && n < buf_len) {
1472                 buf += n;
1473                 buf_len -= n;
1474         }
1475         n = snprintf(buf, buf_len, ") valid hdr fields(");
1476         if (n > 0 && n < buf_len) {
1477                 buf += n;
1478                 buf_len -= n;
1479         }
1480         n = bits_to_str(fkh_bits, fm_fkh_str, FKH_BIT_COUNT, buf, buf_len);
1481         if (n > 0 && n < buf_len) {
1482                 buf += n;
1483                 buf_len -= n;
1484         }
1485         snprintf(buf, buf_len, ")");
1486 }
1487
1488 static void
1489 enic_fm_dump_tcam_match(const struct fm_tcam_match_entry *match,
1490                         uint8_t ingress)
1491 {
1492         char buf[256];
1493
1494         memset(buf, 0, sizeof(buf));
1495         __enic_fm_dump_tcam_match(&match->ftm_mask.fk_hdrset[0],
1496                                   buf, sizeof(buf));
1497         ENICPMD_LOG(DEBUG, " TCAM %s Outer: %s %scounter",
1498                     (ingress) ? "IG" : "EG", buf,
1499                     (match->ftm_flags & FMEF_COUNTER) ? "" : "no ");
1500         memset(buf, 0, sizeof(buf));
1501         __enic_fm_dump_tcam_match(&match->ftm_mask.fk_hdrset[1],
1502                                   buf, sizeof(buf));
1503         if (buf[0])
1504                 ENICPMD_LOG(DEBUG, "         Inner: %s", buf);
1505 }
1506
1507 /* Debug function to dump internal NIC flow structures. */
1508 static void
1509 enic_fm_dump_tcam_entry(const struct fm_tcam_match_entry *fm_match,
1510                         const struct fm_action *fm_action,
1511                         uint8_t ingress)
1512 {
1513         if (!rte_log_can_log(enic_pmd_logtype, RTE_LOG_DEBUG))
1514                 return;
1515         enic_fm_dump_tcam_match(fm_match, ingress);
1516         enic_fm_dump_tcam_actions(fm_action);
1517 }
1518
1519 static int
1520 enic_fm_flow_parse(struct enic_flowman *fm,
1521                    const struct rte_flow_attr *attrs,
1522                    const struct rte_flow_item pattern[],
1523                    const struct rte_flow_action actions[],
1524                    struct rte_flow_error *error)
1525 {
1526         const struct rte_flow_action *action;
1527         unsigned int ret;
1528         static const enum rte_flow_action_type *sa;
1529
1530         ENICPMD_FUNC_TRACE();
1531         ret = 0;
1532         if (!pattern) {
1533                 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1534                                    NULL, "no pattern specified");
1535                 return -rte_errno;
1536         }
1537
1538         if (!actions) {
1539                 rte_flow_error_set(error, EINVAL,
1540                                    RTE_FLOW_ERROR_TYPE_ACTION_NUM,
1541                                    NULL, "no action specified");
1542                 return -rte_errno;
1543         }
1544
1545         if (attrs) {
1546                 if (attrs->priority) {
1547                         rte_flow_error_set(error, ENOTSUP,
1548                                            RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1549                                            NULL,
1550                                            "priorities are not supported");
1551                         return -rte_errno;
1552                 } else if (attrs->transfer) {
1553                         rte_flow_error_set(error, ENOTSUP,
1554                                            RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
1555                                            NULL,
1556                                            "transfer is not supported");
1557                         return -rte_errno;
1558                 } else if (attrs->ingress && attrs->egress) {
1559                         rte_flow_error_set(error, ENOTSUP,
1560                                            RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1561                                            NULL,
1562                                            "bidirectional rules not supported");
1563                         return -rte_errno;
1564                 }
1565
1566         } else {
1567                 rte_flow_error_set(error, EINVAL,
1568                                    RTE_FLOW_ERROR_TYPE_ATTR,
1569                                    NULL, "no attribute specified");
1570                 return -rte_errno;
1571         }
1572
1573         /* Verify Actions. */
1574         sa = (attrs->ingress) ? enic_fm_supported_ig_actions :
1575              enic_fm_supported_eg_actions;
1576         for (action = &actions[0]; action->type != RTE_FLOW_ACTION_TYPE_END;
1577              action++) {
1578                 if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
1579                         continue;
1580                 else if (!enic_fm_match_action(action, sa))
1581                         break;
1582         }
1583         if (action->type != RTE_FLOW_ACTION_TYPE_END) {
1584                 rte_flow_error_set(error, EPERM, RTE_FLOW_ERROR_TYPE_ACTION,
1585                                    action, "invalid action");
1586                 return -rte_errno;
1587         }
1588         ret = enic_fm_copy_entry(fm, pattern, error);
1589         if (ret)
1590                 return ret;
1591         ret = enic_fm_copy_action(fm, actions, attrs->ingress, error);
1592         return ret;
1593 }
1594
1595 static void
1596 enic_fm_counter_free(struct enic_flowman *fm, struct enic_fm_flow *fm_flow)
1597 {
1598         if (!fm_flow->counter_valid)
1599                 return;
1600         SLIST_INSERT_HEAD(&fm->counters, fm_flow->counter, next);
1601         fm_flow->counter_valid = false;
1602 }
1603
1604 static int
1605 enic_fm_more_counters(struct enic_flowman *fm)
1606 {
1607         struct enic_fm_counter *new_stack;
1608         struct enic_fm_counter *ctrs;
1609         struct enic *enic;
1610         int i, rc;
1611         uint64_t args[2];
1612
1613         ENICPMD_FUNC_TRACE();
1614         enic = fm->enic;
1615         new_stack = rte_realloc(fm->counter_stack, (fm->counters_alloced +
1616                                 FM_COUNTERS_EXPAND) *
1617                                 sizeof(struct enic_fm_counter), 0);
1618         if (new_stack == NULL) {
1619                 ENICPMD_LOG(ERR, "cannot alloc counter memory");
1620                 return -ENOMEM;
1621         }
1622         fm->counter_stack = new_stack;
1623
1624         args[0] = FM_COUNTER_BRK;
1625         args[1] = fm->counters_alloced + FM_COUNTERS_EXPAND;
1626         rc = vnic_dev_flowman_cmd(enic->vdev, args, 2);
1627         if (rc != 0) {
1628                 ENICPMD_LOG(ERR, "cannot alloc counters rc=%d", rc);
1629                 return rc;
1630         }
1631         ctrs = (struct enic_fm_counter *)fm->counter_stack +
1632                 fm->counters_alloced;
1633         for (i = 0; i < FM_COUNTERS_EXPAND; i++, ctrs++) {
1634                 ctrs->handle = fm->counters_alloced + i;
1635                 SLIST_INSERT_HEAD(&fm->counters, ctrs, next);
1636         }
1637         fm->counters_alloced += FM_COUNTERS_EXPAND;
1638         ENICPMD_LOG(DEBUG, "%u counters allocated, total: %u",
1639                     FM_COUNTERS_EXPAND, fm->counters_alloced);
1640         return 0;
1641 }
1642
1643 static int
1644 enic_fm_counter_zero(struct enic_flowman *fm, struct enic_fm_counter *c)
1645 {
1646         struct enic *enic;
1647         uint64_t args[3];
1648         int ret;
1649
1650         ENICPMD_FUNC_TRACE();
1651         enic = fm->enic;
1652         args[0] = FM_COUNTER_QUERY;
1653         args[1] = c->handle;
1654         args[2] = 1; /* clear */
1655         ret = vnic_dev_flowman_cmd(enic->vdev, args, 3);
1656         if (ret) {
1657                 ENICPMD_LOG(ERR, "counter init: rc=%d handle=0x%x",
1658                             ret, c->handle);
1659                 return ret;
1660         }
1661         return 0;
1662 }
1663
1664 static int
1665 enic_fm_counter_alloc(struct enic_flowman *fm, struct rte_flow_error *error,
1666                       struct enic_fm_counter **ctr)
1667 {
1668         struct enic_fm_counter *c;
1669         int ret;
1670
1671         ENICPMD_FUNC_TRACE();
1672         *ctr = NULL;
1673         if (SLIST_EMPTY(&fm->counters)) {
1674                 ret = enic_fm_more_counters(fm);
1675                 if (ret)
1676                         return rte_flow_error_set(error, -ret,
1677                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1678                                 NULL, "enic: out of counters");
1679         }
1680         c = SLIST_FIRST(&fm->counters);
1681         SLIST_REMOVE_HEAD(&fm->counters, next);
1682         *ctr = c;
1683         return 0;
1684 }
1685
1686 static int
1687 enic_fm_action_free(struct enic_flowman *fm, uint64_t handle)
1688 {
1689         uint64_t args[2];
1690         int rc;
1691
1692         ENICPMD_FUNC_TRACE();
1693         args[0] = FM_ACTION_FREE;
1694         args[1] = handle;
1695         rc = vnic_dev_flowman_cmd(fm->enic->vdev, args, 2);
1696         if (rc)
1697                 ENICPMD_LOG(ERR, "cannot free action: rc=%d handle=0x%" PRIx64,
1698                             rc, handle);
1699         return rc;
1700 }
1701
1702 static int
1703 enic_fm_entry_free(struct enic_flowman *fm, uint64_t handle)
1704 {
1705         uint64_t args[2];
1706         int rc;
1707
1708         ENICPMD_FUNC_TRACE();
1709         args[0] = FM_MATCH_ENTRY_REMOVE;
1710         args[1] = handle;
1711         rc = vnic_dev_flowman_cmd(fm->enic->vdev, args, 2);
1712         if (rc)
1713                 ENICPMD_LOG(ERR, "cannot free match entry: rc=%d"
1714                             " handle=0x%" PRIx64, rc, handle);
1715         return rc;
1716 }
1717
1718 static struct enic_fm_jump_flow *
1719 find_jump_flow(struct enic_flowman *fm, uint32_t group)
1720 {
1721         struct enic_fm_jump_flow *j;
1722
1723         ENICPMD_FUNC_TRACE();
1724         TAILQ_FOREACH(j, &fm->jump_list, list) {
1725                 if (j->group == group)
1726                         return j;
1727         }
1728         return NULL;
1729 }
1730
1731 static void
1732 remove_jump_flow(struct enic_flowman *fm, struct rte_flow *flow)
1733 {
1734         struct enic_fm_jump_flow *j;
1735
1736         ENICPMD_FUNC_TRACE();
1737         TAILQ_FOREACH(j, &fm->jump_list, list) {
1738                 if (j->flow == flow) {
1739                         TAILQ_REMOVE(&fm->jump_list, j, list);
1740                         free(j);
1741                         return;
1742                 }
1743         }
1744 }
1745
1746 static int
1747 save_jump_flow(struct enic_flowman *fm,
1748                struct rte_flow *flow,
1749                uint32_t group,
1750                struct fm_tcam_match_entry *match,
1751                struct fm_action *action)
1752 {
1753         struct enic_fm_jump_flow *j;
1754
1755         ENICPMD_FUNC_TRACE();
1756         j = calloc(1, sizeof(struct enic_fm_jump_flow));
1757         if (j == NULL)
1758                 return -ENOMEM;
1759         j->flow = flow;
1760         j->group = group;
1761         j->match = *match;
1762         j->action = *action;
1763         TAILQ_INSERT_HEAD(&fm->jump_list, j, list);
1764         ENICPMD_LOG(DEBUG, "saved jump flow: flow=%p group=%u", flow, group);
1765         return 0;
1766 }
1767
1768 static void
1769 __enic_fm_flow_free(struct enic_flowman *fm, struct enic_fm_flow *fm_flow)
1770 {
1771         if (fm_flow->entry_handle != FM_INVALID_HANDLE) {
1772                 enic_fm_entry_free(fm, fm_flow->entry_handle);
1773                 fm_flow->entry_handle = FM_INVALID_HANDLE;
1774         }
1775         if (fm_flow->action_handle != FM_INVALID_HANDLE) {
1776                 enic_fm_action_free(fm, fm_flow->action_handle);
1777                 fm_flow->action_handle = FM_INVALID_HANDLE;
1778         }
1779         enic_fm_counter_free(fm, fm_flow);
1780         if (fm_flow->fet) {
1781                 enic_fet_put(fm, fm_flow->fet);
1782                 fm_flow->fet = NULL;
1783         }
1784 }
1785
1786 static void
1787 enic_fm_flow_free(struct enic_flowman *fm, struct rte_flow *flow)
1788 {
1789         if (flow->fm->fet && flow->fm->fet->default_key)
1790                 remove_jump_flow(fm, flow);
1791         __enic_fm_flow_free(fm, flow->fm);
1792         free(flow->fm);
1793         free(flow);
1794 }
1795
1796 static int
1797 enic_fm_add_tcam_entry(struct enic_flowman *fm,
1798                        struct fm_tcam_match_entry *match_in,
1799                        uint64_t *entry_handle,
1800                        uint8_t ingress,
1801                        struct rte_flow_error *error)
1802 {
1803         struct fm_tcam_match_entry *ftm;
1804         uint64_t args[3];
1805         int ret;
1806
1807         ENICPMD_FUNC_TRACE();
1808         /* Copy entry to the command buffer */
1809         ftm = &fm->cmd.va->fm_tcam_match_entry;
1810         memcpy(ftm, match_in, sizeof(*ftm));
1811         /* Add TCAM entry */
1812         args[0] = FM_TCAM_ENTRY_INSTALL;
1813         args[1] = ingress ? fm->ig_tcam_hndl : fm->eg_tcam_hndl;
1814         args[2] = fm->cmd.pa;
1815         ret = vnic_dev_flowman_cmd(fm->enic->vdev, args, 3);
1816         if (ret != 0) {
1817                 ENICPMD_LOG(ERR, "cannot add %s TCAM entry: rc=%d",
1818                             ingress ? "ingress" : "egress", ret);
1819                 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1820                         NULL, "enic: devcmd(tcam-entry-install)");
1821                 return ret;
1822         }
1823         ENICPMD_LOG(DEBUG, "installed %s TCAM entry: handle=0x%" PRIx64,
1824                     ingress ? "ingress" : "egress", (uint64_t)args[0]);
1825         *entry_handle = args[0];
1826         return 0;
1827 }
1828
1829 static int
1830 enic_fm_add_exact_entry(struct enic_flowman *fm,
1831                         struct fm_tcam_match_entry *match_in,
1832                         uint64_t *entry_handle,
1833                         struct enic_fm_fet *fet,
1834                         struct rte_flow_error *error)
1835 {
1836         struct fm_exact_match_entry *fem;
1837         uint64_t args[3];
1838         int ret;
1839
1840         ENICPMD_FUNC_TRACE();
1841         /* The new entry must have the table's key */
1842         if (memcmp(fet->key.fk_hdrset, match_in->ftm_mask.fk_hdrset,
1843                    sizeof(struct fm_header_set) * FM_HDRSET_MAX)) {
1844                 return rte_flow_error_set(error, EINVAL,
1845                         RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1846                         "enic: key does not match group's key");
1847         }
1848
1849         /* Copy entry to the command buffer */
1850         fem = &fm->cmd.va->fm_exact_match_entry;
1851         /*
1852          * Translate TCAM entry to exact entry. As is only need to drop
1853          * position and mask. The mask is part of the exact match table.
1854          * Position (aka priority) is not supported in the exact match table.
1855          */
1856         fem->fem_data = match_in->ftm_data;
1857         fem->fem_flags = match_in->ftm_flags;
1858         fem->fem_action = match_in->ftm_action;
1859         fem->fem_counter = match_in->ftm_counter;
1860
1861         /* Add exact entry */
1862         args[0] = FM_EXACT_ENTRY_INSTALL;
1863         args[1] = fet->handle;
1864         args[2] = fm->cmd.pa;
1865         ret = vnic_dev_flowman_cmd(fm->enic->vdev, args, 3);
1866         if (ret != 0) {
1867                 ENICPMD_LOG(ERR, "cannot add %s exact entry: group=%u",
1868                             fet->ingress ? "ingress" : "egress", fet->group);
1869                 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1870                         NULL, "enic: devcmd(exact-entry-install)");
1871                 return ret;
1872         }
1873         ENICPMD_LOG(DEBUG, "installed %s exact entry: group=%u"
1874                     " handle=0x%" PRIx64,
1875                     fet->ingress ? "ingress" : "egress", fet->group,
1876                     (uint64_t)args[0]);
1877         *entry_handle = args[0];
1878         return 0;
1879 }
1880
1881 /* Push match-action to the NIC. */
1882 static int
1883 __enic_fm_flow_add_entry(struct enic_flowman *fm,
1884                          struct enic_fm_flow *fm_flow,
1885                          struct fm_tcam_match_entry *match_in,
1886                          struct fm_action *action_in,
1887                          uint32_t group,
1888                          uint8_t ingress,
1889                          struct rte_flow_error *error)
1890 {
1891         struct enic_fm_counter *ctr;
1892         struct fm_action *fma;
1893         uint64_t action_h;
1894         uint64_t entry_h;
1895         uint64_t args[3];
1896         int ret;
1897
1898         ENICPMD_FUNC_TRACE();
1899         /* Allocate action. */
1900         fma = &fm->cmd.va->fm_action;
1901         memcpy(fma, action_in, sizeof(*fma));
1902         args[0] = FM_ACTION_ALLOC;
1903         args[1] = fm->cmd.pa;
1904         ret = vnic_dev_flowman_cmd(fm->enic->vdev, args, 2);
1905         if (ret != 0) {
1906                 ENICPMD_LOG(ERR, "allocating TCAM table action rc=%d", ret);
1907                 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1908                         NULL, "enic: devcmd(action-alloc)");
1909                 return ret;
1910         }
1911         action_h = args[0];
1912         fm_flow->action_handle = action_h;
1913         match_in->ftm_action = action_h;
1914         ENICPMD_LOG(DEBUG, "action allocated: handle=0x%" PRIx64, action_h);
1915
1916         /* Allocate counter if requested. */
1917         if (match_in->ftm_flags & FMEF_COUNTER) {
1918                 ret = enic_fm_counter_alloc(fm, error, &ctr);
1919                 if (ret) /* error has been filled in */
1920                         return ret;
1921                 fm_flow->counter_valid = true;
1922                 fm_flow->counter = ctr;
1923                 match_in->ftm_counter = ctr->handle;
1924         }
1925
1926         /*
1927          * Get the group's table (either TCAM or exact match table) and
1928          * add entry to it. If we use the exact match table, the handler
1929          * will translate the TCAM entry (match_in) to the appropriate
1930          * exact match entry and use that instead.
1931          */
1932         entry_h = FM_INVALID_HANDLE;
1933         if (group == FM_TCAM_RTE_GROUP) {
1934                 ret = enic_fm_add_tcam_entry(fm, match_in, &entry_h, ingress,
1935                                              error);
1936                 if (ret)
1937                         return ret;
1938                 /* Jump action might have a ref to fet */
1939                 fm_flow->fet = fm->fet;
1940                 fm->fet = NULL;
1941         } else {
1942                 struct enic_fm_fet *fet = NULL;
1943
1944                 ret = enic_fet_get(fm, group, ingress,
1945                                    &match_in->ftm_mask, &fet, error);
1946                 if (ret)
1947                         return ret;
1948                 fm_flow->fet = fet;
1949                 ret = enic_fm_add_exact_entry(fm, match_in, &entry_h, fet,
1950                                               error);
1951                 if (ret)
1952                         return ret;
1953         }
1954         /* Clear counter after adding entry, as it requires in-use counter */
1955         if (fm_flow->counter_valid) {
1956                 ret = enic_fm_counter_zero(fm, fm_flow->counter);
1957                 if (ret)
1958                         return ret;
1959         }
1960         fm_flow->entry_handle = entry_h;
1961         return 0;
1962 }
1963
1964 /* Push match-action to the NIC. */
1965 static struct rte_flow *
1966 enic_fm_flow_add_entry(struct enic_flowman *fm,
1967                        struct fm_tcam_match_entry *match_in,
1968                        struct fm_action *action_in,
1969                        const struct rte_flow_attr *attrs,
1970                        struct rte_flow_error *error)
1971 {
1972         struct enic_fm_flow *fm_flow;
1973         struct rte_flow *flow;
1974
1975         ENICPMD_FUNC_TRACE();
1976         enic_fm_dump_tcam_entry(match_in, action_in, attrs->ingress);
1977         flow = calloc(1, sizeof(*flow));
1978         fm_flow = calloc(1, sizeof(*fm_flow));
1979         if (flow == NULL || fm_flow == NULL) {
1980                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1981                         NULL, "enic: cannot allocate rte_flow");
1982                 free(flow);
1983                 free(fm_flow);
1984                 return NULL;
1985         }
1986         flow->fm = fm_flow;
1987         fm_flow->action_handle = FM_INVALID_HANDLE;
1988         fm_flow->entry_handle = FM_INVALID_HANDLE;
1989         if (__enic_fm_flow_add_entry(fm, fm_flow, match_in, action_in,
1990                                      attrs->group, attrs->ingress, error)) {
1991                 enic_fm_flow_free(fm, flow);
1992                 return NULL;
1993         }
1994         return flow;
1995 }
1996
1997 static void
1998 convert_jump_flows(struct enic_flowman *fm, struct enic_fm_fet *fet,
1999                    struct rte_flow_error *error)
2000 {
2001         struct enic_fm_flow *fm_flow;
2002         struct enic_fm_jump_flow *j;
2003         struct fm_action *fma;
2004         uint32_t group;
2005
2006         ENICPMD_FUNC_TRACE();
2007         /*
2008          * Find the saved flows that should jump to the new table (fet).
2009          * Then delete the old TCAM entry that jumps to the default table,
2010          * and add a new one that jumps to the new table.
2011          */
2012         group = fet->group;
2013         j = find_jump_flow(fm, group);
2014         while (j) {
2015                 ENICPMD_LOG(DEBUG, "convert jump flow: flow=%p group=%u",
2016                             j->flow, group);
2017                 /* Delete old entry */
2018                 fm_flow = j->flow->fm;
2019                 __enic_fm_flow_free(fm, fm_flow);
2020
2021                 /* Add new entry */
2022                 fma = &j->action;
2023                 fma->fma_action_ops[0].exact.handle = fet->handle;
2024                 if (__enic_fm_flow_add_entry(fm, fm_flow, &j->match, fma,
2025                         FM_TCAM_RTE_GROUP, fet->ingress, error)) {
2026                         /* Cannot roll back changes at the moment */
2027                         ENICPMD_LOG(ERR, "cannot convert jump flow: flow=%p",
2028                                     j->flow);
2029                 } else {
2030                         fm_flow->fet = fet;
2031                         fet->ref++;
2032                         ENICPMD_LOG(DEBUG, "convert ok: group=%u ref=%u",
2033                                     fet->group, fet->ref);
2034                 }
2035
2036                 TAILQ_REMOVE(&fm->jump_list, j, list);
2037                 free(j);
2038                 j = find_jump_flow(fm, group);
2039         }
2040 }
2041
2042 static void
2043 enic_fm_open_scratch(struct enic_flowman *fm)
2044 {
2045         fm->action_op_count = 0;
2046         fm->fet = NULL;
2047         memset(&fm->tcam_entry, 0, sizeof(fm->tcam_entry));
2048         memset(&fm->action, 0, sizeof(fm->action));
2049 }
2050
2051 static void
2052 enic_fm_close_scratch(struct enic_flowman *fm)
2053 {
2054         if (fm->fet) {
2055                 enic_fet_put(fm, fm->fet);
2056                 fm->fet = NULL;
2057         }
2058         fm->action_op_count = 0;
2059 }
2060
2061 static int
2062 enic_fm_flow_validate(struct rte_eth_dev *dev,
2063                       const struct rte_flow_attr *attrs,
2064                       const struct rte_flow_item pattern[],
2065                       const struct rte_flow_action actions[],
2066                       struct rte_flow_error *error)
2067 {
2068         struct fm_tcam_match_entry *fm_tcam_entry;
2069         struct fm_action *fm_action;
2070         struct enic_flowman *fm;
2071         int ret;
2072
2073         ENICPMD_FUNC_TRACE();
2074         fm = pmd_priv(dev)->fm;
2075         if (fm == NULL)
2076                 return -ENOTSUP;
2077         enic_fm_open_scratch(fm);
2078         ret = enic_fm_flow_parse(fm, attrs, pattern, actions, error);
2079         if (!ret) {
2080                 fm_tcam_entry = &fm->tcam_entry;
2081                 fm_action = &fm->action;
2082                 enic_fm_dump_tcam_entry(fm_tcam_entry, fm_action,
2083                                         attrs->ingress);
2084         }
2085         enic_fm_close_scratch(fm);
2086         return ret;
2087 }
2088
2089 static int
2090 enic_fm_flow_query_count(struct rte_eth_dev *dev,
2091                          struct rte_flow *flow, void *data,
2092                          struct rte_flow_error *error)
2093 {
2094         struct rte_flow_query_count *query;
2095         struct enic_fm_flow *fm_flow;
2096         struct enic *enic;
2097         uint64_t args[3];
2098         int rc;
2099
2100         ENICPMD_FUNC_TRACE();
2101         enic = pmd_priv(dev);
2102         query = data;
2103         fm_flow = flow->fm;
2104         if (!fm_flow->counter_valid)
2105                 return rte_flow_error_set(error, ENOTSUP,
2106                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2107                         "enic: flow does not have counter");
2108
2109         args[0] = FM_COUNTER_QUERY;
2110         args[1] = fm_flow->counter->handle;
2111         args[2] = query->reset;
2112         rc = vnic_dev_flowman_cmd(enic->vdev, args, 3);
2113         if (rc) {
2114                 ENICPMD_LOG(ERR, "cannot query counter: rc=%d handle=0x%x",
2115                             rc, fm_flow->counter->handle);
2116                 return rc;
2117         }
2118         query->hits_set = 1;
2119         query->hits = args[0];
2120         query->bytes_set = 1;
2121         query->bytes = args[1];
2122         return 0;
2123 }
2124
2125 static int
2126 enic_fm_flow_query(struct rte_eth_dev *dev,
2127                    struct rte_flow *flow,
2128                    const struct rte_flow_action *actions,
2129                    void *data,
2130                    struct rte_flow_error *error)
2131 {
2132         int ret = 0;
2133
2134         ENICPMD_FUNC_TRACE();
2135         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
2136                 switch (actions->type) {
2137                 case RTE_FLOW_ACTION_TYPE_VOID:
2138                         break;
2139                 case RTE_FLOW_ACTION_TYPE_COUNT:
2140                         ret = enic_fm_flow_query_count(dev, flow, data, error);
2141                         break;
2142                 default:
2143                         return rte_flow_error_set(error, ENOTSUP,
2144                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2145                                                   actions,
2146                                                   "action not supported");
2147                 }
2148                 if (ret < 0)
2149                         return ret;
2150         }
2151         return 0;
2152 }
2153
2154 static struct rte_flow *
2155 enic_fm_flow_create(struct rte_eth_dev *dev,
2156                     const struct rte_flow_attr *attrs,
2157                     const struct rte_flow_item pattern[],
2158                     const struct rte_flow_action actions[],
2159                     struct rte_flow_error *error)
2160 {
2161         struct fm_tcam_match_entry *fm_tcam_entry;
2162         struct fm_action *fm_action;
2163         struct enic_flowman *fm;
2164         struct enic_fm_fet *fet;
2165         struct rte_flow *flow;
2166         struct enic *enic;
2167         int ret;
2168
2169         ENICPMD_FUNC_TRACE();
2170         enic = pmd_priv(dev);
2171         fm = enic->fm;
2172         if (fm == NULL) {
2173                 rte_flow_error_set(error, ENOTSUP,
2174                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2175                         "flowman is not initialized");
2176                 return NULL;
2177         }
2178         enic_fm_open_scratch(fm);
2179         flow = NULL;
2180         ret = enic_fm_flow_parse(fm, attrs, pattern, actions, error);
2181         if (ret < 0)
2182                 goto error_with_scratch;
2183         fm_tcam_entry = &fm->tcam_entry;
2184         fm_action = &fm->action;
2185         flow = enic_fm_flow_add_entry(fm, fm_tcam_entry, fm_action,
2186                                       attrs, error);
2187         if (flow) {
2188                 LIST_INSERT_HEAD(&enic->flows, flow, next);
2189                 fet = flow->fm->fet;
2190                 if (fet && fet->default_key) {
2191                         /*
2192                          * Jump to non-existent group? Save the relevant info
2193                          * so we can convert this flow when that group
2194                          * materializes.
2195                          */
2196                         save_jump_flow(fm, flow, fet->group,
2197                                        fm_tcam_entry, fm_action);
2198                 } else if (fet && fet->ref == 1) {
2199                         /*
2200                          * A new table is created. Convert the saved flows
2201                          * that should jump to this group.
2202                          */
2203                         convert_jump_flows(fm, fet, error);
2204                 }
2205         }
2206
2207 error_with_scratch:
2208         enic_fm_close_scratch(fm);
2209         return flow;
2210 }
2211
2212 static int
2213 enic_fm_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
2214                      __rte_unused struct rte_flow_error *error)
2215 {
2216         struct enic *enic = pmd_priv(dev);
2217
2218         ENICPMD_FUNC_TRACE();
2219         if (enic->fm == NULL)
2220                 return 0;
2221         LIST_REMOVE(flow, next);
2222         enic_fm_flow_free(enic->fm, flow);
2223         return 0;
2224 }
2225
2226 static int
2227 enic_fm_flow_flush(struct rte_eth_dev *dev,
2228                    __rte_unused struct rte_flow_error *error)
2229 {
2230         struct enic_fm_flow *fm_flow;
2231         struct enic_flowman *fm;
2232         struct rte_flow *flow;
2233         struct enic *enic = pmd_priv(dev);
2234
2235         ENICPMD_FUNC_TRACE();
2236         if (enic->fm == NULL)
2237                 return 0;
2238         fm = enic->fm;
2239         while (!LIST_EMPTY(&enic->flows)) {
2240                 flow = LIST_FIRST(&enic->flows);
2241                 fm_flow = flow->fm;
2242                 LIST_REMOVE(flow, next);
2243                 /*
2244                  * If tables are null, then vNIC is closing, and the firmware
2245                  * has already cleaned up flowman state. So do not try to free
2246                  * resources, as it only causes errors.
2247                  */
2248                 if (fm->ig_tcam_hndl == FM_INVALID_HANDLE) {
2249                         fm_flow->entry_handle = FM_INVALID_HANDLE;
2250                         fm_flow->action_handle = FM_INVALID_HANDLE;
2251                         fm_flow->fet = NULL;
2252                 }
2253                 enic_fm_flow_free(fm, flow);
2254         }
2255         return 0;
2256 }
2257
2258 static int
2259 enic_fm_tbl_free(struct enic_flowman *fm, uint64_t handle)
2260 {
2261         uint64_t args[2];
2262         int rc;
2263
2264         args[0] = FM_MATCH_TABLE_FREE;
2265         args[1] = handle;
2266         rc = vnic_dev_flowman_cmd(fm->enic->vdev, args, 2);
2267         if (rc)
2268                 ENICPMD_LOG(ERR, "cannot free table: rc=%d handle=0x%" PRIx64,
2269                             rc, handle);
2270         return rc;
2271 }
2272
2273 static int
2274 enic_fm_tcam_tbl_alloc(struct enic_flowman *fm, uint32_t direction,
2275                         uint32_t max_entries, uint64_t *handle)
2276 {
2277         struct fm_tcam_match_table *tcam_tbl;
2278         struct enic *enic;
2279         uint64_t args[2];
2280         int rc;
2281
2282         ENICPMD_FUNC_TRACE();
2283         enic = fm->enic;
2284         tcam_tbl = &fm->cmd.va->fm_tcam_match_table;
2285         tcam_tbl->ftt_direction = direction;
2286         tcam_tbl->ftt_stage = FM_STAGE_LAST;
2287         tcam_tbl->ftt_max_entries = max_entries;
2288         args[0] = FM_TCAM_TABLE_ALLOC;
2289         args[1] = fm->cmd.pa;
2290         rc = vnic_dev_flowman_cmd(enic->vdev, args, 2);
2291         if (rc) {
2292                 ENICPMD_LOG(ERR, "cannot alloc %s TCAM table: rc=%d",
2293                             (direction == FM_INGRESS) ? "IG" : "EG", rc);
2294                 return rc;
2295         }
2296         *handle = args[0];
2297         ENICPMD_LOG(DEBUG, "%s TCAM table allocated, handle=0x%" PRIx64,
2298                     (direction == FM_INGRESS) ? "IG" : "EG", *handle);
2299         return 0;
2300 }
2301
2302 static int
2303 enic_fm_init_counters(struct enic_flowman *fm)
2304 {
2305         ENICPMD_FUNC_TRACE();
2306         SLIST_INIT(&fm->counters);
2307         return enic_fm_more_counters(fm);
2308 }
2309
2310 static void
2311 enic_fm_free_all_counters(struct enic_flowman *fm)
2312 {
2313         struct enic *enic;
2314         uint64_t args[2];
2315         int rc;
2316
2317         enic = fm->enic;
2318         args[0] = FM_COUNTER_BRK;
2319         args[1] = 0;
2320         rc = vnic_dev_flowman_cmd(enic->vdev, args, 2);
2321         if (rc != 0)
2322                 ENICPMD_LOG(ERR, "cannot free counters: rc=%d", rc);
2323         rte_free(fm->counter_stack);
2324 }
2325
2326 static int
2327 enic_fm_alloc_tcam_tables(struct enic_flowman *fm)
2328 {
2329         int rc;
2330
2331         ENICPMD_FUNC_TRACE();
2332         rc = enic_fm_tcam_tbl_alloc(fm, FM_INGRESS, FM_MAX_TCAM_TABLE_SIZE,
2333                                     &fm->ig_tcam_hndl);
2334         if (rc)
2335                 return rc;
2336         rc = enic_fm_tcam_tbl_alloc(fm, FM_EGRESS, FM_MAX_TCAM_TABLE_SIZE,
2337                                     &fm->eg_tcam_hndl);
2338         return rc;
2339 }
2340
2341 static void
2342 enic_fm_free_tcam_tables(struct enic_flowman *fm)
2343 {
2344         ENICPMD_FUNC_TRACE();
2345         if (fm->ig_tcam_hndl) {
2346                 ENICPMD_LOG(DEBUG, "free IG TCAM table handle=0x%" PRIx64,
2347                             fm->ig_tcam_hndl);
2348                 enic_fm_tbl_free(fm, fm->ig_tcam_hndl);
2349                 fm->ig_tcam_hndl = FM_INVALID_HANDLE;
2350         }
2351         if (fm->eg_tcam_hndl) {
2352                 ENICPMD_LOG(DEBUG, "free EG TCAM table handle=0x%" PRIx64,
2353                             fm->eg_tcam_hndl);
2354                 enic_fm_tbl_free(fm, fm->eg_tcam_hndl);
2355                 fm->eg_tcam_hndl = FM_INVALID_HANDLE;
2356         }
2357 }
2358
2359 int
2360 enic_fm_init(struct enic *enic)
2361 {
2362         struct enic_flowman *fm;
2363         uint8_t name[RTE_MEMZONE_NAMESIZE];
2364         int rc;
2365
2366         if (enic->flow_filter_mode != FILTER_FLOWMAN)
2367                 return 0;
2368         ENICPMD_FUNC_TRACE();
2369         fm = calloc(1, sizeof(*fm));
2370         if (fm == NULL) {
2371                 ENICPMD_LOG(ERR, "cannot alloc flowman struct");
2372                 return -ENOMEM;
2373         }
2374         fm->enic = enic;
2375         TAILQ_INIT(&fm->fet_list);
2376         TAILQ_INIT(&fm->jump_list);
2377         /* Allocate host memory for flowman commands */
2378         snprintf((char *)name, sizeof(name), "fm-cmd-%s", enic->bdf_name);
2379         fm->cmd.va = enic_alloc_consistent(enic,
2380                 sizeof(union enic_flowman_cmd_mem), &fm->cmd.pa, name);
2381         if (!fm->cmd.va) {
2382                 ENICPMD_LOG(ERR, "cannot allocate flowman command memory");
2383                 rc = -ENOMEM;
2384                 goto error_fm;
2385         }
2386         /* Allocate TCAM tables upfront as they are the main tables */
2387         rc = enic_fm_alloc_tcam_tables(fm);
2388         if (rc) {
2389                 ENICPMD_LOG(ERR, "cannot alloc TCAM tables");
2390                 goto error_cmd;
2391         }
2392         /* Then a number of counters */
2393         rc = enic_fm_init_counters(fm);
2394         if (rc) {
2395                 ENICPMD_LOG(ERR, "cannot alloc counters");
2396                 goto error_tables;
2397         }
2398         /*
2399          * One default exact match table for each direction. We hold onto
2400          * it until close.
2401          */
2402         rc = enic_fet_alloc(fm, 1, NULL, 128, &fm->default_ig_fet);
2403         if (rc) {
2404                 ENICPMD_LOG(ERR, "cannot alloc default IG exact match table");
2405                 goto error_counters;
2406         }
2407         fm->default_ig_fet->ref = 1;
2408         rc = enic_fet_alloc(fm, 0, NULL, 128, &fm->default_eg_fet);
2409         if (rc) {
2410                 ENICPMD_LOG(ERR, "cannot alloc default EG exact match table");
2411                 goto error_ig_fet;
2412         }
2413         fm->default_eg_fet->ref = 1;
2414         enic->fm = fm;
2415         return 0;
2416
2417 error_ig_fet:
2418         enic_fet_free(fm, fm->default_ig_fet);
2419 error_counters:
2420         enic_fm_free_all_counters(fm);
2421 error_tables:
2422         enic_fm_free_tcam_tables(fm);
2423 error_cmd:
2424         enic_free_consistent(enic, sizeof(union enic_flowman_cmd_mem),
2425                 fm->cmd.va, fm->cmd.pa);
2426 error_fm:
2427         free(fm);
2428         return rc;
2429 }
2430
2431 void
2432 enic_fm_destroy(struct enic *enic)
2433 {
2434         struct enic_flowman *fm;
2435         struct enic_fm_fet *fet;
2436
2437         if (enic->fm == NULL)
2438                 return;
2439         ENICPMD_FUNC_TRACE();
2440         fm = enic->fm;
2441         enic_fet_free(fm, fm->default_eg_fet);
2442         enic_fet_free(fm, fm->default_ig_fet);
2443         /* Free all exact match tables still open */
2444         while (!TAILQ_EMPTY(&fm->fet_list)) {
2445                 fet = TAILQ_FIRST(&fm->fet_list);
2446                 enic_fet_free(fm, fet);
2447         }
2448         enic_fm_free_tcam_tables(fm);
2449         enic_fm_free_all_counters(fm);
2450         enic_free_consistent(enic, sizeof(union enic_flowman_cmd_mem),
2451                 fm->cmd.va, fm->cmd.pa);
2452         fm->cmd.va = NULL;
2453         free(fm);
2454         enic->fm = NULL;
2455 }
2456
2457 const struct rte_flow_ops enic_fm_flow_ops = {
2458         .validate = enic_fm_flow_validate,
2459         .create = enic_fm_flow_create,
2460         .destroy = enic_fm_flow_destroy,
2461         .flush = enic_fm_flow_flush,
2462         .query = enic_fm_flow_query,
2463 };