OSDN Git Service

net: dsa: ocelot: convert to mac_select_pcs()
[uclinux-h8/linux.git] / drivers / net / dsa / ocelot / felix.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2019-2021 NXP
3  *
4  * This is an umbrella module for all network switches that are
5  * register-compatible with Ocelot and that perform I/O to their host CPU
6  * through an NPI (Node Processor Interface) Ethernet port.
7  */
8 #include <uapi/linux/if_bridge.h>
9 #include <soc/mscc/ocelot_vcap.h>
10 #include <soc/mscc/ocelot_qsys.h>
11 #include <soc/mscc/ocelot_sys.h>
12 #include <soc/mscc/ocelot_dev.h>
13 #include <soc/mscc/ocelot_ana.h>
14 #include <soc/mscc/ocelot_ptp.h>
15 #include <soc/mscc/ocelot.h>
16 #include <linux/dsa/8021q.h>
17 #include <linux/dsa/ocelot.h>
18 #include <linux/platform_device.h>
19 #include <linux/ptp_classify.h>
20 #include <linux/module.h>
21 #include <linux/of_net.h>
22 #include <linux/pci.h>
23 #include <linux/of.h>
24 #include <net/pkt_sched.h>
25 #include <net/dsa.h>
26 #include "felix.h"
27
28 static int felix_tag_8021q_rxvlan_add(struct felix *felix, int port, u16 vid,
29                                       bool pvid, bool untagged)
30 {
31         struct ocelot_vcap_filter *outer_tagging_rule;
32         struct ocelot *ocelot = &felix->ocelot;
33         struct dsa_switch *ds = felix->ds;
34         int key_length, upstream, err;
35
36         /* We don't need to install the rxvlan into the other ports' filtering
37          * tables, because we're just pushing the rxvlan when sending towards
38          * the CPU
39          */
40         if (!pvid)
41                 return 0;
42
43         key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length;
44         upstream = dsa_upstream_port(ds, port);
45
46         outer_tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter),
47                                      GFP_KERNEL);
48         if (!outer_tagging_rule)
49                 return -ENOMEM;
50
51         outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
52         outer_tagging_rule->prio = 1;
53         outer_tagging_rule->id.cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port);
54         outer_tagging_rule->id.tc_offload = false;
55         outer_tagging_rule->block_id = VCAP_ES0;
56         outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
57         outer_tagging_rule->lookup = 0;
58         outer_tagging_rule->ingress_port.value = port;
59         outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0);
60         outer_tagging_rule->egress_port.value = upstream;
61         outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0);
62         outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG;
63         outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD;
64         outer_tagging_rule->action.tag_a_vid_sel = 1;
65         outer_tagging_rule->action.vid_a_val = vid;
66
67         err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL);
68         if (err)
69                 kfree(outer_tagging_rule);
70
71         return err;
72 }
73
74 static int felix_tag_8021q_txvlan_add(struct felix *felix, int port, u16 vid,
75                                       bool pvid, bool untagged)
76 {
77         struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
78         struct ocelot *ocelot = &felix->ocelot;
79         struct dsa_switch *ds = felix->ds;
80         int upstream, err;
81
82         /* tag_8021q.c assumes we are implementing this via port VLAN
83          * membership, which we aren't. So we don't need to add any VCAP filter
84          * for the CPU port.
85          */
86         if (ocelot->ports[port]->is_dsa_8021q_cpu)
87                 return 0;
88
89         untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
90         if (!untagging_rule)
91                 return -ENOMEM;
92
93         redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
94         if (!redirect_rule) {
95                 kfree(untagging_rule);
96                 return -ENOMEM;
97         }
98
99         upstream = dsa_upstream_port(ds, port);
100
101         untagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
102         untagging_rule->ingress_port_mask = BIT(upstream);
103         untagging_rule->vlan.vid.value = vid;
104         untagging_rule->vlan.vid.mask = VLAN_VID_MASK;
105         untagging_rule->prio = 1;
106         untagging_rule->id.cookie = OCELOT_VCAP_IS1_TAG_8021Q_TXVLAN(ocelot, port);
107         untagging_rule->id.tc_offload = false;
108         untagging_rule->block_id = VCAP_IS1;
109         untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
110         untagging_rule->lookup = 0;
111         untagging_rule->action.vlan_pop_cnt_ena = true;
112         untagging_rule->action.vlan_pop_cnt = 1;
113         untagging_rule->action.pag_override_mask = 0xff;
114         untagging_rule->action.pag_val = port;
115
116         err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL);
117         if (err) {
118                 kfree(untagging_rule);
119                 kfree(redirect_rule);
120                 return err;
121         }
122
123         redirect_rule->key_type = OCELOT_VCAP_KEY_ANY;
124         redirect_rule->ingress_port_mask = BIT(upstream);
125         redirect_rule->pag = port;
126         redirect_rule->prio = 1;
127         redirect_rule->id.cookie = OCELOT_VCAP_IS2_TAG_8021Q_TXVLAN(ocelot, port);
128         redirect_rule->id.tc_offload = false;
129         redirect_rule->block_id = VCAP_IS2;
130         redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
131         redirect_rule->lookup = 0;
132         redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT;
133         redirect_rule->action.port_mask = BIT(port);
134
135         err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL);
136         if (err) {
137                 ocelot_vcap_filter_del(ocelot, untagging_rule);
138                 kfree(redirect_rule);
139                 return err;
140         }
141
142         return 0;
143 }
144
145 static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
146                                     u16 flags)
147 {
148         bool untagged = flags & BRIDGE_VLAN_INFO_UNTAGGED;
149         bool pvid = flags & BRIDGE_VLAN_INFO_PVID;
150         struct ocelot *ocelot = ds->priv;
151
152         if (vid_is_dsa_8021q_rxvlan(vid))
153                 return felix_tag_8021q_rxvlan_add(ocelot_to_felix(ocelot),
154                                                   port, vid, pvid, untagged);
155
156         if (vid_is_dsa_8021q_txvlan(vid))
157                 return felix_tag_8021q_txvlan_add(ocelot_to_felix(ocelot),
158                                                   port, vid, pvid, untagged);
159
160         return 0;
161 }
162
163 static int felix_tag_8021q_rxvlan_del(struct felix *felix, int port, u16 vid)
164 {
165         struct ocelot_vcap_filter *outer_tagging_rule;
166         struct ocelot_vcap_block *block_vcap_es0;
167         struct ocelot *ocelot = &felix->ocelot;
168
169         block_vcap_es0 = &ocelot->block[VCAP_ES0];
170
171         outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0,
172                                                                  port, false);
173         /* In rxvlan_add, we had the "if (!pvid) return 0" logic to avoid
174          * installing outer tagging ES0 rules where they weren't needed.
175          * But in rxvlan_del, the API doesn't give us the "flags" anymore,
176          * so that forces us to be slightly sloppy here, and just assume that
177          * if we didn't find an outer_tagging_rule it means that there was
178          * none in the first place, i.e. rxvlan_del is called on a non-pvid
179          * port. This is most probably true though.
180          */
181         if (!outer_tagging_rule)
182                 return 0;
183
184         return ocelot_vcap_filter_del(ocelot, outer_tagging_rule);
185 }
186
187 static int felix_tag_8021q_txvlan_del(struct felix *felix, int port, u16 vid)
188 {
189         struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
190         struct ocelot_vcap_block *block_vcap_is1;
191         struct ocelot_vcap_block *block_vcap_is2;
192         struct ocelot *ocelot = &felix->ocelot;
193         int err;
194
195         if (ocelot->ports[port]->is_dsa_8021q_cpu)
196                 return 0;
197
198         block_vcap_is1 = &ocelot->block[VCAP_IS1];
199         block_vcap_is2 = &ocelot->block[VCAP_IS2];
200
201         untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1,
202                                                              port, false);
203         if (!untagging_rule)
204                 return 0;
205
206         err = ocelot_vcap_filter_del(ocelot, untagging_rule);
207         if (err)
208                 return err;
209
210         redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2,
211                                                             port, false);
212         if (!redirect_rule)
213                 return 0;
214
215         return ocelot_vcap_filter_del(ocelot, redirect_rule);
216 }
217
218 static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
219 {
220         struct ocelot *ocelot = ds->priv;
221
222         if (vid_is_dsa_8021q_rxvlan(vid))
223                 return felix_tag_8021q_rxvlan_del(ocelot_to_felix(ocelot),
224                                                   port, vid);
225
226         if (vid_is_dsa_8021q_txvlan(vid))
227                 return felix_tag_8021q_txvlan_del(ocelot_to_felix(ocelot),
228                                                   port, vid);
229
230         return 0;
231 }
232
233 /* Alternatively to using the NPI functionality, that same hardware MAC
234  * connected internally to the enetc or fman DSA master can be configured to
235  * use the software-defined tag_8021q frame format. As far as the hardware is
236  * concerned, it thinks it is a "dumb switch" - the queues of the CPU port
237  * module are now disconnected from it, but can still be accessed through
238  * register-based MMIO.
239  */
240 static void felix_8021q_cpu_port_init(struct ocelot *ocelot, int port)
241 {
242         mutex_lock(&ocelot->fwd_domain_lock);
243
244         ocelot->ports[port]->is_dsa_8021q_cpu = true;
245         ocelot->npi = -1;
246
247         /* Overwrite PGID_CPU with the non-tagging port */
248         ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, PGID_CPU);
249
250         ocelot_apply_bridge_fwd_mask(ocelot, true);
251
252         mutex_unlock(&ocelot->fwd_domain_lock);
253 }
254
255 static void felix_8021q_cpu_port_deinit(struct ocelot *ocelot, int port)
256 {
257         mutex_lock(&ocelot->fwd_domain_lock);
258
259         ocelot->ports[port]->is_dsa_8021q_cpu = false;
260
261         /* Restore PGID_CPU */
262         ocelot_write_rix(ocelot, BIT(ocelot->num_phys_ports), ANA_PGID_PGID,
263                          PGID_CPU);
264
265         ocelot_apply_bridge_fwd_mask(ocelot, true);
266
267         mutex_unlock(&ocelot->fwd_domain_lock);
268 }
269
270 /* On switches with no extraction IRQ wired, trapped packets need to be
271  * replicated over Ethernet as well, otherwise we'd get no notification of
272  * their arrival when using the ocelot-8021q tagging protocol.
273  */
274 static int felix_update_trapping_destinations(struct dsa_switch *ds,
275                                               bool using_tag_8021q)
276 {
277         struct ocelot *ocelot = ds->priv;
278         struct felix *felix = ocelot_to_felix(ocelot);
279         struct ocelot_vcap_filter *trap;
280         enum ocelot_mask_mode mask_mode;
281         unsigned long port_mask;
282         struct dsa_port *dp;
283         bool cpu_copy_ena;
284         int cpu = -1, err;
285
286         if (!felix->info->quirk_no_xtr_irq)
287                 return 0;
288
289         /* Figure out the current CPU port */
290         dsa_switch_for_each_cpu_port(dp, ds) {
291                 cpu = dp->index;
292                 break;
293         }
294
295         /* We are sure that "cpu" was found, otherwise
296          * dsa_tree_setup_default_cpu() would have failed earlier.
297          */
298
299         /* Make sure all traps are set up for that destination */
300         list_for_each_entry(trap, &ocelot->traps, trap_list) {
301                 /* Figure out the current trapping destination */
302                 if (using_tag_8021q) {
303                         /* Redirect to the tag_8021q CPU port. If timestamps
304                          * are necessary, also copy trapped packets to the CPU
305                          * port module.
306                          */
307                         mask_mode = OCELOT_MASK_MODE_REDIRECT;
308                         port_mask = BIT(cpu);
309                         cpu_copy_ena = !!trap->take_ts;
310                 } else {
311                         /* Trap packets only to the CPU port module, which is
312                          * redirected to the NPI port (the DSA CPU port)
313                          */
314                         mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
315                         port_mask = 0;
316                         cpu_copy_ena = true;
317                 }
318
319                 if (trap->action.mask_mode == mask_mode &&
320                     trap->action.port_mask == port_mask &&
321                     trap->action.cpu_copy_ena == cpu_copy_ena)
322                         continue;
323
324                 trap->action.mask_mode = mask_mode;
325                 trap->action.port_mask = port_mask;
326                 trap->action.cpu_copy_ena = cpu_copy_ena;
327
328                 err = ocelot_vcap_filter_replace(ocelot, trap);
329                 if (err)
330                         return err;
331         }
332
333         return 0;
334 }
335
336 static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu)
337 {
338         struct ocelot *ocelot = ds->priv;
339         unsigned long cpu_flood;
340         struct dsa_port *dp;
341         int err;
342
343         felix_8021q_cpu_port_init(ocelot, cpu);
344
345         dsa_switch_for_each_available_port(dp, ds) {
346                 /* This overwrites ocelot_init():
347                  * Do not forward BPDU frames to the CPU port module,
348                  * for 2 reasons:
349                  * - When these packets are injected from the tag_8021q
350                  *   CPU port, we want them to go out, not loop back
351                  *   into the system.
352                  * - STP traffic ingressing on a user port should go to
353                  *   the tag_8021q CPU port, not to the hardware CPU
354                  *   port module.
355                  */
356                 ocelot_write_gix(ocelot,
357                                  ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0),
358                                  ANA_PORT_CPU_FWD_BPDU_CFG, dp->index);
359         }
360
361         /* In tag_8021q mode, the CPU port module is unused, except for PTP
362          * frames. So we want to disable flooding of any kind to the CPU port
363          * module, since packets going there will end in a black hole.
364          */
365         cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
366         ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_UC);
367         ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_MC);
368         ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_BC);
369
370         err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD));
371         if (err)
372                 return err;
373
374         err = felix_update_trapping_destinations(ds, true);
375         if (err)
376                 goto out_tag_8021q_unregister;
377
378         /* The ownership of the CPU port module's queues might have just been
379          * transferred to the tag_8021q tagger from the NPI-based tagger.
380          * So there might still be all sorts of crap in the queues. On the
381          * other hand, the MMIO-based matching of PTP frames is very brittle,
382          * so we need to be careful that there are no extra frames to be
383          * dequeued over MMIO, since we would never know to discard them.
384          */
385         ocelot_drain_cpu_queue(ocelot, 0);
386
387         return 0;
388
389 out_tag_8021q_unregister:
390         dsa_tag_8021q_unregister(ds);
391         return err;
392 }
393
394 static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu)
395 {
396         struct ocelot *ocelot = ds->priv;
397         struct dsa_port *dp;
398         int err;
399
400         err = felix_update_trapping_destinations(ds, false);
401         if (err)
402                 dev_err(ds->dev, "felix_teardown_mmio_filtering returned %d",
403                         err);
404
405         dsa_tag_8021q_unregister(ds);
406
407         dsa_switch_for_each_available_port(dp, ds) {
408                 /* Restore the logic from ocelot_init:
409                  * do not forward BPDU frames to the front ports.
410                  */
411                 ocelot_write_gix(ocelot,
412                                  ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
413                                  ANA_PORT_CPU_FWD_BPDU_CFG,
414                                  dp->index);
415         }
416
417         felix_8021q_cpu_port_deinit(ocelot, cpu);
418 }
419
420 /* The CPU port module is connected to the Node Processor Interface (NPI). This
421  * is the mode through which frames can be injected from and extracted to an
422  * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU
423  * running Linux, and this forms a DSA setup together with the enetc or fman
424  * DSA master.
425  */
426 static void felix_npi_port_init(struct ocelot *ocelot, int port)
427 {
428         ocelot->npi = port;
429
430         ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
431                      QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
432                      QSYS_EXT_CPU_CFG);
433
434         /* NPI port Injection/Extraction configuration */
435         ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
436                             ocelot->npi_xtr_prefix);
437         ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
438                             ocelot->npi_inj_prefix);
439
440         /* Disable transmission of pause frames */
441         ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
442 }
443
444 static void felix_npi_port_deinit(struct ocelot *ocelot, int port)
445 {
446         /* Restore hardware defaults */
447         int unused_port = ocelot->num_phys_ports + 2;
448
449         ocelot->npi = -1;
450
451         ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port),
452                      QSYS_EXT_CPU_CFG);
453
454         ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
455                             OCELOT_TAG_PREFIX_DISABLED);
456         ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
457                             OCELOT_TAG_PREFIX_DISABLED);
458
459         /* Enable transmission of pause frames */
460         ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
461 }
462
463 static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu)
464 {
465         struct ocelot *ocelot = ds->priv;
466         unsigned long cpu_flood;
467
468         felix_npi_port_init(ocelot, cpu);
469
470         /* Include the CPU port module (and indirectly, the NPI port)
471          * in the forwarding mask for unknown unicast - the hardware
472          * default value for ANA_FLOODING_FLD_UNICAST excludes
473          * BIT(ocelot->num_phys_ports), and so does ocelot_init,
474          * since Ocelot relies on whitelisting MAC addresses towards
475          * PGID_CPU.
476          * We do this because DSA does not yet perform RX filtering,
477          * and the NPI port does not perform source address learning,
478          * so traffic sent to Linux is effectively unknown from the
479          * switch's perspective.
480          */
481         cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
482         ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_UC);
483         ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_MC);
484         ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_BC);
485
486         return 0;
487 }
488
489 static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu)
490 {
491         struct ocelot *ocelot = ds->priv;
492
493         felix_npi_port_deinit(ocelot, cpu);
494 }
495
496 static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu,
497                                   enum dsa_tag_protocol proto)
498 {
499         int err;
500
501         switch (proto) {
502         case DSA_TAG_PROTO_SEVILLE:
503         case DSA_TAG_PROTO_OCELOT:
504                 err = felix_setup_tag_npi(ds, cpu);
505                 break;
506         case DSA_TAG_PROTO_OCELOT_8021Q:
507                 err = felix_setup_tag_8021q(ds, cpu);
508                 break;
509         default:
510                 err = -EPROTONOSUPPORT;
511         }
512
513         return err;
514 }
515
516 static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu,
517                                    enum dsa_tag_protocol proto)
518 {
519         switch (proto) {
520         case DSA_TAG_PROTO_SEVILLE:
521         case DSA_TAG_PROTO_OCELOT:
522                 felix_teardown_tag_npi(ds, cpu);
523                 break;
524         case DSA_TAG_PROTO_OCELOT_8021Q:
525                 felix_teardown_tag_8021q(ds, cpu);
526                 break;
527         default:
528                 break;
529         }
530 }
531
532 /* This always leaves the switch in a consistent state, because although the
533  * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
534  * or the restoration is guaranteed to work.
535  */
536 static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu,
537                                      enum dsa_tag_protocol proto)
538 {
539         struct ocelot *ocelot = ds->priv;
540         struct felix *felix = ocelot_to_felix(ocelot);
541         enum dsa_tag_protocol old_proto = felix->tag_proto;
542         int err;
543
544         if (proto != DSA_TAG_PROTO_SEVILLE &&
545             proto != DSA_TAG_PROTO_OCELOT &&
546             proto != DSA_TAG_PROTO_OCELOT_8021Q)
547                 return -EPROTONOSUPPORT;
548
549         felix_del_tag_protocol(ds, cpu, old_proto);
550
551         err = felix_set_tag_protocol(ds, cpu, proto);
552         if (err) {
553                 felix_set_tag_protocol(ds, cpu, old_proto);
554                 return err;
555         }
556
557         felix->tag_proto = proto;
558
559         return 0;
560 }
561
562 static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
563                                                     int port,
564                                                     enum dsa_tag_protocol mp)
565 {
566         struct ocelot *ocelot = ds->priv;
567         struct felix *felix = ocelot_to_felix(ocelot);
568
569         return felix->tag_proto;
570 }
571
572 static int felix_set_ageing_time(struct dsa_switch *ds,
573                                  unsigned int ageing_time)
574 {
575         struct ocelot *ocelot = ds->priv;
576
577         ocelot_set_ageing_time(ocelot, ageing_time);
578
579         return 0;
580 }
581
582 static void felix_port_fast_age(struct dsa_switch *ds, int port)
583 {
584         struct ocelot *ocelot = ds->priv;
585         int err;
586
587         err = ocelot_mact_flush(ocelot, port);
588         if (err)
589                 dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n",
590                         port, ERR_PTR(err));
591 }
592
593 static int felix_fdb_dump(struct dsa_switch *ds, int port,
594                           dsa_fdb_dump_cb_t *cb, void *data)
595 {
596         struct ocelot *ocelot = ds->priv;
597
598         return ocelot_fdb_dump(ocelot, port, cb, data);
599 }
600
601 static int felix_fdb_add(struct dsa_switch *ds, int port,
602                          const unsigned char *addr, u16 vid)
603 {
604         struct ocelot *ocelot = ds->priv;
605
606         return ocelot_fdb_add(ocelot, port, addr, vid);
607 }
608
609 static int felix_fdb_del(struct dsa_switch *ds, int port,
610                          const unsigned char *addr, u16 vid)
611 {
612         struct ocelot *ocelot = ds->priv;
613
614         return ocelot_fdb_del(ocelot, port, addr, vid);
615 }
616
617 static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag,
618                              const unsigned char *addr, u16 vid)
619 {
620         struct ocelot *ocelot = ds->priv;
621
622         return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid);
623 }
624
625 static int felix_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag lag,
626                              const unsigned char *addr, u16 vid)
627 {
628         struct ocelot *ocelot = ds->priv;
629
630         return ocelot_lag_fdb_del(ocelot, lag.dev, addr, vid);
631 }
632
633 static int felix_mdb_add(struct dsa_switch *ds, int port,
634                          const struct switchdev_obj_port_mdb *mdb)
635 {
636         struct ocelot *ocelot = ds->priv;
637
638         return ocelot_port_mdb_add(ocelot, port, mdb);
639 }
640
641 static int felix_mdb_del(struct dsa_switch *ds, int port,
642                          const struct switchdev_obj_port_mdb *mdb)
643 {
644         struct ocelot *ocelot = ds->priv;
645
646         return ocelot_port_mdb_del(ocelot, port, mdb);
647 }
648
649 static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
650                                        u8 state)
651 {
652         struct ocelot *ocelot = ds->priv;
653
654         return ocelot_bridge_stp_state_set(ocelot, port, state);
655 }
656
657 static int felix_pre_bridge_flags(struct dsa_switch *ds, int port,
658                                   struct switchdev_brport_flags val,
659                                   struct netlink_ext_ack *extack)
660 {
661         struct ocelot *ocelot = ds->priv;
662
663         return ocelot_port_pre_bridge_flags(ocelot, port, val);
664 }
665
666 static int felix_bridge_flags(struct dsa_switch *ds, int port,
667                               struct switchdev_brport_flags val,
668                               struct netlink_ext_ack *extack)
669 {
670         struct ocelot *ocelot = ds->priv;
671
672         ocelot_port_bridge_flags(ocelot, port, val);
673
674         return 0;
675 }
676
677 static int felix_bridge_join(struct dsa_switch *ds, int port,
678                              struct dsa_bridge bridge, bool *tx_fwd_offload)
679 {
680         struct ocelot *ocelot = ds->priv;
681
682         ocelot_port_bridge_join(ocelot, port, bridge.dev);
683
684         return 0;
685 }
686
687 static void felix_bridge_leave(struct dsa_switch *ds, int port,
688                                struct dsa_bridge bridge)
689 {
690         struct ocelot *ocelot = ds->priv;
691
692         ocelot_port_bridge_leave(ocelot, port, bridge.dev);
693 }
694
695 static int felix_lag_join(struct dsa_switch *ds, int port,
696                           struct dsa_lag lag,
697                           struct netdev_lag_upper_info *info)
698 {
699         struct ocelot *ocelot = ds->priv;
700
701         return ocelot_port_lag_join(ocelot, port, lag.dev, info);
702 }
703
704 static int felix_lag_leave(struct dsa_switch *ds, int port,
705                            struct dsa_lag lag)
706 {
707         struct ocelot *ocelot = ds->priv;
708
709         ocelot_port_lag_leave(ocelot, port, lag.dev);
710
711         return 0;
712 }
713
714 static int felix_lag_change(struct dsa_switch *ds, int port)
715 {
716         struct dsa_port *dp = dsa_to_port(ds, port);
717         struct ocelot *ocelot = ds->priv;
718
719         ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
720
721         return 0;
722 }
723
724 static int felix_vlan_prepare(struct dsa_switch *ds, int port,
725                               const struct switchdev_obj_port_vlan *vlan,
726                               struct netlink_ext_ack *extack)
727 {
728         struct ocelot *ocelot = ds->priv;
729         u16 flags = vlan->flags;
730
731         /* Ocelot switches copy frames as-is to the CPU, so the flags:
732          * egress-untagged or not, pvid or not, make no difference. This
733          * behavior is already better than what DSA just tries to approximate
734          * when it installs the VLAN with the same flags on the CPU port.
735          * Just accept any configuration, and don't let ocelot deny installing
736          * multiple native VLANs on the NPI port, because the switch doesn't
737          * look at the port tag settings towards the NPI interface anyway.
738          */
739         if (port == ocelot->npi)
740                 return 0;
741
742         return ocelot_vlan_prepare(ocelot, port, vlan->vid,
743                                    flags & BRIDGE_VLAN_INFO_PVID,
744                                    flags & BRIDGE_VLAN_INFO_UNTAGGED,
745                                    extack);
746 }
747
748 static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
749                                 struct netlink_ext_ack *extack)
750 {
751         struct ocelot *ocelot = ds->priv;
752
753         return ocelot_port_vlan_filtering(ocelot, port, enabled, extack);
754 }
755
756 static int felix_vlan_add(struct dsa_switch *ds, int port,
757                           const struct switchdev_obj_port_vlan *vlan,
758                           struct netlink_ext_ack *extack)
759 {
760         struct ocelot *ocelot = ds->priv;
761         u16 flags = vlan->flags;
762         int err;
763
764         err = felix_vlan_prepare(ds, port, vlan, extack);
765         if (err)
766                 return err;
767
768         return ocelot_vlan_add(ocelot, port, vlan->vid,
769                                flags & BRIDGE_VLAN_INFO_PVID,
770                                flags & BRIDGE_VLAN_INFO_UNTAGGED);
771 }
772
773 static int felix_vlan_del(struct dsa_switch *ds, int port,
774                           const struct switchdev_obj_port_vlan *vlan)
775 {
776         struct ocelot *ocelot = ds->priv;
777
778         return ocelot_vlan_del(ocelot, port, vlan->vid);
779 }
780
781 static void felix_phylink_get_caps(struct dsa_switch *ds, int port,
782                                    struct phylink_config *config)
783 {
784         struct ocelot *ocelot = ds->priv;
785
786         __set_bit(ocelot->ports[port]->phy_mode,
787                   config->supported_interfaces);
788 }
789
790 static void felix_phylink_validate(struct dsa_switch *ds, int port,
791                                    unsigned long *supported,
792                                    struct phylink_link_state *state)
793 {
794         struct ocelot *ocelot = ds->priv;
795         struct felix *felix = ocelot_to_felix(ocelot);
796
797         if (felix->info->phylink_validate)
798                 felix->info->phylink_validate(ocelot, port, supported, state);
799 }
800
801 static struct phylink_pcs *felix_phylink_mac_select_pcs(struct dsa_switch *ds,
802                                                         int port,
803                                                         phy_interface_t iface)
804 {
805         struct ocelot *ocelot = ds->priv;
806         struct felix *felix = ocelot_to_felix(ocelot);
807         struct phylink_pcs *pcs = NULL;
808
809         if (felix->pcs && felix->pcs[port])
810                 pcs = felix->pcs[port];
811
812         return pcs;
813 }
814
815 static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
816                                         unsigned int link_an_mode,
817                                         phy_interface_t interface)
818 {
819         struct ocelot *ocelot = ds->priv;
820
821         ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
822                                      FELIX_MAC_QUIRKS);
823 }
824
825 static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
826                                       unsigned int link_an_mode,
827                                       phy_interface_t interface,
828                                       struct phy_device *phydev,
829                                       int speed, int duplex,
830                                       bool tx_pause, bool rx_pause)
831 {
832         struct ocelot *ocelot = ds->priv;
833         struct felix *felix = ocelot_to_felix(ocelot);
834
835         ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
836                                    interface, speed, duplex, tx_pause, rx_pause,
837                                    FELIX_MAC_QUIRKS);
838
839         if (felix->info->port_sched_speed_set)
840                 felix->info->port_sched_speed_set(ocelot, port, speed);
841 }
842
843 static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
844 {
845         int i;
846
847         ocelot_rmw_gix(ocelot,
848                        ANA_PORT_QOS_CFG_QOS_PCP_ENA,
849                        ANA_PORT_QOS_CFG_QOS_PCP_ENA,
850                        ANA_PORT_QOS_CFG,
851                        port);
852
853         for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
854                 ocelot_rmw_ix(ocelot,
855                               (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
856                               ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
857                               ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
858                               ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
859                               ANA_PORT_PCP_DEI_MAP,
860                               port, i);
861         }
862 }
863
864 static void felix_get_strings(struct dsa_switch *ds, int port,
865                               u32 stringset, u8 *data)
866 {
867         struct ocelot *ocelot = ds->priv;
868
869         return ocelot_get_strings(ocelot, port, stringset, data);
870 }
871
872 static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
873 {
874         struct ocelot *ocelot = ds->priv;
875
876         ocelot_get_ethtool_stats(ocelot, port, data);
877 }
878
879 static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
880 {
881         struct ocelot *ocelot = ds->priv;
882
883         return ocelot_get_sset_count(ocelot, port, sset);
884 }
885
886 static int felix_get_ts_info(struct dsa_switch *ds, int port,
887                              struct ethtool_ts_info *info)
888 {
889         struct ocelot *ocelot = ds->priv;
890
891         return ocelot_get_ts_info(ocelot, port, info);
892 }
893
894 static int felix_parse_ports_node(struct felix *felix,
895                                   struct device_node *ports_node,
896                                   phy_interface_t *port_phy_modes)
897 {
898         struct ocelot *ocelot = &felix->ocelot;
899         struct device *dev = felix->ocelot.dev;
900         struct device_node *child;
901
902         for_each_available_child_of_node(ports_node, child) {
903                 phy_interface_t phy_mode;
904                 u32 port;
905                 int err;
906
907                 /* Get switch port number from DT */
908                 if (of_property_read_u32(child, "reg", &port) < 0) {
909                         dev_err(dev, "Port number not defined in device tree "
910                                 "(property \"reg\")\n");
911                         of_node_put(child);
912                         return -ENODEV;
913                 }
914
915                 /* Get PHY mode from DT */
916                 err = of_get_phy_mode(child, &phy_mode);
917                 if (err) {
918                         dev_err(dev, "Failed to read phy-mode or "
919                                 "phy-interface-type property for port %d\n",
920                                 port);
921                         of_node_put(child);
922                         return -ENODEV;
923                 }
924
925                 err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode);
926                 if (err < 0) {
927                         dev_err(dev, "Unsupported PHY mode %s on port %d\n",
928                                 phy_modes(phy_mode), port);
929                         of_node_put(child);
930                         return err;
931                 }
932
933                 port_phy_modes[port] = phy_mode;
934         }
935
936         return 0;
937 }
938
939 static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
940 {
941         struct device *dev = felix->ocelot.dev;
942         struct device_node *switch_node;
943         struct device_node *ports_node;
944         int err;
945
946         switch_node = dev->of_node;
947
948         ports_node = of_get_child_by_name(switch_node, "ports");
949         if (!ports_node)
950                 ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
951         if (!ports_node) {
952                 dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n");
953                 return -ENODEV;
954         }
955
956         err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
957         of_node_put(ports_node);
958
959         return err;
960 }
961
962 static int felix_init_structs(struct felix *felix, int num_phys_ports)
963 {
964         struct ocelot *ocelot = &felix->ocelot;
965         phy_interface_t *port_phy_modes;
966         struct resource res;
967         int port, i, err;
968
969         ocelot->num_phys_ports = num_phys_ports;
970         ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
971                                      sizeof(struct ocelot_port *), GFP_KERNEL);
972         if (!ocelot->ports)
973                 return -ENOMEM;
974
975         ocelot->map             = felix->info->map;
976         ocelot->stats_layout    = felix->info->stats_layout;
977         ocelot->num_stats       = felix->info->num_stats;
978         ocelot->num_mact_rows   = felix->info->num_mact_rows;
979         ocelot->vcap            = felix->info->vcap;
980         ocelot->vcap_pol.base   = felix->info->vcap_pol_base;
981         ocelot->vcap_pol.max    = felix->info->vcap_pol_max;
982         ocelot->vcap_pol.base2  = felix->info->vcap_pol_base2;
983         ocelot->vcap_pol.max2   = felix->info->vcap_pol_max2;
984         ocelot->ops             = felix->info->ops;
985         ocelot->npi_inj_prefix  = OCELOT_TAG_PREFIX_SHORT;
986         ocelot->npi_xtr_prefix  = OCELOT_TAG_PREFIX_SHORT;
987         ocelot->devlink         = felix->ds->devlink;
988
989         port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
990                                  GFP_KERNEL);
991         if (!port_phy_modes)
992                 return -ENOMEM;
993
994         err = felix_parse_dt(felix, port_phy_modes);
995         if (err) {
996                 kfree(port_phy_modes);
997                 return err;
998         }
999
1000         for (i = 0; i < TARGET_MAX; i++) {
1001                 struct regmap *target;
1002
1003                 if (!felix->info->target_io_res[i].name)
1004                         continue;
1005
1006                 memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
1007                 res.flags = IORESOURCE_MEM;
1008                 res.start += felix->switch_base;
1009                 res.end += felix->switch_base;
1010
1011                 target = felix->info->init_regmap(ocelot, &res);
1012                 if (IS_ERR(target)) {
1013                         dev_err(ocelot->dev,
1014                                 "Failed to map device memory space\n");
1015                         kfree(port_phy_modes);
1016                         return PTR_ERR(target);
1017                 }
1018
1019                 ocelot->targets[i] = target;
1020         }
1021
1022         err = ocelot_regfields_init(ocelot, felix->info->regfields);
1023         if (err) {
1024                 dev_err(ocelot->dev, "failed to init reg fields map\n");
1025                 kfree(port_phy_modes);
1026                 return err;
1027         }
1028
1029         for (port = 0; port < num_phys_ports; port++) {
1030                 struct ocelot_port *ocelot_port;
1031                 struct regmap *target;
1032
1033                 ocelot_port = devm_kzalloc(ocelot->dev,
1034                                            sizeof(struct ocelot_port),
1035                                            GFP_KERNEL);
1036                 if (!ocelot_port) {
1037                         dev_err(ocelot->dev,
1038                                 "failed to allocate port memory\n");
1039                         kfree(port_phy_modes);
1040                         return -ENOMEM;
1041                 }
1042
1043                 memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
1044                 res.flags = IORESOURCE_MEM;
1045                 res.start += felix->switch_base;
1046                 res.end += felix->switch_base;
1047
1048                 target = felix->info->init_regmap(ocelot, &res);
1049                 if (IS_ERR(target)) {
1050                         dev_err(ocelot->dev,
1051                                 "Failed to map memory space for port %d\n",
1052                                 port);
1053                         kfree(port_phy_modes);
1054                         return PTR_ERR(target);
1055                 }
1056
1057                 ocelot_port->phy_mode = port_phy_modes[port];
1058                 ocelot_port->ocelot = ocelot;
1059                 ocelot_port->target = target;
1060                 ocelot->ports[port] = ocelot_port;
1061         }
1062
1063         kfree(port_phy_modes);
1064
1065         if (felix->info->mdio_bus_alloc) {
1066                 err = felix->info->mdio_bus_alloc(ocelot);
1067                 if (err < 0)
1068                         return err;
1069         }
1070
1071         return 0;
1072 }
1073
1074 static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port,
1075                                            struct sk_buff *skb)
1076 {
1077         struct ocelot_port *ocelot_port = ocelot->ports[port];
1078         struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
1079         struct sk_buff *skb_match = NULL, *skb_tmp;
1080         unsigned long flags;
1081
1082         if (!clone)
1083                 return;
1084
1085         spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags);
1086
1087         skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) {
1088                 if (skb != clone)
1089                         continue;
1090                 __skb_unlink(skb, &ocelot_port->tx_skbs);
1091                 skb_match = skb;
1092                 break;
1093         }
1094
1095         spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags);
1096
1097         WARN_ONCE(!skb_match,
1098                   "Could not find skb clone in TX timestamping list\n");
1099 }
1100
1101 #define work_to_xmit_work(w) \
1102                 container_of((w), struct felix_deferred_xmit_work, work)
1103
1104 static void felix_port_deferred_xmit(struct kthread_work *work)
1105 {
1106         struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
1107         struct dsa_switch *ds = xmit_work->dp->ds;
1108         struct sk_buff *skb = xmit_work->skb;
1109         u32 rew_op = ocelot_ptp_rew_op(skb);
1110         struct ocelot *ocelot = ds->priv;
1111         int port = xmit_work->dp->index;
1112         int retries = 10;
1113
1114         do {
1115                 if (ocelot_can_inject(ocelot, 0))
1116                         break;
1117
1118                 cpu_relax();
1119         } while (--retries);
1120
1121         if (!retries) {
1122                 dev_err(ocelot->dev, "port %d failed to inject skb\n",
1123                         port);
1124                 ocelot_port_purge_txtstamp_skb(ocelot, port, skb);
1125                 kfree_skb(skb);
1126                 return;
1127         }
1128
1129         ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
1130
1131         consume_skb(skb);
1132         kfree(xmit_work);
1133 }
1134
1135 static int felix_connect_tag_protocol(struct dsa_switch *ds,
1136                                       enum dsa_tag_protocol proto)
1137 {
1138         struct ocelot_8021q_tagger_data *tagger_data;
1139
1140         switch (proto) {
1141         case DSA_TAG_PROTO_OCELOT_8021Q:
1142                 tagger_data = ocelot_8021q_tagger_data(ds);
1143                 tagger_data->xmit_work_fn = felix_port_deferred_xmit;
1144                 return 0;
1145         case DSA_TAG_PROTO_OCELOT:
1146         case DSA_TAG_PROTO_SEVILLE:
1147                 return 0;
1148         default:
1149                 return -EPROTONOSUPPORT;
1150         }
1151 }
1152
1153 /* Hardware initialization done here so that we can allocate structures with
1154  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
1155  * us to allocate structures twice (leak memory) and map PCI memory twice
1156  * (which will not work).
1157  */
1158 static int felix_setup(struct dsa_switch *ds)
1159 {
1160         struct ocelot *ocelot = ds->priv;
1161         struct felix *felix = ocelot_to_felix(ocelot);
1162         struct dsa_port *dp;
1163         int err;
1164
1165         err = felix_init_structs(felix, ds->num_ports);
1166         if (err)
1167                 return err;
1168
1169         err = ocelot_init(ocelot);
1170         if (err)
1171                 goto out_mdiobus_free;
1172
1173         if (ocelot->ptp) {
1174                 err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
1175                 if (err) {
1176                         dev_err(ocelot->dev,
1177                                 "Timestamp initialization failed\n");
1178                         ocelot->ptp = 0;
1179                 }
1180         }
1181
1182         dsa_switch_for_each_available_port(dp, ds) {
1183                 ocelot_init_port(ocelot, dp->index);
1184
1185                 /* Set the default QoS Classification based on PCP and DEI
1186                  * bits of vlan tag.
1187                  */
1188                 felix_port_qos_map_init(ocelot, dp->index);
1189         }
1190
1191         err = ocelot_devlink_sb_register(ocelot);
1192         if (err)
1193                 goto out_deinit_ports;
1194
1195         dsa_switch_for_each_cpu_port(dp, ds) {
1196                 /* The initial tag protocol is NPI which always returns 0, so
1197                  * there's no real point in checking for errors.
1198                  */
1199                 felix_set_tag_protocol(ds, dp->index, felix->tag_proto);
1200                 break;
1201         }
1202
1203         ds->mtu_enforcement_ingress = true;
1204         ds->assisted_learning_on_cpu_port = true;
1205
1206         return 0;
1207
1208 out_deinit_ports:
1209         dsa_switch_for_each_available_port(dp, ds)
1210                 ocelot_deinit_port(ocelot, dp->index);
1211
1212         ocelot_deinit_timestamp(ocelot);
1213         ocelot_deinit(ocelot);
1214
1215 out_mdiobus_free:
1216         if (felix->info->mdio_bus_free)
1217                 felix->info->mdio_bus_free(ocelot);
1218
1219         return err;
1220 }
1221
1222 static void felix_teardown(struct dsa_switch *ds)
1223 {
1224         struct ocelot *ocelot = ds->priv;
1225         struct felix *felix = ocelot_to_felix(ocelot);
1226         struct dsa_port *dp;
1227
1228         dsa_switch_for_each_cpu_port(dp, ds) {
1229                 felix_del_tag_protocol(ds, dp->index, felix->tag_proto);
1230                 break;
1231         }
1232
1233         dsa_switch_for_each_available_port(dp, ds)
1234                 ocelot_deinit_port(ocelot, dp->index);
1235
1236         ocelot_devlink_sb_unregister(ocelot);
1237         ocelot_deinit_timestamp(ocelot);
1238         ocelot_deinit(ocelot);
1239
1240         if (felix->info->mdio_bus_free)
1241                 felix->info->mdio_bus_free(ocelot);
1242 }
1243
1244 static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
1245                               struct ifreq *ifr)
1246 {
1247         struct ocelot *ocelot = ds->priv;
1248
1249         return ocelot_hwstamp_get(ocelot, port, ifr);
1250 }
1251
1252 static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
1253                               struct ifreq *ifr)
1254 {
1255         struct ocelot *ocelot = ds->priv;
1256         struct felix *felix = ocelot_to_felix(ocelot);
1257         bool using_tag_8021q;
1258         int err;
1259
1260         err = ocelot_hwstamp_set(ocelot, port, ifr);
1261         if (err)
1262                 return err;
1263
1264         using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
1265
1266         return felix_update_trapping_destinations(ds, using_tag_8021q);
1267 }
1268
1269 static bool felix_check_xtr_pkt(struct ocelot *ocelot, unsigned int ptp_type)
1270 {
1271         struct felix *felix = ocelot_to_felix(ocelot);
1272         int err, grp = 0;
1273
1274         if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q)
1275                 return false;
1276
1277         if (!felix->info->quirk_no_xtr_irq)
1278                 return false;
1279
1280         if (ptp_type == PTP_CLASS_NONE)
1281                 return false;
1282
1283         while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
1284                 struct sk_buff *skb;
1285                 unsigned int type;
1286
1287                 err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
1288                 if (err)
1289                         goto out;
1290
1291                 /* We trap to the CPU port module all PTP frames, but
1292                  * felix_rxtstamp() only gets called for event frames.
1293                  * So we need to avoid sending duplicate general
1294                  * message frames by running a second BPF classifier
1295                  * here and dropping those.
1296                  */
1297                 __skb_push(skb, ETH_HLEN);
1298
1299                 type = ptp_classify_raw(skb);
1300
1301                 __skb_pull(skb, ETH_HLEN);
1302
1303                 if (type == PTP_CLASS_NONE) {
1304                         kfree_skb(skb);
1305                         continue;
1306                 }
1307
1308                 netif_rx(skb);
1309         }
1310
1311 out:
1312         if (err < 0)
1313                 ocelot_drain_cpu_queue(ocelot, 0);
1314
1315         return true;
1316 }
1317
1318 static bool felix_rxtstamp(struct dsa_switch *ds, int port,
1319                            struct sk_buff *skb, unsigned int type)
1320 {
1321         u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo;
1322         struct skb_shared_hwtstamps *shhwtstamps;
1323         struct ocelot *ocelot = ds->priv;
1324         struct timespec64 ts;
1325         u32 tstamp_hi;
1326         u64 tstamp;
1327
1328         /* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb
1329          * for RX timestamping. Then free it, and poll for its copy through
1330          * MMIO in the CPU port module, and inject that into the stack from
1331          * ocelot_xtr_poll().
1332          */
1333         if (felix_check_xtr_pkt(ocelot, type)) {
1334                 kfree_skb(skb);
1335                 return true;
1336         }
1337
1338         ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1339         tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1340
1341         tstamp_hi = tstamp >> 32;
1342         if ((tstamp & 0xffffffff) < tstamp_lo)
1343                 tstamp_hi--;
1344
1345         tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
1346
1347         shhwtstamps = skb_hwtstamps(skb);
1348         memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1349         shhwtstamps->hwtstamp = tstamp;
1350         return false;
1351 }
1352
1353 static void felix_txtstamp(struct dsa_switch *ds, int port,
1354                            struct sk_buff *skb)
1355 {
1356         struct ocelot *ocelot = ds->priv;
1357         struct sk_buff *clone = NULL;
1358
1359         if (!ocelot->ptp)
1360                 return;
1361
1362         if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
1363                 dev_err_ratelimited(ds->dev,
1364                                     "port %d delivering skb without TX timestamp\n",
1365                                     port);
1366                 return;
1367         }
1368
1369         if (clone)
1370                 OCELOT_SKB_CB(skb)->clone = clone;
1371 }
1372
1373 static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
1374 {
1375         struct ocelot *ocelot = ds->priv;
1376
1377         ocelot_port_set_maxlen(ocelot, port, new_mtu);
1378
1379         return 0;
1380 }
1381
1382 static int felix_get_max_mtu(struct dsa_switch *ds, int port)
1383 {
1384         struct ocelot *ocelot = ds->priv;
1385
1386         return ocelot_get_max_mtu(ocelot, port);
1387 }
1388
1389 static int felix_cls_flower_add(struct dsa_switch *ds, int port,
1390                                 struct flow_cls_offload *cls, bool ingress)
1391 {
1392         struct ocelot *ocelot = ds->priv;
1393         struct felix *felix = ocelot_to_felix(ocelot);
1394         bool using_tag_8021q;
1395         int err;
1396
1397         err = ocelot_cls_flower_replace(ocelot, port, cls, ingress);
1398         if (err)
1399                 return err;
1400
1401         using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
1402
1403         return felix_update_trapping_destinations(ds, using_tag_8021q);
1404 }
1405
1406 static int felix_cls_flower_del(struct dsa_switch *ds, int port,
1407                                 struct flow_cls_offload *cls, bool ingress)
1408 {
1409         struct ocelot *ocelot = ds->priv;
1410
1411         return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
1412 }
1413
1414 static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
1415                                   struct flow_cls_offload *cls, bool ingress)
1416 {
1417         struct ocelot *ocelot = ds->priv;
1418
1419         return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
1420 }
1421
1422 static int felix_port_policer_add(struct dsa_switch *ds, int port,
1423                                   struct dsa_mall_policer_tc_entry *policer)
1424 {
1425         struct ocelot *ocelot = ds->priv;
1426         struct ocelot_policer pol = {
1427                 .rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
1428                 .burst = policer->burst,
1429         };
1430
1431         return ocelot_port_policer_add(ocelot, port, &pol);
1432 }
1433
1434 static void felix_port_policer_del(struct dsa_switch *ds, int port)
1435 {
1436         struct ocelot *ocelot = ds->priv;
1437
1438         ocelot_port_policer_del(ocelot, port);
1439 }
1440
1441 static int felix_port_setup_tc(struct dsa_switch *ds, int port,
1442                                enum tc_setup_type type,
1443                                void *type_data)
1444 {
1445         struct ocelot *ocelot = ds->priv;
1446         struct felix *felix = ocelot_to_felix(ocelot);
1447
1448         if (felix->info->port_setup_tc)
1449                 return felix->info->port_setup_tc(ds, port, type, type_data);
1450         else
1451                 return -EOPNOTSUPP;
1452 }
1453
1454 static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
1455                              u16 pool_index,
1456                              struct devlink_sb_pool_info *pool_info)
1457 {
1458         struct ocelot *ocelot = ds->priv;
1459
1460         return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
1461 }
1462
1463 static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
1464                              u16 pool_index, u32 size,
1465                              enum devlink_sb_threshold_type threshold_type,
1466                              struct netlink_ext_ack *extack)
1467 {
1468         struct ocelot *ocelot = ds->priv;
1469
1470         return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
1471                                   threshold_type, extack);
1472 }
1473
1474 static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
1475                                   unsigned int sb_index, u16 pool_index,
1476                                   u32 *p_threshold)
1477 {
1478         struct ocelot *ocelot = ds->priv;
1479
1480         return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
1481                                        p_threshold);
1482 }
1483
1484 static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
1485                                   unsigned int sb_index, u16 pool_index,
1486                                   u32 threshold, struct netlink_ext_ack *extack)
1487 {
1488         struct ocelot *ocelot = ds->priv;
1489
1490         return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
1491                                        threshold, extack);
1492 }
1493
1494 static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
1495                                      unsigned int sb_index, u16 tc_index,
1496                                      enum devlink_sb_pool_type pool_type,
1497                                      u16 *p_pool_index, u32 *p_threshold)
1498 {
1499         struct ocelot *ocelot = ds->priv;
1500
1501         return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
1502                                           pool_type, p_pool_index,
1503                                           p_threshold);
1504 }
1505
1506 static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
1507                                      unsigned int sb_index, u16 tc_index,
1508                                      enum devlink_sb_pool_type pool_type,
1509                                      u16 pool_index, u32 threshold,
1510                                      struct netlink_ext_ack *extack)
1511 {
1512         struct ocelot *ocelot = ds->priv;
1513
1514         return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
1515                                           pool_type, pool_index, threshold,
1516                                           extack);
1517 }
1518
1519 static int felix_sb_occ_snapshot(struct dsa_switch *ds,
1520                                  unsigned int sb_index)
1521 {
1522         struct ocelot *ocelot = ds->priv;
1523
1524         return ocelot_sb_occ_snapshot(ocelot, sb_index);
1525 }
1526
1527 static int felix_sb_occ_max_clear(struct dsa_switch *ds,
1528                                   unsigned int sb_index)
1529 {
1530         struct ocelot *ocelot = ds->priv;
1531
1532         return ocelot_sb_occ_max_clear(ocelot, sb_index);
1533 }
1534
1535 static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
1536                                       unsigned int sb_index, u16 pool_index,
1537                                       u32 *p_cur, u32 *p_max)
1538 {
1539         struct ocelot *ocelot = ds->priv;
1540
1541         return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
1542                                            p_cur, p_max);
1543 }
1544
1545 static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
1546                                          unsigned int sb_index, u16 tc_index,
1547                                          enum devlink_sb_pool_type pool_type,
1548                                          u32 *p_cur, u32 *p_max)
1549 {
1550         struct ocelot *ocelot = ds->priv;
1551
1552         return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
1553                                               pool_type, p_cur, p_max);
1554 }
1555
1556 static int felix_mrp_add(struct dsa_switch *ds, int port,
1557                          const struct switchdev_obj_mrp *mrp)
1558 {
1559         struct ocelot *ocelot = ds->priv;
1560
1561         return ocelot_mrp_add(ocelot, port, mrp);
1562 }
1563
1564 static int felix_mrp_del(struct dsa_switch *ds, int port,
1565                          const struct switchdev_obj_mrp *mrp)
1566 {
1567         struct ocelot *ocelot = ds->priv;
1568
1569         return ocelot_mrp_add(ocelot, port, mrp);
1570 }
1571
1572 static int
1573 felix_mrp_add_ring_role(struct dsa_switch *ds, int port,
1574                         const struct switchdev_obj_ring_role_mrp *mrp)
1575 {
1576         struct ocelot *ocelot = ds->priv;
1577
1578         return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1579 }
1580
1581 static int
1582 felix_mrp_del_ring_role(struct dsa_switch *ds, int port,
1583                         const struct switchdev_obj_ring_role_mrp *mrp)
1584 {
1585         struct ocelot *ocelot = ds->priv;
1586
1587         return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1588 }
1589
1590 const struct dsa_switch_ops felix_switch_ops = {
1591         .get_tag_protocol               = felix_get_tag_protocol,
1592         .change_tag_protocol            = felix_change_tag_protocol,
1593         .connect_tag_protocol           = felix_connect_tag_protocol,
1594         .setup                          = felix_setup,
1595         .teardown                       = felix_teardown,
1596         .set_ageing_time                = felix_set_ageing_time,
1597         .get_strings                    = felix_get_strings,
1598         .get_ethtool_stats              = felix_get_ethtool_stats,
1599         .get_sset_count                 = felix_get_sset_count,
1600         .get_ts_info                    = felix_get_ts_info,
1601         .phylink_get_caps               = felix_phylink_get_caps,
1602         .phylink_validate               = felix_phylink_validate,
1603         .phylink_mac_select_pcs         = felix_phylink_mac_select_pcs,
1604         .phylink_mac_link_down          = felix_phylink_mac_link_down,
1605         .phylink_mac_link_up            = felix_phylink_mac_link_up,
1606         .port_fast_age                  = felix_port_fast_age,
1607         .port_fdb_dump                  = felix_fdb_dump,
1608         .port_fdb_add                   = felix_fdb_add,
1609         .port_fdb_del                   = felix_fdb_del,
1610         .lag_fdb_add                    = felix_lag_fdb_add,
1611         .lag_fdb_del                    = felix_lag_fdb_del,
1612         .port_mdb_add                   = felix_mdb_add,
1613         .port_mdb_del                   = felix_mdb_del,
1614         .port_pre_bridge_flags          = felix_pre_bridge_flags,
1615         .port_bridge_flags              = felix_bridge_flags,
1616         .port_bridge_join               = felix_bridge_join,
1617         .port_bridge_leave              = felix_bridge_leave,
1618         .port_lag_join                  = felix_lag_join,
1619         .port_lag_leave                 = felix_lag_leave,
1620         .port_lag_change                = felix_lag_change,
1621         .port_stp_state_set             = felix_bridge_stp_state_set,
1622         .port_vlan_filtering            = felix_vlan_filtering,
1623         .port_vlan_add                  = felix_vlan_add,
1624         .port_vlan_del                  = felix_vlan_del,
1625         .port_hwtstamp_get              = felix_hwtstamp_get,
1626         .port_hwtstamp_set              = felix_hwtstamp_set,
1627         .port_rxtstamp                  = felix_rxtstamp,
1628         .port_txtstamp                  = felix_txtstamp,
1629         .port_change_mtu                = felix_change_mtu,
1630         .port_max_mtu                   = felix_get_max_mtu,
1631         .port_policer_add               = felix_port_policer_add,
1632         .port_policer_del               = felix_port_policer_del,
1633         .cls_flower_add                 = felix_cls_flower_add,
1634         .cls_flower_del                 = felix_cls_flower_del,
1635         .cls_flower_stats               = felix_cls_flower_stats,
1636         .port_setup_tc                  = felix_port_setup_tc,
1637         .devlink_sb_pool_get            = felix_sb_pool_get,
1638         .devlink_sb_pool_set            = felix_sb_pool_set,
1639         .devlink_sb_port_pool_get       = felix_sb_port_pool_get,
1640         .devlink_sb_port_pool_set       = felix_sb_port_pool_set,
1641         .devlink_sb_tc_pool_bind_get    = felix_sb_tc_pool_bind_get,
1642         .devlink_sb_tc_pool_bind_set    = felix_sb_tc_pool_bind_set,
1643         .devlink_sb_occ_snapshot        = felix_sb_occ_snapshot,
1644         .devlink_sb_occ_max_clear       = felix_sb_occ_max_clear,
1645         .devlink_sb_occ_port_pool_get   = felix_sb_occ_port_pool_get,
1646         .devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
1647         .port_mrp_add                   = felix_mrp_add,
1648         .port_mrp_del                   = felix_mrp_del,
1649         .port_mrp_add_ring_role         = felix_mrp_add_ring_role,
1650         .port_mrp_del_ring_role         = felix_mrp_del_ring_role,
1651         .tag_8021q_vlan_add             = felix_tag_8021q_vlan_add,
1652         .tag_8021q_vlan_del             = felix_tag_8021q_vlan_del,
1653 };
1654
1655 struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
1656 {
1657         struct felix *felix = ocelot_to_felix(ocelot);
1658         struct dsa_switch *ds = felix->ds;
1659
1660         if (!dsa_is_user_port(ds, port))
1661                 return NULL;
1662
1663         return dsa_to_port(ds, port)->slave;
1664 }
1665
1666 int felix_netdev_to_port(struct net_device *dev)
1667 {
1668         struct dsa_port *dp;
1669
1670         dp = dsa_port_from_netdev(dev);
1671         if (IS_ERR(dp))
1672                 return -EINVAL;
1673
1674         return dp->index;
1675 }