xref: /linux/drivers/net/dsa/ocelot/felix.c (revision 5c5416f5)
156051948SVladimir Oltean // SPDX-License-Identifier: GPL-2.0
2adb3dccfSVladimir Oltean /* Copyright 2019-2021 NXP Semiconductors
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>
24588d0550SIoana Ciornei #include <linux/pcs-lynx.h>
25fc411eaaSVladimir Oltean #include <net/pkt_sched.h>
2656051948SVladimir Oltean #include <net/dsa.h>
2756051948SVladimir Oltean #include "felix.h"
2856051948SVladimir Oltean 
29e21268efSVladimir Oltean static int felix_tag_8021q_rxvlan_add(struct felix *felix, int port, u16 vid,
30e21268efSVladimir Oltean 				      bool pvid, bool untagged)
31e21268efSVladimir Oltean {
32e21268efSVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
33e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
34e21268efSVladimir Oltean 	struct dsa_switch *ds = felix->ds;
35e21268efSVladimir Oltean 	int key_length, upstream, err;
36e21268efSVladimir Oltean 
37e21268efSVladimir Oltean 	/* We don't need to install the rxvlan into the other ports' filtering
38e21268efSVladimir Oltean 	 * tables, because we're just pushing the rxvlan when sending towards
39e21268efSVladimir Oltean 	 * the CPU
40e21268efSVladimir Oltean 	 */
41e21268efSVladimir Oltean 	if (!pvid)
42e21268efSVladimir Oltean 		return 0;
43e21268efSVladimir Oltean 
44e21268efSVladimir Oltean 	key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length;
45e21268efSVladimir Oltean 	upstream = dsa_upstream_port(ds, port);
46e21268efSVladimir Oltean 
47e21268efSVladimir Oltean 	outer_tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter),
48e21268efSVladimir Oltean 				     GFP_KERNEL);
49e21268efSVladimir Oltean 	if (!outer_tagging_rule)
50e21268efSVladimir Oltean 		return -ENOMEM;
51e21268efSVladimir Oltean 
52e21268efSVladimir Oltean 	outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
53e21268efSVladimir Oltean 	outer_tagging_rule->prio = 1;
54e21268efSVladimir Oltean 	outer_tagging_rule->id.cookie = port;
55e21268efSVladimir Oltean 	outer_tagging_rule->id.tc_offload = false;
56e21268efSVladimir Oltean 	outer_tagging_rule->block_id = VCAP_ES0;
57e21268efSVladimir Oltean 	outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
58e21268efSVladimir Oltean 	outer_tagging_rule->lookup = 0;
59e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.value = port;
60e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0);
61e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.value = upstream;
62e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0);
63e21268efSVladimir Oltean 	outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG;
64e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD;
65e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_vid_sel = 1;
66e21268efSVladimir Oltean 	outer_tagging_rule->action.vid_a_val = vid;
67e21268efSVladimir Oltean 
68e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL);
69e21268efSVladimir Oltean 	if (err)
70e21268efSVladimir Oltean 		kfree(outer_tagging_rule);
71e21268efSVladimir Oltean 
72e21268efSVladimir Oltean 	return err;
73e21268efSVladimir Oltean }
74e21268efSVladimir Oltean 
75e21268efSVladimir Oltean static int felix_tag_8021q_txvlan_add(struct felix *felix, int port, u16 vid,
76e21268efSVladimir Oltean 				      bool pvid, bool untagged)
77e21268efSVladimir Oltean {
78e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
79e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
80e21268efSVladimir Oltean 	struct dsa_switch *ds = felix->ds;
81e21268efSVladimir Oltean 	int upstream, err;
82e21268efSVladimir Oltean 
83e21268efSVladimir Oltean 	/* tag_8021q.c assumes we are implementing this via port VLAN
84e21268efSVladimir Oltean 	 * membership, which we aren't. So we don't need to add any VCAP filter
85e21268efSVladimir Oltean 	 * for the CPU port.
86e21268efSVladimir Oltean 	 */
87e21268efSVladimir Oltean 	if (ocelot->ports[port]->is_dsa_8021q_cpu)
88e21268efSVladimir Oltean 		return 0;
89e21268efSVladimir Oltean 
90e21268efSVladimir Oltean 	untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
91e21268efSVladimir Oltean 	if (!untagging_rule)
92e21268efSVladimir Oltean 		return -ENOMEM;
93e21268efSVladimir Oltean 
94e21268efSVladimir Oltean 	redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
95e21268efSVladimir Oltean 	if (!redirect_rule) {
96e21268efSVladimir Oltean 		kfree(untagging_rule);
97e21268efSVladimir Oltean 		return -ENOMEM;
98e21268efSVladimir Oltean 	}
99e21268efSVladimir Oltean 
100e21268efSVladimir Oltean 	upstream = dsa_upstream_port(ds, port);
101e21268efSVladimir Oltean 
102e21268efSVladimir Oltean 	untagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
103e21268efSVladimir Oltean 	untagging_rule->ingress_port_mask = BIT(upstream);
104e21268efSVladimir Oltean 	untagging_rule->vlan.vid.value = vid;
105e21268efSVladimir Oltean 	untagging_rule->vlan.vid.mask = VLAN_VID_MASK;
106e21268efSVladimir Oltean 	untagging_rule->prio = 1;
107e21268efSVladimir Oltean 	untagging_rule->id.cookie = port;
108e21268efSVladimir Oltean 	untagging_rule->id.tc_offload = false;
109e21268efSVladimir Oltean 	untagging_rule->block_id = VCAP_IS1;
110e21268efSVladimir Oltean 	untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
111e21268efSVladimir Oltean 	untagging_rule->lookup = 0;
112e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt_ena = true;
113e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt = 1;
114e21268efSVladimir Oltean 	untagging_rule->action.pag_override_mask = 0xff;
115e21268efSVladimir Oltean 	untagging_rule->action.pag_val = port;
116e21268efSVladimir Oltean 
117e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL);
118e21268efSVladimir Oltean 	if (err) {
119e21268efSVladimir Oltean 		kfree(untagging_rule);
120e21268efSVladimir Oltean 		kfree(redirect_rule);
121e21268efSVladimir Oltean 		return err;
122e21268efSVladimir Oltean 	}
123e21268efSVladimir Oltean 
124e21268efSVladimir Oltean 	redirect_rule->key_type = OCELOT_VCAP_KEY_ANY;
125e21268efSVladimir Oltean 	redirect_rule->ingress_port_mask = BIT(upstream);
126e21268efSVladimir Oltean 	redirect_rule->pag = port;
127e21268efSVladimir Oltean 	redirect_rule->prio = 1;
128e21268efSVladimir Oltean 	redirect_rule->id.cookie = port;
129e21268efSVladimir Oltean 	redirect_rule->id.tc_offload = false;
130e21268efSVladimir Oltean 	redirect_rule->block_id = VCAP_IS2;
131e21268efSVladimir Oltean 	redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
132e21268efSVladimir Oltean 	redirect_rule->lookup = 0;
133e21268efSVladimir Oltean 	redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT;
134e21268efSVladimir Oltean 	redirect_rule->action.port_mask = BIT(port);
135e21268efSVladimir Oltean 
136e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL);
137e21268efSVladimir Oltean 	if (err) {
138e21268efSVladimir Oltean 		ocelot_vcap_filter_del(ocelot, untagging_rule);
139e21268efSVladimir Oltean 		kfree(redirect_rule);
140e21268efSVladimir Oltean 		return err;
141e21268efSVladimir Oltean 	}
142e21268efSVladimir Oltean 
143e21268efSVladimir Oltean 	return 0;
144e21268efSVladimir Oltean }
145e21268efSVladimir Oltean 
146e21268efSVladimir Oltean static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
147e21268efSVladimir Oltean 				    u16 flags)
148e21268efSVladimir Oltean {
149e21268efSVladimir Oltean 	bool untagged = flags & BRIDGE_VLAN_INFO_UNTAGGED;
150e21268efSVladimir Oltean 	bool pvid = flags & BRIDGE_VLAN_INFO_PVID;
151e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
152e21268efSVladimir Oltean 
153e21268efSVladimir Oltean 	if (vid_is_dsa_8021q_rxvlan(vid))
154e21268efSVladimir Oltean 		return felix_tag_8021q_rxvlan_add(ocelot_to_felix(ocelot),
155e21268efSVladimir Oltean 						  port, vid, pvid, untagged);
156e21268efSVladimir Oltean 
157e21268efSVladimir Oltean 	if (vid_is_dsa_8021q_txvlan(vid))
158e21268efSVladimir Oltean 		return felix_tag_8021q_txvlan_add(ocelot_to_felix(ocelot),
159e21268efSVladimir Oltean 						  port, vid, pvid, untagged);
160e21268efSVladimir Oltean 
161e21268efSVladimir Oltean 	return 0;
162e21268efSVladimir Oltean }
163e21268efSVladimir Oltean 
164e21268efSVladimir Oltean static int felix_tag_8021q_rxvlan_del(struct felix *felix, int port, u16 vid)
165e21268efSVladimir Oltean {
166e21268efSVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
167e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_es0;
168e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
169e21268efSVladimir Oltean 
170e21268efSVladimir Oltean 	block_vcap_es0 = &ocelot->block[VCAP_ES0];
171e21268efSVladimir Oltean 
172e21268efSVladimir Oltean 	outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0,
173e21268efSVladimir Oltean 								 port, false);
174e21268efSVladimir Oltean 	/* In rxvlan_add, we had the "if (!pvid) return 0" logic to avoid
175e21268efSVladimir Oltean 	 * installing outer tagging ES0 rules where they weren't needed.
176e21268efSVladimir Oltean 	 * But in rxvlan_del, the API doesn't give us the "flags" anymore,
177e21268efSVladimir Oltean 	 * so that forces us to be slightly sloppy here, and just assume that
178e21268efSVladimir Oltean 	 * if we didn't find an outer_tagging_rule it means that there was
179e21268efSVladimir Oltean 	 * none in the first place, i.e. rxvlan_del is called on a non-pvid
180e21268efSVladimir Oltean 	 * port. This is most probably true though.
181e21268efSVladimir Oltean 	 */
182e21268efSVladimir Oltean 	if (!outer_tagging_rule)
183e21268efSVladimir Oltean 		return 0;
184e21268efSVladimir Oltean 
185e21268efSVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, outer_tagging_rule);
186e21268efSVladimir Oltean }
187e21268efSVladimir Oltean 
188e21268efSVladimir Oltean static int felix_tag_8021q_txvlan_del(struct felix *felix, int port, u16 vid)
189e21268efSVladimir Oltean {
190e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
191e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is1;
192e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
193e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
194e21268efSVladimir Oltean 	int err;
195e21268efSVladimir Oltean 
196e21268efSVladimir Oltean 	if (ocelot->ports[port]->is_dsa_8021q_cpu)
197e21268efSVladimir Oltean 		return 0;
198e21268efSVladimir Oltean 
199e21268efSVladimir Oltean 	block_vcap_is1 = &ocelot->block[VCAP_IS1];
200e21268efSVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
201e21268efSVladimir Oltean 
202e21268efSVladimir Oltean 	untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1,
203e21268efSVladimir Oltean 							     port, false);
204e21268efSVladimir Oltean 	if (!untagging_rule)
205e21268efSVladimir Oltean 		return 0;
206e21268efSVladimir Oltean 
207e21268efSVladimir Oltean 	err = ocelot_vcap_filter_del(ocelot, untagging_rule);
208e21268efSVladimir Oltean 	if (err)
209e21268efSVladimir Oltean 		return err;
210e21268efSVladimir Oltean 
211e21268efSVladimir Oltean 	redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2,
212e21268efSVladimir Oltean 							    port, false);
213e21268efSVladimir Oltean 	if (!redirect_rule)
214e21268efSVladimir Oltean 		return 0;
215e21268efSVladimir Oltean 
216e21268efSVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, redirect_rule);
217e21268efSVladimir Oltean }
218e21268efSVladimir Oltean 
219e21268efSVladimir Oltean static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
220e21268efSVladimir Oltean {
221e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
222e21268efSVladimir Oltean 
223e21268efSVladimir Oltean 	if (vid_is_dsa_8021q_rxvlan(vid))
224e21268efSVladimir Oltean 		return felix_tag_8021q_rxvlan_del(ocelot_to_felix(ocelot),
225e21268efSVladimir Oltean 						  port, vid);
226e21268efSVladimir Oltean 
227e21268efSVladimir Oltean 	if (vid_is_dsa_8021q_txvlan(vid))
228e21268efSVladimir Oltean 		return felix_tag_8021q_txvlan_del(ocelot_to_felix(ocelot),
229e21268efSVladimir Oltean 						  port, vid);
230e21268efSVladimir Oltean 
231e21268efSVladimir Oltean 	return 0;
232e21268efSVladimir Oltean }
233e21268efSVladimir Oltean 
234e21268efSVladimir Oltean static const struct dsa_8021q_ops felix_tag_8021q_ops = {
235e21268efSVladimir Oltean 	.vlan_add	= felix_tag_8021q_vlan_add,
236e21268efSVladimir Oltean 	.vlan_del	= felix_tag_8021q_vlan_del,
237e21268efSVladimir Oltean };
238e21268efSVladimir Oltean 
239e21268efSVladimir Oltean /* Alternatively to using the NPI functionality, that same hardware MAC
240e21268efSVladimir Oltean  * connected internally to the enetc or fman DSA master can be configured to
241e21268efSVladimir Oltean  * use the software-defined tag_8021q frame format. As far as the hardware is
242e21268efSVladimir Oltean  * concerned, it thinks it is a "dumb switch" - the queues of the CPU port
243e21268efSVladimir Oltean  * module are now disconnected from it, but can still be accessed through
244e21268efSVladimir Oltean  * register-based MMIO.
245e21268efSVladimir Oltean  */
246e21268efSVladimir Oltean static void felix_8021q_cpu_port_init(struct ocelot *ocelot, int port)
247e21268efSVladimir Oltean {
248e21268efSVladimir Oltean 	ocelot->ports[port]->is_dsa_8021q_cpu = true;
249e21268efSVladimir Oltean 	ocelot->npi = -1;
250e21268efSVladimir Oltean 
251e21268efSVladimir Oltean 	/* Overwrite PGID_CPU with the non-tagging port */
252e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, PGID_CPU);
253e21268efSVladimir Oltean 
254e21268efSVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
255e21268efSVladimir Oltean }
256e21268efSVladimir Oltean 
257e21268efSVladimir Oltean static void felix_8021q_cpu_port_deinit(struct ocelot *ocelot, int port)
258e21268efSVladimir 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 
265e21268efSVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
266e21268efSVladimir Oltean }
267e21268efSVladimir Oltean 
268c8c0ba4fSVladimir Oltean /* Set up a VCAP IS2 rule for delivering PTP frames to the CPU port module.
269c8c0ba4fSVladimir Oltean  * If the quirk_no_xtr_irq is in place, then also copy those PTP frames to the
270c8c0ba4fSVladimir Oltean  * tag_8021q CPU port.
271c8c0ba4fSVladimir Oltean  */
272c8c0ba4fSVladimir Oltean static int felix_setup_mmio_filtering(struct felix *felix)
273c8c0ba4fSVladimir Oltean {
274c8c0ba4fSVladimir Oltean 	unsigned long user_ports = 0, cpu_ports = 0;
275c8c0ba4fSVladimir Oltean 	struct ocelot_vcap_filter *redirect_rule;
276c8c0ba4fSVladimir Oltean 	struct ocelot_vcap_filter *tagging_rule;
277c8c0ba4fSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
278c8c0ba4fSVladimir Oltean 	struct dsa_switch *ds = felix->ds;
279c8c0ba4fSVladimir Oltean 	int port, ret;
280c8c0ba4fSVladimir Oltean 
281c8c0ba4fSVladimir Oltean 	tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
282c8c0ba4fSVladimir Oltean 	if (!tagging_rule)
283c8c0ba4fSVladimir Oltean 		return -ENOMEM;
284c8c0ba4fSVladimir Oltean 
285c8c0ba4fSVladimir Oltean 	redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
286c8c0ba4fSVladimir Oltean 	if (!redirect_rule) {
287c8c0ba4fSVladimir Oltean 		kfree(tagging_rule);
288c8c0ba4fSVladimir Oltean 		return -ENOMEM;
289c8c0ba4fSVladimir Oltean 	}
290c8c0ba4fSVladimir Oltean 
291c8c0ba4fSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
292c8c0ba4fSVladimir Oltean 		if (dsa_is_user_port(ds, port))
293c8c0ba4fSVladimir Oltean 			user_ports |= BIT(port);
294c8c0ba4fSVladimir Oltean 		if (dsa_is_cpu_port(ds, port))
295c8c0ba4fSVladimir Oltean 			cpu_ports |= BIT(port);
296c8c0ba4fSVladimir Oltean 	}
297c8c0ba4fSVladimir Oltean 
298c8c0ba4fSVladimir Oltean 	tagging_rule->key_type = OCELOT_VCAP_KEY_ETYPE;
299c8c0ba4fSVladimir Oltean 	*(__be16 *)tagging_rule->key.etype.etype.value = htons(ETH_P_1588);
300c8c0ba4fSVladimir Oltean 	*(__be16 *)tagging_rule->key.etype.etype.mask = htons(0xffff);
301c8c0ba4fSVladimir Oltean 	tagging_rule->ingress_port_mask = user_ports;
302c8c0ba4fSVladimir Oltean 	tagging_rule->prio = 1;
303c8c0ba4fSVladimir Oltean 	tagging_rule->id.cookie = ocelot->num_phys_ports;
304c8c0ba4fSVladimir Oltean 	tagging_rule->id.tc_offload = false;
305c8c0ba4fSVladimir Oltean 	tagging_rule->block_id = VCAP_IS1;
306c8c0ba4fSVladimir Oltean 	tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
307c8c0ba4fSVladimir Oltean 	tagging_rule->lookup = 0;
308c8c0ba4fSVladimir Oltean 	tagging_rule->action.pag_override_mask = 0xff;
309c8c0ba4fSVladimir Oltean 	tagging_rule->action.pag_val = ocelot->num_phys_ports;
310c8c0ba4fSVladimir Oltean 
311c8c0ba4fSVladimir Oltean 	ret = ocelot_vcap_filter_add(ocelot, tagging_rule, NULL);
312c8c0ba4fSVladimir Oltean 	if (ret) {
313c8c0ba4fSVladimir Oltean 		kfree(tagging_rule);
314c8c0ba4fSVladimir Oltean 		kfree(redirect_rule);
315c8c0ba4fSVladimir Oltean 		return ret;
316c8c0ba4fSVladimir Oltean 	}
317c8c0ba4fSVladimir Oltean 
318c8c0ba4fSVladimir Oltean 	redirect_rule->key_type = OCELOT_VCAP_KEY_ANY;
319c8c0ba4fSVladimir Oltean 	redirect_rule->ingress_port_mask = user_ports;
320c8c0ba4fSVladimir Oltean 	redirect_rule->pag = ocelot->num_phys_ports;
321c8c0ba4fSVladimir Oltean 	redirect_rule->prio = 1;
322c8c0ba4fSVladimir Oltean 	redirect_rule->id.cookie = ocelot->num_phys_ports;
323c8c0ba4fSVladimir Oltean 	redirect_rule->id.tc_offload = false;
324c8c0ba4fSVladimir Oltean 	redirect_rule->block_id = VCAP_IS2;
325c8c0ba4fSVladimir Oltean 	redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
326c8c0ba4fSVladimir Oltean 	redirect_rule->lookup = 0;
327c8c0ba4fSVladimir Oltean 	redirect_rule->action.cpu_copy_ena = true;
328c8c0ba4fSVladimir Oltean 	if (felix->info->quirk_no_xtr_irq) {
329c8c0ba4fSVladimir Oltean 		/* Redirect to the tag_8021q CPU but also copy PTP packets to
330c8c0ba4fSVladimir Oltean 		 * the CPU port module
331c8c0ba4fSVladimir Oltean 		 */
332c8c0ba4fSVladimir Oltean 		redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT;
333c8c0ba4fSVladimir Oltean 		redirect_rule->action.port_mask = cpu_ports;
334c8c0ba4fSVladimir Oltean 	} else {
335c8c0ba4fSVladimir Oltean 		/* Trap PTP packets only to the CPU port module (which is
336c8c0ba4fSVladimir Oltean 		 * redirected to the NPI port)
337c8c0ba4fSVladimir Oltean 		 */
338c8c0ba4fSVladimir Oltean 		redirect_rule->action.mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
339c8c0ba4fSVladimir Oltean 		redirect_rule->action.port_mask = 0;
340c8c0ba4fSVladimir Oltean 	}
341c8c0ba4fSVladimir Oltean 
342c8c0ba4fSVladimir Oltean 	ret = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL);
343c8c0ba4fSVladimir Oltean 	if (ret) {
344c8c0ba4fSVladimir Oltean 		ocelot_vcap_filter_del(ocelot, tagging_rule);
345c8c0ba4fSVladimir Oltean 		kfree(redirect_rule);
346c8c0ba4fSVladimir Oltean 		return ret;
347c8c0ba4fSVladimir Oltean 	}
348c8c0ba4fSVladimir Oltean 
3490a6f17c6SVladimir Oltean 	/* The ownership of the CPU port module's queues might have just been
3500a6f17c6SVladimir Oltean 	 * transferred to the tag_8021q tagger from the NPI-based tagger.
3510a6f17c6SVladimir Oltean 	 * So there might still be all sorts of crap in the queues. On the
3520a6f17c6SVladimir Oltean 	 * other hand, the MMIO-based matching of PTP frames is very brittle,
3530a6f17c6SVladimir Oltean 	 * so we need to be careful that there are no extra frames to be
3540a6f17c6SVladimir Oltean 	 * dequeued over MMIO, since we would never know to discard them.
3550a6f17c6SVladimir Oltean 	 */
3560a6f17c6SVladimir Oltean 	ocelot_drain_cpu_queue(ocelot, 0);
3570a6f17c6SVladimir Oltean 
358c8c0ba4fSVladimir Oltean 	return 0;
359c8c0ba4fSVladimir Oltean }
360c8c0ba4fSVladimir Oltean 
361c8c0ba4fSVladimir Oltean static int felix_teardown_mmio_filtering(struct felix *felix)
362c8c0ba4fSVladimir Oltean {
363c8c0ba4fSVladimir Oltean 	struct ocelot_vcap_filter *tagging_rule, *redirect_rule;
364c8c0ba4fSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is1;
365c8c0ba4fSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
366c8c0ba4fSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
367c8c0ba4fSVladimir Oltean 	int err;
368c8c0ba4fSVladimir Oltean 
369c8c0ba4fSVladimir Oltean 	block_vcap_is1 = &ocelot->block[VCAP_IS1];
370c8c0ba4fSVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
371c8c0ba4fSVladimir Oltean 
372c8c0ba4fSVladimir Oltean 	tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1,
373c8c0ba4fSVladimir Oltean 							   ocelot->num_phys_ports,
374c8c0ba4fSVladimir Oltean 							   false);
375c8c0ba4fSVladimir Oltean 	if (!tagging_rule)
376c8c0ba4fSVladimir Oltean 		return -ENOENT;
377c8c0ba4fSVladimir Oltean 
378c8c0ba4fSVladimir Oltean 	err = ocelot_vcap_filter_del(ocelot, tagging_rule);
379c8c0ba4fSVladimir Oltean 	if (err)
380c8c0ba4fSVladimir Oltean 		return err;
381c8c0ba4fSVladimir Oltean 
382c8c0ba4fSVladimir Oltean 	redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2,
383c8c0ba4fSVladimir Oltean 							    ocelot->num_phys_ports,
384c8c0ba4fSVladimir Oltean 							    false);
385c8c0ba4fSVladimir Oltean 	if (!redirect_rule)
386c8c0ba4fSVladimir Oltean 		return -ENOENT;
387c8c0ba4fSVladimir Oltean 
388c8c0ba4fSVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, redirect_rule);
389c8c0ba4fSVladimir Oltean }
390c8c0ba4fSVladimir Oltean 
391e21268efSVladimir Oltean static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu)
392e21268efSVladimir Oltean {
393e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
394e21268efSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
395e21268efSVladimir Oltean 	unsigned long cpu_flood;
396e21268efSVladimir Oltean 	int port, err;
397e21268efSVladimir Oltean 
398e21268efSVladimir Oltean 	felix_8021q_cpu_port_init(ocelot, cpu);
399e21268efSVladimir Oltean 
400e21268efSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
401e21268efSVladimir Oltean 		if (dsa_is_unused_port(ds, port))
402e21268efSVladimir Oltean 			continue;
403e21268efSVladimir Oltean 
404e21268efSVladimir Oltean 		/* This overwrites ocelot_init():
405e21268efSVladimir Oltean 		 * Do not forward BPDU frames to the CPU port module,
406e21268efSVladimir Oltean 		 * for 2 reasons:
407e21268efSVladimir Oltean 		 * - When these packets are injected from the tag_8021q
408e21268efSVladimir Oltean 		 *   CPU port, we want them to go out, not loop back
409e21268efSVladimir Oltean 		 *   into the system.
410e21268efSVladimir Oltean 		 * - STP traffic ingressing on a user port should go to
411e21268efSVladimir Oltean 		 *   the tag_8021q CPU port, not to the hardware CPU
412e21268efSVladimir Oltean 		 *   port module.
413e21268efSVladimir Oltean 		 */
414e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
415e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0),
416e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG, port);
417e21268efSVladimir Oltean 	}
418e21268efSVladimir Oltean 
419c8c0ba4fSVladimir Oltean 	/* In tag_8021q mode, the CPU port module is unused, except for PTP
420c8c0ba4fSVladimir Oltean 	 * frames. So we want to disable flooding of any kind to the CPU port
421c8c0ba4fSVladimir Oltean 	 * module, since packets going there will end in a black hole.
422e21268efSVladimir Oltean 	 */
423e21268efSVladimir Oltean 	cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
424e21268efSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_UC);
425e21268efSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_MC);
426b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_BC);
427e21268efSVladimir Oltean 
428e21268efSVladimir Oltean 	felix->dsa_8021q_ctx = kzalloc(sizeof(*felix->dsa_8021q_ctx),
429e21268efSVladimir Oltean 				       GFP_KERNEL);
430e21268efSVladimir Oltean 	if (!felix->dsa_8021q_ctx)
431e21268efSVladimir Oltean 		return -ENOMEM;
432e21268efSVladimir Oltean 
433e21268efSVladimir Oltean 	felix->dsa_8021q_ctx->ops = &felix_tag_8021q_ops;
434e21268efSVladimir Oltean 	felix->dsa_8021q_ctx->proto = htons(ETH_P_8021AD);
435e21268efSVladimir Oltean 	felix->dsa_8021q_ctx->ds = ds;
436e21268efSVladimir Oltean 
437e21268efSVladimir Oltean 	err = dsa_8021q_setup(felix->dsa_8021q_ctx, true);
438e21268efSVladimir Oltean 	if (err)
439e21268efSVladimir Oltean 		goto out_free_dsa_8021_ctx;
440e21268efSVladimir Oltean 
441c8c0ba4fSVladimir Oltean 	err = felix_setup_mmio_filtering(felix);
442c8c0ba4fSVladimir Oltean 	if (err)
443c8c0ba4fSVladimir Oltean 		goto out_teardown_dsa_8021q;
444c8c0ba4fSVladimir Oltean 
445e21268efSVladimir Oltean 	return 0;
446e21268efSVladimir Oltean 
447c8c0ba4fSVladimir Oltean out_teardown_dsa_8021q:
448c8c0ba4fSVladimir Oltean 	dsa_8021q_setup(felix->dsa_8021q_ctx, false);
449e21268efSVladimir Oltean out_free_dsa_8021_ctx:
450e21268efSVladimir Oltean 	kfree(felix->dsa_8021q_ctx);
451e21268efSVladimir Oltean 	return err;
452e21268efSVladimir Oltean }
453e21268efSVladimir Oltean 
454e21268efSVladimir Oltean static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu)
455e21268efSVladimir Oltean {
456e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
457e21268efSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
458e21268efSVladimir Oltean 	int err, port;
459e21268efSVladimir Oltean 
460c8c0ba4fSVladimir Oltean 	err = felix_teardown_mmio_filtering(felix);
461c8c0ba4fSVladimir Oltean 	if (err)
462c8c0ba4fSVladimir Oltean 		dev_err(ds->dev, "felix_teardown_mmio_filtering returned %d",
463c8c0ba4fSVladimir Oltean 			err);
464c8c0ba4fSVladimir Oltean 
465e21268efSVladimir Oltean 	err = dsa_8021q_setup(felix->dsa_8021q_ctx, false);
466e21268efSVladimir Oltean 	if (err)
467e21268efSVladimir Oltean 		dev_err(ds->dev, "dsa_8021q_setup returned %d", err);
468e21268efSVladimir Oltean 
469e21268efSVladimir Oltean 	kfree(felix->dsa_8021q_ctx);
470e21268efSVladimir Oltean 
471e21268efSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
472e21268efSVladimir Oltean 		if (dsa_is_unused_port(ds, port))
473e21268efSVladimir Oltean 			continue;
474e21268efSVladimir Oltean 
475e21268efSVladimir Oltean 		/* Restore the logic from ocelot_init:
476e21268efSVladimir Oltean 		 * do not forward BPDU frames to the front ports.
477e21268efSVladimir Oltean 		 */
478e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
479e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
480e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG,
481e21268efSVladimir Oltean 				 port);
482e21268efSVladimir Oltean 	}
483e21268efSVladimir Oltean 
484e21268efSVladimir Oltean 	felix_8021q_cpu_port_deinit(ocelot, cpu);
485e21268efSVladimir Oltean }
486e21268efSVladimir Oltean 
487adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This
488adb3dccfSVladimir Oltean  * is the mode through which frames can be injected from and extracted to an
489adb3dccfSVladimir Oltean  * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU
490adb3dccfSVladimir Oltean  * running Linux, and this forms a DSA setup together with the enetc or fman
491adb3dccfSVladimir Oltean  * DSA master.
492adb3dccfSVladimir Oltean  */
493adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port)
494adb3dccfSVladimir Oltean {
495adb3dccfSVladimir Oltean 	ocelot->npi = port;
496adb3dccfSVladimir Oltean 
497adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
498adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
499adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
500adb3dccfSVladimir Oltean 
501adb3dccfSVladimir Oltean 	/* NPI port Injection/Extraction configuration */
502adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
503adb3dccfSVladimir Oltean 			    ocelot->npi_xtr_prefix);
504adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
505adb3dccfSVladimir Oltean 			    ocelot->npi_inj_prefix);
506adb3dccfSVladimir Oltean 
507adb3dccfSVladimir Oltean 	/* Disable transmission of pause frames */
508adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
509adb3dccfSVladimir Oltean }
510adb3dccfSVladimir Oltean 
511adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port)
512adb3dccfSVladimir Oltean {
513adb3dccfSVladimir Oltean 	/* Restore hardware defaults */
514adb3dccfSVladimir Oltean 	int unused_port = ocelot->num_phys_ports + 2;
515adb3dccfSVladimir Oltean 
516adb3dccfSVladimir Oltean 	ocelot->npi = -1;
517adb3dccfSVladimir Oltean 
518adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port),
519adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
520adb3dccfSVladimir Oltean 
521adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
522adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
523adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
524adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
525adb3dccfSVladimir Oltean 
526adb3dccfSVladimir Oltean 	/* Enable transmission of pause frames */
527adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
528adb3dccfSVladimir Oltean }
529adb3dccfSVladimir Oltean 
530adb3dccfSVladimir Oltean static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu)
531adb3dccfSVladimir Oltean {
532adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
533adb3dccfSVladimir Oltean 	unsigned long cpu_flood;
534adb3dccfSVladimir Oltean 
535adb3dccfSVladimir Oltean 	felix_npi_port_init(ocelot, cpu);
536adb3dccfSVladimir Oltean 
537adb3dccfSVladimir Oltean 	/* Include the CPU port module (and indirectly, the NPI port)
538adb3dccfSVladimir Oltean 	 * in the forwarding mask for unknown unicast - the hardware
539adb3dccfSVladimir Oltean 	 * default value for ANA_FLOODING_FLD_UNICAST excludes
540adb3dccfSVladimir Oltean 	 * BIT(ocelot->num_phys_ports), and so does ocelot_init,
541adb3dccfSVladimir Oltean 	 * since Ocelot relies on whitelisting MAC addresses towards
542adb3dccfSVladimir Oltean 	 * PGID_CPU.
543adb3dccfSVladimir Oltean 	 * We do this because DSA does not yet perform RX filtering,
544adb3dccfSVladimir Oltean 	 * and the NPI port does not perform source address learning,
545adb3dccfSVladimir Oltean 	 * so traffic sent to Linux is effectively unknown from the
546adb3dccfSVladimir Oltean 	 * switch's perspective.
547adb3dccfSVladimir Oltean 	 */
548adb3dccfSVladimir Oltean 	cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
549adb3dccfSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_UC);
5506edb9e8dSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_MC);
551b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_BC);
552adb3dccfSVladimir Oltean 
553adb3dccfSVladimir Oltean 	return 0;
554adb3dccfSVladimir Oltean }
555adb3dccfSVladimir Oltean 
556adb3dccfSVladimir Oltean static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu)
557adb3dccfSVladimir Oltean {
558adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
559adb3dccfSVladimir Oltean 
560adb3dccfSVladimir Oltean 	felix_npi_port_deinit(ocelot, cpu);
561adb3dccfSVladimir Oltean }
562adb3dccfSVladimir Oltean 
563adb3dccfSVladimir Oltean static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu,
564adb3dccfSVladimir Oltean 				  enum dsa_tag_protocol proto)
565adb3dccfSVladimir Oltean {
566adb3dccfSVladimir Oltean 	int err;
567adb3dccfSVladimir Oltean 
568adb3dccfSVladimir Oltean 	switch (proto) {
5697c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
570adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
571adb3dccfSVladimir Oltean 		err = felix_setup_tag_npi(ds, cpu);
572adb3dccfSVladimir Oltean 		break;
573e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
574e21268efSVladimir Oltean 		err = felix_setup_tag_8021q(ds, cpu);
575e21268efSVladimir Oltean 		break;
576adb3dccfSVladimir Oltean 	default:
577adb3dccfSVladimir Oltean 		err = -EPROTONOSUPPORT;
578adb3dccfSVladimir Oltean 	}
579adb3dccfSVladimir Oltean 
580adb3dccfSVladimir Oltean 	return err;
581adb3dccfSVladimir Oltean }
582adb3dccfSVladimir Oltean 
583adb3dccfSVladimir Oltean static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu,
584adb3dccfSVladimir Oltean 				   enum dsa_tag_protocol proto)
585adb3dccfSVladimir Oltean {
586adb3dccfSVladimir Oltean 	switch (proto) {
5877c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
588adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
589adb3dccfSVladimir Oltean 		felix_teardown_tag_npi(ds, cpu);
590adb3dccfSVladimir Oltean 		break;
591e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
592e21268efSVladimir Oltean 		felix_teardown_tag_8021q(ds, cpu);
593e21268efSVladimir Oltean 		break;
594adb3dccfSVladimir Oltean 	default:
595adb3dccfSVladimir Oltean 		break;
596adb3dccfSVladimir Oltean 	}
597adb3dccfSVladimir Oltean }
598adb3dccfSVladimir Oltean 
599e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the
600e21268efSVladimir Oltean  * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
601e21268efSVladimir Oltean  * or the restoration is guaranteed to work.
602e21268efSVladimir Oltean  */
603adb3dccfSVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu,
604adb3dccfSVladimir Oltean 				     enum dsa_tag_protocol proto)
605adb3dccfSVladimir Oltean {
606adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
607adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
608adb3dccfSVladimir Oltean 	enum dsa_tag_protocol old_proto = felix->tag_proto;
609adb3dccfSVladimir Oltean 	int err;
610adb3dccfSVladimir Oltean 
6117c4bb540SVladimir Oltean 	if (proto != DSA_TAG_PROTO_SEVILLE &&
6127c4bb540SVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT &&
613e21268efSVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT_8021Q)
614adb3dccfSVladimir Oltean 		return -EPROTONOSUPPORT;
615adb3dccfSVladimir Oltean 
616adb3dccfSVladimir Oltean 	felix_del_tag_protocol(ds, cpu, old_proto);
617adb3dccfSVladimir Oltean 
618adb3dccfSVladimir Oltean 	err = felix_set_tag_protocol(ds, cpu, proto);
619adb3dccfSVladimir Oltean 	if (err) {
620adb3dccfSVladimir Oltean 		felix_set_tag_protocol(ds, cpu, old_proto);
621adb3dccfSVladimir Oltean 		return err;
622adb3dccfSVladimir Oltean 	}
623adb3dccfSVladimir Oltean 
624adb3dccfSVladimir Oltean 	felix->tag_proto = proto;
625adb3dccfSVladimir Oltean 
626adb3dccfSVladimir Oltean 	return 0;
627adb3dccfSVladimir Oltean }
628adb3dccfSVladimir Oltean 
62956051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
6304d776482SFlorian Fainelli 						    int port,
6314d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
63256051948SVladimir Oltean {
633adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
634adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
635adb3dccfSVladimir Oltean 
636adb3dccfSVladimir Oltean 	return felix->tag_proto;
63756051948SVladimir Oltean }
63856051948SVladimir Oltean 
63956051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
64056051948SVladimir Oltean 				 unsigned int ageing_time)
64156051948SVladimir Oltean {
64256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
64356051948SVladimir Oltean 
64456051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
64556051948SVladimir Oltean 
64656051948SVladimir Oltean 	return 0;
64756051948SVladimir Oltean }
64856051948SVladimir Oltean 
64956051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
65056051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
65156051948SVladimir Oltean {
65256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
65356051948SVladimir Oltean 
65456051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
65556051948SVladimir Oltean }
65656051948SVladimir Oltean 
65756051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
65856051948SVladimir Oltean 			 const unsigned char *addr, u16 vid)
65956051948SVladimir Oltean {
66056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
66156051948SVladimir Oltean 
66287b0f983SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid);
66356051948SVladimir Oltean }
66456051948SVladimir Oltean 
66556051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
66656051948SVladimir Oltean 			 const unsigned char *addr, u16 vid)
66756051948SVladimir Oltean {
66856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
66956051948SVladimir Oltean 
67056051948SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid);
67156051948SVladimir Oltean }
67256051948SVladimir Oltean 
673a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port,
674209edf95SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb)
675209edf95SVladimir Oltean {
676209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
677209edf95SVladimir Oltean 
678a52b2da7SVladimir Oltean 	return ocelot_port_mdb_add(ocelot, port, mdb);
679209edf95SVladimir Oltean }
680209edf95SVladimir Oltean 
681209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port,
682209edf95SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb)
683209edf95SVladimir Oltean {
684209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
685209edf95SVladimir Oltean 
686209edf95SVladimir Oltean 	return ocelot_port_mdb_del(ocelot, port, mdb);
687209edf95SVladimir Oltean }
688209edf95SVladimir Oltean 
68956051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
69056051948SVladimir Oltean 				       u8 state)
69156051948SVladimir Oltean {
69256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
69356051948SVladimir Oltean 
69456051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
69556051948SVladimir Oltean }
69656051948SVladimir Oltean 
697421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port,
698421741eaSVladimir Oltean 				  struct switchdev_brport_flags val,
699421741eaSVladimir Oltean 				  struct netlink_ext_ack *extack)
700421741eaSVladimir Oltean {
701421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
702421741eaSVladimir Oltean 
703421741eaSVladimir Oltean 	return ocelot_port_pre_bridge_flags(ocelot, port, val);
704421741eaSVladimir Oltean }
705421741eaSVladimir Oltean 
706421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port,
707421741eaSVladimir Oltean 			      struct switchdev_brport_flags val,
708421741eaSVladimir Oltean 			      struct netlink_ext_ack *extack)
709421741eaSVladimir Oltean {
710421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
711421741eaSVladimir Oltean 
712421741eaSVladimir Oltean 	ocelot_port_bridge_flags(ocelot, port, val);
713421741eaSVladimir Oltean 
714421741eaSVladimir Oltean 	return 0;
715421741eaSVladimir Oltean }
716421741eaSVladimir Oltean 
71756051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
71856051948SVladimir Oltean 			     struct net_device *br)
71956051948SVladimir Oltean {
72056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
72156051948SVladimir Oltean 
722e4bd44e8SVladimir Oltean 	ocelot_port_bridge_join(ocelot, port, br);
723e4bd44e8SVladimir Oltean 
724e4bd44e8SVladimir Oltean 	return 0;
72556051948SVladimir Oltean }
72656051948SVladimir Oltean 
72756051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
72856051948SVladimir Oltean 			       struct net_device *br)
72956051948SVladimir Oltean {
73056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
73156051948SVladimir Oltean 
73256051948SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, br);
73356051948SVladimir Oltean }
73456051948SVladimir Oltean 
7358fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port,
7368fe6832eSVladimir Oltean 			  struct net_device *bond,
7378fe6832eSVladimir Oltean 			  struct netdev_lag_upper_info *info)
7388fe6832eSVladimir Oltean {
7398fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7408fe6832eSVladimir Oltean 
7418fe6832eSVladimir Oltean 	return ocelot_port_lag_join(ocelot, port, bond, info);
7428fe6832eSVladimir Oltean }
7438fe6832eSVladimir Oltean 
7448fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port,
7458fe6832eSVladimir Oltean 			   struct net_device *bond)
7468fe6832eSVladimir Oltean {
7478fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7488fe6832eSVladimir Oltean 
7498fe6832eSVladimir Oltean 	ocelot_port_lag_leave(ocelot, port, bond);
7508fe6832eSVladimir Oltean 
7518fe6832eSVladimir Oltean 	return 0;
7528fe6832eSVladimir Oltean }
7538fe6832eSVladimir Oltean 
7548fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port)
7558fe6832eSVladimir Oltean {
7568fe6832eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
7578fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7588fe6832eSVladimir Oltean 
7598fe6832eSVladimir Oltean 	ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
7608fe6832eSVladimir Oltean 
7618fe6832eSVladimir Oltean 	return 0;
7628fe6832eSVladimir Oltean }
7638fe6832eSVladimir Oltean 
76456051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
76556051948SVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan)
76656051948SVladimir Oltean {
7672f0402feSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
768b7a9e0daSVladimir Oltean 	u16 flags = vlan->flags;
7692f0402feSVladimir Oltean 
7709a720680SVladimir Oltean 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
7719a720680SVladimir Oltean 	 * egress-untagged or not, pvid or not, make no difference. This
7729a720680SVladimir Oltean 	 * behavior is already better than what DSA just tries to approximate
7739a720680SVladimir Oltean 	 * when it installs the VLAN with the same flags on the CPU port.
7749a720680SVladimir Oltean 	 * Just accept any configuration, and don't let ocelot deny installing
7759a720680SVladimir Oltean 	 * multiple native VLANs on the NPI port, because the switch doesn't
7769a720680SVladimir Oltean 	 * look at the port tag settings towards the NPI interface anyway.
7779a720680SVladimir Oltean 	 */
7789a720680SVladimir Oltean 	if (port == ocelot->npi)
7799a720680SVladimir Oltean 		return 0;
7809a720680SVladimir Oltean 
781b7a9e0daSVladimir Oltean 	return ocelot_vlan_prepare(ocelot, port, vlan->vid,
7822f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_PVID,
7832f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_UNTAGGED);
78456051948SVladimir Oltean }
78556051948SVladimir Oltean 
78689153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
78789153ed6SVladimir Oltean 				struct netlink_ext_ack *extack)
78856051948SVladimir Oltean {
78956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
79056051948SVladimir Oltean 
791bae33f2bSVladimir Oltean 	return ocelot_port_vlan_filtering(ocelot, port, enabled);
79256051948SVladimir Oltean }
79356051948SVladimir Oltean 
7941958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port,
79531046a5fSVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan,
79631046a5fSVladimir Oltean 			  struct netlink_ext_ack *extack)
79756051948SVladimir Oltean {
79856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
799183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
8001958d581SVladimir Oltean 	int err;
80156051948SVladimir Oltean 
8021958d581SVladimir Oltean 	err = felix_vlan_prepare(ds, port, vlan);
8031958d581SVladimir Oltean 	if (err)
8041958d581SVladimir Oltean 		return err;
8051958d581SVladimir Oltean 
8061958d581SVladimir Oltean 	return ocelot_vlan_add(ocelot, port, vlan->vid,
807183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_PVID,
808183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_UNTAGGED);
80956051948SVladimir Oltean }
81056051948SVladimir Oltean 
81156051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
81256051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
81356051948SVladimir Oltean {
81456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
81556051948SVladimir Oltean 
816b7a9e0daSVladimir Oltean 	return ocelot_vlan_del(ocelot, port, vlan->vid);
81756051948SVladimir Oltean }
81856051948SVladimir Oltean 
81956051948SVladimir Oltean static int felix_port_enable(struct dsa_switch *ds, int port,
82056051948SVladimir Oltean 			     struct phy_device *phy)
82156051948SVladimir Oltean {
82256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
82356051948SVladimir Oltean 
82456051948SVladimir Oltean 	ocelot_port_enable(ocelot, port, phy);
82556051948SVladimir Oltean 
82656051948SVladimir Oltean 	return 0;
82756051948SVladimir Oltean }
82856051948SVladimir Oltean 
82956051948SVladimir Oltean static void felix_port_disable(struct dsa_switch *ds, int port)
83056051948SVladimir Oltean {
83156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
83256051948SVladimir Oltean 
83356051948SVladimir Oltean 	return ocelot_port_disable(ocelot, port);
83456051948SVladimir Oltean }
83556051948SVladimir Oltean 
836bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
837bdeced75SVladimir Oltean 				   unsigned long *supported,
838bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
839bdeced75SVladimir Oltean {
840bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
841375e1314SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
842bdeced75SVladimir Oltean 
843375e1314SVladimir Oltean 	if (felix->info->phylink_validate)
844375e1314SVladimir Oltean 		felix->info->phylink_validate(ocelot, port, supported, state);
845bdeced75SVladimir Oltean }
846bdeced75SVladimir Oltean 
847bdeced75SVladimir Oltean static void felix_phylink_mac_config(struct dsa_switch *ds, int port,
848bdeced75SVladimir Oltean 				     unsigned int link_an_mode,
849bdeced75SVladimir Oltean 				     const struct phylink_link_state *state)
850bdeced75SVladimir Oltean {
851bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
852bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
853588d0550SIoana Ciornei 	struct dsa_port *dp = dsa_to_port(ds, port);
854bdeced75SVladimir Oltean 
855588d0550SIoana Ciornei 	if (felix->pcs[port])
856588d0550SIoana Ciornei 		phylink_set_pcs(dp->pl, &felix->pcs[port]->pcs);
857bdeced75SVladimir Oltean }
858bdeced75SVladimir Oltean 
859bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
860bdeced75SVladimir Oltean 					unsigned int link_an_mode,
861bdeced75SVladimir Oltean 					phy_interface_t interface)
862bdeced75SVladimir Oltean {
863bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
864bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
865eb4733d7SVladimir Oltean 	int err;
866bdeced75SVladimir Oltean 
867eb4733d7SVladimir Oltean 	ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA,
868eb4733d7SVladimir Oltean 			 DEV_MAC_ENA_CFG);
869eb4733d7SVladimir Oltean 
870886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
871eb4733d7SVladimir Oltean 
872eb4733d7SVladimir Oltean 	err = ocelot_port_flush(ocelot, port);
873eb4733d7SVladimir Oltean 	if (err)
874eb4733d7SVladimir Oltean 		dev_err(ocelot->dev, "failed to flush port %d: %d\n",
875eb4733d7SVladimir Oltean 			port, err);
876eb4733d7SVladimir Oltean 
877eb4733d7SVladimir Oltean 	/* Put the port in reset. */
878eb4733d7SVladimir Oltean 	ocelot_port_writel(ocelot_port,
879eb4733d7SVladimir Oltean 			   DEV_CLOCK_CFG_MAC_TX_RST |
880eb4733d7SVladimir Oltean 			   DEV_CLOCK_CFG_MAC_RX_RST |
881eb4733d7SVladimir Oltean 			   DEV_CLOCK_CFG_LINK_SPEED(OCELOT_SPEED_1000),
882eb4733d7SVladimir Oltean 			   DEV_CLOCK_CFG);
883bdeced75SVladimir Oltean }
884bdeced75SVladimir Oltean 
885bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
886bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
887bdeced75SVladimir Oltean 				      phy_interface_t interface,
8885b502a7bSRussell King 				      struct phy_device *phydev,
8895b502a7bSRussell King 				      int speed, int duplex,
8905b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
891bdeced75SVladimir Oltean {
892bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
893bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
8947e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
8957e14a2dcSVladimir Oltean 	u32 mac_fc_cfg;
896bdeced75SVladimir Oltean 
8977e14a2dcSVladimir Oltean 	/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
8987e14a2dcSVladimir Oltean 	 * PORT_RST bits in DEV_CLOCK_CFG. Note that the way this system is
8997e14a2dcSVladimir Oltean 	 * integrated is that the MAC speed is fixed and it's the PCS who is
9007e14a2dcSVladimir Oltean 	 * performing the rate adaptation, so we have to write "1000Mbps" into
9017e14a2dcSVladimir Oltean 	 * the LINK_SPEED field of DEV_CLOCK_CFG (which is also its default
9027e14a2dcSVladimir Oltean 	 * value).
9037e14a2dcSVladimir Oltean 	 */
9047e14a2dcSVladimir Oltean 	ocelot_port_writel(ocelot_port,
9057e14a2dcSVladimir Oltean 			   DEV_CLOCK_CFG_LINK_SPEED(OCELOT_SPEED_1000),
9067e14a2dcSVladimir Oltean 			   DEV_CLOCK_CFG);
9077e14a2dcSVladimir Oltean 
9087e14a2dcSVladimir Oltean 	switch (speed) {
9097e14a2dcSVladimir Oltean 	case SPEED_10:
9107e14a2dcSVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(3);
9117e14a2dcSVladimir Oltean 		break;
9127e14a2dcSVladimir Oltean 	case SPEED_100:
9137e14a2dcSVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(2);
9147e14a2dcSVladimir Oltean 		break;
9157e14a2dcSVladimir Oltean 	case SPEED_1000:
9167e14a2dcSVladimir Oltean 	case SPEED_2500:
9177e14a2dcSVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(1);
9187e14a2dcSVladimir Oltean 		break;
9197e14a2dcSVladimir Oltean 	default:
9207e14a2dcSVladimir Oltean 		dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
9217e14a2dcSVladimir Oltean 			port, speed);
9227e14a2dcSVladimir Oltean 		return;
9237e14a2dcSVladimir Oltean 	}
9247e14a2dcSVladimir Oltean 
9257e14a2dcSVladimir Oltean 	/* handle Rx pause in all cases, with 2500base-X this is used for rate
9267e14a2dcSVladimir Oltean 	 * adaptation.
9277e14a2dcSVladimir Oltean 	 */
9287e14a2dcSVladimir Oltean 	mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
9297e14a2dcSVladimir Oltean 
9307e14a2dcSVladimir Oltean 	if (tx_pause)
9317e14a2dcSVladimir Oltean 		mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
9327e14a2dcSVladimir Oltean 			      SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
9337e14a2dcSVladimir Oltean 			      SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
9347e14a2dcSVladimir Oltean 			      SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
9357e14a2dcSVladimir Oltean 
9367e14a2dcSVladimir Oltean 	/* Flow control. Link speed is only used here to evaluate the time
9377e14a2dcSVladimir Oltean 	 * specification in incoming pause frames.
9387e14a2dcSVladimir Oltean 	 */
9397e14a2dcSVladimir Oltean 	ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
9407e14a2dcSVladimir Oltean 
9417e14a2dcSVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
9427e14a2dcSVladimir Oltean 
9437e14a2dcSVladimir Oltean 	/* Undo the effects of felix_phylink_mac_link_down:
9447e14a2dcSVladimir Oltean 	 * enable MAC module
9457e14a2dcSVladimir Oltean 	 */
946bdeced75SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
947bdeced75SVladimir Oltean 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
948bdeced75SVladimir Oltean 
949bdeced75SVladimir Oltean 	/* Enable receiving frames on the port, and activate auto-learning of
950bdeced75SVladimir Oltean 	 * MAC addresses.
951bdeced75SVladimir Oltean 	 */
952bdeced75SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
953bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG_RECV_ENA |
954bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
955bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
956bdeced75SVladimir Oltean 
957bdeced75SVladimir Oltean 	/* Core: Enable port for frame transfer */
958886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
959886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
9607e14a2dcSVladimir Oltean 
9617e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
9627e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
963bdeced75SVladimir Oltean }
964bdeced75SVladimir Oltean 
965bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
966bd2b3161SXiaoliang Yang {
967bd2b3161SXiaoliang Yang 	int i;
968bd2b3161SXiaoliang Yang 
969bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
970bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
971bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
972bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
973bd2b3161SXiaoliang Yang 		       port);
974bd2b3161SXiaoliang Yang 
97570d39a6eSVladimir Oltean 	for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
976bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
977bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
978bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
979bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
980bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
981bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
982bd2b3161SXiaoliang Yang 			      port, i);
983bd2b3161SXiaoliang Yang 	}
984bd2b3161SXiaoliang Yang }
985bd2b3161SXiaoliang Yang 
98656051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
98756051948SVladimir Oltean 			      u32 stringset, u8 *data)
98856051948SVladimir Oltean {
98956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
99056051948SVladimir Oltean 
99156051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
99256051948SVladimir Oltean }
99356051948SVladimir Oltean 
99456051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
99556051948SVladimir Oltean {
99656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
99756051948SVladimir Oltean 
99856051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
99956051948SVladimir Oltean }
100056051948SVladimir Oltean 
100156051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
100256051948SVladimir Oltean {
100356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
100456051948SVladimir Oltean 
100556051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
100656051948SVladimir Oltean }
100756051948SVladimir Oltean 
100856051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
100956051948SVladimir Oltean 			     struct ethtool_ts_info *info)
101056051948SVladimir Oltean {
101156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
101256051948SVladimir Oltean 
101356051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
101456051948SVladimir Oltean }
101556051948SVladimir Oltean 
1016bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
1017bdeced75SVladimir Oltean 				  struct device_node *ports_node,
1018bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
1019bdeced75SVladimir Oltean {
1020bdeced75SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
1021bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1022bdeced75SVladimir Oltean 	struct device_node *child;
1023bdeced75SVladimir Oltean 
102437fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
1025bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
1026bdeced75SVladimir Oltean 		u32 port;
1027bdeced75SVladimir Oltean 		int err;
1028bdeced75SVladimir Oltean 
1029bdeced75SVladimir Oltean 		/* Get switch port number from DT */
1030bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
1031bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
1032bdeced75SVladimir Oltean 				"(property \"reg\")\n");
1033bdeced75SVladimir Oltean 			of_node_put(child);
1034bdeced75SVladimir Oltean 			return -ENODEV;
1035bdeced75SVladimir Oltean 		}
1036bdeced75SVladimir Oltean 
1037bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
1038bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
1039bdeced75SVladimir Oltean 		if (err) {
1040bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
1041bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
1042bdeced75SVladimir Oltean 				port);
1043bdeced75SVladimir Oltean 			of_node_put(child);
1044bdeced75SVladimir Oltean 			return -ENODEV;
1045bdeced75SVladimir Oltean 		}
1046bdeced75SVladimir Oltean 
1047bdeced75SVladimir Oltean 		err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode);
1048bdeced75SVladimir Oltean 		if (err < 0) {
1049bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
1050bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
105159ebb430SSumera Priyadarsini 			of_node_put(child);
1052bdeced75SVladimir Oltean 			return err;
1053bdeced75SVladimir Oltean 		}
1054bdeced75SVladimir Oltean 
1055bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
1056bdeced75SVladimir Oltean 	}
1057bdeced75SVladimir Oltean 
1058bdeced75SVladimir Oltean 	return 0;
1059bdeced75SVladimir Oltean }
1060bdeced75SVladimir Oltean 
1061bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
1062bdeced75SVladimir Oltean {
1063bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1064bdeced75SVladimir Oltean 	struct device_node *switch_node;
1065bdeced75SVladimir Oltean 	struct device_node *ports_node;
1066bdeced75SVladimir Oltean 	int err;
1067bdeced75SVladimir Oltean 
1068bdeced75SVladimir Oltean 	switch_node = dev->of_node;
1069bdeced75SVladimir Oltean 
1070bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
1071bdeced75SVladimir Oltean 	if (!ports_node) {
1072bdeced75SVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
1073bdeced75SVladimir Oltean 		return -ENODEV;
1074bdeced75SVladimir Oltean 	}
1075bdeced75SVladimir Oltean 
1076bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
1077bdeced75SVladimir Oltean 	of_node_put(ports_node);
1078bdeced75SVladimir Oltean 
1079bdeced75SVladimir Oltean 	return err;
1080bdeced75SVladimir Oltean }
1081bdeced75SVladimir Oltean 
108256051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
108356051948SVladimir Oltean {
108456051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
1085bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
1086b4024c9eSClaudiu Manoil 	struct resource res;
108756051948SVladimir Oltean 	int port, i, err;
108856051948SVladimir Oltean 
108956051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
109056051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
109156051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
109256051948SVladimir Oltean 	if (!ocelot->ports)
109356051948SVladimir Oltean 		return -ENOMEM;
109456051948SVladimir Oltean 
109556051948SVladimir Oltean 	ocelot->map		= felix->info->map;
109656051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
109756051948SVladimir Oltean 	ocelot->num_stats	= felix->info->num_stats;
109821ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
109907d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
110056051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
1101cacea62fSVladimir Oltean 	ocelot->npi_inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
1102cacea62fSVladimir Oltean 	ocelot->npi_xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
1103f59fd9caSVladimir Oltean 	ocelot->devlink		= felix->ds->devlink;
110456051948SVladimir Oltean 
1105bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
1106bdeced75SVladimir Oltean 				 GFP_KERNEL);
1107bdeced75SVladimir Oltean 	if (!port_phy_modes)
1108bdeced75SVladimir Oltean 		return -ENOMEM;
1109bdeced75SVladimir Oltean 
1110bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
1111bdeced75SVladimir Oltean 	if (err) {
1112bdeced75SVladimir Oltean 		kfree(port_phy_modes);
1113bdeced75SVladimir Oltean 		return err;
1114bdeced75SVladimir Oltean 	}
1115bdeced75SVladimir Oltean 
111656051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
111756051948SVladimir Oltean 		struct regmap *target;
111856051948SVladimir Oltean 
111956051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
112056051948SVladimir Oltean 			continue;
112156051948SVladimir Oltean 
1122b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
1123b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1124375e1314SVladimir Oltean 		res.start += felix->switch_base;
1125375e1314SVladimir Oltean 		res.end += felix->switch_base;
112656051948SVladimir Oltean 
1127b4024c9eSClaudiu Manoil 		target = ocelot_regmap_init(ocelot, &res);
112856051948SVladimir Oltean 		if (IS_ERR(target)) {
112956051948SVladimir Oltean 			dev_err(ocelot->dev,
113056051948SVladimir Oltean 				"Failed to map device memory space\n");
1131bdeced75SVladimir Oltean 			kfree(port_phy_modes);
113256051948SVladimir Oltean 			return PTR_ERR(target);
113356051948SVladimir Oltean 		}
113456051948SVladimir Oltean 
113556051948SVladimir Oltean 		ocelot->targets[i] = target;
113656051948SVladimir Oltean 	}
113756051948SVladimir Oltean 
113856051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
113956051948SVladimir Oltean 	if (err) {
114056051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
1141bdeced75SVladimir Oltean 		kfree(port_phy_modes);
114256051948SVladimir Oltean 		return err;
114356051948SVladimir Oltean 	}
114456051948SVladimir Oltean 
114556051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
114656051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
114791c724cfSVladimir Oltean 		struct regmap *target;
114856051948SVladimir Oltean 
114956051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
115056051948SVladimir Oltean 					   sizeof(struct ocelot_port),
115156051948SVladimir Oltean 					   GFP_KERNEL);
115256051948SVladimir Oltean 		if (!ocelot_port) {
115356051948SVladimir Oltean 			dev_err(ocelot->dev,
115456051948SVladimir Oltean 				"failed to allocate port memory\n");
1155bdeced75SVladimir Oltean 			kfree(port_phy_modes);
115656051948SVladimir Oltean 			return -ENOMEM;
115756051948SVladimir Oltean 		}
115856051948SVladimir Oltean 
1159b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
1160b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1161375e1314SVladimir Oltean 		res.start += felix->switch_base;
1162375e1314SVladimir Oltean 		res.end += felix->switch_base;
116356051948SVladimir Oltean 
116491c724cfSVladimir Oltean 		target = ocelot_regmap_init(ocelot, &res);
116591c724cfSVladimir Oltean 		if (IS_ERR(target)) {
116656051948SVladimir Oltean 			dev_err(ocelot->dev,
116791c724cfSVladimir Oltean 				"Failed to map memory space for port %d\n",
116891c724cfSVladimir Oltean 				port);
1169bdeced75SVladimir Oltean 			kfree(port_phy_modes);
117091c724cfSVladimir Oltean 			return PTR_ERR(target);
117156051948SVladimir Oltean 		}
117256051948SVladimir Oltean 
1173bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
117456051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
117591c724cfSVladimir Oltean 		ocelot_port->target = target;
117656051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
117756051948SVladimir Oltean 	}
117856051948SVladimir Oltean 
1179bdeced75SVladimir Oltean 	kfree(port_phy_modes);
1180bdeced75SVladimir Oltean 
1181bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
1182bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
1183bdeced75SVladimir Oltean 		if (err < 0)
1184bdeced75SVladimir Oltean 			return err;
1185bdeced75SVladimir Oltean 	}
1186bdeced75SVladimir Oltean 
118756051948SVladimir Oltean 	return 0;
118856051948SVladimir Oltean }
118956051948SVladimir Oltean 
119056051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
119156051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
119256051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
119356051948SVladimir Oltean  * (which will not work).
119456051948SVladimir Oltean  */
119556051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
119656051948SVladimir Oltean {
119756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
119856051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
119956051948SVladimir Oltean 	int port, err;
120056051948SVladimir Oltean 
120156051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
120256051948SVladimir Oltean 	if (err)
120356051948SVladimir Oltean 		return err;
120456051948SVladimir Oltean 
1205d1cc0e93SVladimir Oltean 	err = ocelot_init(ocelot);
1206d1cc0e93SVladimir Oltean 	if (err)
12076b73b7c9SVladimir Oltean 		goto out_mdiobus_free;
1208d1cc0e93SVladimir Oltean 
12092b49d128SYangbo Lu 	if (ocelot->ptp) {
12102ac7c6c5SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
12112b49d128SYangbo Lu 		if (err) {
12122b49d128SYangbo Lu 			dev_err(ocelot->dev,
12132b49d128SYangbo Lu 				"Timestamp initialization failed\n");
12142b49d128SYangbo Lu 			ocelot->ptp = 0;
12152b49d128SYangbo Lu 		}
12162b49d128SYangbo Lu 	}
121756051948SVladimir Oltean 
121856051948SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
1219adb3dccfSVladimir Oltean 		if (dsa_is_unused_port(ds, port))
1220adb3dccfSVladimir Oltean 			continue;
122156051948SVladimir Oltean 
1222adb3dccfSVladimir Oltean 		ocelot_init_port(ocelot, port);
1223bd2b3161SXiaoliang Yang 
1224bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
1225bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
1226bd2b3161SXiaoliang Yang 		 */
1227bd2b3161SXiaoliang Yang 		felix_port_qos_map_init(ocelot, port);
122856051948SVladimir Oltean 	}
122956051948SVladimir Oltean 
1230f59fd9caSVladimir Oltean 	err = ocelot_devlink_sb_register(ocelot);
1231f59fd9caSVladimir Oltean 	if (err)
12326b73b7c9SVladimir Oltean 		goto out_deinit_ports;
1233f59fd9caSVladimir Oltean 
1234adb3dccfSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
1235adb3dccfSVladimir Oltean 		if (!dsa_is_cpu_port(ds, port))
1236adb3dccfSVladimir Oltean 			continue;
1237adb3dccfSVladimir Oltean 
1238adb3dccfSVladimir Oltean 		/* The initial tag protocol is NPI which always returns 0, so
1239adb3dccfSVladimir Oltean 		 * there's no real point in checking for errors.
12401cf3299bSVladimir Oltean 		 */
1241adb3dccfSVladimir Oltean 		felix_set_tag_protocol(ds, port, felix->tag_proto);
1242adb3dccfSVladimir Oltean 	}
12431cf3299bSVladimir Oltean 
12440b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
1245c54913c1SVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
1246bdeced75SVladimir Oltean 
124756051948SVladimir Oltean 	return 0;
12486b73b7c9SVladimir Oltean 
12496b73b7c9SVladimir Oltean out_deinit_ports:
12506b73b7c9SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
12516b73b7c9SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
12526b73b7c9SVladimir Oltean 			continue;
12536b73b7c9SVladimir Oltean 
12546b73b7c9SVladimir Oltean 		ocelot_deinit_port(ocelot, port);
12556b73b7c9SVladimir Oltean 	}
12566b73b7c9SVladimir Oltean 
12576b73b7c9SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
12586b73b7c9SVladimir Oltean 	ocelot_deinit(ocelot);
12596b73b7c9SVladimir Oltean 
12606b73b7c9SVladimir Oltean out_mdiobus_free:
12616b73b7c9SVladimir Oltean 	if (felix->info->mdio_bus_free)
12626b73b7c9SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
12636b73b7c9SVladimir Oltean 
12646b73b7c9SVladimir Oltean 	return err;
126556051948SVladimir Oltean }
126656051948SVladimir Oltean 
126756051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
126856051948SVladimir Oltean {
126956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1270bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1271e5fb512dSVladimir Oltean 	int port;
1272bdeced75SVladimir Oltean 
1273adb3dccfSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
1274adb3dccfSVladimir Oltean 		if (!dsa_is_cpu_port(ds, port))
1275adb3dccfSVladimir Oltean 			continue;
1276adb3dccfSVladimir Oltean 
1277adb3dccfSVladimir Oltean 		felix_del_tag_protocol(ds, port, felix->tag_proto);
1278adb3dccfSVladimir Oltean 	}
1279adb3dccfSVladimir Oltean 
1280f59fd9caSVladimir Oltean 	ocelot_devlink_sb_unregister(ocelot);
1281d19741b0SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
1282d19741b0SVladimir Oltean 	ocelot_deinit(ocelot);
128356051948SVladimir Oltean 
128442b5adbbSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
128542b5adbbSVladimir Oltean 		if (dsa_is_unused_port(ds, port))
128642b5adbbSVladimir Oltean 			continue;
128742b5adbbSVladimir Oltean 
1288e5fb512dSVladimir Oltean 		ocelot_deinit_port(ocelot, port);
128942b5adbbSVladimir Oltean 	}
1290d19741b0SVladimir Oltean 
1291d19741b0SVladimir Oltean 	if (felix->info->mdio_bus_free)
1292d19741b0SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
129356051948SVladimir Oltean }
129456051948SVladimir Oltean 
1295c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
1296c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1297c0bcf537SYangbo Lu {
1298c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1299c0bcf537SYangbo Lu 
1300c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
1301c0bcf537SYangbo Lu }
1302c0bcf537SYangbo Lu 
1303c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
1304c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1305c0bcf537SYangbo Lu {
1306c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1307c0bcf537SYangbo Lu 
1308c0bcf537SYangbo Lu 	return ocelot_hwstamp_set(ocelot, port, ifr);
1309c0bcf537SYangbo Lu }
1310c0bcf537SYangbo Lu 
13110a6f17c6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot, unsigned int ptp_type)
13120a6f17c6SVladimir Oltean {
13130a6f17c6SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
13140a6f17c6SVladimir Oltean 	int err, grp = 0;
13150a6f17c6SVladimir Oltean 
13160a6f17c6SVladimir Oltean 	if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q)
13170a6f17c6SVladimir Oltean 		return false;
13180a6f17c6SVladimir Oltean 
13190a6f17c6SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
13200a6f17c6SVladimir Oltean 		return false;
13210a6f17c6SVladimir Oltean 
13220a6f17c6SVladimir Oltean 	if (ptp_type == PTP_CLASS_NONE)
13230a6f17c6SVladimir Oltean 		return false;
13240a6f17c6SVladimir Oltean 
13250a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
13260a6f17c6SVladimir Oltean 		struct sk_buff *skb;
13270a6f17c6SVladimir Oltean 		unsigned int type;
13280a6f17c6SVladimir Oltean 
13290a6f17c6SVladimir Oltean 		err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
13300a6f17c6SVladimir Oltean 		if (err)
13310a6f17c6SVladimir Oltean 			goto out;
13320a6f17c6SVladimir Oltean 
13330a6f17c6SVladimir Oltean 		/* We trap to the CPU port module all PTP frames, but
13340a6f17c6SVladimir Oltean 		 * felix_rxtstamp() only gets called for event frames.
13350a6f17c6SVladimir Oltean 		 * So we need to avoid sending duplicate general
13360a6f17c6SVladimir Oltean 		 * message frames by running a second BPF classifier
13370a6f17c6SVladimir Oltean 		 * here and dropping those.
13380a6f17c6SVladimir Oltean 		 */
13390a6f17c6SVladimir Oltean 		__skb_push(skb, ETH_HLEN);
13400a6f17c6SVladimir Oltean 
13410a6f17c6SVladimir Oltean 		type = ptp_classify_raw(skb);
13420a6f17c6SVladimir Oltean 
13430a6f17c6SVladimir Oltean 		__skb_pull(skb, ETH_HLEN);
13440a6f17c6SVladimir Oltean 
13450a6f17c6SVladimir Oltean 		if (type == PTP_CLASS_NONE) {
13460a6f17c6SVladimir Oltean 			kfree_skb(skb);
13470a6f17c6SVladimir Oltean 			continue;
13480a6f17c6SVladimir Oltean 		}
13490a6f17c6SVladimir Oltean 
13500a6f17c6SVladimir Oltean 		netif_rx(skb);
13510a6f17c6SVladimir Oltean 	}
13520a6f17c6SVladimir Oltean 
13530a6f17c6SVladimir Oltean out:
13540a6f17c6SVladimir Oltean 	if (err < 0)
13550a6f17c6SVladimir Oltean 		ocelot_drain_cpu_queue(ocelot, 0);
13560a6f17c6SVladimir Oltean 
13570a6f17c6SVladimir Oltean 	return true;
13580a6f17c6SVladimir Oltean }
13590a6f17c6SVladimir Oltean 
1360c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
1361c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
1362c0bcf537SYangbo Lu {
136340d3f295SVladimir Oltean 	u8 *extraction = skb->data - ETH_HLEN - OCELOT_TAG_LEN;
1364c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
1365c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1366c0bcf537SYangbo Lu 	u32 tstamp_lo, tstamp_hi;
1367c0bcf537SYangbo Lu 	struct timespec64 ts;
1368c0bcf537SYangbo Lu 	u64 tstamp, val;
1369c0bcf537SYangbo Lu 
13700a6f17c6SVladimir Oltean 	/* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb
13710a6f17c6SVladimir Oltean 	 * for RX timestamping. Then free it, and poll for its copy through
13720a6f17c6SVladimir Oltean 	 * MMIO in the CPU port module, and inject that into the stack from
13730a6f17c6SVladimir Oltean 	 * ocelot_xtr_poll().
13740a6f17c6SVladimir Oltean 	 */
13750a6f17c6SVladimir Oltean 	if (felix_check_xtr_pkt(ocelot, type)) {
13760a6f17c6SVladimir Oltean 		kfree_skb(skb);
13770a6f17c6SVladimir Oltean 		return true;
13780a6f17c6SVladimir Oltean 	}
13790a6f17c6SVladimir Oltean 
1380c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1381c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1382c0bcf537SYangbo Lu 
138340d3f295SVladimir Oltean 	ocelot_xfh_get_rew_val(extraction, &val);
1384c0bcf537SYangbo Lu 	tstamp_lo = (u32)val;
1385c0bcf537SYangbo Lu 
1386c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
1387c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
1388c0bcf537SYangbo Lu 		tstamp_hi--;
1389c0bcf537SYangbo Lu 
1390c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
1391c0bcf537SYangbo Lu 
1392c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
1393c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1394c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
1395c0bcf537SYangbo Lu 	return false;
1396c0bcf537SYangbo Lu }
1397c0bcf537SYangbo Lu 
1398*5c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port,
1399*5c5416f5SYangbo Lu 			   struct sk_buff *skb)
1400c0bcf537SYangbo Lu {
1401c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1402c0bcf537SYangbo Lu 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1403*5c5416f5SYangbo Lu 	struct sk_buff *clone;
1404c0bcf537SYangbo Lu 
1405cfd12c06SYangbo Lu 	if (ocelot->ptp && ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
1406*5c5416f5SYangbo Lu 		clone = skb_clone_sk(skb);
1407*5c5416f5SYangbo Lu 		if (!clone)
1408*5c5416f5SYangbo Lu 			return;
1409c0bcf537SYangbo Lu 
1410*5c5416f5SYangbo Lu 		ocelot_port_add_txtstamp_skb(ocelot, port, clone);
1411*5c5416f5SYangbo Lu 		DSA_SKB_CB(skb)->clone = clone;
1412*5c5416f5SYangbo Lu 	}
1413c0bcf537SYangbo Lu }
1414c0bcf537SYangbo Lu 
14150b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
14160b912fc9SVladimir Oltean {
14170b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
14180b912fc9SVladimir Oltean 
14190b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
14200b912fc9SVladimir Oltean 
14210b912fc9SVladimir Oltean 	return 0;
14220b912fc9SVladimir Oltean }
14230b912fc9SVladimir Oltean 
14240b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
14250b912fc9SVladimir Oltean {
14260b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
14270b912fc9SVladimir Oltean 
14280b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
14290b912fc9SVladimir Oltean }
14300b912fc9SVladimir Oltean 
143107d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
143207d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
143307d985eeSVladimir Oltean {
143407d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
143507d985eeSVladimir Oltean 
143607d985eeSVladimir Oltean 	return ocelot_cls_flower_replace(ocelot, port, cls, ingress);
143707d985eeSVladimir Oltean }
143807d985eeSVladimir Oltean 
143907d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
144007d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
144107d985eeSVladimir Oltean {
144207d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
144307d985eeSVladimir Oltean 
144407d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
144507d985eeSVladimir Oltean }
144607d985eeSVladimir Oltean 
144707d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
144807d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
144907d985eeSVladimir Oltean {
145007d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
145107d985eeSVladimir Oltean 
145207d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
145307d985eeSVladimir Oltean }
145407d985eeSVladimir Oltean 
1455fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
1456fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
1457fc411eaaSVladimir Oltean {
1458fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1459fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
1460fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
14615f035af7SPo Liu 		.burst = policer->burst,
1462fc411eaaSVladimir Oltean 	};
1463fc411eaaSVladimir Oltean 
1464fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
1465fc411eaaSVladimir Oltean }
1466fc411eaaSVladimir Oltean 
1467fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
1468fc411eaaSVladimir Oltean {
1469fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1470fc411eaaSVladimir Oltean 
1471fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
1472fc411eaaSVladimir Oltean }
1473fc411eaaSVladimir Oltean 
1474de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
1475de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
1476de143c0eSXiaoliang Yang 			       void *type_data)
1477de143c0eSXiaoliang Yang {
1478de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
1479de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
1480de143c0eSXiaoliang Yang 
1481de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
1482de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
1483de143c0eSXiaoliang Yang 	else
1484de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
1485de143c0eSXiaoliang Yang }
1486de143c0eSXiaoliang Yang 
1487f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
1488f59fd9caSVladimir Oltean 			     u16 pool_index,
1489f59fd9caSVladimir Oltean 			     struct devlink_sb_pool_info *pool_info)
1490f59fd9caSVladimir Oltean {
1491f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1492f59fd9caSVladimir Oltean 
1493f59fd9caSVladimir Oltean 	return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
1494f59fd9caSVladimir Oltean }
1495f59fd9caSVladimir Oltean 
1496f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
1497f59fd9caSVladimir Oltean 			     u16 pool_index, u32 size,
1498f59fd9caSVladimir Oltean 			     enum devlink_sb_threshold_type threshold_type,
1499f59fd9caSVladimir Oltean 			     struct netlink_ext_ack *extack)
1500f59fd9caSVladimir Oltean {
1501f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1502f59fd9caSVladimir Oltean 
1503f59fd9caSVladimir Oltean 	return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
1504f59fd9caSVladimir Oltean 				  threshold_type, extack);
1505f59fd9caSVladimir Oltean }
1506f59fd9caSVladimir Oltean 
1507f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
1508f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1509f59fd9caSVladimir Oltean 				  u32 *p_threshold)
1510f59fd9caSVladimir Oltean {
1511f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1512f59fd9caSVladimir Oltean 
1513f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
1514f59fd9caSVladimir Oltean 				       p_threshold);
1515f59fd9caSVladimir Oltean }
1516f59fd9caSVladimir Oltean 
1517f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
1518f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1519f59fd9caSVladimir Oltean 				  u32 threshold, struct netlink_ext_ack *extack)
1520f59fd9caSVladimir Oltean {
1521f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1522f59fd9caSVladimir Oltean 
1523f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
1524f59fd9caSVladimir Oltean 				       threshold, extack);
1525f59fd9caSVladimir Oltean }
1526f59fd9caSVladimir Oltean 
1527f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
1528f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1529f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1530f59fd9caSVladimir Oltean 				     u16 *p_pool_index, u32 *p_threshold)
1531f59fd9caSVladimir Oltean {
1532f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1533f59fd9caSVladimir Oltean 
1534f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
1535f59fd9caSVladimir Oltean 					  pool_type, p_pool_index,
1536f59fd9caSVladimir Oltean 					  p_threshold);
1537f59fd9caSVladimir Oltean }
1538f59fd9caSVladimir Oltean 
1539f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
1540f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1541f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1542f59fd9caSVladimir Oltean 				     u16 pool_index, u32 threshold,
1543f59fd9caSVladimir Oltean 				     struct netlink_ext_ack *extack)
1544f59fd9caSVladimir Oltean {
1545f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1546f59fd9caSVladimir Oltean 
1547f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
1548f59fd9caSVladimir Oltean 					  pool_type, pool_index, threshold,
1549f59fd9caSVladimir Oltean 					  extack);
1550f59fd9caSVladimir Oltean }
1551f59fd9caSVladimir Oltean 
1552f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds,
1553f59fd9caSVladimir Oltean 				 unsigned int sb_index)
1554f59fd9caSVladimir Oltean {
1555f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1556f59fd9caSVladimir Oltean 
1557f59fd9caSVladimir Oltean 	return ocelot_sb_occ_snapshot(ocelot, sb_index);
1558f59fd9caSVladimir Oltean }
1559f59fd9caSVladimir Oltean 
1560f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds,
1561f59fd9caSVladimir Oltean 				  unsigned int sb_index)
1562f59fd9caSVladimir Oltean {
1563f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1564f59fd9caSVladimir Oltean 
1565f59fd9caSVladimir Oltean 	return ocelot_sb_occ_max_clear(ocelot, sb_index);
1566f59fd9caSVladimir Oltean }
1567f59fd9caSVladimir Oltean 
1568f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
1569f59fd9caSVladimir Oltean 				      unsigned int sb_index, u16 pool_index,
1570f59fd9caSVladimir Oltean 				      u32 *p_cur, u32 *p_max)
1571f59fd9caSVladimir Oltean {
1572f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1573f59fd9caSVladimir Oltean 
1574f59fd9caSVladimir Oltean 	return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
1575f59fd9caSVladimir Oltean 					   p_cur, p_max);
1576f59fd9caSVladimir Oltean }
1577f59fd9caSVladimir Oltean 
1578f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
1579f59fd9caSVladimir Oltean 					 unsigned int sb_index, u16 tc_index,
1580f59fd9caSVladimir Oltean 					 enum devlink_sb_pool_type pool_type,
1581f59fd9caSVladimir Oltean 					 u32 *p_cur, u32 *p_max)
1582f59fd9caSVladimir Oltean {
1583f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1584f59fd9caSVladimir Oltean 
1585f59fd9caSVladimir Oltean 	return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
1586f59fd9caSVladimir Oltean 					      pool_type, p_cur, p_max);
1587f59fd9caSVladimir Oltean }
1588f59fd9caSVladimir Oltean 
1589a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port,
1590a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1591a026c50bSHoratiu Vultur {
1592a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1593a026c50bSHoratiu Vultur 
1594a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1595a026c50bSHoratiu Vultur }
1596a026c50bSHoratiu Vultur 
1597a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port,
1598a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1599a026c50bSHoratiu Vultur {
1600a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1601a026c50bSHoratiu Vultur 
1602a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1603a026c50bSHoratiu Vultur }
1604a026c50bSHoratiu Vultur 
1605a026c50bSHoratiu Vultur static int
1606a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port,
1607a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1608a026c50bSHoratiu Vultur {
1609a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1610a026c50bSHoratiu Vultur 
1611a026c50bSHoratiu Vultur 	return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1612a026c50bSHoratiu Vultur }
1613a026c50bSHoratiu Vultur 
1614a026c50bSHoratiu Vultur static int
1615a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port,
1616a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1617a026c50bSHoratiu Vultur {
1618a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1619a026c50bSHoratiu Vultur 
1620a026c50bSHoratiu Vultur 	return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1621a026c50bSHoratiu Vultur }
1622a026c50bSHoratiu Vultur 
1623375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = {
162456051948SVladimir Oltean 	.get_tag_protocol		= felix_get_tag_protocol,
1625adb3dccfSVladimir Oltean 	.change_tag_protocol		= felix_change_tag_protocol,
162656051948SVladimir Oltean 	.setup				= felix_setup,
162756051948SVladimir Oltean 	.teardown			= felix_teardown,
162856051948SVladimir Oltean 	.set_ageing_time		= felix_set_ageing_time,
162956051948SVladimir Oltean 	.get_strings			= felix_get_strings,
163056051948SVladimir Oltean 	.get_ethtool_stats		= felix_get_ethtool_stats,
163156051948SVladimir Oltean 	.get_sset_count			= felix_get_sset_count,
163256051948SVladimir Oltean 	.get_ts_info			= felix_get_ts_info,
1633bdeced75SVladimir Oltean 	.phylink_validate		= felix_phylink_validate,
1634bdeced75SVladimir Oltean 	.phylink_mac_config		= felix_phylink_mac_config,
1635bdeced75SVladimir Oltean 	.phylink_mac_link_down		= felix_phylink_mac_link_down,
1636bdeced75SVladimir Oltean 	.phylink_mac_link_up		= felix_phylink_mac_link_up,
163756051948SVladimir Oltean 	.port_enable			= felix_port_enable,
163856051948SVladimir Oltean 	.port_disable			= felix_port_disable,
163956051948SVladimir Oltean 	.port_fdb_dump			= felix_fdb_dump,
164056051948SVladimir Oltean 	.port_fdb_add			= felix_fdb_add,
164156051948SVladimir Oltean 	.port_fdb_del			= felix_fdb_del,
1642209edf95SVladimir Oltean 	.port_mdb_add			= felix_mdb_add,
1643209edf95SVladimir Oltean 	.port_mdb_del			= felix_mdb_del,
1644421741eaSVladimir Oltean 	.port_pre_bridge_flags		= felix_pre_bridge_flags,
1645421741eaSVladimir Oltean 	.port_bridge_flags		= felix_bridge_flags,
164656051948SVladimir Oltean 	.port_bridge_join		= felix_bridge_join,
164756051948SVladimir Oltean 	.port_bridge_leave		= felix_bridge_leave,
16488fe6832eSVladimir Oltean 	.port_lag_join			= felix_lag_join,
16498fe6832eSVladimir Oltean 	.port_lag_leave			= felix_lag_leave,
16508fe6832eSVladimir Oltean 	.port_lag_change		= felix_lag_change,
165156051948SVladimir Oltean 	.port_stp_state_set		= felix_bridge_stp_state_set,
165256051948SVladimir Oltean 	.port_vlan_filtering		= felix_vlan_filtering,
165356051948SVladimir Oltean 	.port_vlan_add			= felix_vlan_add,
165456051948SVladimir Oltean 	.port_vlan_del			= felix_vlan_del,
1655c0bcf537SYangbo Lu 	.port_hwtstamp_get		= felix_hwtstamp_get,
1656c0bcf537SYangbo Lu 	.port_hwtstamp_set		= felix_hwtstamp_set,
1657c0bcf537SYangbo Lu 	.port_rxtstamp			= felix_rxtstamp,
1658c0bcf537SYangbo Lu 	.port_txtstamp			= felix_txtstamp,
16590b912fc9SVladimir Oltean 	.port_change_mtu		= felix_change_mtu,
16600b912fc9SVladimir Oltean 	.port_max_mtu			= felix_get_max_mtu,
1661fc411eaaSVladimir Oltean 	.port_policer_add		= felix_port_policer_add,
1662fc411eaaSVladimir Oltean 	.port_policer_del		= felix_port_policer_del,
166307d985eeSVladimir Oltean 	.cls_flower_add			= felix_cls_flower_add,
166407d985eeSVladimir Oltean 	.cls_flower_del			= felix_cls_flower_del,
166507d985eeSVladimir Oltean 	.cls_flower_stats		= felix_cls_flower_stats,
1666de143c0eSXiaoliang Yang 	.port_setup_tc			= felix_port_setup_tc,
1667f59fd9caSVladimir Oltean 	.devlink_sb_pool_get		= felix_sb_pool_get,
1668f59fd9caSVladimir Oltean 	.devlink_sb_pool_set		= felix_sb_pool_set,
1669f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_get	= felix_sb_port_pool_get,
1670f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_set	= felix_sb_port_pool_set,
1671f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_get	= felix_sb_tc_pool_bind_get,
1672f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_set	= felix_sb_tc_pool_bind_set,
1673f59fd9caSVladimir Oltean 	.devlink_sb_occ_snapshot	= felix_sb_occ_snapshot,
1674f59fd9caSVladimir Oltean 	.devlink_sb_occ_max_clear	= felix_sb_occ_max_clear,
1675f59fd9caSVladimir Oltean 	.devlink_sb_occ_port_pool_get	= felix_sb_occ_port_pool_get,
1676f59fd9caSVladimir Oltean 	.devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
1677a026c50bSHoratiu Vultur 	.port_mrp_add			= felix_mrp_add,
1678a026c50bSHoratiu Vultur 	.port_mrp_del			= felix_mrp_del,
1679a026c50bSHoratiu Vultur 	.port_mrp_add_ring_role		= felix_mrp_add_ring_role,
1680a026c50bSHoratiu Vultur 	.port_mrp_del_ring_role		= felix_mrp_del_ring_role,
168156051948SVladimir Oltean };
1682319e4dd1SVladimir Oltean 
1683319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
1684319e4dd1SVladimir Oltean {
1685319e4dd1SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1686319e4dd1SVladimir Oltean 	struct dsa_switch *ds = felix->ds;
1687319e4dd1SVladimir Oltean 
1688319e4dd1SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
1689319e4dd1SVladimir Oltean 		return NULL;
1690319e4dd1SVladimir Oltean 
1691319e4dd1SVladimir Oltean 	return dsa_to_port(ds, port)->slave;
1692319e4dd1SVladimir Oltean }
1693319e4dd1SVladimir Oltean 
1694319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev)
1695319e4dd1SVladimir Oltean {
1696319e4dd1SVladimir Oltean 	struct dsa_port *dp;
1697319e4dd1SVladimir Oltean 
1698319e4dd1SVladimir Oltean 	dp = dsa_port_from_netdev(dev);
1699319e4dd1SVladimir Oltean 	if (IS_ERR(dp))
1700319e4dd1SVladimir Oltean 		return -EINVAL;
1701319e4dd1SVladimir Oltean 
1702319e4dd1SVladimir Oltean 	return dp->index;
1703319e4dd1SVladimir Oltean }
1704