xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision dedd6a009f4191989bee83c1faf66728648a223f)
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 
28e21268efSVladimir Oltean static int felix_tag_8021q_rxvlan_add(struct felix *felix, int port, u16 vid,
29e21268efSVladimir Oltean 				      bool pvid, bool untagged)
30e21268efSVladimir Oltean {
31e21268efSVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
32e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
33e21268efSVladimir Oltean 	struct dsa_switch *ds = felix->ds;
34e21268efSVladimir Oltean 	int key_length, upstream, err;
35e21268efSVladimir Oltean 
36e21268efSVladimir Oltean 	/* We don't need to install the rxvlan into the other ports' filtering
37e21268efSVladimir Oltean 	 * tables, because we're just pushing the rxvlan when sending towards
38e21268efSVladimir Oltean 	 * the CPU
39e21268efSVladimir Oltean 	 */
40e21268efSVladimir Oltean 	if (!pvid)
41e21268efSVladimir Oltean 		return 0;
42e21268efSVladimir Oltean 
43e21268efSVladimir Oltean 	key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length;
44e21268efSVladimir Oltean 	upstream = dsa_upstream_port(ds, port);
45e21268efSVladimir Oltean 
46e21268efSVladimir Oltean 	outer_tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter),
47e21268efSVladimir Oltean 				     GFP_KERNEL);
48e21268efSVladimir Oltean 	if (!outer_tagging_rule)
49e21268efSVladimir Oltean 		return -ENOMEM;
50e21268efSVladimir Oltean 
51e21268efSVladimir Oltean 	outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
52e21268efSVladimir Oltean 	outer_tagging_rule->prio = 1;
53c518afecSVladimir Oltean 	outer_tagging_rule->id.cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port);
54e21268efSVladimir Oltean 	outer_tagging_rule->id.tc_offload = false;
55e21268efSVladimir Oltean 	outer_tagging_rule->block_id = VCAP_ES0;
56e21268efSVladimir Oltean 	outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
57e21268efSVladimir Oltean 	outer_tagging_rule->lookup = 0;
58e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.value = port;
59e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0);
60e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.value = upstream;
61e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0);
62e21268efSVladimir Oltean 	outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG;
63e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD;
64e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_vid_sel = 1;
65e21268efSVladimir Oltean 	outer_tagging_rule->action.vid_a_val = vid;
66e21268efSVladimir Oltean 
67e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL);
68e21268efSVladimir Oltean 	if (err)
69e21268efSVladimir Oltean 		kfree(outer_tagging_rule);
70e21268efSVladimir Oltean 
71e21268efSVladimir Oltean 	return err;
72e21268efSVladimir Oltean }
73e21268efSVladimir Oltean 
74e21268efSVladimir Oltean static int felix_tag_8021q_txvlan_add(struct felix *felix, int port, u16 vid,
75e21268efSVladimir Oltean 				      bool pvid, bool untagged)
76e21268efSVladimir Oltean {
77e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
78e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
79e21268efSVladimir Oltean 	struct dsa_switch *ds = felix->ds;
80e21268efSVladimir Oltean 	int upstream, err;
81e21268efSVladimir Oltean 
82e21268efSVladimir Oltean 	/* tag_8021q.c assumes we are implementing this via port VLAN
83e21268efSVladimir Oltean 	 * membership, which we aren't. So we don't need to add any VCAP filter
84e21268efSVladimir Oltean 	 * for the CPU port.
85e21268efSVladimir Oltean 	 */
86e21268efSVladimir Oltean 	if (ocelot->ports[port]->is_dsa_8021q_cpu)
87e21268efSVladimir Oltean 		return 0;
88e21268efSVladimir Oltean 
89e21268efSVladimir Oltean 	untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
90e21268efSVladimir Oltean 	if (!untagging_rule)
91e21268efSVladimir Oltean 		return -ENOMEM;
92e21268efSVladimir Oltean 
93e21268efSVladimir Oltean 	redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
94e21268efSVladimir Oltean 	if (!redirect_rule) {
95e21268efSVladimir Oltean 		kfree(untagging_rule);
96e21268efSVladimir Oltean 		return -ENOMEM;
97e21268efSVladimir Oltean 	}
98e21268efSVladimir Oltean 
99e21268efSVladimir Oltean 	upstream = dsa_upstream_port(ds, port);
100e21268efSVladimir Oltean 
101e21268efSVladimir Oltean 	untagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
102e21268efSVladimir Oltean 	untagging_rule->ingress_port_mask = BIT(upstream);
103e21268efSVladimir Oltean 	untagging_rule->vlan.vid.value = vid;
104e21268efSVladimir Oltean 	untagging_rule->vlan.vid.mask = VLAN_VID_MASK;
105e21268efSVladimir Oltean 	untagging_rule->prio = 1;
106c518afecSVladimir Oltean 	untagging_rule->id.cookie = OCELOT_VCAP_IS1_TAG_8021Q_TXVLAN(ocelot, port);
107e21268efSVladimir Oltean 	untagging_rule->id.tc_offload = false;
108e21268efSVladimir Oltean 	untagging_rule->block_id = VCAP_IS1;
109e21268efSVladimir Oltean 	untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
110e21268efSVladimir Oltean 	untagging_rule->lookup = 0;
111e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt_ena = true;
112e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt = 1;
113e21268efSVladimir Oltean 	untagging_rule->action.pag_override_mask = 0xff;
114e21268efSVladimir Oltean 	untagging_rule->action.pag_val = port;
115e21268efSVladimir Oltean 
116e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL);
117e21268efSVladimir Oltean 	if (err) {
118e21268efSVladimir Oltean 		kfree(untagging_rule);
119e21268efSVladimir Oltean 		kfree(redirect_rule);
120e21268efSVladimir Oltean 		return err;
121e21268efSVladimir Oltean 	}
122e21268efSVladimir Oltean 
123e21268efSVladimir Oltean 	redirect_rule->key_type = OCELOT_VCAP_KEY_ANY;
124e21268efSVladimir Oltean 	redirect_rule->ingress_port_mask = BIT(upstream);
125e21268efSVladimir Oltean 	redirect_rule->pag = port;
126e21268efSVladimir Oltean 	redirect_rule->prio = 1;
127c518afecSVladimir Oltean 	redirect_rule->id.cookie = OCELOT_VCAP_IS2_TAG_8021Q_TXVLAN(ocelot, port);
128e21268efSVladimir Oltean 	redirect_rule->id.tc_offload = false;
129e21268efSVladimir Oltean 	redirect_rule->block_id = VCAP_IS2;
130e21268efSVladimir Oltean 	redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
131e21268efSVladimir Oltean 	redirect_rule->lookup = 0;
132e21268efSVladimir Oltean 	redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT;
133e21268efSVladimir Oltean 	redirect_rule->action.port_mask = BIT(port);
134e21268efSVladimir Oltean 
135e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL);
136e21268efSVladimir Oltean 	if (err) {
137e21268efSVladimir Oltean 		ocelot_vcap_filter_del(ocelot, untagging_rule);
138e21268efSVladimir Oltean 		kfree(redirect_rule);
139e21268efSVladimir Oltean 		return err;
140e21268efSVladimir Oltean 	}
141e21268efSVladimir Oltean 
142e21268efSVladimir Oltean 	return 0;
143e21268efSVladimir Oltean }
144e21268efSVladimir Oltean 
145e21268efSVladimir Oltean static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
146e21268efSVladimir Oltean 				    u16 flags)
147e21268efSVladimir Oltean {
148e21268efSVladimir Oltean 	bool untagged = flags & BRIDGE_VLAN_INFO_UNTAGGED;
149e21268efSVladimir Oltean 	bool pvid = flags & BRIDGE_VLAN_INFO_PVID;
150e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
151e21268efSVladimir Oltean 
152e21268efSVladimir Oltean 	if (vid_is_dsa_8021q_rxvlan(vid))
153e21268efSVladimir Oltean 		return felix_tag_8021q_rxvlan_add(ocelot_to_felix(ocelot),
154e21268efSVladimir Oltean 						  port, vid, pvid, untagged);
155e21268efSVladimir Oltean 
156e21268efSVladimir Oltean 	if (vid_is_dsa_8021q_txvlan(vid))
157e21268efSVladimir Oltean 		return felix_tag_8021q_txvlan_add(ocelot_to_felix(ocelot),
158e21268efSVladimir Oltean 						  port, vid, pvid, untagged);
159e21268efSVladimir Oltean 
160e21268efSVladimir Oltean 	return 0;
161e21268efSVladimir Oltean }
162e21268efSVladimir Oltean 
163e21268efSVladimir Oltean static int felix_tag_8021q_rxvlan_del(struct felix *felix, int port, u16 vid)
164e21268efSVladimir Oltean {
165e21268efSVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
166e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_es0;
167e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
168e21268efSVladimir Oltean 
169e21268efSVladimir Oltean 	block_vcap_es0 = &ocelot->block[VCAP_ES0];
170e21268efSVladimir Oltean 
171e21268efSVladimir Oltean 	outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0,
172e21268efSVladimir Oltean 								 port, false);
173e21268efSVladimir Oltean 	/* In rxvlan_add, we had the "if (!pvid) return 0" logic to avoid
174e21268efSVladimir Oltean 	 * installing outer tagging ES0 rules where they weren't needed.
175e21268efSVladimir Oltean 	 * But in rxvlan_del, the API doesn't give us the "flags" anymore,
176e21268efSVladimir Oltean 	 * so that forces us to be slightly sloppy here, and just assume that
177e21268efSVladimir Oltean 	 * if we didn't find an outer_tagging_rule it means that there was
178e21268efSVladimir Oltean 	 * none in the first place, i.e. rxvlan_del is called on a non-pvid
179e21268efSVladimir Oltean 	 * port. This is most probably true though.
180e21268efSVladimir Oltean 	 */
181e21268efSVladimir Oltean 	if (!outer_tagging_rule)
182e21268efSVladimir Oltean 		return 0;
183e21268efSVladimir Oltean 
184e21268efSVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, outer_tagging_rule);
185e21268efSVladimir Oltean }
186e21268efSVladimir Oltean 
187e21268efSVladimir Oltean static int felix_tag_8021q_txvlan_del(struct felix *felix, int port, u16 vid)
188e21268efSVladimir Oltean {
189e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
190e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is1;
191e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
192e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
193e21268efSVladimir Oltean 	int err;
194e21268efSVladimir Oltean 
195e21268efSVladimir Oltean 	if (ocelot->ports[port]->is_dsa_8021q_cpu)
196e21268efSVladimir Oltean 		return 0;
197e21268efSVladimir Oltean 
198e21268efSVladimir Oltean 	block_vcap_is1 = &ocelot->block[VCAP_IS1];
199e21268efSVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
200e21268efSVladimir Oltean 
201e21268efSVladimir Oltean 	untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1,
202e21268efSVladimir Oltean 							     port, false);
203e21268efSVladimir Oltean 	if (!untagging_rule)
204e21268efSVladimir Oltean 		return 0;
205e21268efSVladimir Oltean 
206e21268efSVladimir Oltean 	err = ocelot_vcap_filter_del(ocelot, untagging_rule);
207e21268efSVladimir Oltean 	if (err)
208e21268efSVladimir Oltean 		return err;
209e21268efSVladimir Oltean 
210e21268efSVladimir Oltean 	redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2,
211e21268efSVladimir Oltean 							    port, false);
212e21268efSVladimir Oltean 	if (!redirect_rule)
213e21268efSVladimir Oltean 		return 0;
214e21268efSVladimir Oltean 
215e21268efSVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, redirect_rule);
216e21268efSVladimir Oltean }
217e21268efSVladimir Oltean 
218e21268efSVladimir Oltean static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
219e21268efSVladimir Oltean {
220e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
221e21268efSVladimir Oltean 
222e21268efSVladimir Oltean 	if (vid_is_dsa_8021q_rxvlan(vid))
223e21268efSVladimir Oltean 		return felix_tag_8021q_rxvlan_del(ocelot_to_felix(ocelot),
224e21268efSVladimir Oltean 						  port, vid);
225e21268efSVladimir Oltean 
226e21268efSVladimir Oltean 	if (vid_is_dsa_8021q_txvlan(vid))
227e21268efSVladimir Oltean 		return felix_tag_8021q_txvlan_del(ocelot_to_felix(ocelot),
228e21268efSVladimir Oltean 						  port, vid);
229e21268efSVladimir Oltean 
230e21268efSVladimir Oltean 	return 0;
231e21268efSVladimir Oltean }
232e21268efSVladimir Oltean 
233e21268efSVladimir Oltean /* Alternatively to using the NPI functionality, that same hardware MAC
234e21268efSVladimir Oltean  * connected internally to the enetc or fman DSA master can be configured to
235e21268efSVladimir Oltean  * use the software-defined tag_8021q frame format. As far as the hardware is
236e21268efSVladimir Oltean  * concerned, it thinks it is a "dumb switch" - the queues of the CPU port
237e21268efSVladimir Oltean  * module are now disconnected from it, but can still be accessed through
238e21268efSVladimir Oltean  * register-based MMIO.
239e21268efSVladimir Oltean  */
240e21268efSVladimir Oltean static void felix_8021q_cpu_port_init(struct ocelot *ocelot, int port)
241e21268efSVladimir Oltean {
2428abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2438abe1970SVladimir Oltean 
244e21268efSVladimir Oltean 	ocelot->ports[port]->is_dsa_8021q_cpu = true;
245e21268efSVladimir Oltean 	ocelot->npi = -1;
246e21268efSVladimir Oltean 
247e21268efSVladimir Oltean 	/* Overwrite PGID_CPU with the non-tagging port */
248e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, PGID_CPU);
249e21268efSVladimir Oltean 
2508abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
2518abe1970SVladimir Oltean 
2528abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
253e21268efSVladimir Oltean }
254e21268efSVladimir Oltean 
255e21268efSVladimir Oltean static void felix_8021q_cpu_port_deinit(struct ocelot *ocelot, int port)
256e21268efSVladimir Oltean {
2578abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2588abe1970SVladimir Oltean 
259e21268efSVladimir Oltean 	ocelot->ports[port]->is_dsa_8021q_cpu = false;
260e21268efSVladimir Oltean 
261e21268efSVladimir Oltean 	/* Restore PGID_CPU */
262e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(ocelot->num_phys_ports), ANA_PGID_PGID,
263e21268efSVladimir Oltean 			 PGID_CPU);
264e21268efSVladimir Oltean 
2658abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
2668abe1970SVladimir Oltean 
2678abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
268e21268efSVladimir Oltean }
269e21268efSVladimir Oltean 
27099348004SVladimir Oltean /* On switches with no extraction IRQ wired, trapped packets need to be
27199348004SVladimir Oltean  * replicated over Ethernet as well, otherwise we'd get no notification of
27299348004SVladimir Oltean  * their arrival when using the ocelot-8021q tagging protocol.
273c8c0ba4fSVladimir Oltean  */
27499348004SVladimir Oltean static int felix_update_trapping_destinations(struct dsa_switch *ds,
27599348004SVladimir Oltean 					      bool using_tag_8021q)
276c8c0ba4fSVladimir Oltean {
27799348004SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
27899348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
27999348004SVladimir Oltean 	struct ocelot_vcap_filter *trap;
28099348004SVladimir Oltean 	enum ocelot_mask_mode mask_mode;
28199348004SVladimir Oltean 	unsigned long port_mask;
2822960bb14SVladimir Oltean 	struct dsa_port *dp;
28399348004SVladimir Oltean 	bool cpu_copy_ena;
28499348004SVladimir Oltean 	int cpu = -1, err;
285c8c0ba4fSVladimir Oltean 
28699348004SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
28799348004SVladimir Oltean 		return 0;
288c8c0ba4fSVladimir Oltean 
28999348004SVladimir Oltean 	/* Figure out the current CPU port */
2902960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
2912960bb14SVladimir Oltean 		cpu = dp->index;
2928d5f7954SVladimir Oltean 		break;
293c8c0ba4fSVladimir Oltean 	}
2948d5f7954SVladimir Oltean 
295d78637a8SVladimir Oltean 	/* We are sure that "cpu" was found, otherwise
296d78637a8SVladimir Oltean 	 * dsa_tree_setup_default_cpu() would have failed earlier.
297d78637a8SVladimir Oltean 	 */
298c8c0ba4fSVladimir Oltean 
29999348004SVladimir Oltean 	/* Make sure all traps are set up for that destination */
30099348004SVladimir Oltean 	list_for_each_entry(trap, &ocelot->traps, trap_list) {
30199348004SVladimir Oltean 		/* Figure out the current trapping destination */
30299348004SVladimir Oltean 		if (using_tag_8021q) {
30399348004SVladimir Oltean 			/* Redirect to the tag_8021q CPU port. If timestamps
30499348004SVladimir Oltean 			 * are necessary, also copy trapped packets to the CPU
30599348004SVladimir Oltean 			 * port module.
306c8c0ba4fSVladimir Oltean 			 */
30799348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_REDIRECT;
30899348004SVladimir Oltean 			port_mask = BIT(cpu);
30999348004SVladimir Oltean 			cpu_copy_ena = !!trap->take_ts;
310c8c0ba4fSVladimir Oltean 		} else {
31199348004SVladimir Oltean 			/* Trap packets only to the CPU port module, which is
31299348004SVladimir Oltean 			 * redirected to the NPI port (the DSA CPU port)
313c8c0ba4fSVladimir Oltean 			 */
31499348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
31599348004SVladimir Oltean 			port_mask = 0;
31699348004SVladimir Oltean 			cpu_copy_ena = true;
317c8c0ba4fSVladimir Oltean 		}
318c8c0ba4fSVladimir Oltean 
31999348004SVladimir Oltean 		if (trap->action.mask_mode == mask_mode &&
32099348004SVladimir Oltean 		    trap->action.port_mask == port_mask &&
32199348004SVladimir Oltean 		    trap->action.cpu_copy_ena == cpu_copy_ena)
32299348004SVladimir Oltean 			continue;
323c8c0ba4fSVladimir Oltean 
32499348004SVladimir Oltean 		trap->action.mask_mode = mask_mode;
32599348004SVladimir Oltean 		trap->action.port_mask = port_mask;
32699348004SVladimir Oltean 		trap->action.cpu_copy_ena = cpu_copy_ena;
3270a6f17c6SVladimir Oltean 
32899348004SVladimir Oltean 		err = ocelot_vcap_filter_replace(ocelot, trap);
329c8c0ba4fSVladimir Oltean 		if (err)
330c8c0ba4fSVladimir Oltean 			return err;
33199348004SVladimir Oltean 	}
332c8c0ba4fSVladimir Oltean 
33399348004SVladimir Oltean 	return 0;
334c8c0ba4fSVladimir Oltean }
335c8c0ba4fSVladimir Oltean 
336e21268efSVladimir Oltean static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu)
337e21268efSVladimir Oltean {
338e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
339e21268efSVladimir Oltean 	unsigned long cpu_flood;
3402960bb14SVladimir Oltean 	struct dsa_port *dp;
3412960bb14SVladimir Oltean 	int err;
342e21268efSVladimir Oltean 
343e21268efSVladimir Oltean 	felix_8021q_cpu_port_init(ocelot, cpu);
344e21268efSVladimir Oltean 
3452960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
346e21268efSVladimir Oltean 		/* This overwrites ocelot_init():
347e21268efSVladimir Oltean 		 * Do not forward BPDU frames to the CPU port module,
348e21268efSVladimir Oltean 		 * for 2 reasons:
349e21268efSVladimir Oltean 		 * - When these packets are injected from the tag_8021q
350e21268efSVladimir Oltean 		 *   CPU port, we want them to go out, not loop back
351e21268efSVladimir Oltean 		 *   into the system.
352e21268efSVladimir Oltean 		 * - STP traffic ingressing on a user port should go to
353e21268efSVladimir Oltean 		 *   the tag_8021q CPU port, not to the hardware CPU
354e21268efSVladimir Oltean 		 *   port module.
355e21268efSVladimir Oltean 		 */
356e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
357e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0),
3582960bb14SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG, dp->index);
359e21268efSVladimir Oltean 	}
360e21268efSVladimir Oltean 
361c8c0ba4fSVladimir Oltean 	/* In tag_8021q mode, the CPU port module is unused, except for PTP
362c8c0ba4fSVladimir Oltean 	 * frames. So we want to disable flooding of any kind to the CPU port
363c8c0ba4fSVladimir Oltean 	 * module, since packets going there will end in a black hole.
364e21268efSVladimir Oltean 	 */
365e21268efSVladimir Oltean 	cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
366e21268efSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_UC);
367e21268efSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_MC);
368b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_BC);
369e21268efSVladimir Oltean 
3705da11eb4SVladimir Oltean 	err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD));
371e21268efSVladimir Oltean 	if (err)
372d7b1fd52SVladimir Oltean 		return err;
373d7b1fd52SVladimir Oltean 
37499348004SVladimir Oltean 	err = felix_update_trapping_destinations(ds, true);
375d7b1fd52SVladimir Oltean 	if (err)
376d7b1fd52SVladimir Oltean 		goto out_tag_8021q_unregister;
377e21268efSVladimir Oltean 
37899348004SVladimir Oltean 	/* The ownership of the CPU port module's queues might have just been
37999348004SVladimir Oltean 	 * transferred to the tag_8021q tagger from the NPI-based tagger.
38099348004SVladimir Oltean 	 * So there might still be all sorts of crap in the queues. On the
38199348004SVladimir Oltean 	 * other hand, the MMIO-based matching of PTP frames is very brittle,
38299348004SVladimir Oltean 	 * so we need to be careful that there are no extra frames to be
38399348004SVladimir Oltean 	 * dequeued over MMIO, since we would never know to discard them.
38499348004SVladimir Oltean 	 */
38599348004SVladimir Oltean 	ocelot_drain_cpu_queue(ocelot, 0);
38699348004SVladimir Oltean 
387e21268efSVladimir Oltean 	return 0;
388e21268efSVladimir Oltean 
389d7b1fd52SVladimir Oltean out_tag_8021q_unregister:
390d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
391e21268efSVladimir Oltean 	return err;
392e21268efSVladimir Oltean }
393e21268efSVladimir Oltean 
394e21268efSVladimir Oltean static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu)
395e21268efSVladimir Oltean {
396e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
3972960bb14SVladimir Oltean 	struct dsa_port *dp;
3982960bb14SVladimir Oltean 	int err;
399e21268efSVladimir Oltean 
40099348004SVladimir Oltean 	err = felix_update_trapping_destinations(ds, false);
401c8c0ba4fSVladimir Oltean 	if (err)
402c8c0ba4fSVladimir Oltean 		dev_err(ds->dev, "felix_teardown_mmio_filtering returned %d",
403c8c0ba4fSVladimir Oltean 			err);
404c8c0ba4fSVladimir Oltean 
405d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
406e21268efSVladimir Oltean 
4072960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
408e21268efSVladimir Oltean 		/* Restore the logic from ocelot_init:
409e21268efSVladimir Oltean 		 * do not forward BPDU frames to the front ports.
410e21268efSVladimir Oltean 		 */
411e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
412e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
413e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG,
4142960bb14SVladimir Oltean 				 dp->index);
415e21268efSVladimir Oltean 	}
416e21268efSVladimir Oltean 
417e21268efSVladimir Oltean 	felix_8021q_cpu_port_deinit(ocelot, cpu);
418e21268efSVladimir Oltean }
419e21268efSVladimir Oltean 
420adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This
421adb3dccfSVladimir Oltean  * is the mode through which frames can be injected from and extracted to an
422adb3dccfSVladimir Oltean  * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU
423adb3dccfSVladimir Oltean  * running Linux, and this forms a DSA setup together with the enetc or fman
424adb3dccfSVladimir Oltean  * DSA master.
425adb3dccfSVladimir Oltean  */
426adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port)
427adb3dccfSVladimir Oltean {
428adb3dccfSVladimir Oltean 	ocelot->npi = port;
429adb3dccfSVladimir Oltean 
430adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
431adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
432adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
433adb3dccfSVladimir Oltean 
434adb3dccfSVladimir Oltean 	/* NPI port Injection/Extraction configuration */
435adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
436adb3dccfSVladimir Oltean 			    ocelot->npi_xtr_prefix);
437adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
438adb3dccfSVladimir Oltean 			    ocelot->npi_inj_prefix);
439adb3dccfSVladimir Oltean 
440adb3dccfSVladimir Oltean 	/* Disable transmission of pause frames */
441adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
442adb3dccfSVladimir Oltean }
443adb3dccfSVladimir Oltean 
444adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port)
445adb3dccfSVladimir Oltean {
446adb3dccfSVladimir Oltean 	/* Restore hardware defaults */
447adb3dccfSVladimir Oltean 	int unused_port = ocelot->num_phys_ports + 2;
448adb3dccfSVladimir Oltean 
449adb3dccfSVladimir Oltean 	ocelot->npi = -1;
450adb3dccfSVladimir Oltean 
451adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port),
452adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
453adb3dccfSVladimir Oltean 
454adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
455adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
456adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
457adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
458adb3dccfSVladimir Oltean 
459adb3dccfSVladimir Oltean 	/* Enable transmission of pause frames */
460adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
461adb3dccfSVladimir Oltean }
462adb3dccfSVladimir Oltean 
463adb3dccfSVladimir Oltean static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu)
464adb3dccfSVladimir Oltean {
465adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
466adb3dccfSVladimir Oltean 	unsigned long cpu_flood;
467adb3dccfSVladimir Oltean 
468adb3dccfSVladimir Oltean 	felix_npi_port_init(ocelot, cpu);
469adb3dccfSVladimir Oltean 
470adb3dccfSVladimir Oltean 	/* Include the CPU port module (and indirectly, the NPI port)
471adb3dccfSVladimir Oltean 	 * in the forwarding mask for unknown unicast - the hardware
472adb3dccfSVladimir Oltean 	 * default value for ANA_FLOODING_FLD_UNICAST excludes
473adb3dccfSVladimir Oltean 	 * BIT(ocelot->num_phys_ports), and so does ocelot_init,
474adb3dccfSVladimir Oltean 	 * since Ocelot relies on whitelisting MAC addresses towards
475adb3dccfSVladimir Oltean 	 * PGID_CPU.
476adb3dccfSVladimir Oltean 	 * We do this because DSA does not yet perform RX filtering,
477adb3dccfSVladimir Oltean 	 * and the NPI port does not perform source address learning,
478adb3dccfSVladimir Oltean 	 * so traffic sent to Linux is effectively unknown from the
479adb3dccfSVladimir Oltean 	 * switch's perspective.
480adb3dccfSVladimir Oltean 	 */
481adb3dccfSVladimir Oltean 	cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
482adb3dccfSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_UC);
4836edb9e8dSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_MC);
484b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_BC);
485adb3dccfSVladimir Oltean 
486adb3dccfSVladimir Oltean 	return 0;
487adb3dccfSVladimir Oltean }
488adb3dccfSVladimir Oltean 
489adb3dccfSVladimir Oltean static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu)
490adb3dccfSVladimir Oltean {
491adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
492adb3dccfSVladimir Oltean 
493adb3dccfSVladimir Oltean 	felix_npi_port_deinit(ocelot, cpu);
494adb3dccfSVladimir Oltean }
495adb3dccfSVladimir Oltean 
496adb3dccfSVladimir Oltean static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu,
497adb3dccfSVladimir Oltean 				  enum dsa_tag_protocol proto)
498adb3dccfSVladimir Oltean {
499adb3dccfSVladimir Oltean 	int err;
500adb3dccfSVladimir Oltean 
501adb3dccfSVladimir Oltean 	switch (proto) {
5027c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
503adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
504adb3dccfSVladimir Oltean 		err = felix_setup_tag_npi(ds, cpu);
505adb3dccfSVladimir Oltean 		break;
506e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
507e21268efSVladimir Oltean 		err = felix_setup_tag_8021q(ds, cpu);
508e21268efSVladimir Oltean 		break;
509adb3dccfSVladimir Oltean 	default:
510adb3dccfSVladimir Oltean 		err = -EPROTONOSUPPORT;
511adb3dccfSVladimir Oltean 	}
512adb3dccfSVladimir Oltean 
513adb3dccfSVladimir Oltean 	return err;
514adb3dccfSVladimir Oltean }
515adb3dccfSVladimir Oltean 
516adb3dccfSVladimir Oltean static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu,
517adb3dccfSVladimir Oltean 				   enum dsa_tag_protocol proto)
518adb3dccfSVladimir Oltean {
519adb3dccfSVladimir Oltean 	switch (proto) {
5207c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
521adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
522adb3dccfSVladimir Oltean 		felix_teardown_tag_npi(ds, cpu);
523adb3dccfSVladimir Oltean 		break;
524e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
525e21268efSVladimir Oltean 		felix_teardown_tag_8021q(ds, cpu);
526e21268efSVladimir Oltean 		break;
527adb3dccfSVladimir Oltean 	default:
528adb3dccfSVladimir Oltean 		break;
529adb3dccfSVladimir Oltean 	}
530adb3dccfSVladimir Oltean }
531adb3dccfSVladimir Oltean 
532e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the
533e21268efSVladimir Oltean  * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
534e21268efSVladimir Oltean  * or the restoration is guaranteed to work.
535e21268efSVladimir Oltean  */
536adb3dccfSVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu,
537adb3dccfSVladimir Oltean 				     enum dsa_tag_protocol proto)
538adb3dccfSVladimir Oltean {
539adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
540adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
541adb3dccfSVladimir Oltean 	enum dsa_tag_protocol old_proto = felix->tag_proto;
542adb3dccfSVladimir Oltean 	int err;
543adb3dccfSVladimir Oltean 
5447c4bb540SVladimir Oltean 	if (proto != DSA_TAG_PROTO_SEVILLE &&
5457c4bb540SVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT &&
546e21268efSVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT_8021Q)
547adb3dccfSVladimir Oltean 		return -EPROTONOSUPPORT;
548adb3dccfSVladimir Oltean 
549adb3dccfSVladimir Oltean 	felix_del_tag_protocol(ds, cpu, old_proto);
550adb3dccfSVladimir Oltean 
551adb3dccfSVladimir Oltean 	err = felix_set_tag_protocol(ds, cpu, proto);
552adb3dccfSVladimir Oltean 	if (err) {
553adb3dccfSVladimir Oltean 		felix_set_tag_protocol(ds, cpu, old_proto);
554adb3dccfSVladimir Oltean 		return err;
555adb3dccfSVladimir Oltean 	}
556adb3dccfSVladimir Oltean 
557adb3dccfSVladimir Oltean 	felix->tag_proto = proto;
558adb3dccfSVladimir Oltean 
559adb3dccfSVladimir Oltean 	return 0;
560adb3dccfSVladimir Oltean }
561adb3dccfSVladimir Oltean 
56256051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
5634d776482SFlorian Fainelli 						    int port,
5644d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
56556051948SVladimir Oltean {
566adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
567adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
568adb3dccfSVladimir Oltean 
569adb3dccfSVladimir Oltean 	return felix->tag_proto;
57056051948SVladimir Oltean }
57156051948SVladimir Oltean 
57256051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
57356051948SVladimir Oltean 				 unsigned int ageing_time)
57456051948SVladimir Oltean {
57556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
57656051948SVladimir Oltean 
57756051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
57856051948SVladimir Oltean 
57956051948SVladimir Oltean 	return 0;
58056051948SVladimir Oltean }
58156051948SVladimir Oltean 
5825cad43a5SVladimir Oltean static void felix_port_fast_age(struct dsa_switch *ds, int port)
5835cad43a5SVladimir Oltean {
5845cad43a5SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5855cad43a5SVladimir Oltean 	int err;
5865cad43a5SVladimir Oltean 
5875cad43a5SVladimir Oltean 	err = ocelot_mact_flush(ocelot, port);
5885cad43a5SVladimir Oltean 	if (err)
5895cad43a5SVladimir Oltean 		dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n",
5905cad43a5SVladimir Oltean 			port, ERR_PTR(err));
5915cad43a5SVladimir Oltean }
5925cad43a5SVladimir Oltean 
59356051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
59456051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
59556051948SVladimir Oltean {
59656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
59756051948SVladimir Oltean 
59856051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
59956051948SVladimir Oltean }
60056051948SVladimir Oltean 
60156051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
60256051948SVladimir Oltean 			 const unsigned char *addr, u16 vid)
60356051948SVladimir Oltean {
60456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
60556051948SVladimir Oltean 
60687b0f983SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid);
60756051948SVladimir Oltean }
60856051948SVladimir Oltean 
60956051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
61056051948SVladimir Oltean 			 const unsigned char *addr, u16 vid)
61156051948SVladimir Oltean {
61256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
61356051948SVladimir Oltean 
61456051948SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid);
61556051948SVladimir Oltean }
61656051948SVladimir Oltean 
617a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port,
618209edf95SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb)
619209edf95SVladimir Oltean {
620209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
621209edf95SVladimir Oltean 
622a52b2da7SVladimir Oltean 	return ocelot_port_mdb_add(ocelot, port, mdb);
623209edf95SVladimir Oltean }
624209edf95SVladimir Oltean 
625209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port,
626209edf95SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb)
627209edf95SVladimir Oltean {
628209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
629209edf95SVladimir Oltean 
630209edf95SVladimir Oltean 	return ocelot_port_mdb_del(ocelot, port, mdb);
631209edf95SVladimir Oltean }
632209edf95SVladimir Oltean 
63356051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
63456051948SVladimir Oltean 				       u8 state)
63556051948SVladimir Oltean {
63656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
63756051948SVladimir Oltean 
63856051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
63956051948SVladimir Oltean }
64056051948SVladimir Oltean 
641421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port,
642421741eaSVladimir Oltean 				  struct switchdev_brport_flags val,
643421741eaSVladimir Oltean 				  struct netlink_ext_ack *extack)
644421741eaSVladimir Oltean {
645421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
646421741eaSVladimir Oltean 
647421741eaSVladimir Oltean 	return ocelot_port_pre_bridge_flags(ocelot, port, val);
648421741eaSVladimir Oltean }
649421741eaSVladimir Oltean 
650421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port,
651421741eaSVladimir Oltean 			      struct switchdev_brport_flags val,
652421741eaSVladimir Oltean 			      struct netlink_ext_ack *extack)
653421741eaSVladimir Oltean {
654421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
655421741eaSVladimir Oltean 
656421741eaSVladimir Oltean 	ocelot_port_bridge_flags(ocelot, port, val);
657421741eaSVladimir Oltean 
658421741eaSVladimir Oltean 	return 0;
659421741eaSVladimir Oltean }
660421741eaSVladimir Oltean 
66156051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
662b079922bSVladimir Oltean 			     struct dsa_bridge bridge, bool *tx_fwd_offload)
66356051948SVladimir Oltean {
66456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
66556051948SVladimir Oltean 
666d3eed0e5SVladimir Oltean 	ocelot_port_bridge_join(ocelot, port, bridge.dev);
667e4bd44e8SVladimir Oltean 
668e4bd44e8SVladimir Oltean 	return 0;
66956051948SVladimir Oltean }
67056051948SVladimir Oltean 
67156051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
672d3eed0e5SVladimir Oltean 			       struct dsa_bridge bridge)
67356051948SVladimir Oltean {
67456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
67556051948SVladimir Oltean 
676d3eed0e5SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, bridge.dev);
67756051948SVladimir Oltean }
67856051948SVladimir Oltean 
6798fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port,
680*dedd6a00SVladimir Oltean 			  struct dsa_lag lag,
6818fe6832eSVladimir Oltean 			  struct netdev_lag_upper_info *info)
6828fe6832eSVladimir Oltean {
6838fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
6848fe6832eSVladimir Oltean 
685*dedd6a00SVladimir Oltean 	return ocelot_port_lag_join(ocelot, port, lag.dev, info);
6868fe6832eSVladimir Oltean }
6878fe6832eSVladimir Oltean 
6888fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port,
689*dedd6a00SVladimir Oltean 			   struct dsa_lag lag)
6908fe6832eSVladimir Oltean {
6918fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
6928fe6832eSVladimir Oltean 
693*dedd6a00SVladimir Oltean 	ocelot_port_lag_leave(ocelot, port, lag.dev);
6948fe6832eSVladimir Oltean 
6958fe6832eSVladimir Oltean 	return 0;
6968fe6832eSVladimir Oltean }
6978fe6832eSVladimir Oltean 
6988fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port)
6998fe6832eSVladimir Oltean {
7008fe6832eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
7018fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7028fe6832eSVladimir Oltean 
7038fe6832eSVladimir Oltean 	ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
7048fe6832eSVladimir Oltean 
7058fe6832eSVladimir Oltean 	return 0;
7068fe6832eSVladimir Oltean }
7078fe6832eSVladimir Oltean 
70856051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
70901af940eSVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan,
71001af940eSVladimir Oltean 			      struct netlink_ext_ack *extack)
71156051948SVladimir Oltean {
7122f0402feSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
713b7a9e0daSVladimir Oltean 	u16 flags = vlan->flags;
7142f0402feSVladimir Oltean 
7159a720680SVladimir Oltean 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
7169a720680SVladimir Oltean 	 * egress-untagged or not, pvid or not, make no difference. This
7179a720680SVladimir Oltean 	 * behavior is already better than what DSA just tries to approximate
7189a720680SVladimir Oltean 	 * when it installs the VLAN with the same flags on the CPU port.
7199a720680SVladimir Oltean 	 * Just accept any configuration, and don't let ocelot deny installing
7209a720680SVladimir Oltean 	 * multiple native VLANs on the NPI port, because the switch doesn't
7219a720680SVladimir Oltean 	 * look at the port tag settings towards the NPI interface anyway.
7229a720680SVladimir Oltean 	 */
7239a720680SVladimir Oltean 	if (port == ocelot->npi)
7249a720680SVladimir Oltean 		return 0;
7259a720680SVladimir Oltean 
726b7a9e0daSVladimir Oltean 	return ocelot_vlan_prepare(ocelot, port, vlan->vid,
7272f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_PVID,
72801af940eSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_UNTAGGED,
72901af940eSVladimir Oltean 				   extack);
73056051948SVladimir Oltean }
73156051948SVladimir Oltean 
73289153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
73389153ed6SVladimir Oltean 				struct netlink_ext_ack *extack)
73456051948SVladimir Oltean {
73556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
73656051948SVladimir Oltean 
7373b95d1b2SVladimir Oltean 	return ocelot_port_vlan_filtering(ocelot, port, enabled, extack);
73856051948SVladimir Oltean }
73956051948SVladimir Oltean 
7401958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port,
74131046a5fSVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan,
74231046a5fSVladimir Oltean 			  struct netlink_ext_ack *extack)
74356051948SVladimir Oltean {
74456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
745183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
7461958d581SVladimir Oltean 	int err;
74756051948SVladimir Oltean 
74801af940eSVladimir Oltean 	err = felix_vlan_prepare(ds, port, vlan, extack);
7491958d581SVladimir Oltean 	if (err)
7501958d581SVladimir Oltean 		return err;
7511958d581SVladimir Oltean 
7521958d581SVladimir Oltean 	return ocelot_vlan_add(ocelot, port, vlan->vid,
753183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_PVID,
754183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_UNTAGGED);
75556051948SVladimir Oltean }
75656051948SVladimir Oltean 
75756051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
75856051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
75956051948SVladimir Oltean {
76056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
76156051948SVladimir Oltean 
762b7a9e0daSVladimir Oltean 	return ocelot_vlan_del(ocelot, port, vlan->vid);
76356051948SVladimir Oltean }
76456051948SVladimir Oltean 
765bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
766bdeced75SVladimir Oltean 				   unsigned long *supported,
767bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
768bdeced75SVladimir Oltean {
769bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
770375e1314SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
771bdeced75SVladimir Oltean 
772375e1314SVladimir Oltean 	if (felix->info->phylink_validate)
773375e1314SVladimir Oltean 		felix->info->phylink_validate(ocelot, port, supported, state);
774bdeced75SVladimir Oltean }
775bdeced75SVladimir Oltean 
776bdeced75SVladimir Oltean static void felix_phylink_mac_config(struct dsa_switch *ds, int port,
777bdeced75SVladimir Oltean 				     unsigned int link_an_mode,
778bdeced75SVladimir Oltean 				     const struct phylink_link_state *state)
779bdeced75SVladimir Oltean {
780bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
781bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
782588d0550SIoana Ciornei 	struct dsa_port *dp = dsa_to_port(ds, port);
783bdeced75SVladimir Oltean 
78449af6a76SColin Foster 	if (felix->pcs && felix->pcs[port])
785e7026f15SColin Foster 		phylink_set_pcs(dp->pl, felix->pcs[port]);
786bdeced75SVladimir Oltean }
787bdeced75SVladimir Oltean 
788bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
789bdeced75SVladimir Oltean 					unsigned int link_an_mode,
790bdeced75SVladimir Oltean 					phy_interface_t interface)
791bdeced75SVladimir Oltean {
792bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
793bdeced75SVladimir Oltean 
794e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
795e6e12df6SVladimir Oltean 				     FELIX_MAC_QUIRKS);
796bdeced75SVladimir Oltean }
797bdeced75SVladimir Oltean 
798bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
799bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
800bdeced75SVladimir Oltean 				      phy_interface_t interface,
8015b502a7bSRussell King 				      struct phy_device *phydev,
8025b502a7bSRussell King 				      int speed, int duplex,
8035b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
804bdeced75SVladimir Oltean {
805bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8067e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
807bdeced75SVladimir Oltean 
808e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
809e6e12df6SVladimir Oltean 				   interface, speed, duplex, tx_pause, rx_pause,
810e6e12df6SVladimir Oltean 				   FELIX_MAC_QUIRKS);
8117e14a2dcSVladimir Oltean 
8127e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
8137e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
814bdeced75SVladimir Oltean }
815bdeced75SVladimir Oltean 
816bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
817bd2b3161SXiaoliang Yang {
818bd2b3161SXiaoliang Yang 	int i;
819bd2b3161SXiaoliang Yang 
820bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
821bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
822bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
823bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
824bd2b3161SXiaoliang Yang 		       port);
825bd2b3161SXiaoliang Yang 
82670d39a6eSVladimir Oltean 	for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
827bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
828bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
829bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
830bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
831bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
832bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
833bd2b3161SXiaoliang Yang 			      port, i);
834bd2b3161SXiaoliang Yang 	}
835bd2b3161SXiaoliang Yang }
836bd2b3161SXiaoliang Yang 
83756051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
83856051948SVladimir Oltean 			      u32 stringset, u8 *data)
83956051948SVladimir Oltean {
84056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
84156051948SVladimir Oltean 
84256051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
84356051948SVladimir Oltean }
84456051948SVladimir Oltean 
84556051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
84656051948SVladimir Oltean {
84756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
84856051948SVladimir Oltean 
84956051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
85056051948SVladimir Oltean }
85156051948SVladimir Oltean 
85256051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
85356051948SVladimir Oltean {
85456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
85556051948SVladimir Oltean 
85656051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
85756051948SVladimir Oltean }
85856051948SVladimir Oltean 
85956051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
86056051948SVladimir Oltean 			     struct ethtool_ts_info *info)
86156051948SVladimir Oltean {
86256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
86356051948SVladimir Oltean 
86456051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
86556051948SVladimir Oltean }
86656051948SVladimir Oltean 
867bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
868bdeced75SVladimir Oltean 				  struct device_node *ports_node,
869bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
870bdeced75SVladimir Oltean {
871bdeced75SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
872bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
873bdeced75SVladimir Oltean 	struct device_node *child;
874bdeced75SVladimir Oltean 
87537fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
876bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
877bdeced75SVladimir Oltean 		u32 port;
878bdeced75SVladimir Oltean 		int err;
879bdeced75SVladimir Oltean 
880bdeced75SVladimir Oltean 		/* Get switch port number from DT */
881bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
882bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
883bdeced75SVladimir Oltean 				"(property \"reg\")\n");
884bdeced75SVladimir Oltean 			of_node_put(child);
885bdeced75SVladimir Oltean 			return -ENODEV;
886bdeced75SVladimir Oltean 		}
887bdeced75SVladimir Oltean 
888bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
889bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
890bdeced75SVladimir Oltean 		if (err) {
891bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
892bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
893bdeced75SVladimir Oltean 				port);
894bdeced75SVladimir Oltean 			of_node_put(child);
895bdeced75SVladimir Oltean 			return -ENODEV;
896bdeced75SVladimir Oltean 		}
897bdeced75SVladimir Oltean 
898bdeced75SVladimir Oltean 		err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode);
899bdeced75SVladimir Oltean 		if (err < 0) {
900bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
901bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
90259ebb430SSumera Priyadarsini 			of_node_put(child);
903bdeced75SVladimir Oltean 			return err;
904bdeced75SVladimir Oltean 		}
905bdeced75SVladimir Oltean 
906bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
907bdeced75SVladimir Oltean 	}
908bdeced75SVladimir Oltean 
909bdeced75SVladimir Oltean 	return 0;
910bdeced75SVladimir Oltean }
911bdeced75SVladimir Oltean 
912bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
913bdeced75SVladimir Oltean {
914bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
915bdeced75SVladimir Oltean 	struct device_node *switch_node;
916bdeced75SVladimir Oltean 	struct device_node *ports_node;
917bdeced75SVladimir Oltean 	int err;
918bdeced75SVladimir Oltean 
919bdeced75SVladimir Oltean 	switch_node = dev->of_node;
920bdeced75SVladimir Oltean 
921bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
922abecbfcdSVladimir Oltean 	if (!ports_node)
923abecbfcdSVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
924bdeced75SVladimir Oltean 	if (!ports_node) {
925abecbfcdSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n");
926bdeced75SVladimir Oltean 		return -ENODEV;
927bdeced75SVladimir Oltean 	}
928bdeced75SVladimir Oltean 
929bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
930bdeced75SVladimir Oltean 	of_node_put(ports_node);
931bdeced75SVladimir Oltean 
932bdeced75SVladimir Oltean 	return err;
933bdeced75SVladimir Oltean }
934bdeced75SVladimir Oltean 
93556051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
93656051948SVladimir Oltean {
93756051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
938bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
939b4024c9eSClaudiu Manoil 	struct resource res;
94056051948SVladimir Oltean 	int port, i, err;
94156051948SVladimir Oltean 
94256051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
94356051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
94456051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
94556051948SVladimir Oltean 	if (!ocelot->ports)
94656051948SVladimir Oltean 		return -ENOMEM;
94756051948SVladimir Oltean 
94856051948SVladimir Oltean 	ocelot->map		= felix->info->map;
94956051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
95056051948SVladimir Oltean 	ocelot->num_stats	= felix->info->num_stats;
95121ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
95207d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
95377043c37SXiaoliang Yang 	ocelot->vcap_pol.base	= felix->info->vcap_pol_base;
95477043c37SXiaoliang Yang 	ocelot->vcap_pol.max	= felix->info->vcap_pol_max;
95577043c37SXiaoliang Yang 	ocelot->vcap_pol.base2	= felix->info->vcap_pol_base2;
95677043c37SXiaoliang Yang 	ocelot->vcap_pol.max2	= felix->info->vcap_pol_max2;
95756051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
958cacea62fSVladimir Oltean 	ocelot->npi_inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
959cacea62fSVladimir Oltean 	ocelot->npi_xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
960f59fd9caSVladimir Oltean 	ocelot->devlink		= felix->ds->devlink;
96156051948SVladimir Oltean 
962bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
963bdeced75SVladimir Oltean 				 GFP_KERNEL);
964bdeced75SVladimir Oltean 	if (!port_phy_modes)
965bdeced75SVladimir Oltean 		return -ENOMEM;
966bdeced75SVladimir Oltean 
967bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
968bdeced75SVladimir Oltean 	if (err) {
969bdeced75SVladimir Oltean 		kfree(port_phy_modes);
970bdeced75SVladimir Oltean 		return err;
971bdeced75SVladimir Oltean 	}
972bdeced75SVladimir Oltean 
97356051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
97456051948SVladimir Oltean 		struct regmap *target;
97556051948SVladimir Oltean 
97656051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
97756051948SVladimir Oltean 			continue;
97856051948SVladimir Oltean 
979b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
980b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
981375e1314SVladimir Oltean 		res.start += felix->switch_base;
982375e1314SVladimir Oltean 		res.end += felix->switch_base;
98356051948SVladimir Oltean 
984242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
98556051948SVladimir Oltean 		if (IS_ERR(target)) {
98656051948SVladimir Oltean 			dev_err(ocelot->dev,
98756051948SVladimir Oltean 				"Failed to map device memory space\n");
988bdeced75SVladimir Oltean 			kfree(port_phy_modes);
98956051948SVladimir Oltean 			return PTR_ERR(target);
99056051948SVladimir Oltean 		}
99156051948SVladimir Oltean 
99256051948SVladimir Oltean 		ocelot->targets[i] = target;
99356051948SVladimir Oltean 	}
99456051948SVladimir Oltean 
99556051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
99656051948SVladimir Oltean 	if (err) {
99756051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
998bdeced75SVladimir Oltean 		kfree(port_phy_modes);
99956051948SVladimir Oltean 		return err;
100056051948SVladimir Oltean 	}
100156051948SVladimir Oltean 
100256051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
100356051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
100491c724cfSVladimir Oltean 		struct regmap *target;
100556051948SVladimir Oltean 
100656051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
100756051948SVladimir Oltean 					   sizeof(struct ocelot_port),
100856051948SVladimir Oltean 					   GFP_KERNEL);
100956051948SVladimir Oltean 		if (!ocelot_port) {
101056051948SVladimir Oltean 			dev_err(ocelot->dev,
101156051948SVladimir Oltean 				"failed to allocate port memory\n");
1012bdeced75SVladimir Oltean 			kfree(port_phy_modes);
101356051948SVladimir Oltean 			return -ENOMEM;
101456051948SVladimir Oltean 		}
101556051948SVladimir Oltean 
1016b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
1017b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1018375e1314SVladimir Oltean 		res.start += felix->switch_base;
1019375e1314SVladimir Oltean 		res.end += felix->switch_base;
102056051948SVladimir Oltean 
1021242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
102291c724cfSVladimir Oltean 		if (IS_ERR(target)) {
102356051948SVladimir Oltean 			dev_err(ocelot->dev,
102491c724cfSVladimir Oltean 				"Failed to map memory space for port %d\n",
102591c724cfSVladimir Oltean 				port);
1026bdeced75SVladimir Oltean 			kfree(port_phy_modes);
102791c724cfSVladimir Oltean 			return PTR_ERR(target);
102856051948SVladimir Oltean 		}
102956051948SVladimir Oltean 
1030bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
103156051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
103291c724cfSVladimir Oltean 		ocelot_port->target = target;
103356051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
103456051948SVladimir Oltean 	}
103556051948SVladimir Oltean 
1036bdeced75SVladimir Oltean 	kfree(port_phy_modes);
1037bdeced75SVladimir Oltean 
1038bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
1039bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
1040bdeced75SVladimir Oltean 		if (err < 0)
1041bdeced75SVladimir Oltean 			return err;
1042bdeced75SVladimir Oltean 	}
1043bdeced75SVladimir Oltean 
104456051948SVladimir Oltean 	return 0;
104556051948SVladimir Oltean }
104656051948SVladimir Oltean 
10471328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port,
10481328a883SVladimir Oltean 					   struct sk_buff *skb)
10491328a883SVladimir Oltean {
10501328a883SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
10511328a883SVladimir Oltean 	struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
10521328a883SVladimir Oltean 	struct sk_buff *skb_match = NULL, *skb_tmp;
10531328a883SVladimir Oltean 	unsigned long flags;
10541328a883SVladimir Oltean 
10551328a883SVladimir Oltean 	if (!clone)
10561328a883SVladimir Oltean 		return;
10571328a883SVladimir Oltean 
10581328a883SVladimir Oltean 	spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags);
10591328a883SVladimir Oltean 
10601328a883SVladimir Oltean 	skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) {
10611328a883SVladimir Oltean 		if (skb != clone)
10621328a883SVladimir Oltean 			continue;
10631328a883SVladimir Oltean 		__skb_unlink(skb, &ocelot_port->tx_skbs);
10641328a883SVladimir Oltean 		skb_match = skb;
10651328a883SVladimir Oltean 		break;
10661328a883SVladimir Oltean 	}
10671328a883SVladimir Oltean 
10681328a883SVladimir Oltean 	spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags);
10691328a883SVladimir Oltean 
10701328a883SVladimir Oltean 	WARN_ONCE(!skb_match,
10711328a883SVladimir Oltean 		  "Could not find skb clone in TX timestamping list\n");
10721328a883SVladimir Oltean }
10731328a883SVladimir Oltean 
107449f885b2SVladimir Oltean #define work_to_xmit_work(w) \
107549f885b2SVladimir Oltean 		container_of((w), struct felix_deferred_xmit_work, work)
107649f885b2SVladimir Oltean 
107749f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work)
107849f885b2SVladimir Oltean {
107949f885b2SVladimir Oltean 	struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
108049f885b2SVladimir Oltean 	struct dsa_switch *ds = xmit_work->dp->ds;
108149f885b2SVladimir Oltean 	struct sk_buff *skb = xmit_work->skb;
108249f885b2SVladimir Oltean 	u32 rew_op = ocelot_ptp_rew_op(skb);
108349f885b2SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
108449f885b2SVladimir Oltean 	int port = xmit_work->dp->index;
108549f885b2SVladimir Oltean 	int retries = 10;
108649f885b2SVladimir Oltean 
108749f885b2SVladimir Oltean 	do {
108849f885b2SVladimir Oltean 		if (ocelot_can_inject(ocelot, 0))
108949f885b2SVladimir Oltean 			break;
109049f885b2SVladimir Oltean 
109149f885b2SVladimir Oltean 		cpu_relax();
109249f885b2SVladimir Oltean 	} while (--retries);
109349f885b2SVladimir Oltean 
109449f885b2SVladimir Oltean 	if (!retries) {
109549f885b2SVladimir Oltean 		dev_err(ocelot->dev, "port %d failed to inject skb\n",
109649f885b2SVladimir Oltean 			port);
10971328a883SVladimir Oltean 		ocelot_port_purge_txtstamp_skb(ocelot, port, skb);
109849f885b2SVladimir Oltean 		kfree_skb(skb);
109949f885b2SVladimir Oltean 		return;
110049f885b2SVladimir Oltean 	}
110149f885b2SVladimir Oltean 
110249f885b2SVladimir Oltean 	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
110349f885b2SVladimir Oltean 
110449f885b2SVladimir Oltean 	consume_skb(skb);
110549f885b2SVladimir Oltean 	kfree(xmit_work);
110649f885b2SVladimir Oltean }
110749f885b2SVladimir Oltean 
110835d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds,
110935d97680SVladimir Oltean 				      enum dsa_tag_protocol proto)
111049f885b2SVladimir Oltean {
111135d97680SVladimir Oltean 	struct ocelot_8021q_tagger_data *tagger_data;
111249f885b2SVladimir Oltean 
111335d97680SVladimir Oltean 	switch (proto) {
111435d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
111535d97680SVladimir Oltean 		tagger_data = ocelot_8021q_tagger_data(ds);
111635d97680SVladimir Oltean 		tagger_data->xmit_work_fn = felix_port_deferred_xmit;
111749f885b2SVladimir Oltean 		return 0;
111835d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
111935d97680SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
112049f885b2SVladimir Oltean 		return 0;
112135d97680SVladimir Oltean 	default:
112235d97680SVladimir Oltean 		return -EPROTONOSUPPORT;
112349f885b2SVladimir Oltean 	}
112449f885b2SVladimir Oltean }
112549f885b2SVladimir Oltean 
112656051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
112756051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
112856051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
112956051948SVladimir Oltean  * (which will not work).
113056051948SVladimir Oltean  */
113156051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
113256051948SVladimir Oltean {
113356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
113456051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
11352960bb14SVladimir Oltean 	struct dsa_port *dp;
11362960bb14SVladimir Oltean 	int err;
113756051948SVladimir Oltean 
113856051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
113956051948SVladimir Oltean 	if (err)
114056051948SVladimir Oltean 		return err;
114156051948SVladimir Oltean 
1142d1cc0e93SVladimir Oltean 	err = ocelot_init(ocelot);
1143d1cc0e93SVladimir Oltean 	if (err)
11446b73b7c9SVladimir Oltean 		goto out_mdiobus_free;
1145d1cc0e93SVladimir Oltean 
11462b49d128SYangbo Lu 	if (ocelot->ptp) {
11472ac7c6c5SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
11482b49d128SYangbo Lu 		if (err) {
11492b49d128SYangbo Lu 			dev_err(ocelot->dev,
11502b49d128SYangbo Lu 				"Timestamp initialization failed\n");
11512b49d128SYangbo Lu 			ocelot->ptp = 0;
11522b49d128SYangbo Lu 		}
11532b49d128SYangbo Lu 	}
115456051948SVladimir Oltean 
11552960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
11562960bb14SVladimir Oltean 		ocelot_init_port(ocelot, dp->index);
1157bd2b3161SXiaoliang Yang 
1158bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
1159bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
1160bd2b3161SXiaoliang Yang 		 */
11612960bb14SVladimir Oltean 		felix_port_qos_map_init(ocelot, dp->index);
116256051948SVladimir Oltean 	}
116356051948SVladimir Oltean 
1164f59fd9caSVladimir Oltean 	err = ocelot_devlink_sb_register(ocelot);
1165f59fd9caSVladimir Oltean 	if (err)
11666b73b7c9SVladimir Oltean 		goto out_deinit_ports;
1167f59fd9caSVladimir Oltean 
11682960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
1169adb3dccfSVladimir Oltean 		/* The initial tag protocol is NPI which always returns 0, so
1170adb3dccfSVladimir Oltean 		 * there's no real point in checking for errors.
11711cf3299bSVladimir Oltean 		 */
11722960bb14SVladimir Oltean 		felix_set_tag_protocol(ds, dp->index, felix->tag_proto);
11738d5f7954SVladimir Oltean 		break;
1174adb3dccfSVladimir Oltean 	}
11751cf3299bSVladimir Oltean 
11760b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
1177c54913c1SVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
1178bdeced75SVladimir Oltean 
117956051948SVladimir Oltean 	return 0;
11806b73b7c9SVladimir Oltean 
11816b73b7c9SVladimir Oltean out_deinit_ports:
11822960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
11832960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
11846b73b7c9SVladimir Oltean 
11856b73b7c9SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
11866b73b7c9SVladimir Oltean 	ocelot_deinit(ocelot);
11876b73b7c9SVladimir Oltean 
11886b73b7c9SVladimir Oltean out_mdiobus_free:
11896b73b7c9SVladimir Oltean 	if (felix->info->mdio_bus_free)
11906b73b7c9SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
11916b73b7c9SVladimir Oltean 
11926b73b7c9SVladimir Oltean 	return err;
119356051948SVladimir Oltean }
119456051948SVladimir Oltean 
119556051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
119656051948SVladimir Oltean {
119756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1198bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
11992960bb14SVladimir Oltean 	struct dsa_port *dp;
1200bdeced75SVladimir Oltean 
12012960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
12022960bb14SVladimir Oltean 		felix_del_tag_protocol(ds, dp->index, felix->tag_proto);
12038d5f7954SVladimir Oltean 		break;
1204adb3dccfSVladimir Oltean 	}
1205adb3dccfSVladimir Oltean 
12062960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
12072960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
1208d19741b0SVladimir Oltean 
120949f885b2SVladimir Oltean 	ocelot_devlink_sb_unregister(ocelot);
121049f885b2SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
121149f885b2SVladimir Oltean 	ocelot_deinit(ocelot);
121249f885b2SVladimir Oltean 
1213d19741b0SVladimir Oltean 	if (felix->info->mdio_bus_free)
1214d19741b0SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
121556051948SVladimir Oltean }
121656051948SVladimir Oltean 
1217c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
1218c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1219c0bcf537SYangbo Lu {
1220c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1221c0bcf537SYangbo Lu 
1222c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
1223c0bcf537SYangbo Lu }
1224c0bcf537SYangbo Lu 
1225c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
1226c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1227c0bcf537SYangbo Lu {
1228c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
122999348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
123099348004SVladimir Oltean 	bool using_tag_8021q;
123199348004SVladimir Oltean 	int err;
1232c0bcf537SYangbo Lu 
123399348004SVladimir Oltean 	err = ocelot_hwstamp_set(ocelot, port, ifr);
123499348004SVladimir Oltean 	if (err)
123599348004SVladimir Oltean 		return err;
123699348004SVladimir Oltean 
123799348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
123899348004SVladimir Oltean 
123999348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
1240c0bcf537SYangbo Lu }
1241c0bcf537SYangbo Lu 
12420a6f17c6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot, unsigned int ptp_type)
12430a6f17c6SVladimir Oltean {
12440a6f17c6SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
12450a6f17c6SVladimir Oltean 	int err, grp = 0;
12460a6f17c6SVladimir Oltean 
12470a6f17c6SVladimir Oltean 	if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q)
12480a6f17c6SVladimir Oltean 		return false;
12490a6f17c6SVladimir Oltean 
12500a6f17c6SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
12510a6f17c6SVladimir Oltean 		return false;
12520a6f17c6SVladimir Oltean 
12530a6f17c6SVladimir Oltean 	if (ptp_type == PTP_CLASS_NONE)
12540a6f17c6SVladimir Oltean 		return false;
12550a6f17c6SVladimir Oltean 
12560a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
12570a6f17c6SVladimir Oltean 		struct sk_buff *skb;
12580a6f17c6SVladimir Oltean 		unsigned int type;
12590a6f17c6SVladimir Oltean 
12600a6f17c6SVladimir Oltean 		err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
12610a6f17c6SVladimir Oltean 		if (err)
12620a6f17c6SVladimir Oltean 			goto out;
12630a6f17c6SVladimir Oltean 
12640a6f17c6SVladimir Oltean 		/* We trap to the CPU port module all PTP frames, but
12650a6f17c6SVladimir Oltean 		 * felix_rxtstamp() only gets called for event frames.
12660a6f17c6SVladimir Oltean 		 * So we need to avoid sending duplicate general
12670a6f17c6SVladimir Oltean 		 * message frames by running a second BPF classifier
12680a6f17c6SVladimir Oltean 		 * here and dropping those.
12690a6f17c6SVladimir Oltean 		 */
12700a6f17c6SVladimir Oltean 		__skb_push(skb, ETH_HLEN);
12710a6f17c6SVladimir Oltean 
12720a6f17c6SVladimir Oltean 		type = ptp_classify_raw(skb);
12730a6f17c6SVladimir Oltean 
12740a6f17c6SVladimir Oltean 		__skb_pull(skb, ETH_HLEN);
12750a6f17c6SVladimir Oltean 
12760a6f17c6SVladimir Oltean 		if (type == PTP_CLASS_NONE) {
12770a6f17c6SVladimir Oltean 			kfree_skb(skb);
12780a6f17c6SVladimir Oltean 			continue;
12790a6f17c6SVladimir Oltean 		}
12800a6f17c6SVladimir Oltean 
12810a6f17c6SVladimir Oltean 		netif_rx(skb);
12820a6f17c6SVladimir Oltean 	}
12830a6f17c6SVladimir Oltean 
12840a6f17c6SVladimir Oltean out:
12850a6f17c6SVladimir Oltean 	if (err < 0)
12860a6f17c6SVladimir Oltean 		ocelot_drain_cpu_queue(ocelot, 0);
12870a6f17c6SVladimir Oltean 
12880a6f17c6SVladimir Oltean 	return true;
12890a6f17c6SVladimir Oltean }
12900a6f17c6SVladimir Oltean 
1291c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
1292c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
1293c0bcf537SYangbo Lu {
129492f62485SVladimir Oltean 	u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo;
1295c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
1296c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1297c0bcf537SYangbo Lu 	struct timespec64 ts;
129892f62485SVladimir Oltean 	u32 tstamp_hi;
129992f62485SVladimir Oltean 	u64 tstamp;
1300c0bcf537SYangbo Lu 
13010a6f17c6SVladimir Oltean 	/* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb
13020a6f17c6SVladimir Oltean 	 * for RX timestamping. Then free it, and poll for its copy through
13030a6f17c6SVladimir Oltean 	 * MMIO in the CPU port module, and inject that into the stack from
13040a6f17c6SVladimir Oltean 	 * ocelot_xtr_poll().
13050a6f17c6SVladimir Oltean 	 */
13060a6f17c6SVladimir Oltean 	if (felix_check_xtr_pkt(ocelot, type)) {
13070a6f17c6SVladimir Oltean 		kfree_skb(skb);
13080a6f17c6SVladimir Oltean 		return true;
13090a6f17c6SVladimir Oltean 	}
13100a6f17c6SVladimir Oltean 
1311c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1312c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1313c0bcf537SYangbo Lu 
1314c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
1315c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
1316c0bcf537SYangbo Lu 		tstamp_hi--;
1317c0bcf537SYangbo Lu 
1318c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
1319c0bcf537SYangbo Lu 
1320c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
1321c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1322c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
1323c0bcf537SYangbo Lu 	return false;
1324c0bcf537SYangbo Lu }
1325c0bcf537SYangbo Lu 
13265c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port,
13275c5416f5SYangbo Lu 			   struct sk_buff *skb)
1328c0bcf537SYangbo Lu {
1329c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1330682eaad9SYangbo Lu 	struct sk_buff *clone = NULL;
1331c0bcf537SYangbo Lu 
1332682eaad9SYangbo Lu 	if (!ocelot->ptp)
13335c5416f5SYangbo Lu 		return;
1334c0bcf537SYangbo Lu 
133552849bcfSVladimir Oltean 	if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
133652849bcfSVladimir Oltean 		dev_err_ratelimited(ds->dev,
133752849bcfSVladimir Oltean 				    "port %d delivering skb without TX timestamp\n",
133852849bcfSVladimir Oltean 				    port);
1339682eaad9SYangbo Lu 		return;
134052849bcfSVladimir Oltean 	}
1341682eaad9SYangbo Lu 
1342682eaad9SYangbo Lu 	if (clone)
1343c4b364ceSYangbo Lu 		OCELOT_SKB_CB(skb)->clone = clone;
13445c5416f5SYangbo Lu }
1345c0bcf537SYangbo Lu 
13460b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
13470b912fc9SVladimir Oltean {
13480b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
13490b912fc9SVladimir Oltean 
13500b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
13510b912fc9SVladimir Oltean 
13520b912fc9SVladimir Oltean 	return 0;
13530b912fc9SVladimir Oltean }
13540b912fc9SVladimir Oltean 
13550b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
13560b912fc9SVladimir Oltean {
13570b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
13580b912fc9SVladimir Oltean 
13590b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
13600b912fc9SVladimir Oltean }
13610b912fc9SVladimir Oltean 
136207d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
136307d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
136407d985eeSVladimir Oltean {
136507d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
136699348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
136799348004SVladimir Oltean 	bool using_tag_8021q;
136899348004SVladimir Oltean 	int err;
136907d985eeSVladimir Oltean 
137099348004SVladimir Oltean 	err = ocelot_cls_flower_replace(ocelot, port, cls, ingress);
137199348004SVladimir Oltean 	if (err)
137299348004SVladimir Oltean 		return err;
137399348004SVladimir Oltean 
137499348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
137599348004SVladimir Oltean 
137699348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
137707d985eeSVladimir Oltean }
137807d985eeSVladimir Oltean 
137907d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
138007d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
138107d985eeSVladimir Oltean {
138207d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
138307d985eeSVladimir Oltean 
138407d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
138507d985eeSVladimir Oltean }
138607d985eeSVladimir Oltean 
138707d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
138807d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
138907d985eeSVladimir Oltean {
139007d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
139107d985eeSVladimir Oltean 
139207d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
139307d985eeSVladimir Oltean }
139407d985eeSVladimir Oltean 
1395fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
1396fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
1397fc411eaaSVladimir Oltean {
1398fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1399fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
1400fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
14015f035af7SPo Liu 		.burst = policer->burst,
1402fc411eaaSVladimir Oltean 	};
1403fc411eaaSVladimir Oltean 
1404fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
1405fc411eaaSVladimir Oltean }
1406fc411eaaSVladimir Oltean 
1407fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
1408fc411eaaSVladimir Oltean {
1409fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1410fc411eaaSVladimir Oltean 
1411fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
1412fc411eaaSVladimir Oltean }
1413fc411eaaSVladimir Oltean 
1414de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
1415de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
1416de143c0eSXiaoliang Yang 			       void *type_data)
1417de143c0eSXiaoliang Yang {
1418de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
1419de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
1420de143c0eSXiaoliang Yang 
1421de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
1422de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
1423de143c0eSXiaoliang Yang 	else
1424de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
1425de143c0eSXiaoliang Yang }
1426de143c0eSXiaoliang Yang 
1427f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
1428f59fd9caSVladimir Oltean 			     u16 pool_index,
1429f59fd9caSVladimir Oltean 			     struct devlink_sb_pool_info *pool_info)
1430f59fd9caSVladimir Oltean {
1431f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1432f59fd9caSVladimir Oltean 
1433f59fd9caSVladimir Oltean 	return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
1434f59fd9caSVladimir Oltean }
1435f59fd9caSVladimir Oltean 
1436f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
1437f59fd9caSVladimir Oltean 			     u16 pool_index, u32 size,
1438f59fd9caSVladimir Oltean 			     enum devlink_sb_threshold_type threshold_type,
1439f59fd9caSVladimir Oltean 			     struct netlink_ext_ack *extack)
1440f59fd9caSVladimir Oltean {
1441f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1442f59fd9caSVladimir Oltean 
1443f59fd9caSVladimir Oltean 	return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
1444f59fd9caSVladimir Oltean 				  threshold_type, extack);
1445f59fd9caSVladimir Oltean }
1446f59fd9caSVladimir Oltean 
1447f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
1448f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1449f59fd9caSVladimir Oltean 				  u32 *p_threshold)
1450f59fd9caSVladimir Oltean {
1451f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1452f59fd9caSVladimir Oltean 
1453f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
1454f59fd9caSVladimir Oltean 				       p_threshold);
1455f59fd9caSVladimir Oltean }
1456f59fd9caSVladimir Oltean 
1457f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
1458f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1459f59fd9caSVladimir Oltean 				  u32 threshold, struct netlink_ext_ack *extack)
1460f59fd9caSVladimir Oltean {
1461f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1462f59fd9caSVladimir Oltean 
1463f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
1464f59fd9caSVladimir Oltean 				       threshold, extack);
1465f59fd9caSVladimir Oltean }
1466f59fd9caSVladimir Oltean 
1467f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
1468f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1469f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1470f59fd9caSVladimir Oltean 				     u16 *p_pool_index, u32 *p_threshold)
1471f59fd9caSVladimir Oltean {
1472f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1473f59fd9caSVladimir Oltean 
1474f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
1475f59fd9caSVladimir Oltean 					  pool_type, p_pool_index,
1476f59fd9caSVladimir Oltean 					  p_threshold);
1477f59fd9caSVladimir Oltean }
1478f59fd9caSVladimir Oltean 
1479f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
1480f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1481f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1482f59fd9caSVladimir Oltean 				     u16 pool_index, u32 threshold,
1483f59fd9caSVladimir Oltean 				     struct netlink_ext_ack *extack)
1484f59fd9caSVladimir Oltean {
1485f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1486f59fd9caSVladimir Oltean 
1487f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
1488f59fd9caSVladimir Oltean 					  pool_type, pool_index, threshold,
1489f59fd9caSVladimir Oltean 					  extack);
1490f59fd9caSVladimir Oltean }
1491f59fd9caSVladimir Oltean 
1492f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds,
1493f59fd9caSVladimir Oltean 				 unsigned int sb_index)
1494f59fd9caSVladimir Oltean {
1495f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1496f59fd9caSVladimir Oltean 
1497f59fd9caSVladimir Oltean 	return ocelot_sb_occ_snapshot(ocelot, sb_index);
1498f59fd9caSVladimir Oltean }
1499f59fd9caSVladimir Oltean 
1500f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds,
1501f59fd9caSVladimir Oltean 				  unsigned int sb_index)
1502f59fd9caSVladimir Oltean {
1503f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1504f59fd9caSVladimir Oltean 
1505f59fd9caSVladimir Oltean 	return ocelot_sb_occ_max_clear(ocelot, sb_index);
1506f59fd9caSVladimir Oltean }
1507f59fd9caSVladimir Oltean 
1508f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
1509f59fd9caSVladimir Oltean 				      unsigned int sb_index, u16 pool_index,
1510f59fd9caSVladimir Oltean 				      u32 *p_cur, u32 *p_max)
1511f59fd9caSVladimir Oltean {
1512f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1513f59fd9caSVladimir Oltean 
1514f59fd9caSVladimir Oltean 	return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
1515f59fd9caSVladimir Oltean 					   p_cur, p_max);
1516f59fd9caSVladimir Oltean }
1517f59fd9caSVladimir Oltean 
1518f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
1519f59fd9caSVladimir Oltean 					 unsigned int sb_index, u16 tc_index,
1520f59fd9caSVladimir Oltean 					 enum devlink_sb_pool_type pool_type,
1521f59fd9caSVladimir Oltean 					 u32 *p_cur, u32 *p_max)
1522f59fd9caSVladimir Oltean {
1523f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1524f59fd9caSVladimir Oltean 
1525f59fd9caSVladimir Oltean 	return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
1526f59fd9caSVladimir Oltean 					      pool_type, p_cur, p_max);
1527f59fd9caSVladimir Oltean }
1528f59fd9caSVladimir Oltean 
1529a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port,
1530a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1531a026c50bSHoratiu Vultur {
1532a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1533a026c50bSHoratiu Vultur 
1534a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1535a026c50bSHoratiu Vultur }
1536a026c50bSHoratiu Vultur 
1537a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port,
1538a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1539a026c50bSHoratiu Vultur {
1540a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1541a026c50bSHoratiu Vultur 
1542a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1543a026c50bSHoratiu Vultur }
1544a026c50bSHoratiu Vultur 
1545a026c50bSHoratiu Vultur static int
1546a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port,
1547a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1548a026c50bSHoratiu Vultur {
1549a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1550a026c50bSHoratiu Vultur 
1551a026c50bSHoratiu Vultur 	return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1552a026c50bSHoratiu Vultur }
1553a026c50bSHoratiu Vultur 
1554a026c50bSHoratiu Vultur static int
1555a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port,
1556a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1557a026c50bSHoratiu Vultur {
1558a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1559a026c50bSHoratiu Vultur 
1560a026c50bSHoratiu Vultur 	return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1561a026c50bSHoratiu Vultur }
1562a026c50bSHoratiu Vultur 
1563375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = {
156456051948SVladimir Oltean 	.get_tag_protocol		= felix_get_tag_protocol,
1565adb3dccfSVladimir Oltean 	.change_tag_protocol		= felix_change_tag_protocol,
156635d97680SVladimir Oltean 	.connect_tag_protocol		= felix_connect_tag_protocol,
156756051948SVladimir Oltean 	.setup				= felix_setup,
156856051948SVladimir Oltean 	.teardown			= felix_teardown,
156956051948SVladimir Oltean 	.set_ageing_time		= felix_set_ageing_time,
157056051948SVladimir Oltean 	.get_strings			= felix_get_strings,
157156051948SVladimir Oltean 	.get_ethtool_stats		= felix_get_ethtool_stats,
157256051948SVladimir Oltean 	.get_sset_count			= felix_get_sset_count,
157356051948SVladimir Oltean 	.get_ts_info			= felix_get_ts_info,
1574bdeced75SVladimir Oltean 	.phylink_validate		= felix_phylink_validate,
1575bdeced75SVladimir Oltean 	.phylink_mac_config		= felix_phylink_mac_config,
1576bdeced75SVladimir Oltean 	.phylink_mac_link_down		= felix_phylink_mac_link_down,
1577bdeced75SVladimir Oltean 	.phylink_mac_link_up		= felix_phylink_mac_link_up,
15785cad43a5SVladimir Oltean 	.port_fast_age			= felix_port_fast_age,
157956051948SVladimir Oltean 	.port_fdb_dump			= felix_fdb_dump,
158056051948SVladimir Oltean 	.port_fdb_add			= felix_fdb_add,
158156051948SVladimir Oltean 	.port_fdb_del			= felix_fdb_del,
1582209edf95SVladimir Oltean 	.port_mdb_add			= felix_mdb_add,
1583209edf95SVladimir Oltean 	.port_mdb_del			= felix_mdb_del,
1584421741eaSVladimir Oltean 	.port_pre_bridge_flags		= felix_pre_bridge_flags,
1585421741eaSVladimir Oltean 	.port_bridge_flags		= felix_bridge_flags,
158656051948SVladimir Oltean 	.port_bridge_join		= felix_bridge_join,
158756051948SVladimir Oltean 	.port_bridge_leave		= felix_bridge_leave,
15888fe6832eSVladimir Oltean 	.port_lag_join			= felix_lag_join,
15898fe6832eSVladimir Oltean 	.port_lag_leave			= felix_lag_leave,
15908fe6832eSVladimir Oltean 	.port_lag_change		= felix_lag_change,
159156051948SVladimir Oltean 	.port_stp_state_set		= felix_bridge_stp_state_set,
159256051948SVladimir Oltean 	.port_vlan_filtering		= felix_vlan_filtering,
159356051948SVladimir Oltean 	.port_vlan_add			= felix_vlan_add,
159456051948SVladimir Oltean 	.port_vlan_del			= felix_vlan_del,
1595c0bcf537SYangbo Lu 	.port_hwtstamp_get		= felix_hwtstamp_get,
1596c0bcf537SYangbo Lu 	.port_hwtstamp_set		= felix_hwtstamp_set,
1597c0bcf537SYangbo Lu 	.port_rxtstamp			= felix_rxtstamp,
1598c0bcf537SYangbo Lu 	.port_txtstamp			= felix_txtstamp,
15990b912fc9SVladimir Oltean 	.port_change_mtu		= felix_change_mtu,
16000b912fc9SVladimir Oltean 	.port_max_mtu			= felix_get_max_mtu,
1601fc411eaaSVladimir Oltean 	.port_policer_add		= felix_port_policer_add,
1602fc411eaaSVladimir Oltean 	.port_policer_del		= felix_port_policer_del,
160307d985eeSVladimir Oltean 	.cls_flower_add			= felix_cls_flower_add,
160407d985eeSVladimir Oltean 	.cls_flower_del			= felix_cls_flower_del,
160507d985eeSVladimir Oltean 	.cls_flower_stats		= felix_cls_flower_stats,
1606de143c0eSXiaoliang Yang 	.port_setup_tc			= felix_port_setup_tc,
1607f59fd9caSVladimir Oltean 	.devlink_sb_pool_get		= felix_sb_pool_get,
1608f59fd9caSVladimir Oltean 	.devlink_sb_pool_set		= felix_sb_pool_set,
1609f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_get	= felix_sb_port_pool_get,
1610f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_set	= felix_sb_port_pool_set,
1611f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_get	= felix_sb_tc_pool_bind_get,
1612f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_set	= felix_sb_tc_pool_bind_set,
1613f59fd9caSVladimir Oltean 	.devlink_sb_occ_snapshot	= felix_sb_occ_snapshot,
1614f59fd9caSVladimir Oltean 	.devlink_sb_occ_max_clear	= felix_sb_occ_max_clear,
1615f59fd9caSVladimir Oltean 	.devlink_sb_occ_port_pool_get	= felix_sb_occ_port_pool_get,
1616f59fd9caSVladimir Oltean 	.devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
1617a026c50bSHoratiu Vultur 	.port_mrp_add			= felix_mrp_add,
1618a026c50bSHoratiu Vultur 	.port_mrp_del			= felix_mrp_del,
1619a026c50bSHoratiu Vultur 	.port_mrp_add_ring_role		= felix_mrp_add_ring_role,
1620a026c50bSHoratiu Vultur 	.port_mrp_del_ring_role		= felix_mrp_del_ring_role,
16215da11eb4SVladimir Oltean 	.tag_8021q_vlan_add		= felix_tag_8021q_vlan_add,
16225da11eb4SVladimir Oltean 	.tag_8021q_vlan_del		= felix_tag_8021q_vlan_del,
162356051948SVladimir Oltean };
1624319e4dd1SVladimir Oltean 
1625319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
1626319e4dd1SVladimir Oltean {
1627319e4dd1SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1628319e4dd1SVladimir Oltean 	struct dsa_switch *ds = felix->ds;
1629319e4dd1SVladimir Oltean 
1630319e4dd1SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
1631319e4dd1SVladimir Oltean 		return NULL;
1632319e4dd1SVladimir Oltean 
1633319e4dd1SVladimir Oltean 	return dsa_to_port(ds, port)->slave;
1634319e4dd1SVladimir Oltean }
1635319e4dd1SVladimir Oltean 
1636319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev)
1637319e4dd1SVladimir Oltean {
1638319e4dd1SVladimir Oltean 	struct dsa_port *dp;
1639319e4dd1SVladimir Oltean 
1640319e4dd1SVladimir Oltean 	dp = dsa_port_from_netdev(dev);
1641319e4dd1SVladimir Oltean 	if (IS_ERR(dp))
1642319e4dd1SVladimir Oltean 		return -EINVAL;
1643319e4dd1SVladimir Oltean 
1644319e4dd1SVladimir Oltean 	return dp->index;
1645319e4dd1SVladimir Oltean }
1646