xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision 129b7532a0edc3f381a6c5f3bb1a155e743d141b)
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 
243e21268efSVladimir Oltean /* Alternatively to using the NPI functionality, that same hardware MAC
244e21268efSVladimir Oltean  * connected internally to the enetc or fman DSA master can be configured to
245e21268efSVladimir Oltean  * use the software-defined tag_8021q frame format. As far as the hardware is
246e21268efSVladimir Oltean  * concerned, it thinks it is a "dumb switch" - the queues of the CPU port
247e21268efSVladimir Oltean  * module are now disconnected from it, but can still be accessed through
248e21268efSVladimir Oltean  * register-based MMIO.
249e21268efSVladimir Oltean  */
250e21268efSVladimir Oltean static void felix_8021q_cpu_port_init(struct ocelot *ocelot, int port)
251e21268efSVladimir Oltean {
2528abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2538abe1970SVladimir Oltean 
25454c31984SVladimir Oltean 	ocelot_port_set_dsa_8021q_cpu(ocelot, port);
255e21268efSVladimir Oltean 
256e21268efSVladimir Oltean 	/* Overwrite PGID_CPU with the non-tagging port */
257e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, PGID_CPU);
258e21268efSVladimir Oltean 
2598abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
2608abe1970SVladimir Oltean 
2618abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
262e21268efSVladimir Oltean }
263e21268efSVladimir Oltean 
264e21268efSVladimir Oltean static void felix_8021q_cpu_port_deinit(struct ocelot *ocelot, int port)
265e21268efSVladimir Oltean {
2668abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2678abe1970SVladimir Oltean 
26854c31984SVladimir Oltean 	ocelot_port_unset_dsa_8021q_cpu(ocelot, port);
269e21268efSVladimir Oltean 
270e21268efSVladimir Oltean 	/* Restore PGID_CPU */
271e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(ocelot->num_phys_ports), ANA_PGID_PGID,
272e21268efSVladimir Oltean 			 PGID_CPU);
273e21268efSVladimir Oltean 
2748abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
2758abe1970SVladimir Oltean 
2768abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
277e21268efSVladimir Oltean }
278e21268efSVladimir Oltean 
279c352e5e8SVladimir Oltean static int felix_trap_get_cpu_port(struct dsa_switch *ds,
280c352e5e8SVladimir Oltean 				   const struct ocelot_vcap_filter *trap)
281c352e5e8SVladimir Oltean {
282c352e5e8SVladimir Oltean 	struct dsa_port *dp;
283c352e5e8SVladimir Oltean 	int first_port;
284c352e5e8SVladimir Oltean 
285c352e5e8SVladimir Oltean 	if (WARN_ON(!trap->ingress_port_mask))
286c352e5e8SVladimir Oltean 		return -1;
287c352e5e8SVladimir Oltean 
288c352e5e8SVladimir Oltean 	first_port = __ffs(trap->ingress_port_mask);
289c352e5e8SVladimir Oltean 	dp = dsa_to_port(ds, first_port);
290c352e5e8SVladimir Oltean 
291c352e5e8SVladimir Oltean 	return dp->cpu_dp->index;
292c352e5e8SVladimir Oltean }
293c352e5e8SVladimir Oltean 
29499348004SVladimir Oltean /* On switches with no extraction IRQ wired, trapped packets need to be
29599348004SVladimir Oltean  * replicated over Ethernet as well, otherwise we'd get no notification of
29699348004SVladimir Oltean  * their arrival when using the ocelot-8021q tagging protocol.
297c8c0ba4fSVladimir Oltean  */
29899348004SVladimir Oltean static int felix_update_trapping_destinations(struct dsa_switch *ds,
29999348004SVladimir Oltean 					      bool using_tag_8021q)
300c8c0ba4fSVladimir Oltean {
30199348004SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
30299348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
303e1846cffSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
30499348004SVladimir Oltean 	struct ocelot_vcap_filter *trap;
30599348004SVladimir Oltean 	enum ocelot_mask_mode mask_mode;
30699348004SVladimir Oltean 	unsigned long port_mask;
30799348004SVladimir Oltean 	bool cpu_copy_ena;
308c352e5e8SVladimir Oltean 	int err;
309c8c0ba4fSVladimir Oltean 
31099348004SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
31199348004SVladimir Oltean 		return 0;
312c8c0ba4fSVladimir Oltean 
313d78637a8SVladimir Oltean 	/* We are sure that "cpu" was found, otherwise
314d78637a8SVladimir Oltean 	 * dsa_tree_setup_default_cpu() would have failed earlier.
315d78637a8SVladimir Oltean 	 */
316e1846cffSVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
317c8c0ba4fSVladimir Oltean 
31899348004SVladimir Oltean 	/* Make sure all traps are set up for that destination */
319e1846cffSVladimir Oltean 	list_for_each_entry(trap, &block_vcap_is2->rules, list) {
320e1846cffSVladimir Oltean 		if (!trap->is_trap)
321e1846cffSVladimir Oltean 			continue;
322e1846cffSVladimir Oltean 
32399348004SVladimir Oltean 		/* Figure out the current trapping destination */
32499348004SVladimir Oltean 		if (using_tag_8021q) {
32599348004SVladimir Oltean 			/* Redirect to the tag_8021q CPU port. If timestamps
32699348004SVladimir Oltean 			 * are necessary, also copy trapped packets to the CPU
32799348004SVladimir Oltean 			 * port module.
328c8c0ba4fSVladimir Oltean 			 */
32999348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_REDIRECT;
330c352e5e8SVladimir Oltean 			port_mask = BIT(felix_trap_get_cpu_port(ds, trap));
33199348004SVladimir Oltean 			cpu_copy_ena = !!trap->take_ts;
332c8c0ba4fSVladimir Oltean 		} else {
33399348004SVladimir Oltean 			/* Trap packets only to the CPU port module, which is
33499348004SVladimir Oltean 			 * redirected to the NPI port (the DSA CPU port)
335c8c0ba4fSVladimir Oltean 			 */
33699348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
33799348004SVladimir Oltean 			port_mask = 0;
33899348004SVladimir Oltean 			cpu_copy_ena = true;
339c8c0ba4fSVladimir Oltean 		}
340c8c0ba4fSVladimir Oltean 
34199348004SVladimir Oltean 		if (trap->action.mask_mode == mask_mode &&
34299348004SVladimir Oltean 		    trap->action.port_mask == port_mask &&
34399348004SVladimir Oltean 		    trap->action.cpu_copy_ena == cpu_copy_ena)
34499348004SVladimir Oltean 			continue;
345c8c0ba4fSVladimir Oltean 
34699348004SVladimir Oltean 		trap->action.mask_mode = mask_mode;
34799348004SVladimir Oltean 		trap->action.port_mask = port_mask;
34899348004SVladimir Oltean 		trap->action.cpu_copy_ena = cpu_copy_ena;
3490a6f17c6SVladimir Oltean 
35099348004SVladimir Oltean 		err = ocelot_vcap_filter_replace(ocelot, trap);
351c8c0ba4fSVladimir Oltean 		if (err)
352c8c0ba4fSVladimir Oltean 			return err;
35399348004SVladimir Oltean 	}
354c8c0ba4fSVladimir Oltean 
35599348004SVladimir Oltean 	return 0;
356c8c0ba4fSVladimir Oltean }
357c8c0ba4fSVladimir Oltean 
358adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This
359adb3dccfSVladimir Oltean  * is the mode through which frames can be injected from and extracted to an
360adb3dccfSVladimir Oltean  * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU
361adb3dccfSVladimir Oltean  * running Linux, and this forms a DSA setup together with the enetc or fman
362adb3dccfSVladimir Oltean  * DSA master.
363adb3dccfSVladimir Oltean  */
364adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port)
365adb3dccfSVladimir Oltean {
366adb3dccfSVladimir Oltean 	ocelot->npi = port;
367adb3dccfSVladimir Oltean 
368adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
369adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
370adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
371adb3dccfSVladimir Oltean 
372adb3dccfSVladimir Oltean 	/* NPI port Injection/Extraction configuration */
373adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
374adb3dccfSVladimir Oltean 			    ocelot->npi_xtr_prefix);
375adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
376adb3dccfSVladimir Oltean 			    ocelot->npi_inj_prefix);
377adb3dccfSVladimir Oltean 
378adb3dccfSVladimir Oltean 	/* Disable transmission of pause frames */
379adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
380adb3dccfSVladimir Oltean }
381adb3dccfSVladimir Oltean 
382adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port)
383adb3dccfSVladimir Oltean {
384adb3dccfSVladimir Oltean 	/* Restore hardware defaults */
385adb3dccfSVladimir Oltean 	int unused_port = ocelot->num_phys_ports + 2;
386adb3dccfSVladimir Oltean 
387adb3dccfSVladimir Oltean 	ocelot->npi = -1;
388adb3dccfSVladimir Oltean 
389adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port),
390adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
391adb3dccfSVladimir Oltean 
392adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
393adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
394adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
395adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
396adb3dccfSVladimir Oltean 
397adb3dccfSVladimir Oltean 	/* Enable transmission of pause frames */
398adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
399adb3dccfSVladimir Oltean }
400adb3dccfSVladimir Oltean 
4017a29d220SVladimir Oltean static int felix_tag_npi_setup(struct dsa_switch *ds)
402adb3dccfSVladimir Oltean {
4037a29d220SVladimir Oltean 	struct dsa_port *dp, *first_cpu_dp = NULL;
404adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
405f9cef64fSVladimir Oltean 
4067a29d220SVladimir Oltean 	dsa_switch_for_each_user_port(dp, ds) {
4077a29d220SVladimir Oltean 		if (first_cpu_dp && dp->cpu_dp != first_cpu_dp) {
4087a29d220SVladimir Oltean 			dev_err(ds->dev, "Multiple NPI ports not supported\n");
4097a29d220SVladimir Oltean 			return -EINVAL;
4107a29d220SVladimir Oltean 		}
411b903a6bdSVladimir Oltean 
4127a29d220SVladimir Oltean 		first_cpu_dp = dp->cpu_dp;
4137a29d220SVladimir Oltean 	}
414adb3dccfSVladimir Oltean 
4157a29d220SVladimir Oltean 	if (!first_cpu_dp)
4167a29d220SVladimir Oltean 		return -EINVAL;
4177a29d220SVladimir Oltean 
4187a29d220SVladimir Oltean 	felix_npi_port_init(ocelot, first_cpu_dp->index);
419adb3dccfSVladimir Oltean 
420adb3dccfSVladimir Oltean 	return 0;
421adb3dccfSVladimir Oltean }
422adb3dccfSVladimir Oltean 
4237a29d220SVladimir Oltean static void felix_tag_npi_teardown(struct dsa_switch *ds)
424adb3dccfSVladimir Oltean {
425adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
426adb3dccfSVladimir Oltean 
4277a29d220SVladimir Oltean 	felix_npi_port_deinit(ocelot, ocelot->npi);
428adb3dccfSVladimir Oltean }
429adb3dccfSVladimir Oltean 
4307a29d220SVladimir Oltean static unsigned long felix_tag_npi_get_host_fwd_mask(struct dsa_switch *ds)
431adb3dccfSVladimir Oltean {
4327a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
4337a29d220SVladimir Oltean 
4347a29d220SVladimir Oltean 	return BIT(ocelot->num_phys_ports);
4357a29d220SVladimir Oltean }
4367a29d220SVladimir Oltean 
4377a29d220SVladimir Oltean static const struct felix_tag_proto_ops felix_tag_npi_proto_ops = {
4387a29d220SVladimir Oltean 	.setup			= felix_tag_npi_setup,
4397a29d220SVladimir Oltean 	.teardown		= felix_tag_npi_teardown,
4407a29d220SVladimir Oltean 	.get_host_fwd_mask	= felix_tag_npi_get_host_fwd_mask,
4417a29d220SVladimir Oltean };
4427a29d220SVladimir Oltean 
4437a29d220SVladimir Oltean static int felix_tag_8021q_setup(struct dsa_switch *ds)
4447a29d220SVladimir Oltean {
4457a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
4467a29d220SVladimir Oltean 	struct dsa_port *dp, *cpu_dp;
447adb3dccfSVladimir Oltean 	int err;
448adb3dccfSVladimir Oltean 
4497a29d220SVladimir Oltean 	err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD));
4507a29d220SVladimir Oltean 	if (err)
451adb3dccfSVladimir Oltean 		return err;
4527a29d220SVladimir Oltean 
4537a29d220SVladimir Oltean 	dsa_switch_for_each_cpu_port(cpu_dp, ds) {
4547a29d220SVladimir Oltean 		felix_8021q_cpu_port_init(ocelot, cpu_dp->index);
4557a29d220SVladimir Oltean 
4567a29d220SVladimir Oltean 		/* TODO we could support multiple CPU ports in tag_8021q mode */
4577a29d220SVladimir Oltean 		break;
458adb3dccfSVladimir Oltean 	}
459adb3dccfSVladimir Oltean 
4607a29d220SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
4617a29d220SVladimir Oltean 		/* This overwrites ocelot_init():
4627a29d220SVladimir Oltean 		 * Do not forward BPDU frames to the CPU port module,
4637a29d220SVladimir Oltean 		 * for 2 reasons:
4647a29d220SVladimir Oltean 		 * - When these packets are injected from the tag_8021q
4657a29d220SVladimir Oltean 		 *   CPU port, we want them to go out, not loop back
4667a29d220SVladimir Oltean 		 *   into the system.
4677a29d220SVladimir Oltean 		 * - STP traffic ingressing on a user port should go to
4687a29d220SVladimir Oltean 		 *   the tag_8021q CPU port, not to the hardware CPU
4697a29d220SVladimir Oltean 		 *   port module.
4707a29d220SVladimir Oltean 		 */
4717a29d220SVladimir Oltean 		ocelot_write_gix(ocelot,
4727a29d220SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0),
4737a29d220SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG, dp->index);
4747a29d220SVladimir Oltean 	}
4757a29d220SVladimir Oltean 
4767a29d220SVladimir Oltean 	/* The ownership of the CPU port module's queues might have just been
4777a29d220SVladimir Oltean 	 * transferred to the tag_8021q tagger from the NPI-based tagger.
4787a29d220SVladimir Oltean 	 * So there might still be all sorts of crap in the queues. On the
4797a29d220SVladimir Oltean 	 * other hand, the MMIO-based matching of PTP frames is very brittle,
4807a29d220SVladimir Oltean 	 * so we need to be careful that there are no extra frames to be
4817a29d220SVladimir Oltean 	 * dequeued over MMIO, since we would never know to discard them.
4827a29d220SVladimir Oltean 	 */
4837a29d220SVladimir Oltean 	ocelot_drain_cpu_queue(ocelot, 0);
4847a29d220SVladimir Oltean 
4857a29d220SVladimir Oltean 	return 0;
4867a29d220SVladimir Oltean }
4877a29d220SVladimir Oltean 
4887a29d220SVladimir Oltean static void felix_tag_8021q_teardown(struct dsa_switch *ds)
489adb3dccfSVladimir Oltean {
4907a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
4917a29d220SVladimir Oltean 	struct dsa_port *dp, *cpu_dp;
4927a29d220SVladimir Oltean 
4937a29d220SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
4947a29d220SVladimir Oltean 		/* Restore the logic from ocelot_init:
4957a29d220SVladimir Oltean 		 * do not forward BPDU frames to the front ports.
4967a29d220SVladimir Oltean 		 */
4977a29d220SVladimir Oltean 		ocelot_write_gix(ocelot,
4987a29d220SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
4997a29d220SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG,
5007a29d220SVladimir Oltean 				 dp->index);
5017a29d220SVladimir Oltean 	}
5027a29d220SVladimir Oltean 
5037a29d220SVladimir Oltean 	dsa_switch_for_each_cpu_port(cpu_dp, ds) {
5047a29d220SVladimir Oltean 		felix_8021q_cpu_port_deinit(ocelot, cpu_dp->index);
5057a29d220SVladimir Oltean 
5067a29d220SVladimir Oltean 		/* TODO we could support multiple CPU ports in tag_8021q mode */
507adb3dccfSVladimir Oltean 		break;
508adb3dccfSVladimir Oltean 	}
5097a29d220SVladimir Oltean 
5107a29d220SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
5117a29d220SVladimir Oltean }
5127a29d220SVladimir Oltean 
5137a29d220SVladimir Oltean static unsigned long felix_tag_8021q_get_host_fwd_mask(struct dsa_switch *ds)
5147a29d220SVladimir Oltean {
5157a29d220SVladimir Oltean 	return dsa_cpu_ports(ds);
5167a29d220SVladimir Oltean }
5177a29d220SVladimir Oltean 
5187a29d220SVladimir Oltean static const struct felix_tag_proto_ops felix_tag_8021q_proto_ops = {
5197a29d220SVladimir Oltean 	.setup			= felix_tag_8021q_setup,
5207a29d220SVladimir Oltean 	.teardown		= felix_tag_8021q_teardown,
5217a29d220SVladimir Oltean 	.get_host_fwd_mask	= felix_tag_8021q_get_host_fwd_mask,
5227a29d220SVladimir Oltean };
5237a29d220SVladimir Oltean 
5247a29d220SVladimir Oltean static void felix_set_host_flood(struct dsa_switch *ds, unsigned long mask,
5257a29d220SVladimir Oltean 				 bool uc, bool mc, bool bc)
5267a29d220SVladimir Oltean {
5277a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5287a29d220SVladimir Oltean 	unsigned long val;
5297a29d220SVladimir Oltean 
5307a29d220SVladimir Oltean 	val = uc ? mask : 0;
5317a29d220SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_UC);
5327a29d220SVladimir Oltean 
5337a29d220SVladimir Oltean 	val = mc ? mask : 0;
5347a29d220SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MC);
5357a29d220SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MCIPV4);
5367a29d220SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MCIPV6);
537*129b7532SVladimir Oltean 
538*129b7532SVladimir Oltean 	val = bc ? mask : 0;
539*129b7532SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_BC);
5407a29d220SVladimir Oltean }
5417a29d220SVladimir Oltean 
5427a29d220SVladimir Oltean static void
5437a29d220SVladimir Oltean felix_migrate_host_flood(struct dsa_switch *ds,
5447a29d220SVladimir Oltean 			 const struct felix_tag_proto_ops *proto_ops,
5457a29d220SVladimir Oltean 			 const struct felix_tag_proto_ops *old_proto_ops)
5467a29d220SVladimir Oltean {
5477a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5487a29d220SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
5497a29d220SVladimir Oltean 	unsigned long mask;
5507a29d220SVladimir Oltean 
5517a29d220SVladimir Oltean 	if (old_proto_ops) {
5527a29d220SVladimir Oltean 		mask = old_proto_ops->get_host_fwd_mask(ds);
5537a29d220SVladimir Oltean 		felix_set_host_flood(ds, mask, false, false, false);
5547a29d220SVladimir Oltean 	}
5557a29d220SVladimir Oltean 
5567a29d220SVladimir Oltean 	mask = proto_ops->get_host_fwd_mask(ds);
5577a29d220SVladimir Oltean 	felix_set_host_flood(ds, mask, !!felix->host_flood_uc_mask,
5587a29d220SVladimir Oltean 			     !!felix->host_flood_mc_mask, true);
5597a29d220SVladimir Oltean }
5607a29d220SVladimir Oltean 
5617a29d220SVladimir Oltean static int felix_migrate_mdbs(struct dsa_switch *ds,
5627a29d220SVladimir Oltean 			      const struct felix_tag_proto_ops *proto_ops,
5637a29d220SVladimir Oltean 			      const struct felix_tag_proto_ops *old_proto_ops)
5647a29d220SVladimir Oltean {
5657a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5667a29d220SVladimir Oltean 	unsigned long from, to;
5677a29d220SVladimir Oltean 
5687a29d220SVladimir Oltean 	if (!old_proto_ops)
5697a29d220SVladimir Oltean 		return 0;
5707a29d220SVladimir Oltean 
5717a29d220SVladimir Oltean 	from = old_proto_ops->get_host_fwd_mask(ds);
5727a29d220SVladimir Oltean 	to = proto_ops->get_host_fwd_mask(ds);
5737a29d220SVladimir Oltean 
5747a29d220SVladimir Oltean 	return ocelot_migrate_mdbs(ocelot, from, to);
5757a29d220SVladimir Oltean }
5767a29d220SVladimir Oltean 
5777a29d220SVladimir Oltean /* Configure the shared hardware resources for a transition between
5787a29d220SVladimir Oltean  * @old_proto_ops and @proto_ops.
5797a29d220SVladimir Oltean  * Manual migration is needed because as far as DSA is concerned, no change of
5807a29d220SVladimir Oltean  * the CPU port is taking place here, just of the tagging protocol.
5817a29d220SVladimir Oltean  */
5827a29d220SVladimir Oltean static int
5837a29d220SVladimir Oltean felix_tag_proto_setup_shared(struct dsa_switch *ds,
5847a29d220SVladimir Oltean 			     const struct felix_tag_proto_ops *proto_ops,
5857a29d220SVladimir Oltean 			     const struct felix_tag_proto_ops *old_proto_ops)
5867a29d220SVladimir Oltean {
5877a29d220SVladimir Oltean 	bool using_tag_8021q = (proto_ops == &felix_tag_8021q_proto_ops);
5887a29d220SVladimir Oltean 	int err;
5897a29d220SVladimir Oltean 
5907a29d220SVladimir Oltean 	err = felix_migrate_mdbs(ds, proto_ops, old_proto_ops);
5917a29d220SVladimir Oltean 	if (err)
5927a29d220SVladimir Oltean 		return err;
5937a29d220SVladimir Oltean 
5947a29d220SVladimir Oltean 	felix_update_trapping_destinations(ds, using_tag_8021q);
5957a29d220SVladimir Oltean 
5967a29d220SVladimir Oltean 	felix_migrate_host_flood(ds, proto_ops, old_proto_ops);
5977a29d220SVladimir Oltean 
5987a29d220SVladimir Oltean 	return 0;
599adb3dccfSVladimir Oltean }
600adb3dccfSVladimir Oltean 
601e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the
602e21268efSVladimir Oltean  * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
603e21268efSVladimir Oltean  * or the restoration is guaranteed to work.
604e21268efSVladimir Oltean  */
605bacf93b0SVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds,
606adb3dccfSVladimir Oltean 				     enum dsa_tag_protocol proto)
607adb3dccfSVladimir Oltean {
6087a29d220SVladimir Oltean 	const struct felix_tag_proto_ops *old_proto_ops, *proto_ops;
609adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
610adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
611adb3dccfSVladimir Oltean 	int err;
612adb3dccfSVladimir Oltean 
6137a29d220SVladimir Oltean 	switch (proto) {
6147a29d220SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
6157a29d220SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
6167a29d220SVladimir Oltean 		proto_ops = &felix_tag_npi_proto_ops;
617bacf93b0SVladimir Oltean 		break;
6187a29d220SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
6197a29d220SVladimir Oltean 		proto_ops = &felix_tag_8021q_proto_ops;
6207a29d220SVladimir Oltean 		break;
6217a29d220SVladimir Oltean 	default:
6227a29d220SVladimir Oltean 		return -EPROTONOSUPPORT;
623bacf93b0SVladimir Oltean 	}
624bacf93b0SVladimir Oltean 
6257a29d220SVladimir Oltean 	old_proto_ops = felix->tag_proto_ops;
6267a29d220SVladimir Oltean 
6277a29d220SVladimir Oltean 	err = proto_ops->setup(ds);
6287a29d220SVladimir Oltean 	if (err)
6297a29d220SVladimir Oltean 		goto setup_failed;
6307a29d220SVladimir Oltean 
6317a29d220SVladimir Oltean 	err = felix_tag_proto_setup_shared(ds, proto_ops, old_proto_ops);
6327a29d220SVladimir Oltean 	if (err)
6337a29d220SVladimir Oltean 		goto setup_shared_failed;
6347a29d220SVladimir Oltean 
6357a29d220SVladimir Oltean 	if (old_proto_ops)
6367a29d220SVladimir Oltean 		old_proto_ops->teardown(ds);
6377a29d220SVladimir Oltean 
6387a29d220SVladimir Oltean 	felix->tag_proto_ops = proto_ops;
639adb3dccfSVladimir Oltean 	felix->tag_proto = proto;
640adb3dccfSVladimir Oltean 
641adb3dccfSVladimir Oltean 	return 0;
6427a29d220SVladimir Oltean 
6437a29d220SVladimir Oltean setup_shared_failed:
6447a29d220SVladimir Oltean 	proto_ops->teardown(ds);
6457a29d220SVladimir Oltean setup_failed:
6467a29d220SVladimir Oltean 	return err;
647adb3dccfSVladimir Oltean }
648adb3dccfSVladimir Oltean 
64956051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
6504d776482SFlorian Fainelli 						    int port,
6514d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
65256051948SVladimir Oltean {
653adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
654adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
655adb3dccfSVladimir Oltean 
656adb3dccfSVladimir Oltean 	return felix->tag_proto;
65756051948SVladimir Oltean }
65856051948SVladimir Oltean 
65972c3b0c7SVladimir Oltean static void felix_port_set_host_flood(struct dsa_switch *ds, int port,
66072c3b0c7SVladimir Oltean 				      bool uc, bool mc)
66172c3b0c7SVladimir Oltean {
66272c3b0c7SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
66372c3b0c7SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
6647a29d220SVladimir Oltean 	unsigned long mask;
66572c3b0c7SVladimir Oltean 
66672c3b0c7SVladimir Oltean 	if (uc)
66772c3b0c7SVladimir Oltean 		felix->host_flood_uc_mask |= BIT(port);
66872c3b0c7SVladimir Oltean 	else
66972c3b0c7SVladimir Oltean 		felix->host_flood_uc_mask &= ~BIT(port);
67072c3b0c7SVladimir Oltean 
67172c3b0c7SVladimir Oltean 	if (mc)
67272c3b0c7SVladimir Oltean 		felix->host_flood_mc_mask |= BIT(port);
67372c3b0c7SVladimir Oltean 	else
67472c3b0c7SVladimir Oltean 		felix->host_flood_mc_mask &= ~BIT(port);
67572c3b0c7SVladimir Oltean 
6767a29d220SVladimir Oltean 	mask = felix->tag_proto_ops->get_host_fwd_mask(ds);
6777a29d220SVladimir Oltean 	felix_set_host_flood(ds, mask, !!felix->host_flood_uc_mask,
6787a29d220SVladimir Oltean 			     !!felix->host_flood_mc_mask, true);
67972c3b0c7SVladimir Oltean }
68072c3b0c7SVladimir Oltean 
68156051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
68256051948SVladimir Oltean 				 unsigned int ageing_time)
68356051948SVladimir Oltean {
68456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
68556051948SVladimir Oltean 
68656051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
68756051948SVladimir Oltean 
68856051948SVladimir Oltean 	return 0;
68956051948SVladimir Oltean }
69056051948SVladimir Oltean 
6915cad43a5SVladimir Oltean static void felix_port_fast_age(struct dsa_switch *ds, int port)
6925cad43a5SVladimir Oltean {
6935cad43a5SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
6945cad43a5SVladimir Oltean 	int err;
6955cad43a5SVladimir Oltean 
6965cad43a5SVladimir Oltean 	err = ocelot_mact_flush(ocelot, port);
6975cad43a5SVladimir Oltean 	if (err)
6985cad43a5SVladimir Oltean 		dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n",
6995cad43a5SVladimir Oltean 			port, ERR_PTR(err));
7005cad43a5SVladimir Oltean }
7015cad43a5SVladimir Oltean 
70256051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
70356051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
70456051948SVladimir Oltean {
70556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
70656051948SVladimir Oltean 
70756051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
70856051948SVladimir Oltean }
70956051948SVladimir Oltean 
71056051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
711c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
712c2693363SVladimir Oltean 			 struct dsa_db db)
71356051948SVladimir Oltean {
71454c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
715e9b3ba43SVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
71656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
71756051948SVladimir Oltean 
71854c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
71954c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
72054c31984SVladimir Oltean 
721e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp) && !bridge_dev &&
7227e580490SVladimir Oltean 	    dsa_fdb_present_in_other_db(ds, port, addr, vid, db))
7237e580490SVladimir Oltean 		return 0;
7247e580490SVladimir Oltean 
725e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp))
726e9b3ba43SVladimir Oltean 		port = PGID_CPU;
727e9b3ba43SVladimir Oltean 
72854c31984SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid, bridge_dev);
72956051948SVladimir Oltean }
73056051948SVladimir Oltean 
73156051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
732c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
733c2693363SVladimir Oltean 			 struct dsa_db db)
73456051948SVladimir Oltean {
73554c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
736e9b3ba43SVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
73756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
73856051948SVladimir Oltean 
73954c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
74054c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
74154c31984SVladimir Oltean 
742e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp) && !bridge_dev &&
7437e580490SVladimir Oltean 	    dsa_fdb_present_in_other_db(ds, port, addr, vid, db))
7447e580490SVladimir Oltean 		return 0;
7457e580490SVladimir Oltean 
746e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp))
747e9b3ba43SVladimir Oltean 		port = PGID_CPU;
748e9b3ba43SVladimir Oltean 
74954c31984SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid, bridge_dev);
75056051948SVladimir Oltean }
75156051948SVladimir Oltean 
752961d8b69SVladimir Oltean static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag,
753c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
754c2693363SVladimir Oltean 			     struct dsa_db db)
755961d8b69SVladimir Oltean {
75654c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
757961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
758961d8b69SVladimir Oltean 
75954c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
76054c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
76154c31984SVladimir Oltean 
76254c31984SVladimir Oltean 	return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid, bridge_dev);
763961d8b69SVladimir Oltean }
764961d8b69SVladimir Oltean 
765961d8b69SVladimir Oltean static int felix_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag lag,
766c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
767c2693363SVladimir Oltean 			     struct dsa_db db)
768961d8b69SVladimir Oltean {
76954c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
770961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
771961d8b69SVladimir Oltean 
77254c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
77354c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
77454c31984SVladimir Oltean 
77554c31984SVladimir Oltean 	return ocelot_lag_fdb_del(ocelot, lag.dev, addr, vid, bridge_dev);
776961d8b69SVladimir Oltean }
777961d8b69SVladimir Oltean 
778a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port,
779c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
780c2693363SVladimir Oltean 			 struct dsa_db db)
781209edf95SVladimir Oltean {
78254c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
783209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
784209edf95SVladimir Oltean 
78554c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
78654c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
78754c31984SVladimir Oltean 
7887e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
7897e580490SVladimir Oltean 	    dsa_mdb_present_in_other_db(ds, port, mdb, db))
7907e580490SVladimir Oltean 		return 0;
7917e580490SVladimir Oltean 
7920ddf83cdSVladimir Oltean 	if (port == ocelot->npi)
7930ddf83cdSVladimir Oltean 		port = ocelot->num_phys_ports;
7940ddf83cdSVladimir Oltean 
79554c31984SVladimir Oltean 	return ocelot_port_mdb_add(ocelot, port, mdb, bridge_dev);
796209edf95SVladimir Oltean }
797209edf95SVladimir Oltean 
798209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port,
799c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
800c2693363SVladimir Oltean 			 struct dsa_db db)
801209edf95SVladimir Oltean {
80254c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
803209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
804209edf95SVladimir Oltean 
80554c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
80654c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
80754c31984SVladimir Oltean 
8087e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
8097e580490SVladimir Oltean 	    dsa_mdb_present_in_other_db(ds, port, mdb, db))
8107e580490SVladimir Oltean 		return 0;
8117e580490SVladimir Oltean 
8120ddf83cdSVladimir Oltean 	if (port == ocelot->npi)
8130ddf83cdSVladimir Oltean 		port = ocelot->num_phys_ports;
8140ddf83cdSVladimir Oltean 
81554c31984SVladimir Oltean 	return ocelot_port_mdb_del(ocelot, port, mdb, bridge_dev);
816209edf95SVladimir Oltean }
817209edf95SVladimir Oltean 
81856051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
81956051948SVladimir Oltean 				       u8 state)
82056051948SVladimir Oltean {
82156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
82256051948SVladimir Oltean 
82356051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
82456051948SVladimir Oltean }
82556051948SVladimir Oltean 
826421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port,
827421741eaSVladimir Oltean 				  struct switchdev_brport_flags val,
828421741eaSVladimir Oltean 				  struct netlink_ext_ack *extack)
829421741eaSVladimir Oltean {
830421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
831421741eaSVladimir Oltean 
832421741eaSVladimir Oltean 	return ocelot_port_pre_bridge_flags(ocelot, port, val);
833421741eaSVladimir Oltean }
834421741eaSVladimir Oltean 
835421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port,
836421741eaSVladimir Oltean 			      struct switchdev_brport_flags val,
837421741eaSVladimir Oltean 			      struct netlink_ext_ack *extack)
838421741eaSVladimir Oltean {
839421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
840421741eaSVladimir Oltean 
841910ee6ccSVladimir Oltean 	if (port == ocelot->npi)
842910ee6ccSVladimir Oltean 		port = ocelot->num_phys_ports;
843910ee6ccSVladimir Oltean 
844421741eaSVladimir Oltean 	ocelot_port_bridge_flags(ocelot, port, val);
845421741eaSVladimir Oltean 
846421741eaSVladimir Oltean 	return 0;
847421741eaSVladimir Oltean }
848421741eaSVladimir Oltean 
84956051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
85006b9cce4SVladimir Oltean 			     struct dsa_bridge bridge, bool *tx_fwd_offload,
85106b9cce4SVladimir Oltean 			     struct netlink_ext_ack *extack)
85256051948SVladimir Oltean {
85356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
85456051948SVladimir Oltean 
85554c31984SVladimir Oltean 	return ocelot_port_bridge_join(ocelot, port, bridge.dev, bridge.num,
85654c31984SVladimir Oltean 				       extack);
85756051948SVladimir Oltean }
85856051948SVladimir Oltean 
85956051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
860d3eed0e5SVladimir Oltean 			       struct dsa_bridge bridge)
86156051948SVladimir Oltean {
86256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
86356051948SVladimir Oltean 
864d3eed0e5SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, bridge.dev);
86556051948SVladimir Oltean }
86656051948SVladimir Oltean 
8678fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port,
868dedd6a00SVladimir Oltean 			  struct dsa_lag lag,
8698fe6832eSVladimir Oltean 			  struct netdev_lag_upper_info *info)
8708fe6832eSVladimir Oltean {
8718fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8728fe6832eSVladimir Oltean 
873dedd6a00SVladimir Oltean 	return ocelot_port_lag_join(ocelot, port, lag.dev, info);
8748fe6832eSVladimir Oltean }
8758fe6832eSVladimir Oltean 
8768fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port,
877dedd6a00SVladimir Oltean 			   struct dsa_lag lag)
8788fe6832eSVladimir Oltean {
8798fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8808fe6832eSVladimir Oltean 
881dedd6a00SVladimir Oltean 	ocelot_port_lag_leave(ocelot, port, lag.dev);
8828fe6832eSVladimir Oltean 
8838fe6832eSVladimir Oltean 	return 0;
8848fe6832eSVladimir Oltean }
8858fe6832eSVladimir Oltean 
8868fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port)
8878fe6832eSVladimir Oltean {
8888fe6832eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
8898fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8908fe6832eSVladimir Oltean 
8918fe6832eSVladimir Oltean 	ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
8928fe6832eSVladimir Oltean 
8938fe6832eSVladimir Oltean 	return 0;
8948fe6832eSVladimir Oltean }
8958fe6832eSVladimir Oltean 
89656051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
89701af940eSVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan,
89801af940eSVladimir Oltean 			      struct netlink_ext_ack *extack)
89956051948SVladimir Oltean {
9002f0402feSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
901b7a9e0daSVladimir Oltean 	u16 flags = vlan->flags;
9022f0402feSVladimir Oltean 
9039a720680SVladimir Oltean 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
9049a720680SVladimir Oltean 	 * egress-untagged or not, pvid or not, make no difference. This
9059a720680SVladimir Oltean 	 * behavior is already better than what DSA just tries to approximate
9069a720680SVladimir Oltean 	 * when it installs the VLAN with the same flags on the CPU port.
9079a720680SVladimir Oltean 	 * Just accept any configuration, and don't let ocelot deny installing
9089a720680SVladimir Oltean 	 * multiple native VLANs on the NPI port, because the switch doesn't
9099a720680SVladimir Oltean 	 * look at the port tag settings towards the NPI interface anyway.
9109a720680SVladimir Oltean 	 */
9119a720680SVladimir Oltean 	if (port == ocelot->npi)
9129a720680SVladimir Oltean 		return 0;
9139a720680SVladimir Oltean 
914b7a9e0daSVladimir Oltean 	return ocelot_vlan_prepare(ocelot, port, vlan->vid,
9152f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_PVID,
91601af940eSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_UNTAGGED,
91701af940eSVladimir Oltean 				   extack);
91856051948SVladimir Oltean }
91956051948SVladimir Oltean 
92089153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
92189153ed6SVladimir Oltean 				struct netlink_ext_ack *extack)
92256051948SVladimir Oltean {
92356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
92456051948SVladimir Oltean 
9253b95d1b2SVladimir Oltean 	return ocelot_port_vlan_filtering(ocelot, port, enabled, extack);
92656051948SVladimir Oltean }
92756051948SVladimir Oltean 
9281958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port,
92931046a5fSVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan,
93031046a5fSVladimir Oltean 			  struct netlink_ext_ack *extack)
93156051948SVladimir Oltean {
93256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
933183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
9341958d581SVladimir Oltean 	int err;
93556051948SVladimir Oltean 
93601af940eSVladimir Oltean 	err = felix_vlan_prepare(ds, port, vlan, extack);
9371958d581SVladimir Oltean 	if (err)
9381958d581SVladimir Oltean 		return err;
9391958d581SVladimir Oltean 
9401958d581SVladimir Oltean 	return ocelot_vlan_add(ocelot, port, vlan->vid,
941183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_PVID,
942183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_UNTAGGED);
94356051948SVladimir Oltean }
94456051948SVladimir Oltean 
94556051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
94656051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
94756051948SVladimir Oltean {
94856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
94956051948SVladimir Oltean 
950b7a9e0daSVladimir Oltean 	return ocelot_vlan_del(ocelot, port, vlan->vid);
95156051948SVladimir Oltean }
95256051948SVladimir Oltean 
95379fda660SRussell King (Oracle) static void felix_phylink_get_caps(struct dsa_switch *ds, int port,
95479fda660SRussell King (Oracle) 				   struct phylink_config *config)
95579fda660SRussell King (Oracle) {
95679fda660SRussell King (Oracle) 	struct ocelot *ocelot = ds->priv;
95779fda660SRussell King (Oracle) 
958f6f04c02SRussell King (Oracle) 	/* This driver does not make use of the speed, duplex, pause or the
959f6f04c02SRussell King (Oracle) 	 * advertisement in its mac_config, so it is safe to mark this driver
960f6f04c02SRussell King (Oracle) 	 * as non-legacy.
961f6f04c02SRussell King (Oracle) 	 */
962f6f04c02SRussell King (Oracle) 	config->legacy_pre_march2020 = false;
963f6f04c02SRussell King (Oracle) 
96479fda660SRussell King (Oracle) 	__set_bit(ocelot->ports[port]->phy_mode,
96579fda660SRussell King (Oracle) 		  config->supported_interfaces);
96679fda660SRussell King (Oracle) }
96779fda660SRussell King (Oracle) 
968bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
969bdeced75SVladimir Oltean 				   unsigned long *supported,
970bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
971bdeced75SVladimir Oltean {
972bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
973375e1314SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
974bdeced75SVladimir Oltean 
975375e1314SVladimir Oltean 	if (felix->info->phylink_validate)
976375e1314SVladimir Oltean 		felix->info->phylink_validate(ocelot, port, supported, state);
977bdeced75SVladimir Oltean }
978bdeced75SVladimir Oltean 
979864ba485SRussell King (Oracle) static struct phylink_pcs *felix_phylink_mac_select_pcs(struct dsa_switch *ds,
980864ba485SRussell King (Oracle) 							int port,
981864ba485SRussell King (Oracle) 							phy_interface_t iface)
982bdeced75SVladimir Oltean {
983bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
984bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
985864ba485SRussell King (Oracle) 	struct phylink_pcs *pcs = NULL;
986bdeced75SVladimir Oltean 
98749af6a76SColin Foster 	if (felix->pcs && felix->pcs[port])
988864ba485SRussell King (Oracle) 		pcs = felix->pcs[port];
989864ba485SRussell King (Oracle) 
990864ba485SRussell King (Oracle) 	return pcs;
991bdeced75SVladimir Oltean }
992bdeced75SVladimir Oltean 
993bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
994bdeced75SVladimir Oltean 					unsigned int link_an_mode,
995bdeced75SVladimir Oltean 					phy_interface_t interface)
996bdeced75SVladimir Oltean {
997bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
998bdeced75SVladimir Oltean 
999e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
1000e6e12df6SVladimir Oltean 				     FELIX_MAC_QUIRKS);
1001bdeced75SVladimir Oltean }
1002bdeced75SVladimir Oltean 
1003bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
1004bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
1005bdeced75SVladimir Oltean 				      phy_interface_t interface,
10065b502a7bSRussell King 				      struct phy_device *phydev,
10075b502a7bSRussell King 				      int speed, int duplex,
10085b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
1009bdeced75SVladimir Oltean {
1010bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
10117e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1012bdeced75SVladimir Oltean 
1013e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
1014e6e12df6SVladimir Oltean 				   interface, speed, duplex, tx_pause, rx_pause,
1015e6e12df6SVladimir Oltean 				   FELIX_MAC_QUIRKS);
10167e14a2dcSVladimir Oltean 
10177e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
10187e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
1019bdeced75SVladimir Oltean }
1020bdeced75SVladimir Oltean 
1021bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
1022bd2b3161SXiaoliang Yang {
1023bd2b3161SXiaoliang Yang 	int i;
1024bd2b3161SXiaoliang Yang 
1025bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
1026bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1027bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1028bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
1029bd2b3161SXiaoliang Yang 		       port);
1030bd2b3161SXiaoliang Yang 
103170d39a6eSVladimir Oltean 	for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
1032bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
1033bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
1034bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
1035bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
1036bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
1037bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
1038bd2b3161SXiaoliang Yang 			      port, i);
1039bd2b3161SXiaoliang Yang 	}
1040bd2b3161SXiaoliang Yang }
1041bd2b3161SXiaoliang Yang 
104256051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
104356051948SVladimir Oltean 			      u32 stringset, u8 *data)
104456051948SVladimir Oltean {
104556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
104656051948SVladimir Oltean 
104756051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
104856051948SVladimir Oltean }
104956051948SVladimir Oltean 
105056051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
105156051948SVladimir Oltean {
105256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
105356051948SVladimir Oltean 
105456051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
105556051948SVladimir Oltean }
105656051948SVladimir Oltean 
105756051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
105856051948SVladimir Oltean {
105956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
106056051948SVladimir Oltean 
106156051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
106256051948SVladimir Oltean }
106356051948SVladimir Oltean 
106456051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
106556051948SVladimir Oltean 			     struct ethtool_ts_info *info)
106656051948SVladimir Oltean {
106756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
106856051948SVladimir Oltean 
106956051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
107056051948SVladimir Oltean }
107156051948SVladimir Oltean 
1072acf242fcSColin Foster static const u32 felix_phy_match_table[PHY_INTERFACE_MODE_MAX] = {
1073acf242fcSColin Foster 	[PHY_INTERFACE_MODE_INTERNAL] = OCELOT_PORT_MODE_INTERNAL,
1074acf242fcSColin Foster 	[PHY_INTERFACE_MODE_SGMII] = OCELOT_PORT_MODE_SGMII,
1075acf242fcSColin Foster 	[PHY_INTERFACE_MODE_QSGMII] = OCELOT_PORT_MODE_QSGMII,
1076acf242fcSColin Foster 	[PHY_INTERFACE_MODE_USXGMII] = OCELOT_PORT_MODE_USXGMII,
107711ecf341SVladimir Oltean 	[PHY_INTERFACE_MODE_1000BASEX] = OCELOT_PORT_MODE_1000BASEX,
1078acf242fcSColin Foster 	[PHY_INTERFACE_MODE_2500BASEX] = OCELOT_PORT_MODE_2500BASEX,
1079acf242fcSColin Foster };
1080acf242fcSColin Foster 
1081acf242fcSColin Foster static int felix_validate_phy_mode(struct felix *felix, int port,
1082acf242fcSColin Foster 				   phy_interface_t phy_mode)
1083acf242fcSColin Foster {
1084acf242fcSColin Foster 	u32 modes = felix->info->port_modes[port];
1085acf242fcSColin Foster 
1086acf242fcSColin Foster 	if (felix_phy_match_table[phy_mode] & modes)
1087acf242fcSColin Foster 		return 0;
1088acf242fcSColin Foster 	return -EOPNOTSUPP;
1089acf242fcSColin Foster }
1090acf242fcSColin Foster 
1091bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
1092bdeced75SVladimir Oltean 				  struct device_node *ports_node,
1093bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
1094bdeced75SVladimir Oltean {
1095bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1096bdeced75SVladimir Oltean 	struct device_node *child;
1097bdeced75SVladimir Oltean 
109837fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
1099bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
1100bdeced75SVladimir Oltean 		u32 port;
1101bdeced75SVladimir Oltean 		int err;
1102bdeced75SVladimir Oltean 
1103bdeced75SVladimir Oltean 		/* Get switch port number from DT */
1104bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
1105bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
1106bdeced75SVladimir Oltean 				"(property \"reg\")\n");
1107bdeced75SVladimir Oltean 			of_node_put(child);
1108bdeced75SVladimir Oltean 			return -ENODEV;
1109bdeced75SVladimir Oltean 		}
1110bdeced75SVladimir Oltean 
1111bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
1112bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
1113bdeced75SVladimir Oltean 		if (err) {
1114bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
1115bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
1116bdeced75SVladimir Oltean 				port);
1117bdeced75SVladimir Oltean 			of_node_put(child);
1118bdeced75SVladimir Oltean 			return -ENODEV;
1119bdeced75SVladimir Oltean 		}
1120bdeced75SVladimir Oltean 
1121acf242fcSColin Foster 		err = felix_validate_phy_mode(felix, port, phy_mode);
1122bdeced75SVladimir Oltean 		if (err < 0) {
1123bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
1124bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
112559ebb430SSumera Priyadarsini 			of_node_put(child);
1126bdeced75SVladimir Oltean 			return err;
1127bdeced75SVladimir Oltean 		}
1128bdeced75SVladimir Oltean 
1129bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
1130bdeced75SVladimir Oltean 	}
1131bdeced75SVladimir Oltean 
1132bdeced75SVladimir Oltean 	return 0;
1133bdeced75SVladimir Oltean }
1134bdeced75SVladimir Oltean 
1135bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
1136bdeced75SVladimir Oltean {
1137bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1138bdeced75SVladimir Oltean 	struct device_node *switch_node;
1139bdeced75SVladimir Oltean 	struct device_node *ports_node;
1140bdeced75SVladimir Oltean 	int err;
1141bdeced75SVladimir Oltean 
1142bdeced75SVladimir Oltean 	switch_node = dev->of_node;
1143bdeced75SVladimir Oltean 
1144bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
1145abecbfcdSVladimir Oltean 	if (!ports_node)
1146abecbfcdSVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
1147bdeced75SVladimir Oltean 	if (!ports_node) {
1148abecbfcdSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n");
1149bdeced75SVladimir Oltean 		return -ENODEV;
1150bdeced75SVladimir Oltean 	}
1151bdeced75SVladimir Oltean 
1152bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
1153bdeced75SVladimir Oltean 	of_node_put(ports_node);
1154bdeced75SVladimir Oltean 
1155bdeced75SVladimir Oltean 	return err;
1156bdeced75SVladimir Oltean }
1157bdeced75SVladimir Oltean 
115856051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
115956051948SVladimir Oltean {
116056051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
1161bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
1162b4024c9eSClaudiu Manoil 	struct resource res;
116356051948SVladimir Oltean 	int port, i, err;
116456051948SVladimir Oltean 
116556051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
116656051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
116756051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
116856051948SVladimir Oltean 	if (!ocelot->ports)
116956051948SVladimir Oltean 		return -ENOMEM;
117056051948SVladimir Oltean 
117156051948SVladimir Oltean 	ocelot->map		= felix->info->map;
117256051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
117321ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
117407d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
117577043c37SXiaoliang Yang 	ocelot->vcap_pol.base	= felix->info->vcap_pol_base;
117677043c37SXiaoliang Yang 	ocelot->vcap_pol.max	= felix->info->vcap_pol_max;
117777043c37SXiaoliang Yang 	ocelot->vcap_pol.base2	= felix->info->vcap_pol_base2;
117877043c37SXiaoliang Yang 	ocelot->vcap_pol.max2	= felix->info->vcap_pol_max2;
117956051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
1180cacea62fSVladimir Oltean 	ocelot->npi_inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
1181cacea62fSVladimir Oltean 	ocelot->npi_xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
1182f59fd9caSVladimir Oltean 	ocelot->devlink		= felix->ds->devlink;
118356051948SVladimir Oltean 
1184bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
1185bdeced75SVladimir Oltean 				 GFP_KERNEL);
1186bdeced75SVladimir Oltean 	if (!port_phy_modes)
1187bdeced75SVladimir Oltean 		return -ENOMEM;
1188bdeced75SVladimir Oltean 
1189bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
1190bdeced75SVladimir Oltean 	if (err) {
1191bdeced75SVladimir Oltean 		kfree(port_phy_modes);
1192bdeced75SVladimir Oltean 		return err;
1193bdeced75SVladimir Oltean 	}
1194bdeced75SVladimir Oltean 
119556051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
119656051948SVladimir Oltean 		struct regmap *target;
119756051948SVladimir Oltean 
119856051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
119956051948SVladimir Oltean 			continue;
120056051948SVladimir Oltean 
1201b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
1202b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1203375e1314SVladimir Oltean 		res.start += felix->switch_base;
1204375e1314SVladimir Oltean 		res.end += felix->switch_base;
120556051948SVladimir Oltean 
1206242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
120756051948SVladimir Oltean 		if (IS_ERR(target)) {
120856051948SVladimir Oltean 			dev_err(ocelot->dev,
120956051948SVladimir Oltean 				"Failed to map device memory space\n");
1210bdeced75SVladimir Oltean 			kfree(port_phy_modes);
121156051948SVladimir Oltean 			return PTR_ERR(target);
121256051948SVladimir Oltean 		}
121356051948SVladimir Oltean 
121456051948SVladimir Oltean 		ocelot->targets[i] = target;
121556051948SVladimir Oltean 	}
121656051948SVladimir Oltean 
121756051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
121856051948SVladimir Oltean 	if (err) {
121956051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
1220bdeced75SVladimir Oltean 		kfree(port_phy_modes);
122156051948SVladimir Oltean 		return err;
122256051948SVladimir Oltean 	}
122356051948SVladimir Oltean 
122456051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
122556051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
122691c724cfSVladimir Oltean 		struct regmap *target;
122756051948SVladimir Oltean 
122856051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
122956051948SVladimir Oltean 					   sizeof(struct ocelot_port),
123056051948SVladimir Oltean 					   GFP_KERNEL);
123156051948SVladimir Oltean 		if (!ocelot_port) {
123256051948SVladimir Oltean 			dev_err(ocelot->dev,
123356051948SVladimir Oltean 				"failed to allocate port memory\n");
1234bdeced75SVladimir Oltean 			kfree(port_phy_modes);
123556051948SVladimir Oltean 			return -ENOMEM;
123656051948SVladimir Oltean 		}
123756051948SVladimir Oltean 
1238b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
1239b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1240375e1314SVladimir Oltean 		res.start += felix->switch_base;
1241375e1314SVladimir Oltean 		res.end += felix->switch_base;
124256051948SVladimir Oltean 
1243242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
124491c724cfSVladimir Oltean 		if (IS_ERR(target)) {
124556051948SVladimir Oltean 			dev_err(ocelot->dev,
124691c724cfSVladimir Oltean 				"Failed to map memory space for port %d\n",
124791c724cfSVladimir Oltean 				port);
1248bdeced75SVladimir Oltean 			kfree(port_phy_modes);
124991c724cfSVladimir Oltean 			return PTR_ERR(target);
125056051948SVladimir Oltean 		}
125156051948SVladimir Oltean 
1252bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
125356051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
125491c724cfSVladimir Oltean 		ocelot_port->target = target;
12557e708760SVladimir Oltean 		ocelot_port->index = port;
125656051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
125756051948SVladimir Oltean 	}
125856051948SVladimir Oltean 
1259bdeced75SVladimir Oltean 	kfree(port_phy_modes);
1260bdeced75SVladimir Oltean 
1261bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
1262bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
1263bdeced75SVladimir Oltean 		if (err < 0)
1264bdeced75SVladimir Oltean 			return err;
1265bdeced75SVladimir Oltean 	}
1266bdeced75SVladimir Oltean 
126756051948SVladimir Oltean 	return 0;
126856051948SVladimir Oltean }
126956051948SVladimir Oltean 
12701328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port,
12711328a883SVladimir Oltean 					   struct sk_buff *skb)
12721328a883SVladimir Oltean {
12731328a883SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
12741328a883SVladimir Oltean 	struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
12751328a883SVladimir Oltean 	struct sk_buff *skb_match = NULL, *skb_tmp;
12761328a883SVladimir Oltean 	unsigned long flags;
12771328a883SVladimir Oltean 
12781328a883SVladimir Oltean 	if (!clone)
12791328a883SVladimir Oltean 		return;
12801328a883SVladimir Oltean 
12811328a883SVladimir Oltean 	spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags);
12821328a883SVladimir Oltean 
12831328a883SVladimir Oltean 	skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) {
12841328a883SVladimir Oltean 		if (skb != clone)
12851328a883SVladimir Oltean 			continue;
12861328a883SVladimir Oltean 		__skb_unlink(skb, &ocelot_port->tx_skbs);
12871328a883SVladimir Oltean 		skb_match = skb;
12881328a883SVladimir Oltean 		break;
12891328a883SVladimir Oltean 	}
12901328a883SVladimir Oltean 
12911328a883SVladimir Oltean 	spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags);
12921328a883SVladimir Oltean 
12931328a883SVladimir Oltean 	WARN_ONCE(!skb_match,
12941328a883SVladimir Oltean 		  "Could not find skb clone in TX timestamping list\n");
12951328a883SVladimir Oltean }
12961328a883SVladimir Oltean 
129749f885b2SVladimir Oltean #define work_to_xmit_work(w) \
129849f885b2SVladimir Oltean 		container_of((w), struct felix_deferred_xmit_work, work)
129949f885b2SVladimir Oltean 
130049f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work)
130149f885b2SVladimir Oltean {
130249f885b2SVladimir Oltean 	struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
130349f885b2SVladimir Oltean 	struct dsa_switch *ds = xmit_work->dp->ds;
130449f885b2SVladimir Oltean 	struct sk_buff *skb = xmit_work->skb;
130549f885b2SVladimir Oltean 	u32 rew_op = ocelot_ptp_rew_op(skb);
130649f885b2SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
130749f885b2SVladimir Oltean 	int port = xmit_work->dp->index;
130849f885b2SVladimir Oltean 	int retries = 10;
130949f885b2SVladimir Oltean 
131049f885b2SVladimir Oltean 	do {
131149f885b2SVladimir Oltean 		if (ocelot_can_inject(ocelot, 0))
131249f885b2SVladimir Oltean 			break;
131349f885b2SVladimir Oltean 
131449f885b2SVladimir Oltean 		cpu_relax();
131549f885b2SVladimir Oltean 	} while (--retries);
131649f885b2SVladimir Oltean 
131749f885b2SVladimir Oltean 	if (!retries) {
131849f885b2SVladimir Oltean 		dev_err(ocelot->dev, "port %d failed to inject skb\n",
131949f885b2SVladimir Oltean 			port);
13201328a883SVladimir Oltean 		ocelot_port_purge_txtstamp_skb(ocelot, port, skb);
132149f885b2SVladimir Oltean 		kfree_skb(skb);
132249f885b2SVladimir Oltean 		return;
132349f885b2SVladimir Oltean 	}
132449f885b2SVladimir Oltean 
132549f885b2SVladimir Oltean 	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
132649f885b2SVladimir Oltean 
132749f885b2SVladimir Oltean 	consume_skb(skb);
132849f885b2SVladimir Oltean 	kfree(xmit_work);
132949f885b2SVladimir Oltean }
133049f885b2SVladimir Oltean 
133135d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds,
133235d97680SVladimir Oltean 				      enum dsa_tag_protocol proto)
133349f885b2SVladimir Oltean {
133435d97680SVladimir Oltean 	struct ocelot_8021q_tagger_data *tagger_data;
133549f885b2SVladimir Oltean 
133635d97680SVladimir Oltean 	switch (proto) {
133735d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
133835d97680SVladimir Oltean 		tagger_data = ocelot_8021q_tagger_data(ds);
133935d97680SVladimir Oltean 		tagger_data->xmit_work_fn = felix_port_deferred_xmit;
134049f885b2SVladimir Oltean 		return 0;
134135d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
134235d97680SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
134349f885b2SVladimir Oltean 		return 0;
134435d97680SVladimir Oltean 	default:
134535d97680SVladimir Oltean 		return -EPROTONOSUPPORT;
134649f885b2SVladimir Oltean 	}
134749f885b2SVladimir Oltean }
134849f885b2SVladimir Oltean 
134956051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
135056051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
135156051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
135256051948SVladimir Oltean  * (which will not work).
135356051948SVladimir Oltean  */
135456051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
135556051948SVladimir Oltean {
135656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
135756051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
13582960bb14SVladimir Oltean 	struct dsa_port *dp;
13592960bb14SVladimir Oltean 	int err;
136056051948SVladimir Oltean 
136156051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
136256051948SVladimir Oltean 	if (err)
136356051948SVladimir Oltean 		return err;
136456051948SVladimir Oltean 
1365d1cc0e93SVladimir Oltean 	err = ocelot_init(ocelot);
1366d1cc0e93SVladimir Oltean 	if (err)
13676b73b7c9SVladimir Oltean 		goto out_mdiobus_free;
1368d1cc0e93SVladimir Oltean 
13692b49d128SYangbo Lu 	if (ocelot->ptp) {
13702ac7c6c5SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
13712b49d128SYangbo Lu 		if (err) {
13722b49d128SYangbo Lu 			dev_err(ocelot->dev,
13732b49d128SYangbo Lu 				"Timestamp initialization failed\n");
13742b49d128SYangbo Lu 			ocelot->ptp = 0;
13752b49d128SYangbo Lu 		}
13762b49d128SYangbo Lu 	}
137756051948SVladimir Oltean 
13782960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
13792960bb14SVladimir Oltean 		ocelot_init_port(ocelot, dp->index);
1380bd2b3161SXiaoliang Yang 
1381bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
1382bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
1383bd2b3161SXiaoliang Yang 		 */
13842960bb14SVladimir Oltean 		felix_port_qos_map_init(ocelot, dp->index);
138556051948SVladimir Oltean 	}
138656051948SVladimir Oltean 
1387f59fd9caSVladimir Oltean 	err = ocelot_devlink_sb_register(ocelot);
1388f59fd9caSVladimir Oltean 	if (err)
13896b73b7c9SVladimir Oltean 		goto out_deinit_ports;
1390f59fd9caSVladimir Oltean 
13917a29d220SVladimir Oltean 	/* The initial tag protocol is NPI which won't fail during initial
13927a29d220SVladimir Oltean 	 * setup, there's no real point in checking for errors.
13931cf3299bSVladimir Oltean 	 */
13947a29d220SVladimir Oltean 	felix_change_tag_protocol(ds, felix->tag_proto);
13951cf3299bSVladimir Oltean 
13960b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
1397c54913c1SVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
139854c31984SVladimir Oltean 	ds->fdb_isolation = true;
139954c31984SVladimir Oltean 	ds->max_num_bridges = ds->num_ports;
1400bdeced75SVladimir Oltean 
140156051948SVladimir Oltean 	return 0;
14026b73b7c9SVladimir Oltean 
14036b73b7c9SVladimir Oltean out_deinit_ports:
14042960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
14052960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
14066b73b7c9SVladimir Oltean 
14076b73b7c9SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
14086b73b7c9SVladimir Oltean 	ocelot_deinit(ocelot);
14096b73b7c9SVladimir Oltean 
14106b73b7c9SVladimir Oltean out_mdiobus_free:
14116b73b7c9SVladimir Oltean 	if (felix->info->mdio_bus_free)
14126b73b7c9SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
14136b73b7c9SVladimir Oltean 
14146b73b7c9SVladimir Oltean 	return err;
141556051948SVladimir Oltean }
141656051948SVladimir Oltean 
141756051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
141856051948SVladimir Oltean {
141956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1420bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
14212960bb14SVladimir Oltean 	struct dsa_port *dp;
1422bdeced75SVladimir Oltean 
14237a29d220SVladimir Oltean 	if (felix->tag_proto_ops)
14247a29d220SVladimir Oltean 		felix->tag_proto_ops->teardown(ds);
1425adb3dccfSVladimir Oltean 
14262960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
14272960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
1428d19741b0SVladimir Oltean 
142949f885b2SVladimir Oltean 	ocelot_devlink_sb_unregister(ocelot);
143049f885b2SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
143149f885b2SVladimir Oltean 	ocelot_deinit(ocelot);
143249f885b2SVladimir Oltean 
1433d19741b0SVladimir Oltean 	if (felix->info->mdio_bus_free)
1434d19741b0SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
143556051948SVladimir Oltean }
143656051948SVladimir Oltean 
1437c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
1438c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1439c0bcf537SYangbo Lu {
1440c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1441c0bcf537SYangbo Lu 
1442c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
1443c0bcf537SYangbo Lu }
1444c0bcf537SYangbo Lu 
1445c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
1446c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1447c0bcf537SYangbo Lu {
1448c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
144999348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
145099348004SVladimir Oltean 	bool using_tag_8021q;
145199348004SVladimir Oltean 	int err;
1452c0bcf537SYangbo Lu 
145399348004SVladimir Oltean 	err = ocelot_hwstamp_set(ocelot, port, ifr);
145499348004SVladimir Oltean 	if (err)
145599348004SVladimir Oltean 		return err;
145699348004SVladimir Oltean 
145799348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
145899348004SVladimir Oltean 
145999348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
1460c0bcf537SYangbo Lu }
1461c0bcf537SYangbo Lu 
1462d219b4b6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot)
14630a6f17c6SVladimir Oltean {
14640a6f17c6SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1465dbd03285SVladimir Oltean 	int err = 0, grp = 0;
14660a6f17c6SVladimir Oltean 
14670a6f17c6SVladimir Oltean 	if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q)
14680a6f17c6SVladimir Oltean 		return false;
14690a6f17c6SVladimir Oltean 
14700a6f17c6SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
14710a6f17c6SVladimir Oltean 		return false;
14720a6f17c6SVladimir Oltean 
14730a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
14740a6f17c6SVladimir Oltean 		struct sk_buff *skb;
14750a6f17c6SVladimir Oltean 		unsigned int type;
14760a6f17c6SVladimir Oltean 
14770a6f17c6SVladimir Oltean 		err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
14780a6f17c6SVladimir Oltean 		if (err)
14790a6f17c6SVladimir Oltean 			goto out;
14800a6f17c6SVladimir Oltean 
14810a6f17c6SVladimir Oltean 		/* We trap to the CPU port module all PTP frames, but
14820a6f17c6SVladimir Oltean 		 * felix_rxtstamp() only gets called for event frames.
14830a6f17c6SVladimir Oltean 		 * So we need to avoid sending duplicate general
14840a6f17c6SVladimir Oltean 		 * message frames by running a second BPF classifier
14850a6f17c6SVladimir Oltean 		 * here and dropping those.
14860a6f17c6SVladimir Oltean 		 */
14870a6f17c6SVladimir Oltean 		__skb_push(skb, ETH_HLEN);
14880a6f17c6SVladimir Oltean 
14890a6f17c6SVladimir Oltean 		type = ptp_classify_raw(skb);
14900a6f17c6SVladimir Oltean 
14910a6f17c6SVladimir Oltean 		__skb_pull(skb, ETH_HLEN);
14920a6f17c6SVladimir Oltean 
14930a6f17c6SVladimir Oltean 		if (type == PTP_CLASS_NONE) {
14940a6f17c6SVladimir Oltean 			kfree_skb(skb);
14950a6f17c6SVladimir Oltean 			continue;
14960a6f17c6SVladimir Oltean 		}
14970a6f17c6SVladimir Oltean 
14980a6f17c6SVladimir Oltean 		netif_rx(skb);
14990a6f17c6SVladimir Oltean 	}
15000a6f17c6SVladimir Oltean 
15010a6f17c6SVladimir Oltean out:
15025d3bb7ddSVladimir Oltean 	if (err < 0) {
15035d3bb7ddSVladimir Oltean 		dev_err_ratelimited(ocelot->dev,
15045d3bb7ddSVladimir Oltean 				    "Error during packet extraction: %pe\n",
15055d3bb7ddSVladimir Oltean 				    ERR_PTR(err));
15060a6f17c6SVladimir Oltean 		ocelot_drain_cpu_queue(ocelot, 0);
15075d3bb7ddSVladimir Oltean 	}
15080a6f17c6SVladimir Oltean 
15090a6f17c6SVladimir Oltean 	return true;
15100a6f17c6SVladimir Oltean }
15110a6f17c6SVladimir Oltean 
1512c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
1513c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
1514c0bcf537SYangbo Lu {
151592f62485SVladimir Oltean 	u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo;
1516c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
1517c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1518c0bcf537SYangbo Lu 	struct timespec64 ts;
151992f62485SVladimir Oltean 	u32 tstamp_hi;
152092f62485SVladimir Oltean 	u64 tstamp;
1521c0bcf537SYangbo Lu 
15220a6f17c6SVladimir Oltean 	/* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb
15230a6f17c6SVladimir Oltean 	 * for RX timestamping. Then free it, and poll for its copy through
15240a6f17c6SVladimir Oltean 	 * MMIO in the CPU port module, and inject that into the stack from
15250a6f17c6SVladimir Oltean 	 * ocelot_xtr_poll().
15260a6f17c6SVladimir Oltean 	 */
1527d219b4b6SVladimir Oltean 	if (felix_check_xtr_pkt(ocelot)) {
15280a6f17c6SVladimir Oltean 		kfree_skb(skb);
15290a6f17c6SVladimir Oltean 		return true;
15300a6f17c6SVladimir Oltean 	}
15310a6f17c6SVladimir Oltean 
1532c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1533c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1534c0bcf537SYangbo Lu 
1535c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
1536c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
1537c0bcf537SYangbo Lu 		tstamp_hi--;
1538c0bcf537SYangbo Lu 
1539c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
1540c0bcf537SYangbo Lu 
1541c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
1542c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1543c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
1544c0bcf537SYangbo Lu 	return false;
1545c0bcf537SYangbo Lu }
1546c0bcf537SYangbo Lu 
15475c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port,
15485c5416f5SYangbo Lu 			   struct sk_buff *skb)
1549c0bcf537SYangbo Lu {
1550c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1551682eaad9SYangbo Lu 	struct sk_buff *clone = NULL;
1552c0bcf537SYangbo Lu 
1553682eaad9SYangbo Lu 	if (!ocelot->ptp)
15545c5416f5SYangbo Lu 		return;
1555c0bcf537SYangbo Lu 
155652849bcfSVladimir Oltean 	if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
155752849bcfSVladimir Oltean 		dev_err_ratelimited(ds->dev,
155852849bcfSVladimir Oltean 				    "port %d delivering skb without TX timestamp\n",
155952849bcfSVladimir Oltean 				    port);
1560682eaad9SYangbo Lu 		return;
156152849bcfSVladimir Oltean 	}
1562682eaad9SYangbo Lu 
1563682eaad9SYangbo Lu 	if (clone)
1564c4b364ceSYangbo Lu 		OCELOT_SKB_CB(skb)->clone = clone;
15655c5416f5SYangbo Lu }
1566c0bcf537SYangbo Lu 
15670b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
15680b912fc9SVladimir Oltean {
15690b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15700b912fc9SVladimir Oltean 
15710b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
15720b912fc9SVladimir Oltean 
15730b912fc9SVladimir Oltean 	return 0;
15740b912fc9SVladimir Oltean }
15750b912fc9SVladimir Oltean 
15760b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
15770b912fc9SVladimir Oltean {
15780b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15790b912fc9SVladimir Oltean 
15800b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
15810b912fc9SVladimir Oltean }
15820b912fc9SVladimir Oltean 
158307d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
158407d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
158507d985eeSVladimir Oltean {
158607d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
158799348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
158899348004SVladimir Oltean 	bool using_tag_8021q;
158999348004SVladimir Oltean 	int err;
159007d985eeSVladimir Oltean 
159199348004SVladimir Oltean 	err = ocelot_cls_flower_replace(ocelot, port, cls, ingress);
159299348004SVladimir Oltean 	if (err)
159399348004SVladimir Oltean 		return err;
159499348004SVladimir Oltean 
159599348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
159699348004SVladimir Oltean 
159799348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
159807d985eeSVladimir Oltean }
159907d985eeSVladimir Oltean 
160007d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
160107d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
160207d985eeSVladimir Oltean {
160307d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
160407d985eeSVladimir Oltean 
160507d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
160607d985eeSVladimir Oltean }
160707d985eeSVladimir Oltean 
160807d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
160907d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
161007d985eeSVladimir Oltean {
161107d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
161207d985eeSVladimir Oltean 
161307d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
161407d985eeSVladimir Oltean }
161507d985eeSVladimir Oltean 
1616fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
1617fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
1618fc411eaaSVladimir Oltean {
1619fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1620fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
1621fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
16225f035af7SPo Liu 		.burst = policer->burst,
1623fc411eaaSVladimir Oltean 	};
1624fc411eaaSVladimir Oltean 
1625fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
1626fc411eaaSVladimir Oltean }
1627fc411eaaSVladimir Oltean 
1628fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
1629fc411eaaSVladimir Oltean {
1630fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1631fc411eaaSVladimir Oltean 
1632fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
1633fc411eaaSVladimir Oltean }
1634fc411eaaSVladimir Oltean 
16355e497497SVladimir Oltean static int felix_port_mirror_add(struct dsa_switch *ds, int port,
16365e497497SVladimir Oltean 				 struct dsa_mall_mirror_tc_entry *mirror,
16375e497497SVladimir Oltean 				 bool ingress, struct netlink_ext_ack *extack)
16385e497497SVladimir Oltean {
16395e497497SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
16405e497497SVladimir Oltean 
16415e497497SVladimir Oltean 	return ocelot_port_mirror_add(ocelot, port, mirror->to_local_port,
16425e497497SVladimir Oltean 				      ingress, extack);
16435e497497SVladimir Oltean }
16445e497497SVladimir Oltean 
16455e497497SVladimir Oltean static void felix_port_mirror_del(struct dsa_switch *ds, int port,
16465e497497SVladimir Oltean 				  struct dsa_mall_mirror_tc_entry *mirror)
16475e497497SVladimir Oltean {
16485e497497SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
16495e497497SVladimir Oltean 
16505e497497SVladimir Oltean 	ocelot_port_mirror_del(ocelot, port, mirror->ingress);
16515e497497SVladimir Oltean }
16525e497497SVladimir Oltean 
1653de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
1654de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
1655de143c0eSXiaoliang Yang 			       void *type_data)
1656de143c0eSXiaoliang Yang {
1657de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
1658de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
1659de143c0eSXiaoliang Yang 
1660de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
1661de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
1662de143c0eSXiaoliang Yang 	else
1663de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
1664de143c0eSXiaoliang Yang }
1665de143c0eSXiaoliang Yang 
1666f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
1667f59fd9caSVladimir Oltean 			     u16 pool_index,
1668f59fd9caSVladimir Oltean 			     struct devlink_sb_pool_info *pool_info)
1669f59fd9caSVladimir Oltean {
1670f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1671f59fd9caSVladimir Oltean 
1672f59fd9caSVladimir Oltean 	return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
1673f59fd9caSVladimir Oltean }
1674f59fd9caSVladimir Oltean 
1675f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
1676f59fd9caSVladimir Oltean 			     u16 pool_index, u32 size,
1677f59fd9caSVladimir Oltean 			     enum devlink_sb_threshold_type threshold_type,
1678f59fd9caSVladimir Oltean 			     struct netlink_ext_ack *extack)
1679f59fd9caSVladimir Oltean {
1680f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1681f59fd9caSVladimir Oltean 
1682f59fd9caSVladimir Oltean 	return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
1683f59fd9caSVladimir Oltean 				  threshold_type, extack);
1684f59fd9caSVladimir Oltean }
1685f59fd9caSVladimir Oltean 
1686f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
1687f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1688f59fd9caSVladimir Oltean 				  u32 *p_threshold)
1689f59fd9caSVladimir Oltean {
1690f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1691f59fd9caSVladimir Oltean 
1692f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
1693f59fd9caSVladimir Oltean 				       p_threshold);
1694f59fd9caSVladimir Oltean }
1695f59fd9caSVladimir Oltean 
1696f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
1697f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1698f59fd9caSVladimir Oltean 				  u32 threshold, struct netlink_ext_ack *extack)
1699f59fd9caSVladimir Oltean {
1700f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1701f59fd9caSVladimir Oltean 
1702f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
1703f59fd9caSVladimir Oltean 				       threshold, extack);
1704f59fd9caSVladimir Oltean }
1705f59fd9caSVladimir Oltean 
1706f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
1707f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1708f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1709f59fd9caSVladimir Oltean 				     u16 *p_pool_index, u32 *p_threshold)
1710f59fd9caSVladimir Oltean {
1711f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1712f59fd9caSVladimir Oltean 
1713f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
1714f59fd9caSVladimir Oltean 					  pool_type, p_pool_index,
1715f59fd9caSVladimir Oltean 					  p_threshold);
1716f59fd9caSVladimir Oltean }
1717f59fd9caSVladimir Oltean 
1718f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
1719f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1720f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1721f59fd9caSVladimir Oltean 				     u16 pool_index, u32 threshold,
1722f59fd9caSVladimir Oltean 				     struct netlink_ext_ack *extack)
1723f59fd9caSVladimir Oltean {
1724f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1725f59fd9caSVladimir Oltean 
1726f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
1727f59fd9caSVladimir Oltean 					  pool_type, pool_index, threshold,
1728f59fd9caSVladimir Oltean 					  extack);
1729f59fd9caSVladimir Oltean }
1730f59fd9caSVladimir Oltean 
1731f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds,
1732f59fd9caSVladimir Oltean 				 unsigned int sb_index)
1733f59fd9caSVladimir Oltean {
1734f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1735f59fd9caSVladimir Oltean 
1736f59fd9caSVladimir Oltean 	return ocelot_sb_occ_snapshot(ocelot, sb_index);
1737f59fd9caSVladimir Oltean }
1738f59fd9caSVladimir Oltean 
1739f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds,
1740f59fd9caSVladimir Oltean 				  unsigned int sb_index)
1741f59fd9caSVladimir Oltean {
1742f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1743f59fd9caSVladimir Oltean 
1744f59fd9caSVladimir Oltean 	return ocelot_sb_occ_max_clear(ocelot, sb_index);
1745f59fd9caSVladimir Oltean }
1746f59fd9caSVladimir Oltean 
1747f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
1748f59fd9caSVladimir Oltean 				      unsigned int sb_index, u16 pool_index,
1749f59fd9caSVladimir Oltean 				      u32 *p_cur, u32 *p_max)
1750f59fd9caSVladimir Oltean {
1751f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1752f59fd9caSVladimir Oltean 
1753f59fd9caSVladimir Oltean 	return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
1754f59fd9caSVladimir Oltean 					   p_cur, p_max);
1755f59fd9caSVladimir Oltean }
1756f59fd9caSVladimir Oltean 
1757f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
1758f59fd9caSVladimir Oltean 					 unsigned int sb_index, u16 tc_index,
1759f59fd9caSVladimir Oltean 					 enum devlink_sb_pool_type pool_type,
1760f59fd9caSVladimir Oltean 					 u32 *p_cur, u32 *p_max)
1761f59fd9caSVladimir Oltean {
1762f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1763f59fd9caSVladimir Oltean 
1764f59fd9caSVladimir Oltean 	return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
1765f59fd9caSVladimir Oltean 					      pool_type, p_cur, p_max);
1766f59fd9caSVladimir Oltean }
1767f59fd9caSVladimir Oltean 
1768a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port,
1769a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1770a026c50bSHoratiu Vultur {
1771a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1772a026c50bSHoratiu Vultur 
1773a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1774a026c50bSHoratiu Vultur }
1775a026c50bSHoratiu Vultur 
1776a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port,
1777a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1778a026c50bSHoratiu Vultur {
1779a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1780a026c50bSHoratiu Vultur 
1781a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1782a026c50bSHoratiu Vultur }
1783a026c50bSHoratiu Vultur 
1784a026c50bSHoratiu Vultur static int
1785a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port,
1786a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1787a026c50bSHoratiu Vultur {
1788a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1789a026c50bSHoratiu Vultur 
1790a026c50bSHoratiu Vultur 	return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1791a026c50bSHoratiu Vultur }
1792a026c50bSHoratiu Vultur 
1793a026c50bSHoratiu Vultur static int
1794a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port,
1795a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1796a026c50bSHoratiu Vultur {
1797a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1798a026c50bSHoratiu Vultur 
1799a026c50bSHoratiu Vultur 	return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1800a026c50bSHoratiu Vultur }
1801a026c50bSHoratiu Vultur 
1802978777d0SVladimir Oltean static int felix_port_get_default_prio(struct dsa_switch *ds, int port)
1803978777d0SVladimir Oltean {
1804978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1805978777d0SVladimir Oltean 
1806978777d0SVladimir Oltean 	return ocelot_port_get_default_prio(ocelot, port);
1807978777d0SVladimir Oltean }
1808978777d0SVladimir Oltean 
1809978777d0SVladimir Oltean static int felix_port_set_default_prio(struct dsa_switch *ds, int port,
1810978777d0SVladimir Oltean 				       u8 prio)
1811978777d0SVladimir Oltean {
1812978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1813978777d0SVladimir Oltean 
1814978777d0SVladimir Oltean 	return ocelot_port_set_default_prio(ocelot, port, prio);
1815978777d0SVladimir Oltean }
1816978777d0SVladimir Oltean 
1817978777d0SVladimir Oltean static int felix_port_get_dscp_prio(struct dsa_switch *ds, int port, u8 dscp)
1818978777d0SVladimir Oltean {
1819978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1820978777d0SVladimir Oltean 
1821978777d0SVladimir Oltean 	return ocelot_port_get_dscp_prio(ocelot, port, dscp);
1822978777d0SVladimir Oltean }
1823978777d0SVladimir Oltean 
1824978777d0SVladimir Oltean static int felix_port_add_dscp_prio(struct dsa_switch *ds, int port, u8 dscp,
1825978777d0SVladimir Oltean 				    u8 prio)
1826978777d0SVladimir Oltean {
1827978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1828978777d0SVladimir Oltean 
1829978777d0SVladimir Oltean 	return ocelot_port_add_dscp_prio(ocelot, port, dscp, prio);
1830978777d0SVladimir Oltean }
1831978777d0SVladimir Oltean 
1832978777d0SVladimir Oltean static int felix_port_del_dscp_prio(struct dsa_switch *ds, int port, u8 dscp,
1833978777d0SVladimir Oltean 				    u8 prio)
1834978777d0SVladimir Oltean {
1835978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1836978777d0SVladimir Oltean 
1837978777d0SVladimir Oltean 	return ocelot_port_del_dscp_prio(ocelot, port, dscp, prio);
1838978777d0SVladimir Oltean }
1839978777d0SVladimir Oltean 
1840375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = {
184156051948SVladimir Oltean 	.get_tag_protocol		= felix_get_tag_protocol,
1842adb3dccfSVladimir Oltean 	.change_tag_protocol		= felix_change_tag_protocol,
184335d97680SVladimir Oltean 	.connect_tag_protocol		= felix_connect_tag_protocol,
184456051948SVladimir Oltean 	.setup				= felix_setup,
184556051948SVladimir Oltean 	.teardown			= felix_teardown,
184656051948SVladimir Oltean 	.set_ageing_time		= felix_set_ageing_time,
184756051948SVladimir Oltean 	.get_strings			= felix_get_strings,
184856051948SVladimir Oltean 	.get_ethtool_stats		= felix_get_ethtool_stats,
184956051948SVladimir Oltean 	.get_sset_count			= felix_get_sset_count,
185056051948SVladimir Oltean 	.get_ts_info			= felix_get_ts_info,
185179fda660SRussell King (Oracle) 	.phylink_get_caps		= felix_phylink_get_caps,
1852bdeced75SVladimir Oltean 	.phylink_validate		= felix_phylink_validate,
1853864ba485SRussell King (Oracle) 	.phylink_mac_select_pcs		= felix_phylink_mac_select_pcs,
1854bdeced75SVladimir Oltean 	.phylink_mac_link_down		= felix_phylink_mac_link_down,
1855bdeced75SVladimir Oltean 	.phylink_mac_link_up		= felix_phylink_mac_link_up,
18565cad43a5SVladimir Oltean 	.port_fast_age			= felix_port_fast_age,
185756051948SVladimir Oltean 	.port_fdb_dump			= felix_fdb_dump,
185856051948SVladimir Oltean 	.port_fdb_add			= felix_fdb_add,
185956051948SVladimir Oltean 	.port_fdb_del			= felix_fdb_del,
1860961d8b69SVladimir Oltean 	.lag_fdb_add			= felix_lag_fdb_add,
1861961d8b69SVladimir Oltean 	.lag_fdb_del			= felix_lag_fdb_del,
1862209edf95SVladimir Oltean 	.port_mdb_add			= felix_mdb_add,
1863209edf95SVladimir Oltean 	.port_mdb_del			= felix_mdb_del,
1864421741eaSVladimir Oltean 	.port_pre_bridge_flags		= felix_pre_bridge_flags,
1865421741eaSVladimir Oltean 	.port_bridge_flags		= felix_bridge_flags,
186656051948SVladimir Oltean 	.port_bridge_join		= felix_bridge_join,
186756051948SVladimir Oltean 	.port_bridge_leave		= felix_bridge_leave,
18688fe6832eSVladimir Oltean 	.port_lag_join			= felix_lag_join,
18698fe6832eSVladimir Oltean 	.port_lag_leave			= felix_lag_leave,
18708fe6832eSVladimir Oltean 	.port_lag_change		= felix_lag_change,
187156051948SVladimir Oltean 	.port_stp_state_set		= felix_bridge_stp_state_set,
187256051948SVladimir Oltean 	.port_vlan_filtering		= felix_vlan_filtering,
187356051948SVladimir Oltean 	.port_vlan_add			= felix_vlan_add,
187456051948SVladimir Oltean 	.port_vlan_del			= felix_vlan_del,
1875c0bcf537SYangbo Lu 	.port_hwtstamp_get		= felix_hwtstamp_get,
1876c0bcf537SYangbo Lu 	.port_hwtstamp_set		= felix_hwtstamp_set,
1877c0bcf537SYangbo Lu 	.port_rxtstamp			= felix_rxtstamp,
1878c0bcf537SYangbo Lu 	.port_txtstamp			= felix_txtstamp,
18790b912fc9SVladimir Oltean 	.port_change_mtu		= felix_change_mtu,
18800b912fc9SVladimir Oltean 	.port_max_mtu			= felix_get_max_mtu,
1881fc411eaaSVladimir Oltean 	.port_policer_add		= felix_port_policer_add,
1882fc411eaaSVladimir Oltean 	.port_policer_del		= felix_port_policer_del,
18835e497497SVladimir Oltean 	.port_mirror_add		= felix_port_mirror_add,
18845e497497SVladimir Oltean 	.port_mirror_del		= felix_port_mirror_del,
188507d985eeSVladimir Oltean 	.cls_flower_add			= felix_cls_flower_add,
188607d985eeSVladimir Oltean 	.cls_flower_del			= felix_cls_flower_del,
188707d985eeSVladimir Oltean 	.cls_flower_stats		= felix_cls_flower_stats,
1888de143c0eSXiaoliang Yang 	.port_setup_tc			= felix_port_setup_tc,
1889f59fd9caSVladimir Oltean 	.devlink_sb_pool_get		= felix_sb_pool_get,
1890f59fd9caSVladimir Oltean 	.devlink_sb_pool_set		= felix_sb_pool_set,
1891f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_get	= felix_sb_port_pool_get,
1892f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_set	= felix_sb_port_pool_set,
1893f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_get	= felix_sb_tc_pool_bind_get,
1894f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_set	= felix_sb_tc_pool_bind_set,
1895f59fd9caSVladimir Oltean 	.devlink_sb_occ_snapshot	= felix_sb_occ_snapshot,
1896f59fd9caSVladimir Oltean 	.devlink_sb_occ_max_clear	= felix_sb_occ_max_clear,
1897f59fd9caSVladimir Oltean 	.devlink_sb_occ_port_pool_get	= felix_sb_occ_port_pool_get,
1898f59fd9caSVladimir Oltean 	.devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
1899a026c50bSHoratiu Vultur 	.port_mrp_add			= felix_mrp_add,
1900a026c50bSHoratiu Vultur 	.port_mrp_del			= felix_mrp_del,
1901a026c50bSHoratiu Vultur 	.port_mrp_add_ring_role		= felix_mrp_add_ring_role,
1902a026c50bSHoratiu Vultur 	.port_mrp_del_ring_role		= felix_mrp_del_ring_role,
19035da11eb4SVladimir Oltean 	.tag_8021q_vlan_add		= felix_tag_8021q_vlan_add,
19045da11eb4SVladimir Oltean 	.tag_8021q_vlan_del		= felix_tag_8021q_vlan_del,
1905978777d0SVladimir Oltean 	.port_get_default_prio		= felix_port_get_default_prio,
1906978777d0SVladimir Oltean 	.port_set_default_prio		= felix_port_set_default_prio,
1907978777d0SVladimir Oltean 	.port_get_dscp_prio		= felix_port_get_dscp_prio,
1908978777d0SVladimir Oltean 	.port_add_dscp_prio		= felix_port_add_dscp_prio,
1909978777d0SVladimir Oltean 	.port_del_dscp_prio		= felix_port_del_dscp_prio,
191072c3b0c7SVladimir Oltean 	.port_set_host_flood		= felix_port_set_host_flood,
191156051948SVladimir Oltean };
1912319e4dd1SVladimir Oltean 
1913319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
1914319e4dd1SVladimir Oltean {
1915319e4dd1SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1916319e4dd1SVladimir Oltean 	struct dsa_switch *ds = felix->ds;
1917319e4dd1SVladimir Oltean 
1918319e4dd1SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
1919319e4dd1SVladimir Oltean 		return NULL;
1920319e4dd1SVladimir Oltean 
1921319e4dd1SVladimir Oltean 	return dsa_to_port(ds, port)->slave;
1922319e4dd1SVladimir Oltean }
1923319e4dd1SVladimir Oltean 
1924319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev)
1925319e4dd1SVladimir Oltean {
1926319e4dd1SVladimir Oltean 	struct dsa_port *dp;
1927319e4dd1SVladimir Oltean 
1928319e4dd1SVladimir Oltean 	dp = dsa_port_from_netdev(dev);
1929319e4dd1SVladimir Oltean 	if (IS_ERR(dp))
1930319e4dd1SVladimir Oltean 		return -EINVAL;
1931319e4dd1SVladimir Oltean 
1932319e4dd1SVladimir Oltean 	return dp->index;
1933319e4dd1SVladimir Oltean }
1934