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