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