xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision c295f9831f1db12330d6a28a1cb2bd2562535e37)
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  */
4804b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add_rx(struct felix *felix, int port, u16 vid)
49e21268efSVladimir Oltean {
50e21268efSVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
51e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
52e21268efSVladimir Oltean 	struct dsa_switch *ds = felix->ds;
53e21268efSVladimir Oltean 	int key_length, upstream, err;
54e21268efSVladimir Oltean 
55e21268efSVladimir Oltean 	key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length;
56e21268efSVladimir Oltean 	upstream = dsa_upstream_port(ds, port);
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 
63e21268efSVladimir Oltean 	outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
64e21268efSVladimir Oltean 	outer_tagging_rule->prio = 1;
65c518afecSVladimir Oltean 	outer_tagging_rule->id.cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port);
66e21268efSVladimir Oltean 	outer_tagging_rule->id.tc_offload = false;
67e21268efSVladimir Oltean 	outer_tagging_rule->block_id = VCAP_ES0;
68e21268efSVladimir Oltean 	outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
69e21268efSVladimir Oltean 	outer_tagging_rule->lookup = 0;
70e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.value = port;
71e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0);
72e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.value = upstream;
73e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0);
74e21268efSVladimir Oltean 	outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG;
75e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD;
76e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_vid_sel = 1;
77e21268efSVladimir Oltean 	outer_tagging_rule->action.vid_a_val = vid;
78e21268efSVladimir Oltean 
79e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL);
80e21268efSVladimir Oltean 	if (err)
81e21268efSVladimir Oltean 		kfree(outer_tagging_rule);
82e21268efSVladimir Oltean 
83e21268efSVladimir Oltean 	return err;
84e21268efSVladimir Oltean }
85e21268efSVladimir Oltean 
8604b67e18SVladimir Oltean static int felix_tag_8021q_vlan_del_rx(struct felix *felix, int port, u16 vid)
8704b67e18SVladimir Oltean {
8804b67e18SVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
8904b67e18SVladimir Oltean 	struct ocelot_vcap_block *block_vcap_es0;
9004b67e18SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
9104b67e18SVladimir Oltean 
9204b67e18SVladimir Oltean 	block_vcap_es0 = &ocelot->block[VCAP_ES0];
9304b67e18SVladimir Oltean 
9404b67e18SVladimir Oltean 	outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0,
9504b67e18SVladimir Oltean 								 port, false);
9604b67e18SVladimir Oltean 	if (!outer_tagging_rule)
9704b67e18SVladimir Oltean 		return -ENOENT;
9804b67e18SVladimir Oltean 
9904b67e18SVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, outer_tagging_rule);
10004b67e18SVladimir Oltean }
10104b67e18SVladimir Oltean 
10204b67e18SVladimir Oltean /* Set up VCAP IS1 rules for stripping the tag_8021q VLAN on TX and VCAP IS2
10304b67e18SVladimir Oltean  * rules for steering those tagged packets towards the correct destination port
10404b67e18SVladimir Oltean  */
10504b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add_tx(struct felix *felix, int port, u16 vid)
106e21268efSVladimir Oltean {
107e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
108e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
109e21268efSVladimir Oltean 	struct dsa_switch *ds = felix->ds;
110e21268efSVladimir Oltean 	int upstream, err;
111e21268efSVladimir Oltean 
112e21268efSVladimir Oltean 	untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
113e21268efSVladimir Oltean 	if (!untagging_rule)
114e21268efSVladimir Oltean 		return -ENOMEM;
115e21268efSVladimir Oltean 
116e21268efSVladimir Oltean 	redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
117e21268efSVladimir Oltean 	if (!redirect_rule) {
118e21268efSVladimir Oltean 		kfree(untagging_rule);
119e21268efSVladimir Oltean 		return -ENOMEM;
120e21268efSVladimir Oltean 	}
121e21268efSVladimir Oltean 
122e21268efSVladimir Oltean 	upstream = dsa_upstream_port(ds, port);
123e21268efSVladimir Oltean 
124e21268efSVladimir Oltean 	untagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
125e21268efSVladimir Oltean 	untagging_rule->ingress_port_mask = BIT(upstream);
126e21268efSVladimir Oltean 	untagging_rule->vlan.vid.value = vid;
127e21268efSVladimir Oltean 	untagging_rule->vlan.vid.mask = VLAN_VID_MASK;
128e21268efSVladimir Oltean 	untagging_rule->prio = 1;
129c518afecSVladimir Oltean 	untagging_rule->id.cookie = OCELOT_VCAP_IS1_TAG_8021Q_TXVLAN(ocelot, port);
130e21268efSVladimir Oltean 	untagging_rule->id.tc_offload = false;
131e21268efSVladimir Oltean 	untagging_rule->block_id = VCAP_IS1;
132e21268efSVladimir Oltean 	untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
133e21268efSVladimir Oltean 	untagging_rule->lookup = 0;
134e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt_ena = true;
135e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt = 1;
136e21268efSVladimir Oltean 	untagging_rule->action.pag_override_mask = 0xff;
137e21268efSVladimir Oltean 	untagging_rule->action.pag_val = port;
138e21268efSVladimir Oltean 
139e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL);
140e21268efSVladimir Oltean 	if (err) {
141e21268efSVladimir Oltean 		kfree(untagging_rule);
142e21268efSVladimir Oltean 		kfree(redirect_rule);
143e21268efSVladimir Oltean 		return err;
144e21268efSVladimir Oltean 	}
145e21268efSVladimir Oltean 
146e21268efSVladimir Oltean 	redirect_rule->key_type = OCELOT_VCAP_KEY_ANY;
147e21268efSVladimir Oltean 	redirect_rule->ingress_port_mask = BIT(upstream);
148e21268efSVladimir Oltean 	redirect_rule->pag = port;
149e21268efSVladimir Oltean 	redirect_rule->prio = 1;
150c518afecSVladimir Oltean 	redirect_rule->id.cookie = OCELOT_VCAP_IS2_TAG_8021Q_TXVLAN(ocelot, port);
151e21268efSVladimir Oltean 	redirect_rule->id.tc_offload = false;
152e21268efSVladimir Oltean 	redirect_rule->block_id = VCAP_IS2;
153e21268efSVladimir Oltean 	redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
154e21268efSVladimir Oltean 	redirect_rule->lookup = 0;
155e21268efSVladimir Oltean 	redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT;
156e21268efSVladimir Oltean 	redirect_rule->action.port_mask = BIT(port);
157e21268efSVladimir Oltean 
158e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL);
159e21268efSVladimir Oltean 	if (err) {
160e21268efSVladimir Oltean 		ocelot_vcap_filter_del(ocelot, untagging_rule);
161e21268efSVladimir Oltean 		kfree(redirect_rule);
162e21268efSVladimir Oltean 		return err;
163e21268efSVladimir Oltean 	}
164e21268efSVladimir Oltean 
165e21268efSVladimir Oltean 	return 0;
166e21268efSVladimir Oltean }
167e21268efSVladimir Oltean 
16804b67e18SVladimir Oltean static int felix_tag_8021q_vlan_del_tx(struct felix *felix, int port, u16 vid)
169e21268efSVladimir Oltean {
170e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
171e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is1;
172e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
173e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
174e21268efSVladimir Oltean 	int err;
175e21268efSVladimir Oltean 
176e21268efSVladimir Oltean 	block_vcap_is1 = &ocelot->block[VCAP_IS1];
177e21268efSVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
178e21268efSVladimir Oltean 
179e21268efSVladimir Oltean 	untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1,
180e21268efSVladimir Oltean 							     port, false);
181e21268efSVladimir Oltean 	if (!untagging_rule)
18208f44db3SVladimir Oltean 		return -ENOENT;
183e21268efSVladimir Oltean 
184e21268efSVladimir Oltean 	err = ocelot_vcap_filter_del(ocelot, untagging_rule);
185e21268efSVladimir Oltean 	if (err)
186e21268efSVladimir Oltean 		return err;
187e21268efSVladimir Oltean 
188e21268efSVladimir Oltean 	redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2,
189e21268efSVladimir Oltean 							    port, false);
190e21268efSVladimir Oltean 	if (!redirect_rule)
19104b67e18SVladimir Oltean 		return -ENOENT;
192e21268efSVladimir Oltean 
193e21268efSVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, redirect_rule);
194e21268efSVladimir Oltean }
195e21268efSVladimir Oltean 
19604b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
19704b67e18SVladimir Oltean 				    u16 flags)
19804b67e18SVladimir Oltean {
19904b67e18SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
20004b67e18SVladimir Oltean 	int err;
20104b67e18SVladimir Oltean 
20204b67e18SVladimir Oltean 	/* tag_8021q.c assumes we are implementing this via port VLAN
20304b67e18SVladimir Oltean 	 * membership, which we aren't. So we don't need to add any VCAP filter
20404b67e18SVladimir Oltean 	 * for the CPU port.
20504b67e18SVladimir Oltean 	 */
20604b67e18SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
20704b67e18SVladimir Oltean 		return 0;
20804b67e18SVladimir Oltean 
20904b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_add_rx(ocelot_to_felix(ocelot), port, vid);
21004b67e18SVladimir Oltean 	if (err)
21104b67e18SVladimir Oltean 		return err;
21204b67e18SVladimir Oltean 
21304b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_add_tx(ocelot_to_felix(ocelot), port, vid);
21404b67e18SVladimir Oltean 	if (err) {
21504b67e18SVladimir Oltean 		felix_tag_8021q_vlan_del_rx(ocelot_to_felix(ocelot), port, vid);
21604b67e18SVladimir Oltean 		return err;
21704b67e18SVladimir Oltean 	}
21804b67e18SVladimir Oltean 
21904b67e18SVladimir Oltean 	return 0;
22004b67e18SVladimir Oltean }
22104b67e18SVladimir Oltean 
222e21268efSVladimir Oltean static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
223e21268efSVladimir Oltean {
224e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
22504b67e18SVladimir Oltean 	int err;
226e21268efSVladimir Oltean 
22704b67e18SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
22804b67e18SVladimir Oltean 		return 0;
229e21268efSVladimir Oltean 
23004b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_del_rx(ocelot_to_felix(ocelot), port, vid);
23104b67e18SVladimir Oltean 	if (err)
23204b67e18SVladimir Oltean 		return err;
23304b67e18SVladimir Oltean 
23404b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_del_tx(ocelot_to_felix(ocelot), port, vid);
23504b67e18SVladimir Oltean 	if (err) {
23604b67e18SVladimir Oltean 		felix_tag_8021q_vlan_add_rx(ocelot_to_felix(ocelot), port, vid);
23704b67e18SVladimir Oltean 		return err;
23804b67e18SVladimir Oltean 	}
239e21268efSVladimir Oltean 
240e21268efSVladimir Oltean 	return 0;
241e21268efSVladimir Oltean }
242e21268efSVladimir Oltean 
243c352e5e8SVladimir Oltean static int felix_trap_get_cpu_port(struct dsa_switch *ds,
244c352e5e8SVladimir Oltean 				   const struct ocelot_vcap_filter *trap)
245c352e5e8SVladimir Oltean {
246c352e5e8SVladimir Oltean 	struct dsa_port *dp;
247c352e5e8SVladimir Oltean 	int first_port;
248c352e5e8SVladimir Oltean 
249c352e5e8SVladimir Oltean 	if (WARN_ON(!trap->ingress_port_mask))
250c352e5e8SVladimir Oltean 		return -1;
251c352e5e8SVladimir Oltean 
252c352e5e8SVladimir Oltean 	first_port = __ffs(trap->ingress_port_mask);
253c352e5e8SVladimir Oltean 	dp = dsa_to_port(ds, first_port);
254c352e5e8SVladimir Oltean 
255c352e5e8SVladimir Oltean 	return dp->cpu_dp->index;
256c352e5e8SVladimir Oltean }
257c352e5e8SVladimir Oltean 
25899348004SVladimir Oltean /* On switches with no extraction IRQ wired, trapped packets need to be
25999348004SVladimir Oltean  * replicated over Ethernet as well, otherwise we'd get no notification of
26099348004SVladimir Oltean  * their arrival when using the ocelot-8021q tagging protocol.
261c8c0ba4fSVladimir Oltean  */
26299348004SVladimir Oltean static int felix_update_trapping_destinations(struct dsa_switch *ds,
26399348004SVladimir Oltean 					      bool using_tag_8021q)
264c8c0ba4fSVladimir Oltean {
26599348004SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
26699348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
267e1846cffSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
26899348004SVladimir Oltean 	struct ocelot_vcap_filter *trap;
26999348004SVladimir Oltean 	enum ocelot_mask_mode mask_mode;
27099348004SVladimir Oltean 	unsigned long port_mask;
27199348004SVladimir Oltean 	bool cpu_copy_ena;
272c352e5e8SVladimir Oltean 	int err;
273c8c0ba4fSVladimir Oltean 
27499348004SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
27599348004SVladimir Oltean 		return 0;
276c8c0ba4fSVladimir Oltean 
277d78637a8SVladimir Oltean 	/* We are sure that "cpu" was found, otherwise
278d78637a8SVladimir Oltean 	 * dsa_tree_setup_default_cpu() would have failed earlier.
279d78637a8SVladimir Oltean 	 */
280e1846cffSVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
281c8c0ba4fSVladimir Oltean 
28299348004SVladimir Oltean 	/* Make sure all traps are set up for that destination */
283e1846cffSVladimir Oltean 	list_for_each_entry(trap, &block_vcap_is2->rules, list) {
284e1846cffSVladimir Oltean 		if (!trap->is_trap)
285e1846cffSVladimir Oltean 			continue;
286e1846cffSVladimir Oltean 
28799348004SVladimir Oltean 		/* Figure out the current trapping destination */
28899348004SVladimir Oltean 		if (using_tag_8021q) {
28999348004SVladimir Oltean 			/* Redirect to the tag_8021q CPU port. If timestamps
29099348004SVladimir Oltean 			 * are necessary, also copy trapped packets to the CPU
29199348004SVladimir Oltean 			 * port module.
292c8c0ba4fSVladimir Oltean 			 */
29399348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_REDIRECT;
294c352e5e8SVladimir Oltean 			port_mask = BIT(felix_trap_get_cpu_port(ds, trap));
29599348004SVladimir Oltean 			cpu_copy_ena = !!trap->take_ts;
296c8c0ba4fSVladimir Oltean 		} else {
29799348004SVladimir Oltean 			/* Trap packets only to the CPU port module, which is
29899348004SVladimir Oltean 			 * redirected to the NPI port (the DSA CPU port)
299c8c0ba4fSVladimir Oltean 			 */
30099348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
30199348004SVladimir Oltean 			port_mask = 0;
30299348004SVladimir Oltean 			cpu_copy_ena = true;
303c8c0ba4fSVladimir Oltean 		}
304c8c0ba4fSVladimir Oltean 
30599348004SVladimir Oltean 		if (trap->action.mask_mode == mask_mode &&
30699348004SVladimir Oltean 		    trap->action.port_mask == port_mask &&
30799348004SVladimir Oltean 		    trap->action.cpu_copy_ena == cpu_copy_ena)
30899348004SVladimir Oltean 			continue;
309c8c0ba4fSVladimir Oltean 
31099348004SVladimir Oltean 		trap->action.mask_mode = mask_mode;
31199348004SVladimir Oltean 		trap->action.port_mask = port_mask;
31299348004SVladimir Oltean 		trap->action.cpu_copy_ena = cpu_copy_ena;
3130a6f17c6SVladimir Oltean 
31499348004SVladimir Oltean 		err = ocelot_vcap_filter_replace(ocelot, trap);
315c8c0ba4fSVladimir Oltean 		if (err)
316c8c0ba4fSVladimir Oltean 			return err;
31799348004SVladimir Oltean 	}
318c8c0ba4fSVladimir Oltean 
31999348004SVladimir Oltean 	return 0;
320c8c0ba4fSVladimir Oltean }
321c8c0ba4fSVladimir Oltean 
322adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This
323adb3dccfSVladimir Oltean  * is the mode through which frames can be injected from and extracted to an
324adb3dccfSVladimir Oltean  * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU
325adb3dccfSVladimir Oltean  * running Linux, and this forms a DSA setup together with the enetc or fman
326adb3dccfSVladimir Oltean  * DSA master.
327adb3dccfSVladimir Oltean  */
328adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port)
329adb3dccfSVladimir Oltean {
330adb3dccfSVladimir Oltean 	ocelot->npi = port;
331adb3dccfSVladimir Oltean 
332adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
333adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
334adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
335adb3dccfSVladimir Oltean 
336adb3dccfSVladimir Oltean 	/* NPI port Injection/Extraction configuration */
337adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
338adb3dccfSVladimir Oltean 			    ocelot->npi_xtr_prefix);
339adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
340adb3dccfSVladimir Oltean 			    ocelot->npi_inj_prefix);
341adb3dccfSVladimir Oltean 
342adb3dccfSVladimir Oltean 	/* Disable transmission of pause frames */
343adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
344adb3dccfSVladimir Oltean }
345adb3dccfSVladimir Oltean 
346adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port)
347adb3dccfSVladimir Oltean {
348adb3dccfSVladimir Oltean 	/* Restore hardware defaults */
349adb3dccfSVladimir Oltean 	int unused_port = ocelot->num_phys_ports + 2;
350adb3dccfSVladimir Oltean 
351adb3dccfSVladimir Oltean 	ocelot->npi = -1;
352adb3dccfSVladimir Oltean 
353adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port),
354adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
355adb3dccfSVladimir Oltean 
356adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
357adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
358adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
359adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
360adb3dccfSVladimir Oltean 
361adb3dccfSVladimir Oltean 	/* Enable transmission of pause frames */
362adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
363adb3dccfSVladimir Oltean }
364adb3dccfSVladimir Oltean 
3657a29d220SVladimir Oltean static int felix_tag_npi_setup(struct dsa_switch *ds)
366adb3dccfSVladimir Oltean {
3677a29d220SVladimir Oltean 	struct dsa_port *dp, *first_cpu_dp = NULL;
368adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
369f9cef64fSVladimir Oltean 
3707a29d220SVladimir Oltean 	dsa_switch_for_each_user_port(dp, ds) {
3717a29d220SVladimir Oltean 		if (first_cpu_dp && dp->cpu_dp != first_cpu_dp) {
3727a29d220SVladimir Oltean 			dev_err(ds->dev, "Multiple NPI ports not supported\n");
3737a29d220SVladimir Oltean 			return -EINVAL;
3747a29d220SVladimir Oltean 		}
375b903a6bdSVladimir Oltean 
3767a29d220SVladimir Oltean 		first_cpu_dp = dp->cpu_dp;
3777a29d220SVladimir Oltean 	}
378adb3dccfSVladimir Oltean 
3797a29d220SVladimir Oltean 	if (!first_cpu_dp)
3807a29d220SVladimir Oltean 		return -EINVAL;
3817a29d220SVladimir Oltean 
3827a29d220SVladimir Oltean 	felix_npi_port_init(ocelot, first_cpu_dp->index);
383adb3dccfSVladimir Oltean 
384adb3dccfSVladimir Oltean 	return 0;
385adb3dccfSVladimir Oltean }
386adb3dccfSVladimir Oltean 
3877a29d220SVladimir Oltean static void felix_tag_npi_teardown(struct dsa_switch *ds)
388adb3dccfSVladimir Oltean {
389adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
390adb3dccfSVladimir Oltean 
3917a29d220SVladimir Oltean 	felix_npi_port_deinit(ocelot, ocelot->npi);
392adb3dccfSVladimir Oltean }
393adb3dccfSVladimir Oltean 
3947a29d220SVladimir Oltean static unsigned long felix_tag_npi_get_host_fwd_mask(struct dsa_switch *ds)
395adb3dccfSVladimir Oltean {
3967a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
3977a29d220SVladimir Oltean 
3987a29d220SVladimir Oltean 	return BIT(ocelot->num_phys_ports);
3997a29d220SVladimir Oltean }
4007a29d220SVladimir Oltean 
4018c166acbSVladimir Oltean /* Alternatively to using the NPI functionality, that same hardware MAC
4028c166acbSVladimir Oltean  * connected internally to the enetc or fman DSA master can be configured to
4038c166acbSVladimir Oltean  * use the software-defined tag_8021q frame format. As far as the hardware is
4048c166acbSVladimir Oltean  * concerned, it thinks it is a "dumb switch" - the queues of the CPU port
4058c166acbSVladimir Oltean  * module are now disconnected from it, but can still be accessed through
4068c166acbSVladimir Oltean  * register-based MMIO.
4078c166acbSVladimir Oltean  */
4087a29d220SVladimir Oltean static const struct felix_tag_proto_ops felix_tag_npi_proto_ops = {
4097a29d220SVladimir Oltean 	.setup			= felix_tag_npi_setup,
4107a29d220SVladimir Oltean 	.teardown		= felix_tag_npi_teardown,
4117a29d220SVladimir Oltean 	.get_host_fwd_mask	= felix_tag_npi_get_host_fwd_mask,
4127a29d220SVladimir Oltean };
4137a29d220SVladimir Oltean 
4147a29d220SVladimir Oltean static int felix_tag_8021q_setup(struct dsa_switch *ds)
4157a29d220SVladimir Oltean {
4167a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
417*c295f983SVladimir Oltean 	struct dsa_port *dp;
418adb3dccfSVladimir Oltean 	int err;
419adb3dccfSVladimir Oltean 
4207a29d220SVladimir Oltean 	err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD));
4217a29d220SVladimir Oltean 	if (err)
422adb3dccfSVladimir Oltean 		return err;
4237a29d220SVladimir Oltean 
424*c295f983SVladimir Oltean 	dsa_switch_for_each_user_port(dp, ds)
425*c295f983SVladimir Oltean 		ocelot_port_assign_dsa_8021q_cpu(ocelot, dp->index,
426*c295f983SVladimir Oltean 						 dp->cpu_dp->index);
4277a29d220SVladimir Oltean 
428*c295f983SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
4297a29d220SVladimir Oltean 		/* This overwrites ocelot_init():
4307a29d220SVladimir Oltean 		 * Do not forward BPDU frames to the CPU port module,
4317a29d220SVladimir Oltean 		 * for 2 reasons:
4327a29d220SVladimir Oltean 		 * - When these packets are injected from the tag_8021q
4337a29d220SVladimir Oltean 		 *   CPU port, we want them to go out, not loop back
4347a29d220SVladimir Oltean 		 *   into the system.
4357a29d220SVladimir Oltean 		 * - STP traffic ingressing on a user port should go to
4367a29d220SVladimir Oltean 		 *   the tag_8021q CPU port, not to the hardware CPU
4377a29d220SVladimir Oltean 		 *   port module.
4387a29d220SVladimir Oltean 		 */
4397a29d220SVladimir Oltean 		ocelot_write_gix(ocelot,
4407a29d220SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0),
4417a29d220SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG, dp->index);
4427a29d220SVladimir Oltean 
4437a29d220SVladimir Oltean 	/* The ownership of the CPU port module's queues might have just been
4447a29d220SVladimir Oltean 	 * transferred to the tag_8021q tagger from the NPI-based tagger.
4457a29d220SVladimir Oltean 	 * So there might still be all sorts of crap in the queues. On the
4467a29d220SVladimir Oltean 	 * other hand, the MMIO-based matching of PTP frames is very brittle,
4477a29d220SVladimir Oltean 	 * so we need to be careful that there are no extra frames to be
4487a29d220SVladimir Oltean 	 * dequeued over MMIO, since we would never know to discard them.
4497a29d220SVladimir Oltean 	 */
4507a29d220SVladimir Oltean 	ocelot_drain_cpu_queue(ocelot, 0);
4517a29d220SVladimir Oltean 
4527a29d220SVladimir Oltean 	return 0;
4537a29d220SVladimir Oltean }
4547a29d220SVladimir Oltean 
4557a29d220SVladimir Oltean static void felix_tag_8021q_teardown(struct dsa_switch *ds)
456adb3dccfSVladimir Oltean {
4577a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
458*c295f983SVladimir Oltean 	struct dsa_port *dp;
4597a29d220SVladimir Oltean 
460*c295f983SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
4617a29d220SVladimir Oltean 		/* Restore the logic from ocelot_init:
4627a29d220SVladimir Oltean 		 * do not forward BPDU frames to the front ports.
4637a29d220SVladimir Oltean 		 */
4647a29d220SVladimir Oltean 		ocelot_write_gix(ocelot,
4657a29d220SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
4667a29d220SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG,
4677a29d220SVladimir Oltean 				 dp->index);
4687a29d220SVladimir Oltean 
469*c295f983SVladimir Oltean 	dsa_switch_for_each_user_port(dp, ds)
470*c295f983SVladimir Oltean 		ocelot_port_unassign_dsa_8021q_cpu(ocelot, dp->index);
4717a29d220SVladimir Oltean 
4727a29d220SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
4737a29d220SVladimir Oltean }
4747a29d220SVladimir Oltean 
4757a29d220SVladimir Oltean static unsigned long felix_tag_8021q_get_host_fwd_mask(struct dsa_switch *ds)
4767a29d220SVladimir Oltean {
4777a29d220SVladimir Oltean 	return dsa_cpu_ports(ds);
4787a29d220SVladimir Oltean }
4797a29d220SVladimir Oltean 
4807a29d220SVladimir Oltean static const struct felix_tag_proto_ops felix_tag_8021q_proto_ops = {
4817a29d220SVladimir Oltean 	.setup			= felix_tag_8021q_setup,
4827a29d220SVladimir Oltean 	.teardown		= felix_tag_8021q_teardown,
4837a29d220SVladimir Oltean 	.get_host_fwd_mask	= felix_tag_8021q_get_host_fwd_mask,
4847a29d220SVladimir Oltean };
4857a29d220SVladimir Oltean 
4867a29d220SVladimir Oltean static void felix_set_host_flood(struct dsa_switch *ds, unsigned long mask,
4877a29d220SVladimir Oltean 				 bool uc, bool mc, bool bc)
4887a29d220SVladimir Oltean {
4897a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
4907a29d220SVladimir Oltean 	unsigned long val;
4917a29d220SVladimir Oltean 
4927a29d220SVladimir Oltean 	val = uc ? mask : 0;
4937a29d220SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_UC);
4947a29d220SVladimir Oltean 
4957a29d220SVladimir Oltean 	val = mc ? mask : 0;
4967a29d220SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MC);
4977a29d220SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MCIPV4);
4987a29d220SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MCIPV6);
499129b7532SVladimir Oltean 
500129b7532SVladimir Oltean 	val = bc ? mask : 0;
501129b7532SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_BC);
5027a29d220SVladimir Oltean }
5037a29d220SVladimir Oltean 
5047a29d220SVladimir Oltean static void
5057a29d220SVladimir Oltean felix_migrate_host_flood(struct dsa_switch *ds,
5067a29d220SVladimir Oltean 			 const struct felix_tag_proto_ops *proto_ops,
5077a29d220SVladimir Oltean 			 const struct felix_tag_proto_ops *old_proto_ops)
5087a29d220SVladimir Oltean {
5097a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5107a29d220SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
5117a29d220SVladimir Oltean 	unsigned long mask;
5127a29d220SVladimir Oltean 
5137a29d220SVladimir Oltean 	if (old_proto_ops) {
5147a29d220SVladimir Oltean 		mask = old_proto_ops->get_host_fwd_mask(ds);
5157a29d220SVladimir Oltean 		felix_set_host_flood(ds, mask, false, false, false);
5167a29d220SVladimir Oltean 	}
5177a29d220SVladimir Oltean 
5187a29d220SVladimir Oltean 	mask = proto_ops->get_host_fwd_mask(ds);
5197a29d220SVladimir Oltean 	felix_set_host_flood(ds, mask, !!felix->host_flood_uc_mask,
5207a29d220SVladimir Oltean 			     !!felix->host_flood_mc_mask, true);
5217a29d220SVladimir Oltean }
5227a29d220SVladimir Oltean 
5237a29d220SVladimir Oltean static int felix_migrate_mdbs(struct dsa_switch *ds,
5247a29d220SVladimir Oltean 			      const struct felix_tag_proto_ops *proto_ops,
5257a29d220SVladimir Oltean 			      const struct felix_tag_proto_ops *old_proto_ops)
5267a29d220SVladimir Oltean {
5277a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5287a29d220SVladimir Oltean 	unsigned long from, to;
5297a29d220SVladimir Oltean 
5307a29d220SVladimir Oltean 	if (!old_proto_ops)
5317a29d220SVladimir Oltean 		return 0;
5327a29d220SVladimir Oltean 
5337a29d220SVladimir Oltean 	from = old_proto_ops->get_host_fwd_mask(ds);
5347a29d220SVladimir Oltean 	to = proto_ops->get_host_fwd_mask(ds);
5357a29d220SVladimir Oltean 
5367a29d220SVladimir Oltean 	return ocelot_migrate_mdbs(ocelot, from, to);
5377a29d220SVladimir Oltean }
5387a29d220SVladimir Oltean 
5397a29d220SVladimir Oltean /* Configure the shared hardware resources for a transition between
5407a29d220SVladimir Oltean  * @old_proto_ops and @proto_ops.
5417a29d220SVladimir Oltean  * Manual migration is needed because as far as DSA is concerned, no change of
5427a29d220SVladimir Oltean  * the CPU port is taking place here, just of the tagging protocol.
5437a29d220SVladimir Oltean  */
5447a29d220SVladimir Oltean static int
5457a29d220SVladimir Oltean felix_tag_proto_setup_shared(struct dsa_switch *ds,
5467a29d220SVladimir Oltean 			     const struct felix_tag_proto_ops *proto_ops,
5477a29d220SVladimir Oltean 			     const struct felix_tag_proto_ops *old_proto_ops)
5487a29d220SVladimir Oltean {
5497a29d220SVladimir Oltean 	bool using_tag_8021q = (proto_ops == &felix_tag_8021q_proto_ops);
5507a29d220SVladimir Oltean 	int err;
5517a29d220SVladimir Oltean 
5527a29d220SVladimir Oltean 	err = felix_migrate_mdbs(ds, proto_ops, old_proto_ops);
5537a29d220SVladimir Oltean 	if (err)
5547a29d220SVladimir Oltean 		return err;
5557a29d220SVladimir Oltean 
5567a29d220SVladimir Oltean 	felix_update_trapping_destinations(ds, using_tag_8021q);
5577a29d220SVladimir Oltean 
5587a29d220SVladimir Oltean 	felix_migrate_host_flood(ds, proto_ops, old_proto_ops);
5597a29d220SVladimir Oltean 
5607a29d220SVladimir Oltean 	return 0;
561adb3dccfSVladimir Oltean }
562adb3dccfSVladimir Oltean 
563e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the
564e21268efSVladimir Oltean  * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
565e21268efSVladimir Oltean  * or the restoration is guaranteed to work.
566e21268efSVladimir Oltean  */
567bacf93b0SVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds,
568adb3dccfSVladimir Oltean 				     enum dsa_tag_protocol proto)
569adb3dccfSVladimir Oltean {
5707a29d220SVladimir Oltean 	const struct felix_tag_proto_ops *old_proto_ops, *proto_ops;
571adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
572adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
573adb3dccfSVladimir Oltean 	int err;
574adb3dccfSVladimir Oltean 
5757a29d220SVladimir Oltean 	switch (proto) {
5767a29d220SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
5777a29d220SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
5787a29d220SVladimir Oltean 		proto_ops = &felix_tag_npi_proto_ops;
579bacf93b0SVladimir Oltean 		break;
5807a29d220SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
5817a29d220SVladimir Oltean 		proto_ops = &felix_tag_8021q_proto_ops;
5827a29d220SVladimir Oltean 		break;
5837a29d220SVladimir Oltean 	default:
5847a29d220SVladimir Oltean 		return -EPROTONOSUPPORT;
585bacf93b0SVladimir Oltean 	}
586bacf93b0SVladimir Oltean 
5877a29d220SVladimir Oltean 	old_proto_ops = felix->tag_proto_ops;
5887a29d220SVladimir Oltean 
5897a29d220SVladimir Oltean 	err = proto_ops->setup(ds);
5907a29d220SVladimir Oltean 	if (err)
5917a29d220SVladimir Oltean 		goto setup_failed;
5927a29d220SVladimir Oltean 
5937a29d220SVladimir Oltean 	err = felix_tag_proto_setup_shared(ds, proto_ops, old_proto_ops);
5947a29d220SVladimir Oltean 	if (err)
5957a29d220SVladimir Oltean 		goto setup_shared_failed;
5967a29d220SVladimir Oltean 
5977a29d220SVladimir Oltean 	if (old_proto_ops)
5987a29d220SVladimir Oltean 		old_proto_ops->teardown(ds);
5997a29d220SVladimir Oltean 
6007a29d220SVladimir Oltean 	felix->tag_proto_ops = proto_ops;
601adb3dccfSVladimir Oltean 	felix->tag_proto = proto;
602adb3dccfSVladimir Oltean 
603adb3dccfSVladimir Oltean 	return 0;
6047a29d220SVladimir Oltean 
6057a29d220SVladimir Oltean setup_shared_failed:
6067a29d220SVladimir Oltean 	proto_ops->teardown(ds);
6077a29d220SVladimir Oltean setup_failed:
6087a29d220SVladimir Oltean 	return err;
609adb3dccfSVladimir Oltean }
610adb3dccfSVladimir Oltean 
61156051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
6124d776482SFlorian Fainelli 						    int port,
6134d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
61456051948SVladimir Oltean {
615adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
616adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
617adb3dccfSVladimir Oltean 
618adb3dccfSVladimir Oltean 	return felix->tag_proto;
61956051948SVladimir Oltean }
62056051948SVladimir Oltean 
62172c3b0c7SVladimir Oltean static void felix_port_set_host_flood(struct dsa_switch *ds, int port,
62272c3b0c7SVladimir Oltean 				      bool uc, bool mc)
62372c3b0c7SVladimir Oltean {
62472c3b0c7SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
62572c3b0c7SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
6267a29d220SVladimir Oltean 	unsigned long mask;
62772c3b0c7SVladimir Oltean 
62872c3b0c7SVladimir Oltean 	if (uc)
62972c3b0c7SVladimir Oltean 		felix->host_flood_uc_mask |= BIT(port);
63072c3b0c7SVladimir Oltean 	else
63172c3b0c7SVladimir Oltean 		felix->host_flood_uc_mask &= ~BIT(port);
63272c3b0c7SVladimir Oltean 
63372c3b0c7SVladimir Oltean 	if (mc)
63472c3b0c7SVladimir Oltean 		felix->host_flood_mc_mask |= BIT(port);
63572c3b0c7SVladimir Oltean 	else
63672c3b0c7SVladimir Oltean 		felix->host_flood_mc_mask &= ~BIT(port);
63772c3b0c7SVladimir Oltean 
6387a29d220SVladimir Oltean 	mask = felix->tag_proto_ops->get_host_fwd_mask(ds);
6397a29d220SVladimir Oltean 	felix_set_host_flood(ds, mask, !!felix->host_flood_uc_mask,
6407a29d220SVladimir Oltean 			     !!felix->host_flood_mc_mask, true);
64172c3b0c7SVladimir Oltean }
64272c3b0c7SVladimir Oltean 
64356051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
64456051948SVladimir Oltean 				 unsigned int ageing_time)
64556051948SVladimir Oltean {
64656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
64756051948SVladimir Oltean 
64856051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
64956051948SVladimir Oltean 
65056051948SVladimir Oltean 	return 0;
65156051948SVladimir Oltean }
65256051948SVladimir Oltean 
6535cad43a5SVladimir Oltean static void felix_port_fast_age(struct dsa_switch *ds, int port)
6545cad43a5SVladimir Oltean {
6555cad43a5SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
6565cad43a5SVladimir Oltean 	int err;
6575cad43a5SVladimir Oltean 
6585cad43a5SVladimir Oltean 	err = ocelot_mact_flush(ocelot, port);
6595cad43a5SVladimir Oltean 	if (err)
6605cad43a5SVladimir Oltean 		dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n",
6615cad43a5SVladimir Oltean 			port, ERR_PTR(err));
6625cad43a5SVladimir Oltean }
6635cad43a5SVladimir Oltean 
66456051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
66556051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
66656051948SVladimir Oltean {
66756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
66856051948SVladimir Oltean 
66956051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
67056051948SVladimir Oltean }
67156051948SVladimir Oltean 
67256051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
673c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
674c2693363SVladimir Oltean 			 struct dsa_db db)
67556051948SVladimir Oltean {
67654c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
677e9b3ba43SVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
67856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
67956051948SVladimir Oltean 
68054c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
68154c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
68254c31984SVladimir Oltean 
683e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp) && !bridge_dev &&
6847e580490SVladimir Oltean 	    dsa_fdb_present_in_other_db(ds, port, addr, vid, db))
6857e580490SVladimir Oltean 		return 0;
6867e580490SVladimir Oltean 
687e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp))
688e9b3ba43SVladimir Oltean 		port = PGID_CPU;
689e9b3ba43SVladimir Oltean 
69054c31984SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid, bridge_dev);
69156051948SVladimir Oltean }
69256051948SVladimir Oltean 
69356051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
694c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
695c2693363SVladimir Oltean 			 struct dsa_db db)
69656051948SVladimir Oltean {
69754c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
698e9b3ba43SVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
69956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
70056051948SVladimir Oltean 
70154c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
70254c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
70354c31984SVladimir Oltean 
704e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp) && !bridge_dev &&
7057e580490SVladimir Oltean 	    dsa_fdb_present_in_other_db(ds, port, addr, vid, db))
7067e580490SVladimir Oltean 		return 0;
7077e580490SVladimir Oltean 
708e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp))
709e9b3ba43SVladimir Oltean 		port = PGID_CPU;
710e9b3ba43SVladimir Oltean 
71154c31984SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid, bridge_dev);
71256051948SVladimir Oltean }
71356051948SVladimir Oltean 
714961d8b69SVladimir Oltean static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag,
715c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
716c2693363SVladimir Oltean 			     struct dsa_db db)
717961d8b69SVladimir Oltean {
71854c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
719961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
720961d8b69SVladimir Oltean 
72154c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
72254c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
72354c31984SVladimir Oltean 
72454c31984SVladimir Oltean 	return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid, bridge_dev);
725961d8b69SVladimir Oltean }
726961d8b69SVladimir Oltean 
727961d8b69SVladimir Oltean static int felix_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag lag,
728c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
729c2693363SVladimir Oltean 			     struct dsa_db db)
730961d8b69SVladimir Oltean {
73154c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
732961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
733961d8b69SVladimir Oltean 
73454c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
73554c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
73654c31984SVladimir Oltean 
73754c31984SVladimir Oltean 	return ocelot_lag_fdb_del(ocelot, lag.dev, addr, vid, bridge_dev);
738961d8b69SVladimir Oltean }
739961d8b69SVladimir Oltean 
740a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port,
741c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
742c2693363SVladimir Oltean 			 struct dsa_db db)
743209edf95SVladimir Oltean {
74454c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
745209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
746209edf95SVladimir Oltean 
74754c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
74854c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
74954c31984SVladimir Oltean 
7507e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
7517e580490SVladimir Oltean 	    dsa_mdb_present_in_other_db(ds, port, mdb, db))
7527e580490SVladimir Oltean 		return 0;
7537e580490SVladimir Oltean 
7540ddf83cdSVladimir Oltean 	if (port == ocelot->npi)
7550ddf83cdSVladimir Oltean 		port = ocelot->num_phys_ports;
7560ddf83cdSVladimir Oltean 
75754c31984SVladimir Oltean 	return ocelot_port_mdb_add(ocelot, port, mdb, bridge_dev);
758209edf95SVladimir Oltean }
759209edf95SVladimir Oltean 
760209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port,
761c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
762c2693363SVladimir Oltean 			 struct dsa_db db)
763209edf95SVladimir Oltean {
76454c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
765209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
766209edf95SVladimir Oltean 
76754c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
76854c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
76954c31984SVladimir Oltean 
7707e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
7717e580490SVladimir Oltean 	    dsa_mdb_present_in_other_db(ds, port, mdb, db))
7727e580490SVladimir Oltean 		return 0;
7737e580490SVladimir Oltean 
7740ddf83cdSVladimir Oltean 	if (port == ocelot->npi)
7750ddf83cdSVladimir Oltean 		port = ocelot->num_phys_ports;
7760ddf83cdSVladimir Oltean 
77754c31984SVladimir Oltean 	return ocelot_port_mdb_del(ocelot, port, mdb, bridge_dev);
778209edf95SVladimir Oltean }
779209edf95SVladimir Oltean 
78056051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
78156051948SVladimir Oltean 				       u8 state)
78256051948SVladimir Oltean {
78356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
78456051948SVladimir Oltean 
78556051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
78656051948SVladimir Oltean }
78756051948SVladimir Oltean 
788421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port,
789421741eaSVladimir Oltean 				  struct switchdev_brport_flags val,
790421741eaSVladimir Oltean 				  struct netlink_ext_ack *extack)
791421741eaSVladimir Oltean {
792421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
793421741eaSVladimir Oltean 
794421741eaSVladimir Oltean 	return ocelot_port_pre_bridge_flags(ocelot, port, val);
795421741eaSVladimir Oltean }
796421741eaSVladimir Oltean 
797421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port,
798421741eaSVladimir Oltean 			      struct switchdev_brport_flags val,
799421741eaSVladimir Oltean 			      struct netlink_ext_ack *extack)
800421741eaSVladimir Oltean {
801421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
802421741eaSVladimir Oltean 
803910ee6ccSVladimir Oltean 	if (port == ocelot->npi)
804910ee6ccSVladimir Oltean 		port = ocelot->num_phys_ports;
805910ee6ccSVladimir Oltean 
806421741eaSVladimir Oltean 	ocelot_port_bridge_flags(ocelot, port, val);
807421741eaSVladimir Oltean 
808421741eaSVladimir Oltean 	return 0;
809421741eaSVladimir Oltean }
810421741eaSVladimir Oltean 
81156051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
81206b9cce4SVladimir Oltean 			     struct dsa_bridge bridge, bool *tx_fwd_offload,
81306b9cce4SVladimir Oltean 			     struct netlink_ext_ack *extack)
81456051948SVladimir Oltean {
81556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
81656051948SVladimir Oltean 
81754c31984SVladimir Oltean 	return ocelot_port_bridge_join(ocelot, port, bridge.dev, bridge.num,
81854c31984SVladimir Oltean 				       extack);
81956051948SVladimir Oltean }
82056051948SVladimir Oltean 
82156051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
822d3eed0e5SVladimir Oltean 			       struct dsa_bridge bridge)
82356051948SVladimir Oltean {
82456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
82556051948SVladimir Oltean 
826d3eed0e5SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, bridge.dev);
82756051948SVladimir Oltean }
82856051948SVladimir Oltean 
8298fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port,
830dedd6a00SVladimir Oltean 			  struct dsa_lag lag,
8318fe6832eSVladimir Oltean 			  struct netdev_lag_upper_info *info)
8328fe6832eSVladimir Oltean {
8338fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8348fe6832eSVladimir Oltean 
835dedd6a00SVladimir Oltean 	return ocelot_port_lag_join(ocelot, port, lag.dev, info);
8368fe6832eSVladimir Oltean }
8378fe6832eSVladimir Oltean 
8388fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port,
839dedd6a00SVladimir Oltean 			   struct dsa_lag lag)
8408fe6832eSVladimir Oltean {
8418fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8428fe6832eSVladimir Oltean 
843dedd6a00SVladimir Oltean 	ocelot_port_lag_leave(ocelot, port, lag.dev);
8448fe6832eSVladimir Oltean 
8458fe6832eSVladimir Oltean 	return 0;
8468fe6832eSVladimir Oltean }
8478fe6832eSVladimir Oltean 
8488fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port)
8498fe6832eSVladimir Oltean {
8508fe6832eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
8518fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8528fe6832eSVladimir Oltean 
8538fe6832eSVladimir Oltean 	ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
8548fe6832eSVladimir Oltean 
8558fe6832eSVladimir Oltean 	return 0;
8568fe6832eSVladimir Oltean }
8578fe6832eSVladimir Oltean 
85856051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
85901af940eSVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan,
86001af940eSVladimir Oltean 			      struct netlink_ext_ack *extack)
86156051948SVladimir Oltean {
8622f0402feSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
863b7a9e0daSVladimir Oltean 	u16 flags = vlan->flags;
8642f0402feSVladimir Oltean 
8659a720680SVladimir Oltean 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
8669a720680SVladimir Oltean 	 * egress-untagged or not, pvid or not, make no difference. This
8679a720680SVladimir Oltean 	 * behavior is already better than what DSA just tries to approximate
8689a720680SVladimir Oltean 	 * when it installs the VLAN with the same flags on the CPU port.
8699a720680SVladimir Oltean 	 * Just accept any configuration, and don't let ocelot deny installing
8709a720680SVladimir Oltean 	 * multiple native VLANs on the NPI port, because the switch doesn't
8719a720680SVladimir Oltean 	 * look at the port tag settings towards the NPI interface anyway.
8729a720680SVladimir Oltean 	 */
8739a720680SVladimir Oltean 	if (port == ocelot->npi)
8749a720680SVladimir Oltean 		return 0;
8759a720680SVladimir Oltean 
876b7a9e0daSVladimir Oltean 	return ocelot_vlan_prepare(ocelot, port, vlan->vid,
8772f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_PVID,
87801af940eSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_UNTAGGED,
87901af940eSVladimir Oltean 				   extack);
88056051948SVladimir Oltean }
88156051948SVladimir Oltean 
88289153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
88389153ed6SVladimir Oltean 				struct netlink_ext_ack *extack)
88456051948SVladimir Oltean {
88556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
88656051948SVladimir Oltean 
8873b95d1b2SVladimir Oltean 	return ocelot_port_vlan_filtering(ocelot, port, enabled, extack);
88856051948SVladimir Oltean }
88956051948SVladimir Oltean 
8901958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port,
89131046a5fSVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan,
89231046a5fSVladimir Oltean 			  struct netlink_ext_ack *extack)
89356051948SVladimir Oltean {
89456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
895183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
8961958d581SVladimir Oltean 	int err;
89756051948SVladimir Oltean 
89801af940eSVladimir Oltean 	err = felix_vlan_prepare(ds, port, vlan, extack);
8991958d581SVladimir Oltean 	if (err)
9001958d581SVladimir Oltean 		return err;
9011958d581SVladimir Oltean 
9021958d581SVladimir Oltean 	return ocelot_vlan_add(ocelot, port, vlan->vid,
903183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_PVID,
904183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_UNTAGGED);
90556051948SVladimir Oltean }
90656051948SVladimir Oltean 
90756051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
90856051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
90956051948SVladimir Oltean {
91056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
91156051948SVladimir Oltean 
912b7a9e0daSVladimir Oltean 	return ocelot_vlan_del(ocelot, port, vlan->vid);
91356051948SVladimir Oltean }
91456051948SVladimir Oltean 
91579fda660SRussell King (Oracle) static void felix_phylink_get_caps(struct dsa_switch *ds, int port,
91679fda660SRussell King (Oracle) 				   struct phylink_config *config)
91779fda660SRussell King (Oracle) {
91879fda660SRussell King (Oracle) 	struct ocelot *ocelot = ds->priv;
91979fda660SRussell King (Oracle) 
920f6f04c02SRussell King (Oracle) 	/* This driver does not make use of the speed, duplex, pause or the
921f6f04c02SRussell King (Oracle) 	 * advertisement in its mac_config, so it is safe to mark this driver
922f6f04c02SRussell King (Oracle) 	 * as non-legacy.
923f6f04c02SRussell King (Oracle) 	 */
924f6f04c02SRussell King (Oracle) 	config->legacy_pre_march2020 = false;
925f6f04c02SRussell King (Oracle) 
92679fda660SRussell King (Oracle) 	__set_bit(ocelot->ports[port]->phy_mode,
92779fda660SRussell King (Oracle) 		  config->supported_interfaces);
92879fda660SRussell King (Oracle) }
92979fda660SRussell King (Oracle) 
930bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
931bdeced75SVladimir Oltean 				   unsigned long *supported,
932bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
933bdeced75SVladimir Oltean {
934bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
935375e1314SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
936bdeced75SVladimir Oltean 
937375e1314SVladimir Oltean 	if (felix->info->phylink_validate)
938375e1314SVladimir Oltean 		felix->info->phylink_validate(ocelot, port, supported, state);
939bdeced75SVladimir Oltean }
940bdeced75SVladimir Oltean 
941864ba485SRussell King (Oracle) static struct phylink_pcs *felix_phylink_mac_select_pcs(struct dsa_switch *ds,
942864ba485SRussell King (Oracle) 							int port,
943864ba485SRussell King (Oracle) 							phy_interface_t iface)
944bdeced75SVladimir Oltean {
945bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
946bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
947864ba485SRussell King (Oracle) 	struct phylink_pcs *pcs = NULL;
948bdeced75SVladimir Oltean 
94949af6a76SColin Foster 	if (felix->pcs && felix->pcs[port])
950864ba485SRussell King (Oracle) 		pcs = felix->pcs[port];
951864ba485SRussell King (Oracle) 
952864ba485SRussell King (Oracle) 	return pcs;
953bdeced75SVladimir Oltean }
954bdeced75SVladimir Oltean 
955bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
956bdeced75SVladimir Oltean 					unsigned int link_an_mode,
957bdeced75SVladimir Oltean 					phy_interface_t interface)
958bdeced75SVladimir Oltean {
959bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
960bdeced75SVladimir Oltean 
961e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
962e6e12df6SVladimir Oltean 				     FELIX_MAC_QUIRKS);
963bdeced75SVladimir Oltean }
964bdeced75SVladimir Oltean 
965bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
966bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
967bdeced75SVladimir Oltean 				      phy_interface_t interface,
9685b502a7bSRussell King 				      struct phy_device *phydev,
9695b502a7bSRussell King 				      int speed, int duplex,
9705b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
971bdeced75SVladimir Oltean {
972bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
9737e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
974bdeced75SVladimir Oltean 
975e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
976e6e12df6SVladimir Oltean 				   interface, speed, duplex, tx_pause, rx_pause,
977e6e12df6SVladimir Oltean 				   FELIX_MAC_QUIRKS);
9787e14a2dcSVladimir Oltean 
9797e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
9807e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
981bdeced75SVladimir Oltean }
982bdeced75SVladimir Oltean 
983bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
984bd2b3161SXiaoliang Yang {
985bd2b3161SXiaoliang Yang 	int i;
986bd2b3161SXiaoliang Yang 
987bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
988bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
989bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
990bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
991bd2b3161SXiaoliang Yang 		       port);
992bd2b3161SXiaoliang Yang 
99370d39a6eSVladimir Oltean 	for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
994bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
995bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
996bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
997bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
998bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
999bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
1000bd2b3161SXiaoliang Yang 			      port, i);
1001bd2b3161SXiaoliang Yang 	}
1002bd2b3161SXiaoliang Yang }
1003bd2b3161SXiaoliang Yang 
100456051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
100556051948SVladimir Oltean 			      u32 stringset, u8 *data)
100656051948SVladimir Oltean {
100756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
100856051948SVladimir Oltean 
100956051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
101056051948SVladimir Oltean }
101156051948SVladimir Oltean 
101256051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
101356051948SVladimir Oltean {
101456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
101556051948SVladimir Oltean 
101656051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
101756051948SVladimir Oltean }
101856051948SVladimir Oltean 
101956051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
102056051948SVladimir Oltean {
102156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
102256051948SVladimir Oltean 
102356051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
102456051948SVladimir Oltean }
102556051948SVladimir Oltean 
102656051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
102756051948SVladimir Oltean 			     struct ethtool_ts_info *info)
102856051948SVladimir Oltean {
102956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
103056051948SVladimir Oltean 
103156051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
103256051948SVladimir Oltean }
103356051948SVladimir Oltean 
1034acf242fcSColin Foster static const u32 felix_phy_match_table[PHY_INTERFACE_MODE_MAX] = {
1035acf242fcSColin Foster 	[PHY_INTERFACE_MODE_INTERNAL] = OCELOT_PORT_MODE_INTERNAL,
1036acf242fcSColin Foster 	[PHY_INTERFACE_MODE_SGMII] = OCELOT_PORT_MODE_SGMII,
1037acf242fcSColin Foster 	[PHY_INTERFACE_MODE_QSGMII] = OCELOT_PORT_MODE_QSGMII,
1038acf242fcSColin Foster 	[PHY_INTERFACE_MODE_USXGMII] = OCELOT_PORT_MODE_USXGMII,
103911ecf341SVladimir Oltean 	[PHY_INTERFACE_MODE_1000BASEX] = OCELOT_PORT_MODE_1000BASEX,
1040acf242fcSColin Foster 	[PHY_INTERFACE_MODE_2500BASEX] = OCELOT_PORT_MODE_2500BASEX,
1041acf242fcSColin Foster };
1042acf242fcSColin Foster 
1043acf242fcSColin Foster static int felix_validate_phy_mode(struct felix *felix, int port,
1044acf242fcSColin Foster 				   phy_interface_t phy_mode)
1045acf242fcSColin Foster {
1046acf242fcSColin Foster 	u32 modes = felix->info->port_modes[port];
1047acf242fcSColin Foster 
1048acf242fcSColin Foster 	if (felix_phy_match_table[phy_mode] & modes)
1049acf242fcSColin Foster 		return 0;
1050acf242fcSColin Foster 	return -EOPNOTSUPP;
1051acf242fcSColin Foster }
1052acf242fcSColin Foster 
1053bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
1054bdeced75SVladimir Oltean 				  struct device_node *ports_node,
1055bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
1056bdeced75SVladimir Oltean {
1057bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1058bdeced75SVladimir Oltean 	struct device_node *child;
1059bdeced75SVladimir Oltean 
106037fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
1061bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
1062bdeced75SVladimir Oltean 		u32 port;
1063bdeced75SVladimir Oltean 		int err;
1064bdeced75SVladimir Oltean 
1065bdeced75SVladimir Oltean 		/* Get switch port number from DT */
1066bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
1067bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
1068bdeced75SVladimir Oltean 				"(property \"reg\")\n");
1069bdeced75SVladimir Oltean 			of_node_put(child);
1070bdeced75SVladimir Oltean 			return -ENODEV;
1071bdeced75SVladimir Oltean 		}
1072bdeced75SVladimir Oltean 
1073bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
1074bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
1075bdeced75SVladimir Oltean 		if (err) {
1076bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
1077bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
1078bdeced75SVladimir Oltean 				port);
1079bdeced75SVladimir Oltean 			of_node_put(child);
1080bdeced75SVladimir Oltean 			return -ENODEV;
1081bdeced75SVladimir Oltean 		}
1082bdeced75SVladimir Oltean 
1083acf242fcSColin Foster 		err = felix_validate_phy_mode(felix, port, phy_mode);
1084bdeced75SVladimir Oltean 		if (err < 0) {
1085bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
1086bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
108759ebb430SSumera Priyadarsini 			of_node_put(child);
1088bdeced75SVladimir Oltean 			return err;
1089bdeced75SVladimir Oltean 		}
1090bdeced75SVladimir Oltean 
1091bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
1092bdeced75SVladimir Oltean 	}
1093bdeced75SVladimir Oltean 
1094bdeced75SVladimir Oltean 	return 0;
1095bdeced75SVladimir Oltean }
1096bdeced75SVladimir Oltean 
1097bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
1098bdeced75SVladimir Oltean {
1099bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1100bdeced75SVladimir Oltean 	struct device_node *switch_node;
1101bdeced75SVladimir Oltean 	struct device_node *ports_node;
1102bdeced75SVladimir Oltean 	int err;
1103bdeced75SVladimir Oltean 
1104bdeced75SVladimir Oltean 	switch_node = dev->of_node;
1105bdeced75SVladimir Oltean 
1106bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
1107abecbfcdSVladimir Oltean 	if (!ports_node)
1108abecbfcdSVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
1109bdeced75SVladimir Oltean 	if (!ports_node) {
1110abecbfcdSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n");
1111bdeced75SVladimir Oltean 		return -ENODEV;
1112bdeced75SVladimir Oltean 	}
1113bdeced75SVladimir Oltean 
1114bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
1115bdeced75SVladimir Oltean 	of_node_put(ports_node);
1116bdeced75SVladimir Oltean 
1117bdeced75SVladimir Oltean 	return err;
1118bdeced75SVladimir Oltean }
1119bdeced75SVladimir Oltean 
112056051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
112156051948SVladimir Oltean {
112256051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
1123bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
1124b4024c9eSClaudiu Manoil 	struct resource res;
112556051948SVladimir Oltean 	int port, i, err;
112656051948SVladimir Oltean 
112756051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
112856051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
112956051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
113056051948SVladimir Oltean 	if (!ocelot->ports)
113156051948SVladimir Oltean 		return -ENOMEM;
113256051948SVladimir Oltean 
113356051948SVladimir Oltean 	ocelot->map		= felix->info->map;
113456051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
113521ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
113607d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
113777043c37SXiaoliang Yang 	ocelot->vcap_pol.base	= felix->info->vcap_pol_base;
113877043c37SXiaoliang Yang 	ocelot->vcap_pol.max	= felix->info->vcap_pol_max;
113977043c37SXiaoliang Yang 	ocelot->vcap_pol.base2	= felix->info->vcap_pol_base2;
114077043c37SXiaoliang Yang 	ocelot->vcap_pol.max2	= felix->info->vcap_pol_max2;
114156051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
1142cacea62fSVladimir Oltean 	ocelot->npi_inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
1143cacea62fSVladimir Oltean 	ocelot->npi_xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
1144f59fd9caSVladimir Oltean 	ocelot->devlink		= felix->ds->devlink;
114556051948SVladimir Oltean 
1146bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
1147bdeced75SVladimir Oltean 				 GFP_KERNEL);
1148bdeced75SVladimir Oltean 	if (!port_phy_modes)
1149bdeced75SVladimir Oltean 		return -ENOMEM;
1150bdeced75SVladimir Oltean 
1151bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
1152bdeced75SVladimir Oltean 	if (err) {
1153bdeced75SVladimir Oltean 		kfree(port_phy_modes);
1154bdeced75SVladimir Oltean 		return err;
1155bdeced75SVladimir Oltean 	}
1156bdeced75SVladimir Oltean 
115756051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
115856051948SVladimir Oltean 		struct regmap *target;
115956051948SVladimir Oltean 
116056051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
116156051948SVladimir Oltean 			continue;
116256051948SVladimir Oltean 
1163b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
1164b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1165375e1314SVladimir Oltean 		res.start += felix->switch_base;
1166375e1314SVladimir Oltean 		res.end += felix->switch_base;
116756051948SVladimir Oltean 
1168242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
116956051948SVladimir Oltean 		if (IS_ERR(target)) {
117056051948SVladimir Oltean 			dev_err(ocelot->dev,
117156051948SVladimir Oltean 				"Failed to map device memory space\n");
1172bdeced75SVladimir Oltean 			kfree(port_phy_modes);
117356051948SVladimir Oltean 			return PTR_ERR(target);
117456051948SVladimir Oltean 		}
117556051948SVladimir Oltean 
117656051948SVladimir Oltean 		ocelot->targets[i] = target;
117756051948SVladimir Oltean 	}
117856051948SVladimir Oltean 
117956051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
118056051948SVladimir Oltean 	if (err) {
118156051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
1182bdeced75SVladimir Oltean 		kfree(port_phy_modes);
118356051948SVladimir Oltean 		return err;
118456051948SVladimir Oltean 	}
118556051948SVladimir Oltean 
118656051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
118756051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
118891c724cfSVladimir Oltean 		struct regmap *target;
118956051948SVladimir Oltean 
119056051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
119156051948SVladimir Oltean 					   sizeof(struct ocelot_port),
119256051948SVladimir Oltean 					   GFP_KERNEL);
119356051948SVladimir Oltean 		if (!ocelot_port) {
119456051948SVladimir Oltean 			dev_err(ocelot->dev,
119556051948SVladimir Oltean 				"failed to allocate port memory\n");
1196bdeced75SVladimir Oltean 			kfree(port_phy_modes);
119756051948SVladimir Oltean 			return -ENOMEM;
119856051948SVladimir Oltean 		}
119956051948SVladimir Oltean 
1200b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
1201b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1202375e1314SVladimir Oltean 		res.start += felix->switch_base;
1203375e1314SVladimir Oltean 		res.end += felix->switch_base;
120456051948SVladimir Oltean 
1205242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
120691c724cfSVladimir Oltean 		if (IS_ERR(target)) {
120756051948SVladimir Oltean 			dev_err(ocelot->dev,
120891c724cfSVladimir Oltean 				"Failed to map memory space for port %d\n",
120991c724cfSVladimir Oltean 				port);
1210bdeced75SVladimir Oltean 			kfree(port_phy_modes);
121191c724cfSVladimir Oltean 			return PTR_ERR(target);
121256051948SVladimir Oltean 		}
121356051948SVladimir Oltean 
1214bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
121556051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
121691c724cfSVladimir Oltean 		ocelot_port->target = target;
12177e708760SVladimir Oltean 		ocelot_port->index = port;
121856051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
121956051948SVladimir Oltean 	}
122056051948SVladimir Oltean 
1221bdeced75SVladimir Oltean 	kfree(port_phy_modes);
1222bdeced75SVladimir Oltean 
1223bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
1224bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
1225bdeced75SVladimir Oltean 		if (err < 0)
1226bdeced75SVladimir Oltean 			return err;
1227bdeced75SVladimir Oltean 	}
1228bdeced75SVladimir Oltean 
122956051948SVladimir Oltean 	return 0;
123056051948SVladimir Oltean }
123156051948SVladimir Oltean 
12321328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port,
12331328a883SVladimir Oltean 					   struct sk_buff *skb)
12341328a883SVladimir Oltean {
12351328a883SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
12361328a883SVladimir Oltean 	struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
12371328a883SVladimir Oltean 	struct sk_buff *skb_match = NULL, *skb_tmp;
12381328a883SVladimir Oltean 	unsigned long flags;
12391328a883SVladimir Oltean 
12401328a883SVladimir Oltean 	if (!clone)
12411328a883SVladimir Oltean 		return;
12421328a883SVladimir Oltean 
12431328a883SVladimir Oltean 	spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags);
12441328a883SVladimir Oltean 
12451328a883SVladimir Oltean 	skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) {
12461328a883SVladimir Oltean 		if (skb != clone)
12471328a883SVladimir Oltean 			continue;
12481328a883SVladimir Oltean 		__skb_unlink(skb, &ocelot_port->tx_skbs);
12491328a883SVladimir Oltean 		skb_match = skb;
12501328a883SVladimir Oltean 		break;
12511328a883SVladimir Oltean 	}
12521328a883SVladimir Oltean 
12531328a883SVladimir Oltean 	spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags);
12541328a883SVladimir Oltean 
12551328a883SVladimir Oltean 	WARN_ONCE(!skb_match,
12561328a883SVladimir Oltean 		  "Could not find skb clone in TX timestamping list\n");
12571328a883SVladimir Oltean }
12581328a883SVladimir Oltean 
125949f885b2SVladimir Oltean #define work_to_xmit_work(w) \
126049f885b2SVladimir Oltean 		container_of((w), struct felix_deferred_xmit_work, work)
126149f885b2SVladimir Oltean 
126249f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work)
126349f885b2SVladimir Oltean {
126449f885b2SVladimir Oltean 	struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
126549f885b2SVladimir Oltean 	struct dsa_switch *ds = xmit_work->dp->ds;
126649f885b2SVladimir Oltean 	struct sk_buff *skb = xmit_work->skb;
126749f885b2SVladimir Oltean 	u32 rew_op = ocelot_ptp_rew_op(skb);
126849f885b2SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
126949f885b2SVladimir Oltean 	int port = xmit_work->dp->index;
127049f885b2SVladimir Oltean 	int retries = 10;
127149f885b2SVladimir Oltean 
127249f885b2SVladimir Oltean 	do {
127349f885b2SVladimir Oltean 		if (ocelot_can_inject(ocelot, 0))
127449f885b2SVladimir Oltean 			break;
127549f885b2SVladimir Oltean 
127649f885b2SVladimir Oltean 		cpu_relax();
127749f885b2SVladimir Oltean 	} while (--retries);
127849f885b2SVladimir Oltean 
127949f885b2SVladimir Oltean 	if (!retries) {
128049f885b2SVladimir Oltean 		dev_err(ocelot->dev, "port %d failed to inject skb\n",
128149f885b2SVladimir Oltean 			port);
12821328a883SVladimir Oltean 		ocelot_port_purge_txtstamp_skb(ocelot, port, skb);
128349f885b2SVladimir Oltean 		kfree_skb(skb);
128449f885b2SVladimir Oltean 		return;
128549f885b2SVladimir Oltean 	}
128649f885b2SVladimir Oltean 
128749f885b2SVladimir Oltean 	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
128849f885b2SVladimir Oltean 
128949f885b2SVladimir Oltean 	consume_skb(skb);
129049f885b2SVladimir Oltean 	kfree(xmit_work);
129149f885b2SVladimir Oltean }
129249f885b2SVladimir Oltean 
129335d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds,
129435d97680SVladimir Oltean 				      enum dsa_tag_protocol proto)
129549f885b2SVladimir Oltean {
129635d97680SVladimir Oltean 	struct ocelot_8021q_tagger_data *tagger_data;
129749f885b2SVladimir Oltean 
129835d97680SVladimir Oltean 	switch (proto) {
129935d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
130035d97680SVladimir Oltean 		tagger_data = ocelot_8021q_tagger_data(ds);
130135d97680SVladimir Oltean 		tagger_data->xmit_work_fn = felix_port_deferred_xmit;
130249f885b2SVladimir Oltean 		return 0;
130335d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
130435d97680SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
130549f885b2SVladimir Oltean 		return 0;
130635d97680SVladimir Oltean 	default:
130735d97680SVladimir Oltean 		return -EPROTONOSUPPORT;
130849f885b2SVladimir Oltean 	}
130949f885b2SVladimir Oltean }
131049f885b2SVladimir Oltean 
131156051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
131256051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
131356051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
131456051948SVladimir Oltean  * (which will not work).
131556051948SVladimir Oltean  */
131656051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
131756051948SVladimir Oltean {
131856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
131956051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
13202960bb14SVladimir Oltean 	struct dsa_port *dp;
13212960bb14SVladimir Oltean 	int err;
132256051948SVladimir Oltean 
132356051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
132456051948SVladimir Oltean 	if (err)
132556051948SVladimir Oltean 		return err;
132656051948SVladimir Oltean 
1327d1cc0e93SVladimir Oltean 	err = ocelot_init(ocelot);
1328d1cc0e93SVladimir Oltean 	if (err)
13296b73b7c9SVladimir Oltean 		goto out_mdiobus_free;
1330d1cc0e93SVladimir Oltean 
13312b49d128SYangbo Lu 	if (ocelot->ptp) {
13322ac7c6c5SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
13332b49d128SYangbo Lu 		if (err) {
13342b49d128SYangbo Lu 			dev_err(ocelot->dev,
13352b49d128SYangbo Lu 				"Timestamp initialization failed\n");
13362b49d128SYangbo Lu 			ocelot->ptp = 0;
13372b49d128SYangbo Lu 		}
13382b49d128SYangbo Lu 	}
133956051948SVladimir Oltean 
13402960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
13412960bb14SVladimir Oltean 		ocelot_init_port(ocelot, dp->index);
1342bd2b3161SXiaoliang Yang 
1343bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
1344bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
1345bd2b3161SXiaoliang Yang 		 */
13462960bb14SVladimir Oltean 		felix_port_qos_map_init(ocelot, dp->index);
134756051948SVladimir Oltean 	}
134856051948SVladimir Oltean 
1349f59fd9caSVladimir Oltean 	err = ocelot_devlink_sb_register(ocelot);
1350f59fd9caSVladimir Oltean 	if (err)
13516b73b7c9SVladimir Oltean 		goto out_deinit_ports;
1352f59fd9caSVladimir Oltean 
13537a29d220SVladimir Oltean 	/* The initial tag protocol is NPI which won't fail during initial
13547a29d220SVladimir Oltean 	 * setup, there's no real point in checking for errors.
13551cf3299bSVladimir Oltean 	 */
13567a29d220SVladimir Oltean 	felix_change_tag_protocol(ds, felix->tag_proto);
13571cf3299bSVladimir Oltean 
13580b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
1359c54913c1SVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
136054c31984SVladimir Oltean 	ds->fdb_isolation = true;
136154c31984SVladimir Oltean 	ds->max_num_bridges = ds->num_ports;
1362bdeced75SVladimir Oltean 
136356051948SVladimir Oltean 	return 0;
13646b73b7c9SVladimir Oltean 
13656b73b7c9SVladimir Oltean out_deinit_ports:
13662960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
13672960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
13686b73b7c9SVladimir Oltean 
13696b73b7c9SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
13706b73b7c9SVladimir Oltean 	ocelot_deinit(ocelot);
13716b73b7c9SVladimir Oltean 
13726b73b7c9SVladimir Oltean out_mdiobus_free:
13736b73b7c9SVladimir Oltean 	if (felix->info->mdio_bus_free)
13746b73b7c9SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
13756b73b7c9SVladimir Oltean 
13766b73b7c9SVladimir Oltean 	return err;
137756051948SVladimir Oltean }
137856051948SVladimir Oltean 
137956051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
138056051948SVladimir Oltean {
138156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1382bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
13832960bb14SVladimir Oltean 	struct dsa_port *dp;
1384bdeced75SVladimir Oltean 
13857a29d220SVladimir Oltean 	if (felix->tag_proto_ops)
13867a29d220SVladimir Oltean 		felix->tag_proto_ops->teardown(ds);
1387adb3dccfSVladimir Oltean 
13882960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
13892960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
1390d19741b0SVladimir Oltean 
139149f885b2SVladimir Oltean 	ocelot_devlink_sb_unregister(ocelot);
139249f885b2SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
139349f885b2SVladimir Oltean 	ocelot_deinit(ocelot);
139449f885b2SVladimir Oltean 
1395d19741b0SVladimir Oltean 	if (felix->info->mdio_bus_free)
1396d19741b0SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
139756051948SVladimir Oltean }
139856051948SVladimir Oltean 
1399c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
1400c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1401c0bcf537SYangbo Lu {
1402c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1403c0bcf537SYangbo Lu 
1404c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
1405c0bcf537SYangbo Lu }
1406c0bcf537SYangbo Lu 
1407c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
1408c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1409c0bcf537SYangbo Lu {
1410c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
141199348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
141299348004SVladimir Oltean 	bool using_tag_8021q;
141399348004SVladimir Oltean 	int err;
1414c0bcf537SYangbo Lu 
141599348004SVladimir Oltean 	err = ocelot_hwstamp_set(ocelot, port, ifr);
141699348004SVladimir Oltean 	if (err)
141799348004SVladimir Oltean 		return err;
141899348004SVladimir Oltean 
141999348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
142099348004SVladimir Oltean 
142199348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
1422c0bcf537SYangbo Lu }
1423c0bcf537SYangbo Lu 
1424d219b4b6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot)
14250a6f17c6SVladimir Oltean {
14260a6f17c6SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1427dbd03285SVladimir Oltean 	int err = 0, grp = 0;
14280a6f17c6SVladimir Oltean 
14290a6f17c6SVladimir Oltean 	if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q)
14300a6f17c6SVladimir Oltean 		return false;
14310a6f17c6SVladimir Oltean 
14320a6f17c6SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
14330a6f17c6SVladimir Oltean 		return false;
14340a6f17c6SVladimir Oltean 
14350a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
14360a6f17c6SVladimir Oltean 		struct sk_buff *skb;
14370a6f17c6SVladimir Oltean 		unsigned int type;
14380a6f17c6SVladimir Oltean 
14390a6f17c6SVladimir Oltean 		err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
14400a6f17c6SVladimir Oltean 		if (err)
14410a6f17c6SVladimir Oltean 			goto out;
14420a6f17c6SVladimir Oltean 
14430a6f17c6SVladimir Oltean 		/* We trap to the CPU port module all PTP frames, but
14440a6f17c6SVladimir Oltean 		 * felix_rxtstamp() only gets called for event frames.
14450a6f17c6SVladimir Oltean 		 * So we need to avoid sending duplicate general
14460a6f17c6SVladimir Oltean 		 * message frames by running a second BPF classifier
14470a6f17c6SVladimir Oltean 		 * here and dropping those.
14480a6f17c6SVladimir Oltean 		 */
14490a6f17c6SVladimir Oltean 		__skb_push(skb, ETH_HLEN);
14500a6f17c6SVladimir Oltean 
14510a6f17c6SVladimir Oltean 		type = ptp_classify_raw(skb);
14520a6f17c6SVladimir Oltean 
14530a6f17c6SVladimir Oltean 		__skb_pull(skb, ETH_HLEN);
14540a6f17c6SVladimir Oltean 
14550a6f17c6SVladimir Oltean 		if (type == PTP_CLASS_NONE) {
14560a6f17c6SVladimir Oltean 			kfree_skb(skb);
14570a6f17c6SVladimir Oltean 			continue;
14580a6f17c6SVladimir Oltean 		}
14590a6f17c6SVladimir Oltean 
14600a6f17c6SVladimir Oltean 		netif_rx(skb);
14610a6f17c6SVladimir Oltean 	}
14620a6f17c6SVladimir Oltean 
14630a6f17c6SVladimir Oltean out:
14645d3bb7ddSVladimir Oltean 	if (err < 0) {
14655d3bb7ddSVladimir Oltean 		dev_err_ratelimited(ocelot->dev,
14665d3bb7ddSVladimir Oltean 				    "Error during packet extraction: %pe\n",
14675d3bb7ddSVladimir Oltean 				    ERR_PTR(err));
14680a6f17c6SVladimir Oltean 		ocelot_drain_cpu_queue(ocelot, 0);
14695d3bb7ddSVladimir Oltean 	}
14700a6f17c6SVladimir Oltean 
14710a6f17c6SVladimir Oltean 	return true;
14720a6f17c6SVladimir Oltean }
14730a6f17c6SVladimir Oltean 
1474c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
1475c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
1476c0bcf537SYangbo Lu {
147792f62485SVladimir Oltean 	u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo;
1478c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
1479c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1480c0bcf537SYangbo Lu 	struct timespec64 ts;
148192f62485SVladimir Oltean 	u32 tstamp_hi;
148292f62485SVladimir Oltean 	u64 tstamp;
1483c0bcf537SYangbo Lu 
14840a6f17c6SVladimir Oltean 	/* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb
14850a6f17c6SVladimir Oltean 	 * for RX timestamping. Then free it, and poll for its copy through
14860a6f17c6SVladimir Oltean 	 * MMIO in the CPU port module, and inject that into the stack from
14870a6f17c6SVladimir Oltean 	 * ocelot_xtr_poll().
14880a6f17c6SVladimir Oltean 	 */
1489d219b4b6SVladimir Oltean 	if (felix_check_xtr_pkt(ocelot)) {
14900a6f17c6SVladimir Oltean 		kfree_skb(skb);
14910a6f17c6SVladimir Oltean 		return true;
14920a6f17c6SVladimir Oltean 	}
14930a6f17c6SVladimir Oltean 
1494c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1495c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1496c0bcf537SYangbo Lu 
1497c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
1498c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
1499c0bcf537SYangbo Lu 		tstamp_hi--;
1500c0bcf537SYangbo Lu 
1501c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
1502c0bcf537SYangbo Lu 
1503c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
1504c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1505c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
1506c0bcf537SYangbo Lu 	return false;
1507c0bcf537SYangbo Lu }
1508c0bcf537SYangbo Lu 
15095c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port,
15105c5416f5SYangbo Lu 			   struct sk_buff *skb)
1511c0bcf537SYangbo Lu {
1512c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1513682eaad9SYangbo Lu 	struct sk_buff *clone = NULL;
1514c0bcf537SYangbo Lu 
1515682eaad9SYangbo Lu 	if (!ocelot->ptp)
15165c5416f5SYangbo Lu 		return;
1517c0bcf537SYangbo Lu 
151852849bcfSVladimir Oltean 	if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
151952849bcfSVladimir Oltean 		dev_err_ratelimited(ds->dev,
152052849bcfSVladimir Oltean 				    "port %d delivering skb without TX timestamp\n",
152152849bcfSVladimir Oltean 				    port);
1522682eaad9SYangbo Lu 		return;
152352849bcfSVladimir Oltean 	}
1524682eaad9SYangbo Lu 
1525682eaad9SYangbo Lu 	if (clone)
1526c4b364ceSYangbo Lu 		OCELOT_SKB_CB(skb)->clone = clone;
15275c5416f5SYangbo Lu }
1528c0bcf537SYangbo Lu 
15290b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
15300b912fc9SVladimir Oltean {
15310b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15320b912fc9SVladimir Oltean 
15330b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
15340b912fc9SVladimir Oltean 
15350b912fc9SVladimir Oltean 	return 0;
15360b912fc9SVladimir Oltean }
15370b912fc9SVladimir Oltean 
15380b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
15390b912fc9SVladimir Oltean {
15400b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15410b912fc9SVladimir Oltean 
15420b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
15430b912fc9SVladimir Oltean }
15440b912fc9SVladimir Oltean 
154507d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
154607d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
154707d985eeSVladimir Oltean {
154807d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
154999348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
155099348004SVladimir Oltean 	bool using_tag_8021q;
155199348004SVladimir Oltean 	int err;
155207d985eeSVladimir Oltean 
155399348004SVladimir Oltean 	err = ocelot_cls_flower_replace(ocelot, port, cls, ingress);
155499348004SVladimir Oltean 	if (err)
155599348004SVladimir Oltean 		return err;
155699348004SVladimir Oltean 
155799348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
155899348004SVladimir Oltean 
155999348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
156007d985eeSVladimir Oltean }
156107d985eeSVladimir Oltean 
156207d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
156307d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
156407d985eeSVladimir Oltean {
156507d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
156607d985eeSVladimir Oltean 
156707d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
156807d985eeSVladimir Oltean }
156907d985eeSVladimir Oltean 
157007d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
157107d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
157207d985eeSVladimir Oltean {
157307d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
157407d985eeSVladimir Oltean 
157507d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
157607d985eeSVladimir Oltean }
157707d985eeSVladimir Oltean 
1578fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
1579fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
1580fc411eaaSVladimir Oltean {
1581fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1582fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
1583fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
15845f035af7SPo Liu 		.burst = policer->burst,
1585fc411eaaSVladimir Oltean 	};
1586fc411eaaSVladimir Oltean 
1587fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
1588fc411eaaSVladimir Oltean }
1589fc411eaaSVladimir Oltean 
1590fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
1591fc411eaaSVladimir Oltean {
1592fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1593fc411eaaSVladimir Oltean 
1594fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
1595fc411eaaSVladimir Oltean }
1596fc411eaaSVladimir Oltean 
15975e497497SVladimir Oltean static int felix_port_mirror_add(struct dsa_switch *ds, int port,
15985e497497SVladimir Oltean 				 struct dsa_mall_mirror_tc_entry *mirror,
15995e497497SVladimir Oltean 				 bool ingress, struct netlink_ext_ack *extack)
16005e497497SVladimir Oltean {
16015e497497SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
16025e497497SVladimir Oltean 
16035e497497SVladimir Oltean 	return ocelot_port_mirror_add(ocelot, port, mirror->to_local_port,
16045e497497SVladimir Oltean 				      ingress, extack);
16055e497497SVladimir Oltean }
16065e497497SVladimir Oltean 
16075e497497SVladimir Oltean static void felix_port_mirror_del(struct dsa_switch *ds, int port,
16085e497497SVladimir Oltean 				  struct dsa_mall_mirror_tc_entry *mirror)
16095e497497SVladimir Oltean {
16105e497497SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
16115e497497SVladimir Oltean 
16125e497497SVladimir Oltean 	ocelot_port_mirror_del(ocelot, port, mirror->ingress);
16135e497497SVladimir Oltean }
16145e497497SVladimir Oltean 
1615de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
1616de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
1617de143c0eSXiaoliang Yang 			       void *type_data)
1618de143c0eSXiaoliang Yang {
1619de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
1620de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
1621de143c0eSXiaoliang Yang 
1622de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
1623de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
1624de143c0eSXiaoliang Yang 	else
1625de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
1626de143c0eSXiaoliang Yang }
1627de143c0eSXiaoliang Yang 
1628f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
1629f59fd9caSVladimir Oltean 			     u16 pool_index,
1630f59fd9caSVladimir Oltean 			     struct devlink_sb_pool_info *pool_info)
1631f59fd9caSVladimir Oltean {
1632f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1633f59fd9caSVladimir Oltean 
1634f59fd9caSVladimir Oltean 	return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
1635f59fd9caSVladimir Oltean }
1636f59fd9caSVladimir Oltean 
1637f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
1638f59fd9caSVladimir Oltean 			     u16 pool_index, u32 size,
1639f59fd9caSVladimir Oltean 			     enum devlink_sb_threshold_type threshold_type,
1640f59fd9caSVladimir Oltean 			     struct netlink_ext_ack *extack)
1641f59fd9caSVladimir Oltean {
1642f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1643f59fd9caSVladimir Oltean 
1644f59fd9caSVladimir Oltean 	return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
1645f59fd9caSVladimir Oltean 				  threshold_type, extack);
1646f59fd9caSVladimir Oltean }
1647f59fd9caSVladimir Oltean 
1648f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
1649f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1650f59fd9caSVladimir Oltean 				  u32 *p_threshold)
1651f59fd9caSVladimir Oltean {
1652f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1653f59fd9caSVladimir Oltean 
1654f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
1655f59fd9caSVladimir Oltean 				       p_threshold);
1656f59fd9caSVladimir Oltean }
1657f59fd9caSVladimir Oltean 
1658f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
1659f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1660f59fd9caSVladimir Oltean 				  u32 threshold, struct netlink_ext_ack *extack)
1661f59fd9caSVladimir Oltean {
1662f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1663f59fd9caSVladimir Oltean 
1664f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
1665f59fd9caSVladimir Oltean 				       threshold, extack);
1666f59fd9caSVladimir Oltean }
1667f59fd9caSVladimir Oltean 
1668f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
1669f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1670f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1671f59fd9caSVladimir Oltean 				     u16 *p_pool_index, u32 *p_threshold)
1672f59fd9caSVladimir Oltean {
1673f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1674f59fd9caSVladimir Oltean 
1675f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
1676f59fd9caSVladimir Oltean 					  pool_type, p_pool_index,
1677f59fd9caSVladimir Oltean 					  p_threshold);
1678f59fd9caSVladimir Oltean }
1679f59fd9caSVladimir Oltean 
1680f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
1681f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1682f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1683f59fd9caSVladimir Oltean 				     u16 pool_index, u32 threshold,
1684f59fd9caSVladimir Oltean 				     struct netlink_ext_ack *extack)
1685f59fd9caSVladimir Oltean {
1686f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1687f59fd9caSVladimir Oltean 
1688f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
1689f59fd9caSVladimir Oltean 					  pool_type, pool_index, threshold,
1690f59fd9caSVladimir Oltean 					  extack);
1691f59fd9caSVladimir Oltean }
1692f59fd9caSVladimir Oltean 
1693f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds,
1694f59fd9caSVladimir Oltean 				 unsigned int sb_index)
1695f59fd9caSVladimir Oltean {
1696f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1697f59fd9caSVladimir Oltean 
1698f59fd9caSVladimir Oltean 	return ocelot_sb_occ_snapshot(ocelot, sb_index);
1699f59fd9caSVladimir Oltean }
1700f59fd9caSVladimir Oltean 
1701f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds,
1702f59fd9caSVladimir Oltean 				  unsigned int sb_index)
1703f59fd9caSVladimir Oltean {
1704f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1705f59fd9caSVladimir Oltean 
1706f59fd9caSVladimir Oltean 	return ocelot_sb_occ_max_clear(ocelot, sb_index);
1707f59fd9caSVladimir Oltean }
1708f59fd9caSVladimir Oltean 
1709f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
1710f59fd9caSVladimir Oltean 				      unsigned int sb_index, u16 pool_index,
1711f59fd9caSVladimir Oltean 				      u32 *p_cur, u32 *p_max)
1712f59fd9caSVladimir Oltean {
1713f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1714f59fd9caSVladimir Oltean 
1715f59fd9caSVladimir Oltean 	return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
1716f59fd9caSVladimir Oltean 					   p_cur, p_max);
1717f59fd9caSVladimir Oltean }
1718f59fd9caSVladimir Oltean 
1719f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
1720f59fd9caSVladimir Oltean 					 unsigned int sb_index, u16 tc_index,
1721f59fd9caSVladimir Oltean 					 enum devlink_sb_pool_type pool_type,
1722f59fd9caSVladimir Oltean 					 u32 *p_cur, u32 *p_max)
1723f59fd9caSVladimir Oltean {
1724f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1725f59fd9caSVladimir Oltean 
1726f59fd9caSVladimir Oltean 	return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
1727f59fd9caSVladimir Oltean 					      pool_type, p_cur, p_max);
1728f59fd9caSVladimir Oltean }
1729f59fd9caSVladimir Oltean 
1730a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port,
1731a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1732a026c50bSHoratiu Vultur {
1733a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1734a026c50bSHoratiu Vultur 
1735a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1736a026c50bSHoratiu Vultur }
1737a026c50bSHoratiu Vultur 
1738a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port,
1739a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1740a026c50bSHoratiu Vultur {
1741a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1742a026c50bSHoratiu Vultur 
1743a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1744a026c50bSHoratiu Vultur }
1745a026c50bSHoratiu Vultur 
1746a026c50bSHoratiu Vultur static int
1747a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port,
1748a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1749a026c50bSHoratiu Vultur {
1750a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1751a026c50bSHoratiu Vultur 
1752a026c50bSHoratiu Vultur 	return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1753a026c50bSHoratiu Vultur }
1754a026c50bSHoratiu Vultur 
1755a026c50bSHoratiu Vultur static int
1756a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port,
1757a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1758a026c50bSHoratiu Vultur {
1759a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1760a026c50bSHoratiu Vultur 
1761a026c50bSHoratiu Vultur 	return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1762a026c50bSHoratiu Vultur }
1763a026c50bSHoratiu Vultur 
1764978777d0SVladimir Oltean static int felix_port_get_default_prio(struct dsa_switch *ds, int port)
1765978777d0SVladimir Oltean {
1766978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1767978777d0SVladimir Oltean 
1768978777d0SVladimir Oltean 	return ocelot_port_get_default_prio(ocelot, port);
1769978777d0SVladimir Oltean }
1770978777d0SVladimir Oltean 
1771978777d0SVladimir Oltean static int felix_port_set_default_prio(struct dsa_switch *ds, int port,
1772978777d0SVladimir Oltean 				       u8 prio)
1773978777d0SVladimir Oltean {
1774978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1775978777d0SVladimir Oltean 
1776978777d0SVladimir Oltean 	return ocelot_port_set_default_prio(ocelot, port, prio);
1777978777d0SVladimir Oltean }
1778978777d0SVladimir Oltean 
1779978777d0SVladimir Oltean static int felix_port_get_dscp_prio(struct dsa_switch *ds, int port, u8 dscp)
1780978777d0SVladimir Oltean {
1781978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1782978777d0SVladimir Oltean 
1783978777d0SVladimir Oltean 	return ocelot_port_get_dscp_prio(ocelot, port, dscp);
1784978777d0SVladimir Oltean }
1785978777d0SVladimir Oltean 
1786978777d0SVladimir Oltean static int felix_port_add_dscp_prio(struct dsa_switch *ds, int port, u8 dscp,
1787978777d0SVladimir Oltean 				    u8 prio)
1788978777d0SVladimir Oltean {
1789978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1790978777d0SVladimir Oltean 
1791978777d0SVladimir Oltean 	return ocelot_port_add_dscp_prio(ocelot, port, dscp, prio);
1792978777d0SVladimir Oltean }
1793978777d0SVladimir Oltean 
1794978777d0SVladimir Oltean static int felix_port_del_dscp_prio(struct dsa_switch *ds, int port, u8 dscp,
1795978777d0SVladimir Oltean 				    u8 prio)
1796978777d0SVladimir Oltean {
1797978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1798978777d0SVladimir Oltean 
1799978777d0SVladimir Oltean 	return ocelot_port_del_dscp_prio(ocelot, port, dscp, prio);
1800978777d0SVladimir Oltean }
1801978777d0SVladimir Oltean 
1802375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = {
180356051948SVladimir Oltean 	.get_tag_protocol		= felix_get_tag_protocol,
1804adb3dccfSVladimir Oltean 	.change_tag_protocol		= felix_change_tag_protocol,
180535d97680SVladimir Oltean 	.connect_tag_protocol		= felix_connect_tag_protocol,
180656051948SVladimir Oltean 	.setup				= felix_setup,
180756051948SVladimir Oltean 	.teardown			= felix_teardown,
180856051948SVladimir Oltean 	.set_ageing_time		= felix_set_ageing_time,
180956051948SVladimir Oltean 	.get_strings			= felix_get_strings,
181056051948SVladimir Oltean 	.get_ethtool_stats		= felix_get_ethtool_stats,
181156051948SVladimir Oltean 	.get_sset_count			= felix_get_sset_count,
181256051948SVladimir Oltean 	.get_ts_info			= felix_get_ts_info,
181379fda660SRussell King (Oracle) 	.phylink_get_caps		= felix_phylink_get_caps,
1814bdeced75SVladimir Oltean 	.phylink_validate		= felix_phylink_validate,
1815864ba485SRussell King (Oracle) 	.phylink_mac_select_pcs		= felix_phylink_mac_select_pcs,
1816bdeced75SVladimir Oltean 	.phylink_mac_link_down		= felix_phylink_mac_link_down,
1817bdeced75SVladimir Oltean 	.phylink_mac_link_up		= felix_phylink_mac_link_up,
18185cad43a5SVladimir Oltean 	.port_fast_age			= felix_port_fast_age,
181956051948SVladimir Oltean 	.port_fdb_dump			= felix_fdb_dump,
182056051948SVladimir Oltean 	.port_fdb_add			= felix_fdb_add,
182156051948SVladimir Oltean 	.port_fdb_del			= felix_fdb_del,
1822961d8b69SVladimir Oltean 	.lag_fdb_add			= felix_lag_fdb_add,
1823961d8b69SVladimir Oltean 	.lag_fdb_del			= felix_lag_fdb_del,
1824209edf95SVladimir Oltean 	.port_mdb_add			= felix_mdb_add,
1825209edf95SVladimir Oltean 	.port_mdb_del			= felix_mdb_del,
1826421741eaSVladimir Oltean 	.port_pre_bridge_flags		= felix_pre_bridge_flags,
1827421741eaSVladimir Oltean 	.port_bridge_flags		= felix_bridge_flags,
182856051948SVladimir Oltean 	.port_bridge_join		= felix_bridge_join,
182956051948SVladimir Oltean 	.port_bridge_leave		= felix_bridge_leave,
18308fe6832eSVladimir Oltean 	.port_lag_join			= felix_lag_join,
18318fe6832eSVladimir Oltean 	.port_lag_leave			= felix_lag_leave,
18328fe6832eSVladimir Oltean 	.port_lag_change		= felix_lag_change,
183356051948SVladimir Oltean 	.port_stp_state_set		= felix_bridge_stp_state_set,
183456051948SVladimir Oltean 	.port_vlan_filtering		= felix_vlan_filtering,
183556051948SVladimir Oltean 	.port_vlan_add			= felix_vlan_add,
183656051948SVladimir Oltean 	.port_vlan_del			= felix_vlan_del,
1837c0bcf537SYangbo Lu 	.port_hwtstamp_get		= felix_hwtstamp_get,
1838c0bcf537SYangbo Lu 	.port_hwtstamp_set		= felix_hwtstamp_set,
1839c0bcf537SYangbo Lu 	.port_rxtstamp			= felix_rxtstamp,
1840c0bcf537SYangbo Lu 	.port_txtstamp			= felix_txtstamp,
18410b912fc9SVladimir Oltean 	.port_change_mtu		= felix_change_mtu,
18420b912fc9SVladimir Oltean 	.port_max_mtu			= felix_get_max_mtu,
1843fc411eaaSVladimir Oltean 	.port_policer_add		= felix_port_policer_add,
1844fc411eaaSVladimir Oltean 	.port_policer_del		= felix_port_policer_del,
18455e497497SVladimir Oltean 	.port_mirror_add		= felix_port_mirror_add,
18465e497497SVladimir Oltean 	.port_mirror_del		= felix_port_mirror_del,
184707d985eeSVladimir Oltean 	.cls_flower_add			= felix_cls_flower_add,
184807d985eeSVladimir Oltean 	.cls_flower_del			= felix_cls_flower_del,
184907d985eeSVladimir Oltean 	.cls_flower_stats		= felix_cls_flower_stats,
1850de143c0eSXiaoliang Yang 	.port_setup_tc			= felix_port_setup_tc,
1851f59fd9caSVladimir Oltean 	.devlink_sb_pool_get		= felix_sb_pool_get,
1852f59fd9caSVladimir Oltean 	.devlink_sb_pool_set		= felix_sb_pool_set,
1853f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_get	= felix_sb_port_pool_get,
1854f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_set	= felix_sb_port_pool_set,
1855f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_get	= felix_sb_tc_pool_bind_get,
1856f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_set	= felix_sb_tc_pool_bind_set,
1857f59fd9caSVladimir Oltean 	.devlink_sb_occ_snapshot	= felix_sb_occ_snapshot,
1858f59fd9caSVladimir Oltean 	.devlink_sb_occ_max_clear	= felix_sb_occ_max_clear,
1859f59fd9caSVladimir Oltean 	.devlink_sb_occ_port_pool_get	= felix_sb_occ_port_pool_get,
1860f59fd9caSVladimir Oltean 	.devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
1861a026c50bSHoratiu Vultur 	.port_mrp_add			= felix_mrp_add,
1862a026c50bSHoratiu Vultur 	.port_mrp_del			= felix_mrp_del,
1863a026c50bSHoratiu Vultur 	.port_mrp_add_ring_role		= felix_mrp_add_ring_role,
1864a026c50bSHoratiu Vultur 	.port_mrp_del_ring_role		= felix_mrp_del_ring_role,
18655da11eb4SVladimir Oltean 	.tag_8021q_vlan_add		= felix_tag_8021q_vlan_add,
18665da11eb4SVladimir Oltean 	.tag_8021q_vlan_del		= felix_tag_8021q_vlan_del,
1867978777d0SVladimir Oltean 	.port_get_default_prio		= felix_port_get_default_prio,
1868978777d0SVladimir Oltean 	.port_set_default_prio		= felix_port_set_default_prio,
1869978777d0SVladimir Oltean 	.port_get_dscp_prio		= felix_port_get_dscp_prio,
1870978777d0SVladimir Oltean 	.port_add_dscp_prio		= felix_port_add_dscp_prio,
1871978777d0SVladimir Oltean 	.port_del_dscp_prio		= felix_port_del_dscp_prio,
187272c3b0c7SVladimir Oltean 	.port_set_host_flood		= felix_port_set_host_flood,
187356051948SVladimir Oltean };
1874319e4dd1SVladimir Oltean 
1875319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
1876319e4dd1SVladimir Oltean {
1877319e4dd1SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1878319e4dd1SVladimir Oltean 	struct dsa_switch *ds = felix->ds;
1879319e4dd1SVladimir Oltean 
1880319e4dd1SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
1881319e4dd1SVladimir Oltean 		return NULL;
1882319e4dd1SVladimir Oltean 
1883319e4dd1SVladimir Oltean 	return dsa_to_port(ds, port)->slave;
1884319e4dd1SVladimir Oltean }
1885319e4dd1SVladimir Oltean 
1886319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev)
1887319e4dd1SVladimir Oltean {
1888319e4dd1SVladimir Oltean 	struct dsa_port *dp;
1889319e4dd1SVladimir Oltean 
1890319e4dd1SVladimir Oltean 	dp = dsa_port_from_netdev(dev);
1891319e4dd1SVladimir Oltean 	if (IS_ERR(dp))
1892319e4dd1SVladimir Oltean 		return -EINVAL;
1893319e4dd1SVladimir Oltean 
1894319e4dd1SVladimir Oltean 	return dp->index;
1895319e4dd1SVladimir Oltean }
1896