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