net/sfc: support flow action drop 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
19 int
20 sfc_mae_attach(struct sfc_adapter *sa)
21 {
22         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
23         struct sfc_mae *mae = &sa->mae;
24         efx_mae_limits_t limits;
25         int rc;
26
27         sfc_log_init(sa, "entry");
28
29         if (!encp->enc_mae_supported) {
30                 mae->status = SFC_MAE_STATUS_UNSUPPORTED;
31                 return 0;
32         }
33
34         sfc_log_init(sa, "init MAE");
35         rc = efx_mae_init(sa->nic);
36         if (rc != 0)
37                 goto fail_mae_init;
38
39         sfc_log_init(sa, "get MAE limits");
40         rc = efx_mae_get_limits(sa->nic, &limits);
41         if (rc != 0)
42                 goto fail_mae_get_limits;
43
44         mae->status = SFC_MAE_STATUS_SUPPORTED;
45         mae->nb_action_rule_prios_max = limits.eml_max_n_action_prios;
46         TAILQ_INIT(&mae->action_sets);
47
48         sfc_log_init(sa, "done");
49
50         return 0;
51
52 fail_mae_get_limits:
53         efx_mae_fini(sa->nic);
54
55 fail_mae_init:
56         sfc_log_init(sa, "failed %d", rc);
57
58         return rc;
59 }
60
61 void
62 sfc_mae_detach(struct sfc_adapter *sa)
63 {
64         struct sfc_mae *mae = &sa->mae;
65         enum sfc_mae_status status_prev = mae->status;
66
67         sfc_log_init(sa, "entry");
68
69         mae->nb_action_rule_prios_max = 0;
70         mae->status = SFC_MAE_STATUS_UNKNOWN;
71
72         if (status_prev != SFC_MAE_STATUS_SUPPORTED)
73                 return;
74
75         efx_mae_fini(sa->nic);
76
77         sfc_log_init(sa, "done");
78 }
79
80 static struct sfc_mae_action_set *
81 sfc_mae_action_set_attach(struct sfc_adapter *sa,
82                           const efx_mae_actions_t *spec)
83 {
84         struct sfc_mae_action_set *action_set;
85         struct sfc_mae *mae = &sa->mae;
86
87         SFC_ASSERT(sfc_adapter_is_locked(sa));
88
89         TAILQ_FOREACH(action_set, &mae->action_sets, entries) {
90                 if (efx_mae_action_set_specs_equal(action_set->spec, spec)) {
91                         ++(action_set->refcnt);
92                         return action_set;
93                 }
94         }
95
96         return NULL;
97 }
98
99 static int
100 sfc_mae_action_set_add(struct sfc_adapter *sa,
101                        efx_mae_actions_t *spec,
102                        struct sfc_mae_action_set **action_setp)
103 {
104         struct sfc_mae_action_set *action_set;
105         struct sfc_mae *mae = &sa->mae;
106
107         SFC_ASSERT(sfc_adapter_is_locked(sa));
108
109         action_set = rte_zmalloc("sfc_mae_action_set", sizeof(*action_set), 0);
110         if (action_set == NULL)
111                 return ENOMEM;
112
113         action_set->refcnt = 1;
114         action_set->spec = spec;
115
116         action_set->fw_rsrc.aset_id.id = EFX_MAE_RSRC_ID_INVALID;
117
118         TAILQ_INSERT_TAIL(&mae->action_sets, action_set, entries);
119
120         *action_setp = action_set;
121
122         return 0;
123 }
124
125 static void
126 sfc_mae_action_set_del(struct sfc_adapter *sa,
127                        struct sfc_mae_action_set *action_set)
128 {
129         struct sfc_mae *mae = &sa->mae;
130
131         SFC_ASSERT(sfc_adapter_is_locked(sa));
132         SFC_ASSERT(action_set->refcnt != 0);
133
134         --(action_set->refcnt);
135
136         if (action_set->refcnt != 0)
137                 return;
138
139         SFC_ASSERT(action_set->fw_rsrc.aset_id.id == EFX_MAE_RSRC_ID_INVALID);
140         SFC_ASSERT(action_set->fw_rsrc.refcnt == 0);
141
142         efx_mae_action_set_spec_fini(sa->nic, action_set->spec);
143         TAILQ_REMOVE(&mae->action_sets, action_set, entries);
144         rte_free(action_set);
145 }
146
147 static int
148 sfc_mae_action_set_enable(struct sfc_adapter *sa,
149                           struct sfc_mae_action_set *action_set)
150 {
151         struct sfc_mae_fw_rsrc *fw_rsrc = &action_set->fw_rsrc;
152         int rc;
153
154         SFC_ASSERT(sfc_adapter_is_locked(sa));
155
156         if (fw_rsrc->refcnt == 0) {
157                 SFC_ASSERT(fw_rsrc->aset_id.id == EFX_MAE_RSRC_ID_INVALID);
158                 SFC_ASSERT(action_set->spec != NULL);
159
160                 rc = efx_mae_action_set_alloc(sa->nic, action_set->spec,
161                                               &fw_rsrc->aset_id);
162                 if (rc != 0)
163                         return rc;
164         }
165
166         ++(fw_rsrc->refcnt);
167
168         return 0;
169 }
170
171 static int
172 sfc_mae_action_set_disable(struct sfc_adapter *sa,
173                            struct sfc_mae_action_set *action_set)
174 {
175         struct sfc_mae_fw_rsrc *fw_rsrc = &action_set->fw_rsrc;
176         int rc;
177
178         SFC_ASSERT(sfc_adapter_is_locked(sa));
179         SFC_ASSERT(fw_rsrc->aset_id.id != EFX_MAE_RSRC_ID_INVALID);
180         SFC_ASSERT(fw_rsrc->refcnt != 0);
181
182         if (fw_rsrc->refcnt == 1) {
183                 rc = efx_mae_action_set_free(sa->nic, &fw_rsrc->aset_id);
184                 if (rc != 0)
185                         return rc;
186
187                 fw_rsrc->aset_id.id = EFX_MAE_RSRC_ID_INVALID;
188         }
189
190         --(fw_rsrc->refcnt);
191
192         return 0;
193 }
194
195 void
196 sfc_mae_flow_cleanup(struct sfc_adapter *sa,
197                      struct rte_flow *flow)
198 {
199         struct sfc_flow_spec *spec;
200         struct sfc_flow_spec_mae *spec_mae;
201
202         if (flow == NULL)
203                 return;
204
205         spec = &flow->spec;
206
207         if (spec == NULL)
208                 return;
209
210         spec_mae = &spec->mae;
211
212         SFC_ASSERT(spec_mae->rule_id.id == EFX_MAE_RSRC_ID_INVALID);
213
214         if (spec_mae->action_set != NULL)
215                 sfc_mae_action_set_del(sa, spec_mae->action_set);
216
217         if (spec_mae->match_spec != NULL)
218                 efx_mae_match_spec_fini(sa->nic, spec_mae->match_spec);
219 }
220
221 static int
222 sfc_mae_rule_parse_item_phy_port(const struct rte_flow_item *item,
223                                  struct sfc_flow_parse_ctx *ctx,
224                                  struct rte_flow_error *error)
225 {
226         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
227         const struct rte_flow_item_phy_port supp_mask = {
228                 .index = 0xffffffff,
229         };
230         const void *def_mask = &rte_flow_item_phy_port_mask;
231         const struct rte_flow_item_phy_port *spec = NULL;
232         const struct rte_flow_item_phy_port *mask = NULL;
233         efx_mport_sel_t mport_v;
234         int rc;
235
236         if (ctx_mae->match_mport_set) {
237                 return rte_flow_error_set(error, ENOTSUP,
238                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
239                                 "Can't handle multiple traffic source items");
240         }
241
242         rc = sfc_flow_parse_init(item,
243                                  (const void **)&spec, (const void **)&mask,
244                                  (const void *)&supp_mask, def_mask,
245                                  sizeof(struct rte_flow_item_phy_port), error);
246         if (rc != 0)
247                 return rc;
248
249         if (mask->index != supp_mask.index) {
250                 return rte_flow_error_set(error, EINVAL,
251                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
252                                 "Bad mask in the PHY_PORT pattern item");
253         }
254
255         /* If "spec" is not set, could be any physical port */
256         if (spec == NULL)
257                 return 0;
258
259         rc = efx_mae_mport_by_phy_port(spec->index, &mport_v);
260         if (rc != 0) {
261                 return rte_flow_error_set(error, rc,
262                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
263                                 "Failed to convert the PHY_PORT index");
264         }
265
266         rc = efx_mae_match_spec_mport_set(ctx_mae->match_spec_action,
267                                           &mport_v, NULL);
268         if (rc != 0) {
269                 return rte_flow_error_set(error, rc,
270                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
271                                 "Failed to set MPORT for the PHY_PORT");
272         }
273
274         ctx_mae->match_mport_set = B_TRUE;
275
276         return 0;
277 }
278
279 static int
280 sfc_mae_rule_parse_item_pf(const struct rte_flow_item *item,
281                            struct sfc_flow_parse_ctx *ctx,
282                            struct rte_flow_error *error)
283 {
284         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
285         const efx_nic_cfg_t *encp = efx_nic_cfg_get(ctx_mae->sa->nic);
286         efx_mport_sel_t mport_v;
287         int rc;
288
289         if (ctx_mae->match_mport_set) {
290                 return rte_flow_error_set(error, ENOTSUP,
291                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
292                                 "Can't handle multiple traffic source items");
293         }
294
295         rc = efx_mae_mport_by_pcie_function(encp->enc_pf, EFX_PCI_VF_INVALID,
296                                             &mport_v);
297         if (rc != 0) {
298                 return rte_flow_error_set(error, rc,
299                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
300                                 "Failed to convert the PF ID");
301         }
302
303         rc = efx_mae_match_spec_mport_set(ctx_mae->match_spec_action,
304                                           &mport_v, NULL);
305         if (rc != 0) {
306                 return rte_flow_error_set(error, rc,
307                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
308                                 "Failed to set MPORT for the PF");
309         }
310
311         ctx_mae->match_mport_set = B_TRUE;
312
313         return 0;
314 }
315
316 static int
317 sfc_mae_rule_parse_item_vf(const struct rte_flow_item *item,
318                            struct sfc_flow_parse_ctx *ctx,
319                            struct rte_flow_error *error)
320 {
321         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
322         const efx_nic_cfg_t *encp = efx_nic_cfg_get(ctx_mae->sa->nic);
323         const struct rte_flow_item_vf supp_mask = {
324                 .id = 0xffffffff,
325         };
326         const void *def_mask = &rte_flow_item_vf_mask;
327         const struct rte_flow_item_vf *spec = NULL;
328         const struct rte_flow_item_vf *mask = NULL;
329         efx_mport_sel_t mport_v;
330         int rc;
331
332         if (ctx_mae->match_mport_set) {
333                 return rte_flow_error_set(error, ENOTSUP,
334                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
335                                 "Can't handle multiple traffic source items");
336         }
337
338         rc = sfc_flow_parse_init(item,
339                                  (const void **)&spec, (const void **)&mask,
340                                  (const void *)&supp_mask, def_mask,
341                                  sizeof(struct rte_flow_item_vf), error);
342         if (rc != 0)
343                 return rc;
344
345         if (mask->id != supp_mask.id) {
346                 return rte_flow_error_set(error, EINVAL,
347                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
348                                 "Bad mask in the VF pattern item");
349         }
350
351         /*
352          * If "spec" is not set, the item requests any VF related to the
353          * PF of the current DPDK port (but not the PF itself).
354          * Reject this match criterion as unsupported.
355          */
356         if (spec == NULL) {
357                 return rte_flow_error_set(error, EINVAL,
358                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
359                                 "Bad spec in the VF pattern item");
360         }
361
362         rc = efx_mae_mport_by_pcie_function(encp->enc_pf, spec->id, &mport_v);
363         if (rc != 0) {
364                 return rte_flow_error_set(error, rc,
365                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
366                                 "Failed to convert the PF + VF IDs");
367         }
368
369         rc = efx_mae_match_spec_mport_set(ctx_mae->match_spec_action,
370                                           &mport_v, NULL);
371         if (rc != 0) {
372                 return rte_flow_error_set(error, rc,
373                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
374                                 "Failed to set MPORT for the PF + VF");
375         }
376
377         ctx_mae->match_mport_set = B_TRUE;
378
379         return 0;
380 }
381
382 struct sfc_mae_field_locator {
383         efx_mae_field_id_t              field_id;
384         size_t                          size;
385         /* Field offset in the corresponding rte_flow_item_ struct */
386         size_t                          ofst;
387 };
388
389 static void
390 sfc_mae_item_build_supp_mask(const struct sfc_mae_field_locator *field_locators,
391                              unsigned int nb_field_locators, void *mask_ptr,
392                              size_t mask_size)
393 {
394         unsigned int i;
395
396         memset(mask_ptr, 0, mask_size);
397
398         for (i = 0; i < nb_field_locators; ++i) {
399                 const struct sfc_mae_field_locator *fl = &field_locators[i];
400
401                 SFC_ASSERT(fl->ofst + fl->size <= mask_size);
402                 memset(RTE_PTR_ADD(mask_ptr, fl->ofst), 0xff, fl->size);
403         }
404 }
405
406 static int
407 sfc_mae_parse_item(const struct sfc_mae_field_locator *field_locators,
408                    unsigned int nb_field_locators, const uint8_t *spec,
409                    const uint8_t *mask, efx_mae_match_spec_t *efx_spec,
410                    struct rte_flow_error *error)
411 {
412         unsigned int i;
413         int rc = 0;
414
415         for (i = 0; i < nb_field_locators; ++i) {
416                 const struct sfc_mae_field_locator *fl = &field_locators[i];
417
418                 rc = efx_mae_match_spec_field_set(efx_spec, fl->field_id,
419                                                   fl->size, spec + fl->ofst,
420                                                   fl->size, mask + fl->ofst);
421                 if (rc != 0)
422                         break;
423         }
424
425         if (rc != 0) {
426                 rc = rte_flow_error_set(error, rc, RTE_FLOW_ERROR_TYPE_ITEM,
427                                 NULL, "Failed to process item fields");
428         }
429
430         return rc;
431 }
432
433 static const struct sfc_mae_field_locator flocs_eth[] = {
434         {
435                 EFX_MAE_FIELD_ETHER_TYPE_BE,
436                 RTE_SIZEOF_FIELD(struct rte_flow_item_eth, type),
437                 offsetof(struct rte_flow_item_eth, type),
438         },
439         {
440                 EFX_MAE_FIELD_ETH_DADDR_BE,
441                 RTE_SIZEOF_FIELD(struct rte_flow_item_eth, dst),
442                 offsetof(struct rte_flow_item_eth, dst),
443         },
444         {
445                 EFX_MAE_FIELD_ETH_SADDR_BE,
446                 RTE_SIZEOF_FIELD(struct rte_flow_item_eth, src),
447                 offsetof(struct rte_flow_item_eth, src),
448         },
449 };
450
451 static int
452 sfc_mae_rule_parse_item_eth(const struct rte_flow_item *item,
453                             struct sfc_flow_parse_ctx *ctx,
454                             struct rte_flow_error *error)
455 {
456         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
457         struct rte_flow_item_eth supp_mask;
458         const uint8_t *spec = NULL;
459         const uint8_t *mask = NULL;
460         int rc;
461
462         sfc_mae_item_build_supp_mask(flocs_eth, RTE_DIM(flocs_eth),
463                                      &supp_mask, sizeof(supp_mask));
464
465         rc = sfc_flow_parse_init(item,
466                                  (const void **)&spec, (const void **)&mask,
467                                  (const void *)&supp_mask,
468                                  &rte_flow_item_eth_mask,
469                                  sizeof(struct rte_flow_item_eth), error);
470         if (rc != 0)
471                 return rc;
472
473         /* If "spec" is not set, could be any Ethernet */
474         if (spec == NULL)
475                 return 0;
476
477         return sfc_mae_parse_item(flocs_eth, RTE_DIM(flocs_eth), spec, mask,
478                                   ctx_mae->match_spec_action, error);
479 }
480
481 static const struct sfc_flow_item sfc_flow_items[] = {
482         {
483                 .type = RTE_FLOW_ITEM_TYPE_PHY_PORT,
484                 /*
485                  * In terms of RTE flow, this item is a META one,
486                  * and its position in the pattern is don't care.
487                  */
488                 .prev_layer = SFC_FLOW_ITEM_ANY_LAYER,
489                 .layer = SFC_FLOW_ITEM_ANY_LAYER,
490                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
491                 .parse = sfc_mae_rule_parse_item_phy_port,
492         },
493         {
494                 .type = RTE_FLOW_ITEM_TYPE_PF,
495                 /*
496                  * In terms of RTE flow, this item is a META one,
497                  * and its position in the pattern is don't care.
498                  */
499                 .prev_layer = SFC_FLOW_ITEM_ANY_LAYER,
500                 .layer = SFC_FLOW_ITEM_ANY_LAYER,
501                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
502                 .parse = sfc_mae_rule_parse_item_pf,
503         },
504         {
505                 .type = RTE_FLOW_ITEM_TYPE_VF,
506                 /*
507                  * In terms of RTE flow, this item is a META one,
508                  * and its position in the pattern is don't care.
509                  */
510                 .prev_layer = SFC_FLOW_ITEM_ANY_LAYER,
511                 .layer = SFC_FLOW_ITEM_ANY_LAYER,
512                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
513                 .parse = sfc_mae_rule_parse_item_vf,
514         },
515         {
516                 .type = RTE_FLOW_ITEM_TYPE_ETH,
517                 .prev_layer = SFC_FLOW_ITEM_START_LAYER,
518                 .layer = SFC_FLOW_ITEM_L2,
519                 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
520                 .parse = sfc_mae_rule_parse_item_eth,
521         },
522 };
523
524 int
525 sfc_mae_rule_parse_pattern(struct sfc_adapter *sa,
526                            const struct rte_flow_item pattern[],
527                            struct sfc_flow_spec_mae *spec,
528                            struct rte_flow_error *error)
529 {
530         struct sfc_mae_parse_ctx ctx_mae;
531         struct sfc_flow_parse_ctx ctx;
532         int rc;
533
534         memset(&ctx_mae, 0, sizeof(ctx_mae));
535         ctx_mae.sa = sa;
536
537         rc = efx_mae_match_spec_init(sa->nic, EFX_MAE_RULE_ACTION,
538                                      spec->priority,
539                                      &ctx_mae.match_spec_action);
540         if (rc != 0) {
541                 rc = rte_flow_error_set(error, rc,
542                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
543                         "Failed to initialise action rule match specification");
544                 goto fail_init_match_spec_action;
545         }
546
547         ctx.type = SFC_FLOW_PARSE_CTX_MAE;
548         ctx.mae = &ctx_mae;
549
550         rc = sfc_flow_parse_pattern(sfc_flow_items, RTE_DIM(sfc_flow_items),
551                                     pattern, &ctx, error);
552         if (rc != 0)
553                 goto fail_parse_pattern;
554
555         if (!efx_mae_match_spec_is_valid(sa->nic, ctx_mae.match_spec_action)) {
556                 rc = rte_flow_error_set(error, ENOTSUP,
557                                         RTE_FLOW_ERROR_TYPE_ITEM, NULL,
558                                         "Inconsistent pattern");
559                 goto fail_validate_match_spec_action;
560         }
561
562         spec->match_spec = ctx_mae.match_spec_action;
563
564         return 0;
565
566 fail_validate_match_spec_action:
567 fail_parse_pattern:
568         efx_mae_match_spec_fini(sa->nic, ctx_mae.match_spec_action);
569
570 fail_init_match_spec_action:
571         return rc;
572 }
573
574 /*
575  * An action supported by MAE may correspond to a bundle of RTE flow actions,
576  * in example, VLAN_PUSH = OF_PUSH_VLAN + OF_VLAN_SET_VID + OF_VLAN_SET_PCP.
577  * That is, related RTE flow actions need to be tracked as parts of a whole
578  * so that they can be combined into a single action and submitted to MAE
579  * representation of a given rule's action set.
580  *
581  * Each RTE flow action provided by an application gets classified as
582  * one belonging to some bundle type. If an action is not supposed to
583  * belong to any bundle, or if this action is END, it is described as
584  * one belonging to a dummy bundle of type EMPTY.
585  *
586  * A currently tracked bundle will be submitted if a repeating
587  * action or an action of different bundle type follows.
588  */
589
590 enum sfc_mae_actions_bundle_type {
591         SFC_MAE_ACTIONS_BUNDLE_EMPTY = 0,
592         SFC_MAE_ACTIONS_BUNDLE_VLAN_PUSH,
593 };
594
595 struct sfc_mae_actions_bundle {
596         enum sfc_mae_actions_bundle_type        type;
597
598         /* Indicates actions already tracked by the current bundle */
599         uint64_t                                actions_mask;
600
601         /* Parameters used by SFC_MAE_ACTIONS_BUNDLE_VLAN_PUSH */
602         rte_be16_t                              vlan_push_tpid;
603         rte_be16_t                              vlan_push_tci;
604 };
605
606 /*
607  * Combine configuration of RTE flow actions tracked by the bundle into a
608  * single action and submit the result to MAE action set specification.
609  * Do nothing in the case of dummy action bundle.
610  */
611 static int
612 sfc_mae_actions_bundle_submit(const struct sfc_mae_actions_bundle *bundle,
613                               efx_mae_actions_t *spec)
614 {
615         int rc = 0;
616
617         switch (bundle->type) {
618         case SFC_MAE_ACTIONS_BUNDLE_EMPTY:
619                 break;
620         case SFC_MAE_ACTIONS_BUNDLE_VLAN_PUSH:
621                 rc = efx_mae_action_set_populate_vlan_push(
622                         spec, bundle->vlan_push_tpid, bundle->vlan_push_tci);
623                 break;
624         default:
625                 SFC_ASSERT(B_FALSE);
626                 break;
627         }
628
629         return rc;
630 }
631
632 /*
633  * Given the type of the next RTE flow action in the line, decide
634  * whether a new bundle is about to start, and, if this is the case,
635  * submit and reset the current bundle.
636  */
637 static int
638 sfc_mae_actions_bundle_sync(const struct rte_flow_action *action,
639                             struct sfc_mae_actions_bundle *bundle,
640                             efx_mae_actions_t *spec,
641                             struct rte_flow_error *error)
642 {
643         enum sfc_mae_actions_bundle_type bundle_type_new;
644         int rc;
645
646         switch (action->type) {
647         case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
648         case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
649         case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
650                 bundle_type_new = SFC_MAE_ACTIONS_BUNDLE_VLAN_PUSH;
651                 break;
652         default:
653                 /*
654                  * Self-sufficient actions, including END, are handled in this
655                  * case. No checks for unsupported actions are needed here
656                  * because parsing doesn't occur at this point.
657                  */
658                 bundle_type_new = SFC_MAE_ACTIONS_BUNDLE_EMPTY;
659                 break;
660         }
661
662         if (bundle_type_new != bundle->type ||
663             (bundle->actions_mask & (1ULL << action->type)) != 0) {
664                 rc = sfc_mae_actions_bundle_submit(bundle, spec);
665                 if (rc != 0)
666                         goto fail_submit;
667
668                 memset(bundle, 0, sizeof(*bundle));
669         }
670
671         bundle->type = bundle_type_new;
672
673         return 0;
674
675 fail_submit:
676         return rte_flow_error_set(error, rc,
677                         RTE_FLOW_ERROR_TYPE_ACTION, NULL,
678                         "Failed to request the (group of) action(s)");
679 }
680
681 static void
682 sfc_mae_rule_parse_action_of_push_vlan(
683                             const struct rte_flow_action_of_push_vlan *conf,
684                             struct sfc_mae_actions_bundle *bundle)
685 {
686         bundle->vlan_push_tpid = conf->ethertype;
687 }
688
689 static void
690 sfc_mae_rule_parse_action_of_set_vlan_vid(
691                             const struct rte_flow_action_of_set_vlan_vid *conf,
692                             struct sfc_mae_actions_bundle *bundle)
693 {
694         bundle->vlan_push_tci |= (conf->vlan_vid &
695                                   rte_cpu_to_be_16(RTE_LEN2MASK(12, uint16_t)));
696 }
697
698 static void
699 sfc_mae_rule_parse_action_of_set_vlan_pcp(
700                             const struct rte_flow_action_of_set_vlan_pcp *conf,
701                             struct sfc_mae_actions_bundle *bundle)
702 {
703         uint16_t vlan_tci_pcp = (uint16_t)(conf->vlan_pcp &
704                                            RTE_LEN2MASK(3, uint8_t)) << 13;
705
706         bundle->vlan_push_tci |= rte_cpu_to_be_16(vlan_tci_pcp);
707 }
708
709 static int
710 sfc_mae_rule_parse_action_mark(const struct rte_flow_action_mark *conf,
711                                efx_mae_actions_t *spec)
712 {
713         return efx_mae_action_set_populate_mark(spec, conf->id);
714 }
715
716 static int
717 sfc_mae_rule_parse_action_phy_port(struct sfc_adapter *sa,
718                                    const struct rte_flow_action_phy_port *conf,
719                                    efx_mae_actions_t *spec)
720 {
721         efx_mport_sel_t mport;
722         uint32_t phy_port;
723         int rc;
724
725         if (conf->original != 0)
726                 phy_port = efx_nic_cfg_get(sa->nic)->enc_assigned_port;
727         else
728                 phy_port = conf->index;
729
730         rc = efx_mae_mport_by_phy_port(phy_port, &mport);
731         if (rc != 0)
732                 return rc;
733
734         return efx_mae_action_set_populate_deliver(spec, &mport);
735 }
736
737 static int
738 sfc_mae_rule_parse_action_pf_vf(struct sfc_adapter *sa,
739                                 const struct rte_flow_action_vf *vf_conf,
740                                 efx_mae_actions_t *spec)
741 {
742         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
743         efx_mport_sel_t mport;
744         uint32_t vf;
745         int rc;
746
747         if (vf_conf == NULL)
748                 vf = EFX_PCI_VF_INVALID;
749         else if (vf_conf->original != 0)
750                 vf = encp->enc_vf;
751         else
752                 vf = vf_conf->id;
753
754         rc = efx_mae_mport_by_pcie_function(encp->enc_pf, vf, &mport);
755         if (rc != 0)
756                 return rc;
757
758         return efx_mae_action_set_populate_deliver(spec, &mport);
759 }
760
761 static int
762 sfc_mae_rule_parse_action(struct sfc_adapter *sa,
763                           const struct rte_flow_action *action,
764                           struct sfc_mae_actions_bundle *bundle,
765                           efx_mae_actions_t *spec,
766                           struct rte_flow_error *error)
767 {
768         int rc = 0;
769
770         switch (action->type) {
771         case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
772                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_OF_POP_VLAN,
773                                        bundle->actions_mask);
774                 rc = efx_mae_action_set_populate_vlan_pop(spec);
775                 break;
776         case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
777                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN,
778                                        bundle->actions_mask);
779                 sfc_mae_rule_parse_action_of_push_vlan(action->conf, bundle);
780                 break;
781         case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
782                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID,
783                                        bundle->actions_mask);
784                 sfc_mae_rule_parse_action_of_set_vlan_vid(action->conf, bundle);
785                 break;
786         case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
787                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP,
788                                        bundle->actions_mask);
789                 sfc_mae_rule_parse_action_of_set_vlan_pcp(action->conf, bundle);
790                 break;
791         case RTE_FLOW_ACTION_TYPE_FLAG:
792                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_FLAG,
793                                        bundle->actions_mask);
794                 rc = efx_mae_action_set_populate_flag(spec);
795                 break;
796         case RTE_FLOW_ACTION_TYPE_MARK:
797                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_MARK,
798                                        bundle->actions_mask);
799                 rc = sfc_mae_rule_parse_action_mark(action->conf, spec);
800                 break;
801         case RTE_FLOW_ACTION_TYPE_PHY_PORT:
802                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_PHY_PORT,
803                                        bundle->actions_mask);
804                 rc = sfc_mae_rule_parse_action_phy_port(sa, action->conf, spec);
805                 break;
806         case RTE_FLOW_ACTION_TYPE_PF:
807                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_PF,
808                                        bundle->actions_mask);
809                 rc = sfc_mae_rule_parse_action_pf_vf(sa, NULL, spec);
810                 break;
811         case RTE_FLOW_ACTION_TYPE_VF:
812                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_VF,
813                                        bundle->actions_mask);
814                 rc = sfc_mae_rule_parse_action_pf_vf(sa, action->conf, spec);
815                 break;
816         case RTE_FLOW_ACTION_TYPE_DROP:
817                 SFC_BUILD_SET_OVERFLOW(RTE_FLOW_ACTION_TYPE_DROP,
818                                        bundle->actions_mask);
819                 rc = efx_mae_action_set_populate_drop(spec);
820                 break;
821         default:
822                 return rte_flow_error_set(error, ENOTSUP,
823                                 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
824                                 "Unsupported action");
825         }
826
827         if (rc != 0) {
828                 rc = rte_flow_error_set(error, rc, RTE_FLOW_ERROR_TYPE_ACTION,
829                                 NULL, "Failed to request the action");
830         } else {
831                 bundle->actions_mask |= (1ULL << action->type);
832         }
833
834         return rc;
835 }
836
837 int
838 sfc_mae_rule_parse_actions(struct sfc_adapter *sa,
839                            const struct rte_flow_action actions[],
840                            struct sfc_mae_action_set **action_setp,
841                            struct rte_flow_error *error)
842 {
843         struct sfc_mae_actions_bundle bundle = {0};
844         const struct rte_flow_action *action;
845         efx_mae_actions_t *spec;
846         int rc;
847
848         if (actions == NULL) {
849                 return rte_flow_error_set(error, EINVAL,
850                                 RTE_FLOW_ERROR_TYPE_ACTION_NUM, NULL,
851                                 "NULL actions");
852         }
853
854         rc = efx_mae_action_set_spec_init(sa->nic, &spec);
855         if (rc != 0)
856                 goto fail_action_set_spec_init;
857
858         for (action = actions;
859              action->type != RTE_FLOW_ACTION_TYPE_END; ++action) {
860                 rc = sfc_mae_actions_bundle_sync(action, &bundle, spec, error);
861                 if (rc != 0)
862                         goto fail_rule_parse_action;
863
864                 rc = sfc_mae_rule_parse_action(sa, action, &bundle, spec,
865                                                error);
866                 if (rc != 0)
867                         goto fail_rule_parse_action;
868         }
869
870         rc = sfc_mae_actions_bundle_sync(action, &bundle, spec, error);
871         if (rc != 0)
872                 goto fail_rule_parse_action;
873
874         *action_setp = sfc_mae_action_set_attach(sa, spec);
875         if (*action_setp != NULL) {
876                 efx_mae_action_set_spec_fini(sa->nic, spec);
877                 return 0;
878         }
879
880         rc = sfc_mae_action_set_add(sa, spec, action_setp);
881         if (rc != 0)
882                 goto fail_action_set_add;
883
884         return 0;
885
886 fail_action_set_add:
887 fail_rule_parse_action:
888         efx_mae_action_set_spec_fini(sa->nic, spec);
889
890 fail_action_set_spec_init:
891         if (rc > 0) {
892                 rc = rte_flow_error_set(error, rc,
893                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
894                         NULL, "Failed to process the action");
895         }
896         return rc;
897 }
898
899 static bool
900 sfc_mae_rules_class_cmp(struct sfc_adapter *sa,
901                         const efx_mae_match_spec_t *left,
902                         const efx_mae_match_spec_t *right)
903 {
904         bool have_same_class;
905         int rc;
906
907         rc = efx_mae_match_specs_class_cmp(sa->nic, left, right,
908                                            &have_same_class);
909
910         return (rc == 0) ? have_same_class : false;
911 }
912
913 static int
914 sfc_mae_action_rule_class_verify(struct sfc_adapter *sa,
915                                  struct sfc_flow_spec_mae *spec)
916 {
917         const struct rte_flow *entry;
918
919         TAILQ_FOREACH_REVERSE(entry, &sa->flow_list, sfc_flow_list, entries) {
920                 const struct sfc_flow_spec *entry_spec = &entry->spec;
921                 const struct sfc_flow_spec_mae *es_mae = &entry_spec->mae;
922                 const efx_mae_match_spec_t *left = es_mae->match_spec;
923                 const efx_mae_match_spec_t *right = spec->match_spec;
924
925                 switch (entry_spec->type) {
926                 case SFC_FLOW_SPEC_FILTER:
927                         /* Ignore VNIC-level flows */
928                         break;
929                 case SFC_FLOW_SPEC_MAE:
930                         if (sfc_mae_rules_class_cmp(sa, left, right))
931                                 return 0;
932                         break;
933                 default:
934                         SFC_ASSERT(false);
935                 }
936         }
937
938         sfc_info(sa, "for now, the HW doesn't support rule validation, and HW "
939                  "support for inner frame pattern items is not guaranteed; "
940                  "other than that, the items are valid from SW standpoint");
941         return 0;
942 }
943
944 /**
945  * Confirm that a given flow can be accepted by the FW.
946  *
947  * @param sa
948  *   Software adapter context
949  * @param flow
950  *   Flow to be verified
951  * @return
952  *   Zero on success and non-zero in the case of error.
953  *   A special value of EAGAIN indicates that the adapter is
954  *   not in started state. This state is compulsory because
955  *   it only makes sense to compare the rule class of the flow
956  *   being validated with classes of the active rules.
957  *   Such classes are wittingly supported by the FW.
958  */
959 int
960 sfc_mae_flow_verify(struct sfc_adapter *sa,
961                     struct rte_flow *flow)
962 {
963         struct sfc_flow_spec *spec = &flow->spec;
964         struct sfc_flow_spec_mae *spec_mae = &spec->mae;
965
966         SFC_ASSERT(sfc_adapter_is_locked(sa));
967
968         if (sa->state != SFC_ADAPTER_STARTED)
969                 return EAGAIN;
970
971         return sfc_mae_action_rule_class_verify(sa, spec_mae);
972 }
973
974 int
975 sfc_mae_flow_insert(struct sfc_adapter *sa,
976                     struct rte_flow *flow)
977 {
978         struct sfc_flow_spec *spec = &flow->spec;
979         struct sfc_flow_spec_mae *spec_mae = &spec->mae;
980         struct sfc_mae_action_set *action_set = spec_mae->action_set;
981         struct sfc_mae_fw_rsrc *fw_rsrc = &action_set->fw_rsrc;
982         int rc;
983
984         SFC_ASSERT(spec_mae->rule_id.id == EFX_MAE_RSRC_ID_INVALID);
985         SFC_ASSERT(action_set != NULL);
986
987         rc = sfc_mae_action_set_enable(sa, action_set);
988         if (rc != 0)
989                 goto fail_action_set_enable;
990
991         rc = efx_mae_action_rule_insert(sa->nic, spec_mae->match_spec,
992                                         NULL, &fw_rsrc->aset_id,
993                                         &spec_mae->rule_id);
994         if (rc != 0)
995                 goto fail_action_rule_insert;
996
997         return 0;
998
999 fail_action_rule_insert:
1000         (void)sfc_mae_action_set_disable(sa, action_set);
1001
1002 fail_action_set_enable:
1003         return rc;
1004 }
1005
1006 int
1007 sfc_mae_flow_remove(struct sfc_adapter *sa,
1008                     struct rte_flow *flow)
1009 {
1010         struct sfc_flow_spec *spec = &flow->spec;
1011         struct sfc_flow_spec_mae *spec_mae = &spec->mae;
1012         struct sfc_mae_action_set *action_set = spec_mae->action_set;
1013         int rc;
1014
1015         SFC_ASSERT(spec_mae->rule_id.id != EFX_MAE_RSRC_ID_INVALID);
1016         SFC_ASSERT(action_set != NULL);
1017
1018         rc = efx_mae_action_rule_remove(sa->nic, &spec_mae->rule_id);
1019         if (rc != 0)
1020                 return rc;
1021
1022         spec_mae->rule_id.id = EFX_MAE_RSRC_ID_INVALID;
1023
1024         return sfc_mae_action_set_disable(sa, action_set);
1025 }