xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision 8c166acb60f8284470db673b6dccd43e6fedfae2)
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 
401*8c166acbSVladimir Oltean /* Alternatively to using the NPI functionality, that same hardware MAC
402*8c166acbSVladimir Oltean  * connected internally to the enetc or fman DSA master can be configured to
403*8c166acbSVladimir Oltean  * use the software-defined tag_8021q frame format. As far as the hardware is
404*8c166acbSVladimir Oltean  * concerned, it thinks it is a "dumb switch" - the queues of the CPU port
405*8c166acbSVladimir Oltean  * module are now disconnected from it, but can still be accessed through
406*8c166acbSVladimir Oltean  * register-based MMIO.
407*8c166acbSVladimir 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;
4177a29d220SVladimir Oltean 	struct dsa_port *dp, *cpu_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 
4247a29d220SVladimir Oltean 	dsa_switch_for_each_cpu_port(cpu_dp, ds) {
425*8c166acbSVladimir Oltean 		ocelot_port_set_dsa_8021q_cpu(ocelot, cpu_dp->index);
4267a29d220SVladimir Oltean 
4277a29d220SVladimir Oltean 		/* TODO we could support multiple CPU ports in tag_8021q mode */
4287a29d220SVladimir Oltean 		break;
429adb3dccfSVladimir Oltean 	}
430adb3dccfSVladimir Oltean 
4317a29d220SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
4327a29d220SVladimir Oltean 		/* This overwrites ocelot_init():
4337a29d220SVladimir Oltean 		 * Do not forward BPDU frames to the CPU port module,
4347a29d220SVladimir Oltean 		 * for 2 reasons:
4357a29d220SVladimir Oltean 		 * - When these packets are injected from the tag_8021q
4367a29d220SVladimir Oltean 		 *   CPU port, we want them to go out, not loop back
4377a29d220SVladimir Oltean 		 *   into the system.
4387a29d220SVladimir Oltean 		 * - STP traffic ingressing on a user port should go to
4397a29d220SVladimir Oltean 		 *   the tag_8021q CPU port, not to the hardware CPU
4407a29d220SVladimir Oltean 		 *   port module.
4417a29d220SVladimir Oltean 		 */
4427a29d220SVladimir Oltean 		ocelot_write_gix(ocelot,
4437a29d220SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0),
4447a29d220SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG, dp->index);
4457a29d220SVladimir Oltean 	}
4467a29d220SVladimir Oltean 
4477a29d220SVladimir Oltean 	/* The ownership of the CPU port module's queues might have just been
4487a29d220SVladimir Oltean 	 * transferred to the tag_8021q tagger from the NPI-based tagger.
4497a29d220SVladimir Oltean 	 * So there might still be all sorts of crap in the queues. On the
4507a29d220SVladimir Oltean 	 * other hand, the MMIO-based matching of PTP frames is very brittle,
4517a29d220SVladimir Oltean 	 * so we need to be careful that there are no extra frames to be
4527a29d220SVladimir Oltean 	 * dequeued over MMIO, since we would never know to discard them.
4537a29d220SVladimir Oltean 	 */
4547a29d220SVladimir Oltean 	ocelot_drain_cpu_queue(ocelot, 0);
4557a29d220SVladimir Oltean 
4567a29d220SVladimir Oltean 	return 0;
4577a29d220SVladimir Oltean }
4587a29d220SVladimir Oltean 
4597a29d220SVladimir Oltean static void felix_tag_8021q_teardown(struct dsa_switch *ds)
460adb3dccfSVladimir Oltean {
4617a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
4627a29d220SVladimir Oltean 	struct dsa_port *dp, *cpu_dp;
4637a29d220SVladimir Oltean 
4647a29d220SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
4657a29d220SVladimir Oltean 		/* Restore the logic from ocelot_init:
4667a29d220SVladimir Oltean 		 * do not forward BPDU frames to the front ports.
4677a29d220SVladimir Oltean 		 */
4687a29d220SVladimir Oltean 		ocelot_write_gix(ocelot,
4697a29d220SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
4707a29d220SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG,
4717a29d220SVladimir Oltean 				 dp->index);
4727a29d220SVladimir Oltean 	}
4737a29d220SVladimir Oltean 
4747a29d220SVladimir Oltean 	dsa_switch_for_each_cpu_port(cpu_dp, ds) {
475*8c166acbSVladimir Oltean 		ocelot_port_unset_dsa_8021q_cpu(ocelot, cpu_dp->index);
4767a29d220SVladimir Oltean 
4777a29d220SVladimir Oltean 		/* TODO we could support multiple CPU ports in tag_8021q mode */
478adb3dccfSVladimir Oltean 		break;
479adb3dccfSVladimir Oltean 	}
4807a29d220SVladimir Oltean 
4817a29d220SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
4827a29d220SVladimir Oltean }
4837a29d220SVladimir Oltean 
4847a29d220SVladimir Oltean static unsigned long felix_tag_8021q_get_host_fwd_mask(struct dsa_switch *ds)
4857a29d220SVladimir Oltean {
4867a29d220SVladimir Oltean 	return dsa_cpu_ports(ds);
4877a29d220SVladimir Oltean }
4887a29d220SVladimir Oltean 
4897a29d220SVladimir Oltean static const struct felix_tag_proto_ops felix_tag_8021q_proto_ops = {
4907a29d220SVladimir Oltean 	.setup			= felix_tag_8021q_setup,
4917a29d220SVladimir Oltean 	.teardown		= felix_tag_8021q_teardown,
4927a29d220SVladimir Oltean 	.get_host_fwd_mask	= felix_tag_8021q_get_host_fwd_mask,
4937a29d220SVladimir Oltean };
4947a29d220SVladimir Oltean 
4957a29d220SVladimir Oltean static void felix_set_host_flood(struct dsa_switch *ds, unsigned long mask,
4967a29d220SVladimir Oltean 				 bool uc, bool mc, bool bc)
4977a29d220SVladimir Oltean {
4987a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
4997a29d220SVladimir Oltean 	unsigned long val;
5007a29d220SVladimir Oltean 
5017a29d220SVladimir Oltean 	val = uc ? mask : 0;
5027a29d220SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_UC);
5037a29d220SVladimir Oltean 
5047a29d220SVladimir Oltean 	val = mc ? mask : 0;
5057a29d220SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MC);
5067a29d220SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MCIPV4);
5077a29d220SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MCIPV6);
508129b7532SVladimir Oltean 
509129b7532SVladimir Oltean 	val = bc ? mask : 0;
510129b7532SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_BC);
5117a29d220SVladimir Oltean }
5127a29d220SVladimir Oltean 
5137a29d220SVladimir Oltean static void
5147a29d220SVladimir Oltean felix_migrate_host_flood(struct dsa_switch *ds,
5157a29d220SVladimir Oltean 			 const struct felix_tag_proto_ops *proto_ops,
5167a29d220SVladimir Oltean 			 const struct felix_tag_proto_ops *old_proto_ops)
5177a29d220SVladimir Oltean {
5187a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5197a29d220SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
5207a29d220SVladimir Oltean 	unsigned long mask;
5217a29d220SVladimir Oltean 
5227a29d220SVladimir Oltean 	if (old_proto_ops) {
5237a29d220SVladimir Oltean 		mask = old_proto_ops->get_host_fwd_mask(ds);
5247a29d220SVladimir Oltean 		felix_set_host_flood(ds, mask, false, false, false);
5257a29d220SVladimir Oltean 	}
5267a29d220SVladimir Oltean 
5277a29d220SVladimir Oltean 	mask = proto_ops->get_host_fwd_mask(ds);
5287a29d220SVladimir Oltean 	felix_set_host_flood(ds, mask, !!felix->host_flood_uc_mask,
5297a29d220SVladimir Oltean 			     !!felix->host_flood_mc_mask, true);
5307a29d220SVladimir Oltean }
5317a29d220SVladimir Oltean 
5327a29d220SVladimir Oltean static int felix_migrate_mdbs(struct dsa_switch *ds,
5337a29d220SVladimir Oltean 			      const struct felix_tag_proto_ops *proto_ops,
5347a29d220SVladimir Oltean 			      const struct felix_tag_proto_ops *old_proto_ops)
5357a29d220SVladimir Oltean {
5367a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5377a29d220SVladimir Oltean 	unsigned long from, to;
5387a29d220SVladimir Oltean 
5397a29d220SVladimir Oltean 	if (!old_proto_ops)
5407a29d220SVladimir Oltean 		return 0;
5417a29d220SVladimir Oltean 
5427a29d220SVladimir Oltean 	from = old_proto_ops->get_host_fwd_mask(ds);
5437a29d220SVladimir Oltean 	to = proto_ops->get_host_fwd_mask(ds);
5447a29d220SVladimir Oltean 
5457a29d220SVladimir Oltean 	return ocelot_migrate_mdbs(ocelot, from, to);
5467a29d220SVladimir Oltean }
5477a29d220SVladimir Oltean 
5487a29d220SVladimir Oltean /* Configure the shared hardware resources for a transition between
5497a29d220SVladimir Oltean  * @old_proto_ops and @proto_ops.
5507a29d220SVladimir Oltean  * Manual migration is needed because as far as DSA is concerned, no change of
5517a29d220SVladimir Oltean  * the CPU port is taking place here, just of the tagging protocol.
5527a29d220SVladimir Oltean  */
5537a29d220SVladimir Oltean static int
5547a29d220SVladimir Oltean felix_tag_proto_setup_shared(struct dsa_switch *ds,
5557a29d220SVladimir Oltean 			     const struct felix_tag_proto_ops *proto_ops,
5567a29d220SVladimir Oltean 			     const struct felix_tag_proto_ops *old_proto_ops)
5577a29d220SVladimir Oltean {
5587a29d220SVladimir Oltean 	bool using_tag_8021q = (proto_ops == &felix_tag_8021q_proto_ops);
5597a29d220SVladimir Oltean 	int err;
5607a29d220SVladimir Oltean 
5617a29d220SVladimir Oltean 	err = felix_migrate_mdbs(ds, proto_ops, old_proto_ops);
5627a29d220SVladimir Oltean 	if (err)
5637a29d220SVladimir Oltean 		return err;
5647a29d220SVladimir Oltean 
5657a29d220SVladimir Oltean 	felix_update_trapping_destinations(ds, using_tag_8021q);
5667a29d220SVladimir Oltean 
5677a29d220SVladimir Oltean 	felix_migrate_host_flood(ds, proto_ops, old_proto_ops);
5687a29d220SVladimir Oltean 
5697a29d220SVladimir Oltean 	return 0;
570adb3dccfSVladimir Oltean }
571adb3dccfSVladimir Oltean 
572e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the
573e21268efSVladimir Oltean  * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
574e21268efSVladimir Oltean  * or the restoration is guaranteed to work.
575e21268efSVladimir Oltean  */
576bacf93b0SVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds,
577adb3dccfSVladimir Oltean 				     enum dsa_tag_protocol proto)
578adb3dccfSVladimir Oltean {
5797a29d220SVladimir Oltean 	const struct felix_tag_proto_ops *old_proto_ops, *proto_ops;
580adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
581adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
582adb3dccfSVladimir Oltean 	int err;
583adb3dccfSVladimir Oltean 
5847a29d220SVladimir Oltean 	switch (proto) {
5857a29d220SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
5867a29d220SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
5877a29d220SVladimir Oltean 		proto_ops = &felix_tag_npi_proto_ops;
588bacf93b0SVladimir Oltean 		break;
5897a29d220SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
5907a29d220SVladimir Oltean 		proto_ops = &felix_tag_8021q_proto_ops;
5917a29d220SVladimir Oltean 		break;
5927a29d220SVladimir Oltean 	default:
5937a29d220SVladimir Oltean 		return -EPROTONOSUPPORT;
594bacf93b0SVladimir Oltean 	}
595bacf93b0SVladimir Oltean 
5967a29d220SVladimir Oltean 	old_proto_ops = felix->tag_proto_ops;
5977a29d220SVladimir Oltean 
5987a29d220SVladimir Oltean 	err = proto_ops->setup(ds);
5997a29d220SVladimir Oltean 	if (err)
6007a29d220SVladimir Oltean 		goto setup_failed;
6017a29d220SVladimir Oltean 
6027a29d220SVladimir Oltean 	err = felix_tag_proto_setup_shared(ds, proto_ops, old_proto_ops);
6037a29d220SVladimir Oltean 	if (err)
6047a29d220SVladimir Oltean 		goto setup_shared_failed;
6057a29d220SVladimir Oltean 
6067a29d220SVladimir Oltean 	if (old_proto_ops)
6077a29d220SVladimir Oltean 		old_proto_ops->teardown(ds);
6087a29d220SVladimir Oltean 
6097a29d220SVladimir Oltean 	felix->tag_proto_ops = proto_ops;
610adb3dccfSVladimir Oltean 	felix->tag_proto = proto;
611adb3dccfSVladimir Oltean 
612adb3dccfSVladimir Oltean 	return 0;
6137a29d220SVladimir Oltean 
6147a29d220SVladimir Oltean setup_shared_failed:
6157a29d220SVladimir Oltean 	proto_ops->teardown(ds);
6167a29d220SVladimir Oltean setup_failed:
6177a29d220SVladimir Oltean 	return err;
618adb3dccfSVladimir Oltean }
619adb3dccfSVladimir Oltean 
62056051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
6214d776482SFlorian Fainelli 						    int port,
6224d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
62356051948SVladimir Oltean {
624adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
625adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
626adb3dccfSVladimir Oltean 
627adb3dccfSVladimir Oltean 	return felix->tag_proto;
62856051948SVladimir Oltean }
62956051948SVladimir Oltean 
63072c3b0c7SVladimir Oltean static void felix_port_set_host_flood(struct dsa_switch *ds, int port,
63172c3b0c7SVladimir Oltean 				      bool uc, bool mc)
63272c3b0c7SVladimir Oltean {
63372c3b0c7SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
63472c3b0c7SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
6357a29d220SVladimir Oltean 	unsigned long mask;
63672c3b0c7SVladimir Oltean 
63772c3b0c7SVladimir Oltean 	if (uc)
63872c3b0c7SVladimir Oltean 		felix->host_flood_uc_mask |= BIT(port);
63972c3b0c7SVladimir Oltean 	else
64072c3b0c7SVladimir Oltean 		felix->host_flood_uc_mask &= ~BIT(port);
64172c3b0c7SVladimir Oltean 
64272c3b0c7SVladimir Oltean 	if (mc)
64372c3b0c7SVladimir Oltean 		felix->host_flood_mc_mask |= BIT(port);
64472c3b0c7SVladimir Oltean 	else
64572c3b0c7SVladimir Oltean 		felix->host_flood_mc_mask &= ~BIT(port);
64672c3b0c7SVladimir Oltean 
6477a29d220SVladimir Oltean 	mask = felix->tag_proto_ops->get_host_fwd_mask(ds);
6487a29d220SVladimir Oltean 	felix_set_host_flood(ds, mask, !!felix->host_flood_uc_mask,
6497a29d220SVladimir Oltean 			     !!felix->host_flood_mc_mask, true);
65072c3b0c7SVladimir Oltean }
65172c3b0c7SVladimir Oltean 
65256051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
65356051948SVladimir Oltean 				 unsigned int ageing_time)
65456051948SVladimir Oltean {
65556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
65656051948SVladimir Oltean 
65756051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
65856051948SVladimir Oltean 
65956051948SVladimir Oltean 	return 0;
66056051948SVladimir Oltean }
66156051948SVladimir Oltean 
6625cad43a5SVladimir Oltean static void felix_port_fast_age(struct dsa_switch *ds, int port)
6635cad43a5SVladimir Oltean {
6645cad43a5SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
6655cad43a5SVladimir Oltean 	int err;
6665cad43a5SVladimir Oltean 
6675cad43a5SVladimir Oltean 	err = ocelot_mact_flush(ocelot, port);
6685cad43a5SVladimir Oltean 	if (err)
6695cad43a5SVladimir Oltean 		dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n",
6705cad43a5SVladimir Oltean 			port, ERR_PTR(err));
6715cad43a5SVladimir Oltean }
6725cad43a5SVladimir Oltean 
67356051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
67456051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
67556051948SVladimir Oltean {
67656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
67756051948SVladimir Oltean 
67856051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
67956051948SVladimir Oltean }
68056051948SVladimir Oltean 
68156051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
682c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
683c2693363SVladimir Oltean 			 struct dsa_db db)
68456051948SVladimir Oltean {
68554c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
686e9b3ba43SVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
68756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
68856051948SVladimir Oltean 
68954c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
69054c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
69154c31984SVladimir Oltean 
692e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp) && !bridge_dev &&
6937e580490SVladimir Oltean 	    dsa_fdb_present_in_other_db(ds, port, addr, vid, db))
6947e580490SVladimir Oltean 		return 0;
6957e580490SVladimir Oltean 
696e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp))
697e9b3ba43SVladimir Oltean 		port = PGID_CPU;
698e9b3ba43SVladimir Oltean 
69954c31984SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid, bridge_dev);
70056051948SVladimir Oltean }
70156051948SVladimir Oltean 
70256051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
703c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
704c2693363SVladimir Oltean 			 struct dsa_db db)
70556051948SVladimir Oltean {
70654c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
707e9b3ba43SVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
70856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
70956051948SVladimir Oltean 
71054c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
71154c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
71254c31984SVladimir Oltean 
713e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp) && !bridge_dev &&
7147e580490SVladimir Oltean 	    dsa_fdb_present_in_other_db(ds, port, addr, vid, db))
7157e580490SVladimir Oltean 		return 0;
7167e580490SVladimir Oltean 
717e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp))
718e9b3ba43SVladimir Oltean 		port = PGID_CPU;
719e9b3ba43SVladimir Oltean 
72054c31984SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid, bridge_dev);
72156051948SVladimir Oltean }
72256051948SVladimir Oltean 
723961d8b69SVladimir Oltean static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag,
724c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
725c2693363SVladimir Oltean 			     struct dsa_db db)
726961d8b69SVladimir Oltean {
72754c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
728961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
729961d8b69SVladimir Oltean 
73054c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
73154c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
73254c31984SVladimir Oltean 
73354c31984SVladimir Oltean 	return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid, bridge_dev);
734961d8b69SVladimir Oltean }
735961d8b69SVladimir Oltean 
736961d8b69SVladimir Oltean static int felix_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag lag,
737c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
738c2693363SVladimir Oltean 			     struct dsa_db db)
739961d8b69SVladimir Oltean {
74054c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
741961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
742961d8b69SVladimir Oltean 
74354c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
74454c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
74554c31984SVladimir Oltean 
74654c31984SVladimir Oltean 	return ocelot_lag_fdb_del(ocelot, lag.dev, addr, vid, bridge_dev);
747961d8b69SVladimir Oltean }
748961d8b69SVladimir Oltean 
749a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port,
750c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
751c2693363SVladimir Oltean 			 struct dsa_db db)
752209edf95SVladimir Oltean {
75354c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
754209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
755209edf95SVladimir Oltean 
75654c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
75754c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
75854c31984SVladimir Oltean 
7597e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
7607e580490SVladimir Oltean 	    dsa_mdb_present_in_other_db(ds, port, mdb, db))
7617e580490SVladimir Oltean 		return 0;
7627e580490SVladimir Oltean 
7630ddf83cdSVladimir Oltean 	if (port == ocelot->npi)
7640ddf83cdSVladimir Oltean 		port = ocelot->num_phys_ports;
7650ddf83cdSVladimir Oltean 
76654c31984SVladimir Oltean 	return ocelot_port_mdb_add(ocelot, port, mdb, bridge_dev);
767209edf95SVladimir Oltean }
768209edf95SVladimir Oltean 
769209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port,
770c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
771c2693363SVladimir Oltean 			 struct dsa_db db)
772209edf95SVladimir Oltean {
77354c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
774209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
775209edf95SVladimir Oltean 
77654c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
77754c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
77854c31984SVladimir Oltean 
7797e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
7807e580490SVladimir Oltean 	    dsa_mdb_present_in_other_db(ds, port, mdb, db))
7817e580490SVladimir Oltean 		return 0;
7827e580490SVladimir Oltean 
7830ddf83cdSVladimir Oltean 	if (port == ocelot->npi)
7840ddf83cdSVladimir Oltean 		port = ocelot->num_phys_ports;
7850ddf83cdSVladimir Oltean 
78654c31984SVladimir Oltean 	return ocelot_port_mdb_del(ocelot, port, mdb, bridge_dev);
787209edf95SVladimir Oltean }
788209edf95SVladimir Oltean 
78956051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
79056051948SVladimir Oltean 				       u8 state)
79156051948SVladimir Oltean {
79256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
79356051948SVladimir Oltean 
79456051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
79556051948SVladimir Oltean }
79656051948SVladimir Oltean 
797421741eaSVladimir Oltean static int felix_pre_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 
803421741eaSVladimir Oltean 	return ocelot_port_pre_bridge_flags(ocelot, port, val);
804421741eaSVladimir Oltean }
805421741eaSVladimir Oltean 
806421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port,
807421741eaSVladimir Oltean 			      struct switchdev_brport_flags val,
808421741eaSVladimir Oltean 			      struct netlink_ext_ack *extack)
809421741eaSVladimir Oltean {
810421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
811421741eaSVladimir Oltean 
812910ee6ccSVladimir Oltean 	if (port == ocelot->npi)
813910ee6ccSVladimir Oltean 		port = ocelot->num_phys_ports;
814910ee6ccSVladimir Oltean 
815421741eaSVladimir Oltean 	ocelot_port_bridge_flags(ocelot, port, val);
816421741eaSVladimir Oltean 
817421741eaSVladimir Oltean 	return 0;
818421741eaSVladimir Oltean }
819421741eaSVladimir Oltean 
82056051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
82106b9cce4SVladimir Oltean 			     struct dsa_bridge bridge, bool *tx_fwd_offload,
82206b9cce4SVladimir Oltean 			     struct netlink_ext_ack *extack)
82356051948SVladimir Oltean {
82456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
82556051948SVladimir Oltean 
82654c31984SVladimir Oltean 	return ocelot_port_bridge_join(ocelot, port, bridge.dev, bridge.num,
82754c31984SVladimir Oltean 				       extack);
82856051948SVladimir Oltean }
82956051948SVladimir Oltean 
83056051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
831d3eed0e5SVladimir Oltean 			       struct dsa_bridge bridge)
83256051948SVladimir Oltean {
83356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
83456051948SVladimir Oltean 
835d3eed0e5SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, bridge.dev);
83656051948SVladimir Oltean }
83756051948SVladimir Oltean 
8388fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port,
839dedd6a00SVladimir Oltean 			  struct dsa_lag lag,
8408fe6832eSVladimir Oltean 			  struct netdev_lag_upper_info *info)
8418fe6832eSVladimir Oltean {
8428fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8438fe6832eSVladimir Oltean 
844dedd6a00SVladimir Oltean 	return ocelot_port_lag_join(ocelot, port, lag.dev, info);
8458fe6832eSVladimir Oltean }
8468fe6832eSVladimir Oltean 
8478fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port,
848dedd6a00SVladimir Oltean 			   struct dsa_lag lag)
8498fe6832eSVladimir Oltean {
8508fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8518fe6832eSVladimir Oltean 
852dedd6a00SVladimir Oltean 	ocelot_port_lag_leave(ocelot, port, lag.dev);
8538fe6832eSVladimir Oltean 
8548fe6832eSVladimir Oltean 	return 0;
8558fe6832eSVladimir Oltean }
8568fe6832eSVladimir Oltean 
8578fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port)
8588fe6832eSVladimir Oltean {
8598fe6832eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
8608fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8618fe6832eSVladimir Oltean 
8628fe6832eSVladimir Oltean 	ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
8638fe6832eSVladimir Oltean 
8648fe6832eSVladimir Oltean 	return 0;
8658fe6832eSVladimir Oltean }
8668fe6832eSVladimir Oltean 
86756051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
86801af940eSVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan,
86901af940eSVladimir Oltean 			      struct netlink_ext_ack *extack)
87056051948SVladimir Oltean {
8712f0402feSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
872b7a9e0daSVladimir Oltean 	u16 flags = vlan->flags;
8732f0402feSVladimir Oltean 
8749a720680SVladimir Oltean 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
8759a720680SVladimir Oltean 	 * egress-untagged or not, pvid or not, make no difference. This
8769a720680SVladimir Oltean 	 * behavior is already better than what DSA just tries to approximate
8779a720680SVladimir Oltean 	 * when it installs the VLAN with the same flags on the CPU port.
8789a720680SVladimir Oltean 	 * Just accept any configuration, and don't let ocelot deny installing
8799a720680SVladimir Oltean 	 * multiple native VLANs on the NPI port, because the switch doesn't
8809a720680SVladimir Oltean 	 * look at the port tag settings towards the NPI interface anyway.
8819a720680SVladimir Oltean 	 */
8829a720680SVladimir Oltean 	if (port == ocelot->npi)
8839a720680SVladimir Oltean 		return 0;
8849a720680SVladimir Oltean 
885b7a9e0daSVladimir Oltean 	return ocelot_vlan_prepare(ocelot, port, vlan->vid,
8862f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_PVID,
88701af940eSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_UNTAGGED,
88801af940eSVladimir Oltean 				   extack);
88956051948SVladimir Oltean }
89056051948SVladimir Oltean 
89189153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
89289153ed6SVladimir Oltean 				struct netlink_ext_ack *extack)
89356051948SVladimir Oltean {
89456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
89556051948SVladimir Oltean 
8963b95d1b2SVladimir Oltean 	return ocelot_port_vlan_filtering(ocelot, port, enabled, extack);
89756051948SVladimir Oltean }
89856051948SVladimir Oltean 
8991958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port,
90031046a5fSVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan,
90131046a5fSVladimir Oltean 			  struct netlink_ext_ack *extack)
90256051948SVladimir Oltean {
90356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
904183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
9051958d581SVladimir Oltean 	int err;
90656051948SVladimir Oltean 
90701af940eSVladimir Oltean 	err = felix_vlan_prepare(ds, port, vlan, extack);
9081958d581SVladimir Oltean 	if (err)
9091958d581SVladimir Oltean 		return err;
9101958d581SVladimir Oltean 
9111958d581SVladimir Oltean 	return ocelot_vlan_add(ocelot, port, vlan->vid,
912183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_PVID,
913183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_UNTAGGED);
91456051948SVladimir Oltean }
91556051948SVladimir Oltean 
91656051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
91756051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
91856051948SVladimir Oltean {
91956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
92056051948SVladimir Oltean 
921b7a9e0daSVladimir Oltean 	return ocelot_vlan_del(ocelot, port, vlan->vid);
92256051948SVladimir Oltean }
92356051948SVladimir Oltean 
92479fda660SRussell King (Oracle) static void felix_phylink_get_caps(struct dsa_switch *ds, int port,
92579fda660SRussell King (Oracle) 				   struct phylink_config *config)
92679fda660SRussell King (Oracle) {
92779fda660SRussell King (Oracle) 	struct ocelot *ocelot = ds->priv;
92879fda660SRussell King (Oracle) 
929f6f04c02SRussell King (Oracle) 	/* This driver does not make use of the speed, duplex, pause or the
930f6f04c02SRussell King (Oracle) 	 * advertisement in its mac_config, so it is safe to mark this driver
931f6f04c02SRussell King (Oracle) 	 * as non-legacy.
932f6f04c02SRussell King (Oracle) 	 */
933f6f04c02SRussell King (Oracle) 	config->legacy_pre_march2020 = false;
934f6f04c02SRussell King (Oracle) 
93579fda660SRussell King (Oracle) 	__set_bit(ocelot->ports[port]->phy_mode,
93679fda660SRussell King (Oracle) 		  config->supported_interfaces);
93779fda660SRussell King (Oracle) }
93879fda660SRussell King (Oracle) 
939bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
940bdeced75SVladimir Oltean 				   unsigned long *supported,
941bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
942bdeced75SVladimir Oltean {
943bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
944375e1314SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
945bdeced75SVladimir Oltean 
946375e1314SVladimir Oltean 	if (felix->info->phylink_validate)
947375e1314SVladimir Oltean 		felix->info->phylink_validate(ocelot, port, supported, state);
948bdeced75SVladimir Oltean }
949bdeced75SVladimir Oltean 
950864ba485SRussell King (Oracle) static struct phylink_pcs *felix_phylink_mac_select_pcs(struct dsa_switch *ds,
951864ba485SRussell King (Oracle) 							int port,
952864ba485SRussell King (Oracle) 							phy_interface_t iface)
953bdeced75SVladimir Oltean {
954bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
955bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
956864ba485SRussell King (Oracle) 	struct phylink_pcs *pcs = NULL;
957bdeced75SVladimir Oltean 
95849af6a76SColin Foster 	if (felix->pcs && felix->pcs[port])
959864ba485SRussell King (Oracle) 		pcs = felix->pcs[port];
960864ba485SRussell King (Oracle) 
961864ba485SRussell King (Oracle) 	return pcs;
962bdeced75SVladimir Oltean }
963bdeced75SVladimir Oltean 
964bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
965bdeced75SVladimir Oltean 					unsigned int link_an_mode,
966bdeced75SVladimir Oltean 					phy_interface_t interface)
967bdeced75SVladimir Oltean {
968bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
969bdeced75SVladimir Oltean 
970e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
971e6e12df6SVladimir Oltean 				     FELIX_MAC_QUIRKS);
972bdeced75SVladimir Oltean }
973bdeced75SVladimir Oltean 
974bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
975bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
976bdeced75SVladimir Oltean 				      phy_interface_t interface,
9775b502a7bSRussell King 				      struct phy_device *phydev,
9785b502a7bSRussell King 				      int speed, int duplex,
9795b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
980bdeced75SVladimir Oltean {
981bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
9827e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
983bdeced75SVladimir Oltean 
984e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
985e6e12df6SVladimir Oltean 				   interface, speed, duplex, tx_pause, rx_pause,
986e6e12df6SVladimir Oltean 				   FELIX_MAC_QUIRKS);
9877e14a2dcSVladimir Oltean 
9887e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
9897e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
990bdeced75SVladimir Oltean }
991bdeced75SVladimir Oltean 
992bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
993bd2b3161SXiaoliang Yang {
994bd2b3161SXiaoliang Yang 	int i;
995bd2b3161SXiaoliang Yang 
996bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
997bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
998bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
999bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
1000bd2b3161SXiaoliang Yang 		       port);
1001bd2b3161SXiaoliang Yang 
100270d39a6eSVladimir Oltean 	for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
1003bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
1004bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
1005bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
1006bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
1007bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
1008bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
1009bd2b3161SXiaoliang Yang 			      port, i);
1010bd2b3161SXiaoliang Yang 	}
1011bd2b3161SXiaoliang Yang }
1012bd2b3161SXiaoliang Yang 
101356051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
101456051948SVladimir Oltean 			      u32 stringset, u8 *data)
101556051948SVladimir Oltean {
101656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
101756051948SVladimir Oltean 
101856051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
101956051948SVladimir Oltean }
102056051948SVladimir Oltean 
102156051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
102256051948SVladimir Oltean {
102356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
102456051948SVladimir Oltean 
102556051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
102656051948SVladimir Oltean }
102756051948SVladimir Oltean 
102856051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
102956051948SVladimir Oltean {
103056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
103156051948SVladimir Oltean 
103256051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
103356051948SVladimir Oltean }
103456051948SVladimir Oltean 
103556051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
103656051948SVladimir Oltean 			     struct ethtool_ts_info *info)
103756051948SVladimir Oltean {
103856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
103956051948SVladimir Oltean 
104056051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
104156051948SVladimir Oltean }
104256051948SVladimir Oltean 
1043acf242fcSColin Foster static const u32 felix_phy_match_table[PHY_INTERFACE_MODE_MAX] = {
1044acf242fcSColin Foster 	[PHY_INTERFACE_MODE_INTERNAL] = OCELOT_PORT_MODE_INTERNAL,
1045acf242fcSColin Foster 	[PHY_INTERFACE_MODE_SGMII] = OCELOT_PORT_MODE_SGMII,
1046acf242fcSColin Foster 	[PHY_INTERFACE_MODE_QSGMII] = OCELOT_PORT_MODE_QSGMII,
1047acf242fcSColin Foster 	[PHY_INTERFACE_MODE_USXGMII] = OCELOT_PORT_MODE_USXGMII,
104811ecf341SVladimir Oltean 	[PHY_INTERFACE_MODE_1000BASEX] = OCELOT_PORT_MODE_1000BASEX,
1049acf242fcSColin Foster 	[PHY_INTERFACE_MODE_2500BASEX] = OCELOT_PORT_MODE_2500BASEX,
1050acf242fcSColin Foster };
1051acf242fcSColin Foster 
1052acf242fcSColin Foster static int felix_validate_phy_mode(struct felix *felix, int port,
1053acf242fcSColin Foster 				   phy_interface_t phy_mode)
1054acf242fcSColin Foster {
1055acf242fcSColin Foster 	u32 modes = felix->info->port_modes[port];
1056acf242fcSColin Foster 
1057acf242fcSColin Foster 	if (felix_phy_match_table[phy_mode] & modes)
1058acf242fcSColin Foster 		return 0;
1059acf242fcSColin Foster 	return -EOPNOTSUPP;
1060acf242fcSColin Foster }
1061acf242fcSColin Foster 
1062bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
1063bdeced75SVladimir Oltean 				  struct device_node *ports_node,
1064bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
1065bdeced75SVladimir Oltean {
1066bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1067bdeced75SVladimir Oltean 	struct device_node *child;
1068bdeced75SVladimir Oltean 
106937fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
1070bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
1071bdeced75SVladimir Oltean 		u32 port;
1072bdeced75SVladimir Oltean 		int err;
1073bdeced75SVladimir Oltean 
1074bdeced75SVladimir Oltean 		/* Get switch port number from DT */
1075bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
1076bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
1077bdeced75SVladimir Oltean 				"(property \"reg\")\n");
1078bdeced75SVladimir Oltean 			of_node_put(child);
1079bdeced75SVladimir Oltean 			return -ENODEV;
1080bdeced75SVladimir Oltean 		}
1081bdeced75SVladimir Oltean 
1082bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
1083bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
1084bdeced75SVladimir Oltean 		if (err) {
1085bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
1086bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
1087bdeced75SVladimir Oltean 				port);
1088bdeced75SVladimir Oltean 			of_node_put(child);
1089bdeced75SVladimir Oltean 			return -ENODEV;
1090bdeced75SVladimir Oltean 		}
1091bdeced75SVladimir Oltean 
1092acf242fcSColin Foster 		err = felix_validate_phy_mode(felix, port, phy_mode);
1093bdeced75SVladimir Oltean 		if (err < 0) {
1094bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
1095bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
109659ebb430SSumera Priyadarsini 			of_node_put(child);
1097bdeced75SVladimir Oltean 			return err;
1098bdeced75SVladimir Oltean 		}
1099bdeced75SVladimir Oltean 
1100bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
1101bdeced75SVladimir Oltean 	}
1102bdeced75SVladimir Oltean 
1103bdeced75SVladimir Oltean 	return 0;
1104bdeced75SVladimir Oltean }
1105bdeced75SVladimir Oltean 
1106bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
1107bdeced75SVladimir Oltean {
1108bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1109bdeced75SVladimir Oltean 	struct device_node *switch_node;
1110bdeced75SVladimir Oltean 	struct device_node *ports_node;
1111bdeced75SVladimir Oltean 	int err;
1112bdeced75SVladimir Oltean 
1113bdeced75SVladimir Oltean 	switch_node = dev->of_node;
1114bdeced75SVladimir Oltean 
1115bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
1116abecbfcdSVladimir Oltean 	if (!ports_node)
1117abecbfcdSVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
1118bdeced75SVladimir Oltean 	if (!ports_node) {
1119abecbfcdSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n");
1120bdeced75SVladimir Oltean 		return -ENODEV;
1121bdeced75SVladimir Oltean 	}
1122bdeced75SVladimir Oltean 
1123bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
1124bdeced75SVladimir Oltean 	of_node_put(ports_node);
1125bdeced75SVladimir Oltean 
1126bdeced75SVladimir Oltean 	return err;
1127bdeced75SVladimir Oltean }
1128bdeced75SVladimir Oltean 
112956051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
113056051948SVladimir Oltean {
113156051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
1132bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
1133b4024c9eSClaudiu Manoil 	struct resource res;
113456051948SVladimir Oltean 	int port, i, err;
113556051948SVladimir Oltean 
113656051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
113756051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
113856051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
113956051948SVladimir Oltean 	if (!ocelot->ports)
114056051948SVladimir Oltean 		return -ENOMEM;
114156051948SVladimir Oltean 
114256051948SVladimir Oltean 	ocelot->map		= felix->info->map;
114356051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
114421ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
114507d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
114677043c37SXiaoliang Yang 	ocelot->vcap_pol.base	= felix->info->vcap_pol_base;
114777043c37SXiaoliang Yang 	ocelot->vcap_pol.max	= felix->info->vcap_pol_max;
114877043c37SXiaoliang Yang 	ocelot->vcap_pol.base2	= felix->info->vcap_pol_base2;
114977043c37SXiaoliang Yang 	ocelot->vcap_pol.max2	= felix->info->vcap_pol_max2;
115056051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
1151cacea62fSVladimir Oltean 	ocelot->npi_inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
1152cacea62fSVladimir Oltean 	ocelot->npi_xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
1153f59fd9caSVladimir Oltean 	ocelot->devlink		= felix->ds->devlink;
115456051948SVladimir Oltean 
1155bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
1156bdeced75SVladimir Oltean 				 GFP_KERNEL);
1157bdeced75SVladimir Oltean 	if (!port_phy_modes)
1158bdeced75SVladimir Oltean 		return -ENOMEM;
1159bdeced75SVladimir Oltean 
1160bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
1161bdeced75SVladimir Oltean 	if (err) {
1162bdeced75SVladimir Oltean 		kfree(port_phy_modes);
1163bdeced75SVladimir Oltean 		return err;
1164bdeced75SVladimir Oltean 	}
1165bdeced75SVladimir Oltean 
116656051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
116756051948SVladimir Oltean 		struct regmap *target;
116856051948SVladimir Oltean 
116956051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
117056051948SVladimir Oltean 			continue;
117156051948SVladimir Oltean 
1172b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
1173b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1174375e1314SVladimir Oltean 		res.start += felix->switch_base;
1175375e1314SVladimir Oltean 		res.end += felix->switch_base;
117656051948SVladimir Oltean 
1177242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
117856051948SVladimir Oltean 		if (IS_ERR(target)) {
117956051948SVladimir Oltean 			dev_err(ocelot->dev,
118056051948SVladimir Oltean 				"Failed to map device memory space\n");
1181bdeced75SVladimir Oltean 			kfree(port_phy_modes);
118256051948SVladimir Oltean 			return PTR_ERR(target);
118356051948SVladimir Oltean 		}
118456051948SVladimir Oltean 
118556051948SVladimir Oltean 		ocelot->targets[i] = target;
118656051948SVladimir Oltean 	}
118756051948SVladimir Oltean 
118856051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
118956051948SVladimir Oltean 	if (err) {
119056051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
1191bdeced75SVladimir Oltean 		kfree(port_phy_modes);
119256051948SVladimir Oltean 		return err;
119356051948SVladimir Oltean 	}
119456051948SVladimir Oltean 
119556051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
119656051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
119791c724cfSVladimir Oltean 		struct regmap *target;
119856051948SVladimir Oltean 
119956051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
120056051948SVladimir Oltean 					   sizeof(struct ocelot_port),
120156051948SVladimir Oltean 					   GFP_KERNEL);
120256051948SVladimir Oltean 		if (!ocelot_port) {
120356051948SVladimir Oltean 			dev_err(ocelot->dev,
120456051948SVladimir Oltean 				"failed to allocate port memory\n");
1205bdeced75SVladimir Oltean 			kfree(port_phy_modes);
120656051948SVladimir Oltean 			return -ENOMEM;
120756051948SVladimir Oltean 		}
120856051948SVladimir Oltean 
1209b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
1210b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1211375e1314SVladimir Oltean 		res.start += felix->switch_base;
1212375e1314SVladimir Oltean 		res.end += felix->switch_base;
121356051948SVladimir Oltean 
1214242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
121591c724cfSVladimir Oltean 		if (IS_ERR(target)) {
121656051948SVladimir Oltean 			dev_err(ocelot->dev,
121791c724cfSVladimir Oltean 				"Failed to map memory space for port %d\n",
121891c724cfSVladimir Oltean 				port);
1219bdeced75SVladimir Oltean 			kfree(port_phy_modes);
122091c724cfSVladimir Oltean 			return PTR_ERR(target);
122156051948SVladimir Oltean 		}
122256051948SVladimir Oltean 
1223bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
122456051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
122591c724cfSVladimir Oltean 		ocelot_port->target = target;
12267e708760SVladimir Oltean 		ocelot_port->index = port;
122756051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
122856051948SVladimir Oltean 	}
122956051948SVladimir Oltean 
1230bdeced75SVladimir Oltean 	kfree(port_phy_modes);
1231bdeced75SVladimir Oltean 
1232bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
1233bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
1234bdeced75SVladimir Oltean 		if (err < 0)
1235bdeced75SVladimir Oltean 			return err;
1236bdeced75SVladimir Oltean 	}
1237bdeced75SVladimir Oltean 
123856051948SVladimir Oltean 	return 0;
123956051948SVladimir Oltean }
124056051948SVladimir Oltean 
12411328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port,
12421328a883SVladimir Oltean 					   struct sk_buff *skb)
12431328a883SVladimir Oltean {
12441328a883SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
12451328a883SVladimir Oltean 	struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
12461328a883SVladimir Oltean 	struct sk_buff *skb_match = NULL, *skb_tmp;
12471328a883SVladimir Oltean 	unsigned long flags;
12481328a883SVladimir Oltean 
12491328a883SVladimir Oltean 	if (!clone)
12501328a883SVladimir Oltean 		return;
12511328a883SVladimir Oltean 
12521328a883SVladimir Oltean 	spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags);
12531328a883SVladimir Oltean 
12541328a883SVladimir Oltean 	skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) {
12551328a883SVladimir Oltean 		if (skb != clone)
12561328a883SVladimir Oltean 			continue;
12571328a883SVladimir Oltean 		__skb_unlink(skb, &ocelot_port->tx_skbs);
12581328a883SVladimir Oltean 		skb_match = skb;
12591328a883SVladimir Oltean 		break;
12601328a883SVladimir Oltean 	}
12611328a883SVladimir Oltean 
12621328a883SVladimir Oltean 	spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags);
12631328a883SVladimir Oltean 
12641328a883SVladimir Oltean 	WARN_ONCE(!skb_match,
12651328a883SVladimir Oltean 		  "Could not find skb clone in TX timestamping list\n");
12661328a883SVladimir Oltean }
12671328a883SVladimir Oltean 
126849f885b2SVladimir Oltean #define work_to_xmit_work(w) \
126949f885b2SVladimir Oltean 		container_of((w), struct felix_deferred_xmit_work, work)
127049f885b2SVladimir Oltean 
127149f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work)
127249f885b2SVladimir Oltean {
127349f885b2SVladimir Oltean 	struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
127449f885b2SVladimir Oltean 	struct dsa_switch *ds = xmit_work->dp->ds;
127549f885b2SVladimir Oltean 	struct sk_buff *skb = xmit_work->skb;
127649f885b2SVladimir Oltean 	u32 rew_op = ocelot_ptp_rew_op(skb);
127749f885b2SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
127849f885b2SVladimir Oltean 	int port = xmit_work->dp->index;
127949f885b2SVladimir Oltean 	int retries = 10;
128049f885b2SVladimir Oltean 
128149f885b2SVladimir Oltean 	do {
128249f885b2SVladimir Oltean 		if (ocelot_can_inject(ocelot, 0))
128349f885b2SVladimir Oltean 			break;
128449f885b2SVladimir Oltean 
128549f885b2SVladimir Oltean 		cpu_relax();
128649f885b2SVladimir Oltean 	} while (--retries);
128749f885b2SVladimir Oltean 
128849f885b2SVladimir Oltean 	if (!retries) {
128949f885b2SVladimir Oltean 		dev_err(ocelot->dev, "port %d failed to inject skb\n",
129049f885b2SVladimir Oltean 			port);
12911328a883SVladimir Oltean 		ocelot_port_purge_txtstamp_skb(ocelot, port, skb);
129249f885b2SVladimir Oltean 		kfree_skb(skb);
129349f885b2SVladimir Oltean 		return;
129449f885b2SVladimir Oltean 	}
129549f885b2SVladimir Oltean 
129649f885b2SVladimir Oltean 	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
129749f885b2SVladimir Oltean 
129849f885b2SVladimir Oltean 	consume_skb(skb);
129949f885b2SVladimir Oltean 	kfree(xmit_work);
130049f885b2SVladimir Oltean }
130149f885b2SVladimir Oltean 
130235d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds,
130335d97680SVladimir Oltean 				      enum dsa_tag_protocol proto)
130449f885b2SVladimir Oltean {
130535d97680SVladimir Oltean 	struct ocelot_8021q_tagger_data *tagger_data;
130649f885b2SVladimir Oltean 
130735d97680SVladimir Oltean 	switch (proto) {
130835d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
130935d97680SVladimir Oltean 		tagger_data = ocelot_8021q_tagger_data(ds);
131035d97680SVladimir Oltean 		tagger_data->xmit_work_fn = felix_port_deferred_xmit;
131149f885b2SVladimir Oltean 		return 0;
131235d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
131335d97680SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
131449f885b2SVladimir Oltean 		return 0;
131535d97680SVladimir Oltean 	default:
131635d97680SVladimir Oltean 		return -EPROTONOSUPPORT;
131749f885b2SVladimir Oltean 	}
131849f885b2SVladimir Oltean }
131949f885b2SVladimir Oltean 
132056051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
132156051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
132256051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
132356051948SVladimir Oltean  * (which will not work).
132456051948SVladimir Oltean  */
132556051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
132656051948SVladimir Oltean {
132756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
132856051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
13292960bb14SVladimir Oltean 	struct dsa_port *dp;
13302960bb14SVladimir Oltean 	int err;
133156051948SVladimir Oltean 
133256051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
133356051948SVladimir Oltean 	if (err)
133456051948SVladimir Oltean 		return err;
133556051948SVladimir Oltean 
1336d1cc0e93SVladimir Oltean 	err = ocelot_init(ocelot);
1337d1cc0e93SVladimir Oltean 	if (err)
13386b73b7c9SVladimir Oltean 		goto out_mdiobus_free;
1339d1cc0e93SVladimir Oltean 
13402b49d128SYangbo Lu 	if (ocelot->ptp) {
13412ac7c6c5SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
13422b49d128SYangbo Lu 		if (err) {
13432b49d128SYangbo Lu 			dev_err(ocelot->dev,
13442b49d128SYangbo Lu 				"Timestamp initialization failed\n");
13452b49d128SYangbo Lu 			ocelot->ptp = 0;
13462b49d128SYangbo Lu 		}
13472b49d128SYangbo Lu 	}
134856051948SVladimir Oltean 
13492960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
13502960bb14SVladimir Oltean 		ocelot_init_port(ocelot, dp->index);
1351bd2b3161SXiaoliang Yang 
1352bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
1353bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
1354bd2b3161SXiaoliang Yang 		 */
13552960bb14SVladimir Oltean 		felix_port_qos_map_init(ocelot, dp->index);
135656051948SVladimir Oltean 	}
135756051948SVladimir Oltean 
1358f59fd9caSVladimir Oltean 	err = ocelot_devlink_sb_register(ocelot);
1359f59fd9caSVladimir Oltean 	if (err)
13606b73b7c9SVladimir Oltean 		goto out_deinit_ports;
1361f59fd9caSVladimir Oltean 
13627a29d220SVladimir Oltean 	/* The initial tag protocol is NPI which won't fail during initial
13637a29d220SVladimir Oltean 	 * setup, there's no real point in checking for errors.
13641cf3299bSVladimir Oltean 	 */
13657a29d220SVladimir Oltean 	felix_change_tag_protocol(ds, felix->tag_proto);
13661cf3299bSVladimir Oltean 
13670b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
1368c54913c1SVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
136954c31984SVladimir Oltean 	ds->fdb_isolation = true;
137054c31984SVladimir Oltean 	ds->max_num_bridges = ds->num_ports;
1371bdeced75SVladimir Oltean 
137256051948SVladimir Oltean 	return 0;
13736b73b7c9SVladimir Oltean 
13746b73b7c9SVladimir Oltean out_deinit_ports:
13752960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
13762960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
13776b73b7c9SVladimir Oltean 
13786b73b7c9SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
13796b73b7c9SVladimir Oltean 	ocelot_deinit(ocelot);
13806b73b7c9SVladimir Oltean 
13816b73b7c9SVladimir Oltean out_mdiobus_free:
13826b73b7c9SVladimir Oltean 	if (felix->info->mdio_bus_free)
13836b73b7c9SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
13846b73b7c9SVladimir Oltean 
13856b73b7c9SVladimir Oltean 	return err;
138656051948SVladimir Oltean }
138756051948SVladimir Oltean 
138856051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
138956051948SVladimir Oltean {
139056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1391bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
13922960bb14SVladimir Oltean 	struct dsa_port *dp;
1393bdeced75SVladimir Oltean 
13947a29d220SVladimir Oltean 	if (felix->tag_proto_ops)
13957a29d220SVladimir Oltean 		felix->tag_proto_ops->teardown(ds);
1396adb3dccfSVladimir Oltean 
13972960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
13982960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
1399d19741b0SVladimir Oltean 
140049f885b2SVladimir Oltean 	ocelot_devlink_sb_unregister(ocelot);
140149f885b2SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
140249f885b2SVladimir Oltean 	ocelot_deinit(ocelot);
140349f885b2SVladimir Oltean 
1404d19741b0SVladimir Oltean 	if (felix->info->mdio_bus_free)
1405d19741b0SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
140656051948SVladimir Oltean }
140756051948SVladimir Oltean 
1408c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
1409c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1410c0bcf537SYangbo Lu {
1411c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1412c0bcf537SYangbo Lu 
1413c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
1414c0bcf537SYangbo Lu }
1415c0bcf537SYangbo Lu 
1416c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
1417c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1418c0bcf537SYangbo Lu {
1419c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
142099348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
142199348004SVladimir Oltean 	bool using_tag_8021q;
142299348004SVladimir Oltean 	int err;
1423c0bcf537SYangbo Lu 
142499348004SVladimir Oltean 	err = ocelot_hwstamp_set(ocelot, port, ifr);
142599348004SVladimir Oltean 	if (err)
142699348004SVladimir Oltean 		return err;
142799348004SVladimir Oltean 
142899348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
142999348004SVladimir Oltean 
143099348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
1431c0bcf537SYangbo Lu }
1432c0bcf537SYangbo Lu 
1433d219b4b6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot)
14340a6f17c6SVladimir Oltean {
14350a6f17c6SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1436dbd03285SVladimir Oltean 	int err = 0, grp = 0;
14370a6f17c6SVladimir Oltean 
14380a6f17c6SVladimir Oltean 	if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q)
14390a6f17c6SVladimir Oltean 		return false;
14400a6f17c6SVladimir Oltean 
14410a6f17c6SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
14420a6f17c6SVladimir Oltean 		return false;
14430a6f17c6SVladimir Oltean 
14440a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
14450a6f17c6SVladimir Oltean 		struct sk_buff *skb;
14460a6f17c6SVladimir Oltean 		unsigned int type;
14470a6f17c6SVladimir Oltean 
14480a6f17c6SVladimir Oltean 		err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
14490a6f17c6SVladimir Oltean 		if (err)
14500a6f17c6SVladimir Oltean 			goto out;
14510a6f17c6SVladimir Oltean 
14520a6f17c6SVladimir Oltean 		/* We trap to the CPU port module all PTP frames, but
14530a6f17c6SVladimir Oltean 		 * felix_rxtstamp() only gets called for event frames.
14540a6f17c6SVladimir Oltean 		 * So we need to avoid sending duplicate general
14550a6f17c6SVladimir Oltean 		 * message frames by running a second BPF classifier
14560a6f17c6SVladimir Oltean 		 * here and dropping those.
14570a6f17c6SVladimir Oltean 		 */
14580a6f17c6SVladimir Oltean 		__skb_push(skb, ETH_HLEN);
14590a6f17c6SVladimir Oltean 
14600a6f17c6SVladimir Oltean 		type = ptp_classify_raw(skb);
14610a6f17c6SVladimir Oltean 
14620a6f17c6SVladimir Oltean 		__skb_pull(skb, ETH_HLEN);
14630a6f17c6SVladimir Oltean 
14640a6f17c6SVladimir Oltean 		if (type == PTP_CLASS_NONE) {
14650a6f17c6SVladimir Oltean 			kfree_skb(skb);
14660a6f17c6SVladimir Oltean 			continue;
14670a6f17c6SVladimir Oltean 		}
14680a6f17c6SVladimir Oltean 
14690a6f17c6SVladimir Oltean 		netif_rx(skb);
14700a6f17c6SVladimir Oltean 	}
14710a6f17c6SVladimir Oltean 
14720a6f17c6SVladimir Oltean out:
14735d3bb7ddSVladimir Oltean 	if (err < 0) {
14745d3bb7ddSVladimir Oltean 		dev_err_ratelimited(ocelot->dev,
14755d3bb7ddSVladimir Oltean 				    "Error during packet extraction: %pe\n",
14765d3bb7ddSVladimir Oltean 				    ERR_PTR(err));
14770a6f17c6SVladimir Oltean 		ocelot_drain_cpu_queue(ocelot, 0);
14785d3bb7ddSVladimir Oltean 	}
14790a6f17c6SVladimir Oltean 
14800a6f17c6SVladimir Oltean 	return true;
14810a6f17c6SVladimir Oltean }
14820a6f17c6SVladimir Oltean 
1483c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
1484c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
1485c0bcf537SYangbo Lu {
148692f62485SVladimir Oltean 	u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo;
1487c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
1488c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1489c0bcf537SYangbo Lu 	struct timespec64 ts;
149092f62485SVladimir Oltean 	u32 tstamp_hi;
149192f62485SVladimir Oltean 	u64 tstamp;
1492c0bcf537SYangbo Lu 
14930a6f17c6SVladimir Oltean 	/* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb
14940a6f17c6SVladimir Oltean 	 * for RX timestamping. Then free it, and poll for its copy through
14950a6f17c6SVladimir Oltean 	 * MMIO in the CPU port module, and inject that into the stack from
14960a6f17c6SVladimir Oltean 	 * ocelot_xtr_poll().
14970a6f17c6SVladimir Oltean 	 */
1498d219b4b6SVladimir Oltean 	if (felix_check_xtr_pkt(ocelot)) {
14990a6f17c6SVladimir Oltean 		kfree_skb(skb);
15000a6f17c6SVladimir Oltean 		return true;
15010a6f17c6SVladimir Oltean 	}
15020a6f17c6SVladimir Oltean 
1503c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1504c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1505c0bcf537SYangbo Lu 
1506c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
1507c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
1508c0bcf537SYangbo Lu 		tstamp_hi--;
1509c0bcf537SYangbo Lu 
1510c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
1511c0bcf537SYangbo Lu 
1512c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
1513c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1514c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
1515c0bcf537SYangbo Lu 	return false;
1516c0bcf537SYangbo Lu }
1517c0bcf537SYangbo Lu 
15185c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port,
15195c5416f5SYangbo Lu 			   struct sk_buff *skb)
1520c0bcf537SYangbo Lu {
1521c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1522682eaad9SYangbo Lu 	struct sk_buff *clone = NULL;
1523c0bcf537SYangbo Lu 
1524682eaad9SYangbo Lu 	if (!ocelot->ptp)
15255c5416f5SYangbo Lu 		return;
1526c0bcf537SYangbo Lu 
152752849bcfSVladimir Oltean 	if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
152852849bcfSVladimir Oltean 		dev_err_ratelimited(ds->dev,
152952849bcfSVladimir Oltean 				    "port %d delivering skb without TX timestamp\n",
153052849bcfSVladimir Oltean 				    port);
1531682eaad9SYangbo Lu 		return;
153252849bcfSVladimir Oltean 	}
1533682eaad9SYangbo Lu 
1534682eaad9SYangbo Lu 	if (clone)
1535c4b364ceSYangbo Lu 		OCELOT_SKB_CB(skb)->clone = clone;
15365c5416f5SYangbo Lu }
1537c0bcf537SYangbo Lu 
15380b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
15390b912fc9SVladimir Oltean {
15400b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15410b912fc9SVladimir Oltean 
15420b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
15430b912fc9SVladimir Oltean 
15440b912fc9SVladimir Oltean 	return 0;
15450b912fc9SVladimir Oltean }
15460b912fc9SVladimir Oltean 
15470b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
15480b912fc9SVladimir Oltean {
15490b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15500b912fc9SVladimir Oltean 
15510b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
15520b912fc9SVladimir Oltean }
15530b912fc9SVladimir Oltean 
155407d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
155507d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
155607d985eeSVladimir Oltean {
155707d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
155899348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
155999348004SVladimir Oltean 	bool using_tag_8021q;
156099348004SVladimir Oltean 	int err;
156107d985eeSVladimir Oltean 
156299348004SVladimir Oltean 	err = ocelot_cls_flower_replace(ocelot, port, cls, ingress);
156399348004SVladimir Oltean 	if (err)
156499348004SVladimir Oltean 		return err;
156599348004SVladimir Oltean 
156699348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
156799348004SVladimir Oltean 
156899348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
156907d985eeSVladimir Oltean }
157007d985eeSVladimir Oltean 
157107d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
157207d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
157307d985eeSVladimir Oltean {
157407d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
157507d985eeSVladimir Oltean 
157607d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
157707d985eeSVladimir Oltean }
157807d985eeSVladimir Oltean 
157907d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
158007d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
158107d985eeSVladimir Oltean {
158207d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
158307d985eeSVladimir Oltean 
158407d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
158507d985eeSVladimir Oltean }
158607d985eeSVladimir Oltean 
1587fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
1588fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
1589fc411eaaSVladimir Oltean {
1590fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1591fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
1592fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
15935f035af7SPo Liu 		.burst = policer->burst,
1594fc411eaaSVladimir Oltean 	};
1595fc411eaaSVladimir Oltean 
1596fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
1597fc411eaaSVladimir Oltean }
1598fc411eaaSVladimir Oltean 
1599fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
1600fc411eaaSVladimir Oltean {
1601fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1602fc411eaaSVladimir Oltean 
1603fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
1604fc411eaaSVladimir Oltean }
1605fc411eaaSVladimir Oltean 
16065e497497SVladimir Oltean static int felix_port_mirror_add(struct dsa_switch *ds, int port,
16075e497497SVladimir Oltean 				 struct dsa_mall_mirror_tc_entry *mirror,
16085e497497SVladimir Oltean 				 bool ingress, struct netlink_ext_ack *extack)
16095e497497SVladimir Oltean {
16105e497497SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
16115e497497SVladimir Oltean 
16125e497497SVladimir Oltean 	return ocelot_port_mirror_add(ocelot, port, mirror->to_local_port,
16135e497497SVladimir Oltean 				      ingress, extack);
16145e497497SVladimir Oltean }
16155e497497SVladimir Oltean 
16165e497497SVladimir Oltean static void felix_port_mirror_del(struct dsa_switch *ds, int port,
16175e497497SVladimir Oltean 				  struct dsa_mall_mirror_tc_entry *mirror)
16185e497497SVladimir Oltean {
16195e497497SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
16205e497497SVladimir Oltean 
16215e497497SVladimir Oltean 	ocelot_port_mirror_del(ocelot, port, mirror->ingress);
16225e497497SVladimir Oltean }
16235e497497SVladimir Oltean 
1624de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
1625de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
1626de143c0eSXiaoliang Yang 			       void *type_data)
1627de143c0eSXiaoliang Yang {
1628de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
1629de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
1630de143c0eSXiaoliang Yang 
1631de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
1632de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
1633de143c0eSXiaoliang Yang 	else
1634de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
1635de143c0eSXiaoliang Yang }
1636de143c0eSXiaoliang Yang 
1637f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
1638f59fd9caSVladimir Oltean 			     u16 pool_index,
1639f59fd9caSVladimir Oltean 			     struct devlink_sb_pool_info *pool_info)
1640f59fd9caSVladimir Oltean {
1641f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1642f59fd9caSVladimir Oltean 
1643f59fd9caSVladimir Oltean 	return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
1644f59fd9caSVladimir Oltean }
1645f59fd9caSVladimir Oltean 
1646f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
1647f59fd9caSVladimir Oltean 			     u16 pool_index, u32 size,
1648f59fd9caSVladimir Oltean 			     enum devlink_sb_threshold_type threshold_type,
1649f59fd9caSVladimir Oltean 			     struct netlink_ext_ack *extack)
1650f59fd9caSVladimir Oltean {
1651f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1652f59fd9caSVladimir Oltean 
1653f59fd9caSVladimir Oltean 	return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
1654f59fd9caSVladimir Oltean 				  threshold_type, extack);
1655f59fd9caSVladimir Oltean }
1656f59fd9caSVladimir Oltean 
1657f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
1658f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1659f59fd9caSVladimir Oltean 				  u32 *p_threshold)
1660f59fd9caSVladimir Oltean {
1661f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1662f59fd9caSVladimir Oltean 
1663f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
1664f59fd9caSVladimir Oltean 				       p_threshold);
1665f59fd9caSVladimir Oltean }
1666f59fd9caSVladimir Oltean 
1667f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
1668f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1669f59fd9caSVladimir Oltean 				  u32 threshold, struct netlink_ext_ack *extack)
1670f59fd9caSVladimir Oltean {
1671f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1672f59fd9caSVladimir Oltean 
1673f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
1674f59fd9caSVladimir Oltean 				       threshold, extack);
1675f59fd9caSVladimir Oltean }
1676f59fd9caSVladimir Oltean 
1677f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
1678f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1679f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1680f59fd9caSVladimir Oltean 				     u16 *p_pool_index, u32 *p_threshold)
1681f59fd9caSVladimir Oltean {
1682f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1683f59fd9caSVladimir Oltean 
1684f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
1685f59fd9caSVladimir Oltean 					  pool_type, p_pool_index,
1686f59fd9caSVladimir Oltean 					  p_threshold);
1687f59fd9caSVladimir Oltean }
1688f59fd9caSVladimir Oltean 
1689f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
1690f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1691f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1692f59fd9caSVladimir Oltean 				     u16 pool_index, u32 threshold,
1693f59fd9caSVladimir Oltean 				     struct netlink_ext_ack *extack)
1694f59fd9caSVladimir Oltean {
1695f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1696f59fd9caSVladimir Oltean 
1697f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
1698f59fd9caSVladimir Oltean 					  pool_type, pool_index, threshold,
1699f59fd9caSVladimir Oltean 					  extack);
1700f59fd9caSVladimir Oltean }
1701f59fd9caSVladimir Oltean 
1702f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds,
1703f59fd9caSVladimir Oltean 				 unsigned int sb_index)
1704f59fd9caSVladimir Oltean {
1705f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1706f59fd9caSVladimir Oltean 
1707f59fd9caSVladimir Oltean 	return ocelot_sb_occ_snapshot(ocelot, sb_index);
1708f59fd9caSVladimir Oltean }
1709f59fd9caSVladimir Oltean 
1710f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds,
1711f59fd9caSVladimir Oltean 				  unsigned int sb_index)
1712f59fd9caSVladimir Oltean {
1713f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1714f59fd9caSVladimir Oltean 
1715f59fd9caSVladimir Oltean 	return ocelot_sb_occ_max_clear(ocelot, sb_index);
1716f59fd9caSVladimir Oltean }
1717f59fd9caSVladimir Oltean 
1718f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
1719f59fd9caSVladimir Oltean 				      unsigned int sb_index, u16 pool_index,
1720f59fd9caSVladimir Oltean 				      u32 *p_cur, u32 *p_max)
1721f59fd9caSVladimir Oltean {
1722f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1723f59fd9caSVladimir Oltean 
1724f59fd9caSVladimir Oltean 	return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
1725f59fd9caSVladimir Oltean 					   p_cur, p_max);
1726f59fd9caSVladimir Oltean }
1727f59fd9caSVladimir Oltean 
1728f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
1729f59fd9caSVladimir Oltean 					 unsigned int sb_index, u16 tc_index,
1730f59fd9caSVladimir Oltean 					 enum devlink_sb_pool_type pool_type,
1731f59fd9caSVladimir Oltean 					 u32 *p_cur, u32 *p_max)
1732f59fd9caSVladimir Oltean {
1733f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1734f59fd9caSVladimir Oltean 
1735f59fd9caSVladimir Oltean 	return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
1736f59fd9caSVladimir Oltean 					      pool_type, p_cur, p_max);
1737f59fd9caSVladimir Oltean }
1738f59fd9caSVladimir Oltean 
1739a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port,
1740a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1741a026c50bSHoratiu Vultur {
1742a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1743a026c50bSHoratiu Vultur 
1744a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1745a026c50bSHoratiu Vultur }
1746a026c50bSHoratiu Vultur 
1747a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port,
1748a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1749a026c50bSHoratiu Vultur {
1750a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1751a026c50bSHoratiu Vultur 
1752a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1753a026c50bSHoratiu Vultur }
1754a026c50bSHoratiu Vultur 
1755a026c50bSHoratiu Vultur static int
1756a026c50bSHoratiu Vultur felix_mrp_add_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_add_ring_role(ocelot, port, mrp);
1762a026c50bSHoratiu Vultur }
1763a026c50bSHoratiu Vultur 
1764a026c50bSHoratiu Vultur static int
1765a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port,
1766a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1767a026c50bSHoratiu Vultur {
1768a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1769a026c50bSHoratiu Vultur 
1770a026c50bSHoratiu Vultur 	return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1771a026c50bSHoratiu Vultur }
1772a026c50bSHoratiu Vultur 
1773978777d0SVladimir Oltean static int felix_port_get_default_prio(struct dsa_switch *ds, int port)
1774978777d0SVladimir Oltean {
1775978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1776978777d0SVladimir Oltean 
1777978777d0SVladimir Oltean 	return ocelot_port_get_default_prio(ocelot, port);
1778978777d0SVladimir Oltean }
1779978777d0SVladimir Oltean 
1780978777d0SVladimir Oltean static int felix_port_set_default_prio(struct dsa_switch *ds, int port,
1781978777d0SVladimir Oltean 				       u8 prio)
1782978777d0SVladimir Oltean {
1783978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1784978777d0SVladimir Oltean 
1785978777d0SVladimir Oltean 	return ocelot_port_set_default_prio(ocelot, port, prio);
1786978777d0SVladimir Oltean }
1787978777d0SVladimir Oltean 
1788978777d0SVladimir Oltean static int felix_port_get_dscp_prio(struct dsa_switch *ds, int port, u8 dscp)
1789978777d0SVladimir Oltean {
1790978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1791978777d0SVladimir Oltean 
1792978777d0SVladimir Oltean 	return ocelot_port_get_dscp_prio(ocelot, port, dscp);
1793978777d0SVladimir Oltean }
1794978777d0SVladimir Oltean 
1795978777d0SVladimir Oltean static int felix_port_add_dscp_prio(struct dsa_switch *ds, int port, u8 dscp,
1796978777d0SVladimir Oltean 				    u8 prio)
1797978777d0SVladimir Oltean {
1798978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1799978777d0SVladimir Oltean 
1800978777d0SVladimir Oltean 	return ocelot_port_add_dscp_prio(ocelot, port, dscp, prio);
1801978777d0SVladimir Oltean }
1802978777d0SVladimir Oltean 
1803978777d0SVladimir Oltean static int felix_port_del_dscp_prio(struct dsa_switch *ds, int port, u8 dscp,
1804978777d0SVladimir Oltean 				    u8 prio)
1805978777d0SVladimir Oltean {
1806978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1807978777d0SVladimir Oltean 
1808978777d0SVladimir Oltean 	return ocelot_port_del_dscp_prio(ocelot, port, dscp, prio);
1809978777d0SVladimir Oltean }
1810978777d0SVladimir Oltean 
1811375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = {
181256051948SVladimir Oltean 	.get_tag_protocol		= felix_get_tag_protocol,
1813adb3dccfSVladimir Oltean 	.change_tag_protocol		= felix_change_tag_protocol,
181435d97680SVladimir Oltean 	.connect_tag_protocol		= felix_connect_tag_protocol,
181556051948SVladimir Oltean 	.setup				= felix_setup,
181656051948SVladimir Oltean 	.teardown			= felix_teardown,
181756051948SVladimir Oltean 	.set_ageing_time		= felix_set_ageing_time,
181856051948SVladimir Oltean 	.get_strings			= felix_get_strings,
181956051948SVladimir Oltean 	.get_ethtool_stats		= felix_get_ethtool_stats,
182056051948SVladimir Oltean 	.get_sset_count			= felix_get_sset_count,
182156051948SVladimir Oltean 	.get_ts_info			= felix_get_ts_info,
182279fda660SRussell King (Oracle) 	.phylink_get_caps		= felix_phylink_get_caps,
1823bdeced75SVladimir Oltean 	.phylink_validate		= felix_phylink_validate,
1824864ba485SRussell King (Oracle) 	.phylink_mac_select_pcs		= felix_phylink_mac_select_pcs,
1825bdeced75SVladimir Oltean 	.phylink_mac_link_down		= felix_phylink_mac_link_down,
1826bdeced75SVladimir Oltean 	.phylink_mac_link_up		= felix_phylink_mac_link_up,
18275cad43a5SVladimir Oltean 	.port_fast_age			= felix_port_fast_age,
182856051948SVladimir Oltean 	.port_fdb_dump			= felix_fdb_dump,
182956051948SVladimir Oltean 	.port_fdb_add			= felix_fdb_add,
183056051948SVladimir Oltean 	.port_fdb_del			= felix_fdb_del,
1831961d8b69SVladimir Oltean 	.lag_fdb_add			= felix_lag_fdb_add,
1832961d8b69SVladimir Oltean 	.lag_fdb_del			= felix_lag_fdb_del,
1833209edf95SVladimir Oltean 	.port_mdb_add			= felix_mdb_add,
1834209edf95SVladimir Oltean 	.port_mdb_del			= felix_mdb_del,
1835421741eaSVladimir Oltean 	.port_pre_bridge_flags		= felix_pre_bridge_flags,
1836421741eaSVladimir Oltean 	.port_bridge_flags		= felix_bridge_flags,
183756051948SVladimir Oltean 	.port_bridge_join		= felix_bridge_join,
183856051948SVladimir Oltean 	.port_bridge_leave		= felix_bridge_leave,
18398fe6832eSVladimir Oltean 	.port_lag_join			= felix_lag_join,
18408fe6832eSVladimir Oltean 	.port_lag_leave			= felix_lag_leave,
18418fe6832eSVladimir Oltean 	.port_lag_change		= felix_lag_change,
184256051948SVladimir Oltean 	.port_stp_state_set		= felix_bridge_stp_state_set,
184356051948SVladimir Oltean 	.port_vlan_filtering		= felix_vlan_filtering,
184456051948SVladimir Oltean 	.port_vlan_add			= felix_vlan_add,
184556051948SVladimir Oltean 	.port_vlan_del			= felix_vlan_del,
1846c0bcf537SYangbo Lu 	.port_hwtstamp_get		= felix_hwtstamp_get,
1847c0bcf537SYangbo Lu 	.port_hwtstamp_set		= felix_hwtstamp_set,
1848c0bcf537SYangbo Lu 	.port_rxtstamp			= felix_rxtstamp,
1849c0bcf537SYangbo Lu 	.port_txtstamp			= felix_txtstamp,
18500b912fc9SVladimir Oltean 	.port_change_mtu		= felix_change_mtu,
18510b912fc9SVladimir Oltean 	.port_max_mtu			= felix_get_max_mtu,
1852fc411eaaSVladimir Oltean 	.port_policer_add		= felix_port_policer_add,
1853fc411eaaSVladimir Oltean 	.port_policer_del		= felix_port_policer_del,
18545e497497SVladimir Oltean 	.port_mirror_add		= felix_port_mirror_add,
18555e497497SVladimir Oltean 	.port_mirror_del		= felix_port_mirror_del,
185607d985eeSVladimir Oltean 	.cls_flower_add			= felix_cls_flower_add,
185707d985eeSVladimir Oltean 	.cls_flower_del			= felix_cls_flower_del,
185807d985eeSVladimir Oltean 	.cls_flower_stats		= felix_cls_flower_stats,
1859de143c0eSXiaoliang Yang 	.port_setup_tc			= felix_port_setup_tc,
1860f59fd9caSVladimir Oltean 	.devlink_sb_pool_get		= felix_sb_pool_get,
1861f59fd9caSVladimir Oltean 	.devlink_sb_pool_set		= felix_sb_pool_set,
1862f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_get	= felix_sb_port_pool_get,
1863f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_set	= felix_sb_port_pool_set,
1864f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_get	= felix_sb_tc_pool_bind_get,
1865f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_set	= felix_sb_tc_pool_bind_set,
1866f59fd9caSVladimir Oltean 	.devlink_sb_occ_snapshot	= felix_sb_occ_snapshot,
1867f59fd9caSVladimir Oltean 	.devlink_sb_occ_max_clear	= felix_sb_occ_max_clear,
1868f59fd9caSVladimir Oltean 	.devlink_sb_occ_port_pool_get	= felix_sb_occ_port_pool_get,
1869f59fd9caSVladimir Oltean 	.devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
1870a026c50bSHoratiu Vultur 	.port_mrp_add			= felix_mrp_add,
1871a026c50bSHoratiu Vultur 	.port_mrp_del			= felix_mrp_del,
1872a026c50bSHoratiu Vultur 	.port_mrp_add_ring_role		= felix_mrp_add_ring_role,
1873a026c50bSHoratiu Vultur 	.port_mrp_del_ring_role		= felix_mrp_del_ring_role,
18745da11eb4SVladimir Oltean 	.tag_8021q_vlan_add		= felix_tag_8021q_vlan_add,
18755da11eb4SVladimir Oltean 	.tag_8021q_vlan_del		= felix_tag_8021q_vlan_del,
1876978777d0SVladimir Oltean 	.port_get_default_prio		= felix_port_get_default_prio,
1877978777d0SVladimir Oltean 	.port_set_default_prio		= felix_port_set_default_prio,
1878978777d0SVladimir Oltean 	.port_get_dscp_prio		= felix_port_get_dscp_prio,
1879978777d0SVladimir Oltean 	.port_add_dscp_prio		= felix_port_add_dscp_prio,
1880978777d0SVladimir Oltean 	.port_del_dscp_prio		= felix_port_del_dscp_prio,
188172c3b0c7SVladimir Oltean 	.port_set_host_flood		= felix_port_set_host_flood,
188256051948SVladimir Oltean };
1883319e4dd1SVladimir Oltean 
1884319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
1885319e4dd1SVladimir Oltean {
1886319e4dd1SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1887319e4dd1SVladimir Oltean 	struct dsa_switch *ds = felix->ds;
1888319e4dd1SVladimir Oltean 
1889319e4dd1SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
1890319e4dd1SVladimir Oltean 		return NULL;
1891319e4dd1SVladimir Oltean 
1892319e4dd1SVladimir Oltean 	return dsa_to_port(ds, port)->slave;
1893319e4dd1SVladimir Oltean }
1894319e4dd1SVladimir Oltean 
1895319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev)
1896319e4dd1SVladimir Oltean {
1897319e4dd1SVladimir Oltean 	struct dsa_port *dp;
1898319e4dd1SVladimir Oltean 
1899319e4dd1SVladimir Oltean 	dp = dsa_port_from_netdev(dev);
1900319e4dd1SVladimir Oltean 	if (IS_ERR(dp))
1901319e4dd1SVladimir Oltean 		return -EINVAL;
1902319e4dd1SVladimir Oltean 
1903319e4dd1SVladimir Oltean 	return dp->index;
1904319e4dd1SVladimir Oltean }
1905