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