xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision 12382d11)
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_strings(struct dsa_switch *ds, int port,
1038 			      u32 stringset, u8 *data)
1039 {
1040 	struct ocelot *ocelot = ds->priv;
1041 
1042 	return ocelot_get_strings(ocelot, port, stringset, data);
1043 }
1044 
1045 static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
1046 {
1047 	struct ocelot *ocelot = ds->priv;
1048 
1049 	ocelot_get_ethtool_stats(ocelot, port, data);
1050 }
1051 
1052 static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
1053 {
1054 	struct ocelot *ocelot = ds->priv;
1055 
1056 	return ocelot_get_sset_count(ocelot, port, sset);
1057 }
1058 
1059 static int felix_get_ts_info(struct dsa_switch *ds, int port,
1060 			     struct ethtool_ts_info *info)
1061 {
1062 	struct ocelot *ocelot = ds->priv;
1063 
1064 	return ocelot_get_ts_info(ocelot, port, info);
1065 }
1066 
1067 static const u32 felix_phy_match_table[PHY_INTERFACE_MODE_MAX] = {
1068 	[PHY_INTERFACE_MODE_INTERNAL] = OCELOT_PORT_MODE_INTERNAL,
1069 	[PHY_INTERFACE_MODE_SGMII] = OCELOT_PORT_MODE_SGMII,
1070 	[PHY_INTERFACE_MODE_QSGMII] = OCELOT_PORT_MODE_QSGMII,
1071 	[PHY_INTERFACE_MODE_USXGMII] = OCELOT_PORT_MODE_USXGMII,
1072 	[PHY_INTERFACE_MODE_1000BASEX] = OCELOT_PORT_MODE_1000BASEX,
1073 	[PHY_INTERFACE_MODE_2500BASEX] = OCELOT_PORT_MODE_2500BASEX,
1074 };
1075 
1076 static int felix_validate_phy_mode(struct felix *felix, int port,
1077 				   phy_interface_t phy_mode)
1078 {
1079 	u32 modes = felix->info->port_modes[port];
1080 
1081 	if (felix_phy_match_table[phy_mode] & modes)
1082 		return 0;
1083 	return -EOPNOTSUPP;
1084 }
1085 
1086 static int felix_parse_ports_node(struct felix *felix,
1087 				  struct device_node *ports_node,
1088 				  phy_interface_t *port_phy_modes)
1089 {
1090 	struct device *dev = felix->ocelot.dev;
1091 	struct device_node *child;
1092 
1093 	for_each_available_child_of_node(ports_node, child) {
1094 		phy_interface_t phy_mode;
1095 		u32 port;
1096 		int err;
1097 
1098 		/* Get switch port number from DT */
1099 		if (of_property_read_u32(child, "reg", &port) < 0) {
1100 			dev_err(dev, "Port number not defined in device tree "
1101 				"(property \"reg\")\n");
1102 			of_node_put(child);
1103 			return -ENODEV;
1104 		}
1105 
1106 		/* Get PHY mode from DT */
1107 		err = of_get_phy_mode(child, &phy_mode);
1108 		if (err) {
1109 			dev_err(dev, "Failed to read phy-mode or "
1110 				"phy-interface-type property for port %d\n",
1111 				port);
1112 			of_node_put(child);
1113 			return -ENODEV;
1114 		}
1115 
1116 		err = felix_validate_phy_mode(felix, port, phy_mode);
1117 		if (err < 0) {
1118 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
1119 				phy_modes(phy_mode), port);
1120 			of_node_put(child);
1121 			return err;
1122 		}
1123 
1124 		port_phy_modes[port] = phy_mode;
1125 	}
1126 
1127 	return 0;
1128 }
1129 
1130 static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
1131 {
1132 	struct device *dev = felix->ocelot.dev;
1133 	struct device_node *switch_node;
1134 	struct device_node *ports_node;
1135 	int err;
1136 
1137 	switch_node = dev->of_node;
1138 
1139 	ports_node = of_get_child_by_name(switch_node, "ports");
1140 	if (!ports_node)
1141 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
1142 	if (!ports_node) {
1143 		dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n");
1144 		return -ENODEV;
1145 	}
1146 
1147 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
1148 	of_node_put(ports_node);
1149 
1150 	return err;
1151 }
1152 
1153 static int felix_init_structs(struct felix *felix, int num_phys_ports)
1154 {
1155 	struct ocelot *ocelot = &felix->ocelot;
1156 	phy_interface_t *port_phy_modes;
1157 	struct resource res;
1158 	int port, i, err;
1159 
1160 	ocelot->num_phys_ports = num_phys_ports;
1161 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
1162 				     sizeof(struct ocelot_port *), GFP_KERNEL);
1163 	if (!ocelot->ports)
1164 		return -ENOMEM;
1165 
1166 	ocelot->map		= felix->info->map;
1167 	ocelot->stats_layout	= felix->info->stats_layout;
1168 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
1169 	ocelot->vcap		= felix->info->vcap;
1170 	ocelot->vcap_pol.base	= felix->info->vcap_pol_base;
1171 	ocelot->vcap_pol.max	= felix->info->vcap_pol_max;
1172 	ocelot->vcap_pol.base2	= felix->info->vcap_pol_base2;
1173 	ocelot->vcap_pol.max2	= felix->info->vcap_pol_max2;
1174 	ocelot->ops		= felix->info->ops;
1175 	ocelot->npi_inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
1176 	ocelot->npi_xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
1177 	ocelot->devlink		= felix->ds->devlink;
1178 
1179 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
1180 				 GFP_KERNEL);
1181 	if (!port_phy_modes)
1182 		return -ENOMEM;
1183 
1184 	err = felix_parse_dt(felix, port_phy_modes);
1185 	if (err) {
1186 		kfree(port_phy_modes);
1187 		return err;
1188 	}
1189 
1190 	for (i = 0; i < TARGET_MAX; i++) {
1191 		struct regmap *target;
1192 
1193 		if (!felix->info->target_io_res[i].name)
1194 			continue;
1195 
1196 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
1197 		res.flags = IORESOURCE_MEM;
1198 		res.start += felix->switch_base;
1199 		res.end += felix->switch_base;
1200 
1201 		target = felix->info->init_regmap(ocelot, &res);
1202 		if (IS_ERR(target)) {
1203 			dev_err(ocelot->dev,
1204 				"Failed to map device memory space\n");
1205 			kfree(port_phy_modes);
1206 			return PTR_ERR(target);
1207 		}
1208 
1209 		ocelot->targets[i] = target;
1210 	}
1211 
1212 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
1213 	if (err) {
1214 		dev_err(ocelot->dev, "failed to init reg fields map\n");
1215 		kfree(port_phy_modes);
1216 		return err;
1217 	}
1218 
1219 	for (port = 0; port < num_phys_ports; port++) {
1220 		struct ocelot_port *ocelot_port;
1221 		struct regmap *target;
1222 
1223 		ocelot_port = devm_kzalloc(ocelot->dev,
1224 					   sizeof(struct ocelot_port),
1225 					   GFP_KERNEL);
1226 		if (!ocelot_port) {
1227 			dev_err(ocelot->dev,
1228 				"failed to allocate port memory\n");
1229 			kfree(port_phy_modes);
1230 			return -ENOMEM;
1231 		}
1232 
1233 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
1234 		res.flags = IORESOURCE_MEM;
1235 		res.start += felix->switch_base;
1236 		res.end += felix->switch_base;
1237 
1238 		target = felix->info->init_regmap(ocelot, &res);
1239 		if (IS_ERR(target)) {
1240 			dev_err(ocelot->dev,
1241 				"Failed to map memory space for port %d\n",
1242 				port);
1243 			kfree(port_phy_modes);
1244 			return PTR_ERR(target);
1245 		}
1246 
1247 		ocelot_port->phy_mode = port_phy_modes[port];
1248 		ocelot_port->ocelot = ocelot;
1249 		ocelot_port->target = target;
1250 		ocelot_port->index = port;
1251 		ocelot->ports[port] = ocelot_port;
1252 	}
1253 
1254 	kfree(port_phy_modes);
1255 
1256 	if (felix->info->mdio_bus_alloc) {
1257 		err = felix->info->mdio_bus_alloc(ocelot);
1258 		if (err < 0)
1259 			return err;
1260 	}
1261 
1262 	return 0;
1263 }
1264 
1265 static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port,
1266 					   struct sk_buff *skb)
1267 {
1268 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1269 	struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
1270 	struct sk_buff *skb_match = NULL, *skb_tmp;
1271 	unsigned long flags;
1272 
1273 	if (!clone)
1274 		return;
1275 
1276 	spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags);
1277 
1278 	skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) {
1279 		if (skb != clone)
1280 			continue;
1281 		__skb_unlink(skb, &ocelot_port->tx_skbs);
1282 		skb_match = skb;
1283 		break;
1284 	}
1285 
1286 	spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags);
1287 
1288 	WARN_ONCE(!skb_match,
1289 		  "Could not find skb clone in TX timestamping list\n");
1290 }
1291 
1292 #define work_to_xmit_work(w) \
1293 		container_of((w), struct felix_deferred_xmit_work, work)
1294 
1295 static void felix_port_deferred_xmit(struct kthread_work *work)
1296 {
1297 	struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
1298 	struct dsa_switch *ds = xmit_work->dp->ds;
1299 	struct sk_buff *skb = xmit_work->skb;
1300 	u32 rew_op = ocelot_ptp_rew_op(skb);
1301 	struct ocelot *ocelot = ds->priv;
1302 	int port = xmit_work->dp->index;
1303 	int retries = 10;
1304 
1305 	do {
1306 		if (ocelot_can_inject(ocelot, 0))
1307 			break;
1308 
1309 		cpu_relax();
1310 	} while (--retries);
1311 
1312 	if (!retries) {
1313 		dev_err(ocelot->dev, "port %d failed to inject skb\n",
1314 			port);
1315 		ocelot_port_purge_txtstamp_skb(ocelot, port, skb);
1316 		kfree_skb(skb);
1317 		return;
1318 	}
1319 
1320 	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
1321 
1322 	consume_skb(skb);
1323 	kfree(xmit_work);
1324 }
1325 
1326 static int felix_connect_tag_protocol(struct dsa_switch *ds,
1327 				      enum dsa_tag_protocol proto)
1328 {
1329 	struct ocelot_8021q_tagger_data *tagger_data;
1330 
1331 	switch (proto) {
1332 	case DSA_TAG_PROTO_OCELOT_8021Q:
1333 		tagger_data = ocelot_8021q_tagger_data(ds);
1334 		tagger_data->xmit_work_fn = felix_port_deferred_xmit;
1335 		return 0;
1336 	case DSA_TAG_PROTO_OCELOT:
1337 	case DSA_TAG_PROTO_SEVILLE:
1338 		return 0;
1339 	default:
1340 		return -EPROTONOSUPPORT;
1341 	}
1342 }
1343 
1344 /* Hardware initialization done here so that we can allocate structures with
1345  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
1346  * us to allocate structures twice (leak memory) and map PCI memory twice
1347  * (which will not work).
1348  */
1349 static int felix_setup(struct dsa_switch *ds)
1350 {
1351 	struct ocelot *ocelot = ds->priv;
1352 	struct felix *felix = ocelot_to_felix(ocelot);
1353 	struct dsa_port *dp;
1354 	int err;
1355 
1356 	err = felix_init_structs(felix, ds->num_ports);
1357 	if (err)
1358 		return err;
1359 
1360 	err = ocelot_init(ocelot);
1361 	if (err)
1362 		goto out_mdiobus_free;
1363 
1364 	if (ocelot->ptp) {
1365 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
1366 		if (err) {
1367 			dev_err(ocelot->dev,
1368 				"Timestamp initialization failed\n");
1369 			ocelot->ptp = 0;
1370 		}
1371 	}
1372 
1373 	dsa_switch_for_each_available_port(dp, ds) {
1374 		ocelot_init_port(ocelot, dp->index);
1375 
1376 		/* Set the default QoS Classification based on PCP and DEI
1377 		 * bits of vlan tag.
1378 		 */
1379 		felix_port_qos_map_init(ocelot, dp->index);
1380 	}
1381 
1382 	err = ocelot_devlink_sb_register(ocelot);
1383 	if (err)
1384 		goto out_deinit_ports;
1385 
1386 	/* The initial tag protocol is NPI which won't fail during initial
1387 	 * setup, there's no real point in checking for errors.
1388 	 */
1389 	felix_change_tag_protocol(ds, felix->tag_proto);
1390 
1391 	ds->mtu_enforcement_ingress = true;
1392 	ds->assisted_learning_on_cpu_port = true;
1393 	ds->fdb_isolation = true;
1394 	ds->max_num_bridges = ds->num_ports;
1395 
1396 	return 0;
1397 
1398 out_deinit_ports:
1399 	dsa_switch_for_each_available_port(dp, ds)
1400 		ocelot_deinit_port(ocelot, dp->index);
1401 
1402 	ocelot_deinit_timestamp(ocelot);
1403 	ocelot_deinit(ocelot);
1404 
1405 out_mdiobus_free:
1406 	if (felix->info->mdio_bus_free)
1407 		felix->info->mdio_bus_free(ocelot);
1408 
1409 	return err;
1410 }
1411 
1412 static void felix_teardown(struct dsa_switch *ds)
1413 {
1414 	struct ocelot *ocelot = ds->priv;
1415 	struct felix *felix = ocelot_to_felix(ocelot);
1416 	struct dsa_port *dp;
1417 
1418 	if (felix->tag_proto_ops)
1419 		felix->tag_proto_ops->teardown(ds);
1420 
1421 	dsa_switch_for_each_available_port(dp, ds)
1422 		ocelot_deinit_port(ocelot, dp->index);
1423 
1424 	ocelot_devlink_sb_unregister(ocelot);
1425 	ocelot_deinit_timestamp(ocelot);
1426 	ocelot_deinit(ocelot);
1427 
1428 	if (felix->info->mdio_bus_free)
1429 		felix->info->mdio_bus_free(ocelot);
1430 }
1431 
1432 static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
1433 			      struct ifreq *ifr)
1434 {
1435 	struct ocelot *ocelot = ds->priv;
1436 
1437 	return ocelot_hwstamp_get(ocelot, port, ifr);
1438 }
1439 
1440 static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
1441 			      struct ifreq *ifr)
1442 {
1443 	struct ocelot *ocelot = ds->priv;
1444 	struct felix *felix = ocelot_to_felix(ocelot);
1445 	bool using_tag_8021q;
1446 	int err;
1447 
1448 	err = ocelot_hwstamp_set(ocelot, port, ifr);
1449 	if (err)
1450 		return err;
1451 
1452 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
1453 
1454 	return felix_update_trapping_destinations(ds, using_tag_8021q);
1455 }
1456 
1457 static bool felix_check_xtr_pkt(struct ocelot *ocelot)
1458 {
1459 	struct felix *felix = ocelot_to_felix(ocelot);
1460 	int err = 0, grp = 0;
1461 
1462 	if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q)
1463 		return false;
1464 
1465 	if (!felix->info->quirk_no_xtr_irq)
1466 		return false;
1467 
1468 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
1469 		struct sk_buff *skb;
1470 		unsigned int type;
1471 
1472 		err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
1473 		if (err)
1474 			goto out;
1475 
1476 		/* We trap to the CPU port module all PTP frames, but
1477 		 * felix_rxtstamp() only gets called for event frames.
1478 		 * So we need to avoid sending duplicate general
1479 		 * message frames by running a second BPF classifier
1480 		 * here and dropping those.
1481 		 */
1482 		__skb_push(skb, ETH_HLEN);
1483 
1484 		type = ptp_classify_raw(skb);
1485 
1486 		__skb_pull(skb, ETH_HLEN);
1487 
1488 		if (type == PTP_CLASS_NONE) {
1489 			kfree_skb(skb);
1490 			continue;
1491 		}
1492 
1493 		netif_rx(skb);
1494 	}
1495 
1496 out:
1497 	if (err < 0) {
1498 		dev_err_ratelimited(ocelot->dev,
1499 				    "Error during packet extraction: %pe\n",
1500 				    ERR_PTR(err));
1501 		ocelot_drain_cpu_queue(ocelot, 0);
1502 	}
1503 
1504 	return true;
1505 }
1506 
1507 static bool felix_rxtstamp(struct dsa_switch *ds, int port,
1508 			   struct sk_buff *skb, unsigned int type)
1509 {
1510 	u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo;
1511 	struct skb_shared_hwtstamps *shhwtstamps;
1512 	struct ocelot *ocelot = ds->priv;
1513 	struct timespec64 ts;
1514 	u32 tstamp_hi;
1515 	u64 tstamp;
1516 
1517 	/* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb
1518 	 * for RX timestamping. Then free it, and poll for its copy through
1519 	 * MMIO in the CPU port module, and inject that into the stack from
1520 	 * ocelot_xtr_poll().
1521 	 */
1522 	if (felix_check_xtr_pkt(ocelot)) {
1523 		kfree_skb(skb);
1524 		return true;
1525 	}
1526 
1527 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1528 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1529 
1530 	tstamp_hi = tstamp >> 32;
1531 	if ((tstamp & 0xffffffff) < tstamp_lo)
1532 		tstamp_hi--;
1533 
1534 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
1535 
1536 	shhwtstamps = skb_hwtstamps(skb);
1537 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1538 	shhwtstamps->hwtstamp = tstamp;
1539 	return false;
1540 }
1541 
1542 static void felix_txtstamp(struct dsa_switch *ds, int port,
1543 			   struct sk_buff *skb)
1544 {
1545 	struct ocelot *ocelot = ds->priv;
1546 	struct sk_buff *clone = NULL;
1547 
1548 	if (!ocelot->ptp)
1549 		return;
1550 
1551 	if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
1552 		dev_err_ratelimited(ds->dev,
1553 				    "port %d delivering skb without TX timestamp\n",
1554 				    port);
1555 		return;
1556 	}
1557 
1558 	if (clone)
1559 		OCELOT_SKB_CB(skb)->clone = clone;
1560 }
1561 
1562 static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
1563 {
1564 	struct ocelot *ocelot = ds->priv;
1565 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1566 	struct felix *felix = ocelot_to_felix(ocelot);
1567 
1568 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
1569 
1570 	mutex_lock(&ocelot->tas_lock);
1571 
1572 	if (ocelot_port->taprio && felix->info->tas_guard_bands_update)
1573 		felix->info->tas_guard_bands_update(ocelot, port);
1574 
1575 	mutex_unlock(&ocelot->tas_lock);
1576 
1577 	return 0;
1578 }
1579 
1580 static int felix_get_max_mtu(struct dsa_switch *ds, int port)
1581 {
1582 	struct ocelot *ocelot = ds->priv;
1583 
1584 	return ocelot_get_max_mtu(ocelot, port);
1585 }
1586 
1587 static int felix_cls_flower_add(struct dsa_switch *ds, int port,
1588 				struct flow_cls_offload *cls, bool ingress)
1589 {
1590 	struct ocelot *ocelot = ds->priv;
1591 	struct felix *felix = ocelot_to_felix(ocelot);
1592 	bool using_tag_8021q;
1593 	int err;
1594 
1595 	err = ocelot_cls_flower_replace(ocelot, port, cls, ingress);
1596 	if (err)
1597 		return err;
1598 
1599 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
1600 
1601 	return felix_update_trapping_destinations(ds, using_tag_8021q);
1602 }
1603 
1604 static int felix_cls_flower_del(struct dsa_switch *ds, int port,
1605 				struct flow_cls_offload *cls, bool ingress)
1606 {
1607 	struct ocelot *ocelot = ds->priv;
1608 
1609 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
1610 }
1611 
1612 static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
1613 				  struct flow_cls_offload *cls, bool ingress)
1614 {
1615 	struct ocelot *ocelot = ds->priv;
1616 
1617 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
1618 }
1619 
1620 static int felix_port_policer_add(struct dsa_switch *ds, int port,
1621 				  struct dsa_mall_policer_tc_entry *policer)
1622 {
1623 	struct ocelot *ocelot = ds->priv;
1624 	struct ocelot_policer pol = {
1625 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
1626 		.burst = policer->burst,
1627 	};
1628 
1629 	return ocelot_port_policer_add(ocelot, port, &pol);
1630 }
1631 
1632 static void felix_port_policer_del(struct dsa_switch *ds, int port)
1633 {
1634 	struct ocelot *ocelot = ds->priv;
1635 
1636 	ocelot_port_policer_del(ocelot, port);
1637 }
1638 
1639 static int felix_port_mirror_add(struct dsa_switch *ds, int port,
1640 				 struct dsa_mall_mirror_tc_entry *mirror,
1641 				 bool ingress, struct netlink_ext_ack *extack)
1642 {
1643 	struct ocelot *ocelot = ds->priv;
1644 
1645 	return ocelot_port_mirror_add(ocelot, port, mirror->to_local_port,
1646 				      ingress, extack);
1647 }
1648 
1649 static void felix_port_mirror_del(struct dsa_switch *ds, int port,
1650 				  struct dsa_mall_mirror_tc_entry *mirror)
1651 {
1652 	struct ocelot *ocelot = ds->priv;
1653 
1654 	ocelot_port_mirror_del(ocelot, port, mirror->ingress);
1655 }
1656 
1657 static int felix_port_setup_tc(struct dsa_switch *ds, int port,
1658 			       enum tc_setup_type type,
1659 			       void *type_data)
1660 {
1661 	struct ocelot *ocelot = ds->priv;
1662 	struct felix *felix = ocelot_to_felix(ocelot);
1663 
1664 	if (felix->info->port_setup_tc)
1665 		return felix->info->port_setup_tc(ds, port, type, type_data);
1666 	else
1667 		return -EOPNOTSUPP;
1668 }
1669 
1670 static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
1671 			     u16 pool_index,
1672 			     struct devlink_sb_pool_info *pool_info)
1673 {
1674 	struct ocelot *ocelot = ds->priv;
1675 
1676 	return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
1677 }
1678 
1679 static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
1680 			     u16 pool_index, u32 size,
1681 			     enum devlink_sb_threshold_type threshold_type,
1682 			     struct netlink_ext_ack *extack)
1683 {
1684 	struct ocelot *ocelot = ds->priv;
1685 
1686 	return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
1687 				  threshold_type, extack);
1688 }
1689 
1690 static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
1691 				  unsigned int sb_index, u16 pool_index,
1692 				  u32 *p_threshold)
1693 {
1694 	struct ocelot *ocelot = ds->priv;
1695 
1696 	return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
1697 				       p_threshold);
1698 }
1699 
1700 static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
1701 				  unsigned int sb_index, u16 pool_index,
1702 				  u32 threshold, struct netlink_ext_ack *extack)
1703 {
1704 	struct ocelot *ocelot = ds->priv;
1705 
1706 	return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
1707 				       threshold, extack);
1708 }
1709 
1710 static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
1711 				     unsigned int sb_index, u16 tc_index,
1712 				     enum devlink_sb_pool_type pool_type,
1713 				     u16 *p_pool_index, u32 *p_threshold)
1714 {
1715 	struct ocelot *ocelot = ds->priv;
1716 
1717 	return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
1718 					  pool_type, p_pool_index,
1719 					  p_threshold);
1720 }
1721 
1722 static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
1723 				     unsigned int sb_index, u16 tc_index,
1724 				     enum devlink_sb_pool_type pool_type,
1725 				     u16 pool_index, u32 threshold,
1726 				     struct netlink_ext_ack *extack)
1727 {
1728 	struct ocelot *ocelot = ds->priv;
1729 
1730 	return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
1731 					  pool_type, pool_index, threshold,
1732 					  extack);
1733 }
1734 
1735 static int felix_sb_occ_snapshot(struct dsa_switch *ds,
1736 				 unsigned int sb_index)
1737 {
1738 	struct ocelot *ocelot = ds->priv;
1739 
1740 	return ocelot_sb_occ_snapshot(ocelot, sb_index);
1741 }
1742 
1743 static int felix_sb_occ_max_clear(struct dsa_switch *ds,
1744 				  unsigned int sb_index)
1745 {
1746 	struct ocelot *ocelot = ds->priv;
1747 
1748 	return ocelot_sb_occ_max_clear(ocelot, sb_index);
1749 }
1750 
1751 static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
1752 				      unsigned int sb_index, u16 pool_index,
1753 				      u32 *p_cur, u32 *p_max)
1754 {
1755 	struct ocelot *ocelot = ds->priv;
1756 
1757 	return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
1758 					   p_cur, p_max);
1759 }
1760 
1761 static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
1762 					 unsigned int sb_index, u16 tc_index,
1763 					 enum devlink_sb_pool_type pool_type,
1764 					 u32 *p_cur, u32 *p_max)
1765 {
1766 	struct ocelot *ocelot = ds->priv;
1767 
1768 	return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
1769 					      pool_type, p_cur, p_max);
1770 }
1771 
1772 static int felix_mrp_add(struct dsa_switch *ds, int port,
1773 			 const struct switchdev_obj_mrp *mrp)
1774 {
1775 	struct ocelot *ocelot = ds->priv;
1776 
1777 	return ocelot_mrp_add(ocelot, port, mrp);
1778 }
1779 
1780 static int felix_mrp_del(struct dsa_switch *ds, int port,
1781 			 const struct switchdev_obj_mrp *mrp)
1782 {
1783 	struct ocelot *ocelot = ds->priv;
1784 
1785 	return ocelot_mrp_add(ocelot, port, mrp);
1786 }
1787 
1788 static int
1789 felix_mrp_add_ring_role(struct dsa_switch *ds, int port,
1790 			const struct switchdev_obj_ring_role_mrp *mrp)
1791 {
1792 	struct ocelot *ocelot = ds->priv;
1793 
1794 	return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1795 }
1796 
1797 static int
1798 felix_mrp_del_ring_role(struct dsa_switch *ds, int port,
1799 			const struct switchdev_obj_ring_role_mrp *mrp)
1800 {
1801 	struct ocelot *ocelot = ds->priv;
1802 
1803 	return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1804 }
1805 
1806 static int felix_port_get_default_prio(struct dsa_switch *ds, int port)
1807 {
1808 	struct ocelot *ocelot = ds->priv;
1809 
1810 	return ocelot_port_get_default_prio(ocelot, port);
1811 }
1812 
1813 static int felix_port_set_default_prio(struct dsa_switch *ds, int port,
1814 				       u8 prio)
1815 {
1816 	struct ocelot *ocelot = ds->priv;
1817 
1818 	return ocelot_port_set_default_prio(ocelot, port, prio);
1819 }
1820 
1821 static int felix_port_get_dscp_prio(struct dsa_switch *ds, int port, u8 dscp)
1822 {
1823 	struct ocelot *ocelot = ds->priv;
1824 
1825 	return ocelot_port_get_dscp_prio(ocelot, port, dscp);
1826 }
1827 
1828 static int felix_port_add_dscp_prio(struct dsa_switch *ds, int port, u8 dscp,
1829 				    u8 prio)
1830 {
1831 	struct ocelot *ocelot = ds->priv;
1832 
1833 	return ocelot_port_add_dscp_prio(ocelot, port, dscp, prio);
1834 }
1835 
1836 static int felix_port_del_dscp_prio(struct dsa_switch *ds, int port, u8 dscp,
1837 				    u8 prio)
1838 {
1839 	struct ocelot *ocelot = ds->priv;
1840 
1841 	return ocelot_port_del_dscp_prio(ocelot, port, dscp, prio);
1842 }
1843 
1844 const struct dsa_switch_ops felix_switch_ops = {
1845 	.get_tag_protocol		= felix_get_tag_protocol,
1846 	.change_tag_protocol		= felix_change_tag_protocol,
1847 	.connect_tag_protocol		= felix_connect_tag_protocol,
1848 	.setup				= felix_setup,
1849 	.teardown			= felix_teardown,
1850 	.set_ageing_time		= felix_set_ageing_time,
1851 	.get_strings			= felix_get_strings,
1852 	.get_ethtool_stats		= felix_get_ethtool_stats,
1853 	.get_sset_count			= felix_get_sset_count,
1854 	.get_ts_info			= felix_get_ts_info,
1855 	.phylink_get_caps		= felix_phylink_get_caps,
1856 	.phylink_validate		= felix_phylink_validate,
1857 	.phylink_mac_select_pcs		= felix_phylink_mac_select_pcs,
1858 	.phylink_mac_link_down		= felix_phylink_mac_link_down,
1859 	.phylink_mac_link_up		= felix_phylink_mac_link_up,
1860 	.port_fast_age			= felix_port_fast_age,
1861 	.port_fdb_dump			= felix_fdb_dump,
1862 	.port_fdb_add			= felix_fdb_add,
1863 	.port_fdb_del			= felix_fdb_del,
1864 	.lag_fdb_add			= felix_lag_fdb_add,
1865 	.lag_fdb_del			= felix_lag_fdb_del,
1866 	.port_mdb_add			= felix_mdb_add,
1867 	.port_mdb_del			= felix_mdb_del,
1868 	.port_pre_bridge_flags		= felix_pre_bridge_flags,
1869 	.port_bridge_flags		= felix_bridge_flags,
1870 	.port_bridge_join		= felix_bridge_join,
1871 	.port_bridge_leave		= felix_bridge_leave,
1872 	.port_lag_join			= felix_lag_join,
1873 	.port_lag_leave			= felix_lag_leave,
1874 	.port_lag_change		= felix_lag_change,
1875 	.port_stp_state_set		= felix_bridge_stp_state_set,
1876 	.port_vlan_filtering		= felix_vlan_filtering,
1877 	.port_vlan_add			= felix_vlan_add,
1878 	.port_vlan_del			= felix_vlan_del,
1879 	.port_hwtstamp_get		= felix_hwtstamp_get,
1880 	.port_hwtstamp_set		= felix_hwtstamp_set,
1881 	.port_rxtstamp			= felix_rxtstamp,
1882 	.port_txtstamp			= felix_txtstamp,
1883 	.port_change_mtu		= felix_change_mtu,
1884 	.port_max_mtu			= felix_get_max_mtu,
1885 	.port_policer_add		= felix_port_policer_add,
1886 	.port_policer_del		= felix_port_policer_del,
1887 	.port_mirror_add		= felix_port_mirror_add,
1888 	.port_mirror_del		= felix_port_mirror_del,
1889 	.cls_flower_add			= felix_cls_flower_add,
1890 	.cls_flower_del			= felix_cls_flower_del,
1891 	.cls_flower_stats		= felix_cls_flower_stats,
1892 	.port_setup_tc			= felix_port_setup_tc,
1893 	.devlink_sb_pool_get		= felix_sb_pool_get,
1894 	.devlink_sb_pool_set		= felix_sb_pool_set,
1895 	.devlink_sb_port_pool_get	= felix_sb_port_pool_get,
1896 	.devlink_sb_port_pool_set	= felix_sb_port_pool_set,
1897 	.devlink_sb_tc_pool_bind_get	= felix_sb_tc_pool_bind_get,
1898 	.devlink_sb_tc_pool_bind_set	= felix_sb_tc_pool_bind_set,
1899 	.devlink_sb_occ_snapshot	= felix_sb_occ_snapshot,
1900 	.devlink_sb_occ_max_clear	= felix_sb_occ_max_clear,
1901 	.devlink_sb_occ_port_pool_get	= felix_sb_occ_port_pool_get,
1902 	.devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
1903 	.port_mrp_add			= felix_mrp_add,
1904 	.port_mrp_del			= felix_mrp_del,
1905 	.port_mrp_add_ring_role		= felix_mrp_add_ring_role,
1906 	.port_mrp_del_ring_role		= felix_mrp_del_ring_role,
1907 	.tag_8021q_vlan_add		= felix_tag_8021q_vlan_add,
1908 	.tag_8021q_vlan_del		= felix_tag_8021q_vlan_del,
1909 	.port_get_default_prio		= felix_port_get_default_prio,
1910 	.port_set_default_prio		= felix_port_set_default_prio,
1911 	.port_get_dscp_prio		= felix_port_get_dscp_prio,
1912 	.port_add_dscp_prio		= felix_port_add_dscp_prio,
1913 	.port_del_dscp_prio		= felix_port_del_dscp_prio,
1914 	.port_set_host_flood		= felix_port_set_host_flood,
1915 };
1916 
1917 struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
1918 {
1919 	struct felix *felix = ocelot_to_felix(ocelot);
1920 	struct dsa_switch *ds = felix->ds;
1921 
1922 	if (!dsa_is_user_port(ds, port))
1923 		return NULL;
1924 
1925 	return dsa_to_port(ds, port)->slave;
1926 }
1927 
1928 int felix_netdev_to_port(struct net_device *dev)
1929 {
1930 	struct dsa_port *dp;
1931 
1932 	dp = dsa_port_from_netdev(dev);
1933 	if (IS_ERR(dp))
1934 		return -EINVAL;
1935 
1936 	return dp->index;
1937 }
1938