xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision a4e044dc4c5b1e03f4195224c47108dc44122917)
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 
28f9cef64fSVladimir Oltean /* Translate the DSA database API into the ocelot switch library API,
29f9cef64fSVladimir Oltean  * which uses VID 0 for all ports that aren't part of a bridge,
30f9cef64fSVladimir Oltean  * and expects the bridge_dev to be NULL in that case.
31f9cef64fSVladimir Oltean  */
32f9cef64fSVladimir Oltean static struct net_device *felix_classify_db(struct dsa_db db)
33f9cef64fSVladimir Oltean {
34f9cef64fSVladimir Oltean 	switch (db.type) {
35f9cef64fSVladimir Oltean 	case DSA_DB_PORT:
36f9cef64fSVladimir Oltean 	case DSA_DB_LAG:
37f9cef64fSVladimir Oltean 		return NULL;
38f9cef64fSVladimir Oltean 	case DSA_DB_BRIDGE:
39f9cef64fSVladimir Oltean 		return db.bridge.dev;
40f9cef64fSVladimir Oltean 	default:
41f9cef64fSVladimir Oltean 		return ERR_PTR(-EOPNOTSUPP);
42f9cef64fSVladimir Oltean 	}
43f9cef64fSVladimir Oltean }
44f9cef64fSVladimir Oltean 
4504b67e18SVladimir Oltean /* Set up VCAP ES0 rules for pushing a tag_8021q VLAN towards the CPU such that
4604b67e18SVladimir Oltean  * the tagger can perform RX source port identification.
4704b67e18SVladimir Oltean  */
48*a4e044dcSVladimir Oltean static int felix_tag_8021q_vlan_add_rx(struct dsa_switch *ds, int port,
49*a4e044dcSVladimir Oltean 				       int upstream, u16 vid)
50e21268efSVladimir Oltean {
51e21268efSVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
52*a4e044dcSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
53*a4e044dcSVladimir Oltean 	unsigned long cookie;
54*a4e044dcSVladimir Oltean 	int key_length, err;
55e21268efSVladimir Oltean 
56e21268efSVladimir Oltean 	key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length;
57e21268efSVladimir Oltean 
58e21268efSVladimir Oltean 	outer_tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter),
59e21268efSVladimir Oltean 				     GFP_KERNEL);
60e21268efSVladimir Oltean 	if (!outer_tagging_rule)
61e21268efSVladimir Oltean 		return -ENOMEM;
62e21268efSVladimir Oltean 
63*a4e044dcSVladimir Oltean 	cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port, upstream);
64*a4e044dcSVladimir Oltean 
65e21268efSVladimir Oltean 	outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
66e21268efSVladimir Oltean 	outer_tagging_rule->prio = 1;
67*a4e044dcSVladimir Oltean 	outer_tagging_rule->id.cookie = cookie;
68e21268efSVladimir Oltean 	outer_tagging_rule->id.tc_offload = false;
69e21268efSVladimir Oltean 	outer_tagging_rule->block_id = VCAP_ES0;
70e21268efSVladimir Oltean 	outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
71e21268efSVladimir Oltean 	outer_tagging_rule->lookup = 0;
72e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.value = port;
73e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0);
74e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.value = upstream;
75e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0);
76e21268efSVladimir Oltean 	outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG;
77e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD;
78e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_vid_sel = 1;
79e21268efSVladimir Oltean 	outer_tagging_rule->action.vid_a_val = vid;
80e21268efSVladimir Oltean 
81e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL);
82e21268efSVladimir Oltean 	if (err)
83e21268efSVladimir Oltean 		kfree(outer_tagging_rule);
84e21268efSVladimir Oltean 
85e21268efSVladimir Oltean 	return err;
86e21268efSVladimir Oltean }
87e21268efSVladimir Oltean 
88*a4e044dcSVladimir Oltean static int felix_tag_8021q_vlan_del_rx(struct dsa_switch *ds, int port,
89*a4e044dcSVladimir Oltean 				       int upstream, u16 vid)
9004b67e18SVladimir Oltean {
9104b67e18SVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
9204b67e18SVladimir Oltean 	struct ocelot_vcap_block *block_vcap_es0;
93*a4e044dcSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
94*a4e044dcSVladimir Oltean 	unsigned long cookie;
9504b67e18SVladimir Oltean 
9604b67e18SVladimir Oltean 	block_vcap_es0 = &ocelot->block[VCAP_ES0];
97*a4e044dcSVladimir Oltean 	cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port, upstream);
9804b67e18SVladimir Oltean 
9904b67e18SVladimir Oltean 	outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0,
100*a4e044dcSVladimir Oltean 								 cookie, false);
10104b67e18SVladimir Oltean 	if (!outer_tagging_rule)
10204b67e18SVladimir Oltean 		return -ENOENT;
10304b67e18SVladimir Oltean 
10404b67e18SVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, outer_tagging_rule);
10504b67e18SVladimir Oltean }
10604b67e18SVladimir Oltean 
10704b67e18SVladimir Oltean /* Set up VCAP IS1 rules for stripping the tag_8021q VLAN on TX and VCAP IS2
10804b67e18SVladimir Oltean  * rules for steering those tagged packets towards the correct destination port
10904b67e18SVladimir Oltean  */
110*a4e044dcSVladimir Oltean static int felix_tag_8021q_vlan_add_tx(struct dsa_switch *ds, int port,
111*a4e044dcSVladimir Oltean 				       u16 vid)
112e21268efSVladimir Oltean {
113e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
114*a4e044dcSVladimir Oltean 	unsigned long cpu_ports = dsa_cpu_ports(ds);
115*a4e044dcSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
116*a4e044dcSVladimir Oltean 	unsigned long cookie;
117*a4e044dcSVladimir Oltean 	int err;
118e21268efSVladimir Oltean 
119e21268efSVladimir Oltean 	untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
120e21268efSVladimir Oltean 	if (!untagging_rule)
121e21268efSVladimir Oltean 		return -ENOMEM;
122e21268efSVladimir Oltean 
123e21268efSVladimir Oltean 	redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
124e21268efSVladimir Oltean 	if (!redirect_rule) {
125e21268efSVladimir Oltean 		kfree(untagging_rule);
126e21268efSVladimir Oltean 		return -ENOMEM;
127e21268efSVladimir Oltean 	}
128e21268efSVladimir Oltean 
129*a4e044dcSVladimir Oltean 	cookie = OCELOT_VCAP_IS1_TAG_8021Q_TXVLAN(ocelot, port);
130e21268efSVladimir Oltean 
131e21268efSVladimir Oltean 	untagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
132*a4e044dcSVladimir Oltean 	untagging_rule->ingress_port_mask = cpu_ports;
133e21268efSVladimir Oltean 	untagging_rule->vlan.vid.value = vid;
134e21268efSVladimir Oltean 	untagging_rule->vlan.vid.mask = VLAN_VID_MASK;
135e21268efSVladimir Oltean 	untagging_rule->prio = 1;
136*a4e044dcSVladimir Oltean 	untagging_rule->id.cookie = cookie;
137e21268efSVladimir Oltean 	untagging_rule->id.tc_offload = false;
138e21268efSVladimir Oltean 	untagging_rule->block_id = VCAP_IS1;
139e21268efSVladimir Oltean 	untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
140e21268efSVladimir Oltean 	untagging_rule->lookup = 0;
141e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt_ena = true;
142e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt = 1;
143e21268efSVladimir Oltean 	untagging_rule->action.pag_override_mask = 0xff;
144e21268efSVladimir Oltean 	untagging_rule->action.pag_val = port;
145e21268efSVladimir Oltean 
146e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL);
147e21268efSVladimir Oltean 	if (err) {
148e21268efSVladimir Oltean 		kfree(untagging_rule);
149e21268efSVladimir Oltean 		kfree(redirect_rule);
150e21268efSVladimir Oltean 		return err;
151e21268efSVladimir Oltean 	}
152e21268efSVladimir Oltean 
153*a4e044dcSVladimir Oltean 	cookie = OCELOT_VCAP_IS2_TAG_8021Q_TXVLAN(ocelot, port);
154*a4e044dcSVladimir Oltean 
155e21268efSVladimir Oltean 	redirect_rule->key_type = OCELOT_VCAP_KEY_ANY;
156*a4e044dcSVladimir Oltean 	redirect_rule->ingress_port_mask = cpu_ports;
157e21268efSVladimir Oltean 	redirect_rule->pag = port;
158e21268efSVladimir Oltean 	redirect_rule->prio = 1;
159*a4e044dcSVladimir Oltean 	redirect_rule->id.cookie = cookie;
160e21268efSVladimir Oltean 	redirect_rule->id.tc_offload = false;
161e21268efSVladimir Oltean 	redirect_rule->block_id = VCAP_IS2;
162e21268efSVladimir Oltean 	redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
163e21268efSVladimir Oltean 	redirect_rule->lookup = 0;
164e21268efSVladimir Oltean 	redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT;
165e21268efSVladimir Oltean 	redirect_rule->action.port_mask = BIT(port);
166e21268efSVladimir Oltean 
167e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL);
168e21268efSVladimir Oltean 	if (err) {
169e21268efSVladimir Oltean 		ocelot_vcap_filter_del(ocelot, untagging_rule);
170e21268efSVladimir Oltean 		kfree(redirect_rule);
171e21268efSVladimir Oltean 		return err;
172e21268efSVladimir Oltean 	}
173e21268efSVladimir Oltean 
174e21268efSVladimir Oltean 	return 0;
175e21268efSVladimir Oltean }
176e21268efSVladimir Oltean 
177*a4e044dcSVladimir Oltean static int felix_tag_8021q_vlan_del_tx(struct dsa_switch *ds, int port, u16 vid)
178e21268efSVladimir Oltean {
179e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
180e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is1;
181e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
182*a4e044dcSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
183*a4e044dcSVladimir Oltean 	unsigned long cookie;
184e21268efSVladimir Oltean 	int err;
185e21268efSVladimir Oltean 
186e21268efSVladimir Oltean 	block_vcap_is1 = &ocelot->block[VCAP_IS1];
187e21268efSVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
188e21268efSVladimir Oltean 
189*a4e044dcSVladimir Oltean 	cookie = OCELOT_VCAP_IS1_TAG_8021Q_TXVLAN(ocelot, port);
190e21268efSVladimir Oltean 	untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1,
191*a4e044dcSVladimir Oltean 							     cookie, false);
192e21268efSVladimir Oltean 	if (!untagging_rule)
19308f44db3SVladimir Oltean 		return -ENOENT;
194e21268efSVladimir Oltean 
195e21268efSVladimir Oltean 	err = ocelot_vcap_filter_del(ocelot, untagging_rule);
196e21268efSVladimir Oltean 	if (err)
197e21268efSVladimir Oltean 		return err;
198e21268efSVladimir Oltean 
199*a4e044dcSVladimir Oltean 	cookie = OCELOT_VCAP_IS2_TAG_8021Q_TXVLAN(ocelot, port);
200e21268efSVladimir Oltean 	redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2,
201*a4e044dcSVladimir Oltean 							    cookie, false);
202e21268efSVladimir Oltean 	if (!redirect_rule)
20304b67e18SVladimir Oltean 		return -ENOENT;
204e21268efSVladimir Oltean 
205e21268efSVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, redirect_rule);
206e21268efSVladimir Oltean }
207e21268efSVladimir Oltean 
20804b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
20904b67e18SVladimir Oltean 				    u16 flags)
21004b67e18SVladimir Oltean {
211*a4e044dcSVladimir Oltean 	struct dsa_port *cpu_dp;
21204b67e18SVladimir Oltean 	int err;
21304b67e18SVladimir Oltean 
21404b67e18SVladimir Oltean 	/* tag_8021q.c assumes we are implementing this via port VLAN
21504b67e18SVladimir Oltean 	 * membership, which we aren't. So we don't need to add any VCAP filter
21604b67e18SVladimir Oltean 	 * for the CPU port.
21704b67e18SVladimir Oltean 	 */
21804b67e18SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
21904b67e18SVladimir Oltean 		return 0;
22004b67e18SVladimir Oltean 
221*a4e044dcSVladimir Oltean 	dsa_switch_for_each_cpu_port(cpu_dp, ds) {
222*a4e044dcSVladimir Oltean 		err = felix_tag_8021q_vlan_add_rx(ds, port, cpu_dp->index, vid);
22304b67e18SVladimir Oltean 		if (err)
22404b67e18SVladimir Oltean 			return err;
22504b67e18SVladimir Oltean 	}
22604b67e18SVladimir Oltean 
227*a4e044dcSVladimir Oltean 	err = felix_tag_8021q_vlan_add_tx(ds, port, vid);
228*a4e044dcSVladimir Oltean 	if (err)
229*a4e044dcSVladimir Oltean 		goto add_tx_failed;
230*a4e044dcSVladimir Oltean 
23104b67e18SVladimir Oltean 	return 0;
232*a4e044dcSVladimir Oltean 
233*a4e044dcSVladimir Oltean add_tx_failed:
234*a4e044dcSVladimir Oltean 	dsa_switch_for_each_cpu_port(cpu_dp, ds)
235*a4e044dcSVladimir Oltean 		felix_tag_8021q_vlan_del_rx(ds, port, cpu_dp->index, vid);
236*a4e044dcSVladimir Oltean 
237*a4e044dcSVladimir Oltean 	return err;
23804b67e18SVladimir Oltean }
23904b67e18SVladimir Oltean 
240e21268efSVladimir Oltean static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
241e21268efSVladimir Oltean {
242*a4e044dcSVladimir Oltean 	struct dsa_port *cpu_dp;
24304b67e18SVladimir Oltean 	int err;
244e21268efSVladimir Oltean 
24504b67e18SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
24604b67e18SVladimir Oltean 		return 0;
247e21268efSVladimir Oltean 
248*a4e044dcSVladimir Oltean 	dsa_switch_for_each_cpu_port(cpu_dp, ds) {
249*a4e044dcSVladimir Oltean 		err = felix_tag_8021q_vlan_del_rx(ds, port, cpu_dp->index, vid);
25004b67e18SVladimir Oltean 		if (err)
25104b67e18SVladimir Oltean 			return err;
25204b67e18SVladimir Oltean 	}
253e21268efSVladimir Oltean 
254*a4e044dcSVladimir Oltean 	err = felix_tag_8021q_vlan_del_tx(ds, port, vid);
255*a4e044dcSVladimir Oltean 	if (err)
256*a4e044dcSVladimir Oltean 		goto del_tx_failed;
257*a4e044dcSVladimir Oltean 
258e21268efSVladimir Oltean 	return 0;
259*a4e044dcSVladimir Oltean 
260*a4e044dcSVladimir Oltean del_tx_failed:
261*a4e044dcSVladimir Oltean 	dsa_switch_for_each_cpu_port(cpu_dp, ds)
262*a4e044dcSVladimir Oltean 		felix_tag_8021q_vlan_add_rx(ds, port, cpu_dp->index, vid);
263*a4e044dcSVladimir Oltean 
264*a4e044dcSVladimir Oltean 	return err;
265e21268efSVladimir Oltean }
266e21268efSVladimir Oltean 
267c352e5e8SVladimir Oltean static int felix_trap_get_cpu_port(struct dsa_switch *ds,
268c352e5e8SVladimir Oltean 				   const struct ocelot_vcap_filter *trap)
269c352e5e8SVladimir Oltean {
270c352e5e8SVladimir Oltean 	struct dsa_port *dp;
271c352e5e8SVladimir Oltean 	int first_port;
272c352e5e8SVladimir Oltean 
273c352e5e8SVladimir Oltean 	if (WARN_ON(!trap->ingress_port_mask))
274c352e5e8SVladimir Oltean 		return -1;
275c352e5e8SVladimir Oltean 
276c352e5e8SVladimir Oltean 	first_port = __ffs(trap->ingress_port_mask);
277c352e5e8SVladimir Oltean 	dp = dsa_to_port(ds, first_port);
278c352e5e8SVladimir Oltean 
279c352e5e8SVladimir Oltean 	return dp->cpu_dp->index;
280c352e5e8SVladimir Oltean }
281c352e5e8SVladimir Oltean 
28299348004SVladimir Oltean /* On switches with no extraction IRQ wired, trapped packets need to be
28399348004SVladimir Oltean  * replicated over Ethernet as well, otherwise we'd get no notification of
28499348004SVladimir Oltean  * their arrival when using the ocelot-8021q tagging protocol.
285c8c0ba4fSVladimir Oltean  */
28699348004SVladimir Oltean static int felix_update_trapping_destinations(struct dsa_switch *ds,
28799348004SVladimir Oltean 					      bool using_tag_8021q)
288c8c0ba4fSVladimir Oltean {
28999348004SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
29099348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
291e1846cffSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
29299348004SVladimir Oltean 	struct ocelot_vcap_filter *trap;
29399348004SVladimir Oltean 	enum ocelot_mask_mode mask_mode;
29499348004SVladimir Oltean 	unsigned long port_mask;
29599348004SVladimir Oltean 	bool cpu_copy_ena;
296c352e5e8SVladimir Oltean 	int err;
297c8c0ba4fSVladimir Oltean 
29899348004SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
29999348004SVladimir Oltean 		return 0;
300c8c0ba4fSVladimir Oltean 
301d78637a8SVladimir Oltean 	/* We are sure that "cpu" was found, otherwise
302d78637a8SVladimir Oltean 	 * dsa_tree_setup_default_cpu() would have failed earlier.
303d78637a8SVladimir Oltean 	 */
304e1846cffSVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
305c8c0ba4fSVladimir Oltean 
30699348004SVladimir Oltean 	/* Make sure all traps are set up for that destination */
307e1846cffSVladimir Oltean 	list_for_each_entry(trap, &block_vcap_is2->rules, list) {
308e1846cffSVladimir Oltean 		if (!trap->is_trap)
309e1846cffSVladimir Oltean 			continue;
310e1846cffSVladimir Oltean 
31199348004SVladimir Oltean 		/* Figure out the current trapping destination */
31299348004SVladimir Oltean 		if (using_tag_8021q) {
31399348004SVladimir Oltean 			/* Redirect to the tag_8021q CPU port. If timestamps
31499348004SVladimir Oltean 			 * are necessary, also copy trapped packets to the CPU
31599348004SVladimir Oltean 			 * port module.
316c8c0ba4fSVladimir Oltean 			 */
31799348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_REDIRECT;
318c352e5e8SVladimir Oltean 			port_mask = BIT(felix_trap_get_cpu_port(ds, trap));
31999348004SVladimir Oltean 			cpu_copy_ena = !!trap->take_ts;
320c8c0ba4fSVladimir Oltean 		} else {
32199348004SVladimir Oltean 			/* Trap packets only to the CPU port module, which is
32299348004SVladimir Oltean 			 * redirected to the NPI port (the DSA CPU port)
323c8c0ba4fSVladimir Oltean 			 */
32499348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
32599348004SVladimir Oltean 			port_mask = 0;
32699348004SVladimir Oltean 			cpu_copy_ena = true;
327c8c0ba4fSVladimir Oltean 		}
328c8c0ba4fSVladimir Oltean 
32999348004SVladimir Oltean 		if (trap->action.mask_mode == mask_mode &&
33099348004SVladimir Oltean 		    trap->action.port_mask == port_mask &&
33199348004SVladimir Oltean 		    trap->action.cpu_copy_ena == cpu_copy_ena)
33299348004SVladimir Oltean 			continue;
333c8c0ba4fSVladimir Oltean 
33499348004SVladimir Oltean 		trap->action.mask_mode = mask_mode;
33599348004SVladimir Oltean 		trap->action.port_mask = port_mask;
33699348004SVladimir Oltean 		trap->action.cpu_copy_ena = cpu_copy_ena;
3370a6f17c6SVladimir Oltean 
33899348004SVladimir Oltean 		err = ocelot_vcap_filter_replace(ocelot, trap);
339c8c0ba4fSVladimir Oltean 		if (err)
340c8c0ba4fSVladimir Oltean 			return err;
34199348004SVladimir Oltean 	}
342c8c0ba4fSVladimir Oltean 
34399348004SVladimir Oltean 	return 0;
344c8c0ba4fSVladimir Oltean }
345c8c0ba4fSVladimir Oltean 
346adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This
347adb3dccfSVladimir Oltean  * is the mode through which frames can be injected from and extracted to an
348adb3dccfSVladimir Oltean  * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU
349adb3dccfSVladimir Oltean  * running Linux, and this forms a DSA setup together with the enetc or fman
350adb3dccfSVladimir Oltean  * DSA master.
351adb3dccfSVladimir Oltean  */
352adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port)
353adb3dccfSVladimir Oltean {
354adb3dccfSVladimir Oltean 	ocelot->npi = port;
355adb3dccfSVladimir Oltean 
356adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
357adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
358adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
359adb3dccfSVladimir Oltean 
360adb3dccfSVladimir Oltean 	/* NPI port Injection/Extraction configuration */
361adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
362adb3dccfSVladimir Oltean 			    ocelot->npi_xtr_prefix);
363adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
364adb3dccfSVladimir Oltean 			    ocelot->npi_inj_prefix);
365adb3dccfSVladimir Oltean 
366adb3dccfSVladimir Oltean 	/* Disable transmission of pause frames */
367adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
368adb3dccfSVladimir Oltean }
369adb3dccfSVladimir Oltean 
370adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port)
371adb3dccfSVladimir Oltean {
372adb3dccfSVladimir Oltean 	/* Restore hardware defaults */
373adb3dccfSVladimir Oltean 	int unused_port = ocelot->num_phys_ports + 2;
374adb3dccfSVladimir Oltean 
375adb3dccfSVladimir Oltean 	ocelot->npi = -1;
376adb3dccfSVladimir Oltean 
377adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port),
378adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
379adb3dccfSVladimir Oltean 
380adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
381adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
382adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
383adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
384adb3dccfSVladimir Oltean 
385adb3dccfSVladimir Oltean 	/* Enable transmission of pause frames */
386adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
387adb3dccfSVladimir Oltean }
388adb3dccfSVladimir Oltean 
3897a29d220SVladimir Oltean static int felix_tag_npi_setup(struct dsa_switch *ds)
390adb3dccfSVladimir Oltean {
3917a29d220SVladimir Oltean 	struct dsa_port *dp, *first_cpu_dp = NULL;
392adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
393f9cef64fSVladimir Oltean 
3947a29d220SVladimir Oltean 	dsa_switch_for_each_user_port(dp, ds) {
3957a29d220SVladimir Oltean 		if (first_cpu_dp && dp->cpu_dp != first_cpu_dp) {
3967a29d220SVladimir Oltean 			dev_err(ds->dev, "Multiple NPI ports not supported\n");
3977a29d220SVladimir Oltean 			return -EINVAL;
3987a29d220SVladimir Oltean 		}
399b903a6bdSVladimir Oltean 
4007a29d220SVladimir Oltean 		first_cpu_dp = dp->cpu_dp;
4017a29d220SVladimir Oltean 	}
402adb3dccfSVladimir Oltean 
4037a29d220SVladimir Oltean 	if (!first_cpu_dp)
4047a29d220SVladimir Oltean 		return -EINVAL;
4057a29d220SVladimir Oltean 
4067a29d220SVladimir Oltean 	felix_npi_port_init(ocelot, first_cpu_dp->index);
407adb3dccfSVladimir Oltean 
408adb3dccfSVladimir Oltean 	return 0;
409adb3dccfSVladimir Oltean }
410adb3dccfSVladimir Oltean 
4117a29d220SVladimir Oltean static void felix_tag_npi_teardown(struct dsa_switch *ds)
412adb3dccfSVladimir Oltean {
413adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
414adb3dccfSVladimir Oltean 
4157a29d220SVladimir Oltean 	felix_npi_port_deinit(ocelot, ocelot->npi);
416adb3dccfSVladimir Oltean }
417adb3dccfSVladimir Oltean 
4187a29d220SVladimir Oltean static unsigned long felix_tag_npi_get_host_fwd_mask(struct dsa_switch *ds)
419adb3dccfSVladimir Oltean {
4207a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
4217a29d220SVladimir Oltean 
4227a29d220SVladimir Oltean 	return BIT(ocelot->num_phys_ports);
4237a29d220SVladimir Oltean }
4247a29d220SVladimir Oltean 
4258c166acbSVladimir Oltean /* Alternatively to using the NPI functionality, that same hardware MAC
4268c166acbSVladimir Oltean  * connected internally to the enetc or fman DSA master can be configured to
4278c166acbSVladimir Oltean  * use the software-defined tag_8021q frame format. As far as the hardware is
4288c166acbSVladimir Oltean  * concerned, it thinks it is a "dumb switch" - the queues of the CPU port
4298c166acbSVladimir Oltean  * module are now disconnected from it, but can still be accessed through
4308c166acbSVladimir Oltean  * register-based MMIO.
4318c166acbSVladimir Oltean  */
4327a29d220SVladimir Oltean static const struct felix_tag_proto_ops felix_tag_npi_proto_ops = {
4337a29d220SVladimir Oltean 	.setup			= felix_tag_npi_setup,
4347a29d220SVladimir Oltean 	.teardown		= felix_tag_npi_teardown,
4357a29d220SVladimir Oltean 	.get_host_fwd_mask	= felix_tag_npi_get_host_fwd_mask,
4367a29d220SVladimir Oltean };
4377a29d220SVladimir Oltean 
4387a29d220SVladimir Oltean static int felix_tag_8021q_setup(struct dsa_switch *ds)
4397a29d220SVladimir Oltean {
4407a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
441c295f983SVladimir Oltean 	struct dsa_port *dp;
442adb3dccfSVladimir Oltean 	int err;
443adb3dccfSVladimir Oltean 
4447a29d220SVladimir Oltean 	err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD));
4457a29d220SVladimir Oltean 	if (err)
446adb3dccfSVladimir Oltean 		return err;
4477a29d220SVladimir Oltean 
448c295f983SVladimir Oltean 	dsa_switch_for_each_user_port(dp, ds)
449c295f983SVladimir Oltean 		ocelot_port_assign_dsa_8021q_cpu(ocelot, dp->index,
450c295f983SVladimir Oltean 						 dp->cpu_dp->index);
4517a29d220SVladimir Oltean 
452c295f983SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
4537a29d220SVladimir Oltean 		/* This overwrites ocelot_init():
4547a29d220SVladimir Oltean 		 * Do not forward BPDU frames to the CPU port module,
4557a29d220SVladimir Oltean 		 * for 2 reasons:
4567a29d220SVladimir Oltean 		 * - When these packets are injected from the tag_8021q
4577a29d220SVladimir Oltean 		 *   CPU port, we want them to go out, not loop back
4587a29d220SVladimir Oltean 		 *   into the system.
4597a29d220SVladimir Oltean 		 * - STP traffic ingressing on a user port should go to
4607a29d220SVladimir Oltean 		 *   the tag_8021q CPU port, not to the hardware CPU
4617a29d220SVladimir Oltean 		 *   port module.
4627a29d220SVladimir Oltean 		 */
4637a29d220SVladimir Oltean 		ocelot_write_gix(ocelot,
4647a29d220SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0),
4657a29d220SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG, dp->index);
4667a29d220SVladimir Oltean 
4677a29d220SVladimir Oltean 	/* The ownership of the CPU port module's queues might have just been
4687a29d220SVladimir Oltean 	 * transferred to the tag_8021q tagger from the NPI-based tagger.
4697a29d220SVladimir Oltean 	 * So there might still be all sorts of crap in the queues. On the
4707a29d220SVladimir Oltean 	 * other hand, the MMIO-based matching of PTP frames is very brittle,
4717a29d220SVladimir Oltean 	 * so we need to be careful that there are no extra frames to be
4727a29d220SVladimir Oltean 	 * dequeued over MMIO, since we would never know to discard them.
4737a29d220SVladimir Oltean 	 */
4747a29d220SVladimir Oltean 	ocelot_drain_cpu_queue(ocelot, 0);
4757a29d220SVladimir Oltean 
4767a29d220SVladimir Oltean 	return 0;
4777a29d220SVladimir Oltean }
4787a29d220SVladimir Oltean 
4797a29d220SVladimir Oltean static void felix_tag_8021q_teardown(struct dsa_switch *ds)
480adb3dccfSVladimir Oltean {
4817a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
482c295f983SVladimir Oltean 	struct dsa_port *dp;
4837a29d220SVladimir Oltean 
484c295f983SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
4857a29d220SVladimir Oltean 		/* Restore the logic from ocelot_init:
4867a29d220SVladimir Oltean 		 * do not forward BPDU frames to the front ports.
4877a29d220SVladimir Oltean 		 */
4887a29d220SVladimir Oltean 		ocelot_write_gix(ocelot,
4897a29d220SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
4907a29d220SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG,
4917a29d220SVladimir Oltean 				 dp->index);
4927a29d220SVladimir Oltean 
493c295f983SVladimir Oltean 	dsa_switch_for_each_user_port(dp, ds)
494c295f983SVladimir Oltean 		ocelot_port_unassign_dsa_8021q_cpu(ocelot, dp->index);
4957a29d220SVladimir Oltean 
4967a29d220SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
4977a29d220SVladimir Oltean }
4987a29d220SVladimir Oltean 
4997a29d220SVladimir Oltean static unsigned long felix_tag_8021q_get_host_fwd_mask(struct dsa_switch *ds)
5007a29d220SVladimir Oltean {
5017a29d220SVladimir Oltean 	return dsa_cpu_ports(ds);
5027a29d220SVladimir Oltean }
5037a29d220SVladimir Oltean 
5047a29d220SVladimir Oltean static const struct felix_tag_proto_ops felix_tag_8021q_proto_ops = {
5057a29d220SVladimir Oltean 	.setup			= felix_tag_8021q_setup,
5067a29d220SVladimir Oltean 	.teardown		= felix_tag_8021q_teardown,
5077a29d220SVladimir Oltean 	.get_host_fwd_mask	= felix_tag_8021q_get_host_fwd_mask,
5087a29d220SVladimir Oltean };
5097a29d220SVladimir Oltean 
5107a29d220SVladimir Oltean static void felix_set_host_flood(struct dsa_switch *ds, unsigned long mask,
5117a29d220SVladimir Oltean 				 bool uc, bool mc, bool bc)
5127a29d220SVladimir Oltean {
5137a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5147a29d220SVladimir Oltean 	unsigned long val;
5157a29d220SVladimir Oltean 
5167a29d220SVladimir Oltean 	val = uc ? mask : 0;
5177a29d220SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_UC);
5187a29d220SVladimir Oltean 
5197a29d220SVladimir Oltean 	val = mc ? mask : 0;
5207a29d220SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MC);
5217a29d220SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MCIPV4);
5227a29d220SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MCIPV6);
523129b7532SVladimir Oltean 
524129b7532SVladimir Oltean 	val = bc ? mask : 0;
525129b7532SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_BC);
5267a29d220SVladimir Oltean }
5277a29d220SVladimir Oltean 
5287a29d220SVladimir Oltean static void
5297a29d220SVladimir Oltean felix_migrate_host_flood(struct dsa_switch *ds,
5307a29d220SVladimir Oltean 			 const struct felix_tag_proto_ops *proto_ops,
5317a29d220SVladimir Oltean 			 const struct felix_tag_proto_ops *old_proto_ops)
5327a29d220SVladimir Oltean {
5337a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5347a29d220SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
5357a29d220SVladimir Oltean 	unsigned long mask;
5367a29d220SVladimir Oltean 
5377a29d220SVladimir Oltean 	if (old_proto_ops) {
5387a29d220SVladimir Oltean 		mask = old_proto_ops->get_host_fwd_mask(ds);
5397a29d220SVladimir Oltean 		felix_set_host_flood(ds, mask, false, false, false);
5407a29d220SVladimir Oltean 	}
5417a29d220SVladimir Oltean 
5427a29d220SVladimir Oltean 	mask = proto_ops->get_host_fwd_mask(ds);
5437a29d220SVladimir Oltean 	felix_set_host_flood(ds, mask, !!felix->host_flood_uc_mask,
5447a29d220SVladimir Oltean 			     !!felix->host_flood_mc_mask, true);
5457a29d220SVladimir Oltean }
5467a29d220SVladimir Oltean 
5477a29d220SVladimir Oltean static int felix_migrate_mdbs(struct dsa_switch *ds,
5487a29d220SVladimir Oltean 			      const struct felix_tag_proto_ops *proto_ops,
5497a29d220SVladimir Oltean 			      const struct felix_tag_proto_ops *old_proto_ops)
5507a29d220SVladimir Oltean {
5517a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5527a29d220SVladimir Oltean 	unsigned long from, to;
5537a29d220SVladimir Oltean 
5547a29d220SVladimir Oltean 	if (!old_proto_ops)
5557a29d220SVladimir Oltean 		return 0;
5567a29d220SVladimir Oltean 
5577a29d220SVladimir Oltean 	from = old_proto_ops->get_host_fwd_mask(ds);
5587a29d220SVladimir Oltean 	to = proto_ops->get_host_fwd_mask(ds);
5597a29d220SVladimir Oltean 
5607a29d220SVladimir Oltean 	return ocelot_migrate_mdbs(ocelot, from, to);
5617a29d220SVladimir Oltean }
5627a29d220SVladimir Oltean 
5637a29d220SVladimir Oltean /* Configure the shared hardware resources for a transition between
5647a29d220SVladimir Oltean  * @old_proto_ops and @proto_ops.
5657a29d220SVladimir Oltean  * Manual migration is needed because as far as DSA is concerned, no change of
5667a29d220SVladimir Oltean  * the CPU port is taking place here, just of the tagging protocol.
5677a29d220SVladimir Oltean  */
5687a29d220SVladimir Oltean static int
5697a29d220SVladimir Oltean felix_tag_proto_setup_shared(struct dsa_switch *ds,
5707a29d220SVladimir Oltean 			     const struct felix_tag_proto_ops *proto_ops,
5717a29d220SVladimir Oltean 			     const struct felix_tag_proto_ops *old_proto_ops)
5727a29d220SVladimir Oltean {
5737a29d220SVladimir Oltean 	bool using_tag_8021q = (proto_ops == &felix_tag_8021q_proto_ops);
5747a29d220SVladimir Oltean 	int err;
5757a29d220SVladimir Oltean 
5767a29d220SVladimir Oltean 	err = felix_migrate_mdbs(ds, proto_ops, old_proto_ops);
5777a29d220SVladimir Oltean 	if (err)
5787a29d220SVladimir Oltean 		return err;
5797a29d220SVladimir Oltean 
5807a29d220SVladimir Oltean 	felix_update_trapping_destinations(ds, using_tag_8021q);
5817a29d220SVladimir Oltean 
5827a29d220SVladimir Oltean 	felix_migrate_host_flood(ds, proto_ops, old_proto_ops);
5837a29d220SVladimir Oltean 
5847a29d220SVladimir Oltean 	return 0;
585adb3dccfSVladimir Oltean }
586adb3dccfSVladimir Oltean 
587e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the
588e21268efSVladimir Oltean  * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
589e21268efSVladimir Oltean  * or the restoration is guaranteed to work.
590e21268efSVladimir Oltean  */
591bacf93b0SVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds,
592adb3dccfSVladimir Oltean 				     enum dsa_tag_protocol proto)
593adb3dccfSVladimir Oltean {
5947a29d220SVladimir Oltean 	const struct felix_tag_proto_ops *old_proto_ops, *proto_ops;
595adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
596adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
597adb3dccfSVladimir Oltean 	int err;
598adb3dccfSVladimir Oltean 
5997a29d220SVladimir Oltean 	switch (proto) {
6007a29d220SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
6017a29d220SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
6027a29d220SVladimir Oltean 		proto_ops = &felix_tag_npi_proto_ops;
603bacf93b0SVladimir Oltean 		break;
6047a29d220SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
6057a29d220SVladimir Oltean 		proto_ops = &felix_tag_8021q_proto_ops;
6067a29d220SVladimir Oltean 		break;
6077a29d220SVladimir Oltean 	default:
6087a29d220SVladimir Oltean 		return -EPROTONOSUPPORT;
609bacf93b0SVladimir Oltean 	}
610bacf93b0SVladimir Oltean 
6117a29d220SVladimir Oltean 	old_proto_ops = felix->tag_proto_ops;
6127a29d220SVladimir Oltean 
6137a29d220SVladimir Oltean 	err = proto_ops->setup(ds);
6147a29d220SVladimir Oltean 	if (err)
6157a29d220SVladimir Oltean 		goto setup_failed;
6167a29d220SVladimir Oltean 
6177a29d220SVladimir Oltean 	err = felix_tag_proto_setup_shared(ds, proto_ops, old_proto_ops);
6187a29d220SVladimir Oltean 	if (err)
6197a29d220SVladimir Oltean 		goto setup_shared_failed;
6207a29d220SVladimir Oltean 
6217a29d220SVladimir Oltean 	if (old_proto_ops)
6227a29d220SVladimir Oltean 		old_proto_ops->teardown(ds);
6237a29d220SVladimir Oltean 
6247a29d220SVladimir Oltean 	felix->tag_proto_ops = proto_ops;
625adb3dccfSVladimir Oltean 	felix->tag_proto = proto;
626adb3dccfSVladimir Oltean 
627adb3dccfSVladimir Oltean 	return 0;
6287a29d220SVladimir Oltean 
6297a29d220SVladimir Oltean setup_shared_failed:
6307a29d220SVladimir Oltean 	proto_ops->teardown(ds);
6317a29d220SVladimir Oltean setup_failed:
6327a29d220SVladimir Oltean 	return err;
633adb3dccfSVladimir Oltean }
634adb3dccfSVladimir Oltean 
63556051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
6364d776482SFlorian Fainelli 						    int port,
6374d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
63856051948SVladimir Oltean {
639adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
640adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
641adb3dccfSVladimir Oltean 
642adb3dccfSVladimir Oltean 	return felix->tag_proto;
64356051948SVladimir Oltean }
64456051948SVladimir Oltean 
64572c3b0c7SVladimir Oltean static void felix_port_set_host_flood(struct dsa_switch *ds, int port,
64672c3b0c7SVladimir Oltean 				      bool uc, bool mc)
64772c3b0c7SVladimir Oltean {
64872c3b0c7SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
64972c3b0c7SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
6507a29d220SVladimir Oltean 	unsigned long mask;
65172c3b0c7SVladimir Oltean 
65272c3b0c7SVladimir Oltean 	if (uc)
65372c3b0c7SVladimir Oltean 		felix->host_flood_uc_mask |= BIT(port);
65472c3b0c7SVladimir Oltean 	else
65572c3b0c7SVladimir Oltean 		felix->host_flood_uc_mask &= ~BIT(port);
65672c3b0c7SVladimir Oltean 
65772c3b0c7SVladimir Oltean 	if (mc)
65872c3b0c7SVladimir Oltean 		felix->host_flood_mc_mask |= BIT(port);
65972c3b0c7SVladimir Oltean 	else
66072c3b0c7SVladimir Oltean 		felix->host_flood_mc_mask &= ~BIT(port);
66172c3b0c7SVladimir Oltean 
6627a29d220SVladimir Oltean 	mask = felix->tag_proto_ops->get_host_fwd_mask(ds);
6637a29d220SVladimir Oltean 	felix_set_host_flood(ds, mask, !!felix->host_flood_uc_mask,
6647a29d220SVladimir Oltean 			     !!felix->host_flood_mc_mask, true);
66572c3b0c7SVladimir Oltean }
66672c3b0c7SVladimir Oltean 
66756051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
66856051948SVladimir Oltean 				 unsigned int ageing_time)
66956051948SVladimir Oltean {
67056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
67156051948SVladimir Oltean 
67256051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
67356051948SVladimir Oltean 
67456051948SVladimir Oltean 	return 0;
67556051948SVladimir Oltean }
67656051948SVladimir Oltean 
6775cad43a5SVladimir Oltean static void felix_port_fast_age(struct dsa_switch *ds, int port)
6785cad43a5SVladimir Oltean {
6795cad43a5SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
6805cad43a5SVladimir Oltean 	int err;
6815cad43a5SVladimir Oltean 
6825cad43a5SVladimir Oltean 	err = ocelot_mact_flush(ocelot, port);
6835cad43a5SVladimir Oltean 	if (err)
6845cad43a5SVladimir Oltean 		dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n",
6855cad43a5SVladimir Oltean 			port, ERR_PTR(err));
6865cad43a5SVladimir Oltean }
6875cad43a5SVladimir Oltean 
68856051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
68956051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
69056051948SVladimir Oltean {
69156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
69256051948SVladimir Oltean 
69356051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
69456051948SVladimir Oltean }
69556051948SVladimir Oltean 
69656051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
697c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
698c2693363SVladimir Oltean 			 struct dsa_db db)
69956051948SVladimir Oltean {
70054c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
701e9b3ba43SVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
70256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
70356051948SVladimir Oltean 
70454c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
70554c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
70654c31984SVladimir Oltean 
707e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp) && !bridge_dev &&
7087e580490SVladimir Oltean 	    dsa_fdb_present_in_other_db(ds, port, addr, vid, db))
7097e580490SVladimir Oltean 		return 0;
7107e580490SVladimir Oltean 
711e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp))
712e9b3ba43SVladimir Oltean 		port = PGID_CPU;
713e9b3ba43SVladimir Oltean 
71454c31984SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid, bridge_dev);
71556051948SVladimir Oltean }
71656051948SVladimir Oltean 
71756051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
718c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
719c2693363SVladimir Oltean 			 struct dsa_db db)
72056051948SVladimir Oltean {
72154c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
722e9b3ba43SVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
72356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
72456051948SVladimir Oltean 
72554c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
72654c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
72754c31984SVladimir Oltean 
728e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp) && !bridge_dev &&
7297e580490SVladimir Oltean 	    dsa_fdb_present_in_other_db(ds, port, addr, vid, db))
7307e580490SVladimir Oltean 		return 0;
7317e580490SVladimir Oltean 
732e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp))
733e9b3ba43SVladimir Oltean 		port = PGID_CPU;
734e9b3ba43SVladimir Oltean 
73554c31984SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid, bridge_dev);
73656051948SVladimir Oltean }
73756051948SVladimir Oltean 
738961d8b69SVladimir Oltean static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag,
739c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
740c2693363SVladimir Oltean 			     struct dsa_db db)
741961d8b69SVladimir Oltean {
74254c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
743961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
744961d8b69SVladimir Oltean 
74554c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
74654c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
74754c31984SVladimir Oltean 
74854c31984SVladimir Oltean 	return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid, bridge_dev);
749961d8b69SVladimir Oltean }
750961d8b69SVladimir Oltean 
751961d8b69SVladimir Oltean static int felix_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag lag,
752c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
753c2693363SVladimir Oltean 			     struct dsa_db db)
754961d8b69SVladimir Oltean {
75554c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
756961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
757961d8b69SVladimir Oltean 
75854c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
75954c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
76054c31984SVladimir Oltean 
76154c31984SVladimir Oltean 	return ocelot_lag_fdb_del(ocelot, lag.dev, addr, vid, bridge_dev);
762961d8b69SVladimir Oltean }
763961d8b69SVladimir Oltean 
764a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port,
765c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
766c2693363SVladimir Oltean 			 struct dsa_db db)
767209edf95SVladimir Oltean {
76854c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
769209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
770209edf95SVladimir Oltean 
77154c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
77254c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
77354c31984SVladimir Oltean 
7747e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
7757e580490SVladimir Oltean 	    dsa_mdb_present_in_other_db(ds, port, mdb, db))
7767e580490SVladimir Oltean 		return 0;
7777e580490SVladimir Oltean 
7780ddf83cdSVladimir Oltean 	if (port == ocelot->npi)
7790ddf83cdSVladimir Oltean 		port = ocelot->num_phys_ports;
7800ddf83cdSVladimir Oltean 
78154c31984SVladimir Oltean 	return ocelot_port_mdb_add(ocelot, port, mdb, bridge_dev);
782209edf95SVladimir Oltean }
783209edf95SVladimir Oltean 
784209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port,
785c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
786c2693363SVladimir Oltean 			 struct dsa_db db)
787209edf95SVladimir Oltean {
78854c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
789209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
790209edf95SVladimir Oltean 
79154c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
79254c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
79354c31984SVladimir Oltean 
7947e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
7957e580490SVladimir Oltean 	    dsa_mdb_present_in_other_db(ds, port, mdb, db))
7967e580490SVladimir Oltean 		return 0;
7977e580490SVladimir Oltean 
7980ddf83cdSVladimir Oltean 	if (port == ocelot->npi)
7990ddf83cdSVladimir Oltean 		port = ocelot->num_phys_ports;
8000ddf83cdSVladimir Oltean 
80154c31984SVladimir Oltean 	return ocelot_port_mdb_del(ocelot, port, mdb, bridge_dev);
802209edf95SVladimir Oltean }
803209edf95SVladimir Oltean 
80456051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
80556051948SVladimir Oltean 				       u8 state)
80656051948SVladimir Oltean {
80756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
80856051948SVladimir Oltean 
80956051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
81056051948SVladimir Oltean }
81156051948SVladimir Oltean 
812421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port,
813421741eaSVladimir Oltean 				  struct switchdev_brport_flags val,
814421741eaSVladimir Oltean 				  struct netlink_ext_ack *extack)
815421741eaSVladimir Oltean {
816421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
817421741eaSVladimir Oltean 
818421741eaSVladimir Oltean 	return ocelot_port_pre_bridge_flags(ocelot, port, val);
819421741eaSVladimir Oltean }
820421741eaSVladimir Oltean 
821421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port,
822421741eaSVladimir Oltean 			      struct switchdev_brport_flags val,
823421741eaSVladimir Oltean 			      struct netlink_ext_ack *extack)
824421741eaSVladimir Oltean {
825421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
826421741eaSVladimir Oltean 
827910ee6ccSVladimir Oltean 	if (port == ocelot->npi)
828910ee6ccSVladimir Oltean 		port = ocelot->num_phys_ports;
829910ee6ccSVladimir Oltean 
830421741eaSVladimir Oltean 	ocelot_port_bridge_flags(ocelot, port, val);
831421741eaSVladimir Oltean 
832421741eaSVladimir Oltean 	return 0;
833421741eaSVladimir Oltean }
834421741eaSVladimir Oltean 
83556051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
83606b9cce4SVladimir Oltean 			     struct dsa_bridge bridge, bool *tx_fwd_offload,
83706b9cce4SVladimir Oltean 			     struct netlink_ext_ack *extack)
83856051948SVladimir Oltean {
83956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
84056051948SVladimir Oltean 
84154c31984SVladimir Oltean 	return ocelot_port_bridge_join(ocelot, port, bridge.dev, bridge.num,
84254c31984SVladimir Oltean 				       extack);
84356051948SVladimir Oltean }
84456051948SVladimir Oltean 
84556051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
846d3eed0e5SVladimir Oltean 			       struct dsa_bridge bridge)
84756051948SVladimir Oltean {
84856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
84956051948SVladimir Oltean 
850d3eed0e5SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, bridge.dev);
85156051948SVladimir Oltean }
85256051948SVladimir Oltean 
8538fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port,
854dedd6a00SVladimir Oltean 			  struct dsa_lag lag,
8558fe6832eSVladimir Oltean 			  struct netdev_lag_upper_info *info)
8568fe6832eSVladimir Oltean {
8578fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8588fe6832eSVladimir Oltean 
859dedd6a00SVladimir Oltean 	return ocelot_port_lag_join(ocelot, port, lag.dev, info);
8608fe6832eSVladimir Oltean }
8618fe6832eSVladimir Oltean 
8628fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port,
863dedd6a00SVladimir Oltean 			   struct dsa_lag lag)
8648fe6832eSVladimir Oltean {
8658fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8668fe6832eSVladimir Oltean 
867dedd6a00SVladimir Oltean 	ocelot_port_lag_leave(ocelot, port, lag.dev);
8688fe6832eSVladimir Oltean 
8698fe6832eSVladimir Oltean 	return 0;
8708fe6832eSVladimir Oltean }
8718fe6832eSVladimir Oltean 
8728fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port)
8738fe6832eSVladimir Oltean {
8748fe6832eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
8758fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8768fe6832eSVladimir Oltean 
8778fe6832eSVladimir Oltean 	ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
8788fe6832eSVladimir Oltean 
8798fe6832eSVladimir Oltean 	return 0;
8808fe6832eSVladimir Oltean }
8818fe6832eSVladimir Oltean 
88256051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
88301af940eSVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan,
88401af940eSVladimir Oltean 			      struct netlink_ext_ack *extack)
88556051948SVladimir Oltean {
8862f0402feSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
887b7a9e0daSVladimir Oltean 	u16 flags = vlan->flags;
8882f0402feSVladimir Oltean 
8899a720680SVladimir Oltean 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
8909a720680SVladimir Oltean 	 * egress-untagged or not, pvid or not, make no difference. This
8919a720680SVladimir Oltean 	 * behavior is already better than what DSA just tries to approximate
8929a720680SVladimir Oltean 	 * when it installs the VLAN with the same flags on the CPU port.
8939a720680SVladimir Oltean 	 * Just accept any configuration, and don't let ocelot deny installing
8949a720680SVladimir Oltean 	 * multiple native VLANs on the NPI port, because the switch doesn't
8959a720680SVladimir Oltean 	 * look at the port tag settings towards the NPI interface anyway.
8969a720680SVladimir Oltean 	 */
8979a720680SVladimir Oltean 	if (port == ocelot->npi)
8989a720680SVladimir Oltean 		return 0;
8999a720680SVladimir Oltean 
900b7a9e0daSVladimir Oltean 	return ocelot_vlan_prepare(ocelot, port, vlan->vid,
9012f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_PVID,
90201af940eSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_UNTAGGED,
90301af940eSVladimir Oltean 				   extack);
90456051948SVladimir Oltean }
90556051948SVladimir Oltean 
90689153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
90789153ed6SVladimir Oltean 				struct netlink_ext_ack *extack)
90856051948SVladimir Oltean {
90956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
91056051948SVladimir Oltean 
9113b95d1b2SVladimir Oltean 	return ocelot_port_vlan_filtering(ocelot, port, enabled, extack);
91256051948SVladimir Oltean }
91356051948SVladimir Oltean 
9141958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port,
91531046a5fSVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan,
91631046a5fSVladimir Oltean 			  struct netlink_ext_ack *extack)
91756051948SVladimir Oltean {
91856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
919183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
9201958d581SVladimir Oltean 	int err;
92156051948SVladimir Oltean 
92201af940eSVladimir Oltean 	err = felix_vlan_prepare(ds, port, vlan, extack);
9231958d581SVladimir Oltean 	if (err)
9241958d581SVladimir Oltean 		return err;
9251958d581SVladimir Oltean 
9261958d581SVladimir Oltean 	return ocelot_vlan_add(ocelot, port, vlan->vid,
927183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_PVID,
928183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_UNTAGGED);
92956051948SVladimir Oltean }
93056051948SVladimir Oltean 
93156051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
93256051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
93356051948SVladimir Oltean {
93456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
93556051948SVladimir Oltean 
936b7a9e0daSVladimir Oltean 	return ocelot_vlan_del(ocelot, port, vlan->vid);
93756051948SVladimir Oltean }
93856051948SVladimir Oltean 
93979fda660SRussell King (Oracle) static void felix_phylink_get_caps(struct dsa_switch *ds, int port,
94079fda660SRussell King (Oracle) 				   struct phylink_config *config)
94179fda660SRussell King (Oracle) {
94279fda660SRussell King (Oracle) 	struct ocelot *ocelot = ds->priv;
94379fda660SRussell King (Oracle) 
944f6f04c02SRussell King (Oracle) 	/* This driver does not make use of the speed, duplex, pause or the
945f6f04c02SRussell King (Oracle) 	 * advertisement in its mac_config, so it is safe to mark this driver
946f6f04c02SRussell King (Oracle) 	 * as non-legacy.
947f6f04c02SRussell King (Oracle) 	 */
948f6f04c02SRussell King (Oracle) 	config->legacy_pre_march2020 = false;
949f6f04c02SRussell King (Oracle) 
95079fda660SRussell King (Oracle) 	__set_bit(ocelot->ports[port]->phy_mode,
95179fda660SRussell King (Oracle) 		  config->supported_interfaces);
95279fda660SRussell King (Oracle) }
95379fda660SRussell King (Oracle) 
954bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
955bdeced75SVladimir Oltean 				   unsigned long *supported,
956bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
957bdeced75SVladimir Oltean {
958bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
959375e1314SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
960bdeced75SVladimir Oltean 
961375e1314SVladimir Oltean 	if (felix->info->phylink_validate)
962375e1314SVladimir Oltean 		felix->info->phylink_validate(ocelot, port, supported, state);
963bdeced75SVladimir Oltean }
964bdeced75SVladimir Oltean 
965864ba485SRussell King (Oracle) static struct phylink_pcs *felix_phylink_mac_select_pcs(struct dsa_switch *ds,
966864ba485SRussell King (Oracle) 							int port,
967864ba485SRussell King (Oracle) 							phy_interface_t iface)
968bdeced75SVladimir Oltean {
969bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
970bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
971864ba485SRussell King (Oracle) 	struct phylink_pcs *pcs = NULL;
972bdeced75SVladimir Oltean 
97349af6a76SColin Foster 	if (felix->pcs && felix->pcs[port])
974864ba485SRussell King (Oracle) 		pcs = felix->pcs[port];
975864ba485SRussell King (Oracle) 
976864ba485SRussell King (Oracle) 	return pcs;
977bdeced75SVladimir Oltean }
978bdeced75SVladimir Oltean 
979bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
980bdeced75SVladimir Oltean 					unsigned int link_an_mode,
981bdeced75SVladimir Oltean 					phy_interface_t interface)
982bdeced75SVladimir Oltean {
983bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
984bdeced75SVladimir Oltean 
985e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
986e6e12df6SVladimir Oltean 				     FELIX_MAC_QUIRKS);
987bdeced75SVladimir Oltean }
988bdeced75SVladimir Oltean 
989bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
990bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
991bdeced75SVladimir Oltean 				      phy_interface_t interface,
9925b502a7bSRussell King 				      struct phy_device *phydev,
9935b502a7bSRussell King 				      int speed, int duplex,
9945b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
995bdeced75SVladimir Oltean {
996bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
9977e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
998bdeced75SVladimir Oltean 
999e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
1000e6e12df6SVladimir Oltean 				   interface, speed, duplex, tx_pause, rx_pause,
1001e6e12df6SVladimir Oltean 				   FELIX_MAC_QUIRKS);
10027e14a2dcSVladimir Oltean 
10037e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
10047e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
1005bdeced75SVladimir Oltean }
1006bdeced75SVladimir Oltean 
1007bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
1008bd2b3161SXiaoliang Yang {
1009bd2b3161SXiaoliang Yang 	int i;
1010bd2b3161SXiaoliang Yang 
1011bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
1012bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1013bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1014bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
1015bd2b3161SXiaoliang Yang 		       port);
1016bd2b3161SXiaoliang Yang 
101770d39a6eSVladimir Oltean 	for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
1018bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
1019bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
1020bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
1021bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
1022bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
1023bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
1024bd2b3161SXiaoliang Yang 			      port, i);
1025bd2b3161SXiaoliang Yang 	}
1026bd2b3161SXiaoliang Yang }
1027bd2b3161SXiaoliang Yang 
102856051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
102956051948SVladimir Oltean 			      u32 stringset, u8 *data)
103056051948SVladimir Oltean {
103156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
103256051948SVladimir Oltean 
103356051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
103456051948SVladimir Oltean }
103556051948SVladimir Oltean 
103656051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
103756051948SVladimir Oltean {
103856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
103956051948SVladimir Oltean 
104056051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
104156051948SVladimir Oltean }
104256051948SVladimir Oltean 
104356051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
104456051948SVladimir Oltean {
104556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
104656051948SVladimir Oltean 
104756051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
104856051948SVladimir Oltean }
104956051948SVladimir Oltean 
105056051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
105156051948SVladimir Oltean 			     struct ethtool_ts_info *info)
105256051948SVladimir Oltean {
105356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
105456051948SVladimir Oltean 
105556051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
105656051948SVladimir Oltean }
105756051948SVladimir Oltean 
1058acf242fcSColin Foster static const u32 felix_phy_match_table[PHY_INTERFACE_MODE_MAX] = {
1059acf242fcSColin Foster 	[PHY_INTERFACE_MODE_INTERNAL] = OCELOT_PORT_MODE_INTERNAL,
1060acf242fcSColin Foster 	[PHY_INTERFACE_MODE_SGMII] = OCELOT_PORT_MODE_SGMII,
1061acf242fcSColin Foster 	[PHY_INTERFACE_MODE_QSGMII] = OCELOT_PORT_MODE_QSGMII,
1062acf242fcSColin Foster 	[PHY_INTERFACE_MODE_USXGMII] = OCELOT_PORT_MODE_USXGMII,
106311ecf341SVladimir Oltean 	[PHY_INTERFACE_MODE_1000BASEX] = OCELOT_PORT_MODE_1000BASEX,
1064acf242fcSColin Foster 	[PHY_INTERFACE_MODE_2500BASEX] = OCELOT_PORT_MODE_2500BASEX,
1065acf242fcSColin Foster };
1066acf242fcSColin Foster 
1067acf242fcSColin Foster static int felix_validate_phy_mode(struct felix *felix, int port,
1068acf242fcSColin Foster 				   phy_interface_t phy_mode)
1069acf242fcSColin Foster {
1070acf242fcSColin Foster 	u32 modes = felix->info->port_modes[port];
1071acf242fcSColin Foster 
1072acf242fcSColin Foster 	if (felix_phy_match_table[phy_mode] & modes)
1073acf242fcSColin Foster 		return 0;
1074acf242fcSColin Foster 	return -EOPNOTSUPP;
1075acf242fcSColin Foster }
1076acf242fcSColin Foster 
1077bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
1078bdeced75SVladimir Oltean 				  struct device_node *ports_node,
1079bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
1080bdeced75SVladimir Oltean {
1081bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1082bdeced75SVladimir Oltean 	struct device_node *child;
1083bdeced75SVladimir Oltean 
108437fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
1085bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
1086bdeced75SVladimir Oltean 		u32 port;
1087bdeced75SVladimir Oltean 		int err;
1088bdeced75SVladimir Oltean 
1089bdeced75SVladimir Oltean 		/* Get switch port number from DT */
1090bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
1091bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
1092bdeced75SVladimir Oltean 				"(property \"reg\")\n");
1093bdeced75SVladimir Oltean 			of_node_put(child);
1094bdeced75SVladimir Oltean 			return -ENODEV;
1095bdeced75SVladimir Oltean 		}
1096bdeced75SVladimir Oltean 
1097bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
1098bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
1099bdeced75SVladimir Oltean 		if (err) {
1100bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
1101bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
1102bdeced75SVladimir Oltean 				port);
1103bdeced75SVladimir Oltean 			of_node_put(child);
1104bdeced75SVladimir Oltean 			return -ENODEV;
1105bdeced75SVladimir Oltean 		}
1106bdeced75SVladimir Oltean 
1107acf242fcSColin Foster 		err = felix_validate_phy_mode(felix, port, phy_mode);
1108bdeced75SVladimir Oltean 		if (err < 0) {
1109bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
1110bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
111159ebb430SSumera Priyadarsini 			of_node_put(child);
1112bdeced75SVladimir Oltean 			return err;
1113bdeced75SVladimir Oltean 		}
1114bdeced75SVladimir Oltean 
1115bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
1116bdeced75SVladimir Oltean 	}
1117bdeced75SVladimir Oltean 
1118bdeced75SVladimir Oltean 	return 0;
1119bdeced75SVladimir Oltean }
1120bdeced75SVladimir Oltean 
1121bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
1122bdeced75SVladimir Oltean {
1123bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1124bdeced75SVladimir Oltean 	struct device_node *switch_node;
1125bdeced75SVladimir Oltean 	struct device_node *ports_node;
1126bdeced75SVladimir Oltean 	int err;
1127bdeced75SVladimir Oltean 
1128bdeced75SVladimir Oltean 	switch_node = dev->of_node;
1129bdeced75SVladimir Oltean 
1130bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
1131abecbfcdSVladimir Oltean 	if (!ports_node)
1132abecbfcdSVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
1133bdeced75SVladimir Oltean 	if (!ports_node) {
1134abecbfcdSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n");
1135bdeced75SVladimir Oltean 		return -ENODEV;
1136bdeced75SVladimir Oltean 	}
1137bdeced75SVladimir Oltean 
1138bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
1139bdeced75SVladimir Oltean 	of_node_put(ports_node);
1140bdeced75SVladimir Oltean 
1141bdeced75SVladimir Oltean 	return err;
1142bdeced75SVladimir Oltean }
1143bdeced75SVladimir Oltean 
114456051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
114556051948SVladimir Oltean {
114656051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
1147bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
1148b4024c9eSClaudiu Manoil 	struct resource res;
114956051948SVladimir Oltean 	int port, i, err;
115056051948SVladimir Oltean 
115156051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
115256051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
115356051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
115456051948SVladimir Oltean 	if (!ocelot->ports)
115556051948SVladimir Oltean 		return -ENOMEM;
115656051948SVladimir Oltean 
115756051948SVladimir Oltean 	ocelot->map		= felix->info->map;
115856051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
115921ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
116007d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
116177043c37SXiaoliang Yang 	ocelot->vcap_pol.base	= felix->info->vcap_pol_base;
116277043c37SXiaoliang Yang 	ocelot->vcap_pol.max	= felix->info->vcap_pol_max;
116377043c37SXiaoliang Yang 	ocelot->vcap_pol.base2	= felix->info->vcap_pol_base2;
116477043c37SXiaoliang Yang 	ocelot->vcap_pol.max2	= felix->info->vcap_pol_max2;
116556051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
1166cacea62fSVladimir Oltean 	ocelot->npi_inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
1167cacea62fSVladimir Oltean 	ocelot->npi_xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
1168f59fd9caSVladimir Oltean 	ocelot->devlink		= felix->ds->devlink;
116956051948SVladimir Oltean 
1170bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
1171bdeced75SVladimir Oltean 				 GFP_KERNEL);
1172bdeced75SVladimir Oltean 	if (!port_phy_modes)
1173bdeced75SVladimir Oltean 		return -ENOMEM;
1174bdeced75SVladimir Oltean 
1175bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
1176bdeced75SVladimir Oltean 	if (err) {
1177bdeced75SVladimir Oltean 		kfree(port_phy_modes);
1178bdeced75SVladimir Oltean 		return err;
1179bdeced75SVladimir Oltean 	}
1180bdeced75SVladimir Oltean 
118156051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
118256051948SVladimir Oltean 		struct regmap *target;
118356051948SVladimir Oltean 
118456051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
118556051948SVladimir Oltean 			continue;
118656051948SVladimir Oltean 
1187b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
1188b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1189375e1314SVladimir Oltean 		res.start += felix->switch_base;
1190375e1314SVladimir Oltean 		res.end += felix->switch_base;
119156051948SVladimir Oltean 
1192242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
119356051948SVladimir Oltean 		if (IS_ERR(target)) {
119456051948SVladimir Oltean 			dev_err(ocelot->dev,
119556051948SVladimir Oltean 				"Failed to map device memory space\n");
1196bdeced75SVladimir Oltean 			kfree(port_phy_modes);
119756051948SVladimir Oltean 			return PTR_ERR(target);
119856051948SVladimir Oltean 		}
119956051948SVladimir Oltean 
120056051948SVladimir Oltean 		ocelot->targets[i] = target;
120156051948SVladimir Oltean 	}
120256051948SVladimir Oltean 
120356051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
120456051948SVladimir Oltean 	if (err) {
120556051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
1206bdeced75SVladimir Oltean 		kfree(port_phy_modes);
120756051948SVladimir Oltean 		return err;
120856051948SVladimir Oltean 	}
120956051948SVladimir Oltean 
121056051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
121156051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
121291c724cfSVladimir Oltean 		struct regmap *target;
121356051948SVladimir Oltean 
121456051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
121556051948SVladimir Oltean 					   sizeof(struct ocelot_port),
121656051948SVladimir Oltean 					   GFP_KERNEL);
121756051948SVladimir Oltean 		if (!ocelot_port) {
121856051948SVladimir Oltean 			dev_err(ocelot->dev,
121956051948SVladimir Oltean 				"failed to allocate port memory\n");
1220bdeced75SVladimir Oltean 			kfree(port_phy_modes);
122156051948SVladimir Oltean 			return -ENOMEM;
122256051948SVladimir Oltean 		}
122356051948SVladimir Oltean 
1224b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
1225b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1226375e1314SVladimir Oltean 		res.start += felix->switch_base;
1227375e1314SVladimir Oltean 		res.end += felix->switch_base;
122856051948SVladimir Oltean 
1229242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
123091c724cfSVladimir Oltean 		if (IS_ERR(target)) {
123156051948SVladimir Oltean 			dev_err(ocelot->dev,
123291c724cfSVladimir Oltean 				"Failed to map memory space for port %d\n",
123391c724cfSVladimir Oltean 				port);
1234bdeced75SVladimir Oltean 			kfree(port_phy_modes);
123591c724cfSVladimir Oltean 			return PTR_ERR(target);
123656051948SVladimir Oltean 		}
123756051948SVladimir Oltean 
1238bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
123956051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
124091c724cfSVladimir Oltean 		ocelot_port->target = target;
12417e708760SVladimir Oltean 		ocelot_port->index = port;
124256051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
124356051948SVladimir Oltean 	}
124456051948SVladimir Oltean 
1245bdeced75SVladimir Oltean 	kfree(port_phy_modes);
1246bdeced75SVladimir Oltean 
1247bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
1248bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
1249bdeced75SVladimir Oltean 		if (err < 0)
1250bdeced75SVladimir Oltean 			return err;
1251bdeced75SVladimir Oltean 	}
1252bdeced75SVladimir Oltean 
125356051948SVladimir Oltean 	return 0;
125456051948SVladimir Oltean }
125556051948SVladimir Oltean 
12561328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port,
12571328a883SVladimir Oltean 					   struct sk_buff *skb)
12581328a883SVladimir Oltean {
12591328a883SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
12601328a883SVladimir Oltean 	struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
12611328a883SVladimir Oltean 	struct sk_buff *skb_match = NULL, *skb_tmp;
12621328a883SVladimir Oltean 	unsigned long flags;
12631328a883SVladimir Oltean 
12641328a883SVladimir Oltean 	if (!clone)
12651328a883SVladimir Oltean 		return;
12661328a883SVladimir Oltean 
12671328a883SVladimir Oltean 	spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags);
12681328a883SVladimir Oltean 
12691328a883SVladimir Oltean 	skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) {
12701328a883SVladimir Oltean 		if (skb != clone)
12711328a883SVladimir Oltean 			continue;
12721328a883SVladimir Oltean 		__skb_unlink(skb, &ocelot_port->tx_skbs);
12731328a883SVladimir Oltean 		skb_match = skb;
12741328a883SVladimir Oltean 		break;
12751328a883SVladimir Oltean 	}
12761328a883SVladimir Oltean 
12771328a883SVladimir Oltean 	spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags);
12781328a883SVladimir Oltean 
12791328a883SVladimir Oltean 	WARN_ONCE(!skb_match,
12801328a883SVladimir Oltean 		  "Could not find skb clone in TX timestamping list\n");
12811328a883SVladimir Oltean }
12821328a883SVladimir Oltean 
128349f885b2SVladimir Oltean #define work_to_xmit_work(w) \
128449f885b2SVladimir Oltean 		container_of((w), struct felix_deferred_xmit_work, work)
128549f885b2SVladimir Oltean 
128649f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work)
128749f885b2SVladimir Oltean {
128849f885b2SVladimir Oltean 	struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
128949f885b2SVladimir Oltean 	struct dsa_switch *ds = xmit_work->dp->ds;
129049f885b2SVladimir Oltean 	struct sk_buff *skb = xmit_work->skb;
129149f885b2SVladimir Oltean 	u32 rew_op = ocelot_ptp_rew_op(skb);
129249f885b2SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
129349f885b2SVladimir Oltean 	int port = xmit_work->dp->index;
129449f885b2SVladimir Oltean 	int retries = 10;
129549f885b2SVladimir Oltean 
129649f885b2SVladimir Oltean 	do {
129749f885b2SVladimir Oltean 		if (ocelot_can_inject(ocelot, 0))
129849f885b2SVladimir Oltean 			break;
129949f885b2SVladimir Oltean 
130049f885b2SVladimir Oltean 		cpu_relax();
130149f885b2SVladimir Oltean 	} while (--retries);
130249f885b2SVladimir Oltean 
130349f885b2SVladimir Oltean 	if (!retries) {
130449f885b2SVladimir Oltean 		dev_err(ocelot->dev, "port %d failed to inject skb\n",
130549f885b2SVladimir Oltean 			port);
13061328a883SVladimir Oltean 		ocelot_port_purge_txtstamp_skb(ocelot, port, skb);
130749f885b2SVladimir Oltean 		kfree_skb(skb);
130849f885b2SVladimir Oltean 		return;
130949f885b2SVladimir Oltean 	}
131049f885b2SVladimir Oltean 
131149f885b2SVladimir Oltean 	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
131249f885b2SVladimir Oltean 
131349f885b2SVladimir Oltean 	consume_skb(skb);
131449f885b2SVladimir Oltean 	kfree(xmit_work);
131549f885b2SVladimir Oltean }
131649f885b2SVladimir Oltean 
131735d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds,
131835d97680SVladimir Oltean 				      enum dsa_tag_protocol proto)
131949f885b2SVladimir Oltean {
132035d97680SVladimir Oltean 	struct ocelot_8021q_tagger_data *tagger_data;
132149f885b2SVladimir Oltean 
132235d97680SVladimir Oltean 	switch (proto) {
132335d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
132435d97680SVladimir Oltean 		tagger_data = ocelot_8021q_tagger_data(ds);
132535d97680SVladimir Oltean 		tagger_data->xmit_work_fn = felix_port_deferred_xmit;
132649f885b2SVladimir Oltean 		return 0;
132735d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
132835d97680SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
132949f885b2SVladimir Oltean 		return 0;
133035d97680SVladimir Oltean 	default:
133135d97680SVladimir Oltean 		return -EPROTONOSUPPORT;
133249f885b2SVladimir Oltean 	}
133349f885b2SVladimir Oltean }
133449f885b2SVladimir Oltean 
133556051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
133656051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
133756051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
133856051948SVladimir Oltean  * (which will not work).
133956051948SVladimir Oltean  */
134056051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
134156051948SVladimir Oltean {
134256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
134356051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
13442960bb14SVladimir Oltean 	struct dsa_port *dp;
13452960bb14SVladimir Oltean 	int err;
134656051948SVladimir Oltean 
134756051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
134856051948SVladimir Oltean 	if (err)
134956051948SVladimir Oltean 		return err;
135056051948SVladimir Oltean 
1351d1cc0e93SVladimir Oltean 	err = ocelot_init(ocelot);
1352d1cc0e93SVladimir Oltean 	if (err)
13536b73b7c9SVladimir Oltean 		goto out_mdiobus_free;
1354d1cc0e93SVladimir Oltean 
13552b49d128SYangbo Lu 	if (ocelot->ptp) {
13562ac7c6c5SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
13572b49d128SYangbo Lu 		if (err) {
13582b49d128SYangbo Lu 			dev_err(ocelot->dev,
13592b49d128SYangbo Lu 				"Timestamp initialization failed\n");
13602b49d128SYangbo Lu 			ocelot->ptp = 0;
13612b49d128SYangbo Lu 		}
13622b49d128SYangbo Lu 	}
136356051948SVladimir Oltean 
13642960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
13652960bb14SVladimir Oltean 		ocelot_init_port(ocelot, dp->index);
1366bd2b3161SXiaoliang Yang 
1367bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
1368bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
1369bd2b3161SXiaoliang Yang 		 */
13702960bb14SVladimir Oltean 		felix_port_qos_map_init(ocelot, dp->index);
137156051948SVladimir Oltean 	}
137256051948SVladimir Oltean 
1373f59fd9caSVladimir Oltean 	err = ocelot_devlink_sb_register(ocelot);
1374f59fd9caSVladimir Oltean 	if (err)
13756b73b7c9SVladimir Oltean 		goto out_deinit_ports;
1376f59fd9caSVladimir Oltean 
13777a29d220SVladimir Oltean 	/* The initial tag protocol is NPI which won't fail during initial
13787a29d220SVladimir Oltean 	 * setup, there's no real point in checking for errors.
13791cf3299bSVladimir Oltean 	 */
13807a29d220SVladimir Oltean 	felix_change_tag_protocol(ds, felix->tag_proto);
13811cf3299bSVladimir Oltean 
13820b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
1383c54913c1SVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
138454c31984SVladimir Oltean 	ds->fdb_isolation = true;
138554c31984SVladimir Oltean 	ds->max_num_bridges = ds->num_ports;
1386bdeced75SVladimir Oltean 
138756051948SVladimir Oltean 	return 0;
13886b73b7c9SVladimir Oltean 
13896b73b7c9SVladimir Oltean out_deinit_ports:
13902960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
13912960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
13926b73b7c9SVladimir Oltean 
13936b73b7c9SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
13946b73b7c9SVladimir Oltean 	ocelot_deinit(ocelot);
13956b73b7c9SVladimir Oltean 
13966b73b7c9SVladimir Oltean out_mdiobus_free:
13976b73b7c9SVladimir Oltean 	if (felix->info->mdio_bus_free)
13986b73b7c9SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
13996b73b7c9SVladimir Oltean 
14006b73b7c9SVladimir Oltean 	return err;
140156051948SVladimir Oltean }
140256051948SVladimir Oltean 
140356051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
140456051948SVladimir Oltean {
140556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1406bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
14072960bb14SVladimir Oltean 	struct dsa_port *dp;
1408bdeced75SVladimir Oltean 
14097a29d220SVladimir Oltean 	if (felix->tag_proto_ops)
14107a29d220SVladimir Oltean 		felix->tag_proto_ops->teardown(ds);
1411adb3dccfSVladimir Oltean 
14122960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
14132960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
1414d19741b0SVladimir Oltean 
141549f885b2SVladimir Oltean 	ocelot_devlink_sb_unregister(ocelot);
141649f885b2SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
141749f885b2SVladimir Oltean 	ocelot_deinit(ocelot);
141849f885b2SVladimir Oltean 
1419d19741b0SVladimir Oltean 	if (felix->info->mdio_bus_free)
1420d19741b0SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
142156051948SVladimir Oltean }
142256051948SVladimir Oltean 
1423c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
1424c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1425c0bcf537SYangbo Lu {
1426c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1427c0bcf537SYangbo Lu 
1428c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
1429c0bcf537SYangbo Lu }
1430c0bcf537SYangbo Lu 
1431c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
1432c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1433c0bcf537SYangbo Lu {
1434c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
143599348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
143699348004SVladimir Oltean 	bool using_tag_8021q;
143799348004SVladimir Oltean 	int err;
1438c0bcf537SYangbo Lu 
143999348004SVladimir Oltean 	err = ocelot_hwstamp_set(ocelot, port, ifr);
144099348004SVladimir Oltean 	if (err)
144199348004SVladimir Oltean 		return err;
144299348004SVladimir Oltean 
144399348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
144499348004SVladimir Oltean 
144599348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
1446c0bcf537SYangbo Lu }
1447c0bcf537SYangbo Lu 
1448d219b4b6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot)
14490a6f17c6SVladimir Oltean {
14500a6f17c6SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1451dbd03285SVladimir Oltean 	int err = 0, grp = 0;
14520a6f17c6SVladimir Oltean 
14530a6f17c6SVladimir Oltean 	if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q)
14540a6f17c6SVladimir Oltean 		return false;
14550a6f17c6SVladimir Oltean 
14560a6f17c6SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
14570a6f17c6SVladimir Oltean 		return false;
14580a6f17c6SVladimir Oltean 
14590a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
14600a6f17c6SVladimir Oltean 		struct sk_buff *skb;
14610a6f17c6SVladimir Oltean 		unsigned int type;
14620a6f17c6SVladimir Oltean 
14630a6f17c6SVladimir Oltean 		err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
14640a6f17c6SVladimir Oltean 		if (err)
14650a6f17c6SVladimir Oltean 			goto out;
14660a6f17c6SVladimir Oltean 
14670a6f17c6SVladimir Oltean 		/* We trap to the CPU port module all PTP frames, but
14680a6f17c6SVladimir Oltean 		 * felix_rxtstamp() only gets called for event frames.
14690a6f17c6SVladimir Oltean 		 * So we need to avoid sending duplicate general
14700a6f17c6SVladimir Oltean 		 * message frames by running a second BPF classifier
14710a6f17c6SVladimir Oltean 		 * here and dropping those.
14720a6f17c6SVladimir Oltean 		 */
14730a6f17c6SVladimir Oltean 		__skb_push(skb, ETH_HLEN);
14740a6f17c6SVladimir Oltean 
14750a6f17c6SVladimir Oltean 		type = ptp_classify_raw(skb);
14760a6f17c6SVladimir Oltean 
14770a6f17c6SVladimir Oltean 		__skb_pull(skb, ETH_HLEN);
14780a6f17c6SVladimir Oltean 
14790a6f17c6SVladimir Oltean 		if (type == PTP_CLASS_NONE) {
14800a6f17c6SVladimir Oltean 			kfree_skb(skb);
14810a6f17c6SVladimir Oltean 			continue;
14820a6f17c6SVladimir Oltean 		}
14830a6f17c6SVladimir Oltean 
14840a6f17c6SVladimir Oltean 		netif_rx(skb);
14850a6f17c6SVladimir Oltean 	}
14860a6f17c6SVladimir Oltean 
14870a6f17c6SVladimir Oltean out:
14885d3bb7ddSVladimir Oltean 	if (err < 0) {
14895d3bb7ddSVladimir Oltean 		dev_err_ratelimited(ocelot->dev,
14905d3bb7ddSVladimir Oltean 				    "Error during packet extraction: %pe\n",
14915d3bb7ddSVladimir Oltean 				    ERR_PTR(err));
14920a6f17c6SVladimir Oltean 		ocelot_drain_cpu_queue(ocelot, 0);
14935d3bb7ddSVladimir Oltean 	}
14940a6f17c6SVladimir Oltean 
14950a6f17c6SVladimir Oltean 	return true;
14960a6f17c6SVladimir Oltean }
14970a6f17c6SVladimir Oltean 
1498c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
1499c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
1500c0bcf537SYangbo Lu {
150192f62485SVladimir Oltean 	u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo;
1502c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
1503c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1504c0bcf537SYangbo Lu 	struct timespec64 ts;
150592f62485SVladimir Oltean 	u32 tstamp_hi;
150692f62485SVladimir Oltean 	u64 tstamp;
1507c0bcf537SYangbo Lu 
15080a6f17c6SVladimir Oltean 	/* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb
15090a6f17c6SVladimir Oltean 	 * for RX timestamping. Then free it, and poll for its copy through
15100a6f17c6SVladimir Oltean 	 * MMIO in the CPU port module, and inject that into the stack from
15110a6f17c6SVladimir Oltean 	 * ocelot_xtr_poll().
15120a6f17c6SVladimir Oltean 	 */
1513d219b4b6SVladimir Oltean 	if (felix_check_xtr_pkt(ocelot)) {
15140a6f17c6SVladimir Oltean 		kfree_skb(skb);
15150a6f17c6SVladimir Oltean 		return true;
15160a6f17c6SVladimir Oltean 	}
15170a6f17c6SVladimir Oltean 
1518c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1519c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1520c0bcf537SYangbo Lu 
1521c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
1522c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
1523c0bcf537SYangbo Lu 		tstamp_hi--;
1524c0bcf537SYangbo Lu 
1525c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
1526c0bcf537SYangbo Lu 
1527c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
1528c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1529c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
1530c0bcf537SYangbo Lu 	return false;
1531c0bcf537SYangbo Lu }
1532c0bcf537SYangbo Lu 
15335c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port,
15345c5416f5SYangbo Lu 			   struct sk_buff *skb)
1535c0bcf537SYangbo Lu {
1536c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1537682eaad9SYangbo Lu 	struct sk_buff *clone = NULL;
1538c0bcf537SYangbo Lu 
1539682eaad9SYangbo Lu 	if (!ocelot->ptp)
15405c5416f5SYangbo Lu 		return;
1541c0bcf537SYangbo Lu 
154252849bcfSVladimir Oltean 	if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
154352849bcfSVladimir Oltean 		dev_err_ratelimited(ds->dev,
154452849bcfSVladimir Oltean 				    "port %d delivering skb without TX timestamp\n",
154552849bcfSVladimir Oltean 				    port);
1546682eaad9SYangbo Lu 		return;
154752849bcfSVladimir Oltean 	}
1548682eaad9SYangbo Lu 
1549682eaad9SYangbo Lu 	if (clone)
1550c4b364ceSYangbo Lu 		OCELOT_SKB_CB(skb)->clone = clone;
15515c5416f5SYangbo Lu }
1552c0bcf537SYangbo Lu 
15530b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
15540b912fc9SVladimir Oltean {
15550b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15560b912fc9SVladimir Oltean 
15570b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
15580b912fc9SVladimir Oltean 
15590b912fc9SVladimir Oltean 	return 0;
15600b912fc9SVladimir Oltean }
15610b912fc9SVladimir Oltean 
15620b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
15630b912fc9SVladimir Oltean {
15640b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15650b912fc9SVladimir Oltean 
15660b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
15670b912fc9SVladimir Oltean }
15680b912fc9SVladimir Oltean 
156907d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
157007d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
157107d985eeSVladimir Oltean {
157207d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
157399348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
157499348004SVladimir Oltean 	bool using_tag_8021q;
157599348004SVladimir Oltean 	int err;
157607d985eeSVladimir Oltean 
157799348004SVladimir Oltean 	err = ocelot_cls_flower_replace(ocelot, port, cls, ingress);
157899348004SVladimir Oltean 	if (err)
157999348004SVladimir Oltean 		return err;
158099348004SVladimir Oltean 
158199348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
158299348004SVladimir Oltean 
158399348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
158407d985eeSVladimir Oltean }
158507d985eeSVladimir Oltean 
158607d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
158707d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
158807d985eeSVladimir Oltean {
158907d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
159007d985eeSVladimir Oltean 
159107d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
159207d985eeSVladimir Oltean }
159307d985eeSVladimir Oltean 
159407d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
159507d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
159607d985eeSVladimir Oltean {
159707d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
159807d985eeSVladimir Oltean 
159907d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
160007d985eeSVladimir Oltean }
160107d985eeSVladimir Oltean 
1602fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
1603fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
1604fc411eaaSVladimir Oltean {
1605fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1606fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
1607fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
16085f035af7SPo Liu 		.burst = policer->burst,
1609fc411eaaSVladimir Oltean 	};
1610fc411eaaSVladimir Oltean 
1611fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
1612fc411eaaSVladimir Oltean }
1613fc411eaaSVladimir Oltean 
1614fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
1615fc411eaaSVladimir Oltean {
1616fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1617fc411eaaSVladimir Oltean 
1618fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
1619fc411eaaSVladimir Oltean }
1620fc411eaaSVladimir Oltean 
16215e497497SVladimir Oltean static int felix_port_mirror_add(struct dsa_switch *ds, int port,
16225e497497SVladimir Oltean 				 struct dsa_mall_mirror_tc_entry *mirror,
16235e497497SVladimir Oltean 				 bool ingress, struct netlink_ext_ack *extack)
16245e497497SVladimir Oltean {
16255e497497SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
16265e497497SVladimir Oltean 
16275e497497SVladimir Oltean 	return ocelot_port_mirror_add(ocelot, port, mirror->to_local_port,
16285e497497SVladimir Oltean 				      ingress, extack);
16295e497497SVladimir Oltean }
16305e497497SVladimir Oltean 
16315e497497SVladimir Oltean static void felix_port_mirror_del(struct dsa_switch *ds, int port,
16325e497497SVladimir Oltean 				  struct dsa_mall_mirror_tc_entry *mirror)
16335e497497SVladimir Oltean {
16345e497497SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
16355e497497SVladimir Oltean 
16365e497497SVladimir Oltean 	ocelot_port_mirror_del(ocelot, port, mirror->ingress);
16375e497497SVladimir Oltean }
16385e497497SVladimir Oltean 
1639de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
1640de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
1641de143c0eSXiaoliang Yang 			       void *type_data)
1642de143c0eSXiaoliang Yang {
1643de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
1644de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
1645de143c0eSXiaoliang Yang 
1646de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
1647de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
1648de143c0eSXiaoliang Yang 	else
1649de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
1650de143c0eSXiaoliang Yang }
1651de143c0eSXiaoliang Yang 
1652f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
1653f59fd9caSVladimir Oltean 			     u16 pool_index,
1654f59fd9caSVladimir Oltean 			     struct devlink_sb_pool_info *pool_info)
1655f59fd9caSVladimir Oltean {
1656f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1657f59fd9caSVladimir Oltean 
1658f59fd9caSVladimir Oltean 	return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
1659f59fd9caSVladimir Oltean }
1660f59fd9caSVladimir Oltean 
1661f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
1662f59fd9caSVladimir Oltean 			     u16 pool_index, u32 size,
1663f59fd9caSVladimir Oltean 			     enum devlink_sb_threshold_type threshold_type,
1664f59fd9caSVladimir Oltean 			     struct netlink_ext_ack *extack)
1665f59fd9caSVladimir Oltean {
1666f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1667f59fd9caSVladimir Oltean 
1668f59fd9caSVladimir Oltean 	return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
1669f59fd9caSVladimir Oltean 				  threshold_type, extack);
1670f59fd9caSVladimir Oltean }
1671f59fd9caSVladimir Oltean 
1672f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
1673f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1674f59fd9caSVladimir Oltean 				  u32 *p_threshold)
1675f59fd9caSVladimir Oltean {
1676f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1677f59fd9caSVladimir Oltean 
1678f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
1679f59fd9caSVladimir Oltean 				       p_threshold);
1680f59fd9caSVladimir Oltean }
1681f59fd9caSVladimir Oltean 
1682f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
1683f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1684f59fd9caSVladimir Oltean 				  u32 threshold, struct netlink_ext_ack *extack)
1685f59fd9caSVladimir Oltean {
1686f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1687f59fd9caSVladimir Oltean 
1688f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
1689f59fd9caSVladimir Oltean 				       threshold, extack);
1690f59fd9caSVladimir Oltean }
1691f59fd9caSVladimir Oltean 
1692f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
1693f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1694f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1695f59fd9caSVladimir Oltean 				     u16 *p_pool_index, u32 *p_threshold)
1696f59fd9caSVladimir Oltean {
1697f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1698f59fd9caSVladimir Oltean 
1699f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
1700f59fd9caSVladimir Oltean 					  pool_type, p_pool_index,
1701f59fd9caSVladimir Oltean 					  p_threshold);
1702f59fd9caSVladimir Oltean }
1703f59fd9caSVladimir Oltean 
1704f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
1705f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1706f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1707f59fd9caSVladimir Oltean 				     u16 pool_index, u32 threshold,
1708f59fd9caSVladimir Oltean 				     struct netlink_ext_ack *extack)
1709f59fd9caSVladimir Oltean {
1710f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1711f59fd9caSVladimir Oltean 
1712f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
1713f59fd9caSVladimir Oltean 					  pool_type, pool_index, threshold,
1714f59fd9caSVladimir Oltean 					  extack);
1715f59fd9caSVladimir Oltean }
1716f59fd9caSVladimir Oltean 
1717f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds,
1718f59fd9caSVladimir Oltean 				 unsigned int sb_index)
1719f59fd9caSVladimir Oltean {
1720f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1721f59fd9caSVladimir Oltean 
1722f59fd9caSVladimir Oltean 	return ocelot_sb_occ_snapshot(ocelot, sb_index);
1723f59fd9caSVladimir Oltean }
1724f59fd9caSVladimir Oltean 
1725f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds,
1726f59fd9caSVladimir Oltean 				  unsigned int sb_index)
1727f59fd9caSVladimir Oltean {
1728f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1729f59fd9caSVladimir Oltean 
1730f59fd9caSVladimir Oltean 	return ocelot_sb_occ_max_clear(ocelot, sb_index);
1731f59fd9caSVladimir Oltean }
1732f59fd9caSVladimir Oltean 
1733f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
1734f59fd9caSVladimir Oltean 				      unsigned int sb_index, u16 pool_index,
1735f59fd9caSVladimir Oltean 				      u32 *p_cur, u32 *p_max)
1736f59fd9caSVladimir Oltean {
1737f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1738f59fd9caSVladimir Oltean 
1739f59fd9caSVladimir Oltean 	return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
1740f59fd9caSVladimir Oltean 					   p_cur, p_max);
1741f59fd9caSVladimir Oltean }
1742f59fd9caSVladimir Oltean 
1743f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
1744f59fd9caSVladimir Oltean 					 unsigned int sb_index, u16 tc_index,
1745f59fd9caSVladimir Oltean 					 enum devlink_sb_pool_type pool_type,
1746f59fd9caSVladimir Oltean 					 u32 *p_cur, u32 *p_max)
1747f59fd9caSVladimir Oltean {
1748f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1749f59fd9caSVladimir Oltean 
1750f59fd9caSVladimir Oltean 	return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
1751f59fd9caSVladimir Oltean 					      pool_type, p_cur, p_max);
1752f59fd9caSVladimir Oltean }
1753f59fd9caSVladimir Oltean 
1754a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port,
1755a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1756a026c50bSHoratiu Vultur {
1757a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1758a026c50bSHoratiu Vultur 
1759a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1760a026c50bSHoratiu Vultur }
1761a026c50bSHoratiu Vultur 
1762a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port,
1763a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1764a026c50bSHoratiu Vultur {
1765a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1766a026c50bSHoratiu Vultur 
1767a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1768a026c50bSHoratiu Vultur }
1769a026c50bSHoratiu Vultur 
1770a026c50bSHoratiu Vultur static int
1771a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port,
1772a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1773a026c50bSHoratiu Vultur {
1774a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1775a026c50bSHoratiu Vultur 
1776a026c50bSHoratiu Vultur 	return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1777a026c50bSHoratiu Vultur }
1778a026c50bSHoratiu Vultur 
1779a026c50bSHoratiu Vultur static int
1780a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port,
1781a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1782a026c50bSHoratiu Vultur {
1783a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1784a026c50bSHoratiu Vultur 
1785a026c50bSHoratiu Vultur 	return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1786a026c50bSHoratiu Vultur }
1787a026c50bSHoratiu Vultur 
1788978777d0SVladimir Oltean static int felix_port_get_default_prio(struct dsa_switch *ds, int port)
1789978777d0SVladimir Oltean {
1790978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1791978777d0SVladimir Oltean 
1792978777d0SVladimir Oltean 	return ocelot_port_get_default_prio(ocelot, port);
1793978777d0SVladimir Oltean }
1794978777d0SVladimir Oltean 
1795978777d0SVladimir Oltean static int felix_port_set_default_prio(struct dsa_switch *ds, int port,
1796978777d0SVladimir Oltean 				       u8 prio)
1797978777d0SVladimir Oltean {
1798978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1799978777d0SVladimir Oltean 
1800978777d0SVladimir Oltean 	return ocelot_port_set_default_prio(ocelot, port, prio);
1801978777d0SVladimir Oltean }
1802978777d0SVladimir Oltean 
1803978777d0SVladimir Oltean static int felix_port_get_dscp_prio(struct dsa_switch *ds, int port, u8 dscp)
1804978777d0SVladimir Oltean {
1805978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1806978777d0SVladimir Oltean 
1807978777d0SVladimir Oltean 	return ocelot_port_get_dscp_prio(ocelot, port, dscp);
1808978777d0SVladimir Oltean }
1809978777d0SVladimir Oltean 
1810978777d0SVladimir Oltean static int felix_port_add_dscp_prio(struct dsa_switch *ds, int port, u8 dscp,
1811978777d0SVladimir Oltean 				    u8 prio)
1812978777d0SVladimir Oltean {
1813978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1814978777d0SVladimir Oltean 
1815978777d0SVladimir Oltean 	return ocelot_port_add_dscp_prio(ocelot, port, dscp, prio);
1816978777d0SVladimir Oltean }
1817978777d0SVladimir Oltean 
1818978777d0SVladimir Oltean static int felix_port_del_dscp_prio(struct dsa_switch *ds, int port, u8 dscp,
1819978777d0SVladimir Oltean 				    u8 prio)
1820978777d0SVladimir Oltean {
1821978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1822978777d0SVladimir Oltean 
1823978777d0SVladimir Oltean 	return ocelot_port_del_dscp_prio(ocelot, port, dscp, prio);
1824978777d0SVladimir Oltean }
1825978777d0SVladimir Oltean 
1826375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = {
182756051948SVladimir Oltean 	.get_tag_protocol		= felix_get_tag_protocol,
1828adb3dccfSVladimir Oltean 	.change_tag_protocol		= felix_change_tag_protocol,
182935d97680SVladimir Oltean 	.connect_tag_protocol		= felix_connect_tag_protocol,
183056051948SVladimir Oltean 	.setup				= felix_setup,
183156051948SVladimir Oltean 	.teardown			= felix_teardown,
183256051948SVladimir Oltean 	.set_ageing_time		= felix_set_ageing_time,
183356051948SVladimir Oltean 	.get_strings			= felix_get_strings,
183456051948SVladimir Oltean 	.get_ethtool_stats		= felix_get_ethtool_stats,
183556051948SVladimir Oltean 	.get_sset_count			= felix_get_sset_count,
183656051948SVladimir Oltean 	.get_ts_info			= felix_get_ts_info,
183779fda660SRussell King (Oracle) 	.phylink_get_caps		= felix_phylink_get_caps,
1838bdeced75SVladimir Oltean 	.phylink_validate		= felix_phylink_validate,
1839864ba485SRussell King (Oracle) 	.phylink_mac_select_pcs		= felix_phylink_mac_select_pcs,
1840bdeced75SVladimir Oltean 	.phylink_mac_link_down		= felix_phylink_mac_link_down,
1841bdeced75SVladimir Oltean 	.phylink_mac_link_up		= felix_phylink_mac_link_up,
18425cad43a5SVladimir Oltean 	.port_fast_age			= felix_port_fast_age,
184356051948SVladimir Oltean 	.port_fdb_dump			= felix_fdb_dump,
184456051948SVladimir Oltean 	.port_fdb_add			= felix_fdb_add,
184556051948SVladimir Oltean 	.port_fdb_del			= felix_fdb_del,
1846961d8b69SVladimir Oltean 	.lag_fdb_add			= felix_lag_fdb_add,
1847961d8b69SVladimir Oltean 	.lag_fdb_del			= felix_lag_fdb_del,
1848209edf95SVladimir Oltean 	.port_mdb_add			= felix_mdb_add,
1849209edf95SVladimir Oltean 	.port_mdb_del			= felix_mdb_del,
1850421741eaSVladimir Oltean 	.port_pre_bridge_flags		= felix_pre_bridge_flags,
1851421741eaSVladimir Oltean 	.port_bridge_flags		= felix_bridge_flags,
185256051948SVladimir Oltean 	.port_bridge_join		= felix_bridge_join,
185356051948SVladimir Oltean 	.port_bridge_leave		= felix_bridge_leave,
18548fe6832eSVladimir Oltean 	.port_lag_join			= felix_lag_join,
18558fe6832eSVladimir Oltean 	.port_lag_leave			= felix_lag_leave,
18568fe6832eSVladimir Oltean 	.port_lag_change		= felix_lag_change,
185756051948SVladimir Oltean 	.port_stp_state_set		= felix_bridge_stp_state_set,
185856051948SVladimir Oltean 	.port_vlan_filtering		= felix_vlan_filtering,
185956051948SVladimir Oltean 	.port_vlan_add			= felix_vlan_add,
186056051948SVladimir Oltean 	.port_vlan_del			= felix_vlan_del,
1861c0bcf537SYangbo Lu 	.port_hwtstamp_get		= felix_hwtstamp_get,
1862c0bcf537SYangbo Lu 	.port_hwtstamp_set		= felix_hwtstamp_set,
1863c0bcf537SYangbo Lu 	.port_rxtstamp			= felix_rxtstamp,
1864c0bcf537SYangbo Lu 	.port_txtstamp			= felix_txtstamp,
18650b912fc9SVladimir Oltean 	.port_change_mtu		= felix_change_mtu,
18660b912fc9SVladimir Oltean 	.port_max_mtu			= felix_get_max_mtu,
1867fc411eaaSVladimir Oltean 	.port_policer_add		= felix_port_policer_add,
1868fc411eaaSVladimir Oltean 	.port_policer_del		= felix_port_policer_del,
18695e497497SVladimir Oltean 	.port_mirror_add		= felix_port_mirror_add,
18705e497497SVladimir Oltean 	.port_mirror_del		= felix_port_mirror_del,
187107d985eeSVladimir Oltean 	.cls_flower_add			= felix_cls_flower_add,
187207d985eeSVladimir Oltean 	.cls_flower_del			= felix_cls_flower_del,
187307d985eeSVladimir Oltean 	.cls_flower_stats		= felix_cls_flower_stats,
1874de143c0eSXiaoliang Yang 	.port_setup_tc			= felix_port_setup_tc,
1875f59fd9caSVladimir Oltean 	.devlink_sb_pool_get		= felix_sb_pool_get,
1876f59fd9caSVladimir Oltean 	.devlink_sb_pool_set		= felix_sb_pool_set,
1877f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_get	= felix_sb_port_pool_get,
1878f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_set	= felix_sb_port_pool_set,
1879f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_get	= felix_sb_tc_pool_bind_get,
1880f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_set	= felix_sb_tc_pool_bind_set,
1881f59fd9caSVladimir Oltean 	.devlink_sb_occ_snapshot	= felix_sb_occ_snapshot,
1882f59fd9caSVladimir Oltean 	.devlink_sb_occ_max_clear	= felix_sb_occ_max_clear,
1883f59fd9caSVladimir Oltean 	.devlink_sb_occ_port_pool_get	= felix_sb_occ_port_pool_get,
1884f59fd9caSVladimir Oltean 	.devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
1885a026c50bSHoratiu Vultur 	.port_mrp_add			= felix_mrp_add,
1886a026c50bSHoratiu Vultur 	.port_mrp_del			= felix_mrp_del,
1887a026c50bSHoratiu Vultur 	.port_mrp_add_ring_role		= felix_mrp_add_ring_role,
1888a026c50bSHoratiu Vultur 	.port_mrp_del_ring_role		= felix_mrp_del_ring_role,
18895da11eb4SVladimir Oltean 	.tag_8021q_vlan_add		= felix_tag_8021q_vlan_add,
18905da11eb4SVladimir Oltean 	.tag_8021q_vlan_del		= felix_tag_8021q_vlan_del,
1891978777d0SVladimir Oltean 	.port_get_default_prio		= felix_port_get_default_prio,
1892978777d0SVladimir Oltean 	.port_set_default_prio		= felix_port_set_default_prio,
1893978777d0SVladimir Oltean 	.port_get_dscp_prio		= felix_port_get_dscp_prio,
1894978777d0SVladimir Oltean 	.port_add_dscp_prio		= felix_port_add_dscp_prio,
1895978777d0SVladimir Oltean 	.port_del_dscp_prio		= felix_port_del_dscp_prio,
189672c3b0c7SVladimir Oltean 	.port_set_host_flood		= felix_port_set_host_flood,
189756051948SVladimir Oltean };
1898319e4dd1SVladimir Oltean 
1899319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
1900319e4dd1SVladimir Oltean {
1901319e4dd1SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1902319e4dd1SVladimir Oltean 	struct dsa_switch *ds = felix->ds;
1903319e4dd1SVladimir Oltean 
1904319e4dd1SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
1905319e4dd1SVladimir Oltean 		return NULL;
1906319e4dd1SVladimir Oltean 
1907319e4dd1SVladimir Oltean 	return dsa_to_port(ds, port)->slave;
1908319e4dd1SVladimir Oltean }
1909319e4dd1SVladimir Oltean 
1910319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev)
1911319e4dd1SVladimir Oltean {
1912319e4dd1SVladimir Oltean 	struct dsa_port *dp;
1913319e4dd1SVladimir Oltean 
1914319e4dd1SVladimir Oltean 	dp = dsa_port_from_netdev(dev);
1915319e4dd1SVladimir Oltean 	if (IS_ERR(dp))
1916319e4dd1SVladimir Oltean 		return -EINVAL;
1917319e4dd1SVladimir Oltean 
1918319e4dd1SVladimir Oltean 	return dp->index;
1919319e4dd1SVladimir Oltean }
1920