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