xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision 54c319846086e57071fd0e92d20f2cba0fbf0e79)
156051948SVladimir Oltean // SPDX-License-Identifier: GPL-2.0
23c9cfb52SVladimir Oltean /* Copyright 2019-2021 NXP
3375e1314SVladimir Oltean  *
4375e1314SVladimir Oltean  * This is an umbrella module for all network switches that are
5375e1314SVladimir Oltean  * register-compatible with Ocelot and that perform I/O to their host CPU
6375e1314SVladimir Oltean  * through an NPI (Node Processor Interface) Ethernet port.
756051948SVladimir Oltean  */
856051948SVladimir Oltean #include <uapi/linux/if_bridge.h>
907d985eeSVladimir Oltean #include <soc/mscc/ocelot_vcap.h>
10bdeced75SVladimir Oltean #include <soc/mscc/ocelot_qsys.h>
11bdeced75SVladimir Oltean #include <soc/mscc/ocelot_sys.h>
12bdeced75SVladimir Oltean #include <soc/mscc/ocelot_dev.h>
13bdeced75SVladimir Oltean #include <soc/mscc/ocelot_ana.h>
142b49d128SYangbo Lu #include <soc/mscc/ocelot_ptp.h>
1556051948SVladimir Oltean #include <soc/mscc/ocelot.h>
16e21268efSVladimir Oltean #include <linux/dsa/8021q.h>
1740d3f295SVladimir Oltean #include <linux/dsa/ocelot.h>
1884705fc1SMaxim Kochetkov #include <linux/platform_device.h>
190a6f17c6SVladimir Oltean #include <linux/ptp_classify.h>
2056051948SVladimir Oltean #include <linux/module.h>
21bdeced75SVladimir Oltean #include <linux/of_net.h>
2256051948SVladimir Oltean #include <linux/pci.h>
2356051948SVladimir Oltean #include <linux/of.h>
24fc411eaaSVladimir Oltean #include <net/pkt_sched.h>
2556051948SVladimir Oltean #include <net/dsa.h>
2656051948SVladimir Oltean #include "felix.h"
2756051948SVladimir Oltean 
2804b67e18SVladimir Oltean /* Set up VCAP ES0 rules for pushing a tag_8021q VLAN towards the CPU such that
2904b67e18SVladimir Oltean  * the tagger can perform RX source port identification.
3004b67e18SVladimir Oltean  */
3104b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add_rx(struct felix *felix, int port, u16 vid)
32e21268efSVladimir Oltean {
33e21268efSVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
34e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
35e21268efSVladimir Oltean 	struct dsa_switch *ds = felix->ds;
36e21268efSVladimir Oltean 	int key_length, upstream, err;
37e21268efSVladimir Oltean 
38e21268efSVladimir Oltean 	key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length;
39e21268efSVladimir Oltean 	upstream = dsa_upstream_port(ds, port);
40e21268efSVladimir Oltean 
41e21268efSVladimir Oltean 	outer_tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter),
42e21268efSVladimir Oltean 				     GFP_KERNEL);
43e21268efSVladimir Oltean 	if (!outer_tagging_rule)
44e21268efSVladimir Oltean 		return -ENOMEM;
45e21268efSVladimir Oltean 
46e21268efSVladimir Oltean 	outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
47e21268efSVladimir Oltean 	outer_tagging_rule->prio = 1;
48c518afecSVladimir Oltean 	outer_tagging_rule->id.cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port);
49e21268efSVladimir Oltean 	outer_tagging_rule->id.tc_offload = false;
50e21268efSVladimir Oltean 	outer_tagging_rule->block_id = VCAP_ES0;
51e21268efSVladimir Oltean 	outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
52e21268efSVladimir Oltean 	outer_tagging_rule->lookup = 0;
53e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.value = port;
54e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0);
55e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.value = upstream;
56e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0);
57e21268efSVladimir Oltean 	outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG;
58e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD;
59e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_vid_sel = 1;
60e21268efSVladimir Oltean 	outer_tagging_rule->action.vid_a_val = vid;
61e21268efSVladimir Oltean 
62e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL);
63e21268efSVladimir Oltean 	if (err)
64e21268efSVladimir Oltean 		kfree(outer_tagging_rule);
65e21268efSVladimir Oltean 
66e21268efSVladimir Oltean 	return err;
67e21268efSVladimir Oltean }
68e21268efSVladimir Oltean 
6904b67e18SVladimir Oltean static int felix_tag_8021q_vlan_del_rx(struct felix *felix, int port, u16 vid)
7004b67e18SVladimir Oltean {
7104b67e18SVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
7204b67e18SVladimir Oltean 	struct ocelot_vcap_block *block_vcap_es0;
7304b67e18SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
7404b67e18SVladimir Oltean 
7504b67e18SVladimir Oltean 	block_vcap_es0 = &ocelot->block[VCAP_ES0];
7604b67e18SVladimir Oltean 
7704b67e18SVladimir Oltean 	outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0,
7804b67e18SVladimir Oltean 								 port, false);
7904b67e18SVladimir Oltean 	if (!outer_tagging_rule)
8004b67e18SVladimir Oltean 		return -ENOENT;
8104b67e18SVladimir Oltean 
8204b67e18SVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, outer_tagging_rule);
8304b67e18SVladimir Oltean }
8404b67e18SVladimir Oltean 
8504b67e18SVladimir Oltean /* Set up VCAP IS1 rules for stripping the tag_8021q VLAN on TX and VCAP IS2
8604b67e18SVladimir Oltean  * rules for steering those tagged packets towards the correct destination port
8704b67e18SVladimir Oltean  */
8804b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add_tx(struct felix *felix, int port, u16 vid)
89e21268efSVladimir Oltean {
90e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
91e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
92e21268efSVladimir Oltean 	struct dsa_switch *ds = felix->ds;
93e21268efSVladimir Oltean 	int upstream, err;
94e21268efSVladimir Oltean 
95e21268efSVladimir Oltean 	untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
96e21268efSVladimir Oltean 	if (!untagging_rule)
97e21268efSVladimir Oltean 		return -ENOMEM;
98e21268efSVladimir Oltean 
99e21268efSVladimir Oltean 	redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
100e21268efSVladimir Oltean 	if (!redirect_rule) {
101e21268efSVladimir Oltean 		kfree(untagging_rule);
102e21268efSVladimir Oltean 		return -ENOMEM;
103e21268efSVladimir Oltean 	}
104e21268efSVladimir Oltean 
105e21268efSVladimir Oltean 	upstream = dsa_upstream_port(ds, port);
106e21268efSVladimir Oltean 
107e21268efSVladimir Oltean 	untagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
108e21268efSVladimir Oltean 	untagging_rule->ingress_port_mask = BIT(upstream);
109e21268efSVladimir Oltean 	untagging_rule->vlan.vid.value = vid;
110e21268efSVladimir Oltean 	untagging_rule->vlan.vid.mask = VLAN_VID_MASK;
111e21268efSVladimir Oltean 	untagging_rule->prio = 1;
112c518afecSVladimir Oltean 	untagging_rule->id.cookie = OCELOT_VCAP_IS1_TAG_8021Q_TXVLAN(ocelot, port);
113e21268efSVladimir Oltean 	untagging_rule->id.tc_offload = false;
114e21268efSVladimir Oltean 	untagging_rule->block_id = VCAP_IS1;
115e21268efSVladimir Oltean 	untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
116e21268efSVladimir Oltean 	untagging_rule->lookup = 0;
117e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt_ena = true;
118e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt = 1;
119e21268efSVladimir Oltean 	untagging_rule->action.pag_override_mask = 0xff;
120e21268efSVladimir Oltean 	untagging_rule->action.pag_val = port;
121e21268efSVladimir Oltean 
122e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL);
123e21268efSVladimir Oltean 	if (err) {
124e21268efSVladimir Oltean 		kfree(untagging_rule);
125e21268efSVladimir Oltean 		kfree(redirect_rule);
126e21268efSVladimir Oltean 		return err;
127e21268efSVladimir Oltean 	}
128e21268efSVladimir Oltean 
129e21268efSVladimir Oltean 	redirect_rule->key_type = OCELOT_VCAP_KEY_ANY;
130e21268efSVladimir Oltean 	redirect_rule->ingress_port_mask = BIT(upstream);
131e21268efSVladimir Oltean 	redirect_rule->pag = port;
132e21268efSVladimir Oltean 	redirect_rule->prio = 1;
133c518afecSVladimir Oltean 	redirect_rule->id.cookie = OCELOT_VCAP_IS2_TAG_8021Q_TXVLAN(ocelot, port);
134e21268efSVladimir Oltean 	redirect_rule->id.tc_offload = false;
135e21268efSVladimir Oltean 	redirect_rule->block_id = VCAP_IS2;
136e21268efSVladimir Oltean 	redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
137e21268efSVladimir Oltean 	redirect_rule->lookup = 0;
138e21268efSVladimir Oltean 	redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT;
139e21268efSVladimir Oltean 	redirect_rule->action.port_mask = BIT(port);
140e21268efSVladimir Oltean 
141e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL);
142e21268efSVladimir Oltean 	if (err) {
143e21268efSVladimir Oltean 		ocelot_vcap_filter_del(ocelot, untagging_rule);
144e21268efSVladimir Oltean 		kfree(redirect_rule);
145e21268efSVladimir Oltean 		return err;
146e21268efSVladimir Oltean 	}
147e21268efSVladimir Oltean 
148e21268efSVladimir Oltean 	return 0;
149e21268efSVladimir Oltean }
150e21268efSVladimir Oltean 
15104b67e18SVladimir Oltean static int felix_tag_8021q_vlan_del_tx(struct felix *felix, int port, u16 vid)
152e21268efSVladimir Oltean {
153e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
154e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is1;
155e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
156e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
157e21268efSVladimir Oltean 	int err;
158e21268efSVladimir Oltean 
159e21268efSVladimir Oltean 	block_vcap_is1 = &ocelot->block[VCAP_IS1];
160e21268efSVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
161e21268efSVladimir Oltean 
162e21268efSVladimir Oltean 	untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1,
163e21268efSVladimir Oltean 							     port, false);
164e21268efSVladimir Oltean 	if (!untagging_rule)
16508f44db3SVladimir Oltean 		return -ENOENT;
166e21268efSVladimir Oltean 
167e21268efSVladimir Oltean 	err = ocelot_vcap_filter_del(ocelot, untagging_rule);
168e21268efSVladimir Oltean 	if (err)
169e21268efSVladimir Oltean 		return err;
170e21268efSVladimir Oltean 
171e21268efSVladimir Oltean 	redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2,
172e21268efSVladimir Oltean 							    port, false);
173e21268efSVladimir Oltean 	if (!redirect_rule)
17404b67e18SVladimir Oltean 		return -ENOENT;
175e21268efSVladimir Oltean 
176e21268efSVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, redirect_rule);
177e21268efSVladimir Oltean }
178e21268efSVladimir Oltean 
17904b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
18004b67e18SVladimir Oltean 				    u16 flags)
18104b67e18SVladimir Oltean {
18204b67e18SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
18304b67e18SVladimir Oltean 	int err;
18404b67e18SVladimir Oltean 
18504b67e18SVladimir Oltean 	/* tag_8021q.c assumes we are implementing this via port VLAN
18604b67e18SVladimir Oltean 	 * membership, which we aren't. So we don't need to add any VCAP filter
18704b67e18SVladimir Oltean 	 * for the CPU port.
18804b67e18SVladimir Oltean 	 */
18904b67e18SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
19004b67e18SVladimir Oltean 		return 0;
19104b67e18SVladimir Oltean 
19204b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_add_rx(ocelot_to_felix(ocelot), port, vid);
19304b67e18SVladimir Oltean 	if (err)
19404b67e18SVladimir Oltean 		return err;
19504b67e18SVladimir Oltean 
19604b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_add_tx(ocelot_to_felix(ocelot), port, vid);
19704b67e18SVladimir Oltean 	if (err) {
19804b67e18SVladimir Oltean 		felix_tag_8021q_vlan_del_rx(ocelot_to_felix(ocelot), port, vid);
19904b67e18SVladimir Oltean 		return err;
20004b67e18SVladimir Oltean 	}
20104b67e18SVladimir Oltean 
20204b67e18SVladimir Oltean 	return 0;
20304b67e18SVladimir Oltean }
20404b67e18SVladimir Oltean 
205e21268efSVladimir Oltean static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
206e21268efSVladimir Oltean {
207e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
20804b67e18SVladimir Oltean 	int err;
209e21268efSVladimir Oltean 
21004b67e18SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
21104b67e18SVladimir Oltean 		return 0;
212e21268efSVladimir Oltean 
21304b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_del_rx(ocelot_to_felix(ocelot), port, vid);
21404b67e18SVladimir Oltean 	if (err)
21504b67e18SVladimir Oltean 		return err;
21604b67e18SVladimir Oltean 
21704b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_del_tx(ocelot_to_felix(ocelot), port, vid);
21804b67e18SVladimir Oltean 	if (err) {
21904b67e18SVladimir Oltean 		felix_tag_8021q_vlan_add_rx(ocelot_to_felix(ocelot), port, vid);
22004b67e18SVladimir Oltean 		return err;
22104b67e18SVladimir Oltean 	}
222e21268efSVladimir Oltean 
223e21268efSVladimir Oltean 	return 0;
224e21268efSVladimir Oltean }
225e21268efSVladimir Oltean 
226e21268efSVladimir Oltean /* Alternatively to using the NPI functionality, that same hardware MAC
227e21268efSVladimir Oltean  * connected internally to the enetc or fman DSA master can be configured to
228e21268efSVladimir Oltean  * use the software-defined tag_8021q frame format. As far as the hardware is
229e21268efSVladimir Oltean  * concerned, it thinks it is a "dumb switch" - the queues of the CPU port
230e21268efSVladimir Oltean  * module are now disconnected from it, but can still be accessed through
231e21268efSVladimir Oltean  * register-based MMIO.
232e21268efSVladimir Oltean  */
233e21268efSVladimir Oltean static void felix_8021q_cpu_port_init(struct ocelot *ocelot, int port)
234e21268efSVladimir Oltean {
2358abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2368abe1970SVladimir Oltean 
237*54c31984SVladimir Oltean 	ocelot_port_set_dsa_8021q_cpu(ocelot, port);
238e21268efSVladimir Oltean 	ocelot->npi = -1;
239e21268efSVladimir Oltean 
240e21268efSVladimir Oltean 	/* Overwrite PGID_CPU with the non-tagging port */
241e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, PGID_CPU);
242e21268efSVladimir Oltean 
2438abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
2448abe1970SVladimir Oltean 
2458abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
246e21268efSVladimir Oltean }
247e21268efSVladimir Oltean 
248e21268efSVladimir Oltean static void felix_8021q_cpu_port_deinit(struct ocelot *ocelot, int port)
249e21268efSVladimir Oltean {
2508abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2518abe1970SVladimir Oltean 
252e21268efSVladimir Oltean 	ocelot->ports[port]->is_dsa_8021q_cpu = false;
253*54c31984SVladimir Oltean 	ocelot_port_unset_dsa_8021q_cpu(ocelot, port);
254e21268efSVladimir Oltean 
255e21268efSVladimir Oltean 	/* Restore PGID_CPU */
256e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(ocelot->num_phys_ports), ANA_PGID_PGID,
257e21268efSVladimir Oltean 			 PGID_CPU);
258e21268efSVladimir Oltean 
2598abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
2608abe1970SVladimir Oltean 
2618abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
262e21268efSVladimir Oltean }
263e21268efSVladimir Oltean 
26499348004SVladimir Oltean /* On switches with no extraction IRQ wired, trapped packets need to be
26599348004SVladimir Oltean  * replicated over Ethernet as well, otherwise we'd get no notification of
26699348004SVladimir Oltean  * their arrival when using the ocelot-8021q tagging protocol.
267c8c0ba4fSVladimir Oltean  */
26899348004SVladimir Oltean static int felix_update_trapping_destinations(struct dsa_switch *ds,
26999348004SVladimir Oltean 					      bool using_tag_8021q)
270c8c0ba4fSVladimir Oltean {
27199348004SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
27299348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
27399348004SVladimir Oltean 	struct ocelot_vcap_filter *trap;
27499348004SVladimir Oltean 	enum ocelot_mask_mode mask_mode;
27599348004SVladimir Oltean 	unsigned long port_mask;
2762960bb14SVladimir Oltean 	struct dsa_port *dp;
27799348004SVladimir Oltean 	bool cpu_copy_ena;
27899348004SVladimir Oltean 	int cpu = -1, err;
279c8c0ba4fSVladimir Oltean 
28099348004SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
28199348004SVladimir Oltean 		return 0;
282c8c0ba4fSVladimir Oltean 
28399348004SVladimir Oltean 	/* Figure out the current CPU port */
2842960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
2852960bb14SVladimir Oltean 		cpu = dp->index;
2868d5f7954SVladimir Oltean 		break;
287c8c0ba4fSVladimir Oltean 	}
2888d5f7954SVladimir Oltean 
289d78637a8SVladimir Oltean 	/* We are sure that "cpu" was found, otherwise
290d78637a8SVladimir Oltean 	 * dsa_tree_setup_default_cpu() would have failed earlier.
291d78637a8SVladimir Oltean 	 */
292c8c0ba4fSVladimir Oltean 
29399348004SVladimir Oltean 	/* Make sure all traps are set up for that destination */
29499348004SVladimir Oltean 	list_for_each_entry(trap, &ocelot->traps, trap_list) {
29599348004SVladimir Oltean 		/* Figure out the current trapping destination */
29699348004SVladimir Oltean 		if (using_tag_8021q) {
29799348004SVladimir Oltean 			/* Redirect to the tag_8021q CPU port. If timestamps
29899348004SVladimir Oltean 			 * are necessary, also copy trapped packets to the CPU
29999348004SVladimir Oltean 			 * port module.
300c8c0ba4fSVladimir Oltean 			 */
30199348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_REDIRECT;
30299348004SVladimir Oltean 			port_mask = BIT(cpu);
30399348004SVladimir Oltean 			cpu_copy_ena = !!trap->take_ts;
304c8c0ba4fSVladimir Oltean 		} else {
30599348004SVladimir Oltean 			/* Trap packets only to the CPU port module, which is
30699348004SVladimir Oltean 			 * redirected to the NPI port (the DSA CPU port)
307c8c0ba4fSVladimir Oltean 			 */
30899348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
30999348004SVladimir Oltean 			port_mask = 0;
31099348004SVladimir Oltean 			cpu_copy_ena = true;
311c8c0ba4fSVladimir Oltean 		}
312c8c0ba4fSVladimir Oltean 
31399348004SVladimir Oltean 		if (trap->action.mask_mode == mask_mode &&
31499348004SVladimir Oltean 		    trap->action.port_mask == port_mask &&
31599348004SVladimir Oltean 		    trap->action.cpu_copy_ena == cpu_copy_ena)
31699348004SVladimir Oltean 			continue;
317c8c0ba4fSVladimir Oltean 
31899348004SVladimir Oltean 		trap->action.mask_mode = mask_mode;
31999348004SVladimir Oltean 		trap->action.port_mask = port_mask;
32099348004SVladimir Oltean 		trap->action.cpu_copy_ena = cpu_copy_ena;
3210a6f17c6SVladimir Oltean 
32299348004SVladimir Oltean 		err = ocelot_vcap_filter_replace(ocelot, trap);
323c8c0ba4fSVladimir Oltean 		if (err)
324c8c0ba4fSVladimir Oltean 			return err;
32599348004SVladimir Oltean 	}
326c8c0ba4fSVladimir Oltean 
32799348004SVladimir Oltean 	return 0;
328c8c0ba4fSVladimir Oltean }
329c8c0ba4fSVladimir Oltean 
330e21268efSVladimir Oltean static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu)
331e21268efSVladimir Oltean {
332e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
333e21268efSVladimir Oltean 	unsigned long cpu_flood;
3342960bb14SVladimir Oltean 	struct dsa_port *dp;
3352960bb14SVladimir Oltean 	int err;
336e21268efSVladimir Oltean 
337e21268efSVladimir Oltean 	felix_8021q_cpu_port_init(ocelot, cpu);
338e21268efSVladimir Oltean 
3392960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
340e21268efSVladimir Oltean 		/* This overwrites ocelot_init():
341e21268efSVladimir Oltean 		 * Do not forward BPDU frames to the CPU port module,
342e21268efSVladimir Oltean 		 * for 2 reasons:
343e21268efSVladimir Oltean 		 * - When these packets are injected from the tag_8021q
344e21268efSVladimir Oltean 		 *   CPU port, we want them to go out, not loop back
345e21268efSVladimir Oltean 		 *   into the system.
346e21268efSVladimir Oltean 		 * - STP traffic ingressing on a user port should go to
347e21268efSVladimir Oltean 		 *   the tag_8021q CPU port, not to the hardware CPU
348e21268efSVladimir Oltean 		 *   port module.
349e21268efSVladimir Oltean 		 */
350e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
351e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0),
3522960bb14SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG, dp->index);
353e21268efSVladimir Oltean 	}
354e21268efSVladimir Oltean 
355c8c0ba4fSVladimir Oltean 	/* In tag_8021q mode, the CPU port module is unused, except for PTP
356c8c0ba4fSVladimir Oltean 	 * frames. So we want to disable flooding of any kind to the CPU port
357c8c0ba4fSVladimir Oltean 	 * module, since packets going there will end in a black hole.
358e21268efSVladimir Oltean 	 */
359e21268efSVladimir Oltean 	cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
360e21268efSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_UC);
361e21268efSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_MC);
362b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_BC);
363e21268efSVladimir Oltean 
3645da11eb4SVladimir Oltean 	err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD));
365e21268efSVladimir Oltean 	if (err)
366d7b1fd52SVladimir Oltean 		return err;
367d7b1fd52SVladimir Oltean 
36899348004SVladimir Oltean 	err = felix_update_trapping_destinations(ds, true);
369d7b1fd52SVladimir Oltean 	if (err)
370d7b1fd52SVladimir Oltean 		goto out_tag_8021q_unregister;
371e21268efSVladimir Oltean 
37299348004SVladimir Oltean 	/* The ownership of the CPU port module's queues might have just been
37399348004SVladimir Oltean 	 * transferred to the tag_8021q tagger from the NPI-based tagger.
37499348004SVladimir Oltean 	 * So there might still be all sorts of crap in the queues. On the
37599348004SVladimir Oltean 	 * other hand, the MMIO-based matching of PTP frames is very brittle,
37699348004SVladimir Oltean 	 * so we need to be careful that there are no extra frames to be
37799348004SVladimir Oltean 	 * dequeued over MMIO, since we would never know to discard them.
37899348004SVladimir Oltean 	 */
37999348004SVladimir Oltean 	ocelot_drain_cpu_queue(ocelot, 0);
38099348004SVladimir Oltean 
381e21268efSVladimir Oltean 	return 0;
382e21268efSVladimir Oltean 
383d7b1fd52SVladimir Oltean out_tag_8021q_unregister:
384d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
385e21268efSVladimir Oltean 	return err;
386e21268efSVladimir Oltean }
387e21268efSVladimir Oltean 
388e21268efSVladimir Oltean static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu)
389e21268efSVladimir Oltean {
390e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
3912960bb14SVladimir Oltean 	struct dsa_port *dp;
3922960bb14SVladimir Oltean 	int err;
393e21268efSVladimir Oltean 
39499348004SVladimir Oltean 	err = felix_update_trapping_destinations(ds, false);
395c8c0ba4fSVladimir Oltean 	if (err)
396c8c0ba4fSVladimir Oltean 		dev_err(ds->dev, "felix_teardown_mmio_filtering returned %d",
397c8c0ba4fSVladimir Oltean 			err);
398c8c0ba4fSVladimir Oltean 
399d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
400e21268efSVladimir Oltean 
4012960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
402e21268efSVladimir Oltean 		/* Restore the logic from ocelot_init:
403e21268efSVladimir Oltean 		 * do not forward BPDU frames to the front ports.
404e21268efSVladimir Oltean 		 */
405e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
406e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
407e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG,
4082960bb14SVladimir Oltean 				 dp->index);
409e21268efSVladimir Oltean 	}
410e21268efSVladimir Oltean 
411e21268efSVladimir Oltean 	felix_8021q_cpu_port_deinit(ocelot, cpu);
412e21268efSVladimir Oltean }
413e21268efSVladimir Oltean 
414adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This
415adb3dccfSVladimir Oltean  * is the mode through which frames can be injected from and extracted to an
416adb3dccfSVladimir Oltean  * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU
417adb3dccfSVladimir Oltean  * running Linux, and this forms a DSA setup together with the enetc or fman
418adb3dccfSVladimir Oltean  * DSA master.
419adb3dccfSVladimir Oltean  */
420adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port)
421adb3dccfSVladimir Oltean {
422adb3dccfSVladimir Oltean 	ocelot->npi = port;
423adb3dccfSVladimir Oltean 
424adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
425adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
426adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
427adb3dccfSVladimir Oltean 
428adb3dccfSVladimir Oltean 	/* NPI port Injection/Extraction configuration */
429adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
430adb3dccfSVladimir Oltean 			    ocelot->npi_xtr_prefix);
431adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
432adb3dccfSVladimir Oltean 			    ocelot->npi_inj_prefix);
433adb3dccfSVladimir Oltean 
434adb3dccfSVladimir Oltean 	/* Disable transmission of pause frames */
435adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
436adb3dccfSVladimir Oltean }
437adb3dccfSVladimir Oltean 
438adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port)
439adb3dccfSVladimir Oltean {
440adb3dccfSVladimir Oltean 	/* Restore hardware defaults */
441adb3dccfSVladimir Oltean 	int unused_port = ocelot->num_phys_ports + 2;
442adb3dccfSVladimir Oltean 
443adb3dccfSVladimir Oltean 	ocelot->npi = -1;
444adb3dccfSVladimir Oltean 
445adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port),
446adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
447adb3dccfSVladimir Oltean 
448adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
449adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
450adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
451adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
452adb3dccfSVladimir Oltean 
453adb3dccfSVladimir Oltean 	/* Enable transmission of pause frames */
454adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
455adb3dccfSVladimir Oltean }
456adb3dccfSVladimir Oltean 
457adb3dccfSVladimir Oltean static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu)
458adb3dccfSVladimir Oltean {
459adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
460adb3dccfSVladimir Oltean 	unsigned long cpu_flood;
461adb3dccfSVladimir Oltean 
462adb3dccfSVladimir Oltean 	felix_npi_port_init(ocelot, cpu);
463adb3dccfSVladimir Oltean 
464adb3dccfSVladimir Oltean 	/* Include the CPU port module (and indirectly, the NPI port)
465adb3dccfSVladimir Oltean 	 * in the forwarding mask for unknown unicast - the hardware
466adb3dccfSVladimir Oltean 	 * default value for ANA_FLOODING_FLD_UNICAST excludes
467adb3dccfSVladimir Oltean 	 * BIT(ocelot->num_phys_ports), and so does ocelot_init,
468adb3dccfSVladimir Oltean 	 * since Ocelot relies on whitelisting MAC addresses towards
469adb3dccfSVladimir Oltean 	 * PGID_CPU.
470adb3dccfSVladimir Oltean 	 * We do this because DSA does not yet perform RX filtering,
471adb3dccfSVladimir Oltean 	 * and the NPI port does not perform source address learning,
472adb3dccfSVladimir Oltean 	 * so traffic sent to Linux is effectively unknown from the
473adb3dccfSVladimir Oltean 	 * switch's perspective.
474adb3dccfSVladimir Oltean 	 */
475adb3dccfSVladimir Oltean 	cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
476adb3dccfSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_UC);
4776edb9e8dSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_MC);
478b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_BC);
479adb3dccfSVladimir Oltean 
480adb3dccfSVladimir Oltean 	return 0;
481adb3dccfSVladimir Oltean }
482adb3dccfSVladimir Oltean 
483adb3dccfSVladimir Oltean static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu)
484adb3dccfSVladimir Oltean {
485adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
486adb3dccfSVladimir Oltean 
487adb3dccfSVladimir Oltean 	felix_npi_port_deinit(ocelot, cpu);
488adb3dccfSVladimir Oltean }
489adb3dccfSVladimir Oltean 
490adb3dccfSVladimir Oltean static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu,
491adb3dccfSVladimir Oltean 				  enum dsa_tag_protocol proto)
492adb3dccfSVladimir Oltean {
493adb3dccfSVladimir Oltean 	int err;
494adb3dccfSVladimir Oltean 
495adb3dccfSVladimir Oltean 	switch (proto) {
4967c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
497adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
498adb3dccfSVladimir Oltean 		err = felix_setup_tag_npi(ds, cpu);
499adb3dccfSVladimir Oltean 		break;
500e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
501e21268efSVladimir Oltean 		err = felix_setup_tag_8021q(ds, cpu);
502e21268efSVladimir Oltean 		break;
503adb3dccfSVladimir Oltean 	default:
504adb3dccfSVladimir Oltean 		err = -EPROTONOSUPPORT;
505adb3dccfSVladimir Oltean 	}
506adb3dccfSVladimir Oltean 
507adb3dccfSVladimir Oltean 	return err;
508adb3dccfSVladimir Oltean }
509adb3dccfSVladimir Oltean 
510adb3dccfSVladimir Oltean static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu,
511adb3dccfSVladimir Oltean 				   enum dsa_tag_protocol proto)
512adb3dccfSVladimir Oltean {
513adb3dccfSVladimir Oltean 	switch (proto) {
5147c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
515adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
516adb3dccfSVladimir Oltean 		felix_teardown_tag_npi(ds, cpu);
517adb3dccfSVladimir Oltean 		break;
518e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
519e21268efSVladimir Oltean 		felix_teardown_tag_8021q(ds, cpu);
520e21268efSVladimir Oltean 		break;
521adb3dccfSVladimir Oltean 	default:
522adb3dccfSVladimir Oltean 		break;
523adb3dccfSVladimir Oltean 	}
524adb3dccfSVladimir Oltean }
525adb3dccfSVladimir Oltean 
526e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the
527e21268efSVladimir Oltean  * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
528e21268efSVladimir Oltean  * or the restoration is guaranteed to work.
529e21268efSVladimir Oltean  */
530adb3dccfSVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu,
531adb3dccfSVladimir Oltean 				     enum dsa_tag_protocol proto)
532adb3dccfSVladimir Oltean {
533adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
534adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
535adb3dccfSVladimir Oltean 	enum dsa_tag_protocol old_proto = felix->tag_proto;
536adb3dccfSVladimir Oltean 	int err;
537adb3dccfSVladimir Oltean 
5387c4bb540SVladimir Oltean 	if (proto != DSA_TAG_PROTO_SEVILLE &&
5397c4bb540SVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT &&
540e21268efSVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT_8021Q)
541adb3dccfSVladimir Oltean 		return -EPROTONOSUPPORT;
542adb3dccfSVladimir Oltean 
543adb3dccfSVladimir Oltean 	felix_del_tag_protocol(ds, cpu, old_proto);
544adb3dccfSVladimir Oltean 
545adb3dccfSVladimir Oltean 	err = felix_set_tag_protocol(ds, cpu, proto);
546adb3dccfSVladimir Oltean 	if (err) {
547adb3dccfSVladimir Oltean 		felix_set_tag_protocol(ds, cpu, old_proto);
548adb3dccfSVladimir Oltean 		return err;
549adb3dccfSVladimir Oltean 	}
550adb3dccfSVladimir Oltean 
551adb3dccfSVladimir Oltean 	felix->tag_proto = proto;
552adb3dccfSVladimir Oltean 
553adb3dccfSVladimir Oltean 	return 0;
554adb3dccfSVladimir Oltean }
555adb3dccfSVladimir Oltean 
55656051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
5574d776482SFlorian Fainelli 						    int port,
5584d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
55956051948SVladimir Oltean {
560adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
561adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
562adb3dccfSVladimir Oltean 
563adb3dccfSVladimir Oltean 	return felix->tag_proto;
56456051948SVladimir Oltean }
56556051948SVladimir Oltean 
56656051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
56756051948SVladimir Oltean 				 unsigned int ageing_time)
56856051948SVladimir Oltean {
56956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
57056051948SVladimir Oltean 
57156051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
57256051948SVladimir Oltean 
57356051948SVladimir Oltean 	return 0;
57456051948SVladimir Oltean }
57556051948SVladimir Oltean 
5765cad43a5SVladimir Oltean static void felix_port_fast_age(struct dsa_switch *ds, int port)
5775cad43a5SVladimir Oltean {
5785cad43a5SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5795cad43a5SVladimir Oltean 	int err;
5805cad43a5SVladimir Oltean 
5815cad43a5SVladimir Oltean 	err = ocelot_mact_flush(ocelot, port);
5825cad43a5SVladimir Oltean 	if (err)
5835cad43a5SVladimir Oltean 		dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n",
5845cad43a5SVladimir Oltean 			port, ERR_PTR(err));
5855cad43a5SVladimir Oltean }
5865cad43a5SVladimir Oltean 
58756051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
58856051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
58956051948SVladimir Oltean {
59056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
59156051948SVladimir Oltean 
59256051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
59356051948SVladimir Oltean }
59456051948SVladimir Oltean 
595*54c31984SVladimir Oltean /* Translate the DSA database API into the ocelot switch library API,
596*54c31984SVladimir Oltean  * which uses VID 0 for all ports that aren't part of a bridge,
597*54c31984SVladimir Oltean  * and expects the bridge_dev to be NULL in that case.
598*54c31984SVladimir Oltean  */
599*54c31984SVladimir Oltean static struct net_device *felix_classify_db(struct dsa_db db)
600*54c31984SVladimir Oltean {
601*54c31984SVladimir Oltean 	switch (db.type) {
602*54c31984SVladimir Oltean 	case DSA_DB_PORT:
603*54c31984SVladimir Oltean 	case DSA_DB_LAG:
604*54c31984SVladimir Oltean 		return NULL;
605*54c31984SVladimir Oltean 	case DSA_DB_BRIDGE:
606*54c31984SVladimir Oltean 		return db.bridge.dev;
607*54c31984SVladimir Oltean 	default:
608*54c31984SVladimir Oltean 		return ERR_PTR(-EOPNOTSUPP);
609*54c31984SVladimir Oltean 	}
610*54c31984SVladimir Oltean }
611*54c31984SVladimir Oltean 
61256051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
613c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
614c2693363SVladimir Oltean 			 struct dsa_db db)
61556051948SVladimir Oltean {
616*54c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
61756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
61856051948SVladimir Oltean 
619*54c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
620*54c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
621*54c31984SVladimir Oltean 
622*54c31984SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid, bridge_dev);
62356051948SVladimir Oltean }
62456051948SVladimir Oltean 
62556051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
626c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
627c2693363SVladimir Oltean 			 struct dsa_db db)
62856051948SVladimir Oltean {
629*54c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
63056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
63156051948SVladimir Oltean 
632*54c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
633*54c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
634*54c31984SVladimir Oltean 
635*54c31984SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid, bridge_dev);
63656051948SVladimir Oltean }
63756051948SVladimir Oltean 
638961d8b69SVladimir Oltean static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag,
639c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
640c2693363SVladimir Oltean 			     struct dsa_db db)
641961d8b69SVladimir Oltean {
642*54c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
643961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
644961d8b69SVladimir Oltean 
645*54c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
646*54c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
647*54c31984SVladimir Oltean 
648*54c31984SVladimir Oltean 	return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid, bridge_dev);
649961d8b69SVladimir Oltean }
650961d8b69SVladimir Oltean 
651961d8b69SVladimir Oltean static int felix_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag lag,
652c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
653c2693363SVladimir Oltean 			     struct dsa_db db)
654961d8b69SVladimir Oltean {
655*54c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
656961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
657961d8b69SVladimir Oltean 
658*54c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
659*54c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
660*54c31984SVladimir Oltean 
661*54c31984SVladimir Oltean 	return ocelot_lag_fdb_del(ocelot, lag.dev, addr, vid, bridge_dev);
662961d8b69SVladimir Oltean }
663961d8b69SVladimir Oltean 
664a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port,
665c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
666c2693363SVladimir Oltean 			 struct dsa_db db)
667209edf95SVladimir Oltean {
668*54c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
669209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
670209edf95SVladimir Oltean 
671*54c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
672*54c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
673*54c31984SVladimir Oltean 
674*54c31984SVladimir Oltean 	return ocelot_port_mdb_add(ocelot, port, mdb, bridge_dev);
675209edf95SVladimir Oltean }
676209edf95SVladimir Oltean 
677209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port,
678c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
679c2693363SVladimir Oltean 			 struct dsa_db db)
680209edf95SVladimir Oltean {
681*54c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
682209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
683209edf95SVladimir Oltean 
684*54c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
685*54c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
686*54c31984SVladimir Oltean 
687*54c31984SVladimir Oltean 	return ocelot_port_mdb_del(ocelot, port, mdb, bridge_dev);
688209edf95SVladimir Oltean }
689209edf95SVladimir Oltean 
69056051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
69156051948SVladimir Oltean 				       u8 state)
69256051948SVladimir Oltean {
69356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
69456051948SVladimir Oltean 
69556051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
69656051948SVladimir Oltean }
69756051948SVladimir Oltean 
698421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port,
699421741eaSVladimir Oltean 				  struct switchdev_brport_flags val,
700421741eaSVladimir Oltean 				  struct netlink_ext_ack *extack)
701421741eaSVladimir Oltean {
702421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
703421741eaSVladimir Oltean 
704421741eaSVladimir Oltean 	return ocelot_port_pre_bridge_flags(ocelot, port, val);
705421741eaSVladimir Oltean }
706421741eaSVladimir Oltean 
707421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port,
708421741eaSVladimir Oltean 			      struct switchdev_brport_flags val,
709421741eaSVladimir Oltean 			      struct netlink_ext_ack *extack)
710421741eaSVladimir Oltean {
711421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
712421741eaSVladimir Oltean 
713421741eaSVladimir Oltean 	ocelot_port_bridge_flags(ocelot, port, val);
714421741eaSVladimir Oltean 
715421741eaSVladimir Oltean 	return 0;
716421741eaSVladimir Oltean }
717421741eaSVladimir Oltean 
71856051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
71906b9cce4SVladimir Oltean 			     struct dsa_bridge bridge, bool *tx_fwd_offload,
72006b9cce4SVladimir Oltean 			     struct netlink_ext_ack *extack)
72156051948SVladimir Oltean {
72256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
72356051948SVladimir Oltean 
724*54c31984SVladimir Oltean 	return ocelot_port_bridge_join(ocelot, port, bridge.dev, bridge.num,
725*54c31984SVladimir Oltean 				       extack);
72656051948SVladimir Oltean }
72756051948SVladimir Oltean 
72856051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
729d3eed0e5SVladimir Oltean 			       struct dsa_bridge bridge)
73056051948SVladimir Oltean {
73156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
73256051948SVladimir Oltean 
733d3eed0e5SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, bridge.dev);
73456051948SVladimir Oltean }
73556051948SVladimir Oltean 
7368fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port,
737dedd6a00SVladimir Oltean 			  struct dsa_lag lag,
7388fe6832eSVladimir Oltean 			  struct netdev_lag_upper_info *info)
7398fe6832eSVladimir Oltean {
7408fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7418fe6832eSVladimir Oltean 
742dedd6a00SVladimir Oltean 	return ocelot_port_lag_join(ocelot, port, lag.dev, info);
7438fe6832eSVladimir Oltean }
7448fe6832eSVladimir Oltean 
7458fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port,
746dedd6a00SVladimir Oltean 			   struct dsa_lag lag)
7478fe6832eSVladimir Oltean {
7488fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7498fe6832eSVladimir Oltean 
750dedd6a00SVladimir Oltean 	ocelot_port_lag_leave(ocelot, port, lag.dev);
7518fe6832eSVladimir Oltean 
7528fe6832eSVladimir Oltean 	return 0;
7538fe6832eSVladimir Oltean }
7548fe6832eSVladimir Oltean 
7558fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port)
7568fe6832eSVladimir Oltean {
7578fe6832eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
7588fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7598fe6832eSVladimir Oltean 
7608fe6832eSVladimir Oltean 	ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
7618fe6832eSVladimir Oltean 
7628fe6832eSVladimir Oltean 	return 0;
7638fe6832eSVladimir Oltean }
7648fe6832eSVladimir Oltean 
76556051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
76601af940eSVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan,
76701af940eSVladimir Oltean 			      struct netlink_ext_ack *extack)
76856051948SVladimir Oltean {
7692f0402feSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
770b7a9e0daSVladimir Oltean 	u16 flags = vlan->flags;
7712f0402feSVladimir Oltean 
7729a720680SVladimir Oltean 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
7739a720680SVladimir Oltean 	 * egress-untagged or not, pvid or not, make no difference. This
7749a720680SVladimir Oltean 	 * behavior is already better than what DSA just tries to approximate
7759a720680SVladimir Oltean 	 * when it installs the VLAN with the same flags on the CPU port.
7769a720680SVladimir Oltean 	 * Just accept any configuration, and don't let ocelot deny installing
7779a720680SVladimir Oltean 	 * multiple native VLANs on the NPI port, because the switch doesn't
7789a720680SVladimir Oltean 	 * look at the port tag settings towards the NPI interface anyway.
7799a720680SVladimir Oltean 	 */
7809a720680SVladimir Oltean 	if (port == ocelot->npi)
7819a720680SVladimir Oltean 		return 0;
7829a720680SVladimir Oltean 
783b7a9e0daSVladimir Oltean 	return ocelot_vlan_prepare(ocelot, port, vlan->vid,
7842f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_PVID,
78501af940eSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_UNTAGGED,
78601af940eSVladimir Oltean 				   extack);
78756051948SVladimir Oltean }
78856051948SVladimir Oltean 
78989153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
79089153ed6SVladimir Oltean 				struct netlink_ext_ack *extack)
79156051948SVladimir Oltean {
79256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
79356051948SVladimir Oltean 
7943b95d1b2SVladimir Oltean 	return ocelot_port_vlan_filtering(ocelot, port, enabled, extack);
79556051948SVladimir Oltean }
79656051948SVladimir Oltean 
7971958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port,
79831046a5fSVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan,
79931046a5fSVladimir Oltean 			  struct netlink_ext_ack *extack)
80056051948SVladimir Oltean {
80156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
802183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
8031958d581SVladimir Oltean 	int err;
80456051948SVladimir Oltean 
80501af940eSVladimir Oltean 	err = felix_vlan_prepare(ds, port, vlan, extack);
8061958d581SVladimir Oltean 	if (err)
8071958d581SVladimir Oltean 		return err;
8081958d581SVladimir Oltean 
8091958d581SVladimir Oltean 	return ocelot_vlan_add(ocelot, port, vlan->vid,
810183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_PVID,
811183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_UNTAGGED);
81256051948SVladimir Oltean }
81356051948SVladimir Oltean 
81456051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
81556051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
81656051948SVladimir Oltean {
81756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
81856051948SVladimir Oltean 
819b7a9e0daSVladimir Oltean 	return ocelot_vlan_del(ocelot, port, vlan->vid);
82056051948SVladimir Oltean }
82156051948SVladimir Oltean 
82279fda660SRussell King (Oracle) static void felix_phylink_get_caps(struct dsa_switch *ds, int port,
82379fda660SRussell King (Oracle) 				   struct phylink_config *config)
82479fda660SRussell King (Oracle) {
82579fda660SRussell King (Oracle) 	struct ocelot *ocelot = ds->priv;
82679fda660SRussell King (Oracle) 
827f6f04c02SRussell King (Oracle) 	/* This driver does not make use of the speed, duplex, pause or the
828f6f04c02SRussell King (Oracle) 	 * advertisement in its mac_config, so it is safe to mark this driver
829f6f04c02SRussell King (Oracle) 	 * as non-legacy.
830f6f04c02SRussell King (Oracle) 	 */
831f6f04c02SRussell King (Oracle) 	config->legacy_pre_march2020 = false;
832f6f04c02SRussell King (Oracle) 
83379fda660SRussell King (Oracle) 	__set_bit(ocelot->ports[port]->phy_mode,
83479fda660SRussell King (Oracle) 		  config->supported_interfaces);
83579fda660SRussell King (Oracle) }
83679fda660SRussell King (Oracle) 
837bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
838bdeced75SVladimir Oltean 				   unsigned long *supported,
839bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
840bdeced75SVladimir Oltean {
841bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
842375e1314SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
843bdeced75SVladimir Oltean 
844375e1314SVladimir Oltean 	if (felix->info->phylink_validate)
845375e1314SVladimir Oltean 		felix->info->phylink_validate(ocelot, port, supported, state);
846bdeced75SVladimir Oltean }
847bdeced75SVladimir Oltean 
848864ba485SRussell King (Oracle) static struct phylink_pcs *felix_phylink_mac_select_pcs(struct dsa_switch *ds,
849864ba485SRussell King (Oracle) 							int port,
850864ba485SRussell King (Oracle) 							phy_interface_t iface)
851bdeced75SVladimir Oltean {
852bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
853bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
854864ba485SRussell King (Oracle) 	struct phylink_pcs *pcs = NULL;
855bdeced75SVladimir Oltean 
85649af6a76SColin Foster 	if (felix->pcs && felix->pcs[port])
857864ba485SRussell King (Oracle) 		pcs = felix->pcs[port];
858864ba485SRussell King (Oracle) 
859864ba485SRussell King (Oracle) 	return pcs;
860bdeced75SVladimir Oltean }
861bdeced75SVladimir Oltean 
862bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
863bdeced75SVladimir Oltean 					unsigned int link_an_mode,
864bdeced75SVladimir Oltean 					phy_interface_t interface)
865bdeced75SVladimir Oltean {
866bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
867bdeced75SVladimir Oltean 
868e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
869e6e12df6SVladimir Oltean 				     FELIX_MAC_QUIRKS);
870bdeced75SVladimir Oltean }
871bdeced75SVladimir Oltean 
872bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
873bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
874bdeced75SVladimir Oltean 				      phy_interface_t interface,
8755b502a7bSRussell King 				      struct phy_device *phydev,
8765b502a7bSRussell King 				      int speed, int duplex,
8775b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
878bdeced75SVladimir Oltean {
879bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8807e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
881bdeced75SVladimir Oltean 
882e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
883e6e12df6SVladimir Oltean 				   interface, speed, duplex, tx_pause, rx_pause,
884e6e12df6SVladimir Oltean 				   FELIX_MAC_QUIRKS);
8857e14a2dcSVladimir Oltean 
8867e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
8877e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
888bdeced75SVladimir Oltean }
889bdeced75SVladimir Oltean 
890bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
891bd2b3161SXiaoliang Yang {
892bd2b3161SXiaoliang Yang 	int i;
893bd2b3161SXiaoliang Yang 
894bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
895bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
896bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
897bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
898bd2b3161SXiaoliang Yang 		       port);
899bd2b3161SXiaoliang Yang 
90070d39a6eSVladimir Oltean 	for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
901bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
902bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
903bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
904bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
905bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
906bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
907bd2b3161SXiaoliang Yang 			      port, i);
908bd2b3161SXiaoliang Yang 	}
909bd2b3161SXiaoliang Yang }
910bd2b3161SXiaoliang Yang 
91156051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
91256051948SVladimir Oltean 			      u32 stringset, u8 *data)
91356051948SVladimir Oltean {
91456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
91556051948SVladimir Oltean 
91656051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
91756051948SVladimir Oltean }
91856051948SVladimir Oltean 
91956051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
92056051948SVladimir Oltean {
92156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
92256051948SVladimir Oltean 
92356051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
92456051948SVladimir Oltean }
92556051948SVladimir Oltean 
92656051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
92756051948SVladimir Oltean {
92856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
92956051948SVladimir Oltean 
93056051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
93156051948SVladimir Oltean }
93256051948SVladimir Oltean 
93356051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
93456051948SVladimir Oltean 			     struct ethtool_ts_info *info)
93556051948SVladimir Oltean {
93656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
93756051948SVladimir Oltean 
93856051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
93956051948SVladimir Oltean }
94056051948SVladimir Oltean 
941bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
942bdeced75SVladimir Oltean 				  struct device_node *ports_node,
943bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
944bdeced75SVladimir Oltean {
945bdeced75SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
946bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
947bdeced75SVladimir Oltean 	struct device_node *child;
948bdeced75SVladimir Oltean 
94937fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
950bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
951bdeced75SVladimir Oltean 		u32 port;
952bdeced75SVladimir Oltean 		int err;
953bdeced75SVladimir Oltean 
954bdeced75SVladimir Oltean 		/* Get switch port number from DT */
955bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
956bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
957bdeced75SVladimir Oltean 				"(property \"reg\")\n");
958bdeced75SVladimir Oltean 			of_node_put(child);
959bdeced75SVladimir Oltean 			return -ENODEV;
960bdeced75SVladimir Oltean 		}
961bdeced75SVladimir Oltean 
962bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
963bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
964bdeced75SVladimir Oltean 		if (err) {
965bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
966bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
967bdeced75SVladimir Oltean 				port);
968bdeced75SVladimir Oltean 			of_node_put(child);
969bdeced75SVladimir Oltean 			return -ENODEV;
970bdeced75SVladimir Oltean 		}
971bdeced75SVladimir Oltean 
972bdeced75SVladimir Oltean 		err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode);
973bdeced75SVladimir Oltean 		if (err < 0) {
974bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
975bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
97659ebb430SSumera Priyadarsini 			of_node_put(child);
977bdeced75SVladimir Oltean 			return err;
978bdeced75SVladimir Oltean 		}
979bdeced75SVladimir Oltean 
980bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
981bdeced75SVladimir Oltean 	}
982bdeced75SVladimir Oltean 
983bdeced75SVladimir Oltean 	return 0;
984bdeced75SVladimir Oltean }
985bdeced75SVladimir Oltean 
986bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
987bdeced75SVladimir Oltean {
988bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
989bdeced75SVladimir Oltean 	struct device_node *switch_node;
990bdeced75SVladimir Oltean 	struct device_node *ports_node;
991bdeced75SVladimir Oltean 	int err;
992bdeced75SVladimir Oltean 
993bdeced75SVladimir Oltean 	switch_node = dev->of_node;
994bdeced75SVladimir Oltean 
995bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
996abecbfcdSVladimir Oltean 	if (!ports_node)
997abecbfcdSVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
998bdeced75SVladimir Oltean 	if (!ports_node) {
999abecbfcdSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n");
1000bdeced75SVladimir Oltean 		return -ENODEV;
1001bdeced75SVladimir Oltean 	}
1002bdeced75SVladimir Oltean 
1003bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
1004bdeced75SVladimir Oltean 	of_node_put(ports_node);
1005bdeced75SVladimir Oltean 
1006bdeced75SVladimir Oltean 	return err;
1007bdeced75SVladimir Oltean }
1008bdeced75SVladimir Oltean 
100956051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
101056051948SVladimir Oltean {
101156051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
1012bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
1013b4024c9eSClaudiu Manoil 	struct resource res;
101456051948SVladimir Oltean 	int port, i, err;
101556051948SVladimir Oltean 
101656051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
101756051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
101856051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
101956051948SVladimir Oltean 	if (!ocelot->ports)
102056051948SVladimir Oltean 		return -ENOMEM;
102156051948SVladimir Oltean 
102256051948SVladimir Oltean 	ocelot->map		= felix->info->map;
102356051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
102456051948SVladimir Oltean 	ocelot->num_stats	= felix->info->num_stats;
102521ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
102607d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
102777043c37SXiaoliang Yang 	ocelot->vcap_pol.base	= felix->info->vcap_pol_base;
102877043c37SXiaoliang Yang 	ocelot->vcap_pol.max	= felix->info->vcap_pol_max;
102977043c37SXiaoliang Yang 	ocelot->vcap_pol.base2	= felix->info->vcap_pol_base2;
103077043c37SXiaoliang Yang 	ocelot->vcap_pol.max2	= felix->info->vcap_pol_max2;
103156051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
1032cacea62fSVladimir Oltean 	ocelot->npi_inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
1033cacea62fSVladimir Oltean 	ocelot->npi_xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
1034f59fd9caSVladimir Oltean 	ocelot->devlink		= felix->ds->devlink;
103556051948SVladimir Oltean 
1036bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
1037bdeced75SVladimir Oltean 				 GFP_KERNEL);
1038bdeced75SVladimir Oltean 	if (!port_phy_modes)
1039bdeced75SVladimir Oltean 		return -ENOMEM;
1040bdeced75SVladimir Oltean 
1041bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
1042bdeced75SVladimir Oltean 	if (err) {
1043bdeced75SVladimir Oltean 		kfree(port_phy_modes);
1044bdeced75SVladimir Oltean 		return err;
1045bdeced75SVladimir Oltean 	}
1046bdeced75SVladimir Oltean 
104756051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
104856051948SVladimir Oltean 		struct regmap *target;
104956051948SVladimir Oltean 
105056051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
105156051948SVladimir Oltean 			continue;
105256051948SVladimir Oltean 
1053b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
1054b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1055375e1314SVladimir Oltean 		res.start += felix->switch_base;
1056375e1314SVladimir Oltean 		res.end += felix->switch_base;
105756051948SVladimir Oltean 
1058242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
105956051948SVladimir Oltean 		if (IS_ERR(target)) {
106056051948SVladimir Oltean 			dev_err(ocelot->dev,
106156051948SVladimir Oltean 				"Failed to map device memory space\n");
1062bdeced75SVladimir Oltean 			kfree(port_phy_modes);
106356051948SVladimir Oltean 			return PTR_ERR(target);
106456051948SVladimir Oltean 		}
106556051948SVladimir Oltean 
106656051948SVladimir Oltean 		ocelot->targets[i] = target;
106756051948SVladimir Oltean 	}
106856051948SVladimir Oltean 
106956051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
107056051948SVladimir Oltean 	if (err) {
107156051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
1072bdeced75SVladimir Oltean 		kfree(port_phy_modes);
107356051948SVladimir Oltean 		return err;
107456051948SVladimir Oltean 	}
107556051948SVladimir Oltean 
107656051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
107756051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
107891c724cfSVladimir Oltean 		struct regmap *target;
107956051948SVladimir Oltean 
108056051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
108156051948SVladimir Oltean 					   sizeof(struct ocelot_port),
108256051948SVladimir Oltean 					   GFP_KERNEL);
108356051948SVladimir Oltean 		if (!ocelot_port) {
108456051948SVladimir Oltean 			dev_err(ocelot->dev,
108556051948SVladimir Oltean 				"failed to allocate port memory\n");
1086bdeced75SVladimir Oltean 			kfree(port_phy_modes);
108756051948SVladimir Oltean 			return -ENOMEM;
108856051948SVladimir Oltean 		}
108956051948SVladimir Oltean 
1090b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
1091b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1092375e1314SVladimir Oltean 		res.start += felix->switch_base;
1093375e1314SVladimir Oltean 		res.end += felix->switch_base;
109456051948SVladimir Oltean 
1095242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
109691c724cfSVladimir Oltean 		if (IS_ERR(target)) {
109756051948SVladimir Oltean 			dev_err(ocelot->dev,
109891c724cfSVladimir Oltean 				"Failed to map memory space for port %d\n",
109991c724cfSVladimir Oltean 				port);
1100bdeced75SVladimir Oltean 			kfree(port_phy_modes);
110191c724cfSVladimir Oltean 			return PTR_ERR(target);
110256051948SVladimir Oltean 		}
110356051948SVladimir Oltean 
1104bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
110556051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
110691c724cfSVladimir Oltean 		ocelot_port->target = target;
110756051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
110856051948SVladimir Oltean 	}
110956051948SVladimir Oltean 
1110bdeced75SVladimir Oltean 	kfree(port_phy_modes);
1111bdeced75SVladimir Oltean 
1112bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
1113bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
1114bdeced75SVladimir Oltean 		if (err < 0)
1115bdeced75SVladimir Oltean 			return err;
1116bdeced75SVladimir Oltean 	}
1117bdeced75SVladimir Oltean 
111856051948SVladimir Oltean 	return 0;
111956051948SVladimir Oltean }
112056051948SVladimir Oltean 
11211328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port,
11221328a883SVladimir Oltean 					   struct sk_buff *skb)
11231328a883SVladimir Oltean {
11241328a883SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
11251328a883SVladimir Oltean 	struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
11261328a883SVladimir Oltean 	struct sk_buff *skb_match = NULL, *skb_tmp;
11271328a883SVladimir Oltean 	unsigned long flags;
11281328a883SVladimir Oltean 
11291328a883SVladimir Oltean 	if (!clone)
11301328a883SVladimir Oltean 		return;
11311328a883SVladimir Oltean 
11321328a883SVladimir Oltean 	spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags);
11331328a883SVladimir Oltean 
11341328a883SVladimir Oltean 	skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) {
11351328a883SVladimir Oltean 		if (skb != clone)
11361328a883SVladimir Oltean 			continue;
11371328a883SVladimir Oltean 		__skb_unlink(skb, &ocelot_port->tx_skbs);
11381328a883SVladimir Oltean 		skb_match = skb;
11391328a883SVladimir Oltean 		break;
11401328a883SVladimir Oltean 	}
11411328a883SVladimir Oltean 
11421328a883SVladimir Oltean 	spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags);
11431328a883SVladimir Oltean 
11441328a883SVladimir Oltean 	WARN_ONCE(!skb_match,
11451328a883SVladimir Oltean 		  "Could not find skb clone in TX timestamping list\n");
11461328a883SVladimir Oltean }
11471328a883SVladimir Oltean 
114849f885b2SVladimir Oltean #define work_to_xmit_work(w) \
114949f885b2SVladimir Oltean 		container_of((w), struct felix_deferred_xmit_work, work)
115049f885b2SVladimir Oltean 
115149f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work)
115249f885b2SVladimir Oltean {
115349f885b2SVladimir Oltean 	struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
115449f885b2SVladimir Oltean 	struct dsa_switch *ds = xmit_work->dp->ds;
115549f885b2SVladimir Oltean 	struct sk_buff *skb = xmit_work->skb;
115649f885b2SVladimir Oltean 	u32 rew_op = ocelot_ptp_rew_op(skb);
115749f885b2SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
115849f885b2SVladimir Oltean 	int port = xmit_work->dp->index;
115949f885b2SVladimir Oltean 	int retries = 10;
116049f885b2SVladimir Oltean 
116149f885b2SVladimir Oltean 	do {
116249f885b2SVladimir Oltean 		if (ocelot_can_inject(ocelot, 0))
116349f885b2SVladimir Oltean 			break;
116449f885b2SVladimir Oltean 
116549f885b2SVladimir Oltean 		cpu_relax();
116649f885b2SVladimir Oltean 	} while (--retries);
116749f885b2SVladimir Oltean 
116849f885b2SVladimir Oltean 	if (!retries) {
116949f885b2SVladimir Oltean 		dev_err(ocelot->dev, "port %d failed to inject skb\n",
117049f885b2SVladimir Oltean 			port);
11711328a883SVladimir Oltean 		ocelot_port_purge_txtstamp_skb(ocelot, port, skb);
117249f885b2SVladimir Oltean 		kfree_skb(skb);
117349f885b2SVladimir Oltean 		return;
117449f885b2SVladimir Oltean 	}
117549f885b2SVladimir Oltean 
117649f885b2SVladimir Oltean 	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
117749f885b2SVladimir Oltean 
117849f885b2SVladimir Oltean 	consume_skb(skb);
117949f885b2SVladimir Oltean 	kfree(xmit_work);
118049f885b2SVladimir Oltean }
118149f885b2SVladimir Oltean 
118235d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds,
118335d97680SVladimir Oltean 				      enum dsa_tag_protocol proto)
118449f885b2SVladimir Oltean {
118535d97680SVladimir Oltean 	struct ocelot_8021q_tagger_data *tagger_data;
118649f885b2SVladimir Oltean 
118735d97680SVladimir Oltean 	switch (proto) {
118835d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
118935d97680SVladimir Oltean 		tagger_data = ocelot_8021q_tagger_data(ds);
119035d97680SVladimir Oltean 		tagger_data->xmit_work_fn = felix_port_deferred_xmit;
119149f885b2SVladimir Oltean 		return 0;
119235d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
119335d97680SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
119449f885b2SVladimir Oltean 		return 0;
119535d97680SVladimir Oltean 	default:
119635d97680SVladimir Oltean 		return -EPROTONOSUPPORT;
119749f885b2SVladimir Oltean 	}
119849f885b2SVladimir Oltean }
119949f885b2SVladimir Oltean 
120056051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
120156051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
120256051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
120356051948SVladimir Oltean  * (which will not work).
120456051948SVladimir Oltean  */
120556051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
120656051948SVladimir Oltean {
120756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
120856051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
12092960bb14SVladimir Oltean 	struct dsa_port *dp;
12102960bb14SVladimir Oltean 	int err;
121156051948SVladimir Oltean 
121256051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
121356051948SVladimir Oltean 	if (err)
121456051948SVladimir Oltean 		return err;
121556051948SVladimir Oltean 
1216d1cc0e93SVladimir Oltean 	err = ocelot_init(ocelot);
1217d1cc0e93SVladimir Oltean 	if (err)
12186b73b7c9SVladimir Oltean 		goto out_mdiobus_free;
1219d1cc0e93SVladimir Oltean 
12202b49d128SYangbo Lu 	if (ocelot->ptp) {
12212ac7c6c5SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
12222b49d128SYangbo Lu 		if (err) {
12232b49d128SYangbo Lu 			dev_err(ocelot->dev,
12242b49d128SYangbo Lu 				"Timestamp initialization failed\n");
12252b49d128SYangbo Lu 			ocelot->ptp = 0;
12262b49d128SYangbo Lu 		}
12272b49d128SYangbo Lu 	}
122856051948SVladimir Oltean 
12292960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
12302960bb14SVladimir Oltean 		ocelot_init_port(ocelot, dp->index);
1231bd2b3161SXiaoliang Yang 
1232bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
1233bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
1234bd2b3161SXiaoliang Yang 		 */
12352960bb14SVladimir Oltean 		felix_port_qos_map_init(ocelot, dp->index);
123656051948SVladimir Oltean 	}
123756051948SVladimir Oltean 
1238f59fd9caSVladimir Oltean 	err = ocelot_devlink_sb_register(ocelot);
1239f59fd9caSVladimir Oltean 	if (err)
12406b73b7c9SVladimir Oltean 		goto out_deinit_ports;
1241f59fd9caSVladimir Oltean 
12422960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
1243adb3dccfSVladimir Oltean 		/* The initial tag protocol is NPI which always returns 0, so
1244adb3dccfSVladimir Oltean 		 * there's no real point in checking for errors.
12451cf3299bSVladimir Oltean 		 */
12462960bb14SVladimir Oltean 		felix_set_tag_protocol(ds, dp->index, felix->tag_proto);
12478d5f7954SVladimir Oltean 		break;
1248adb3dccfSVladimir Oltean 	}
12491cf3299bSVladimir Oltean 
12500b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
1251c54913c1SVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
1252*54c31984SVladimir Oltean 	ds->fdb_isolation = true;
1253*54c31984SVladimir Oltean 	ds->max_num_bridges = ds->num_ports;
1254bdeced75SVladimir Oltean 
125556051948SVladimir Oltean 	return 0;
12566b73b7c9SVladimir Oltean 
12576b73b7c9SVladimir Oltean out_deinit_ports:
12582960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
12592960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
12606b73b7c9SVladimir Oltean 
12616b73b7c9SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
12626b73b7c9SVladimir Oltean 	ocelot_deinit(ocelot);
12636b73b7c9SVladimir Oltean 
12646b73b7c9SVladimir Oltean out_mdiobus_free:
12656b73b7c9SVladimir Oltean 	if (felix->info->mdio_bus_free)
12666b73b7c9SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
12676b73b7c9SVladimir Oltean 
12686b73b7c9SVladimir Oltean 	return err;
126956051948SVladimir Oltean }
127056051948SVladimir Oltean 
127156051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
127256051948SVladimir Oltean {
127356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1274bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
12752960bb14SVladimir Oltean 	struct dsa_port *dp;
1276bdeced75SVladimir Oltean 
12772960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
12782960bb14SVladimir Oltean 		felix_del_tag_protocol(ds, dp->index, felix->tag_proto);
12798d5f7954SVladimir Oltean 		break;
1280adb3dccfSVladimir Oltean 	}
1281adb3dccfSVladimir Oltean 
12822960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
12832960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
1284d19741b0SVladimir Oltean 
128549f885b2SVladimir Oltean 	ocelot_devlink_sb_unregister(ocelot);
128649f885b2SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
128749f885b2SVladimir Oltean 	ocelot_deinit(ocelot);
128849f885b2SVladimir Oltean 
1289d19741b0SVladimir Oltean 	if (felix->info->mdio_bus_free)
1290d19741b0SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
129156051948SVladimir Oltean }
129256051948SVladimir Oltean 
1293c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
1294c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1295c0bcf537SYangbo Lu {
1296c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1297c0bcf537SYangbo Lu 
1298c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
1299c0bcf537SYangbo Lu }
1300c0bcf537SYangbo Lu 
1301c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
1302c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1303c0bcf537SYangbo Lu {
1304c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
130599348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
130699348004SVladimir Oltean 	bool using_tag_8021q;
130799348004SVladimir Oltean 	int err;
1308c0bcf537SYangbo Lu 
130999348004SVladimir Oltean 	err = ocelot_hwstamp_set(ocelot, port, ifr);
131099348004SVladimir Oltean 	if (err)
131199348004SVladimir Oltean 		return err;
131299348004SVladimir Oltean 
131399348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
131499348004SVladimir Oltean 
131599348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
1316c0bcf537SYangbo Lu }
1317c0bcf537SYangbo Lu 
13180a6f17c6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot, unsigned int ptp_type)
13190a6f17c6SVladimir Oltean {
13200a6f17c6SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
13210a6f17c6SVladimir Oltean 	int err, grp = 0;
13220a6f17c6SVladimir Oltean 
13230a6f17c6SVladimir Oltean 	if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q)
13240a6f17c6SVladimir Oltean 		return false;
13250a6f17c6SVladimir Oltean 
13260a6f17c6SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
13270a6f17c6SVladimir Oltean 		return false;
13280a6f17c6SVladimir Oltean 
13290a6f17c6SVladimir Oltean 	if (ptp_type == PTP_CLASS_NONE)
13300a6f17c6SVladimir Oltean 		return false;
13310a6f17c6SVladimir Oltean 
13320a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
13330a6f17c6SVladimir Oltean 		struct sk_buff *skb;
13340a6f17c6SVladimir Oltean 		unsigned int type;
13350a6f17c6SVladimir Oltean 
13360a6f17c6SVladimir Oltean 		err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
13370a6f17c6SVladimir Oltean 		if (err)
13380a6f17c6SVladimir Oltean 			goto out;
13390a6f17c6SVladimir Oltean 
13400a6f17c6SVladimir Oltean 		/* We trap to the CPU port module all PTP frames, but
13410a6f17c6SVladimir Oltean 		 * felix_rxtstamp() only gets called for event frames.
13420a6f17c6SVladimir Oltean 		 * So we need to avoid sending duplicate general
13430a6f17c6SVladimir Oltean 		 * message frames by running a second BPF classifier
13440a6f17c6SVladimir Oltean 		 * here and dropping those.
13450a6f17c6SVladimir Oltean 		 */
13460a6f17c6SVladimir Oltean 		__skb_push(skb, ETH_HLEN);
13470a6f17c6SVladimir Oltean 
13480a6f17c6SVladimir Oltean 		type = ptp_classify_raw(skb);
13490a6f17c6SVladimir Oltean 
13500a6f17c6SVladimir Oltean 		__skb_pull(skb, ETH_HLEN);
13510a6f17c6SVladimir Oltean 
13520a6f17c6SVladimir Oltean 		if (type == PTP_CLASS_NONE) {
13530a6f17c6SVladimir Oltean 			kfree_skb(skb);
13540a6f17c6SVladimir Oltean 			continue;
13550a6f17c6SVladimir Oltean 		}
13560a6f17c6SVladimir Oltean 
13570a6f17c6SVladimir Oltean 		netif_rx(skb);
13580a6f17c6SVladimir Oltean 	}
13590a6f17c6SVladimir Oltean 
13600a6f17c6SVladimir Oltean out:
13610a6f17c6SVladimir Oltean 	if (err < 0)
13620a6f17c6SVladimir Oltean 		ocelot_drain_cpu_queue(ocelot, 0);
13630a6f17c6SVladimir Oltean 
13640a6f17c6SVladimir Oltean 	return true;
13650a6f17c6SVladimir Oltean }
13660a6f17c6SVladimir Oltean 
1367c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
1368c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
1369c0bcf537SYangbo Lu {
137092f62485SVladimir Oltean 	u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo;
1371c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
1372c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1373c0bcf537SYangbo Lu 	struct timespec64 ts;
137492f62485SVladimir Oltean 	u32 tstamp_hi;
137592f62485SVladimir Oltean 	u64 tstamp;
1376c0bcf537SYangbo Lu 
13770a6f17c6SVladimir Oltean 	/* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb
13780a6f17c6SVladimir Oltean 	 * for RX timestamping. Then free it, and poll for its copy through
13790a6f17c6SVladimir Oltean 	 * MMIO in the CPU port module, and inject that into the stack from
13800a6f17c6SVladimir Oltean 	 * ocelot_xtr_poll().
13810a6f17c6SVladimir Oltean 	 */
13820a6f17c6SVladimir Oltean 	if (felix_check_xtr_pkt(ocelot, type)) {
13830a6f17c6SVladimir Oltean 		kfree_skb(skb);
13840a6f17c6SVladimir Oltean 		return true;
13850a6f17c6SVladimir Oltean 	}
13860a6f17c6SVladimir Oltean 
1387c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1388c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1389c0bcf537SYangbo Lu 
1390c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
1391c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
1392c0bcf537SYangbo Lu 		tstamp_hi--;
1393c0bcf537SYangbo Lu 
1394c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
1395c0bcf537SYangbo Lu 
1396c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
1397c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1398c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
1399c0bcf537SYangbo Lu 	return false;
1400c0bcf537SYangbo Lu }
1401c0bcf537SYangbo Lu 
14025c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port,
14035c5416f5SYangbo Lu 			   struct sk_buff *skb)
1404c0bcf537SYangbo Lu {
1405c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1406682eaad9SYangbo Lu 	struct sk_buff *clone = NULL;
1407c0bcf537SYangbo Lu 
1408682eaad9SYangbo Lu 	if (!ocelot->ptp)
14095c5416f5SYangbo Lu 		return;
1410c0bcf537SYangbo Lu 
141152849bcfSVladimir Oltean 	if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
141252849bcfSVladimir Oltean 		dev_err_ratelimited(ds->dev,
141352849bcfSVladimir Oltean 				    "port %d delivering skb without TX timestamp\n",
141452849bcfSVladimir Oltean 				    port);
1415682eaad9SYangbo Lu 		return;
141652849bcfSVladimir Oltean 	}
1417682eaad9SYangbo Lu 
1418682eaad9SYangbo Lu 	if (clone)
1419c4b364ceSYangbo Lu 		OCELOT_SKB_CB(skb)->clone = clone;
14205c5416f5SYangbo Lu }
1421c0bcf537SYangbo Lu 
14220b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
14230b912fc9SVladimir Oltean {
14240b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
14250b912fc9SVladimir Oltean 
14260b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
14270b912fc9SVladimir Oltean 
14280b912fc9SVladimir Oltean 	return 0;
14290b912fc9SVladimir Oltean }
14300b912fc9SVladimir Oltean 
14310b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
14320b912fc9SVladimir Oltean {
14330b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
14340b912fc9SVladimir Oltean 
14350b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
14360b912fc9SVladimir Oltean }
14370b912fc9SVladimir Oltean 
143807d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
143907d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
144007d985eeSVladimir Oltean {
144107d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
144299348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
144399348004SVladimir Oltean 	bool using_tag_8021q;
144499348004SVladimir Oltean 	int err;
144507d985eeSVladimir Oltean 
144699348004SVladimir Oltean 	err = ocelot_cls_flower_replace(ocelot, port, cls, ingress);
144799348004SVladimir Oltean 	if (err)
144899348004SVladimir Oltean 		return err;
144999348004SVladimir Oltean 
145099348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
145199348004SVladimir Oltean 
145299348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
145307d985eeSVladimir Oltean }
145407d985eeSVladimir Oltean 
145507d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
145607d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
145707d985eeSVladimir Oltean {
145807d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
145907d985eeSVladimir Oltean 
146007d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
146107d985eeSVladimir Oltean }
146207d985eeSVladimir Oltean 
146307d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
146407d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
146507d985eeSVladimir Oltean {
146607d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
146707d985eeSVladimir Oltean 
146807d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
146907d985eeSVladimir Oltean }
147007d985eeSVladimir Oltean 
1471fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
1472fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
1473fc411eaaSVladimir Oltean {
1474fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1475fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
1476fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
14775f035af7SPo Liu 		.burst = policer->burst,
1478fc411eaaSVladimir Oltean 	};
1479fc411eaaSVladimir Oltean 
1480fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
1481fc411eaaSVladimir Oltean }
1482fc411eaaSVladimir Oltean 
1483fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
1484fc411eaaSVladimir Oltean {
1485fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1486fc411eaaSVladimir Oltean 
1487fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
1488fc411eaaSVladimir Oltean }
1489fc411eaaSVladimir Oltean 
1490de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
1491de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
1492de143c0eSXiaoliang Yang 			       void *type_data)
1493de143c0eSXiaoliang Yang {
1494de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
1495de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
1496de143c0eSXiaoliang Yang 
1497de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
1498de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
1499de143c0eSXiaoliang Yang 	else
1500de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
1501de143c0eSXiaoliang Yang }
1502de143c0eSXiaoliang Yang 
1503f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
1504f59fd9caSVladimir Oltean 			     u16 pool_index,
1505f59fd9caSVladimir Oltean 			     struct devlink_sb_pool_info *pool_info)
1506f59fd9caSVladimir Oltean {
1507f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1508f59fd9caSVladimir Oltean 
1509f59fd9caSVladimir Oltean 	return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
1510f59fd9caSVladimir Oltean }
1511f59fd9caSVladimir Oltean 
1512f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
1513f59fd9caSVladimir Oltean 			     u16 pool_index, u32 size,
1514f59fd9caSVladimir Oltean 			     enum devlink_sb_threshold_type threshold_type,
1515f59fd9caSVladimir Oltean 			     struct netlink_ext_ack *extack)
1516f59fd9caSVladimir Oltean {
1517f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1518f59fd9caSVladimir Oltean 
1519f59fd9caSVladimir Oltean 	return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
1520f59fd9caSVladimir Oltean 				  threshold_type, extack);
1521f59fd9caSVladimir Oltean }
1522f59fd9caSVladimir Oltean 
1523f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
1524f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1525f59fd9caSVladimir Oltean 				  u32 *p_threshold)
1526f59fd9caSVladimir Oltean {
1527f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1528f59fd9caSVladimir Oltean 
1529f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
1530f59fd9caSVladimir Oltean 				       p_threshold);
1531f59fd9caSVladimir Oltean }
1532f59fd9caSVladimir Oltean 
1533f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
1534f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1535f59fd9caSVladimir Oltean 				  u32 threshold, struct netlink_ext_ack *extack)
1536f59fd9caSVladimir Oltean {
1537f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1538f59fd9caSVladimir Oltean 
1539f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
1540f59fd9caSVladimir Oltean 				       threshold, extack);
1541f59fd9caSVladimir Oltean }
1542f59fd9caSVladimir Oltean 
1543f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
1544f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1545f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1546f59fd9caSVladimir Oltean 				     u16 *p_pool_index, u32 *p_threshold)
1547f59fd9caSVladimir Oltean {
1548f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1549f59fd9caSVladimir Oltean 
1550f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
1551f59fd9caSVladimir Oltean 					  pool_type, p_pool_index,
1552f59fd9caSVladimir Oltean 					  p_threshold);
1553f59fd9caSVladimir Oltean }
1554f59fd9caSVladimir Oltean 
1555f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
1556f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1557f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1558f59fd9caSVladimir Oltean 				     u16 pool_index, u32 threshold,
1559f59fd9caSVladimir Oltean 				     struct netlink_ext_ack *extack)
1560f59fd9caSVladimir Oltean {
1561f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1562f59fd9caSVladimir Oltean 
1563f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
1564f59fd9caSVladimir Oltean 					  pool_type, pool_index, threshold,
1565f59fd9caSVladimir Oltean 					  extack);
1566f59fd9caSVladimir Oltean }
1567f59fd9caSVladimir Oltean 
1568f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds,
1569f59fd9caSVladimir Oltean 				 unsigned int sb_index)
1570f59fd9caSVladimir Oltean {
1571f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1572f59fd9caSVladimir Oltean 
1573f59fd9caSVladimir Oltean 	return ocelot_sb_occ_snapshot(ocelot, sb_index);
1574f59fd9caSVladimir Oltean }
1575f59fd9caSVladimir Oltean 
1576f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds,
1577f59fd9caSVladimir Oltean 				  unsigned int sb_index)
1578f59fd9caSVladimir Oltean {
1579f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1580f59fd9caSVladimir Oltean 
1581f59fd9caSVladimir Oltean 	return ocelot_sb_occ_max_clear(ocelot, sb_index);
1582f59fd9caSVladimir Oltean }
1583f59fd9caSVladimir Oltean 
1584f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
1585f59fd9caSVladimir Oltean 				      unsigned int sb_index, u16 pool_index,
1586f59fd9caSVladimir Oltean 				      u32 *p_cur, u32 *p_max)
1587f59fd9caSVladimir Oltean {
1588f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1589f59fd9caSVladimir Oltean 
1590f59fd9caSVladimir Oltean 	return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
1591f59fd9caSVladimir Oltean 					   p_cur, p_max);
1592f59fd9caSVladimir Oltean }
1593f59fd9caSVladimir Oltean 
1594f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
1595f59fd9caSVladimir Oltean 					 unsigned int sb_index, u16 tc_index,
1596f59fd9caSVladimir Oltean 					 enum devlink_sb_pool_type pool_type,
1597f59fd9caSVladimir Oltean 					 u32 *p_cur, u32 *p_max)
1598f59fd9caSVladimir Oltean {
1599f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1600f59fd9caSVladimir Oltean 
1601f59fd9caSVladimir Oltean 	return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
1602f59fd9caSVladimir Oltean 					      pool_type, p_cur, p_max);
1603f59fd9caSVladimir Oltean }
1604f59fd9caSVladimir Oltean 
1605a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port,
1606a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1607a026c50bSHoratiu Vultur {
1608a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1609a026c50bSHoratiu Vultur 
1610a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1611a026c50bSHoratiu Vultur }
1612a026c50bSHoratiu Vultur 
1613a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port,
1614a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1615a026c50bSHoratiu Vultur {
1616a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1617a026c50bSHoratiu Vultur 
1618a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1619a026c50bSHoratiu Vultur }
1620a026c50bSHoratiu Vultur 
1621a026c50bSHoratiu Vultur static int
1622a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port,
1623a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1624a026c50bSHoratiu Vultur {
1625a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1626a026c50bSHoratiu Vultur 
1627a026c50bSHoratiu Vultur 	return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1628a026c50bSHoratiu Vultur }
1629a026c50bSHoratiu Vultur 
1630a026c50bSHoratiu Vultur static int
1631a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port,
1632a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1633a026c50bSHoratiu Vultur {
1634a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1635a026c50bSHoratiu Vultur 
1636a026c50bSHoratiu Vultur 	return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1637a026c50bSHoratiu Vultur }
1638a026c50bSHoratiu Vultur 
1639375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = {
164056051948SVladimir Oltean 	.get_tag_protocol		= felix_get_tag_protocol,
1641adb3dccfSVladimir Oltean 	.change_tag_protocol		= felix_change_tag_protocol,
164235d97680SVladimir Oltean 	.connect_tag_protocol		= felix_connect_tag_protocol,
164356051948SVladimir Oltean 	.setup				= felix_setup,
164456051948SVladimir Oltean 	.teardown			= felix_teardown,
164556051948SVladimir Oltean 	.set_ageing_time		= felix_set_ageing_time,
164656051948SVladimir Oltean 	.get_strings			= felix_get_strings,
164756051948SVladimir Oltean 	.get_ethtool_stats		= felix_get_ethtool_stats,
164856051948SVladimir Oltean 	.get_sset_count			= felix_get_sset_count,
164956051948SVladimir Oltean 	.get_ts_info			= felix_get_ts_info,
165079fda660SRussell King (Oracle) 	.phylink_get_caps		= felix_phylink_get_caps,
1651bdeced75SVladimir Oltean 	.phylink_validate		= felix_phylink_validate,
1652864ba485SRussell King (Oracle) 	.phylink_mac_select_pcs		= felix_phylink_mac_select_pcs,
1653bdeced75SVladimir Oltean 	.phylink_mac_link_down		= felix_phylink_mac_link_down,
1654bdeced75SVladimir Oltean 	.phylink_mac_link_up		= felix_phylink_mac_link_up,
16555cad43a5SVladimir Oltean 	.port_fast_age			= felix_port_fast_age,
165656051948SVladimir Oltean 	.port_fdb_dump			= felix_fdb_dump,
165756051948SVladimir Oltean 	.port_fdb_add			= felix_fdb_add,
165856051948SVladimir Oltean 	.port_fdb_del			= felix_fdb_del,
1659961d8b69SVladimir Oltean 	.lag_fdb_add			= felix_lag_fdb_add,
1660961d8b69SVladimir Oltean 	.lag_fdb_del			= felix_lag_fdb_del,
1661209edf95SVladimir Oltean 	.port_mdb_add			= felix_mdb_add,
1662209edf95SVladimir Oltean 	.port_mdb_del			= felix_mdb_del,
1663421741eaSVladimir Oltean 	.port_pre_bridge_flags		= felix_pre_bridge_flags,
1664421741eaSVladimir Oltean 	.port_bridge_flags		= felix_bridge_flags,
166556051948SVladimir Oltean 	.port_bridge_join		= felix_bridge_join,
166656051948SVladimir Oltean 	.port_bridge_leave		= felix_bridge_leave,
16678fe6832eSVladimir Oltean 	.port_lag_join			= felix_lag_join,
16688fe6832eSVladimir Oltean 	.port_lag_leave			= felix_lag_leave,
16698fe6832eSVladimir Oltean 	.port_lag_change		= felix_lag_change,
167056051948SVladimir Oltean 	.port_stp_state_set		= felix_bridge_stp_state_set,
167156051948SVladimir Oltean 	.port_vlan_filtering		= felix_vlan_filtering,
167256051948SVladimir Oltean 	.port_vlan_add			= felix_vlan_add,
167356051948SVladimir Oltean 	.port_vlan_del			= felix_vlan_del,
1674c0bcf537SYangbo Lu 	.port_hwtstamp_get		= felix_hwtstamp_get,
1675c0bcf537SYangbo Lu 	.port_hwtstamp_set		= felix_hwtstamp_set,
1676c0bcf537SYangbo Lu 	.port_rxtstamp			= felix_rxtstamp,
1677c0bcf537SYangbo Lu 	.port_txtstamp			= felix_txtstamp,
16780b912fc9SVladimir Oltean 	.port_change_mtu		= felix_change_mtu,
16790b912fc9SVladimir Oltean 	.port_max_mtu			= felix_get_max_mtu,
1680fc411eaaSVladimir Oltean 	.port_policer_add		= felix_port_policer_add,
1681fc411eaaSVladimir Oltean 	.port_policer_del		= felix_port_policer_del,
168207d985eeSVladimir Oltean 	.cls_flower_add			= felix_cls_flower_add,
168307d985eeSVladimir Oltean 	.cls_flower_del			= felix_cls_flower_del,
168407d985eeSVladimir Oltean 	.cls_flower_stats		= felix_cls_flower_stats,
1685de143c0eSXiaoliang Yang 	.port_setup_tc			= felix_port_setup_tc,
1686f59fd9caSVladimir Oltean 	.devlink_sb_pool_get		= felix_sb_pool_get,
1687f59fd9caSVladimir Oltean 	.devlink_sb_pool_set		= felix_sb_pool_set,
1688f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_get	= felix_sb_port_pool_get,
1689f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_set	= felix_sb_port_pool_set,
1690f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_get	= felix_sb_tc_pool_bind_get,
1691f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_set	= felix_sb_tc_pool_bind_set,
1692f59fd9caSVladimir Oltean 	.devlink_sb_occ_snapshot	= felix_sb_occ_snapshot,
1693f59fd9caSVladimir Oltean 	.devlink_sb_occ_max_clear	= felix_sb_occ_max_clear,
1694f59fd9caSVladimir Oltean 	.devlink_sb_occ_port_pool_get	= felix_sb_occ_port_pool_get,
1695f59fd9caSVladimir Oltean 	.devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
1696a026c50bSHoratiu Vultur 	.port_mrp_add			= felix_mrp_add,
1697a026c50bSHoratiu Vultur 	.port_mrp_del			= felix_mrp_del,
1698a026c50bSHoratiu Vultur 	.port_mrp_add_ring_role		= felix_mrp_add_ring_role,
1699a026c50bSHoratiu Vultur 	.port_mrp_del_ring_role		= felix_mrp_del_ring_role,
17005da11eb4SVladimir Oltean 	.tag_8021q_vlan_add		= felix_tag_8021q_vlan_add,
17015da11eb4SVladimir Oltean 	.tag_8021q_vlan_del		= felix_tag_8021q_vlan_del,
170256051948SVladimir Oltean };
1703319e4dd1SVladimir Oltean 
1704319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
1705319e4dd1SVladimir Oltean {
1706319e4dd1SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1707319e4dd1SVladimir Oltean 	struct dsa_switch *ds = felix->ds;
1708319e4dd1SVladimir Oltean 
1709319e4dd1SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
1710319e4dd1SVladimir Oltean 		return NULL;
1711319e4dd1SVladimir Oltean 
1712319e4dd1SVladimir Oltean 	return dsa_to_port(ds, port)->slave;
1713319e4dd1SVladimir Oltean }
1714319e4dd1SVladimir Oltean 
1715319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev)
1716319e4dd1SVladimir Oltean {
1717319e4dd1SVladimir Oltean 	struct dsa_port *dp;
1718319e4dd1SVladimir Oltean 
1719319e4dd1SVladimir Oltean 	dp = dsa_port_from_netdev(dev);
1720319e4dd1SVladimir Oltean 	if (IS_ERR(dp))
1721319e4dd1SVladimir Oltean 		return -EINVAL;
1722319e4dd1SVladimir Oltean 
1723319e4dd1SVladimir Oltean 	return dp->index;
1724319e4dd1SVladimir Oltean }
1725