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