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