xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision 79fda660bdbba839f434cf8674ff4a20d88fbeb2)
156051948SVladimir Oltean // SPDX-License-Identifier: GPL-2.0
23c9cfb52SVladimir Oltean /* Copyright 2019-2021 NXP
3375e1314SVladimir Oltean  *
4375e1314SVladimir Oltean  * This is an umbrella module for all network switches that are
5375e1314SVladimir Oltean  * register-compatible with Ocelot and that perform I/O to their host CPU
6375e1314SVladimir Oltean  * through an NPI (Node Processor Interface) Ethernet port.
756051948SVladimir Oltean  */
856051948SVladimir Oltean #include <uapi/linux/if_bridge.h>
907d985eeSVladimir Oltean #include <soc/mscc/ocelot_vcap.h>
10bdeced75SVladimir Oltean #include <soc/mscc/ocelot_qsys.h>
11bdeced75SVladimir Oltean #include <soc/mscc/ocelot_sys.h>
12bdeced75SVladimir Oltean #include <soc/mscc/ocelot_dev.h>
13bdeced75SVladimir Oltean #include <soc/mscc/ocelot_ana.h>
142b49d128SYangbo Lu #include <soc/mscc/ocelot_ptp.h>
1556051948SVladimir Oltean #include <soc/mscc/ocelot.h>
16e21268efSVladimir Oltean #include <linux/dsa/8021q.h>
1740d3f295SVladimir Oltean #include <linux/dsa/ocelot.h>
1884705fc1SMaxim Kochetkov #include <linux/platform_device.h>
190a6f17c6SVladimir Oltean #include <linux/ptp_classify.h>
2056051948SVladimir Oltean #include <linux/module.h>
21bdeced75SVladimir Oltean #include <linux/of_net.h>
2256051948SVladimir Oltean #include <linux/pci.h>
2356051948SVladimir Oltean #include <linux/of.h>
24fc411eaaSVladimir Oltean #include <net/pkt_sched.h>
2556051948SVladimir Oltean #include <net/dsa.h>
2656051948SVladimir Oltean #include "felix.h"
2756051948SVladimir Oltean 
28e21268efSVladimir Oltean static int felix_tag_8021q_rxvlan_add(struct felix *felix, int port, u16 vid,
29e21268efSVladimir Oltean 				      bool pvid, bool untagged)
30e21268efSVladimir Oltean {
31e21268efSVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
32e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
33e21268efSVladimir Oltean 	struct dsa_switch *ds = felix->ds;
34e21268efSVladimir Oltean 	int key_length, upstream, err;
35e21268efSVladimir Oltean 
36e21268efSVladimir Oltean 	/* We don't need to install the rxvlan into the other ports' filtering
37e21268efSVladimir Oltean 	 * tables, because we're just pushing the rxvlan when sending towards
38e21268efSVladimir Oltean 	 * the CPU
39e21268efSVladimir Oltean 	 */
40e21268efSVladimir Oltean 	if (!pvid)
41e21268efSVladimir Oltean 		return 0;
42e21268efSVladimir Oltean 
43e21268efSVladimir Oltean 	key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length;
44e21268efSVladimir Oltean 	upstream = dsa_upstream_port(ds, port);
45e21268efSVladimir Oltean 
46e21268efSVladimir Oltean 	outer_tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter),
47e21268efSVladimir Oltean 				     GFP_KERNEL);
48e21268efSVladimir Oltean 	if (!outer_tagging_rule)
49e21268efSVladimir Oltean 		return -ENOMEM;
50e21268efSVladimir Oltean 
51e21268efSVladimir Oltean 	outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
52e21268efSVladimir Oltean 	outer_tagging_rule->prio = 1;
53c518afecSVladimir Oltean 	outer_tagging_rule->id.cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port);
54e21268efSVladimir Oltean 	outer_tagging_rule->id.tc_offload = false;
55e21268efSVladimir Oltean 	outer_tagging_rule->block_id = VCAP_ES0;
56e21268efSVladimir Oltean 	outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
57e21268efSVladimir Oltean 	outer_tagging_rule->lookup = 0;
58e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.value = port;
59e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0);
60e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.value = upstream;
61e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0);
62e21268efSVladimir Oltean 	outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG;
63e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD;
64e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_vid_sel = 1;
65e21268efSVladimir Oltean 	outer_tagging_rule->action.vid_a_val = vid;
66e21268efSVladimir Oltean 
67e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL);
68e21268efSVladimir Oltean 	if (err)
69e21268efSVladimir Oltean 		kfree(outer_tagging_rule);
70e21268efSVladimir Oltean 
71e21268efSVladimir Oltean 	return err;
72e21268efSVladimir Oltean }
73e21268efSVladimir Oltean 
74e21268efSVladimir Oltean static int felix_tag_8021q_txvlan_add(struct felix *felix, int port, u16 vid,
75e21268efSVladimir Oltean 				      bool pvid, bool untagged)
76e21268efSVladimir Oltean {
77e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
78e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
79e21268efSVladimir Oltean 	struct dsa_switch *ds = felix->ds;
80e21268efSVladimir Oltean 	int upstream, err;
81e21268efSVladimir Oltean 
82e21268efSVladimir Oltean 	/* tag_8021q.c assumes we are implementing this via port VLAN
83e21268efSVladimir Oltean 	 * membership, which we aren't. So we don't need to add any VCAP filter
84e21268efSVladimir Oltean 	 * for the CPU port.
85e21268efSVladimir Oltean 	 */
86e21268efSVladimir Oltean 	if (ocelot->ports[port]->is_dsa_8021q_cpu)
87e21268efSVladimir Oltean 		return 0;
88e21268efSVladimir Oltean 
89e21268efSVladimir Oltean 	untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
90e21268efSVladimir Oltean 	if (!untagging_rule)
91e21268efSVladimir Oltean 		return -ENOMEM;
92e21268efSVladimir Oltean 
93e21268efSVladimir Oltean 	redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
94e21268efSVladimir Oltean 	if (!redirect_rule) {
95e21268efSVladimir Oltean 		kfree(untagging_rule);
96e21268efSVladimir Oltean 		return -ENOMEM;
97e21268efSVladimir Oltean 	}
98e21268efSVladimir Oltean 
99e21268efSVladimir Oltean 	upstream = dsa_upstream_port(ds, port);
100e21268efSVladimir Oltean 
101e21268efSVladimir Oltean 	untagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
102e21268efSVladimir Oltean 	untagging_rule->ingress_port_mask = BIT(upstream);
103e21268efSVladimir Oltean 	untagging_rule->vlan.vid.value = vid;
104e21268efSVladimir Oltean 	untagging_rule->vlan.vid.mask = VLAN_VID_MASK;
105e21268efSVladimir Oltean 	untagging_rule->prio = 1;
106c518afecSVladimir Oltean 	untagging_rule->id.cookie = OCELOT_VCAP_IS1_TAG_8021Q_TXVLAN(ocelot, port);
107e21268efSVladimir Oltean 	untagging_rule->id.tc_offload = false;
108e21268efSVladimir Oltean 	untagging_rule->block_id = VCAP_IS1;
109e21268efSVladimir Oltean 	untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
110e21268efSVladimir Oltean 	untagging_rule->lookup = 0;
111e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt_ena = true;
112e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt = 1;
113e21268efSVladimir Oltean 	untagging_rule->action.pag_override_mask = 0xff;
114e21268efSVladimir Oltean 	untagging_rule->action.pag_val = port;
115e21268efSVladimir Oltean 
116e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL);
117e21268efSVladimir Oltean 	if (err) {
118e21268efSVladimir Oltean 		kfree(untagging_rule);
119e21268efSVladimir Oltean 		kfree(redirect_rule);
120e21268efSVladimir Oltean 		return err;
121e21268efSVladimir Oltean 	}
122e21268efSVladimir Oltean 
123e21268efSVladimir Oltean 	redirect_rule->key_type = OCELOT_VCAP_KEY_ANY;
124e21268efSVladimir Oltean 	redirect_rule->ingress_port_mask = BIT(upstream);
125e21268efSVladimir Oltean 	redirect_rule->pag = port;
126e21268efSVladimir Oltean 	redirect_rule->prio = 1;
127c518afecSVladimir Oltean 	redirect_rule->id.cookie = OCELOT_VCAP_IS2_TAG_8021Q_TXVLAN(ocelot, port);
128e21268efSVladimir Oltean 	redirect_rule->id.tc_offload = false;
129e21268efSVladimir Oltean 	redirect_rule->block_id = VCAP_IS2;
130e21268efSVladimir Oltean 	redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
131e21268efSVladimir Oltean 	redirect_rule->lookup = 0;
132e21268efSVladimir Oltean 	redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT;
133e21268efSVladimir Oltean 	redirect_rule->action.port_mask = BIT(port);
134e21268efSVladimir Oltean 
135e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL);
136e21268efSVladimir Oltean 	if (err) {
137e21268efSVladimir Oltean 		ocelot_vcap_filter_del(ocelot, untagging_rule);
138e21268efSVladimir Oltean 		kfree(redirect_rule);
139e21268efSVladimir Oltean 		return err;
140e21268efSVladimir Oltean 	}
141e21268efSVladimir Oltean 
142e21268efSVladimir Oltean 	return 0;
143e21268efSVladimir Oltean }
144e21268efSVladimir Oltean 
145e21268efSVladimir Oltean static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
146e21268efSVladimir Oltean 				    u16 flags)
147e21268efSVladimir Oltean {
148e21268efSVladimir Oltean 	bool untagged = flags & BRIDGE_VLAN_INFO_UNTAGGED;
149e21268efSVladimir Oltean 	bool pvid = flags & BRIDGE_VLAN_INFO_PVID;
150e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
151e21268efSVladimir Oltean 
152e21268efSVladimir Oltean 	if (vid_is_dsa_8021q_rxvlan(vid))
153e21268efSVladimir Oltean 		return felix_tag_8021q_rxvlan_add(ocelot_to_felix(ocelot),
154e21268efSVladimir Oltean 						  port, vid, pvid, untagged);
155e21268efSVladimir Oltean 
156e21268efSVladimir Oltean 	if (vid_is_dsa_8021q_txvlan(vid))
157e21268efSVladimir Oltean 		return felix_tag_8021q_txvlan_add(ocelot_to_felix(ocelot),
158e21268efSVladimir Oltean 						  port, vid, pvid, untagged);
159e21268efSVladimir Oltean 
160e21268efSVladimir Oltean 	return 0;
161e21268efSVladimir Oltean }
162e21268efSVladimir Oltean 
163e21268efSVladimir Oltean static int felix_tag_8021q_rxvlan_del(struct felix *felix, int port, u16 vid)
164e21268efSVladimir Oltean {
165e21268efSVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
166e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_es0;
167e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
168e21268efSVladimir Oltean 
169e21268efSVladimir Oltean 	block_vcap_es0 = &ocelot->block[VCAP_ES0];
170e21268efSVladimir Oltean 
171e21268efSVladimir Oltean 	outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0,
172e21268efSVladimir Oltean 								 port, false);
173e21268efSVladimir Oltean 	/* In rxvlan_add, we had the "if (!pvid) return 0" logic to avoid
174e21268efSVladimir Oltean 	 * installing outer tagging ES0 rules where they weren't needed.
175e21268efSVladimir Oltean 	 * But in rxvlan_del, the API doesn't give us the "flags" anymore,
176e21268efSVladimir Oltean 	 * so that forces us to be slightly sloppy here, and just assume that
177e21268efSVladimir Oltean 	 * if we didn't find an outer_tagging_rule it means that there was
178e21268efSVladimir Oltean 	 * none in the first place, i.e. rxvlan_del is called on a non-pvid
179e21268efSVladimir Oltean 	 * port. This is most probably true though.
180e21268efSVladimir Oltean 	 */
181e21268efSVladimir Oltean 	if (!outer_tagging_rule)
182e21268efSVladimir Oltean 		return 0;
183e21268efSVladimir Oltean 
184e21268efSVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, outer_tagging_rule);
185e21268efSVladimir Oltean }
186e21268efSVladimir Oltean 
187e21268efSVladimir Oltean static int felix_tag_8021q_txvlan_del(struct felix *felix, int port, u16 vid)
188e21268efSVladimir Oltean {
189e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
190e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is1;
191e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
192e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
193e21268efSVladimir Oltean 	int err;
194e21268efSVladimir Oltean 
195e21268efSVladimir Oltean 	if (ocelot->ports[port]->is_dsa_8021q_cpu)
196e21268efSVladimir Oltean 		return 0;
197e21268efSVladimir Oltean 
198e21268efSVladimir Oltean 	block_vcap_is1 = &ocelot->block[VCAP_IS1];
199e21268efSVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
200e21268efSVladimir Oltean 
201e21268efSVladimir Oltean 	untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1,
202e21268efSVladimir Oltean 							     port, false);
203e21268efSVladimir Oltean 	if (!untagging_rule)
204e21268efSVladimir Oltean 		return 0;
205e21268efSVladimir Oltean 
206e21268efSVladimir Oltean 	err = ocelot_vcap_filter_del(ocelot, untagging_rule);
207e21268efSVladimir Oltean 	if (err)
208e21268efSVladimir Oltean 		return err;
209e21268efSVladimir Oltean 
210e21268efSVladimir Oltean 	redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2,
211e21268efSVladimir Oltean 							    port, false);
212e21268efSVladimir Oltean 	if (!redirect_rule)
213e21268efSVladimir Oltean 		return 0;
214e21268efSVladimir Oltean 
215e21268efSVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, redirect_rule);
216e21268efSVladimir Oltean }
217e21268efSVladimir Oltean 
218e21268efSVladimir Oltean static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
219e21268efSVladimir Oltean {
220e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
221e21268efSVladimir Oltean 
222e21268efSVladimir Oltean 	if (vid_is_dsa_8021q_rxvlan(vid))
223e21268efSVladimir Oltean 		return felix_tag_8021q_rxvlan_del(ocelot_to_felix(ocelot),
224e21268efSVladimir Oltean 						  port, vid);
225e21268efSVladimir Oltean 
226e21268efSVladimir Oltean 	if (vid_is_dsa_8021q_txvlan(vid))
227e21268efSVladimir Oltean 		return felix_tag_8021q_txvlan_del(ocelot_to_felix(ocelot),
228e21268efSVladimir Oltean 						  port, vid);
229e21268efSVladimir Oltean 
230e21268efSVladimir Oltean 	return 0;
231e21268efSVladimir Oltean }
232e21268efSVladimir Oltean 
233e21268efSVladimir Oltean /* Alternatively to using the NPI functionality, that same hardware MAC
234e21268efSVladimir Oltean  * connected internally to the enetc or fman DSA master can be configured to
235e21268efSVladimir Oltean  * use the software-defined tag_8021q frame format. As far as the hardware is
236e21268efSVladimir Oltean  * concerned, it thinks it is a "dumb switch" - the queues of the CPU port
237e21268efSVladimir Oltean  * module are now disconnected from it, but can still be accessed through
238e21268efSVladimir Oltean  * register-based MMIO.
239e21268efSVladimir Oltean  */
240e21268efSVladimir Oltean static void felix_8021q_cpu_port_init(struct ocelot *ocelot, int port)
241e21268efSVladimir Oltean {
2428abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2438abe1970SVladimir Oltean 
244e21268efSVladimir Oltean 	ocelot->ports[port]->is_dsa_8021q_cpu = true;
245e21268efSVladimir Oltean 	ocelot->npi = -1;
246e21268efSVladimir Oltean 
247e21268efSVladimir Oltean 	/* Overwrite PGID_CPU with the non-tagging port */
248e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, PGID_CPU);
249e21268efSVladimir Oltean 
2508abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
2518abe1970SVladimir Oltean 
2528abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
253e21268efSVladimir Oltean }
254e21268efSVladimir Oltean 
255e21268efSVladimir Oltean static void felix_8021q_cpu_port_deinit(struct ocelot *ocelot, int port)
256e21268efSVladimir Oltean {
2578abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2588abe1970SVladimir Oltean 
259e21268efSVladimir Oltean 	ocelot->ports[port]->is_dsa_8021q_cpu = false;
260e21268efSVladimir Oltean 
261e21268efSVladimir Oltean 	/* Restore PGID_CPU */
262e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(ocelot->num_phys_ports), ANA_PGID_PGID,
263e21268efSVladimir Oltean 			 PGID_CPU);
264e21268efSVladimir Oltean 
2658abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
2668abe1970SVladimir Oltean 
2678abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
268e21268efSVladimir Oltean }
269e21268efSVladimir Oltean 
27099348004SVladimir Oltean /* On switches with no extraction IRQ wired, trapped packets need to be
27199348004SVladimir Oltean  * replicated over Ethernet as well, otherwise we'd get no notification of
27299348004SVladimir Oltean  * their arrival when using the ocelot-8021q tagging protocol.
273c8c0ba4fSVladimir Oltean  */
27499348004SVladimir Oltean static int felix_update_trapping_destinations(struct dsa_switch *ds,
27599348004SVladimir Oltean 					      bool using_tag_8021q)
276c8c0ba4fSVladimir Oltean {
27799348004SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
27899348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
27999348004SVladimir Oltean 	struct ocelot_vcap_filter *trap;
28099348004SVladimir Oltean 	enum ocelot_mask_mode mask_mode;
28199348004SVladimir Oltean 	unsigned long port_mask;
2822960bb14SVladimir Oltean 	struct dsa_port *dp;
28399348004SVladimir Oltean 	bool cpu_copy_ena;
28499348004SVladimir Oltean 	int cpu = -1, err;
285c8c0ba4fSVladimir Oltean 
28699348004SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
28799348004SVladimir Oltean 		return 0;
288c8c0ba4fSVladimir Oltean 
28999348004SVladimir Oltean 	/* Figure out the current CPU port */
2902960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
2912960bb14SVladimir Oltean 		cpu = dp->index;
2928d5f7954SVladimir Oltean 		break;
293c8c0ba4fSVladimir Oltean 	}
2948d5f7954SVladimir Oltean 
295d78637a8SVladimir Oltean 	/* We are sure that "cpu" was found, otherwise
296d78637a8SVladimir Oltean 	 * dsa_tree_setup_default_cpu() would have failed earlier.
297d78637a8SVladimir Oltean 	 */
298c8c0ba4fSVladimir Oltean 
29999348004SVladimir Oltean 	/* Make sure all traps are set up for that destination */
30099348004SVladimir Oltean 	list_for_each_entry(trap, &ocelot->traps, trap_list) {
30199348004SVladimir Oltean 		/* Figure out the current trapping destination */
30299348004SVladimir Oltean 		if (using_tag_8021q) {
30399348004SVladimir Oltean 			/* Redirect to the tag_8021q CPU port. If timestamps
30499348004SVladimir Oltean 			 * are necessary, also copy trapped packets to the CPU
30599348004SVladimir Oltean 			 * port module.
306c8c0ba4fSVladimir Oltean 			 */
30799348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_REDIRECT;
30899348004SVladimir Oltean 			port_mask = BIT(cpu);
30999348004SVladimir Oltean 			cpu_copy_ena = !!trap->take_ts;
310c8c0ba4fSVladimir Oltean 		} else {
31199348004SVladimir Oltean 			/* Trap packets only to the CPU port module, which is
31299348004SVladimir Oltean 			 * redirected to the NPI port (the DSA CPU port)
313c8c0ba4fSVladimir Oltean 			 */
31499348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
31599348004SVladimir Oltean 			port_mask = 0;
31699348004SVladimir Oltean 			cpu_copy_ena = true;
317c8c0ba4fSVladimir Oltean 		}
318c8c0ba4fSVladimir Oltean 
31999348004SVladimir Oltean 		if (trap->action.mask_mode == mask_mode &&
32099348004SVladimir Oltean 		    trap->action.port_mask == port_mask &&
32199348004SVladimir Oltean 		    trap->action.cpu_copy_ena == cpu_copy_ena)
32299348004SVladimir Oltean 			continue;
323c8c0ba4fSVladimir Oltean 
32499348004SVladimir Oltean 		trap->action.mask_mode = mask_mode;
32599348004SVladimir Oltean 		trap->action.port_mask = port_mask;
32699348004SVladimir Oltean 		trap->action.cpu_copy_ena = cpu_copy_ena;
3270a6f17c6SVladimir Oltean 
32899348004SVladimir Oltean 		err = ocelot_vcap_filter_replace(ocelot, trap);
329c8c0ba4fSVladimir Oltean 		if (err)
330c8c0ba4fSVladimir Oltean 			return err;
33199348004SVladimir Oltean 	}
332c8c0ba4fSVladimir Oltean 
33399348004SVladimir Oltean 	return 0;
334c8c0ba4fSVladimir Oltean }
335c8c0ba4fSVladimir Oltean 
336e21268efSVladimir Oltean static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu)
337e21268efSVladimir Oltean {
338e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
339e21268efSVladimir Oltean 	unsigned long cpu_flood;
3402960bb14SVladimir Oltean 	struct dsa_port *dp;
3412960bb14SVladimir Oltean 	int err;
342e21268efSVladimir Oltean 
343e21268efSVladimir Oltean 	felix_8021q_cpu_port_init(ocelot, cpu);
344e21268efSVladimir Oltean 
3452960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
346e21268efSVladimir Oltean 		/* This overwrites ocelot_init():
347e21268efSVladimir Oltean 		 * Do not forward BPDU frames to the CPU port module,
348e21268efSVladimir Oltean 		 * for 2 reasons:
349e21268efSVladimir Oltean 		 * - When these packets are injected from the tag_8021q
350e21268efSVladimir Oltean 		 *   CPU port, we want them to go out, not loop back
351e21268efSVladimir Oltean 		 *   into the system.
352e21268efSVladimir Oltean 		 * - STP traffic ingressing on a user port should go to
353e21268efSVladimir Oltean 		 *   the tag_8021q CPU port, not to the hardware CPU
354e21268efSVladimir Oltean 		 *   port module.
355e21268efSVladimir Oltean 		 */
356e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
357e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0),
3582960bb14SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG, dp->index);
359e21268efSVladimir Oltean 	}
360e21268efSVladimir Oltean 
361c8c0ba4fSVladimir Oltean 	/* In tag_8021q mode, the CPU port module is unused, except for PTP
362c8c0ba4fSVladimir Oltean 	 * frames. So we want to disable flooding of any kind to the CPU port
363c8c0ba4fSVladimir Oltean 	 * module, since packets going there will end in a black hole.
364e21268efSVladimir Oltean 	 */
365e21268efSVladimir Oltean 	cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
366e21268efSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_UC);
367e21268efSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_MC);
368b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_BC);
369e21268efSVladimir Oltean 
3705da11eb4SVladimir Oltean 	err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD));
371e21268efSVladimir Oltean 	if (err)
372d7b1fd52SVladimir Oltean 		return err;
373d7b1fd52SVladimir Oltean 
37499348004SVladimir Oltean 	err = felix_update_trapping_destinations(ds, true);
375d7b1fd52SVladimir Oltean 	if (err)
376d7b1fd52SVladimir Oltean 		goto out_tag_8021q_unregister;
377e21268efSVladimir Oltean 
37899348004SVladimir Oltean 	/* The ownership of the CPU port module's queues might have just been
37999348004SVladimir Oltean 	 * transferred to the tag_8021q tagger from the NPI-based tagger.
38099348004SVladimir Oltean 	 * So there might still be all sorts of crap in the queues. On the
38199348004SVladimir Oltean 	 * other hand, the MMIO-based matching of PTP frames is very brittle,
38299348004SVladimir Oltean 	 * so we need to be careful that there are no extra frames to be
38399348004SVladimir Oltean 	 * dequeued over MMIO, since we would never know to discard them.
38499348004SVladimir Oltean 	 */
38599348004SVladimir Oltean 	ocelot_drain_cpu_queue(ocelot, 0);
38699348004SVladimir Oltean 
387e21268efSVladimir Oltean 	return 0;
388e21268efSVladimir Oltean 
389d7b1fd52SVladimir Oltean out_tag_8021q_unregister:
390d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
391e21268efSVladimir Oltean 	return err;
392e21268efSVladimir Oltean }
393e21268efSVladimir Oltean 
394e21268efSVladimir Oltean static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu)
395e21268efSVladimir Oltean {
396e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
3972960bb14SVladimir Oltean 	struct dsa_port *dp;
3982960bb14SVladimir Oltean 	int err;
399e21268efSVladimir Oltean 
40099348004SVladimir Oltean 	err = felix_update_trapping_destinations(ds, false);
401c8c0ba4fSVladimir Oltean 	if (err)
402c8c0ba4fSVladimir Oltean 		dev_err(ds->dev, "felix_teardown_mmio_filtering returned %d",
403c8c0ba4fSVladimir Oltean 			err);
404c8c0ba4fSVladimir Oltean 
405d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
406e21268efSVladimir Oltean 
4072960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
408e21268efSVladimir Oltean 		/* Restore the logic from ocelot_init:
409e21268efSVladimir Oltean 		 * do not forward BPDU frames to the front ports.
410e21268efSVladimir Oltean 		 */
411e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
412e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
413e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG,
4142960bb14SVladimir Oltean 				 dp->index);
415e21268efSVladimir Oltean 	}
416e21268efSVladimir Oltean 
417e21268efSVladimir Oltean 	felix_8021q_cpu_port_deinit(ocelot, cpu);
418e21268efSVladimir Oltean }
419e21268efSVladimir Oltean 
420adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This
421adb3dccfSVladimir Oltean  * is the mode through which frames can be injected from and extracted to an
422adb3dccfSVladimir Oltean  * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU
423adb3dccfSVladimir Oltean  * running Linux, and this forms a DSA setup together with the enetc or fman
424adb3dccfSVladimir Oltean  * DSA master.
425adb3dccfSVladimir Oltean  */
426adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port)
427adb3dccfSVladimir Oltean {
428adb3dccfSVladimir Oltean 	ocelot->npi = port;
429adb3dccfSVladimir Oltean 
430adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
431adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
432adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
433adb3dccfSVladimir Oltean 
434adb3dccfSVladimir Oltean 	/* NPI port Injection/Extraction configuration */
435adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
436adb3dccfSVladimir Oltean 			    ocelot->npi_xtr_prefix);
437adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
438adb3dccfSVladimir Oltean 			    ocelot->npi_inj_prefix);
439adb3dccfSVladimir Oltean 
440adb3dccfSVladimir Oltean 	/* Disable transmission of pause frames */
441adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
442adb3dccfSVladimir Oltean }
443adb3dccfSVladimir Oltean 
444adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port)
445adb3dccfSVladimir Oltean {
446adb3dccfSVladimir Oltean 	/* Restore hardware defaults */
447adb3dccfSVladimir Oltean 	int unused_port = ocelot->num_phys_ports + 2;
448adb3dccfSVladimir Oltean 
449adb3dccfSVladimir Oltean 	ocelot->npi = -1;
450adb3dccfSVladimir Oltean 
451adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port),
452adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
453adb3dccfSVladimir Oltean 
454adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
455adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
456adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
457adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
458adb3dccfSVladimir Oltean 
459adb3dccfSVladimir Oltean 	/* Enable transmission of pause frames */
460adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
461adb3dccfSVladimir Oltean }
462adb3dccfSVladimir Oltean 
463adb3dccfSVladimir Oltean static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu)
464adb3dccfSVladimir Oltean {
465adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
466adb3dccfSVladimir Oltean 	unsigned long cpu_flood;
467adb3dccfSVladimir Oltean 
468adb3dccfSVladimir Oltean 	felix_npi_port_init(ocelot, cpu);
469adb3dccfSVladimir Oltean 
470adb3dccfSVladimir Oltean 	/* Include the CPU port module (and indirectly, the NPI port)
471adb3dccfSVladimir Oltean 	 * in the forwarding mask for unknown unicast - the hardware
472adb3dccfSVladimir Oltean 	 * default value for ANA_FLOODING_FLD_UNICAST excludes
473adb3dccfSVladimir Oltean 	 * BIT(ocelot->num_phys_ports), and so does ocelot_init,
474adb3dccfSVladimir Oltean 	 * since Ocelot relies on whitelisting MAC addresses towards
475adb3dccfSVladimir Oltean 	 * PGID_CPU.
476adb3dccfSVladimir Oltean 	 * We do this because DSA does not yet perform RX filtering,
477adb3dccfSVladimir Oltean 	 * and the NPI port does not perform source address learning,
478adb3dccfSVladimir Oltean 	 * so traffic sent to Linux is effectively unknown from the
479adb3dccfSVladimir Oltean 	 * switch's perspective.
480adb3dccfSVladimir Oltean 	 */
481adb3dccfSVladimir Oltean 	cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
482adb3dccfSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_UC);
4836edb9e8dSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_MC);
484b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_BC);
485adb3dccfSVladimir Oltean 
486adb3dccfSVladimir Oltean 	return 0;
487adb3dccfSVladimir Oltean }
488adb3dccfSVladimir Oltean 
489adb3dccfSVladimir Oltean static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu)
490adb3dccfSVladimir Oltean {
491adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
492adb3dccfSVladimir Oltean 
493adb3dccfSVladimir Oltean 	felix_npi_port_deinit(ocelot, cpu);
494adb3dccfSVladimir Oltean }
495adb3dccfSVladimir Oltean 
496adb3dccfSVladimir Oltean static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu,
497adb3dccfSVladimir Oltean 				  enum dsa_tag_protocol proto)
498adb3dccfSVladimir Oltean {
499adb3dccfSVladimir Oltean 	int err;
500adb3dccfSVladimir Oltean 
501adb3dccfSVladimir Oltean 	switch (proto) {
5027c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
503adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
504adb3dccfSVladimir Oltean 		err = felix_setup_tag_npi(ds, cpu);
505adb3dccfSVladimir Oltean 		break;
506e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
507e21268efSVladimir Oltean 		err = felix_setup_tag_8021q(ds, cpu);
508e21268efSVladimir Oltean 		break;
509adb3dccfSVladimir Oltean 	default:
510adb3dccfSVladimir Oltean 		err = -EPROTONOSUPPORT;
511adb3dccfSVladimir Oltean 	}
512adb3dccfSVladimir Oltean 
513adb3dccfSVladimir Oltean 	return err;
514adb3dccfSVladimir Oltean }
515adb3dccfSVladimir Oltean 
516adb3dccfSVladimir Oltean static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu,
517adb3dccfSVladimir Oltean 				   enum dsa_tag_protocol proto)
518adb3dccfSVladimir Oltean {
519adb3dccfSVladimir Oltean 	switch (proto) {
5207c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
521adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
522adb3dccfSVladimir Oltean 		felix_teardown_tag_npi(ds, cpu);
523adb3dccfSVladimir Oltean 		break;
524e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
525e21268efSVladimir Oltean 		felix_teardown_tag_8021q(ds, cpu);
526e21268efSVladimir Oltean 		break;
527adb3dccfSVladimir Oltean 	default:
528adb3dccfSVladimir Oltean 		break;
529adb3dccfSVladimir Oltean 	}
530adb3dccfSVladimir Oltean }
531adb3dccfSVladimir Oltean 
532e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the
533e21268efSVladimir Oltean  * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
534e21268efSVladimir Oltean  * or the restoration is guaranteed to work.
535e21268efSVladimir Oltean  */
536adb3dccfSVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu,
537adb3dccfSVladimir Oltean 				     enum dsa_tag_protocol proto)
538adb3dccfSVladimir Oltean {
539adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
540adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
541adb3dccfSVladimir Oltean 	enum dsa_tag_protocol old_proto = felix->tag_proto;
542adb3dccfSVladimir Oltean 	int err;
543adb3dccfSVladimir Oltean 
5447c4bb540SVladimir Oltean 	if (proto != DSA_TAG_PROTO_SEVILLE &&
5457c4bb540SVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT &&
546e21268efSVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT_8021Q)
547adb3dccfSVladimir Oltean 		return -EPROTONOSUPPORT;
548adb3dccfSVladimir Oltean 
549adb3dccfSVladimir Oltean 	felix_del_tag_protocol(ds, cpu, old_proto);
550adb3dccfSVladimir Oltean 
551adb3dccfSVladimir Oltean 	err = felix_set_tag_protocol(ds, cpu, proto);
552adb3dccfSVladimir Oltean 	if (err) {
553adb3dccfSVladimir Oltean 		felix_set_tag_protocol(ds, cpu, old_proto);
554adb3dccfSVladimir Oltean 		return err;
555adb3dccfSVladimir Oltean 	}
556adb3dccfSVladimir Oltean 
557adb3dccfSVladimir Oltean 	felix->tag_proto = proto;
558adb3dccfSVladimir Oltean 
559adb3dccfSVladimir Oltean 	return 0;
560adb3dccfSVladimir Oltean }
561adb3dccfSVladimir Oltean 
56256051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
5634d776482SFlorian Fainelli 						    int port,
5644d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
56556051948SVladimir Oltean {
566adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
567adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
568adb3dccfSVladimir Oltean 
569adb3dccfSVladimir Oltean 	return felix->tag_proto;
57056051948SVladimir Oltean }
57156051948SVladimir Oltean 
57256051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
57356051948SVladimir Oltean 				 unsigned int ageing_time)
57456051948SVladimir Oltean {
57556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
57656051948SVladimir Oltean 
57756051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
57856051948SVladimir Oltean 
57956051948SVladimir Oltean 	return 0;
58056051948SVladimir Oltean }
58156051948SVladimir Oltean 
5825cad43a5SVladimir Oltean static void felix_port_fast_age(struct dsa_switch *ds, int port)
5835cad43a5SVladimir Oltean {
5845cad43a5SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5855cad43a5SVladimir Oltean 	int err;
5865cad43a5SVladimir Oltean 
5875cad43a5SVladimir Oltean 	err = ocelot_mact_flush(ocelot, port);
5885cad43a5SVladimir Oltean 	if (err)
5895cad43a5SVladimir Oltean 		dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n",
5905cad43a5SVladimir Oltean 			port, ERR_PTR(err));
5915cad43a5SVladimir Oltean }
5925cad43a5SVladimir Oltean 
59356051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
59456051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
59556051948SVladimir Oltean {
59656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
59756051948SVladimir Oltean 
59856051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
59956051948SVladimir Oltean }
60056051948SVladimir Oltean 
60156051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
60256051948SVladimir Oltean 			 const unsigned char *addr, u16 vid)
60356051948SVladimir Oltean {
60456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
60556051948SVladimir Oltean 
60687b0f983SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid);
60756051948SVladimir Oltean }
60856051948SVladimir Oltean 
60956051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
61056051948SVladimir Oltean 			 const unsigned char *addr, u16 vid)
61156051948SVladimir Oltean {
61256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
61356051948SVladimir Oltean 
61456051948SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid);
61556051948SVladimir Oltean }
61656051948SVladimir Oltean 
617961d8b69SVladimir Oltean static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag,
618961d8b69SVladimir Oltean 			     const unsigned char *addr, u16 vid)
619961d8b69SVladimir Oltean {
620961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
621961d8b69SVladimir Oltean 
622961d8b69SVladimir Oltean 	return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid);
623961d8b69SVladimir Oltean }
624961d8b69SVladimir Oltean 
625961d8b69SVladimir Oltean static int felix_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag lag,
626961d8b69SVladimir Oltean 			     const unsigned char *addr, u16 vid)
627961d8b69SVladimir Oltean {
628961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
629961d8b69SVladimir Oltean 
630961d8b69SVladimir Oltean 	return ocelot_lag_fdb_del(ocelot, lag.dev, addr, vid);
631961d8b69SVladimir Oltean }
632961d8b69SVladimir Oltean 
633a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port,
634209edf95SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb)
635209edf95SVladimir Oltean {
636209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
637209edf95SVladimir Oltean 
638a52b2da7SVladimir Oltean 	return ocelot_port_mdb_add(ocelot, port, mdb);
639209edf95SVladimir Oltean }
640209edf95SVladimir Oltean 
641209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port,
642209edf95SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb)
643209edf95SVladimir Oltean {
644209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
645209edf95SVladimir Oltean 
646209edf95SVladimir Oltean 	return ocelot_port_mdb_del(ocelot, port, mdb);
647209edf95SVladimir Oltean }
648209edf95SVladimir Oltean 
64956051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
65056051948SVladimir Oltean 				       u8 state)
65156051948SVladimir Oltean {
65256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
65356051948SVladimir Oltean 
65456051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
65556051948SVladimir Oltean }
65656051948SVladimir Oltean 
657421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port,
658421741eaSVladimir Oltean 				  struct switchdev_brport_flags val,
659421741eaSVladimir Oltean 				  struct netlink_ext_ack *extack)
660421741eaSVladimir Oltean {
661421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
662421741eaSVladimir Oltean 
663421741eaSVladimir Oltean 	return ocelot_port_pre_bridge_flags(ocelot, port, val);
664421741eaSVladimir Oltean }
665421741eaSVladimir Oltean 
666421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port,
667421741eaSVladimir Oltean 			      struct switchdev_brport_flags val,
668421741eaSVladimir Oltean 			      struct netlink_ext_ack *extack)
669421741eaSVladimir Oltean {
670421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
671421741eaSVladimir Oltean 
672421741eaSVladimir Oltean 	ocelot_port_bridge_flags(ocelot, port, val);
673421741eaSVladimir Oltean 
674421741eaSVladimir Oltean 	return 0;
675421741eaSVladimir Oltean }
676421741eaSVladimir Oltean 
67756051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
678b079922bSVladimir Oltean 			     struct dsa_bridge bridge, bool *tx_fwd_offload)
67956051948SVladimir Oltean {
68056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
68156051948SVladimir Oltean 
682d3eed0e5SVladimir Oltean 	ocelot_port_bridge_join(ocelot, port, bridge.dev);
683e4bd44e8SVladimir Oltean 
684e4bd44e8SVladimir Oltean 	return 0;
68556051948SVladimir Oltean }
68656051948SVladimir Oltean 
68756051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
688d3eed0e5SVladimir Oltean 			       struct dsa_bridge bridge)
68956051948SVladimir Oltean {
69056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
69156051948SVladimir Oltean 
692d3eed0e5SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, bridge.dev);
69356051948SVladimir Oltean }
69456051948SVladimir Oltean 
6958fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port,
696dedd6a00SVladimir Oltean 			  struct dsa_lag lag,
6978fe6832eSVladimir Oltean 			  struct netdev_lag_upper_info *info)
6988fe6832eSVladimir Oltean {
6998fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7008fe6832eSVladimir Oltean 
701dedd6a00SVladimir Oltean 	return ocelot_port_lag_join(ocelot, port, lag.dev, info);
7028fe6832eSVladimir Oltean }
7038fe6832eSVladimir Oltean 
7048fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port,
705dedd6a00SVladimir Oltean 			   struct dsa_lag lag)
7068fe6832eSVladimir Oltean {
7078fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7088fe6832eSVladimir Oltean 
709dedd6a00SVladimir Oltean 	ocelot_port_lag_leave(ocelot, port, lag.dev);
7108fe6832eSVladimir Oltean 
7118fe6832eSVladimir Oltean 	return 0;
7128fe6832eSVladimir Oltean }
7138fe6832eSVladimir Oltean 
7148fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port)
7158fe6832eSVladimir Oltean {
7168fe6832eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
7178fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7188fe6832eSVladimir Oltean 
7198fe6832eSVladimir Oltean 	ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
7208fe6832eSVladimir Oltean 
7218fe6832eSVladimir Oltean 	return 0;
7228fe6832eSVladimir Oltean }
7238fe6832eSVladimir Oltean 
72456051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
72501af940eSVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan,
72601af940eSVladimir Oltean 			      struct netlink_ext_ack *extack)
72756051948SVladimir Oltean {
7282f0402feSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
729b7a9e0daSVladimir Oltean 	u16 flags = vlan->flags;
7302f0402feSVladimir Oltean 
7319a720680SVladimir Oltean 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
7329a720680SVladimir Oltean 	 * egress-untagged or not, pvid or not, make no difference. This
7339a720680SVladimir Oltean 	 * behavior is already better than what DSA just tries to approximate
7349a720680SVladimir Oltean 	 * when it installs the VLAN with the same flags on the CPU port.
7359a720680SVladimir Oltean 	 * Just accept any configuration, and don't let ocelot deny installing
7369a720680SVladimir Oltean 	 * multiple native VLANs on the NPI port, because the switch doesn't
7379a720680SVladimir Oltean 	 * look at the port tag settings towards the NPI interface anyway.
7389a720680SVladimir Oltean 	 */
7399a720680SVladimir Oltean 	if (port == ocelot->npi)
7409a720680SVladimir Oltean 		return 0;
7419a720680SVladimir Oltean 
742b7a9e0daSVladimir Oltean 	return ocelot_vlan_prepare(ocelot, port, vlan->vid,
7432f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_PVID,
74401af940eSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_UNTAGGED,
74501af940eSVladimir Oltean 				   extack);
74656051948SVladimir Oltean }
74756051948SVladimir Oltean 
74889153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
74989153ed6SVladimir Oltean 				struct netlink_ext_ack *extack)
75056051948SVladimir Oltean {
75156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
75256051948SVladimir Oltean 
7533b95d1b2SVladimir Oltean 	return ocelot_port_vlan_filtering(ocelot, port, enabled, extack);
75456051948SVladimir Oltean }
75556051948SVladimir Oltean 
7561958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port,
75731046a5fSVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan,
75831046a5fSVladimir Oltean 			  struct netlink_ext_ack *extack)
75956051948SVladimir Oltean {
76056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
761183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
7621958d581SVladimir Oltean 	int err;
76356051948SVladimir Oltean 
76401af940eSVladimir Oltean 	err = felix_vlan_prepare(ds, port, vlan, extack);
7651958d581SVladimir Oltean 	if (err)
7661958d581SVladimir Oltean 		return err;
7671958d581SVladimir Oltean 
7681958d581SVladimir Oltean 	return ocelot_vlan_add(ocelot, port, vlan->vid,
769183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_PVID,
770183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_UNTAGGED);
77156051948SVladimir Oltean }
77256051948SVladimir Oltean 
77356051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
77456051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
77556051948SVladimir Oltean {
77656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
77756051948SVladimir Oltean 
778b7a9e0daSVladimir Oltean 	return ocelot_vlan_del(ocelot, port, vlan->vid);
77956051948SVladimir Oltean }
78056051948SVladimir Oltean 
781*79fda660SRussell King (Oracle) static void felix_phylink_get_caps(struct dsa_switch *ds, int port,
782*79fda660SRussell King (Oracle) 				   struct phylink_config *config)
783*79fda660SRussell King (Oracle) {
784*79fda660SRussell King (Oracle) 	struct ocelot *ocelot = ds->priv;
785*79fda660SRussell King (Oracle) 
786*79fda660SRussell King (Oracle) 	__set_bit(ocelot->ports[port]->phy_mode,
787*79fda660SRussell King (Oracle) 		  config->supported_interfaces);
788*79fda660SRussell King (Oracle) }
789*79fda660SRussell King (Oracle) 
790bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
791bdeced75SVladimir Oltean 				   unsigned long *supported,
792bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
793bdeced75SVladimir Oltean {
794bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
795375e1314SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
796bdeced75SVladimir Oltean 
797375e1314SVladimir Oltean 	if (felix->info->phylink_validate)
798375e1314SVladimir Oltean 		felix->info->phylink_validate(ocelot, port, supported, state);
799bdeced75SVladimir Oltean }
800bdeced75SVladimir Oltean 
801bdeced75SVladimir Oltean static void felix_phylink_mac_config(struct dsa_switch *ds, int port,
802bdeced75SVladimir Oltean 				     unsigned int link_an_mode,
803bdeced75SVladimir Oltean 				     const struct phylink_link_state *state)
804bdeced75SVladimir Oltean {
805bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
806bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
807588d0550SIoana Ciornei 	struct dsa_port *dp = dsa_to_port(ds, port);
808bdeced75SVladimir Oltean 
80949af6a76SColin Foster 	if (felix->pcs && felix->pcs[port])
810e7026f15SColin Foster 		phylink_set_pcs(dp->pl, felix->pcs[port]);
811bdeced75SVladimir Oltean }
812bdeced75SVladimir Oltean 
813bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
814bdeced75SVladimir Oltean 					unsigned int link_an_mode,
815bdeced75SVladimir Oltean 					phy_interface_t interface)
816bdeced75SVladimir Oltean {
817bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
818bdeced75SVladimir Oltean 
819e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
820e6e12df6SVladimir Oltean 				     FELIX_MAC_QUIRKS);
821bdeced75SVladimir Oltean }
822bdeced75SVladimir Oltean 
823bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
824bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
825bdeced75SVladimir Oltean 				      phy_interface_t interface,
8265b502a7bSRussell King 				      struct phy_device *phydev,
8275b502a7bSRussell King 				      int speed, int duplex,
8285b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
829bdeced75SVladimir Oltean {
830bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8317e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
832bdeced75SVladimir Oltean 
833e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
834e6e12df6SVladimir Oltean 				   interface, speed, duplex, tx_pause, rx_pause,
835e6e12df6SVladimir Oltean 				   FELIX_MAC_QUIRKS);
8367e14a2dcSVladimir Oltean 
8377e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
8387e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
839bdeced75SVladimir Oltean }
840bdeced75SVladimir Oltean 
841bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
842bd2b3161SXiaoliang Yang {
843bd2b3161SXiaoliang Yang 	int i;
844bd2b3161SXiaoliang Yang 
845bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
846bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
847bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
848bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
849bd2b3161SXiaoliang Yang 		       port);
850bd2b3161SXiaoliang Yang 
85170d39a6eSVladimir Oltean 	for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
852bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
853bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
854bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
855bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
856bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
857bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
858bd2b3161SXiaoliang Yang 			      port, i);
859bd2b3161SXiaoliang Yang 	}
860bd2b3161SXiaoliang Yang }
861bd2b3161SXiaoliang Yang 
86256051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
86356051948SVladimir Oltean 			      u32 stringset, u8 *data)
86456051948SVladimir Oltean {
86556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
86656051948SVladimir Oltean 
86756051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
86856051948SVladimir Oltean }
86956051948SVladimir Oltean 
87056051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
87156051948SVladimir Oltean {
87256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
87356051948SVladimir Oltean 
87456051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
87556051948SVladimir Oltean }
87656051948SVladimir Oltean 
87756051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
87856051948SVladimir Oltean {
87956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
88056051948SVladimir Oltean 
88156051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
88256051948SVladimir Oltean }
88356051948SVladimir Oltean 
88456051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
88556051948SVladimir Oltean 			     struct ethtool_ts_info *info)
88656051948SVladimir Oltean {
88756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
88856051948SVladimir Oltean 
88956051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
89056051948SVladimir Oltean }
89156051948SVladimir Oltean 
892bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
893bdeced75SVladimir Oltean 				  struct device_node *ports_node,
894bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
895bdeced75SVladimir Oltean {
896bdeced75SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
897bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
898bdeced75SVladimir Oltean 	struct device_node *child;
899bdeced75SVladimir Oltean 
90037fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
901bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
902bdeced75SVladimir Oltean 		u32 port;
903bdeced75SVladimir Oltean 		int err;
904bdeced75SVladimir Oltean 
905bdeced75SVladimir Oltean 		/* Get switch port number from DT */
906bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
907bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
908bdeced75SVladimir Oltean 				"(property \"reg\")\n");
909bdeced75SVladimir Oltean 			of_node_put(child);
910bdeced75SVladimir Oltean 			return -ENODEV;
911bdeced75SVladimir Oltean 		}
912bdeced75SVladimir Oltean 
913bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
914bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
915bdeced75SVladimir Oltean 		if (err) {
916bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
917bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
918bdeced75SVladimir Oltean 				port);
919bdeced75SVladimir Oltean 			of_node_put(child);
920bdeced75SVladimir Oltean 			return -ENODEV;
921bdeced75SVladimir Oltean 		}
922bdeced75SVladimir Oltean 
923bdeced75SVladimir Oltean 		err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode);
924bdeced75SVladimir Oltean 		if (err < 0) {
925bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
926bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
92759ebb430SSumera Priyadarsini 			of_node_put(child);
928bdeced75SVladimir Oltean 			return err;
929bdeced75SVladimir Oltean 		}
930bdeced75SVladimir Oltean 
931bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
932bdeced75SVladimir Oltean 	}
933bdeced75SVladimir Oltean 
934bdeced75SVladimir Oltean 	return 0;
935bdeced75SVladimir Oltean }
936bdeced75SVladimir Oltean 
937bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
938bdeced75SVladimir Oltean {
939bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
940bdeced75SVladimir Oltean 	struct device_node *switch_node;
941bdeced75SVladimir Oltean 	struct device_node *ports_node;
942bdeced75SVladimir Oltean 	int err;
943bdeced75SVladimir Oltean 
944bdeced75SVladimir Oltean 	switch_node = dev->of_node;
945bdeced75SVladimir Oltean 
946bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
947abecbfcdSVladimir Oltean 	if (!ports_node)
948abecbfcdSVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
949bdeced75SVladimir Oltean 	if (!ports_node) {
950abecbfcdSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n");
951bdeced75SVladimir Oltean 		return -ENODEV;
952bdeced75SVladimir Oltean 	}
953bdeced75SVladimir Oltean 
954bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
955bdeced75SVladimir Oltean 	of_node_put(ports_node);
956bdeced75SVladimir Oltean 
957bdeced75SVladimir Oltean 	return err;
958bdeced75SVladimir Oltean }
959bdeced75SVladimir Oltean 
96056051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
96156051948SVladimir Oltean {
96256051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
963bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
964b4024c9eSClaudiu Manoil 	struct resource res;
96556051948SVladimir Oltean 	int port, i, err;
96656051948SVladimir Oltean 
96756051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
96856051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
96956051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
97056051948SVladimir Oltean 	if (!ocelot->ports)
97156051948SVladimir Oltean 		return -ENOMEM;
97256051948SVladimir Oltean 
97356051948SVladimir Oltean 	ocelot->map		= felix->info->map;
97456051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
97556051948SVladimir Oltean 	ocelot->num_stats	= felix->info->num_stats;
97621ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
97707d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
97877043c37SXiaoliang Yang 	ocelot->vcap_pol.base	= felix->info->vcap_pol_base;
97977043c37SXiaoliang Yang 	ocelot->vcap_pol.max	= felix->info->vcap_pol_max;
98077043c37SXiaoliang Yang 	ocelot->vcap_pol.base2	= felix->info->vcap_pol_base2;
98177043c37SXiaoliang Yang 	ocelot->vcap_pol.max2	= felix->info->vcap_pol_max2;
98256051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
983cacea62fSVladimir Oltean 	ocelot->npi_inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
984cacea62fSVladimir Oltean 	ocelot->npi_xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
985f59fd9caSVladimir Oltean 	ocelot->devlink		= felix->ds->devlink;
98656051948SVladimir Oltean 
987bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
988bdeced75SVladimir Oltean 				 GFP_KERNEL);
989bdeced75SVladimir Oltean 	if (!port_phy_modes)
990bdeced75SVladimir Oltean 		return -ENOMEM;
991bdeced75SVladimir Oltean 
992bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
993bdeced75SVladimir Oltean 	if (err) {
994bdeced75SVladimir Oltean 		kfree(port_phy_modes);
995bdeced75SVladimir Oltean 		return err;
996bdeced75SVladimir Oltean 	}
997bdeced75SVladimir Oltean 
99856051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
99956051948SVladimir Oltean 		struct regmap *target;
100056051948SVladimir Oltean 
100156051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
100256051948SVladimir Oltean 			continue;
100356051948SVladimir Oltean 
1004b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
1005b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1006375e1314SVladimir Oltean 		res.start += felix->switch_base;
1007375e1314SVladimir Oltean 		res.end += felix->switch_base;
100856051948SVladimir Oltean 
1009242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
101056051948SVladimir Oltean 		if (IS_ERR(target)) {
101156051948SVladimir Oltean 			dev_err(ocelot->dev,
101256051948SVladimir Oltean 				"Failed to map device memory space\n");
1013bdeced75SVladimir Oltean 			kfree(port_phy_modes);
101456051948SVladimir Oltean 			return PTR_ERR(target);
101556051948SVladimir Oltean 		}
101656051948SVladimir Oltean 
101756051948SVladimir Oltean 		ocelot->targets[i] = target;
101856051948SVladimir Oltean 	}
101956051948SVladimir Oltean 
102056051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
102156051948SVladimir Oltean 	if (err) {
102256051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
1023bdeced75SVladimir Oltean 		kfree(port_phy_modes);
102456051948SVladimir Oltean 		return err;
102556051948SVladimir Oltean 	}
102656051948SVladimir Oltean 
102756051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
102856051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
102991c724cfSVladimir Oltean 		struct regmap *target;
103056051948SVladimir Oltean 
103156051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
103256051948SVladimir Oltean 					   sizeof(struct ocelot_port),
103356051948SVladimir Oltean 					   GFP_KERNEL);
103456051948SVladimir Oltean 		if (!ocelot_port) {
103556051948SVladimir Oltean 			dev_err(ocelot->dev,
103656051948SVladimir Oltean 				"failed to allocate port memory\n");
1037bdeced75SVladimir Oltean 			kfree(port_phy_modes);
103856051948SVladimir Oltean 			return -ENOMEM;
103956051948SVladimir Oltean 		}
104056051948SVladimir Oltean 
1041b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
1042b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1043375e1314SVladimir Oltean 		res.start += felix->switch_base;
1044375e1314SVladimir Oltean 		res.end += felix->switch_base;
104556051948SVladimir Oltean 
1046242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
104791c724cfSVladimir Oltean 		if (IS_ERR(target)) {
104856051948SVladimir Oltean 			dev_err(ocelot->dev,
104991c724cfSVladimir Oltean 				"Failed to map memory space for port %d\n",
105091c724cfSVladimir Oltean 				port);
1051bdeced75SVladimir Oltean 			kfree(port_phy_modes);
105291c724cfSVladimir Oltean 			return PTR_ERR(target);
105356051948SVladimir Oltean 		}
105456051948SVladimir Oltean 
1055bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
105656051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
105791c724cfSVladimir Oltean 		ocelot_port->target = target;
105856051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
105956051948SVladimir Oltean 	}
106056051948SVladimir Oltean 
1061bdeced75SVladimir Oltean 	kfree(port_phy_modes);
1062bdeced75SVladimir Oltean 
1063bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
1064bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
1065bdeced75SVladimir Oltean 		if (err < 0)
1066bdeced75SVladimir Oltean 			return err;
1067bdeced75SVladimir Oltean 	}
1068bdeced75SVladimir Oltean 
106956051948SVladimir Oltean 	return 0;
107056051948SVladimir Oltean }
107156051948SVladimir Oltean 
10721328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port,
10731328a883SVladimir Oltean 					   struct sk_buff *skb)
10741328a883SVladimir Oltean {
10751328a883SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
10761328a883SVladimir Oltean 	struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
10771328a883SVladimir Oltean 	struct sk_buff *skb_match = NULL, *skb_tmp;
10781328a883SVladimir Oltean 	unsigned long flags;
10791328a883SVladimir Oltean 
10801328a883SVladimir Oltean 	if (!clone)
10811328a883SVladimir Oltean 		return;
10821328a883SVladimir Oltean 
10831328a883SVladimir Oltean 	spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags);
10841328a883SVladimir Oltean 
10851328a883SVladimir Oltean 	skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) {
10861328a883SVladimir Oltean 		if (skb != clone)
10871328a883SVladimir Oltean 			continue;
10881328a883SVladimir Oltean 		__skb_unlink(skb, &ocelot_port->tx_skbs);
10891328a883SVladimir Oltean 		skb_match = skb;
10901328a883SVladimir Oltean 		break;
10911328a883SVladimir Oltean 	}
10921328a883SVladimir Oltean 
10931328a883SVladimir Oltean 	spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags);
10941328a883SVladimir Oltean 
10951328a883SVladimir Oltean 	WARN_ONCE(!skb_match,
10961328a883SVladimir Oltean 		  "Could not find skb clone in TX timestamping list\n");
10971328a883SVladimir Oltean }
10981328a883SVladimir Oltean 
109949f885b2SVladimir Oltean #define work_to_xmit_work(w) \
110049f885b2SVladimir Oltean 		container_of((w), struct felix_deferred_xmit_work, work)
110149f885b2SVladimir Oltean 
110249f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work)
110349f885b2SVladimir Oltean {
110449f885b2SVladimir Oltean 	struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
110549f885b2SVladimir Oltean 	struct dsa_switch *ds = xmit_work->dp->ds;
110649f885b2SVladimir Oltean 	struct sk_buff *skb = xmit_work->skb;
110749f885b2SVladimir Oltean 	u32 rew_op = ocelot_ptp_rew_op(skb);
110849f885b2SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
110949f885b2SVladimir Oltean 	int port = xmit_work->dp->index;
111049f885b2SVladimir Oltean 	int retries = 10;
111149f885b2SVladimir Oltean 
111249f885b2SVladimir Oltean 	do {
111349f885b2SVladimir Oltean 		if (ocelot_can_inject(ocelot, 0))
111449f885b2SVladimir Oltean 			break;
111549f885b2SVladimir Oltean 
111649f885b2SVladimir Oltean 		cpu_relax();
111749f885b2SVladimir Oltean 	} while (--retries);
111849f885b2SVladimir Oltean 
111949f885b2SVladimir Oltean 	if (!retries) {
112049f885b2SVladimir Oltean 		dev_err(ocelot->dev, "port %d failed to inject skb\n",
112149f885b2SVladimir Oltean 			port);
11221328a883SVladimir Oltean 		ocelot_port_purge_txtstamp_skb(ocelot, port, skb);
112349f885b2SVladimir Oltean 		kfree_skb(skb);
112449f885b2SVladimir Oltean 		return;
112549f885b2SVladimir Oltean 	}
112649f885b2SVladimir Oltean 
112749f885b2SVladimir Oltean 	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
112849f885b2SVladimir Oltean 
112949f885b2SVladimir Oltean 	consume_skb(skb);
113049f885b2SVladimir Oltean 	kfree(xmit_work);
113149f885b2SVladimir Oltean }
113249f885b2SVladimir Oltean 
113335d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds,
113435d97680SVladimir Oltean 				      enum dsa_tag_protocol proto)
113549f885b2SVladimir Oltean {
113635d97680SVladimir Oltean 	struct ocelot_8021q_tagger_data *tagger_data;
113749f885b2SVladimir Oltean 
113835d97680SVladimir Oltean 	switch (proto) {
113935d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
114035d97680SVladimir Oltean 		tagger_data = ocelot_8021q_tagger_data(ds);
114135d97680SVladimir Oltean 		tagger_data->xmit_work_fn = felix_port_deferred_xmit;
114249f885b2SVladimir Oltean 		return 0;
114335d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
114435d97680SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
114549f885b2SVladimir Oltean 		return 0;
114635d97680SVladimir Oltean 	default:
114735d97680SVladimir Oltean 		return -EPROTONOSUPPORT;
114849f885b2SVladimir Oltean 	}
114949f885b2SVladimir Oltean }
115049f885b2SVladimir Oltean 
115156051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
115256051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
115356051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
115456051948SVladimir Oltean  * (which will not work).
115556051948SVladimir Oltean  */
115656051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
115756051948SVladimir Oltean {
115856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
115956051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
11602960bb14SVladimir Oltean 	struct dsa_port *dp;
11612960bb14SVladimir Oltean 	int err;
116256051948SVladimir Oltean 
116356051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
116456051948SVladimir Oltean 	if (err)
116556051948SVladimir Oltean 		return err;
116656051948SVladimir Oltean 
1167d1cc0e93SVladimir Oltean 	err = ocelot_init(ocelot);
1168d1cc0e93SVladimir Oltean 	if (err)
11696b73b7c9SVladimir Oltean 		goto out_mdiobus_free;
1170d1cc0e93SVladimir Oltean 
11712b49d128SYangbo Lu 	if (ocelot->ptp) {
11722ac7c6c5SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
11732b49d128SYangbo Lu 		if (err) {
11742b49d128SYangbo Lu 			dev_err(ocelot->dev,
11752b49d128SYangbo Lu 				"Timestamp initialization failed\n");
11762b49d128SYangbo Lu 			ocelot->ptp = 0;
11772b49d128SYangbo Lu 		}
11782b49d128SYangbo Lu 	}
117956051948SVladimir Oltean 
11802960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
11812960bb14SVladimir Oltean 		ocelot_init_port(ocelot, dp->index);
1182bd2b3161SXiaoliang Yang 
1183bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
1184bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
1185bd2b3161SXiaoliang Yang 		 */
11862960bb14SVladimir Oltean 		felix_port_qos_map_init(ocelot, dp->index);
118756051948SVladimir Oltean 	}
118856051948SVladimir Oltean 
1189f59fd9caSVladimir Oltean 	err = ocelot_devlink_sb_register(ocelot);
1190f59fd9caSVladimir Oltean 	if (err)
11916b73b7c9SVladimir Oltean 		goto out_deinit_ports;
1192f59fd9caSVladimir Oltean 
11932960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
1194adb3dccfSVladimir Oltean 		/* The initial tag protocol is NPI which always returns 0, so
1195adb3dccfSVladimir Oltean 		 * there's no real point in checking for errors.
11961cf3299bSVladimir Oltean 		 */
11972960bb14SVladimir Oltean 		felix_set_tag_protocol(ds, dp->index, felix->tag_proto);
11988d5f7954SVladimir Oltean 		break;
1199adb3dccfSVladimir Oltean 	}
12001cf3299bSVladimir Oltean 
12010b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
1202c54913c1SVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
1203bdeced75SVladimir Oltean 
120456051948SVladimir Oltean 	return 0;
12056b73b7c9SVladimir Oltean 
12066b73b7c9SVladimir Oltean out_deinit_ports:
12072960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
12082960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
12096b73b7c9SVladimir Oltean 
12106b73b7c9SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
12116b73b7c9SVladimir Oltean 	ocelot_deinit(ocelot);
12126b73b7c9SVladimir Oltean 
12136b73b7c9SVladimir Oltean out_mdiobus_free:
12146b73b7c9SVladimir Oltean 	if (felix->info->mdio_bus_free)
12156b73b7c9SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
12166b73b7c9SVladimir Oltean 
12176b73b7c9SVladimir Oltean 	return err;
121856051948SVladimir Oltean }
121956051948SVladimir Oltean 
122056051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
122156051948SVladimir Oltean {
122256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1223bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
12242960bb14SVladimir Oltean 	struct dsa_port *dp;
1225bdeced75SVladimir Oltean 
12262960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
12272960bb14SVladimir Oltean 		felix_del_tag_protocol(ds, dp->index, felix->tag_proto);
12288d5f7954SVladimir Oltean 		break;
1229adb3dccfSVladimir Oltean 	}
1230adb3dccfSVladimir Oltean 
12312960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
12322960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
1233d19741b0SVladimir Oltean 
123449f885b2SVladimir Oltean 	ocelot_devlink_sb_unregister(ocelot);
123549f885b2SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
123649f885b2SVladimir Oltean 	ocelot_deinit(ocelot);
123749f885b2SVladimir Oltean 
1238d19741b0SVladimir Oltean 	if (felix->info->mdio_bus_free)
1239d19741b0SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
124056051948SVladimir Oltean }
124156051948SVladimir Oltean 
1242c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
1243c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1244c0bcf537SYangbo Lu {
1245c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1246c0bcf537SYangbo Lu 
1247c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
1248c0bcf537SYangbo Lu }
1249c0bcf537SYangbo Lu 
1250c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
1251c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1252c0bcf537SYangbo Lu {
1253c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
125499348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
125599348004SVladimir Oltean 	bool using_tag_8021q;
125699348004SVladimir Oltean 	int err;
1257c0bcf537SYangbo Lu 
125899348004SVladimir Oltean 	err = ocelot_hwstamp_set(ocelot, port, ifr);
125999348004SVladimir Oltean 	if (err)
126099348004SVladimir Oltean 		return err;
126199348004SVladimir Oltean 
126299348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
126399348004SVladimir Oltean 
126499348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
1265c0bcf537SYangbo Lu }
1266c0bcf537SYangbo Lu 
12670a6f17c6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot, unsigned int ptp_type)
12680a6f17c6SVladimir Oltean {
12690a6f17c6SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
12700a6f17c6SVladimir Oltean 	int err, grp = 0;
12710a6f17c6SVladimir Oltean 
12720a6f17c6SVladimir Oltean 	if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q)
12730a6f17c6SVladimir Oltean 		return false;
12740a6f17c6SVladimir Oltean 
12750a6f17c6SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
12760a6f17c6SVladimir Oltean 		return false;
12770a6f17c6SVladimir Oltean 
12780a6f17c6SVladimir Oltean 	if (ptp_type == PTP_CLASS_NONE)
12790a6f17c6SVladimir Oltean 		return false;
12800a6f17c6SVladimir Oltean 
12810a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
12820a6f17c6SVladimir Oltean 		struct sk_buff *skb;
12830a6f17c6SVladimir Oltean 		unsigned int type;
12840a6f17c6SVladimir Oltean 
12850a6f17c6SVladimir Oltean 		err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
12860a6f17c6SVladimir Oltean 		if (err)
12870a6f17c6SVladimir Oltean 			goto out;
12880a6f17c6SVladimir Oltean 
12890a6f17c6SVladimir Oltean 		/* We trap to the CPU port module all PTP frames, but
12900a6f17c6SVladimir Oltean 		 * felix_rxtstamp() only gets called for event frames.
12910a6f17c6SVladimir Oltean 		 * So we need to avoid sending duplicate general
12920a6f17c6SVladimir Oltean 		 * message frames by running a second BPF classifier
12930a6f17c6SVladimir Oltean 		 * here and dropping those.
12940a6f17c6SVladimir Oltean 		 */
12950a6f17c6SVladimir Oltean 		__skb_push(skb, ETH_HLEN);
12960a6f17c6SVladimir Oltean 
12970a6f17c6SVladimir Oltean 		type = ptp_classify_raw(skb);
12980a6f17c6SVladimir Oltean 
12990a6f17c6SVladimir Oltean 		__skb_pull(skb, ETH_HLEN);
13000a6f17c6SVladimir Oltean 
13010a6f17c6SVladimir Oltean 		if (type == PTP_CLASS_NONE) {
13020a6f17c6SVladimir Oltean 			kfree_skb(skb);
13030a6f17c6SVladimir Oltean 			continue;
13040a6f17c6SVladimir Oltean 		}
13050a6f17c6SVladimir Oltean 
13060a6f17c6SVladimir Oltean 		netif_rx(skb);
13070a6f17c6SVladimir Oltean 	}
13080a6f17c6SVladimir Oltean 
13090a6f17c6SVladimir Oltean out:
13100a6f17c6SVladimir Oltean 	if (err < 0)
13110a6f17c6SVladimir Oltean 		ocelot_drain_cpu_queue(ocelot, 0);
13120a6f17c6SVladimir Oltean 
13130a6f17c6SVladimir Oltean 	return true;
13140a6f17c6SVladimir Oltean }
13150a6f17c6SVladimir Oltean 
1316c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
1317c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
1318c0bcf537SYangbo Lu {
131992f62485SVladimir Oltean 	u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo;
1320c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
1321c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1322c0bcf537SYangbo Lu 	struct timespec64 ts;
132392f62485SVladimir Oltean 	u32 tstamp_hi;
132492f62485SVladimir Oltean 	u64 tstamp;
1325c0bcf537SYangbo Lu 
13260a6f17c6SVladimir Oltean 	/* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb
13270a6f17c6SVladimir Oltean 	 * for RX timestamping. Then free it, and poll for its copy through
13280a6f17c6SVladimir Oltean 	 * MMIO in the CPU port module, and inject that into the stack from
13290a6f17c6SVladimir Oltean 	 * ocelot_xtr_poll().
13300a6f17c6SVladimir Oltean 	 */
13310a6f17c6SVladimir Oltean 	if (felix_check_xtr_pkt(ocelot, type)) {
13320a6f17c6SVladimir Oltean 		kfree_skb(skb);
13330a6f17c6SVladimir Oltean 		return true;
13340a6f17c6SVladimir Oltean 	}
13350a6f17c6SVladimir Oltean 
1336c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1337c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1338c0bcf537SYangbo Lu 
1339c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
1340c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
1341c0bcf537SYangbo Lu 		tstamp_hi--;
1342c0bcf537SYangbo Lu 
1343c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
1344c0bcf537SYangbo Lu 
1345c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
1346c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1347c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
1348c0bcf537SYangbo Lu 	return false;
1349c0bcf537SYangbo Lu }
1350c0bcf537SYangbo Lu 
13515c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port,
13525c5416f5SYangbo Lu 			   struct sk_buff *skb)
1353c0bcf537SYangbo Lu {
1354c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1355682eaad9SYangbo Lu 	struct sk_buff *clone = NULL;
1356c0bcf537SYangbo Lu 
1357682eaad9SYangbo Lu 	if (!ocelot->ptp)
13585c5416f5SYangbo Lu 		return;
1359c0bcf537SYangbo Lu 
136052849bcfSVladimir Oltean 	if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
136152849bcfSVladimir Oltean 		dev_err_ratelimited(ds->dev,
136252849bcfSVladimir Oltean 				    "port %d delivering skb without TX timestamp\n",
136352849bcfSVladimir Oltean 				    port);
1364682eaad9SYangbo Lu 		return;
136552849bcfSVladimir Oltean 	}
1366682eaad9SYangbo Lu 
1367682eaad9SYangbo Lu 	if (clone)
1368c4b364ceSYangbo Lu 		OCELOT_SKB_CB(skb)->clone = clone;
13695c5416f5SYangbo Lu }
1370c0bcf537SYangbo Lu 
13710b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
13720b912fc9SVladimir Oltean {
13730b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
13740b912fc9SVladimir Oltean 
13750b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
13760b912fc9SVladimir Oltean 
13770b912fc9SVladimir Oltean 	return 0;
13780b912fc9SVladimir Oltean }
13790b912fc9SVladimir Oltean 
13800b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
13810b912fc9SVladimir Oltean {
13820b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
13830b912fc9SVladimir Oltean 
13840b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
13850b912fc9SVladimir Oltean }
13860b912fc9SVladimir Oltean 
138707d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
138807d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
138907d985eeSVladimir Oltean {
139007d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
139199348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
139299348004SVladimir Oltean 	bool using_tag_8021q;
139399348004SVladimir Oltean 	int err;
139407d985eeSVladimir Oltean 
139599348004SVladimir Oltean 	err = ocelot_cls_flower_replace(ocelot, port, cls, ingress);
139699348004SVladimir Oltean 	if (err)
139799348004SVladimir Oltean 		return err;
139899348004SVladimir Oltean 
139999348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
140099348004SVladimir Oltean 
140199348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
140207d985eeSVladimir Oltean }
140307d985eeSVladimir Oltean 
140407d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
140507d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
140607d985eeSVladimir Oltean {
140707d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
140807d985eeSVladimir Oltean 
140907d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
141007d985eeSVladimir Oltean }
141107d985eeSVladimir Oltean 
141207d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
141307d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
141407d985eeSVladimir Oltean {
141507d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
141607d985eeSVladimir Oltean 
141707d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
141807d985eeSVladimir Oltean }
141907d985eeSVladimir Oltean 
1420fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
1421fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
1422fc411eaaSVladimir Oltean {
1423fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1424fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
1425fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
14265f035af7SPo Liu 		.burst = policer->burst,
1427fc411eaaSVladimir Oltean 	};
1428fc411eaaSVladimir Oltean 
1429fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
1430fc411eaaSVladimir Oltean }
1431fc411eaaSVladimir Oltean 
1432fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
1433fc411eaaSVladimir Oltean {
1434fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1435fc411eaaSVladimir Oltean 
1436fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
1437fc411eaaSVladimir Oltean }
1438fc411eaaSVladimir Oltean 
1439de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
1440de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
1441de143c0eSXiaoliang Yang 			       void *type_data)
1442de143c0eSXiaoliang Yang {
1443de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
1444de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
1445de143c0eSXiaoliang Yang 
1446de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
1447de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
1448de143c0eSXiaoliang Yang 	else
1449de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
1450de143c0eSXiaoliang Yang }
1451de143c0eSXiaoliang Yang 
1452f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
1453f59fd9caSVladimir Oltean 			     u16 pool_index,
1454f59fd9caSVladimir Oltean 			     struct devlink_sb_pool_info *pool_info)
1455f59fd9caSVladimir Oltean {
1456f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1457f59fd9caSVladimir Oltean 
1458f59fd9caSVladimir Oltean 	return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
1459f59fd9caSVladimir Oltean }
1460f59fd9caSVladimir Oltean 
1461f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
1462f59fd9caSVladimir Oltean 			     u16 pool_index, u32 size,
1463f59fd9caSVladimir Oltean 			     enum devlink_sb_threshold_type threshold_type,
1464f59fd9caSVladimir Oltean 			     struct netlink_ext_ack *extack)
1465f59fd9caSVladimir Oltean {
1466f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1467f59fd9caSVladimir Oltean 
1468f59fd9caSVladimir Oltean 	return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
1469f59fd9caSVladimir Oltean 				  threshold_type, extack);
1470f59fd9caSVladimir Oltean }
1471f59fd9caSVladimir Oltean 
1472f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
1473f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1474f59fd9caSVladimir Oltean 				  u32 *p_threshold)
1475f59fd9caSVladimir Oltean {
1476f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1477f59fd9caSVladimir Oltean 
1478f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
1479f59fd9caSVladimir Oltean 				       p_threshold);
1480f59fd9caSVladimir Oltean }
1481f59fd9caSVladimir Oltean 
1482f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
1483f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1484f59fd9caSVladimir Oltean 				  u32 threshold, struct netlink_ext_ack *extack)
1485f59fd9caSVladimir Oltean {
1486f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1487f59fd9caSVladimir Oltean 
1488f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
1489f59fd9caSVladimir Oltean 				       threshold, extack);
1490f59fd9caSVladimir Oltean }
1491f59fd9caSVladimir Oltean 
1492f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
1493f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1494f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1495f59fd9caSVladimir Oltean 				     u16 *p_pool_index, u32 *p_threshold)
1496f59fd9caSVladimir Oltean {
1497f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1498f59fd9caSVladimir Oltean 
1499f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
1500f59fd9caSVladimir Oltean 					  pool_type, p_pool_index,
1501f59fd9caSVladimir Oltean 					  p_threshold);
1502f59fd9caSVladimir Oltean }
1503f59fd9caSVladimir Oltean 
1504f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
1505f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1506f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1507f59fd9caSVladimir Oltean 				     u16 pool_index, u32 threshold,
1508f59fd9caSVladimir Oltean 				     struct netlink_ext_ack *extack)
1509f59fd9caSVladimir Oltean {
1510f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1511f59fd9caSVladimir Oltean 
1512f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
1513f59fd9caSVladimir Oltean 					  pool_type, pool_index, threshold,
1514f59fd9caSVladimir Oltean 					  extack);
1515f59fd9caSVladimir Oltean }
1516f59fd9caSVladimir Oltean 
1517f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds,
1518f59fd9caSVladimir Oltean 				 unsigned int sb_index)
1519f59fd9caSVladimir Oltean {
1520f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1521f59fd9caSVladimir Oltean 
1522f59fd9caSVladimir Oltean 	return ocelot_sb_occ_snapshot(ocelot, sb_index);
1523f59fd9caSVladimir Oltean }
1524f59fd9caSVladimir Oltean 
1525f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds,
1526f59fd9caSVladimir Oltean 				  unsigned int sb_index)
1527f59fd9caSVladimir Oltean {
1528f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1529f59fd9caSVladimir Oltean 
1530f59fd9caSVladimir Oltean 	return ocelot_sb_occ_max_clear(ocelot, sb_index);
1531f59fd9caSVladimir Oltean }
1532f59fd9caSVladimir Oltean 
1533f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
1534f59fd9caSVladimir Oltean 				      unsigned int sb_index, u16 pool_index,
1535f59fd9caSVladimir Oltean 				      u32 *p_cur, u32 *p_max)
1536f59fd9caSVladimir Oltean {
1537f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1538f59fd9caSVladimir Oltean 
1539f59fd9caSVladimir Oltean 	return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
1540f59fd9caSVladimir Oltean 					   p_cur, p_max);
1541f59fd9caSVladimir Oltean }
1542f59fd9caSVladimir Oltean 
1543f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
1544f59fd9caSVladimir Oltean 					 unsigned int sb_index, u16 tc_index,
1545f59fd9caSVladimir Oltean 					 enum devlink_sb_pool_type pool_type,
1546f59fd9caSVladimir Oltean 					 u32 *p_cur, u32 *p_max)
1547f59fd9caSVladimir Oltean {
1548f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1549f59fd9caSVladimir Oltean 
1550f59fd9caSVladimir Oltean 	return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
1551f59fd9caSVladimir Oltean 					      pool_type, p_cur, p_max);
1552f59fd9caSVladimir Oltean }
1553f59fd9caSVladimir Oltean 
1554a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port,
1555a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1556a026c50bSHoratiu Vultur {
1557a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1558a026c50bSHoratiu Vultur 
1559a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1560a026c50bSHoratiu Vultur }
1561a026c50bSHoratiu Vultur 
1562a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port,
1563a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1564a026c50bSHoratiu Vultur {
1565a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1566a026c50bSHoratiu Vultur 
1567a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1568a026c50bSHoratiu Vultur }
1569a026c50bSHoratiu Vultur 
1570a026c50bSHoratiu Vultur static int
1571a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port,
1572a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1573a026c50bSHoratiu Vultur {
1574a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1575a026c50bSHoratiu Vultur 
1576a026c50bSHoratiu Vultur 	return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1577a026c50bSHoratiu Vultur }
1578a026c50bSHoratiu Vultur 
1579a026c50bSHoratiu Vultur static int
1580a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port,
1581a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1582a026c50bSHoratiu Vultur {
1583a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1584a026c50bSHoratiu Vultur 
1585a026c50bSHoratiu Vultur 	return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1586a026c50bSHoratiu Vultur }
1587a026c50bSHoratiu Vultur 
1588375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = {
158956051948SVladimir Oltean 	.get_tag_protocol		= felix_get_tag_protocol,
1590adb3dccfSVladimir Oltean 	.change_tag_protocol		= felix_change_tag_protocol,
159135d97680SVladimir Oltean 	.connect_tag_protocol		= felix_connect_tag_protocol,
159256051948SVladimir Oltean 	.setup				= felix_setup,
159356051948SVladimir Oltean 	.teardown			= felix_teardown,
159456051948SVladimir Oltean 	.set_ageing_time		= felix_set_ageing_time,
159556051948SVladimir Oltean 	.get_strings			= felix_get_strings,
159656051948SVladimir Oltean 	.get_ethtool_stats		= felix_get_ethtool_stats,
159756051948SVladimir Oltean 	.get_sset_count			= felix_get_sset_count,
159856051948SVladimir Oltean 	.get_ts_info			= felix_get_ts_info,
1599*79fda660SRussell King (Oracle) 	.phylink_get_caps		= felix_phylink_get_caps,
1600bdeced75SVladimir Oltean 	.phylink_validate		= felix_phylink_validate,
1601bdeced75SVladimir Oltean 	.phylink_mac_config		= felix_phylink_mac_config,
1602bdeced75SVladimir Oltean 	.phylink_mac_link_down		= felix_phylink_mac_link_down,
1603bdeced75SVladimir Oltean 	.phylink_mac_link_up		= felix_phylink_mac_link_up,
16045cad43a5SVladimir Oltean 	.port_fast_age			= felix_port_fast_age,
160556051948SVladimir Oltean 	.port_fdb_dump			= felix_fdb_dump,
160656051948SVladimir Oltean 	.port_fdb_add			= felix_fdb_add,
160756051948SVladimir Oltean 	.port_fdb_del			= felix_fdb_del,
1608961d8b69SVladimir Oltean 	.lag_fdb_add			= felix_lag_fdb_add,
1609961d8b69SVladimir Oltean 	.lag_fdb_del			= felix_lag_fdb_del,
1610209edf95SVladimir Oltean 	.port_mdb_add			= felix_mdb_add,
1611209edf95SVladimir Oltean 	.port_mdb_del			= felix_mdb_del,
1612421741eaSVladimir Oltean 	.port_pre_bridge_flags		= felix_pre_bridge_flags,
1613421741eaSVladimir Oltean 	.port_bridge_flags		= felix_bridge_flags,
161456051948SVladimir Oltean 	.port_bridge_join		= felix_bridge_join,
161556051948SVladimir Oltean 	.port_bridge_leave		= felix_bridge_leave,
16168fe6832eSVladimir Oltean 	.port_lag_join			= felix_lag_join,
16178fe6832eSVladimir Oltean 	.port_lag_leave			= felix_lag_leave,
16188fe6832eSVladimir Oltean 	.port_lag_change		= felix_lag_change,
161956051948SVladimir Oltean 	.port_stp_state_set		= felix_bridge_stp_state_set,
162056051948SVladimir Oltean 	.port_vlan_filtering		= felix_vlan_filtering,
162156051948SVladimir Oltean 	.port_vlan_add			= felix_vlan_add,
162256051948SVladimir Oltean 	.port_vlan_del			= felix_vlan_del,
1623c0bcf537SYangbo Lu 	.port_hwtstamp_get		= felix_hwtstamp_get,
1624c0bcf537SYangbo Lu 	.port_hwtstamp_set		= felix_hwtstamp_set,
1625c0bcf537SYangbo Lu 	.port_rxtstamp			= felix_rxtstamp,
1626c0bcf537SYangbo Lu 	.port_txtstamp			= felix_txtstamp,
16270b912fc9SVladimir Oltean 	.port_change_mtu		= felix_change_mtu,
16280b912fc9SVladimir Oltean 	.port_max_mtu			= felix_get_max_mtu,
1629fc411eaaSVladimir Oltean 	.port_policer_add		= felix_port_policer_add,
1630fc411eaaSVladimir Oltean 	.port_policer_del		= felix_port_policer_del,
163107d985eeSVladimir Oltean 	.cls_flower_add			= felix_cls_flower_add,
163207d985eeSVladimir Oltean 	.cls_flower_del			= felix_cls_flower_del,
163307d985eeSVladimir Oltean 	.cls_flower_stats		= felix_cls_flower_stats,
1634de143c0eSXiaoliang Yang 	.port_setup_tc			= felix_port_setup_tc,
1635f59fd9caSVladimir Oltean 	.devlink_sb_pool_get		= felix_sb_pool_get,
1636f59fd9caSVladimir Oltean 	.devlink_sb_pool_set		= felix_sb_pool_set,
1637f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_get	= felix_sb_port_pool_get,
1638f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_set	= felix_sb_port_pool_set,
1639f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_get	= felix_sb_tc_pool_bind_get,
1640f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_set	= felix_sb_tc_pool_bind_set,
1641f59fd9caSVladimir Oltean 	.devlink_sb_occ_snapshot	= felix_sb_occ_snapshot,
1642f59fd9caSVladimir Oltean 	.devlink_sb_occ_max_clear	= felix_sb_occ_max_clear,
1643f59fd9caSVladimir Oltean 	.devlink_sb_occ_port_pool_get	= felix_sb_occ_port_pool_get,
1644f59fd9caSVladimir Oltean 	.devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
1645a026c50bSHoratiu Vultur 	.port_mrp_add			= felix_mrp_add,
1646a026c50bSHoratiu Vultur 	.port_mrp_del			= felix_mrp_del,
1647a026c50bSHoratiu Vultur 	.port_mrp_add_ring_role		= felix_mrp_add_ring_role,
1648a026c50bSHoratiu Vultur 	.port_mrp_del_ring_role		= felix_mrp_del_ring_role,
16495da11eb4SVladimir Oltean 	.tag_8021q_vlan_add		= felix_tag_8021q_vlan_add,
16505da11eb4SVladimir Oltean 	.tag_8021q_vlan_del		= felix_tag_8021q_vlan_del,
165156051948SVladimir Oltean };
1652319e4dd1SVladimir Oltean 
1653319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
1654319e4dd1SVladimir Oltean {
1655319e4dd1SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1656319e4dd1SVladimir Oltean 	struct dsa_switch *ds = felix->ds;
1657319e4dd1SVladimir Oltean 
1658319e4dd1SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
1659319e4dd1SVladimir Oltean 		return NULL;
1660319e4dd1SVladimir Oltean 
1661319e4dd1SVladimir Oltean 	return dsa_to_port(ds, port)->slave;
1662319e4dd1SVladimir Oltean }
1663319e4dd1SVladimir Oltean 
1664319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev)
1665319e4dd1SVladimir Oltean {
1666319e4dd1SVladimir Oltean 	struct dsa_port *dp;
1667319e4dd1SVladimir Oltean 
1668319e4dd1SVladimir Oltean 	dp = dsa_port_from_netdev(dev);
1669319e4dd1SVladimir Oltean 	if (IS_ERR(dp))
1670319e4dd1SVladimir Oltean 		return -EINVAL;
1671319e4dd1SVladimir Oltean 
1672319e4dd1SVladimir Oltean 	return dp->index;
1673319e4dd1SVladimir Oltean }
1674