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