xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision 40d3f295b5feda409784e569550057b5fbc2a295)
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>
17*40d3f295SVladimir Oltean #include <linux/dsa/ocelot.h>
1884705fc1SMaxim Kochetkov #include <linux/platform_device.h>
1956051948SVladimir Oltean #include <linux/module.h>
20bdeced75SVladimir Oltean #include <linux/of_net.h>
2156051948SVladimir Oltean #include <linux/pci.h>
2256051948SVladimir Oltean #include <linux/of.h>
23588d0550SIoana Ciornei #include <linux/pcs-lynx.h>
24fc411eaaSVladimir Oltean #include <net/pkt_sched.h>
2556051948SVladimir Oltean #include <net/dsa.h>
2656051948SVladimir Oltean #include "felix.h"
2756051948SVladimir Oltean 
28e21268efSVladimir Oltean static int felix_tag_8021q_rxvlan_add(struct felix *felix, int port, u16 vid,
29e21268efSVladimir Oltean 				      bool pvid, bool untagged)
30e21268efSVladimir Oltean {
31e21268efSVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
32e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
33e21268efSVladimir Oltean 	struct dsa_switch *ds = felix->ds;
34e21268efSVladimir Oltean 	int key_length, upstream, err;
35e21268efSVladimir Oltean 
36e21268efSVladimir Oltean 	/* We don't need to install the rxvlan into the other ports' filtering
37e21268efSVladimir Oltean 	 * tables, because we're just pushing the rxvlan when sending towards
38e21268efSVladimir Oltean 	 * the CPU
39e21268efSVladimir Oltean 	 */
40e21268efSVladimir Oltean 	if (!pvid)
41e21268efSVladimir Oltean 		return 0;
42e21268efSVladimir Oltean 
43e21268efSVladimir Oltean 	key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length;
44e21268efSVladimir Oltean 	upstream = dsa_upstream_port(ds, port);
45e21268efSVladimir Oltean 
46e21268efSVladimir Oltean 	outer_tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter),
47e21268efSVladimir Oltean 				     GFP_KERNEL);
48e21268efSVladimir Oltean 	if (!outer_tagging_rule)
49e21268efSVladimir Oltean 		return -ENOMEM;
50e21268efSVladimir Oltean 
51e21268efSVladimir Oltean 	outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
52e21268efSVladimir Oltean 	outer_tagging_rule->prio = 1;
53e21268efSVladimir Oltean 	outer_tagging_rule->id.cookie = port;
54e21268efSVladimir Oltean 	outer_tagging_rule->id.tc_offload = false;
55e21268efSVladimir Oltean 	outer_tagging_rule->block_id = VCAP_ES0;
56e21268efSVladimir Oltean 	outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
57e21268efSVladimir Oltean 	outer_tagging_rule->lookup = 0;
58e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.value = port;
59e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0);
60e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.value = upstream;
61e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0);
62e21268efSVladimir Oltean 	outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG;
63e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD;
64e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_vid_sel = 1;
65e21268efSVladimir Oltean 	outer_tagging_rule->action.vid_a_val = vid;
66e21268efSVladimir Oltean 
67e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL);
68e21268efSVladimir Oltean 	if (err)
69e21268efSVladimir Oltean 		kfree(outer_tagging_rule);
70e21268efSVladimir Oltean 
71e21268efSVladimir Oltean 	return err;
72e21268efSVladimir Oltean }
73e21268efSVladimir Oltean 
74e21268efSVladimir Oltean static int felix_tag_8021q_txvlan_add(struct felix *felix, int port, u16 vid,
75e21268efSVladimir Oltean 				      bool pvid, bool untagged)
76e21268efSVladimir Oltean {
77e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
78e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
79e21268efSVladimir Oltean 	struct dsa_switch *ds = felix->ds;
80e21268efSVladimir Oltean 	int upstream, err;
81e21268efSVladimir Oltean 
82e21268efSVladimir Oltean 	/* tag_8021q.c assumes we are implementing this via port VLAN
83e21268efSVladimir Oltean 	 * membership, which we aren't. So we don't need to add any VCAP filter
84e21268efSVladimir Oltean 	 * for the CPU port.
85e21268efSVladimir Oltean 	 */
86e21268efSVladimir Oltean 	if (ocelot->ports[port]->is_dsa_8021q_cpu)
87e21268efSVladimir Oltean 		return 0;
88e21268efSVladimir Oltean 
89e21268efSVladimir Oltean 	untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
90e21268efSVladimir Oltean 	if (!untagging_rule)
91e21268efSVladimir Oltean 		return -ENOMEM;
92e21268efSVladimir Oltean 
93e21268efSVladimir Oltean 	redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
94e21268efSVladimir Oltean 	if (!redirect_rule) {
95e21268efSVladimir Oltean 		kfree(untagging_rule);
96e21268efSVladimir Oltean 		return -ENOMEM;
97e21268efSVladimir Oltean 	}
98e21268efSVladimir Oltean 
99e21268efSVladimir Oltean 	upstream = dsa_upstream_port(ds, port);
100e21268efSVladimir Oltean 
101e21268efSVladimir Oltean 	untagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
102e21268efSVladimir Oltean 	untagging_rule->ingress_port_mask = BIT(upstream);
103e21268efSVladimir Oltean 	untagging_rule->vlan.vid.value = vid;
104e21268efSVladimir Oltean 	untagging_rule->vlan.vid.mask = VLAN_VID_MASK;
105e21268efSVladimir Oltean 	untagging_rule->prio = 1;
106e21268efSVladimir Oltean 	untagging_rule->id.cookie = port;
107e21268efSVladimir Oltean 	untagging_rule->id.tc_offload = false;
108e21268efSVladimir Oltean 	untagging_rule->block_id = VCAP_IS1;
109e21268efSVladimir Oltean 	untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
110e21268efSVladimir Oltean 	untagging_rule->lookup = 0;
111e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt_ena = true;
112e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt = 1;
113e21268efSVladimir Oltean 	untagging_rule->action.pag_override_mask = 0xff;
114e21268efSVladimir Oltean 	untagging_rule->action.pag_val = port;
115e21268efSVladimir Oltean 
116e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL);
117e21268efSVladimir Oltean 	if (err) {
118e21268efSVladimir Oltean 		kfree(untagging_rule);
119e21268efSVladimir Oltean 		kfree(redirect_rule);
120e21268efSVladimir Oltean 		return err;
121e21268efSVladimir Oltean 	}
122e21268efSVladimir Oltean 
123e21268efSVladimir Oltean 	redirect_rule->key_type = OCELOT_VCAP_KEY_ANY;
124e21268efSVladimir Oltean 	redirect_rule->ingress_port_mask = BIT(upstream);
125e21268efSVladimir Oltean 	redirect_rule->pag = port;
126e21268efSVladimir Oltean 	redirect_rule->prio = 1;
127e21268efSVladimir Oltean 	redirect_rule->id.cookie = port;
128e21268efSVladimir Oltean 	redirect_rule->id.tc_offload = false;
129e21268efSVladimir Oltean 	redirect_rule->block_id = VCAP_IS2;
130e21268efSVladimir Oltean 	redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
131e21268efSVladimir Oltean 	redirect_rule->lookup = 0;
132e21268efSVladimir Oltean 	redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT;
133e21268efSVladimir Oltean 	redirect_rule->action.port_mask = BIT(port);
134e21268efSVladimir Oltean 
135e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL);
136e21268efSVladimir Oltean 	if (err) {
137e21268efSVladimir Oltean 		ocelot_vcap_filter_del(ocelot, untagging_rule);
138e21268efSVladimir Oltean 		kfree(redirect_rule);
139e21268efSVladimir Oltean 		return err;
140e21268efSVladimir Oltean 	}
141e21268efSVladimir Oltean 
142e21268efSVladimir Oltean 	return 0;
143e21268efSVladimir Oltean }
144e21268efSVladimir Oltean 
145e21268efSVladimir Oltean static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
146e21268efSVladimir Oltean 				    u16 flags)
147e21268efSVladimir Oltean {
148e21268efSVladimir Oltean 	bool untagged = flags & BRIDGE_VLAN_INFO_UNTAGGED;
149e21268efSVladimir Oltean 	bool pvid = flags & BRIDGE_VLAN_INFO_PVID;
150e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
151e21268efSVladimir Oltean 
152e21268efSVladimir Oltean 	if (vid_is_dsa_8021q_rxvlan(vid))
153e21268efSVladimir Oltean 		return felix_tag_8021q_rxvlan_add(ocelot_to_felix(ocelot),
154e21268efSVladimir Oltean 						  port, vid, pvid, untagged);
155e21268efSVladimir Oltean 
156e21268efSVladimir Oltean 	if (vid_is_dsa_8021q_txvlan(vid))
157e21268efSVladimir Oltean 		return felix_tag_8021q_txvlan_add(ocelot_to_felix(ocelot),
158e21268efSVladimir Oltean 						  port, vid, pvid, untagged);
159e21268efSVladimir Oltean 
160e21268efSVladimir Oltean 	return 0;
161e21268efSVladimir Oltean }
162e21268efSVladimir Oltean 
163e21268efSVladimir Oltean static int felix_tag_8021q_rxvlan_del(struct felix *felix, int port, u16 vid)
164e21268efSVladimir Oltean {
165e21268efSVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
166e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_es0;
167e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
168e21268efSVladimir Oltean 
169e21268efSVladimir Oltean 	block_vcap_es0 = &ocelot->block[VCAP_ES0];
170e21268efSVladimir Oltean 
171e21268efSVladimir Oltean 	outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0,
172e21268efSVladimir Oltean 								 port, false);
173e21268efSVladimir Oltean 	/* In rxvlan_add, we had the "if (!pvid) return 0" logic to avoid
174e21268efSVladimir Oltean 	 * installing outer tagging ES0 rules where they weren't needed.
175e21268efSVladimir Oltean 	 * But in rxvlan_del, the API doesn't give us the "flags" anymore,
176e21268efSVladimir Oltean 	 * so that forces us to be slightly sloppy here, and just assume that
177e21268efSVladimir Oltean 	 * if we didn't find an outer_tagging_rule it means that there was
178e21268efSVladimir Oltean 	 * none in the first place, i.e. rxvlan_del is called on a non-pvid
179e21268efSVladimir Oltean 	 * port. This is most probably true though.
180e21268efSVladimir Oltean 	 */
181e21268efSVladimir Oltean 	if (!outer_tagging_rule)
182e21268efSVladimir Oltean 		return 0;
183e21268efSVladimir Oltean 
184e21268efSVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, outer_tagging_rule);
185e21268efSVladimir Oltean }
186e21268efSVladimir Oltean 
187e21268efSVladimir Oltean static int felix_tag_8021q_txvlan_del(struct felix *felix, int port, u16 vid)
188e21268efSVladimir Oltean {
189e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
190e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is1;
191e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
192e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
193e21268efSVladimir Oltean 	int err;
194e21268efSVladimir Oltean 
195e21268efSVladimir Oltean 	if (ocelot->ports[port]->is_dsa_8021q_cpu)
196e21268efSVladimir Oltean 		return 0;
197e21268efSVladimir Oltean 
198e21268efSVladimir Oltean 	block_vcap_is1 = &ocelot->block[VCAP_IS1];
199e21268efSVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
200e21268efSVladimir Oltean 
201e21268efSVladimir Oltean 	untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1,
202e21268efSVladimir Oltean 							     port, false);
203e21268efSVladimir Oltean 	if (!untagging_rule)
204e21268efSVladimir Oltean 		return 0;
205e21268efSVladimir Oltean 
206e21268efSVladimir Oltean 	err = ocelot_vcap_filter_del(ocelot, untagging_rule);
207e21268efSVladimir Oltean 	if (err)
208e21268efSVladimir Oltean 		return err;
209e21268efSVladimir Oltean 
210e21268efSVladimir Oltean 	redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2,
211e21268efSVladimir Oltean 							    port, false);
212e21268efSVladimir Oltean 	if (!redirect_rule)
213e21268efSVladimir Oltean 		return 0;
214e21268efSVladimir Oltean 
215e21268efSVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, redirect_rule);
216e21268efSVladimir Oltean }
217e21268efSVladimir Oltean 
218e21268efSVladimir Oltean static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
219e21268efSVladimir Oltean {
220e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
221e21268efSVladimir Oltean 
222e21268efSVladimir Oltean 	if (vid_is_dsa_8021q_rxvlan(vid))
223e21268efSVladimir Oltean 		return felix_tag_8021q_rxvlan_del(ocelot_to_felix(ocelot),
224e21268efSVladimir Oltean 						  port, vid);
225e21268efSVladimir Oltean 
226e21268efSVladimir Oltean 	if (vid_is_dsa_8021q_txvlan(vid))
227e21268efSVladimir Oltean 		return felix_tag_8021q_txvlan_del(ocelot_to_felix(ocelot),
228e21268efSVladimir Oltean 						  port, vid);
229e21268efSVladimir Oltean 
230e21268efSVladimir Oltean 	return 0;
231e21268efSVladimir Oltean }
232e21268efSVladimir Oltean 
233e21268efSVladimir Oltean static const struct dsa_8021q_ops felix_tag_8021q_ops = {
234e21268efSVladimir Oltean 	.vlan_add	= felix_tag_8021q_vlan_add,
235e21268efSVladimir Oltean 	.vlan_del	= felix_tag_8021q_vlan_del,
236e21268efSVladimir Oltean };
237e21268efSVladimir Oltean 
238e21268efSVladimir Oltean /* Alternatively to using the NPI functionality, that same hardware MAC
239e21268efSVladimir Oltean  * connected internally to the enetc or fman DSA master can be configured to
240e21268efSVladimir Oltean  * use the software-defined tag_8021q frame format. As far as the hardware is
241e21268efSVladimir Oltean  * concerned, it thinks it is a "dumb switch" - the queues of the CPU port
242e21268efSVladimir Oltean  * module are now disconnected from it, but can still be accessed through
243e21268efSVladimir Oltean  * register-based MMIO.
244e21268efSVladimir Oltean  */
245e21268efSVladimir Oltean static void felix_8021q_cpu_port_init(struct ocelot *ocelot, int port)
246e21268efSVladimir Oltean {
247e21268efSVladimir Oltean 	ocelot->ports[port]->is_dsa_8021q_cpu = true;
248e21268efSVladimir Oltean 	ocelot->npi = -1;
249e21268efSVladimir Oltean 
250e21268efSVladimir Oltean 	/* Overwrite PGID_CPU with the non-tagging port */
251e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, PGID_CPU);
252e21268efSVladimir Oltean 
253e21268efSVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
254e21268efSVladimir Oltean }
255e21268efSVladimir Oltean 
256e21268efSVladimir Oltean static void felix_8021q_cpu_port_deinit(struct ocelot *ocelot, int port)
257e21268efSVladimir Oltean {
258e21268efSVladimir Oltean 	ocelot->ports[port]->is_dsa_8021q_cpu = false;
259e21268efSVladimir Oltean 
260e21268efSVladimir Oltean 	/* Restore PGID_CPU */
261e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(ocelot->num_phys_ports), ANA_PGID_PGID,
262e21268efSVladimir Oltean 			 PGID_CPU);
263e21268efSVladimir Oltean 
264e21268efSVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
265e21268efSVladimir Oltean }
266e21268efSVladimir Oltean 
267e21268efSVladimir Oltean static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu)
268e21268efSVladimir Oltean {
269e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
270e21268efSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
271e21268efSVladimir Oltean 	unsigned long cpu_flood;
272e21268efSVladimir Oltean 	int port, err;
273e21268efSVladimir Oltean 
274e21268efSVladimir Oltean 	felix_8021q_cpu_port_init(ocelot, cpu);
275e21268efSVladimir Oltean 
276e21268efSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
277e21268efSVladimir Oltean 		if (dsa_is_unused_port(ds, port))
278e21268efSVladimir Oltean 			continue;
279e21268efSVladimir Oltean 
280e21268efSVladimir Oltean 		/* This overwrites ocelot_init():
281e21268efSVladimir Oltean 		 * Do not forward BPDU frames to the CPU port module,
282e21268efSVladimir Oltean 		 * for 2 reasons:
283e21268efSVladimir Oltean 		 * - When these packets are injected from the tag_8021q
284e21268efSVladimir Oltean 		 *   CPU port, we want them to go out, not loop back
285e21268efSVladimir Oltean 		 *   into the system.
286e21268efSVladimir Oltean 		 * - STP traffic ingressing on a user port should go to
287e21268efSVladimir Oltean 		 *   the tag_8021q CPU port, not to the hardware CPU
288e21268efSVladimir Oltean 		 *   port module.
289e21268efSVladimir Oltean 		 */
290e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
291e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0),
292e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG, port);
293e21268efSVladimir Oltean 	}
294e21268efSVladimir Oltean 
295e21268efSVladimir Oltean 	/* In tag_8021q mode, the CPU port module is unused. So we
296e21268efSVladimir Oltean 	 * want to disable flooding of any kind to the CPU port module,
297e21268efSVladimir Oltean 	 * since packets going there will end in a black hole.
298e21268efSVladimir Oltean 	 */
299e21268efSVladimir Oltean 	cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
300e21268efSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_UC);
301e21268efSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_MC);
302b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_BC);
303e21268efSVladimir Oltean 
304e21268efSVladimir Oltean 	felix->dsa_8021q_ctx = kzalloc(sizeof(*felix->dsa_8021q_ctx),
305e21268efSVladimir Oltean 				       GFP_KERNEL);
306e21268efSVladimir Oltean 	if (!felix->dsa_8021q_ctx)
307e21268efSVladimir Oltean 		return -ENOMEM;
308e21268efSVladimir Oltean 
309e21268efSVladimir Oltean 	felix->dsa_8021q_ctx->ops = &felix_tag_8021q_ops;
310e21268efSVladimir Oltean 	felix->dsa_8021q_ctx->proto = htons(ETH_P_8021AD);
311e21268efSVladimir Oltean 	felix->dsa_8021q_ctx->ds = ds;
312e21268efSVladimir Oltean 
313e21268efSVladimir Oltean 	err = dsa_8021q_setup(felix->dsa_8021q_ctx, true);
314e21268efSVladimir Oltean 	if (err)
315e21268efSVladimir Oltean 		goto out_free_dsa_8021_ctx;
316e21268efSVladimir Oltean 
317e21268efSVladimir Oltean 	return 0;
318e21268efSVladimir Oltean 
319e21268efSVladimir Oltean out_free_dsa_8021_ctx:
320e21268efSVladimir Oltean 	kfree(felix->dsa_8021q_ctx);
321e21268efSVladimir Oltean 	return err;
322e21268efSVladimir Oltean }
323e21268efSVladimir Oltean 
324e21268efSVladimir Oltean static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu)
325e21268efSVladimir Oltean {
326e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
327e21268efSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
328e21268efSVladimir Oltean 	int err, port;
329e21268efSVladimir Oltean 
330e21268efSVladimir Oltean 	err = dsa_8021q_setup(felix->dsa_8021q_ctx, false);
331e21268efSVladimir Oltean 	if (err)
332e21268efSVladimir Oltean 		dev_err(ds->dev, "dsa_8021q_setup returned %d", err);
333e21268efSVladimir Oltean 
334e21268efSVladimir Oltean 	kfree(felix->dsa_8021q_ctx);
335e21268efSVladimir Oltean 
336e21268efSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
337e21268efSVladimir Oltean 		if (dsa_is_unused_port(ds, port))
338e21268efSVladimir Oltean 			continue;
339e21268efSVladimir Oltean 
340e21268efSVladimir Oltean 		/* Restore the logic from ocelot_init:
341e21268efSVladimir Oltean 		 * do not forward BPDU frames to the front ports.
342e21268efSVladimir Oltean 		 */
343e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
344e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
345e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG,
346e21268efSVladimir Oltean 				 port);
347e21268efSVladimir Oltean 	}
348e21268efSVladimir Oltean 
349e21268efSVladimir Oltean 	felix_8021q_cpu_port_deinit(ocelot, cpu);
350e21268efSVladimir Oltean }
351e21268efSVladimir Oltean 
352adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This
353adb3dccfSVladimir Oltean  * is the mode through which frames can be injected from and extracted to an
354adb3dccfSVladimir Oltean  * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU
355adb3dccfSVladimir Oltean  * running Linux, and this forms a DSA setup together with the enetc or fman
356adb3dccfSVladimir Oltean  * DSA master.
357adb3dccfSVladimir Oltean  */
358adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port)
359adb3dccfSVladimir Oltean {
360adb3dccfSVladimir Oltean 	ocelot->npi = port;
361adb3dccfSVladimir Oltean 
362adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
363adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
364adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
365adb3dccfSVladimir Oltean 
366adb3dccfSVladimir Oltean 	/* NPI port Injection/Extraction configuration */
367adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
368adb3dccfSVladimir Oltean 			    ocelot->npi_xtr_prefix);
369adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
370adb3dccfSVladimir Oltean 			    ocelot->npi_inj_prefix);
371adb3dccfSVladimir Oltean 
372adb3dccfSVladimir Oltean 	/* Disable transmission of pause frames */
373adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
374adb3dccfSVladimir Oltean }
375adb3dccfSVladimir Oltean 
376adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port)
377adb3dccfSVladimir Oltean {
378adb3dccfSVladimir Oltean 	/* Restore hardware defaults */
379adb3dccfSVladimir Oltean 	int unused_port = ocelot->num_phys_ports + 2;
380adb3dccfSVladimir Oltean 
381adb3dccfSVladimir Oltean 	ocelot->npi = -1;
382adb3dccfSVladimir Oltean 
383adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port),
384adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
385adb3dccfSVladimir Oltean 
386adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
387adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
388adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
389adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
390adb3dccfSVladimir Oltean 
391adb3dccfSVladimir Oltean 	/* Enable transmission of pause frames */
392adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
393adb3dccfSVladimir Oltean }
394adb3dccfSVladimir Oltean 
395adb3dccfSVladimir Oltean static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu)
396adb3dccfSVladimir Oltean {
397adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
398adb3dccfSVladimir Oltean 	unsigned long cpu_flood;
399adb3dccfSVladimir Oltean 
400adb3dccfSVladimir Oltean 	felix_npi_port_init(ocelot, cpu);
401adb3dccfSVladimir Oltean 
402adb3dccfSVladimir Oltean 	/* Include the CPU port module (and indirectly, the NPI port)
403adb3dccfSVladimir Oltean 	 * in the forwarding mask for unknown unicast - the hardware
404adb3dccfSVladimir Oltean 	 * default value for ANA_FLOODING_FLD_UNICAST excludes
405adb3dccfSVladimir Oltean 	 * BIT(ocelot->num_phys_ports), and so does ocelot_init,
406adb3dccfSVladimir Oltean 	 * since Ocelot relies on whitelisting MAC addresses towards
407adb3dccfSVladimir Oltean 	 * PGID_CPU.
408adb3dccfSVladimir Oltean 	 * We do this because DSA does not yet perform RX filtering,
409adb3dccfSVladimir Oltean 	 * and the NPI port does not perform source address learning,
410adb3dccfSVladimir Oltean 	 * so traffic sent to Linux is effectively unknown from the
411adb3dccfSVladimir Oltean 	 * switch's perspective.
412adb3dccfSVladimir Oltean 	 */
413adb3dccfSVladimir Oltean 	cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
414adb3dccfSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_UC);
4156edb9e8dSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_MC);
416b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_BC);
417adb3dccfSVladimir Oltean 
418adb3dccfSVladimir Oltean 	return 0;
419adb3dccfSVladimir Oltean }
420adb3dccfSVladimir Oltean 
421adb3dccfSVladimir Oltean static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu)
422adb3dccfSVladimir Oltean {
423adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
424adb3dccfSVladimir Oltean 
425adb3dccfSVladimir Oltean 	felix_npi_port_deinit(ocelot, cpu);
426adb3dccfSVladimir Oltean }
427adb3dccfSVladimir Oltean 
428adb3dccfSVladimir Oltean static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu,
429adb3dccfSVladimir Oltean 				  enum dsa_tag_protocol proto)
430adb3dccfSVladimir Oltean {
431adb3dccfSVladimir Oltean 	int err;
432adb3dccfSVladimir Oltean 
433adb3dccfSVladimir Oltean 	switch (proto) {
434adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
435adb3dccfSVladimir Oltean 		err = felix_setup_tag_npi(ds, cpu);
436adb3dccfSVladimir Oltean 		break;
437e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
438e21268efSVladimir Oltean 		err = felix_setup_tag_8021q(ds, cpu);
439e21268efSVladimir Oltean 		break;
440adb3dccfSVladimir Oltean 	default:
441adb3dccfSVladimir Oltean 		err = -EPROTONOSUPPORT;
442adb3dccfSVladimir Oltean 	}
443adb3dccfSVladimir Oltean 
444adb3dccfSVladimir Oltean 	return err;
445adb3dccfSVladimir Oltean }
446adb3dccfSVladimir Oltean 
447adb3dccfSVladimir Oltean static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu,
448adb3dccfSVladimir Oltean 				   enum dsa_tag_protocol proto)
449adb3dccfSVladimir Oltean {
450adb3dccfSVladimir Oltean 	switch (proto) {
451adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
452adb3dccfSVladimir Oltean 		felix_teardown_tag_npi(ds, cpu);
453adb3dccfSVladimir Oltean 		break;
454e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
455e21268efSVladimir Oltean 		felix_teardown_tag_8021q(ds, cpu);
456e21268efSVladimir Oltean 		break;
457adb3dccfSVladimir Oltean 	default:
458adb3dccfSVladimir Oltean 		break;
459adb3dccfSVladimir Oltean 	}
460adb3dccfSVladimir Oltean }
461adb3dccfSVladimir Oltean 
462e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the
463e21268efSVladimir Oltean  * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
464e21268efSVladimir Oltean  * or the restoration is guaranteed to work.
465e21268efSVladimir Oltean  */
466adb3dccfSVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu,
467adb3dccfSVladimir Oltean 				     enum dsa_tag_protocol proto)
468adb3dccfSVladimir Oltean {
469adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
470adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
471adb3dccfSVladimir Oltean 	enum dsa_tag_protocol old_proto = felix->tag_proto;
472adb3dccfSVladimir Oltean 	int err;
473adb3dccfSVladimir Oltean 
474e21268efSVladimir Oltean 	if (proto != DSA_TAG_PROTO_OCELOT &&
475e21268efSVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT_8021Q)
476adb3dccfSVladimir Oltean 		return -EPROTONOSUPPORT;
477adb3dccfSVladimir Oltean 
478adb3dccfSVladimir Oltean 	felix_del_tag_protocol(ds, cpu, old_proto);
479adb3dccfSVladimir Oltean 
480adb3dccfSVladimir Oltean 	err = felix_set_tag_protocol(ds, cpu, proto);
481adb3dccfSVladimir Oltean 	if (err) {
482adb3dccfSVladimir Oltean 		felix_set_tag_protocol(ds, cpu, old_proto);
483adb3dccfSVladimir Oltean 		return err;
484adb3dccfSVladimir Oltean 	}
485adb3dccfSVladimir Oltean 
486adb3dccfSVladimir Oltean 	felix->tag_proto = proto;
487adb3dccfSVladimir Oltean 
488adb3dccfSVladimir Oltean 	return 0;
489adb3dccfSVladimir Oltean }
490adb3dccfSVladimir Oltean 
49156051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
4924d776482SFlorian Fainelli 						    int port,
4934d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
49456051948SVladimir Oltean {
495adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
496adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
497adb3dccfSVladimir Oltean 
498adb3dccfSVladimir Oltean 	return felix->tag_proto;
49956051948SVladimir Oltean }
50056051948SVladimir Oltean 
50156051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
50256051948SVladimir Oltean 				 unsigned int ageing_time)
50356051948SVladimir Oltean {
50456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
50556051948SVladimir Oltean 
50656051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
50756051948SVladimir Oltean 
50856051948SVladimir Oltean 	return 0;
50956051948SVladimir Oltean }
51056051948SVladimir Oltean 
51156051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
51256051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
51356051948SVladimir Oltean {
51456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
51556051948SVladimir Oltean 
51656051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
51756051948SVladimir Oltean }
51856051948SVladimir Oltean 
51956051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
52056051948SVladimir Oltean 			 const unsigned char *addr, u16 vid)
52156051948SVladimir Oltean {
52256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
52356051948SVladimir Oltean 
52487b0f983SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid);
52556051948SVladimir Oltean }
52656051948SVladimir Oltean 
52756051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
52856051948SVladimir Oltean 			 const unsigned char *addr, u16 vid)
52956051948SVladimir Oltean {
53056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
53156051948SVladimir Oltean 
53256051948SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid);
53356051948SVladimir Oltean }
53456051948SVladimir Oltean 
535a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port,
536209edf95SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb)
537209edf95SVladimir Oltean {
538209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
539209edf95SVladimir Oltean 
540a52b2da7SVladimir Oltean 	return ocelot_port_mdb_add(ocelot, port, mdb);
541209edf95SVladimir Oltean }
542209edf95SVladimir Oltean 
543209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port,
544209edf95SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb)
545209edf95SVladimir Oltean {
546209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
547209edf95SVladimir Oltean 
548209edf95SVladimir Oltean 	return ocelot_port_mdb_del(ocelot, port, mdb);
549209edf95SVladimir Oltean }
550209edf95SVladimir Oltean 
55156051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
55256051948SVladimir Oltean 				       u8 state)
55356051948SVladimir Oltean {
55456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
55556051948SVladimir Oltean 
55656051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
55756051948SVladimir Oltean }
55856051948SVladimir Oltean 
559421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port,
560421741eaSVladimir Oltean 				  struct switchdev_brport_flags val,
561421741eaSVladimir Oltean 				  struct netlink_ext_ack *extack)
562421741eaSVladimir Oltean {
563421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
564421741eaSVladimir Oltean 
565421741eaSVladimir Oltean 	return ocelot_port_pre_bridge_flags(ocelot, port, val);
566421741eaSVladimir Oltean }
567421741eaSVladimir Oltean 
568421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port,
569421741eaSVladimir Oltean 			      struct switchdev_brport_flags val,
570421741eaSVladimir Oltean 			      struct netlink_ext_ack *extack)
571421741eaSVladimir Oltean {
572421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
573421741eaSVladimir Oltean 
574421741eaSVladimir Oltean 	ocelot_port_bridge_flags(ocelot, port, val);
575421741eaSVladimir Oltean 
576421741eaSVladimir Oltean 	return 0;
577421741eaSVladimir Oltean }
578421741eaSVladimir Oltean 
57956051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
58056051948SVladimir Oltean 			     struct net_device *br)
58156051948SVladimir Oltean {
58256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
58356051948SVladimir Oltean 
58456051948SVladimir Oltean 	return ocelot_port_bridge_join(ocelot, port, br);
58556051948SVladimir Oltean }
58656051948SVladimir Oltean 
58756051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
58856051948SVladimir Oltean 			       struct net_device *br)
58956051948SVladimir Oltean {
59056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
59156051948SVladimir Oltean 
59256051948SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, br);
59356051948SVladimir Oltean }
59456051948SVladimir Oltean 
5958fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port,
5968fe6832eSVladimir Oltean 			  struct net_device *bond,
5978fe6832eSVladimir Oltean 			  struct netdev_lag_upper_info *info)
5988fe6832eSVladimir Oltean {
5998fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
6008fe6832eSVladimir Oltean 
6018fe6832eSVladimir Oltean 	return ocelot_port_lag_join(ocelot, port, bond, info);
6028fe6832eSVladimir Oltean }
6038fe6832eSVladimir Oltean 
6048fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port,
6058fe6832eSVladimir Oltean 			   struct net_device *bond)
6068fe6832eSVladimir Oltean {
6078fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
6088fe6832eSVladimir Oltean 
6098fe6832eSVladimir Oltean 	ocelot_port_lag_leave(ocelot, port, bond);
6108fe6832eSVladimir Oltean 
6118fe6832eSVladimir Oltean 	return 0;
6128fe6832eSVladimir Oltean }
6138fe6832eSVladimir Oltean 
6148fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port)
6158fe6832eSVladimir Oltean {
6168fe6832eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
6178fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
6188fe6832eSVladimir Oltean 
6198fe6832eSVladimir Oltean 	ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
6208fe6832eSVladimir Oltean 
6218fe6832eSVladimir Oltean 	return 0;
6228fe6832eSVladimir Oltean }
6238fe6832eSVladimir Oltean 
62456051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
62556051948SVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan)
62656051948SVladimir Oltean {
6272f0402feSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
628b7a9e0daSVladimir Oltean 	u16 flags = vlan->flags;
6292f0402feSVladimir Oltean 
6309a720680SVladimir Oltean 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
6319a720680SVladimir Oltean 	 * egress-untagged or not, pvid or not, make no difference. This
6329a720680SVladimir Oltean 	 * behavior is already better than what DSA just tries to approximate
6339a720680SVladimir Oltean 	 * when it installs the VLAN with the same flags on the CPU port.
6349a720680SVladimir Oltean 	 * Just accept any configuration, and don't let ocelot deny installing
6359a720680SVladimir Oltean 	 * multiple native VLANs on the NPI port, because the switch doesn't
6369a720680SVladimir Oltean 	 * look at the port tag settings towards the NPI interface anyway.
6379a720680SVladimir Oltean 	 */
6389a720680SVladimir Oltean 	if (port == ocelot->npi)
6399a720680SVladimir Oltean 		return 0;
6409a720680SVladimir Oltean 
641b7a9e0daSVladimir Oltean 	return ocelot_vlan_prepare(ocelot, port, vlan->vid,
6422f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_PVID,
6432f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_UNTAGGED);
64456051948SVladimir Oltean }
64556051948SVladimir Oltean 
646bae33f2bSVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled)
64756051948SVladimir Oltean {
64856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
64956051948SVladimir Oltean 
650bae33f2bSVladimir Oltean 	return ocelot_port_vlan_filtering(ocelot, port, enabled);
65156051948SVladimir Oltean }
65256051948SVladimir Oltean 
6531958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port,
65456051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
65556051948SVladimir Oltean {
65656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
657183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
6581958d581SVladimir Oltean 	int err;
65956051948SVladimir Oltean 
6601958d581SVladimir Oltean 	err = felix_vlan_prepare(ds, port, vlan);
6611958d581SVladimir Oltean 	if (err)
6621958d581SVladimir Oltean 		return err;
6631958d581SVladimir Oltean 
6641958d581SVladimir Oltean 	return ocelot_vlan_add(ocelot, port, vlan->vid,
665183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_PVID,
666183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_UNTAGGED);
66756051948SVladimir Oltean }
66856051948SVladimir Oltean 
66956051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
67056051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
67156051948SVladimir Oltean {
67256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
67356051948SVladimir Oltean 
674b7a9e0daSVladimir Oltean 	return ocelot_vlan_del(ocelot, port, vlan->vid);
67556051948SVladimir Oltean }
67656051948SVladimir Oltean 
67756051948SVladimir Oltean static int felix_port_enable(struct dsa_switch *ds, int port,
67856051948SVladimir Oltean 			     struct phy_device *phy)
67956051948SVladimir Oltean {
68056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
68156051948SVladimir Oltean 
68256051948SVladimir Oltean 	ocelot_port_enable(ocelot, port, phy);
68356051948SVladimir Oltean 
68456051948SVladimir Oltean 	return 0;
68556051948SVladimir Oltean }
68656051948SVladimir Oltean 
68756051948SVladimir Oltean static void felix_port_disable(struct dsa_switch *ds, int port)
68856051948SVladimir Oltean {
68956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
69056051948SVladimir Oltean 
69156051948SVladimir Oltean 	return ocelot_port_disable(ocelot, port);
69256051948SVladimir Oltean }
69356051948SVladimir Oltean 
694bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
695bdeced75SVladimir Oltean 				   unsigned long *supported,
696bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
697bdeced75SVladimir Oltean {
698bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
699375e1314SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
700bdeced75SVladimir Oltean 
701375e1314SVladimir Oltean 	if (felix->info->phylink_validate)
702375e1314SVladimir Oltean 		felix->info->phylink_validate(ocelot, port, supported, state);
703bdeced75SVladimir Oltean }
704bdeced75SVladimir Oltean 
705bdeced75SVladimir Oltean static void felix_phylink_mac_config(struct dsa_switch *ds, int port,
706bdeced75SVladimir Oltean 				     unsigned int link_an_mode,
707bdeced75SVladimir Oltean 				     const struct phylink_link_state *state)
708bdeced75SVladimir Oltean {
709bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
710bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
711588d0550SIoana Ciornei 	struct dsa_port *dp = dsa_to_port(ds, port);
712bdeced75SVladimir Oltean 
713588d0550SIoana Ciornei 	if (felix->pcs[port])
714588d0550SIoana Ciornei 		phylink_set_pcs(dp->pl, &felix->pcs[port]->pcs);
715bdeced75SVladimir Oltean }
716bdeced75SVladimir Oltean 
717bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
718bdeced75SVladimir Oltean 					unsigned int link_an_mode,
719bdeced75SVladimir Oltean 					phy_interface_t interface)
720bdeced75SVladimir Oltean {
721bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
722bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
723eb4733d7SVladimir Oltean 	int err;
724bdeced75SVladimir Oltean 
725eb4733d7SVladimir Oltean 	ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA,
726eb4733d7SVladimir Oltean 			 DEV_MAC_ENA_CFG);
727eb4733d7SVladimir Oltean 
728886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
729eb4733d7SVladimir Oltean 
730eb4733d7SVladimir Oltean 	err = ocelot_port_flush(ocelot, port);
731eb4733d7SVladimir Oltean 	if (err)
732eb4733d7SVladimir Oltean 		dev_err(ocelot->dev, "failed to flush port %d: %d\n",
733eb4733d7SVladimir Oltean 			port, err);
734eb4733d7SVladimir Oltean 
735eb4733d7SVladimir Oltean 	/* Put the port in reset. */
736eb4733d7SVladimir Oltean 	ocelot_port_writel(ocelot_port,
737eb4733d7SVladimir Oltean 			   DEV_CLOCK_CFG_MAC_TX_RST |
738eb4733d7SVladimir Oltean 			   DEV_CLOCK_CFG_MAC_RX_RST |
739eb4733d7SVladimir Oltean 			   DEV_CLOCK_CFG_LINK_SPEED(OCELOT_SPEED_1000),
740eb4733d7SVladimir Oltean 			   DEV_CLOCK_CFG);
741bdeced75SVladimir Oltean }
742bdeced75SVladimir Oltean 
743bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
744bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
745bdeced75SVladimir Oltean 				      phy_interface_t interface,
7465b502a7bSRussell King 				      struct phy_device *phydev,
7475b502a7bSRussell King 				      int speed, int duplex,
7485b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
749bdeced75SVladimir Oltean {
750bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
751bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
7527e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
7537e14a2dcSVladimir Oltean 	u32 mac_fc_cfg;
754bdeced75SVladimir Oltean 
7557e14a2dcSVladimir Oltean 	/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
7567e14a2dcSVladimir Oltean 	 * PORT_RST bits in DEV_CLOCK_CFG. Note that the way this system is
7577e14a2dcSVladimir Oltean 	 * integrated is that the MAC speed is fixed and it's the PCS who is
7587e14a2dcSVladimir Oltean 	 * performing the rate adaptation, so we have to write "1000Mbps" into
7597e14a2dcSVladimir Oltean 	 * the LINK_SPEED field of DEV_CLOCK_CFG (which is also its default
7607e14a2dcSVladimir Oltean 	 * value).
7617e14a2dcSVladimir Oltean 	 */
7627e14a2dcSVladimir Oltean 	ocelot_port_writel(ocelot_port,
7637e14a2dcSVladimir Oltean 			   DEV_CLOCK_CFG_LINK_SPEED(OCELOT_SPEED_1000),
7647e14a2dcSVladimir Oltean 			   DEV_CLOCK_CFG);
7657e14a2dcSVladimir Oltean 
7667e14a2dcSVladimir Oltean 	switch (speed) {
7677e14a2dcSVladimir Oltean 	case SPEED_10:
7687e14a2dcSVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(3);
7697e14a2dcSVladimir Oltean 		break;
7707e14a2dcSVladimir Oltean 	case SPEED_100:
7717e14a2dcSVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(2);
7727e14a2dcSVladimir Oltean 		break;
7737e14a2dcSVladimir Oltean 	case SPEED_1000:
7747e14a2dcSVladimir Oltean 	case SPEED_2500:
7757e14a2dcSVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(1);
7767e14a2dcSVladimir Oltean 		break;
7777e14a2dcSVladimir Oltean 	default:
7787e14a2dcSVladimir Oltean 		dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
7797e14a2dcSVladimir Oltean 			port, speed);
7807e14a2dcSVladimir Oltean 		return;
7817e14a2dcSVladimir Oltean 	}
7827e14a2dcSVladimir Oltean 
7837e14a2dcSVladimir Oltean 	/* handle Rx pause in all cases, with 2500base-X this is used for rate
7847e14a2dcSVladimir Oltean 	 * adaptation.
7857e14a2dcSVladimir Oltean 	 */
7867e14a2dcSVladimir Oltean 	mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
7877e14a2dcSVladimir Oltean 
7887e14a2dcSVladimir Oltean 	if (tx_pause)
7897e14a2dcSVladimir Oltean 		mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
7907e14a2dcSVladimir Oltean 			      SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
7917e14a2dcSVladimir Oltean 			      SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
7927e14a2dcSVladimir Oltean 			      SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
7937e14a2dcSVladimir Oltean 
7947e14a2dcSVladimir Oltean 	/* Flow control. Link speed is only used here to evaluate the time
7957e14a2dcSVladimir Oltean 	 * specification in incoming pause frames.
7967e14a2dcSVladimir Oltean 	 */
7977e14a2dcSVladimir Oltean 	ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
7987e14a2dcSVladimir Oltean 
7997e14a2dcSVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
8007e14a2dcSVladimir Oltean 
8017e14a2dcSVladimir Oltean 	/* Undo the effects of felix_phylink_mac_link_down:
8027e14a2dcSVladimir Oltean 	 * enable MAC module
8037e14a2dcSVladimir Oltean 	 */
804bdeced75SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
805bdeced75SVladimir Oltean 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
806bdeced75SVladimir Oltean 
807bdeced75SVladimir Oltean 	/* Enable receiving frames on the port, and activate auto-learning of
808bdeced75SVladimir Oltean 	 * MAC addresses.
809bdeced75SVladimir Oltean 	 */
810bdeced75SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
811bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG_RECV_ENA |
812bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
813bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
814bdeced75SVladimir Oltean 
815bdeced75SVladimir Oltean 	/* Core: Enable port for frame transfer */
816886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
817886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
8187e14a2dcSVladimir Oltean 
8197e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
8207e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
821bdeced75SVladimir Oltean }
822bdeced75SVladimir Oltean 
823bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
824bd2b3161SXiaoliang Yang {
825bd2b3161SXiaoliang Yang 	int i;
826bd2b3161SXiaoliang Yang 
827bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
828bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
829bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
830bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
831bd2b3161SXiaoliang Yang 		       port);
832bd2b3161SXiaoliang Yang 
83370d39a6eSVladimir Oltean 	for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
834bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
835bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
836bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
837bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
838bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
839bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
840bd2b3161SXiaoliang Yang 			      port, i);
841bd2b3161SXiaoliang Yang 	}
842bd2b3161SXiaoliang Yang }
843bd2b3161SXiaoliang Yang 
84456051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
84556051948SVladimir Oltean 			      u32 stringset, u8 *data)
84656051948SVladimir Oltean {
84756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
84856051948SVladimir Oltean 
84956051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
85056051948SVladimir Oltean }
85156051948SVladimir Oltean 
85256051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
85356051948SVladimir Oltean {
85456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
85556051948SVladimir Oltean 
85656051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
85756051948SVladimir Oltean }
85856051948SVladimir Oltean 
85956051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
86056051948SVladimir Oltean {
86156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
86256051948SVladimir Oltean 
86356051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
86456051948SVladimir Oltean }
86556051948SVladimir Oltean 
86656051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
86756051948SVladimir Oltean 			     struct ethtool_ts_info *info)
86856051948SVladimir Oltean {
86956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
87056051948SVladimir Oltean 
87156051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
87256051948SVladimir Oltean }
87356051948SVladimir Oltean 
874bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
875bdeced75SVladimir Oltean 				  struct device_node *ports_node,
876bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
877bdeced75SVladimir Oltean {
878bdeced75SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
879bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
880bdeced75SVladimir Oltean 	struct device_node *child;
881bdeced75SVladimir Oltean 
88237fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
883bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
884bdeced75SVladimir Oltean 		u32 port;
885bdeced75SVladimir Oltean 		int err;
886bdeced75SVladimir Oltean 
887bdeced75SVladimir Oltean 		/* Get switch port number from DT */
888bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
889bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
890bdeced75SVladimir Oltean 				"(property \"reg\")\n");
891bdeced75SVladimir Oltean 			of_node_put(child);
892bdeced75SVladimir Oltean 			return -ENODEV;
893bdeced75SVladimir Oltean 		}
894bdeced75SVladimir Oltean 
895bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
896bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
897bdeced75SVladimir Oltean 		if (err) {
898bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
899bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
900bdeced75SVladimir Oltean 				port);
901bdeced75SVladimir Oltean 			of_node_put(child);
902bdeced75SVladimir Oltean 			return -ENODEV;
903bdeced75SVladimir Oltean 		}
904bdeced75SVladimir Oltean 
905bdeced75SVladimir Oltean 		err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode);
906bdeced75SVladimir Oltean 		if (err < 0) {
907bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
908bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
90959ebb430SSumera Priyadarsini 			of_node_put(child);
910bdeced75SVladimir Oltean 			return err;
911bdeced75SVladimir Oltean 		}
912bdeced75SVladimir Oltean 
913bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
914bdeced75SVladimir Oltean 	}
915bdeced75SVladimir Oltean 
916bdeced75SVladimir Oltean 	return 0;
917bdeced75SVladimir Oltean }
918bdeced75SVladimir Oltean 
919bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
920bdeced75SVladimir Oltean {
921bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
922bdeced75SVladimir Oltean 	struct device_node *switch_node;
923bdeced75SVladimir Oltean 	struct device_node *ports_node;
924bdeced75SVladimir Oltean 	int err;
925bdeced75SVladimir Oltean 
926bdeced75SVladimir Oltean 	switch_node = dev->of_node;
927bdeced75SVladimir Oltean 
928bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
929bdeced75SVladimir Oltean 	if (!ports_node) {
930bdeced75SVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
931bdeced75SVladimir Oltean 		return -ENODEV;
932bdeced75SVladimir Oltean 	}
933bdeced75SVladimir Oltean 
934bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
935bdeced75SVladimir Oltean 	of_node_put(ports_node);
936bdeced75SVladimir Oltean 
937bdeced75SVladimir Oltean 	return err;
938bdeced75SVladimir Oltean }
939bdeced75SVladimir Oltean 
94056051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
94156051948SVladimir Oltean {
94256051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
943bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
944b4024c9eSClaudiu Manoil 	struct resource res;
94556051948SVladimir Oltean 	int port, i, err;
94656051948SVladimir Oltean 
94756051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
94856051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
94956051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
95056051948SVladimir Oltean 	if (!ocelot->ports)
95156051948SVladimir Oltean 		return -ENOMEM;
95256051948SVladimir Oltean 
95356051948SVladimir Oltean 	ocelot->map		= felix->info->map;
95456051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
95556051948SVladimir Oltean 	ocelot->num_stats	= felix->info->num_stats;
95621ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
95707d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
95856051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
959cacea62fSVladimir Oltean 	ocelot->npi_inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
960cacea62fSVladimir Oltean 	ocelot->npi_xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
961f59fd9caSVladimir Oltean 	ocelot->devlink		= felix->ds->devlink;
96256051948SVladimir Oltean 
963bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
964bdeced75SVladimir Oltean 				 GFP_KERNEL);
965bdeced75SVladimir Oltean 	if (!port_phy_modes)
966bdeced75SVladimir Oltean 		return -ENOMEM;
967bdeced75SVladimir Oltean 
968bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
969bdeced75SVladimir Oltean 	if (err) {
970bdeced75SVladimir Oltean 		kfree(port_phy_modes);
971bdeced75SVladimir Oltean 		return err;
972bdeced75SVladimir Oltean 	}
973bdeced75SVladimir Oltean 
97456051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
97556051948SVladimir Oltean 		struct regmap *target;
97656051948SVladimir Oltean 
97756051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
97856051948SVladimir Oltean 			continue;
97956051948SVladimir Oltean 
980b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
981b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
982375e1314SVladimir Oltean 		res.start += felix->switch_base;
983375e1314SVladimir Oltean 		res.end += felix->switch_base;
98456051948SVladimir Oltean 
985b4024c9eSClaudiu Manoil 		target = ocelot_regmap_init(ocelot, &res);
98656051948SVladimir Oltean 		if (IS_ERR(target)) {
98756051948SVladimir Oltean 			dev_err(ocelot->dev,
98856051948SVladimir Oltean 				"Failed to map device memory space\n");
989bdeced75SVladimir Oltean 			kfree(port_phy_modes);
99056051948SVladimir Oltean 			return PTR_ERR(target);
99156051948SVladimir Oltean 		}
99256051948SVladimir Oltean 
99356051948SVladimir Oltean 		ocelot->targets[i] = target;
99456051948SVladimir Oltean 	}
99556051948SVladimir Oltean 
99656051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
99756051948SVladimir Oltean 	if (err) {
99856051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
999bdeced75SVladimir Oltean 		kfree(port_phy_modes);
100056051948SVladimir Oltean 		return err;
100156051948SVladimir Oltean 	}
100256051948SVladimir Oltean 
100356051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
100456051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
100591c724cfSVladimir Oltean 		struct regmap *target;
100667c24049SVladimir Oltean 		u8 *template;
100756051948SVladimir Oltean 
100856051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
100956051948SVladimir Oltean 					   sizeof(struct ocelot_port),
101056051948SVladimir Oltean 					   GFP_KERNEL);
101156051948SVladimir Oltean 		if (!ocelot_port) {
101256051948SVladimir Oltean 			dev_err(ocelot->dev,
101356051948SVladimir Oltean 				"failed to allocate port memory\n");
1014bdeced75SVladimir Oltean 			kfree(port_phy_modes);
101556051948SVladimir Oltean 			return -ENOMEM;
101656051948SVladimir Oltean 		}
101756051948SVladimir Oltean 
1018b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
1019b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1020375e1314SVladimir Oltean 		res.start += felix->switch_base;
1021375e1314SVladimir Oltean 		res.end += felix->switch_base;
102256051948SVladimir Oltean 
102391c724cfSVladimir Oltean 		target = ocelot_regmap_init(ocelot, &res);
102491c724cfSVladimir Oltean 		if (IS_ERR(target)) {
102556051948SVladimir Oltean 			dev_err(ocelot->dev,
102691c724cfSVladimir Oltean 				"Failed to map memory space for port %d\n",
102791c724cfSVladimir Oltean 				port);
1028bdeced75SVladimir Oltean 			kfree(port_phy_modes);
102991c724cfSVladimir Oltean 			return PTR_ERR(target);
103056051948SVladimir Oltean 		}
103156051948SVladimir Oltean 
10325124197cSVladimir Oltean 		template = devm_kzalloc(ocelot->dev, OCELOT_TOTAL_TAG_LEN,
103367c24049SVladimir Oltean 					GFP_KERNEL);
103467c24049SVladimir Oltean 		if (!template) {
103567c24049SVladimir Oltean 			dev_err(ocelot->dev,
103667c24049SVladimir Oltean 				"Failed to allocate memory for DSA tag\n");
103767c24049SVladimir Oltean 			kfree(port_phy_modes);
103867c24049SVladimir Oltean 			return -ENOMEM;
103967c24049SVladimir Oltean 		}
104067c24049SVladimir Oltean 
1041bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
104256051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
104391c724cfSVladimir Oltean 		ocelot_port->target = target;
104467c24049SVladimir Oltean 		ocelot_port->xmit_template = template;
104556051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
104667c24049SVladimir Oltean 
104767c24049SVladimir Oltean 		felix->info->xmit_template_populate(ocelot, port);
104856051948SVladimir Oltean 	}
104956051948SVladimir Oltean 
1050bdeced75SVladimir Oltean 	kfree(port_phy_modes);
1051bdeced75SVladimir Oltean 
1052bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
1053bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
1054bdeced75SVladimir Oltean 		if (err < 0)
1055bdeced75SVladimir Oltean 			return err;
1056bdeced75SVladimir Oltean 	}
1057bdeced75SVladimir Oltean 
105856051948SVladimir Oltean 	return 0;
105956051948SVladimir Oltean }
106056051948SVladimir Oltean 
106156051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
106256051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
106356051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
106456051948SVladimir Oltean  * (which will not work).
106556051948SVladimir Oltean  */
106656051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
106756051948SVladimir Oltean {
106856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
106956051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
107056051948SVladimir Oltean 	int port, err;
107156051948SVladimir Oltean 
107256051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
107356051948SVladimir Oltean 	if (err)
107456051948SVladimir Oltean 		return err;
107556051948SVladimir Oltean 
1076d1cc0e93SVladimir Oltean 	err = ocelot_init(ocelot);
1077d1cc0e93SVladimir Oltean 	if (err)
1078d1cc0e93SVladimir Oltean 		return err;
1079d1cc0e93SVladimir Oltean 
10802b49d128SYangbo Lu 	if (ocelot->ptp) {
10812ac7c6c5SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
10822b49d128SYangbo Lu 		if (err) {
10832b49d128SYangbo Lu 			dev_err(ocelot->dev,
10842b49d128SYangbo Lu 				"Timestamp initialization failed\n");
10852b49d128SYangbo Lu 			ocelot->ptp = 0;
10862b49d128SYangbo Lu 		}
10872b49d128SYangbo Lu 	}
108856051948SVladimir Oltean 
108956051948SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
1090adb3dccfSVladimir Oltean 		if (dsa_is_unused_port(ds, port))
1091adb3dccfSVladimir Oltean 			continue;
109256051948SVladimir Oltean 
1093adb3dccfSVladimir Oltean 		ocelot_init_port(ocelot, port);
1094bd2b3161SXiaoliang Yang 
1095bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
1096bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
1097bd2b3161SXiaoliang Yang 		 */
1098bd2b3161SXiaoliang Yang 		felix_port_qos_map_init(ocelot, port);
109956051948SVladimir Oltean 	}
110056051948SVladimir Oltean 
1101f59fd9caSVladimir Oltean 	err = ocelot_devlink_sb_register(ocelot);
1102f59fd9caSVladimir Oltean 	if (err)
1103f59fd9caSVladimir Oltean 		return err;
1104f59fd9caSVladimir Oltean 
1105adb3dccfSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
1106adb3dccfSVladimir Oltean 		if (!dsa_is_cpu_port(ds, port))
1107adb3dccfSVladimir Oltean 			continue;
1108adb3dccfSVladimir Oltean 
1109adb3dccfSVladimir Oltean 		/* The initial tag protocol is NPI which always returns 0, so
1110adb3dccfSVladimir Oltean 		 * there's no real point in checking for errors.
11111cf3299bSVladimir Oltean 		 */
1112adb3dccfSVladimir Oltean 		felix_set_tag_protocol(ds, port, felix->tag_proto);
1113adb3dccfSVladimir Oltean 	}
11141cf3299bSVladimir Oltean 
11150b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
1116c54913c1SVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
1117bdeced75SVladimir Oltean 
111856051948SVladimir Oltean 	return 0;
111956051948SVladimir Oltean }
112056051948SVladimir Oltean 
112156051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
112256051948SVladimir Oltean {
112356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1124bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1125e5fb512dSVladimir Oltean 	int port;
1126bdeced75SVladimir Oltean 
1127adb3dccfSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
1128adb3dccfSVladimir Oltean 		if (!dsa_is_cpu_port(ds, port))
1129adb3dccfSVladimir Oltean 			continue;
1130adb3dccfSVladimir Oltean 
1131adb3dccfSVladimir Oltean 		felix_del_tag_protocol(ds, port, felix->tag_proto);
1132adb3dccfSVladimir Oltean 	}
1133adb3dccfSVladimir Oltean 
1134f59fd9caSVladimir Oltean 	ocelot_devlink_sb_unregister(ocelot);
1135d19741b0SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
1136d19741b0SVladimir Oltean 	ocelot_deinit(ocelot);
113756051948SVladimir Oltean 
1138e5fb512dSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++)
1139e5fb512dSVladimir Oltean 		ocelot_deinit_port(ocelot, port);
1140d19741b0SVladimir Oltean 
1141d19741b0SVladimir Oltean 	if (felix->info->mdio_bus_free)
1142d19741b0SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
114356051948SVladimir Oltean }
114456051948SVladimir Oltean 
1145c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
1146c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1147c0bcf537SYangbo Lu {
1148c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1149c0bcf537SYangbo Lu 
1150c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
1151c0bcf537SYangbo Lu }
1152c0bcf537SYangbo Lu 
1153c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
1154c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1155c0bcf537SYangbo Lu {
1156c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1157c0bcf537SYangbo Lu 
1158c0bcf537SYangbo Lu 	return ocelot_hwstamp_set(ocelot, port, ifr);
1159c0bcf537SYangbo Lu }
1160c0bcf537SYangbo Lu 
1161c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
1162c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
1163c0bcf537SYangbo Lu {
1164*40d3f295SVladimir Oltean 	u8 *extraction = skb->data - ETH_HLEN - OCELOT_TAG_LEN;
1165c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
1166c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1167c0bcf537SYangbo Lu 	u32 tstamp_lo, tstamp_hi;
1168c0bcf537SYangbo Lu 	struct timespec64 ts;
1169c0bcf537SYangbo Lu 	u64 tstamp, val;
1170c0bcf537SYangbo Lu 
1171c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1172c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1173c0bcf537SYangbo Lu 
1174*40d3f295SVladimir Oltean 	ocelot_xfh_get_rew_val(extraction, &val);
1175c0bcf537SYangbo Lu 	tstamp_lo = (u32)val;
1176c0bcf537SYangbo Lu 
1177c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
1178c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
1179c0bcf537SYangbo Lu 		tstamp_hi--;
1180c0bcf537SYangbo Lu 
1181c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
1182c0bcf537SYangbo Lu 
1183c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
1184c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1185c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
1186c0bcf537SYangbo Lu 	return false;
1187c0bcf537SYangbo Lu }
1188c0bcf537SYangbo Lu 
11893243e04aSChen Wandun static bool felix_txtstamp(struct dsa_switch *ds, int port,
1190c0bcf537SYangbo Lu 			   struct sk_buff *clone, unsigned int type)
1191c0bcf537SYangbo Lu {
1192c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1193c0bcf537SYangbo Lu 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1194c0bcf537SYangbo Lu 
1195e2f9a8feSVladimir Oltean 	if (ocelot->ptp && (skb_shinfo(clone)->tx_flags & SKBTX_HW_TSTAMP) &&
1196e2f9a8feSVladimir Oltean 	    ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
1197e2f9a8feSVladimir Oltean 		ocelot_port_add_txtstamp_skb(ocelot, port, clone);
1198c0bcf537SYangbo Lu 		return true;
1199e2f9a8feSVladimir Oltean 	}
1200c0bcf537SYangbo Lu 
1201c0bcf537SYangbo Lu 	return false;
1202c0bcf537SYangbo Lu }
1203c0bcf537SYangbo Lu 
12040b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
12050b912fc9SVladimir Oltean {
12060b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
12070b912fc9SVladimir Oltean 
12080b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
12090b912fc9SVladimir Oltean 
12100b912fc9SVladimir Oltean 	return 0;
12110b912fc9SVladimir Oltean }
12120b912fc9SVladimir Oltean 
12130b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
12140b912fc9SVladimir Oltean {
12150b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
12160b912fc9SVladimir Oltean 
12170b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
12180b912fc9SVladimir Oltean }
12190b912fc9SVladimir Oltean 
122007d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
122107d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
122207d985eeSVladimir Oltean {
122307d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
122407d985eeSVladimir Oltean 
122507d985eeSVladimir Oltean 	return ocelot_cls_flower_replace(ocelot, port, cls, ingress);
122607d985eeSVladimir Oltean }
122707d985eeSVladimir Oltean 
122807d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
122907d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
123007d985eeSVladimir Oltean {
123107d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
123207d985eeSVladimir Oltean 
123307d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
123407d985eeSVladimir Oltean }
123507d985eeSVladimir Oltean 
123607d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
123707d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
123807d985eeSVladimir Oltean {
123907d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
124007d985eeSVladimir Oltean 
124107d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
124207d985eeSVladimir Oltean }
124307d985eeSVladimir Oltean 
1244fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
1245fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
1246fc411eaaSVladimir Oltean {
1247fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1248fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
1249fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
12505f035af7SPo Liu 		.burst = policer->burst,
1251fc411eaaSVladimir Oltean 	};
1252fc411eaaSVladimir Oltean 
1253fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
1254fc411eaaSVladimir Oltean }
1255fc411eaaSVladimir Oltean 
1256fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
1257fc411eaaSVladimir Oltean {
1258fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1259fc411eaaSVladimir Oltean 
1260fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
1261fc411eaaSVladimir Oltean }
1262fc411eaaSVladimir Oltean 
1263de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
1264de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
1265de143c0eSXiaoliang Yang 			       void *type_data)
1266de143c0eSXiaoliang Yang {
1267de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
1268de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
1269de143c0eSXiaoliang Yang 
1270de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
1271de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
1272de143c0eSXiaoliang Yang 	else
1273de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
1274de143c0eSXiaoliang Yang }
1275de143c0eSXiaoliang Yang 
1276f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
1277f59fd9caSVladimir Oltean 			     u16 pool_index,
1278f59fd9caSVladimir Oltean 			     struct devlink_sb_pool_info *pool_info)
1279f59fd9caSVladimir Oltean {
1280f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1281f59fd9caSVladimir Oltean 
1282f59fd9caSVladimir Oltean 	return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
1283f59fd9caSVladimir Oltean }
1284f59fd9caSVladimir Oltean 
1285f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
1286f59fd9caSVladimir Oltean 			     u16 pool_index, u32 size,
1287f59fd9caSVladimir Oltean 			     enum devlink_sb_threshold_type threshold_type,
1288f59fd9caSVladimir Oltean 			     struct netlink_ext_ack *extack)
1289f59fd9caSVladimir Oltean {
1290f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1291f59fd9caSVladimir Oltean 
1292f59fd9caSVladimir Oltean 	return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
1293f59fd9caSVladimir Oltean 				  threshold_type, extack);
1294f59fd9caSVladimir Oltean }
1295f59fd9caSVladimir Oltean 
1296f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
1297f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1298f59fd9caSVladimir Oltean 				  u32 *p_threshold)
1299f59fd9caSVladimir Oltean {
1300f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1301f59fd9caSVladimir Oltean 
1302f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
1303f59fd9caSVladimir Oltean 				       p_threshold);
1304f59fd9caSVladimir Oltean }
1305f59fd9caSVladimir Oltean 
1306f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
1307f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1308f59fd9caSVladimir Oltean 				  u32 threshold, struct netlink_ext_ack *extack)
1309f59fd9caSVladimir Oltean {
1310f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1311f59fd9caSVladimir Oltean 
1312f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
1313f59fd9caSVladimir Oltean 				       threshold, extack);
1314f59fd9caSVladimir Oltean }
1315f59fd9caSVladimir Oltean 
1316f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
1317f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1318f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1319f59fd9caSVladimir Oltean 				     u16 *p_pool_index, u32 *p_threshold)
1320f59fd9caSVladimir Oltean {
1321f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1322f59fd9caSVladimir Oltean 
1323f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
1324f59fd9caSVladimir Oltean 					  pool_type, p_pool_index,
1325f59fd9caSVladimir Oltean 					  p_threshold);
1326f59fd9caSVladimir Oltean }
1327f59fd9caSVladimir Oltean 
1328f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
1329f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1330f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1331f59fd9caSVladimir Oltean 				     u16 pool_index, u32 threshold,
1332f59fd9caSVladimir Oltean 				     struct netlink_ext_ack *extack)
1333f59fd9caSVladimir Oltean {
1334f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1335f59fd9caSVladimir Oltean 
1336f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
1337f59fd9caSVladimir Oltean 					  pool_type, pool_index, threshold,
1338f59fd9caSVladimir Oltean 					  extack);
1339f59fd9caSVladimir Oltean }
1340f59fd9caSVladimir Oltean 
1341f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds,
1342f59fd9caSVladimir Oltean 				 unsigned int sb_index)
1343f59fd9caSVladimir Oltean {
1344f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1345f59fd9caSVladimir Oltean 
1346f59fd9caSVladimir Oltean 	return ocelot_sb_occ_snapshot(ocelot, sb_index);
1347f59fd9caSVladimir Oltean }
1348f59fd9caSVladimir Oltean 
1349f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds,
1350f59fd9caSVladimir Oltean 				  unsigned int sb_index)
1351f59fd9caSVladimir Oltean {
1352f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1353f59fd9caSVladimir Oltean 
1354f59fd9caSVladimir Oltean 	return ocelot_sb_occ_max_clear(ocelot, sb_index);
1355f59fd9caSVladimir Oltean }
1356f59fd9caSVladimir Oltean 
1357f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
1358f59fd9caSVladimir Oltean 				      unsigned int sb_index, u16 pool_index,
1359f59fd9caSVladimir Oltean 				      u32 *p_cur, u32 *p_max)
1360f59fd9caSVladimir Oltean {
1361f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1362f59fd9caSVladimir Oltean 
1363f59fd9caSVladimir Oltean 	return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
1364f59fd9caSVladimir Oltean 					   p_cur, p_max);
1365f59fd9caSVladimir Oltean }
1366f59fd9caSVladimir Oltean 
1367f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
1368f59fd9caSVladimir Oltean 					 unsigned int sb_index, u16 tc_index,
1369f59fd9caSVladimir Oltean 					 enum devlink_sb_pool_type pool_type,
1370f59fd9caSVladimir Oltean 					 u32 *p_cur, u32 *p_max)
1371f59fd9caSVladimir Oltean {
1372f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1373f59fd9caSVladimir Oltean 
1374f59fd9caSVladimir Oltean 	return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
1375f59fd9caSVladimir Oltean 					      pool_type, p_cur, p_max);
1376f59fd9caSVladimir Oltean }
1377f59fd9caSVladimir Oltean 
1378375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = {
137956051948SVladimir Oltean 	.get_tag_protocol		= felix_get_tag_protocol,
1380adb3dccfSVladimir Oltean 	.change_tag_protocol		= felix_change_tag_protocol,
138156051948SVladimir Oltean 	.setup				= felix_setup,
138256051948SVladimir Oltean 	.teardown			= felix_teardown,
138356051948SVladimir Oltean 	.set_ageing_time		= felix_set_ageing_time,
138456051948SVladimir Oltean 	.get_strings			= felix_get_strings,
138556051948SVladimir Oltean 	.get_ethtool_stats		= felix_get_ethtool_stats,
138656051948SVladimir Oltean 	.get_sset_count			= felix_get_sset_count,
138756051948SVladimir Oltean 	.get_ts_info			= felix_get_ts_info,
1388bdeced75SVladimir Oltean 	.phylink_validate		= felix_phylink_validate,
1389bdeced75SVladimir Oltean 	.phylink_mac_config		= felix_phylink_mac_config,
1390bdeced75SVladimir Oltean 	.phylink_mac_link_down		= felix_phylink_mac_link_down,
1391bdeced75SVladimir Oltean 	.phylink_mac_link_up		= felix_phylink_mac_link_up,
139256051948SVladimir Oltean 	.port_enable			= felix_port_enable,
139356051948SVladimir Oltean 	.port_disable			= felix_port_disable,
139456051948SVladimir Oltean 	.port_fdb_dump			= felix_fdb_dump,
139556051948SVladimir Oltean 	.port_fdb_add			= felix_fdb_add,
139656051948SVladimir Oltean 	.port_fdb_del			= felix_fdb_del,
1397209edf95SVladimir Oltean 	.port_mdb_add			= felix_mdb_add,
1398209edf95SVladimir Oltean 	.port_mdb_del			= felix_mdb_del,
1399421741eaSVladimir Oltean 	.port_pre_bridge_flags		= felix_pre_bridge_flags,
1400421741eaSVladimir Oltean 	.port_bridge_flags		= felix_bridge_flags,
140156051948SVladimir Oltean 	.port_bridge_join		= felix_bridge_join,
140256051948SVladimir Oltean 	.port_bridge_leave		= felix_bridge_leave,
14038fe6832eSVladimir Oltean 	.port_lag_join			= felix_lag_join,
14048fe6832eSVladimir Oltean 	.port_lag_leave			= felix_lag_leave,
14058fe6832eSVladimir Oltean 	.port_lag_change		= felix_lag_change,
140656051948SVladimir Oltean 	.port_stp_state_set		= felix_bridge_stp_state_set,
140756051948SVladimir Oltean 	.port_vlan_filtering		= felix_vlan_filtering,
140856051948SVladimir Oltean 	.port_vlan_add			= felix_vlan_add,
140956051948SVladimir Oltean 	.port_vlan_del			= felix_vlan_del,
1410c0bcf537SYangbo Lu 	.port_hwtstamp_get		= felix_hwtstamp_get,
1411c0bcf537SYangbo Lu 	.port_hwtstamp_set		= felix_hwtstamp_set,
1412c0bcf537SYangbo Lu 	.port_rxtstamp			= felix_rxtstamp,
1413c0bcf537SYangbo Lu 	.port_txtstamp			= felix_txtstamp,
14140b912fc9SVladimir Oltean 	.port_change_mtu		= felix_change_mtu,
14150b912fc9SVladimir Oltean 	.port_max_mtu			= felix_get_max_mtu,
1416fc411eaaSVladimir Oltean 	.port_policer_add		= felix_port_policer_add,
1417fc411eaaSVladimir Oltean 	.port_policer_del		= felix_port_policer_del,
141807d985eeSVladimir Oltean 	.cls_flower_add			= felix_cls_flower_add,
141907d985eeSVladimir Oltean 	.cls_flower_del			= felix_cls_flower_del,
142007d985eeSVladimir Oltean 	.cls_flower_stats		= felix_cls_flower_stats,
1421de143c0eSXiaoliang Yang 	.port_setup_tc			= felix_port_setup_tc,
1422f59fd9caSVladimir Oltean 	.devlink_sb_pool_get		= felix_sb_pool_get,
1423f59fd9caSVladimir Oltean 	.devlink_sb_pool_set		= felix_sb_pool_set,
1424f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_get	= felix_sb_port_pool_get,
1425f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_set	= felix_sb_port_pool_set,
1426f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_get	= felix_sb_tc_pool_bind_get,
1427f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_set	= felix_sb_tc_pool_bind_set,
1428f59fd9caSVladimir Oltean 	.devlink_sb_occ_snapshot	= felix_sb_occ_snapshot,
1429f59fd9caSVladimir Oltean 	.devlink_sb_occ_max_clear	= felix_sb_occ_max_clear,
1430f59fd9caSVladimir Oltean 	.devlink_sb_occ_port_pool_get	= felix_sb_occ_port_pool_get,
1431f59fd9caSVladimir Oltean 	.devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
143256051948SVladimir Oltean };
1433319e4dd1SVladimir Oltean 
1434319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
1435319e4dd1SVladimir Oltean {
1436319e4dd1SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1437319e4dd1SVladimir Oltean 	struct dsa_switch *ds = felix->ds;
1438319e4dd1SVladimir Oltean 
1439319e4dd1SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
1440319e4dd1SVladimir Oltean 		return NULL;
1441319e4dd1SVladimir Oltean 
1442319e4dd1SVladimir Oltean 	return dsa_to_port(ds, port)->slave;
1443319e4dd1SVladimir Oltean }
1444319e4dd1SVladimir Oltean 
1445319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev)
1446319e4dd1SVladimir Oltean {
1447319e4dd1SVladimir Oltean 	struct dsa_port *dp;
1448319e4dd1SVladimir Oltean 
1449319e4dd1SVladimir Oltean 	dp = dsa_port_from_netdev(dev);
1450319e4dd1SVladimir Oltean 	if (IS_ERR(dp))
1451319e4dd1SVladimir Oltean 		return -EINVAL;
1452319e4dd1SVladimir Oltean 
1453319e4dd1SVladimir Oltean 	return dp->index;
1454319e4dd1SVladimir Oltean }
1455