net/sfc: support flow item VLAN in transfer rules
[dpdk.git] / drivers / net / sfc / sfc_mae.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright(c) 2019-2020 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_common.h>
13
14 #include "efx.h"
15
16 #include "sfc.h"
17 #include "sfc_log.h"
18 #include "sfc_switch.h"
19
20 static int
21 sfc_mae_assign_entity_mport(struct sfc_adapter *sa,
22                             efx_mport_sel_t *mportp)
23 {
24         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
25
26         return efx_mae_mport_by_pcie_function(encp->enc_pf, encp->enc_vf,
27                                               mportp);
28 }
29
30 int
31 sfc_mae_attach(struct sfc_adapter *sa)
32 {
33         struct sfc_adapter_shared * const sas = sfc_sa2shared(sa);
34         struct sfc_mae_switch_port_request switch_port_request = {0};
35         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
36         efx_mport_sel_t entity_mport;
37         struct sfc_mae *mae = &sa->mae;
38         efx_mae_limits_t limits;
39         int rc;
40
41         sfc_log_init(sa, "entry");
42
43         if (!encp->enc_mae_supported) {
44                 mae->status = SFC_MAE_STATUS_UNSUPPORTED;
45                 return 0;
46         }
47
48         sfc_log_init(sa, "init MAE");
49         rc = efx_mae_init(sa->nic);
50         if (rc != 0)
51                 goto fail_mae_init;
52
53         sfc_log_init(sa, "get MAE limits");
54         rc = efx_mae_get_limits(sa->nic, &limits);
55         if (rc != 0)
56                 goto fail_mae_get_limits;
57
58         sfc_log_init(sa, "assign entity MPORT");
59         rc = sfc_mae_assign_entity_mport(sa, &entity_mport);
60         if (rc != 0)
61                 goto fail_mae_assign_entity_mport;
62
63         sfc_log_init(sa, "assign RTE switch domain");
64         rc = sfc_mae_assign_switch_domain(sa, &mae->switch_domain_id);
65         if (rc != 0)
66                 goto fail_mae_assign_switch_domain;
67
68         sfc_log_init(sa, "assign RTE switch port");
69         switch_port_request.type = SFC_MAE_SWITCH_PORT_INDEPENDENT;
70         switch_port_request.entity_mportp = &entity_mport;
71         /*
72          * As of now, the driver does not support representors, so
73          * RTE ethdev MPORT simply matches that of the entity.
74          */
75         switch_port_request.ethdev_mportp = &entity_mport;
76         switch_port_request.ethdev_port_id = sas->port_id;
77         rc = sfc_mae_assign_switch_port(mae->switch_domain_id,
78                                         &switch_port_request,
79                                         &mae->switch_port_id);
80         if (rc != 0)
81                 goto fail_mae_assign_switch_port;
82
83         mae->status = SFC_MAE_STATUS_SUPPORTED;
84         mae->nb_action_rule_prios_max = limits.eml_max_n_action_prios;
85         TAILQ_INIT(&mae->action_sets);
86
87         sfc_log_init(sa, "done");
88
89         return 0;
90
91 fail_mae_assign_switch_port:
92 fail_mae_assign_switch_domain:
93 fail_mae_assign_entity_mport:
94 fail_mae_get_limits:
95         efx_mae_fini(sa->nic);
96
97 fail_mae_init:
98         sfc_log_init(sa, "failed %d", rc);
99
100         return rc;
101 }
102
103 void
104 sfc_mae_detach(struct sfc_adapter *sa)
105 {
106         struct sfc_mae *mae = &sa->mae;
107         enum sfc_mae_status status_prev = mae->status;
108
109         sfc_log_init(sa, "entry");
110
111         mae->nb_action_rule_prios_max = 0;
112         mae->status = SFC_MAE_STATUS_UNKNOWN;
113
114         if (status_prev != SFC_MAE_STATUS_SUPPORTED)
115                 return;
116
117         efx_mae_fini(sa->nic);
118
119         sfc_log_init(sa, "done");
120 }
121
122 static struct sfc_mae_action_set *
123 sfc_mae_action_set_attach(struct sfc_adapter *sa,
124                           const efx_mae_actions_t *spec)
125 {
126         struct sfc_mae_action_set *action_set;
127         struct sfc_mae *mae = &sa->mae;
128
129         SFC_ASSERT(sfc_adapter_is_locked(sa));
130
131         TAILQ_FOREACH(action_set, &mae->action_sets, entries) {
132                 if (efx_mae_action_set_specs_equal(action_set->spec, spec)) {
133                         ++(action_set->refcnt);
134                         return action_set;
135                 }
136         }
137
138         return NULL;
139 }
140
141 static int
142 sfc_mae_action_set_add(struct sfc_adapter *sa,
143                        efx_mae_actions_t *spec,
144                        struct sfc_mae_action_set **action_setp)
145 {
146         struct sfc_mae_action_set *action_set;
147         struct sfc_mae *mae = &sa->mae;
148
149         SFC_ASSERT(sfc_adapter_is_locked(sa));
150
151         action_set = rte_zmalloc("sfc_mae_action_set", sizeof(*action_set), 0);
152         if (action_set == NULL)
153                 return ENOMEM;
154
155         action_set->refcnt = 1;
156         action_set->spec = spec;
157
158         action_set->fw_rsrc.aset_id.id = EFX_MAE_RSRC_ID_INVALID;
159
160         TAILQ_INSERT_TAIL(&mae->action_sets, action_set, entries);
161
162         *action_setp = action_set;
163
164         return 0;
165 }
166
167 static void
168 sfc_mae_action_set_del(struct sfc_adapter *sa,
169                        struct sfc_mae_action_set *action_set)
170 {
171         struct sfc_mae *mae = &sa->mae;
172
173         SFC_ASSERT(sfc_adapter_is_locked(sa));
174         SFC_ASSERT(action_set->refcnt != 0);
175
176         --(action_set->refcnt);
177
178         if (action_set->refcnt != 0)
179                 return;
180
181         SFC_ASSERT(action_set->fw_rsrc.aset_id.id == EFX_MAE_RSRC_ID_INVALID);
182         SFC_ASSERT(action_set->fw_rsrc.refcnt == 0);
183
184         efx_mae_action_set_spec_fini(sa->nic, action_set->spec);
185         TAILQ_REMOVE(&mae->action_sets, action_set, entries);
186         rte_free(action_set);
187 }
188
189 static int
190 sfc_mae_action_set_enable(struct sfc_adapter *sa,
191                           struct sfc_mae_action_set *action_set)
192 {
193         struct sfc_mae_fw_rsrc *fw_rsrc = &action_set->fw_rsrc;
194         int rc;
195
196         SFC_ASSERT(sfc_adapter_is_locked(sa));
197
198         if (fw_rsrc->refcnt == 0) {
199                 SFC_ASSERT(fw_rsrc->aset_id.id == EFX_MAE_RSRC_ID_INVALID);
200                 SFC_ASSERT(action_set->spec != NULL);
201
202                 rc = efx_mae_action_set_alloc(sa->nic, action_set->spec,
203                                               &fw_rsrc->aset_id);
204                 if (rc != 0)
205                         return rc;
206         }
207
208         ++(fw_rsrc->refcnt);
209
210         return 0;
211 }
212
213 static int
214 sfc_mae_action_set_disable(struct sfc_adapter *sa,
215                            struct sfc_mae_action_set *action_set)
216 {
217         struct sfc_mae_fw_rsrc *fw_rsrc = &action_set->fw_rsrc;
218         int rc;
219
220         SFC_ASSERT(sfc_adapter_is_locked(sa));
221         SFC_ASSERT(fw_rsrc->aset_id.id != EFX_MAE_RSRC_ID_INVALID);
222         SFC_ASSERT(fw_rsrc->refcnt != 0);
223
224         if (fw_rsrc->refcnt == 1) {
225                 rc = efx_mae_action_set_free(sa->nic, &fw_rsrc->aset_id);
226                 if (rc != 0)
227                         return rc;
228
229                 fw_rsrc->aset_id.id = EFX_MAE_RSRC_ID_INVALID;
230         }
231
232         --(fw_rsrc->refcnt);
233
234         return 0;
235 }
236
237 void
238 sfc_mae_flow_cleanup(struct sfc_adapter *sa,
239                      struct rte_flow *flow)
240 {
241         struct sfc_flow_spec *spec;
242         struct sfc_flow_spec_mae *spec_mae;
243
244         if (flow == NULL)
245                 return;
246
247         spec = &flow->spec;
248
249         if (spec == NULL)
250                 return;
251
252         spec_mae = &spec->mae;
253
254         SFC_ASSERT(spec_mae->rule_id.id == EFX_MAE_RSRC_ID_INVALID);
255
256         if (spec_mae->action_set != NULL)
257                 sfc_mae_action_set_del(sa, spec_mae->action_set);
258
259         if (spec_mae->match_spec != NULL)
260                 efx_mae_match_spec_fini(sa->nic, spec_mae->match_spec);
261 }
262
263 static int
264 sfc_mae_set_ethertypes(struct sfc_mae_parse_ctx *ctx)
265 {
266         efx_mae_match_spec_t *efx_spec = ctx->match_spec_action;
267         struct sfc_mae_pattern_data *pdata = &ctx->pattern_data;
268         const efx_mae_field_id_t field_ids[] = {
269                 EFX_MAE_FIELD_VLAN0_PROTO_BE,
270                 EFX_MAE_FIELD_VLAN1_PROTO_BE,
271         };
272         const struct sfc_mae_ethertype *et;
273         unsigned int i;
274         int rc;
275
276         /*
277          * In accordance with RTE flow API convention, the innermost L2
278          * item's "type" ("inner_type") is a L3 EtherType. If there is
279          * no L3 item, it's 0x0000/0x0000.
280          */
281         et = &pdata->ethertypes[pdata->nb_vlan_tags];
282         rc = efx_mae_match_spec_field_set(efx_spec, EFX_MAE_FIELD_ETHER_TYPE_BE,
283                                           sizeof(et->value),
284                                           (const uint8_t *)&et->value,
285                                           sizeof(et->mask),
286                                           (const uint8_t *)&et->mask);
287         if (rc != 0)
288                 return rc;
289
290         /*
291          * sfc_mae_rule_parse_item_vlan() has already made sure
292          * that pdata->nb_vlan_tags does not exceed this figure.
293          */
294         RTE_BUILD_BUG_ON(SFC_MAE_MATCH_VLAN_MAX_NTAGS != 2);
295
296         for (i = 0; i < pdata->nb_vlan_tags; ++i) {
297                 et = &pdata->ethertypes[i];
298
299                 rc = efx_mae_match_spec_field_set(efx_spec, field_ids[i],
300                                                   sizeof(et->value),
301                                                   (const uint8_t *)&et->value,
302                                                   sizeof(et->mask),
303                                                   (const uint8_t *)&et->mask);
304                 if (rc != 0)
305                         return rc;
306         }
307
308         return 0;
309 }
310
311 static int
312 sfc_mae_rule_process_pattern_data(struct sfc_mae_parse_ctx *ctx,
313                                   struct rte_flow_error *error)
314 {
315         struct sfc_mae_pattern_data *pdata = &ctx->pattern_data;
316         struct sfc_mae_ethertype *ethertypes = pdata->ethertypes;
317         const rte_be16_t supported_tpids[] = {
318                 /* VLAN standard TPID (always the first element) */
319                 RTE_BE16(RTE_ETHER_TYPE_VLAN),
320
321                 /* Double-tagging TPIDs */
322                 RTE_BE16(RTE_ETHER_TYPE_QINQ),
323                 RTE_BE16(RTE_ETHER_TYPE_QINQ1),
324                 RTE_BE16(RTE_ETHER_TYPE_QINQ2),
325                 RTE_BE16(RTE_ETHER_TYPE_QINQ3),
326         };
327         unsigned int nb_supported_tpids = RTE_DIM(supported_tpids);
328         unsigned int ethertype_idx;
329         int rc;
330
331         /*
332          * sfc_mae_rule_parse_item_vlan() has already made sure
333          * that pdata->nb_vlan_tags does not exceed this figure.
334          */
335         RTE_BUILD_BUG_ON(SFC_MAE_MATCH_VLAN_MAX_NTAGS != 2);
336
337         for (ethertype_idx = 0;
338              ethertype_idx < pdata->nb_vlan_tags; ++ethertype_idx) {
339                 unsigned int tpid_idx;
340
341                 /* Exact match is supported only. */
342                 if (ethertypes[ethertype_idx].mask != RTE_BE16(0xffff)) {
343                         rc = EINVAL;
344                         goto fail;
345                 }
346
347                 for (tpid_idx = pdata->nb_vlan_tags - ethertype_idx - 1;
348                      tpid_idx < nb_supported_tpids; ++tpid_idx) {
349                         if (ethertypes[ethertype_idx].value ==
350                             supported_tpids[tpid_idx])
351                                 break;
352                 }
353
354                 if (tpid_idx == nb_supported_tpids) {
355                         rc = EINVAL;
356                         goto fail;
357                 }
358
359                 nb_supported_tpids = 1;
360         }
361
362         /*
363          * Now, when the number of VLAN tags is known, set fields
364          * ETHER_TYPE, VLAN0_PROTO and VLAN1_PROTO so that the first
365          * one is either a valid L3 EtherType (or 0x0000/0x0000),
366          * and the last two are valid TPIDs (or 0x0000/0x0000).
367          */
368         rc = sfc_mae_set_ethertypes(ctx);
369         if (rc != 0)
370                 goto fail;
371
372         return 0;
373
374 fail:
375         return rte_flow_error_set(error, rc, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
376                                   "Failed to process pattern data");
377 }
378
379 static int
380 sfc_mae_rule_parse_item_port_id(const struct rte_flow_item *item,
381                                 struct sfc_flow_parse_ctx *ctx,
382                                 struct rte_flow_error *error)
383 {
384         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
385         const struct rte_flow_item_port_id supp_mask = {
386                 .id = 0xffffffff,
387         };
388         const void *def_mask = &rte_flow_item_port_id_mask;
389         const struct rte_flow_item_port_id *spec = NULL;
390         const struct rte_flow_item_port_id *mask = NULL;
391         efx_mport_sel_t mport_sel;
392         int rc;
393
394         if (ctx_mae->match_mport_set) {
395                 return rte_flow_error_set(error, ENOTSUP,
396                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
397                                 "Can't handle multiple traffic source items");
398         }
399
400         rc = sfc_flow_parse_init(item,
401                                  (const void **)&spec, (const void **)&mask,
402                                  (const void *)&supp_mask, def_mask,
403                                  sizeof(struct rte_flow_item_port_id), error);
404         if (rc != 0)
405                 return rc;
406
407         if (mask->id != supp_mask.id) {
408                 return rte_flow_error_set(error, EINVAL,
409                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
410                                 "Bad mask in the PORT_ID pattern item");
411         }
412
413         /* If "spec" is not set, could be any port ID */
414         if (spec == NULL)
415                 return 0;
416
417         if (spec->id > UINT16_MAX) {
418                 return rte_flow_error_set(error, EOVERFLOW,
419                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
420                                           "The port ID is too large");
421         }
422
423         rc = sfc_mae_switch_port_by_ethdev(ctx_mae->sa->mae.switch_domain_id,
424                                            spec->id, &mport_sel);
425         if (rc != 0) {
426                 return rte_flow_error_set(error, rc,
427                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
428                                 "Can't find RTE ethdev by the port ID");
429         }
430
431         rc = efx_mae_match_spec_mport_set(ctx_mae->match_spec_action,
432                                           &mport_sel, NULL);
433         if (rc != 0) {
434                 return rte_flow_error_set(error, rc,
435                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
436                                 "Failed to set MPORT for the port ID");
437         }
438
439         ctx_mae->match_mport_set = B_TRUE;
440
441         return 0;
442 }
443
444 static int
445 sfc_mae_rule_parse_item_phy_port(const struct rte_flow_item *item,
446                                  struct sfc_flow_parse_ctx *ctx,
447                                  struct rte_flow_error *error)
448 {
449         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
450         const struct rte_flow_item_phy_port supp_mask = {
451                 .index = 0xffffffff,
452         };
453         const void *def_mask = &rte_flow_item_phy_port_mask;
454         const struct rte_flow_item_phy_port *spec = NULL;
455         const struct rte_flow_item_phy_port *mask = NULL;
456         efx_mport_sel_t mport_v;
457         int rc;
458
459         if (ctx_mae->match_mport_set) {
460                 return rte_flow_error_set(error, ENOTSUP,
461                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
462                                 "Can't handle multiple traffic source items");
463         }
464
465         rc = sfc_flow_parse_init(item,
466                                  (const void **)&spec, (const void **)&mask,
467                                  (const void *)&supp_mask, def_mask,
468                                  sizeof(struct rte_flow_item_phy_port), error);
469         if (rc != 0)
470                 return rc;
471
472         if (mask->index != supp_mask.index) {
473                 return rte_flow_error_set(error, EINVAL,
474                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
475                                 "Bad mask in the PHY_PORT pattern item");
476         }
477
478         /* If "spec" is not set, could be any physical port */
479         if (spec == NULL)
480                 return 0;
481
482         rc = efx_mae_mport_by_phy_port(spec->index, &mport_v);
483         if (rc != 0) {
484                 return rte_flow_error_set(error, rc,
485                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
486                                 "Failed to convert the PHY_PORT index");
487         }
488
489         rc = efx_mae_match_spec_mport_set(ctx_mae->match_spec_action,
490                                           &mport_v, NULL);
491         if (rc != 0) {
492                 return rte_flow_error_set(error, rc,
493                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
494                                 "Failed to set MPORT for the PHY_PORT");
495         }
496
497         ctx_mae->match_mport_set = B_TRUE;
498
499         return 0;
500 }
501
502 static int
503 sfc_mae_rule_parse_item_pf(const struct rte_flow_item *item,
504                            struct sfc_flow_parse_ctx *ctx,
505                            struct rte_flow_error *error)
506 {
507         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
508         const efx_nic_cfg_t *encp = efx_nic_cfg_get(ctx_mae->sa->nic);
509         efx_mport_sel_t mport_v;
510         int rc;
511
512         if (ctx_mae->match_mport_set) {
513                 return rte_flow_error_set(error, ENOTSUP,
514                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
515                                 "Can't handle multiple traffic source items");
516         }
517
518         rc = efx_mae_mport_by_pcie_function(encp->enc_pf, EFX_PCI_VF_INVALID,
519                                             &mport_v);
520         if (rc != 0) {
521                 return rte_flow_error_set(error, rc,
522                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
523                                 "Failed to convert the PF ID");
524         }
525
526         rc = efx_mae_match_spec_mport_set(ctx_mae->match_spec_action,
527                                           &mport_v, NULL);
528         if (rc != 0) {
529                 return rte_flow_error_set(error, rc,
530                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
531                                 "Failed to set MPORT for the PF");
532         }
533
534         ctx_mae->match_mport_set = B_TRUE;
535
536         return 0;
537 }
538
539 static int
540 sfc_mae_rule_parse_item_vf(const struct rte_flow_item *item,
541                            struct sfc_flow_parse_ctx *ctx,
542                            struct rte_flow_error *error)
543 {
544         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
545         const efx_nic_cfg_t *encp = efx_nic_cfg_get(ctx_mae->sa->nic);
546         const struct rte_flow_item_vf supp_mask = {
547                 .id = 0xffffffff,
548         };
549         const void *def_mask = &rte_flow_item_vf_mask;
550         const struct rte_flow_item_vf *spec = NULL;
551         const struct rte_flow_item_vf *mask = NULL;
552         efx_mport_sel_t mport_v;
553         int rc;
554
555         if (ctx_mae->match_mport_set) {
556                 return rte_flow_error_set(error, ENOTSUP,
557                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
558                                 "Can't handle multiple traffic source items");
559         }
560
561         rc = sfc_flow_parse_init(item,
562                                  (const void **)&spec, (const void **)&mask,
563                                  (const void *)&supp_mask, def_mask,
564                                  sizeof(struct rte_flow_item_vf), error);
565         if (rc != 0)
566                 return rc;
567
568         if (mask->id != supp_mask.id) {
569                 return rte_flow_error_set(error, EINVAL,
570                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
571                                 "Bad mask in the VF pattern item");
572         }
573
574         /*
575          * If "spec" is not set, the item requests any VF related to the
576          * PF of the current DPDK port (but not the PF itself).
577          * Reject this match criterion as unsupported.
578          */
579         if (spec == NULL) {
580                 return rte_flow_error_set(error, EINVAL,
581                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
582                                 "Bad spec in the VF pattern item");
583         }
584
585         rc = efx_mae_mport_by_pcie_function(encp->enc_pf, spec->id, &mport_v);
586         if (rc != 0) {
587                 return rte_flow_error_set(error, rc,
588                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
589                                 "Failed to convert the PF + VF IDs");
590         }
591
592         rc = efx_mae_match_spec_mport_set(ctx_mae->match_spec_action,
593                                           &mport_v, NULL);
594         if (rc != 0) {
595                 return rte_flow_error_set(error, rc,
596                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
597                                 "Failed to set MPORT for the PF + VF");
598         }
599
600         ctx_mae->match_mport_set = B_TRUE;
601
602         return 0;
603 }
604
605 /*
606  * Having this field ID in a field locator means that this
607  * locator cannot be used to actually set the field at the
608  * time when the corresponding item gets encountered. Such
609  * fields get stashed in the parsing context instead. This
610  * is required to resolve dependencies between the stashed
611  * fields. See sfc_mae_rule_process_pattern_data().
612  */
613 #define SFC_MAE_FIELD_HANDLING_DEFERRED EFX_MAE_FIELD_NIDS
614
615 struct sfc_mae_field_locator {
616         efx_mae_field_id_t              field_id;
617         size_t                          size;
618         /* Field offset in the corresponding rte_flow_item_ struct */
619         size_t                          ofst;
620 };
621
622 static void
623 sfc_mae_item_build_supp_mask(const struct sfc_mae_field_locator *field_locators,
624                              unsigned int nb_field_locators, void *mask_ptr,
625                              size_t mask_size)
626 {
627         unsigned int i;
628
629         memset(mask_ptr, 0, mask_size);
630
631         for (i = 0; i < nb_field_locators; ++i) {
632                 const struct sfc_mae_field_locator *fl = &field_locators[i];
633
634                 SFC_ASSERT(fl->ofst + fl->size <= mask_size);
635                 memset(RTE_PTR_ADD(mask_ptr, fl->ofst), 0xff, fl->size);
636         }
637 }
638
639 static int
640 sfc_mae_parse_item(const struct sfc_mae_field_locator *field_locators,
641                    unsigned int nb_field_locators, const uint8_t *spec,
642                    const uint8_t *mask, efx_mae_match_spec_t *efx_spec,
643                    struct rte_flow_error *error)
644 {
645         unsigned int i;
646         int rc = 0;
647
648         for (i = 0; i < nb_field_locators; ++i) {
649                 const struct sfc_mae_field_locator *fl = &field_locators[i];
650
651                 if (fl->field_id == SFC_MAE_FIELD_HANDLING_DEFERRED)
652                         continue;
653
654                 rc = efx_mae_match_spec_field_set(efx_spec, fl->field_id,
655                                                   fl->size, spec + fl->ofst,
656                                                   fl->size, mask + fl->ofst);
657                 if (rc != 0)
658                         break;
659         }
660
661         if (rc != 0) {
662                 rc = rte_flow_error_set(error, rc, RTE_FLOW_ERROR_TYPE_ITEM,
663                                 NULL, "Failed to process item fields");
664         }
665
666         return rc;
667 }
668
669 static const struct sfc_mae_field_locator flocs_eth[] = {
670         {
671                 /*
672                  * This locator is used only for building supported fields mask.
673                  * The field is handled by sfc_mae_rule_process_pattern_data().
674                  */
675                 SFC_MAE_FIELD_HANDLING_DEFERRED,
676                 RTE_SIZEOF_FIELD(struct rte_flow_item_eth, type),
677                 offsetof(struct rte_flow_item_eth, type),
678         },
679         {
680                 EFX_MAE_FIELD_ETH_DADDR_BE,
681                 RTE_SIZEOF_FIELD(struct rte_flow_item_eth, dst),
682                 offsetof(struct rte_flow_item_eth, dst),
683         },
684         {
685                 EFX_MAE_FIELD_ETH_SADDR_BE,
686                 RTE_SIZEOF_FIELD(struct rte_flow_item_eth, src),
687                 offsetof(struct rte_flow_item_eth, src),
688         },
689 };
690
691 static int
692 sfc_mae_rule_parse_item_eth(const struct rte_flow_item *item,
693                             struct sfc_flow_parse_ctx *ctx,
694                             struct rte_flow_error *error)
695 {
696         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
697         struct rte_flow_item_eth supp_mask;
698         const uint8_t *spec = NULL;
699         const uint8_t *mask = NULL;
700         int rc;
701
702         sfc_mae_item_build_supp_mask(flocs_eth, RTE_DIM(flocs_eth),
703                                      &supp_mask, sizeof(supp_mask));
704
705         rc = sfc_flow_parse_init(item,
706                                  (const void **)&spec, (const void **)&mask,
707                                  (const void *)&supp_mask,
708                                  &rte_flow_item_eth_mask,
709                                  sizeof(struct rte_flow_item_eth), error);
710         if (rc != 0)
711                 return rc;
712
713         if (spec != NULL) {
714                 struct sfc_mae_pattern_data *pdata = &ctx_mae->pattern_data;
715                 struct sfc_mae_ethertype *ethertypes = pdata->ethertypes;
716                 const struct rte_flow_item_eth *item_spec;
717                 const struct rte_flow_item_eth *item_mask;
718
719                 item_spec = (const struct rte_flow_item_eth *)spec;
720                 item_mask = (const struct rte_flow_item_eth *)mask;
721
722                 ethertypes[0].value = item_spec->type;
723                 ethertypes[0].mask = item_mask->type;
724         } else {
725                 /*
726                  * The specification is empty. This is wrong in the case
727                  * when there are more network patterns in line. Other
728                  * than that, any Ethernet can match. All of that is
729                  * checked at the end of parsing.
730                  */
731                 return 0;
732         }
733
734         return sfc_mae_parse_item(flocs_eth, RTE_DIM(flocs_eth), spec, mask,
735                                   ctx_mae->match_spec_action, error);
736 }
737
738 static const struct sfc_mae_field_locator flocs_vlan[] = {
739         /* Outermost tag */
740         {
741                 EFX_MAE_FIELD_VLAN0_TCI_BE,
742                 RTE_SIZEOF_FIELD(struct rte_flow_item_vlan, tci),
743                 offsetof(struct rte_flow_item_vlan, tci),
744         },
745         {
746                 /*
747                  * This locator is used only for building supported fields mask.
748                  * The field is handled by sfc_mae_rule_process_pattern_data().
749                  */
750                 SFC_MAE_FIELD_HANDLING_DEFERRED,
751                 RTE_SIZEOF_FIELD(struct rte_flow_item_vlan, inner_type),
752                 offsetof(struct rte_flow_item_vlan, inner_type),
753         },
754
755         /* Innermost tag */
756         {
757                 EFX_MAE_FIELD_VLAN1_TCI_BE,
758                 RTE_SIZEOF_FIELD(struct rte_flow_item_vlan, tci),
759                 offsetof(struct rte_flow_item_vlan, tci),
760         },
761         {
762                 /*
763                  * This locator is used only for building supported fields mask.
764                  * The field is handled by sfc_mae_rule_process_pattern_data().
765                  */
766                 SFC_MAE_FIELD_HANDLING_DEFERRED,
767                 RTE_SIZEOF_FIELD(struct rte_flow_item_vlan, inner_type),
768                 offsetof(struct rte_flow_item_vlan, inner_type),
769         },
770 };
771
772 static int
773 sfc_mae_rule_parse_item_vlan(const struct rte_flow_item *item,
774                              struct sfc_flow_parse_ctx *ctx,
775                              struct rte_flow_error *error)
776 {
777         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
778         struct sfc_mae_pattern_data *pdata = &ctx_mae->pattern_data;
779         const struct sfc_mae_field_locator *flocs;
780         struct rte_flow_item_vlan supp_mask;
781         const uint8_t *spec = NULL;
782         const uint8_t *mask = NULL;
783         unsigned int nb_flocs;
784         int rc;
785
786         RTE_BUILD_BUG_ON(SFC_MAE_MATCH_VLAN_MAX_NTAGS != 2);
787
788         if (pdata->nb_vlan_tags == SFC_MAE_MATCH_VLAN_MAX_NTAGS) {
789                 return rte_flow_error_set(error, ENOTSUP,
790                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
791                                 "Can't match that many VLAN tags");
792         }
793
794         nb_flocs = RTE_DIM(flocs_vlan) / SFC_MAE_MATCH_VLAN_MAX_NTAGS;
795         flocs = flocs_vlan + pdata->nb_vlan_tags * nb_flocs;
796
797         /* If parsing fails, this can remain incremented. */
798         ++pdata->nb_vlan_tags;
799
800         sfc_mae_item_build_supp_mask(flocs, nb_flocs,
801                                      &supp_mask, sizeof(supp_mask));
802
803         rc = sfc_flow_parse_init(item,
804                                  (const void **)&spec, (const void **)&mask,
805                                  (const void *)&supp_mask,
806                                  &rte_flow_item_vlan_mask,
807                                  sizeof(struct rte_flow_item_vlan), error);
808         if (rc != 0)
809                 return rc;
810
811         if (spec != NULL) {
812                 struct sfc_mae_ethertype *ethertypes = pdata->ethertypes;
813                 const struct rte_flow_item_vlan *item_spec;
814                 const struct rte_flow_item_vlan *item_mask;
815
816                 item_spec = (const struct rte_flow_item_vlan *)spec;
817                 item_mask = (const struct rte_flow_item_vlan *)mask;
818
819                 ethertypes[pdata->nb_vlan_tags].value = item_spec->inner_type;
820                 ethertypes[pdata->nb_vlan_tags].mask = item_mask->inner_type;
821         } else {
822                 /*
823                  * The specification is empty. This is wrong in the case
824                  * when there are more network patterns in line. Other
825                  * than that, any Ethernet can match. All of that is
826                  * checked at the end of parsing.
827                  */
828                 return 0;
829         }
830
831         return sfc_mae_parse_item(flocs, nb_flocs, spec, mask,
832                                   ctx_mae->match_spec_action, error);
833 }
834
835 static const struct sfc_flow_item sfc_flow_items[] = {
836         {
837                 .type = RTE_FLOW_ITEM_TYPE_PORT_ID,
838                 /*
839                  * In terms of RTE flow, this item is a META one,
840                  * and its position in the pattern is don't care.
841                  */
842                 .prev_layer = SFC_FLOW_ITEM_ANY_LAYER,
843                 .layer = SFC_FLOW_ITEM_ANY_LAYER,
844                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
845                 .parse = sfc_mae_rule_parse_item_port_id,
846         },
847         {
848                 .type = RTE_FLOW_ITEM_TYPE_PHY_PORT,
849                 /*
850                  * In terms of RTE flow, this item is a META one,
851                  * and its position in the pattern is don't care.
852                  */
853                 .prev_layer = SFC_FLOW_ITEM_ANY_LAYER,
854                 .layer = SFC_FLOW_ITEM_ANY_LAYER,
855                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
856                 .parse = sfc_mae_rule_parse_item_phy_port,
857         },
858         {
859                 .type = RTE_FLOW_ITEM_TYPE_PF,
860                 /*
861                  * In terms of RTE flow, this item is a META one,
862                  * and its position in the pattern is don't care.
863                  */
864                 .prev_layer = SFC_FLOW_ITEM_ANY_LAYER,
865                 .layer = SFC_FLOW_ITEM_ANY_LAYER,
866                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
867                 .parse = sfc_mae_rule_parse_item_pf,
868         },
869         {
870                 .type = RTE_FLOW_ITEM_TYPE_VF,
871                 /*
872                  * In terms of RTE flow, this item is a META one,
873                  * and its position in the pattern is don't care.
874                  */
875                 .prev_layer = SFC_FLOW_ITEM_ANY_LAYER,
876                 .layer = SFC_FLOW_ITEM_ANY_LAYER,
877                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
878                 .parse = sfc_mae_rule_parse_item_vf,
879         },
880         {
881                 .type = RTE_FLOW_ITEM_TYPE_ETH,
882                 .prev_layer = SFC_FLOW_ITEM_START_LAYER,
883                 .layer = SFC_FLOW_ITEM_L2,
884                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
885                 .parse = sfc_mae_rule_parse_item_eth,
886         },
887         {
888                 .type = RTE_FLOW_ITEM_TYPE_VLAN,
889                 .prev_layer = SFC_FLOW_ITEM_L2,
890                 .layer = SFC_FLOW_ITEM_L2,
891                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
892                 .parse = sfc_mae_rule_parse_item_vlan,
893         },
894 };
895
896 int
897 sfc_mae_rule_parse_pattern(struct sfc_adapter *sa,
898                            const struct rte_flow_item pattern[],
899                            struct sfc_flow_spec_mae *spec,
900                            struct rte_flow_error *error)
901 {
902         struct sfc_mae_parse_ctx ctx_mae;
903         struct sfc_flow_parse_ctx ctx;
904         int rc;
905
906         memset(&ctx_mae, 0, sizeof(ctx_mae));
907         ctx_mae.sa = sa;
908
909         rc = efx_mae_match_spec_init(sa->nic, EFX_MAE_RULE_ACTION,
910                                      spec->priority,
911                                      &ctx_mae.match_spec_action);
912         if (rc != 0) {
913                 rc = rte_flow_error_set(error, rc,
914                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
915                         "Failed to initialise action rule match specification");
916                 goto fail_init_match_spec_action;
917         }
918
919         ctx.type = SFC_FLOW_PARSE_CTX_MAE;
920         ctx.mae = &ctx_mae;
921
922         rc = sfc_flow_parse_pattern(sfc_flow_items, RTE_DIM(sfc_flow_items),
923                                     pattern, &ctx, error);
924         if (rc != 0)
925                 goto fail_parse_pattern;
926
927         rc = sfc_mae_rule_process_pattern_data(&ctx_mae, error);
928         if (rc != 0)
929                 goto fail_process_pattern_data;
930
931         if (!efx_mae_match_spec_is_valid(sa->nic, ctx_mae.match_spec_action)) {
932                 rc = rte_flow_error_set(error, ENOTSUP,
933                                         RTE_FLOW_ERROR_TYPE_ITEM, NULL,
934                                         "Inconsistent pattern");
935                 goto fail_validate_match_spec_action;
936         }
937
938         spec->match_spec = ctx_mae.match_spec_action;
939
940         return 0;
941
942 fail_validate_match_spec_action:
943 fail_process_pattern_data:
944 fail_parse_pattern:
945         efx_mae_match_spec_fini(sa->nic, ctx_mae.match_spec_action);
946
947 fail_init_match_spec_action:
948         return rc;
949 }
950
951 /*
952  * An action supported by MAE may correspond to a bundle of RTE flow actions,
953  * in example, VLAN_PUSH = OF_PUSH_VLAN + OF_VLAN_SET_VID + OF_VLAN_SET_PCP.
954  * That is, related RTE flow actions need to be tracked as parts of a whole
955  * so that they can be combined into a single action and submitted to MAE
956  * representation of a given rule's action set.
957  *
958  * Each RTE flow action provided by an application gets classified as
959  * one belonging to some bundle type. If an action is not supposed to
960  * belong to any bundle, or if this action is END, it is described as
961  * one belonging to a dummy bundle of type EMPTY.
962  *
963  * A currently tracked bundle will be submitted if a repeating
964  * action or an action of different bundle type follows.
965  */
966
967 enum sfc_mae_actions_bundle_type {
968         SFC_MAE_ACTIONS_BUNDLE_EMPTY = 0,
969         SFC_MAE_ACTIONS_BUNDLE_VLAN_PUSH,
970 };
971
972 struct sfc_mae_actions_bundle {
973         enum sfc_mae_actions_bundle_type        type;
974
975         /* Indicates actions already tracked by the current bundle */
976         uint64_t                                actions_mask;
977
978         /* Parameters used by SFC_MAE_ACTIONS_BUNDLE_VLAN_PUSH */
979         rte_be16_t                              vlan_push_tpid;
980         rte_be16_t                              vlan_push_tci;
981 };
982
983 /*
984  * Combine configuration of RTE flow actions tracked by the bundle into a
985  * single action and submit the result to MAE action set specification.
986  * Do nothing in the case of dummy action bundle.
987  */
988 static int
989 sfc_mae_actions_bundle_submit(const struct sfc_mae_actions_bundle *bundle,
990                               efx_mae_actions_t *spec)
991 {
992         int rc = 0;
993
994         switch (bundle->type) {
995         case SFC_MAE_ACTIONS_BUNDLE_EMPTY:
996                 break;
997         case SFC_MAE_ACTIONS_BUNDLE_VLAN_PUSH:
998                 rc = efx_mae_action_set_populate_vlan_push(
999                         spec, bundle->vlan_push_tpid, bundle->vlan_push_tci);
1000                 break;
1001         default:
1002                 SFC_ASSERT(B_FALSE);
1003                 break;
1004         }
1005
1006         return rc;
1007 }
1008
1009 /*
1010  * Given the type of the next RTE flow action in the line, decide
1011  * whether a new bundle is about to start, and, if this is the case,
1012  * submit and reset the current bundle.
1013  */
1014 static int
1015 sfc_mae_actions_bundle_sync(const struct rte_flow_action *action,
1016                             struct sfc_mae_actions_bundle *bundle,
1017                             efx_mae_actions_t *spec,
1018                             struct rte_flow_error *error)
1019 {
1020         enum sfc_mae_actions_bundle_type bundle_type_new;
1021         int rc;
1022
1023         switch (action->type) {
1024         case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
1025         case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
1026         case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
1027                 bundle_type_new = SFC_MAE_ACTIONS_BUNDLE_VLAN_PUSH;
1028                 break;
1029         default:
1030                 /*
1031                  * Self-sufficient actions, including END, are handled in this
1032                  * case. No checks for unsupported actions are needed here
1033                  * because parsing doesn't occur at this point.
1034                  */
1035                 bundle_type_new = SFC_MAE_ACTIONS_BUNDLE_EMPTY;
1036                 break;
1037         }
1038
1039         if (bundle_type_new != bundle->type ||
1040             (bundle->actions_mask & (1ULL << action->type)) != 0) {
1041                 rc = sfc_mae_actions_bundle_submit(bundle, spec);
1042                 if (rc != 0)
1043                         goto fail_submit;
1044
1045                 memset(bundle, 0, sizeof(*bundle));
1046         }
1047
1048         bundle->type = bundle_type_new;
1049
1050         return 0;
1051
1052 fail_submit:
1053         return rte_flow_error_set(error, rc,
1054                         RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1055                         "Failed to request the (group of) action(s)");
1056 }
1057
1058 static void
1059 sfc_mae_rule_parse_action_of_push_vlan(
1060                             const struct rte_flow_action_of_push_vlan *conf,
1061                             struct sfc_mae_actions_bundle *bundle)
1062 {
1063         bundle->vlan_push_tpid = conf->ethertype;
1064 }
1065
1066 static void
1067 sfc_mae_rule_parse_action_of_set_vlan_vid(
1068                             const struct rte_flow_action_of_set_vlan_vid *conf,
1069                             struct sfc_mae_actions_bundle *bundle)
1070 {
1071         bundle->vlan_push_tci |= (conf->vlan_vid &
1072                                   rte_cpu_to_be_16(RTE_LEN2MASK(12, uint16_t)));
1073 }
1074
1075 static void
1076 sfc_mae_rule_parse_action_of_set_vlan_pcp(
1077                             const struct rte_flow_action_of_set_vlan_pcp *conf,
1078                             struct sfc_mae_actions_bundle *bundle)
1079 {
1080         uint16_t vlan_tci_pcp = (uint16_t)(conf->vlan_pcp &
1081                                            RTE_LEN2MASK(3, uint8_t)) << 13;
1082
1083         bundle->vlan_push_tci |= rte_cpu_to_be_16(vlan_tci_pcp);
1084 }
1085
1086 static int
1087 sfc_mae_rule_parse_action_mark(const struct rte_flow_action_mark *conf,
1088                                efx_mae_actions_t *spec)
1089 {
1090         return efx_mae_action_set_populate_mark(spec, conf->id);
1091 }
1092
1093 static int
1094 sfc_mae_rule_parse_action_phy_port(struct sfc_adapter *sa,
1095                                    const struct rte_flow_action_phy_port *conf,
1096                                    efx_mae_actions_t *spec)
1097 {
1098         efx_mport_sel_t mport;
1099         uint32_t phy_port;
1100         int rc;
1101
1102         if (conf->original != 0)
1103                 phy_port = efx_nic_cfg_get(sa->nic)->enc_assigned_port;
1104         else
1105                 phy_port = conf->index;
1106
1107         rc = efx_mae_mport_by_phy_port(phy_port, &mport);
1108         if (rc != 0)
1109                 return rc;
1110
1111         return efx_mae_action_set_populate_deliver(spec, &mport);
1112 }
1113
1114 static int
1115 sfc_mae_rule_parse_action_pf_vf(struct sfc_adapter *sa,
1116                                 const struct rte_flow_action_vf *vf_conf,
1117                                 efx_mae_actions_t *spec)
1118 {
1119         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
1120         efx_mport_sel_t mport;
1121         uint32_t vf;
1122         int rc;
1123
1124         if (vf_conf == NULL)
1125                 vf = EFX_PCI_VF_INVALID;
1126         else if (vf_conf->original != 0)
1127                 vf = encp->enc_vf;
1128         else
1129                 vf = vf_conf->id;
1130
1131         rc = efx_mae_mport_by_pcie_function(encp->enc_pf, vf, &mport);
1132         if (rc != 0)
1133                 return rc;
1134
1135         return efx_mae_action_set_populate_deliver(spec, &mport);
1136 }
1137
1138 static int
1139 sfc_mae_rule_parse_action_port_id(struct sfc_adapter *sa,
1140                                   const struct rte_flow_action_port_id *conf,
1141                                   efx_mae_actions_t *spec)
1142 {
1143         struct sfc_adapter_shared * const sas = sfc_sa2shared(sa);
1144         struct sfc_mae *mae = &sa->mae;
1145         efx_mport_sel_t mport;
1146         uint16_t port_id;
1147         int rc;
1148
1149         port_id = (conf->original != 0) ? sas->port_id : conf->id;
1150
1151         rc = sfc_mae_switch_port_by_ethdev(mae->switch_domain_id,
1152                                            port_id, &mport);
1153         if (rc != 0)
1154                 return rc;
1155
1156         return efx_mae_action_set_populate_deliver(spec, &mport);
1157 }
1158
1159 static int
1160 sfc_mae_rule_parse_action(struct sfc_adapter *sa,
1161                           const struct rte_flow_action *action,
1162                           struct sfc_mae_actions_bundle *bundle,
1163                           efx_mae_actions_t *spec,
1164                           struct rte_flow_error *error)
1165 {
1166         int rc = 0;
1167
1168         switch (action->type) {
1169         case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
1170                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_OF_POP_VLAN,
1171                                        bundle->actions_mask);
1172                 rc = efx_mae_action_set_populate_vlan_pop(spec);
1173                 break;
1174         case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
1175                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN,
1176                                        bundle->actions_mask);
1177                 sfc_mae_rule_parse_action_of_push_vlan(action->conf, bundle);
1178                 break;
1179         case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
1180                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID,
1181                                        bundle->actions_mask);
1182                 sfc_mae_rule_parse_action_of_set_vlan_vid(action->conf, bundle);
1183                 break;
1184         case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
1185                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP,
1186                                        bundle->actions_mask);
1187                 sfc_mae_rule_parse_action_of_set_vlan_pcp(action->conf, bundle);
1188                 break;
1189         case RTE_FLOW_ACTION_TYPE_FLAG:
1190                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_FLAG,
1191                                        bundle->actions_mask);
1192                 rc = efx_mae_action_set_populate_flag(spec);
1193                 break;
1194         case RTE_FLOW_ACTION_TYPE_MARK:
1195                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_MARK,
1196                                        bundle->actions_mask);
1197                 rc = sfc_mae_rule_parse_action_mark(action->conf, spec);
1198                 break;
1199         case RTE_FLOW_ACTION_TYPE_PHY_PORT:
1200                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_PHY_PORT,
1201                                        bundle->actions_mask);
1202                 rc = sfc_mae_rule_parse_action_phy_port(sa, action->conf, spec);
1203                 break;
1204         case RTE_FLOW_ACTION_TYPE_PF:
1205                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_PF,
1206                                        bundle->actions_mask);
1207                 rc = sfc_mae_rule_parse_action_pf_vf(sa, NULL, spec);
1208                 break;
1209         case RTE_FLOW_ACTION_TYPE_VF:
1210                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_VF,
1211                                        bundle->actions_mask);
1212                 rc = sfc_mae_rule_parse_action_pf_vf(sa, action->conf, spec);
1213                 break;
1214         case RTE_FLOW_ACTION_TYPE_PORT_ID:
1215                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_PORT_ID,
1216                                        bundle->actions_mask);
1217                 rc = sfc_mae_rule_parse_action_port_id(sa, action->conf, spec);
1218                 break;
1219         case RTE_FLOW_ACTION_TYPE_DROP:
1220                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_DROP,
1221                                        bundle->actions_mask);
1222                 rc = efx_mae_action_set_populate_drop(spec);
1223                 break;
1224         default:
1225                 return rte_flow_error_set(error, ENOTSUP,
1226                                 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1227                                 "Unsupported action");
1228         }
1229
1230         if (rc != 0) {
1231                 rc = rte_flow_error_set(error, rc, RTE_FLOW_ERROR_TYPE_ACTION,
1232                                 NULL, "Failed to request the action");
1233         } else {
1234                 bundle->actions_mask |= (1ULL << action->type);
1235         }
1236
1237         return rc;
1238 }
1239
1240 int
1241 sfc_mae_rule_parse_actions(struct sfc_adapter *sa,
1242                            const struct rte_flow_action actions[],
1243                            struct sfc_mae_action_set **action_setp,
1244                            struct rte_flow_error *error)
1245 {
1246         struct sfc_mae_actions_bundle bundle = {0};
1247         const struct rte_flow_action *action;
1248         efx_mae_actions_t *spec;
1249         int rc;
1250
1251         if (actions == NULL) {
1252                 return rte_flow_error_set(error, EINVAL,
1253                                 RTE_FLOW_ERROR_TYPE_ACTION_NUM, NULL,
1254                                 "NULL actions");
1255         }
1256
1257         rc = efx_mae_action_set_spec_init(sa->nic, &spec);
1258         if (rc != 0)
1259                 goto fail_action_set_spec_init;
1260
1261         for (action = actions;
1262              action->type != RTE_FLOW_ACTION_TYPE_END; ++action) {
1263                 rc = sfc_mae_actions_bundle_sync(action, &bundle, spec, error);
1264                 if (rc != 0)
1265                         goto fail_rule_parse_action;
1266
1267                 rc = sfc_mae_rule_parse_action(sa, action, &bundle, spec,
1268                                                error);
1269                 if (rc != 0)
1270                         goto fail_rule_parse_action;
1271         }
1272
1273         rc = sfc_mae_actions_bundle_sync(action, &bundle, spec, error);
1274         if (rc != 0)
1275                 goto fail_rule_parse_action;
1276
1277         *action_setp = sfc_mae_action_set_attach(sa, spec);
1278         if (*action_setp != NULL) {
1279                 efx_mae_action_set_spec_fini(sa->nic, spec);
1280                 return 0;
1281         }
1282
1283         rc = sfc_mae_action_set_add(sa, spec, action_setp);
1284         if (rc != 0)
1285                 goto fail_action_set_add;
1286
1287         return 0;
1288
1289 fail_action_set_add:
1290 fail_rule_parse_action:
1291         efx_mae_action_set_spec_fini(sa->nic, spec);
1292
1293 fail_action_set_spec_init:
1294         if (rc > 0) {
1295                 rc = rte_flow_error_set(error, rc,
1296                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1297                         NULL, "Failed to process the action");
1298         }
1299         return rc;
1300 }
1301
1302 static bool
1303 sfc_mae_rules_class_cmp(struct sfc_adapter *sa,
1304                         const efx_mae_match_spec_t *left,
1305                         const efx_mae_match_spec_t *right)
1306 {
1307         bool have_same_class;
1308         int rc;
1309
1310         rc = efx_mae_match_specs_class_cmp(sa->nic, left, right,
1311                                            &have_same_class);
1312
1313         return (rc == 0) ? have_same_class : false;
1314 }
1315
1316 static int
1317 sfc_mae_action_rule_class_verify(struct sfc_adapter *sa,
1318                                  struct sfc_flow_spec_mae *spec)
1319 {
1320         const struct rte_flow *entry;
1321
1322         TAILQ_FOREACH_REVERSE(entry, &sa->flow_list, sfc_flow_list, entries) {
1323                 const struct sfc_flow_spec *entry_spec = &entry->spec;
1324                 const struct sfc_flow_spec_mae *es_mae = &entry_spec->mae;
1325                 const efx_mae_match_spec_t *left = es_mae->match_spec;
1326                 const efx_mae_match_spec_t *right = spec->match_spec;
1327
1328                 switch (entry_spec->type) {
1329                 case SFC_FLOW_SPEC_FILTER:
1330                         /* Ignore VNIC-level flows */
1331                         break;
1332                 case SFC_FLOW_SPEC_MAE:
1333                         if (sfc_mae_rules_class_cmp(sa, left, right))
1334                                 return 0;
1335                         break;
1336                 default:
1337                         SFC_ASSERT(false);
1338                 }
1339         }
1340
1341         sfc_info(sa, "for now, the HW doesn't support rule validation, and HW "
1342                  "support for inner frame pattern items is not guaranteed; "
1343                  "other than that, the items are valid from SW standpoint");
1344         return 0;
1345 }
1346
1347 /**
1348  * Confirm that a given flow can be accepted by the FW.
1349  *
1350  * @param sa
1351  *   Software adapter context
1352  * @param flow
1353  *   Flow to be verified
1354  * @return
1355  *   Zero on success and non-zero in the case of error.
1356  *   A special value of EAGAIN indicates that the adapter is
1357  *   not in started state. This state is compulsory because
1358  *   it only makes sense to compare the rule class of the flow
1359  *   being validated with classes of the active rules.
1360  *   Such classes are wittingly supported by the FW.
1361  */
1362 int
1363 sfc_mae_flow_verify(struct sfc_adapter *sa,
1364                     struct rte_flow *flow)
1365 {
1366         struct sfc_flow_spec *spec = &flow->spec;
1367         struct sfc_flow_spec_mae *spec_mae = &spec->mae;
1368
1369         SFC_ASSERT(sfc_adapter_is_locked(sa));
1370
1371         if (sa->state != SFC_ADAPTER_STARTED)
1372                 return EAGAIN;
1373
1374         return sfc_mae_action_rule_class_verify(sa, spec_mae);
1375 }
1376
1377 int
1378 sfc_mae_flow_insert(struct sfc_adapter *sa,
1379                     struct rte_flow *flow)
1380 {
1381         struct sfc_flow_spec *spec = &flow->spec;
1382         struct sfc_flow_spec_mae *spec_mae = &spec->mae;
1383         struct sfc_mae_action_set *action_set = spec_mae->action_set;
1384         struct sfc_mae_fw_rsrc *fw_rsrc = &action_set->fw_rsrc;
1385         int rc;
1386
1387         SFC_ASSERT(spec_mae->rule_id.id == EFX_MAE_RSRC_ID_INVALID);
1388         SFC_ASSERT(action_set != NULL);
1389
1390         rc = sfc_mae_action_set_enable(sa, action_set);
1391         if (rc != 0)
1392                 goto fail_action_set_enable;
1393
1394         rc = efx_mae_action_rule_insert(sa->nic, spec_mae->match_spec,
1395                                         NULL, &fw_rsrc->aset_id,
1396                                         &spec_mae->rule_id);
1397         if (rc != 0)
1398                 goto fail_action_rule_insert;
1399
1400         return 0;
1401
1402 fail_action_rule_insert:
1403         (void)sfc_mae_action_set_disable(sa, action_set);
1404
1405 fail_action_set_enable:
1406         return rc;
1407 }
1408
1409 int
1410 sfc_mae_flow_remove(struct sfc_adapter *sa,
1411                     struct rte_flow *flow)
1412 {
1413         struct sfc_flow_spec *spec = &flow->spec;
1414         struct sfc_flow_spec_mae *spec_mae = &spec->mae;
1415         struct sfc_mae_action_set *action_set = spec_mae->action_set;
1416         int rc;
1417
1418         SFC_ASSERT(spec_mae->rule_id.id != EFX_MAE_RSRC_ID_INVALID);
1419         SFC_ASSERT(action_set != NULL);
1420
1421         rc = efx_mae_action_rule_remove(sa->nic, &spec_mae->rule_id);
1422         if (rc != 0)
1423                 return rc;
1424
1425         spec_mae->rule_id.id = EFX_MAE_RSRC_ID_INVALID;
1426
1427         return sfc_mae_action_set_disable(sa, action_set);
1428 }