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