net/hns3: fix return value for unsupported tuple
[dpdk.git] / drivers / net / sfc / sfc_mae.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright(c) 2019-2021 Xilinx, Inc.
4  * Copyright(c) 2019 Solarflare Communications Inc.
5  *
6  * This software was jointly developed between OKTET Labs (under contract
7  * for Solarflare) and Solarflare Communications, Inc.
8  */
9
10 #include <stdbool.h>
11
12 #include <rte_bitops.h>
13 #include <rte_common.h>
14 #include <rte_vxlan.h>
15
16 #include "efx.h"
17
18 #include "sfc.h"
19 #include "sfc_flow_tunnel.h"
20 #include "sfc_mae_counter.h"
21 #include "sfc_log.h"
22 #include "sfc_switch.h"
23 #include "sfc_service.h"
24
25 static int
26 sfc_mae_assign_ethdev_mport(struct sfc_adapter *sa,
27                             efx_mport_sel_t *mportp)
28 {
29         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
30
31         return efx_mae_mport_by_pcie_function(encp->enc_pf, encp->enc_vf,
32                                               mportp);
33 }
34
35 static int
36 sfc_mae_assign_entity_mport(struct sfc_adapter *sa,
37                             efx_mport_sel_t *mportp)
38 {
39         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
40         int rc = 0;
41
42         if (encp->enc_mae_admin) {
43                 /*
44                  * This ethdev sits on MAE admin PF. The represented
45                  * entity is the network port assigned to that PF.
46                  */
47                 rc = efx_mae_mport_by_phy_port(encp->enc_assigned_port, mportp);
48         } else {
49                 /*
50                  * This ethdev sits on unprivileged PF / VF. The entity
51                  * represented by the ethdev can change dynamically
52                  * as MAE admin changes default traffic rules.
53                  *
54                  * For the sake of simplicity, do not fill in the m-port
55                  * and assume that flow rules should not be allowed to
56                  * reference the entity represented by this ethdev.
57                  */
58                 efx_mae_mport_invalid(mportp);
59         }
60
61         return rc;
62 }
63
64 static int
65 sfc_mae_counter_registry_init(struct sfc_mae_counter_registry *registry,
66                               uint32_t nb_counters_max)
67 {
68         return sfc_mae_counters_init(&registry->counters, nb_counters_max);
69 }
70
71 static void
72 sfc_mae_counter_registry_fini(struct sfc_mae_counter_registry *registry)
73 {
74         sfc_mae_counters_fini(&registry->counters);
75 }
76
77 static int
78 sfc_mae_internal_rule_find_empty_slot(struct sfc_adapter *sa,
79                                       struct sfc_mae_rule **rule)
80 {
81         struct sfc_mae *mae = &sa->mae;
82         struct sfc_mae_internal_rules *internal_rules = &mae->internal_rules;
83         unsigned int entry;
84         int rc;
85
86         for (entry = 0; entry < SFC_MAE_NB_RULES_MAX; entry++) {
87                 if (internal_rules->rules[entry].spec == NULL)
88                         break;
89         }
90
91         if (entry == SFC_MAE_NB_RULES_MAX) {
92                 rc = ENOSPC;
93                 sfc_err(sa, "failed too many rules (%u rules used)", entry);
94                 goto fail_too_many_rules;
95         }
96
97         *rule = &internal_rules->rules[entry];
98
99         return 0;
100
101 fail_too_many_rules:
102         return rc;
103 }
104
105 int
106 sfc_mae_rule_add_mport_match_deliver(struct sfc_adapter *sa,
107                                      const efx_mport_sel_t *mport_match,
108                                      const efx_mport_sel_t *mport_deliver,
109                                      int prio, struct sfc_mae_rule **rulep)
110 {
111         struct sfc_mae *mae = &sa->mae;
112         struct sfc_mae_rule *rule;
113         int rc;
114
115         sfc_log_init(sa, "entry");
116
117         if (prio > 0 && (unsigned int)prio >= mae->nb_action_rule_prios_max) {
118                 rc = EINVAL;
119                 sfc_err(sa, "failed: invalid priority %d (max %u)", prio,
120                         mae->nb_action_rule_prios_max);
121                 goto fail_invalid_prio;
122         }
123         if (prio < 0)
124                 prio = mae->nb_action_rule_prios_max - 1;
125
126         rc = sfc_mae_internal_rule_find_empty_slot(sa, &rule);
127         if (rc != 0)
128                 goto fail_find_empty_slot;
129
130         sfc_log_init(sa, "init MAE match spec");
131         rc = efx_mae_match_spec_init(sa->nic, EFX_MAE_RULE_ACTION,
132                                      (uint32_t)prio, &rule->spec);
133         if (rc != 0) {
134                 sfc_err(sa, "failed to init MAE match spec");
135                 goto fail_match_init;
136         }
137
138         rc = efx_mae_match_spec_mport_set(rule->spec, mport_match, NULL);
139         if (rc != 0) {
140                 sfc_err(sa, "failed to get MAE match mport selector");
141                 goto fail_mport_set;
142         }
143
144         rc = efx_mae_action_set_spec_init(sa->nic, &rule->actions);
145         if (rc != 0) {
146                 sfc_err(sa, "failed to init MAE action set");
147                 goto fail_action_init;
148         }
149
150         rc = efx_mae_action_set_populate_deliver(rule->actions,
151                                                  mport_deliver);
152         if (rc != 0) {
153                 sfc_err(sa, "failed to populate deliver action");
154                 goto fail_populate_deliver;
155         }
156
157         rc = efx_mae_action_set_alloc(sa->nic, rule->actions,
158                                       &rule->action_set);
159         if (rc != 0) {
160                 sfc_err(sa, "failed to allocate action set");
161                 goto fail_action_set_alloc;
162         }
163
164         rc = efx_mae_action_rule_insert(sa->nic, rule->spec, NULL,
165                                         &rule->action_set,
166                                         &rule->rule_id);
167         if (rc != 0) {
168                 sfc_err(sa, "failed to insert action rule");
169                 goto fail_rule_insert;
170         }
171
172         *rulep = rule;
173
174         sfc_log_init(sa, "done");
175
176         return 0;
177
178 fail_rule_insert:
179         efx_mae_action_set_free(sa->nic, &rule->action_set);
180
181 fail_action_set_alloc:
182 fail_populate_deliver:
183         efx_mae_action_set_spec_fini(sa->nic, rule->actions);
184
185 fail_action_init:
186 fail_mport_set:
187         efx_mae_match_spec_fini(sa->nic, rule->spec);
188
189 fail_match_init:
190 fail_find_empty_slot:
191 fail_invalid_prio:
192         sfc_log_init(sa, "failed: %s", rte_strerror(rc));
193         return rc;
194 }
195
196 void
197 sfc_mae_rule_del(struct sfc_adapter *sa, struct sfc_mae_rule *rule)
198 {
199         if (rule == NULL || rule->spec == NULL)
200                 return;
201
202         efx_mae_action_rule_remove(sa->nic, &rule->rule_id);
203         efx_mae_action_set_free(sa->nic, &rule->action_set);
204         efx_mae_action_set_spec_fini(sa->nic, rule->actions);
205         efx_mae_match_spec_fini(sa->nic, rule->spec);
206
207         rule->spec = NULL;
208 }
209
210 int
211 sfc_mae_attach(struct sfc_adapter *sa)
212 {
213         struct sfc_adapter_shared * const sas = sfc_sa2shared(sa);
214         struct sfc_mae_switch_port_request switch_port_request = {0};
215         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
216         efx_mport_sel_t ethdev_mport;
217         efx_mport_sel_t entity_mport;
218         struct sfc_mae *mae = &sa->mae;
219         struct sfc_mae_bounce_eh *bounce_eh = &mae->bounce_eh;
220         efx_mae_limits_t limits;
221         int rc;
222
223         sfc_log_init(sa, "entry");
224
225         if (!encp->enc_mae_supported) {
226                 mae->status = SFC_MAE_STATUS_UNSUPPORTED;
227                 return 0;
228         }
229
230         if (encp->enc_mae_admin) {
231                 sfc_log_init(sa, "init MAE");
232                 rc = efx_mae_init(sa->nic);
233                 if (rc != 0)
234                         goto fail_mae_init;
235
236                 sfc_log_init(sa, "get MAE limits");
237                 rc = efx_mae_get_limits(sa->nic, &limits);
238                 if (rc != 0)
239                         goto fail_mae_get_limits;
240
241                 sfc_log_init(sa, "init MAE counter registry");
242                 rc = sfc_mae_counter_registry_init(&mae->counter_registry,
243                                                    limits.eml_max_n_counters);
244                 if (rc != 0) {
245                         sfc_err(sa, "failed to init MAE counters registry for %u entries: %s",
246                                 limits.eml_max_n_counters, rte_strerror(rc));
247                         goto fail_counter_registry_init;
248                 }
249         }
250
251         sfc_log_init(sa, "assign ethdev MPORT");
252         rc = sfc_mae_assign_ethdev_mport(sa, &ethdev_mport);
253         if (rc != 0)
254                 goto fail_mae_assign_ethdev_mport;
255
256         sfc_log_init(sa, "assign entity MPORT");
257         rc = sfc_mae_assign_entity_mport(sa, &entity_mport);
258         if (rc != 0)
259                 goto fail_mae_assign_entity_mport;
260
261         sfc_log_init(sa, "assign RTE switch domain");
262         rc = sfc_mae_assign_switch_domain(sa, &mae->switch_domain_id);
263         if (rc != 0)
264                 goto fail_mae_assign_switch_domain;
265
266         sfc_log_init(sa, "assign RTE switch port");
267         switch_port_request.type = SFC_MAE_SWITCH_PORT_INDEPENDENT;
268         switch_port_request.ethdev_mportp = &ethdev_mport;
269         switch_port_request.entity_mportp = &entity_mport;
270         switch_port_request.ethdev_port_id = sas->port_id;
271         switch_port_request.port_data.indep.mae_admin =
272                 encp->enc_mae_admin == B_TRUE;
273         rc = sfc_mae_assign_switch_port(mae->switch_domain_id,
274                                         &switch_port_request,
275                                         &mae->switch_port_id);
276         if (rc != 0)
277                 goto fail_mae_assign_switch_port;
278
279         if (encp->enc_mae_admin) {
280                 sfc_log_init(sa, "allocate encap. header bounce buffer");
281                 bounce_eh->buf_size = limits.eml_encap_header_size_limit;
282                 bounce_eh->buf = rte_malloc("sfc_mae_bounce_eh",
283                                             bounce_eh->buf_size, 0);
284                 if (bounce_eh->buf == NULL)
285                         goto fail_mae_alloc_bounce_eh;
286
287                 mae->nb_outer_rule_prios_max = limits.eml_max_n_outer_prios;
288                 mae->nb_action_rule_prios_max = limits.eml_max_n_action_prios;
289                 mae->encap_types_supported = limits.eml_encap_types_supported;
290         }
291
292         TAILQ_INIT(&mae->outer_rules);
293         TAILQ_INIT(&mae->mac_addrs);
294         TAILQ_INIT(&mae->encap_headers);
295         TAILQ_INIT(&mae->action_sets);
296
297         if (encp->enc_mae_admin)
298                 mae->status = SFC_MAE_STATUS_ADMIN;
299         else
300                 mae->status = SFC_MAE_STATUS_SUPPORTED;
301
302         sfc_log_init(sa, "done");
303
304         return 0;
305
306 fail_mae_alloc_bounce_eh:
307 fail_mae_assign_switch_port:
308 fail_mae_assign_switch_domain:
309 fail_mae_assign_entity_mport:
310 fail_mae_assign_ethdev_mport:
311         if (encp->enc_mae_admin)
312                 sfc_mae_counter_registry_fini(&mae->counter_registry);
313
314 fail_counter_registry_init:
315 fail_mae_get_limits:
316         if (encp->enc_mae_admin)
317                 efx_mae_fini(sa->nic);
318
319 fail_mae_init:
320         sfc_log_init(sa, "failed %d", rc);
321
322         return rc;
323 }
324
325 void
326 sfc_mae_detach(struct sfc_adapter *sa)
327 {
328         struct sfc_mae *mae = &sa->mae;
329         enum sfc_mae_status status_prev = mae->status;
330
331         sfc_log_init(sa, "entry");
332
333         mae->nb_action_rule_prios_max = 0;
334         mae->status = SFC_MAE_STATUS_UNKNOWN;
335
336         if (status_prev != SFC_MAE_STATUS_ADMIN)
337                 return;
338
339         rte_free(mae->bounce_eh.buf);
340         sfc_mae_counter_registry_fini(&mae->counter_registry);
341
342         efx_mae_fini(sa->nic);
343
344         sfc_log_init(sa, "done");
345 }
346
347 static struct sfc_mae_outer_rule *
348 sfc_mae_outer_rule_attach(struct sfc_adapter *sa,
349                           const efx_mae_match_spec_t *match_spec,
350                           efx_tunnel_protocol_t encap_type)
351 {
352         struct sfc_mae_outer_rule *rule;
353         struct sfc_mae *mae = &sa->mae;
354
355         SFC_ASSERT(sfc_adapter_is_locked(sa));
356
357         TAILQ_FOREACH(rule, &mae->outer_rules, entries) {
358                 if (efx_mae_match_specs_equal(rule->match_spec, match_spec) &&
359                     rule->encap_type == encap_type) {
360                         sfc_dbg(sa, "attaching to outer_rule=%p", rule);
361                         ++(rule->refcnt);
362                         return rule;
363                 }
364         }
365
366         return NULL;
367 }
368
369 static int
370 sfc_mae_outer_rule_add(struct sfc_adapter *sa,
371                        efx_mae_match_spec_t *match_spec,
372                        efx_tunnel_protocol_t encap_type,
373                        struct sfc_mae_outer_rule **rulep)
374 {
375         struct sfc_mae_outer_rule *rule;
376         struct sfc_mae *mae = &sa->mae;
377
378         SFC_ASSERT(sfc_adapter_is_locked(sa));
379
380         rule = rte_zmalloc("sfc_mae_outer_rule", sizeof(*rule), 0);
381         if (rule == NULL)
382                 return ENOMEM;
383
384         rule->refcnt = 1;
385         rule->match_spec = match_spec;
386         rule->encap_type = encap_type;
387
388         rule->fw_rsrc.rule_id.id = EFX_MAE_RSRC_ID_INVALID;
389
390         TAILQ_INSERT_TAIL(&mae->outer_rules, rule, entries);
391
392         *rulep = rule;
393
394         sfc_dbg(sa, "added outer_rule=%p", rule);
395
396         return 0;
397 }
398
399 static void
400 sfc_mae_outer_rule_del(struct sfc_adapter *sa,
401                        struct sfc_mae_outer_rule *rule)
402 {
403         struct sfc_mae *mae = &sa->mae;
404
405         SFC_ASSERT(sfc_adapter_is_locked(sa));
406         SFC_ASSERT(rule->refcnt != 0);
407
408         --(rule->refcnt);
409
410         if (rule->refcnt != 0)
411                 return;
412
413         if (rule->fw_rsrc.rule_id.id != EFX_MAE_RSRC_ID_INVALID ||
414             rule->fw_rsrc.refcnt != 0) {
415                 sfc_err(sa, "deleting outer_rule=%p abandons its FW resource: OR_ID=0x%08x, refcnt=%u",
416                         rule, rule->fw_rsrc.rule_id.id, rule->fw_rsrc.refcnt);
417         }
418
419         efx_mae_match_spec_fini(sa->nic, rule->match_spec);
420
421         TAILQ_REMOVE(&mae->outer_rules, rule, entries);
422         rte_free(rule);
423
424         sfc_dbg(sa, "deleted outer_rule=%p", rule);
425 }
426
427 static int
428 sfc_mae_outer_rule_enable(struct sfc_adapter *sa,
429                           struct sfc_mae_outer_rule *rule,
430                           efx_mae_match_spec_t *match_spec_action)
431 {
432         struct sfc_mae_fw_rsrc *fw_rsrc = &rule->fw_rsrc;
433         int rc;
434
435         SFC_ASSERT(sfc_adapter_is_locked(sa));
436
437         if (fw_rsrc->refcnt == 0) {
438                 SFC_ASSERT(fw_rsrc->rule_id.id == EFX_MAE_RSRC_ID_INVALID);
439                 SFC_ASSERT(rule->match_spec != NULL);
440
441                 rc = efx_mae_outer_rule_insert(sa->nic, rule->match_spec,
442                                                rule->encap_type,
443                                                &fw_rsrc->rule_id);
444                 if (rc != 0) {
445                         sfc_err(sa, "failed to enable outer_rule=%p: %s",
446                                 rule, strerror(rc));
447                         return rc;
448                 }
449         }
450
451         if (match_spec_action == NULL)
452                 goto skip_action_rule;
453
454         rc = efx_mae_match_spec_outer_rule_id_set(match_spec_action,
455                                                   &fw_rsrc->rule_id);
456         if (rc != 0) {
457                 if (fw_rsrc->refcnt == 0) {
458                         (void)efx_mae_outer_rule_remove(sa->nic,
459                                                         &fw_rsrc->rule_id);
460                         fw_rsrc->rule_id.id = EFX_MAE_RSRC_ID_INVALID;
461                 }
462
463                 sfc_err(sa, "can't match on outer rule ID: %s", strerror(rc));
464
465                 return rc;
466         }
467
468 skip_action_rule:
469         if (fw_rsrc->refcnt == 0) {
470                 sfc_dbg(sa, "enabled outer_rule=%p: OR_ID=0x%08x",
471                         rule, fw_rsrc->rule_id.id);
472         }
473
474         ++(fw_rsrc->refcnt);
475
476         return 0;
477 }
478
479 static void
480 sfc_mae_outer_rule_disable(struct sfc_adapter *sa,
481                            struct sfc_mae_outer_rule *rule)
482 {
483         struct sfc_mae_fw_rsrc *fw_rsrc = &rule->fw_rsrc;
484         int rc;
485
486         SFC_ASSERT(sfc_adapter_is_locked(sa));
487
488         if (fw_rsrc->rule_id.id == EFX_MAE_RSRC_ID_INVALID ||
489             fw_rsrc->refcnt == 0) {
490                 sfc_err(sa, "failed to disable outer_rule=%p: already disabled; OR_ID=0x%08x, refcnt=%u",
491                         rule, fw_rsrc->rule_id.id, fw_rsrc->refcnt);
492                 return;
493         }
494
495         if (fw_rsrc->refcnt == 1) {
496                 rc = efx_mae_outer_rule_remove(sa->nic, &fw_rsrc->rule_id);
497                 if (rc == 0) {
498                         sfc_dbg(sa, "disabled outer_rule=%p with OR_ID=0x%08x",
499                                 rule, fw_rsrc->rule_id.id);
500                 } else {
501                         sfc_err(sa, "failed to disable outer_rule=%p with OR_ID=0x%08x: %s",
502                                 rule, fw_rsrc->rule_id.id, strerror(rc));
503                 }
504                 fw_rsrc->rule_id.id = EFX_MAE_RSRC_ID_INVALID;
505         }
506
507         --(fw_rsrc->refcnt);
508 }
509
510 static struct sfc_mae_mac_addr *
511 sfc_mae_mac_addr_attach(struct sfc_adapter *sa,
512                         const uint8_t addr_bytes[EFX_MAC_ADDR_LEN])
513 {
514         struct sfc_mae_mac_addr *mac_addr;
515         struct sfc_mae *mae = &sa->mae;
516
517         SFC_ASSERT(sfc_adapter_is_locked(sa));
518
519         TAILQ_FOREACH(mac_addr, &mae->mac_addrs, entries) {
520                 if (memcmp(mac_addr->addr_bytes, addr_bytes,
521                            EFX_MAC_ADDR_LEN) == 0) {
522                         sfc_dbg(sa, "attaching to mac_addr=%p", mac_addr);
523                         ++(mac_addr->refcnt);
524                         return mac_addr;
525                 }
526         }
527
528         return NULL;
529 }
530
531 static int
532 sfc_mae_mac_addr_add(struct sfc_adapter *sa,
533                      const uint8_t addr_bytes[EFX_MAC_ADDR_LEN],
534                      struct sfc_mae_mac_addr **mac_addrp)
535 {
536         struct sfc_mae_mac_addr *mac_addr;
537         struct sfc_mae *mae = &sa->mae;
538
539         SFC_ASSERT(sfc_adapter_is_locked(sa));
540
541         mac_addr = rte_zmalloc("sfc_mae_mac_addr", sizeof(*mac_addr), 0);
542         if (mac_addr == NULL)
543                 return ENOMEM;
544
545         rte_memcpy(mac_addr->addr_bytes, addr_bytes, EFX_MAC_ADDR_LEN);
546
547         mac_addr->refcnt = 1;
548         mac_addr->fw_rsrc.mac_id.id = EFX_MAE_RSRC_ID_INVALID;
549
550         TAILQ_INSERT_TAIL(&mae->mac_addrs, mac_addr, entries);
551
552         *mac_addrp = mac_addr;
553
554         sfc_dbg(sa, "added mac_addr=%p", mac_addr);
555
556         return 0;
557 }
558
559 static void
560 sfc_mae_mac_addr_del(struct sfc_adapter *sa, struct sfc_mae_mac_addr *mac_addr)
561 {
562         struct sfc_mae *mae = &sa->mae;
563
564         if (mac_addr == NULL)
565                 return;
566
567         SFC_ASSERT(sfc_adapter_is_locked(sa));
568         SFC_ASSERT(mac_addr->refcnt != 0);
569
570         --(mac_addr->refcnt);
571
572         if (mac_addr->refcnt != 0)
573                 return;
574
575         if (mac_addr->fw_rsrc.mac_id.id != EFX_MAE_RSRC_ID_INVALID ||
576             mac_addr->fw_rsrc.refcnt != 0) {
577                 sfc_err(sa, "deleting mac_addr=%p abandons its FW resource: MAC_ID=0x%08x, refcnt=%u",
578                         mac_addr, mac_addr->fw_rsrc.mac_id.id,
579                         mac_addr->fw_rsrc.refcnt);
580         }
581
582         TAILQ_REMOVE(&mae->mac_addrs, mac_addr, entries);
583         rte_free(mac_addr);
584
585         sfc_dbg(sa, "deleted mac_addr=%p", mac_addr);
586 }
587
588 enum sfc_mae_mac_addr_type {
589         SFC_MAE_MAC_ADDR_DST,
590         SFC_MAE_MAC_ADDR_SRC
591 };
592
593 static int
594 sfc_mae_mac_addr_enable(struct sfc_adapter *sa,
595                         struct sfc_mae_mac_addr *mac_addr,
596                         enum sfc_mae_mac_addr_type type,
597                         efx_mae_actions_t *aset_spec)
598 {
599         struct sfc_mae_fw_rsrc *fw_rsrc;
600         int rc = 0;
601
602         if (mac_addr == NULL)
603                 return 0;
604
605         SFC_ASSERT(sfc_adapter_is_locked(sa));
606
607         fw_rsrc = &mac_addr->fw_rsrc;
608
609         if (fw_rsrc->refcnt == 0) {
610                 SFC_ASSERT(fw_rsrc->mac_id.id == EFX_MAE_RSRC_ID_INVALID);
611
612                 rc = efx_mae_mac_addr_alloc(sa->nic, mac_addr->addr_bytes,
613                                             &fw_rsrc->mac_id);
614                 if (rc != 0) {
615                         sfc_err(sa, "failed to enable mac_addr=%p: %s",
616                                 mac_addr, strerror(rc));
617                         return rc;
618                 }
619         }
620
621         switch (type) {
622         case SFC_MAE_MAC_ADDR_DST:
623                 rc = efx_mae_action_set_fill_in_dst_mac_id(aset_spec,
624                                                            &fw_rsrc->mac_id);
625                 break;
626         case SFC_MAE_MAC_ADDR_SRC:
627                 rc = efx_mae_action_set_fill_in_src_mac_id(aset_spec,
628                                                            &fw_rsrc->mac_id);
629                 break;
630         default:
631                 rc = EINVAL;
632                 break;
633         }
634
635         if (rc != 0) {
636                 if (fw_rsrc->refcnt == 0) {
637                         (void)efx_mae_mac_addr_free(sa->nic, &fw_rsrc->mac_id);
638                         fw_rsrc->mac_id.id = EFX_MAE_RSRC_ID_INVALID;
639                 }
640
641                 sfc_err(sa, "cannot fill in MAC address entry ID: %s",
642                         strerror(rc));
643
644                 return rc;
645         }
646
647         if (fw_rsrc->refcnt == 0) {
648                 sfc_dbg(sa, "enabled mac_addr=%p: MAC_ID=0x%08x",
649                         mac_addr, fw_rsrc->mac_id.id);
650         }
651
652         ++(fw_rsrc->refcnt);
653
654         return 0;
655 }
656
657 static void
658 sfc_mae_mac_addr_disable(struct sfc_adapter *sa,
659                          struct sfc_mae_mac_addr *mac_addr)
660 {
661         struct sfc_mae_fw_rsrc *fw_rsrc;
662         int rc;
663
664         if (mac_addr == NULL)
665                 return;
666
667         SFC_ASSERT(sfc_adapter_is_locked(sa));
668
669         fw_rsrc = &mac_addr->fw_rsrc;
670
671         if (fw_rsrc->mac_id.id == EFX_MAE_RSRC_ID_INVALID ||
672             fw_rsrc->refcnt == 0) {
673                 sfc_err(sa, "failed to disable mac_addr=%p: already disabled; MAC_ID=0x%08x, refcnt=%u",
674                         mac_addr, fw_rsrc->mac_id.id, fw_rsrc->refcnt);
675                 return;
676         }
677
678         if (fw_rsrc->refcnt == 1) {
679                 rc = efx_mae_mac_addr_free(sa->nic, &fw_rsrc->mac_id);
680                 if (rc == 0) {
681                         sfc_dbg(sa, "disabled mac_addr=%p with MAC_ID=0x%08x",
682                                 mac_addr, fw_rsrc->mac_id.id);
683                 } else {
684                         sfc_err(sa, "failed to disable mac_addr=%p with MAC_ID=0x%08x: %s",
685                                 mac_addr, fw_rsrc->mac_id.id, strerror(rc));
686                 }
687                 fw_rsrc->mac_id.id = EFX_MAE_RSRC_ID_INVALID;
688         }
689
690         --(fw_rsrc->refcnt);
691 }
692
693 static struct sfc_mae_encap_header *
694 sfc_mae_encap_header_attach(struct sfc_adapter *sa,
695                             const struct sfc_mae_bounce_eh *bounce_eh)
696 {
697         struct sfc_mae_encap_header *encap_header;
698         struct sfc_mae *mae = &sa->mae;
699
700         SFC_ASSERT(sfc_adapter_is_locked(sa));
701
702         TAILQ_FOREACH(encap_header, &mae->encap_headers, entries) {
703                 if (encap_header->size == bounce_eh->size &&
704                     memcmp(encap_header->buf, bounce_eh->buf,
705                            bounce_eh->size) == 0) {
706                         sfc_dbg(sa, "attaching to encap_header=%p",
707                                 encap_header);
708                         ++(encap_header->refcnt);
709                         return encap_header;
710                 }
711         }
712
713         return NULL;
714 }
715
716 static int
717 sfc_mae_encap_header_add(struct sfc_adapter *sa,
718                          const struct sfc_mae_bounce_eh *bounce_eh,
719                          struct sfc_mae_encap_header **encap_headerp)
720 {
721         struct sfc_mae_encap_header *encap_header;
722         struct sfc_mae *mae = &sa->mae;
723
724         SFC_ASSERT(sfc_adapter_is_locked(sa));
725
726         encap_header = rte_zmalloc("sfc_mae_encap_header",
727                                    sizeof(*encap_header), 0);
728         if (encap_header == NULL)
729                 return ENOMEM;
730
731         encap_header->size = bounce_eh->size;
732
733         encap_header->buf = rte_malloc("sfc_mae_encap_header_buf",
734                                        encap_header->size, 0);
735         if (encap_header->buf == NULL) {
736                 rte_free(encap_header);
737                 return ENOMEM;
738         }
739
740         rte_memcpy(encap_header->buf, bounce_eh->buf, bounce_eh->size);
741
742         encap_header->refcnt = 1;
743         encap_header->type = bounce_eh->type;
744         encap_header->fw_rsrc.eh_id.id = EFX_MAE_RSRC_ID_INVALID;
745
746         TAILQ_INSERT_TAIL(&mae->encap_headers, encap_header, entries);
747
748         *encap_headerp = encap_header;
749
750         sfc_dbg(sa, "added encap_header=%p", encap_header);
751
752         return 0;
753 }
754
755 static void
756 sfc_mae_encap_header_del(struct sfc_adapter *sa,
757                        struct sfc_mae_encap_header *encap_header)
758 {
759         struct sfc_mae *mae = &sa->mae;
760
761         if (encap_header == NULL)
762                 return;
763
764         SFC_ASSERT(sfc_adapter_is_locked(sa));
765         SFC_ASSERT(encap_header->refcnt != 0);
766
767         --(encap_header->refcnt);
768
769         if (encap_header->refcnt != 0)
770                 return;
771
772         if (encap_header->fw_rsrc.eh_id.id != EFX_MAE_RSRC_ID_INVALID ||
773             encap_header->fw_rsrc.refcnt != 0) {
774                 sfc_err(sa, "deleting encap_header=%p abandons its FW resource: EH_ID=0x%08x, refcnt=%u",
775                         encap_header, encap_header->fw_rsrc.eh_id.id,
776                         encap_header->fw_rsrc.refcnt);
777         }
778
779         TAILQ_REMOVE(&mae->encap_headers, encap_header, entries);
780         rte_free(encap_header->buf);
781         rte_free(encap_header);
782
783         sfc_dbg(sa, "deleted encap_header=%p", encap_header);
784 }
785
786 static int
787 sfc_mae_encap_header_enable(struct sfc_adapter *sa,
788                             struct sfc_mae_encap_header *encap_header,
789                             efx_mae_actions_t *action_set_spec)
790 {
791         struct sfc_mae_fw_rsrc *fw_rsrc;
792         int rc;
793
794         if (encap_header == NULL)
795                 return 0;
796
797         SFC_ASSERT(sfc_adapter_is_locked(sa));
798
799         fw_rsrc = &encap_header->fw_rsrc;
800
801         if (fw_rsrc->refcnt == 0) {
802                 SFC_ASSERT(fw_rsrc->eh_id.id == EFX_MAE_RSRC_ID_INVALID);
803                 SFC_ASSERT(encap_header->buf != NULL);
804                 SFC_ASSERT(encap_header->size != 0);
805
806                 rc = efx_mae_encap_header_alloc(sa->nic, encap_header->type,
807                                                 encap_header->buf,
808                                                 encap_header->size,
809                                                 &fw_rsrc->eh_id);
810                 if (rc != 0) {
811                         sfc_err(sa, "failed to enable encap_header=%p: %s",
812                                 encap_header, strerror(rc));
813                         return rc;
814                 }
815         }
816
817         rc = efx_mae_action_set_fill_in_eh_id(action_set_spec,
818                                               &fw_rsrc->eh_id);
819         if (rc != 0) {
820                 if (fw_rsrc->refcnt == 0) {
821                         (void)efx_mae_encap_header_free(sa->nic,
822                                                         &fw_rsrc->eh_id);
823                         fw_rsrc->eh_id.id = EFX_MAE_RSRC_ID_INVALID;
824                 }
825
826                 sfc_err(sa, "can't fill in encap. header ID: %s", strerror(rc));
827
828                 return rc;
829         }
830
831         if (fw_rsrc->refcnt == 0) {
832                 sfc_dbg(sa, "enabled encap_header=%p: EH_ID=0x%08x",
833                         encap_header, fw_rsrc->eh_id.id);
834         }
835
836         ++(fw_rsrc->refcnt);
837
838         return 0;
839 }
840
841 static void
842 sfc_mae_encap_header_disable(struct sfc_adapter *sa,
843                              struct sfc_mae_encap_header *encap_header)
844 {
845         struct sfc_mae_fw_rsrc *fw_rsrc;
846         int rc;
847
848         if (encap_header == NULL)
849                 return;
850
851         SFC_ASSERT(sfc_adapter_is_locked(sa));
852
853         fw_rsrc = &encap_header->fw_rsrc;
854
855         if (fw_rsrc->eh_id.id == EFX_MAE_RSRC_ID_INVALID ||
856             fw_rsrc->refcnt == 0) {
857                 sfc_err(sa, "failed to disable encap_header=%p: already disabled; EH_ID=0x%08x, refcnt=%u",
858                         encap_header, fw_rsrc->eh_id.id, fw_rsrc->refcnt);
859                 return;
860         }
861
862         if (fw_rsrc->refcnt == 1) {
863                 rc = efx_mae_encap_header_free(sa->nic, &fw_rsrc->eh_id);
864                 if (rc == 0) {
865                         sfc_dbg(sa, "disabled encap_header=%p with EH_ID=0x%08x",
866                                 encap_header, fw_rsrc->eh_id.id);
867                 } else {
868                         sfc_err(sa, "failed to disable encap_header=%p with EH_ID=0x%08x: %s",
869                                 encap_header, fw_rsrc->eh_id.id, strerror(rc));
870                 }
871                 fw_rsrc->eh_id.id = EFX_MAE_RSRC_ID_INVALID;
872         }
873
874         --(fw_rsrc->refcnt);
875 }
876
877 static int
878 sfc_mae_counters_enable(struct sfc_adapter *sa,
879                         struct sfc_mae_counter_id *counters,
880                         unsigned int n_counters,
881                         efx_mae_actions_t *action_set_spec)
882 {
883         int rc;
884
885         sfc_log_init(sa, "entry");
886
887         if (n_counters == 0) {
888                 sfc_log_init(sa, "no counters - skip");
889                 return 0;
890         }
891
892         SFC_ASSERT(sfc_adapter_is_locked(sa));
893         SFC_ASSERT(n_counters == 1);
894
895         rc = sfc_mae_counter_enable(sa, &counters[0]);
896         if (rc != 0) {
897                 sfc_err(sa, "failed to enable MAE counter %u: %s",
898                         counters[0].mae_id.id, rte_strerror(rc));
899                 goto fail_counter_add;
900         }
901
902         rc = efx_mae_action_set_fill_in_counter_id(action_set_spec,
903                                                    &counters[0].mae_id);
904         if (rc != 0) {
905                 sfc_err(sa, "failed to fill in MAE counter %u in action set: %s",
906                         counters[0].mae_id.id, rte_strerror(rc));
907                 goto fail_fill_in_id;
908         }
909
910         return 0;
911
912 fail_fill_in_id:
913         (void)sfc_mae_counter_disable(sa, &counters[0]);
914
915 fail_counter_add:
916         sfc_log_init(sa, "failed: %s", rte_strerror(rc));
917         return rc;
918 }
919
920 static int
921 sfc_mae_counters_disable(struct sfc_adapter *sa,
922                          struct sfc_mae_counter_id *counters,
923                          unsigned int n_counters)
924 {
925         if (n_counters == 0)
926                 return 0;
927
928         SFC_ASSERT(sfc_adapter_is_locked(sa));
929         SFC_ASSERT(n_counters == 1);
930
931         if (counters[0].mae_id.id == EFX_MAE_RSRC_ID_INVALID) {
932                 sfc_err(sa, "failed to disable: already disabled");
933                 return EALREADY;
934         }
935
936         return sfc_mae_counter_disable(sa, &counters[0]);
937 }
938
939 struct sfc_mae_aset_ctx {
940         uint64_t                        *ft_switch_hit_counter;
941         struct sfc_ft_ctx               *counter_ft_ctx;
942         struct sfc_mae_encap_header     *encap_header;
943         unsigned int                    n_counters;
944         struct sfc_mae_mac_addr         *dst_mac;
945         struct sfc_mae_mac_addr         *src_mac;
946
947         efx_mae_actions_t               *spec;
948 };
949
950 static struct sfc_mae_action_set *
951 sfc_mae_action_set_attach(struct sfc_adapter *sa,
952                           const struct sfc_mae_aset_ctx *ctx)
953 {
954         struct sfc_mae_action_set *action_set;
955         struct sfc_mae *mae = &sa->mae;
956
957         SFC_ASSERT(sfc_adapter_is_locked(sa));
958
959         /*
960          * Shared counters are not supported, hence, action
961          * sets with counters are not attachable.
962          */
963         if (ctx->n_counters != 0)
964                 return NULL;
965
966         TAILQ_FOREACH(action_set, &mae->action_sets, entries) {
967                 if (action_set->encap_header == ctx->encap_header &&
968                     action_set->dst_mac_addr == ctx->dst_mac &&
969                     action_set->src_mac_addr == ctx->src_mac &&
970                     efx_mae_action_set_specs_equal(action_set->spec,
971                                                    ctx->spec)) {
972                         sfc_dbg(sa, "attaching to action_set=%p", action_set);
973                         ++(action_set->refcnt);
974                         return action_set;
975                 }
976         }
977
978         return NULL;
979 }
980
981 static int
982 sfc_mae_action_set_add(struct sfc_adapter *sa,
983                        const struct rte_flow_action actions[],
984                        const struct sfc_mae_aset_ctx *ctx,
985                        struct sfc_mae_action_set **action_setp)
986 {
987         struct sfc_mae_action_set *action_set;
988         struct sfc_mae *mae = &sa->mae;
989         unsigned int i;
990
991         SFC_ASSERT(sfc_adapter_is_locked(sa));
992
993         action_set = rte_zmalloc("sfc_mae_action_set", sizeof(*action_set), 0);
994         if (action_set == NULL) {
995                 sfc_err(sa, "failed to alloc action set");
996                 return ENOMEM;
997         }
998
999         if (ctx->n_counters > 0) {
1000                 const struct rte_flow_action *action;
1001
1002                 action_set->counters = rte_malloc("sfc_mae_counter_ids",
1003                         sizeof(action_set->counters[0]) * ctx->n_counters, 0);
1004                 if (action_set->counters == NULL) {
1005                         rte_free(action_set);
1006                         sfc_err(sa, "failed to alloc counters");
1007                         return ENOMEM;
1008                 }
1009
1010                 for (i = 0; i < ctx->n_counters; ++i) {
1011                         action_set->counters[i].rte_id_valid = B_FALSE;
1012                         action_set->counters[i].mae_id.id =
1013                                 EFX_MAE_RSRC_ID_INVALID;
1014
1015                         action_set->counters[i].ft_ctx = ctx->counter_ft_ctx;
1016                         action_set->counters[i].ft_switch_hit_counter =
1017                                 ctx->ft_switch_hit_counter;
1018                 }
1019
1020                 for (action = actions, i = 0;
1021                      action->type != RTE_FLOW_ACTION_TYPE_END &&
1022                      i < ctx->n_counters; ++action) {
1023                         const struct rte_flow_action_count *conf;
1024
1025                         if (action->type != RTE_FLOW_ACTION_TYPE_COUNT)
1026                                 continue;
1027
1028                         conf = action->conf;
1029
1030                         action_set->counters[i].rte_id_valid = B_TRUE;
1031                         action_set->counters[i].rte_id = conf->id;
1032                         i++;
1033                 }
1034                 action_set->n_counters = ctx->n_counters;
1035         }
1036
1037         action_set->refcnt = 1;
1038         action_set->spec = ctx->spec;
1039         action_set->encap_header = ctx->encap_header;
1040         action_set->dst_mac_addr = ctx->dst_mac;
1041         action_set->src_mac_addr = ctx->src_mac;
1042
1043         action_set->fw_rsrc.aset_id.id = EFX_MAE_RSRC_ID_INVALID;
1044
1045         TAILQ_INSERT_TAIL(&mae->action_sets, action_set, entries);
1046
1047         *action_setp = action_set;
1048
1049         sfc_dbg(sa, "added action_set=%p", action_set);
1050
1051         return 0;
1052 }
1053
1054 static void
1055 sfc_mae_action_set_del(struct sfc_adapter *sa,
1056                        struct sfc_mae_action_set *action_set)
1057 {
1058         struct sfc_mae *mae = &sa->mae;
1059
1060         SFC_ASSERT(sfc_adapter_is_locked(sa));
1061         SFC_ASSERT(action_set->refcnt != 0);
1062
1063         --(action_set->refcnt);
1064
1065         if (action_set->refcnt != 0)
1066                 return;
1067
1068         if (action_set->fw_rsrc.aset_id.id != EFX_MAE_RSRC_ID_INVALID ||
1069             action_set->fw_rsrc.refcnt != 0) {
1070                 sfc_err(sa, "deleting action_set=%p abandons its FW resource: AS_ID=0x%08x, refcnt=%u",
1071                         action_set, action_set->fw_rsrc.aset_id.id,
1072                         action_set->fw_rsrc.refcnt);
1073         }
1074
1075         efx_mae_action_set_spec_fini(sa->nic, action_set->spec);
1076         sfc_mae_encap_header_del(sa, action_set->encap_header);
1077         sfc_mae_mac_addr_del(sa, action_set->dst_mac_addr);
1078         sfc_mae_mac_addr_del(sa, action_set->src_mac_addr);
1079         if (action_set->n_counters > 0) {
1080                 SFC_ASSERT(action_set->n_counters == 1);
1081                 SFC_ASSERT(action_set->counters[0].mae_id.id ==
1082                            EFX_MAE_RSRC_ID_INVALID);
1083                 rte_free(action_set->counters);
1084         }
1085         TAILQ_REMOVE(&mae->action_sets, action_set, entries);
1086         rte_free(action_set);
1087
1088         sfc_dbg(sa, "deleted action_set=%p", action_set);
1089 }
1090
1091 static int
1092 sfc_mae_action_set_enable(struct sfc_adapter *sa,
1093                           struct sfc_mae_action_set *action_set)
1094 {
1095         struct sfc_mae_encap_header *encap_header = action_set->encap_header;
1096         struct sfc_mae_mac_addr *dst_mac_addr = action_set->dst_mac_addr;
1097         struct sfc_mae_mac_addr *src_mac_addr = action_set->src_mac_addr;
1098         struct sfc_mae_counter_id *counters = action_set->counters;
1099         struct sfc_mae_fw_rsrc *fw_rsrc = &action_set->fw_rsrc;
1100         int rc;
1101
1102         SFC_ASSERT(sfc_adapter_is_locked(sa));
1103
1104         if (fw_rsrc->refcnt == 0) {
1105                 SFC_ASSERT(fw_rsrc->aset_id.id == EFX_MAE_RSRC_ID_INVALID);
1106                 SFC_ASSERT(action_set->spec != NULL);
1107
1108                 rc = sfc_mae_mac_addr_enable(sa, dst_mac_addr,
1109                                              SFC_MAE_MAC_ADDR_DST,
1110                                              action_set->spec);
1111                 if (rc != 0)
1112                         return rc;
1113
1114                 rc = sfc_mae_mac_addr_enable(sa, src_mac_addr,
1115                                              SFC_MAE_MAC_ADDR_SRC,
1116                                              action_set->spec);
1117                 if (rc != 0) {
1118                         sfc_mae_mac_addr_disable(sa, dst_mac_addr);
1119                         return rc;
1120                 }
1121
1122                 rc = sfc_mae_encap_header_enable(sa, encap_header,
1123                                                  action_set->spec);
1124                 if (rc != 0) {
1125                         sfc_mae_mac_addr_disable(sa, src_mac_addr);
1126                         sfc_mae_mac_addr_disable(sa, dst_mac_addr);
1127                         return rc;
1128                 }
1129
1130                 rc = sfc_mae_counters_enable(sa, counters,
1131                                              action_set->n_counters,
1132                                              action_set->spec);
1133                 if (rc != 0) {
1134                         sfc_err(sa, "failed to enable %u MAE counters: %s",
1135                                 action_set->n_counters, rte_strerror(rc));
1136
1137                         sfc_mae_encap_header_disable(sa, encap_header);
1138                         sfc_mae_mac_addr_disable(sa, src_mac_addr);
1139                         sfc_mae_mac_addr_disable(sa, dst_mac_addr);
1140                         return rc;
1141                 }
1142
1143                 rc = efx_mae_action_set_alloc(sa->nic, action_set->spec,
1144                                               &fw_rsrc->aset_id);
1145                 if (rc != 0) {
1146                         sfc_err(sa, "failed to enable action_set=%p: %s",
1147                                 action_set, strerror(rc));
1148
1149                         (void)sfc_mae_counters_disable(sa, counters,
1150                                                        action_set->n_counters);
1151                         sfc_mae_encap_header_disable(sa, encap_header);
1152                         sfc_mae_mac_addr_disable(sa, src_mac_addr);
1153                         sfc_mae_mac_addr_disable(sa, dst_mac_addr);
1154                         return rc;
1155                 }
1156
1157                 sfc_dbg(sa, "enabled action_set=%p: AS_ID=0x%08x",
1158                         action_set, fw_rsrc->aset_id.id);
1159         }
1160
1161         ++(fw_rsrc->refcnt);
1162
1163         return 0;
1164 }
1165
1166 static void
1167 sfc_mae_action_set_disable(struct sfc_adapter *sa,
1168                            struct sfc_mae_action_set *action_set)
1169 {
1170         struct sfc_mae_fw_rsrc *fw_rsrc = &action_set->fw_rsrc;
1171         int rc;
1172
1173         SFC_ASSERT(sfc_adapter_is_locked(sa));
1174
1175         if (fw_rsrc->aset_id.id == EFX_MAE_RSRC_ID_INVALID ||
1176             fw_rsrc->refcnt == 0) {
1177                 sfc_err(sa, "failed to disable action_set=%p: already disabled; AS_ID=0x%08x, refcnt=%u",
1178                         action_set, fw_rsrc->aset_id.id, fw_rsrc->refcnt);
1179                 return;
1180         }
1181
1182         if (fw_rsrc->refcnt == 1) {
1183                 rc = efx_mae_action_set_free(sa->nic, &fw_rsrc->aset_id);
1184                 if (rc == 0) {
1185                         sfc_dbg(sa, "disabled action_set=%p with AS_ID=0x%08x",
1186                                 action_set, fw_rsrc->aset_id.id);
1187                 } else {
1188                         sfc_err(sa, "failed to disable action_set=%p with AS_ID=0x%08x: %s",
1189                                 action_set, fw_rsrc->aset_id.id, strerror(rc));
1190                 }
1191                 fw_rsrc->aset_id.id = EFX_MAE_RSRC_ID_INVALID;
1192
1193                 rc = sfc_mae_counters_disable(sa, action_set->counters,
1194                                               action_set->n_counters);
1195                 if (rc != 0) {
1196                         sfc_err(sa, "failed to disable %u MAE counters: %s",
1197                                 action_set->n_counters, rte_strerror(rc));
1198                 }
1199
1200                 sfc_mae_encap_header_disable(sa, action_set->encap_header);
1201                 sfc_mae_mac_addr_disable(sa, action_set->src_mac_addr);
1202                 sfc_mae_mac_addr_disable(sa, action_set->dst_mac_addr);
1203         }
1204
1205         --(fw_rsrc->refcnt);
1206 }
1207
1208 void
1209 sfc_mae_flow_cleanup(struct sfc_adapter *sa,
1210                      struct rte_flow *flow)
1211 {
1212         struct sfc_flow_spec_mae *spec_mae;
1213
1214         if (flow == NULL)
1215                 return;
1216
1217         spec_mae = &flow->spec.mae;
1218
1219         if (spec_mae->ft_ctx != NULL) {
1220                 if (spec_mae->ft_rule_type == SFC_FT_RULE_TUNNEL)
1221                         spec_mae->ft_ctx->tunnel_rule_is_set = B_FALSE;
1222
1223                 SFC_ASSERT(spec_mae->ft_ctx->refcnt != 0);
1224                 --(spec_mae->ft_ctx->refcnt);
1225         }
1226
1227         SFC_ASSERT(spec_mae->rule_id.id == EFX_MAE_RSRC_ID_INVALID);
1228
1229         if (spec_mae->outer_rule != NULL)
1230                 sfc_mae_outer_rule_del(sa, spec_mae->outer_rule);
1231
1232         if (spec_mae->action_set != NULL)
1233                 sfc_mae_action_set_del(sa, spec_mae->action_set);
1234
1235         if (spec_mae->match_spec != NULL)
1236                 efx_mae_match_spec_fini(sa->nic, spec_mae->match_spec);
1237 }
1238
1239 static int
1240 sfc_mae_set_ethertypes(struct sfc_mae_parse_ctx *ctx)
1241 {
1242         struct sfc_mae_pattern_data *pdata = &ctx->pattern_data;
1243         const efx_mae_field_id_t *fremap = ctx->field_ids_remap;
1244         const efx_mae_field_id_t field_ids[] = {
1245                 EFX_MAE_FIELD_VLAN0_PROTO_BE,
1246                 EFX_MAE_FIELD_VLAN1_PROTO_BE,
1247         };
1248         const struct sfc_mae_ethertype *et;
1249         unsigned int i;
1250         int rc;
1251
1252         /*
1253          * In accordance with RTE flow API convention, the innermost L2
1254          * item's "type" ("inner_type") is a L3 EtherType. If there is
1255          * no L3 item, it's 0x0000/0x0000.
1256          */
1257         et = &pdata->ethertypes[pdata->nb_vlan_tags];
1258         rc = efx_mae_match_spec_field_set(ctx->match_spec,
1259                                           fremap[EFX_MAE_FIELD_ETHER_TYPE_BE],
1260                                           sizeof(et->value),
1261                                           (const uint8_t *)&et->value,
1262                                           sizeof(et->mask),
1263                                           (const uint8_t *)&et->mask);
1264         if (rc != 0)
1265                 return rc;
1266
1267         /*
1268          * sfc_mae_rule_parse_item_vlan() has already made sure
1269          * that pdata->nb_vlan_tags does not exceed this figure.
1270          */
1271         RTE_BUILD_BUG_ON(SFC_MAE_MATCH_VLAN_MAX_NTAGS != 2);
1272
1273         for (i = 0; i < pdata->nb_vlan_tags; ++i) {
1274                 et = &pdata->ethertypes[i];
1275
1276                 rc = efx_mae_match_spec_field_set(ctx->match_spec,
1277                                                   fremap[field_ids[i]],
1278                                                   sizeof(et->value),
1279                                                   (const uint8_t *)&et->value,
1280                                                   sizeof(et->mask),
1281                                                   (const uint8_t *)&et->mask);
1282                 if (rc != 0)
1283                         return rc;
1284         }
1285
1286         return 0;
1287 }
1288
1289 static int
1290 sfc_mae_rule_process_pattern_data(struct sfc_mae_parse_ctx *ctx,
1291                                   struct rte_flow_error *error)
1292 {
1293         const efx_mae_field_id_t *fremap = ctx->field_ids_remap;
1294         struct sfc_mae_pattern_data *pdata = &ctx->pattern_data;
1295         struct sfc_mae_ethertype *ethertypes = pdata->ethertypes;
1296         const rte_be16_t supported_tpids[] = {
1297                 /* VLAN standard TPID (always the first element) */
1298                 RTE_BE16(RTE_ETHER_TYPE_VLAN),
1299
1300                 /* Double-tagging TPIDs */
1301                 RTE_BE16(RTE_ETHER_TYPE_QINQ),
1302                 RTE_BE16(RTE_ETHER_TYPE_QINQ1),
1303                 RTE_BE16(RTE_ETHER_TYPE_QINQ2),
1304                 RTE_BE16(RTE_ETHER_TYPE_QINQ3),
1305         };
1306         bool enforce_tag_presence[SFC_MAE_MATCH_VLAN_MAX_NTAGS] = {0};
1307         unsigned int nb_supported_tpids = RTE_DIM(supported_tpids);
1308         unsigned int ethertype_idx;
1309         const uint8_t *valuep;
1310         const uint8_t *maskp;
1311         int rc;
1312
1313         if (pdata->innermost_ethertype_restriction.mask != 0 &&
1314             pdata->nb_vlan_tags < SFC_MAE_MATCH_VLAN_MAX_NTAGS) {
1315                 /*
1316                  * If a single item VLAN is followed by a L3 item, value
1317                  * of "type" in item ETH can't be a double-tagging TPID.
1318                  */
1319                 nb_supported_tpids = 1;
1320         }
1321
1322         /*
1323          * sfc_mae_rule_parse_item_vlan() has already made sure
1324          * that pdata->nb_vlan_tags does not exceed this figure.
1325          */
1326         RTE_BUILD_BUG_ON(SFC_MAE_MATCH_VLAN_MAX_NTAGS != 2);
1327
1328         for (ethertype_idx = 0;
1329              ethertype_idx < pdata->nb_vlan_tags; ++ethertype_idx) {
1330                 rte_be16_t tpid_v = ethertypes[ethertype_idx].value;
1331                 rte_be16_t tpid_m = ethertypes[ethertype_idx].mask;
1332                 unsigned int tpid_idx;
1333
1334                 /*
1335                  * This loop can have only two iterations. On the second one,
1336                  * drop outer tag presence enforcement bit because the inner
1337                  * tag presence automatically assumes that for the outer tag.
1338                  */
1339                 enforce_tag_presence[0] = B_FALSE;
1340
1341                 if (tpid_m == RTE_BE16(0)) {
1342                         if (pdata->tci_masks[ethertype_idx] == RTE_BE16(0))
1343                                 enforce_tag_presence[ethertype_idx] = B_TRUE;
1344
1345                         /* No match on this field, and no value check. */
1346                         nb_supported_tpids = 1;
1347                         continue;
1348                 }
1349
1350                 /* Exact match is supported only. */
1351                 if (tpid_m != RTE_BE16(0xffff)) {
1352                         sfc_err(ctx->sa, "TPID mask must be 0x0 or 0xffff; got 0x%04x",
1353                                 rte_be_to_cpu_16(tpid_m));
1354                         rc = EINVAL;
1355                         goto fail;
1356                 }
1357
1358                 for (tpid_idx = pdata->nb_vlan_tags - ethertype_idx - 1;
1359                      tpid_idx < nb_supported_tpids; ++tpid_idx) {
1360                         if (tpid_v == supported_tpids[tpid_idx])
1361                                 break;
1362                 }
1363
1364                 if (tpid_idx == nb_supported_tpids) {
1365                         sfc_err(ctx->sa, "TPID 0x%04x is unsupported",
1366                                 rte_be_to_cpu_16(tpid_v));
1367                         rc = EINVAL;
1368                         goto fail;
1369                 }
1370
1371                 nb_supported_tpids = 1;
1372         }
1373
1374         if (pdata->innermost_ethertype_restriction.mask == RTE_BE16(0xffff)) {
1375                 struct sfc_mae_ethertype *et = &ethertypes[ethertype_idx];
1376                 rte_be16_t enforced_et;
1377
1378                 enforced_et = pdata->innermost_ethertype_restriction.value;
1379
1380                 if (et->mask == 0) {
1381                         et->mask = RTE_BE16(0xffff);
1382                         et->value = enforced_et;
1383                 } else if (et->mask != RTE_BE16(0xffff) ||
1384                            et->value != enforced_et) {
1385                         sfc_err(ctx->sa, "L3 EtherType must be 0x0/0x0 or 0x%04x/0xffff; got 0x%04x/0x%04x",
1386                                 rte_be_to_cpu_16(enforced_et),
1387                                 rte_be_to_cpu_16(et->value),
1388                                 rte_be_to_cpu_16(et->mask));
1389                         rc = EINVAL;
1390                         goto fail;
1391                 }
1392         }
1393
1394         /*
1395          * Now, when the number of VLAN tags is known, set fields
1396          * ETHER_TYPE, VLAN0_PROTO and VLAN1_PROTO so that the first
1397          * one is either a valid L3 EtherType (or 0x0000/0x0000),
1398          * and the last two are valid TPIDs (or 0x0000/0x0000).
1399          */
1400         rc = sfc_mae_set_ethertypes(ctx);
1401         if (rc != 0)
1402                 goto fail;
1403
1404         if (pdata->l3_next_proto_restriction_mask == 0xff) {
1405                 if (pdata->l3_next_proto_mask == 0) {
1406                         pdata->l3_next_proto_mask = 0xff;
1407                         pdata->l3_next_proto_value =
1408                                 pdata->l3_next_proto_restriction_value;
1409                 } else if (pdata->l3_next_proto_mask != 0xff ||
1410                            pdata->l3_next_proto_value !=
1411                            pdata->l3_next_proto_restriction_value) {
1412                         sfc_err(ctx->sa, "L3 next protocol must be 0x0/0x0 or 0x%02x/0xff; got 0x%02x/0x%02x",
1413                                 pdata->l3_next_proto_restriction_value,
1414                                 pdata->l3_next_proto_value,
1415                                 pdata->l3_next_proto_mask);
1416                         rc = EINVAL;
1417                         goto fail;
1418                 }
1419         }
1420
1421         if (enforce_tag_presence[0] || pdata->has_ovlan_mask) {
1422                 rc = efx_mae_match_spec_bit_set(ctx->match_spec,
1423                                                 fremap[EFX_MAE_FIELD_HAS_OVLAN],
1424                                                 enforce_tag_presence[0] ||
1425                                                 pdata->has_ovlan_value);
1426                 if (rc != 0)
1427                         goto fail;
1428         }
1429
1430         if (enforce_tag_presence[1] || pdata->has_ivlan_mask) {
1431                 rc = efx_mae_match_spec_bit_set(ctx->match_spec,
1432                                                 fremap[EFX_MAE_FIELD_HAS_IVLAN],
1433                                                 enforce_tag_presence[1] ||
1434                                                 pdata->has_ivlan_value);
1435                 if (rc != 0)
1436                         goto fail;
1437         }
1438
1439         valuep = (const uint8_t *)&pdata->l3_next_proto_value;
1440         maskp = (const uint8_t *)&pdata->l3_next_proto_mask;
1441         rc = efx_mae_match_spec_field_set(ctx->match_spec,
1442                                           fremap[EFX_MAE_FIELD_IP_PROTO],
1443                                           sizeof(pdata->l3_next_proto_value),
1444                                           valuep,
1445                                           sizeof(pdata->l3_next_proto_mask),
1446                                           maskp);
1447         if (rc != 0)
1448                 goto fail;
1449
1450         return 0;
1451
1452 fail:
1453         return rte_flow_error_set(error, rc, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1454                                   "Failed to process pattern data");
1455 }
1456
1457 static int
1458 sfc_mae_rule_parse_item_mark(const struct rte_flow_item *item,
1459                              struct sfc_flow_parse_ctx *ctx,
1460                              struct rte_flow_error *error)
1461 {
1462         const struct rte_flow_item_mark *spec = item->spec;
1463         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
1464         struct sfc_ft_ctx *ft_ctx = ctx_mae->ft_ctx;
1465
1466         if (spec == NULL) {
1467                 return rte_flow_error_set(error, EINVAL,
1468                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
1469                                 "NULL spec in item MARK");
1470         }
1471
1472         /*
1473          * This item is used in tunnel offload support only.
1474          * It must go before any network header items. This
1475          * way, sfc_mae_rule_preparse_item_mark() must have
1476          * already parsed it. Only one item MARK is allowed.
1477          */
1478         if (ctx_mae->ft_rule_type != SFC_FT_RULE_SWITCH ||
1479             spec->id != (uint32_t)SFC_FT_CTX_ID_TO_FLOW_MARK(ft_ctx->id)) {
1480                 return rte_flow_error_set(error, EINVAL,
1481                                           RTE_FLOW_ERROR_TYPE_ITEM,
1482                                           item, "invalid item MARK");
1483         }
1484
1485         return 0;
1486 }
1487
1488 static int
1489 sfc_mae_rule_parse_item_port_id(const struct rte_flow_item *item,
1490                                 struct sfc_flow_parse_ctx *ctx,
1491                                 struct rte_flow_error *error)
1492 {
1493         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
1494         const struct rte_flow_item_port_id supp_mask = {
1495                 .id = 0xffffffff,
1496         };
1497         const void *def_mask = &rte_flow_item_port_id_mask;
1498         const struct rte_flow_item_port_id *spec = NULL;
1499         const struct rte_flow_item_port_id *mask = NULL;
1500         efx_mport_sel_t mport_sel;
1501         int rc;
1502
1503         if (ctx_mae->match_mport_set) {
1504                 return rte_flow_error_set(error, ENOTSUP,
1505                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
1506                                 "Can't handle multiple traffic source items");
1507         }
1508
1509         rc = sfc_flow_parse_init(item,
1510                                  (const void **)&spec, (const void **)&mask,
1511                                  (const void *)&supp_mask, def_mask,
1512                                  sizeof(struct rte_flow_item_port_id), error);
1513         if (rc != 0)
1514                 return rc;
1515
1516         if (mask->id != supp_mask.id) {
1517                 return rte_flow_error_set(error, EINVAL,
1518                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
1519                                 "Bad mask in the PORT_ID pattern item");
1520         }
1521
1522         /* If "spec" is not set, could be any port ID */
1523         if (spec == NULL)
1524                 return 0;
1525
1526         if (spec->id > UINT16_MAX) {
1527                 return rte_flow_error_set(error, EOVERFLOW,
1528                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1529                                           "The port ID is too large");
1530         }
1531
1532         rc = sfc_mae_switch_get_ethdev_mport(ctx_mae->sa->mae.switch_domain_id,
1533                                              spec->id, &mport_sel);
1534         if (rc != 0) {
1535                 return rte_flow_error_set(error, rc,
1536                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
1537                                 "Can't get m-port for the given ethdev");
1538         }
1539
1540         rc = efx_mae_match_spec_mport_set(ctx_mae->match_spec,
1541                                           &mport_sel, NULL);
1542         if (rc != 0) {
1543                 return rte_flow_error_set(error, rc,
1544                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
1545                                 "Failed to set MPORT for the port ID");
1546         }
1547
1548         ctx_mae->match_mport_set = B_TRUE;
1549
1550         return 0;
1551 }
1552
1553 static int
1554 sfc_mae_rule_parse_item_ethdev_based(const struct rte_flow_item *item,
1555                                      struct sfc_flow_parse_ctx *ctx,
1556                                      struct rte_flow_error *error)
1557 {
1558         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
1559         const struct rte_flow_item_ethdev supp_mask = {
1560                 .port_id = 0xffff,
1561         };
1562         const void *def_mask = &rte_flow_item_ethdev_mask;
1563         const struct rte_flow_item_ethdev *spec = NULL;
1564         const struct rte_flow_item_ethdev *mask = NULL;
1565         efx_mport_sel_t mport_sel;
1566         int rc;
1567
1568         if (ctx_mae->match_mport_set) {
1569                 return rte_flow_error_set(error, ENOTSUP,
1570                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
1571                                 "Can't handle multiple traffic source items");
1572         }
1573
1574         rc = sfc_flow_parse_init(item,
1575                                  (const void **)&spec, (const void **)&mask,
1576                                  (const void *)&supp_mask, def_mask,
1577                                  sizeof(struct rte_flow_item_ethdev), error);
1578         if (rc != 0)
1579                 return rc;
1580
1581         if (mask->port_id != supp_mask.port_id) {
1582                 return rte_flow_error_set(error, EINVAL,
1583                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
1584                                 "Bad mask in the ethdev-based pattern item");
1585         }
1586
1587         /* If "spec" is not set, could be any port ID */
1588         if (spec == NULL)
1589                 return 0;
1590
1591         switch (item->type) {
1592         case RTE_FLOW_ITEM_TYPE_PORT_REPRESENTOR:
1593                 rc = sfc_mae_switch_get_ethdev_mport(
1594                                 ctx_mae->sa->mae.switch_domain_id,
1595                                 spec->port_id, &mport_sel);
1596                 if (rc != 0) {
1597                         return rte_flow_error_set(error, rc,
1598                                         RTE_FLOW_ERROR_TYPE_ITEM, item,
1599                                         "Can't get m-port for the given ethdev");
1600                 }
1601                 break;
1602         case RTE_FLOW_ITEM_TYPE_REPRESENTED_PORT:
1603                 rc = sfc_mae_switch_get_entity_mport(
1604                                 ctx_mae->sa->mae.switch_domain_id,
1605                                 spec->port_id, &mport_sel);
1606                 if (rc != 0) {
1607                         return rte_flow_error_set(error, rc,
1608                                         RTE_FLOW_ERROR_TYPE_ITEM, item,
1609                                         "Can't get m-port for the given ethdev");
1610                 }
1611                 break;
1612         default:
1613                 return rte_flow_error_set(error, EINVAL,
1614                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
1615                                 "Unsupported ethdev-based flow item");
1616         }
1617
1618         rc = efx_mae_match_spec_mport_set(ctx_mae->match_spec,
1619                                           &mport_sel, NULL);
1620         if (rc != 0) {
1621                 return rte_flow_error_set(error, rc,
1622                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
1623                                 "Failed to set MPORT for the port ID");
1624         }
1625
1626         ctx_mae->match_mport_set = B_TRUE;
1627
1628         return 0;
1629 }
1630
1631 static int
1632 sfc_mae_rule_parse_item_phy_port(const struct rte_flow_item *item,
1633                                  struct sfc_flow_parse_ctx *ctx,
1634                                  struct rte_flow_error *error)
1635 {
1636         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
1637         const struct rte_flow_item_phy_port supp_mask = {
1638                 .index = 0xffffffff,
1639         };
1640         const void *def_mask = &rte_flow_item_phy_port_mask;
1641         const struct rte_flow_item_phy_port *spec = NULL;
1642         const struct rte_flow_item_phy_port *mask = NULL;
1643         efx_mport_sel_t mport_v;
1644         int rc;
1645
1646         if (ctx_mae->match_mport_set) {
1647                 return rte_flow_error_set(error, ENOTSUP,
1648                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
1649                                 "Can't handle multiple traffic source items");
1650         }
1651
1652         rc = sfc_flow_parse_init(item,
1653                                  (const void **)&spec, (const void **)&mask,
1654                                  (const void *)&supp_mask, def_mask,
1655                                  sizeof(struct rte_flow_item_phy_port), error);
1656         if (rc != 0)
1657                 return rc;
1658
1659         if (mask->index != supp_mask.index) {
1660                 return rte_flow_error_set(error, EINVAL,
1661                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
1662                                 "Bad mask in the PHY_PORT pattern item");
1663         }
1664
1665         /* If "spec" is not set, could be any physical port */
1666         if (spec == NULL)
1667                 return 0;
1668
1669         rc = efx_mae_mport_by_phy_port(spec->index, &mport_v);
1670         if (rc != 0) {
1671                 return rte_flow_error_set(error, rc,
1672                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
1673                                 "Failed to convert the PHY_PORT index");
1674         }
1675
1676         rc = efx_mae_match_spec_mport_set(ctx_mae->match_spec, &mport_v, NULL);
1677         if (rc != 0) {
1678                 return rte_flow_error_set(error, rc,
1679                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
1680                                 "Failed to set MPORT for the PHY_PORT");
1681         }
1682
1683         ctx_mae->match_mport_set = B_TRUE;
1684
1685         return 0;
1686 }
1687
1688 static int
1689 sfc_mae_rule_parse_item_pf(const struct rte_flow_item *item,
1690                            struct sfc_flow_parse_ctx *ctx,
1691                            struct rte_flow_error *error)
1692 {
1693         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
1694         const efx_nic_cfg_t *encp = efx_nic_cfg_get(ctx_mae->sa->nic);
1695         efx_mport_sel_t mport_v;
1696         int rc;
1697
1698         if (ctx_mae->match_mport_set) {
1699                 return rte_flow_error_set(error, ENOTSUP,
1700                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
1701                                 "Can't handle multiple traffic source items");
1702         }
1703
1704         rc = efx_mae_mport_by_pcie_function(encp->enc_pf, EFX_PCI_VF_INVALID,
1705                                             &mport_v);
1706         if (rc != 0) {
1707                 return rte_flow_error_set(error, rc,
1708                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
1709                                 "Failed to convert the PF ID");
1710         }
1711
1712         rc = efx_mae_match_spec_mport_set(ctx_mae->match_spec, &mport_v, NULL);
1713         if (rc != 0) {
1714                 return rte_flow_error_set(error, rc,
1715                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
1716                                 "Failed to set MPORT for the PF");
1717         }
1718
1719         ctx_mae->match_mport_set = B_TRUE;
1720
1721         return 0;
1722 }
1723
1724 static int
1725 sfc_mae_rule_parse_item_vf(const struct rte_flow_item *item,
1726                            struct sfc_flow_parse_ctx *ctx,
1727                            struct rte_flow_error *error)
1728 {
1729         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
1730         const efx_nic_cfg_t *encp = efx_nic_cfg_get(ctx_mae->sa->nic);
1731         const struct rte_flow_item_vf supp_mask = {
1732                 .id = 0xffffffff,
1733         };
1734         const void *def_mask = &rte_flow_item_vf_mask;
1735         const struct rte_flow_item_vf *spec = NULL;
1736         const struct rte_flow_item_vf *mask = NULL;
1737         efx_mport_sel_t mport_v;
1738         int rc;
1739
1740         if (ctx_mae->match_mport_set) {
1741                 return rte_flow_error_set(error, ENOTSUP,
1742                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
1743                                 "Can't handle multiple traffic source items");
1744         }
1745
1746         rc = sfc_flow_parse_init(item,
1747                                  (const void **)&spec, (const void **)&mask,
1748                                  (const void *)&supp_mask, def_mask,
1749                                  sizeof(struct rte_flow_item_vf), error);
1750         if (rc != 0)
1751                 return rc;
1752
1753         if (mask->id != supp_mask.id) {
1754                 return rte_flow_error_set(error, EINVAL,
1755                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
1756                                 "Bad mask in the VF pattern item");
1757         }
1758
1759         /*
1760          * If "spec" is not set, the item requests any VF related to the
1761          * PF of the current DPDK port (but not the PF itself).
1762          * Reject this match criterion as unsupported.
1763          */
1764         if (spec == NULL) {
1765                 return rte_flow_error_set(error, EINVAL,
1766                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
1767                                 "Bad spec in the VF pattern item");
1768         }
1769
1770         rc = efx_mae_mport_by_pcie_function(encp->enc_pf, spec->id, &mport_v);
1771         if (rc != 0) {
1772                 return rte_flow_error_set(error, rc,
1773                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
1774                                 "Failed to convert the PF + VF IDs");
1775         }
1776
1777         rc = efx_mae_match_spec_mport_set(ctx_mae->match_spec, &mport_v, NULL);
1778         if (rc != 0) {
1779                 return rte_flow_error_set(error, rc,
1780                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
1781                                 "Failed to set MPORT for the PF + VF");
1782         }
1783
1784         ctx_mae->match_mport_set = B_TRUE;
1785
1786         return 0;
1787 }
1788
1789 /*
1790  * Having this field ID in a field locator means that this
1791  * locator cannot be used to actually set the field at the
1792  * time when the corresponding item gets encountered. Such
1793  * fields get stashed in the parsing context instead. This
1794  * is required to resolve dependencies between the stashed
1795  * fields. See sfc_mae_rule_process_pattern_data().
1796  */
1797 #define SFC_MAE_FIELD_HANDLING_DEFERRED EFX_MAE_FIELD_NIDS
1798
1799 struct sfc_mae_field_locator {
1800         efx_mae_field_id_t              field_id;
1801         size_t                          size;
1802         /* Field offset in the corresponding rte_flow_item_ struct */
1803         size_t                          ofst;
1804 };
1805
1806 static void
1807 sfc_mae_item_build_supp_mask(const struct sfc_mae_field_locator *field_locators,
1808                              unsigned int nb_field_locators, void *mask_ptr,
1809                              size_t mask_size)
1810 {
1811         unsigned int i;
1812
1813         memset(mask_ptr, 0, mask_size);
1814
1815         for (i = 0; i < nb_field_locators; ++i) {
1816                 const struct sfc_mae_field_locator *fl = &field_locators[i];
1817
1818                 SFC_ASSERT(fl->ofst + fl->size <= mask_size);
1819                 memset(RTE_PTR_ADD(mask_ptr, fl->ofst), 0xff, fl->size);
1820         }
1821 }
1822
1823 static int
1824 sfc_mae_parse_item(const struct sfc_mae_field_locator *field_locators,
1825                    unsigned int nb_field_locators, const uint8_t *spec,
1826                    const uint8_t *mask, struct sfc_mae_parse_ctx *ctx,
1827                    struct rte_flow_error *error)
1828 {
1829         const efx_mae_field_id_t *fremap = ctx->field_ids_remap;
1830         unsigned int i;
1831         int rc = 0;
1832
1833         for (i = 0; i < nb_field_locators; ++i) {
1834                 const struct sfc_mae_field_locator *fl = &field_locators[i];
1835
1836                 if (fl->field_id == SFC_MAE_FIELD_HANDLING_DEFERRED)
1837                         continue;
1838
1839                 rc = efx_mae_match_spec_field_set(ctx->match_spec,
1840                                                   fremap[fl->field_id],
1841                                                   fl->size, spec + fl->ofst,
1842                                                   fl->size, mask + fl->ofst);
1843                 if (rc != 0)
1844                         break;
1845         }
1846
1847         if (rc != 0) {
1848                 rc = rte_flow_error_set(error, rc, RTE_FLOW_ERROR_TYPE_ITEM,
1849                                 NULL, "Failed to process item fields");
1850         }
1851
1852         return rc;
1853 }
1854
1855 static const struct sfc_mae_field_locator flocs_eth[] = {
1856         {
1857                 /*
1858                  * This locator is used only for building supported fields mask.
1859                  * The field is handled by sfc_mae_rule_process_pattern_data().
1860                  */
1861                 SFC_MAE_FIELD_HANDLING_DEFERRED,
1862                 RTE_SIZEOF_FIELD(struct rte_flow_item_eth, type),
1863                 offsetof(struct rte_flow_item_eth, type),
1864         },
1865         {
1866                 EFX_MAE_FIELD_ETH_DADDR_BE,
1867                 RTE_SIZEOF_FIELD(struct rte_flow_item_eth, dst),
1868                 offsetof(struct rte_flow_item_eth, dst),
1869         },
1870         {
1871                 EFX_MAE_FIELD_ETH_SADDR_BE,
1872                 RTE_SIZEOF_FIELD(struct rte_flow_item_eth, src),
1873                 offsetof(struct rte_flow_item_eth, src),
1874         },
1875 };
1876
1877 static int
1878 sfc_mae_rule_parse_item_eth(const struct rte_flow_item *item,
1879                             struct sfc_flow_parse_ctx *ctx,
1880                             struct rte_flow_error *error)
1881 {
1882         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
1883         struct rte_flow_item_eth override_mask;
1884         struct rte_flow_item_eth supp_mask;
1885         const uint8_t *spec = NULL;
1886         const uint8_t *mask = NULL;
1887         int rc;
1888
1889         sfc_mae_item_build_supp_mask(flocs_eth, RTE_DIM(flocs_eth),
1890                                      &supp_mask, sizeof(supp_mask));
1891         supp_mask.has_vlan = 1;
1892
1893         rc = sfc_flow_parse_init(item,
1894                                  (const void **)&spec, (const void **)&mask,
1895                                  (const void *)&supp_mask,
1896                                  &rte_flow_item_eth_mask,
1897                                  sizeof(struct rte_flow_item_eth), error);
1898         if (rc != 0)
1899                 return rc;
1900
1901         if (ctx_mae->ft_rule_type == SFC_FT_RULE_TUNNEL && mask != NULL) {
1902                 /*
1903                  * The HW/FW hasn't got support for match on MAC addresses in
1904                  * outer rules yet (this will change). Match on VLAN presence
1905                  * isn't supported either. Ignore these match criteria.
1906                  */
1907                 memcpy(&override_mask, mask, sizeof(override_mask));
1908                 memset(&override_mask.hdr.dst_addr, 0,
1909                        sizeof(override_mask.hdr.dst_addr));
1910                 memset(&override_mask.hdr.src_addr, 0,
1911                        sizeof(override_mask.hdr.src_addr));
1912                 override_mask.has_vlan = 0;
1913
1914                 mask = (const uint8_t *)&override_mask;
1915         }
1916
1917         if (spec != NULL) {
1918                 struct sfc_mae_pattern_data *pdata = &ctx_mae->pattern_data;
1919                 struct sfc_mae_ethertype *ethertypes = pdata->ethertypes;
1920                 const struct rte_flow_item_eth *item_spec;
1921                 const struct rte_flow_item_eth *item_mask;
1922
1923                 item_spec = (const struct rte_flow_item_eth *)spec;
1924                 item_mask = (const struct rte_flow_item_eth *)mask;
1925
1926                 /*
1927                  * Remember various match criteria in the parsing context.
1928                  * sfc_mae_rule_process_pattern_data() will consider them
1929                  * altogether when the rest of the items have been parsed.
1930                  */
1931                 ethertypes[0].value = item_spec->type;
1932                 ethertypes[0].mask = item_mask->type;
1933                 if (item_mask->has_vlan) {
1934                         pdata->has_ovlan_mask = B_TRUE;
1935                         if (item_spec->has_vlan)
1936                                 pdata->has_ovlan_value = B_TRUE;
1937                 }
1938         } else {
1939                 /*
1940                  * The specification is empty. The overall pattern
1941                  * validity will be enforced at the end of parsing.
1942                  * See sfc_mae_rule_process_pattern_data().
1943                  */
1944                 return 0;
1945         }
1946
1947         return sfc_mae_parse_item(flocs_eth, RTE_DIM(flocs_eth), spec, mask,
1948                                   ctx_mae, error);
1949 }
1950
1951 static const struct sfc_mae_field_locator flocs_vlan[] = {
1952         /* Outermost tag */
1953         {
1954                 EFX_MAE_FIELD_VLAN0_TCI_BE,
1955                 RTE_SIZEOF_FIELD(struct rte_flow_item_vlan, tci),
1956                 offsetof(struct rte_flow_item_vlan, tci),
1957         },
1958         {
1959                 /*
1960                  * This locator is used only for building supported fields mask.
1961                  * The field is handled by sfc_mae_rule_process_pattern_data().
1962                  */
1963                 SFC_MAE_FIELD_HANDLING_DEFERRED,
1964                 RTE_SIZEOF_FIELD(struct rte_flow_item_vlan, inner_type),
1965                 offsetof(struct rte_flow_item_vlan, inner_type),
1966         },
1967
1968         /* Innermost tag */
1969         {
1970                 EFX_MAE_FIELD_VLAN1_TCI_BE,
1971                 RTE_SIZEOF_FIELD(struct rte_flow_item_vlan, tci),
1972                 offsetof(struct rte_flow_item_vlan, tci),
1973         },
1974         {
1975                 /*
1976                  * This locator is used only for building supported fields mask.
1977                  * The field is handled by sfc_mae_rule_process_pattern_data().
1978                  */
1979                 SFC_MAE_FIELD_HANDLING_DEFERRED,
1980                 RTE_SIZEOF_FIELD(struct rte_flow_item_vlan, inner_type),
1981                 offsetof(struct rte_flow_item_vlan, inner_type),
1982         },
1983 };
1984
1985 static int
1986 sfc_mae_rule_parse_item_vlan(const struct rte_flow_item *item,
1987                              struct sfc_flow_parse_ctx *ctx,
1988                              struct rte_flow_error *error)
1989 {
1990         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
1991         struct sfc_mae_pattern_data *pdata = &ctx_mae->pattern_data;
1992         boolean_t *has_vlan_mp_by_nb_tags[SFC_MAE_MATCH_VLAN_MAX_NTAGS] = {
1993                 &pdata->has_ovlan_mask,
1994                 &pdata->has_ivlan_mask,
1995         };
1996         boolean_t *has_vlan_vp_by_nb_tags[SFC_MAE_MATCH_VLAN_MAX_NTAGS] = {
1997                 &pdata->has_ovlan_value,
1998                 &pdata->has_ivlan_value,
1999         };
2000         boolean_t *cur_tag_presence_bit_mp;
2001         boolean_t *cur_tag_presence_bit_vp;
2002         const struct sfc_mae_field_locator *flocs;
2003         struct rte_flow_item_vlan supp_mask;
2004         const uint8_t *spec = NULL;
2005         const uint8_t *mask = NULL;
2006         unsigned int nb_flocs;
2007         int rc;
2008
2009         RTE_BUILD_BUG_ON(SFC_MAE_MATCH_VLAN_MAX_NTAGS != 2);
2010
2011         if (pdata->nb_vlan_tags == SFC_MAE_MATCH_VLAN_MAX_NTAGS) {
2012                 return rte_flow_error_set(error, ENOTSUP,
2013                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
2014                                 "Can't match that many VLAN tags");
2015         }
2016
2017         cur_tag_presence_bit_mp = has_vlan_mp_by_nb_tags[pdata->nb_vlan_tags];
2018         cur_tag_presence_bit_vp = has_vlan_vp_by_nb_tags[pdata->nb_vlan_tags];
2019
2020         if (*cur_tag_presence_bit_mp == B_TRUE &&
2021             *cur_tag_presence_bit_vp == B_FALSE) {
2022                 return rte_flow_error_set(error, EINVAL,
2023                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
2024                                 "The previous item enforces no (more) VLAN, "
2025                                 "so the current item (VLAN) must not exist");
2026         }
2027
2028         nb_flocs = RTE_DIM(flocs_vlan) / SFC_MAE_MATCH_VLAN_MAX_NTAGS;
2029         flocs = flocs_vlan + pdata->nb_vlan_tags * nb_flocs;
2030
2031         sfc_mae_item_build_supp_mask(flocs, nb_flocs,
2032                                      &supp_mask, sizeof(supp_mask));
2033         /*
2034          * This only means that the field is supported by the driver and libefx.
2035          * Support on NIC level will be checked when all items have been parsed.
2036          */
2037         supp_mask.has_more_vlan = 1;
2038
2039         rc = sfc_flow_parse_init(item,
2040                                  (const void **)&spec, (const void **)&mask,
2041                                  (const void *)&supp_mask,
2042                                  &rte_flow_item_vlan_mask,
2043                                  sizeof(struct rte_flow_item_vlan), error);
2044         if (rc != 0)
2045                 return rc;
2046
2047         if (spec != NULL) {
2048                 struct sfc_mae_ethertype *et = pdata->ethertypes;
2049                 const struct rte_flow_item_vlan *item_spec;
2050                 const struct rte_flow_item_vlan *item_mask;
2051
2052                 item_spec = (const struct rte_flow_item_vlan *)spec;
2053                 item_mask = (const struct rte_flow_item_vlan *)mask;
2054
2055                 /*
2056                  * Remember various match criteria in the parsing context.
2057                  * sfc_mae_rule_process_pattern_data() will consider them
2058                  * altogether when the rest of the items have been parsed.
2059                  */
2060                 et[pdata->nb_vlan_tags + 1].value = item_spec->inner_type;
2061                 et[pdata->nb_vlan_tags + 1].mask = item_mask->inner_type;
2062                 pdata->tci_masks[pdata->nb_vlan_tags] = item_mask->tci;
2063                 if (item_mask->has_more_vlan) {
2064                         if (pdata->nb_vlan_tags ==
2065                             SFC_MAE_MATCH_VLAN_MAX_NTAGS) {
2066                                 return rte_flow_error_set(error, ENOTSUP,
2067                                         RTE_FLOW_ERROR_TYPE_ITEM, item,
2068                                         "Can't use 'has_more_vlan' in "
2069                                         "the second item VLAN");
2070                         }
2071                         pdata->has_ivlan_mask = B_TRUE;
2072                         if (item_spec->has_more_vlan)
2073                                 pdata->has_ivlan_value = B_TRUE;
2074                 }
2075
2076                 /* Convert TCI to MAE representation right now. */
2077                 rc = sfc_mae_parse_item(flocs, nb_flocs, spec, mask,
2078                                         ctx_mae, error);
2079                 if (rc != 0)
2080                         return rc;
2081         }
2082
2083         ++(pdata->nb_vlan_tags);
2084
2085         return 0;
2086 }
2087
2088 static const struct sfc_mae_field_locator flocs_ipv4[] = {
2089         {
2090                 EFX_MAE_FIELD_SRC_IP4_BE,
2091                 RTE_SIZEOF_FIELD(struct rte_flow_item_ipv4, hdr.src_addr),
2092                 offsetof(struct rte_flow_item_ipv4, hdr.src_addr),
2093         },
2094         {
2095                 EFX_MAE_FIELD_DST_IP4_BE,
2096                 RTE_SIZEOF_FIELD(struct rte_flow_item_ipv4, hdr.dst_addr),
2097                 offsetof(struct rte_flow_item_ipv4, hdr.dst_addr),
2098         },
2099         {
2100                 /*
2101                  * This locator is used only for building supported fields mask.
2102                  * The field is handled by sfc_mae_rule_process_pattern_data().
2103                  */
2104                 SFC_MAE_FIELD_HANDLING_DEFERRED,
2105                 RTE_SIZEOF_FIELD(struct rte_flow_item_ipv4, hdr.next_proto_id),
2106                 offsetof(struct rte_flow_item_ipv4, hdr.next_proto_id),
2107         },
2108         {
2109                 EFX_MAE_FIELD_IP_TOS,
2110                 RTE_SIZEOF_FIELD(struct rte_flow_item_ipv4,
2111                                  hdr.type_of_service),
2112                 offsetof(struct rte_flow_item_ipv4, hdr.type_of_service),
2113         },
2114         {
2115                 EFX_MAE_FIELD_IP_TTL,
2116                 RTE_SIZEOF_FIELD(struct rte_flow_item_ipv4, hdr.time_to_live),
2117                 offsetof(struct rte_flow_item_ipv4, hdr.time_to_live),
2118         },
2119 };
2120
2121 static int
2122 sfc_mae_rule_parse_item_ipv4(const struct rte_flow_item *item,
2123                              struct sfc_flow_parse_ctx *ctx,
2124                              struct rte_flow_error *error)
2125 {
2126         rte_be16_t ethertype_ipv4_be = RTE_BE16(RTE_ETHER_TYPE_IPV4);
2127         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
2128         struct sfc_mae_pattern_data *pdata = &ctx_mae->pattern_data;
2129         struct rte_flow_item_ipv4 supp_mask;
2130         const uint8_t *spec = NULL;
2131         const uint8_t *mask = NULL;
2132         int rc;
2133
2134         sfc_mae_item_build_supp_mask(flocs_ipv4, RTE_DIM(flocs_ipv4),
2135                                      &supp_mask, sizeof(supp_mask));
2136
2137         rc = sfc_flow_parse_init(item,
2138                                  (const void **)&spec, (const void **)&mask,
2139                                  (const void *)&supp_mask,
2140                                  &rte_flow_item_ipv4_mask,
2141                                  sizeof(struct rte_flow_item_ipv4), error);
2142         if (rc != 0)
2143                 return rc;
2144
2145         pdata->innermost_ethertype_restriction.value = ethertype_ipv4_be;
2146         pdata->innermost_ethertype_restriction.mask = RTE_BE16(0xffff);
2147
2148         if (spec != NULL) {
2149                 const struct rte_flow_item_ipv4 *item_spec;
2150                 const struct rte_flow_item_ipv4 *item_mask;
2151
2152                 item_spec = (const struct rte_flow_item_ipv4 *)spec;
2153                 item_mask = (const struct rte_flow_item_ipv4 *)mask;
2154
2155                 pdata->l3_next_proto_value = item_spec->hdr.next_proto_id;
2156                 pdata->l3_next_proto_mask = item_mask->hdr.next_proto_id;
2157         } else {
2158                 return 0;
2159         }
2160
2161         return sfc_mae_parse_item(flocs_ipv4, RTE_DIM(flocs_ipv4), spec, mask,
2162                                   ctx_mae, error);
2163 }
2164
2165 static const struct sfc_mae_field_locator flocs_ipv6[] = {
2166         {
2167                 EFX_MAE_FIELD_SRC_IP6_BE,
2168                 RTE_SIZEOF_FIELD(struct rte_flow_item_ipv6, hdr.src_addr),
2169                 offsetof(struct rte_flow_item_ipv6, hdr.src_addr),
2170         },
2171         {
2172                 EFX_MAE_FIELD_DST_IP6_BE,
2173                 RTE_SIZEOF_FIELD(struct rte_flow_item_ipv6, hdr.dst_addr),
2174                 offsetof(struct rte_flow_item_ipv6, hdr.dst_addr),
2175         },
2176         {
2177                 /*
2178                  * This locator is used only for building supported fields mask.
2179                  * The field is handled by sfc_mae_rule_process_pattern_data().
2180                  */
2181                 SFC_MAE_FIELD_HANDLING_DEFERRED,
2182                 RTE_SIZEOF_FIELD(struct rte_flow_item_ipv6, hdr.proto),
2183                 offsetof(struct rte_flow_item_ipv6, hdr.proto),
2184         },
2185         {
2186                 EFX_MAE_FIELD_IP_TTL,
2187                 RTE_SIZEOF_FIELD(struct rte_flow_item_ipv6, hdr.hop_limits),
2188                 offsetof(struct rte_flow_item_ipv6, hdr.hop_limits),
2189         },
2190 };
2191
2192 static int
2193 sfc_mae_rule_parse_item_ipv6(const struct rte_flow_item *item,
2194                              struct sfc_flow_parse_ctx *ctx,
2195                              struct rte_flow_error *error)
2196 {
2197         rte_be16_t ethertype_ipv6_be = RTE_BE16(RTE_ETHER_TYPE_IPV6);
2198         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
2199         const efx_mae_field_id_t *fremap = ctx_mae->field_ids_remap;
2200         struct sfc_mae_pattern_data *pdata = &ctx_mae->pattern_data;
2201         struct rte_flow_item_ipv6 supp_mask;
2202         const uint8_t *spec = NULL;
2203         const uint8_t *mask = NULL;
2204         rte_be32_t vtc_flow_be;
2205         uint32_t vtc_flow;
2206         uint8_t tc_value;
2207         uint8_t tc_mask;
2208         int rc;
2209
2210         sfc_mae_item_build_supp_mask(flocs_ipv6, RTE_DIM(flocs_ipv6),
2211                                      &supp_mask, sizeof(supp_mask));
2212
2213         vtc_flow_be = RTE_BE32(RTE_IPV6_HDR_TC_MASK);
2214         memcpy(&supp_mask, &vtc_flow_be, sizeof(vtc_flow_be));
2215
2216         rc = sfc_flow_parse_init(item,
2217                                  (const void **)&spec, (const void **)&mask,
2218                                  (const void *)&supp_mask,
2219                                  &rte_flow_item_ipv6_mask,
2220                                  sizeof(struct rte_flow_item_ipv6), error);
2221         if (rc != 0)
2222                 return rc;
2223
2224         pdata->innermost_ethertype_restriction.value = ethertype_ipv6_be;
2225         pdata->innermost_ethertype_restriction.mask = RTE_BE16(0xffff);
2226
2227         if (spec != NULL) {
2228                 const struct rte_flow_item_ipv6 *item_spec;
2229                 const struct rte_flow_item_ipv6 *item_mask;
2230
2231                 item_spec = (const struct rte_flow_item_ipv6 *)spec;
2232                 item_mask = (const struct rte_flow_item_ipv6 *)mask;
2233
2234                 pdata->l3_next_proto_value = item_spec->hdr.proto;
2235                 pdata->l3_next_proto_mask = item_mask->hdr.proto;
2236         } else {
2237                 return 0;
2238         }
2239
2240         rc = sfc_mae_parse_item(flocs_ipv6, RTE_DIM(flocs_ipv6), spec, mask,
2241                                 ctx_mae, error);
2242         if (rc != 0)
2243                 return rc;
2244
2245         memcpy(&vtc_flow_be, spec, sizeof(vtc_flow_be));
2246         vtc_flow = rte_be_to_cpu_32(vtc_flow_be);
2247         tc_value = (vtc_flow & RTE_IPV6_HDR_TC_MASK) >> RTE_IPV6_HDR_TC_SHIFT;
2248
2249         memcpy(&vtc_flow_be, mask, sizeof(vtc_flow_be));
2250         vtc_flow = rte_be_to_cpu_32(vtc_flow_be);
2251         tc_mask = (vtc_flow & RTE_IPV6_HDR_TC_MASK) >> RTE_IPV6_HDR_TC_SHIFT;
2252
2253         rc = efx_mae_match_spec_field_set(ctx_mae->match_spec,
2254                                           fremap[EFX_MAE_FIELD_IP_TOS],
2255                                           sizeof(tc_value), &tc_value,
2256                                           sizeof(tc_mask), &tc_mask);
2257         if (rc != 0) {
2258                 return rte_flow_error_set(error, rc, RTE_FLOW_ERROR_TYPE_ITEM,
2259                                 NULL, "Failed to process item fields");
2260         }
2261
2262         return 0;
2263 }
2264
2265 static const struct sfc_mae_field_locator flocs_tcp[] = {
2266         {
2267                 EFX_MAE_FIELD_L4_SPORT_BE,
2268                 RTE_SIZEOF_FIELD(struct rte_flow_item_tcp, hdr.src_port),
2269                 offsetof(struct rte_flow_item_tcp, hdr.src_port),
2270         },
2271         {
2272                 EFX_MAE_FIELD_L4_DPORT_BE,
2273                 RTE_SIZEOF_FIELD(struct rte_flow_item_tcp, hdr.dst_port),
2274                 offsetof(struct rte_flow_item_tcp, hdr.dst_port),
2275         },
2276         {
2277                 EFX_MAE_FIELD_TCP_FLAGS_BE,
2278                 /*
2279                  * The values have been picked intentionally since the
2280                  * target MAE field is oversize (16 bit). This mapping
2281                  * relies on the fact that the MAE field is big-endian.
2282                  */
2283                 RTE_SIZEOF_FIELD(struct rte_flow_item_tcp, hdr.data_off) +
2284                 RTE_SIZEOF_FIELD(struct rte_flow_item_tcp, hdr.tcp_flags),
2285                 offsetof(struct rte_flow_item_tcp, hdr.data_off),
2286         },
2287 };
2288
2289 static int
2290 sfc_mae_rule_parse_item_tcp(const struct rte_flow_item *item,
2291                             struct sfc_flow_parse_ctx *ctx,
2292                             struct rte_flow_error *error)
2293 {
2294         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
2295         struct sfc_mae_pattern_data *pdata = &ctx_mae->pattern_data;
2296         struct rte_flow_item_tcp supp_mask;
2297         const uint8_t *spec = NULL;
2298         const uint8_t *mask = NULL;
2299         int rc;
2300
2301         /*
2302          * When encountered among outermost items, item TCP is invalid.
2303          * Check which match specification is being constructed now.
2304          */
2305         if (ctx_mae->match_spec != ctx_mae->match_spec_action) {
2306                 return rte_flow_error_set(error, EINVAL,
2307                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2308                                           "TCP in outer frame is invalid");
2309         }
2310
2311         sfc_mae_item_build_supp_mask(flocs_tcp, RTE_DIM(flocs_tcp),
2312                                      &supp_mask, sizeof(supp_mask));
2313
2314         rc = sfc_flow_parse_init(item,
2315                                  (const void **)&spec, (const void **)&mask,
2316                                  (const void *)&supp_mask,
2317                                  &rte_flow_item_tcp_mask,
2318                                  sizeof(struct rte_flow_item_tcp), error);
2319         if (rc != 0)
2320                 return rc;
2321
2322         pdata->l3_next_proto_restriction_value = IPPROTO_TCP;
2323         pdata->l3_next_proto_restriction_mask = 0xff;
2324
2325         if (spec == NULL)
2326                 return 0;
2327
2328         return sfc_mae_parse_item(flocs_tcp, RTE_DIM(flocs_tcp), spec, mask,
2329                                   ctx_mae, error);
2330 }
2331
2332 static const struct sfc_mae_field_locator flocs_udp[] = {
2333         {
2334                 EFX_MAE_FIELD_L4_SPORT_BE,
2335                 RTE_SIZEOF_FIELD(struct rte_flow_item_udp, hdr.src_port),
2336                 offsetof(struct rte_flow_item_udp, hdr.src_port),
2337         },
2338         {
2339                 EFX_MAE_FIELD_L4_DPORT_BE,
2340                 RTE_SIZEOF_FIELD(struct rte_flow_item_udp, hdr.dst_port),
2341                 offsetof(struct rte_flow_item_udp, hdr.dst_port),
2342         },
2343 };
2344
2345 static int
2346 sfc_mae_rule_parse_item_udp(const struct rte_flow_item *item,
2347                             struct sfc_flow_parse_ctx *ctx,
2348                             struct rte_flow_error *error)
2349 {
2350         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
2351         struct sfc_mae_pattern_data *pdata = &ctx_mae->pattern_data;
2352         struct rte_flow_item_udp supp_mask;
2353         const uint8_t *spec = NULL;
2354         const uint8_t *mask = NULL;
2355         int rc;
2356
2357         sfc_mae_item_build_supp_mask(flocs_udp, RTE_DIM(flocs_udp),
2358                                      &supp_mask, sizeof(supp_mask));
2359
2360         rc = sfc_flow_parse_init(item,
2361                                  (const void **)&spec, (const void **)&mask,
2362                                  (const void *)&supp_mask,
2363                                  &rte_flow_item_udp_mask,
2364                                  sizeof(struct rte_flow_item_udp), error);
2365         if (rc != 0)
2366                 return rc;
2367
2368         pdata->l3_next_proto_restriction_value = IPPROTO_UDP;
2369         pdata->l3_next_proto_restriction_mask = 0xff;
2370
2371         if (spec == NULL)
2372                 return 0;
2373
2374         return sfc_mae_parse_item(flocs_udp, RTE_DIM(flocs_udp), spec, mask,
2375                                   ctx_mae, error);
2376 }
2377
2378 static const struct sfc_mae_field_locator flocs_tunnel[] = {
2379         {
2380                 /*
2381                  * The size and offset values are relevant
2382                  * for Geneve and NVGRE, too.
2383                  */
2384                 .size = RTE_SIZEOF_FIELD(struct rte_flow_item_vxlan, vni),
2385                 .ofst = offsetof(struct rte_flow_item_vxlan, vni),
2386         },
2387 };
2388
2389 /*
2390  * An auxiliary registry which allows using non-encap. field IDs
2391  * directly when building a match specification of type ACTION.
2392  *
2393  * See sfc_mae_rule_parse_pattern() and sfc_mae_rule_parse_item_tunnel().
2394  */
2395 static const efx_mae_field_id_t field_ids_no_remap[] = {
2396 #define FIELD_ID_NO_REMAP(_field) \
2397         [EFX_MAE_FIELD_##_field] = EFX_MAE_FIELD_##_field
2398
2399         FIELD_ID_NO_REMAP(ETHER_TYPE_BE),
2400         FIELD_ID_NO_REMAP(ETH_SADDR_BE),
2401         FIELD_ID_NO_REMAP(ETH_DADDR_BE),
2402         FIELD_ID_NO_REMAP(VLAN0_TCI_BE),
2403         FIELD_ID_NO_REMAP(VLAN0_PROTO_BE),
2404         FIELD_ID_NO_REMAP(VLAN1_TCI_BE),
2405         FIELD_ID_NO_REMAP(VLAN1_PROTO_BE),
2406         FIELD_ID_NO_REMAP(SRC_IP4_BE),
2407         FIELD_ID_NO_REMAP(DST_IP4_BE),
2408         FIELD_ID_NO_REMAP(IP_PROTO),
2409         FIELD_ID_NO_REMAP(IP_TOS),
2410         FIELD_ID_NO_REMAP(IP_TTL),
2411         FIELD_ID_NO_REMAP(SRC_IP6_BE),
2412         FIELD_ID_NO_REMAP(DST_IP6_BE),
2413         FIELD_ID_NO_REMAP(L4_SPORT_BE),
2414         FIELD_ID_NO_REMAP(L4_DPORT_BE),
2415         FIELD_ID_NO_REMAP(TCP_FLAGS_BE),
2416         FIELD_ID_NO_REMAP(HAS_OVLAN),
2417         FIELD_ID_NO_REMAP(HAS_IVLAN),
2418
2419 #undef FIELD_ID_NO_REMAP
2420 };
2421
2422 /*
2423  * An auxiliary registry which allows using "ENC" field IDs
2424  * when building a match specification of type OUTER.
2425  *
2426  * See sfc_mae_rule_encap_parse_init().
2427  */
2428 static const efx_mae_field_id_t field_ids_remap_to_encap[] = {
2429 #define FIELD_ID_REMAP_TO_ENCAP(_field) \
2430         [EFX_MAE_FIELD_##_field] = EFX_MAE_FIELD_ENC_##_field
2431
2432         FIELD_ID_REMAP_TO_ENCAP(ETHER_TYPE_BE),
2433         FIELD_ID_REMAP_TO_ENCAP(ETH_SADDR_BE),
2434         FIELD_ID_REMAP_TO_ENCAP(ETH_DADDR_BE),
2435         FIELD_ID_REMAP_TO_ENCAP(VLAN0_TCI_BE),
2436         FIELD_ID_REMAP_TO_ENCAP(VLAN0_PROTO_BE),
2437         FIELD_ID_REMAP_TO_ENCAP(VLAN1_TCI_BE),
2438         FIELD_ID_REMAP_TO_ENCAP(VLAN1_PROTO_BE),
2439         FIELD_ID_REMAP_TO_ENCAP(SRC_IP4_BE),
2440         FIELD_ID_REMAP_TO_ENCAP(DST_IP4_BE),
2441         FIELD_ID_REMAP_TO_ENCAP(IP_PROTO),
2442         FIELD_ID_REMAP_TO_ENCAP(IP_TOS),
2443         FIELD_ID_REMAP_TO_ENCAP(IP_TTL),
2444         FIELD_ID_REMAP_TO_ENCAP(SRC_IP6_BE),
2445         FIELD_ID_REMAP_TO_ENCAP(DST_IP6_BE),
2446         FIELD_ID_REMAP_TO_ENCAP(L4_SPORT_BE),
2447         FIELD_ID_REMAP_TO_ENCAP(L4_DPORT_BE),
2448         FIELD_ID_REMAP_TO_ENCAP(HAS_OVLAN),
2449         FIELD_ID_REMAP_TO_ENCAP(HAS_IVLAN),
2450
2451 #undef FIELD_ID_REMAP_TO_ENCAP
2452 };
2453
2454 static int
2455 sfc_mae_rule_parse_item_tunnel(const struct rte_flow_item *item,
2456                                struct sfc_flow_parse_ctx *ctx,
2457                                struct rte_flow_error *error)
2458 {
2459         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
2460         uint8_t vnet_id_v[sizeof(uint32_t)] = {0};
2461         uint8_t vnet_id_m[sizeof(uint32_t)] = {0};
2462         const struct rte_flow_item_vxlan *vxp;
2463         uint8_t supp_mask[sizeof(uint64_t)];
2464         const uint8_t *spec = NULL;
2465         const uint8_t *mask = NULL;
2466         int rc;
2467
2468         if (ctx_mae->ft_rule_type == SFC_FT_RULE_SWITCH) {
2469                 /*
2470                  * As a workaround, pattern processing has started from
2471                  * this (tunnel) item. No pattern data to process yet.
2472                  */
2473         } else {
2474                 /*
2475                  * We're about to start processing inner frame items.
2476                  * Process pattern data that has been deferred so far
2477                  * and reset pattern data storage.
2478                  */
2479                 rc = sfc_mae_rule_process_pattern_data(ctx_mae, error);
2480                 if (rc != 0)
2481                         return rc;
2482         }
2483
2484         memset(&ctx_mae->pattern_data, 0, sizeof(ctx_mae->pattern_data));
2485
2486         sfc_mae_item_build_supp_mask(flocs_tunnel, RTE_DIM(flocs_tunnel),
2487                                      &supp_mask, sizeof(supp_mask));
2488
2489         /*
2490          * This tunnel item was preliminarily detected by
2491          * sfc_mae_rule_encap_parse_init(). Default mask
2492          * was also picked by that helper. Use it here.
2493          */
2494         rc = sfc_flow_parse_init(item,
2495                                  (const void **)&spec, (const void **)&mask,
2496                                  (const void *)&supp_mask,
2497                                  ctx_mae->tunnel_def_mask,
2498                                  ctx_mae->tunnel_def_mask_size,  error);
2499         if (rc != 0)
2500                 return rc;
2501
2502         /*
2503          * This item and later ones comprise a
2504          * match specification of type ACTION.
2505          */
2506         ctx_mae->match_spec = ctx_mae->match_spec_action;
2507
2508         /* This item and later ones use non-encap. EFX MAE field IDs. */
2509         ctx_mae->field_ids_remap = field_ids_no_remap;
2510
2511         if (spec == NULL)
2512                 return 0;
2513
2514         /*
2515          * Field EFX_MAE_FIELD_ENC_VNET_ID_BE is a 32-bit one.
2516          * Copy 24-bit VNI, which is BE, at offset 1 in it.
2517          * The extra byte is 0 both in the mask and in the value.
2518          */
2519         vxp = (const struct rte_flow_item_vxlan *)spec;
2520         memcpy(vnet_id_v + 1, &vxp->vni, sizeof(vxp->vni));
2521
2522         vxp = (const struct rte_flow_item_vxlan *)mask;
2523         memcpy(vnet_id_m + 1, &vxp->vni, sizeof(vxp->vni));
2524
2525         rc = efx_mae_match_spec_field_set(ctx_mae->match_spec,
2526                                           EFX_MAE_FIELD_ENC_VNET_ID_BE,
2527                                           sizeof(vnet_id_v), vnet_id_v,
2528                                           sizeof(vnet_id_m), vnet_id_m);
2529         if (rc != 0) {
2530                 rc = rte_flow_error_set(error, rc, RTE_FLOW_ERROR_TYPE_ITEM,
2531                                         item, "Failed to set VXLAN VNI");
2532         }
2533
2534         return rc;
2535 }
2536
2537 static const struct sfc_flow_item sfc_flow_items[] = {
2538         {
2539                 .type = RTE_FLOW_ITEM_TYPE_MARK,
2540                 .name = "MARK",
2541                 .prev_layer = SFC_FLOW_ITEM_ANY_LAYER,
2542                 .layer = SFC_FLOW_ITEM_ANY_LAYER,
2543                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
2544                 .parse = sfc_mae_rule_parse_item_mark,
2545         },
2546         {
2547                 .type = RTE_FLOW_ITEM_TYPE_PORT_ID,
2548                 .name = "PORT_ID",
2549                 /*
2550                  * In terms of RTE flow, this item is a META one,
2551                  * and its position in the pattern is don't care.
2552                  */
2553                 .prev_layer = SFC_FLOW_ITEM_ANY_LAYER,
2554                 .layer = SFC_FLOW_ITEM_ANY_LAYER,
2555                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
2556                 .parse = sfc_mae_rule_parse_item_port_id,
2557         },
2558         {
2559                 .type = RTE_FLOW_ITEM_TYPE_PORT_REPRESENTOR,
2560                 .name = "PORT_REPRESENTOR",
2561                 /*
2562                  * In terms of RTE flow, this item is a META one,
2563                  * and its position in the pattern is don't care.
2564                  */
2565                 .prev_layer = SFC_FLOW_ITEM_ANY_LAYER,
2566                 .layer = SFC_FLOW_ITEM_ANY_LAYER,
2567                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
2568                 .parse = sfc_mae_rule_parse_item_ethdev_based,
2569         },
2570         {
2571                 .type = RTE_FLOW_ITEM_TYPE_REPRESENTED_PORT,
2572                 .name = "REPRESENTED_PORT",
2573                 /*
2574                  * In terms of RTE flow, this item is a META one,
2575                  * and its position in the pattern is don't care.
2576                  */
2577                 .prev_layer = SFC_FLOW_ITEM_ANY_LAYER,
2578                 .layer = SFC_FLOW_ITEM_ANY_LAYER,
2579                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
2580                 .parse = sfc_mae_rule_parse_item_ethdev_based,
2581         },
2582         {
2583                 .type = RTE_FLOW_ITEM_TYPE_PHY_PORT,
2584                 .name = "PHY_PORT",
2585                 /*
2586                  * In terms of RTE flow, this item is a META one,
2587                  * and its position in the pattern is don't care.
2588                  */
2589                 .prev_layer = SFC_FLOW_ITEM_ANY_LAYER,
2590                 .layer = SFC_FLOW_ITEM_ANY_LAYER,
2591                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
2592                 .parse = sfc_mae_rule_parse_item_phy_port,
2593         },
2594         {
2595                 .type = RTE_FLOW_ITEM_TYPE_PF,
2596                 .name = "PF",
2597                 /*
2598                  * In terms of RTE flow, this item is a META one,
2599                  * and its position in the pattern is don't care.
2600                  */
2601                 .prev_layer = SFC_FLOW_ITEM_ANY_LAYER,
2602                 .layer = SFC_FLOW_ITEM_ANY_LAYER,
2603                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
2604                 .parse = sfc_mae_rule_parse_item_pf,
2605         },
2606         {
2607                 .type = RTE_FLOW_ITEM_TYPE_VF,
2608                 .name = "VF",
2609                 /*
2610                  * In terms of RTE flow, this item is a META one,
2611                  * and its position in the pattern is don't care.
2612                  */
2613                 .prev_layer = SFC_FLOW_ITEM_ANY_LAYER,
2614                 .layer = SFC_FLOW_ITEM_ANY_LAYER,
2615                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
2616                 .parse = sfc_mae_rule_parse_item_vf,
2617         },
2618         {
2619                 .type = RTE_FLOW_ITEM_TYPE_ETH,
2620                 .name = "ETH",
2621                 .prev_layer = SFC_FLOW_ITEM_START_LAYER,
2622                 .layer = SFC_FLOW_ITEM_L2,
2623                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
2624                 .parse = sfc_mae_rule_parse_item_eth,
2625         },
2626         {
2627                 .type = RTE_FLOW_ITEM_TYPE_VLAN,
2628                 .name = "VLAN",
2629                 .prev_layer = SFC_FLOW_ITEM_L2,
2630                 .layer = SFC_FLOW_ITEM_L2,
2631                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
2632                 .parse = sfc_mae_rule_parse_item_vlan,
2633         },
2634         {
2635                 .type = RTE_FLOW_ITEM_TYPE_IPV4,
2636                 .name = "IPV4",
2637                 .prev_layer = SFC_FLOW_ITEM_L2,
2638                 .layer = SFC_FLOW_ITEM_L3,
2639                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
2640                 .parse = sfc_mae_rule_parse_item_ipv4,
2641         },
2642         {
2643                 .type = RTE_FLOW_ITEM_TYPE_IPV6,
2644                 .name = "IPV6",
2645                 .prev_layer = SFC_FLOW_ITEM_L2,
2646                 .layer = SFC_FLOW_ITEM_L3,
2647                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
2648                 .parse = sfc_mae_rule_parse_item_ipv6,
2649         },
2650         {
2651                 .type = RTE_FLOW_ITEM_TYPE_TCP,
2652                 .name = "TCP",
2653                 .prev_layer = SFC_FLOW_ITEM_L3,
2654                 .layer = SFC_FLOW_ITEM_L4,
2655                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
2656                 .parse = sfc_mae_rule_parse_item_tcp,
2657         },
2658         {
2659                 .type = RTE_FLOW_ITEM_TYPE_UDP,
2660                 .name = "UDP",
2661                 .prev_layer = SFC_FLOW_ITEM_L3,
2662                 .layer = SFC_FLOW_ITEM_L4,
2663                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
2664                 .parse = sfc_mae_rule_parse_item_udp,
2665         },
2666         {
2667                 .type = RTE_FLOW_ITEM_TYPE_VXLAN,
2668                 .name = "VXLAN",
2669                 .prev_layer = SFC_FLOW_ITEM_L4,
2670                 .layer = SFC_FLOW_ITEM_START_LAYER,
2671                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
2672                 .parse = sfc_mae_rule_parse_item_tunnel,
2673         },
2674         {
2675                 .type = RTE_FLOW_ITEM_TYPE_GENEVE,
2676                 .name = "GENEVE",
2677                 .prev_layer = SFC_FLOW_ITEM_L4,
2678                 .layer = SFC_FLOW_ITEM_START_LAYER,
2679                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
2680                 .parse = sfc_mae_rule_parse_item_tunnel,
2681         },
2682         {
2683                 .type = RTE_FLOW_ITEM_TYPE_NVGRE,
2684                 .name = "NVGRE",
2685                 .prev_layer = SFC_FLOW_ITEM_L3,
2686                 .layer = SFC_FLOW_ITEM_START_LAYER,
2687                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
2688                 .parse = sfc_mae_rule_parse_item_tunnel,
2689         },
2690 };
2691
2692 static int
2693 sfc_mae_rule_process_outer(struct sfc_adapter *sa,
2694                            struct sfc_mae_parse_ctx *ctx,
2695                            struct sfc_mae_outer_rule **rulep,
2696                            struct rte_flow_error *error)
2697 {
2698         efx_mae_rule_id_t invalid_rule_id = { .id = EFX_MAE_RSRC_ID_INVALID };
2699         int rc;
2700
2701         if (ctx->encap_type == EFX_TUNNEL_PROTOCOL_NONE) {
2702                 *rulep = NULL;
2703                 goto no_or_id;
2704         }
2705
2706         SFC_ASSERT(ctx->match_spec_outer != NULL);
2707
2708         if (!efx_mae_match_spec_is_valid(sa->nic, ctx->match_spec_outer)) {
2709                 return rte_flow_error_set(error, ENOTSUP,
2710                                           RTE_FLOW_ERROR_TYPE_ITEM, NULL,
2711                                           "Inconsistent pattern (outer)");
2712         }
2713
2714         *rulep = sfc_mae_outer_rule_attach(sa, ctx->match_spec_outer,
2715                                            ctx->encap_type);
2716         if (*rulep != NULL) {
2717                 efx_mae_match_spec_fini(sa->nic, ctx->match_spec_outer);
2718         } else {
2719                 rc = sfc_mae_outer_rule_add(sa, ctx->match_spec_outer,
2720                                             ctx->encap_type, rulep);
2721                 if (rc != 0) {
2722                         return rte_flow_error_set(error, rc,
2723                                         RTE_FLOW_ERROR_TYPE_ITEM, NULL,
2724                                         "Failed to process the pattern");
2725                 }
2726         }
2727
2728         /* The spec has now been tracked by the outer rule entry. */
2729         ctx->match_spec_outer = NULL;
2730
2731 no_or_id:
2732         switch (ctx->ft_rule_type) {
2733         case SFC_FT_RULE_NONE:
2734                 break;
2735         case SFC_FT_RULE_TUNNEL:
2736                 /* No action rule */
2737                 return 0;
2738         case SFC_FT_RULE_SWITCH:
2739                 /*
2740                  * Match on recirculation ID rather than
2741                  * on the outer rule allocation handle.
2742                  */
2743                 rc = efx_mae_match_spec_recirc_id_set(ctx->match_spec_action,
2744                                 SFC_FT_CTX_ID_TO_CTX_MARK(ctx->ft_ctx->id));
2745                 if (rc != 0) {
2746                         return rte_flow_error_set(error, rc,
2747                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2748                                         "FT: SWITCH: AR: failed to request match on RECIRC_ID");
2749                 }
2750                 return 0;
2751         default:
2752                 SFC_ASSERT(B_FALSE);
2753         }
2754
2755         /*
2756          * In MAE, lookup sequence comprises outer parse, outer rule lookup,
2757          * inner parse (when some outer rule is hit) and action rule lookup.
2758          * If the currently processed flow does not come with an outer rule,
2759          * its action rule must be available only for packets which miss in
2760          * outer rule table. Set OR_ID match field to 0xffffffff/0xffffffff
2761          * in the action rule specification; this ensures correct behaviour.
2762          *
2763          * If, on the other hand, this flow does have an outer rule, its ID
2764          * may be unknown at the moment (not yet allocated), but OR_ID mask
2765          * has to be set to 0xffffffff anyway for correct class comparisons.
2766          * When the outer rule has been allocated, this match field will be
2767          * overridden by sfc_mae_outer_rule_enable() to use the right value.
2768          */
2769         rc = efx_mae_match_spec_outer_rule_id_set(ctx->match_spec_action,
2770                                                   &invalid_rule_id);
2771         if (rc != 0) {
2772                 if (*rulep != NULL)
2773                         sfc_mae_outer_rule_del(sa, *rulep);
2774
2775                 *rulep = NULL;
2776
2777                 return rte_flow_error_set(error, rc,
2778                                           RTE_FLOW_ERROR_TYPE_ITEM, NULL,
2779                                           "Failed to process the pattern");
2780         }
2781
2782         return 0;
2783 }
2784
2785 static int
2786 sfc_mae_rule_preparse_item_mark(const struct rte_flow_item_mark *spec,
2787                                 struct sfc_mae_parse_ctx *ctx)
2788 {
2789         struct sfc_ft_ctx *ft_ctx;
2790         uint32_t user_mark;
2791
2792         if (spec == NULL) {
2793                 sfc_err(ctx->sa, "FT: SWITCH: NULL spec in item MARK");
2794                 return EINVAL;
2795         }
2796
2797         ft_ctx = sfc_ft_ctx_pick(ctx->sa, spec->id);
2798         if (ft_ctx == NULL) {
2799                 sfc_err(ctx->sa, "FT: SWITCH: invalid context");
2800                 return EINVAL;
2801         }
2802
2803         if (ft_ctx->refcnt == 0) {
2804                 sfc_err(ctx->sa, "FT: SWITCH: inactive context (ID=%u)",
2805                         ft_ctx->id);
2806                 return ENOENT;
2807         }
2808
2809         user_mark = SFC_FT_FLOW_MARK_TO_USER_MARK(spec->id);
2810         if (user_mark != 0) {
2811                 sfc_err(ctx->sa, "FT: SWITCH: invalid item MARK");
2812                 return EINVAL;
2813         }
2814
2815         sfc_dbg(ctx->sa, "FT: SWITCH: detected");
2816
2817         ctx->ft_rule_type = SFC_FT_RULE_SWITCH;
2818         ctx->ft_ctx = ft_ctx;
2819
2820         return 0;
2821 }
2822
2823 static int
2824 sfc_mae_rule_encap_parse_init(struct sfc_adapter *sa,
2825                               struct sfc_mae_parse_ctx *ctx,
2826                               struct rte_flow_error *error)
2827 {
2828         const struct rte_flow_item *pattern = ctx->pattern;
2829         struct sfc_mae *mae = &sa->mae;
2830         uint8_t recirc_id = 0;
2831         int rc;
2832
2833         if (pattern == NULL) {
2834                 rte_flow_error_set(error, EINVAL,
2835                                    RTE_FLOW_ERROR_TYPE_ITEM_NUM, NULL,
2836                                    "NULL pattern");
2837                 return -rte_errno;
2838         }
2839
2840         for (;;) {
2841                 switch (pattern->type) {
2842                 case RTE_FLOW_ITEM_TYPE_MARK:
2843                         rc = sfc_mae_rule_preparse_item_mark(pattern->spec,
2844                                                              ctx);
2845                         if (rc != 0) {
2846                                 return rte_flow_error_set(error, rc,
2847                                                   RTE_FLOW_ERROR_TYPE_ITEM,
2848                                                   pattern, "FT: SWITCH: invalid item MARK");
2849                         }
2850                         ++pattern;
2851                         continue;
2852                 case RTE_FLOW_ITEM_TYPE_VXLAN:
2853                         ctx->encap_type = EFX_TUNNEL_PROTOCOL_VXLAN;
2854                         ctx->tunnel_def_mask = &rte_flow_item_vxlan_mask;
2855                         ctx->tunnel_def_mask_size =
2856                                 sizeof(rte_flow_item_vxlan_mask);
2857                         break;
2858                 case RTE_FLOW_ITEM_TYPE_GENEVE:
2859                         ctx->encap_type = EFX_TUNNEL_PROTOCOL_GENEVE;
2860                         ctx->tunnel_def_mask = &rte_flow_item_geneve_mask;
2861                         ctx->tunnel_def_mask_size =
2862                                 sizeof(rte_flow_item_geneve_mask);
2863                         break;
2864                 case RTE_FLOW_ITEM_TYPE_NVGRE:
2865                         ctx->encap_type = EFX_TUNNEL_PROTOCOL_NVGRE;
2866                         ctx->tunnel_def_mask = &rte_flow_item_nvgre_mask;
2867                         ctx->tunnel_def_mask_size =
2868                                 sizeof(rte_flow_item_nvgre_mask);
2869                         break;
2870                 case RTE_FLOW_ITEM_TYPE_END:
2871                         break;
2872                 default:
2873                         ++pattern;
2874                         continue;
2875                 };
2876
2877                 break;
2878         }
2879
2880         switch (ctx->ft_rule_type) {
2881         case SFC_FT_RULE_NONE:
2882                 if (pattern->type == RTE_FLOW_ITEM_TYPE_END)
2883                         return 0;
2884                 break;
2885         case SFC_FT_RULE_TUNNEL:
2886                 if (pattern->type != RTE_FLOW_ITEM_TYPE_END) {
2887                         return rte_flow_error_set(error, ENOTSUP,
2888                                                   RTE_FLOW_ERROR_TYPE_ITEM,
2889                                                   pattern, "FT: TUNNEL: invalid item");
2890                 }
2891                 ctx->encap_type = ctx->ft_ctx->encap_type;
2892                 break;
2893         case SFC_FT_RULE_SWITCH:
2894                 if (pattern->type == RTE_FLOW_ITEM_TYPE_END) {
2895                         return rte_flow_error_set(error, EINVAL,
2896                                                   RTE_FLOW_ERROR_TYPE_ITEM,
2897                                                   NULL, "FT: SWITCH: missing tunnel item");
2898                 } else if (ctx->encap_type != ctx->ft_ctx->encap_type) {
2899                         return rte_flow_error_set(error, EINVAL,
2900                                                   RTE_FLOW_ERROR_TYPE_ITEM,
2901                                                   pattern, "FT: SWITCH: tunnel type mismatch");
2902                 }
2903
2904                 /*
2905                  * The HW/FW hasn't got support for the use of "ENC" fields in
2906                  * action rules (except the VNET_ID one) yet. As a workaround,
2907                  * start parsing the pattern from the tunnel item.
2908                  */
2909                 ctx->pattern = pattern;
2910                 break;
2911         default:
2912                 SFC_ASSERT(B_FALSE);
2913                 break;
2914         }
2915
2916         if ((mae->encap_types_supported & (1U << ctx->encap_type)) == 0) {
2917                 return rte_flow_error_set(error, ENOTSUP,
2918                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2919                                           "OR: unsupported tunnel type");
2920         }
2921
2922         switch (ctx->ft_rule_type) {
2923         case SFC_FT_RULE_TUNNEL:
2924                 recirc_id = SFC_FT_CTX_ID_TO_CTX_MARK(ctx->ft_ctx->id);
2925                 /* FALLTHROUGH */
2926         case SFC_FT_RULE_NONE:
2927                 if (ctx->priority >= mae->nb_outer_rule_prios_max) {
2928                         return rte_flow_error_set(error, ENOTSUP,
2929                                         RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
2930                                         NULL, "OR: unsupported priority level");
2931                 }
2932
2933                 rc = efx_mae_match_spec_init(sa->nic,
2934                                              EFX_MAE_RULE_OUTER, ctx->priority,
2935                                              &ctx->match_spec_outer);
2936                 if (rc != 0) {
2937                         return rte_flow_error_set(error, rc,
2938                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2939                                 "OR: failed to initialise the match specification");
2940                 }
2941
2942                 /*
2943                  * Outermost items comprise a match
2944                  * specification of type OUTER.
2945                  */
2946                 ctx->match_spec = ctx->match_spec_outer;
2947
2948                 /* Outermost items use "ENC" EFX MAE field IDs. */
2949                 ctx->field_ids_remap = field_ids_remap_to_encap;
2950
2951                 rc = efx_mae_outer_rule_recirc_id_set(ctx->match_spec,
2952                                                       recirc_id);
2953                 if (rc != 0) {
2954                         return rte_flow_error_set(error, rc,
2955                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2956                                         "OR: failed to initialise RECIRC_ID");
2957                 }
2958                 break;
2959         case SFC_FT_RULE_SWITCH:
2960                 /* Outermost items -> "ENC" match fields in the action rule. */
2961                 ctx->field_ids_remap = field_ids_remap_to_encap;
2962                 ctx->match_spec = ctx->match_spec_action;
2963
2964                 /* No own outer rule; match on TUNNEL OR's RECIRC_ID is used. */
2965                 ctx->encap_type = EFX_TUNNEL_PROTOCOL_NONE;
2966                 break;
2967         default:
2968                 SFC_ASSERT(B_FALSE);
2969                 break;
2970         }
2971
2972         return 0;
2973 }
2974
2975 static void
2976 sfc_mae_rule_encap_parse_fini(struct sfc_adapter *sa,
2977                               struct sfc_mae_parse_ctx *ctx)
2978 {
2979         if (ctx->encap_type == EFX_TUNNEL_PROTOCOL_NONE)
2980                 return;
2981
2982         if (ctx->match_spec_outer != NULL)
2983                 efx_mae_match_spec_fini(sa->nic, ctx->match_spec_outer);
2984 }
2985
2986 int
2987 sfc_mae_rule_parse_pattern(struct sfc_adapter *sa,
2988                            const struct rte_flow_item pattern[],
2989                            struct sfc_flow_spec_mae *spec,
2990                            struct rte_flow_error *error)
2991 {
2992         struct sfc_mae_parse_ctx ctx_mae;
2993         unsigned int priority_shift = 0;
2994         struct sfc_flow_parse_ctx ctx;
2995         int rc;
2996
2997         memset(&ctx_mae, 0, sizeof(ctx_mae));
2998         ctx_mae.ft_rule_type = spec->ft_rule_type;
2999         ctx_mae.priority = spec->priority;
3000         ctx_mae.ft_ctx = spec->ft_ctx;
3001         ctx_mae.sa = sa;
3002
3003         switch (ctx_mae.ft_rule_type) {
3004         case SFC_FT_RULE_TUNNEL:
3005                 /*
3006                  * By design, this flow should be represented solely by the
3007                  * outer rule. But the HW/FW hasn't got support for setting
3008                  * Rx mark from RECIRC_ID on outer rule lookup yet. Neither
3009                  * does it support outer rule counters. As a workaround, an
3010                  * action rule of lower priority is used to do the job.
3011                  */
3012                 priority_shift = 1;
3013
3014                 /* FALLTHROUGH */
3015         case SFC_FT_RULE_SWITCH:
3016                 if (ctx_mae.priority != 0) {
3017                         /*
3018                          * Because of the above workaround, deny the use
3019                          * of priorities to TUNNEL and SWITCH rules.
3020                          */
3021                         rc = rte_flow_error_set(error, ENOTSUP,
3022                                 RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY, NULL,
3023                                 "FT: priorities are not supported");
3024                         goto fail_priority_check;
3025                 }
3026
3027                 /* FALLTHROUGH */
3028         case SFC_FT_RULE_NONE:
3029                 rc = efx_mae_match_spec_init(sa->nic, EFX_MAE_RULE_ACTION,
3030                                              spec->priority + priority_shift,
3031                                              &ctx_mae.match_spec_action);
3032                 if (rc != 0) {
3033                         rc = rte_flow_error_set(error, rc,
3034                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
3035                                 "AR: failed to initialise the match specification");
3036                         goto fail_init_match_spec_action;
3037                 }
3038                 break;
3039         default:
3040                 SFC_ASSERT(B_FALSE);
3041                 break;
3042         }
3043
3044         /*
3045          * As a preliminary setting, assume that there is no encapsulation
3046          * in the pattern. That is, pattern items are about to comprise a
3047          * match specification of type ACTION and use non-encap. field IDs.
3048          *
3049          * sfc_mae_rule_encap_parse_init() below may override this.
3050          */
3051         ctx_mae.encap_type = EFX_TUNNEL_PROTOCOL_NONE;
3052         ctx_mae.match_spec = ctx_mae.match_spec_action;
3053         ctx_mae.field_ids_remap = field_ids_no_remap;
3054         ctx_mae.pattern = pattern;
3055
3056         ctx.type = SFC_FLOW_PARSE_CTX_MAE;
3057         ctx.mae = &ctx_mae;
3058
3059         rc = sfc_mae_rule_encap_parse_init(sa, &ctx_mae, error);
3060         if (rc != 0)
3061                 goto fail_encap_parse_init;
3062
3063         /*
3064          * sfc_mae_rule_encap_parse_init() may have detected tunnel offload
3065          * SWITCH rule. Remember its properties for later use.
3066          */
3067         spec->ft_rule_type = ctx_mae.ft_rule_type;
3068         spec->ft_ctx = ctx_mae.ft_ctx;
3069
3070         rc = sfc_flow_parse_pattern(sa, sfc_flow_items, RTE_DIM(sfc_flow_items),
3071                                     ctx_mae.pattern, &ctx, error);
3072         if (rc != 0)
3073                 goto fail_parse_pattern;
3074
3075         rc = sfc_mae_rule_process_pattern_data(&ctx_mae, error);
3076         if (rc != 0)
3077                 goto fail_process_pattern_data;
3078
3079         rc = sfc_mae_rule_process_outer(sa, &ctx_mae, &spec->outer_rule, error);
3080         if (rc != 0)
3081                 goto fail_process_outer;
3082
3083         if (ctx_mae.match_spec_action != NULL &&
3084             !efx_mae_match_spec_is_valid(sa->nic, ctx_mae.match_spec_action)) {
3085                 rc = rte_flow_error_set(error, ENOTSUP,
3086                                         RTE_FLOW_ERROR_TYPE_ITEM, NULL,
3087                                         "Inconsistent pattern");
3088                 goto fail_validate_match_spec_action;
3089         }
3090
3091         spec->match_spec = ctx_mae.match_spec_action;
3092
3093         return 0;
3094
3095 fail_validate_match_spec_action:
3096 fail_process_outer:
3097 fail_process_pattern_data:
3098 fail_parse_pattern:
3099         sfc_mae_rule_encap_parse_fini(sa, &ctx_mae);
3100
3101 fail_encap_parse_init:
3102         if (ctx_mae.match_spec_action != NULL)
3103                 efx_mae_match_spec_fini(sa->nic, ctx_mae.match_spec_action);
3104
3105 fail_init_match_spec_action:
3106 fail_priority_check:
3107         return rc;
3108 }
3109
3110 static int
3111 sfc_mae_rule_parse_action_set_mac(struct sfc_adapter *sa,
3112                                   enum sfc_mae_mac_addr_type type,
3113                                   const struct rte_flow_action_set_mac *conf,
3114                                   struct sfc_mae_aset_ctx *ctx,
3115                                   struct rte_flow_error *error)
3116 {
3117         struct sfc_mae_mac_addr **mac_addrp;
3118         int rc;
3119
3120         if (conf == NULL) {
3121                 return rte_flow_error_set(error, EINVAL,
3122                                 RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
3123                                 "the MAC address entry definition is NULL");
3124         }
3125
3126         switch (type) {
3127         case SFC_MAE_MAC_ADDR_DST:
3128                 rc = efx_mae_action_set_populate_set_dst_mac(ctx->spec);
3129                 mac_addrp = &ctx->dst_mac;
3130                 break;
3131         case SFC_MAE_MAC_ADDR_SRC:
3132                 rc = efx_mae_action_set_populate_set_src_mac(ctx->spec);
3133                 mac_addrp = &ctx->src_mac;
3134                 break;
3135         default:
3136                 rc = EINVAL;
3137                 break;
3138         }
3139
3140         if (rc != 0)
3141                 goto error;
3142
3143         *mac_addrp = sfc_mae_mac_addr_attach(sa, conf->mac_addr);
3144         if (*mac_addrp != NULL)
3145                 return 0;
3146
3147         rc = sfc_mae_mac_addr_add(sa, conf->mac_addr, mac_addrp);
3148         if (rc != 0)
3149                 goto error;
3150
3151         return 0;
3152
3153 error:
3154         return rte_flow_error_set(error, rc, RTE_FLOW_ERROR_TYPE_ACTION,
3155                                   NULL, "failed to request set MAC action");
3156 }
3157
3158 /*
3159  * An action supported by MAE may correspond to a bundle of RTE flow actions,
3160  * in example, VLAN_PUSH = OF_PUSH_VLAN + OF_VLAN_SET_VID + OF_VLAN_SET_PCP.
3161  * That is, related RTE flow actions need to be tracked as parts of a whole
3162  * so that they can be combined into a single action and submitted to MAE
3163  * representation of a given rule's action set.
3164  *
3165  * Each RTE flow action provided by an application gets classified as
3166  * one belonging to some bundle type. If an action is not supposed to
3167  * belong to any bundle, or if this action is END, it is described as
3168  * one belonging to a dummy bundle of type EMPTY.
3169  *
3170  * A currently tracked bundle will be submitted if a repeating
3171  * action or an action of different bundle type follows.
3172  */
3173
3174 enum sfc_mae_actions_bundle_type {
3175         SFC_MAE_ACTIONS_BUNDLE_EMPTY = 0,
3176         SFC_MAE_ACTIONS_BUNDLE_VLAN_PUSH,
3177 };
3178
3179 struct sfc_mae_actions_bundle {
3180         enum sfc_mae_actions_bundle_type        type;
3181
3182         /* Indicates actions already tracked by the current bundle */
3183         uint64_t                                actions_mask;
3184
3185         /* Parameters used by SFC_MAE_ACTIONS_BUNDLE_VLAN_PUSH */
3186         rte_be16_t                              vlan_push_tpid;
3187         rte_be16_t                              vlan_push_tci;
3188 };
3189
3190 /*
3191  * Combine configuration of RTE flow actions tracked by the bundle into a
3192  * single action and submit the result to MAE action set specification.
3193  * Do nothing in the case of dummy action bundle.
3194  */
3195 static int
3196 sfc_mae_actions_bundle_submit(const struct sfc_mae_actions_bundle *bundle,
3197                               efx_mae_actions_t *spec)
3198 {
3199         int rc = 0;
3200
3201         switch (bundle->type) {
3202         case SFC_MAE_ACTIONS_BUNDLE_EMPTY:
3203                 break;
3204         case SFC_MAE_ACTIONS_BUNDLE_VLAN_PUSH:
3205                 rc = efx_mae_action_set_populate_vlan_push(
3206                         spec, bundle->vlan_push_tpid, bundle->vlan_push_tci);
3207                 break;
3208         default:
3209                 SFC_ASSERT(B_FALSE);
3210                 break;
3211         }
3212
3213         return rc;
3214 }
3215
3216 /*
3217  * Given the type of the next RTE flow action in the line, decide
3218  * whether a new bundle is about to start, and, if this is the case,
3219  * submit and reset the current bundle.
3220  */
3221 static int
3222 sfc_mae_actions_bundle_sync(const struct rte_flow_action *action,
3223                             struct sfc_mae_actions_bundle *bundle,
3224                             efx_mae_actions_t *spec,
3225                             struct rte_flow_error *error)
3226 {
3227         enum sfc_mae_actions_bundle_type bundle_type_new;
3228         int rc;
3229
3230         switch (action->type) {
3231         case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
3232         case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
3233         case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
3234                 bundle_type_new = SFC_MAE_ACTIONS_BUNDLE_VLAN_PUSH;
3235                 break;
3236         default:
3237                 /*
3238                  * Self-sufficient actions, including END, are handled in this
3239                  * case. No checks for unsupported actions are needed here
3240                  * because parsing doesn't occur at this point.
3241                  */
3242                 bundle_type_new = SFC_MAE_ACTIONS_BUNDLE_EMPTY;
3243                 break;
3244         }
3245
3246         if (bundle_type_new != bundle->type ||
3247             (bundle->actions_mask & (1ULL << action->type)) != 0) {
3248                 rc = sfc_mae_actions_bundle_submit(bundle, spec);
3249                 if (rc != 0)
3250                         goto fail_submit;
3251
3252                 memset(bundle, 0, sizeof(*bundle));
3253         }
3254
3255         bundle->type = bundle_type_new;
3256
3257         return 0;
3258
3259 fail_submit:
3260         return rte_flow_error_set(error, rc,
3261                         RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3262                         "Failed to request the (group of) action(s)");
3263 }
3264
3265 static void
3266 sfc_mae_rule_parse_action_of_push_vlan(
3267                             const struct rte_flow_action_of_push_vlan *conf,
3268                             struct sfc_mae_actions_bundle *bundle)
3269 {
3270         bundle->vlan_push_tpid = conf->ethertype;
3271 }
3272
3273 static void
3274 sfc_mae_rule_parse_action_of_set_vlan_vid(
3275                             const struct rte_flow_action_of_set_vlan_vid *conf,
3276                             struct sfc_mae_actions_bundle *bundle)
3277 {
3278         bundle->vlan_push_tci |= (conf->vlan_vid &
3279                                   rte_cpu_to_be_16(RTE_LEN2MASK(12, uint16_t)));
3280 }
3281
3282 static void
3283 sfc_mae_rule_parse_action_of_set_vlan_pcp(
3284                             const struct rte_flow_action_of_set_vlan_pcp *conf,
3285                             struct sfc_mae_actions_bundle *bundle)
3286 {
3287         uint16_t vlan_tci_pcp = (uint16_t)(conf->vlan_pcp &
3288                                            RTE_LEN2MASK(3, uint8_t)) << 13;
3289
3290         bundle->vlan_push_tci |= rte_cpu_to_be_16(vlan_tci_pcp);
3291 }
3292
3293 struct sfc_mae_parsed_item {
3294         const struct rte_flow_item      *item;
3295         size_t                          proto_header_ofst;
3296         size_t                          proto_header_size;
3297 };
3298
3299 /*
3300  * For each 16-bit word of the given header, override
3301  * bits enforced by the corresponding 16-bit mask.
3302  */
3303 static void
3304 sfc_mae_header_force_item_masks(uint8_t *header_buf,
3305                                 const struct sfc_mae_parsed_item *parsed_items,
3306                                 unsigned int nb_parsed_items)
3307 {
3308         unsigned int item_idx;
3309
3310         for (item_idx = 0; item_idx < nb_parsed_items; ++item_idx) {
3311                 const struct sfc_mae_parsed_item *parsed_item;
3312                 const struct rte_flow_item *item;
3313                 size_t proto_header_size;
3314                 size_t ofst;
3315
3316                 parsed_item = &parsed_items[item_idx];
3317                 proto_header_size = parsed_item->proto_header_size;
3318                 item = parsed_item->item;
3319
3320                 for (ofst = 0; ofst < proto_header_size;
3321                      ofst += sizeof(rte_be16_t)) {
3322                         rte_be16_t *wp = RTE_PTR_ADD(header_buf, ofst);
3323                         const rte_be16_t *w_maskp;
3324                         const rte_be16_t *w_specp;
3325
3326                         w_maskp = RTE_PTR_ADD(item->mask, ofst);
3327                         w_specp = RTE_PTR_ADD(item->spec, ofst);
3328
3329                         *wp &= ~(*w_maskp);
3330                         *wp |= (*w_specp & *w_maskp);
3331                 }
3332
3333                 header_buf += proto_header_size;
3334         }
3335 }
3336
3337 #define SFC_IPV4_TTL_DEF        0x40
3338 #define SFC_IPV6_VTC_FLOW_DEF   0x60000000
3339 #define SFC_IPV6_HOP_LIMITS_DEF 0xff
3340 #define SFC_VXLAN_FLAGS_DEF     0x08000000
3341
3342 static int
3343 sfc_mae_rule_parse_action_vxlan_encap(
3344                             struct sfc_mae *mae,
3345                             const struct rte_flow_action_vxlan_encap *conf,
3346                             efx_mae_actions_t *spec,
3347                             struct rte_flow_error *error)
3348 {
3349         struct sfc_mae_bounce_eh *bounce_eh = &mae->bounce_eh;
3350         struct rte_flow_item *pattern = conf->definition;
3351         uint8_t *buf = bounce_eh->buf;
3352
3353         /* This array will keep track of non-VOID pattern items. */
3354         struct sfc_mae_parsed_item parsed_items[1 /* Ethernet */ +
3355                                                 2 /* VLAN tags */ +
3356                                                 1 /* IPv4 or IPv6 */ +
3357                                                 1 /* UDP */ +
3358                                                 1 /* VXLAN */];
3359         unsigned int nb_parsed_items = 0;
3360
3361         size_t eth_ethertype_ofst = offsetof(struct rte_ether_hdr, ether_type);
3362         uint8_t dummy_buf[RTE_MAX(sizeof(struct rte_ipv4_hdr),
3363                                   sizeof(struct rte_ipv6_hdr))];
3364         struct rte_ipv4_hdr *ipv4 = (void *)dummy_buf;
3365         struct rte_ipv6_hdr *ipv6 = (void *)dummy_buf;
3366         struct rte_vxlan_hdr *vxlan = NULL;
3367         struct rte_udp_hdr *udp = NULL;
3368         unsigned int nb_vlan_tags = 0;
3369         size_t next_proto_ofst = 0;
3370         size_t ethertype_ofst = 0;
3371         uint64_t exp_items;
3372         int rc;
3373
3374         if (pattern == NULL) {
3375                 return rte_flow_error_set(error, EINVAL,
3376                                 RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
3377                                 "The encap. header definition is NULL");
3378         }
3379
3380         bounce_eh->type = EFX_TUNNEL_PROTOCOL_VXLAN;
3381         bounce_eh->size = 0;
3382
3383         /*
3384          * Process pattern items and remember non-VOID ones.
3385          * Defer applying masks until after the complete header
3386          * has been built from the pattern items.
3387          */
3388         exp_items = RTE_BIT64(RTE_FLOW_ITEM_TYPE_ETH);
3389
3390         for (; pattern->type != RTE_FLOW_ITEM_TYPE_END; ++pattern) {
3391                 struct sfc_mae_parsed_item *parsed_item;
3392                 const uint64_t exp_items_extra_vlan[] = {
3393                         RTE_BIT64(RTE_FLOW_ITEM_TYPE_VLAN), 0
3394                 };
3395                 size_t proto_header_size;
3396                 rte_be16_t *ethertypep;
3397                 uint8_t *next_protop;
3398                 uint8_t *buf_cur;
3399
3400                 if (pattern->spec == NULL) {
3401                         return rte_flow_error_set(error, EINVAL,
3402                                         RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
3403                                         "NULL item spec in the encap. header");
3404                 }
3405
3406                 if (pattern->mask == NULL) {
3407                         return rte_flow_error_set(error, EINVAL,
3408                                         RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
3409                                         "NULL item mask in the encap. header");
3410                 }
3411
3412                 if (pattern->last != NULL) {
3413                         /* This is not a match pattern, so disallow range. */
3414                         return rte_flow_error_set(error, EINVAL,
3415                                         RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
3416                                         "Range item in the encap. header");
3417                 }
3418
3419                 if (pattern->type == RTE_FLOW_ITEM_TYPE_VOID) {
3420                         /* Handle VOID separately, for clarity. */
3421                         continue;
3422                 }
3423
3424                 if ((exp_items & RTE_BIT64(pattern->type)) == 0) {
3425                         return rte_flow_error_set(error, ENOTSUP,
3426                                         RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
3427                                         "Unexpected item in the encap. header");
3428                 }
3429
3430                 parsed_item = &parsed_items[nb_parsed_items];
3431                 buf_cur = buf + bounce_eh->size;
3432
3433                 switch (pattern->type) {
3434                 case RTE_FLOW_ITEM_TYPE_ETH:
3435                         SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ITEM_TYPE_ETH,
3436                                                exp_items);
3437                         RTE_BUILD_BUG_ON(offsetof(struct rte_flow_item_eth,
3438                                                   hdr) != 0);
3439
3440                         proto_header_size = sizeof(struct rte_ether_hdr);
3441
3442                         ethertype_ofst = eth_ethertype_ofst;
3443
3444                         exp_items = RTE_BIT64(RTE_FLOW_ITEM_TYPE_VLAN) |
3445                                     RTE_BIT64(RTE_FLOW_ITEM_TYPE_IPV4) |
3446                                     RTE_BIT64(RTE_FLOW_ITEM_TYPE_IPV6);
3447                         break;
3448                 case RTE_FLOW_ITEM_TYPE_VLAN:
3449                         SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ITEM_TYPE_VLAN,
3450                                                exp_items);
3451                         RTE_BUILD_BUG_ON(offsetof(struct rte_flow_item_vlan,
3452                                                   hdr) != 0);
3453
3454                         proto_header_size = sizeof(struct rte_vlan_hdr);
3455
3456                         ethertypep = RTE_PTR_ADD(buf, eth_ethertype_ofst);
3457                         *ethertypep = RTE_BE16(RTE_ETHER_TYPE_QINQ);
3458
3459                         ethertypep = RTE_PTR_ADD(buf, ethertype_ofst);
3460                         *ethertypep = RTE_BE16(RTE_ETHER_TYPE_VLAN);
3461
3462                         ethertype_ofst =
3463                             bounce_eh->size +
3464                             offsetof(struct rte_vlan_hdr, eth_proto);
3465
3466                         exp_items = RTE_BIT64(RTE_FLOW_ITEM_TYPE_IPV4) |
3467                                     RTE_BIT64(RTE_FLOW_ITEM_TYPE_IPV6);
3468                         exp_items |= exp_items_extra_vlan[nb_vlan_tags];
3469
3470                         ++nb_vlan_tags;
3471                         break;
3472                 case RTE_FLOW_ITEM_TYPE_IPV4:
3473                         SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ITEM_TYPE_IPV4,
3474                                                exp_items);
3475                         RTE_BUILD_BUG_ON(offsetof(struct rte_flow_item_ipv4,
3476                                                   hdr) != 0);
3477
3478                         proto_header_size = sizeof(struct rte_ipv4_hdr);
3479
3480                         ethertypep = RTE_PTR_ADD(buf, ethertype_ofst);
3481                         *ethertypep = RTE_BE16(RTE_ETHER_TYPE_IPV4);
3482
3483                         next_proto_ofst =
3484                             bounce_eh->size +
3485                             offsetof(struct rte_ipv4_hdr, next_proto_id);
3486
3487                         ipv4 = (struct rte_ipv4_hdr *)buf_cur;
3488
3489                         exp_items = RTE_BIT64(RTE_FLOW_ITEM_TYPE_UDP);
3490                         break;
3491                 case RTE_FLOW_ITEM_TYPE_IPV6:
3492                         SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ITEM_TYPE_IPV6,
3493                                                exp_items);
3494                         RTE_BUILD_BUG_ON(offsetof(struct rte_flow_item_ipv6,
3495                                                   hdr) != 0);
3496
3497                         proto_header_size = sizeof(struct rte_ipv6_hdr);
3498
3499                         ethertypep = RTE_PTR_ADD(buf, ethertype_ofst);
3500                         *ethertypep = RTE_BE16(RTE_ETHER_TYPE_IPV6);
3501
3502                         next_proto_ofst = bounce_eh->size +
3503                                           offsetof(struct rte_ipv6_hdr, proto);
3504
3505                         ipv6 = (struct rte_ipv6_hdr *)buf_cur;
3506
3507                         exp_items = RTE_BIT64(RTE_FLOW_ITEM_TYPE_UDP);
3508                         break;
3509                 case RTE_FLOW_ITEM_TYPE_UDP:
3510                         SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ITEM_TYPE_UDP,
3511                                                exp_items);
3512                         RTE_BUILD_BUG_ON(offsetof(struct rte_flow_item_udp,
3513                                                   hdr) != 0);
3514
3515                         proto_header_size = sizeof(struct rte_udp_hdr);
3516
3517                         next_protop = RTE_PTR_ADD(buf, next_proto_ofst);
3518                         *next_protop = IPPROTO_UDP;
3519
3520                         udp = (struct rte_udp_hdr *)buf_cur;
3521
3522                         exp_items = RTE_BIT64(RTE_FLOW_ITEM_TYPE_VXLAN);
3523                         break;
3524                 case RTE_FLOW_ITEM_TYPE_VXLAN:
3525                         SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ITEM_TYPE_VXLAN,
3526                                                exp_items);
3527                         RTE_BUILD_BUG_ON(offsetof(struct rte_flow_item_vxlan,
3528                                                   hdr) != 0);
3529
3530                         proto_header_size = sizeof(struct rte_vxlan_hdr);
3531
3532                         vxlan = (struct rte_vxlan_hdr *)buf_cur;
3533
3534                         udp->dst_port = RTE_BE16(RTE_VXLAN_DEFAULT_PORT);
3535                         udp->dgram_len = RTE_BE16(sizeof(*udp) +
3536                                                   sizeof(*vxlan));
3537                         udp->dgram_cksum = 0;
3538
3539                         exp_items = 0;
3540                         break;
3541                 default:
3542                         return rte_flow_error_set(error, ENOTSUP,
3543                                         RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
3544                                         "Unknown item in the encap. header");
3545                 }
3546
3547                 if (bounce_eh->size + proto_header_size > bounce_eh->buf_size) {
3548                         return rte_flow_error_set(error, E2BIG,
3549                                         RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
3550                                         "The encap. header is too big");
3551                 }
3552
3553                 if ((proto_header_size & 1) != 0) {
3554                         return rte_flow_error_set(error, EINVAL,
3555                                         RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
3556                                         "Odd layer size in the encap. header");
3557                 }
3558
3559                 rte_memcpy(buf_cur, pattern->spec, proto_header_size);
3560                 bounce_eh->size += proto_header_size;
3561
3562                 parsed_item->item = pattern;
3563                 parsed_item->proto_header_size = proto_header_size;
3564                 ++nb_parsed_items;
3565         }
3566
3567         if (exp_items != 0) {
3568                 /* Parsing item VXLAN would have reset exp_items to 0. */
3569                 return rte_flow_error_set(error, ENOTSUP,
3570                                         RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
3571                                         "No item VXLAN in the encap. header");
3572         }
3573
3574         /* One of the pointers (ipv4, ipv6) refers to a dummy area. */
3575         ipv4->version_ihl = RTE_IPV4_VHL_DEF;
3576         ipv4->time_to_live = SFC_IPV4_TTL_DEF;
3577         ipv4->total_length = RTE_BE16(sizeof(*ipv4) + sizeof(*udp) +
3578                                       sizeof(*vxlan));
3579         /* The HW cannot compute this checksum. */
3580         ipv4->hdr_checksum = 0;
3581         ipv4->hdr_checksum = rte_ipv4_cksum(ipv4);
3582
3583         ipv6->vtc_flow = RTE_BE32(SFC_IPV6_VTC_FLOW_DEF);
3584         ipv6->hop_limits = SFC_IPV6_HOP_LIMITS_DEF;
3585         ipv6->payload_len = udp->dgram_len;
3586
3587         vxlan->vx_flags = RTE_BE32(SFC_VXLAN_FLAGS_DEF);
3588
3589         /* Take care of the masks. */
3590         sfc_mae_header_force_item_masks(buf, parsed_items, nb_parsed_items);
3591
3592         rc = efx_mae_action_set_populate_encap(spec);
3593         if (rc != 0) {
3594                 rc = rte_flow_error_set(error, rc, RTE_FLOW_ERROR_TYPE_ACTION,
3595                                 NULL, "failed to request action ENCAP");
3596         }
3597
3598         return rc;
3599 }
3600
3601 static int
3602 sfc_mae_rule_parse_action_mark(struct sfc_adapter *sa,
3603                                const struct rte_flow_action_mark *conf,
3604                                const struct sfc_flow_spec_mae *spec_mae,
3605                                efx_mae_actions_t *spec)
3606 {
3607         int rc;
3608
3609         if (spec_mae->ft_rule_type == SFC_FT_RULE_TUNNEL) {
3610                 /* Workaround. See sfc_flow_parse_rte_to_mae() */
3611         } else if (conf->id > SFC_FT_USER_MARK_MASK) {
3612                 sfc_err(sa, "the mark value is too large");
3613                 return EINVAL;
3614         }
3615
3616         rc = efx_mae_action_set_populate_mark(spec, conf->id);
3617         if (rc != 0)
3618                 sfc_err(sa, "failed to request action MARK: %s", strerror(rc));
3619
3620         return rc;
3621 }
3622
3623 static int
3624 sfc_mae_rule_parse_action_count(struct sfc_adapter *sa,
3625                                 const struct rte_flow_action_count *conf
3626                                         __rte_unused,
3627                                 efx_mae_actions_t *spec)
3628 {
3629         int rc;
3630
3631         if ((sa->counter_rxq.state & SFC_COUNTER_RXQ_INITIALIZED) == 0) {
3632                 sfc_err(sa,
3633                         "counter queue is not configured for COUNT action");
3634                 rc = EINVAL;
3635                 goto fail_counter_queue_uninit;
3636         }
3637
3638         if (sfc_get_service_lcore(SOCKET_ID_ANY) == RTE_MAX_LCORE) {
3639                 rc = EINVAL;
3640                 goto fail_no_service_core;
3641         }
3642
3643         rc = efx_mae_action_set_populate_count(spec);
3644         if (rc != 0) {
3645                 sfc_err(sa,
3646                         "failed to populate counters in MAE action set: %s",
3647                         rte_strerror(rc));
3648                 goto fail_populate_count;
3649         }
3650
3651         return 0;
3652
3653 fail_populate_count:
3654 fail_no_service_core:
3655 fail_counter_queue_uninit:
3656
3657         return rc;
3658 }
3659
3660 static int
3661 sfc_mae_rule_parse_action_phy_port(struct sfc_adapter *sa,
3662                                    const struct rte_flow_action_phy_port *conf,
3663                                    efx_mae_actions_t *spec)
3664 {
3665         efx_mport_sel_t mport;
3666         uint32_t phy_port;
3667         int rc;
3668
3669         if (conf->original != 0)
3670                 phy_port = efx_nic_cfg_get(sa->nic)->enc_assigned_port;
3671         else
3672                 phy_port = conf->index;
3673
3674         rc = efx_mae_mport_by_phy_port(phy_port, &mport);
3675         if (rc != 0) {
3676                 sfc_err(sa, "failed to convert phys. port ID %u to m-port selector: %s",
3677                         phy_port, strerror(rc));
3678                 return rc;
3679         }
3680
3681         rc = efx_mae_action_set_populate_deliver(spec, &mport);
3682         if (rc != 0) {
3683                 sfc_err(sa, "failed to request action DELIVER with m-port selector 0x%08x: %s",
3684                         mport.sel, strerror(rc));
3685         }
3686
3687         return rc;
3688 }
3689
3690 static int
3691 sfc_mae_rule_parse_action_pf_vf(struct sfc_adapter *sa,
3692                                 const struct rte_flow_action_vf *vf_conf,
3693                                 efx_mae_actions_t *spec)
3694 {
3695         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
3696         efx_mport_sel_t mport;
3697         uint32_t vf;
3698         int rc;
3699
3700         if (vf_conf == NULL)
3701                 vf = EFX_PCI_VF_INVALID;
3702         else if (vf_conf->original != 0)
3703                 vf = encp->enc_vf;
3704         else
3705                 vf = vf_conf->id;
3706
3707         rc = efx_mae_mport_by_pcie_function(encp->enc_pf, vf, &mport);
3708         if (rc != 0) {
3709                 sfc_err(sa, "failed to convert PF %u VF %d to m-port: %s",
3710                         encp->enc_pf, (vf != EFX_PCI_VF_INVALID) ? (int)vf : -1,
3711                         strerror(rc));
3712                 return rc;
3713         }
3714
3715         rc = efx_mae_action_set_populate_deliver(spec, &mport);
3716         if (rc != 0) {
3717                 sfc_err(sa, "failed to request action DELIVER with m-port selector 0x%08x: %s",
3718                         mport.sel, strerror(rc));
3719         }
3720
3721         return rc;
3722 }
3723
3724 static int
3725 sfc_mae_rule_parse_action_port_id(struct sfc_adapter *sa,
3726                                   const struct rte_flow_action_port_id *conf,
3727                                   efx_mae_actions_t *spec)
3728 {
3729         struct sfc_adapter_shared * const sas = sfc_sa2shared(sa);
3730         struct sfc_mae *mae = &sa->mae;
3731         efx_mport_sel_t mport;
3732         uint16_t port_id;
3733         int rc;
3734
3735         if (conf->id > UINT16_MAX)
3736                 return EOVERFLOW;
3737
3738         port_id = (conf->original != 0) ? sas->port_id : conf->id;
3739
3740         rc = sfc_mae_switch_get_ethdev_mport(mae->switch_domain_id,
3741                                              port_id, &mport);
3742         if (rc != 0) {
3743                 sfc_err(sa, "failed to get m-port for the given ethdev (port_id=%u): %s",
3744                         port_id, strerror(rc));
3745                 return rc;
3746         }
3747
3748         rc = efx_mae_action_set_populate_deliver(spec, &mport);
3749         if (rc != 0) {
3750                 sfc_err(sa, "failed to request action DELIVER with m-port selector 0x%08x: %s",
3751                         mport.sel, strerror(rc));
3752         }
3753
3754         return rc;
3755 }
3756
3757 static int
3758 sfc_mae_rule_parse_action_port_representor(struct sfc_adapter *sa,
3759                 const struct rte_flow_action_ethdev *conf,
3760                 efx_mae_actions_t *spec)
3761 {
3762         struct sfc_mae *mae = &sa->mae;
3763         efx_mport_sel_t mport;
3764         int rc;
3765
3766         rc = sfc_mae_switch_get_ethdev_mport(mae->switch_domain_id,
3767                                              conf->port_id, &mport);
3768         if (rc != 0) {
3769                 sfc_err(sa, "failed to get m-port for the given ethdev (port_id=%u): %s",
3770                         conf->port_id, strerror(rc));
3771                 return rc;
3772         }
3773
3774         rc = efx_mae_action_set_populate_deliver(spec, &mport);
3775         if (rc != 0) {
3776                 sfc_err(sa, "failed to request action DELIVER with m-port selector 0x%08x: %s",
3777                         mport.sel, strerror(rc));
3778         }
3779
3780         return rc;
3781 }
3782
3783 static int
3784 sfc_mae_rule_parse_action_represented_port(struct sfc_adapter *sa,
3785                 const struct rte_flow_action_ethdev *conf,
3786                 efx_mae_actions_t *spec)
3787 {
3788         struct sfc_mae *mae = &sa->mae;
3789         efx_mport_sel_t mport;
3790         int rc;
3791
3792         rc = sfc_mae_switch_get_entity_mport(mae->switch_domain_id,
3793                                              conf->port_id, &mport);
3794         if (rc != 0) {
3795                 sfc_err(sa, "failed to get m-port for the given ethdev (port_id=%u): %s",
3796                         conf->port_id, strerror(rc));
3797                 return rc;
3798         }
3799
3800         rc = efx_mae_action_set_populate_deliver(spec, &mport);
3801         if (rc != 0) {
3802                 sfc_err(sa, "failed to request action DELIVER with m-port selector 0x%08x: %s",
3803                         mport.sel, strerror(rc));
3804         }
3805
3806         return rc;
3807 }
3808
3809 static const char * const action_names[] = {
3810         [RTE_FLOW_ACTION_TYPE_VXLAN_DECAP] = "VXLAN_DECAP",
3811         [RTE_FLOW_ACTION_TYPE_OF_POP_VLAN] = "OF_POP_VLAN",
3812         [RTE_FLOW_ACTION_TYPE_SET_MAC_DST] = "SET_MAC_DST",
3813         [RTE_FLOW_ACTION_TYPE_SET_MAC_SRC] = "SET_MAC_SRC",
3814         [RTE_FLOW_ACTION_TYPE_OF_DEC_NW_TTL] = "OF_DEC_NW_TTL",
3815         [RTE_FLOW_ACTION_TYPE_DEC_TTL] = "DEC_TTL",
3816         [RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN] = "OF_PUSH_VLAN",
3817         [RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID] = "OF_SET_VLAN_VID",
3818         [RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP] = "OF_SET_VLAN_PCP",
3819         [RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP] = "VXLAN_ENCAP",
3820         [RTE_FLOW_ACTION_TYPE_COUNT] = "COUNT",
3821         [RTE_FLOW_ACTION_TYPE_FLAG] = "FLAG",
3822         [RTE_FLOW_ACTION_TYPE_MARK] = "MARK",
3823         [RTE_FLOW_ACTION_TYPE_PHY_PORT] = "PHY_PORT",
3824         [RTE_FLOW_ACTION_TYPE_PF] = "PF",
3825         [RTE_FLOW_ACTION_TYPE_VF] = "VF",
3826         [RTE_FLOW_ACTION_TYPE_PORT_ID] = "PORT_ID",
3827         [RTE_FLOW_ACTION_TYPE_PORT_REPRESENTOR] = "PORT_REPRESENTOR",
3828         [RTE_FLOW_ACTION_TYPE_REPRESENTED_PORT] = "REPRESENTED_PORT",
3829         [RTE_FLOW_ACTION_TYPE_DROP] = "DROP",
3830         [RTE_FLOW_ACTION_TYPE_JUMP] = "JUMP",
3831 };
3832
3833 static int
3834 sfc_mae_rule_parse_action(struct sfc_adapter *sa,
3835                           const struct rte_flow_action *action,
3836                           const struct sfc_flow_spec_mae *spec_mae,
3837                           struct sfc_mae_actions_bundle *bundle,
3838                           struct sfc_mae_aset_ctx *ctx,
3839                           struct rte_flow_error *error)
3840 {
3841         const struct sfc_mae_outer_rule *outer_rule = spec_mae->outer_rule;
3842         const uint64_t rx_metadata = sa->negotiated_rx_metadata;
3843         efx_mae_actions_t *spec = ctx->spec;
3844         bool custom_error = B_FALSE;
3845         int rc = 0;
3846
3847         switch (action->type) {
3848         case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
3849                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_VXLAN_DECAP,
3850                                        bundle->actions_mask);
3851                 if (outer_rule == NULL ||
3852                     outer_rule->encap_type != EFX_TUNNEL_PROTOCOL_VXLAN)
3853                         rc = EINVAL;
3854                 else
3855                         rc = efx_mae_action_set_populate_decap(spec);
3856                 break;
3857         case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
3858                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_OF_POP_VLAN,
3859                                        bundle->actions_mask);
3860                 rc = efx_mae_action_set_populate_vlan_pop(spec);
3861                 break;
3862         case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
3863                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_SET_MAC_DST,
3864                                        bundle->actions_mask);
3865                 rc = sfc_mae_rule_parse_action_set_mac(sa, SFC_MAE_MAC_ADDR_DST,
3866                                                        action->conf, ctx,
3867                                                        error);
3868                 custom_error = B_TRUE;
3869                 break;
3870         case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
3871                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_SET_MAC_SRC,
3872                                        bundle->actions_mask);
3873                 rc = sfc_mae_rule_parse_action_set_mac(sa, SFC_MAE_MAC_ADDR_SRC,
3874                                                        action->conf, ctx,
3875                                                        error);
3876                 custom_error = B_TRUE;
3877                 break;
3878         case RTE_FLOW_ACTION_TYPE_OF_DEC_NW_TTL:
3879         case RTE_FLOW_ACTION_TYPE_DEC_TTL:
3880                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_OF_DEC_NW_TTL,
3881                                        bundle->actions_mask);
3882                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_DEC_TTL,
3883                                        bundle->actions_mask);
3884                 rc = efx_mae_action_set_populate_decr_ip_ttl(spec);
3885                 break;
3886         case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
3887                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN,
3888                                        bundle->actions_mask);
3889                 sfc_mae_rule_parse_action_of_push_vlan(action->conf, bundle);
3890                 break;
3891         case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
3892                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID,
3893                                        bundle->actions_mask);
3894                 sfc_mae_rule_parse_action_of_set_vlan_vid(action->conf, bundle);
3895                 break;
3896         case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
3897                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP,
3898                                        bundle->actions_mask);
3899                 sfc_mae_rule_parse_action_of_set_vlan_pcp(action->conf, bundle);
3900                 break;
3901         case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
3902                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP,
3903                                        bundle->actions_mask);
3904                 rc = sfc_mae_rule_parse_action_vxlan_encap(&sa->mae,
3905                                                            action->conf,
3906                                                            spec, error);
3907                 custom_error = B_TRUE;
3908                 break;
3909         case RTE_FLOW_ACTION_TYPE_COUNT:
3910                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_COUNT,
3911                                        bundle->actions_mask);
3912                 rc = sfc_mae_rule_parse_action_count(sa, action->conf, spec);
3913                 break;
3914         case RTE_FLOW_ACTION_TYPE_FLAG:
3915                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_FLAG,
3916                                        bundle->actions_mask);
3917                 if ((rx_metadata & RTE_ETH_RX_METADATA_USER_FLAG) != 0) {
3918                         rc = efx_mae_action_set_populate_flag(spec);
3919                 } else {
3920                         rc = rte_flow_error_set(error, ENOTSUP,
3921                                                 RTE_FLOW_ERROR_TYPE_ACTION,
3922                                                 action,
3923                                                 "flag delivery has not been negotiated");
3924                         custom_error = B_TRUE;
3925                 }
3926                 break;
3927         case RTE_FLOW_ACTION_TYPE_MARK:
3928                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_MARK,
3929                                        bundle->actions_mask);
3930                 if ((rx_metadata & RTE_ETH_RX_METADATA_USER_MARK) != 0 ||
3931                     spec_mae->ft_rule_type == SFC_FT_RULE_TUNNEL) {
3932                         rc = sfc_mae_rule_parse_action_mark(sa, action->conf,
3933                                                             spec_mae, spec);
3934                 } else {
3935                         rc = rte_flow_error_set(error, ENOTSUP,
3936                                                 RTE_FLOW_ERROR_TYPE_ACTION,
3937                                                 action,
3938                                                 "mark delivery has not been negotiated");
3939                         custom_error = B_TRUE;
3940                 }
3941                 break;
3942         case RTE_FLOW_ACTION_TYPE_PHY_PORT:
3943                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_PHY_PORT,
3944                                        bundle->actions_mask);
3945                 rc = sfc_mae_rule_parse_action_phy_port(sa, action->conf, spec);
3946                 break;
3947         case RTE_FLOW_ACTION_TYPE_PF:
3948                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_PF,
3949                                        bundle->actions_mask);
3950                 rc = sfc_mae_rule_parse_action_pf_vf(sa, NULL, spec);
3951                 break;
3952         case RTE_FLOW_ACTION_TYPE_VF:
3953                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_VF,
3954                                        bundle->actions_mask);
3955                 rc = sfc_mae_rule_parse_action_pf_vf(sa, action->conf, spec);
3956                 break;
3957         case RTE_FLOW_ACTION_TYPE_PORT_ID:
3958                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_PORT_ID,
3959                                        bundle->actions_mask);
3960                 rc = sfc_mae_rule_parse_action_port_id(sa, action->conf, spec);
3961                 break;
3962         case RTE_FLOW_ACTION_TYPE_PORT_REPRESENTOR:
3963                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_PORT_REPRESENTOR,
3964                                        bundle->actions_mask);
3965                 rc = sfc_mae_rule_parse_action_port_representor(sa,
3966                                 action->conf, spec);
3967                 break;
3968         case RTE_FLOW_ACTION_TYPE_REPRESENTED_PORT:
3969                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_REPRESENTED_PORT,
3970                                        bundle->actions_mask);
3971                 rc = sfc_mae_rule_parse_action_represented_port(sa,
3972                                 action->conf, spec);
3973                 break;
3974         case RTE_FLOW_ACTION_TYPE_DROP:
3975                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_DROP,
3976                                        bundle->actions_mask);
3977                 rc = efx_mae_action_set_populate_drop(spec);
3978                 break;
3979         case RTE_FLOW_ACTION_TYPE_JUMP:
3980                 if (spec_mae->ft_rule_type == SFC_FT_RULE_TUNNEL) {
3981                         /* Workaround. See sfc_flow_parse_rte_to_mae() */
3982                         break;
3983                 }
3984                 /* FALLTHROUGH */
3985         default:
3986                 return rte_flow_error_set(error, ENOTSUP,
3987                                 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
3988                                 "Unsupported action");
3989         }
3990
3991         if (rc == 0) {
3992                 bundle->actions_mask |= (1ULL << action->type);
3993         } else if (!custom_error) {
3994                 if (action->type < RTE_DIM(action_names)) {
3995                         const char *action_name = action_names[action->type];
3996
3997                         if (action_name != NULL) {
3998                                 sfc_err(sa, "action %s was rejected: %s",
3999                                         action_name, strerror(rc));
4000                         }
4001                 }
4002                 rc = rte_flow_error_set(error, rc, RTE_FLOW_ERROR_TYPE_ACTION,
4003                                 NULL, "Failed to request the action");
4004         }
4005
4006         return rc;
4007 }
4008
4009 static void
4010 sfc_mae_bounce_eh_invalidate(struct sfc_mae_bounce_eh *bounce_eh)
4011 {
4012         bounce_eh->type = EFX_TUNNEL_PROTOCOL_NONE;
4013 }
4014
4015 static int
4016 sfc_mae_process_encap_header(struct sfc_adapter *sa,
4017                              const struct sfc_mae_bounce_eh *bounce_eh,
4018                              struct sfc_mae_encap_header **encap_headerp)
4019 {
4020         if (bounce_eh->type == EFX_TUNNEL_PROTOCOL_NONE) {
4021                 encap_headerp = NULL;
4022                 return 0;
4023         }
4024
4025         *encap_headerp = sfc_mae_encap_header_attach(sa, bounce_eh);
4026         if (*encap_headerp != NULL)
4027                 return 0;
4028
4029         return sfc_mae_encap_header_add(sa, bounce_eh, encap_headerp);
4030 }
4031
4032 int
4033 sfc_mae_rule_parse_actions(struct sfc_adapter *sa,
4034                            const struct rte_flow_action actions[],
4035                            struct sfc_flow_spec_mae *spec_mae,
4036                            struct rte_flow_error *error)
4037 {
4038         struct sfc_mae_actions_bundle bundle = {0};
4039         const struct rte_flow_action *action;
4040         struct sfc_mae_aset_ctx ctx = {0};
4041         struct sfc_mae *mae = &sa->mae;
4042         int rc;
4043
4044         rte_errno = 0;
4045
4046         if (actions == NULL) {
4047                 return rte_flow_error_set(error, EINVAL,
4048                                 RTE_FLOW_ERROR_TYPE_ACTION_NUM, NULL,
4049                                 "NULL actions");
4050         }
4051
4052         rc = efx_mae_action_set_spec_init(sa->nic, &ctx.spec);
4053         if (rc != 0)
4054                 goto fail_action_set_spec_init;
4055
4056         for (action = actions;
4057              action->type != RTE_FLOW_ACTION_TYPE_END; ++action) {
4058                 if (action->type == RTE_FLOW_ACTION_TYPE_COUNT)
4059                         ++(ctx.n_counters);
4060         }
4061
4062         if (spec_mae->ft_rule_type == SFC_FT_RULE_SWITCH) {
4063                 /* TUNNEL rules don't decapsulate packets. SWITCH rules do. */
4064                 rc = efx_mae_action_set_populate_decap(ctx.spec);
4065                 if (rc != 0)
4066                         goto fail_enforce_ft_decap;
4067
4068                 if (ctx.n_counters == 0 &&
4069                     sfc_mae_counter_stream_enabled(sa)) {
4070                         /*
4071                          * The user opted not to use action COUNT in this rule,
4072                          * but the counter should be enabled implicitly because
4073                          * packets hitting this rule contribute to the tunnel's
4074                          * total number of hits. See sfc_mae_counter_get().
4075                          */
4076                         rc = efx_mae_action_set_populate_count(ctx.spec);
4077                         if (rc != 0)
4078                                 goto fail_enforce_ft_count;
4079
4080                         ctx.n_counters = 1;
4081                 }
4082         }
4083
4084         /* Cleanup after previous encap. header bounce buffer usage. */
4085         sfc_mae_bounce_eh_invalidate(&mae->bounce_eh);
4086
4087         for (action = actions;
4088              action->type != RTE_FLOW_ACTION_TYPE_END; ++action) {
4089                 rc = sfc_mae_actions_bundle_sync(action, &bundle,
4090                                                  ctx.spec, error);
4091                 if (rc != 0)
4092                         goto fail_rule_parse_action;
4093
4094                 rc = sfc_mae_rule_parse_action(sa, action, spec_mae,
4095                                                &bundle, &ctx, error);
4096                 if (rc != 0)
4097                         goto fail_rule_parse_action;
4098         }
4099
4100         rc = sfc_mae_actions_bundle_sync(action, &bundle, ctx.spec, error);
4101         if (rc != 0)
4102                 goto fail_rule_parse_action;
4103
4104         rc = sfc_mae_process_encap_header(sa, &mae->bounce_eh,
4105                                           &ctx.encap_header);
4106         if (rc != 0)
4107                 goto fail_process_encap_header;
4108
4109         if (ctx.n_counters > 1) {
4110                 rc = ENOTSUP;
4111                 sfc_err(sa, "too many count actions requested: %u",
4112                         ctx.n_counters);
4113                 goto fail_nb_count;
4114         }
4115
4116         switch (spec_mae->ft_rule_type) {
4117         case SFC_FT_RULE_NONE:
4118                 break;
4119         case SFC_FT_RULE_TUNNEL:
4120                 /* Workaround. See sfc_flow_parse_rte_to_mae() */
4121                 rc = sfc_mae_rule_parse_action_pf_vf(sa, NULL, ctx.spec);
4122                 if (rc != 0)
4123                         goto fail_workaround_tunnel_delivery;
4124
4125                 ctx.counter_ft_ctx = spec_mae->ft_ctx;
4126                 break;
4127         case SFC_FT_RULE_SWITCH:
4128                 /*
4129                  * Packets that go to the rule's AR have FT mark set (from the
4130                  * TUNNEL rule OR's RECIRC_ID). Remove this mark in matching
4131                  * packets. The user may have provided their own action
4132                  * MARK above, so don't check the return value here.
4133                  */
4134                 (void)efx_mae_action_set_populate_mark(ctx.spec, 0);
4135
4136                 ctx.ft_switch_hit_counter =
4137                         &spec_mae->ft_ctx->switch_hit_counter;
4138                 break;
4139         default:
4140                 SFC_ASSERT(B_FALSE);
4141         }
4142
4143         spec_mae->action_set = sfc_mae_action_set_attach(sa, &ctx);
4144         if (spec_mae->action_set != NULL) {
4145                 sfc_mae_encap_header_del(sa, ctx.encap_header);
4146                 efx_mae_action_set_spec_fini(sa->nic, ctx.spec);
4147                 return 0;
4148         }
4149
4150         rc = sfc_mae_action_set_add(sa, actions, &ctx, &spec_mae->action_set);
4151         if (rc != 0)
4152                 goto fail_action_set_add;
4153
4154         return 0;
4155
4156 fail_action_set_add:
4157 fail_workaround_tunnel_delivery:
4158 fail_nb_count:
4159         sfc_mae_encap_header_del(sa, ctx.encap_header);
4160
4161 fail_process_encap_header:
4162 fail_rule_parse_action:
4163         sfc_mae_mac_addr_del(sa, ctx.src_mac);
4164         sfc_mae_mac_addr_del(sa, ctx.dst_mac);
4165         efx_mae_action_set_spec_fini(sa->nic, ctx.spec);
4166
4167 fail_enforce_ft_count:
4168 fail_enforce_ft_decap:
4169 fail_action_set_spec_init:
4170         if (rc > 0 && rte_errno == 0) {
4171                 rc = rte_flow_error_set(error, rc,
4172                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4173                         NULL, "Failed to process the action");
4174         }
4175         return rc;
4176 }
4177
4178 static bool
4179 sfc_mae_rules_class_cmp(struct sfc_adapter *sa,
4180                         const efx_mae_match_spec_t *left,
4181                         const efx_mae_match_spec_t *right)
4182 {
4183         bool have_same_class;
4184         int rc;
4185
4186         rc = efx_mae_match_specs_class_cmp(sa->nic, left, right,
4187                                            &have_same_class);
4188
4189         return (rc == 0) ? have_same_class : false;
4190 }
4191
4192 static int
4193 sfc_mae_outer_rule_class_verify(struct sfc_adapter *sa,
4194                                 struct sfc_mae_outer_rule *rule)
4195 {
4196         struct sfc_mae_fw_rsrc *fw_rsrc = &rule->fw_rsrc;
4197         struct sfc_mae_outer_rule *entry;
4198         struct sfc_mae *mae = &sa->mae;
4199
4200         if (fw_rsrc->rule_id.id != EFX_MAE_RSRC_ID_INVALID) {
4201                 /* An active rule is reused. It's class is wittingly valid. */
4202                 return 0;
4203         }
4204
4205         TAILQ_FOREACH_REVERSE(entry, &mae->outer_rules,
4206                               sfc_mae_outer_rules, entries) {
4207                 const efx_mae_match_spec_t *left = entry->match_spec;
4208                 const efx_mae_match_spec_t *right = rule->match_spec;
4209
4210                 if (entry == rule)
4211                         continue;
4212
4213                 if (sfc_mae_rules_class_cmp(sa, left, right))
4214                         return 0;
4215         }
4216
4217         sfc_info(sa, "for now, the HW doesn't support rule validation, and HW "
4218                  "support for outer frame pattern items is not guaranteed; "
4219                  "other than that, the items are valid from SW standpoint");
4220         return 0;
4221 }
4222
4223 static int
4224 sfc_mae_action_rule_class_verify(struct sfc_adapter *sa,
4225                                  struct sfc_flow_spec_mae *spec)
4226 {
4227         const struct rte_flow *entry;
4228
4229         if (spec->match_spec == NULL)
4230                 return 0;
4231
4232         TAILQ_FOREACH_REVERSE(entry, &sa->flow_list, sfc_flow_list, entries) {
4233                 const struct sfc_flow_spec *entry_spec = &entry->spec;
4234                 const struct sfc_flow_spec_mae *es_mae = &entry_spec->mae;
4235                 const efx_mae_match_spec_t *left = es_mae->match_spec;
4236                 const efx_mae_match_spec_t *right = spec->match_spec;
4237
4238                 switch (entry_spec->type) {
4239                 case SFC_FLOW_SPEC_FILTER:
4240                         /* Ignore VNIC-level flows */
4241                         break;
4242                 case SFC_FLOW_SPEC_MAE:
4243                         if (sfc_mae_rules_class_cmp(sa, left, right))
4244                                 return 0;
4245                         break;
4246                 default:
4247                         SFC_ASSERT(false);
4248                 }
4249         }
4250
4251         sfc_info(sa, "for now, the HW doesn't support rule validation, and HW "
4252                  "support for inner frame pattern items is not guaranteed; "
4253                  "other than that, the items are valid from SW standpoint");
4254         return 0;
4255 }
4256
4257 /**
4258  * Confirm that a given flow can be accepted by the FW.
4259  *
4260  * @param sa
4261  *   Software adapter context
4262  * @param flow
4263  *   Flow to be verified
4264  * @return
4265  *   Zero on success and non-zero in the case of error.
4266  *   A special value of EAGAIN indicates that the adapter is
4267  *   not in started state. This state is compulsory because
4268  *   it only makes sense to compare the rule class of the flow
4269  *   being validated with classes of the active rules.
4270  *   Such classes are wittingly supported by the FW.
4271  */
4272 int
4273 sfc_mae_flow_verify(struct sfc_adapter *sa,
4274                     struct rte_flow *flow)
4275 {
4276         struct sfc_flow_spec *spec = &flow->spec;
4277         struct sfc_flow_spec_mae *spec_mae = &spec->mae;
4278         struct sfc_mae_outer_rule *outer_rule = spec_mae->outer_rule;
4279         int rc;
4280
4281         SFC_ASSERT(sfc_adapter_is_locked(sa));
4282
4283         if (sa->state != SFC_ETHDEV_STARTED)
4284                 return EAGAIN;
4285
4286         if (outer_rule != NULL) {
4287                 rc = sfc_mae_outer_rule_class_verify(sa, outer_rule);
4288                 if (rc != 0)
4289                         return rc;
4290         }
4291
4292         return sfc_mae_action_rule_class_verify(sa, spec_mae);
4293 }
4294
4295 int
4296 sfc_mae_flow_insert(struct sfc_adapter *sa,
4297                     struct rte_flow *flow)
4298 {
4299         struct sfc_flow_spec *spec = &flow->spec;
4300         struct sfc_flow_spec_mae *spec_mae = &spec->mae;
4301         struct sfc_mae_outer_rule *outer_rule = spec_mae->outer_rule;
4302         struct sfc_mae_action_set *action_set = spec_mae->action_set;
4303         struct sfc_mae_fw_rsrc *fw_rsrc;
4304         int rc;
4305
4306         SFC_ASSERT(spec_mae->rule_id.id == EFX_MAE_RSRC_ID_INVALID);
4307
4308         if (outer_rule != NULL) {
4309                 rc = sfc_mae_outer_rule_enable(sa, outer_rule,
4310                                                spec_mae->match_spec);
4311                 if (rc != 0)
4312                         goto fail_outer_rule_enable;
4313         }
4314
4315         if (spec_mae->ft_rule_type == SFC_FT_RULE_TUNNEL) {
4316                 spec_mae->ft_ctx->reset_tunnel_hit_counter =
4317                         spec_mae->ft_ctx->switch_hit_counter;
4318         }
4319
4320         if (action_set == NULL) {
4321                 sfc_dbg(sa, "enabled flow=%p (no AR)", flow);
4322                 return 0;
4323         }
4324
4325         rc = sfc_mae_action_set_enable(sa, action_set);
4326         if (rc != 0)
4327                 goto fail_action_set_enable;
4328
4329         if (action_set->n_counters > 0) {
4330                 rc = sfc_mae_counter_start(sa);
4331                 if (rc != 0) {
4332                         sfc_err(sa, "failed to start MAE counters support: %s",
4333                                 rte_strerror(rc));
4334                         goto fail_mae_counter_start;
4335                 }
4336         }
4337
4338         fw_rsrc = &action_set->fw_rsrc;
4339
4340         rc = efx_mae_action_rule_insert(sa->nic, spec_mae->match_spec,
4341                                         NULL, &fw_rsrc->aset_id,
4342                                         &spec_mae->rule_id);
4343         if (rc != 0)
4344                 goto fail_action_rule_insert;
4345
4346         sfc_dbg(sa, "enabled flow=%p: AR_ID=0x%08x",
4347                 flow, spec_mae->rule_id.id);
4348
4349         return 0;
4350
4351 fail_action_rule_insert:
4352 fail_mae_counter_start:
4353         sfc_mae_action_set_disable(sa, action_set);
4354
4355 fail_action_set_enable:
4356         if (outer_rule != NULL)
4357                 sfc_mae_outer_rule_disable(sa, outer_rule);
4358
4359 fail_outer_rule_enable:
4360         return rc;
4361 }
4362
4363 int
4364 sfc_mae_flow_remove(struct sfc_adapter *sa,
4365                     struct rte_flow *flow)
4366 {
4367         struct sfc_flow_spec *spec = &flow->spec;
4368         struct sfc_flow_spec_mae *spec_mae = &spec->mae;
4369         struct sfc_mae_action_set *action_set = spec_mae->action_set;
4370         struct sfc_mae_outer_rule *outer_rule = spec_mae->outer_rule;
4371         int rc;
4372
4373         if (action_set == NULL) {
4374                 sfc_dbg(sa, "disabled flow=%p (no AR)", flow);
4375                 goto skip_action_rule;
4376         }
4377
4378         SFC_ASSERT(spec_mae->rule_id.id != EFX_MAE_RSRC_ID_INVALID);
4379
4380         rc = efx_mae_action_rule_remove(sa->nic, &spec_mae->rule_id);
4381         if (rc != 0) {
4382                 sfc_err(sa, "failed to disable flow=%p with AR_ID=0x%08x: %s",
4383                         flow, spec_mae->rule_id.id, strerror(rc));
4384         }
4385         sfc_dbg(sa, "disabled flow=%p with AR_ID=0x%08x",
4386                 flow, spec_mae->rule_id.id);
4387         spec_mae->rule_id.id = EFX_MAE_RSRC_ID_INVALID;
4388
4389         sfc_mae_action_set_disable(sa, action_set);
4390
4391 skip_action_rule:
4392         if (outer_rule != NULL)
4393                 sfc_mae_outer_rule_disable(sa, outer_rule);
4394
4395         return 0;
4396 }
4397
4398 static int
4399 sfc_mae_query_counter(struct sfc_adapter *sa,
4400                       struct sfc_flow_spec_mae *spec,
4401                       const struct rte_flow_action *action,
4402                       struct rte_flow_query_count *data,
4403                       struct rte_flow_error *error)
4404 {
4405         struct sfc_mae_action_set *action_set = spec->action_set;
4406         const struct rte_flow_action_count *conf = action->conf;
4407         unsigned int i;
4408         int rc;
4409
4410         if (action_set == NULL || action_set->n_counters == 0) {
4411                 return rte_flow_error_set(error, EINVAL,
4412                         RTE_FLOW_ERROR_TYPE_ACTION, action,
4413                         "Queried flow rule does not have count actions");
4414         }
4415
4416         for (i = 0; i < action_set->n_counters; i++) {
4417                 /*
4418                  * Get the first available counter of the flow rule if
4419                  * counter ID is not specified, provided that this
4420                  * counter is not an automatic (implicit) one.
4421                  */
4422                 if (conf != NULL && action_set->counters[i].rte_id != conf->id)
4423                         continue;
4424
4425                 rc = sfc_mae_counter_get(&sa->mae.counter_registry.counters,
4426                                          &action_set->counters[i], data);
4427                 if (rc != 0) {
4428                         return rte_flow_error_set(error, EINVAL,
4429                                 RTE_FLOW_ERROR_TYPE_ACTION, action,
4430                                 "Queried flow rule counter action is invalid");
4431                 }
4432
4433                 return 0;
4434         }
4435
4436         return rte_flow_error_set(error, ENOENT,
4437                                   RTE_FLOW_ERROR_TYPE_ACTION, action,
4438                                   "no such flow rule action or such count ID");
4439 }
4440
4441 int
4442 sfc_mae_flow_query(struct rte_eth_dev *dev,
4443                    struct rte_flow *flow,
4444                    const struct rte_flow_action *action,
4445                    void *data,
4446                    struct rte_flow_error *error)
4447 {
4448         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
4449         struct sfc_flow_spec *spec = &flow->spec;
4450         struct sfc_flow_spec_mae *spec_mae = &spec->mae;
4451
4452         switch (action->type) {
4453         case RTE_FLOW_ACTION_TYPE_COUNT:
4454                 return sfc_mae_query_counter(sa, spec_mae, action,
4455                                              data, error);
4456         default:
4457                 return rte_flow_error_set(error, ENOTSUP,
4458                         RTE_FLOW_ERROR_TYPE_ACTION, NULL,
4459                         "Query for action of this type is not supported");
4460         }
4461 }
4462
4463 int
4464 sfc_mae_switchdev_init(struct sfc_adapter *sa)
4465 {
4466         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
4467         struct sfc_mae *mae = &sa->mae;
4468         efx_mport_sel_t pf;
4469         efx_mport_sel_t phy;
4470         int rc;
4471
4472         sfc_log_init(sa, "entry");
4473
4474         if (!sa->switchdev) {
4475                 sfc_log_init(sa, "switchdev is not enabled - skip");
4476                 return 0;
4477         }
4478
4479         if (mae->status != SFC_MAE_STATUS_ADMIN) {
4480                 rc = ENOTSUP;
4481                 sfc_err(sa, "failed to init switchdev - no admin MAE privilege");
4482                 goto fail_no_mae;
4483         }
4484
4485         rc = efx_mae_mport_by_pcie_function(encp->enc_pf, EFX_PCI_VF_INVALID,
4486                                             &pf);
4487         if (rc != 0) {
4488                 sfc_err(sa, "failed get PF mport");
4489                 goto fail_pf_get;
4490         }
4491
4492         rc = efx_mae_mport_by_phy_port(encp->enc_assigned_port, &phy);
4493         if (rc != 0) {
4494                 sfc_err(sa, "failed get PHY mport");
4495                 goto fail_phy_get;
4496         }
4497
4498         rc = sfc_mae_rule_add_mport_match_deliver(sa, &pf, &phy,
4499                         SFC_MAE_RULE_PRIO_LOWEST,
4500                         &mae->switchdev_rule_pf_to_ext);
4501         if (rc != 0) {
4502                 sfc_err(sa, "failed add MAE rule to forward from PF to PHY");
4503                 goto fail_pf_add;
4504         }
4505
4506         rc = sfc_mae_rule_add_mport_match_deliver(sa, &phy, &pf,
4507                         SFC_MAE_RULE_PRIO_LOWEST,
4508                         &mae->switchdev_rule_ext_to_pf);
4509         if (rc != 0) {
4510                 sfc_err(sa, "failed add MAE rule to forward from PHY to PF");
4511                 goto fail_phy_add;
4512         }
4513
4514         sfc_log_init(sa, "done");
4515
4516         return 0;
4517
4518 fail_phy_add:
4519         sfc_mae_rule_del(sa, mae->switchdev_rule_pf_to_ext);
4520
4521 fail_pf_add:
4522 fail_phy_get:
4523 fail_pf_get:
4524 fail_no_mae:
4525         sfc_log_init(sa, "failed: %s", rte_strerror(rc));
4526         return rc;
4527 }
4528
4529 void
4530 sfc_mae_switchdev_fini(struct sfc_adapter *sa)
4531 {
4532         struct sfc_mae *mae = &sa->mae;
4533
4534         if (!sa->switchdev)
4535                 return;
4536
4537         sfc_mae_rule_del(sa, mae->switchdev_rule_pf_to_ext);
4538         sfc_mae_rule_del(sa, mae->switchdev_rule_ext_to_pf);
4539 }