net/enic: generate VXLAN src port if it is zero in template
[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 /* Generate a reasonable source port number */
982 static uint16_t
983 gen_src_port(void)
984 {
985         /* Min/max below are the default values in OVS-DPDK and Linux */
986         uint16_t p = rte_rand();
987         p = RTE_MAX(p, 32768);
988         p = RTE_MIN(p, 61000);
989         return rte_cpu_to_be_16(p);
990 }
991
992 /* VXLAN encap is done via flowman compound action */
993 static int
994 enic_fm_copy_vxlan_encap(struct enic_flowman *fm,
995                          const struct rte_flow_item *item,
996                          struct rte_flow_error *error)
997 {
998         struct fm_action_op fm_op;
999         struct rte_ether_hdr *eth;
1000         struct rte_udp_hdr *udp;
1001         uint16_t *ethertype;
1002         void *template;
1003         uint8_t off;
1004
1005         ENICPMD_FUNC_TRACE();
1006         memset(&fm_op, 0, sizeof(fm_op));
1007         fm_op.fa_op = FMOP_ENCAP;
1008         template = fm->action.fma_data;
1009         off = 0;
1010         /*
1011          * Copy flow items to the flowman template starting L2.
1012          * L2 must be ethernet.
1013          */
1014         flow_item_skip_void(&item);
1015         if (item->type != RTE_FLOW_ITEM_TYPE_ETH)
1016                 return rte_flow_error_set(error, EINVAL,
1017                         RTE_FLOW_ERROR_TYPE_ITEM, item,
1018                         "vxlan-encap: first item should be ethernet");
1019         eth = (struct rte_ether_hdr *)template;
1020         ethertype = &eth->ether_type;
1021         append_template(&template, &off, item->spec,
1022                         sizeof(struct rte_flow_item_eth));
1023         item++;
1024         flow_item_skip_void(&item);
1025         /* Optional VLAN */
1026         if (item->type == RTE_FLOW_ITEM_TYPE_VLAN) {
1027                 const struct rte_flow_item_vlan *spec;
1028
1029                 ENICPMD_LOG(DEBUG, "vxlan-encap: vlan");
1030                 spec = item->spec;
1031                 fm_op.encap.outer_vlan = rte_be_to_cpu_16(spec->tci);
1032                 item++;
1033                 flow_item_skip_void(&item);
1034         }
1035         /* L3 must be IPv4, IPv6 */
1036         switch (item->type) {
1037         case RTE_FLOW_ITEM_TYPE_IPV4:
1038         {
1039                 struct rte_ipv4_hdr *ip4;
1040
1041                 ENICPMD_LOG(DEBUG, "vxlan-encap: ipv4");
1042                 *ethertype = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
1043                 ip4 = (struct rte_ipv4_hdr *)template;
1044                 /*
1045                  * Offset of IPv4 length field and its initial value
1046                  * (IP + UDP + VXLAN) are specified in the action. The NIC
1047                  * will add inner packet length.
1048                  */
1049                 fm_op.encap.len1_offset = off +
1050                         offsetof(struct rte_ipv4_hdr, total_length);
1051                 fm_op.encap.len1_delta = sizeof(struct rte_ipv4_hdr) +
1052                         sizeof(struct rte_udp_hdr) +
1053                         sizeof(struct rte_vxlan_hdr);
1054                 append_template(&template, &off, item->spec,
1055                                 sizeof(struct rte_ipv4_hdr));
1056                 ip4->version_ihl = RTE_IPV4_VHL_DEF;
1057                 if (ip4->time_to_live == 0)
1058                         ip4->time_to_live = IP_DEFTTL;
1059                 ip4->next_proto_id = IPPROTO_UDP;
1060                 break;
1061         }
1062         case RTE_FLOW_ITEM_TYPE_IPV6:
1063         {
1064                 struct rte_ipv6_hdr *ip6;
1065
1066                 ENICPMD_LOG(DEBUG, "vxlan-encap: ipv6");
1067                 *ethertype = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
1068                 ip6 = (struct rte_ipv6_hdr *)template;
1069                 fm_op.encap.len1_offset = off +
1070                         offsetof(struct rte_ipv6_hdr, payload_len);
1071                 fm_op.encap.len1_delta = sizeof(struct rte_udp_hdr) +
1072                         sizeof(struct rte_vxlan_hdr);
1073                 append_template(&template, &off, item->spec,
1074                                 sizeof(struct rte_ipv6_hdr));
1075                 ip6->vtc_flow |= rte_cpu_to_be_32(IP6_VTC_FLOW);
1076                 if (ip6->hop_limits == 0)
1077                         ip6->hop_limits = IP_DEFTTL;
1078                 ip6->proto = IPPROTO_UDP;
1079                 break;
1080         }
1081         default:
1082                 return rte_flow_error_set(error,
1083                         EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
1084                         "vxlan-encap: L3 must be IPv4/IPv6");
1085         }
1086         item++;
1087         flow_item_skip_void(&item);
1088
1089         /* L4 is UDP */
1090         if (item->type != RTE_FLOW_ITEM_TYPE_UDP)
1091                 return rte_flow_error_set(error, EINVAL,
1092                         RTE_FLOW_ERROR_TYPE_ITEM, item,
1093                         "vxlan-encap: UDP must follow IPv4/IPv6");
1094         /* UDP length = UDP + VXLAN. NIC will add inner packet length. */
1095         fm_op.encap.len2_offset =
1096                 off + offsetof(struct rte_udp_hdr, dgram_len);
1097         fm_op.encap.len2_delta =
1098                 sizeof(struct rte_udp_hdr) + sizeof(struct rte_vxlan_hdr);
1099         udp = (struct rte_udp_hdr *)template;
1100         append_template(&template, &off, item->spec,
1101                         sizeof(struct rte_udp_hdr));
1102         /*
1103          * Firmware does not hash/fill source port yet. Generate a
1104          * random port, as there is *usually* one rte_flow for the
1105          * given inner packet stream (i.e. a single stream has one
1106          * random port).
1107          */
1108         if (udp->src_port == 0)
1109                 udp->src_port = gen_src_port();
1110         item++;
1111         flow_item_skip_void(&item);
1112
1113         /* Finally VXLAN */
1114         if (item->type != RTE_FLOW_ITEM_TYPE_VXLAN)
1115                 return rte_flow_error_set(error,
1116                         EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
1117                         "vxlan-encap: VXLAN must follow UDP");
1118         append_template(&template, &off, item->spec,
1119                         sizeof(struct rte_flow_item_vxlan));
1120
1121         /*
1122          * Fill in the rest of the action structure.
1123          * Indicate that we want to encap with vxlan at packet start.
1124          */
1125         fm_op.encap.template_offset = 0;
1126         fm_op.encap.template_len = off;
1127         return enic_fm_append_action_op(fm, &fm_op, error);
1128 }
1129
1130 static int
1131 enic_fm_find_vnic(struct enic *enic, const struct rte_pci_addr *addr,
1132                   uint64_t *handle)
1133 {
1134         uint32_t bdf;
1135         uint64_t args[2];
1136         int rc;
1137
1138         ENICPMD_FUNC_TRACE();
1139         ENICPMD_LOG(DEBUG, "bdf=%x:%x:%x", addr->bus, addr->devid,
1140                     addr->function);
1141         bdf = addr->bus << 8 | addr->devid << 3 | addr->function;
1142         args[0] = FM_VNIC_FIND;
1143         args[1] = bdf;
1144         rc = vnic_dev_flowman_cmd(enic->vdev, args, 2);
1145         if (rc != 0) {
1146                 /* Expected to fail if BDF is not on the adapter */
1147                 ENICPMD_LOG(DEBUG, "cannot find vnic handle: rc=%d", rc);
1148                 return rc;
1149         }
1150         *handle = args[0];
1151         ENICPMD_LOG(DEBUG, "found vnic: handle=0x%" PRIx64, *handle);
1152         return 0;
1153 }
1154
1155 /*
1156  * Egress: target port should be either PF uplink or VF.
1157  * Supported cases
1158  * 1. VF egress -> PF uplink
1159  *   PF may be this VF's PF, or another PF, as long as they are on the same VIC.
1160  * 2. VF egress -> VF
1161  *
1162  * Unsupported cases
1163  * 1. PF egress -> VF
1164  *   App should be using representor to pass packets to VF
1165  */
1166 static int
1167 vf_egress_port_id_action(struct enic_flowman *fm,
1168                          struct rte_eth_dev *dst_dev,
1169                          uint64_t dst_vnic_h,
1170                          struct fm_action_op *fm_op,
1171                          struct rte_flow_error *error)
1172 {
1173         struct enic *src_enic, *dst_enic;
1174         struct enic_vf_representor *vf;
1175         uint8_t uif;
1176         int ret;
1177
1178         ENICPMD_FUNC_TRACE();
1179         src_enic = fm->user_enic;
1180         dst_enic = pmd_priv(dst_dev);
1181         if (!(src_enic->rte_dev->data->dev_flags & RTE_ETH_DEV_REPRESENTOR)) {
1182                 return rte_flow_error_set(error, EINVAL,
1183                         RTE_FLOW_ERROR_TYPE_ACTION,
1184                         NULL, "source port is not VF representor");
1185         }
1186
1187         /* VF -> PF uplink. dst is not VF representor */
1188         if (!(dst_dev->data->dev_flags & RTE_ETH_DEV_REPRESENTOR)) {
1189                 /* PF is the VF's PF? Then nothing to do */
1190                 vf = VF_ENIC_TO_VF_REP(src_enic);
1191                 if (vf->pf == dst_enic) {
1192                         ENICPMD_LOG(DEBUG, "destination port is VF's PF");
1193                         return 0;
1194                 }
1195                 /* If not, steer to the remote PF's uplink */
1196                 uif = dst_enic->fm_vnic_uif;
1197                 ENICPMD_LOG(DEBUG, "steer to uplink %u", uif);
1198                 memset(fm_op, 0, sizeof(*fm_op));
1199                 fm_op->fa_op = FMOP_SET_EGPORT;
1200                 fm_op->set_egport.egport = uif;
1201                 ret = enic_fm_append_action_op(fm, fm_op, error);
1202                 return ret;
1203         }
1204
1205         /* VF -> VF loopback. Hairpin and steer to vnic */
1206         memset(fm_op, 0, sizeof(*fm_op));
1207         fm_op->fa_op = FMOP_EG_HAIRPIN;
1208         ret = enic_fm_append_action_op(fm, fm_op, error);
1209         if (ret)
1210                 return ret;
1211         ENICPMD_LOG(DEBUG, "egress hairpin");
1212         fm->hairpin_steer_vnic_h = dst_vnic_h;
1213         fm->need_hairpin_steer = 1;
1214         return 0;
1215 }
1216
1217 /* Translate flow actions to flowman TCAM entry actions */
1218 static int
1219 enic_fm_copy_action(struct enic_flowman *fm,
1220                     const struct rte_flow_action actions[],
1221                     uint8_t ingress,
1222                     struct rte_flow_error *error)
1223 {
1224         enum {
1225                 FATE = 1 << 0,
1226                 DECAP = 1 << 1,
1227                 PASSTHRU = 1 << 2,
1228                 COUNT = 1 << 3,
1229                 ENCAP = 1 << 4,
1230                 PUSH_VLAN = 1 << 5,
1231                 PORT_ID = 1 << 6,
1232         };
1233         struct fm_tcam_match_entry *fmt;
1234         struct fm_action_op fm_op;
1235         bool need_ovlan_action;
1236         struct enic *enic;
1237         uint32_t overlap;
1238         uint64_t vnic_h;
1239         uint16_t ovlan;
1240         bool first_rq;
1241         bool steer;
1242         int ret;
1243
1244         ENICPMD_FUNC_TRACE();
1245         fmt = &fm->tcam_entry;
1246         need_ovlan_action = false;
1247         ovlan = 0;
1248         first_rq = true;
1249         steer = false;
1250         enic = fm->user_enic;
1251         overlap = 0;
1252         vnic_h = enic->fm_vnic_handle;
1253
1254         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1255                 switch (actions->type) {
1256                 case RTE_FLOW_ACTION_TYPE_VOID:
1257                         continue;
1258                 case RTE_FLOW_ACTION_TYPE_PASSTHRU: {
1259                         if (overlap & PASSTHRU)
1260                                 goto unsupported;
1261                         overlap |= PASSTHRU;
1262                         break;
1263                 }
1264                 case RTE_FLOW_ACTION_TYPE_JUMP: {
1265                         const struct rte_flow_action_jump *jump =
1266                                 actions->conf;
1267                         struct enic_fm_fet *fet;
1268
1269                         if (overlap & FATE)
1270                                 goto unsupported;
1271                         ret = enic_fet_get(fm, jump->group, ingress, NULL,
1272                                            &fet, error);
1273                         if (ret)
1274                                 return ret;
1275                         overlap |= FATE;
1276                         memset(&fm_op, 0, sizeof(fm_op));
1277                         fm_op.fa_op = FMOP_EXACT_MATCH;
1278                         fm_op.exact.handle = fet->handle;
1279                         fm->fet = fet;
1280                         ret = enic_fm_append_action_op(fm, &fm_op, error);
1281                         if (ret)
1282                                 return ret;
1283                         break;
1284                 }
1285                 case RTE_FLOW_ACTION_TYPE_MARK: {
1286                         const struct rte_flow_action_mark *mark =
1287                                 actions->conf;
1288
1289                         if (mark->id >= ENIC_MAGIC_FILTER_ID - 1)
1290                                 return rte_flow_error_set(error, EINVAL,
1291                                         RTE_FLOW_ERROR_TYPE_ACTION,
1292                                         NULL, "invalid mark id");
1293                         memset(&fm_op, 0, sizeof(fm_op));
1294                         fm_op.fa_op = FMOP_MARK;
1295                         fm_op.mark.mark = mark->id + 1;
1296                         ret = enic_fm_append_action_op(fm, &fm_op, error);
1297                         if (ret)
1298                                 return ret;
1299                         break;
1300                 }
1301                 case RTE_FLOW_ACTION_TYPE_FLAG: {
1302                         /* ENIC_MAGIC_FILTER_ID is reserved for flagging */
1303                         memset(&fm_op, 0, sizeof(fm_op));
1304                         fm_op.fa_op = FMOP_MARK;
1305                         fm_op.mark.mark = ENIC_MAGIC_FILTER_ID;
1306                         ret = enic_fm_append_action_op(fm, &fm_op, error);
1307                         if (ret)
1308                                 return ret;
1309                         break;
1310                 }
1311                 case RTE_FLOW_ACTION_TYPE_QUEUE: {
1312                         const struct rte_flow_action_queue *queue =
1313                                 actions->conf;
1314
1315                         /*
1316                          * If fate other than QUEUE or RSS, fail. Multiple
1317                          * rss and queue actions are ok.
1318                          */
1319                         if ((overlap & FATE) && first_rq)
1320                                 goto unsupported;
1321                         first_rq = false;
1322                         overlap |= FATE;
1323                         memset(&fm_op, 0, sizeof(fm_op));
1324                         fm_op.fa_op = FMOP_RQ_STEER;
1325                         fm_op.rq_steer.rq_index =
1326                                 enic_rte_rq_idx_to_sop_idx(queue->index);
1327                         fm_op.rq_steer.rq_count = 1;
1328                         fm_op.rq_steer.vnic_handle = vnic_h;
1329                         ret = enic_fm_append_action_op(fm, &fm_op, error);
1330                         if (ret)
1331                                 return ret;
1332                         ENICPMD_LOG(DEBUG, "create QUEUE action rq: %u",
1333                                     fm_op.rq_steer.rq_index);
1334                         steer = true;
1335                         break;
1336                 }
1337                 case RTE_FLOW_ACTION_TYPE_DROP: {
1338                         if (overlap & FATE)
1339                                 goto unsupported;
1340                         overlap |= FATE;
1341                         memset(&fm_op, 0, sizeof(fm_op));
1342                         fm_op.fa_op = FMOP_DROP;
1343                         ret = enic_fm_append_action_op(fm, &fm_op, error);
1344                         if (ret)
1345                                 return ret;
1346                         ENICPMD_LOG(DEBUG, "create DROP action");
1347                         break;
1348                 }
1349                 case RTE_FLOW_ACTION_TYPE_COUNT: {
1350                         if (overlap & COUNT)
1351                                 goto unsupported;
1352                         overlap |= COUNT;
1353                         /* Count is associated with entry not action on VIC. */
1354                         fmt->ftm_flags |= FMEF_COUNTER;
1355                         break;
1356                 }
1357                 case RTE_FLOW_ACTION_TYPE_RSS: {
1358                         const struct rte_flow_action_rss *rss = actions->conf;
1359                         bool allow;
1360                         uint16_t i;
1361
1362                         /*
1363                          * If fate other than QUEUE or RSS, fail. Multiple
1364                          * rss and queue actions are ok.
1365                          */
1366                         if ((overlap & FATE) && first_rq)
1367                                 goto unsupported;
1368                         first_rq = false;
1369                         overlap |= FATE;
1370
1371                         /*
1372                          * Hardware only supports RSS actions on outer level
1373                          * with default type and function. Queues must be
1374                          * sequential.
1375                          */
1376                         allow = rss->func == RTE_ETH_HASH_FUNCTION_DEFAULT &&
1377                                 rss->level == 0 && (rss->types == 0 ||
1378                                 rss->types == enic->rss_hf) &&
1379                                 rss->queue_num <= enic->rq_count &&
1380                                 rss->queue[rss->queue_num - 1] < enic->rq_count;
1381
1382
1383                         /* Identity queue map needs to be sequential */
1384                         for (i = 1; i < rss->queue_num; i++)
1385                                 allow = allow && (rss->queue[i] ==
1386                                         rss->queue[i - 1] + 1);
1387                         if (!allow)
1388                                 goto unsupported;
1389
1390                         memset(&fm_op, 0, sizeof(fm_op));
1391                         fm_op.fa_op = FMOP_RQ_STEER;
1392                         fm_op.rq_steer.rq_index =
1393                                 enic_rte_rq_idx_to_sop_idx(rss->queue[0]);
1394                         fm_op.rq_steer.rq_count = rss->queue_num;
1395                         fm_op.rq_steer.vnic_handle = vnic_h;
1396                         ret = enic_fm_append_action_op(fm, &fm_op, error);
1397                         if (ret)
1398                                 return ret;
1399                         ENICPMD_LOG(DEBUG, "create QUEUE action rq: %u",
1400                                     fm_op.rq_steer.rq_index);
1401                         steer = true;
1402                         break;
1403                 }
1404                 case RTE_FLOW_ACTION_TYPE_PORT_ID: {
1405                         const struct rte_flow_action_port_id *port;
1406                         struct rte_eth_dev *dev;
1407
1408                         if (!ingress && (overlap & PORT_ID)) {
1409                                 ENICPMD_LOG(DEBUG, "cannot have multiple egress PORT_ID actions");
1410                                 goto unsupported;
1411                         }
1412                         port = actions->conf;
1413                         if (port->original) {
1414                                 vnic_h = enic->fm_vnic_handle; /* This port */
1415                                 break;
1416                         }
1417                         ENICPMD_LOG(DEBUG, "port id %u", port->id);
1418                         if (!rte_eth_dev_is_valid_port(port->id)) {
1419                                 return rte_flow_error_set(error, EINVAL,
1420                                         RTE_FLOW_ERROR_TYPE_ACTION,
1421                                         NULL, "invalid port_id");
1422                         }
1423                         dev = &rte_eth_devices[port->id];
1424                         if (!dev_is_enic(dev)) {
1425                                 return rte_flow_error_set(error, EINVAL,
1426                                         RTE_FLOW_ERROR_TYPE_ACTION,
1427                                         NULL, "port_id is not enic");
1428                         }
1429                         if (enic->switch_domain_id !=
1430                             pmd_priv(dev)->switch_domain_id) {
1431                                 return rte_flow_error_set(error, EINVAL,
1432                                         RTE_FLOW_ERROR_TYPE_ACTION,
1433                                         NULL, "destination and source ports are not in the same switch domain");
1434                         }
1435                         vnic_h = pmd_priv(dev)->fm_vnic_handle;
1436                         overlap |= PORT_ID;
1437                         /*
1438                          * Ingress. Nothing more to do. We add an implicit
1439                          * steer at the end if needed.
1440                          */
1441                         if (ingress)
1442                                 break;
1443                         /* Egress */
1444                         ret = vf_egress_port_id_action(fm, dev, vnic_h, &fm_op,
1445                                 error);
1446                         if (ret)
1447                                 return ret;
1448                         break;
1449                 }
1450                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP: {
1451                         if (overlap & DECAP)
1452                                 goto unsupported;
1453                         overlap |= DECAP;
1454
1455                         ret = enic_fm_copy_vxlan_decap(fm, fmt, actions,
1456                                 error);
1457                         if (ret != 0)
1458                                 return ret;
1459                         break;
1460                 }
1461                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP: {
1462                         const struct rte_flow_action_vxlan_encap *encap;
1463
1464                         encap = actions->conf;
1465                         if (overlap & ENCAP)
1466                                 goto unsupported;
1467                         overlap |= ENCAP;
1468                         ret = enic_fm_copy_vxlan_encap(fm, encap->definition,
1469                                 error);
1470                         if (ret != 0)
1471                                 return ret;
1472                         break;
1473                 }
1474                 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN: {
1475                         memset(&fm_op, 0, sizeof(fm_op));
1476                         fm_op.fa_op = FMOP_POP_VLAN;
1477                         ret = enic_fm_append_action_op(fm, &fm_op, error);
1478                         if (ret)
1479                                 return ret;
1480                         break;
1481                 }
1482                 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN: {
1483                         const struct rte_flow_action_of_push_vlan *vlan;
1484
1485                         if (overlap & PASSTHRU)
1486                                 goto unsupported;
1487                         vlan = actions->conf;
1488                         if (vlan->ethertype != RTE_BE16(RTE_ETHER_TYPE_VLAN)) {
1489                                 return rte_flow_error_set(error, EINVAL,
1490                                         RTE_FLOW_ERROR_TYPE_ACTION,
1491                                         NULL, "unexpected push_vlan ethertype");
1492                         }
1493                         overlap |= PUSH_VLAN;
1494                         need_ovlan_action = true;
1495                         break;
1496                 }
1497                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP: {
1498                         const struct rte_flow_action_of_set_vlan_pcp *pcp;
1499
1500                         pcp = actions->conf;
1501                         if (pcp->vlan_pcp > 7) {
1502                                 return rte_flow_error_set(error, EINVAL,
1503                                         RTE_FLOW_ERROR_TYPE_ACTION,
1504                                         NULL, "invalid vlan_pcp");
1505                         }
1506                         need_ovlan_action = true;
1507                         ovlan |= ((uint16_t)pcp->vlan_pcp) << 13;
1508                         break;
1509                 }
1510                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID: {
1511                         const struct rte_flow_action_of_set_vlan_vid *vid;
1512
1513                         vid = actions->conf;
1514                         need_ovlan_action = true;
1515                         ovlan |= rte_be_to_cpu_16(vid->vlan_vid);
1516                         break;
1517                 }
1518                 default:
1519                         goto unsupported;
1520                 }
1521         }
1522
1523         if (!(overlap & (FATE | PASSTHRU | COUNT | PORT_ID)))
1524                 goto unsupported;
1525         /* Egress from VF: need implicit WQ match */
1526         if (enic_is_vf_rep(enic) && !ingress) {
1527                 fmt->ftm_data.fk_wq_id = 0;
1528                 fmt->ftm_mask.fk_wq_id = 0xffff;
1529                 fmt->ftm_data.fk_wq_vnic = enic->fm_vnic_handle;
1530                 ENICPMD_LOG(DEBUG, "add implicit wq id match for vf %d",
1531                             VF_ENIC_TO_VF_REP(enic)->vf_id);
1532         }
1533         if (need_ovlan_action) {
1534                 memset(&fm_op, 0, sizeof(fm_op));
1535                 fm_op.fa_op = FMOP_SET_OVLAN;
1536                 fm_op.ovlan.vlan = ovlan;
1537                 ret = enic_fm_append_action_op(fm, &fm_op, error);
1538                 if (ret)
1539                         return ret;
1540         }
1541         /* Add steer op for PORT_ID without QUEUE */
1542         if ((overlap & PORT_ID) && !steer && ingress) {
1543                 memset(&fm_op, 0, sizeof(fm_op));
1544                 /* Always to queue 0 for now as generic RSS is not available */
1545                 fm_op.fa_op = FMOP_RQ_STEER;
1546                 fm_op.rq_steer.rq_index = 0;
1547                 fm_op.rq_steer.vnic_handle = vnic_h;
1548                 ret = enic_fm_append_action_op(fm, &fm_op, error);
1549                 if (ret)
1550                         return ret;
1551                 ENICPMD_LOG(DEBUG, "add implicit steer op");
1552         }
1553         /* Add required END */
1554         memset(&fm_op, 0, sizeof(fm_op));
1555         fm_op.fa_op = FMOP_END;
1556         ret = enic_fm_append_action_op(fm, &fm_op, error);
1557         if (ret)
1558                 return ret;
1559         enic_fm_reorder_action_op(fm);
1560         return 0;
1561
1562 unsupported:
1563         return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
1564                                   NULL, "enic: unsupported action");
1565 }
1566
1567 /** Check if the action is supported */
1568 static int
1569 enic_fm_match_action(const struct rte_flow_action *action,
1570                      const enum rte_flow_action_type *supported_actions)
1571 {
1572         for (; *supported_actions != RTE_FLOW_ACTION_TYPE_END;
1573              supported_actions++) {
1574                 if (action->type == *supported_actions)
1575                         return 1;
1576         }
1577         return 0;
1578 }
1579
1580 /* Debug function to dump internal NIC action structure. */
1581 static void
1582 enic_fm_dump_tcam_actions(const struct fm_action *fm_action)
1583 {
1584         /* Manually keep in sync with FMOP commands */
1585         const char *fmop_str[FMOP_OP_MAX] = {
1586                 [FMOP_END] = "end",
1587                 [FMOP_DROP] = "drop",
1588                 [FMOP_RQ_STEER] = "steer",
1589                 [FMOP_EXACT_MATCH] = "exmatch",
1590                 [FMOP_MARK] = "mark",
1591                 [FMOP_EXT_MARK] = "ext_mark",
1592                 [FMOP_TAG] = "tag",
1593                 [FMOP_EG_HAIRPIN] = "eg_hairpin",
1594                 [FMOP_IG_HAIRPIN] = "ig_hairpin",
1595                 [FMOP_ENCAP_IVLAN] = "encap_ivlan",
1596                 [FMOP_ENCAP_NOIVLAN] = "encap_noivlan",
1597                 [FMOP_ENCAP] = "encap",
1598                 [FMOP_SET_OVLAN] = "set_ovlan",
1599                 [FMOP_DECAP_NOSTRIP] = "decap_nostrip",
1600                 [FMOP_DECAP_STRIP] = "decap_strip",
1601                 [FMOP_POP_VLAN] = "pop_vlan",
1602                 [FMOP_SET_EGPORT] = "set_egport",
1603                 [FMOP_RQ_STEER_ONLY] = "rq_steer_only",
1604                 [FMOP_SET_ENCAP_VLAN] = "set_encap_vlan",
1605                 [FMOP_EMIT] = "emit",
1606                 [FMOP_MODIFY] = "modify",
1607         };
1608         const struct fm_action_op *op = &fm_action->fma_action_ops[0];
1609         char buf[128], *bp = buf;
1610         const char *op_str;
1611         int i, n, buf_len;
1612
1613         buf[0] = '\0';
1614         buf_len = sizeof(buf);
1615         for (i = 0; i < FM_ACTION_OP_MAX; i++) {
1616                 if (op->fa_op == FMOP_END)
1617                         break;
1618                 if (op->fa_op >= FMOP_OP_MAX)
1619                         op_str = "unknown";
1620                 else
1621                         op_str = fmop_str[op->fa_op];
1622                 n = snprintf(bp, buf_len, "%s,", op_str);
1623                 if (n > 0 && n < buf_len) {
1624                         bp += n;
1625                         buf_len -= n;
1626                 }
1627                 op++;
1628         }
1629         /* Remove trailing comma */
1630         if (buf[0])
1631                 *(bp - 1) = '\0';
1632         ENICPMD_LOG(DEBUG, "       Acions: %s", buf);
1633 }
1634
1635 static int
1636 bits_to_str(uint32_t bits, const char *strings[], int max,
1637             char *buf, int buf_len)
1638 {
1639         int i, n = 0, len = 0;
1640
1641         for (i = 0; i < max; i++) {
1642                 if (bits & (1 << i)) {
1643                         n = snprintf(buf, buf_len, "%s,", strings[i]);
1644                         if (n > 0 && n < buf_len) {
1645                                 buf += n;
1646                                 buf_len -= n;
1647                                 len += n;
1648                         }
1649                 }
1650         }
1651         /* Remove trailing comma */
1652         if (len) {
1653                 *(buf - 1) = '\0';
1654                 len--;
1655         }
1656         return len;
1657 }
1658
1659 /* Debug function to dump internal NIC filter structure. */
1660 static void
1661 __enic_fm_dump_tcam_match(const struct fm_header_set *fk_hdrset, char *buf,
1662                           int buf_len)
1663 {
1664         /* Manually keep in sync with FKM_BITS */
1665         const char *fm_fkm_str[FKM_BIT_COUNT] = {
1666                 [FKM_QTAG_BIT] = "qtag",
1667                 [FKM_CMD_BIT] = "cmd",
1668                 [FKM_IPV4_BIT] = "ip4",
1669                 [FKM_IPV6_BIT] = "ip6",
1670                 [FKM_ROCE_BIT] = "roce",
1671                 [FKM_UDP_BIT] = "udp",
1672                 [FKM_TCP_BIT] = "tcp",
1673                 [FKM_TCPORUDP_BIT] = "tcpportudp",
1674                 [FKM_IPFRAG_BIT] = "ipfrag",
1675                 [FKM_NVGRE_BIT] = "nvgre",
1676                 [FKM_VXLAN_BIT] = "vxlan",
1677                 [FKM_GENEVE_BIT] = "geneve",
1678                 [FKM_NSH_BIT] = "nsh",
1679                 [FKM_ROCEV2_BIT] = "rocev2",
1680                 [FKM_VLAN_PRES_BIT] = "vlan_pres",
1681                 [FKM_IPOK_BIT] = "ipok",
1682                 [FKM_L4OK_BIT] = "l4ok",
1683                 [FKM_ROCEOK_BIT] = "roceok",
1684                 [FKM_FCSOK_BIT] = "fcsok",
1685                 [FKM_EG_SPAN_BIT] = "eg_span",
1686                 [FKM_IG_SPAN_BIT] = "ig_span",
1687                 [FKM_EG_HAIRPINNED_BIT] = "eg_hairpinned",
1688         };
1689         /* Manually keep in sync with FKH_BITS */
1690         const char *fm_fkh_str[FKH_BIT_COUNT] = {
1691                 [FKH_ETHER_BIT] = "eth",
1692                 [FKH_QTAG_BIT] = "qtag",
1693                 [FKH_L2RAW_BIT] = "l2raw",
1694                 [FKH_IPV4_BIT] = "ip4",
1695                 [FKH_IPV6_BIT] = "ip6",
1696                 [FKH_L3RAW_BIT] = "l3raw",
1697                 [FKH_UDP_BIT] = "udp",
1698                 [FKH_TCP_BIT] = "tcp",
1699                 [FKH_ICMP_BIT] = "icmp",
1700                 [FKH_VXLAN_BIT] = "vxlan",
1701                 [FKH_L4RAW_BIT] = "l4raw",
1702         };
1703         uint32_t fkh_bits = fk_hdrset->fk_header_select;
1704         uint32_t fkm_bits = fk_hdrset->fk_metadata;
1705         int n;
1706
1707         if (!fkm_bits && !fkh_bits)
1708                 return;
1709         n = snprintf(buf, buf_len, "metadata(");
1710         if (n > 0 && n < buf_len) {
1711                 buf += n;
1712                 buf_len -= n;
1713         }
1714         n = bits_to_str(fkm_bits, fm_fkm_str, FKM_BIT_COUNT, buf, buf_len);
1715         if (n > 0 && n < buf_len) {
1716                 buf += n;
1717                 buf_len -= n;
1718         }
1719         n = snprintf(buf, buf_len, ") valid hdr fields(");
1720         if (n > 0 && n < buf_len) {
1721                 buf += n;
1722                 buf_len -= n;
1723         }
1724         n = bits_to_str(fkh_bits, fm_fkh_str, FKH_BIT_COUNT, buf, buf_len);
1725         if (n > 0 && n < buf_len) {
1726                 buf += n;
1727                 buf_len -= n;
1728         }
1729         snprintf(buf, buf_len, ")");
1730 }
1731
1732 static void
1733 enic_fm_dump_tcam_match(const struct fm_tcam_match_entry *match,
1734                         uint8_t ingress)
1735 {
1736         char buf[256];
1737
1738         memset(buf, 0, sizeof(buf));
1739         __enic_fm_dump_tcam_match(&match->ftm_mask.fk_hdrset[0],
1740                                   buf, sizeof(buf));
1741         ENICPMD_LOG(DEBUG, " TCAM %s Outer: %s %scounter position %u",
1742                     (ingress) ? "IG" : "EG", buf,
1743                     (match->ftm_flags & FMEF_COUNTER) ? "" : "no ",
1744                     match->ftm_position);
1745         memset(buf, 0, sizeof(buf));
1746         __enic_fm_dump_tcam_match(&match->ftm_mask.fk_hdrset[1],
1747                                   buf, sizeof(buf));
1748         if (buf[0])
1749                 ENICPMD_LOG(DEBUG, "         Inner: %s", buf);
1750 }
1751
1752 /* Debug function to dump internal NIC flow structures. */
1753 static void
1754 enic_fm_dump_tcam_entry(const struct fm_tcam_match_entry *fm_match,
1755                         const struct fm_action *fm_action,
1756                         uint8_t ingress)
1757 {
1758         if (!rte_log_can_log(enic_pmd_logtype, RTE_LOG_DEBUG))
1759                 return;
1760         enic_fm_dump_tcam_match(fm_match, ingress);
1761         enic_fm_dump_tcam_actions(fm_action);
1762 }
1763
1764 static int
1765 enic_fm_flow_parse(struct enic_flowman *fm,
1766                    const struct rte_flow_attr *attrs,
1767                    const struct rte_flow_item pattern[],
1768                    const struct rte_flow_action actions[],
1769                    struct rte_flow_error *error)
1770 {
1771         const struct rte_flow_action *action;
1772         unsigned int ret;
1773         static const enum rte_flow_action_type *sa;
1774
1775         ENICPMD_FUNC_TRACE();
1776         ret = 0;
1777         if (!pattern) {
1778                 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
1779                                    NULL, "no pattern specified");
1780                 return -rte_errno;
1781         }
1782
1783         if (!actions) {
1784                 rte_flow_error_set(error, EINVAL,
1785                                    RTE_FLOW_ERROR_TYPE_ACTION_NUM,
1786                                    NULL, "no action specified");
1787                 return -rte_errno;
1788         }
1789
1790         if (attrs) {
1791                 if (attrs->group != FM_TCAM_RTE_GROUP && attrs->priority) {
1792                         rte_flow_error_set(error, ENOTSUP,
1793                                            RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1794                                            NULL,
1795                                            "priorities are not supported for non-default (0) groups");
1796                         return -rte_errno;
1797                 } else if (!fm->owner_enic->switchdev_mode && attrs->transfer) {
1798                         rte_flow_error_set(error, ENOTSUP,
1799                                            RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
1800                                            NULL,
1801                                            "transfer is not supported");
1802                         return -rte_errno;
1803                 } else if (attrs->ingress && attrs->egress) {
1804                         rte_flow_error_set(error, ENOTSUP,
1805                                            RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1806                                            NULL,
1807                                            "bidirectional rules not supported");
1808                         return -rte_errno;
1809                 }
1810
1811         } else {
1812                 rte_flow_error_set(error, EINVAL,
1813                                    RTE_FLOW_ERROR_TYPE_ATTR,
1814                                    NULL, "no attribute specified");
1815                 return -rte_errno;
1816         }
1817
1818         /* Verify Actions. */
1819         sa = (attrs->ingress) ? enic_fm_supported_ig_actions :
1820              enic_fm_supported_eg_actions;
1821         for (action = &actions[0]; action->type != RTE_FLOW_ACTION_TYPE_END;
1822              action++) {
1823                 if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
1824                         continue;
1825                 else if (!enic_fm_match_action(action, sa))
1826                         break;
1827         }
1828         if (action->type != RTE_FLOW_ACTION_TYPE_END) {
1829                 rte_flow_error_set(error, EPERM, RTE_FLOW_ERROR_TYPE_ACTION,
1830                                    action, "invalid action");
1831                 return -rte_errno;
1832         }
1833         ret = enic_fm_copy_entry(fm, pattern, error);
1834         if (ret)
1835                 return ret;
1836         ret = enic_fm_copy_action(fm, actions, attrs->ingress, error);
1837         return ret;
1838 }
1839
1840 static void
1841 enic_fm_counter_free(struct enic_flowman *fm, struct enic_fm_flow *fm_flow)
1842 {
1843         if (!fm_flow->counter_valid)
1844                 return;
1845         SLIST_INSERT_HEAD(&fm->counters, fm_flow->counter, next);
1846         fm_flow->counter_valid = false;
1847 }
1848
1849 static int
1850 enic_fm_more_counters(struct enic_flowman *fm)
1851 {
1852         struct enic_fm_counter *new_stack;
1853         struct enic_fm_counter *ctrs;
1854         int i, rc;
1855         uint64_t args[2];
1856
1857         ENICPMD_FUNC_TRACE();
1858         new_stack = rte_realloc(fm->counter_stack, (fm->counters_alloced +
1859                                 FM_COUNTERS_EXPAND) *
1860                                 sizeof(struct enic_fm_counter), 0);
1861         if (new_stack == NULL) {
1862                 ENICPMD_LOG(ERR, "cannot alloc counter memory");
1863                 return -ENOMEM;
1864         }
1865         fm->counter_stack = new_stack;
1866
1867         args[0] = FM_COUNTER_BRK;
1868         args[1] = fm->counters_alloced + FM_COUNTERS_EXPAND;
1869         rc = flowman_cmd(fm, args, 2);
1870         if (rc != 0) {
1871                 ENICPMD_LOG(ERR, "cannot alloc counters rc=%d", rc);
1872                 return rc;
1873         }
1874         ctrs = (struct enic_fm_counter *)fm->counter_stack +
1875                 fm->counters_alloced;
1876         for (i = 0; i < FM_COUNTERS_EXPAND; i++, ctrs++) {
1877                 ctrs->handle = fm->counters_alloced + i;
1878                 SLIST_INSERT_HEAD(&fm->counters, ctrs, next);
1879         }
1880         fm->counters_alloced += FM_COUNTERS_EXPAND;
1881         ENICPMD_LOG(DEBUG, "%u counters allocated, total: %u",
1882                     FM_COUNTERS_EXPAND, fm->counters_alloced);
1883         return 0;
1884 }
1885
1886 static int
1887 enic_fm_counter_zero(struct enic_flowman *fm, struct enic_fm_counter *c)
1888 {
1889         uint64_t args[3];
1890         int ret;
1891
1892         ENICPMD_FUNC_TRACE();
1893         args[0] = FM_COUNTER_QUERY;
1894         args[1] = c->handle;
1895         args[2] = 1; /* clear */
1896         ret = flowman_cmd(fm, args, 3);
1897         if (ret) {
1898                 ENICPMD_LOG(ERR, "counter init: rc=%d handle=0x%x",
1899                             ret, c->handle);
1900                 return ret;
1901         }
1902         return 0;
1903 }
1904
1905 static int
1906 enic_fm_counter_alloc(struct enic_flowman *fm, struct rte_flow_error *error,
1907                       struct enic_fm_counter **ctr)
1908 {
1909         struct enic_fm_counter *c;
1910         int ret;
1911
1912         ENICPMD_FUNC_TRACE();
1913         *ctr = NULL;
1914         if (SLIST_EMPTY(&fm->counters)) {
1915                 ret = enic_fm_more_counters(fm);
1916                 if (ret)
1917                         return rte_flow_error_set(error, -ret,
1918                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1919                                 NULL, "enic: out of counters");
1920         }
1921         c = SLIST_FIRST(&fm->counters);
1922         SLIST_REMOVE_HEAD(&fm->counters, next);
1923         *ctr = c;
1924         return 0;
1925 }
1926
1927 static int
1928 enic_fm_action_free(struct enic_flowman *fm, uint64_t handle)
1929 {
1930         uint64_t args[2];
1931         int rc;
1932
1933         ENICPMD_FUNC_TRACE();
1934         args[0] = FM_ACTION_FREE;
1935         args[1] = handle;
1936         rc = flowman_cmd(fm, args, 2);
1937         if (rc)
1938                 ENICPMD_LOG(ERR, "cannot free action: rc=%d handle=0x%" PRIx64,
1939                             rc, handle);
1940         return rc;
1941 }
1942
1943 static int
1944 enic_fm_entry_free(struct enic_flowman *fm, uint64_t handle)
1945 {
1946         uint64_t args[2];
1947         int rc;
1948
1949         ENICPMD_FUNC_TRACE();
1950         args[0] = FM_MATCH_ENTRY_REMOVE;
1951         args[1] = handle;
1952         rc = flowman_cmd(fm, args, 2);
1953         if (rc)
1954                 ENICPMD_LOG(ERR, "cannot free match entry: rc=%d"
1955                             " handle=0x%" PRIx64, rc, handle);
1956         return rc;
1957 }
1958
1959 static struct enic_fm_jump_flow *
1960 find_jump_flow(struct enic_flowman *fm, uint32_t group)
1961 {
1962         struct enic_fm_jump_flow *j;
1963
1964         ENICPMD_FUNC_TRACE();
1965         TAILQ_FOREACH(j, &fm->jump_list, list) {
1966                 if (j->group == group)
1967                         return j;
1968         }
1969         return NULL;
1970 }
1971
1972 static void
1973 remove_jump_flow(struct enic_flowman *fm, struct rte_flow *flow)
1974 {
1975         struct enic_fm_jump_flow *j;
1976
1977         ENICPMD_FUNC_TRACE();
1978         TAILQ_FOREACH(j, &fm->jump_list, list) {
1979                 if (j->flow == flow) {
1980                         TAILQ_REMOVE(&fm->jump_list, j, list);
1981                         free(j);
1982                         return;
1983                 }
1984         }
1985 }
1986
1987 static int
1988 save_jump_flow(struct enic_flowman *fm,
1989                struct rte_flow *flow,
1990                uint32_t group,
1991                struct fm_tcam_match_entry *match,
1992                struct fm_action *action)
1993 {
1994         struct enic_fm_jump_flow *j;
1995
1996         ENICPMD_FUNC_TRACE();
1997         j = calloc(1, sizeof(struct enic_fm_jump_flow));
1998         if (j == NULL)
1999                 return -ENOMEM;
2000         j->flow = flow;
2001         j->group = group;
2002         j->match = *match;
2003         j->action = *action;
2004         TAILQ_INSERT_HEAD(&fm->jump_list, j, list);
2005         ENICPMD_LOG(DEBUG, "saved jump flow: flow=%p group=%u", flow, group);
2006         return 0;
2007 }
2008
2009 static void
2010 __enic_fm_flow_free(struct enic_flowman *fm, struct enic_fm_flow *fm_flow)
2011 {
2012         if (fm_flow->entry_handle != FM_INVALID_HANDLE) {
2013                 enic_fm_entry_free(fm, fm_flow->entry_handle);
2014                 fm_flow->entry_handle = FM_INVALID_HANDLE;
2015         }
2016         if (fm_flow->action_handle != FM_INVALID_HANDLE) {
2017                 enic_fm_action_free(fm, fm_flow->action_handle);
2018                 fm_flow->action_handle = FM_INVALID_HANDLE;
2019         }
2020         enic_fm_counter_free(fm, fm_flow);
2021         if (fm_flow->fet) {
2022                 enic_fet_put(fm, fm_flow->fet);
2023                 fm_flow->fet = NULL;
2024         }
2025 }
2026
2027 static void
2028 enic_fm_flow_free(struct enic_flowman *fm, struct rte_flow *flow)
2029 {
2030         struct enic_fm_flow *steer = flow->fm->hairpin_steer_flow;
2031
2032         if (flow->fm->fet && flow->fm->fet->default_key)
2033                 remove_jump_flow(fm, flow);
2034         __enic_fm_flow_free(fm, flow->fm);
2035         if (steer) {
2036                 __enic_fm_flow_free(fm, steer);
2037                 free(steer);
2038         }
2039         free(flow->fm);
2040         free(flow);
2041 }
2042
2043 static int
2044 enic_fm_add_tcam_entry(struct enic_flowman *fm,
2045                        struct fm_tcam_match_entry *match_in,
2046                        uint64_t *entry_handle,
2047                        uint8_t ingress,
2048                        struct rte_flow_error *error)
2049 {
2050         struct fm_tcam_match_entry *ftm;
2051         uint64_t args[3];
2052         int ret;
2053
2054         ENICPMD_FUNC_TRACE();
2055         /* Copy entry to the command buffer */
2056         ftm = &fm->cmd.va->fm_tcam_match_entry;
2057         memcpy(ftm, match_in, sizeof(*ftm));
2058         /* Add TCAM entry */
2059         args[0] = FM_TCAM_ENTRY_INSTALL;
2060         args[1] = ingress ? fm->ig_tcam_hndl : fm->eg_tcam_hndl;
2061         args[2] = fm->cmd.pa;
2062         ret = flowman_cmd(fm, args, 3);
2063         if (ret != 0) {
2064                 ENICPMD_LOG(ERR, "cannot add %s TCAM entry: rc=%d",
2065                             ingress ? "ingress" : "egress", ret);
2066                 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2067                         NULL, "enic: devcmd(tcam-entry-install)");
2068                 return ret;
2069         }
2070         ENICPMD_LOG(DEBUG, "installed %s TCAM entry: handle=0x%" PRIx64,
2071                     ingress ? "ingress" : "egress", (uint64_t)args[0]);
2072         *entry_handle = args[0];
2073         return 0;
2074 }
2075
2076 static int
2077 enic_fm_add_exact_entry(struct enic_flowman *fm,
2078                         struct fm_tcam_match_entry *match_in,
2079                         uint64_t *entry_handle,
2080                         struct enic_fm_fet *fet,
2081                         struct rte_flow_error *error)
2082 {
2083         struct fm_exact_match_entry *fem;
2084         uint64_t args[3];
2085         int ret;
2086
2087         ENICPMD_FUNC_TRACE();
2088         /* The new entry must have the table's key */
2089         if (memcmp(fet->key.fk_hdrset, match_in->ftm_mask.fk_hdrset,
2090                    sizeof(struct fm_header_set) * FM_HDRSET_MAX)) {
2091                 return rte_flow_error_set(error, EINVAL,
2092                         RTE_FLOW_ERROR_TYPE_ITEM, NULL,
2093                         "enic: key does not match group's key");
2094         }
2095
2096         /* Copy entry to the command buffer */
2097         fem = &fm->cmd.va->fm_exact_match_entry;
2098         /*
2099          * Translate TCAM entry to exact entry. As is only need to drop
2100          * position and mask. The mask is part of the exact match table.
2101          * Position (aka priority) is not supported in the exact match table.
2102          */
2103         fem->fem_data = match_in->ftm_data;
2104         fem->fem_flags = match_in->ftm_flags;
2105         fem->fem_action = match_in->ftm_action;
2106         fem->fem_counter = match_in->ftm_counter;
2107
2108         /* Add exact entry */
2109         args[0] = FM_EXACT_ENTRY_INSTALL;
2110         args[1] = fet->handle;
2111         args[2] = fm->cmd.pa;
2112         ret = flowman_cmd(fm, args, 3);
2113         if (ret != 0) {
2114                 ENICPMD_LOG(ERR, "cannot add %s exact entry: group=%u",
2115                             fet->ingress ? "ingress" : "egress", fet->group);
2116                 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2117                         NULL, "enic: devcmd(exact-entry-install)");
2118                 return ret;
2119         }
2120         ENICPMD_LOG(DEBUG, "installed %s exact entry: group=%u"
2121                     " handle=0x%" PRIx64,
2122                     fet->ingress ? "ingress" : "egress", fet->group,
2123                     (uint64_t)args[0]);
2124         *entry_handle = args[0];
2125         return 0;
2126 }
2127
2128 /* Push match-action to the NIC. */
2129 static int
2130 __enic_fm_flow_add_entry(struct enic_flowman *fm,
2131                          struct enic_fm_flow *fm_flow,
2132                          struct fm_tcam_match_entry *match_in,
2133                          struct fm_action *action_in,
2134                          uint32_t group,
2135                          uint8_t ingress,
2136                          struct rte_flow_error *error)
2137 {
2138         struct enic_fm_counter *ctr;
2139         struct fm_action *fma;
2140         uint64_t action_h;
2141         uint64_t entry_h;
2142         uint64_t args[3];
2143         int ret;
2144
2145         ENICPMD_FUNC_TRACE();
2146         /* Allocate action. */
2147         fma = &fm->cmd.va->fm_action;
2148         memcpy(fma, action_in, sizeof(*fma));
2149         args[0] = FM_ACTION_ALLOC;
2150         args[1] = fm->cmd.pa;
2151         ret = flowman_cmd(fm, args, 2);
2152         if (ret != 0) {
2153                 ENICPMD_LOG(ERR, "allocating TCAM table action rc=%d", ret);
2154                 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2155                         NULL, "enic: devcmd(action-alloc)");
2156                 return ret;
2157         }
2158         action_h = args[0];
2159         fm_flow->action_handle = action_h;
2160         match_in->ftm_action = action_h;
2161         ENICPMD_LOG(DEBUG, "action allocated: handle=0x%" PRIx64, action_h);
2162
2163         /* Allocate counter if requested. */
2164         if (match_in->ftm_flags & FMEF_COUNTER) {
2165                 ret = enic_fm_counter_alloc(fm, error, &ctr);
2166                 if (ret) /* error has been filled in */
2167                         return ret;
2168                 fm_flow->counter_valid = true;
2169                 fm_flow->counter = ctr;
2170                 match_in->ftm_counter = ctr->handle;
2171         }
2172
2173         /*
2174          * Get the group's table (either TCAM or exact match table) and
2175          * add entry to it. If we use the exact match table, the handler
2176          * will translate the TCAM entry (match_in) to the appropriate
2177          * exact match entry and use that instead.
2178          */
2179         entry_h = FM_INVALID_HANDLE;
2180         if (group == FM_TCAM_RTE_GROUP) {
2181                 ret = enic_fm_add_tcam_entry(fm, match_in, &entry_h, ingress,
2182                                              error);
2183                 if (ret)
2184                         return ret;
2185                 /* Jump action might have a ref to fet */
2186                 fm_flow->fet = fm->fet;
2187                 fm->fet = NULL;
2188         } else {
2189                 struct enic_fm_fet *fet = NULL;
2190
2191                 ret = enic_fet_get(fm, group, ingress,
2192                                    &match_in->ftm_mask, &fet, error);
2193                 if (ret)
2194                         return ret;
2195                 fm_flow->fet = fet;
2196                 ret = enic_fm_add_exact_entry(fm, match_in, &entry_h, fet,
2197                                               error);
2198                 if (ret)
2199                         return ret;
2200         }
2201         /* Clear counter after adding entry, as it requires in-use counter */
2202         if (fm_flow->counter_valid) {
2203                 ret = enic_fm_counter_zero(fm, fm_flow->counter);
2204                 if (ret)
2205                         return ret;
2206         }
2207         fm_flow->entry_handle = entry_h;
2208         return 0;
2209 }
2210
2211 /* Push match-action to the NIC. */
2212 static struct rte_flow *
2213 enic_fm_flow_add_entry(struct enic_flowman *fm,
2214                        struct fm_tcam_match_entry *match_in,
2215                        struct fm_action *action_in,
2216                        const struct rte_flow_attr *attrs,
2217                        struct rte_flow_error *error)
2218 {
2219         struct enic_fm_flow *fm_flow;
2220         struct rte_flow *flow;
2221
2222         ENICPMD_FUNC_TRACE();
2223         match_in->ftm_position = attrs->priority;
2224         enic_fm_dump_tcam_entry(match_in, action_in, attrs->ingress);
2225         flow = calloc(1, sizeof(*flow));
2226         fm_flow = calloc(1, sizeof(*fm_flow));
2227         if (flow == NULL || fm_flow == NULL) {
2228                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
2229                         NULL, "enic: cannot allocate rte_flow");
2230                 free(flow);
2231                 free(fm_flow);
2232                 return NULL;
2233         }
2234         flow->fm = fm_flow;
2235         fm_flow->action_handle = FM_INVALID_HANDLE;
2236         fm_flow->entry_handle = FM_INVALID_HANDLE;
2237         if (__enic_fm_flow_add_entry(fm, fm_flow, match_in, action_in,
2238                                      attrs->group, attrs->ingress, error)) {
2239                 enic_fm_flow_free(fm, flow);
2240                 return NULL;
2241         }
2242         return flow;
2243 }
2244
2245 static void
2246 convert_jump_flows(struct enic_flowman *fm, struct enic_fm_fet *fet,
2247                    struct rte_flow_error *error)
2248 {
2249         struct enic_fm_flow *fm_flow;
2250         struct enic_fm_jump_flow *j;
2251         struct fm_action *fma;
2252         uint32_t group;
2253
2254         ENICPMD_FUNC_TRACE();
2255         /*
2256          * Find the saved flows that should jump to the new table (fet).
2257          * Then delete the old TCAM entry that jumps to the default table,
2258          * and add a new one that jumps to the new table.
2259          */
2260         group = fet->group;
2261         j = find_jump_flow(fm, group);
2262         while (j) {
2263                 ENICPMD_LOG(DEBUG, "convert jump flow: flow=%p group=%u",
2264                             j->flow, group);
2265                 /* Delete old entry */
2266                 fm_flow = j->flow->fm;
2267                 __enic_fm_flow_free(fm, fm_flow);
2268
2269                 /* Add new entry */
2270                 fma = &j->action;
2271                 fma->fma_action_ops[0].exact.handle = fet->handle;
2272                 if (__enic_fm_flow_add_entry(fm, fm_flow, &j->match, fma,
2273                         FM_TCAM_RTE_GROUP, fet->ingress, error)) {
2274                         /* Cannot roll back changes at the moment */
2275                         ENICPMD_LOG(ERR, "cannot convert jump flow: flow=%p",
2276                                     j->flow);
2277                 } else {
2278                         fm_flow->fet = fet;
2279                         fet->ref++;
2280                         ENICPMD_LOG(DEBUG, "convert ok: group=%u ref=%u",
2281                                     fet->group, fet->ref);
2282                 }
2283
2284                 TAILQ_REMOVE(&fm->jump_list, j, list);
2285                 free(j);
2286                 j = find_jump_flow(fm, group);
2287         }
2288 }
2289
2290 static int
2291 add_hairpin_steer(struct enic_flowman *fm, struct rte_flow *flow,
2292                   struct rte_flow_error *error)
2293 {
2294         struct fm_tcam_match_entry *fm_tcam_entry;
2295         struct enic_fm_flow *fm_flow;
2296         struct fm_action *fm_action;
2297         struct fm_action_op fm_op;
2298         int ret;
2299
2300         ENICPMD_FUNC_TRACE();
2301         fm_flow = calloc(1, sizeof(*fm_flow));
2302         if (fm_flow == NULL) {
2303                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
2304                         NULL, "enic: cannot allocate rte_flow");
2305                 return -ENOMEM;
2306         }
2307         /* Original egress hairpin flow */
2308         fm_tcam_entry = &fm->tcam_entry;
2309         fm_action = &fm->action;
2310         /* Use the match pattern of the egress flow as is, without counters */
2311         fm_tcam_entry->ftm_flags &= ~FMEF_COUNTER;
2312         /* The only action is steer to vnic */
2313         fm->action_op_count = 0;
2314         memset(fm_action, 0, sizeof(*fm_action));
2315         memset(&fm_op, 0, sizeof(fm_op));
2316         /* Always to queue 0 for now */
2317         fm_op.fa_op = FMOP_RQ_STEER;
2318         fm_op.rq_steer.rq_index = 0;
2319         fm_op.rq_steer.vnic_handle = fm->hairpin_steer_vnic_h;
2320         ret = enic_fm_append_action_op(fm, &fm_op, error);
2321         if (ret)
2322                 goto error_with_flow;
2323         ENICPMD_LOG(DEBUG, "add steer op");
2324         /* Add required END */
2325         memset(&fm_op, 0, sizeof(fm_op));
2326         fm_op.fa_op = FMOP_END;
2327         ret = enic_fm_append_action_op(fm, &fm_op, error);
2328         if (ret)
2329                 goto error_with_flow;
2330         /* Add the ingress flow */
2331         fm_flow->action_handle = FM_INVALID_HANDLE;
2332         fm_flow->entry_handle = FM_INVALID_HANDLE;
2333         ret = __enic_fm_flow_add_entry(fm, fm_flow, fm_tcam_entry, fm_action,
2334                                        FM_TCAM_RTE_GROUP, 1 /* ingress */, error);
2335         if (ret) {
2336                 ENICPMD_LOG(ERR, "cannot add hairpin-steer flow");
2337                 goto error_with_flow;
2338         }
2339         /* The new flow is now the egress flow's paired flow */
2340         flow->fm->hairpin_steer_flow = fm_flow;
2341         return 0;
2342
2343 error_with_flow:
2344         free(fm_flow);
2345         return ret;
2346 }
2347
2348 static void
2349 enic_fm_open_scratch(struct enic_flowman *fm)
2350 {
2351         fm->action_op_count = 0;
2352         fm->fet = NULL;
2353         fm->need_hairpin_steer = 0;
2354         fm->hairpin_steer_vnic_h = 0;
2355         memset(&fm->tcam_entry, 0, sizeof(fm->tcam_entry));
2356         memset(&fm->action, 0, sizeof(fm->action));
2357 }
2358
2359 static void
2360 enic_fm_close_scratch(struct enic_flowman *fm)
2361 {
2362         if (fm->fet) {
2363                 enic_fet_put(fm, fm->fet);
2364                 fm->fet = NULL;
2365         }
2366         fm->action_op_count = 0;
2367 }
2368
2369 static int
2370 enic_fm_flow_validate(struct rte_eth_dev *dev,
2371                       const struct rte_flow_attr *attrs,
2372                       const struct rte_flow_item pattern[],
2373                       const struct rte_flow_action actions[],
2374                       struct rte_flow_error *error)
2375 {
2376         struct fm_tcam_match_entry *fm_tcam_entry;
2377         struct fm_action *fm_action;
2378         struct enic_flowman *fm;
2379         int ret;
2380
2381         ENICPMD_FUNC_TRACE();
2382         fm = begin_fm(pmd_priv(dev));
2383         if (fm == NULL)
2384                 return -ENOTSUP;
2385         enic_fm_open_scratch(fm);
2386         ret = enic_fm_flow_parse(fm, attrs, pattern, actions, error);
2387         if (!ret) {
2388                 fm_tcam_entry = &fm->tcam_entry;
2389                 fm_action = &fm->action;
2390                 enic_fm_dump_tcam_entry(fm_tcam_entry, fm_action,
2391                                         attrs->ingress);
2392         }
2393         enic_fm_close_scratch(fm);
2394         end_fm(fm);
2395         return ret;
2396 }
2397
2398 static int
2399 enic_fm_flow_query_count(struct rte_eth_dev *dev,
2400                          struct rte_flow *flow, void *data,
2401                          struct rte_flow_error *error)
2402 {
2403         struct rte_flow_query_count *query;
2404         struct enic_fm_flow *fm_flow;
2405         struct enic_flowman *fm;
2406         uint64_t args[3];
2407         int rc;
2408
2409         ENICPMD_FUNC_TRACE();
2410         fm = begin_fm(pmd_priv(dev));
2411         query = data;
2412         fm_flow = flow->fm;
2413         if (!fm_flow->counter_valid) {
2414                 rc = rte_flow_error_set(error, ENOTSUP,
2415                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2416                         "enic: flow does not have counter");
2417                 goto exit;
2418         }
2419
2420         args[0] = FM_COUNTER_QUERY;
2421         args[1] = fm_flow->counter->handle;
2422         args[2] = query->reset;
2423         rc = flowman_cmd(fm, args, 3);
2424         if (rc) {
2425                 ENICPMD_LOG(ERR, "cannot query counter: rc=%d handle=0x%x",
2426                             rc, fm_flow->counter->handle);
2427                 goto exit;
2428         }
2429         query->hits_set = 1;
2430         query->hits = args[0];
2431         query->bytes_set = 1;
2432         query->bytes = args[1];
2433         rc = 0;
2434 exit:
2435         end_fm(fm);
2436         return rc;
2437 }
2438
2439 static int
2440 enic_fm_flow_query(struct rte_eth_dev *dev,
2441                    struct rte_flow *flow,
2442                    const struct rte_flow_action *actions,
2443                    void *data,
2444                    struct rte_flow_error *error)
2445 {
2446         int ret = 0;
2447
2448         ENICPMD_FUNC_TRACE();
2449         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
2450                 switch (actions->type) {
2451                 case RTE_FLOW_ACTION_TYPE_VOID:
2452                         break;
2453                 case RTE_FLOW_ACTION_TYPE_COUNT:
2454                         ret = enic_fm_flow_query_count(dev, flow, data, error);
2455                         break;
2456                 default:
2457                         return rte_flow_error_set(error, ENOTSUP,
2458                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2459                                                   actions,
2460                                                   "action not supported");
2461                 }
2462                 if (ret < 0)
2463                         return ret;
2464         }
2465         return 0;
2466 }
2467
2468 static struct rte_flow *
2469 enic_fm_flow_create(struct rte_eth_dev *dev,
2470                     const struct rte_flow_attr *attrs,
2471                     const struct rte_flow_item pattern[],
2472                     const struct rte_flow_action actions[],
2473                     struct rte_flow_error *error)
2474 {
2475         struct fm_tcam_match_entry *fm_tcam_entry;
2476         struct fm_action *fm_action;
2477         struct enic_flowman *fm;
2478         struct enic_fm_fet *fet;
2479         struct rte_flow *flow;
2480         struct enic *enic;
2481         int ret;
2482
2483         ENICPMD_FUNC_TRACE();
2484         enic = pmd_priv(dev);
2485         fm = begin_fm(enic);
2486         if (fm == NULL) {
2487                 rte_flow_error_set(error, ENOTSUP,
2488                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2489                         "flowman is not initialized");
2490                 return NULL;
2491         }
2492         enic_fm_open_scratch(fm);
2493         flow = NULL;
2494         ret = enic_fm_flow_parse(fm, attrs, pattern, actions, error);
2495         if (ret < 0)
2496                 goto error_with_scratch;
2497         fm_tcam_entry = &fm->tcam_entry;
2498         fm_action = &fm->action;
2499         flow = enic_fm_flow_add_entry(fm, fm_tcam_entry, fm_action,
2500                                       attrs, error);
2501         if (flow) {
2502                 /* Add ingress rule that pairs with hairpin rule */
2503                 if (fm->need_hairpin_steer) {
2504                         ret = add_hairpin_steer(fm, flow, error);
2505                         if (ret) {
2506                                 enic_fm_flow_free(fm, flow);
2507                                 flow = NULL;
2508                                 goto error_with_scratch;
2509                         }
2510                 }
2511                 LIST_INSERT_HEAD(&enic->flows, flow, next);
2512                 fet = flow->fm->fet;
2513                 if (fet && fet->default_key) {
2514                         /*
2515                          * Jump to non-existent group? Save the relevant info
2516                          * so we can convert this flow when that group
2517                          * materializes.
2518                          */
2519                         save_jump_flow(fm, flow, fet->group,
2520                                        fm_tcam_entry, fm_action);
2521                 } else if (fet && fet->ref == 1) {
2522                         /*
2523                          * A new table is created. Convert the saved flows
2524                          * that should jump to this group.
2525                          */
2526                         convert_jump_flows(fm, fet, error);
2527                 }
2528         }
2529
2530 error_with_scratch:
2531         enic_fm_close_scratch(fm);
2532         end_fm(fm);
2533         return flow;
2534 }
2535
2536 static int
2537 enic_fm_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
2538                      __rte_unused struct rte_flow_error *error)
2539 {
2540         struct enic *enic = pmd_priv(dev);
2541         struct enic_flowman *fm;
2542
2543         ENICPMD_FUNC_TRACE();
2544         fm = begin_fm(enic);
2545         if (fm == NULL)
2546                 return 0;
2547         LIST_REMOVE(flow, next);
2548         enic_fm_flow_free(fm, flow);
2549         end_fm(fm);
2550         return 0;
2551 }
2552
2553 static int
2554 enic_fm_flow_flush(struct rte_eth_dev *dev,
2555                    __rte_unused struct rte_flow_error *error)
2556 {
2557         LIST_HEAD(enic_flows, rte_flow) internal;
2558         struct enic_fm_flow *fm_flow;
2559         struct enic_flowman *fm;
2560         struct rte_flow *flow;
2561         struct enic *enic = pmd_priv(dev);
2562
2563         ENICPMD_FUNC_TRACE();
2564
2565         fm = begin_fm(enic);
2566         if (fm == NULL)
2567                 return 0;
2568         /* Destroy all non-internal flows */
2569         LIST_INIT(&internal);
2570         while (!LIST_EMPTY(&enic->flows)) {
2571                 flow = LIST_FIRST(&enic->flows);
2572                 fm_flow = flow->fm;
2573                 LIST_REMOVE(flow, next);
2574                 if (flow->internal) {
2575                         LIST_INSERT_HEAD(&internal, flow, next);
2576                         continue;
2577                 }
2578                 /*
2579                  * If tables are null, then vNIC is closing, and the firmware
2580                  * has already cleaned up flowman state. So do not try to free
2581                  * resources, as it only causes errors.
2582                  */
2583                 if (fm->ig_tcam_hndl == FM_INVALID_HANDLE) {
2584                         fm_flow->entry_handle = FM_INVALID_HANDLE;
2585                         fm_flow->action_handle = FM_INVALID_HANDLE;
2586                         fm_flow->fet = NULL;
2587                 }
2588                 enic_fm_flow_free(fm, flow);
2589         }
2590         while (!LIST_EMPTY(&internal)) {
2591                 flow = LIST_FIRST(&internal);
2592                 LIST_REMOVE(flow, next);
2593                 LIST_INSERT_HEAD(&enic->flows, flow, next);
2594         }
2595         end_fm(fm);
2596         return 0;
2597 }
2598
2599 static int
2600 enic_fm_tbl_free(struct enic_flowman *fm, uint64_t handle)
2601 {
2602         uint64_t args[2];
2603         int rc;
2604
2605         args[0] = FM_MATCH_TABLE_FREE;
2606         args[1] = handle;
2607         rc = flowman_cmd(fm, args, 2);
2608         if (rc)
2609                 ENICPMD_LOG(ERR, "cannot free table: rc=%d handle=0x%" PRIx64,
2610                             rc, handle);
2611         return rc;
2612 }
2613
2614 static int
2615 enic_fm_tcam_tbl_alloc(struct enic_flowman *fm, uint32_t direction,
2616                         uint32_t max_entries, uint64_t *handle)
2617 {
2618         struct fm_tcam_match_table *tcam_tbl;
2619         uint64_t args[2];
2620         int rc;
2621
2622         ENICPMD_FUNC_TRACE();
2623         tcam_tbl = &fm->cmd.va->fm_tcam_match_table;
2624         tcam_tbl->ftt_direction = direction;
2625         tcam_tbl->ftt_stage = FM_STAGE_LAST;
2626         tcam_tbl->ftt_max_entries = max_entries;
2627         args[0] = FM_TCAM_TABLE_ALLOC;
2628         args[1] = fm->cmd.pa;
2629         rc = flowman_cmd(fm, args, 2);
2630         if (rc) {
2631                 ENICPMD_LOG(ERR, "cannot alloc %s TCAM table: rc=%d",
2632                             (direction == FM_INGRESS) ? "IG" : "EG", rc);
2633                 return rc;
2634         }
2635         *handle = args[0];
2636         ENICPMD_LOG(DEBUG, "%s TCAM table allocated, handle=0x%" PRIx64,
2637                     (direction == FM_INGRESS) ? "IG" : "EG", *handle);
2638         return 0;
2639 }
2640
2641 static int
2642 enic_fm_init_counters(struct enic_flowman *fm)
2643 {
2644         ENICPMD_FUNC_TRACE();
2645         SLIST_INIT(&fm->counters);
2646         return enic_fm_more_counters(fm);
2647 }
2648
2649 static void
2650 enic_fm_free_all_counters(struct enic_flowman *fm)
2651 {
2652         uint64_t args[2];
2653         int rc;
2654
2655         args[0] = FM_COUNTER_BRK;
2656         args[1] = 0;
2657         rc = flowman_cmd(fm, args, 2);
2658         if (rc != 0)
2659                 ENICPMD_LOG(ERR, "cannot free counters: rc=%d", rc);
2660         rte_free(fm->counter_stack);
2661 }
2662
2663 static int
2664 enic_fm_alloc_tcam_tables(struct enic_flowman *fm)
2665 {
2666         int rc;
2667
2668         ENICPMD_FUNC_TRACE();
2669         rc = enic_fm_tcam_tbl_alloc(fm, FM_INGRESS, FM_MAX_TCAM_TABLE_SIZE,
2670                                     &fm->ig_tcam_hndl);
2671         if (rc)
2672                 return rc;
2673         rc = enic_fm_tcam_tbl_alloc(fm, FM_EGRESS, FM_MAX_TCAM_TABLE_SIZE,
2674                                     &fm->eg_tcam_hndl);
2675         return rc;
2676 }
2677
2678 static void
2679 enic_fm_free_tcam_tables(struct enic_flowman *fm)
2680 {
2681         ENICPMD_FUNC_TRACE();
2682         if (fm->ig_tcam_hndl) {
2683                 ENICPMD_LOG(DEBUG, "free IG TCAM table handle=0x%" PRIx64,
2684                             fm->ig_tcam_hndl);
2685                 enic_fm_tbl_free(fm, fm->ig_tcam_hndl);
2686                 fm->ig_tcam_hndl = FM_INVALID_HANDLE;
2687         }
2688         if (fm->eg_tcam_hndl) {
2689                 ENICPMD_LOG(DEBUG, "free EG TCAM table handle=0x%" PRIx64,
2690                             fm->eg_tcam_hndl);
2691                 enic_fm_tbl_free(fm, fm->eg_tcam_hndl);
2692                 fm->eg_tcam_hndl = FM_INVALID_HANDLE;
2693         }
2694 }
2695
2696 int
2697 enic_fm_init(struct enic *enic)
2698 {
2699         const struct rte_pci_addr *addr;
2700         struct enic_flowman *fm;
2701         uint8_t name[RTE_MEMZONE_NAMESIZE];
2702         int rc;
2703
2704         if (enic->flow_filter_mode != FILTER_FLOWMAN)
2705                 return 0;
2706         ENICPMD_FUNC_TRACE();
2707         /* Get vnic handle and save for port-id action */
2708         if (enic_is_vf_rep(enic))
2709                 addr = &VF_ENIC_TO_VF_REP(enic)->bdf;
2710         else
2711                 addr = &RTE_ETH_DEV_TO_PCI(enic->rte_dev)->addr;
2712         rc = enic_fm_find_vnic(enic, addr, &enic->fm_vnic_handle);
2713         if (rc) {
2714                 ENICPMD_LOG(ERR, "cannot find vnic handle for %x:%x:%x",
2715                             addr->bus, addr->devid, addr->function);
2716                 return rc;
2717         }
2718         /* Save UIF for egport action */
2719         enic->fm_vnic_uif = vnic_dev_uif(enic->vdev);
2720         ENICPMD_LOG(DEBUG, "uif %u", enic->fm_vnic_uif);
2721         /* Nothing else to do for representor. It will share the PF flowman */
2722         if (enic_is_vf_rep(enic))
2723                 return 0;
2724         fm = calloc(1, sizeof(*fm));
2725         if (fm == NULL) {
2726                 ENICPMD_LOG(ERR, "cannot alloc flowman struct");
2727                 return -ENOMEM;
2728         }
2729         fm->owner_enic = enic;
2730         rte_spinlock_init(&fm->lock);
2731         TAILQ_INIT(&fm->fet_list);
2732         TAILQ_INIT(&fm->jump_list);
2733         /* Allocate host memory for flowman commands */
2734         snprintf((char *)name, sizeof(name), "fm-cmd-%s", enic->bdf_name);
2735         fm->cmd.va = enic_alloc_consistent(enic,
2736                 sizeof(union enic_flowman_cmd_mem), &fm->cmd.pa, name);
2737         if (!fm->cmd.va) {
2738                 ENICPMD_LOG(ERR, "cannot allocate flowman command memory");
2739                 rc = -ENOMEM;
2740                 goto error_fm;
2741         }
2742         /* Allocate TCAM tables upfront as they are the main tables */
2743         rc = enic_fm_alloc_tcam_tables(fm);
2744         if (rc) {
2745                 ENICPMD_LOG(ERR, "cannot alloc TCAM tables");
2746                 goto error_cmd;
2747         }
2748         /* Then a number of counters */
2749         rc = enic_fm_init_counters(fm);
2750         if (rc) {
2751                 ENICPMD_LOG(ERR, "cannot alloc counters");
2752                 goto error_tables;
2753         }
2754         /*
2755          * One default exact match table for each direction. We hold onto
2756          * it until close.
2757          */
2758         rc = enic_fet_alloc(fm, 1, NULL, 128, &fm->default_ig_fet);
2759         if (rc) {
2760                 ENICPMD_LOG(ERR, "cannot alloc default IG exact match table");
2761                 goto error_counters;
2762         }
2763         fm->default_ig_fet->ref = 1;
2764         rc = enic_fet_alloc(fm, 0, NULL, 128, &fm->default_eg_fet);
2765         if (rc) {
2766                 ENICPMD_LOG(ERR, "cannot alloc default EG exact match table");
2767                 goto error_ig_fet;
2768         }
2769         fm->default_eg_fet->ref = 1;
2770         fm->vf_rep_tag = FM_VF_REP_TAG;
2771         enic->fm = fm;
2772         return 0;
2773
2774 error_ig_fet:
2775         enic_fet_free(fm, fm->default_ig_fet);
2776 error_counters:
2777         enic_fm_free_all_counters(fm);
2778 error_tables:
2779         enic_fm_free_tcam_tables(fm);
2780 error_cmd:
2781         enic_free_consistent(enic, sizeof(union enic_flowman_cmd_mem),
2782                 fm->cmd.va, fm->cmd.pa);
2783 error_fm:
2784         free(fm);
2785         return rc;
2786 }
2787
2788 void
2789 enic_fm_destroy(struct enic *enic)
2790 {
2791         struct enic_flowman *fm;
2792         struct enic_fm_fet *fet;
2793
2794         ENICPMD_FUNC_TRACE();
2795         if (enic_is_vf_rep(enic)) {
2796                 delete_rep_flows(enic);
2797                 return;
2798         }
2799         if (enic->fm == NULL)
2800                 return;
2801         fm = enic->fm;
2802         enic_fet_free(fm, fm->default_eg_fet);
2803         enic_fet_free(fm, fm->default_ig_fet);
2804         /* Free all exact match tables still open */
2805         while (!TAILQ_EMPTY(&fm->fet_list)) {
2806                 fet = TAILQ_FIRST(&fm->fet_list);
2807                 enic_fet_free(fm, fet);
2808         }
2809         enic_fm_free_tcam_tables(fm);
2810         enic_fm_free_all_counters(fm);
2811         enic_free_consistent(enic, sizeof(union enic_flowman_cmd_mem),
2812                 fm->cmd.va, fm->cmd.pa);
2813         fm->cmd.va = NULL;
2814         free(fm);
2815         enic->fm = NULL;
2816 }
2817
2818 int
2819 enic_fm_allocate_switch_domain(struct enic *pf)
2820 {
2821         const struct rte_pci_addr *cur_a, *prev_a;
2822         struct rte_eth_dev *dev;
2823         struct enic *cur, *prev;
2824         uint16_t domain_id;
2825         uint64_t vnic_h;
2826         uint16_t pid;
2827         int ret;
2828
2829         ENICPMD_FUNC_TRACE();
2830         if (enic_is_vf_rep(pf))
2831                 return -EINVAL;
2832         cur = pf;
2833         cur_a = &RTE_ETH_DEV_TO_PCI(cur->rte_dev)->addr;
2834         /* Go through ports and find another PF that is on the same adapter */
2835         RTE_ETH_FOREACH_DEV(pid) {
2836                 dev = &rte_eth_devices[pid];
2837                 if (!dev_is_enic(dev))
2838                         continue;
2839                 if (dev->data->dev_flags & RTE_ETH_DEV_REPRESENTOR)
2840                         continue;
2841                 if (dev == cur->rte_dev)
2842                         continue;
2843                 /* dev is another PF. Is it on the same adapter? */
2844                 prev = pmd_priv(dev);
2845                 prev_a = &RTE_ETH_DEV_TO_PCI(dev)->addr;
2846                 if (!enic_fm_find_vnic(cur, prev_a, &vnic_h)) {
2847                         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",
2848                                 cur->rte_dev->data->port_id,
2849                                 cur_a->bus, cur_a->devid, cur_a->function,
2850                                 dev->data->port_id,
2851                                 prev_a->bus, prev_a->devid, prev_a->function,
2852                                 prev->switch_domain_id);
2853                         cur->switch_domain_id = prev->switch_domain_id;
2854                         return 0;
2855                 }
2856         }
2857         ret = rte_eth_switch_domain_alloc(&domain_id);
2858         if (ret) {
2859                 ENICPMD_LOG(WARNING, "failed to allocate switch domain for device %d",
2860                             ret);
2861         }
2862         cur->switch_domain_id = domain_id;
2863         ENICPMD_LOG(DEBUG, "Port %u (PF BDF %x:%x:%x) is the 1st PF on the VIC. Allocated switch domain id %u",
2864                     cur->rte_dev->data->port_id,
2865                     cur_a->bus, cur_a->devid, cur_a->function,
2866                     domain_id);
2867         return ret;
2868 }
2869
2870 const struct rte_flow_ops enic_fm_flow_ops = {
2871         .validate = enic_fm_flow_validate,
2872         .create = enic_fm_flow_create,
2873         .destroy = enic_fm_flow_destroy,
2874         .flush = enic_fm_flow_flush,
2875         .query = enic_fm_flow_query,
2876 };
2877
2878 /* Add a high priority flow that loops representor packets to VF */
2879 int
2880 enic_fm_add_rep2vf_flow(struct enic_vf_representor *vf)
2881 {
2882         struct fm_tcam_match_entry *fm_tcam_entry;
2883         struct rte_flow *flow0, *flow1;
2884         struct fm_action *fm_action;
2885         struct rte_flow_error error;
2886         struct rte_flow_attr attrs;
2887         struct fm_action_op fm_op;
2888         struct enic_flowman *fm;
2889         struct enic *pf;
2890         uint8_t tag;
2891
2892         pf = vf->pf;
2893         fm = pf->fm;
2894         tag = fm->vf_rep_tag;
2895         enic_fm_open_scratch(fm);
2896         fm_tcam_entry = &fm->tcam_entry;
2897         fm_action = &fm->action;
2898         /* Egress rule: match WQ ID and tag+hairpin */
2899         fm_tcam_entry->ftm_data.fk_wq_id = vf->pf_wq_idx;
2900         fm_tcam_entry->ftm_mask.fk_wq_id = 0xffff;
2901         fm_tcam_entry->ftm_flags |= FMEF_COUNTER;
2902         memset(&fm_op, 0, sizeof(fm_op));
2903         fm_op.fa_op = FMOP_TAG;
2904         fm_op.tag.tag = tag;
2905         enic_fm_append_action_op(fm, &fm_op, &error);
2906         memset(&fm_op, 0, sizeof(fm_op));
2907         fm_op.fa_op = FMOP_EG_HAIRPIN;
2908         enic_fm_append_action_op(fm, &fm_op, &error);
2909         memset(&fm_op, 0, sizeof(fm_op));
2910         fm_op.fa_op = FMOP_END;
2911         enic_fm_append_action_op(fm, &fm_op, &error);
2912         attrs.group = 0;
2913         attrs.ingress = 0;
2914         attrs.egress = 1;
2915         attrs.priority = FM_HIGHEST_PRIORITY;
2916         flow0 = enic_fm_flow_add_entry(fm, fm_tcam_entry, fm_action,
2917                                        &attrs, &error);
2918         enic_fm_close_scratch(fm);
2919         if (flow0 == NULL) {
2920                 ENICPMD_LOG(ERR, "Cannot create flow 0 for representor->VF");
2921                 return -EINVAL;
2922         }
2923         LIST_INSERT_HEAD(&pf->flows, flow0, next);
2924         /* Make this flow internal, so the user app cannot delete it */
2925         flow0->internal = 1;
2926         ENICPMD_LOG(DEBUG, "representor->VF %d flow created: wq %d -> tag %d hairpin",
2927                     vf->vf_id, vf->pf_wq_idx, tag);
2928
2929         /* Ingress: steer hairpinned to VF RQ 0 */
2930         enic_fm_open_scratch(fm);
2931         fm_tcam_entry->ftm_flags |= FMEF_COUNTER;
2932         fm_tcam_entry->ftm_data.fk_hdrset[0].fk_metadata |= FKM_EG_HAIRPINNED;
2933         fm_tcam_entry->ftm_mask.fk_hdrset[0].fk_metadata |= FKM_EG_HAIRPINNED;
2934         fm_tcam_entry->ftm_data.fk_packet_tag = tag;
2935         fm_tcam_entry->ftm_mask.fk_packet_tag = 0xff;
2936         memset(&fm_op, 0, sizeof(fm_op));
2937         fm_op.fa_op = FMOP_RQ_STEER;
2938         fm_op.rq_steer.rq_index = 0;
2939         fm_op.rq_steer.vnic_handle = vf->enic.fm_vnic_handle;
2940         enic_fm_append_action_op(fm, &fm_op, &error);
2941         memset(&fm_op, 0, sizeof(fm_op));
2942         fm_op.fa_op = FMOP_END;
2943         enic_fm_append_action_op(fm, &fm_op, &error);
2944         attrs.group = 0;
2945         attrs.ingress = 1;
2946         attrs.egress = 0;
2947         attrs.priority = FM_HIGHEST_PRIORITY;
2948         flow1 = enic_fm_flow_add_entry(fm, fm_tcam_entry, fm_action,
2949                                        &attrs, &error);
2950         enic_fm_close_scratch(fm);
2951         if (flow1 == NULL) {
2952                 ENICPMD_LOG(ERR, "Cannot create flow 1 for representor->VF");
2953                 enic_fm_flow_destroy(pf->rte_dev, flow0, &error);
2954                 return -EINVAL;
2955         }
2956         LIST_INSERT_HEAD(&pf->flows, flow1, next);
2957         flow1->internal = 1;
2958         ENICPMD_LOG(DEBUG, "representor->VF %d flow created: tag %d hairpinned -> VF RQ %d",
2959                     vf->vf_id, tag, fm_op.rq_steer.rq_index);
2960         vf->rep2vf_flow[0] = flow0;
2961         vf->rep2vf_flow[1] = flow1;
2962         /* Done with this tag, use a different one next time */
2963         fm->vf_rep_tag++;
2964         return 0;
2965 }
2966
2967 /*
2968  * Add a low priority flow that matches all packets from VF and loops them
2969  * back to the representor.
2970  */
2971 int
2972 enic_fm_add_vf2rep_flow(struct enic_vf_representor *vf)
2973 {
2974         struct fm_tcam_match_entry *fm_tcam_entry;
2975         struct rte_flow *flow0, *flow1;
2976         struct fm_action *fm_action;
2977         struct rte_flow_error error;
2978         struct rte_flow_attr attrs;
2979         struct fm_action_op fm_op;
2980         struct enic_flowman *fm;
2981         struct enic *pf;
2982         uint8_t tag;
2983
2984         pf = vf->pf;
2985         fm = pf->fm;
2986         tag = fm->vf_rep_tag;
2987         enic_fm_open_scratch(fm);
2988         fm_tcam_entry = &fm->tcam_entry;
2989         fm_action = &fm->action;
2990         /* Egress rule: match-any and tag+hairpin */
2991         fm_tcam_entry->ftm_data.fk_wq_id = 0;
2992         fm_tcam_entry->ftm_mask.fk_wq_id = 0xffff;
2993         fm_tcam_entry->ftm_data.fk_wq_vnic = vf->enic.fm_vnic_handle;
2994         fm_tcam_entry->ftm_flags |= FMEF_COUNTER;
2995         memset(&fm_op, 0, sizeof(fm_op));
2996         fm_op.fa_op = FMOP_TAG;
2997         fm_op.tag.tag = tag;
2998         enic_fm_append_action_op(fm, &fm_op, &error);
2999         memset(&fm_op, 0, sizeof(fm_op));
3000         fm_op.fa_op = FMOP_EG_HAIRPIN;
3001         enic_fm_append_action_op(fm, &fm_op, &error);
3002         memset(&fm_op, 0, sizeof(fm_op));
3003         fm_op.fa_op = FMOP_END;
3004         enic_fm_append_action_op(fm, &fm_op, &error);
3005         attrs.group = 0;
3006         attrs.ingress = 0;
3007         attrs.egress = 1;
3008         attrs.priority = FM_LOWEST_PRIORITY;
3009         flow0 = enic_fm_flow_add_entry(fm, fm_tcam_entry, fm_action,
3010                                        &attrs, &error);
3011         enic_fm_close_scratch(fm);
3012         if (flow0 == NULL) {
3013                 ENICPMD_LOG(ERR, "Cannot create flow 0 for VF->representor");
3014                 return -EINVAL;
3015         }
3016         LIST_INSERT_HEAD(&pf->flows, flow0, next);
3017         /* Make this flow internal, so the user app cannot delete it */
3018         flow0->internal = 1;
3019         ENICPMD_LOG(DEBUG, "VF %d->representor flow created: wq %d (low prio) -> tag %d hairpin",
3020                     vf->vf_id, fm_tcam_entry->ftm_data.fk_wq_id, tag);
3021
3022         /* Ingress: steer hairpinned to VF rep RQ */
3023         enic_fm_open_scratch(fm);
3024         fm_tcam_entry->ftm_flags |= FMEF_COUNTER;
3025         fm_tcam_entry->ftm_data.fk_hdrset[0].fk_metadata |= FKM_EG_HAIRPINNED;
3026         fm_tcam_entry->ftm_mask.fk_hdrset[0].fk_metadata |= FKM_EG_HAIRPINNED;
3027         fm_tcam_entry->ftm_data.fk_packet_tag = tag;
3028         fm_tcam_entry->ftm_mask.fk_packet_tag = 0xff;
3029         memset(&fm_op, 0, sizeof(fm_op));
3030         fm_op.fa_op = FMOP_RQ_STEER;
3031         fm_op.rq_steer.rq_index = vf->pf_rq_sop_idx;
3032         fm_op.rq_steer.vnic_handle = pf->fm_vnic_handle;
3033         enic_fm_append_action_op(fm, &fm_op, &error);
3034         memset(&fm_op, 0, sizeof(fm_op));
3035         fm_op.fa_op = FMOP_END;
3036         enic_fm_append_action_op(fm, &fm_op, &error);
3037         attrs.group = 0;
3038         attrs.ingress = 1;
3039         attrs.egress = 0;
3040         attrs.priority = FM_HIGHEST_PRIORITY;
3041         flow1 = enic_fm_flow_add_entry(fm, fm_tcam_entry, fm_action,
3042                                        &attrs, &error);
3043         enic_fm_close_scratch(fm);
3044         if (flow1 == NULL) {
3045                 ENICPMD_LOG(ERR, "Cannot create flow 1 for VF->representor");
3046                 enic_fm_flow_destroy(pf->rte_dev, flow0, &error);
3047                 return -EINVAL;
3048         }
3049         LIST_INSERT_HEAD(&pf->flows, flow1, next);
3050         flow1->internal = 1;
3051         ENICPMD_LOG(DEBUG, "VF %d->representor flow created: tag %d hairpinned -> PF RQ %d",
3052                     vf->vf_id, tag, vf->pf_rq_sop_idx);
3053         vf->vf2rep_flow[0] = flow0;
3054         vf->vf2rep_flow[1] = flow1;
3055         /* Done with this tag, use a different one next time */
3056         fm->vf_rep_tag++;
3057         return 0;
3058 }
3059
3060 /* Destroy representor flows created by enic_fm_add_{rep2vf,vf2rep}_flow */
3061 static void
3062 delete_rep_flows(struct enic *enic)
3063 {
3064         struct enic_vf_representor *vf;
3065         struct rte_flow_error error;
3066         struct rte_eth_dev *dev;
3067         uint32_t i;
3068
3069         RTE_ASSERT(enic_is_vf_rep(enic));
3070         vf = VF_ENIC_TO_VF_REP(enic);
3071         dev = vf->pf->rte_dev;
3072         for (i = 0; i < ARRAY_SIZE(vf->vf2rep_flow); i++) {
3073                 if (vf->vf2rep_flow[i])
3074                         enic_fm_flow_destroy(dev, vf->vf2rep_flow[i], &error);
3075         }
3076         for (i = 0; i < ARRAY_SIZE(vf->rep2vf_flow); i++) {
3077                 if (vf->rep2vf_flow[i])
3078                         enic_fm_flow_destroy(dev, vf->rep2vf_flow[i], &error);
3079         }
3080 }
3081
3082 static struct enic_flowman *
3083 begin_fm(struct enic *enic)
3084 {
3085         struct enic_vf_representor *vf;
3086         struct enic_flowman *fm;
3087
3088         /* Representor uses PF flowman */
3089         if (enic_is_vf_rep(enic)) {
3090                 vf = VF_ENIC_TO_VF_REP(enic);
3091                 fm = vf->pf->fm;
3092         } else {
3093                 fm = enic->fm;
3094         }
3095         /* Save the API caller and lock if representors exist */
3096         if (fm) {
3097                 if (fm->owner_enic->switchdev_mode)
3098                         rte_spinlock_lock(&fm->lock);
3099                 fm->user_enic = enic;
3100         }
3101         return fm;
3102 }
3103
3104 static void
3105 end_fm(struct enic_flowman *fm)
3106 {
3107         fm->user_enic = NULL;
3108         if (fm->owner_enic->switchdev_mode)
3109                 rte_spinlock_unlock(&fm->lock);
3110 }