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