xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision 3b95d1b29386dd4328e471fda2eee74b7fd4a4b3)
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 /* Alternatively to using the NPI functionality, that same hardware MAC
235e21268efSVladimir Oltean  * connected internally to the enetc or fman DSA master can be configured to
236e21268efSVladimir Oltean  * use the software-defined tag_8021q frame format. As far as the hardware is
237e21268efSVladimir Oltean  * concerned, it thinks it is a "dumb switch" - the queues of the CPU port
238e21268efSVladimir Oltean  * module are now disconnected from it, but can still be accessed through
239e21268efSVladimir Oltean  * register-based MMIO.
240e21268efSVladimir Oltean  */
241e21268efSVladimir Oltean static void felix_8021q_cpu_port_init(struct ocelot *ocelot, int port)
242e21268efSVladimir Oltean {
243e21268efSVladimir Oltean 	ocelot->ports[port]->is_dsa_8021q_cpu = true;
244e21268efSVladimir Oltean 	ocelot->npi = -1;
245e21268efSVladimir Oltean 
246e21268efSVladimir Oltean 	/* Overwrite PGID_CPU with the non-tagging port */
247e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, PGID_CPU);
248e21268efSVladimir Oltean 
249e21268efSVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
250e21268efSVladimir Oltean }
251e21268efSVladimir Oltean 
252e21268efSVladimir Oltean static void felix_8021q_cpu_port_deinit(struct ocelot *ocelot, int port)
253e21268efSVladimir Oltean {
254e21268efSVladimir Oltean 	ocelot->ports[port]->is_dsa_8021q_cpu = false;
255e21268efSVladimir Oltean 
256e21268efSVladimir Oltean 	/* Restore PGID_CPU */
257e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(ocelot->num_phys_ports), ANA_PGID_PGID,
258e21268efSVladimir Oltean 			 PGID_CPU);
259e21268efSVladimir Oltean 
260e21268efSVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
261e21268efSVladimir Oltean }
262e21268efSVladimir Oltean 
263c8c0ba4fSVladimir Oltean /* Set up a VCAP IS2 rule for delivering PTP frames to the CPU port module.
264c8c0ba4fSVladimir Oltean  * If the quirk_no_xtr_irq is in place, then also copy those PTP frames to the
265c8c0ba4fSVladimir Oltean  * tag_8021q CPU port.
266c8c0ba4fSVladimir Oltean  */
267c8c0ba4fSVladimir Oltean static int felix_setup_mmio_filtering(struct felix *felix)
268c8c0ba4fSVladimir Oltean {
269c8c0ba4fSVladimir Oltean 	unsigned long user_ports = 0, cpu_ports = 0;
270c8c0ba4fSVladimir Oltean 	struct ocelot_vcap_filter *redirect_rule;
271c8c0ba4fSVladimir Oltean 	struct ocelot_vcap_filter *tagging_rule;
272c8c0ba4fSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
273c8c0ba4fSVladimir Oltean 	struct dsa_switch *ds = felix->ds;
274c8c0ba4fSVladimir Oltean 	int port, ret;
275c8c0ba4fSVladimir Oltean 
276c8c0ba4fSVladimir Oltean 	tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
277c8c0ba4fSVladimir Oltean 	if (!tagging_rule)
278c8c0ba4fSVladimir Oltean 		return -ENOMEM;
279c8c0ba4fSVladimir Oltean 
280c8c0ba4fSVladimir Oltean 	redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
281c8c0ba4fSVladimir Oltean 	if (!redirect_rule) {
282c8c0ba4fSVladimir Oltean 		kfree(tagging_rule);
283c8c0ba4fSVladimir Oltean 		return -ENOMEM;
284c8c0ba4fSVladimir Oltean 	}
285c8c0ba4fSVladimir Oltean 
286c8c0ba4fSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
287c8c0ba4fSVladimir Oltean 		if (dsa_is_user_port(ds, port))
288c8c0ba4fSVladimir Oltean 			user_ports |= BIT(port);
289c8c0ba4fSVladimir Oltean 		if (dsa_is_cpu_port(ds, port))
290c8c0ba4fSVladimir Oltean 			cpu_ports |= BIT(port);
291c8c0ba4fSVladimir Oltean 	}
292c8c0ba4fSVladimir Oltean 
293c8c0ba4fSVladimir Oltean 	tagging_rule->key_type = OCELOT_VCAP_KEY_ETYPE;
294c8c0ba4fSVladimir Oltean 	*(__be16 *)tagging_rule->key.etype.etype.value = htons(ETH_P_1588);
295c8c0ba4fSVladimir Oltean 	*(__be16 *)tagging_rule->key.etype.etype.mask = htons(0xffff);
296c8c0ba4fSVladimir Oltean 	tagging_rule->ingress_port_mask = user_ports;
297c8c0ba4fSVladimir Oltean 	tagging_rule->prio = 1;
298c8c0ba4fSVladimir Oltean 	tagging_rule->id.cookie = ocelot->num_phys_ports;
299c8c0ba4fSVladimir Oltean 	tagging_rule->id.tc_offload = false;
300c8c0ba4fSVladimir Oltean 	tagging_rule->block_id = VCAP_IS1;
301c8c0ba4fSVladimir Oltean 	tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
302c8c0ba4fSVladimir Oltean 	tagging_rule->lookup = 0;
303c8c0ba4fSVladimir Oltean 	tagging_rule->action.pag_override_mask = 0xff;
304c8c0ba4fSVladimir Oltean 	tagging_rule->action.pag_val = ocelot->num_phys_ports;
305c8c0ba4fSVladimir Oltean 
306c8c0ba4fSVladimir Oltean 	ret = ocelot_vcap_filter_add(ocelot, tagging_rule, NULL);
307c8c0ba4fSVladimir Oltean 	if (ret) {
308c8c0ba4fSVladimir Oltean 		kfree(tagging_rule);
309c8c0ba4fSVladimir Oltean 		kfree(redirect_rule);
310c8c0ba4fSVladimir Oltean 		return ret;
311c8c0ba4fSVladimir Oltean 	}
312c8c0ba4fSVladimir Oltean 
313c8c0ba4fSVladimir Oltean 	redirect_rule->key_type = OCELOT_VCAP_KEY_ANY;
314c8c0ba4fSVladimir Oltean 	redirect_rule->ingress_port_mask = user_ports;
315c8c0ba4fSVladimir Oltean 	redirect_rule->pag = ocelot->num_phys_ports;
316c8c0ba4fSVladimir Oltean 	redirect_rule->prio = 1;
317c8c0ba4fSVladimir Oltean 	redirect_rule->id.cookie = ocelot->num_phys_ports;
318c8c0ba4fSVladimir Oltean 	redirect_rule->id.tc_offload = false;
319c8c0ba4fSVladimir Oltean 	redirect_rule->block_id = VCAP_IS2;
320c8c0ba4fSVladimir Oltean 	redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
321c8c0ba4fSVladimir Oltean 	redirect_rule->lookup = 0;
322c8c0ba4fSVladimir Oltean 	redirect_rule->action.cpu_copy_ena = true;
323c8c0ba4fSVladimir Oltean 	if (felix->info->quirk_no_xtr_irq) {
324c8c0ba4fSVladimir Oltean 		/* Redirect to the tag_8021q CPU but also copy PTP packets to
325c8c0ba4fSVladimir Oltean 		 * the CPU port module
326c8c0ba4fSVladimir Oltean 		 */
327c8c0ba4fSVladimir Oltean 		redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT;
328c8c0ba4fSVladimir Oltean 		redirect_rule->action.port_mask = cpu_ports;
329c8c0ba4fSVladimir Oltean 	} else {
330c8c0ba4fSVladimir Oltean 		/* Trap PTP packets only to the CPU port module (which is
331c8c0ba4fSVladimir Oltean 		 * redirected to the NPI port)
332c8c0ba4fSVladimir Oltean 		 */
333c8c0ba4fSVladimir Oltean 		redirect_rule->action.mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
334c8c0ba4fSVladimir Oltean 		redirect_rule->action.port_mask = 0;
335c8c0ba4fSVladimir Oltean 	}
336c8c0ba4fSVladimir Oltean 
337c8c0ba4fSVladimir Oltean 	ret = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL);
338c8c0ba4fSVladimir Oltean 	if (ret) {
339c8c0ba4fSVladimir Oltean 		ocelot_vcap_filter_del(ocelot, tagging_rule);
340c8c0ba4fSVladimir Oltean 		kfree(redirect_rule);
341c8c0ba4fSVladimir Oltean 		return ret;
342c8c0ba4fSVladimir Oltean 	}
343c8c0ba4fSVladimir Oltean 
3440a6f17c6SVladimir Oltean 	/* The ownership of the CPU port module's queues might have just been
3450a6f17c6SVladimir Oltean 	 * transferred to the tag_8021q tagger from the NPI-based tagger.
3460a6f17c6SVladimir Oltean 	 * So there might still be all sorts of crap in the queues. On the
3470a6f17c6SVladimir Oltean 	 * other hand, the MMIO-based matching of PTP frames is very brittle,
3480a6f17c6SVladimir Oltean 	 * so we need to be careful that there are no extra frames to be
3490a6f17c6SVladimir Oltean 	 * dequeued over MMIO, since we would never know to discard them.
3500a6f17c6SVladimir Oltean 	 */
3510a6f17c6SVladimir Oltean 	ocelot_drain_cpu_queue(ocelot, 0);
3520a6f17c6SVladimir Oltean 
353c8c0ba4fSVladimir Oltean 	return 0;
354c8c0ba4fSVladimir Oltean }
355c8c0ba4fSVladimir Oltean 
356c8c0ba4fSVladimir Oltean static int felix_teardown_mmio_filtering(struct felix *felix)
357c8c0ba4fSVladimir Oltean {
358c8c0ba4fSVladimir Oltean 	struct ocelot_vcap_filter *tagging_rule, *redirect_rule;
359c8c0ba4fSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is1;
360c8c0ba4fSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
361c8c0ba4fSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
362c8c0ba4fSVladimir Oltean 	int err;
363c8c0ba4fSVladimir Oltean 
364c8c0ba4fSVladimir Oltean 	block_vcap_is1 = &ocelot->block[VCAP_IS1];
365c8c0ba4fSVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
366c8c0ba4fSVladimir Oltean 
367c8c0ba4fSVladimir Oltean 	tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1,
368c8c0ba4fSVladimir Oltean 							   ocelot->num_phys_ports,
369c8c0ba4fSVladimir Oltean 							   false);
370c8c0ba4fSVladimir Oltean 	if (!tagging_rule)
371c8c0ba4fSVladimir Oltean 		return -ENOENT;
372c8c0ba4fSVladimir Oltean 
373c8c0ba4fSVladimir Oltean 	err = ocelot_vcap_filter_del(ocelot, tagging_rule);
374c8c0ba4fSVladimir Oltean 	if (err)
375c8c0ba4fSVladimir Oltean 		return err;
376c8c0ba4fSVladimir Oltean 
377c8c0ba4fSVladimir Oltean 	redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2,
378c8c0ba4fSVladimir Oltean 							    ocelot->num_phys_ports,
379c8c0ba4fSVladimir Oltean 							    false);
380c8c0ba4fSVladimir Oltean 	if (!redirect_rule)
381c8c0ba4fSVladimir Oltean 		return -ENOENT;
382c8c0ba4fSVladimir Oltean 
383c8c0ba4fSVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, redirect_rule);
384c8c0ba4fSVladimir Oltean }
385c8c0ba4fSVladimir Oltean 
386e21268efSVladimir Oltean static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu)
387e21268efSVladimir Oltean {
388e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
389e21268efSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
390e21268efSVladimir Oltean 	unsigned long cpu_flood;
391e21268efSVladimir Oltean 	int port, err;
392e21268efSVladimir Oltean 
393e21268efSVladimir Oltean 	felix_8021q_cpu_port_init(ocelot, cpu);
394e21268efSVladimir Oltean 
395e21268efSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
396e21268efSVladimir Oltean 		if (dsa_is_unused_port(ds, port))
397e21268efSVladimir Oltean 			continue;
398e21268efSVladimir Oltean 
399e21268efSVladimir Oltean 		/* This overwrites ocelot_init():
400e21268efSVladimir Oltean 		 * Do not forward BPDU frames to the CPU port module,
401e21268efSVladimir Oltean 		 * for 2 reasons:
402e21268efSVladimir Oltean 		 * - When these packets are injected from the tag_8021q
403e21268efSVladimir Oltean 		 *   CPU port, we want them to go out, not loop back
404e21268efSVladimir Oltean 		 *   into the system.
405e21268efSVladimir Oltean 		 * - STP traffic ingressing on a user port should go to
406e21268efSVladimir Oltean 		 *   the tag_8021q CPU port, not to the hardware CPU
407e21268efSVladimir Oltean 		 *   port module.
408e21268efSVladimir Oltean 		 */
409e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
410e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0),
411e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG, port);
412e21268efSVladimir Oltean 	}
413e21268efSVladimir Oltean 
414c8c0ba4fSVladimir Oltean 	/* In tag_8021q mode, the CPU port module is unused, except for PTP
415c8c0ba4fSVladimir Oltean 	 * frames. So we want to disable flooding of any kind to the CPU port
416c8c0ba4fSVladimir Oltean 	 * module, since packets going there will end in a black hole.
417e21268efSVladimir Oltean 	 */
418e21268efSVladimir Oltean 	cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
419e21268efSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_UC);
420e21268efSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_MC);
421b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_BC);
422e21268efSVladimir Oltean 
4235da11eb4SVladimir Oltean 	err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD));
424e21268efSVladimir Oltean 	if (err)
425d7b1fd52SVladimir Oltean 		return err;
426d7b1fd52SVladimir Oltean 
427328621f6SVladimir Oltean 	err = felix_setup_mmio_filtering(felix);
428d7b1fd52SVladimir Oltean 	if (err)
429d7b1fd52SVladimir Oltean 		goto out_tag_8021q_unregister;
430e21268efSVladimir Oltean 
431e21268efSVladimir Oltean 	return 0;
432e21268efSVladimir Oltean 
433d7b1fd52SVladimir Oltean out_tag_8021q_unregister:
434d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
435e21268efSVladimir Oltean 	return err;
436e21268efSVladimir Oltean }
437e21268efSVladimir Oltean 
438e21268efSVladimir Oltean static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu)
439e21268efSVladimir Oltean {
440e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
441e21268efSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
442e21268efSVladimir Oltean 	int err, port;
443e21268efSVladimir Oltean 
444c8c0ba4fSVladimir Oltean 	err = felix_teardown_mmio_filtering(felix);
445c8c0ba4fSVladimir Oltean 	if (err)
446c8c0ba4fSVladimir Oltean 		dev_err(ds->dev, "felix_teardown_mmio_filtering returned %d",
447c8c0ba4fSVladimir Oltean 			err);
448c8c0ba4fSVladimir Oltean 
449d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
450e21268efSVladimir Oltean 
451e21268efSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
452e21268efSVladimir Oltean 		if (dsa_is_unused_port(ds, port))
453e21268efSVladimir Oltean 			continue;
454e21268efSVladimir Oltean 
455e21268efSVladimir Oltean 		/* Restore the logic from ocelot_init:
456e21268efSVladimir Oltean 		 * do not forward BPDU frames to the front ports.
457e21268efSVladimir Oltean 		 */
458e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
459e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
460e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG,
461e21268efSVladimir Oltean 				 port);
462e21268efSVladimir Oltean 	}
463e21268efSVladimir Oltean 
464e21268efSVladimir Oltean 	felix_8021q_cpu_port_deinit(ocelot, cpu);
465e21268efSVladimir Oltean }
466e21268efSVladimir Oltean 
467adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This
468adb3dccfSVladimir Oltean  * is the mode through which frames can be injected from and extracted to an
469adb3dccfSVladimir Oltean  * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU
470adb3dccfSVladimir Oltean  * running Linux, and this forms a DSA setup together with the enetc or fman
471adb3dccfSVladimir Oltean  * DSA master.
472adb3dccfSVladimir Oltean  */
473adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port)
474adb3dccfSVladimir Oltean {
475adb3dccfSVladimir Oltean 	ocelot->npi = port;
476adb3dccfSVladimir Oltean 
477adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
478adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
479adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
480adb3dccfSVladimir Oltean 
481adb3dccfSVladimir Oltean 	/* NPI port Injection/Extraction configuration */
482adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
483adb3dccfSVladimir Oltean 			    ocelot->npi_xtr_prefix);
484adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
485adb3dccfSVladimir Oltean 			    ocelot->npi_inj_prefix);
486adb3dccfSVladimir Oltean 
487adb3dccfSVladimir Oltean 	/* Disable transmission of pause frames */
488adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
489adb3dccfSVladimir Oltean }
490adb3dccfSVladimir Oltean 
491adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port)
492adb3dccfSVladimir Oltean {
493adb3dccfSVladimir Oltean 	/* Restore hardware defaults */
494adb3dccfSVladimir Oltean 	int unused_port = ocelot->num_phys_ports + 2;
495adb3dccfSVladimir Oltean 
496adb3dccfSVladimir Oltean 	ocelot->npi = -1;
497adb3dccfSVladimir Oltean 
498adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port),
499adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
500adb3dccfSVladimir Oltean 
501adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
502adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
503adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
504adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
505adb3dccfSVladimir Oltean 
506adb3dccfSVladimir Oltean 	/* Enable transmission of pause frames */
507adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
508adb3dccfSVladimir Oltean }
509adb3dccfSVladimir Oltean 
510adb3dccfSVladimir Oltean static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu)
511adb3dccfSVladimir Oltean {
512adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
513adb3dccfSVladimir Oltean 	unsigned long cpu_flood;
514adb3dccfSVladimir Oltean 
515adb3dccfSVladimir Oltean 	felix_npi_port_init(ocelot, cpu);
516adb3dccfSVladimir Oltean 
517adb3dccfSVladimir Oltean 	/* Include the CPU port module (and indirectly, the NPI port)
518adb3dccfSVladimir Oltean 	 * in the forwarding mask for unknown unicast - the hardware
519adb3dccfSVladimir Oltean 	 * default value for ANA_FLOODING_FLD_UNICAST excludes
520adb3dccfSVladimir Oltean 	 * BIT(ocelot->num_phys_ports), and so does ocelot_init,
521adb3dccfSVladimir Oltean 	 * since Ocelot relies on whitelisting MAC addresses towards
522adb3dccfSVladimir Oltean 	 * PGID_CPU.
523adb3dccfSVladimir Oltean 	 * We do this because DSA does not yet perform RX filtering,
524adb3dccfSVladimir Oltean 	 * and the NPI port does not perform source address learning,
525adb3dccfSVladimir Oltean 	 * so traffic sent to Linux is effectively unknown from the
526adb3dccfSVladimir Oltean 	 * switch's perspective.
527adb3dccfSVladimir Oltean 	 */
528adb3dccfSVladimir Oltean 	cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
529adb3dccfSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_UC);
5306edb9e8dSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_MC);
531b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_BC);
532adb3dccfSVladimir Oltean 
533adb3dccfSVladimir Oltean 	return 0;
534adb3dccfSVladimir Oltean }
535adb3dccfSVladimir Oltean 
536adb3dccfSVladimir Oltean static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu)
537adb3dccfSVladimir Oltean {
538adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
539adb3dccfSVladimir Oltean 
540adb3dccfSVladimir Oltean 	felix_npi_port_deinit(ocelot, cpu);
541adb3dccfSVladimir Oltean }
542adb3dccfSVladimir Oltean 
543adb3dccfSVladimir Oltean static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu,
544adb3dccfSVladimir Oltean 				  enum dsa_tag_protocol proto)
545adb3dccfSVladimir Oltean {
546adb3dccfSVladimir Oltean 	int err;
547adb3dccfSVladimir Oltean 
548adb3dccfSVladimir Oltean 	switch (proto) {
5497c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
550adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
551adb3dccfSVladimir Oltean 		err = felix_setup_tag_npi(ds, cpu);
552adb3dccfSVladimir Oltean 		break;
553e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
554e21268efSVladimir Oltean 		err = felix_setup_tag_8021q(ds, cpu);
555e21268efSVladimir Oltean 		break;
556adb3dccfSVladimir Oltean 	default:
557adb3dccfSVladimir Oltean 		err = -EPROTONOSUPPORT;
558adb3dccfSVladimir Oltean 	}
559adb3dccfSVladimir Oltean 
560adb3dccfSVladimir Oltean 	return err;
561adb3dccfSVladimir Oltean }
562adb3dccfSVladimir Oltean 
563adb3dccfSVladimir Oltean static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu,
564adb3dccfSVladimir Oltean 				   enum dsa_tag_protocol proto)
565adb3dccfSVladimir Oltean {
566adb3dccfSVladimir Oltean 	switch (proto) {
5677c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
568adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
569adb3dccfSVladimir Oltean 		felix_teardown_tag_npi(ds, cpu);
570adb3dccfSVladimir Oltean 		break;
571e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
572e21268efSVladimir Oltean 		felix_teardown_tag_8021q(ds, cpu);
573e21268efSVladimir Oltean 		break;
574adb3dccfSVladimir Oltean 	default:
575adb3dccfSVladimir Oltean 		break;
576adb3dccfSVladimir Oltean 	}
577adb3dccfSVladimir Oltean }
578adb3dccfSVladimir Oltean 
579e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the
580e21268efSVladimir Oltean  * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
581e21268efSVladimir Oltean  * or the restoration is guaranteed to work.
582e21268efSVladimir Oltean  */
583adb3dccfSVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu,
584adb3dccfSVladimir Oltean 				     enum dsa_tag_protocol proto)
585adb3dccfSVladimir Oltean {
586adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
587adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
588adb3dccfSVladimir Oltean 	enum dsa_tag_protocol old_proto = felix->tag_proto;
589adb3dccfSVladimir Oltean 	int err;
590adb3dccfSVladimir Oltean 
5917c4bb540SVladimir Oltean 	if (proto != DSA_TAG_PROTO_SEVILLE &&
5927c4bb540SVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT &&
593e21268efSVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT_8021Q)
594adb3dccfSVladimir Oltean 		return -EPROTONOSUPPORT;
595adb3dccfSVladimir Oltean 
596adb3dccfSVladimir Oltean 	felix_del_tag_protocol(ds, cpu, old_proto);
597adb3dccfSVladimir Oltean 
598adb3dccfSVladimir Oltean 	err = felix_set_tag_protocol(ds, cpu, proto);
599adb3dccfSVladimir Oltean 	if (err) {
600adb3dccfSVladimir Oltean 		felix_set_tag_protocol(ds, cpu, old_proto);
601adb3dccfSVladimir Oltean 		return err;
602adb3dccfSVladimir Oltean 	}
603adb3dccfSVladimir Oltean 
604adb3dccfSVladimir Oltean 	felix->tag_proto = proto;
605adb3dccfSVladimir Oltean 
606adb3dccfSVladimir Oltean 	return 0;
607adb3dccfSVladimir Oltean }
608adb3dccfSVladimir Oltean 
60956051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
6104d776482SFlorian Fainelli 						    int port,
6114d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
61256051948SVladimir Oltean {
613adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
614adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
615adb3dccfSVladimir Oltean 
616adb3dccfSVladimir Oltean 	return felix->tag_proto;
61756051948SVladimir Oltean }
61856051948SVladimir Oltean 
61956051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
62056051948SVladimir Oltean 				 unsigned int ageing_time)
62156051948SVladimir Oltean {
62256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
62356051948SVladimir Oltean 
62456051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
62556051948SVladimir Oltean 
62656051948SVladimir Oltean 	return 0;
62756051948SVladimir Oltean }
62856051948SVladimir Oltean 
62956051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
63056051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
63156051948SVladimir Oltean {
63256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
63356051948SVladimir Oltean 
63456051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
63556051948SVladimir Oltean }
63656051948SVladimir Oltean 
63756051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
63856051948SVladimir Oltean 			 const unsigned char *addr, u16 vid)
63956051948SVladimir Oltean {
64056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
64156051948SVladimir Oltean 
64287b0f983SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid);
64356051948SVladimir Oltean }
64456051948SVladimir Oltean 
64556051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
64656051948SVladimir Oltean 			 const unsigned char *addr, u16 vid)
64756051948SVladimir Oltean {
64856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
64956051948SVladimir Oltean 
65056051948SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid);
65156051948SVladimir Oltean }
65256051948SVladimir Oltean 
653a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port,
654209edf95SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb)
655209edf95SVladimir Oltean {
656209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
657209edf95SVladimir Oltean 
658a52b2da7SVladimir Oltean 	return ocelot_port_mdb_add(ocelot, port, mdb);
659209edf95SVladimir Oltean }
660209edf95SVladimir Oltean 
661209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port,
662209edf95SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb)
663209edf95SVladimir Oltean {
664209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
665209edf95SVladimir Oltean 
666209edf95SVladimir Oltean 	return ocelot_port_mdb_del(ocelot, port, mdb);
667209edf95SVladimir Oltean }
668209edf95SVladimir Oltean 
66956051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
67056051948SVladimir Oltean 				       u8 state)
67156051948SVladimir Oltean {
67256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
67356051948SVladimir Oltean 
67456051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
67556051948SVladimir Oltean }
67656051948SVladimir Oltean 
677421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port,
678421741eaSVladimir Oltean 				  struct switchdev_brport_flags val,
679421741eaSVladimir Oltean 				  struct netlink_ext_ack *extack)
680421741eaSVladimir Oltean {
681421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
682421741eaSVladimir Oltean 
683421741eaSVladimir Oltean 	return ocelot_port_pre_bridge_flags(ocelot, port, val);
684421741eaSVladimir Oltean }
685421741eaSVladimir Oltean 
686421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port,
687421741eaSVladimir Oltean 			      struct switchdev_brport_flags val,
688421741eaSVladimir Oltean 			      struct netlink_ext_ack *extack)
689421741eaSVladimir Oltean {
690421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
691421741eaSVladimir Oltean 
692421741eaSVladimir Oltean 	ocelot_port_bridge_flags(ocelot, port, val);
693421741eaSVladimir Oltean 
694421741eaSVladimir Oltean 	return 0;
695421741eaSVladimir Oltean }
696421741eaSVladimir Oltean 
69756051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
69856051948SVladimir Oltean 			     struct net_device *br)
69956051948SVladimir Oltean {
70056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
70156051948SVladimir Oltean 
702e4bd44e8SVladimir Oltean 	ocelot_port_bridge_join(ocelot, port, br);
703e4bd44e8SVladimir Oltean 
704e4bd44e8SVladimir Oltean 	return 0;
70556051948SVladimir Oltean }
70656051948SVladimir Oltean 
70756051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
70856051948SVladimir Oltean 			       struct net_device *br)
70956051948SVladimir Oltean {
71056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
71156051948SVladimir Oltean 
71256051948SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, br);
71356051948SVladimir Oltean }
71456051948SVladimir Oltean 
7158fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port,
7168fe6832eSVladimir Oltean 			  struct net_device *bond,
7178fe6832eSVladimir Oltean 			  struct netdev_lag_upper_info *info)
7188fe6832eSVladimir Oltean {
7198fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7208fe6832eSVladimir Oltean 
7218fe6832eSVladimir Oltean 	return ocelot_port_lag_join(ocelot, port, bond, info);
7228fe6832eSVladimir Oltean }
7238fe6832eSVladimir Oltean 
7248fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port,
7258fe6832eSVladimir Oltean 			   struct net_device *bond)
7268fe6832eSVladimir Oltean {
7278fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7288fe6832eSVladimir Oltean 
7298fe6832eSVladimir Oltean 	ocelot_port_lag_leave(ocelot, port, bond);
7308fe6832eSVladimir Oltean 
7318fe6832eSVladimir Oltean 	return 0;
7328fe6832eSVladimir Oltean }
7338fe6832eSVladimir Oltean 
7348fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port)
7358fe6832eSVladimir Oltean {
7368fe6832eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
7378fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7388fe6832eSVladimir Oltean 
7398fe6832eSVladimir Oltean 	ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
7408fe6832eSVladimir Oltean 
7418fe6832eSVladimir Oltean 	return 0;
7428fe6832eSVladimir Oltean }
7438fe6832eSVladimir Oltean 
74456051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
74501af940eSVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan,
74601af940eSVladimir Oltean 			      struct netlink_ext_ack *extack)
74756051948SVladimir Oltean {
7482f0402feSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
749b7a9e0daSVladimir Oltean 	u16 flags = vlan->flags;
7502f0402feSVladimir Oltean 
7519a720680SVladimir Oltean 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
7529a720680SVladimir Oltean 	 * egress-untagged or not, pvid or not, make no difference. This
7539a720680SVladimir Oltean 	 * behavior is already better than what DSA just tries to approximate
7549a720680SVladimir Oltean 	 * when it installs the VLAN with the same flags on the CPU port.
7559a720680SVladimir Oltean 	 * Just accept any configuration, and don't let ocelot deny installing
7569a720680SVladimir Oltean 	 * multiple native VLANs on the NPI port, because the switch doesn't
7579a720680SVladimir Oltean 	 * look at the port tag settings towards the NPI interface anyway.
7589a720680SVladimir Oltean 	 */
7599a720680SVladimir Oltean 	if (port == ocelot->npi)
7609a720680SVladimir Oltean 		return 0;
7619a720680SVladimir Oltean 
762b7a9e0daSVladimir Oltean 	return ocelot_vlan_prepare(ocelot, port, vlan->vid,
7632f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_PVID,
76401af940eSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_UNTAGGED,
76501af940eSVladimir Oltean 				   extack);
76656051948SVladimir Oltean }
76756051948SVladimir Oltean 
76889153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
76989153ed6SVladimir Oltean 				struct netlink_ext_ack *extack)
77056051948SVladimir Oltean {
77156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
77256051948SVladimir Oltean 
773*3b95d1b2SVladimir Oltean 	return ocelot_port_vlan_filtering(ocelot, port, enabled, extack);
77456051948SVladimir Oltean }
77556051948SVladimir Oltean 
7761958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port,
77731046a5fSVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan,
77831046a5fSVladimir Oltean 			  struct netlink_ext_ack *extack)
77956051948SVladimir Oltean {
78056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
781183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
7821958d581SVladimir Oltean 	int err;
78356051948SVladimir Oltean 
78401af940eSVladimir Oltean 	err = felix_vlan_prepare(ds, port, vlan, extack);
7851958d581SVladimir Oltean 	if (err)
7861958d581SVladimir Oltean 		return err;
7871958d581SVladimir Oltean 
7881958d581SVladimir Oltean 	return ocelot_vlan_add(ocelot, port, vlan->vid,
789183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_PVID,
790183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_UNTAGGED);
79156051948SVladimir Oltean }
79256051948SVladimir Oltean 
79356051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
79456051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
79556051948SVladimir Oltean {
79656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
79756051948SVladimir Oltean 
798b7a9e0daSVladimir Oltean 	return ocelot_vlan_del(ocelot, port, vlan->vid);
79956051948SVladimir Oltean }
80056051948SVladimir Oltean 
801bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
802bdeced75SVladimir Oltean 				   unsigned long *supported,
803bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
804bdeced75SVladimir Oltean {
805bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
806375e1314SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
807bdeced75SVladimir Oltean 
808375e1314SVladimir Oltean 	if (felix->info->phylink_validate)
809375e1314SVladimir Oltean 		felix->info->phylink_validate(ocelot, port, supported, state);
810bdeced75SVladimir Oltean }
811bdeced75SVladimir Oltean 
812bdeced75SVladimir Oltean static void felix_phylink_mac_config(struct dsa_switch *ds, int port,
813bdeced75SVladimir Oltean 				     unsigned int link_an_mode,
814bdeced75SVladimir Oltean 				     const struct phylink_link_state *state)
815bdeced75SVladimir Oltean {
816bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
817bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
818588d0550SIoana Ciornei 	struct dsa_port *dp = dsa_to_port(ds, port);
819bdeced75SVladimir Oltean 
820588d0550SIoana Ciornei 	if (felix->pcs[port])
821588d0550SIoana Ciornei 		phylink_set_pcs(dp->pl, &felix->pcs[port]->pcs);
822bdeced75SVladimir Oltean }
823bdeced75SVladimir Oltean 
824bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
825bdeced75SVladimir Oltean 					unsigned int link_an_mode,
826bdeced75SVladimir Oltean 					phy_interface_t interface)
827bdeced75SVladimir Oltean {
828bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
829bdeced75SVladimir Oltean 
830e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
831e6e12df6SVladimir Oltean 				     FELIX_MAC_QUIRKS);
832bdeced75SVladimir Oltean }
833bdeced75SVladimir Oltean 
834bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
835bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
836bdeced75SVladimir Oltean 				      phy_interface_t interface,
8375b502a7bSRussell King 				      struct phy_device *phydev,
8385b502a7bSRussell King 				      int speed, int duplex,
8395b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
840bdeced75SVladimir Oltean {
841bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8427e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
843bdeced75SVladimir Oltean 
844e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
845e6e12df6SVladimir Oltean 				   interface, speed, duplex, tx_pause, rx_pause,
846e6e12df6SVladimir Oltean 				   FELIX_MAC_QUIRKS);
8477e14a2dcSVladimir Oltean 
8487e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
8497e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
850bdeced75SVladimir Oltean }
851bdeced75SVladimir Oltean 
852bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
853bd2b3161SXiaoliang Yang {
854bd2b3161SXiaoliang Yang 	int i;
855bd2b3161SXiaoliang Yang 
856bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
857bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
858bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
859bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
860bd2b3161SXiaoliang Yang 		       port);
861bd2b3161SXiaoliang Yang 
86270d39a6eSVladimir Oltean 	for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
863bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
864bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
865bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
866bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
867bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
868bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
869bd2b3161SXiaoliang Yang 			      port, i);
870bd2b3161SXiaoliang Yang 	}
871bd2b3161SXiaoliang Yang }
872bd2b3161SXiaoliang Yang 
87356051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
87456051948SVladimir Oltean 			      u32 stringset, u8 *data)
87556051948SVladimir Oltean {
87656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
87756051948SVladimir Oltean 
87856051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
87956051948SVladimir Oltean }
88056051948SVladimir Oltean 
88156051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
88256051948SVladimir Oltean {
88356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
88456051948SVladimir Oltean 
88556051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
88656051948SVladimir Oltean }
88756051948SVladimir Oltean 
88856051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
88956051948SVladimir Oltean {
89056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
89156051948SVladimir Oltean 
89256051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
89356051948SVladimir Oltean }
89456051948SVladimir Oltean 
89556051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
89656051948SVladimir Oltean 			     struct ethtool_ts_info *info)
89756051948SVladimir Oltean {
89856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
89956051948SVladimir Oltean 
90056051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
90156051948SVladimir Oltean }
90256051948SVladimir Oltean 
903bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
904bdeced75SVladimir Oltean 				  struct device_node *ports_node,
905bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
906bdeced75SVladimir Oltean {
907bdeced75SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
908bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
909bdeced75SVladimir Oltean 	struct device_node *child;
910bdeced75SVladimir Oltean 
91137fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
912bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
913bdeced75SVladimir Oltean 		u32 port;
914bdeced75SVladimir Oltean 		int err;
915bdeced75SVladimir Oltean 
916bdeced75SVladimir Oltean 		/* Get switch port number from DT */
917bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
918bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
919bdeced75SVladimir Oltean 				"(property \"reg\")\n");
920bdeced75SVladimir Oltean 			of_node_put(child);
921bdeced75SVladimir Oltean 			return -ENODEV;
922bdeced75SVladimir Oltean 		}
923bdeced75SVladimir Oltean 
924bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
925bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
926bdeced75SVladimir Oltean 		if (err) {
927bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
928bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
929bdeced75SVladimir Oltean 				port);
930bdeced75SVladimir Oltean 			of_node_put(child);
931bdeced75SVladimir Oltean 			return -ENODEV;
932bdeced75SVladimir Oltean 		}
933bdeced75SVladimir Oltean 
934bdeced75SVladimir Oltean 		err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode);
935bdeced75SVladimir Oltean 		if (err < 0) {
936bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
937bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
93859ebb430SSumera Priyadarsini 			of_node_put(child);
939bdeced75SVladimir Oltean 			return err;
940bdeced75SVladimir Oltean 		}
941bdeced75SVladimir Oltean 
942bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
943bdeced75SVladimir Oltean 	}
944bdeced75SVladimir Oltean 
945bdeced75SVladimir Oltean 	return 0;
946bdeced75SVladimir Oltean }
947bdeced75SVladimir Oltean 
948bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
949bdeced75SVladimir Oltean {
950bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
951bdeced75SVladimir Oltean 	struct device_node *switch_node;
952bdeced75SVladimir Oltean 	struct device_node *ports_node;
953bdeced75SVladimir Oltean 	int err;
954bdeced75SVladimir Oltean 
955bdeced75SVladimir Oltean 	switch_node = dev->of_node;
956bdeced75SVladimir Oltean 
957bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
958bdeced75SVladimir Oltean 	if (!ports_node) {
959bdeced75SVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
960bdeced75SVladimir Oltean 		return -ENODEV;
961bdeced75SVladimir Oltean 	}
962bdeced75SVladimir Oltean 
963bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
964bdeced75SVladimir Oltean 	of_node_put(ports_node);
965bdeced75SVladimir Oltean 
966bdeced75SVladimir Oltean 	return err;
967bdeced75SVladimir Oltean }
968bdeced75SVladimir Oltean 
96956051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
97056051948SVladimir Oltean {
97156051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
972bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
973b4024c9eSClaudiu Manoil 	struct resource res;
97456051948SVladimir Oltean 	int port, i, err;
97556051948SVladimir Oltean 
97656051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
97756051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
97856051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
97956051948SVladimir Oltean 	if (!ocelot->ports)
98056051948SVladimir Oltean 		return -ENOMEM;
98156051948SVladimir Oltean 
98256051948SVladimir Oltean 	ocelot->map		= felix->info->map;
98356051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
98456051948SVladimir Oltean 	ocelot->num_stats	= felix->info->num_stats;
98521ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
98607d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
98756051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
988cacea62fSVladimir Oltean 	ocelot->npi_inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
989cacea62fSVladimir Oltean 	ocelot->npi_xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
990f59fd9caSVladimir Oltean 	ocelot->devlink		= felix->ds->devlink;
99156051948SVladimir Oltean 
992bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
993bdeced75SVladimir Oltean 				 GFP_KERNEL);
994bdeced75SVladimir Oltean 	if (!port_phy_modes)
995bdeced75SVladimir Oltean 		return -ENOMEM;
996bdeced75SVladimir Oltean 
997bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
998bdeced75SVladimir Oltean 	if (err) {
999bdeced75SVladimir Oltean 		kfree(port_phy_modes);
1000bdeced75SVladimir Oltean 		return err;
1001bdeced75SVladimir Oltean 	}
1002bdeced75SVladimir Oltean 
100356051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
100456051948SVladimir Oltean 		struct regmap *target;
100556051948SVladimir Oltean 
100656051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
100756051948SVladimir Oltean 			continue;
100856051948SVladimir Oltean 
1009b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
1010b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1011375e1314SVladimir Oltean 		res.start += felix->switch_base;
1012375e1314SVladimir Oltean 		res.end += felix->switch_base;
101356051948SVladimir Oltean 
1014b4024c9eSClaudiu Manoil 		target = ocelot_regmap_init(ocelot, &res);
101556051948SVladimir Oltean 		if (IS_ERR(target)) {
101656051948SVladimir Oltean 			dev_err(ocelot->dev,
101756051948SVladimir Oltean 				"Failed to map device memory space\n");
1018bdeced75SVladimir Oltean 			kfree(port_phy_modes);
101956051948SVladimir Oltean 			return PTR_ERR(target);
102056051948SVladimir Oltean 		}
102156051948SVladimir Oltean 
102256051948SVladimir Oltean 		ocelot->targets[i] = target;
102356051948SVladimir Oltean 	}
102456051948SVladimir Oltean 
102556051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
102656051948SVladimir Oltean 	if (err) {
102756051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
1028bdeced75SVladimir Oltean 		kfree(port_phy_modes);
102956051948SVladimir Oltean 		return err;
103056051948SVladimir Oltean 	}
103156051948SVladimir Oltean 
103256051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
103356051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
103491c724cfSVladimir Oltean 		struct regmap *target;
103556051948SVladimir Oltean 
103656051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
103756051948SVladimir Oltean 					   sizeof(struct ocelot_port),
103856051948SVladimir Oltean 					   GFP_KERNEL);
103956051948SVladimir Oltean 		if (!ocelot_port) {
104056051948SVladimir Oltean 			dev_err(ocelot->dev,
104156051948SVladimir Oltean 				"failed to allocate port memory\n");
1042bdeced75SVladimir Oltean 			kfree(port_phy_modes);
104356051948SVladimir Oltean 			return -ENOMEM;
104456051948SVladimir Oltean 		}
104556051948SVladimir Oltean 
1046b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
1047b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1048375e1314SVladimir Oltean 		res.start += felix->switch_base;
1049375e1314SVladimir Oltean 		res.end += felix->switch_base;
105056051948SVladimir Oltean 
105191c724cfSVladimir Oltean 		target = ocelot_regmap_init(ocelot, &res);
105291c724cfSVladimir Oltean 		if (IS_ERR(target)) {
105356051948SVladimir Oltean 			dev_err(ocelot->dev,
105491c724cfSVladimir Oltean 				"Failed to map memory space for port %d\n",
105591c724cfSVladimir Oltean 				port);
1056bdeced75SVladimir Oltean 			kfree(port_phy_modes);
105791c724cfSVladimir Oltean 			return PTR_ERR(target);
105856051948SVladimir Oltean 		}
105956051948SVladimir Oltean 
1060bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
106156051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
106291c724cfSVladimir Oltean 		ocelot_port->target = target;
106356051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
106456051948SVladimir Oltean 	}
106556051948SVladimir Oltean 
1066bdeced75SVladimir Oltean 	kfree(port_phy_modes);
1067bdeced75SVladimir Oltean 
1068bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
1069bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
1070bdeced75SVladimir Oltean 		if (err < 0)
1071bdeced75SVladimir Oltean 			return err;
1072bdeced75SVladimir Oltean 	}
1073bdeced75SVladimir Oltean 
107456051948SVladimir Oltean 	return 0;
107556051948SVladimir Oltean }
107656051948SVladimir Oltean 
107756051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
107856051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
107956051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
108056051948SVladimir Oltean  * (which will not work).
108156051948SVladimir Oltean  */
108256051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
108356051948SVladimir Oltean {
108456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
108556051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
108656051948SVladimir Oltean 	int port, err;
108756051948SVladimir Oltean 
108856051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
108956051948SVladimir Oltean 	if (err)
109056051948SVladimir Oltean 		return err;
109156051948SVladimir Oltean 
1092d1cc0e93SVladimir Oltean 	err = ocelot_init(ocelot);
1093d1cc0e93SVladimir Oltean 	if (err)
10946b73b7c9SVladimir Oltean 		goto out_mdiobus_free;
1095d1cc0e93SVladimir Oltean 
10962b49d128SYangbo Lu 	if (ocelot->ptp) {
10972ac7c6c5SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
10982b49d128SYangbo Lu 		if (err) {
10992b49d128SYangbo Lu 			dev_err(ocelot->dev,
11002b49d128SYangbo Lu 				"Timestamp initialization failed\n");
11012b49d128SYangbo Lu 			ocelot->ptp = 0;
11022b49d128SYangbo Lu 		}
11032b49d128SYangbo Lu 	}
110456051948SVladimir Oltean 
110556051948SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
1106adb3dccfSVladimir Oltean 		if (dsa_is_unused_port(ds, port))
1107adb3dccfSVladimir Oltean 			continue;
110856051948SVladimir Oltean 
1109adb3dccfSVladimir Oltean 		ocelot_init_port(ocelot, port);
1110bd2b3161SXiaoliang Yang 
1111bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
1112bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
1113bd2b3161SXiaoliang Yang 		 */
1114bd2b3161SXiaoliang Yang 		felix_port_qos_map_init(ocelot, port);
111556051948SVladimir Oltean 	}
111656051948SVladimir Oltean 
1117f59fd9caSVladimir Oltean 	err = ocelot_devlink_sb_register(ocelot);
1118f59fd9caSVladimir Oltean 	if (err)
11196b73b7c9SVladimir Oltean 		goto out_deinit_ports;
1120f59fd9caSVladimir Oltean 
1121adb3dccfSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
1122adb3dccfSVladimir Oltean 		if (!dsa_is_cpu_port(ds, port))
1123adb3dccfSVladimir Oltean 			continue;
1124adb3dccfSVladimir Oltean 
1125adb3dccfSVladimir Oltean 		/* The initial tag protocol is NPI which always returns 0, so
1126adb3dccfSVladimir Oltean 		 * there's no real point in checking for errors.
11271cf3299bSVladimir Oltean 		 */
1128adb3dccfSVladimir Oltean 		felix_set_tag_protocol(ds, port, felix->tag_proto);
1129adb3dccfSVladimir Oltean 	}
11301cf3299bSVladimir Oltean 
11310b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
1132c54913c1SVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
1133bdeced75SVladimir Oltean 
113456051948SVladimir Oltean 	return 0;
11356b73b7c9SVladimir Oltean 
11366b73b7c9SVladimir Oltean out_deinit_ports:
11376b73b7c9SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
11386b73b7c9SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
11396b73b7c9SVladimir Oltean 			continue;
11406b73b7c9SVladimir Oltean 
11416b73b7c9SVladimir Oltean 		ocelot_deinit_port(ocelot, port);
11426b73b7c9SVladimir Oltean 	}
11436b73b7c9SVladimir Oltean 
11446b73b7c9SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
11456b73b7c9SVladimir Oltean 	ocelot_deinit(ocelot);
11466b73b7c9SVladimir Oltean 
11476b73b7c9SVladimir Oltean out_mdiobus_free:
11486b73b7c9SVladimir Oltean 	if (felix->info->mdio_bus_free)
11496b73b7c9SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
11506b73b7c9SVladimir Oltean 
11516b73b7c9SVladimir Oltean 	return err;
115256051948SVladimir Oltean }
115356051948SVladimir Oltean 
115456051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
115556051948SVladimir Oltean {
115656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1157bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1158e5fb512dSVladimir Oltean 	int port;
1159bdeced75SVladimir Oltean 
1160adb3dccfSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
1161adb3dccfSVladimir Oltean 		if (!dsa_is_cpu_port(ds, port))
1162adb3dccfSVladimir Oltean 			continue;
1163adb3dccfSVladimir Oltean 
1164adb3dccfSVladimir Oltean 		felix_del_tag_protocol(ds, port, felix->tag_proto);
1165adb3dccfSVladimir Oltean 	}
1166adb3dccfSVladimir Oltean 
1167f59fd9caSVladimir Oltean 	ocelot_devlink_sb_unregister(ocelot);
1168d19741b0SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
1169d19741b0SVladimir Oltean 	ocelot_deinit(ocelot);
117056051948SVladimir Oltean 
117142b5adbbSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
117242b5adbbSVladimir Oltean 		if (dsa_is_unused_port(ds, port))
117342b5adbbSVladimir Oltean 			continue;
117442b5adbbSVladimir Oltean 
1175e5fb512dSVladimir Oltean 		ocelot_deinit_port(ocelot, port);
117642b5adbbSVladimir Oltean 	}
1177d19741b0SVladimir Oltean 
1178d19741b0SVladimir Oltean 	if (felix->info->mdio_bus_free)
1179d19741b0SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
118056051948SVladimir Oltean }
118156051948SVladimir Oltean 
1182c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
1183c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1184c0bcf537SYangbo Lu {
1185c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1186c0bcf537SYangbo Lu 
1187c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
1188c0bcf537SYangbo Lu }
1189c0bcf537SYangbo Lu 
1190c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
1191c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1192c0bcf537SYangbo Lu {
1193c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1194c0bcf537SYangbo Lu 
1195c0bcf537SYangbo Lu 	return ocelot_hwstamp_set(ocelot, port, ifr);
1196c0bcf537SYangbo Lu }
1197c0bcf537SYangbo Lu 
11980a6f17c6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot, unsigned int ptp_type)
11990a6f17c6SVladimir Oltean {
12000a6f17c6SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
12010a6f17c6SVladimir Oltean 	int err, grp = 0;
12020a6f17c6SVladimir Oltean 
12030a6f17c6SVladimir Oltean 	if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q)
12040a6f17c6SVladimir Oltean 		return false;
12050a6f17c6SVladimir Oltean 
12060a6f17c6SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
12070a6f17c6SVladimir Oltean 		return false;
12080a6f17c6SVladimir Oltean 
12090a6f17c6SVladimir Oltean 	if (ptp_type == PTP_CLASS_NONE)
12100a6f17c6SVladimir Oltean 		return false;
12110a6f17c6SVladimir Oltean 
12120a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
12130a6f17c6SVladimir Oltean 		struct sk_buff *skb;
12140a6f17c6SVladimir Oltean 		unsigned int type;
12150a6f17c6SVladimir Oltean 
12160a6f17c6SVladimir Oltean 		err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
12170a6f17c6SVladimir Oltean 		if (err)
12180a6f17c6SVladimir Oltean 			goto out;
12190a6f17c6SVladimir Oltean 
12200a6f17c6SVladimir Oltean 		/* We trap to the CPU port module all PTP frames, but
12210a6f17c6SVladimir Oltean 		 * felix_rxtstamp() only gets called for event frames.
12220a6f17c6SVladimir Oltean 		 * So we need to avoid sending duplicate general
12230a6f17c6SVladimir Oltean 		 * message frames by running a second BPF classifier
12240a6f17c6SVladimir Oltean 		 * here and dropping those.
12250a6f17c6SVladimir Oltean 		 */
12260a6f17c6SVladimir Oltean 		__skb_push(skb, ETH_HLEN);
12270a6f17c6SVladimir Oltean 
12280a6f17c6SVladimir Oltean 		type = ptp_classify_raw(skb);
12290a6f17c6SVladimir Oltean 
12300a6f17c6SVladimir Oltean 		__skb_pull(skb, ETH_HLEN);
12310a6f17c6SVladimir Oltean 
12320a6f17c6SVladimir Oltean 		if (type == PTP_CLASS_NONE) {
12330a6f17c6SVladimir Oltean 			kfree_skb(skb);
12340a6f17c6SVladimir Oltean 			continue;
12350a6f17c6SVladimir Oltean 		}
12360a6f17c6SVladimir Oltean 
12370a6f17c6SVladimir Oltean 		netif_rx(skb);
12380a6f17c6SVladimir Oltean 	}
12390a6f17c6SVladimir Oltean 
12400a6f17c6SVladimir Oltean out:
12410a6f17c6SVladimir Oltean 	if (err < 0)
12420a6f17c6SVladimir Oltean 		ocelot_drain_cpu_queue(ocelot, 0);
12430a6f17c6SVladimir Oltean 
12440a6f17c6SVladimir Oltean 	return true;
12450a6f17c6SVladimir Oltean }
12460a6f17c6SVladimir Oltean 
1247c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
1248c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
1249c0bcf537SYangbo Lu {
125040d3f295SVladimir Oltean 	u8 *extraction = skb->data - ETH_HLEN - OCELOT_TAG_LEN;
1251c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
1252c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1253c0bcf537SYangbo Lu 	u32 tstamp_lo, tstamp_hi;
1254c0bcf537SYangbo Lu 	struct timespec64 ts;
1255c0bcf537SYangbo Lu 	u64 tstamp, val;
1256c0bcf537SYangbo Lu 
12570a6f17c6SVladimir Oltean 	/* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb
12580a6f17c6SVladimir Oltean 	 * for RX timestamping. Then free it, and poll for its copy through
12590a6f17c6SVladimir Oltean 	 * MMIO in the CPU port module, and inject that into the stack from
12600a6f17c6SVladimir Oltean 	 * ocelot_xtr_poll().
12610a6f17c6SVladimir Oltean 	 */
12620a6f17c6SVladimir Oltean 	if (felix_check_xtr_pkt(ocelot, type)) {
12630a6f17c6SVladimir Oltean 		kfree_skb(skb);
12640a6f17c6SVladimir Oltean 		return true;
12650a6f17c6SVladimir Oltean 	}
12660a6f17c6SVladimir Oltean 
1267c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1268c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1269c0bcf537SYangbo Lu 
127040d3f295SVladimir Oltean 	ocelot_xfh_get_rew_val(extraction, &val);
1271c0bcf537SYangbo Lu 	tstamp_lo = (u32)val;
1272c0bcf537SYangbo Lu 
1273c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
1274c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
1275c0bcf537SYangbo Lu 		tstamp_hi--;
1276c0bcf537SYangbo Lu 
1277c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
1278c0bcf537SYangbo Lu 
1279c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
1280c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1281c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
1282c0bcf537SYangbo Lu 	return false;
1283c0bcf537SYangbo Lu }
1284c0bcf537SYangbo Lu 
12855c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port,
12865c5416f5SYangbo Lu 			   struct sk_buff *skb)
1287c0bcf537SYangbo Lu {
1288c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1289682eaad9SYangbo Lu 	struct sk_buff *clone = NULL;
1290c0bcf537SYangbo Lu 
1291682eaad9SYangbo Lu 	if (!ocelot->ptp)
12925c5416f5SYangbo Lu 		return;
1293c0bcf537SYangbo Lu 
1294682eaad9SYangbo Lu 	if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone))
1295682eaad9SYangbo Lu 		return;
1296682eaad9SYangbo Lu 
1297682eaad9SYangbo Lu 	if (clone)
1298c4b364ceSYangbo Lu 		OCELOT_SKB_CB(skb)->clone = clone;
12995c5416f5SYangbo Lu }
1300c0bcf537SYangbo Lu 
13010b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
13020b912fc9SVladimir Oltean {
13030b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
13040b912fc9SVladimir Oltean 
13050b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
13060b912fc9SVladimir Oltean 
13070b912fc9SVladimir Oltean 	return 0;
13080b912fc9SVladimir Oltean }
13090b912fc9SVladimir Oltean 
13100b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
13110b912fc9SVladimir Oltean {
13120b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
13130b912fc9SVladimir Oltean 
13140b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
13150b912fc9SVladimir Oltean }
13160b912fc9SVladimir Oltean 
131707d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
131807d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
131907d985eeSVladimir Oltean {
132007d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
132107d985eeSVladimir Oltean 
132207d985eeSVladimir Oltean 	return ocelot_cls_flower_replace(ocelot, port, cls, ingress);
132307d985eeSVladimir Oltean }
132407d985eeSVladimir Oltean 
132507d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
132607d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
132707d985eeSVladimir Oltean {
132807d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
132907d985eeSVladimir Oltean 
133007d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
133107d985eeSVladimir Oltean }
133207d985eeSVladimir Oltean 
133307d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
133407d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
133507d985eeSVladimir Oltean {
133607d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
133707d985eeSVladimir Oltean 
133807d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
133907d985eeSVladimir Oltean }
134007d985eeSVladimir Oltean 
1341fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
1342fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
1343fc411eaaSVladimir Oltean {
1344fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1345fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
1346fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
13475f035af7SPo Liu 		.burst = policer->burst,
1348fc411eaaSVladimir Oltean 	};
1349fc411eaaSVladimir Oltean 
1350fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
1351fc411eaaSVladimir Oltean }
1352fc411eaaSVladimir Oltean 
1353fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
1354fc411eaaSVladimir Oltean {
1355fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1356fc411eaaSVladimir Oltean 
1357fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
1358fc411eaaSVladimir Oltean }
1359fc411eaaSVladimir Oltean 
1360de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
1361de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
1362de143c0eSXiaoliang Yang 			       void *type_data)
1363de143c0eSXiaoliang Yang {
1364de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
1365de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
1366de143c0eSXiaoliang Yang 
1367de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
1368de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
1369de143c0eSXiaoliang Yang 	else
1370de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
1371de143c0eSXiaoliang Yang }
1372de143c0eSXiaoliang Yang 
1373f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
1374f59fd9caSVladimir Oltean 			     u16 pool_index,
1375f59fd9caSVladimir Oltean 			     struct devlink_sb_pool_info *pool_info)
1376f59fd9caSVladimir Oltean {
1377f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1378f59fd9caSVladimir Oltean 
1379f59fd9caSVladimir Oltean 	return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
1380f59fd9caSVladimir Oltean }
1381f59fd9caSVladimir Oltean 
1382f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
1383f59fd9caSVladimir Oltean 			     u16 pool_index, u32 size,
1384f59fd9caSVladimir Oltean 			     enum devlink_sb_threshold_type threshold_type,
1385f59fd9caSVladimir Oltean 			     struct netlink_ext_ack *extack)
1386f59fd9caSVladimir Oltean {
1387f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1388f59fd9caSVladimir Oltean 
1389f59fd9caSVladimir Oltean 	return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
1390f59fd9caSVladimir Oltean 				  threshold_type, extack);
1391f59fd9caSVladimir Oltean }
1392f59fd9caSVladimir Oltean 
1393f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
1394f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1395f59fd9caSVladimir Oltean 				  u32 *p_threshold)
1396f59fd9caSVladimir Oltean {
1397f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1398f59fd9caSVladimir Oltean 
1399f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
1400f59fd9caSVladimir Oltean 				       p_threshold);
1401f59fd9caSVladimir Oltean }
1402f59fd9caSVladimir Oltean 
1403f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
1404f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1405f59fd9caSVladimir Oltean 				  u32 threshold, struct netlink_ext_ack *extack)
1406f59fd9caSVladimir Oltean {
1407f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1408f59fd9caSVladimir Oltean 
1409f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
1410f59fd9caSVladimir Oltean 				       threshold, extack);
1411f59fd9caSVladimir Oltean }
1412f59fd9caSVladimir Oltean 
1413f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
1414f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1415f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1416f59fd9caSVladimir Oltean 				     u16 *p_pool_index, u32 *p_threshold)
1417f59fd9caSVladimir Oltean {
1418f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1419f59fd9caSVladimir Oltean 
1420f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
1421f59fd9caSVladimir Oltean 					  pool_type, p_pool_index,
1422f59fd9caSVladimir Oltean 					  p_threshold);
1423f59fd9caSVladimir Oltean }
1424f59fd9caSVladimir Oltean 
1425f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
1426f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1427f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1428f59fd9caSVladimir Oltean 				     u16 pool_index, u32 threshold,
1429f59fd9caSVladimir Oltean 				     struct netlink_ext_ack *extack)
1430f59fd9caSVladimir Oltean {
1431f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1432f59fd9caSVladimir Oltean 
1433f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
1434f59fd9caSVladimir Oltean 					  pool_type, pool_index, threshold,
1435f59fd9caSVladimir Oltean 					  extack);
1436f59fd9caSVladimir Oltean }
1437f59fd9caSVladimir Oltean 
1438f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds,
1439f59fd9caSVladimir Oltean 				 unsigned int sb_index)
1440f59fd9caSVladimir Oltean {
1441f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1442f59fd9caSVladimir Oltean 
1443f59fd9caSVladimir Oltean 	return ocelot_sb_occ_snapshot(ocelot, sb_index);
1444f59fd9caSVladimir Oltean }
1445f59fd9caSVladimir Oltean 
1446f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds,
1447f59fd9caSVladimir Oltean 				  unsigned int sb_index)
1448f59fd9caSVladimir Oltean {
1449f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1450f59fd9caSVladimir Oltean 
1451f59fd9caSVladimir Oltean 	return ocelot_sb_occ_max_clear(ocelot, sb_index);
1452f59fd9caSVladimir Oltean }
1453f59fd9caSVladimir Oltean 
1454f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
1455f59fd9caSVladimir Oltean 				      unsigned int sb_index, u16 pool_index,
1456f59fd9caSVladimir Oltean 				      u32 *p_cur, u32 *p_max)
1457f59fd9caSVladimir Oltean {
1458f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1459f59fd9caSVladimir Oltean 
1460f59fd9caSVladimir Oltean 	return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
1461f59fd9caSVladimir Oltean 					   p_cur, p_max);
1462f59fd9caSVladimir Oltean }
1463f59fd9caSVladimir Oltean 
1464f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
1465f59fd9caSVladimir Oltean 					 unsigned int sb_index, u16 tc_index,
1466f59fd9caSVladimir Oltean 					 enum devlink_sb_pool_type pool_type,
1467f59fd9caSVladimir Oltean 					 u32 *p_cur, u32 *p_max)
1468f59fd9caSVladimir Oltean {
1469f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1470f59fd9caSVladimir Oltean 
1471f59fd9caSVladimir Oltean 	return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
1472f59fd9caSVladimir Oltean 					      pool_type, p_cur, p_max);
1473f59fd9caSVladimir Oltean }
1474f59fd9caSVladimir Oltean 
1475a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port,
1476a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1477a026c50bSHoratiu Vultur {
1478a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1479a026c50bSHoratiu Vultur 
1480a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1481a026c50bSHoratiu Vultur }
1482a026c50bSHoratiu Vultur 
1483a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port,
1484a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1485a026c50bSHoratiu Vultur {
1486a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1487a026c50bSHoratiu Vultur 
1488a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1489a026c50bSHoratiu Vultur }
1490a026c50bSHoratiu Vultur 
1491a026c50bSHoratiu Vultur static int
1492a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port,
1493a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1494a026c50bSHoratiu Vultur {
1495a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1496a026c50bSHoratiu Vultur 
1497a026c50bSHoratiu Vultur 	return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1498a026c50bSHoratiu Vultur }
1499a026c50bSHoratiu Vultur 
1500a026c50bSHoratiu Vultur static int
1501a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port,
1502a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1503a026c50bSHoratiu Vultur {
1504a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1505a026c50bSHoratiu Vultur 
1506a026c50bSHoratiu Vultur 	return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1507a026c50bSHoratiu Vultur }
1508a026c50bSHoratiu Vultur 
1509375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = {
151056051948SVladimir Oltean 	.get_tag_protocol		= felix_get_tag_protocol,
1511adb3dccfSVladimir Oltean 	.change_tag_protocol		= felix_change_tag_protocol,
151256051948SVladimir Oltean 	.setup				= felix_setup,
151356051948SVladimir Oltean 	.teardown			= felix_teardown,
151456051948SVladimir Oltean 	.set_ageing_time		= felix_set_ageing_time,
151556051948SVladimir Oltean 	.get_strings			= felix_get_strings,
151656051948SVladimir Oltean 	.get_ethtool_stats		= felix_get_ethtool_stats,
151756051948SVladimir Oltean 	.get_sset_count			= felix_get_sset_count,
151856051948SVladimir Oltean 	.get_ts_info			= felix_get_ts_info,
1519bdeced75SVladimir Oltean 	.phylink_validate		= felix_phylink_validate,
1520bdeced75SVladimir Oltean 	.phylink_mac_config		= felix_phylink_mac_config,
1521bdeced75SVladimir Oltean 	.phylink_mac_link_down		= felix_phylink_mac_link_down,
1522bdeced75SVladimir Oltean 	.phylink_mac_link_up		= felix_phylink_mac_link_up,
152356051948SVladimir Oltean 	.port_fdb_dump			= felix_fdb_dump,
152456051948SVladimir Oltean 	.port_fdb_add			= felix_fdb_add,
152556051948SVladimir Oltean 	.port_fdb_del			= felix_fdb_del,
1526209edf95SVladimir Oltean 	.port_mdb_add			= felix_mdb_add,
1527209edf95SVladimir Oltean 	.port_mdb_del			= felix_mdb_del,
1528421741eaSVladimir Oltean 	.port_pre_bridge_flags		= felix_pre_bridge_flags,
1529421741eaSVladimir Oltean 	.port_bridge_flags		= felix_bridge_flags,
153056051948SVladimir Oltean 	.port_bridge_join		= felix_bridge_join,
153156051948SVladimir Oltean 	.port_bridge_leave		= felix_bridge_leave,
15328fe6832eSVladimir Oltean 	.port_lag_join			= felix_lag_join,
15338fe6832eSVladimir Oltean 	.port_lag_leave			= felix_lag_leave,
15348fe6832eSVladimir Oltean 	.port_lag_change		= felix_lag_change,
153556051948SVladimir Oltean 	.port_stp_state_set		= felix_bridge_stp_state_set,
153656051948SVladimir Oltean 	.port_vlan_filtering		= felix_vlan_filtering,
153756051948SVladimir Oltean 	.port_vlan_add			= felix_vlan_add,
153856051948SVladimir Oltean 	.port_vlan_del			= felix_vlan_del,
1539c0bcf537SYangbo Lu 	.port_hwtstamp_get		= felix_hwtstamp_get,
1540c0bcf537SYangbo Lu 	.port_hwtstamp_set		= felix_hwtstamp_set,
1541c0bcf537SYangbo Lu 	.port_rxtstamp			= felix_rxtstamp,
1542c0bcf537SYangbo Lu 	.port_txtstamp			= felix_txtstamp,
15430b912fc9SVladimir Oltean 	.port_change_mtu		= felix_change_mtu,
15440b912fc9SVladimir Oltean 	.port_max_mtu			= felix_get_max_mtu,
1545fc411eaaSVladimir Oltean 	.port_policer_add		= felix_port_policer_add,
1546fc411eaaSVladimir Oltean 	.port_policer_del		= felix_port_policer_del,
154707d985eeSVladimir Oltean 	.cls_flower_add			= felix_cls_flower_add,
154807d985eeSVladimir Oltean 	.cls_flower_del			= felix_cls_flower_del,
154907d985eeSVladimir Oltean 	.cls_flower_stats		= felix_cls_flower_stats,
1550de143c0eSXiaoliang Yang 	.port_setup_tc			= felix_port_setup_tc,
1551f59fd9caSVladimir Oltean 	.devlink_sb_pool_get		= felix_sb_pool_get,
1552f59fd9caSVladimir Oltean 	.devlink_sb_pool_set		= felix_sb_pool_set,
1553f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_get	= felix_sb_port_pool_get,
1554f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_set	= felix_sb_port_pool_set,
1555f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_get	= felix_sb_tc_pool_bind_get,
1556f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_set	= felix_sb_tc_pool_bind_set,
1557f59fd9caSVladimir Oltean 	.devlink_sb_occ_snapshot	= felix_sb_occ_snapshot,
1558f59fd9caSVladimir Oltean 	.devlink_sb_occ_max_clear	= felix_sb_occ_max_clear,
1559f59fd9caSVladimir Oltean 	.devlink_sb_occ_port_pool_get	= felix_sb_occ_port_pool_get,
1560f59fd9caSVladimir Oltean 	.devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
1561a026c50bSHoratiu Vultur 	.port_mrp_add			= felix_mrp_add,
1562a026c50bSHoratiu Vultur 	.port_mrp_del			= felix_mrp_del,
1563a026c50bSHoratiu Vultur 	.port_mrp_add_ring_role		= felix_mrp_add_ring_role,
1564a026c50bSHoratiu Vultur 	.port_mrp_del_ring_role		= felix_mrp_del_ring_role,
15655da11eb4SVladimir Oltean 	.tag_8021q_vlan_add		= felix_tag_8021q_vlan_add,
15665da11eb4SVladimir Oltean 	.tag_8021q_vlan_del		= felix_tag_8021q_vlan_del,
156756051948SVladimir Oltean };
1568319e4dd1SVladimir Oltean 
1569319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
1570319e4dd1SVladimir Oltean {
1571319e4dd1SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1572319e4dd1SVladimir Oltean 	struct dsa_switch *ds = felix->ds;
1573319e4dd1SVladimir Oltean 
1574319e4dd1SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
1575319e4dd1SVladimir Oltean 		return NULL;
1576319e4dd1SVladimir Oltean 
1577319e4dd1SVladimir Oltean 	return dsa_to_port(ds, port)->slave;
1578319e4dd1SVladimir Oltean }
1579319e4dd1SVladimir Oltean 
1580319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev)
1581319e4dd1SVladimir Oltean {
1582319e4dd1SVladimir Oltean 	struct dsa_port *dp;
1583319e4dd1SVladimir Oltean 
1584319e4dd1SVladimir Oltean 	dp = dsa_port_from_netdev(dev);
1585319e4dd1SVladimir Oltean 	if (IS_ERR(dp))
1586319e4dd1SVladimir Oltean 		return -EINVAL;
1587319e4dd1SVladimir Oltean 
1588319e4dd1SVladimir Oltean 	return dp->index;
1589319e4dd1SVladimir Oltean }
1590