xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision 72c3b0c7359a6f91dd03e08b839adccd9d4268a8)
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 
45b903a6bdSVladimir Oltean static void felix_migrate_pgid_bit(struct dsa_switch *ds, int from, int to,
46b903a6bdSVladimir Oltean 				   int pgid)
47b903a6bdSVladimir Oltean {
48b903a6bdSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
49b903a6bdSVladimir Oltean 	bool on;
50b903a6bdSVladimir Oltean 	u32 val;
51b903a6bdSVladimir Oltean 
52b903a6bdSVladimir Oltean 	val = ocelot_read_rix(ocelot, ANA_PGID_PGID, pgid);
53b903a6bdSVladimir Oltean 	on = !!(val & BIT(from));
54b903a6bdSVladimir Oltean 	val &= ~BIT(from);
55b903a6bdSVladimir Oltean 	if (on)
56b903a6bdSVladimir Oltean 		val |= BIT(to);
57b903a6bdSVladimir Oltean 	else
58b903a6bdSVladimir Oltean 		val &= ~BIT(to);
59b903a6bdSVladimir Oltean 
60b903a6bdSVladimir Oltean 	ocelot_write_rix(ocelot, val, ANA_PGID_PGID, pgid);
61b903a6bdSVladimir Oltean }
62b903a6bdSVladimir Oltean 
63b903a6bdSVladimir Oltean static void felix_migrate_flood_to_npi_port(struct dsa_switch *ds, int port)
64b903a6bdSVladimir Oltean {
65b903a6bdSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
66b903a6bdSVladimir Oltean 
67b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, port, ocelot->num_phys_ports, PGID_UC);
68b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, port, ocelot->num_phys_ports, PGID_MC);
69b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, port, ocelot->num_phys_ports, PGID_BC);
70b903a6bdSVladimir Oltean }
71b903a6bdSVladimir Oltean 
72b903a6bdSVladimir Oltean static void
73b903a6bdSVladimir Oltean felix_migrate_flood_to_tag_8021q_port(struct dsa_switch *ds, int port)
74b903a6bdSVladimir Oltean {
75b903a6bdSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
76b903a6bdSVladimir Oltean 
77b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, ocelot->num_phys_ports, port, PGID_UC);
78b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, ocelot->num_phys_ports, port, PGID_MC);
79b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, ocelot->num_phys_ports, port, PGID_BC);
80b903a6bdSVladimir Oltean }
81b903a6bdSVladimir Oltean 
8204b67e18SVladimir Oltean /* Set up VCAP ES0 rules for pushing a tag_8021q VLAN towards the CPU such that
8304b67e18SVladimir Oltean  * the tagger can perform RX source port identification.
8404b67e18SVladimir Oltean  */
8504b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add_rx(struct felix *felix, int port, u16 vid)
86e21268efSVladimir Oltean {
87e21268efSVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
88e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
89e21268efSVladimir Oltean 	struct dsa_switch *ds = felix->ds;
90e21268efSVladimir Oltean 	int key_length, upstream, err;
91e21268efSVladimir Oltean 
92e21268efSVladimir Oltean 	key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length;
93e21268efSVladimir Oltean 	upstream = dsa_upstream_port(ds, port);
94e21268efSVladimir Oltean 
95e21268efSVladimir Oltean 	outer_tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter),
96e21268efSVladimir Oltean 				     GFP_KERNEL);
97e21268efSVladimir Oltean 	if (!outer_tagging_rule)
98e21268efSVladimir Oltean 		return -ENOMEM;
99e21268efSVladimir Oltean 
100e21268efSVladimir Oltean 	outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
101e21268efSVladimir Oltean 	outer_tagging_rule->prio = 1;
102c518afecSVladimir Oltean 	outer_tagging_rule->id.cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port);
103e21268efSVladimir Oltean 	outer_tagging_rule->id.tc_offload = false;
104e21268efSVladimir Oltean 	outer_tagging_rule->block_id = VCAP_ES0;
105e21268efSVladimir Oltean 	outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
106e21268efSVladimir Oltean 	outer_tagging_rule->lookup = 0;
107e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.value = port;
108e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0);
109e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.value = upstream;
110e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0);
111e21268efSVladimir Oltean 	outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG;
112e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD;
113e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_vid_sel = 1;
114e21268efSVladimir Oltean 	outer_tagging_rule->action.vid_a_val = vid;
115e21268efSVladimir Oltean 
116e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL);
117e21268efSVladimir Oltean 	if (err)
118e21268efSVladimir Oltean 		kfree(outer_tagging_rule);
119e21268efSVladimir Oltean 
120e21268efSVladimir Oltean 	return err;
121e21268efSVladimir Oltean }
122e21268efSVladimir Oltean 
12304b67e18SVladimir Oltean static int felix_tag_8021q_vlan_del_rx(struct felix *felix, int port, u16 vid)
12404b67e18SVladimir Oltean {
12504b67e18SVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
12604b67e18SVladimir Oltean 	struct ocelot_vcap_block *block_vcap_es0;
12704b67e18SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
12804b67e18SVladimir Oltean 
12904b67e18SVladimir Oltean 	block_vcap_es0 = &ocelot->block[VCAP_ES0];
13004b67e18SVladimir Oltean 
13104b67e18SVladimir Oltean 	outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0,
13204b67e18SVladimir Oltean 								 port, false);
13304b67e18SVladimir Oltean 	if (!outer_tagging_rule)
13404b67e18SVladimir Oltean 		return -ENOENT;
13504b67e18SVladimir Oltean 
13604b67e18SVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, outer_tagging_rule);
13704b67e18SVladimir Oltean }
13804b67e18SVladimir Oltean 
13904b67e18SVladimir Oltean /* Set up VCAP IS1 rules for stripping the tag_8021q VLAN on TX and VCAP IS2
14004b67e18SVladimir Oltean  * rules for steering those tagged packets towards the correct destination port
14104b67e18SVladimir Oltean  */
14204b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add_tx(struct felix *felix, int port, u16 vid)
143e21268efSVladimir Oltean {
144e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
145e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
146e21268efSVladimir Oltean 	struct dsa_switch *ds = felix->ds;
147e21268efSVladimir Oltean 	int upstream, err;
148e21268efSVladimir Oltean 
149e21268efSVladimir Oltean 	untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
150e21268efSVladimir Oltean 	if (!untagging_rule)
151e21268efSVladimir Oltean 		return -ENOMEM;
152e21268efSVladimir Oltean 
153e21268efSVladimir Oltean 	redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
154e21268efSVladimir Oltean 	if (!redirect_rule) {
155e21268efSVladimir Oltean 		kfree(untagging_rule);
156e21268efSVladimir Oltean 		return -ENOMEM;
157e21268efSVladimir Oltean 	}
158e21268efSVladimir Oltean 
159e21268efSVladimir Oltean 	upstream = dsa_upstream_port(ds, port);
160e21268efSVladimir Oltean 
161e21268efSVladimir Oltean 	untagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
162e21268efSVladimir Oltean 	untagging_rule->ingress_port_mask = BIT(upstream);
163e21268efSVladimir Oltean 	untagging_rule->vlan.vid.value = vid;
164e21268efSVladimir Oltean 	untagging_rule->vlan.vid.mask = VLAN_VID_MASK;
165e21268efSVladimir Oltean 	untagging_rule->prio = 1;
166c518afecSVladimir Oltean 	untagging_rule->id.cookie = OCELOT_VCAP_IS1_TAG_8021Q_TXVLAN(ocelot, port);
167e21268efSVladimir Oltean 	untagging_rule->id.tc_offload = false;
168e21268efSVladimir Oltean 	untagging_rule->block_id = VCAP_IS1;
169e21268efSVladimir Oltean 	untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
170e21268efSVladimir Oltean 	untagging_rule->lookup = 0;
171e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt_ena = true;
172e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt = 1;
173e21268efSVladimir Oltean 	untagging_rule->action.pag_override_mask = 0xff;
174e21268efSVladimir Oltean 	untagging_rule->action.pag_val = port;
175e21268efSVladimir Oltean 
176e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL);
177e21268efSVladimir Oltean 	if (err) {
178e21268efSVladimir Oltean 		kfree(untagging_rule);
179e21268efSVladimir Oltean 		kfree(redirect_rule);
180e21268efSVladimir Oltean 		return err;
181e21268efSVladimir Oltean 	}
182e21268efSVladimir Oltean 
183e21268efSVladimir Oltean 	redirect_rule->key_type = OCELOT_VCAP_KEY_ANY;
184e21268efSVladimir Oltean 	redirect_rule->ingress_port_mask = BIT(upstream);
185e21268efSVladimir Oltean 	redirect_rule->pag = port;
186e21268efSVladimir Oltean 	redirect_rule->prio = 1;
187c518afecSVladimir Oltean 	redirect_rule->id.cookie = OCELOT_VCAP_IS2_TAG_8021Q_TXVLAN(ocelot, port);
188e21268efSVladimir Oltean 	redirect_rule->id.tc_offload = false;
189e21268efSVladimir Oltean 	redirect_rule->block_id = VCAP_IS2;
190e21268efSVladimir Oltean 	redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
191e21268efSVladimir Oltean 	redirect_rule->lookup = 0;
192e21268efSVladimir Oltean 	redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT;
193e21268efSVladimir Oltean 	redirect_rule->action.port_mask = BIT(port);
194e21268efSVladimir Oltean 
195e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL);
196e21268efSVladimir Oltean 	if (err) {
197e21268efSVladimir Oltean 		ocelot_vcap_filter_del(ocelot, untagging_rule);
198e21268efSVladimir Oltean 		kfree(redirect_rule);
199e21268efSVladimir Oltean 		return err;
200e21268efSVladimir Oltean 	}
201e21268efSVladimir Oltean 
202e21268efSVladimir Oltean 	return 0;
203e21268efSVladimir Oltean }
204e21268efSVladimir Oltean 
20504b67e18SVladimir Oltean static int felix_tag_8021q_vlan_del_tx(struct felix *felix, int port, u16 vid)
206e21268efSVladimir Oltean {
207e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
208e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is1;
209e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
210e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
211e21268efSVladimir Oltean 	int err;
212e21268efSVladimir Oltean 
213e21268efSVladimir Oltean 	block_vcap_is1 = &ocelot->block[VCAP_IS1];
214e21268efSVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
215e21268efSVladimir Oltean 
216e21268efSVladimir Oltean 	untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1,
217e21268efSVladimir Oltean 							     port, false);
218e21268efSVladimir Oltean 	if (!untagging_rule)
21908f44db3SVladimir Oltean 		return -ENOENT;
220e21268efSVladimir Oltean 
221e21268efSVladimir Oltean 	err = ocelot_vcap_filter_del(ocelot, untagging_rule);
222e21268efSVladimir Oltean 	if (err)
223e21268efSVladimir Oltean 		return err;
224e21268efSVladimir Oltean 
225e21268efSVladimir Oltean 	redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2,
226e21268efSVladimir Oltean 							    port, false);
227e21268efSVladimir Oltean 	if (!redirect_rule)
22804b67e18SVladimir Oltean 		return -ENOENT;
229e21268efSVladimir Oltean 
230e21268efSVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, redirect_rule);
231e21268efSVladimir Oltean }
232e21268efSVladimir Oltean 
23304b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
23404b67e18SVladimir Oltean 				    u16 flags)
23504b67e18SVladimir Oltean {
23604b67e18SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
23704b67e18SVladimir Oltean 	int err;
23804b67e18SVladimir Oltean 
23904b67e18SVladimir Oltean 	/* tag_8021q.c assumes we are implementing this via port VLAN
24004b67e18SVladimir Oltean 	 * membership, which we aren't. So we don't need to add any VCAP filter
24104b67e18SVladimir Oltean 	 * for the CPU port.
24204b67e18SVladimir Oltean 	 */
24304b67e18SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
24404b67e18SVladimir Oltean 		return 0;
24504b67e18SVladimir Oltean 
24604b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_add_rx(ocelot_to_felix(ocelot), port, vid);
24704b67e18SVladimir Oltean 	if (err)
24804b67e18SVladimir Oltean 		return err;
24904b67e18SVladimir Oltean 
25004b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_add_tx(ocelot_to_felix(ocelot), port, vid);
25104b67e18SVladimir Oltean 	if (err) {
25204b67e18SVladimir Oltean 		felix_tag_8021q_vlan_del_rx(ocelot_to_felix(ocelot), port, vid);
25304b67e18SVladimir Oltean 		return err;
25404b67e18SVladimir Oltean 	}
25504b67e18SVladimir Oltean 
25604b67e18SVladimir Oltean 	return 0;
25704b67e18SVladimir Oltean }
25804b67e18SVladimir Oltean 
259e21268efSVladimir Oltean static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
260e21268efSVladimir Oltean {
261e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
26204b67e18SVladimir Oltean 	int err;
263e21268efSVladimir Oltean 
26404b67e18SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
26504b67e18SVladimir Oltean 		return 0;
266e21268efSVladimir Oltean 
26704b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_del_rx(ocelot_to_felix(ocelot), port, vid);
26804b67e18SVladimir Oltean 	if (err)
26904b67e18SVladimir Oltean 		return err;
27004b67e18SVladimir Oltean 
27104b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_del_tx(ocelot_to_felix(ocelot), port, vid);
27204b67e18SVladimir Oltean 	if (err) {
27304b67e18SVladimir Oltean 		felix_tag_8021q_vlan_add_rx(ocelot_to_felix(ocelot), port, vid);
27404b67e18SVladimir Oltean 		return err;
27504b67e18SVladimir Oltean 	}
276e21268efSVladimir Oltean 
277e21268efSVladimir Oltean 	return 0;
278e21268efSVladimir Oltean }
279e21268efSVladimir Oltean 
280e21268efSVladimir Oltean /* Alternatively to using the NPI functionality, that same hardware MAC
281e21268efSVladimir Oltean  * connected internally to the enetc or fman DSA master can be configured to
282e21268efSVladimir Oltean  * use the software-defined tag_8021q frame format. As far as the hardware is
283e21268efSVladimir Oltean  * concerned, it thinks it is a "dumb switch" - the queues of the CPU port
284e21268efSVladimir Oltean  * module are now disconnected from it, but can still be accessed through
285e21268efSVladimir Oltean  * register-based MMIO.
286e21268efSVladimir Oltean  */
287e21268efSVladimir Oltean static void felix_8021q_cpu_port_init(struct ocelot *ocelot, int port)
288e21268efSVladimir Oltean {
2898abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2908abe1970SVladimir Oltean 
29154c31984SVladimir Oltean 	ocelot_port_set_dsa_8021q_cpu(ocelot, port);
292e21268efSVladimir Oltean 
293e21268efSVladimir Oltean 	/* Overwrite PGID_CPU with the non-tagging port */
294e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, PGID_CPU);
295e21268efSVladimir Oltean 
2968abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
2978abe1970SVladimir Oltean 
2988abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
299e21268efSVladimir Oltean }
300e21268efSVladimir Oltean 
301e21268efSVladimir Oltean static void felix_8021q_cpu_port_deinit(struct ocelot *ocelot, int port)
302e21268efSVladimir Oltean {
3038abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
3048abe1970SVladimir Oltean 
30554c31984SVladimir Oltean 	ocelot_port_unset_dsa_8021q_cpu(ocelot, port);
306e21268efSVladimir Oltean 
307e21268efSVladimir Oltean 	/* Restore PGID_CPU */
308e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(ocelot->num_phys_ports), ANA_PGID_PGID,
309e21268efSVladimir Oltean 			 PGID_CPU);
310e21268efSVladimir Oltean 
3118abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
3128abe1970SVladimir Oltean 
3138abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
314e21268efSVladimir Oltean }
315e21268efSVladimir Oltean 
31699348004SVladimir Oltean /* On switches with no extraction IRQ wired, trapped packets need to be
31799348004SVladimir Oltean  * replicated over Ethernet as well, otherwise we'd get no notification of
31899348004SVladimir Oltean  * their arrival when using the ocelot-8021q tagging protocol.
319c8c0ba4fSVladimir Oltean  */
32099348004SVladimir Oltean static int felix_update_trapping_destinations(struct dsa_switch *ds,
32199348004SVladimir Oltean 					      bool using_tag_8021q)
322c8c0ba4fSVladimir Oltean {
32399348004SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
32499348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
325e1846cffSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
32699348004SVladimir Oltean 	struct ocelot_vcap_filter *trap;
32799348004SVladimir Oltean 	enum ocelot_mask_mode mask_mode;
32899348004SVladimir Oltean 	unsigned long port_mask;
3292960bb14SVladimir Oltean 	struct dsa_port *dp;
33099348004SVladimir Oltean 	bool cpu_copy_ena;
33199348004SVladimir Oltean 	int cpu = -1, err;
332c8c0ba4fSVladimir Oltean 
33399348004SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
33499348004SVladimir Oltean 		return 0;
335c8c0ba4fSVladimir Oltean 
33699348004SVladimir Oltean 	/* Figure out the current CPU port */
3372960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
3382960bb14SVladimir Oltean 		cpu = dp->index;
3398d5f7954SVladimir Oltean 		break;
340c8c0ba4fSVladimir Oltean 	}
3418d5f7954SVladimir Oltean 
342d78637a8SVladimir Oltean 	/* We are sure that "cpu" was found, otherwise
343d78637a8SVladimir Oltean 	 * dsa_tree_setup_default_cpu() would have failed earlier.
344d78637a8SVladimir Oltean 	 */
345e1846cffSVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
346c8c0ba4fSVladimir Oltean 
34799348004SVladimir Oltean 	/* Make sure all traps are set up for that destination */
348e1846cffSVladimir Oltean 	list_for_each_entry(trap, &block_vcap_is2->rules, list) {
349e1846cffSVladimir Oltean 		if (!trap->is_trap)
350e1846cffSVladimir Oltean 			continue;
351e1846cffSVladimir Oltean 
35299348004SVladimir Oltean 		/* Figure out the current trapping destination */
35399348004SVladimir Oltean 		if (using_tag_8021q) {
35499348004SVladimir Oltean 			/* Redirect to the tag_8021q CPU port. If timestamps
35599348004SVladimir Oltean 			 * are necessary, also copy trapped packets to the CPU
35699348004SVladimir Oltean 			 * port module.
357c8c0ba4fSVladimir Oltean 			 */
35899348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_REDIRECT;
35999348004SVladimir Oltean 			port_mask = BIT(cpu);
36099348004SVladimir Oltean 			cpu_copy_ena = !!trap->take_ts;
361c8c0ba4fSVladimir Oltean 		} else {
36299348004SVladimir Oltean 			/* Trap packets only to the CPU port module, which is
36399348004SVladimir Oltean 			 * redirected to the NPI port (the DSA CPU port)
364c8c0ba4fSVladimir Oltean 			 */
36599348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
36699348004SVladimir Oltean 			port_mask = 0;
36799348004SVladimir Oltean 			cpu_copy_ena = true;
368c8c0ba4fSVladimir Oltean 		}
369c8c0ba4fSVladimir Oltean 
37099348004SVladimir Oltean 		if (trap->action.mask_mode == mask_mode &&
37199348004SVladimir Oltean 		    trap->action.port_mask == port_mask &&
37299348004SVladimir Oltean 		    trap->action.cpu_copy_ena == cpu_copy_ena)
37399348004SVladimir Oltean 			continue;
374c8c0ba4fSVladimir Oltean 
37599348004SVladimir Oltean 		trap->action.mask_mode = mask_mode;
37699348004SVladimir Oltean 		trap->action.port_mask = port_mask;
37799348004SVladimir Oltean 		trap->action.cpu_copy_ena = cpu_copy_ena;
3780a6f17c6SVladimir Oltean 
37999348004SVladimir Oltean 		err = ocelot_vcap_filter_replace(ocelot, trap);
380c8c0ba4fSVladimir Oltean 		if (err)
381c8c0ba4fSVladimir Oltean 			return err;
38299348004SVladimir Oltean 	}
383c8c0ba4fSVladimir Oltean 
38499348004SVladimir Oltean 	return 0;
385c8c0ba4fSVladimir Oltean }
386c8c0ba4fSVladimir Oltean 
387c69f40acSVladimir Oltean static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu)
388e21268efSVladimir Oltean {
389e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
3902960bb14SVladimir Oltean 	struct dsa_port *dp;
3912960bb14SVladimir Oltean 	int err;
392e21268efSVladimir Oltean 
393e21268efSVladimir Oltean 	felix_8021q_cpu_port_init(ocelot, cpu);
394e21268efSVladimir Oltean 
3952960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
396e21268efSVladimir Oltean 		/* This overwrites ocelot_init():
397e21268efSVladimir Oltean 		 * Do not forward BPDU frames to the CPU port module,
398e21268efSVladimir Oltean 		 * for 2 reasons:
399e21268efSVladimir Oltean 		 * - When these packets are injected from the tag_8021q
400e21268efSVladimir Oltean 		 *   CPU port, we want them to go out, not loop back
401e21268efSVladimir Oltean 		 *   into the system.
402e21268efSVladimir Oltean 		 * - STP traffic ingressing on a user port should go to
403e21268efSVladimir Oltean 		 *   the tag_8021q CPU port, not to the hardware CPU
404e21268efSVladimir Oltean 		 *   port module.
405e21268efSVladimir Oltean 		 */
406e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
407e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0),
4082960bb14SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG, dp->index);
409e21268efSVladimir Oltean 	}
410e21268efSVladimir Oltean 
4115da11eb4SVladimir Oltean 	err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD));
412e21268efSVladimir Oltean 	if (err)
413d7b1fd52SVladimir Oltean 		return err;
414d7b1fd52SVladimir Oltean 
41528de0f9fSVladimir Oltean 	err = ocelot_migrate_mdbs(ocelot, BIT(ocelot->num_phys_ports),
41628de0f9fSVladimir Oltean 				  BIT(cpu));
417f9cef64fSVladimir Oltean 	if (err)
418a51c1c3fSVladimir Oltean 		goto out_tag_8021q_unregister;
419b903a6bdSVladimir Oltean 
420b903a6bdSVladimir Oltean 	felix_migrate_flood_to_tag_8021q_port(ds, cpu);
421f9cef64fSVladimir Oltean 
422f9cef64fSVladimir Oltean 	err = felix_update_trapping_destinations(ds, true);
423f9cef64fSVladimir Oltean 	if (err)
424b903a6bdSVladimir Oltean 		goto out_migrate_flood;
425f9cef64fSVladimir Oltean 
42699348004SVladimir Oltean 	/* The ownership of the CPU port module's queues might have just been
42799348004SVladimir Oltean 	 * transferred to the tag_8021q tagger from the NPI-based tagger.
42899348004SVladimir Oltean 	 * So there might still be all sorts of crap in the queues. On the
42999348004SVladimir Oltean 	 * other hand, the MMIO-based matching of PTP frames is very brittle,
43099348004SVladimir Oltean 	 * so we need to be careful that there are no extra frames to be
43199348004SVladimir Oltean 	 * dequeued over MMIO, since we would never know to discard them.
43299348004SVladimir Oltean 	 */
43399348004SVladimir Oltean 	ocelot_drain_cpu_queue(ocelot, 0);
43499348004SVladimir Oltean 
435e21268efSVladimir Oltean 	return 0;
436e21268efSVladimir Oltean 
437b903a6bdSVladimir Oltean out_migrate_flood:
438b903a6bdSVladimir Oltean 	felix_migrate_flood_to_npi_port(ds, cpu);
43928de0f9fSVladimir Oltean 	ocelot_migrate_mdbs(ocelot, BIT(cpu), BIT(ocelot->num_phys_ports));
440d7b1fd52SVladimir Oltean out_tag_8021q_unregister:
441d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
442e21268efSVladimir Oltean 	return err;
443e21268efSVladimir Oltean }
444e21268efSVladimir Oltean 
445e21268efSVladimir Oltean static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu)
446e21268efSVladimir Oltean {
447e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
4482960bb14SVladimir Oltean 	struct dsa_port *dp;
4492960bb14SVladimir Oltean 	int err;
450e21268efSVladimir Oltean 
45199348004SVladimir Oltean 	err = felix_update_trapping_destinations(ds, false);
452c8c0ba4fSVladimir Oltean 	if (err)
453c8c0ba4fSVladimir Oltean 		dev_err(ds->dev, "felix_teardown_mmio_filtering returned %d",
454c8c0ba4fSVladimir Oltean 			err);
455c8c0ba4fSVladimir Oltean 
456d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
457e21268efSVladimir Oltean 
4582960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
459e21268efSVladimir Oltean 		/* Restore the logic from ocelot_init:
460e21268efSVladimir Oltean 		 * do not forward BPDU frames to the front ports.
461e21268efSVladimir Oltean 		 */
462e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
463e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
464e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG,
4652960bb14SVladimir Oltean 				 dp->index);
466e21268efSVladimir Oltean 	}
467e21268efSVladimir Oltean 
468e21268efSVladimir Oltean 	felix_8021q_cpu_port_deinit(ocelot, cpu);
469e21268efSVladimir Oltean }
470e21268efSVladimir Oltean 
471adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This
472adb3dccfSVladimir Oltean  * is the mode through which frames can be injected from and extracted to an
473adb3dccfSVladimir Oltean  * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU
474adb3dccfSVladimir Oltean  * running Linux, and this forms a DSA setup together with the enetc or fman
475adb3dccfSVladimir Oltean  * DSA master.
476adb3dccfSVladimir Oltean  */
477adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port)
478adb3dccfSVladimir Oltean {
479adb3dccfSVladimir Oltean 	ocelot->npi = port;
480adb3dccfSVladimir Oltean 
481adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
482adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
483adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
484adb3dccfSVladimir Oltean 
485adb3dccfSVladimir Oltean 	/* NPI port Injection/Extraction configuration */
486adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
487adb3dccfSVladimir Oltean 			    ocelot->npi_xtr_prefix);
488adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
489adb3dccfSVladimir Oltean 			    ocelot->npi_inj_prefix);
490adb3dccfSVladimir Oltean 
491adb3dccfSVladimir Oltean 	/* Disable transmission of pause frames */
492adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
493adb3dccfSVladimir Oltean }
494adb3dccfSVladimir Oltean 
495adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port)
496adb3dccfSVladimir Oltean {
497adb3dccfSVladimir Oltean 	/* Restore hardware defaults */
498adb3dccfSVladimir Oltean 	int unused_port = ocelot->num_phys_ports + 2;
499adb3dccfSVladimir Oltean 
500adb3dccfSVladimir Oltean 	ocelot->npi = -1;
501adb3dccfSVladimir Oltean 
502adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port),
503adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
504adb3dccfSVladimir Oltean 
505adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
506adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
507adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
508adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
509adb3dccfSVladimir Oltean 
510adb3dccfSVladimir Oltean 	/* Enable transmission of pause frames */
511adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
512adb3dccfSVladimir Oltean }
513adb3dccfSVladimir Oltean 
514c69f40acSVladimir Oltean static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu)
515adb3dccfSVladimir Oltean {
516adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
517f9cef64fSVladimir Oltean 	int err;
518f9cef64fSVladimir Oltean 
51928de0f9fSVladimir Oltean 	err = ocelot_migrate_mdbs(ocelot, BIT(cpu),
52028de0f9fSVladimir Oltean 				  BIT(ocelot->num_phys_ports));
521f9cef64fSVladimir Oltean 	if (err)
522a51c1c3fSVladimir Oltean 		return err;
523b903a6bdSVladimir Oltean 
524b903a6bdSVladimir Oltean 	felix_migrate_flood_to_npi_port(ds, cpu);
525adb3dccfSVladimir Oltean 
526adb3dccfSVladimir Oltean 	felix_npi_port_init(ocelot, cpu);
527adb3dccfSVladimir Oltean 
528adb3dccfSVladimir Oltean 	return 0;
529adb3dccfSVladimir Oltean }
530adb3dccfSVladimir Oltean 
531adb3dccfSVladimir Oltean static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu)
532adb3dccfSVladimir Oltean {
533adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
534adb3dccfSVladimir Oltean 
535adb3dccfSVladimir Oltean 	felix_npi_port_deinit(ocelot, cpu);
536adb3dccfSVladimir Oltean }
537adb3dccfSVladimir Oltean 
538adb3dccfSVladimir Oltean static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu,
539c69f40acSVladimir Oltean 				  enum dsa_tag_protocol proto)
540adb3dccfSVladimir Oltean {
541adb3dccfSVladimir Oltean 	int err;
542adb3dccfSVladimir Oltean 
543adb3dccfSVladimir Oltean 	switch (proto) {
5447c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
545adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
546c69f40acSVladimir Oltean 		err = felix_setup_tag_npi(ds, cpu);
547adb3dccfSVladimir Oltean 		break;
548e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
549c69f40acSVladimir Oltean 		err = felix_setup_tag_8021q(ds, cpu);
550e21268efSVladimir Oltean 		break;
551adb3dccfSVladimir Oltean 	default:
552adb3dccfSVladimir Oltean 		err = -EPROTONOSUPPORT;
553adb3dccfSVladimir Oltean 	}
554adb3dccfSVladimir Oltean 
555adb3dccfSVladimir Oltean 	return err;
556adb3dccfSVladimir Oltean }
557adb3dccfSVladimir Oltean 
558adb3dccfSVladimir Oltean static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu,
559adb3dccfSVladimir Oltean 				   enum dsa_tag_protocol proto)
560adb3dccfSVladimir Oltean {
561adb3dccfSVladimir Oltean 	switch (proto) {
5627c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
563adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
564adb3dccfSVladimir Oltean 		felix_teardown_tag_npi(ds, cpu);
565adb3dccfSVladimir Oltean 		break;
566e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
567e21268efSVladimir Oltean 		felix_teardown_tag_8021q(ds, cpu);
568e21268efSVladimir Oltean 		break;
569adb3dccfSVladimir Oltean 	default:
570adb3dccfSVladimir Oltean 		break;
571adb3dccfSVladimir Oltean 	}
572adb3dccfSVladimir Oltean }
573adb3dccfSVladimir Oltean 
574e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the
575e21268efSVladimir Oltean  * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
576e21268efSVladimir Oltean  * or the restoration is guaranteed to work.
577e21268efSVladimir Oltean  */
578adb3dccfSVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu,
579adb3dccfSVladimir Oltean 				     enum dsa_tag_protocol proto)
580adb3dccfSVladimir Oltean {
581adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
582adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
583adb3dccfSVladimir Oltean 	enum dsa_tag_protocol old_proto = felix->tag_proto;
58400fa91bcSVladimir Oltean 	bool cpu_port_active = false;
58500fa91bcSVladimir Oltean 	struct dsa_port *dp;
586adb3dccfSVladimir Oltean 	int err;
587adb3dccfSVladimir Oltean 
5887c4bb540SVladimir Oltean 	if (proto != DSA_TAG_PROTO_SEVILLE &&
5897c4bb540SVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT &&
590e21268efSVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT_8021Q)
591adb3dccfSVladimir Oltean 		return -EPROTONOSUPPORT;
592adb3dccfSVladimir Oltean 
59300fa91bcSVladimir Oltean 	/* We don't support multiple CPU ports, yet the DT blob may have
59400fa91bcSVladimir Oltean 	 * multiple CPU ports defined. The first CPU port is the active one,
59500fa91bcSVladimir Oltean 	 * the others are inactive. In this case, DSA will call
59600fa91bcSVladimir Oltean 	 * ->change_tag_protocol() multiple times, once per CPU port.
59700fa91bcSVladimir Oltean 	 * Since we implement the tagging protocol change towards "ocelot" or
59800fa91bcSVladimir Oltean 	 * "seville" as effectively initializing the NPI port, what we are
59900fa91bcSVladimir Oltean 	 * doing is effectively changing who the NPI port is to the last @cpu
60000fa91bcSVladimir Oltean 	 * argument passed, which is an unused DSA CPU port and not the one
60100fa91bcSVladimir Oltean 	 * that should actively pass traffic.
60200fa91bcSVladimir Oltean 	 * Suppress DSA's calls on CPU ports that are inactive.
60300fa91bcSVladimir Oltean 	 */
60400fa91bcSVladimir Oltean 	dsa_switch_for_each_user_port(dp, ds) {
60500fa91bcSVladimir Oltean 		if (dp->cpu_dp->index == cpu) {
60600fa91bcSVladimir Oltean 			cpu_port_active = true;
60700fa91bcSVladimir Oltean 			break;
60800fa91bcSVladimir Oltean 		}
60900fa91bcSVladimir Oltean 	}
61000fa91bcSVladimir Oltean 
61100fa91bcSVladimir Oltean 	if (!cpu_port_active)
61200fa91bcSVladimir Oltean 		return 0;
61300fa91bcSVladimir Oltean 
614adb3dccfSVladimir Oltean 	felix_del_tag_protocol(ds, cpu, old_proto);
615adb3dccfSVladimir Oltean 
616c69f40acSVladimir Oltean 	err = felix_set_tag_protocol(ds, cpu, proto);
617adb3dccfSVladimir Oltean 	if (err) {
618c69f40acSVladimir Oltean 		felix_set_tag_protocol(ds, cpu, old_proto);
619adb3dccfSVladimir Oltean 		return err;
620adb3dccfSVladimir Oltean 	}
621adb3dccfSVladimir Oltean 
622adb3dccfSVladimir Oltean 	felix->tag_proto = proto;
623adb3dccfSVladimir Oltean 
624adb3dccfSVladimir Oltean 	return 0;
625adb3dccfSVladimir Oltean }
626adb3dccfSVladimir Oltean 
62756051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
6284d776482SFlorian Fainelli 						    int port,
6294d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
63056051948SVladimir Oltean {
631adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
632adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
633adb3dccfSVladimir Oltean 
634adb3dccfSVladimir Oltean 	return felix->tag_proto;
63556051948SVladimir Oltean }
63656051948SVladimir Oltean 
637*72c3b0c7SVladimir Oltean static void felix_port_set_host_flood(struct dsa_switch *ds, int port,
638*72c3b0c7SVladimir Oltean 				      bool uc, bool mc)
639*72c3b0c7SVladimir Oltean {
640*72c3b0c7SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
641*72c3b0c7SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
642*72c3b0c7SVladimir Oltean 	unsigned long mask, val;
643*72c3b0c7SVladimir Oltean 
644*72c3b0c7SVladimir Oltean 	if (uc)
645*72c3b0c7SVladimir Oltean 		felix->host_flood_uc_mask |= BIT(port);
646*72c3b0c7SVladimir Oltean 	else
647*72c3b0c7SVladimir Oltean 		felix->host_flood_uc_mask &= ~BIT(port);
648*72c3b0c7SVladimir Oltean 
649*72c3b0c7SVladimir Oltean 	if (mc)
650*72c3b0c7SVladimir Oltean 		felix->host_flood_mc_mask |= BIT(port);
651*72c3b0c7SVladimir Oltean 	else
652*72c3b0c7SVladimir Oltean 		felix->host_flood_mc_mask &= ~BIT(port);
653*72c3b0c7SVladimir Oltean 
654*72c3b0c7SVladimir Oltean 	if (felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q)
655*72c3b0c7SVladimir Oltean 		mask = dsa_cpu_ports(ds);
656*72c3b0c7SVladimir Oltean 	else
657*72c3b0c7SVladimir Oltean 		mask = BIT(ocelot->num_phys_ports);
658*72c3b0c7SVladimir Oltean 
659*72c3b0c7SVladimir Oltean 	val = (felix->host_flood_uc_mask) ? mask : 0;
660*72c3b0c7SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_UC);
661*72c3b0c7SVladimir Oltean 
662*72c3b0c7SVladimir Oltean 	val = (felix->host_flood_mc_mask) ? mask : 0;
663*72c3b0c7SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MC);
664*72c3b0c7SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MCIPV4);
665*72c3b0c7SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MCIPV6);
666*72c3b0c7SVladimir Oltean }
667*72c3b0c7SVladimir Oltean 
66856051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
66956051948SVladimir Oltean 				 unsigned int ageing_time)
67056051948SVladimir Oltean {
67156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
67256051948SVladimir Oltean 
67356051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
67456051948SVladimir Oltean 
67556051948SVladimir Oltean 	return 0;
67656051948SVladimir Oltean }
67756051948SVladimir Oltean 
6785cad43a5SVladimir Oltean static void felix_port_fast_age(struct dsa_switch *ds, int port)
6795cad43a5SVladimir Oltean {
6805cad43a5SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
6815cad43a5SVladimir Oltean 	int err;
6825cad43a5SVladimir Oltean 
6835cad43a5SVladimir Oltean 	err = ocelot_mact_flush(ocelot, port);
6845cad43a5SVladimir Oltean 	if (err)
6855cad43a5SVladimir Oltean 		dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n",
6865cad43a5SVladimir Oltean 			port, ERR_PTR(err));
6875cad43a5SVladimir Oltean }
6885cad43a5SVladimir Oltean 
68956051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
69056051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
69156051948SVladimir Oltean {
69256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
69356051948SVladimir Oltean 
69456051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
69556051948SVladimir Oltean }
69656051948SVladimir Oltean 
69756051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
698c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
699c2693363SVladimir Oltean 			 struct dsa_db db)
70056051948SVladimir Oltean {
70154c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
702e9b3ba43SVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
70356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
70456051948SVladimir Oltean 
70554c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
70654c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
70754c31984SVladimir Oltean 
708e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp) && !bridge_dev &&
7097e580490SVladimir Oltean 	    dsa_fdb_present_in_other_db(ds, port, addr, vid, db))
7107e580490SVladimir Oltean 		return 0;
7117e580490SVladimir Oltean 
712e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp))
713e9b3ba43SVladimir Oltean 		port = PGID_CPU;
714e9b3ba43SVladimir Oltean 
71554c31984SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid, bridge_dev);
71656051948SVladimir Oltean }
71756051948SVladimir Oltean 
71856051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
719c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
720c2693363SVladimir Oltean 			 struct dsa_db db)
72156051948SVladimir Oltean {
72254c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
723e9b3ba43SVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
72456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
72556051948SVladimir Oltean 
72654c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
72754c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
72854c31984SVladimir Oltean 
729e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp) && !bridge_dev &&
7307e580490SVladimir Oltean 	    dsa_fdb_present_in_other_db(ds, port, addr, vid, db))
7317e580490SVladimir Oltean 		return 0;
7327e580490SVladimir Oltean 
733e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp))
734e9b3ba43SVladimir Oltean 		port = PGID_CPU;
735e9b3ba43SVladimir Oltean 
73654c31984SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid, bridge_dev);
73756051948SVladimir Oltean }
73856051948SVladimir Oltean 
739961d8b69SVladimir Oltean static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag,
740c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
741c2693363SVladimir Oltean 			     struct dsa_db db)
742961d8b69SVladimir Oltean {
74354c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
744961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
745961d8b69SVladimir Oltean 
74654c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
74754c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
74854c31984SVladimir Oltean 
74954c31984SVladimir Oltean 	return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid, bridge_dev);
750961d8b69SVladimir Oltean }
751961d8b69SVladimir Oltean 
752961d8b69SVladimir Oltean static int felix_lag_fdb_del(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_del(ocelot, lag.dev, addr, vid, bridge_dev);
763961d8b69SVladimir Oltean }
764961d8b69SVladimir Oltean 
765a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port,
766c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
767c2693363SVladimir Oltean 			 struct dsa_db db)
768209edf95SVladimir Oltean {
76954c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
770209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
771209edf95SVladimir Oltean 
77254c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
77354c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
77454c31984SVladimir Oltean 
7757e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
7767e580490SVladimir Oltean 	    dsa_mdb_present_in_other_db(ds, port, mdb, db))
7777e580490SVladimir Oltean 		return 0;
7787e580490SVladimir Oltean 
7790ddf83cdSVladimir Oltean 	if (port == ocelot->npi)
7800ddf83cdSVladimir Oltean 		port = ocelot->num_phys_ports;
7810ddf83cdSVladimir Oltean 
78254c31984SVladimir Oltean 	return ocelot_port_mdb_add(ocelot, port, mdb, bridge_dev);
783209edf95SVladimir Oltean }
784209edf95SVladimir Oltean 
785209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port,
786c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
787c2693363SVladimir Oltean 			 struct dsa_db db)
788209edf95SVladimir Oltean {
78954c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
790209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
791209edf95SVladimir Oltean 
79254c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
79354c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
79454c31984SVladimir Oltean 
7957e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
7967e580490SVladimir Oltean 	    dsa_mdb_present_in_other_db(ds, port, mdb, db))
7977e580490SVladimir Oltean 		return 0;
7987e580490SVladimir Oltean 
7990ddf83cdSVladimir Oltean 	if (port == ocelot->npi)
8000ddf83cdSVladimir Oltean 		port = ocelot->num_phys_ports;
8010ddf83cdSVladimir Oltean 
80254c31984SVladimir Oltean 	return ocelot_port_mdb_del(ocelot, port, mdb, bridge_dev);
803209edf95SVladimir Oltean }
804209edf95SVladimir Oltean 
80556051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
80656051948SVladimir Oltean 				       u8 state)
80756051948SVladimir Oltean {
80856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
80956051948SVladimir Oltean 
81056051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
81156051948SVladimir Oltean }
81256051948SVladimir Oltean 
813421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port,
814421741eaSVladimir Oltean 				  struct switchdev_brport_flags val,
815421741eaSVladimir Oltean 				  struct netlink_ext_ack *extack)
816421741eaSVladimir Oltean {
817421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
818421741eaSVladimir Oltean 
819421741eaSVladimir Oltean 	return ocelot_port_pre_bridge_flags(ocelot, port, val);
820421741eaSVladimir Oltean }
821421741eaSVladimir Oltean 
822421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port,
823421741eaSVladimir Oltean 			      struct switchdev_brport_flags val,
824421741eaSVladimir Oltean 			      struct netlink_ext_ack *extack)
825421741eaSVladimir Oltean {
826421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
827421741eaSVladimir Oltean 
828910ee6ccSVladimir Oltean 	if (port == ocelot->npi)
829910ee6ccSVladimir Oltean 		port = ocelot->num_phys_ports;
830910ee6ccSVladimir Oltean 
831421741eaSVladimir Oltean 	ocelot_port_bridge_flags(ocelot, port, val);
832421741eaSVladimir Oltean 
833421741eaSVladimir Oltean 	return 0;
834421741eaSVladimir Oltean }
835421741eaSVladimir Oltean 
83656051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
83706b9cce4SVladimir Oltean 			     struct dsa_bridge bridge, bool *tx_fwd_offload,
83806b9cce4SVladimir Oltean 			     struct netlink_ext_ack *extack)
83956051948SVladimir Oltean {
84056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
84156051948SVladimir Oltean 
84254c31984SVladimir Oltean 	return ocelot_port_bridge_join(ocelot, port, bridge.dev, bridge.num,
84354c31984SVladimir Oltean 				       extack);
84456051948SVladimir Oltean }
84556051948SVladimir Oltean 
84656051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
847d3eed0e5SVladimir Oltean 			       struct dsa_bridge bridge)
84856051948SVladimir Oltean {
84956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
85056051948SVladimir Oltean 
851d3eed0e5SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, bridge.dev);
85256051948SVladimir Oltean }
85356051948SVladimir Oltean 
8548fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port,
855dedd6a00SVladimir Oltean 			  struct dsa_lag lag,
8568fe6832eSVladimir Oltean 			  struct netdev_lag_upper_info *info)
8578fe6832eSVladimir Oltean {
8588fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8598fe6832eSVladimir Oltean 
860dedd6a00SVladimir Oltean 	return ocelot_port_lag_join(ocelot, port, lag.dev, info);
8618fe6832eSVladimir Oltean }
8628fe6832eSVladimir Oltean 
8638fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port,
864dedd6a00SVladimir Oltean 			   struct dsa_lag lag)
8658fe6832eSVladimir Oltean {
8668fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8678fe6832eSVladimir Oltean 
868dedd6a00SVladimir Oltean 	ocelot_port_lag_leave(ocelot, port, lag.dev);
8698fe6832eSVladimir Oltean 
8708fe6832eSVladimir Oltean 	return 0;
8718fe6832eSVladimir Oltean }
8728fe6832eSVladimir Oltean 
8738fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port)
8748fe6832eSVladimir Oltean {
8758fe6832eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
8768fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8778fe6832eSVladimir Oltean 
8788fe6832eSVladimir Oltean 	ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
8798fe6832eSVladimir Oltean 
8808fe6832eSVladimir Oltean 	return 0;
8818fe6832eSVladimir Oltean }
8828fe6832eSVladimir Oltean 
88356051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
88401af940eSVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan,
88501af940eSVladimir Oltean 			      struct netlink_ext_ack *extack)
88656051948SVladimir Oltean {
8872f0402feSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
888b7a9e0daSVladimir Oltean 	u16 flags = vlan->flags;
8892f0402feSVladimir Oltean 
8909a720680SVladimir Oltean 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
8919a720680SVladimir Oltean 	 * egress-untagged or not, pvid or not, make no difference. This
8929a720680SVladimir Oltean 	 * behavior is already better than what DSA just tries to approximate
8939a720680SVladimir Oltean 	 * when it installs the VLAN with the same flags on the CPU port.
8949a720680SVladimir Oltean 	 * Just accept any configuration, and don't let ocelot deny installing
8959a720680SVladimir Oltean 	 * multiple native VLANs on the NPI port, because the switch doesn't
8969a720680SVladimir Oltean 	 * look at the port tag settings towards the NPI interface anyway.
8979a720680SVladimir Oltean 	 */
8989a720680SVladimir Oltean 	if (port == ocelot->npi)
8999a720680SVladimir Oltean 		return 0;
9009a720680SVladimir Oltean 
901b7a9e0daSVladimir Oltean 	return ocelot_vlan_prepare(ocelot, port, vlan->vid,
9022f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_PVID,
90301af940eSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_UNTAGGED,
90401af940eSVladimir Oltean 				   extack);
90556051948SVladimir Oltean }
90656051948SVladimir Oltean 
90789153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
90889153ed6SVladimir Oltean 				struct netlink_ext_ack *extack)
90956051948SVladimir Oltean {
91056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
91156051948SVladimir Oltean 
9123b95d1b2SVladimir Oltean 	return ocelot_port_vlan_filtering(ocelot, port, enabled, extack);
91356051948SVladimir Oltean }
91456051948SVladimir Oltean 
9151958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port,
91631046a5fSVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan,
91731046a5fSVladimir Oltean 			  struct netlink_ext_ack *extack)
91856051948SVladimir Oltean {
91956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
920183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
9211958d581SVladimir Oltean 	int err;
92256051948SVladimir Oltean 
92301af940eSVladimir Oltean 	err = felix_vlan_prepare(ds, port, vlan, extack);
9241958d581SVladimir Oltean 	if (err)
9251958d581SVladimir Oltean 		return err;
9261958d581SVladimir Oltean 
9271958d581SVladimir Oltean 	return ocelot_vlan_add(ocelot, port, vlan->vid,
928183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_PVID,
929183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_UNTAGGED);
93056051948SVladimir Oltean }
93156051948SVladimir Oltean 
93256051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
93356051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
93456051948SVladimir Oltean {
93556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
93656051948SVladimir Oltean 
937b7a9e0daSVladimir Oltean 	return ocelot_vlan_del(ocelot, port, vlan->vid);
93856051948SVladimir Oltean }
93956051948SVladimir Oltean 
94079fda660SRussell King (Oracle) static void felix_phylink_get_caps(struct dsa_switch *ds, int port,
94179fda660SRussell King (Oracle) 				   struct phylink_config *config)
94279fda660SRussell King (Oracle) {
94379fda660SRussell King (Oracle) 	struct ocelot *ocelot = ds->priv;
94479fda660SRussell King (Oracle) 
945f6f04c02SRussell King (Oracle) 	/* This driver does not make use of the speed, duplex, pause or the
946f6f04c02SRussell King (Oracle) 	 * advertisement in its mac_config, so it is safe to mark this driver
947f6f04c02SRussell King (Oracle) 	 * as non-legacy.
948f6f04c02SRussell King (Oracle) 	 */
949f6f04c02SRussell King (Oracle) 	config->legacy_pre_march2020 = false;
950f6f04c02SRussell King (Oracle) 
95179fda660SRussell King (Oracle) 	__set_bit(ocelot->ports[port]->phy_mode,
95279fda660SRussell King (Oracle) 		  config->supported_interfaces);
95379fda660SRussell King (Oracle) }
95479fda660SRussell King (Oracle) 
955bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
956bdeced75SVladimir Oltean 				   unsigned long *supported,
957bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
958bdeced75SVladimir Oltean {
959bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
960375e1314SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
961bdeced75SVladimir Oltean 
962375e1314SVladimir Oltean 	if (felix->info->phylink_validate)
963375e1314SVladimir Oltean 		felix->info->phylink_validate(ocelot, port, supported, state);
964bdeced75SVladimir Oltean }
965bdeced75SVladimir Oltean 
966864ba485SRussell King (Oracle) static struct phylink_pcs *felix_phylink_mac_select_pcs(struct dsa_switch *ds,
967864ba485SRussell King (Oracle) 							int port,
968864ba485SRussell King (Oracle) 							phy_interface_t iface)
969bdeced75SVladimir Oltean {
970bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
971bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
972864ba485SRussell King (Oracle) 	struct phylink_pcs *pcs = NULL;
973bdeced75SVladimir Oltean 
97449af6a76SColin Foster 	if (felix->pcs && felix->pcs[port])
975864ba485SRussell King (Oracle) 		pcs = felix->pcs[port];
976864ba485SRussell King (Oracle) 
977864ba485SRussell King (Oracle) 	return pcs;
978bdeced75SVladimir Oltean }
979bdeced75SVladimir Oltean 
980bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
981bdeced75SVladimir Oltean 					unsigned int link_an_mode,
982bdeced75SVladimir Oltean 					phy_interface_t interface)
983bdeced75SVladimir Oltean {
984bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
985bdeced75SVladimir Oltean 
986e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
987e6e12df6SVladimir Oltean 				     FELIX_MAC_QUIRKS);
988bdeced75SVladimir Oltean }
989bdeced75SVladimir Oltean 
990bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
991bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
992bdeced75SVladimir Oltean 				      phy_interface_t interface,
9935b502a7bSRussell King 				      struct phy_device *phydev,
9945b502a7bSRussell King 				      int speed, int duplex,
9955b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
996bdeced75SVladimir Oltean {
997bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
9987e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
999bdeced75SVladimir Oltean 
1000e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
1001e6e12df6SVladimir Oltean 				   interface, speed, duplex, tx_pause, rx_pause,
1002e6e12df6SVladimir Oltean 				   FELIX_MAC_QUIRKS);
10037e14a2dcSVladimir Oltean 
10047e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
10057e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
1006bdeced75SVladimir Oltean }
1007bdeced75SVladimir Oltean 
1008bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
1009bd2b3161SXiaoliang Yang {
1010bd2b3161SXiaoliang Yang 	int i;
1011bd2b3161SXiaoliang Yang 
1012bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
1013bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1014bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1015bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
1016bd2b3161SXiaoliang Yang 		       port);
1017bd2b3161SXiaoliang Yang 
101870d39a6eSVladimir Oltean 	for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
1019bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
1020bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
1021bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
1022bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
1023bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
1024bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
1025bd2b3161SXiaoliang Yang 			      port, i);
1026bd2b3161SXiaoliang Yang 	}
1027bd2b3161SXiaoliang Yang }
1028bd2b3161SXiaoliang Yang 
102956051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
103056051948SVladimir Oltean 			      u32 stringset, u8 *data)
103156051948SVladimir Oltean {
103256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
103356051948SVladimir Oltean 
103456051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
103556051948SVladimir Oltean }
103656051948SVladimir Oltean 
103756051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
103856051948SVladimir Oltean {
103956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
104056051948SVladimir Oltean 
104156051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
104256051948SVladimir Oltean }
104356051948SVladimir Oltean 
104456051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
104556051948SVladimir Oltean {
104656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
104756051948SVladimir Oltean 
104856051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
104956051948SVladimir Oltean }
105056051948SVladimir Oltean 
105156051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
105256051948SVladimir Oltean 			     struct ethtool_ts_info *info)
105356051948SVladimir Oltean {
105456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
105556051948SVladimir Oltean 
105656051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
105756051948SVladimir Oltean }
105856051948SVladimir Oltean 
1059acf242fcSColin Foster static const u32 felix_phy_match_table[PHY_INTERFACE_MODE_MAX] = {
1060acf242fcSColin Foster 	[PHY_INTERFACE_MODE_INTERNAL] = OCELOT_PORT_MODE_INTERNAL,
1061acf242fcSColin Foster 	[PHY_INTERFACE_MODE_SGMII] = OCELOT_PORT_MODE_SGMII,
1062acf242fcSColin Foster 	[PHY_INTERFACE_MODE_QSGMII] = OCELOT_PORT_MODE_QSGMII,
1063acf242fcSColin Foster 	[PHY_INTERFACE_MODE_USXGMII] = OCELOT_PORT_MODE_USXGMII,
106411ecf341SVladimir Oltean 	[PHY_INTERFACE_MODE_1000BASEX] = OCELOT_PORT_MODE_1000BASEX,
1065acf242fcSColin Foster 	[PHY_INTERFACE_MODE_2500BASEX] = OCELOT_PORT_MODE_2500BASEX,
1066acf242fcSColin Foster };
1067acf242fcSColin Foster 
1068acf242fcSColin Foster static int felix_validate_phy_mode(struct felix *felix, int port,
1069acf242fcSColin Foster 				   phy_interface_t phy_mode)
1070acf242fcSColin Foster {
1071acf242fcSColin Foster 	u32 modes = felix->info->port_modes[port];
1072acf242fcSColin Foster 
1073acf242fcSColin Foster 	if (felix_phy_match_table[phy_mode] & modes)
1074acf242fcSColin Foster 		return 0;
1075acf242fcSColin Foster 	return -EOPNOTSUPP;
1076acf242fcSColin Foster }
1077acf242fcSColin Foster 
1078bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
1079bdeced75SVladimir Oltean 				  struct device_node *ports_node,
1080bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
1081bdeced75SVladimir Oltean {
1082bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1083bdeced75SVladimir Oltean 	struct device_node *child;
1084bdeced75SVladimir Oltean 
108537fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
1086bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
1087bdeced75SVladimir Oltean 		u32 port;
1088bdeced75SVladimir Oltean 		int err;
1089bdeced75SVladimir Oltean 
1090bdeced75SVladimir Oltean 		/* Get switch port number from DT */
1091bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
1092bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
1093bdeced75SVladimir Oltean 				"(property \"reg\")\n");
1094bdeced75SVladimir Oltean 			of_node_put(child);
1095bdeced75SVladimir Oltean 			return -ENODEV;
1096bdeced75SVladimir Oltean 		}
1097bdeced75SVladimir Oltean 
1098bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
1099bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
1100bdeced75SVladimir Oltean 		if (err) {
1101bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
1102bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
1103bdeced75SVladimir Oltean 				port);
1104bdeced75SVladimir Oltean 			of_node_put(child);
1105bdeced75SVladimir Oltean 			return -ENODEV;
1106bdeced75SVladimir Oltean 		}
1107bdeced75SVladimir Oltean 
1108acf242fcSColin Foster 		err = felix_validate_phy_mode(felix, port, phy_mode);
1109bdeced75SVladimir Oltean 		if (err < 0) {
1110bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
1111bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
111259ebb430SSumera Priyadarsini 			of_node_put(child);
1113bdeced75SVladimir Oltean 			return err;
1114bdeced75SVladimir Oltean 		}
1115bdeced75SVladimir Oltean 
1116bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
1117bdeced75SVladimir Oltean 	}
1118bdeced75SVladimir Oltean 
1119bdeced75SVladimir Oltean 	return 0;
1120bdeced75SVladimir Oltean }
1121bdeced75SVladimir Oltean 
1122bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
1123bdeced75SVladimir Oltean {
1124bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1125bdeced75SVladimir Oltean 	struct device_node *switch_node;
1126bdeced75SVladimir Oltean 	struct device_node *ports_node;
1127bdeced75SVladimir Oltean 	int err;
1128bdeced75SVladimir Oltean 
1129bdeced75SVladimir Oltean 	switch_node = dev->of_node;
1130bdeced75SVladimir Oltean 
1131bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
1132abecbfcdSVladimir Oltean 	if (!ports_node)
1133abecbfcdSVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
1134bdeced75SVladimir Oltean 	if (!ports_node) {
1135abecbfcdSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n");
1136bdeced75SVladimir Oltean 		return -ENODEV;
1137bdeced75SVladimir Oltean 	}
1138bdeced75SVladimir Oltean 
1139bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
1140bdeced75SVladimir Oltean 	of_node_put(ports_node);
1141bdeced75SVladimir Oltean 
1142bdeced75SVladimir Oltean 	return err;
1143bdeced75SVladimir Oltean }
1144bdeced75SVladimir Oltean 
114556051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
114656051948SVladimir Oltean {
114756051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
1148bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
1149b4024c9eSClaudiu Manoil 	struct resource res;
115056051948SVladimir Oltean 	int port, i, err;
115156051948SVladimir Oltean 
115256051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
115356051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
115456051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
115556051948SVladimir Oltean 	if (!ocelot->ports)
115656051948SVladimir Oltean 		return -ENOMEM;
115756051948SVladimir Oltean 
115856051948SVladimir Oltean 	ocelot->map		= felix->info->map;
115956051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
116021ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
116107d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
116277043c37SXiaoliang Yang 	ocelot->vcap_pol.base	= felix->info->vcap_pol_base;
116377043c37SXiaoliang Yang 	ocelot->vcap_pol.max	= felix->info->vcap_pol_max;
116477043c37SXiaoliang Yang 	ocelot->vcap_pol.base2	= felix->info->vcap_pol_base2;
116577043c37SXiaoliang Yang 	ocelot->vcap_pol.max2	= felix->info->vcap_pol_max2;
116656051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
1167cacea62fSVladimir Oltean 	ocelot->npi_inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
1168cacea62fSVladimir Oltean 	ocelot->npi_xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
1169f59fd9caSVladimir Oltean 	ocelot->devlink		= felix->ds->devlink;
117056051948SVladimir Oltean 
1171bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
1172bdeced75SVladimir Oltean 				 GFP_KERNEL);
1173bdeced75SVladimir Oltean 	if (!port_phy_modes)
1174bdeced75SVladimir Oltean 		return -ENOMEM;
1175bdeced75SVladimir Oltean 
1176bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
1177bdeced75SVladimir Oltean 	if (err) {
1178bdeced75SVladimir Oltean 		kfree(port_phy_modes);
1179bdeced75SVladimir Oltean 		return err;
1180bdeced75SVladimir Oltean 	}
1181bdeced75SVladimir Oltean 
118256051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
118356051948SVladimir Oltean 		struct regmap *target;
118456051948SVladimir Oltean 
118556051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
118656051948SVladimir Oltean 			continue;
118756051948SVladimir Oltean 
1188b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
1189b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1190375e1314SVladimir Oltean 		res.start += felix->switch_base;
1191375e1314SVladimir Oltean 		res.end += felix->switch_base;
119256051948SVladimir Oltean 
1193242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
119456051948SVladimir Oltean 		if (IS_ERR(target)) {
119556051948SVladimir Oltean 			dev_err(ocelot->dev,
119656051948SVladimir Oltean 				"Failed to map device memory space\n");
1197bdeced75SVladimir Oltean 			kfree(port_phy_modes);
119856051948SVladimir Oltean 			return PTR_ERR(target);
119956051948SVladimir Oltean 		}
120056051948SVladimir Oltean 
120156051948SVladimir Oltean 		ocelot->targets[i] = target;
120256051948SVladimir Oltean 	}
120356051948SVladimir Oltean 
120456051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
120556051948SVladimir Oltean 	if (err) {
120656051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
1207bdeced75SVladimir Oltean 		kfree(port_phy_modes);
120856051948SVladimir Oltean 		return err;
120956051948SVladimir Oltean 	}
121056051948SVladimir Oltean 
121156051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
121256051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
121391c724cfSVladimir Oltean 		struct regmap *target;
121456051948SVladimir Oltean 
121556051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
121656051948SVladimir Oltean 					   sizeof(struct ocelot_port),
121756051948SVladimir Oltean 					   GFP_KERNEL);
121856051948SVladimir Oltean 		if (!ocelot_port) {
121956051948SVladimir Oltean 			dev_err(ocelot->dev,
122056051948SVladimir Oltean 				"failed to allocate port memory\n");
1221bdeced75SVladimir Oltean 			kfree(port_phy_modes);
122256051948SVladimir Oltean 			return -ENOMEM;
122356051948SVladimir Oltean 		}
122456051948SVladimir Oltean 
1225b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
1226b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1227375e1314SVladimir Oltean 		res.start += felix->switch_base;
1228375e1314SVladimir Oltean 		res.end += felix->switch_base;
122956051948SVladimir Oltean 
1230242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
123191c724cfSVladimir Oltean 		if (IS_ERR(target)) {
123256051948SVladimir Oltean 			dev_err(ocelot->dev,
123391c724cfSVladimir Oltean 				"Failed to map memory space for port %d\n",
123491c724cfSVladimir Oltean 				port);
1235bdeced75SVladimir Oltean 			kfree(port_phy_modes);
123691c724cfSVladimir Oltean 			return PTR_ERR(target);
123756051948SVladimir Oltean 		}
123856051948SVladimir Oltean 
1239bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
124056051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
124191c724cfSVladimir Oltean 		ocelot_port->target = target;
124256051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
124356051948SVladimir Oltean 	}
124456051948SVladimir Oltean 
1245bdeced75SVladimir Oltean 	kfree(port_phy_modes);
1246bdeced75SVladimir Oltean 
1247bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
1248bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
1249bdeced75SVladimir Oltean 		if (err < 0)
1250bdeced75SVladimir Oltean 			return err;
1251bdeced75SVladimir Oltean 	}
1252bdeced75SVladimir Oltean 
125356051948SVladimir Oltean 	return 0;
125456051948SVladimir Oltean }
125556051948SVladimir Oltean 
12561328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port,
12571328a883SVladimir Oltean 					   struct sk_buff *skb)
12581328a883SVladimir Oltean {
12591328a883SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
12601328a883SVladimir Oltean 	struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
12611328a883SVladimir Oltean 	struct sk_buff *skb_match = NULL, *skb_tmp;
12621328a883SVladimir Oltean 	unsigned long flags;
12631328a883SVladimir Oltean 
12641328a883SVladimir Oltean 	if (!clone)
12651328a883SVladimir Oltean 		return;
12661328a883SVladimir Oltean 
12671328a883SVladimir Oltean 	spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags);
12681328a883SVladimir Oltean 
12691328a883SVladimir Oltean 	skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) {
12701328a883SVladimir Oltean 		if (skb != clone)
12711328a883SVladimir Oltean 			continue;
12721328a883SVladimir Oltean 		__skb_unlink(skb, &ocelot_port->tx_skbs);
12731328a883SVladimir Oltean 		skb_match = skb;
12741328a883SVladimir Oltean 		break;
12751328a883SVladimir Oltean 	}
12761328a883SVladimir Oltean 
12771328a883SVladimir Oltean 	spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags);
12781328a883SVladimir Oltean 
12791328a883SVladimir Oltean 	WARN_ONCE(!skb_match,
12801328a883SVladimir Oltean 		  "Could not find skb clone in TX timestamping list\n");
12811328a883SVladimir Oltean }
12821328a883SVladimir Oltean 
128349f885b2SVladimir Oltean #define work_to_xmit_work(w) \
128449f885b2SVladimir Oltean 		container_of((w), struct felix_deferred_xmit_work, work)
128549f885b2SVladimir Oltean 
128649f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work)
128749f885b2SVladimir Oltean {
128849f885b2SVladimir Oltean 	struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
128949f885b2SVladimir Oltean 	struct dsa_switch *ds = xmit_work->dp->ds;
129049f885b2SVladimir Oltean 	struct sk_buff *skb = xmit_work->skb;
129149f885b2SVladimir Oltean 	u32 rew_op = ocelot_ptp_rew_op(skb);
129249f885b2SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
129349f885b2SVladimir Oltean 	int port = xmit_work->dp->index;
129449f885b2SVladimir Oltean 	int retries = 10;
129549f885b2SVladimir Oltean 
129649f885b2SVladimir Oltean 	do {
129749f885b2SVladimir Oltean 		if (ocelot_can_inject(ocelot, 0))
129849f885b2SVladimir Oltean 			break;
129949f885b2SVladimir Oltean 
130049f885b2SVladimir Oltean 		cpu_relax();
130149f885b2SVladimir Oltean 	} while (--retries);
130249f885b2SVladimir Oltean 
130349f885b2SVladimir Oltean 	if (!retries) {
130449f885b2SVladimir Oltean 		dev_err(ocelot->dev, "port %d failed to inject skb\n",
130549f885b2SVladimir Oltean 			port);
13061328a883SVladimir Oltean 		ocelot_port_purge_txtstamp_skb(ocelot, port, skb);
130749f885b2SVladimir Oltean 		kfree_skb(skb);
130849f885b2SVladimir Oltean 		return;
130949f885b2SVladimir Oltean 	}
131049f885b2SVladimir Oltean 
131149f885b2SVladimir Oltean 	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
131249f885b2SVladimir Oltean 
131349f885b2SVladimir Oltean 	consume_skb(skb);
131449f885b2SVladimir Oltean 	kfree(xmit_work);
131549f885b2SVladimir Oltean }
131649f885b2SVladimir Oltean 
131735d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds,
131835d97680SVladimir Oltean 				      enum dsa_tag_protocol proto)
131949f885b2SVladimir Oltean {
132035d97680SVladimir Oltean 	struct ocelot_8021q_tagger_data *tagger_data;
132149f885b2SVladimir Oltean 
132235d97680SVladimir Oltean 	switch (proto) {
132335d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
132435d97680SVladimir Oltean 		tagger_data = ocelot_8021q_tagger_data(ds);
132535d97680SVladimir Oltean 		tagger_data->xmit_work_fn = felix_port_deferred_xmit;
132649f885b2SVladimir Oltean 		return 0;
132735d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
132835d97680SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
132949f885b2SVladimir Oltean 		return 0;
133035d97680SVladimir Oltean 	default:
133135d97680SVladimir Oltean 		return -EPROTONOSUPPORT;
133249f885b2SVladimir Oltean 	}
133349f885b2SVladimir Oltean }
133449f885b2SVladimir Oltean 
133556051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
133656051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
133756051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
133856051948SVladimir Oltean  * (which will not work).
133956051948SVladimir Oltean  */
134056051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
134156051948SVladimir Oltean {
134256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
134356051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1344f2e2662cSVladimir Oltean 	unsigned long cpu_flood;
13452960bb14SVladimir Oltean 	struct dsa_port *dp;
13462960bb14SVladimir Oltean 	int err;
134756051948SVladimir Oltean 
134856051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
134956051948SVladimir Oltean 	if (err)
135056051948SVladimir Oltean 		return err;
135156051948SVladimir Oltean 
1352d1cc0e93SVladimir Oltean 	err = ocelot_init(ocelot);
1353d1cc0e93SVladimir Oltean 	if (err)
13546b73b7c9SVladimir Oltean 		goto out_mdiobus_free;
1355d1cc0e93SVladimir Oltean 
13562b49d128SYangbo Lu 	if (ocelot->ptp) {
13572ac7c6c5SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
13582b49d128SYangbo Lu 		if (err) {
13592b49d128SYangbo Lu 			dev_err(ocelot->dev,
13602b49d128SYangbo Lu 				"Timestamp initialization failed\n");
13612b49d128SYangbo Lu 			ocelot->ptp = 0;
13622b49d128SYangbo Lu 		}
13632b49d128SYangbo Lu 	}
136456051948SVladimir Oltean 
13652960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
13662960bb14SVladimir Oltean 		ocelot_init_port(ocelot, dp->index);
1367bd2b3161SXiaoliang Yang 
1368bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
1369bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
1370bd2b3161SXiaoliang Yang 		 */
13712960bb14SVladimir Oltean 		felix_port_qos_map_init(ocelot, dp->index);
137256051948SVladimir Oltean 	}
137356051948SVladimir Oltean 
1374f59fd9caSVladimir Oltean 	err = ocelot_devlink_sb_register(ocelot);
1375f59fd9caSVladimir Oltean 	if (err)
13766b73b7c9SVladimir Oltean 		goto out_deinit_ports;
1377f59fd9caSVladimir Oltean 
13782960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
1379adb3dccfSVladimir Oltean 		/* The initial tag protocol is NPI which always returns 0, so
1380adb3dccfSVladimir Oltean 		 * there's no real point in checking for errors.
13811cf3299bSVladimir Oltean 		 */
1382c69f40acSVladimir Oltean 		felix_set_tag_protocol(ds, dp->index, felix->tag_proto);
1383f2e2662cSVladimir Oltean 
1384f2e2662cSVladimir Oltean 		/* Start off with flooding disabled towards the NPI port
1385f2e2662cSVladimir Oltean 		 * (actually CPU port module).
1386f2e2662cSVladimir Oltean 		 */
1387f2e2662cSVladimir Oltean 		cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
1388f2e2662cSVladimir Oltean 		ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_UC);
1389f2e2662cSVladimir Oltean 		ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_MC);
1390f2e2662cSVladimir Oltean 
13918d5f7954SVladimir Oltean 		break;
1392adb3dccfSVladimir Oltean 	}
13931cf3299bSVladimir Oltean 
13940b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
1395c54913c1SVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
139654c31984SVladimir Oltean 	ds->fdb_isolation = true;
139754c31984SVladimir Oltean 	ds->max_num_bridges = ds->num_ports;
1398bdeced75SVladimir Oltean 
139956051948SVladimir Oltean 	return 0;
14006b73b7c9SVladimir Oltean 
14016b73b7c9SVladimir Oltean out_deinit_ports:
14022960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
14032960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
14046b73b7c9SVladimir Oltean 
14056b73b7c9SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
14066b73b7c9SVladimir Oltean 	ocelot_deinit(ocelot);
14076b73b7c9SVladimir Oltean 
14086b73b7c9SVladimir Oltean out_mdiobus_free:
14096b73b7c9SVladimir Oltean 	if (felix->info->mdio_bus_free)
14106b73b7c9SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
14116b73b7c9SVladimir Oltean 
14126b73b7c9SVladimir Oltean 	return err;
141356051948SVladimir Oltean }
141456051948SVladimir Oltean 
141556051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
141656051948SVladimir Oltean {
141756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1418bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
14192960bb14SVladimir Oltean 	struct dsa_port *dp;
1420bdeced75SVladimir Oltean 
14212960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
14222960bb14SVladimir Oltean 		felix_del_tag_protocol(ds, dp->index, felix->tag_proto);
14238d5f7954SVladimir Oltean 		break;
1424adb3dccfSVladimir Oltean 	}
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,
1910*72c3b0c7SVladimir 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