xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision c352e5e8e8f2888c99ee164632af365157da2a41)
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 
316*c352e5e8SVladimir Oltean static int felix_trap_get_cpu_port(struct dsa_switch *ds,
317*c352e5e8SVladimir Oltean 				   const struct ocelot_vcap_filter *trap)
318*c352e5e8SVladimir Oltean {
319*c352e5e8SVladimir Oltean 	struct dsa_port *dp;
320*c352e5e8SVladimir Oltean 	int first_port;
321*c352e5e8SVladimir Oltean 
322*c352e5e8SVladimir Oltean 	if (WARN_ON(!trap->ingress_port_mask))
323*c352e5e8SVladimir Oltean 		return -1;
324*c352e5e8SVladimir Oltean 
325*c352e5e8SVladimir Oltean 	first_port = __ffs(trap->ingress_port_mask);
326*c352e5e8SVladimir Oltean 	dp = dsa_to_port(ds, first_port);
327*c352e5e8SVladimir Oltean 
328*c352e5e8SVladimir Oltean 	return dp->cpu_dp->index;
329*c352e5e8SVladimir Oltean }
330*c352e5e8SVladimir Oltean 
33199348004SVladimir Oltean /* On switches with no extraction IRQ wired, trapped packets need to be
33299348004SVladimir Oltean  * replicated over Ethernet as well, otherwise we'd get no notification of
33399348004SVladimir Oltean  * their arrival when using the ocelot-8021q tagging protocol.
334c8c0ba4fSVladimir Oltean  */
33599348004SVladimir Oltean static int felix_update_trapping_destinations(struct dsa_switch *ds,
33699348004SVladimir Oltean 					      bool using_tag_8021q)
337c8c0ba4fSVladimir Oltean {
33899348004SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
33999348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
340e1846cffSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
34199348004SVladimir Oltean 	struct ocelot_vcap_filter *trap;
34299348004SVladimir Oltean 	enum ocelot_mask_mode mask_mode;
34399348004SVladimir Oltean 	unsigned long port_mask;
34499348004SVladimir Oltean 	bool cpu_copy_ena;
345*c352e5e8SVladimir Oltean 	int err;
346c8c0ba4fSVladimir Oltean 
34799348004SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
34899348004SVladimir Oltean 		return 0;
349c8c0ba4fSVladimir Oltean 
350d78637a8SVladimir Oltean 	/* We are sure that "cpu" was found, otherwise
351d78637a8SVladimir Oltean 	 * dsa_tree_setup_default_cpu() would have failed earlier.
352d78637a8SVladimir Oltean 	 */
353e1846cffSVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
354c8c0ba4fSVladimir Oltean 
35599348004SVladimir Oltean 	/* Make sure all traps are set up for that destination */
356e1846cffSVladimir Oltean 	list_for_each_entry(trap, &block_vcap_is2->rules, list) {
357e1846cffSVladimir Oltean 		if (!trap->is_trap)
358e1846cffSVladimir Oltean 			continue;
359e1846cffSVladimir Oltean 
36099348004SVladimir Oltean 		/* Figure out the current trapping destination */
36199348004SVladimir Oltean 		if (using_tag_8021q) {
36299348004SVladimir Oltean 			/* Redirect to the tag_8021q CPU port. If timestamps
36399348004SVladimir Oltean 			 * are necessary, also copy trapped packets to the CPU
36499348004SVladimir Oltean 			 * port module.
365c8c0ba4fSVladimir Oltean 			 */
36699348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_REDIRECT;
367*c352e5e8SVladimir Oltean 			port_mask = BIT(felix_trap_get_cpu_port(ds, trap));
36899348004SVladimir Oltean 			cpu_copy_ena = !!trap->take_ts;
369c8c0ba4fSVladimir Oltean 		} else {
37099348004SVladimir Oltean 			/* Trap packets only to the CPU port module, which is
37199348004SVladimir Oltean 			 * redirected to the NPI port (the DSA CPU port)
372c8c0ba4fSVladimir Oltean 			 */
37399348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
37499348004SVladimir Oltean 			port_mask = 0;
37599348004SVladimir Oltean 			cpu_copy_ena = true;
376c8c0ba4fSVladimir Oltean 		}
377c8c0ba4fSVladimir Oltean 
37899348004SVladimir Oltean 		if (trap->action.mask_mode == mask_mode &&
37999348004SVladimir Oltean 		    trap->action.port_mask == port_mask &&
38099348004SVladimir Oltean 		    trap->action.cpu_copy_ena == cpu_copy_ena)
38199348004SVladimir Oltean 			continue;
382c8c0ba4fSVladimir Oltean 
38399348004SVladimir Oltean 		trap->action.mask_mode = mask_mode;
38499348004SVladimir Oltean 		trap->action.port_mask = port_mask;
38599348004SVladimir Oltean 		trap->action.cpu_copy_ena = cpu_copy_ena;
3860a6f17c6SVladimir Oltean 
38799348004SVladimir Oltean 		err = ocelot_vcap_filter_replace(ocelot, trap);
388c8c0ba4fSVladimir Oltean 		if (err)
389c8c0ba4fSVladimir Oltean 			return err;
39099348004SVladimir Oltean 	}
391c8c0ba4fSVladimir Oltean 
39299348004SVladimir Oltean 	return 0;
393c8c0ba4fSVladimir Oltean }
394c8c0ba4fSVladimir Oltean 
395c69f40acSVladimir Oltean static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu)
396e21268efSVladimir Oltean {
397e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
3982960bb14SVladimir Oltean 	struct dsa_port *dp;
3992960bb14SVladimir Oltean 	int err;
400e21268efSVladimir Oltean 
401e21268efSVladimir Oltean 	felix_8021q_cpu_port_init(ocelot, cpu);
402e21268efSVladimir Oltean 
4032960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
404e21268efSVladimir Oltean 		/* This overwrites ocelot_init():
405e21268efSVladimir Oltean 		 * Do not forward BPDU frames to the CPU port module,
406e21268efSVladimir Oltean 		 * for 2 reasons:
407e21268efSVladimir Oltean 		 * - When these packets are injected from the tag_8021q
408e21268efSVladimir Oltean 		 *   CPU port, we want them to go out, not loop back
409e21268efSVladimir Oltean 		 *   into the system.
410e21268efSVladimir Oltean 		 * - STP traffic ingressing on a user port should go to
411e21268efSVladimir Oltean 		 *   the tag_8021q CPU port, not to the hardware CPU
412e21268efSVladimir Oltean 		 *   port module.
413e21268efSVladimir Oltean 		 */
414e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
415e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0),
4162960bb14SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG, dp->index);
417e21268efSVladimir Oltean 	}
418e21268efSVladimir Oltean 
4195da11eb4SVladimir Oltean 	err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD));
420e21268efSVladimir Oltean 	if (err)
421d7b1fd52SVladimir Oltean 		return err;
422d7b1fd52SVladimir Oltean 
42328de0f9fSVladimir Oltean 	err = ocelot_migrate_mdbs(ocelot, BIT(ocelot->num_phys_ports),
42428de0f9fSVladimir Oltean 				  BIT(cpu));
425f9cef64fSVladimir Oltean 	if (err)
426a51c1c3fSVladimir Oltean 		goto out_tag_8021q_unregister;
427b903a6bdSVladimir Oltean 
428b903a6bdSVladimir Oltean 	felix_migrate_flood_to_tag_8021q_port(ds, cpu);
429f9cef64fSVladimir Oltean 
430f9cef64fSVladimir Oltean 	err = felix_update_trapping_destinations(ds, true);
431f9cef64fSVladimir Oltean 	if (err)
432b903a6bdSVladimir Oltean 		goto out_migrate_flood;
433f9cef64fSVladimir Oltean 
43499348004SVladimir Oltean 	/* The ownership of the CPU port module's queues might have just been
43599348004SVladimir Oltean 	 * transferred to the tag_8021q tagger from the NPI-based tagger.
43699348004SVladimir Oltean 	 * So there might still be all sorts of crap in the queues. On the
43799348004SVladimir Oltean 	 * other hand, the MMIO-based matching of PTP frames is very brittle,
43899348004SVladimir Oltean 	 * so we need to be careful that there are no extra frames to be
43999348004SVladimir Oltean 	 * dequeued over MMIO, since we would never know to discard them.
44099348004SVladimir Oltean 	 */
44199348004SVladimir Oltean 	ocelot_drain_cpu_queue(ocelot, 0);
44299348004SVladimir Oltean 
443e21268efSVladimir Oltean 	return 0;
444e21268efSVladimir Oltean 
445b903a6bdSVladimir Oltean out_migrate_flood:
446b903a6bdSVladimir Oltean 	felix_migrate_flood_to_npi_port(ds, cpu);
44728de0f9fSVladimir Oltean 	ocelot_migrate_mdbs(ocelot, BIT(cpu), BIT(ocelot->num_phys_ports));
448d7b1fd52SVladimir Oltean out_tag_8021q_unregister:
449d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
450e21268efSVladimir Oltean 	return err;
451e21268efSVladimir Oltean }
452e21268efSVladimir Oltean 
453e21268efSVladimir Oltean static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu)
454e21268efSVladimir Oltean {
455e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
4562960bb14SVladimir Oltean 	struct dsa_port *dp;
4572960bb14SVladimir Oltean 	int err;
458e21268efSVladimir Oltean 
45999348004SVladimir Oltean 	err = felix_update_trapping_destinations(ds, false);
460c8c0ba4fSVladimir Oltean 	if (err)
461c8c0ba4fSVladimir Oltean 		dev_err(ds->dev, "felix_teardown_mmio_filtering returned %d",
462c8c0ba4fSVladimir Oltean 			err);
463c8c0ba4fSVladimir Oltean 
464d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
465e21268efSVladimir Oltean 
4662960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
467e21268efSVladimir Oltean 		/* Restore the logic from ocelot_init:
468e21268efSVladimir Oltean 		 * do not forward BPDU frames to the front ports.
469e21268efSVladimir Oltean 		 */
470e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
471e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
472e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG,
4732960bb14SVladimir Oltean 				 dp->index);
474e21268efSVladimir Oltean 	}
475e21268efSVladimir Oltean 
476e21268efSVladimir Oltean 	felix_8021q_cpu_port_deinit(ocelot, cpu);
477e21268efSVladimir Oltean }
478e21268efSVladimir Oltean 
479adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This
480adb3dccfSVladimir Oltean  * is the mode through which frames can be injected from and extracted to an
481adb3dccfSVladimir Oltean  * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU
482adb3dccfSVladimir Oltean  * running Linux, and this forms a DSA setup together with the enetc or fman
483adb3dccfSVladimir Oltean  * DSA master.
484adb3dccfSVladimir Oltean  */
485adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port)
486adb3dccfSVladimir Oltean {
487adb3dccfSVladimir Oltean 	ocelot->npi = port;
488adb3dccfSVladimir Oltean 
489adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
490adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
491adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
492adb3dccfSVladimir Oltean 
493adb3dccfSVladimir Oltean 	/* NPI port Injection/Extraction configuration */
494adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
495adb3dccfSVladimir Oltean 			    ocelot->npi_xtr_prefix);
496adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
497adb3dccfSVladimir Oltean 			    ocelot->npi_inj_prefix);
498adb3dccfSVladimir Oltean 
499adb3dccfSVladimir Oltean 	/* Disable transmission of pause frames */
500adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
501adb3dccfSVladimir Oltean }
502adb3dccfSVladimir Oltean 
503adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port)
504adb3dccfSVladimir Oltean {
505adb3dccfSVladimir Oltean 	/* Restore hardware defaults */
506adb3dccfSVladimir Oltean 	int unused_port = ocelot->num_phys_ports + 2;
507adb3dccfSVladimir Oltean 
508adb3dccfSVladimir Oltean 	ocelot->npi = -1;
509adb3dccfSVladimir Oltean 
510adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port),
511adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
512adb3dccfSVladimir Oltean 
513adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
514adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
515adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
516adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
517adb3dccfSVladimir Oltean 
518adb3dccfSVladimir Oltean 	/* Enable transmission of pause frames */
519adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
520adb3dccfSVladimir Oltean }
521adb3dccfSVladimir Oltean 
522c69f40acSVladimir Oltean static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu)
523adb3dccfSVladimir Oltean {
524adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
525f9cef64fSVladimir Oltean 	int err;
526f9cef64fSVladimir Oltean 
52728de0f9fSVladimir Oltean 	err = ocelot_migrate_mdbs(ocelot, BIT(cpu),
52828de0f9fSVladimir Oltean 				  BIT(ocelot->num_phys_ports));
529f9cef64fSVladimir Oltean 	if (err)
530a51c1c3fSVladimir Oltean 		return err;
531b903a6bdSVladimir Oltean 
532b903a6bdSVladimir Oltean 	felix_migrate_flood_to_npi_port(ds, cpu);
533adb3dccfSVladimir Oltean 
534adb3dccfSVladimir Oltean 	felix_npi_port_init(ocelot, cpu);
535adb3dccfSVladimir Oltean 
536adb3dccfSVladimir Oltean 	return 0;
537adb3dccfSVladimir Oltean }
538adb3dccfSVladimir Oltean 
539adb3dccfSVladimir Oltean static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu)
540adb3dccfSVladimir Oltean {
541adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
542adb3dccfSVladimir Oltean 
543adb3dccfSVladimir Oltean 	felix_npi_port_deinit(ocelot, cpu);
544adb3dccfSVladimir Oltean }
545adb3dccfSVladimir Oltean 
546adb3dccfSVladimir Oltean static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu,
547c69f40acSVladimir Oltean 				  enum dsa_tag_protocol proto)
548adb3dccfSVladimir Oltean {
549adb3dccfSVladimir Oltean 	int err;
550adb3dccfSVladimir Oltean 
551adb3dccfSVladimir Oltean 	switch (proto) {
5527c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
553adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
554c69f40acSVladimir Oltean 		err = felix_setup_tag_npi(ds, cpu);
555adb3dccfSVladimir Oltean 		break;
556e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
557c69f40acSVladimir Oltean 		err = felix_setup_tag_8021q(ds, cpu);
558e21268efSVladimir Oltean 		break;
559adb3dccfSVladimir Oltean 	default:
560adb3dccfSVladimir Oltean 		err = -EPROTONOSUPPORT;
561adb3dccfSVladimir Oltean 	}
562adb3dccfSVladimir Oltean 
563adb3dccfSVladimir Oltean 	return err;
564adb3dccfSVladimir Oltean }
565adb3dccfSVladimir Oltean 
566adb3dccfSVladimir Oltean static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu,
567adb3dccfSVladimir Oltean 				   enum dsa_tag_protocol proto)
568adb3dccfSVladimir Oltean {
569adb3dccfSVladimir Oltean 	switch (proto) {
5707c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
571adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
572adb3dccfSVladimir Oltean 		felix_teardown_tag_npi(ds, cpu);
573adb3dccfSVladimir Oltean 		break;
574e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
575e21268efSVladimir Oltean 		felix_teardown_tag_8021q(ds, cpu);
576e21268efSVladimir Oltean 		break;
577adb3dccfSVladimir Oltean 	default:
578adb3dccfSVladimir Oltean 		break;
579adb3dccfSVladimir Oltean 	}
580adb3dccfSVladimir Oltean }
581adb3dccfSVladimir Oltean 
582e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the
583e21268efSVladimir Oltean  * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
584e21268efSVladimir Oltean  * or the restoration is guaranteed to work.
585e21268efSVladimir Oltean  */
586bacf93b0SVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds,
587adb3dccfSVladimir Oltean 				     enum dsa_tag_protocol proto)
588adb3dccfSVladimir Oltean {
589adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
590adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
591adb3dccfSVladimir Oltean 	enum dsa_tag_protocol old_proto = felix->tag_proto;
592bacf93b0SVladimir Oltean 	struct dsa_port *cpu_dp;
593adb3dccfSVladimir Oltean 	int err;
594adb3dccfSVladimir Oltean 
5957c4bb540SVladimir Oltean 	if (proto != DSA_TAG_PROTO_SEVILLE &&
5967c4bb540SVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT &&
597e21268efSVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT_8021Q)
598adb3dccfSVladimir Oltean 		return -EPROTONOSUPPORT;
599adb3dccfSVladimir Oltean 
600bacf93b0SVladimir Oltean 	dsa_switch_for_each_cpu_port(cpu_dp, ds) {
601bacf93b0SVladimir Oltean 		felix_del_tag_protocol(ds, cpu_dp->index, old_proto);
60200fa91bcSVladimir Oltean 
603bacf93b0SVladimir Oltean 		err = felix_set_tag_protocol(ds, cpu_dp->index, proto);
604adb3dccfSVladimir Oltean 		if (err) {
605bacf93b0SVladimir Oltean 			felix_set_tag_protocol(ds, cpu_dp->index, old_proto);
606adb3dccfSVladimir Oltean 			return err;
607adb3dccfSVladimir Oltean 		}
608adb3dccfSVladimir Oltean 
609bacf93b0SVladimir Oltean 		/* Stop at first CPU port */
610bacf93b0SVladimir Oltean 		break;
611bacf93b0SVladimir Oltean 	}
612bacf93b0SVladimir Oltean 
613adb3dccfSVladimir Oltean 	felix->tag_proto = proto;
614adb3dccfSVladimir Oltean 
615adb3dccfSVladimir Oltean 	return 0;
616adb3dccfSVladimir Oltean }
617adb3dccfSVladimir Oltean 
61856051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
6194d776482SFlorian Fainelli 						    int port,
6204d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
62156051948SVladimir Oltean {
622adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
623adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
624adb3dccfSVladimir Oltean 
625adb3dccfSVladimir Oltean 	return felix->tag_proto;
62656051948SVladimir Oltean }
62756051948SVladimir Oltean 
62872c3b0c7SVladimir Oltean static void felix_port_set_host_flood(struct dsa_switch *ds, int port,
62972c3b0c7SVladimir Oltean 				      bool uc, bool mc)
63072c3b0c7SVladimir Oltean {
63172c3b0c7SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
63272c3b0c7SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
63372c3b0c7SVladimir Oltean 	unsigned long mask, val;
63472c3b0c7SVladimir Oltean 
63572c3b0c7SVladimir Oltean 	if (uc)
63672c3b0c7SVladimir Oltean 		felix->host_flood_uc_mask |= BIT(port);
63772c3b0c7SVladimir Oltean 	else
63872c3b0c7SVladimir Oltean 		felix->host_flood_uc_mask &= ~BIT(port);
63972c3b0c7SVladimir Oltean 
64072c3b0c7SVladimir Oltean 	if (mc)
64172c3b0c7SVladimir Oltean 		felix->host_flood_mc_mask |= BIT(port);
64272c3b0c7SVladimir Oltean 	else
64372c3b0c7SVladimir Oltean 		felix->host_flood_mc_mask &= ~BIT(port);
64472c3b0c7SVladimir Oltean 
64572c3b0c7SVladimir Oltean 	if (felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q)
64672c3b0c7SVladimir Oltean 		mask = dsa_cpu_ports(ds);
64772c3b0c7SVladimir Oltean 	else
64872c3b0c7SVladimir Oltean 		mask = BIT(ocelot->num_phys_ports);
64972c3b0c7SVladimir Oltean 
65072c3b0c7SVladimir Oltean 	val = (felix->host_flood_uc_mask) ? mask : 0;
65172c3b0c7SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_UC);
65272c3b0c7SVladimir Oltean 
65372c3b0c7SVladimir Oltean 	val = (felix->host_flood_mc_mask) ? mask : 0;
65472c3b0c7SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MC);
65572c3b0c7SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MCIPV4);
65672c3b0c7SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MCIPV6);
65772c3b0c7SVladimir Oltean }
65872c3b0c7SVladimir Oltean 
65956051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
66056051948SVladimir Oltean 				 unsigned int ageing_time)
66156051948SVladimir Oltean {
66256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
66356051948SVladimir Oltean 
66456051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
66556051948SVladimir Oltean 
66656051948SVladimir Oltean 	return 0;
66756051948SVladimir Oltean }
66856051948SVladimir Oltean 
6695cad43a5SVladimir Oltean static void felix_port_fast_age(struct dsa_switch *ds, int port)
6705cad43a5SVladimir Oltean {
6715cad43a5SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
6725cad43a5SVladimir Oltean 	int err;
6735cad43a5SVladimir Oltean 
6745cad43a5SVladimir Oltean 	err = ocelot_mact_flush(ocelot, port);
6755cad43a5SVladimir Oltean 	if (err)
6765cad43a5SVladimir Oltean 		dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n",
6775cad43a5SVladimir Oltean 			port, ERR_PTR(err));
6785cad43a5SVladimir Oltean }
6795cad43a5SVladimir Oltean 
68056051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
68156051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
68256051948SVladimir Oltean {
68356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
68456051948SVladimir Oltean 
68556051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
68656051948SVladimir Oltean }
68756051948SVladimir Oltean 
68856051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
689c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
690c2693363SVladimir Oltean 			 struct dsa_db db)
69156051948SVladimir Oltean {
69254c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
693e9b3ba43SVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
69456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
69556051948SVladimir Oltean 
69654c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
69754c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
69854c31984SVladimir Oltean 
699e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp) && !bridge_dev &&
7007e580490SVladimir Oltean 	    dsa_fdb_present_in_other_db(ds, port, addr, vid, db))
7017e580490SVladimir Oltean 		return 0;
7027e580490SVladimir Oltean 
703e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp))
704e9b3ba43SVladimir Oltean 		port = PGID_CPU;
705e9b3ba43SVladimir Oltean 
70654c31984SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid, bridge_dev);
70756051948SVladimir Oltean }
70856051948SVladimir Oltean 
70956051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
710c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
711c2693363SVladimir Oltean 			 struct dsa_db db)
71256051948SVladimir Oltean {
71354c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
714e9b3ba43SVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
71556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
71656051948SVladimir Oltean 
71754c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
71854c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
71954c31984SVladimir Oltean 
720e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp) && !bridge_dev &&
7217e580490SVladimir Oltean 	    dsa_fdb_present_in_other_db(ds, port, addr, vid, db))
7227e580490SVladimir Oltean 		return 0;
7237e580490SVladimir Oltean 
724e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp))
725e9b3ba43SVladimir Oltean 		port = PGID_CPU;
726e9b3ba43SVladimir Oltean 
72754c31984SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid, bridge_dev);
72856051948SVladimir Oltean }
72956051948SVladimir Oltean 
730961d8b69SVladimir Oltean static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag,
731c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
732c2693363SVladimir Oltean 			     struct dsa_db db)
733961d8b69SVladimir Oltean {
73454c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
735961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
736961d8b69SVladimir Oltean 
73754c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
73854c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
73954c31984SVladimir Oltean 
74054c31984SVladimir Oltean 	return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid, bridge_dev);
741961d8b69SVladimir Oltean }
742961d8b69SVladimir Oltean 
743961d8b69SVladimir Oltean static int felix_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag lag,
744c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
745c2693363SVladimir Oltean 			     struct dsa_db db)
746961d8b69SVladimir Oltean {
74754c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
748961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
749961d8b69SVladimir Oltean 
75054c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
75154c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
75254c31984SVladimir Oltean 
75354c31984SVladimir Oltean 	return ocelot_lag_fdb_del(ocelot, lag.dev, addr, vid, bridge_dev);
754961d8b69SVladimir Oltean }
755961d8b69SVladimir Oltean 
756a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port,
757c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
758c2693363SVladimir Oltean 			 struct dsa_db db)
759209edf95SVladimir Oltean {
76054c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
761209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
762209edf95SVladimir Oltean 
76354c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
76454c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
76554c31984SVladimir Oltean 
7667e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
7677e580490SVladimir Oltean 	    dsa_mdb_present_in_other_db(ds, port, mdb, db))
7687e580490SVladimir Oltean 		return 0;
7697e580490SVladimir Oltean 
7700ddf83cdSVladimir Oltean 	if (port == ocelot->npi)
7710ddf83cdSVladimir Oltean 		port = ocelot->num_phys_ports;
7720ddf83cdSVladimir Oltean 
77354c31984SVladimir Oltean 	return ocelot_port_mdb_add(ocelot, port, mdb, bridge_dev);
774209edf95SVladimir Oltean }
775209edf95SVladimir Oltean 
776209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port,
777c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
778c2693363SVladimir Oltean 			 struct dsa_db db)
779209edf95SVladimir Oltean {
78054c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
781209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
782209edf95SVladimir Oltean 
78354c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
78454c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
78554c31984SVladimir Oltean 
7867e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
7877e580490SVladimir Oltean 	    dsa_mdb_present_in_other_db(ds, port, mdb, db))
7887e580490SVladimir Oltean 		return 0;
7897e580490SVladimir Oltean 
7900ddf83cdSVladimir Oltean 	if (port == ocelot->npi)
7910ddf83cdSVladimir Oltean 		port = ocelot->num_phys_ports;
7920ddf83cdSVladimir Oltean 
79354c31984SVladimir Oltean 	return ocelot_port_mdb_del(ocelot, port, mdb, bridge_dev);
794209edf95SVladimir Oltean }
795209edf95SVladimir Oltean 
79656051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
79756051948SVladimir Oltean 				       u8 state)
79856051948SVladimir Oltean {
79956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
80056051948SVladimir Oltean 
80156051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
80256051948SVladimir Oltean }
80356051948SVladimir Oltean 
804421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port,
805421741eaSVladimir Oltean 				  struct switchdev_brport_flags val,
806421741eaSVladimir Oltean 				  struct netlink_ext_ack *extack)
807421741eaSVladimir Oltean {
808421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
809421741eaSVladimir Oltean 
810421741eaSVladimir Oltean 	return ocelot_port_pre_bridge_flags(ocelot, port, val);
811421741eaSVladimir Oltean }
812421741eaSVladimir Oltean 
813421741eaSVladimir Oltean static int felix_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 
819910ee6ccSVladimir Oltean 	if (port == ocelot->npi)
820910ee6ccSVladimir Oltean 		port = ocelot->num_phys_ports;
821910ee6ccSVladimir Oltean 
822421741eaSVladimir Oltean 	ocelot_port_bridge_flags(ocelot, port, val);
823421741eaSVladimir Oltean 
824421741eaSVladimir Oltean 	return 0;
825421741eaSVladimir Oltean }
826421741eaSVladimir Oltean 
82756051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
82806b9cce4SVladimir Oltean 			     struct dsa_bridge bridge, bool *tx_fwd_offload,
82906b9cce4SVladimir Oltean 			     struct netlink_ext_ack *extack)
83056051948SVladimir Oltean {
83156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
83256051948SVladimir Oltean 
83354c31984SVladimir Oltean 	return ocelot_port_bridge_join(ocelot, port, bridge.dev, bridge.num,
83454c31984SVladimir Oltean 				       extack);
83556051948SVladimir Oltean }
83656051948SVladimir Oltean 
83756051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
838d3eed0e5SVladimir Oltean 			       struct dsa_bridge bridge)
83956051948SVladimir Oltean {
84056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
84156051948SVladimir Oltean 
842d3eed0e5SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, bridge.dev);
84356051948SVladimir Oltean }
84456051948SVladimir Oltean 
8458fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port,
846dedd6a00SVladimir Oltean 			  struct dsa_lag lag,
8478fe6832eSVladimir Oltean 			  struct netdev_lag_upper_info *info)
8488fe6832eSVladimir Oltean {
8498fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8508fe6832eSVladimir Oltean 
851dedd6a00SVladimir Oltean 	return ocelot_port_lag_join(ocelot, port, lag.dev, info);
8528fe6832eSVladimir Oltean }
8538fe6832eSVladimir Oltean 
8548fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port,
855dedd6a00SVladimir Oltean 			   struct dsa_lag lag)
8568fe6832eSVladimir Oltean {
8578fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8588fe6832eSVladimir Oltean 
859dedd6a00SVladimir Oltean 	ocelot_port_lag_leave(ocelot, port, lag.dev);
8608fe6832eSVladimir Oltean 
8618fe6832eSVladimir Oltean 	return 0;
8628fe6832eSVladimir Oltean }
8638fe6832eSVladimir Oltean 
8648fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port)
8658fe6832eSVladimir Oltean {
8668fe6832eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
8678fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8688fe6832eSVladimir Oltean 
8698fe6832eSVladimir Oltean 	ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
8708fe6832eSVladimir Oltean 
8718fe6832eSVladimir Oltean 	return 0;
8728fe6832eSVladimir Oltean }
8738fe6832eSVladimir Oltean 
87456051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
87501af940eSVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan,
87601af940eSVladimir Oltean 			      struct netlink_ext_ack *extack)
87756051948SVladimir Oltean {
8782f0402feSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
879b7a9e0daSVladimir Oltean 	u16 flags = vlan->flags;
8802f0402feSVladimir Oltean 
8819a720680SVladimir Oltean 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
8829a720680SVladimir Oltean 	 * egress-untagged or not, pvid or not, make no difference. This
8839a720680SVladimir Oltean 	 * behavior is already better than what DSA just tries to approximate
8849a720680SVladimir Oltean 	 * when it installs the VLAN with the same flags on the CPU port.
8859a720680SVladimir Oltean 	 * Just accept any configuration, and don't let ocelot deny installing
8869a720680SVladimir Oltean 	 * multiple native VLANs on the NPI port, because the switch doesn't
8879a720680SVladimir Oltean 	 * look at the port tag settings towards the NPI interface anyway.
8889a720680SVladimir Oltean 	 */
8899a720680SVladimir Oltean 	if (port == ocelot->npi)
8909a720680SVladimir Oltean 		return 0;
8919a720680SVladimir Oltean 
892b7a9e0daSVladimir Oltean 	return ocelot_vlan_prepare(ocelot, port, vlan->vid,
8932f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_PVID,
89401af940eSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_UNTAGGED,
89501af940eSVladimir Oltean 				   extack);
89656051948SVladimir Oltean }
89756051948SVladimir Oltean 
89889153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
89989153ed6SVladimir Oltean 				struct netlink_ext_ack *extack)
90056051948SVladimir Oltean {
90156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
90256051948SVladimir Oltean 
9033b95d1b2SVladimir Oltean 	return ocelot_port_vlan_filtering(ocelot, port, enabled, extack);
90456051948SVladimir Oltean }
90556051948SVladimir Oltean 
9061958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port,
90731046a5fSVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan,
90831046a5fSVladimir Oltean 			  struct netlink_ext_ack *extack)
90956051948SVladimir Oltean {
91056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
911183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
9121958d581SVladimir Oltean 	int err;
91356051948SVladimir Oltean 
91401af940eSVladimir Oltean 	err = felix_vlan_prepare(ds, port, vlan, extack);
9151958d581SVladimir Oltean 	if (err)
9161958d581SVladimir Oltean 		return err;
9171958d581SVladimir Oltean 
9181958d581SVladimir Oltean 	return ocelot_vlan_add(ocelot, port, vlan->vid,
919183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_PVID,
920183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_UNTAGGED);
92156051948SVladimir Oltean }
92256051948SVladimir Oltean 
92356051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
92456051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
92556051948SVladimir Oltean {
92656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
92756051948SVladimir Oltean 
928b7a9e0daSVladimir Oltean 	return ocelot_vlan_del(ocelot, port, vlan->vid);
92956051948SVladimir Oltean }
93056051948SVladimir Oltean 
93179fda660SRussell King (Oracle) static void felix_phylink_get_caps(struct dsa_switch *ds, int port,
93279fda660SRussell King (Oracle) 				   struct phylink_config *config)
93379fda660SRussell King (Oracle) {
93479fda660SRussell King (Oracle) 	struct ocelot *ocelot = ds->priv;
93579fda660SRussell King (Oracle) 
936f6f04c02SRussell King (Oracle) 	/* This driver does not make use of the speed, duplex, pause or the
937f6f04c02SRussell King (Oracle) 	 * advertisement in its mac_config, so it is safe to mark this driver
938f6f04c02SRussell King (Oracle) 	 * as non-legacy.
939f6f04c02SRussell King (Oracle) 	 */
940f6f04c02SRussell King (Oracle) 	config->legacy_pre_march2020 = false;
941f6f04c02SRussell King (Oracle) 
94279fda660SRussell King (Oracle) 	__set_bit(ocelot->ports[port]->phy_mode,
94379fda660SRussell King (Oracle) 		  config->supported_interfaces);
94479fda660SRussell King (Oracle) }
94579fda660SRussell King (Oracle) 
946bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
947bdeced75SVladimir Oltean 				   unsigned long *supported,
948bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
949bdeced75SVladimir Oltean {
950bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
951375e1314SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
952bdeced75SVladimir Oltean 
953375e1314SVladimir Oltean 	if (felix->info->phylink_validate)
954375e1314SVladimir Oltean 		felix->info->phylink_validate(ocelot, port, supported, state);
955bdeced75SVladimir Oltean }
956bdeced75SVladimir Oltean 
957864ba485SRussell King (Oracle) static struct phylink_pcs *felix_phylink_mac_select_pcs(struct dsa_switch *ds,
958864ba485SRussell King (Oracle) 							int port,
959864ba485SRussell King (Oracle) 							phy_interface_t iface)
960bdeced75SVladimir Oltean {
961bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
962bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
963864ba485SRussell King (Oracle) 	struct phylink_pcs *pcs = NULL;
964bdeced75SVladimir Oltean 
96549af6a76SColin Foster 	if (felix->pcs && felix->pcs[port])
966864ba485SRussell King (Oracle) 		pcs = felix->pcs[port];
967864ba485SRussell King (Oracle) 
968864ba485SRussell King (Oracle) 	return pcs;
969bdeced75SVladimir Oltean }
970bdeced75SVladimir Oltean 
971bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
972bdeced75SVladimir Oltean 					unsigned int link_an_mode,
973bdeced75SVladimir Oltean 					phy_interface_t interface)
974bdeced75SVladimir Oltean {
975bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
976bdeced75SVladimir Oltean 
977e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
978e6e12df6SVladimir Oltean 				     FELIX_MAC_QUIRKS);
979bdeced75SVladimir Oltean }
980bdeced75SVladimir Oltean 
981bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
982bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
983bdeced75SVladimir Oltean 				      phy_interface_t interface,
9845b502a7bSRussell King 				      struct phy_device *phydev,
9855b502a7bSRussell King 				      int speed, int duplex,
9865b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
987bdeced75SVladimir Oltean {
988bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
9897e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
990bdeced75SVladimir Oltean 
991e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
992e6e12df6SVladimir Oltean 				   interface, speed, duplex, tx_pause, rx_pause,
993e6e12df6SVladimir Oltean 				   FELIX_MAC_QUIRKS);
9947e14a2dcSVladimir Oltean 
9957e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
9967e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
997bdeced75SVladimir Oltean }
998bdeced75SVladimir Oltean 
999bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
1000bd2b3161SXiaoliang Yang {
1001bd2b3161SXiaoliang Yang 	int i;
1002bd2b3161SXiaoliang Yang 
1003bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
1004bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1005bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1006bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
1007bd2b3161SXiaoliang Yang 		       port);
1008bd2b3161SXiaoliang Yang 
100970d39a6eSVladimir Oltean 	for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
1010bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
1011bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
1012bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
1013bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
1014bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
1015bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
1016bd2b3161SXiaoliang Yang 			      port, i);
1017bd2b3161SXiaoliang Yang 	}
1018bd2b3161SXiaoliang Yang }
1019bd2b3161SXiaoliang Yang 
102056051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
102156051948SVladimir Oltean 			      u32 stringset, u8 *data)
102256051948SVladimir Oltean {
102356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
102456051948SVladimir Oltean 
102556051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
102656051948SVladimir Oltean }
102756051948SVladimir Oltean 
102856051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
102956051948SVladimir Oltean {
103056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
103156051948SVladimir Oltean 
103256051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
103356051948SVladimir Oltean }
103456051948SVladimir Oltean 
103556051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
103656051948SVladimir Oltean {
103756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
103856051948SVladimir Oltean 
103956051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
104056051948SVladimir Oltean }
104156051948SVladimir Oltean 
104256051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
104356051948SVladimir Oltean 			     struct ethtool_ts_info *info)
104456051948SVladimir Oltean {
104556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
104656051948SVladimir Oltean 
104756051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
104856051948SVladimir Oltean }
104956051948SVladimir Oltean 
1050acf242fcSColin Foster static const u32 felix_phy_match_table[PHY_INTERFACE_MODE_MAX] = {
1051acf242fcSColin Foster 	[PHY_INTERFACE_MODE_INTERNAL] = OCELOT_PORT_MODE_INTERNAL,
1052acf242fcSColin Foster 	[PHY_INTERFACE_MODE_SGMII] = OCELOT_PORT_MODE_SGMII,
1053acf242fcSColin Foster 	[PHY_INTERFACE_MODE_QSGMII] = OCELOT_PORT_MODE_QSGMII,
1054acf242fcSColin Foster 	[PHY_INTERFACE_MODE_USXGMII] = OCELOT_PORT_MODE_USXGMII,
105511ecf341SVladimir Oltean 	[PHY_INTERFACE_MODE_1000BASEX] = OCELOT_PORT_MODE_1000BASEX,
1056acf242fcSColin Foster 	[PHY_INTERFACE_MODE_2500BASEX] = OCELOT_PORT_MODE_2500BASEX,
1057acf242fcSColin Foster };
1058acf242fcSColin Foster 
1059acf242fcSColin Foster static int felix_validate_phy_mode(struct felix *felix, int port,
1060acf242fcSColin Foster 				   phy_interface_t phy_mode)
1061acf242fcSColin Foster {
1062acf242fcSColin Foster 	u32 modes = felix->info->port_modes[port];
1063acf242fcSColin Foster 
1064acf242fcSColin Foster 	if (felix_phy_match_table[phy_mode] & modes)
1065acf242fcSColin Foster 		return 0;
1066acf242fcSColin Foster 	return -EOPNOTSUPP;
1067acf242fcSColin Foster }
1068acf242fcSColin Foster 
1069bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
1070bdeced75SVladimir Oltean 				  struct device_node *ports_node,
1071bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
1072bdeced75SVladimir Oltean {
1073bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1074bdeced75SVladimir Oltean 	struct device_node *child;
1075bdeced75SVladimir Oltean 
107637fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
1077bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
1078bdeced75SVladimir Oltean 		u32 port;
1079bdeced75SVladimir Oltean 		int err;
1080bdeced75SVladimir Oltean 
1081bdeced75SVladimir Oltean 		/* Get switch port number from DT */
1082bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
1083bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
1084bdeced75SVladimir Oltean 				"(property \"reg\")\n");
1085bdeced75SVladimir Oltean 			of_node_put(child);
1086bdeced75SVladimir Oltean 			return -ENODEV;
1087bdeced75SVladimir Oltean 		}
1088bdeced75SVladimir Oltean 
1089bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
1090bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
1091bdeced75SVladimir Oltean 		if (err) {
1092bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
1093bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
1094bdeced75SVladimir Oltean 				port);
1095bdeced75SVladimir Oltean 			of_node_put(child);
1096bdeced75SVladimir Oltean 			return -ENODEV;
1097bdeced75SVladimir Oltean 		}
1098bdeced75SVladimir Oltean 
1099acf242fcSColin Foster 		err = felix_validate_phy_mode(felix, port, phy_mode);
1100bdeced75SVladimir Oltean 		if (err < 0) {
1101bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
1102bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
110359ebb430SSumera Priyadarsini 			of_node_put(child);
1104bdeced75SVladimir Oltean 			return err;
1105bdeced75SVladimir Oltean 		}
1106bdeced75SVladimir Oltean 
1107bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
1108bdeced75SVladimir Oltean 	}
1109bdeced75SVladimir Oltean 
1110bdeced75SVladimir Oltean 	return 0;
1111bdeced75SVladimir Oltean }
1112bdeced75SVladimir Oltean 
1113bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
1114bdeced75SVladimir Oltean {
1115bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1116bdeced75SVladimir Oltean 	struct device_node *switch_node;
1117bdeced75SVladimir Oltean 	struct device_node *ports_node;
1118bdeced75SVladimir Oltean 	int err;
1119bdeced75SVladimir Oltean 
1120bdeced75SVladimir Oltean 	switch_node = dev->of_node;
1121bdeced75SVladimir Oltean 
1122bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
1123abecbfcdSVladimir Oltean 	if (!ports_node)
1124abecbfcdSVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
1125bdeced75SVladimir Oltean 	if (!ports_node) {
1126abecbfcdSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n");
1127bdeced75SVladimir Oltean 		return -ENODEV;
1128bdeced75SVladimir Oltean 	}
1129bdeced75SVladimir Oltean 
1130bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
1131bdeced75SVladimir Oltean 	of_node_put(ports_node);
1132bdeced75SVladimir Oltean 
1133bdeced75SVladimir Oltean 	return err;
1134bdeced75SVladimir Oltean }
1135bdeced75SVladimir Oltean 
113656051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
113756051948SVladimir Oltean {
113856051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
1139bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
1140b4024c9eSClaudiu Manoil 	struct resource res;
114156051948SVladimir Oltean 	int port, i, err;
114256051948SVladimir Oltean 
114356051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
114456051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
114556051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
114656051948SVladimir Oltean 	if (!ocelot->ports)
114756051948SVladimir Oltean 		return -ENOMEM;
114856051948SVladimir Oltean 
114956051948SVladimir Oltean 	ocelot->map		= felix->info->map;
115056051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
115121ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
115207d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
115377043c37SXiaoliang Yang 	ocelot->vcap_pol.base	= felix->info->vcap_pol_base;
115477043c37SXiaoliang Yang 	ocelot->vcap_pol.max	= felix->info->vcap_pol_max;
115577043c37SXiaoliang Yang 	ocelot->vcap_pol.base2	= felix->info->vcap_pol_base2;
115677043c37SXiaoliang Yang 	ocelot->vcap_pol.max2	= felix->info->vcap_pol_max2;
115756051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
1158cacea62fSVladimir Oltean 	ocelot->npi_inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
1159cacea62fSVladimir Oltean 	ocelot->npi_xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
1160f59fd9caSVladimir Oltean 	ocelot->devlink		= felix->ds->devlink;
116156051948SVladimir Oltean 
1162bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
1163bdeced75SVladimir Oltean 				 GFP_KERNEL);
1164bdeced75SVladimir Oltean 	if (!port_phy_modes)
1165bdeced75SVladimir Oltean 		return -ENOMEM;
1166bdeced75SVladimir Oltean 
1167bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
1168bdeced75SVladimir Oltean 	if (err) {
1169bdeced75SVladimir Oltean 		kfree(port_phy_modes);
1170bdeced75SVladimir Oltean 		return err;
1171bdeced75SVladimir Oltean 	}
1172bdeced75SVladimir Oltean 
117356051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
117456051948SVladimir Oltean 		struct regmap *target;
117556051948SVladimir Oltean 
117656051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
117756051948SVladimir Oltean 			continue;
117856051948SVladimir Oltean 
1179b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
1180b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1181375e1314SVladimir Oltean 		res.start += felix->switch_base;
1182375e1314SVladimir Oltean 		res.end += felix->switch_base;
118356051948SVladimir Oltean 
1184242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
118556051948SVladimir Oltean 		if (IS_ERR(target)) {
118656051948SVladimir Oltean 			dev_err(ocelot->dev,
118756051948SVladimir Oltean 				"Failed to map device memory space\n");
1188bdeced75SVladimir Oltean 			kfree(port_phy_modes);
118956051948SVladimir Oltean 			return PTR_ERR(target);
119056051948SVladimir Oltean 		}
119156051948SVladimir Oltean 
119256051948SVladimir Oltean 		ocelot->targets[i] = target;
119356051948SVladimir Oltean 	}
119456051948SVladimir Oltean 
119556051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
119656051948SVladimir Oltean 	if (err) {
119756051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
1198bdeced75SVladimir Oltean 		kfree(port_phy_modes);
119956051948SVladimir Oltean 		return err;
120056051948SVladimir Oltean 	}
120156051948SVladimir Oltean 
120256051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
120356051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
120491c724cfSVladimir Oltean 		struct regmap *target;
120556051948SVladimir Oltean 
120656051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
120756051948SVladimir Oltean 					   sizeof(struct ocelot_port),
120856051948SVladimir Oltean 					   GFP_KERNEL);
120956051948SVladimir Oltean 		if (!ocelot_port) {
121056051948SVladimir Oltean 			dev_err(ocelot->dev,
121156051948SVladimir Oltean 				"failed to allocate port memory\n");
1212bdeced75SVladimir Oltean 			kfree(port_phy_modes);
121356051948SVladimir Oltean 			return -ENOMEM;
121456051948SVladimir Oltean 		}
121556051948SVladimir Oltean 
1216b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
1217b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1218375e1314SVladimir Oltean 		res.start += felix->switch_base;
1219375e1314SVladimir Oltean 		res.end += felix->switch_base;
122056051948SVladimir Oltean 
1221242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
122291c724cfSVladimir Oltean 		if (IS_ERR(target)) {
122356051948SVladimir Oltean 			dev_err(ocelot->dev,
122491c724cfSVladimir Oltean 				"Failed to map memory space for port %d\n",
122591c724cfSVladimir Oltean 				port);
1226bdeced75SVladimir Oltean 			kfree(port_phy_modes);
122791c724cfSVladimir Oltean 			return PTR_ERR(target);
122856051948SVladimir Oltean 		}
122956051948SVladimir Oltean 
1230bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
123156051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
123291c724cfSVladimir Oltean 		ocelot_port->target = target;
123356051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
123456051948SVladimir Oltean 	}
123556051948SVladimir Oltean 
1236bdeced75SVladimir Oltean 	kfree(port_phy_modes);
1237bdeced75SVladimir Oltean 
1238bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
1239bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
1240bdeced75SVladimir Oltean 		if (err < 0)
1241bdeced75SVladimir Oltean 			return err;
1242bdeced75SVladimir Oltean 	}
1243bdeced75SVladimir Oltean 
124456051948SVladimir Oltean 	return 0;
124556051948SVladimir Oltean }
124656051948SVladimir Oltean 
12471328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port,
12481328a883SVladimir Oltean 					   struct sk_buff *skb)
12491328a883SVladimir Oltean {
12501328a883SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
12511328a883SVladimir Oltean 	struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
12521328a883SVladimir Oltean 	struct sk_buff *skb_match = NULL, *skb_tmp;
12531328a883SVladimir Oltean 	unsigned long flags;
12541328a883SVladimir Oltean 
12551328a883SVladimir Oltean 	if (!clone)
12561328a883SVladimir Oltean 		return;
12571328a883SVladimir Oltean 
12581328a883SVladimir Oltean 	spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags);
12591328a883SVladimir Oltean 
12601328a883SVladimir Oltean 	skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) {
12611328a883SVladimir Oltean 		if (skb != clone)
12621328a883SVladimir Oltean 			continue;
12631328a883SVladimir Oltean 		__skb_unlink(skb, &ocelot_port->tx_skbs);
12641328a883SVladimir Oltean 		skb_match = skb;
12651328a883SVladimir Oltean 		break;
12661328a883SVladimir Oltean 	}
12671328a883SVladimir Oltean 
12681328a883SVladimir Oltean 	spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags);
12691328a883SVladimir Oltean 
12701328a883SVladimir Oltean 	WARN_ONCE(!skb_match,
12711328a883SVladimir Oltean 		  "Could not find skb clone in TX timestamping list\n");
12721328a883SVladimir Oltean }
12731328a883SVladimir Oltean 
127449f885b2SVladimir Oltean #define work_to_xmit_work(w) \
127549f885b2SVladimir Oltean 		container_of((w), struct felix_deferred_xmit_work, work)
127649f885b2SVladimir Oltean 
127749f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work)
127849f885b2SVladimir Oltean {
127949f885b2SVladimir Oltean 	struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
128049f885b2SVladimir Oltean 	struct dsa_switch *ds = xmit_work->dp->ds;
128149f885b2SVladimir Oltean 	struct sk_buff *skb = xmit_work->skb;
128249f885b2SVladimir Oltean 	u32 rew_op = ocelot_ptp_rew_op(skb);
128349f885b2SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
128449f885b2SVladimir Oltean 	int port = xmit_work->dp->index;
128549f885b2SVladimir Oltean 	int retries = 10;
128649f885b2SVladimir Oltean 
128749f885b2SVladimir Oltean 	do {
128849f885b2SVladimir Oltean 		if (ocelot_can_inject(ocelot, 0))
128949f885b2SVladimir Oltean 			break;
129049f885b2SVladimir Oltean 
129149f885b2SVladimir Oltean 		cpu_relax();
129249f885b2SVladimir Oltean 	} while (--retries);
129349f885b2SVladimir Oltean 
129449f885b2SVladimir Oltean 	if (!retries) {
129549f885b2SVladimir Oltean 		dev_err(ocelot->dev, "port %d failed to inject skb\n",
129649f885b2SVladimir Oltean 			port);
12971328a883SVladimir Oltean 		ocelot_port_purge_txtstamp_skb(ocelot, port, skb);
129849f885b2SVladimir Oltean 		kfree_skb(skb);
129949f885b2SVladimir Oltean 		return;
130049f885b2SVladimir Oltean 	}
130149f885b2SVladimir Oltean 
130249f885b2SVladimir Oltean 	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
130349f885b2SVladimir Oltean 
130449f885b2SVladimir Oltean 	consume_skb(skb);
130549f885b2SVladimir Oltean 	kfree(xmit_work);
130649f885b2SVladimir Oltean }
130749f885b2SVladimir Oltean 
130835d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds,
130935d97680SVladimir Oltean 				      enum dsa_tag_protocol proto)
131049f885b2SVladimir Oltean {
131135d97680SVladimir Oltean 	struct ocelot_8021q_tagger_data *tagger_data;
131249f885b2SVladimir Oltean 
131335d97680SVladimir Oltean 	switch (proto) {
131435d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
131535d97680SVladimir Oltean 		tagger_data = ocelot_8021q_tagger_data(ds);
131635d97680SVladimir Oltean 		tagger_data->xmit_work_fn = felix_port_deferred_xmit;
131749f885b2SVladimir Oltean 		return 0;
131835d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
131935d97680SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
132049f885b2SVladimir Oltean 		return 0;
132135d97680SVladimir Oltean 	default:
132235d97680SVladimir Oltean 		return -EPROTONOSUPPORT;
132349f885b2SVladimir Oltean 	}
132449f885b2SVladimir Oltean }
132549f885b2SVladimir Oltean 
132656051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
132756051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
132856051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
132956051948SVladimir Oltean  * (which will not work).
133056051948SVladimir Oltean  */
133156051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
133256051948SVladimir Oltean {
133356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
133456051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1335f2e2662cSVladimir Oltean 	unsigned long cpu_flood;
13362960bb14SVladimir Oltean 	struct dsa_port *dp;
13372960bb14SVladimir Oltean 	int err;
133856051948SVladimir Oltean 
133956051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
134056051948SVladimir Oltean 	if (err)
134156051948SVladimir Oltean 		return err;
134256051948SVladimir Oltean 
1343d1cc0e93SVladimir Oltean 	err = ocelot_init(ocelot);
1344d1cc0e93SVladimir Oltean 	if (err)
13456b73b7c9SVladimir Oltean 		goto out_mdiobus_free;
1346d1cc0e93SVladimir Oltean 
13472b49d128SYangbo Lu 	if (ocelot->ptp) {
13482ac7c6c5SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
13492b49d128SYangbo Lu 		if (err) {
13502b49d128SYangbo Lu 			dev_err(ocelot->dev,
13512b49d128SYangbo Lu 				"Timestamp initialization failed\n");
13522b49d128SYangbo Lu 			ocelot->ptp = 0;
13532b49d128SYangbo Lu 		}
13542b49d128SYangbo Lu 	}
135556051948SVladimir Oltean 
13562960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
13572960bb14SVladimir Oltean 		ocelot_init_port(ocelot, dp->index);
1358bd2b3161SXiaoliang Yang 
1359bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
1360bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
1361bd2b3161SXiaoliang Yang 		 */
13622960bb14SVladimir Oltean 		felix_port_qos_map_init(ocelot, dp->index);
136356051948SVladimir Oltean 	}
136456051948SVladimir Oltean 
1365f59fd9caSVladimir Oltean 	err = ocelot_devlink_sb_register(ocelot);
1366f59fd9caSVladimir Oltean 	if (err)
13676b73b7c9SVladimir Oltean 		goto out_deinit_ports;
1368f59fd9caSVladimir Oltean 
13692960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
1370adb3dccfSVladimir Oltean 		/* The initial tag protocol is NPI which always returns 0, so
1371adb3dccfSVladimir Oltean 		 * there's no real point in checking for errors.
13721cf3299bSVladimir Oltean 		 */
1373c69f40acSVladimir Oltean 		felix_set_tag_protocol(ds, dp->index, felix->tag_proto);
1374f2e2662cSVladimir Oltean 
1375f2e2662cSVladimir Oltean 		/* Start off with flooding disabled towards the NPI port
1376f2e2662cSVladimir Oltean 		 * (actually CPU port module).
1377f2e2662cSVladimir Oltean 		 */
1378f2e2662cSVladimir Oltean 		cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
1379f2e2662cSVladimir Oltean 		ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_UC);
1380f2e2662cSVladimir Oltean 		ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_MC);
1381f2e2662cSVladimir Oltean 
13828d5f7954SVladimir Oltean 		break;
1383adb3dccfSVladimir Oltean 	}
13841cf3299bSVladimir Oltean 
13850b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
1386c54913c1SVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
138754c31984SVladimir Oltean 	ds->fdb_isolation = true;
138854c31984SVladimir Oltean 	ds->max_num_bridges = ds->num_ports;
1389bdeced75SVladimir Oltean 
139056051948SVladimir Oltean 	return 0;
13916b73b7c9SVladimir Oltean 
13926b73b7c9SVladimir Oltean out_deinit_ports:
13932960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
13942960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
13956b73b7c9SVladimir Oltean 
13966b73b7c9SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
13976b73b7c9SVladimir Oltean 	ocelot_deinit(ocelot);
13986b73b7c9SVladimir Oltean 
13996b73b7c9SVladimir Oltean out_mdiobus_free:
14006b73b7c9SVladimir Oltean 	if (felix->info->mdio_bus_free)
14016b73b7c9SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
14026b73b7c9SVladimir Oltean 
14036b73b7c9SVladimir Oltean 	return err;
140456051948SVladimir Oltean }
140556051948SVladimir Oltean 
140656051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
140756051948SVladimir Oltean {
140856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1409bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
14102960bb14SVladimir Oltean 	struct dsa_port *dp;
1411bdeced75SVladimir Oltean 
14122960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
14132960bb14SVladimir Oltean 		felix_del_tag_protocol(ds, dp->index, felix->tag_proto);
14148d5f7954SVladimir Oltean 		break;
1415adb3dccfSVladimir Oltean 	}
1416adb3dccfSVladimir Oltean 
14172960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
14182960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
1419d19741b0SVladimir Oltean 
142049f885b2SVladimir Oltean 	ocelot_devlink_sb_unregister(ocelot);
142149f885b2SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
142249f885b2SVladimir Oltean 	ocelot_deinit(ocelot);
142349f885b2SVladimir Oltean 
1424d19741b0SVladimir Oltean 	if (felix->info->mdio_bus_free)
1425d19741b0SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
142656051948SVladimir Oltean }
142756051948SVladimir Oltean 
1428c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
1429c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1430c0bcf537SYangbo Lu {
1431c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1432c0bcf537SYangbo Lu 
1433c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
1434c0bcf537SYangbo Lu }
1435c0bcf537SYangbo Lu 
1436c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
1437c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1438c0bcf537SYangbo Lu {
1439c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
144099348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
144199348004SVladimir Oltean 	bool using_tag_8021q;
144299348004SVladimir Oltean 	int err;
1443c0bcf537SYangbo Lu 
144499348004SVladimir Oltean 	err = ocelot_hwstamp_set(ocelot, port, ifr);
144599348004SVladimir Oltean 	if (err)
144699348004SVladimir Oltean 		return err;
144799348004SVladimir Oltean 
144899348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
144999348004SVladimir Oltean 
145099348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
1451c0bcf537SYangbo Lu }
1452c0bcf537SYangbo Lu 
1453d219b4b6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot)
14540a6f17c6SVladimir Oltean {
14550a6f17c6SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1456dbd03285SVladimir Oltean 	int err = 0, grp = 0;
14570a6f17c6SVladimir Oltean 
14580a6f17c6SVladimir Oltean 	if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q)
14590a6f17c6SVladimir Oltean 		return false;
14600a6f17c6SVladimir Oltean 
14610a6f17c6SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
14620a6f17c6SVladimir Oltean 		return false;
14630a6f17c6SVladimir Oltean 
14640a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
14650a6f17c6SVladimir Oltean 		struct sk_buff *skb;
14660a6f17c6SVladimir Oltean 		unsigned int type;
14670a6f17c6SVladimir Oltean 
14680a6f17c6SVladimir Oltean 		err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
14690a6f17c6SVladimir Oltean 		if (err)
14700a6f17c6SVladimir Oltean 			goto out;
14710a6f17c6SVladimir Oltean 
14720a6f17c6SVladimir Oltean 		/* We trap to the CPU port module all PTP frames, but
14730a6f17c6SVladimir Oltean 		 * felix_rxtstamp() only gets called for event frames.
14740a6f17c6SVladimir Oltean 		 * So we need to avoid sending duplicate general
14750a6f17c6SVladimir Oltean 		 * message frames by running a second BPF classifier
14760a6f17c6SVladimir Oltean 		 * here and dropping those.
14770a6f17c6SVladimir Oltean 		 */
14780a6f17c6SVladimir Oltean 		__skb_push(skb, ETH_HLEN);
14790a6f17c6SVladimir Oltean 
14800a6f17c6SVladimir Oltean 		type = ptp_classify_raw(skb);
14810a6f17c6SVladimir Oltean 
14820a6f17c6SVladimir Oltean 		__skb_pull(skb, ETH_HLEN);
14830a6f17c6SVladimir Oltean 
14840a6f17c6SVladimir Oltean 		if (type == PTP_CLASS_NONE) {
14850a6f17c6SVladimir Oltean 			kfree_skb(skb);
14860a6f17c6SVladimir Oltean 			continue;
14870a6f17c6SVladimir Oltean 		}
14880a6f17c6SVladimir Oltean 
14890a6f17c6SVladimir Oltean 		netif_rx(skb);
14900a6f17c6SVladimir Oltean 	}
14910a6f17c6SVladimir Oltean 
14920a6f17c6SVladimir Oltean out:
14935d3bb7ddSVladimir Oltean 	if (err < 0) {
14945d3bb7ddSVladimir Oltean 		dev_err_ratelimited(ocelot->dev,
14955d3bb7ddSVladimir Oltean 				    "Error during packet extraction: %pe\n",
14965d3bb7ddSVladimir Oltean 				    ERR_PTR(err));
14970a6f17c6SVladimir Oltean 		ocelot_drain_cpu_queue(ocelot, 0);
14985d3bb7ddSVladimir Oltean 	}
14990a6f17c6SVladimir Oltean 
15000a6f17c6SVladimir Oltean 	return true;
15010a6f17c6SVladimir Oltean }
15020a6f17c6SVladimir Oltean 
1503c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
1504c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
1505c0bcf537SYangbo Lu {
150692f62485SVladimir Oltean 	u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo;
1507c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
1508c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1509c0bcf537SYangbo Lu 	struct timespec64 ts;
151092f62485SVladimir Oltean 	u32 tstamp_hi;
151192f62485SVladimir Oltean 	u64 tstamp;
1512c0bcf537SYangbo Lu 
15130a6f17c6SVladimir Oltean 	/* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb
15140a6f17c6SVladimir Oltean 	 * for RX timestamping. Then free it, and poll for its copy through
15150a6f17c6SVladimir Oltean 	 * MMIO in the CPU port module, and inject that into the stack from
15160a6f17c6SVladimir Oltean 	 * ocelot_xtr_poll().
15170a6f17c6SVladimir Oltean 	 */
1518d219b4b6SVladimir Oltean 	if (felix_check_xtr_pkt(ocelot)) {
15190a6f17c6SVladimir Oltean 		kfree_skb(skb);
15200a6f17c6SVladimir Oltean 		return true;
15210a6f17c6SVladimir Oltean 	}
15220a6f17c6SVladimir Oltean 
1523c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1524c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1525c0bcf537SYangbo Lu 
1526c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
1527c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
1528c0bcf537SYangbo Lu 		tstamp_hi--;
1529c0bcf537SYangbo Lu 
1530c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
1531c0bcf537SYangbo Lu 
1532c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
1533c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1534c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
1535c0bcf537SYangbo Lu 	return false;
1536c0bcf537SYangbo Lu }
1537c0bcf537SYangbo Lu 
15385c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port,
15395c5416f5SYangbo Lu 			   struct sk_buff *skb)
1540c0bcf537SYangbo Lu {
1541c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1542682eaad9SYangbo Lu 	struct sk_buff *clone = NULL;
1543c0bcf537SYangbo Lu 
1544682eaad9SYangbo Lu 	if (!ocelot->ptp)
15455c5416f5SYangbo Lu 		return;
1546c0bcf537SYangbo Lu 
154752849bcfSVladimir Oltean 	if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
154852849bcfSVladimir Oltean 		dev_err_ratelimited(ds->dev,
154952849bcfSVladimir Oltean 				    "port %d delivering skb without TX timestamp\n",
155052849bcfSVladimir Oltean 				    port);
1551682eaad9SYangbo Lu 		return;
155252849bcfSVladimir Oltean 	}
1553682eaad9SYangbo Lu 
1554682eaad9SYangbo Lu 	if (clone)
1555c4b364ceSYangbo Lu 		OCELOT_SKB_CB(skb)->clone = clone;
15565c5416f5SYangbo Lu }
1557c0bcf537SYangbo Lu 
15580b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
15590b912fc9SVladimir Oltean {
15600b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15610b912fc9SVladimir Oltean 
15620b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
15630b912fc9SVladimir Oltean 
15640b912fc9SVladimir Oltean 	return 0;
15650b912fc9SVladimir Oltean }
15660b912fc9SVladimir Oltean 
15670b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
15680b912fc9SVladimir Oltean {
15690b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15700b912fc9SVladimir Oltean 
15710b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
15720b912fc9SVladimir Oltean }
15730b912fc9SVladimir Oltean 
157407d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
157507d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
157607d985eeSVladimir Oltean {
157707d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
157899348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
157999348004SVladimir Oltean 	bool using_tag_8021q;
158099348004SVladimir Oltean 	int err;
158107d985eeSVladimir Oltean 
158299348004SVladimir Oltean 	err = ocelot_cls_flower_replace(ocelot, port, cls, ingress);
158399348004SVladimir Oltean 	if (err)
158499348004SVladimir Oltean 		return err;
158599348004SVladimir Oltean 
158699348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
158799348004SVladimir Oltean 
158899348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
158907d985eeSVladimir Oltean }
159007d985eeSVladimir Oltean 
159107d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
159207d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
159307d985eeSVladimir Oltean {
159407d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
159507d985eeSVladimir Oltean 
159607d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
159707d985eeSVladimir Oltean }
159807d985eeSVladimir Oltean 
159907d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
160007d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
160107d985eeSVladimir Oltean {
160207d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
160307d985eeSVladimir Oltean 
160407d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
160507d985eeSVladimir Oltean }
160607d985eeSVladimir Oltean 
1607fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
1608fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
1609fc411eaaSVladimir Oltean {
1610fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1611fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
1612fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
16135f035af7SPo Liu 		.burst = policer->burst,
1614fc411eaaSVladimir Oltean 	};
1615fc411eaaSVladimir Oltean 
1616fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
1617fc411eaaSVladimir Oltean }
1618fc411eaaSVladimir Oltean 
1619fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
1620fc411eaaSVladimir Oltean {
1621fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1622fc411eaaSVladimir Oltean 
1623fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
1624fc411eaaSVladimir Oltean }
1625fc411eaaSVladimir Oltean 
16265e497497SVladimir Oltean static int felix_port_mirror_add(struct dsa_switch *ds, int port,
16275e497497SVladimir Oltean 				 struct dsa_mall_mirror_tc_entry *mirror,
16285e497497SVladimir Oltean 				 bool ingress, struct netlink_ext_ack *extack)
16295e497497SVladimir Oltean {
16305e497497SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
16315e497497SVladimir Oltean 
16325e497497SVladimir Oltean 	return ocelot_port_mirror_add(ocelot, port, mirror->to_local_port,
16335e497497SVladimir Oltean 				      ingress, extack);
16345e497497SVladimir Oltean }
16355e497497SVladimir Oltean 
16365e497497SVladimir Oltean static void felix_port_mirror_del(struct dsa_switch *ds, int port,
16375e497497SVladimir Oltean 				  struct dsa_mall_mirror_tc_entry *mirror)
16385e497497SVladimir Oltean {
16395e497497SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
16405e497497SVladimir Oltean 
16415e497497SVladimir Oltean 	ocelot_port_mirror_del(ocelot, port, mirror->ingress);
16425e497497SVladimir Oltean }
16435e497497SVladimir Oltean 
1644de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
1645de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
1646de143c0eSXiaoliang Yang 			       void *type_data)
1647de143c0eSXiaoliang Yang {
1648de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
1649de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
1650de143c0eSXiaoliang Yang 
1651de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
1652de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
1653de143c0eSXiaoliang Yang 	else
1654de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
1655de143c0eSXiaoliang Yang }
1656de143c0eSXiaoliang Yang 
1657f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
1658f59fd9caSVladimir Oltean 			     u16 pool_index,
1659f59fd9caSVladimir Oltean 			     struct devlink_sb_pool_info *pool_info)
1660f59fd9caSVladimir Oltean {
1661f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1662f59fd9caSVladimir Oltean 
1663f59fd9caSVladimir Oltean 	return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
1664f59fd9caSVladimir Oltean }
1665f59fd9caSVladimir Oltean 
1666f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
1667f59fd9caSVladimir Oltean 			     u16 pool_index, u32 size,
1668f59fd9caSVladimir Oltean 			     enum devlink_sb_threshold_type threshold_type,
1669f59fd9caSVladimir Oltean 			     struct netlink_ext_ack *extack)
1670f59fd9caSVladimir Oltean {
1671f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1672f59fd9caSVladimir Oltean 
1673f59fd9caSVladimir Oltean 	return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
1674f59fd9caSVladimir Oltean 				  threshold_type, extack);
1675f59fd9caSVladimir Oltean }
1676f59fd9caSVladimir Oltean 
1677f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
1678f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1679f59fd9caSVladimir Oltean 				  u32 *p_threshold)
1680f59fd9caSVladimir Oltean {
1681f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1682f59fd9caSVladimir Oltean 
1683f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
1684f59fd9caSVladimir Oltean 				       p_threshold);
1685f59fd9caSVladimir Oltean }
1686f59fd9caSVladimir Oltean 
1687f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
1688f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1689f59fd9caSVladimir Oltean 				  u32 threshold, struct netlink_ext_ack *extack)
1690f59fd9caSVladimir Oltean {
1691f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1692f59fd9caSVladimir Oltean 
1693f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
1694f59fd9caSVladimir Oltean 				       threshold, extack);
1695f59fd9caSVladimir Oltean }
1696f59fd9caSVladimir Oltean 
1697f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
1698f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1699f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1700f59fd9caSVladimir Oltean 				     u16 *p_pool_index, u32 *p_threshold)
1701f59fd9caSVladimir Oltean {
1702f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1703f59fd9caSVladimir Oltean 
1704f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
1705f59fd9caSVladimir Oltean 					  pool_type, p_pool_index,
1706f59fd9caSVladimir Oltean 					  p_threshold);
1707f59fd9caSVladimir Oltean }
1708f59fd9caSVladimir Oltean 
1709f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
1710f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1711f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1712f59fd9caSVladimir Oltean 				     u16 pool_index, u32 threshold,
1713f59fd9caSVladimir Oltean 				     struct netlink_ext_ack *extack)
1714f59fd9caSVladimir Oltean {
1715f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1716f59fd9caSVladimir Oltean 
1717f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
1718f59fd9caSVladimir Oltean 					  pool_type, pool_index, threshold,
1719f59fd9caSVladimir Oltean 					  extack);
1720f59fd9caSVladimir Oltean }
1721f59fd9caSVladimir Oltean 
1722f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds,
1723f59fd9caSVladimir Oltean 				 unsigned int sb_index)
1724f59fd9caSVladimir Oltean {
1725f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1726f59fd9caSVladimir Oltean 
1727f59fd9caSVladimir Oltean 	return ocelot_sb_occ_snapshot(ocelot, sb_index);
1728f59fd9caSVladimir Oltean }
1729f59fd9caSVladimir Oltean 
1730f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds,
1731f59fd9caSVladimir Oltean 				  unsigned int sb_index)
1732f59fd9caSVladimir Oltean {
1733f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1734f59fd9caSVladimir Oltean 
1735f59fd9caSVladimir Oltean 	return ocelot_sb_occ_max_clear(ocelot, sb_index);
1736f59fd9caSVladimir Oltean }
1737f59fd9caSVladimir Oltean 
1738f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
1739f59fd9caSVladimir Oltean 				      unsigned int sb_index, u16 pool_index,
1740f59fd9caSVladimir Oltean 				      u32 *p_cur, u32 *p_max)
1741f59fd9caSVladimir Oltean {
1742f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1743f59fd9caSVladimir Oltean 
1744f59fd9caSVladimir Oltean 	return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
1745f59fd9caSVladimir Oltean 					   p_cur, p_max);
1746f59fd9caSVladimir Oltean }
1747f59fd9caSVladimir Oltean 
1748f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
1749f59fd9caSVladimir Oltean 					 unsigned int sb_index, u16 tc_index,
1750f59fd9caSVladimir Oltean 					 enum devlink_sb_pool_type pool_type,
1751f59fd9caSVladimir Oltean 					 u32 *p_cur, u32 *p_max)
1752f59fd9caSVladimir Oltean {
1753f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1754f59fd9caSVladimir Oltean 
1755f59fd9caSVladimir Oltean 	return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
1756f59fd9caSVladimir Oltean 					      pool_type, p_cur, p_max);
1757f59fd9caSVladimir Oltean }
1758f59fd9caSVladimir Oltean 
1759a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port,
1760a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1761a026c50bSHoratiu Vultur {
1762a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1763a026c50bSHoratiu Vultur 
1764a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1765a026c50bSHoratiu Vultur }
1766a026c50bSHoratiu Vultur 
1767a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port,
1768a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1769a026c50bSHoratiu Vultur {
1770a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1771a026c50bSHoratiu Vultur 
1772a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1773a026c50bSHoratiu Vultur }
1774a026c50bSHoratiu Vultur 
1775a026c50bSHoratiu Vultur static int
1776a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port,
1777a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1778a026c50bSHoratiu Vultur {
1779a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1780a026c50bSHoratiu Vultur 
1781a026c50bSHoratiu Vultur 	return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1782a026c50bSHoratiu Vultur }
1783a026c50bSHoratiu Vultur 
1784a026c50bSHoratiu Vultur static int
1785a026c50bSHoratiu Vultur felix_mrp_del_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_del_ring_role(ocelot, port, mrp);
1791a026c50bSHoratiu Vultur }
1792a026c50bSHoratiu Vultur 
1793978777d0SVladimir Oltean static int felix_port_get_default_prio(struct dsa_switch *ds, int port)
1794978777d0SVladimir Oltean {
1795978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1796978777d0SVladimir Oltean 
1797978777d0SVladimir Oltean 	return ocelot_port_get_default_prio(ocelot, port);
1798978777d0SVladimir Oltean }
1799978777d0SVladimir Oltean 
1800978777d0SVladimir Oltean static int felix_port_set_default_prio(struct dsa_switch *ds, int port,
1801978777d0SVladimir Oltean 				       u8 prio)
1802978777d0SVladimir Oltean {
1803978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1804978777d0SVladimir Oltean 
1805978777d0SVladimir Oltean 	return ocelot_port_set_default_prio(ocelot, port, prio);
1806978777d0SVladimir Oltean }
1807978777d0SVladimir Oltean 
1808978777d0SVladimir Oltean static int felix_port_get_dscp_prio(struct dsa_switch *ds, int port, u8 dscp)
1809978777d0SVladimir Oltean {
1810978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1811978777d0SVladimir Oltean 
1812978777d0SVladimir Oltean 	return ocelot_port_get_dscp_prio(ocelot, port, dscp);
1813978777d0SVladimir Oltean }
1814978777d0SVladimir Oltean 
1815978777d0SVladimir Oltean static int felix_port_add_dscp_prio(struct dsa_switch *ds, int port, u8 dscp,
1816978777d0SVladimir Oltean 				    u8 prio)
1817978777d0SVladimir Oltean {
1818978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1819978777d0SVladimir Oltean 
1820978777d0SVladimir Oltean 	return ocelot_port_add_dscp_prio(ocelot, port, dscp, prio);
1821978777d0SVladimir Oltean }
1822978777d0SVladimir Oltean 
1823978777d0SVladimir Oltean static int felix_port_del_dscp_prio(struct dsa_switch *ds, int port, u8 dscp,
1824978777d0SVladimir Oltean 				    u8 prio)
1825978777d0SVladimir Oltean {
1826978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1827978777d0SVladimir Oltean 
1828978777d0SVladimir Oltean 	return ocelot_port_del_dscp_prio(ocelot, port, dscp, prio);
1829978777d0SVladimir Oltean }
1830978777d0SVladimir Oltean 
1831375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = {
183256051948SVladimir Oltean 	.get_tag_protocol		= felix_get_tag_protocol,
1833adb3dccfSVladimir Oltean 	.change_tag_protocol		= felix_change_tag_protocol,
183435d97680SVladimir Oltean 	.connect_tag_protocol		= felix_connect_tag_protocol,
183556051948SVladimir Oltean 	.setup				= felix_setup,
183656051948SVladimir Oltean 	.teardown			= felix_teardown,
183756051948SVladimir Oltean 	.set_ageing_time		= felix_set_ageing_time,
183856051948SVladimir Oltean 	.get_strings			= felix_get_strings,
183956051948SVladimir Oltean 	.get_ethtool_stats		= felix_get_ethtool_stats,
184056051948SVladimir Oltean 	.get_sset_count			= felix_get_sset_count,
184156051948SVladimir Oltean 	.get_ts_info			= felix_get_ts_info,
184279fda660SRussell King (Oracle) 	.phylink_get_caps		= felix_phylink_get_caps,
1843bdeced75SVladimir Oltean 	.phylink_validate		= felix_phylink_validate,
1844864ba485SRussell King (Oracle) 	.phylink_mac_select_pcs		= felix_phylink_mac_select_pcs,
1845bdeced75SVladimir Oltean 	.phylink_mac_link_down		= felix_phylink_mac_link_down,
1846bdeced75SVladimir Oltean 	.phylink_mac_link_up		= felix_phylink_mac_link_up,
18475cad43a5SVladimir Oltean 	.port_fast_age			= felix_port_fast_age,
184856051948SVladimir Oltean 	.port_fdb_dump			= felix_fdb_dump,
184956051948SVladimir Oltean 	.port_fdb_add			= felix_fdb_add,
185056051948SVladimir Oltean 	.port_fdb_del			= felix_fdb_del,
1851961d8b69SVladimir Oltean 	.lag_fdb_add			= felix_lag_fdb_add,
1852961d8b69SVladimir Oltean 	.lag_fdb_del			= felix_lag_fdb_del,
1853209edf95SVladimir Oltean 	.port_mdb_add			= felix_mdb_add,
1854209edf95SVladimir Oltean 	.port_mdb_del			= felix_mdb_del,
1855421741eaSVladimir Oltean 	.port_pre_bridge_flags		= felix_pre_bridge_flags,
1856421741eaSVladimir Oltean 	.port_bridge_flags		= felix_bridge_flags,
185756051948SVladimir Oltean 	.port_bridge_join		= felix_bridge_join,
185856051948SVladimir Oltean 	.port_bridge_leave		= felix_bridge_leave,
18598fe6832eSVladimir Oltean 	.port_lag_join			= felix_lag_join,
18608fe6832eSVladimir Oltean 	.port_lag_leave			= felix_lag_leave,
18618fe6832eSVladimir Oltean 	.port_lag_change		= felix_lag_change,
186256051948SVladimir Oltean 	.port_stp_state_set		= felix_bridge_stp_state_set,
186356051948SVladimir Oltean 	.port_vlan_filtering		= felix_vlan_filtering,
186456051948SVladimir Oltean 	.port_vlan_add			= felix_vlan_add,
186556051948SVladimir Oltean 	.port_vlan_del			= felix_vlan_del,
1866c0bcf537SYangbo Lu 	.port_hwtstamp_get		= felix_hwtstamp_get,
1867c0bcf537SYangbo Lu 	.port_hwtstamp_set		= felix_hwtstamp_set,
1868c0bcf537SYangbo Lu 	.port_rxtstamp			= felix_rxtstamp,
1869c0bcf537SYangbo Lu 	.port_txtstamp			= felix_txtstamp,
18700b912fc9SVladimir Oltean 	.port_change_mtu		= felix_change_mtu,
18710b912fc9SVladimir Oltean 	.port_max_mtu			= felix_get_max_mtu,
1872fc411eaaSVladimir Oltean 	.port_policer_add		= felix_port_policer_add,
1873fc411eaaSVladimir Oltean 	.port_policer_del		= felix_port_policer_del,
18745e497497SVladimir Oltean 	.port_mirror_add		= felix_port_mirror_add,
18755e497497SVladimir Oltean 	.port_mirror_del		= felix_port_mirror_del,
187607d985eeSVladimir Oltean 	.cls_flower_add			= felix_cls_flower_add,
187707d985eeSVladimir Oltean 	.cls_flower_del			= felix_cls_flower_del,
187807d985eeSVladimir Oltean 	.cls_flower_stats		= felix_cls_flower_stats,
1879de143c0eSXiaoliang Yang 	.port_setup_tc			= felix_port_setup_tc,
1880f59fd9caSVladimir Oltean 	.devlink_sb_pool_get		= felix_sb_pool_get,
1881f59fd9caSVladimir Oltean 	.devlink_sb_pool_set		= felix_sb_pool_set,
1882f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_get	= felix_sb_port_pool_get,
1883f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_set	= felix_sb_port_pool_set,
1884f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_get	= felix_sb_tc_pool_bind_get,
1885f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_set	= felix_sb_tc_pool_bind_set,
1886f59fd9caSVladimir Oltean 	.devlink_sb_occ_snapshot	= felix_sb_occ_snapshot,
1887f59fd9caSVladimir Oltean 	.devlink_sb_occ_max_clear	= felix_sb_occ_max_clear,
1888f59fd9caSVladimir Oltean 	.devlink_sb_occ_port_pool_get	= felix_sb_occ_port_pool_get,
1889f59fd9caSVladimir Oltean 	.devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
1890a026c50bSHoratiu Vultur 	.port_mrp_add			= felix_mrp_add,
1891a026c50bSHoratiu Vultur 	.port_mrp_del			= felix_mrp_del,
1892a026c50bSHoratiu Vultur 	.port_mrp_add_ring_role		= felix_mrp_add_ring_role,
1893a026c50bSHoratiu Vultur 	.port_mrp_del_ring_role		= felix_mrp_del_ring_role,
18945da11eb4SVladimir Oltean 	.tag_8021q_vlan_add		= felix_tag_8021q_vlan_add,
18955da11eb4SVladimir Oltean 	.tag_8021q_vlan_del		= felix_tag_8021q_vlan_del,
1896978777d0SVladimir Oltean 	.port_get_default_prio		= felix_port_get_default_prio,
1897978777d0SVladimir Oltean 	.port_set_default_prio		= felix_port_set_default_prio,
1898978777d0SVladimir Oltean 	.port_get_dscp_prio		= felix_port_get_dscp_prio,
1899978777d0SVladimir Oltean 	.port_add_dscp_prio		= felix_port_add_dscp_prio,
1900978777d0SVladimir Oltean 	.port_del_dscp_prio		= felix_port_del_dscp_prio,
190172c3b0c7SVladimir Oltean 	.port_set_host_flood		= felix_port_set_host_flood,
190256051948SVladimir Oltean };
1903319e4dd1SVladimir Oltean 
1904319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
1905319e4dd1SVladimir Oltean {
1906319e4dd1SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1907319e4dd1SVladimir Oltean 	struct dsa_switch *ds = felix->ds;
1908319e4dd1SVladimir Oltean 
1909319e4dd1SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
1910319e4dd1SVladimir Oltean 		return NULL;
1911319e4dd1SVladimir Oltean 
1912319e4dd1SVladimir Oltean 	return dsa_to_port(ds, port)->slave;
1913319e4dd1SVladimir Oltean }
1914319e4dd1SVladimir Oltean 
1915319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev)
1916319e4dd1SVladimir Oltean {
1917319e4dd1SVladimir Oltean 	struct dsa_port *dp;
1918319e4dd1SVladimir Oltean 
1919319e4dd1SVladimir Oltean 	dp = dsa_port_from_netdev(dev);
1920319e4dd1SVladimir Oltean 	if (IS_ERR(dp))
1921319e4dd1SVladimir Oltean 		return -EINVAL;
1922319e4dd1SVladimir Oltean 
1923319e4dd1SVladimir Oltean 	return dp->index;
1924319e4dd1SVladimir Oltean }
1925